Skip to contentSkip to conversation

x86/amd: Avoid cpu_has_hypervisor evaluating true on native hardware

PATCH In review Andrew Cooper 3 participants 6 messages Feb 11, 2020 – Feb 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/amd: Avoid cpu_has_hypervisor evaluating true on native hardware

Currently when booting native on AMD hardware, cpuidmask_defaults._1cd gets configured with the HYPERVISOR bit before native CPUID is scanned for feature bits.

This results in cpu_has_hypervisor becoming set as part of identify_cpu(), and ends up appearing in the raw and host CPU policies.

A combination of this bug, and c/s bb502a8ca59 "x86: check feature flags after resume" which checks that feature bits don't go missing, results in broken S3 on AMD hardware.

Alter amd_init_levelling() to exclude the HYPERVISOR bit from cpumask_defaults, and update domain_cpu_policy_changed() to allow it to be explicitly forwarded.

This also fixes a bug on kexec, where the hypervisor bit is left enabled for the new kernel to find.

These changes highlight a further but - dom0 construction is asymetric with domU construction, by not having any calls to domain_cpu_policy_changed(). Extend arch_domain_create() to always call domain_cpu_policy_changed().

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Igor Druzhinin <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Claudia <[email protected]>

v2:
* Rewrite the commit message. No change to the patch content.

Marek/Claudia: Do either of you want a Reported-by tag seeing as you found a brand new way that this was broken?

---
 xen/arch/x86/cpu/amd.c       | 3 ---
 xen/arch/x86/domain.c        | 2 ++
 xen/arch/x86/domctl.c        | 9 ++++++++-
 xen/include/asm-x86/domain.h | 2 ++
 4 files changed, 12 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/amd.c +0 -3
index e351dd227f..f95a8e0fd3 100644
@@ -298,9 +298,6 @@ static void __init noinline amd_init_levelling(void)
 			ecx |= cpufeat_mask(X86_FEATURE_OSXSAVE);
 		edx |= cpufeat_mask(X86_FEATURE_APIC);
 
-		/* Allow the HYPERVISOR bit to be set via guest policy. */
-		ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);
-
 		cpuidmask_defaults._1cd = ((uint64_t)ecx << 32) | edx;
 	}
 
xen/arch/x86/domain.c +2 -0
index f53ae5ff86..12bd554391 100644
@@ -656,6 +656,8 @@ int arch_domain_create(struct domain *d,
      */
     d->arch.x87_fip_width = cpu_has_fpu_sel ? 0 : 8;
 
+    domain_cpu_policy_changed(d);
+
     return 0;
 
  fail:
xen/arch/x86/domctl.c +8 -1
index 4fa9c91140..ce76d6d776 100644
@@ -48,7 +48,7 @@ static int gdbsx_guest_mem_io(domid_t domid, struct xen_domctl_gdbsx_memio *iop)
 }
 #endif
 
-static void domain_cpu_policy_changed(struct domain *d)
+void domain_cpu_policy_changed(struct domain *d)
 {
     const struct cpuid_policy *p = d->arch.cpuid;
     struct vcpu *v;
@@ -106,6 +106,13 @@ static void domain_cpu_policy_changed(struct domain *d)
                     ecx = 0;
                 edx = cpufeat_mask(X86_FEATURE_APIC);
 
+                /*
+                 * If the Hypervisor bit is set in the policy, we can also
+                 * forward it into real CPUID.
+                 */
+                if ( p->basic.hypervisor )
+                    ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);
+
                 mask |= ((uint64_t)ecx << 32) | edx;
                 break;
             }
xen/include/asm-x86/domain.h +2 -0
index f0c25ffec0..1843c76d1a 100644
@@ -624,6 +624,8 @@ struct guest_memory_policy
 void update_guest_memory_policy(struct vcpu *v,
                                 struct guest_memory_policy *policy);
 
