Skip to contentSkip to conversation

x86/amd: do not expose HWCR.TscFreqSel to guests

PATCH Superseded Roger Pau Monne 3 participants 9 messages Sep 13, 2023 – Sep 14, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/amd: do not expose HWCR.TscFreqSel to guests

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

Fixes 14b95b3b8546 ('x86/AMD: expose HWCR.TscFreqSel to guests')
---
 xen/arch/x86/msr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/msr.c +1 -2
index 3f0450259cdf..c33dc78cd8f6 100644
@@ -240,8 +240,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
     case MSR_K8_HWCR:
         if ( !(cp->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) )
             goto gp_fault;
-        *val = get_cpu_family(cp->basic.raw_fms, NULL, NULL) >= 0x10
-               ? K8_HWCR_TSC_FREQ_SEL : 0;
+        *val = 0;
         break;
 
     case MSR_VIRT_SPEC_CTRL:
Signature · 1 line
2.42.0
#1 lore raw
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

Quoted 4 lines
Fixes 14b95b3b8546 ('x86/AMD: expose HWCR.TscFreqSel to guests')

As mentioned before, with this Fixes: tag I think the description absolutely needs to mention the (changed) view on the Linux log message that had triggered the original change. It certainly helps here that Thomas has already signaled agreement to a Linux side change.

Jan

#2 lore raw
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 8 lines
Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Quoted 9 lines
Quoted 4 lines
Fixes 14b95b3b8546 ('x86/AMD: expose HWCR.TscFreqSel to guests')

As mentioned before, with this Fixes: tag I think the description absolutely needs to mention the (changed) view on the Linux log message that had triggered the original change. It certainly helps here that Thomas has already signaled agreement to a Linux side change.

Sure, what about:

"The motivation for exposing HWCR.TscFreqSel was to avoid warning messages from Linux. It has been agreed that Linux should be changed instead to not complaint about missing HWCR.TscFreqSel when running virtualized."

Thanks, Roger.

#3 lore raw
Quoted 20 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
Quoted 17 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

Quoted 6 lines
Quoted 2 lines

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Hmm, yes, that's one way to look at things. Yet exposing plain zero is pretty clearly not within spec here, and any such quirks we add generally want justifying imo (as they might bite us again later).

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
Fixes 14b95b3b8546 ('x86/AMD: expose HWCR.TscFreqSel to guests')

As mentioned before, with this Fixes: tag I think the description absolutely needs to mention the (changed) view on the Linux log message that had triggered the original change. It certainly helps here that Thomas has already signaled agreement to a Linux side change.

Quoted 6 lines

Sure, what about:

"The motivation for exposing HWCR.TscFreqSel was to avoid warning messages from Linux. It has been agreed that Linux should be changed instead to not complaint about missing HWCR.TscFreqSel when running virtualized."

Reads okay to me, thanks.

Jan

#4 lore raw
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

Quoted 6 lines
Quoted 3 lines

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Hmm, yes, that's one way to look at things. Yet exposing plain zero is pretty clearly not within spec here,

As I understand it, the fact that HWCR.TscFreqSel is read-only doesn't exclude the possibility of it changing using other means (iow: we should consider that a write to a different register could have the side effect of toggling the bit).

The PPR I'm reading doesn't mention that the bit must be 1, just that it's 1 on reset and read-only.

Thanks, Roger.

#5 lore raw
Quoted 32 lines · Roger Pau Monné
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

Quoted 6 lines
Quoted 3 lines

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Hmm, yes, that's one way to look at things. Yet exposing plain zero is pretty clearly not within spec here,

Quoted 7 lines

As I understand it, the fact that HWCR.TscFreqSel is read-only doesn't exclude the possibility of it changing using other means (iow: we should consider that a write to a different register could have the side effect of toggling the bit).

The PPR I'm reading doesn't mention that the bit must be 1, just that it's 1 on reset and read-only.

