Add CpuidUserDis support
PATCH v3 1/2 x86: Refactor conditional guard in probe_cpuid_faulting()
In reply to Alejandro VallejoMove vendor-specific checks to the vendor-specific callers. While at it move the synth cap setters to the callers too, as it's needed for a later patch and it's not a functional change either.
No functional change.
---
v3:
* Moved the setting of LCAP_* bits to intel.c and amd.c
--- xen/arch/x86/cpu/amd.c | 13 ++++++++++++- xen/arch/x86/cpu/common.c | 13 ------------- xen/arch/x86/cpu/intel.c | 12 +++++++++++- 3 files changed, 23 insertions(+), 15 deletions(-)
xen/arch/x86/cpu/amd.c +12 -1
@@ -271,8 +271,19 @@ static void __init noinline amd_init_levelling(void) {const struct cpuidmask *m =NULL ; -if (probe_cpuid_faulting ()) +/* +* If there's support for CpuidUserDis or CPUID faulting then +* we can skip levelling because CPUID accesses are trapped anyway. +* +* CPUID faulting is an Intel feature analogous to CpuidUserDis, so +* that can only be present when Xen is itself virtualized (because +* it can be emulated) +*/ +if (cpu_has_hypervisor &&probe_cpuid_faulting ()) { + expected_levelling_cap |= LCAP_faulting; + levelling_caps |= LCAP_faulting;return ; + }probe_masking_msrs ();
xen/arch/x86/cpu/common.c +0 -13
@@ -131,17 +131,6 @@ bool __init probe_cpuid_faulting(void)uint64_t val;int rc; -/* -* Don't bother looking for CPUID faulting if we aren't virtualised on -* AMD or Hygon hardware - it won't be present. Likewise for Fam0F -* Intel hardware. -*/ -if (((boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) || - ((boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ) && - boot_cpu_data.x86 ==0xf )) && - !cpu_has_hypervisor) -return false; -if ((rc =rdmsr_safe (MSR_INTEL_PLATFORM_INFO , val)) ==0 ) raw_cpu_policy.platform_info.cpuid_faulting = val &MSR_PLATFORM_INFO_CPUID_FAULTING ;
@@ -155,8 +144,6 @@ bool __init probe_cpuid_faulting(void)return false; } - expected_levelling_cap |= LCAP_faulting; - levelling_caps |= LCAP_faulting;setup_force_cpu_cap (X86_FEATURE_CPUID_FAULTING );return true;
xen/arch/x86/cpu/intel.c +11 -1
@@ -226,8 +226,18 @@ static void cf_check intel_ctxt_switch_masking(const struct vcpu *next)*/ static void __init noinline intel_init_levelling(void) { -if (probe_cpuid_faulting ()) +/* +* Intel Fam0f is old enough that probing for CPUID faulting support +* introduces spurious #GP(0) when the appropriate MSRs are read, +* so skip it altogether. In the case where Xen is virtualized these +* MSRs may be emulated though, so we allow it in that case. +*/ +if ((boot_cpu_data.x86 !=0xf || cpu_has_hypervisor) && +probe_cpuid_faulting ()) { + expected_levelling_cap |= LCAP_faulting; + levelling_caps |= LCAP_faulting;return ; + }probe_masking_msrs ();
Signature · 1 line
2.34.1
PATCH v3 2/2 x86: Add support for CpuidUserDis
In reply to Alejandro VallejoBecause CpuIdUserDis is reported in CPUID itself, the extended leaf containing that bit must be retrieved before calling c_early_init()
---
v3:
* Moved LCAP_* setters to the callers in patch1/v3
* Added rationale for checking CPUID faulting before CpuidUserDis in AMD
--- xen/arch/x86/cpu/amd.c | 23 ++++++++++++++++++-- xen/arch/x86/cpu/common.c | 38 ++++++++++++++++++++++++---------- xen/arch/x86/include/asm/amd.h | 1 + 3 files changed, 49 insertions(+), 13 deletions(-)
xen/arch/x86/cpu/amd.c +21 -2
@@ -277,9 +277,14 @@ static void __init noinline amd_init_levelling(void)* * CPUID faulting is an Intel feature analogous to CpuidUserDis, so * that can only be present when Xen is itself virtualized (because -* it can be emulated) +* it can be emulated). +* +* Note that probing for the Intel feature _first_ isn't a mistake, +* but a means to ensure MSR_INTEL_PLATFORM_INFO is read and added +* to the raw CPU policy if present. */ -if (cpu_has_hypervisor &&probe_cpuid_faulting ()) { +if ((cpu_has_hypervisor &&probe_cpuid_faulting ()) || +boot_cpu_has (X86_FEATURE_CPUID_USER_DIS )) { expected_levelling_cap |= LCAP_faulting; levelling_caps |= LCAP_faulting;return ;
@@ -374,6 +379,20 @@ static void __init noinline amd_init_levelling(void) ctxt_switch_masking = amd_ctxt_switch_masking; } +void amd_set_cpuid_user_dis (bool enable) +{ +const uint64_t bit =K8_HWCR_CPUID_USER_DIS ; +uint64_t val; + +rdmsrl (MSR_K8_HWCR , val); + +if (!!(val & bit) == enable) +return ; + + val ^= bit; +wrmsrl (MSR_K8_HWCR , val); +} +/* * Check for the presence of an AMD erratum. Arguments are defined in amd.h * for each known erratum. Return 1 if erratum is found.
xen/arch/x86/cpu/common.c +27 -11
@@ -4,6 +4,7 @@#include <xen/param.h> #include <xen/smp.h> +#include <asm/amd.h> #include <asm/cpu-policy.h> #include <asm/current.h> #include <asm/debugreg.h>
@@ -166,8 +167,10 @@ static void set_cpuid_faulting(bool enable)void ctxt_switch_levelling (const struct vcpu *next) {const struct domain *nextd = next ? next->domain :NULL ; +bool enable_cpuid_faulting; -if (cpu_has_cpuid_faulting) { +if (cpu_has_cpuid_faulting || +boot_cpu_has (X86_FEATURE_CPUID_USER_DIS )) {/* * No need to alter the faulting setting if we are switching * to idle; it won't affect any code running in idle context.
@@ -188,12 +191,18 @@ void ctxt_switch_levelling(const struct vcpu *next)* an interim escape hatch in the form of * `dom0=no-cpuid-faulting` to restore the older behaviour. */ -set_cpuid_faulting (nextd && (opt_dom0_cpuid_faulting || - !is_control_domain (nextd) || - !is_pv_domain (nextd)) && - (is_pv_domain (nextd) || - next->arch.msrs-> - misc_features_enables.cpuid_faulting)); + enable_cpuid_faulting = nextd && (opt_dom0_cpuid_faulting || + !is_control_domain (nextd) || + !is_pv_domain (nextd)) && + (is_pv_domain (nextd) || + next->arch.msrs-> + misc_features_enables.cpuid_faulting); + +if (cpu_has_cpuid_faulting) +set_cpuid_faulting (enable_cpuid_faulting); +else +amd_set_cpuid_user_dis (enable_cpuid_faulting); +return ; }
@@ -402,6 +411,17 @@ static void generic_identify(struct cpuinfo_x86 *c) c->apicid =phys_pkg_id ((ebx >>24 ) &0xFF ,0 ); c->phys_proc_id = c->apicid; + eax =cpuid_eax (0x80000000 ); +if ((eax >>16 ) ==0x8000 ) + c->extended_cpuid_level = eax; + +/* +* These AMD-defined flags are out of place, but we need +* them early for the CPUID faulting probe code +*/ +if (c->extended_cpuid_level >=0x80000021 ) + c->x86_capability[FEATURESET_e21a] =cpuid_eax (0x80000021 ); +if (this_cpu->c_early_init) this_cpu->c_early_init (c);
@@ -418,10 +438,6 @@ static void generic_identify(struct cpuinfo_x86 *c) (cpuid_ecx(CPUID_PM_LEAF) & CPUID6_ECX_APERFMPERF_CAPABILITY) )__set_bit (X86_FEATURE_APERFMPERF , c->x86_capability); - eax =cpuid_eax (0x80000000 ); -if ((eax >>16 ) ==0x8000 ) - c->extended_cpuid_level = eax; -/* AMD-defined flags: level 0x80000001 */ if (c->extended_cpuid_level >=0x80000001 ) cpuid(0x80000001, &tmp, &tmp,
xen/arch/x86/include/asm/amd.h +1 -0
@@ -155,5 +155,6 @@ extern bool amd_legacy_ssbd;extern bool amd_virt_spec_ctrl;bool amd_setup_legacy_ssbd (void );void amd_set_legacy_ssbd (bool enable); +void amd_set_cpuid_user_dis (bool enable);#endif /* __AMD_H__ */
Signature · 1 line
2.34.1
v3 cover Add CpuidUserDis support
v3:
* Move LCAP setters from patch 2 to patch 1
* Comment on rationale for checking CPUID faulting before CpuidUserDis on AMD
Nowadays AMD supports trapping the CPUID instruction from ring>0 to ring0, (CpuidUserDis) akin to Intel's "CPUID faulting". There is a difference in that the toggle bit is in a different MSR and the support bit is in CPUID itself rather than yet another MSR. This patch enables AMD hosts to use it when supported in order to provide correct CPUID contents to PV guests.
Patch 1 moves vendor-specific code on probe_cpuid_faulting() to amd.c/intel.c
Patch 2 adds support for CpuidUserDis, hooking it in the probing path and the context switching path.
Alejandro Vallejo (2):
x86: Refactor conditional guard in probe_cpuid_faulting()
x86: Add support for CpuidUserDis
xen/arch/x86/cpu/amd.c | 32 ++++++++++++++++++++- xen/arch/x86/cpu/common.c | 51 ++++++++++++++++++---------------- xen/arch/x86/cpu/intel.c | 12 +++++++- xen/arch/x86/include/asm/amd.h | 1 + 4 files changed, 70 insertions(+), 26 deletions(-)
Signature · 1 line
2.34.1
Re: [PATCH v3 2/2] x86: Add support for CpuidUserDis
In reply to Alejandro VallejoQuoted 4 lines · Alejandro Vallejo
Because CpuIdUserDis is reported in CPUID itself, the extended leaf containing that bit must be retrieved before calling
c_early_init()Signed-off-by Alejandro Vallejo <[email protected]>