+void domain_cpu_policy_changed(struct domain *d);
+
 bool update_runstate_area(struct vcpu *);
 bool update_secondary_system_time(struct vcpu *,
                                   struct vcpu_time_info *);
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 85 lines · Andrew Cooper

Currently when booting native on AMD hardware, cpuidmask_defaults._1cd gets configured with the HYPERVISOR bit before native CPUID is scanned for feature bits.

This results in cpu_has_hypervisor becoming set as part of identify_cpu(), and ends up appearing in the raw and host CPU policies.

A combination of this bug, and c/s bb502a8ca59 "x86: check feature flags after resume" which checks that feature bits don't go missing, results in broken S3 on AMD hardware.

Alter amd_init_levelling() to exclude the HYPERVISOR bit from cpumask_defaults, and update domain_cpu_policy_changed() to allow it to be explicitly forwarded.

This also fixes a bug on kexec, where the hypervisor bit is left enabled for the new kernel to find.

These changes highlight a further but - dom0 construction is asymetric with domU construction, by not having any calls to domain_cpu_policy_changed(). Extend arch_domain_create() to always call domain_cpu_policy_changed().

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Igor Druzhinin <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Claudia <[email protected]>

v2:
* Rewrite the commit message. No change to the patch content.

Marek/Claudia: Do either of you want a Reported-by tag seeing as you found a brand new way that this was broken?

---
 xen/arch/x86/cpu/amd.c       | 3 ---
 xen/arch/x86/domain.c        | 2 ++
 xen/arch/x86/domctl.c        | 9 ++++++++-
 xen/include/asm-x86/domain.h | 2 ++
 4 files changed, 12 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/amd.c +0 -3
index e351dd227f..f95a8e0fd3 100644
@@ -298,9 +298,6 @@ static void __init noinline amd_init_levelling(void)
 			ecx |= cpufeat_mask(X86_FEATURE_OSXSAVE);
 		edx |= cpufeat_mask(X86_FEATURE_APIC);
 
-		/* Allow the HYPERVISOR bit to be set via guest policy. */
-		ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);
-
 		cpuidmask_defaults._1cd = ((uint64_t)ecx << 32) | edx;
 	}
 
xen/arch/x86/domain.c +2 -0
index f53ae5ff86..12bd554391 100644
@@ -656,6 +656,8 @@ int arch_domain_create(struct domain *d,
      */
     d->arch.x87_fip_width = cpu_has_fpu_sel ? 0 : 8;
 
+    domain_cpu_policy_changed(d);
+
     return 0;
 
  fail:
xen/arch/x86/domctl.c +1 -1
index 4fa9c91140..ce76d6d776 100644
@@ -48,7 +48,7 @@ static int gdbsx_guest_mem_io(domid_t domid, struct xen_domctl_gdbsx_memio *iop)
 }
 #endif
 
-static void domain_cpu_policy_changed(struct domain *d)
+void domain_cpu_policy_changed(struct domain *d)
 {
     const struct cpuid_policy *p = d->arch.cpuid;
     struct vcpu *v;

xen/arch/x86/domctl.c static void domain_cpu_policy_changed(struct domain *d) · line 114

Quoted 9 lines
xen/arch/x86/domctl.c +6 -0
@@ -106,6 +106,13 @@ static void domain_cpu_policy_changed(struct domain *d)
                     ecx = 0;
                 edx = cpufeat_mask(X86_FEATURE_APIC);
 
+                /*
+                 * If the Hypervisor bit is set in the policy, we can also
+                 * forward it into real CPUID.
+                 */
+                if ( p->basic.hypervisor )
+                    ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);

AFAICT dom0 will also get the hypervisor bit set by default, as that's part of both the HVM and the PV max policy?

If so:

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 18 lines · Roger Pau Monné
Quoted 17 lines · Andrew Cooper
xen/arch/x86/domctl.c +1 -1
index 4fa9c91140..ce76d6d776 100644
@@ -48,7 +48,7 @@ static int gdbsx_guest_mem_io(domid_t domid, struct xen_domctl_gdbsx_memio *iop)
 }
 #endif
 