Sure; the PPR being incomplete doesn't help here. My interpretation, based on the bit having been r/w in earlier families, is that AMD wanted to retain its meaning without allowing it to be configurable anymore. Possibly a sign of it remaining so going forward.

Jan

#6 lore raw
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

Quoted 6 lines
Quoted 3 lines

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Hmm, yes, that's one way to look at things. Yet exposing plain zero is pretty clearly not within spec here,

As I understand it, the fact that HWCR.TscFreqSel is read-only doesn't exclude the possibility of it changing using other means (iow: we should consider that a write to a different register could have the side effect of toggling the bit).

Quoted 7 lines
Quoted 2 lines

The PPR I'm reading doesn't mention that the bit must be 1, just that it's 1 on reset and read-only.

Sure; the PPR being incomplete doesn't help here. My interpretation, based on the bit having been r/w in earlier families, is that AMD wanted to retain its meaning without allowing it to be configurable anymore. Possibly a sign of it remaining so going forward.

What about:

"Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models. Since HWCR contains both architectural and non-architectural bits, going forward care must be taken to assert the exposed value is correct on newer CPU families."

Thanks, Roger.

#7 lore raw
Quoted 47 lines · Roger Pau Monné
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

Quoted 5 lines

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

This imo wants (or even needs) extending to address the aspect of then exposing, on newer families, a r/o bit with the wrong value.

Quoted 6 lines
Quoted 3 lines

We could always be exposing bits with the wrong values on newer (unreleased?) families, I'm not sure why it needs explicit mentioning here.

Hmm, yes, that's one way to look at things. Yet exposing plain zero is pretty clearly not within spec here,

As I understand it, the fact that HWCR.TscFreqSel is read-only doesn't exclude the possibility of it changing using other means (iow: we should consider that a write to a different register could have the side effect of toggling the bit).

Quoted 7 lines
Quoted 2 lines

The PPR I'm reading doesn't mention that the bit must be 1, just that it's 1 on reset and read-only.

Sure; the PPR being incomplete doesn't help here. My interpretation, based on the bit having been r/w in earlier families, is that AMD wanted to retain its meaning without allowing it to be configurable anymore. Possibly a sign of it remaining so going forward.

Quoted 7 lines

What about:

"Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models. Since HWCR contains both architectural and non-architectural bits, going forward care must be taken to assert the exposed value is correct on newer CPU families."

Fine with me.

Jan

#8 lore raw
In reply to Roger Pau Monne
Quoted 7 lines · Roger Pau Monne

OpenBSD 7.3 will unconditionally access HWCR if the TSC is reported as Invariant, and it will then attempt to also unconditionally access PSTATE0 if HWCR.TscFreqSel is set (currently the case on Xen).

The relation between HWCR.TscFreqSel and PSTATE0 is not clearly written down in the PPR, but it's natural for OSes to attempt to fetch the P0 frequency if the TSC increments at the P0 frequency.

"TSC is stated to increment at ..."  would be slightly clearer IMO.

Quoted 10 lines

Exposing PSTATEn (PSTATE0 at least) with all zeroes is not a suitable solution because the PstateEn bit is read-write, and OSes could legitimately attempt to set PstateEn=1 which Xen couldn't handle.

In order to fix expose an empty HWCR, which is an architectural MSR and so must be accessible.

Note it was not safe to expose the TscFreqSel bit because it is not architectural, and could change meaning between models.

I'd suggest rearranging and adjusting these two paragraphs.

---
Furthermore, the TscFreqSel bit is model specific and was never safe to expose like this in the first place.  At a minimum it should have had a toolstack adjustment to know not to migrate such a VM.

Therefore, simply remove the bit.  Note the MSR_HWCR itself is an architectural register, and does need to be accessible by the guest.
---

Quoted 5 lines
Fixes 14b95b3b8546 ('x86/AMD: expose HWCR.TscFreqSel to guests')

For the change itself, Reviewed-by: Andrew Cooper
<[email protected]>