-static void domain_cpu_policy_changed(struct domain *d)
+void domain_cpu_policy_changed(struct domain *d)
 {
     const struct cpuid_policy *p = d->arch.cpuid;
     struct vcpu *v;
@@ -106,6 +106,13 @@ static void domain_cpu_policy_changed(struct domain *d)
                     ecx = 0;
                 edx = cpufeat_mask(X86_FEATURE_APIC);
 
Quoted 8 lines

xen/arch/x86/domctl.c static void domain_cpu_policy_changed(struct domain *d) · line 114

Quoted 6 lines
xen/arch/x86/domctl.c +6 -0
+                /*
+                 * If the Hypervisor bit is set in the policy, we can also
+                 * forward it into real CPUID.
+                 */
+                if ( p->basic.hypervisor )
+                    ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);

AFAICT dom0 will also get the hypervisor bit set by default, as that's part of both the HVM and the PV max policy?

Correct.

Quoted 4 lines

If so:

Thanks.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#3 lore raw
In reply to Roger Pau Monné
Quoted 33 lines · Roger Pau Monné
Quoted 32 lines · Andrew Cooper

Currently when booting native on AMD hardware, cpuidmask_defaults._1cd gets configured with the HYPERVISOR bit before native CPUID is scanned for feature bits.

This results in cpu_has_hypervisor becoming set as part of identify_cpu(), and ends up appearing in the raw and host CPU policies.

A combination of this bug, and c/s bb502a8ca59 "x86: check feature flags after resume" which checks that feature bits don't go missing, results in broken S3 on AMD hardware.

Alter amd_init_levelling() to exclude the HYPERVISOR bit from cpumask_defaults, and update domain_cpu_policy_changed() to allow it to be explicitly forwarded.

This also fixes a bug on kexec, where the hypervisor bit is left enabled for the new kernel to find.

These changes highlight a further but - dom0 construction is asymetric with domU construction, by not having any calls to domain_cpu_policy_changed(). Extend arch_domain_create() to always call domain_cpu_policy_changed().

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Igor Druzhinin <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Claudia <[email protected]>

Quoted 5 lines
Quoted 5 lines

v2:
* Rewrite the commit message. No change to the patch content.

Marek/Claudia: Do either of you want a Reported-by tag seeing as you found a brand new way that this was broken?

I understand this is addressing only one half of their issue. Since you said you don't find it surprising, do you have any idea why the OSXSAVE bit is behaving differently on AMD and on Intel?

Quoted 14 lines

xen/arch/x86/domctl.c static void domain_cpu_policy_changed(struct domain *d) · line 114

Quoted 10 lines
xen/arch/x86/domctl.c +6 -0
@@ -106,6 +106,13 @@ static void domain_cpu_policy_changed(struct domain *d)
                     ecx = 0;
                 edx = cpufeat_mask(X86_FEATURE_APIC);
 
+                /*
+                 * If the Hypervisor bit is set in the policy, we can also
+                 * forward it into real CPUID.
+                 */
+                if ( p->basic.hypervisor )
+                    ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);

AFAICT dom0 will also get the hypervisor bit set by default, as that's part of both the HVM and the PV max policy?

Quoted 3 lines

If so:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Roger Pau Monné
Quoted 32 lines · Andrew Cooper

Currently when booting native on AMD hardware, cpuidmask_defaults._1cd gets configured with the HYPERVISOR bit before native CPUID is scanned for feature bits.

This results in cpu_has_hypervisor becoming set as part of identify_cpu(), and ends up appearing in the raw and host CPU policies.

A combination of this bug, and c/s bb502a8ca59 "x86: check feature flags after resume" which checks that feature bits don't go missing, results in broken S3 on AMD hardware.

Alter amd_init_levelling() to exclude the HYPERVISOR bit from cpumask_defaults, and update domain_cpu_policy_changed() to allow it to be explicitly forwarded.

This also fixes a bug on kexec, where the hypervisor bit is left enabled for the new kernel to find.

These changes highlight a further but - dom0 construction is asymetric with domU construction, by not having any calls to domain_cpu_policy_changed(). Extend arch_domain_create() to always call domain_cpu_policy_changed().

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Igor Druzhinin <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Claudia <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

v2:
* Rewrite the commit message. No change to the patch content.

Marek/Claudia: Do either of you want a Reported-by tag seeing as you found a brand new way that this was broken?

I understand this is addressing only one half of their issue. Since you said you don't find it surprising, do you have any idea why the OSXSAVE bit is behaving differently on AMD and on Intel?

It isn't behaving differently between Intel and AMD, I don't think.

The diagnostics are asymmetric - they ever printed when a feature disappears, not for a feature appearing.

OSXSAVE is clear until fairly late on boot (therefore misses being cached), but is restored as part of mmu_cr4_features (before the feature check).

The only reason anything gets printed in the first place is because the HYPERVISOR bit disappeared.

Overall, OSXSAVE (and in principle OSPKE if we start supporting it in PV guests) are benign.  We could filter them out of the diagnostics but don't currently have a suitable featuremask, and I'm not sure adding one is worth it.

Quoted 14 lines
Quoted 14 lines

xen/arch/x86/domctl.c static void domain_cpu_policy_changed(struct domain *d) · line 114

Quoted 11 lines
xen/arch/x86/domctl.c +6 -0
@@ -106,6 +106,13 @@ static void domain_cpu_policy_changed(struct domain *d)
                     ecx = 0;
                 edx = cpufeat_mask(X86_FEATURE_APIC);
 
+                /*
+                 * If the Hypervisor bit is set in the policy, we can also
+                 * forward it into real CPUID.
+                 */
+                if ( p->basic.hypervisor )
+                    ecx |= cpufeat_mask(X86_FEATURE_HYPERVISOR);

AFAICT dom0 will also get the hypervisor bit set by default, as that's part of both the HVM and the PV max policy?

Quoted 4 lines
Quoted 3 lines

If so:

Thanks.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 40 lines · Andrew Cooper
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Roger Pau Monné
Quoted 37 lines · Andrew Cooper

Currently when booting native on AMD hardware, cpuidmask_defaults._1cd gets configured with the HYPERVISOR bit before native CPUID is scanned for feature bits.

This results in cpu_has_hypervisor becoming set as part of identify_cpu(), and ends up appearing in the raw and host CPU policies.

A combination of this bug, and c/s bb502a8ca59 "x86: check feature flags after resume" which checks that feature bits don't go missing, results in broken S3 on AMD hardware.

Alter amd_init_levelling() to exclude the HYPERVISOR bit from cpumask_defaults, and update domain_cpu_policy_changed() to allow it to be explicitly forwarded.

This also fixes a bug on kexec, where the hypervisor bit is left enabled for the new kernel to find.

These changes highlight a further but - dom0 construction is asymetric with domU construction, by not having any calls to domain_cpu_policy_changed(). Extend arch_domain_create() to always call domain_cpu_policy_changed().

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Igor Druzhinin <[email protected]>
CC: Marek Marczykowski-Górecki <[email protected]>
CC: Claudia <[email protected]>

v2:
* Rewrite the commit message. No change to the patch content.

Marek/Claudia: Do either of you want a Reported-by tag seeing as you found a brand new way that this was broken?

Quoted 8 lines
Quoted 3 lines

I understand this is addressing only one half of their issue. Since you said you don't find it surprising, do you have any idea why the OSXSAVE bit is behaving differently on AMD and on Intel?

It isn't behaving differently between Intel and AMD, I don't think.

The diagnostics are asymmetric - they ever printed when a feature disappears, not for a feature appearing.

Oh, I see - this is the part I was missing.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel