x86: Merge cpuid and msr policy objects
v2 cover x86: Merge cpuid and msr policy objects
This is in order to be able to put MSR_ARCH_CAPS in a featureset. In hindsight it was a mistake separating CPUID and read-only MSR data into separate structs.
Patches 1-8 were posted previously and have had the feedback addressed. Patches 9-15 are the result of splitting the older RFC patch 9 apart, with the discussed adjustment to aliases accounted for.
Gitlab run showing the series to be buildable at each changeset:
https://gitlab.com/xen-project/people/andyhhp/xen/-/jobs/4053607867
P.S. I'm not sure I believe the net diffstat below. I think the cpuid.c => cpu-policy.c rename is confusing it.
Andrew Cooper (15):
x86: Rename struct cpu_policy to struct old_cpuid_policy
x86: Rename {domctl,sysctl}.cpu_policy.{cpuid,msr}_policy fields
x86: Rename struct cpuid_policy to struct cpu_policy
x86: Merge struct msr_policy into struct cpu_policy
x86: Merge the system {cpuid,msr} policy objects
x86: Merge a domain's {cpuid,msr} policy objects
x86: Merge xc_cpu_policy's cpuid and msr objects
x86: Drop struct old_cpu_policy
x86: Out-of-inline the policy<->featureset convertors x86/boot: Move MSR policy initialisation logic into cpu-policy.c x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c x86/emul: Switch x86_emulate_ctxt to cpu_policy tools/fuzz: Rework afl-policy-fuzzer
libx86: Update library API for cpu_policy
x86: Remove temporary {cpuid,msr}_policy defines
tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 64 +-
.../fuzz/x86_instruction_emulator/fuzz-emul.c | 2 +-
tools/libs/guest/xg_cpuid_x86.c | 50 +-
tools/libs/guest/xg_private.h | 5 +-
tools/tests/cpu-policy/test-cpu-policy.c | 54 +-
tools/tests/tsx/test-tsx.c | 71 +-
tools/tests/x86_emulator/Makefile | 2 +-
tools/tests/x86_emulator/test_x86_emulator.c | 2 +-
tools/tests/x86_emulator/x86-emulate.c | 4 +-
tools/tests/x86_emulator/x86-emulate.h | 2 +-
xen/arch/x86/Makefile | 1 +
xen/arch/x86/{cpuid.c => cpu-policy.c} | 692 ++++----------
xen/arch/x86/cpu/common.c | 4 +-
xen/arch/x86/cpu/mcheck/mce_intel.c | 2 +-
xen/arch/x86/cpuid.c | 856 +-----------------
xen/arch/x86/domain.c | 17 +-
xen/arch/x86/domctl.c | 51 +-
xen/arch/x86/hvm/emulate.c | 4 +-
xen/arch/x86/hvm/hvm.c | 5 +-
xen/arch/x86/hvm/svm/svm.c | 2 +-
xen/arch/x86/hvm/vlapic.c | 2 +-
xen/arch/x86/hvm/vmx/vmx.c | 8 +-
xen/arch/x86/include/asm/cpu-policy.h | 27 +
xen/arch/x86/include/asm/cpuid.h | 21 +-
xen/arch/x86/include/asm/domain.h | 13 +-
xen/arch/x86/include/asm/msr.h | 14 +-
xen/arch/x86/mm/mem_sharing.c | 3 +-
xen/arch/x86/mm/shadow/hvm.c | 2 +-
xen/arch/x86/msr.c | 160 +---
xen/arch/x86/pv/domain.c | 3 +-
xen/arch/x86/pv/emul-priv-op.c | 6 +-
xen/arch/x86/pv/ro-page-fault.c | 2 +-
xen/arch/x86/setup.c | 5 +-
xen/arch/x86/sysctl.c | 79 +-
xen/arch/x86/traps.c | 2 +-
xen/arch/x86/x86_emulate/private.h | 4 +-
xen/arch/x86/x86_emulate/x86_emulate.c | 2 +-
xen/arch/x86/x86_emulate/x86_emulate.h | 9 +-
xen/arch/x86/xstate.c | 4 +-
xen/include/public/domctl.h | 4 +-
xen/include/public/sysctl.h | 4 +-
xen/include/xen/lib/x86/cpu-policy.h | 510 ++++++++++-
xen/include/xen/lib/x86/cpuid.h | 475 ----------
xen/include/xen/lib/x86/msr.h | 104 ---
xen/lib/x86/cpuid.c | 68 +-
xen/lib/x86/msr.c | 6 +-
xen/lib/x86/policy.c | 8 +-
47 files changed, 1005 insertions(+), 2430 deletions(-)
copy xen/arch/x86/{cpuid.c => cpu-policy.c} (52%)
create mode 100644 xen/arch/x86/include/asm/cpu-policy.h
delete mode 100644 xen/include/xen/lib/x86/cpuid.h
delete mode 100644 xen/include/xen/lib/x86/msr.hSignature · 1 line
2.30.2
PATCH v2 1/15 x86: Rename struct cpu_policy to struct old_cpuid_policy
We want to merge struct cpuid_policy and struct msr_policy together, and the result wants to be called struct cpu_policy.
The current struct cpu_policy, being a pair of pointers, isn't terribly useful. Rename the type to struct old_cpu_policy, but it will disappear entirely once the merge is complete.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
--- tools/libs/guest/xg_cpuid_x86.c | 4 ++-- tools/tests/cpu-policy/test-cpu-policy.c | 4 ++-- xen/arch/x86/domctl.c | 4 ++-- xen/arch/x86/include/asm/cpuid.h | 2 +- xen/arch/x86/sysctl.c | 4 ++-- xen/include/xen/lib/x86/cpu-policy.h | 6 +++--- xen/lib/x86/policy.c | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +2 -2
@@ -868,8 +868,8 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host, xc_cpu_policy_t *guest) {struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ; -struct cpu_policy h = { &host->cpuid, &host->msr }; -struct cpu_policy g = { &guest->cpuid, &guest->msr }; +struct old_cpu_policy h = { &host->cpuid, &host->msr }; +struct old_cpu_policy g = { &guest->cpuid, &guest->msr };int rc =x86_cpu_policies_are_compatible (&h, &g, &err);if ( !rc )
tools/tests/cpu-policy/test-cpu-policy.c +2 -2
@@ -602,7 +602,7 @@ static void test_is_compatible_success(void)for (size_t i =0 ; i <ARRAY_SIZE (tests); ++i ) {struct test *t = &tests[i]; -struct cpu_policy sys = { +struct old_cpu_policy sys = { &t->host_cpuid, &t->host_msr, }, new = {
@@ -654,7 +654,7 @@ static void test_is_compatible_failure(void)for (size_t i =0 ; i <ARRAY_SIZE (tests); ++i ) {struct test *t = &tests[i]; -struct cpu_policy sys = { +struct old_cpu_policy sys = { &t->host_cpuid, &t->host_msr, }, new = {
xen/arch/x86/domctl.c +2 -2
@@ -40,8 +40,8 @@static int update_domain_cpu_policy (struct domain *d,xen_domctl_cpu_policy_t *xdpc) { -struct cpu_policy new = {}; -const struct cpu_policy *sys =is_pv_domain (d) +struct old_cpu_policy new = {}; +const struct old_cpu_policy *sys =is_pv_domain (d) ? &system_policies[XEN_SYSCTL_cpu_policy_pv_max] : &system_policies[XEN_SYSCTL_cpu_policy_hvm_max];struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ;
xen/arch/x86/include/asm/cpuid.h +1 -1
@@ -51,7 +51,7 @@ extern struct cpuid_policy raw_cpuid_policy, host_cpuid_policy, pv_max_cpuid_policy, pv_def_cpuid_policy, hvm_max_cpuid_policy, hvm_def_cpuid_policy; -extern const struct cpu_policy system_policies[]; +extern const struct old_cpu_policy system_policies[];/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu);
xen/arch/x86/sysctl.c +2 -2
@@ -32,7 +32,7 @@#include <asm/psr.h> #include <asm/cpuid.h> -const struct cpu_policy system_policies[6 ] = { +const struct old_cpu_policy system_policies[6 ] = { [ XEN_SYSCTL_cpu_policy_raw ] = { &raw_cpuid_policy, &raw_msr_policy,
@@ -391,7 +391,7 @@ long arch_do_sysctl(case XEN_SYSCTL_get_cpu_policy: { -const struct cpu_policy *policy; +const struct old_cpu_policy *policy;/* Reserved field set, or bad policy index? */ if ( sysctl->u.cpu_policy._rsvd ||
xen/include/xen/lib/x86/cpu-policy.h +3 -3
@@ -5,7 +5,7 @@#include <xen/lib/x86/cpuid.h> #include <xen/lib/x86/msr.h> -struct cpu_policy +struct old_cpu_policy {struct cpuid_policy *cpuid;struct msr_policy *msr;
@@ -33,8 +33,8 @@ struct cpu_policy_errors* incompatibility is detected, the optional err pointer may identify the * problematic leaf/subleaf and/or MSR. */ -int x86_cpu_policies_are_compatible (const struct cpu_policy *host, -const struct cpu_policy *guest, +int x86_cpu_policies_are_compatible (const struct old_cpu_policy *host, +const struct old_cpu_policy *guest,struct cpu_policy_errors *err);#endif /* !XEN_LIB_X86_POLICIES_H */
xen/lib/x86/policy.c +2 -2
@@ -2,8 +2,8 @@#include <xen/lib/x86/cpu-policy.h> -int x86_cpu_policies_are_compatible (const struct cpu_policy *host, -const struct cpu_policy *guest, +int x86_cpu_policies_are_compatible (const struct old_cpu_policy *host, +const struct old_cpu_policy *guest,struct cpu_policy_errors *err) {struct cpu_policy_errors e =INIT_CPU_POLICY_ERRORS ;
Signature · 1 line
2.30.2
PATCH v2 2/15 x86: Rename {domctl,sysctl}.cpu_policy.{cpuid,msr}_policy fields
In reply to Andrew CooperThese weren't great names to begin with, and using {leaves,msrs} matches up better with the existing nr_{leaves,msr} parameters anyway.
Furthermore, by renaming these fields we can get away with using some #define trickery to avoid the struct {cpuid,msr}_policy merge needing to happen in a single changeset.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Fix typo in commit message
--- tools/libs/guest/xg_cpuid_x86.c | 12 ++++++------ xen/arch/x86/domctl.c | 12 ++++++------ xen/arch/x86/sysctl.c | 8 ++++---- xen/include/public/domctl.h | 4 ++-- xen/include/public/sysctl.h | 4 ++-- 5 files changed, 20 insertions(+), 20 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +6 -6
@@ -145,9 +145,9 @@ static int get_system_cpu_policy(xc_interface *xch, uint32_t index, sysctl.cmd = XEN_SYSCTL_get_cpu_policy; sysctl.u.cpu_policy.index = index; sysctl.u.cpu_policy.nr_leaves = *nr_leaves; -set_xen_guest_handle (sysctl.u.cpu_policy.cpuid_policy, leaves); +set_xen_guest_handle (sysctl.u.cpu_policy.leaves, leaves); sysctl.u.cpu_policy.nr_msrs = *nr_msrs; -set_xen_guest_handle (sysctl.u.cpu_policy.msr_policy, msrs); +set_xen_guest_handle (sysctl.u.cpu_policy.msrs, msrs); ret =do_sysctl (xch, &sysctl);
@@ -183,9 +183,9 @@ static int get_domain_cpu_policy(xc_interface *xch, uint32_t domid, domctl.cmd = XEN_DOMCTL_get_cpu_policy; domctl.domain = domid; domctl.u.cpu_policy.nr_leaves = *nr_leaves; -set_xen_guest_handle (domctl.u.cpu_policy.cpuid_policy, leaves); +set_xen_guest_handle (domctl.u.cpu_policy.leaves, leaves); domctl.u.cpu_policy.nr_msrs = *nr_msrs; -set_xen_guest_handle (domctl.u.cpu_policy.msr_policy, msrs); +set_xen_guest_handle (domctl.u.cpu_policy.msrs, msrs); ret =do_domctl (xch, &domctl);
@@ -232,9 +232,9 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid, domctl.cmd = XEN_DOMCTL_set_cpu_policy; domctl.domain = domid; domctl.u.cpu_policy.nr_leaves = nr_leaves; -set_xen_guest_handle (domctl.u.cpu_policy.cpuid_policy, leaves); +set_xen_guest_handle (domctl.u.cpu_policy.leaves, leaves); domctl.u.cpu_policy.nr_msrs = nr_msrs; -set_xen_guest_handle (domctl.u.cpu_policy.msr_policy, msrs); +set_xen_guest_handle (domctl.u.cpu_policy.msrs, msrs); domctl.u.cpu_policy.err_leaf =-1 ; domctl.u.cpu_policy.err_subleaf =-1 ; domctl.u.cpu_policy.err_msr =-1 ;
xen/arch/x86/domctl.c +6 -6
@@ -54,10 +54,10 @@ static int update_domain_cpu_policy(struct domain *d,/* Merge the toolstack provided data. */ if ( (ret =x86_cpuid_copy_from_buffer ( - new.cpuid, xdpc->cpuid_policy, xdpc->nr_leaves, + new.cpuid, xdpc->leaves, xdpc->nr_leaves, &err.leaf, &err.subleaf)) || (ret =x86_msr_copy_from_buffer ( - new.msr, xdpc->msr_policy, xdpc->nr_msrs, &err.msr)) ) + new.msr, xdpc->msrs, xdpc->nr_msrs, &err.msr)) ) goto out;/* Trim any newly-stale out-of-range leaves. */
@@ -1317,20 +1317,20 @@ long arch_do_domctl(case XEN_DOMCTL_get_cpu_policy:/* Process the CPUID leaves. */ -if (guest_handle_is_null (domctl->u.cpu_policy.cpuid_policy) ) +if (guest_handle_is_null (domctl->u.cpu_policy.leaves) ) domctl->u.cpu_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ;else if ( (ret =x86_cpuid_copy_to_buffer ( d->arch.cpuid, - domctl->u.cpu_policy.cpuid_policy, + domctl->u.cpu_policy.leaves, &domctl->u.cpu_policy.nr_leaves)) )break ;/* Process the MSR entries. */ -if (guest_handle_is_null (domctl->u.cpu_policy.msr_policy) ) +if (guest_handle_is_null (domctl->u.cpu_policy.msrs) ) domctl->u.cpu_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ;else if ( (ret =x86_msr_copy_to_buffer ( d->arch.msr, - domctl->u.cpu_policy.msr_policy, + domctl->u.cpu_policy.msrs, &domctl->u.cpu_policy.nr_msrs)) )break ;
xen/arch/x86/sysctl.c +4 -4
@@ -411,11 +411,11 @@ long arch_do_sysctl( }/* Process the CPUID leaves. */ -if (guest_handle_is_null (sysctl->u.cpu_policy.cpuid_policy) ) +if (guest_handle_is_null (sysctl->u.cpu_policy.leaves) ) sysctl->u.cpu_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ;else if ( (ret =x86_cpuid_copy_to_buffer ( policy->cpuid, - sysctl->u.cpu_policy.cpuid_policy, + sysctl->u.cpu_policy.leaves, &sysctl->u.cpu_policy.nr_leaves)) )break ;
@@ -427,11 +427,11 @@ long arch_do_sysctl( }/* Process the MSR entries. */ -if (guest_handle_is_null (sysctl->u.cpu_policy.msr_policy) ) +if (guest_handle_is_null (sysctl->u.cpu_policy.msrs) ) sysctl->u.cpu_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ;else if ( (ret =x86_msr_copy_to_buffer ( policy->msr, - sysctl->u.cpu_policy.msr_policy, + sysctl->u.cpu_policy.msrs, &sysctl->u.cpu_policy.nr_msrs)) )break ;
xen/include/public/domctl.h +2 -2
@@ -683,8 +683,8 @@ struct xen_domctl_cpu_policy {* 'cpuid_policy'. */ uint32_t nr_msrs;/* IN/OUT: Number of MSRs in/written to * 'msr_policy' */ -XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* IN/OUT */ -XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* IN/OUT */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) leaves;/* IN/OUT */ +XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msrs;/* IN/OUT */ /* * OUT, set_policy only. Written in some (but not all) error cases to
xen/include/public/sysctl.h +2 -2
@@ -1050,8 +1050,8 @@ struct xen_sysctl_cpu_policy {* 'msr_policy', or the maximum number of MSRs if * the guest handle is NULL. */ uint32_t _rsvd;/* Must be zero. */ -XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) cpuid_policy;/* OUT */ -XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msr_policy;/* OUT */ +XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t) leaves;/* OUT */ +XEN_GUEST_HANDLE_64 (xen_msr_entry_t) msrs;/* OUT */ };typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t ;DEFINE_XEN_GUEST_HANDLE (xen_sysctl_cpu_policy_t);
Signature · 1 line
2.30.2
PATCH v2 3/15 x86: Rename struct cpuid_policy to struct cpu_policy
In reply to Andrew CooperAlso merge lib/x86/cpuid.h entirely into lib/x86/cpu-policy.h
Use a temporary define to make struct cpuid_policy still work.
There's one forward declaration of struct cpuid_policy in tools/tests/x86_emulator/x86-emulate.h that isn't covered by the define, and it's easier to rename that now than to rearrange the includes.
No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Retain test/x86_emulator handcoded dependency on cpuid-autogen.h
* Rebase over x86_emulate() split
--- tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 2 +- tools/tests/x86_emulator/Makefile | 2 +- tools/tests/x86_emulator/x86-emulate.h | 2 +- xen/arch/x86/include/asm/cpuid.h | 1 - xen/arch/x86/x86_emulate/x86_emulate.h | 2 +- xen/include/xen/lib/x86/cpu-policy.h | 463 ++++++++++++++++++++- xen/include/xen/lib/x86/cpuid.h | 475 ---------------------- xen/lib/x86/cpuid.c | 2 +- 8 files changed, 467 insertions(+), 482 deletions(-) delete mode 100644 xen/include/xen/lib/x86/cpuid.h
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +1 -1
@@ -9,7 +9,7 @@#include <getopt.h> #include <xen-tools/common-macros.h> -#include <xen/lib/x86/cpuid.h> +#include <xen/lib/x86/cpu-policy.h> #include <xen/lib/x86/msr.h> #include <xen/domctl.h>
tools/tests/x86_emulator/Makefile +1 -1
@@ -292,7 +292,7 @@ HOSTCFLAGS += $(CFLAGS_xeninclude) -I. $(HOSTCFLAGS-$(XEN_COMPILE_ARCH)) x86.h := $(addprefix $(XEN_ROOT)/tools/include/xen/asm/,\ x86-vendors.h x86-defns.h msr-index.h) \ $(addprefix $(XEN_ROOT)/tools/include/xen/lib/x86/, \ - cpuid.h cpuid-autogen.h) + cpu-policy.h cpuid-autogen.h) x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h x86_emulate/private.h $(x86.h) $(OBJS): %.o: %.c $(x86_emulate.h)
tools/tests/x86_emulator/x86-emulate.h +1 -1
@@ -77,7 +77,7 @@#define is_canonical_address (x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63))extern uint32_t mxcsr_mask; -extern struct cpuid_policy cp; +extern struct cpu_policy cp;#define MMAP_SZ 16384bool emul_test_init (void );
xen/arch/x86/include/asm/cpuid.h +0 -1
@@ -9,7 +9,6 @@#include <xen/percpu.h> #include <xen/lib/x86/cpu-policy.h> -#include <xen/lib/x86/cpuid.h> #include <public/sysctl.h>
xen/arch/x86/x86_emulate/x86_emulate.h +1 -1
@@ -23,7 +23,7 @@#ifndef __X86_EMULATE_H__#define __X86_EMULATE_H__ -#include <xen/lib/x86/cpuid.h> +#include <xen/lib/x86/cpu-policy.h> #define MAX_INST_LEN 15
xen/include/xen/lib/x86/cpu-policy.h +462 -1
477 lines not on this page · raw message
xen/include/xen/lib/x86/cpuid.h deleted +0 -475
475 lines not on this page · raw message
xen/lib/x86/cpuid.c +1 -1
@@ -1,6 +1,6 @@#include "private.h" -#include <xen/lib/x86/cpuid.h> +#include <xen/lib/x86/cpu-policy.h> static void zero_leaves (struct cpuid_leaf *l,unsigned int first,unsigned int last)
Signature · 1 line
2.30.2
PATCH v2 4/15 x86: Merge struct msr_policy into struct cpu_policy
In reply to Andrew CooperAs with the cpuid side, use a temporary define to make struct msr_policy still work.
Note, this means that domains now have two separate struct cpu_policy allocations with disjoint information, and system policies are in a similar position, as well as xc_cpu_policy objects in libxenguest. All of these duplications will be addressed in the following patches.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Alter msr_policy -> cpu_policy in comments.
--- tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 1 - xen/arch/x86/include/asm/msr.h | 3 +- xen/include/xen/lib/x86/cpu-policy.h | 81 ++++++++++++++++- xen/include/xen/lib/x86/msr.h | 104 ---------------------- xen/lib/x86/msr.c | 2 +- 5 files changed, 83 insertions(+), 108 deletions(-) delete mode 100644 xen/include/xen/lib/x86/msr.h
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +0 -1
@@ -10,7 +10,6 @@#include <xen-tools/common-macros.h> #include <xen/lib/x86/cpu-policy.h> -#include <xen/lib/x86/msr.h> #include <xen/domctl.h> static bool debug;
xen/arch/x86/include/asm/msr.h +2 -1
@@ -6,8 +6,9 @@#include <xen/types.h> #include <xen/percpu.h> #include <xen/errno.h> +#include <xen/kernel.h> -#include <xen/lib/x86/msr.h> +#include <xen/lib/x86/cpu-policy.h> #include <asm/asm_defns.h> #include <asm/cpufeature.h>
xen/include/xen/lib/x86/cpu-policy.h +80 -1
120 lines not on this page · raw message
xen/include/xen/lib/x86/msr.h deleted +0 -104
104 lines not on this page · raw message
xen/lib/x86/msr.c +1 -1
@@ -1,6 +1,6 @@#include "private.h" -#include <xen/lib/x86/msr.h> +#include <xen/lib/x86/cpu-policy.h> /* * Copy a single MSR into the provided msr_entry_buffer_t buffer, performing a
Signature · 1 line
2.30.2
PATCH v2 5/15 x86: Merge the system {cpuid,msr} policy objects
In reply to Andrew CooperRight now, they're the same underlying type, containing disjoint information.
Introduce a new cpu-policy.{h,c} to be the new location for all policy handling logic. Place the combined objects in __ro_after_init, which is new since the original logic was written.
As we're trying to phase out the use of struct old_cpu_policy entirely, rework update_domain_cpu_policy() to not pointer-chase through system_policies[].
This in turn allows system_policies[] in sysctl.c to become static and reduced in scope to XEN_SYSCTL_get_cpu_policy.
No practical change. This undoes the transient doubling of storage space from earlier patches.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Reword commit message
* Reintroduce dropped const from system_policies[]
--- xen/arch/x86/Makefile | 1 + xen/arch/x86/cpu-policy.c | 18 +++++++ xen/arch/x86/cpu/common.c | 4 +- xen/arch/x86/cpuid.c | 66 +++++++++++-------------- xen/arch/x86/domctl.c | 17 +++++-- xen/arch/x86/include/asm/cpu-policy.h | 14 ++++++ xen/arch/x86/include/asm/cpuid.h | 6 --- xen/arch/x86/include/asm/msr.h | 7 --- xen/arch/x86/msr.c | 38 ++++++-------- xen/arch/x86/sysctl.c | 71 ++++++++++----------------- 10 files changed, 116 insertions(+), 126 deletions(-) create mode 100644 xen/arch/x86/cpu-policy.c create mode 100644 xen/arch/x86/include/asm/cpu-policy.h
xen/arch/x86/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-y += bitops.o obj-bin-y += bzimage.init.o obj-bin-y += clear_page.o obj-bin-y += copy_page.o +obj-y += cpu-policy.o obj-y += cpuid.o obj-$(CONFIG_PV) += compat.o obj-$(CONFIG_PV32) += x86_64/compat.o
xen/arch/x86/cpu-policy.c new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#include <xen/cache.h> +#include <xen/kernel.h> + +#include <xen/lib/x86/cpu-policy.h> + +#include <asm/cpu-policy.h> + +struct cpu_policy __ro_after_init raw_cpu_policy; +struct cpu_policy __ro_after_init host_cpu_policy; +#ifdef CONFIG_PV +struct cpu_policy __ro_after_init pv_max_cpu_policy; +struct cpu_policy __ro_after_init pv_def_cpu_policy; +#endif +#ifdef CONFIG_HVM +struct cpu_policy __ro_after_init hvm_max_cpu_policy; +struct cpu_policy __ro_after_init hvm_def_cpu_policy; +#endif
xen/arch/x86/cpu/common.c +3 -1
@@ -3,6 +3,8 @@#include <xen/delay.h> #include <xen/param.h> #include <xen/smp.h> + +#include <asm/cpu-policy.h> #include <asm/current.h> #include <asm/debugreg.h> #include <asm/processor.h>
@@ -141,7 +143,7 @@ bool __init probe_cpuid_faulting(void)return false;if ((rc =rdmsr_safe (MSR_INTEL_PLATFORM_INFO , val)) ==0 ) - raw_msr_policy.platform_info.cpuid_faulting = + raw_cpu_policy.platform_info.cpuid_faulting = val &MSR_PLATFORM_INFO_CPUID_FAULTING ; if (rc ||
xen/arch/x86/cpuid.c +28 -38
171 lines not on this page · raw message
xen/arch/x86/domctl.c +12 -5
@@ -35,18 +35,25 @@#include <asm/mem_sharing.h> #include <asm/xstate.h> #include <asm/psr.h> -#include <asm/cpuid.h> +#include <asm/cpu-policy.h> static int update_domain_cpu_policy (struct domain *d,xen_domctl_cpu_policy_t *xdpc) {struct old_cpu_policy new = {}; -const struct old_cpu_policy *sys =is_pv_domain (d) - ? &system_policies[XEN_SYSCTL_cpu_policy_pv_max] - : &system_policies[XEN_SYSCTL_cpu_policy_hvm_max]; +struct cpu_policy *sys =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL ); +struct old_cpu_policy old_sys = { sys, sys };struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ;int ret = -ENOMEM ; +if ( !sys ) + { +ASSERT_UNREACHABLE (); +return -EOPNOTSUPP ; + } +/* Start by copying the domain's existing policies. */ if ( !(new.cpuid =xmemdup (d->arch.cpuid)) || !(new.msr =xmemdup (d->arch.msr)) )
@@ -64,7 +71,7 @@ static int update_domain_cpu_policy(struct domain *d,x86_cpuid_policy_clear_out_of_range_leaves (new.cpuid);/* Audit the combined dataset. */ - ret =x86_cpu_policies_are_compatible (sys, &new, &err); + ret =x86_cpu_policies_are_compatible (&old_sys, &new, &err);if ( ret ) goto out;
xen/arch/x86/include/asm/cpu-policy.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef X86_CPU_POLICY_H +#define X86_CPU_POLICY_H + +struct cpu_policy ; + +extern struct cpu_policy raw_cpu_policy; +extern struct cpu_policy host_cpu_policy; +extern struct cpu_policy pv_max_cpu_policy; +extern struct cpu_policy pv_def_cpu_policy; +extern struct cpu_policy hvm_max_cpu_policy; +extern struct cpu_policy hvm_def_cpu_policy; + +#endif /* X86_CPU_POLICY_H */
xen/arch/x86/include/asm/cpuid.h +0 -6
@@ -46,12 +46,6 @@ DECLARE_PER_CPU(struct cpuidmasks, cpuidmasks);/* Default masking MSR values, calculated at boot. */ extern struct cpuidmasks cpuidmask_defaults; -extern struct cpuid_policy raw_cpuid_policy, host_cpuid_policy, - pv_max_cpuid_policy, pv_def_cpuid_policy, - hvm_max_cpuid_policy, hvm_def_cpuid_policy; - -extern const struct old_cpu_policy system_policies[]; -/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu);
xen/arch/x86/include/asm/msr.h +0 -7
@@ -292,13 +292,6 @@ static inline void wrmsr_tsc_aux(uint32_t val)uint64_t msr_spec_ctrl_valid_bits (const struct cpuid_policy *cp); -extern struct msr_policy raw_msr_policy, - host_msr_policy, - pv_max_msr_policy, - pv_def_msr_policy, - hvm_max_msr_policy, - hvm_def_msr_policy; -/* Container object for per-vCPU MSRs */ struct vcpu_msrs {
xen/arch/x86/msr.c +14 -24
@@ -25,6 +25,7 @@#include <xen/sched.h> #include <asm/amd.h> +#include <asm/cpu-policy.h> #include <asm/debugreg.h> #include <asm/hvm/nestedhvm.h> #include <asm/hvm/viridian.h>
@@ -37,20 +38,9 @@DEFINE_PER_CPU (uint32_t, tsc_aux); -struct msr_policy __read_mostly raw_msr_policy, -__read_mostly host_msr_policy; -#ifdef CONFIG_PV -struct msr_policy __read_mostly pv_max_msr_policy; -struct msr_policy __read_mostly pv_def_msr_policy; -#endif -#ifdef CONFIG_HVM -struct msr_policy __read_mostly hvm_max_msr_policy; -struct msr_policy __read_mostly hvm_def_msr_policy; -#endif -static void __init calculate_raw_policy (void ) { -struct msr_policy *mp = &raw_msr_policy; +struct msr_policy *mp = &raw_cpu_policy;/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* Was already added by probe_cpuid_faulting() */
@@ -61,9 +51,9 @@ static void __init calculate_raw_policy(void)static void __init calculate_host_policy (void ) { -struct msr_policy *mp = &host_msr_policy; +struct msr_policy *mp = &host_cpu_policy; - *mp = raw_msr_policy; + *mp = raw_cpu_policy;/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */
@@ -81,25 +71,25 @@ static void __init calculate_host_policy(void)static void __init calculate_pv_max_policy (void ) { -struct msr_policy *mp = &pv_max_msr_policy; +struct msr_policy *mp = &pv_max_cpu_policy; - *mp = host_msr_policy; + *mp = host_cpu_policy; mp->arch_caps.raw =0 ;/* Not supported yet. */ }static void __init calculate_pv_def_policy (void ) { -struct msr_policy *mp = &pv_def_msr_policy; +struct msr_policy *mp = &pv_def_cpu_policy; - *mp = pv_max_msr_policy; + *mp = pv_max_cpu_policy; }static void __init calculate_hvm_max_policy (void ) { -struct msr_policy *mp = &hvm_max_msr_policy; +struct msr_policy *mp = &hvm_max_cpu_policy; - *mp = host_msr_policy; + *mp = host_cpu_policy;/* It's always possible to emulate CPUID faulting for HVM guests */ mp->platform_info.cpuid_faulting = true;
@@ -109,9 +99,9 @@ static void __init calculate_hvm_max_policy(void)static void __init calculate_hvm_def_policy (void ) { -struct msr_policy *mp = &hvm_def_msr_policy; +struct msr_policy *mp = &hvm_def_cpu_policy; - *mp = hvm_max_msr_policy; + *mp = hvm_max_cpu_policy; }void __init init_guest_msr_policy (void )
@@ -135,8 +125,8 @@ void __init init_guest_msr_policy(void)int init_domain_msr_policy (struct domain *d) {struct msr_policy *mp =is_pv_domain (d) - ? (IS_ENABLED (CONFIG_PV ) ? &pv_def_msr_policy :NULL ) - : (IS_ENABLED (CONFIG_HVM ) ? &hvm_def_msr_policy :NULL ); + ? (IS_ENABLED (CONFIG_PV ) ? &pv_def_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_def_cpu_policy :NULL );if ( !mp ) {
xen/arch/x86/sysctl.c +26 -45
115 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 6/15 x86: Merge a domain's {cpuid,msr} policy objects
In reply to Andrew CooperRight now, they're the same underlying type, containing disjoint information.
Drop the d->arch.msr pointer, and union d->arch.cpuid to give it a second name of cpu_policy in the interim.
Merge init_domain_{cpuid,msr}_policy() into a single init_domain_cpu_policy(), moving the implementation into cpu-policy.c
No practical change. This undoes the transient doubling of storage space from earlier patches.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Reword commit message.
* Undo accidental deletion of v->arch.msrs.
--- xen/arch/x86/cpu-policy.c | 49 +++++++++++++++++++++++++++ xen/arch/x86/cpuid.c | 23 ------------- xen/arch/x86/domain.c | 15 +++----- xen/arch/x86/domctl.c | 35 ++++++++++--------- xen/arch/x86/include/asm/cpu-policy.h | 4 +++ xen/arch/x86/include/asm/cpuid.h | 3 -- xen/arch/x86/include/asm/domain.h | 13 +++++-- xen/arch/x86/include/asm/msr.h | 1 - xen/arch/x86/mm/mem_sharing.c | 3 +- xen/arch/x86/msr.c | 44 ------------------------ 10 files changed, 86 insertions(+), 104 deletions(-)
xen/arch/x86/cpu-policy.c +49 -0
@@ -1,10 +1,13 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ #include <xen/cache.h> #include <xen/kernel.h> +#include <xen/sched.h> #include <xen/lib/x86/cpu-policy.h> #include <asm/cpu-policy.h> +#include <asm/msr-index.h> +#include <asm/setup.h> struct cpu_policy __ro_after_init raw_cpu_policy;struct cpu_policy __ro_after_init host_cpu_policy;
@@ -16,3 +19,49 @@ struct cpu_policy __ro_after_init pv_def_cpu_policy;struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif + +int init_domain_cpu_policy (struct domain *d) +{ +struct cpu_policy *p =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_def_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_def_cpu_policy :NULL ); + +if ( !p ) + { +ASSERT_UNREACHABLE (); +return -EOPNOTSUPP ; + } + + p =xmemdup (p); +if ( !p ) +return -ENOMEM ; + +/* See comment in ctxt_switch_levelling() */ +if ( !opt_dom0_cpuid_faulting &&is_control_domain (d) &&is_pv_domain (d) ) + p->platform_info.cpuid_faulting = false; + +/* +* Expose the "hardware speculation behaviour" bits of ARCH_CAPS to dom0, +* so dom0 can turn off workarounds as appropriate. Temporary, until the +* domain policy logic gains a better understanding of MSRs. +*/ +if (is_hardware_domain (d) && cpu_has_arch_caps ) + { +uint64_t val; + +rdmsrl (MSR_ARCH_CAPABILITIES , val); + + p->arch_caps.raw = val & + (ARCH_CAPS_RDCL_NO |ARCH_CAPS_IBRS_ALL |ARCH_CAPS_RSBA | +ARCH_CAPS_SSB_NO |ARCH_CAPS_MDS_NO |ARCH_CAPS_IF_PSCHANGE_MC_NO | +ARCH_CAPS_TAA_NO |ARCH_CAPS_SBDR_SSDP_NO |ARCH_CAPS_FBSDP_NO | +ARCH_CAPS_PSDP_NO |ARCH_CAPS_FB_CLEAR |ARCH_CAPS_RRSBA | +ARCH_CAPS_BHI_NO |ARCH_CAPS_PBRSB_NO ); + } + + d->arch.cpu_policy = p; + +recalculate_cpuid_policy (d); + +return 0 ; +}
xen/arch/x86/cpuid.c +0 -23
@@ -784,29 +784,6 @@ void recalculate_cpuid_policy(struct domain *d) p->extd.raw[0x19 ] =EMPTY_LEAF ; } -int init_domain_cpuid_policy (struct domain *d) -{ -struct cpuid_policy *p =is_pv_domain (d) - ? (IS_ENABLED (CONFIG_PV ) ? &pv_def_cpu_policy :NULL ) - : (IS_ENABLED (CONFIG_HVM ) ? &hvm_def_cpu_policy :NULL ); - -if ( !p ) - { -ASSERT_UNREACHABLE (); -return -EOPNOTSUPP ; - } - - p =xmemdup (p); -if ( !p ) -return -ENOMEM ; - - d->arch.cpuid = p; - -recalculate_cpuid_policy (d); - -return 0 ; -} -void __init init_dom0_cpuid_policy (struct domain *d) {struct cpuid_policy *p = d->arch.cpuid;
xen/arch/x86/domain.c +5 -10
@@ -66,6 +66,7 @@#ifdef CONFIG_COMPAT #include <compat/vcpu.h> #endif +#include <asm/cpu-policy.h> #include <asm/psr.h> #include <asm/pv/domain.h> #include <asm/pv/mm.h>
@@ -743,8 +744,7 @@ int arch_domain_create(struct domain *d, d->arch.ctxt_switch = &idle_csw; - d->arch.cpuid =ZERO_BLOCK_PTR ;/* Catch stray misuses. */ - d->arch.msr =ZERO_BLOCK_PTR ; + d->arch.cpu_policy =ZERO_BLOCK_PTR ;/* Catch stray misuses. */ return 0 ; }
@@ -799,10 +799,7 @@ int arch_domain_create(struct domain *d, goto fail; paging_initialised = true; -if ( (rc =init_domain_cpuid_policy (d)) ) - goto fail; - -if ( (rc =init_domain_msr_policy (d)) ) +if ( (rc =init_domain_cpu_policy (d)) ) goto fail; d->arch.ioport_caps =
@@ -873,8 +870,7 @@ int arch_domain_create(struct domain *d,iommu_domain_destroy (d);cleanup_domain_irq_mapping (d);free_xenheap_page (d->shared_info); -xfree (d->arch.cpuid); -xfree (d->arch.msr); +XFREE (d->arch.cpu_policy);if ( paging_initialised )paging_final_teardown (d);free_perdomain_mappings (d);
@@ -888,8 +884,7 @@ void arch_domain_destroy(struct domain *d)hvm_domain_destroy (d);xfree (d->arch.e820); -xfree (d->arch.cpuid); -xfree (d->arch.msr); +XFREE (d->arch.cpu_policy);free_domain_pirqs (d);if ( !is_idle_domain (d) )
xen/arch/x86/domctl.c +17 -18
@@ -40,11 +40,11 @@static int update_domain_cpu_policy (struct domain *d,xen_domctl_cpu_policy_t *xdpc) { -struct old_cpu_policy new = {}; +struct cpu_policy *new;struct cpu_policy *sys =is_pv_domain (d) ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL ); -struct old_cpu_policy old_sys = { sys, sys }; +struct old_cpu_policy old_sys = { sys, sys }, old_new;struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ;int ret = -ENOMEM ;
@@ -54,33 +54,33 @@ static int update_domain_cpu_policy(struct domain *d,return -EOPNOTSUPP ; } -/* Start by copying the domain's existing policies. */ -if ( !(new.cpuid =xmemdup (d->arch.cpuid)) || - !(new.msr =xmemdup (d->arch.msr)) ) +/* Start by copying the domain's existing policy. */ +if ( !(new =xmemdup (d->arch.cpu_policy)) ) goto out; + old_new = (struct old_cpu_policy ){ new, new }; +/* Merge the toolstack provided data. */ if ( (ret =x86_cpuid_copy_from_buffer ( - new.cpuid, xdpc->leaves, xdpc->nr_leaves, + new, xdpc->leaves, xdpc->nr_leaves, &err.leaf, &err.subleaf)) || (ret =x86_msr_copy_from_buffer ( - new.msr, xdpc->msrs, xdpc->nr_msrs, &err.msr)) ) + new, xdpc->msrs, xdpc->nr_msrs, &err.msr)) ) goto out;/* Trim any newly-stale out-of-range leaves. */ -x86_cpuid_policy_clear_out_of_range_leaves (new.cpuid); +x86_cpuid_policy_clear_out_of_range_leaves (new);/* Audit the combined dataset. */ - ret =x86_cpu_policies_are_compatible (&old_sys, &new, &err); + ret =x86_cpu_policies_are_compatible (&old_sys, &old_new, &err);if ( ret ) goto out;/* -* Audit was successful. Replace existing policies, leaving the old -* policies to be freed. +* Audit was successful. Replace the existing policy, leaving the old one +* to be freed. */ -SWAP (new.cpuid, d->arch.cpuid); -SWAP (new.msr, d->arch.msr); +SWAP (new, d->arch.cpu_policy);/* TODO: Drop when x86_cpu_policies_are_compatible() is completed. */ recalculate_cpuid_policy (d);
@@ -89,9 +89,8 @@ static int update_domain_cpu_policy(struct domain *d,domain_cpu_policy_changed (d); out: -/* Free whichever cpuid/msr structs are not installed in struct domain. */ -xfree (new.cpuid); -xfree (new.msr); +/* Free whichever struct is not installed in struct domain. */ +xfree (new);if ( ret ) {
@@ -1327,7 +1326,7 @@ long arch_do_domctl(if (guest_handle_is_null (domctl->u.cpu_policy.leaves) ) domctl->u.cpu_policy.nr_leaves =CPUID_MAX_SERIALISED_LEAVES ;else if ( (ret =x86_cpuid_copy_to_buffer ( - d->arch.cpuid, + d->arch.cpu_policy, domctl->u.cpu_policy.leaves, &domctl->u.cpu_policy.nr_leaves)) )break ;
@@ -1336,7 +1335,7 @@ long arch_do_domctl(if (guest_handle_is_null (domctl->u.cpu_policy.msrs) ) domctl->u.cpu_policy.nr_msrs =MSR_MAX_SERIALISED_ENTRIES ;else if ( (ret =x86_msr_copy_to_buffer ( - d->arch.msr, + d->arch.cpu_policy, domctl->u.cpu_policy.msrs, &domctl->u.cpu_policy.nr_msrs)) )break ;
xen/arch/x86/include/asm/cpu-policy.h +4 -0
@@ -3,6 +3,7 @@#define X86_CPU_POLICY_H struct cpu_policy ; +struct domain ;extern struct cpu_policy raw_cpu_policy;extern struct cpu_policy host_cpu_policy;
@@ -11,4 +12,7 @@ extern struct cpu_policy pv_def_cpu_policy;extern struct cpu_policy hvm_max_cpu_policy;extern struct cpu_policy hvm_def_cpu_policy; +/* Allocate and initialise a CPU policy suitable for the domain. */ +int init_domain_cpu_policy (struct domain *d); +#endif /* X86_CPU_POLICY_H */
xen/arch/x86/include/asm/cpuid.h +0 -3
@@ -49,9 +49,6 @@ extern struct cpuidmasks cpuidmask_defaults;/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu); -/* Allocate and initialise a CPUID policy suitable for the domain. */ -int init_domain_cpuid_policy (struct domain *d); -/* Apply dom0-specific tweaks to the CPUID policy. */ void init_dom0_cpuid_policy (struct domain *d);
xen/arch/x86/include/asm/domain.h +10 -3
@@ -386,9 +386,16 @@ struct arch_domain*/ uint8_t x87_fip_width; -/* CPUID and MSR policy objects. */ -struct cpuid_policy *cpuid; -struct msr_policy *msr; +/* +* The domain's CPU Policy. "cpu_policy" is considered the canonical +* pointer, but the "cpuid" and "msr" aliases exist so the most +* appropriate one can be used for local code clarity. +*/ +union { +struct cpu_policy *cpu_policy; +struct cpu_policy *cpuid; +struct cpu_policy *msr; + };struct PITState vpit;
xen/arch/x86/include/asm/msr.h +0 -1
@@ -419,7 +419,6 @@ struct vcpu_msrs };void init_guest_msr_policy (void ); -int init_domain_msr_policy (struct domain *d);int init_vcpu_msr_policy (struct vcpu *v);/*
xen/arch/x86/mm/mem_sharing.c +1 -2
@@ -1902,8 +1902,7 @@ static int fork(struct domain *cd, struct domain *d)domain_pause (d); cd->max_pages = d->max_pages; - *cd->arch.cpuid = *d->arch.cpuid; - *cd->arch.msr = *d->arch.msr; + *cd->arch.cpu_policy = *d->arch.cpu_policy; cd->vmtrace_size = d->vmtrace_size; cd->parent = d; }
xen/arch/x86/msr.c +0 -44
50 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 7/15 x86: Merge xc_cpu_policy's cpuid and msr objects
In reply to Andrew CooperRight now, they're the same underlying type, containing disjoint information.
Use a single object instead. Also take the opportunity to rename 'entries' to 'msrs' which is more descriptive, and more in line with nr_msrs being the count of MSR entries in the API.
test-tsx uses xg_private.h to access the internals of xc_cpu_policy, so needs updating at the same time. Take the opportunity to improve the code clarity by passing a cpu_policy rather than an xc_cpu_policy into some functions.
No practical change. This undoes the transient doubling of storage space from earlier patches.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Reword the commit message.
* Clean up test-tsx a bit more.
--- tools/libs/guest/xg_cpuid_x86.c | 36 ++++++++--------- tools/libs/guest/xg_private.h | 5 +-- tools/tests/tsx/test-tsx.c | 71 +++++++++++++++------------------ 3 files changed, 53 insertions(+), 59 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +18 -18
@@ -431,7 +431,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore,xc_dominfo_t di;unsigned int i, nr_leaves, nr_msrs;xen_cpuid_leaf_t *leaves =NULL ; -struct cpuid_policy *p =NULL ; +struct cpu_policy *p =NULL ;uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ;uint32_t host_featureset[FEATURESET_NR_ENTRIES ] = {};uint32_t len =ARRAY_SIZE (host_featureset);
@@ -692,7 +692,7 @@ static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy,uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ;int rc; - rc =x86_cpuid_copy_from_buffer (&policy->cpuid, policy->leaves, + rc =x86_cpuid_copy_from_buffer (&policy->policy, policy->leaves, nr_leaves, &err_leaf, &err_subleaf);if ( rc ) {
@@ -702,7 +702,7 @@ static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy,return rc; } - rc =x86_msr_copy_from_buffer (&policy->msr, policy->entries, + rc =x86_msr_copy_from_buffer (&policy->policy, policy->msrs, nr_entries, &err_msr);if ( rc ) {
@@ -719,18 +719,18 @@ int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx, xc_cpu_policy_t *policy) {unsigned int nr_leaves =ARRAY_SIZE (policy->leaves); -unsigned int nr_entries =ARRAY_SIZE (policy->entries); +unsigned int nr_msrs =ARRAY_SIZE (policy->msrs);int rc; rc =get_system_cpu_policy (xch, policy_idx, &nr_leaves, policy->leaves, - &nr_entries, policy->entries); + &nr_msrs, policy->msrs);if ( rc ) {PERROR ("Failed to obtain %u policy" , policy_idx);return rc; } - rc =deserialize_policy (xch, policy, nr_leaves, nr_entries); + rc =deserialize_policy (xch, policy, nr_leaves, nr_msrs);if ( rc ) { errno = -rc;
@@ -744,18 +744,18 @@ int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid, xc_cpu_policy_t *policy) {unsigned int nr_leaves =ARRAY_SIZE (policy->leaves); -unsigned int nr_entries =ARRAY_SIZE (policy->entries); +unsigned int nr_msrs =ARRAY_SIZE (policy->msrs);int rc; rc =get_domain_cpu_policy (xch, domid, &nr_leaves, policy->leaves, - &nr_entries, policy->entries); + &nr_msrs, policy->msrs);if ( rc ) {PERROR ("Failed to obtain domain %u policy" , domid);return rc; } - rc =deserialize_policy (xch, policy, nr_leaves, nr_entries); + rc =deserialize_policy (xch, policy, nr_leaves, nr_msrs);if ( rc ) { errno = -rc;
@@ -770,16 +770,16 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid, {uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ;unsigned int nr_leaves =ARRAY_SIZE (policy->leaves); -unsigned int nr_entries =ARRAY_SIZE (policy->entries); +unsigned int nr_msrs =ARRAY_SIZE (policy->msrs);int rc; rc =xc_cpu_policy_serialise (xch, policy, policy->leaves, &nr_leaves, - policy->entries, &nr_entries); + policy->msrs, &nr_msrs);if ( rc )return rc; rc =xc_set_domain_cpu_policy (xch, domid, nr_leaves, policy->leaves, - nr_entries, policy->entries, + nr_msrs, policy->msrs, &err_leaf, &err_subleaf, &err_msr);if ( rc ) {
@@ -802,7 +802,7 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *p,if ( leaves ) { - rc =x86_cpuid_copy_to_buffer (&p->cpuid, leaves, nr_leaves); + rc =x86_cpuid_copy_to_buffer (&p->policy, leaves, nr_leaves);if ( rc ) {ERROR ("Failed to serialize CPUID policy" );
@@ -813,7 +813,7 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t *p,if ( msrs ) { - rc =x86_msr_copy_to_buffer (&p->msr, msrs, nr_msrs); + rc =x86_msr_copy_to_buffer (&p->policy, msrs, nr_msrs);if ( rc ) {ERROR ("Failed to serialize MSR policy" );
@@ -831,7 +831,7 @@ int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t *policy, uint32_t nr) {unsigned int err_leaf =-1 , err_subleaf =-1 ; -int rc =x86_cpuid_copy_from_buffer (&policy->cpuid, leaves, nr, +int rc =x86_cpuid_copy_from_buffer (&policy->policy, leaves, nr, &err_leaf, &err_subleaf);if ( rc )
@@ -850,7 +850,7 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy, const xen_msr_entry_t *msrs, uint32_t nr) { unsigned int err_msr = -1; -int rc =x86_msr_copy_from_buffer (&policy->msr, msrs, nr, &err_msr); +int rc =x86_msr_copy_from_buffer (&policy->policy, msrs, nr, &err_msr);if ( rc ) {
@@ -868,8 +868,8 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host, xc_cpu_policy_t *guest) {struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ; -struct old_cpu_policy h = { &host->cpuid, &host->msr }; -struct old_cpu_policy g = { &guest->cpuid, &guest->msr }; +struct old_cpu_policy h = { &host->policy, &host->policy }; +struct old_cpu_policy g = { &guest->policy, &guest->policy };int rc =x86_cpu_policies_are_compatible (&h, &g, &err);if ( !rc )
tools/libs/guest/xg_private.h +2 -3
@@ -173,10 +173,9 @@ int pin_table(xc_interface *xch, unsigned int type, unsigned long mfn,#include <xen/lib/x86/cpu-policy.h> struct xc_cpu_policy { -struct cpuid_policy cpuid; -struct msr_policy msr; +struct cpu_policy policy;xen_cpuid_leaf_t leaves[CPUID_MAX_SERIALISED_LEAVES ]; -xen_msr_entry_t entries[MSR_MAX_SERIALISED_ENTRIES ]; +xen_msr_entry_t msrs[MSR_MAX_SERIALISED_ENTRIES ]; };#endif /* x86 */
tools/tests/tsx/test-tsx.c +33 -38
167 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 8/15 x86: Drop struct old_cpu_policy
In reply to Andrew CooperWith all the complicated callers of x86_cpu_policies_are_compatible() updated to use a single cpu_policy object, we can drop the final user of struct old_cpu_policy.
Update x86_cpu_policies_are_compatible() to take (new) cpu_policy pointers, reducing the amount of internal pointer chasing, and update all callers to pass their cpu_policy objects directly.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Fix host/guest typo in xc_cpu_policy_is_compatible()
--- tools/libs/guest/xg_cpuid_x86.c | 4 +- tools/tests/cpu-policy/test-cpu-policy.c | 50 +++++++----------------- xen/arch/x86/domctl.c | 7 +--- xen/include/xen/lib/x86/cpu-policy.h | 12 ++---- xen/lib/x86/policy.c | 12 +++--- 5 files changed, 27 insertions(+), 58 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +1 -3
@@ -868,9 +868,7 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host, xc_cpu_policy_t *guest) {struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ; -struct old_cpu_policy h = { &host->policy, &host->policy }; -struct old_cpu_policy g = { &guest->policy, &guest->policy }; -int rc =x86_cpu_policies_are_compatible (&h, &g, &err); +int rc =x86_cpu_policies_are_compatible (&host->policy, &guest->policy, &err);if ( !rc )return true;
tools/tests/cpu-policy/test-cpu-policy.c +15 -35
128 lines not on this page · raw message
xen/arch/x86/domctl.c +2 -5
@@ -41,10 +41,9 @@ static int update_domain_cpu_policy(struct domain *d, xen_domctl_cpu_policy_t *xdpc) {struct cpu_policy *new; -struct cpu_policy *sys =is_pv_domain (d) +const struct cpu_policy *sys =is_pv_domain (d) ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL ); -struct old_cpu_policy old_sys = { sys, sys }, old_new;struct cpu_policy_errors err =INIT_CPU_POLICY_ERRORS ;int ret = -ENOMEM ;
@@ -58,8 +57,6 @@ static int update_domain_cpu_policy(struct domain *d,if ( !(new =xmemdup (d->arch.cpu_policy)) ) goto out; - old_new = (struct old_cpu_policy ){ new, new }; -/* Merge the toolstack provided data. */ if ( (ret = x86_cpuid_copy_from_buffer( new, xdpc->leaves, xdpc->nr_leaves,
@@ -72,7 +69,7 @@ static int update_domain_cpu_policy(struct domain *d,x86_cpuid_policy_clear_out_of_range_leaves (new);/* Audit the combined dataset. */ - ret =x86_cpu_policies_are_compatible (&old_sys, &old_new, &err); + ret =x86_cpu_policies_are_compatible (sys, new, &err);if ( ret ) goto out;
xen/include/xen/lib/x86/cpu-policy.h +3 -9
@@ -379,12 +379,6 @@ struct cpu_policy#define cpuid_policy cpu_policy#define msr_policy cpu_policy -struct old_cpu_policy -{ -struct cpuid_policy *cpuid; -struct msr_policy *msr; -}; -struct cpu_policy_errors {uint32_t leaf, subleaf;
@@ -559,7 +553,7 @@ int x86_msr_copy_from_buffer(struct msr_policy *policy, const msr_entry_buffer_t msrs, uint32_t nr_entries, uint32_t *err_msr); -/* +/** * Calculate whether two policies are compatible. * * i.e. Can a VM configured with @guest run on a CPU supporting @host.
@@ -573,8 +567,8 @@ int x86_msr_copy_from_buffer(struct msr_policy *policy,* incompatibility is detected, the optional err pointer may identify the * problematic leaf/subleaf and/or MSR. */ -int x86_cpu_policies_are_compatible (const struct old_cpu_policy *host, -const struct old_cpu_policy *guest, +int x86_cpu_policies_are_compatible (const struct cpu_policy *host, +const struct cpu_policy *guest,struct cpu_policy_errors *err);#endif /* !XEN_LIB_X86_POLICIES_H */
xen/lib/x86/policy.c +6 -6
@@ -2,8 +2,8 @@#include <xen/lib/x86/cpu-policy.h> -int x86_cpu_policies_are_compatible (const struct old_cpu_policy *host, -const struct old_cpu_policy *guest, +int x86_cpu_policies_are_compatible (const struct cpu_policy *host, +const struct cpu_policy *guest,struct cpu_policy_errors *err) {struct cpu_policy_errors e =INIT_CPU_POLICY_ERRORS ;
@@ -15,18 +15,18 @@ int x86_cpu_policies_are_compatible(const struct old_cpu_policy *host,#define FAIL_MSR (m) \ do { e.msr = (m); goto out; } while ( 0 ) -if ( guest->cpuid->basic.max_leaf > host->cpuid->basic.max_leaf ) +if ( guest->basic.max_leaf > host->basic.max_leaf )FAIL_CPUID (0 ,NA ); -if ( guest->cpuid->feat.max_subleaf > host->cpuid->feat.max_subleaf ) +if ( guest->feat.max_subleaf > host->feat.max_subleaf )FAIL_CPUID (7 ,0 ); -if ( guest->cpuid->extd.max_leaf > host->cpuid->extd.max_leaf ) +if ( guest->extd.max_leaf > host->extd.max_leaf )FAIL_CPUID (0x80000000 ,NA );/* TODO: Audit more CPUID data. */ -if ( ~host->msr->platform_info.raw & guest->msr->platform_info.raw ) +if ( ~host->platform_info.raw & guest->platform_info.raw )FAIL_MSR (MSR_INTEL_PLATFORM_INFO );#undef FAIL_MSR
Signature · 1 line
2.30.2
PATCH v2 9/15 x86: Out-of-inline the policy<->featureset convertors
In reply to Andrew CooperThese are already getting over-large for being inline functions, and are only going to grow more over time. Out of line them, yielding the following net delta from bloat-o-meter:
add/remove: 2/0 grow/shrink: 0/4 up/down: 276/-1877 (-1601)
Switch to the newer cpu_policy terminology while doing so.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
--- tools/libs/guest/xg_cpuid_x86.c | 2 +- xen/arch/x86/cpuid.c | 28 +++++++-------- xen/arch/x86/sysctl.c | 2 +- xen/include/xen/lib/x86/cpu-policy.h | 52 ++++++---------------------- xen/lib/x86/cpuid.c | 42 ++++++++++++++++++++++ 5 files changed, 68 insertions(+), 58 deletions(-)
tools/libs/guest/xg_cpuid_x86.c +1 -1
@@ -565,7 +565,7 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, } } -cpuid_featureset_to_policy (feat, p); +x86_cpu_featureset_to_policy (feat, p); }else {
xen/arch/x86/cpuid.c +14 -14
@@ -368,7 +368,7 @@ static void __init calculate_host_policy(void) p->extd.max_leaf =0x80000000 |min_t (uint32_t, max_extd_leaf &0xffff ,ARRAY_SIZE (p->extd.raw) -1 ); -cpuid_featureset_to_policy (boot_cpu_data.x86_capability, p); +x86_cpu_featureset_to_policy (boot_cpu_data.x86_capability, p);recalculate_xstate (p);recalculate_misc (p);
@@ -450,7 +450,7 @@ static void __init calculate_pv_max_policy(void)unsigned int i; *p = host_cpu_policy; -cpuid_policy_to_featureset (p, pv_featureset); +x86_cpu_policy_to_featureset (p, pv_featureset);for ( i =0 ; i <ARRAY_SIZE (pv_featureset); ++i ) pv_featureset[i] &= pv_max_featuremask[i];
@@ -468,7 +468,7 @@ static void __init calculate_pv_max_policy(void)guest_common_feature_adjustments (pv_featureset);sanitise_featureset (pv_featureset); -cpuid_featureset_to_policy (pv_featureset, p); +x86_cpu_featureset_to_policy (pv_featureset, p);recalculate_xstate (p); p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */
@@ -481,7 +481,7 @@ static void __init calculate_pv_def_policy(void)unsigned int i; *p = pv_max_cpu_policy; -cpuid_policy_to_featureset (p, pv_featureset); +x86_cpu_policy_to_featureset (p, pv_featureset);for ( i =0 ; i <ARRAY_SIZE (pv_featureset); ++i ) pv_featureset[i] &= pv_def_featuremask[i];
@@ -490,7 +490,7 @@ static void __init calculate_pv_def_policy(void)guest_common_default_feature_adjustments (pv_featureset);sanitise_featureset (pv_featureset); -cpuid_featureset_to_policy (pv_featureset, p); +x86_cpu_featureset_to_policy (pv_featureset, p);recalculate_xstate (p); }
@@ -502,7 +502,7 @@ static void __init calculate_hvm_max_policy(void)const uint32_t *hvm_featuremask; *p = host_cpu_policy; -cpuid_policy_to_featureset (p, hvm_featureset); +x86_cpu_policy_to_featureset (p, hvm_featureset); hvm_featuremask =hvm_hap_supported () ? hvm_hap_max_featuremask : hvm_shadow_max_featuremask;
@@ -581,7 +581,7 @@ static void __init calculate_hvm_max_policy(void)guest_common_feature_adjustments (hvm_featureset);sanitise_featureset (hvm_featureset); -cpuid_featureset_to_policy (hvm_featureset, p); +x86_cpu_featureset_to_policy (hvm_featureset, p);recalculate_xstate (p); }
@@ -593,7 +593,7 @@ static void __init calculate_hvm_def_policy(void)const uint32_t *hvm_featuremask; *p = hvm_max_cpu_policy; -cpuid_policy_to_featureset (p, hvm_featureset); +x86_cpu_policy_to_featureset (p, hvm_featureset); hvm_featuremask =hvm_hap_supported () ? hvm_hap_def_featuremask : hvm_shadow_def_featuremask;
@@ -612,7 +612,7 @@ static void __init calculate_hvm_def_policy(void)__set_bit (X86_FEATURE_VIRT_SSBD , hvm_featureset);sanitise_featureset (hvm_featureset); -cpuid_featureset_to_policy (hvm_featureset, p); +x86_cpu_featureset_to_policy (hvm_featureset, p);recalculate_xstate (p); }
@@ -682,8 +682,8 @@ void recalculate_cpuid_policy(struct domain *d) ? CPUID_GUEST_NR_EXTD_AMD : CPUID_GUEST_NR_EXTD_INTEL) - 1); -cpuid_policy_to_featureset (p, fs); -cpuid_policy_to_featureset (max, max_fs); +x86_cpu_policy_to_featureset (p, fs); +x86_cpu_policy_to_featureset (max, max_fs);if (is_hvm_domain (d) ) {
@@ -740,7 +740,7 @@ void recalculate_cpuid_policy(struct domain *d) (cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | cpufeat_mask(X86_FEATURE_NO_FPU_SEL))); -cpuid_featureset_to_policy (fs, p); +x86_cpu_featureset_to_policy (fs, p);/* Pass host cacheline size through to guests. */ p->basic.clflush_size = max->basic.clflush_size;
@@ -806,7 +806,7 @@ void __init init_dom0_cpuid_policy(struct domain *d)uint32_t fs[FSCAPINTS ];unsigned int i; -cpuid_policy_to_featureset (p, fs); +x86_cpu_policy_to_featureset (p, fs);for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) {
@@ -814,7 +814,7 @@ void __init init_dom0_cpuid_policy(struct domain *d) fs[i] &= ~dom0_disable_feat[i]; } -cpuid_featureset_to_policy (fs, p); +x86_cpu_featureset_to_policy (fs, p);recalculate_cpuid_policy (d); }
xen/arch/x86/sysctl.c +1 -1
@@ -338,7 +338,7 @@ long arch_do_sysctl( ret = -EINVAL ;if ( !ret ) -cpuid_policy_to_featureset (p, featureset); +x86_cpu_policy_to_featureset (p, featureset);/* Copy the requested featureset into place. */ if ( !ret && copy_to_guest(sysctl->u.cpu_featureset.features,
xen/include/xen/lib/x86/cpu-policy.h +10 -42
59 lines not on this page · raw message
xen/lib/x86/cpuid.c +42 -0
48 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v2 09/15] x86: Out-of-inline the policy<->featureset convertors
Quoted 9 lines · Andrew Cooper
These are already getting over-large for being inline functions, and are only going to grow more over time. Out of line them, yielding the following net delta from bloat-o-meter:
add/remove: 2/0 grow/shrink: 0/4 up/down: 276/-1877 (-1601)
Switch to the newer
cpu_policyterminology while doing so.Signed-off-by Andrew Cooper <[email protected]>
I take it you have a reason to ...
Quoted 37 lines
xen/lib/x86/cpuid.c +31
-0@@ -60,6 +60,48 @@ const char *x86_cpuid_vendor_to_str(unsigned int vendor) } } +void x86_cpu_policy_to_featureset ( +const struct cpu_policy *p,uint32_t fs[FEATURESET_NR_ENTRIES ]) +{ + fs[FEATURESET_1d] = p->basic._1d; + fs[FEATURESET_1c] = p->basic._1c; + fs[FEATURESET_e1d] = p->extd.e1d; + fs[FEATURESET_e1c] = p->extd.e1c; + fs[FEATURESET_Da1] = p->xstate.Da1; + fs[FEATURESET_7b0] = p->feat._7b0; + fs[FEATURESET_7c0] = p->feat._7c0; + fs[FEATURESET_e7d] = p->extd.e7d; + fs[FEATURESET_e8b] = p->extd.e8b; + fs[FEATURESET_7d0] = p->feat._7d0; + fs[FEATURESET_7a1] = p->feat._7a1; + fs[FEATURESET_e21a] = p->extd.e21a; + fs[FEATURESET_7b1] = p->feat._7b1; + fs[FEATURESET_7d2] = p->feat._7d2; + fs[FEATURESET_7c1] = p->feat._7c1; + fs[FEATURESET_7d1] = p->feat._7d1; +} + +void x86_cpu_featureset_to_policy ( +const uint32_t fs[FEATURESET_NR_ENTRIES ],struct cpu_policy *p) +{ + p->basic._1d = fs[FEATURESET_1d]; + p->basic._1c = fs[FEATURESET_1c]; + p->extd.e1d = fs[FEATURESET_e1d]; + p->extd.e1c = fs[FEATURESET_e1c]; + p->xstate.Da1 = fs[FEATURESET_Da1]; + p->feat._7b0 = fs[FEATURESET_7b0]; + p->feat._7c0 = fs[FEATURESET_7c0];
xen/lib/x86/cpuid.c const char *x86_cpuid_vendor_to_str(unsigned int vendor) · line 103
Quoted 10 lines
xen/lib/x86/cpuid.c +10
-0+ p->extd.e7d = fs[FEATURESET_e7d]; + p->extd.e8b = fs[FEATURESET_e8b]; + p->feat._7d0 = fs[FEATURESET_7d0]; + p->feat._7a1 = fs[FEATURESET_7a1]; + p->extd.e21a = fs[FEATURESET_e21a]; + p->feat._7b1 = fs[FEATURESET_7b1]; + p->feat._7d2 = fs[FEATURESET_7d2]; + p->feat._7c1 = fs[FEATURESET_7c1]; + p->feat._7d1 = fs[FEATURESET_7d1]; +}
... add quite a few padding blanks in here, unlike in the originals?
Jan
Re: [PATCH v2 09/15] x86: Out-of-inline the policy<->featureset convertors
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
These are already getting over-large for being inline functions, and are only going to grow more over time. Out of line them, yielding the following net delta from bloat-o-meter:
add/remove: 2/0 grow/shrink: 0/4 up/down: 276/-1877 (-1601)
Switch to the newer
cpu_policyterminology while doing so.Signed-off-by Andrew Cooper <[email protected]>Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 43 lines
I take it you have a reason to ...
Quoted 40 lines
xen/lib/x86/cpuid.c +34
-0@@ -60,6 +60,48 @@ const char *x86_cpuid_vendor_to_str(unsigned int vendor) } } +void x86_cpu_policy_to_featureset ( +const struct cpu_policy *p,uint32_t fs[FEATURESET_NR_ENTRIES ]) +{ + fs[FEATURESET_1d] = p->basic._1d; + fs[FEATURESET_1c] = p->basic._1c; + fs[FEATURESET_e1d] = p->extd.e1d; + fs[FEATURESET_e1c] = p->extd.e1c; + fs[FEATURESET_Da1] = p->xstate.Da1; + fs[FEATURESET_7b0] = p->feat._7b0; + fs[FEATURESET_7c0] = p->feat._7c0; + fs[FEATURESET_e7d] = p->extd.e7d; + fs[FEATURESET_e8b] = p->extd.e8b; + fs[FEATURESET_7d0] = p->feat._7d0; + fs[FEATURESET_7a1] = p->feat._7a1; + fs[FEATURESET_e21a] = p->extd.e21a; + fs[FEATURESET_7b1] = p->feat._7b1; + fs[FEATURESET_7d2] = p->feat._7d2; + fs[FEATURESET_7c1] = p->feat._7c1; + fs[FEATURESET_7d1] = p->feat._7d1; +} + +void x86_cpu_featureset_to_policy ( +const uint32_t fs[FEATURESET_NR_ENTRIES ],struct cpu_policy *p) +{ + p->basic._1d = fs[FEATURESET_1d]; + p->basic._1c = fs[FEATURESET_1c]; + p->extd.e1d = fs[FEATURESET_e1d]; + p->extd.e1c = fs[FEATURESET_e1c]; + p->xstate.Da1 = fs[FEATURESET_Da1]; + p->feat._7b0 = fs[FEATURESET_7b0]; + p->feat._7c0 = fs[FEATURESET_7c0]; + p->extd.e7d = fs[FEATURESET_e7d]; + p->extd.e8b = fs[FEATURESET_e8b]; + p->feat._7d0 = fs[FEATURESET_7d0];
Quoted 8 lines
xen/lib/x86/cpuid.c const char *x86_cpuid_vendor_to_str(unsigned int vendor) · line 103
Quoted 7 lines
xen/lib/x86/cpuid.c +7
-0+ p->feat._7a1 = fs[FEATURESET_7a1]; + p->extd.e21a = fs[FEATURESET_e21a]; + p->feat._7b1 = fs[FEATURESET_7b1]; + p->feat._7d2 = fs[FEATURESET_7d2]; + p->feat._7c1 = fs[FEATURESET_7c1]; + p->feat._7d1 = fs[FEATURESET_7d1]; +}... add quite a few padding blanks in here, unlike in the originals?
Yeah. There was already one misalignment, and I haven't quite decided on the MSR syntax yet but it's going to be longer still.
Here specifically, we've got p->arch_caps.{a,d} at a minimum, so column width is based on the MSR name.
This is just a guestimate of "plenty for now".
~Andrew
PATCH v2 10/15 x86/boot: Move MSR policy initialisation logic into cpu-policy.c
In reply to Andrew CooperSwitch to the newer cpu_policy nomenclature.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
--- xen/arch/x86/cpu-policy.c | 84 +++++++++++++++++++++++++++ xen/arch/x86/include/asm/cpu-policy.h | 3 + xen/arch/x86/include/asm/msr.h | 1 - xen/arch/x86/msr.c | 84 --------------------------- xen/arch/x86/setup.c | 3 +- 5 files changed, 89 insertions(+), 86 deletions(-)
xen/arch/x86/cpu-policy.c +84 -0
@@ -20,6 +20,90 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +static void __init calculate_raw_policy (void ) +{ +struct cpu_policy *p = &raw_cpu_policy; + +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ +/* Was already added by probe_cpuid_faulting() */ + +if ( cpu_has_arch_caps ) +rdmsrl (MSR_ARCH_CAPABILITIES , p->arch_caps.raw); +} + +static void __init calculate_host_policy (void ) +{ +struct cpu_policy *p = &host_cpu_policy; + + *p = raw_cpu_policy; + +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ +/* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */ + p->platform_info.cpuid_faulting = cpu_has_cpuid_faulting; + +/* Temporary, until we have known_features[] for feature bits in MSRs. */ + p->arch_caps.raw &= + (ARCH_CAPS_RDCL_NO |ARCH_CAPS_IBRS_ALL |ARCH_CAPS_RSBA | +ARCH_CAPS_SKIP_L1DFL |ARCH_CAPS_SSB_NO |ARCH_CAPS_MDS_NO | +ARCH_CAPS_IF_PSCHANGE_MC_NO |ARCH_CAPS_TSX_CTRL |ARCH_CAPS_TAA_NO | +ARCH_CAPS_SBDR_SSDP_NO |ARCH_CAPS_FBSDP_NO |ARCH_CAPS_PSDP_NO | +ARCH_CAPS_FB_CLEAR |ARCH_CAPS_RRSBA |ARCH_CAPS_BHI_NO | +ARCH_CAPS_PBRSB_NO ); +} + +static void __init calculate_pv_max_policy (void ) +{ +struct cpu_policy *p = &pv_max_cpu_policy; + + *p = host_cpu_policy; + + p->arch_caps.raw =0 ;/* Not supported yet. */ +} + +static void __init calculate_pv_def_policy (void ) +{ +struct cpu_policy *p = &pv_def_cpu_policy; + + *p = pv_max_cpu_policy; +} + +static void __init calculate_hvm_max_policy (void ) +{ +struct cpu_policy *p = &hvm_max_cpu_policy; + + *p = host_cpu_policy; + +/* It's always possible to emulate CPUID faulting for HVM guests */ + p->platform_info.cpuid_faulting = true; + + p->arch_caps.raw =0 ;/* Not supported yet. */ +} + +static void __init calculate_hvm_def_policy (void ) +{ +struct cpu_policy *p = &hvm_def_cpu_policy; + + *p = hvm_max_cpu_policy; +} + +void __init init_guest_cpu_policies (void ) +{ +calculate_raw_policy (); +calculate_host_policy (); + +if (IS_ENABLED (CONFIG_PV ) ) + { +calculate_pv_max_policy (); +calculate_pv_def_policy (); + } + +if ( hvm_enabled ) + { +calculate_hvm_max_policy (); +calculate_hvm_def_policy (); + } +} +int init_domain_cpu_policy (struct domain *d) {struct cpu_policy *p =is_pv_domain (d)
xen/arch/x86/include/asm/cpu-policy.h +3 -0
@@ -12,6 +12,9 @@ extern struct cpu_policy pv_def_cpu_policy;extern struct cpu_policy hvm_max_cpu_policy;extern struct cpu_policy hvm_def_cpu_policy; +/* Initialise the guest cpu_policy objects. */ +void init_guest_cpu_policies (void ); +/* Allocate and initialise a CPU policy suitable for the domain. */ int init_domain_cpu_policy (struct domain *d);
xen/arch/x86/include/asm/msr.h +0 -1
@@ -418,7 +418,6 @@ struct vcpu_msrsuint32_t dr_mask[4 ]; }; -void init_guest_msr_policy (void );int init_vcpu_msr_policy (struct vcpu *v);/*
xen/arch/x86/msr.c +0 -84
90 lines not on this page · raw message
xen/arch/x86/setup.c +2 -1
@@ -50,6 +50,7 @@#include <asm/nmi.h> #include <asm/alternative.h> #include <asm/mc146818rtc.h> +#include <asm/cpu-policy.h> #include <asm/cpuid.h> #include <asm/spec_ctrl.h> #include <asm/guest.h>
@@ -1991,7 +1992,7 @@ void __init noreturn __start_xen(unsigned long mbi_p)panic ("Could not protect TXT memory regions\n" );init_guest_cpuid (); -init_guest_msr_policy (); +init_guest_cpu_policies ();if ( xen_cpuidle ) xen_processor_pmbits |=XEN_PROCESSOR_PM_CX ;
Signature · 1 line
2.30.2
Re: [PATCH v2 10/15] x86/boot: Move MSR policy initialisation logic into cpu-policy.c
Quoted 5 lines · Andrew Cooper
Switch to the newer
cpu_policynomenclature.No practical change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 11/15 x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c
In reply to Andrew CooperSwitch to the newer cpu_policy nomenclature. Do some easy cleanup of includes.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
--- xen/arch/x86/cpu-policy.c | 752 ++++++++++++++++++++++++ xen/arch/x86/cpuid.c | 817 +------------------------- xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/include/asm/cpu-policy.h | 6 + xen/arch/x86/include/asm/cpuid.h | 11 +- xen/arch/x86/pv/domain.c | 1 + xen/arch/x86/setup.c | 2 - 7 files changed, 764 insertions(+), 826 deletions(-)
xen/arch/x86/cpu-policy.c +752 -0
@@ -1,13 +1,19 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ #include <xen/cache.h> #include <xen/kernel.h> +#include <xen/param.h> #include <xen/sched.h> #include <xen/lib/x86/cpu-policy.h> +#include <asm/amd.h> #include <asm/cpu-policy.h> +#include <asm/hvm/nestedhvm.h> +#include <asm/hvm/svm/svm.h> #include <asm/msr-index.h> +#include <asm/paging.h> #include <asm/setup.h> +#include <asm/xstate.h> struct cpu_policy __ro_after_init raw_cpu_policy;struct cpu_policy __ro_after_init host_cpu_policy;
@@ -20,10 +26,332 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +const uint32_t known_features[] =INIT_KNOWN_FEATURES ; + +static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; +static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; +static const uint32_t __initconst hvm_hap_max_featuremask[] = +INIT_HVM_HAP_MAX_FEATURES ; +static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; +static const uint32_t __initconst hvm_shadow_def_featuremask[] = +INIT_HVM_SHADOW_DEF_FEATURES ; +static const uint32_t __initconst hvm_hap_def_featuremask[] = +INIT_HVM_HAP_DEF_FEATURES ; +static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; + +static const struct feature_name { +const char *name; +unsigned int bit; +} feature_names[] __initconstrel = INIT_FEATURE_NAMES; + +/* +* Parse a list of cpuid feature names -> bool, calling the callback for any +* matches found. +* +* always_inline, because this is init code only and we really don't want a +* function pointer call in the middle of the loop. +*/ +static int __init always_inline parse_cpuid( +const char *s,void (*callback)(unsigned int feat,bool val)) +{ +const char *ss; +int val, rc =0 ; + +do { +const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; +const char *feat; + + ss =strchr (s,',' ); +if ( !ss ) + ss =strchr (s,'\0' ); + +/* Skip the 'no-' prefix for name comparisons. */ + feat = s; +if (strncmp (s,"no-" ,3 ) ==0 ) + feat +=3 ; + +/* (Re)initalise lhs and rhs for binary search. */ + lhs = feature_names; + rhs = feature_names +ARRAY_SIZE (feature_names); + +while ( lhs < rhs ) + { +int res; + + mid = lhs + (rhs - lhs) /2 ; + res =cmdline_strcmp (feat, mid->name); + +if ( res <0 ) + { + rhs = mid; +continue ; + } +if ( res >0 ) + { + lhs = mid +1 ; +continue ; + } + +if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) + { +callback (mid->bit, val); + mid =NULL ; + } + +break ; + } + +/* +* Mid being NULL means that the name and boolean were successfully +* identified. Everything else is an error. +*/ +if ( mid ) + rc = -EINVAL ; + + s = ss +1 ; + }while ( *ss ); + +return rc; +} + +static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) +{ +if ( !val ) +setup_clear_cpu_cap (feat); +else if ( feat ==X86_FEATURE_RDRAND && + (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) +setup_force_cpu_cap (X86_FEATURE_RDRAND ); +} + +static int __init cf_check parse_xen_cpuid(const char *s) +{ +return parse_cpuid (s, _parse_xen_cpuid); +} +custom_param ("cpuid" , parse_xen_cpuid); + +static bool __initdata dom0_cpuid_cmdline; +static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; +static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; + +static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) +{ +__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat); +__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); +} + +static int __init cf_check parse_dom0_cpuid(const char *s) +{ + dom0_cpuid_cmdline = true; + +return parse_cpuid (s, _parse_dom0_cpuid); +} +custom_param ("dom0-cpuid" , parse_dom0_cpuid); + +#define EMPTY_LEAF ((struct cpuid_leaf){}) +static void zero_leaves (struct cpuid_leaf *l, +unsigned int first,unsigned int last) +{ +memset (&l[first],0 ,sizeof (*l) * (last - first +1 )); +} + +static void sanitise_featureset (uint32_t *fs) +{ +/* for_each_set_bit() uses unsigned longs. Extend with zeroes. */ +uint32_t disabled_features[ +ROUNDUP (FSCAPINTS ,sizeof (unsigned long )/sizeof (uint32_t ))] = {}; +unsigned int i; + +for ( i =0 ; i <FSCAPINTS ; ++i ) + { +/* Clamp to known mask. */ + fs[i] &= known_features[i]; + +/* +* Identify which features with deep dependencies have been +* disabled. +*/ + disabled_features[i] = ~fs[i] & deep_features[i]; + } + +for_each_set_bit (i, (void *)disabled_features, +sizeof (disabled_features) *8 ) + { +const uint32_t *dfs =x86_cpuid_lookup_deep_deps (i); +unsigned int j; + +ASSERT (dfs);/* deep_features[] should guarentee this. */ + +for ( j =0 ; j <FSCAPINTS ; ++j ) + { + fs[j] &= ~dfs[j]; + disabled_features[j] &= ~dfs[j]; + } + } +} + +static void recalculate_xstate (struct cpu_policy *p) +{ +uint64_t xstates =XSTATE_FP_SSE ; +uint32_t xstate_size =XSTATE_AREA_MIN_SIZE ; +unsigned int i, Da1 = p->xstate.Da1; + +/* +* The Da1 leaf is the only piece of information preserved in the common +* case. Everything else is derived from other feature state. +*/ +memset (&p->xstate,0 ,sizeof (p->xstate)); + +if ( !p->basic.xsave ) +return ; + +if ( p->basic.avx ) + { + xstates |=X86_XCR0_YMM ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_YMM_POS ] + + xstate_sizes[X86_XCR0_YMM_POS ]); + } + +if ( p->feat.mpx ) + { + xstates |=X86_XCR0_BNDREGS |X86_XCR0_BNDCSR ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_BNDCSR_POS ] + + xstate_sizes[X86_XCR0_BNDCSR_POS ]); + } + +if ( p->feat.avx512f ) + { + xstates |=X86_XCR0_OPMASK |X86_XCR0_ZMM |X86_XCR0_HI_ZMM ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_HI_ZMM_POS ] + + xstate_sizes[X86_XCR0_HI_ZMM_POS ]); + } + +if ( p->feat.pku ) + { + xstates |=X86_XCR0_PKRU ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_PKRU_POS ] + + xstate_sizes[X86_XCR0_PKRU_POS ]); + } + + p->xstate.max_size = xstate_size; + p->xstate.xcr0_low = xstates & ~XSTATE_XSAVES_ONLY ; + p->xstate.xcr0_high = (xstates & ~XSTATE_XSAVES_ONLY ) >>32 ; + + p->xstate.Da1 = Da1; +if ( p->xstate.xsaves ) + { + p->xstate.xss_low = xstates &XSTATE_XSAVES_ONLY ; + p->xstate.xss_high = (xstates &XSTATE_XSAVES_ONLY ) >>32 ; + } +else + xstates &= ~XSTATE_XSAVES_ONLY ; + +for ( i =2 ; i <min (63ul ,ARRAY_SIZE (p->xstate.comp)); ++i ) + { +uint64_t curr_xstate =1ul << i; + +if ( !(xstates & curr_xstate) ) +continue ; + + p->xstate.comp[i].size = xstate_sizes[i]; + p->xstate.comp[i].offset = xstate_offsets[i]; + p->xstate.comp[i].xss = curr_xstate &XSTATE_XSAVES_ONLY ; + p->xstate.comp[i].align = curr_xstate & xstate_align; + } +} + +/* +* Misc adjustments to the policy. Mostly clobbering reserved fields and +* duplicating shared fields. Intentionally hidden fields are annotated. +*/ +static void recalculate_misc (struct cpu_policy *p) +{ + p->basic.raw_fms &=0x0fff0fff ;/* Clobber Processor Type on Intel. */ + p->basic.apic_id =0 ;/* Dynamic. */ + + p->basic.raw[0x5 ] =EMPTY_LEAF ;/* MONITOR not exposed to guests. */ + p->basic.raw[0x6 ] =EMPTY_LEAF ;/* Therm/Power not exposed to guests. */ + + p->basic.raw[0x8 ] =EMPTY_LEAF ; + +/* TODO: Rework topology logic. */ +memset (p->topo.raw,0 ,sizeof (p->topo.raw)); + + p->basic.raw[0xc ] =EMPTY_LEAF ; + + p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ; + +/* Most of Power/RAS hidden from guests. */ + p->extd.raw[0x7 ].a = p->extd.raw[0x7 ].b = p->extd.raw[0x7 ].c =0 ; + + p->extd.raw[0x8 ].d =0 ; + +switch ( p->x86_vendor ) + { +case X86_VENDOR_INTEL : + p->basic.l2_nr_queries =1 ;/* Fixed to 1 query. */ + p->basic.raw[0x3 ] =EMPTY_LEAF ;/* PSN - always hidden. */ + p->basic.raw[0x9 ] =EMPTY_LEAF ;/* DCA - always hidden. */ + + p->extd.vendor_ebx =0 ; + p->extd.vendor_ecx =0 ; + p->extd.vendor_edx =0 ; + + p->extd.raw[0x1 ].a = p->extd.raw[0x1 ].b =0 ; + + p->extd.raw[0x5 ] =EMPTY_LEAF ; + p->extd.raw[0x6 ].a = p->extd.raw[0x6 ].b = p->extd.raw[0x6 ].d =0 ; + + p->extd.raw[0x8 ].a &=0x0000ffff ; + p->extd.raw[0x8 ].c =0 ; +break ; + +case X86_VENDOR_AMD : +case X86_VENDOR_HYGON : +zero_leaves (p->basic.raw,0x2 ,0x3 ); +memset (p->cache.raw,0 ,sizeof (p->cache.raw)); +zero_leaves (p->basic.raw,0x9 ,0xa ); + + p->extd.vendor_ebx = p->basic.vendor_ebx; + p->extd.vendor_ecx = p->basic.vendor_ecx; + p->extd.vendor_edx = p->basic.vendor_edx; + + p->extd.raw_fms = p->basic.raw_fms; + p->extd.raw[0x1 ].b &=0xff00ffff ; + p->extd.e1d |= p->basic._1d &CPUID_COMMON_1D_FEATURES ; + + p->extd.raw[0x8 ].a &=0x0000ffff ;/* GuestMaxPhysAddr hidden. */ + p->extd.raw[0x8 ].c &=0x0003f0ff ; + + p->extd.raw[0x9 ] =EMPTY_LEAF ; + +zero_leaves (p->extd.raw,0xb ,0x18 ); + +/* 0x19 - TLB details. Pass through. */ +/* 0x1a - Perf hints. Pass through. */ + + p->extd.raw[0x1b ] =EMPTY_LEAF ;/* IBS - not supported. */ + p->extd.raw[0x1c ] =EMPTY_LEAF ;/* LWP - not supported. */ + p->extd.raw[0x1d ] =EMPTY_LEAF ;/* TopoExt Cache */ + p->extd.raw[0x1e ] =EMPTY_LEAF ;/* TopoExt APIC ID/Core/Node */ + p->extd.raw[0x1f ] =EMPTY_LEAF ;/* SEV */ + p->extd.raw[0x20 ] =EMPTY_LEAF ;/* Platform QoS */ +break ; + } +} +static void __init calculate_raw_policy (void ) {struct cpu_policy *p = &raw_cpu_policy; +x86_cpuid_policy_fill_native (p); + +/* Nothing good will come from Xen and libx86 disagreeing on vendor. */ +ASSERT (p->x86_vendor == boot_cpu_data.x86_vendor); +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* Was already added by probe_cpuid_faulting() */
@@ -34,9 +362,50 @@ static void __init calculate_raw_policy(void)static void __init calculate_host_policy (void ) {struct cpu_policy *p = &host_cpu_policy; +unsigned int max_extd_leaf; *p = raw_cpu_policy; + p->basic.max_leaf = +min_t (uint32_t, p->basic.max_leaf,ARRAY_SIZE (p->basic.raw) -1 ); + p->feat.max_subleaf = +min_t (uint32_t, p->feat.max_subleaf,ARRAY_SIZE (p->feat.raw) -1 ); + + max_extd_leaf = p->extd.max_leaf; + +/* +* For AMD/Hygon hardware before Zen3, we unilaterally modify LFENCE to be +* dispatch serialising for Spectre mitigations. Extend max_extd_leaf +* beyond what hardware supports, to include the feature leaf containing +* this information. +*/ +if ( cpu_has_lfence_dispatch ) + max_extd_leaf =max (max_extd_leaf,0x80000021 ); + + p->extd.max_leaf =0x80000000 |min_t (uint32_t, max_extd_leaf &0xffff , +ARRAY_SIZE (p->extd.raw) -1 ); + +x86_cpu_featureset_to_policy (boot_cpu_data.x86_capability, p); +recalculate_xstate (p); +recalculate_misc (p); + +/* When vPMU is disabled, drop it from the host policy. */ +if ( vpmu_mode ==XENPMU_MODE_OFF ) + p->basic.raw[0xa ] =EMPTY_LEAF ; + +if ( p->extd.svm ) + { +/* Clamp to implemented features which require hardware support. */ + p->extd.raw[0xa ].d &= ((1u <<SVM_FEATURE_NPT ) | + (1u <<SVM_FEATURE_LBRV ) | + (1u <<SVM_FEATURE_NRIPS ) | + (1u <<SVM_FEATURE_PAUSEFILTER ) | + (1u <<SVM_FEATURE_DECODEASSISTS )); +/* Enable features which are always emulated. */ + p->extd.raw[0xa ].d |= ((1u <<SVM_FEATURE_VMCBCLEAN ) | + (1u <<SVM_FEATURE_TSCRATEMSR )); + } +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */ p->platform_info.cpuid_faulting = cpu_has_cpuid_faulting;
@@ -51,11 +420,88 @@ static void __init calculate_host_policy(void) ARCH_CAPS_PBRSB_NO); } +static void __init guest_common_default_feature_adjustments (uint32_t *fs) +{ +/* +* IvyBridge client parts suffer from leakage of RDRAND data due to SRBDS +* (XSA-320 / CVE-2020-0543), and won't be receiving microcode to +* compensate. +* +* Mitigate by hiding RDRAND from guests by default, unless explicitly +* overridden on the Xen command line (cpuid=rdrand). Irrespective of the +* default setting, guests can use RDRAND if explicitly enabled +* (cpuid="host,rdrand=1") in the VM's config file, and VMs which were +* previously using RDRAND can migrate in. +*/ +if ( boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL && + boot_cpu_data.x86 ==6 && boot_cpu_data.x86_model ==0x3a && + cpu_has_rdrand && !is_forced_cpu_cap (X86_FEATURE_RDRAND ) ) +__clear_bit (X86_FEATURE_RDRAND , fs); + +/* +* On certain hardware, speculative or errata workarounds can result in +* TSX being placed in "force-abort" mode, where it doesn't actually +* function as expected, but is technically compatible with the ISA. +* +* Do not advertise RTM to guests by default if it won't actually work. +*/ +if ( rtm_disabled ) +__clear_bit (X86_FEATURE_RTM , fs); +} + +static void __init guest_common_feature_adjustments (uint32_t *fs) +{ +/* Unconditionally claim to be able to set the hypervisor bit. */ +__set_bit (X86_FEATURE_HYPERVISOR , fs); + +/* +* If IBRS is offered to the guest, unconditionally offer STIBP. It is a +* nop on non-HT hardware, and has this behaviour to make heterogeneous +* setups easier to manage. +*/ +if (test_bit (X86_FEATURE_IBRSB , fs) ) +__set_bit (X86_FEATURE_STIBP , fs); +if (test_bit (X86_FEATURE_IBRS , fs) ) +__set_bit (X86_FEATURE_AMD_STIBP , fs); + +/* +* On hardware which supports IBRS/IBPB, we can offer IBPB independently +* of IBRS by using the AMD feature bit. An administrator may wish for +* performance reasons to offer IBPB without IBRS. +*/ +if ( host_cpu_policy.feat.ibrsb ) +__set_bit (X86_FEATURE_IBPB , fs); +} +static void __init calculate_pv_max_policy (void ) {struct cpu_policy *p = &pv_max_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; *p = host_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= pv_max_featuremask[i]; + +/* +* If Xen isn't virtualising MSR_SPEC_CTRL for PV guests (functional +* availability, or admin choice), hide the feature. +*/ +if ( !boot_cpu_has (X86_FEATURE_SC_MSR_PV ) ) + { +__clear_bit (X86_FEATURE_IBRSB , fs); +__clear_bit (X86_FEATURE_IBRS , fs); + } + +guest_common_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); + + p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */ p->arch_caps.raw =0 ;/* Not supported yet. */ }
@@ -63,15 +509,112 @@ static void __init calculate_pv_max_policy(void)static void __init calculate_pv_def_policy (void ) {struct cpu_policy *p = &pv_def_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; *p = pv_max_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= pv_def_featuremask[i]; + +guest_common_feature_adjustments (fs); +guest_common_default_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); }static void __init calculate_hvm_max_policy (void ) {struct cpu_policy *p = &hvm_max_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; +const uint32_t *mask; *p = host_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + + mask =hvm_hap_supported () ? + hvm_hap_max_featuremask : hvm_shadow_max_featuremask; + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= mask[i]; + +/* +* Xen can provide an (x2)APIC emulation to HVM guests even if the host's +* (x2)APIC isn't enabled. +*/ +__set_bit (X86_FEATURE_APIC , fs); +__set_bit (X86_FEATURE_X2APIC , fs); + +/* +* We don't support EFER.LMSLE at all. AMD has dropped the feature from +* hardware and allocated a CPUID bit to indicate its absence. +*/ +__set_bit (X86_FEATURE_NO_LMSL , fs); + +/* +* On AMD, PV guests are entirely unable to use SYSENTER as Xen runs in +* long mode (and init_amd() has cleared it out of host capabilities), but +* HVM guests are able if running in protected mode. +*/ +if ( (boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) && + raw_cpu_policy.basic.sep ) +__set_bit (X86_FEATURE_SEP , fs); + +/* +* VIRT_SSBD is exposed in the default policy as a result of +* amd_virt_spec_ctrl being set, it also needs exposing in the max policy. +*/ +if ( amd_virt_spec_ctrl ) +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +/* +* If Xen isn't virtualising MSR_SPEC_CTRL for HVM guests (functional +* availability, or admin choice), hide the feature. +*/ +if ( !boot_cpu_has (X86_FEATURE_SC_MSR_HVM ) ) + { +__clear_bit (X86_FEATURE_IBRSB , fs); +__clear_bit (X86_FEATURE_IBRS , fs); + } +else if (boot_cpu_has (X86_FEATURE_AMD_SSBD ) ) +/* +* If SPEC_CTRL.SSBD is available VIRT_SPEC_CTRL.SSBD can be exposed +* and implemented using the former. Expose in the max policy only as +* the preference is for guests to use SPEC_CTRL.SSBD if available. +*/ +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +/* +* With VT-x, some features are only supported by Xen if dedicated +* hardware support is also available. +*/ +if ( cpu_has_vmx ) + { +if ( !cpu_has_vmx_mpx ) +__clear_bit (X86_FEATURE_MPX , fs); + +if ( !cpu_has_vmx_xsaves ) +__clear_bit (X86_FEATURE_XSAVES , fs); + } + +/* +* Xen doesn't use PKS, so the guest support for it has opted to not use +* the VMCS load/save controls for efficiency reasons. This depends on +* the exact vmentry/exit behaviour, so don't expose PKS in other +* situations until someone has cross-checked the behaviour for safety. +*/ +if ( !cpu_has_vmx ) +__clear_bit (X86_FEATURE_PKS , fs); + +guest_common_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p);/* It's always possible to emulate CPUID faulting for HVM guests */ p->platform_info.cpuid_faulting = true;
@@ -82,8 +625,32 @@ static void __init calculate_hvm_max_policy(void)static void __init calculate_hvm_def_policy (void ) {struct cpu_policy *p = &hvm_def_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; +const uint32_t *mask; *p = hvm_max_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + + mask =hvm_hap_supported () ? + hvm_hap_def_featuremask : hvm_shadow_def_featuremask; + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= mask[i]; + +guest_common_feature_adjustments (fs); +guest_common_default_feature_adjustments (fs); + +/* +* Only expose VIRT_SSBD if AMD_SSBD is not available, and thus +* amd_virt_spec_ctrl is set. +*/ +if ( amd_virt_spec_ctrl ) +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); }void __init init_guest_cpu_policies (void )
@@ -149,3 +716,188 @@ int init_domain_cpu_policy(struct domain *d)return 0 ; } + +void recalculate_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; +const struct cpu_policy *max =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL ); +uint32_t fs[FSCAPINTS ], max_fs[FSCAPINTS ]; +unsigned int i; + +if ( !max ) + { +ASSERT_UNREACHABLE (); +return ; + } + + p->x86_vendor =x86_cpuid_lookup_vendor ( + p->basic.vendor_ebx, p->basic.vendor_ecx, p->basic.vendor_edx); + + p->basic.max_leaf =min (p->basic.max_leaf, max->basic.max_leaf); + p->feat.max_subleaf =min (p->feat.max_subleaf, max->feat.max_subleaf); + p->extd.max_leaf =0x80000000 |min (p->extd.max_leaf &0xffff , + ((p->x86_vendor & (X86_VENDOR_AMD | +X86_VENDOR_HYGON )) + ?CPUID_GUEST_NR_EXTD_AMD + :CPUID_GUEST_NR_EXTD_INTEL ) -1 ); + +x86_cpu_policy_to_featureset (p, fs); +x86_cpu_policy_to_featureset (max, max_fs); + +if (is_hvm_domain (d) ) + { +/* +* HVM domains using Shadow paging have further restrictions on their +* available paging features. +*/ +if ( !hap_enabled (d) ) + { +for ( i =0 ; i <ARRAY_SIZE (max_fs); i++ ) + max_fs[i] &= hvm_shadow_max_featuremask[i]; + } + +/* Hide nested-virt if it hasn't been explicitly configured. */ +if ( !nestedhvm_enabled (d) ) + { +__clear_bit (X86_FEATURE_VMX , max_fs); +__clear_bit (X86_FEATURE_SVM , max_fs); + } + } + +/* +* Allow the toolstack to set HTT, X2APIC and CMP_LEGACY. These bits +* affect how to interpret topology information in other cpuid leaves. +*/ +__set_bit (X86_FEATURE_HTT , max_fs); +__set_bit (X86_FEATURE_X2APIC , max_fs); +__set_bit (X86_FEATURE_CMP_LEGACY , max_fs); + +/* +* 32bit PV domains can't use any Long Mode features, and cannot use +* SYSCALL on non-AMD hardware. +*/ +if (is_pv_32bit_domain (d) ) + { +__clear_bit (X86_FEATURE_LM , max_fs); +if ( !(boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) ) +__clear_bit (X86_FEATURE_SYSCALL , max_fs); + } + +/* Clamp the toolstacks choices to reality. */ +for ( i =0 ; i <ARRAY_SIZE (fs); i++ ) + fs[i] &= max_fs[i]; + +if ( p->basic.max_leaf <XSTATE_CPUID ) +__clear_bit (X86_FEATURE_XSAVE , fs); + +sanitise_featureset (fs); + +/* Fold host's FDP_EXCP_ONLY and NO_FPU_SEL into guest's view. */ + fs[FEATURESET_7b0] &= ~(cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | +cpufeat_mask (X86_FEATURE_NO_FPU_SEL )); + fs[FEATURESET_7b0] |= (host_cpu_policy.feat._7b0 & + (cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | +cpufeat_mask (X86_FEATURE_NO_FPU_SEL ))); + +x86_cpu_featureset_to_policy (fs, p); + +/* Pass host cacheline size through to guests. */ + p->basic.clflush_size = max->basic.clflush_size; + + p->extd.maxphysaddr =min (p->extd.maxphysaddr, max->extd.maxphysaddr); + p->extd.maxphysaddr =min_t (uint8_t, p->extd.maxphysaddr, +paging_max_paddr_bits (d)); + p->extd.maxphysaddr =max_t (uint8_t, p->extd.maxphysaddr, + (p->basic.pae || p->basic.pse36) ?36 :32 ); + + p->extd.maxlinaddr = p->extd.lm ?48 :32 ; + +recalculate_xstate (p); +recalculate_misc (p); + +for ( i =0 ; i <ARRAY_SIZE (p->cache.raw); ++i ) + { +if ( p->cache.subleaf[i].type >=1 && + p->cache.subleaf[i].type <=3 ) + { +/* Subleaf has a valid cache type. Zero reserved fields. */ + p->cache.raw[i].a &=0xffffc3ffu ; + p->cache.raw[i].d &=0x00000007u ; + } +else + { +/* Subleaf is not valid. Zero the rest of the union. */ +zero_leaves (p->cache.raw, i,ARRAY_SIZE (p->cache.raw) -1 ); +break ; + } + } + +if ( vpmu_mode ==XENPMU_MODE_OFF || + ((vpmu_mode &XENPMU_MODE_ALL ) && !is_hardware_domain (d)) ) + p->basic.raw[0xa ] =EMPTY_LEAF ; + +if ( !p->extd.svm ) + p->extd.raw[0xa ] =EMPTY_LEAF ; + +if ( !p->extd.page1gb ) + p->extd.raw[0x19 ] =EMPTY_LEAF ; +} + +void __init init_dom0_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; + +/* dom0 can't migrate. Give it ITSC if available. */ +if ( cpu_has_itsc ) + p->extd.itsc = true; + +/* +* Expose the "hardware speculation behaviour" bits of ARCH_CAPS to dom0, +* so dom0 can turn off workarounds as appropriate. Temporary, until the +* domain policy logic gains a better understanding of MSRs. +*/ +if ( cpu_has_arch_caps ) + p->feat.arch_caps = true; + +/* Apply dom0-cpuid= command line settings, if provided. */ +if ( dom0_cpuid_cmdline ) + { +uint32_t fs[FSCAPINTS ]; +unsigned int i; + +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + { + fs[i] |= dom0_enable_feat [i]; + fs[i] &= ~dom0_disable_feat[i]; + } + +x86_cpu_featureset_to_policy (fs, p); + +recalculate_cpuid_policy (d); + } +} + +static void __init __maybe_unused build_assertions (void ) +{ +BUILD_BUG_ON (ARRAY_SIZE (known_features) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (pv_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (hvm_shadow_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (hvm_hap_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (deep_features) !=FSCAPINTS ); + +/* Find some more clever allocation scheme if this trips. */ +BUILD_BUG_ON (sizeof (struct cpu_policy ) >PAGE_SIZE ); + +BUILD_BUG_ON (sizeof (raw_cpu_policy.basic) != +sizeof (raw_cpu_policy.basic.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.feat) != +sizeof (raw_cpu_policy.feat.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.xstate) != +sizeof (raw_cpu_policy.xstate.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.extd) != +sizeof (raw_cpu_policy.extd.raw)); +}
xen/arch/x86/cpuid.c +4 -813
839 lines not on this page · raw message
xen/arch/x86/hvm/hvm.c +0 -1
@@ -77,7 +77,6 @@#include <public/memory.h> #include <public/vm_event.h> #include <public/arch-x86/cpuid.h> -#include <asm/cpuid.h> #include <compat/hvm/hvm_op.h>
xen/arch/x86/include/asm/cpu-policy.h +6 -0
@@ -18,4 +18,10 @@ void init_guest_cpu_policies(void);/* Allocate and initialise a CPU policy suitable for the domain. */ int init_domain_cpu_policy (struct domain *d); +/* Apply dom0-specific tweaks to the CPUID policy. */ +void init_dom0_cpuid_policy (struct domain *d); + +/* Clamp the CPUID policy to reality. */ +void recalculate_cpuid_policy (struct domain *d); +#endif /* X86_CPU_POLICY_H */
xen/arch/x86/include/asm/cpuid.h +1 -10
@@ -8,14 +8,10 @@#include <xen/kernel.h> #include <xen/percpu.h> -#include <xen/lib/x86/cpu-policy.h> -#include <public/sysctl.h> extern const uint32_t known_features[FSCAPINTS ]; -void init_guest_cpuid (void ); -/* * Expected levelling capabilities (given cpuid vendor/family information), * and levelling capabilities actually available (given MSR probing).
@@ -49,13 +45,8 @@ extern struct cpuidmasks cpuidmask_defaults;/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu); -/* Apply dom0-specific tweaks to the CPUID policy. */ -void init_dom0_cpuid_policy (struct domain *d); - -/* Clamp the CPUID policy to reality. */ -void recalculate_cpuid_policy (struct domain *d); -struct vcpu ; +struct cpuid_leaf ;void guest_cpuid (const struct vcpu *v,uint32_t leaf,uint32_t subleaf,struct cpuid_leaf *res);
xen/arch/x86/pv/domain.c +1 -0
@@ -10,6 +10,7 @@#include <xen/param.h> #include <xen/sched.h> +#include <asm/cpu-policy.h> #include <asm/cpufeature.h> #include <asm/invpcid.h> #include <asm/spec_ctrl.h>
xen/arch/x86/setup.c +0 -2
@@ -51,7 +51,6 @@#include <asm/alternative.h> #include <asm/mc146818rtc.h> #include <asm/cpu-policy.h> -#include <asm/cpuid.h> #include <asm/spec_ctrl.h> #include <asm/guest.h> #include <asm/microcode.h>
@@ -1991,7 +1990,6 @@ void __init noreturn __start_xen(unsigned long mbi_p)if ( !tboot_protect_mem_regions () )panic ("Could not protect TXT memory regions\n" ); -init_guest_cpuid ();init_guest_cpu_policies ();if ( xen_cpuidle )
Signature · 1 line
2.30.2
Re: [PATCH v2 11/15] x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c
Quoted 161 lines · Andrew Cooper
Switch to the newer
cpu_policynomenclature. Do some easy cleanup of includes.No practical change.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>v2:
* New--- xen/arch/x86/cpu-policy.c | 752 ++++++++++++++++++++++++ xen/arch/x86/cpuid.c | 817 +------------------------- xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/include/asm/cpu-policy.h | 6 + xen/arch/x86/include/asm/cpuid.h | 11 +- xen/arch/x86/pv/domain.c | 1 + xen/arch/x86/setup.c | 2 - 7 files changed, 764 insertions(+), 826 deletions(-)xen/arch/x86/cpu-policy.c +116
-0@@ -1,13 +1,19 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ #include <xen/cache.h> #include <xen/kernel.h> +#include <xen/param.h> #include <xen/sched.h> #include <xen/lib/x86/cpu-policy.h> +#include <asm/amd.h> #include <asm/cpu-policy.h> +#include <asm/hvm/nestedhvm.h> +#include <asm/hvm/svm/svm.h> #include <asm/msr-index.h> +#include <asm/paging.h> #include <asm/setup.h> +#include <asm/xstate.h> struct cpu_policy __ro_after_init raw_cpu_policy;struct cpu_policy __ro_after_init host_cpu_policy;@@ -20,10 +26,332 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +const uint32_t known_features[] =INIT_KNOWN_FEATURES ; + +static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; +static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; +static const uint32_t __initconst hvm_hap_max_featuremask[] = +INIT_HVM_HAP_MAX_FEATURES ; +static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; +static const uint32_t __initconst hvm_shadow_def_featuremask[] = +INIT_HVM_SHADOW_DEF_FEATURES ; +static const uint32_t __initconst hvm_hap_def_featuremask[] = +INIT_HVM_HAP_DEF_FEATURES ; +static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; + +static const struct feature_name { +const char *name; +unsigned int bit; +} feature_names[] __initconstrel = INIT_FEATURE_NAMES; + +/* +* Parse a list of cpuid feature names -> bool, calling the callback for any +* matches found. +* +* always_inline, because this is init code only and we really don't want a +* function pointer call in the middle of the loop. +*/ +static int __init always_inline parse_cpuid( +const char *s,void (*callback)(unsigned int feat,bool val)) +{ +const char *ss; +int val, rc =0 ; + +do { +const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; +const char *feat; + + ss =strchr (s,',' ); +if ( !ss ) + ss =strchr (s,'\0' ); + +/* Skip the 'no-' prefix for name comparisons. */ + feat = s; +if (strncmp (s,"no-" ,3 ) ==0 ) + feat +=3 ; + +/* (Re)initalise lhs and rhs for binary search. */ + lhs = feature_names; + rhs = feature_names +ARRAY_SIZE (feature_names); + +while ( lhs < rhs ) + { +int res; + + mid = lhs + (rhs - lhs) /2 ; + res =cmdline_strcmp (feat, mid->name); + +if ( res <0 ) + { + rhs = mid; +continue ; + } +if ( res >0 ) + { + lhs = mid +1 ; +continue ; + } + +if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) + { +callback (mid->bit, val); + mid =NULL ; + } + +break ; + } + +/* +* Mid being NULL means that the name and boolean were successfully +* identified. Everything else is an error. +*/ +if ( mid ) + rc = -EINVAL ; + + s = ss +1 ; + }while ( *ss ); + +return rc; +} + +static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) +{ +if ( !val ) +setup_clear_cpu_cap (feat); +else if ( feat ==X86_FEATURE_RDRAND && + (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) +setup_force_cpu_cap (X86_FEATURE_RDRAND ); +} + +static int __init cf_check parse_xen_cpuid(const char *s) +{ +return parse_cpuid (s, _parse_xen_cpuid); +} +custom_param ("cpuid" , parse_xen_cpuid); + +static bool __initdata dom0_cpuid_cmdline; +static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; +static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; + +static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) +{ +__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat);
xen/arch/x86/cpu-policy.c struct cpu_policy __ro_after_init hvm_max_cpu_policy; · line 148
Quoted 10 lines
xen/arch/x86/cpu-policy.c +10
-0+__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); +} + +static int __init cf_check parse_dom0_cpuid(const char *s) +{ + dom0_cpuid_cmdline = true; + +return parse_cpuid (s, _parse_dom0_cpuid); +} +custom_param ("dom0-cpuid" , parse_dom0_cpuid);
Unless the plan is to completely remove cpuid.c, this command line handling would imo better fit there. I understand that to keep dom0_{en,dis}able_feat[] static, the _parse_dom0_cpuid() helper would then need to be exposed (under a different name), but I think that's quite okay, the more that it's an __init function.
Quoted 495 lines
xen/arch/x86/cpu-policy.c +440
-0+#define EMPTY_LEAF ((struct cpuid_leaf){}) +static void zero_leaves (struct cpuid_leaf *l, +unsigned int first,unsigned int last) +{ +memset (&l[first],0 ,sizeof (*l) * (last - first +1 )); +} + +static void sanitise_featureset (uint32_t *fs) +{ +/* for_each_set_bit() uses unsigned longs. Extend with zeroes. */ +uint32_t disabled_features[ +ROUNDUP (FSCAPINTS ,sizeof (unsigned long )/sizeof (uint32_t ))] = {}; +unsigned int i; + +for ( i =0 ; i <FSCAPINTS ; ++i ) + { +/* Clamp to known mask. */ + fs[i] &= known_features[i]; + +/* +* Identify which features with deep dependencies have been +* disabled. +*/ + disabled_features[i] = ~fs[i] & deep_features[i]; + } + +for_each_set_bit (i, (void *)disabled_features, +sizeof (disabled_features) *8 ) + { +const uint32_t *dfs =x86_cpuid_lookup_deep_deps (i); +unsigned int j; + +ASSERT (dfs);/* deep_features[] should guarentee this. */ + +for ( j =0 ; j <FSCAPINTS ; ++j ) + { + fs[j] &= ~dfs[j]; + disabled_features[j] &= ~dfs[j]; + } + } +} + +static void recalculate_xstate (struct cpu_policy *p) +{ +uint64_t xstates =XSTATE_FP_SSE ; +uint32_t xstate_size =XSTATE_AREA_MIN_SIZE ; +unsigned int i, Da1 = p->xstate.Da1; + +/* +* The Da1 leaf is the only piece of information preserved in the common +* case. Everything else is derived from other feature state. +*/ +memset (&p->xstate,0 ,sizeof (p->xstate)); + +if ( !p->basic.xsave ) +return ; + +if ( p->basic.avx ) + { + xstates |=X86_XCR0_YMM ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_YMM_POS ] + + xstate_sizes[X86_XCR0_YMM_POS ]); + } + +if ( p->feat.mpx ) + { + xstates |=X86_XCR0_BNDREGS |X86_XCR0_BNDCSR ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_BNDCSR_POS ] + + xstate_sizes[X86_XCR0_BNDCSR_POS ]); + } + +if ( p->feat.avx512f ) + { + xstates |=X86_XCR0_OPMASK |X86_XCR0_ZMM |X86_XCR0_HI_ZMM ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_HI_ZMM_POS ] + + xstate_sizes[X86_XCR0_HI_ZMM_POS ]); + } + +if ( p->feat.pku ) + { + xstates |=X86_XCR0_PKRU ; + xstate_size =max (xstate_size, + xstate_offsets[X86_XCR0_PKRU_POS ] + + xstate_sizes[X86_XCR0_PKRU_POS ]); + } + + p->xstate.max_size = xstate_size; + p->xstate.xcr0_low = xstates & ~XSTATE_XSAVES_ONLY ; + p->xstate.xcr0_high = (xstates & ~XSTATE_XSAVES_ONLY ) >>32 ; + + p->xstate.Da1 = Da1; +if ( p->xstate.xsaves ) + { + p->xstate.xss_low = xstates &XSTATE_XSAVES_ONLY ; + p->xstate.xss_high = (xstates &XSTATE_XSAVES_ONLY ) >>32 ; + } +else + xstates &= ~XSTATE_XSAVES_ONLY ; + +for ( i =2 ; i <min (63ul ,ARRAY_SIZE (p->xstate.comp)); ++i ) + { +uint64_t curr_xstate =1ul << i; + +if ( !(xstates & curr_xstate) ) +continue ; + + p->xstate.comp[i].size = xstate_sizes[i]; + p->xstate.comp[i].offset = xstate_offsets[i]; + p->xstate.comp[i].xss = curr_xstate &XSTATE_XSAVES_ONLY ; + p->xstate.comp[i].align = curr_xstate & xstate_align; + } +} + +/* +* Misc adjustments to the policy. Mostly clobbering reserved fields and +* duplicating shared fields. Intentionally hidden fields are annotated. +*/ +static void recalculate_misc (struct cpu_policy *p) +{ + p->basic.raw_fms &=0x0fff0fff ;/* Clobber Processor Type on Intel. */ + p->basic.apic_id =0 ;/* Dynamic. */ + + p->basic.raw[0x5 ] =EMPTY_LEAF ;/* MONITOR not exposed to guests. */ + p->basic.raw[0x6 ] =EMPTY_LEAF ;/* Therm/Power not exposed to guests. */ + + p->basic.raw[0x8 ] =EMPTY_LEAF ; + +/* TODO: Rework topology logic. */ +memset (p->topo.raw,0 ,sizeof (p->topo.raw)); + + p->basic.raw[0xc ] =EMPTY_LEAF ; + + p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ; + +/* Most of Power/RAS hidden from guests. */ + p->extd.raw[0x7 ].a = p->extd.raw[0x7 ].b = p->extd.raw[0x7 ].c =0 ; + + p->extd.raw[0x8 ].d =0 ; + +switch ( p->x86_vendor ) + { +case X86_VENDOR_INTEL : + p->basic.l2_nr_queries =1 ;/* Fixed to 1 query. */ + p->basic.raw[0x3 ] =EMPTY_LEAF ;/* PSN - always hidden. */ + p->basic.raw[0x9 ] =EMPTY_LEAF ;/* DCA - always hidden. */ + + p->extd.vendor_ebx =0 ; + p->extd.vendor_ecx =0 ; + p->extd.vendor_edx =0 ; + + p->extd.raw[0x1 ].a = p->extd.raw[0x1 ].b =0 ; + + p->extd.raw[0x5 ] =EMPTY_LEAF ; + p->extd.raw[0x6 ].a = p->extd.raw[0x6 ].b = p->extd.raw[0x6 ].d =0 ; + + p->extd.raw[0x8 ].a &=0x0000ffff ; + p->extd.raw[0x8 ].c =0 ; +break ; + +case X86_VENDOR_AMD : +case X86_VENDOR_HYGON : +zero_leaves (p->basic.raw,0x2 ,0x3 ); +memset (p->cache.raw,0 ,sizeof (p->cache.raw)); +zero_leaves (p->basic.raw,0x9 ,0xa ); + + p->extd.vendor_ebx = p->basic.vendor_ebx; + p->extd.vendor_ecx = p->basic.vendor_ecx; + p->extd.vendor_edx = p->basic.vendor_edx; + + p->extd.raw_fms = p->basic.raw_fms; + p->extd.raw[0x1 ].b &=0xff00ffff ; + p->extd.e1d |= p->basic._1d &CPUID_COMMON_1D_FEATURES ; + + p->extd.raw[0x8 ].a &=0x0000ffff ;/* GuestMaxPhysAddr hidden. */ + p->extd.raw[0x8 ].c &=0x0003f0ff ; + + p->extd.raw[0x9 ] =EMPTY_LEAF ; + +zero_leaves (p->extd.raw,0xb ,0x18 ); + +/* 0x19 - TLB details. Pass through. */ +/* 0x1a - Perf hints. Pass through. */ + + p->extd.raw[0x1b ] =EMPTY_LEAF ;/* IBS - not supported. */ + p->extd.raw[0x1c ] =EMPTY_LEAF ;/* LWP - not supported. */ + p->extd.raw[0x1d ] =EMPTY_LEAF ;/* TopoExt Cache */ + p->extd.raw[0x1e ] =EMPTY_LEAF ;/* TopoExt APIC ID/Core/Node */ + p->extd.raw[0x1f ] =EMPTY_LEAF ;/* SEV */ + p->extd.raw[0x20 ] =EMPTY_LEAF ;/* Platform QoS */ +break ; + } +} +static void __init calculate_raw_policy (void ) {struct cpu_policy *p = &raw_cpu_policy; +x86_cpuid_policy_fill_native (p); + +/* Nothing good will come from Xen and libx86 disagreeing on vendor. */ +ASSERT (p->x86_vendor == boot_cpu_data.x86_vendor); +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* Was already added by probe_cpuid_faulting() */ @@ -34,9 +362,50 @@ static void __init calculate_raw_policy(void)static void __init calculate_host_policy (void ) {struct cpu_policy *p = &host_cpu_policy; +unsigned int max_extd_leaf; *p = raw_cpu_policy; + p->basic.max_leaf = +min_t (uint32_t, p->basic.max_leaf,ARRAY_SIZE (p->basic.raw) -1 ); + p->feat.max_subleaf = +min_t (uint32_t, p->feat.max_subleaf,ARRAY_SIZE (p->feat.raw) -1 ); + + max_extd_leaf = p->extd.max_leaf; + +/* +* For AMD/Hygon hardware before Zen3, we unilaterally modify LFENCE to be +* dispatch serialising for Spectre mitigations. Extend max_extd_leaf +* beyond what hardware supports, to include the feature leaf containing +* this information. +*/ +if ( cpu_has_lfence_dispatch ) + max_extd_leaf =max (max_extd_leaf,0x80000021 ); + + p->extd.max_leaf =0x80000000 |min_t (uint32_t, max_extd_leaf &0xffff , +ARRAY_SIZE (p->extd.raw) -1 ); + +x86_cpu_featureset_to_policy (boot_cpu_data.x86_capability, p); +recalculate_xstate (p); +recalculate_misc (p); + +/* When vPMU is disabled, drop it from the host policy. */ +if ( vpmu_mode ==XENPMU_MODE_OFF ) + p->basic.raw[0xa ] =EMPTY_LEAF ; + +if ( p->extd.svm ) + { +/* Clamp to implemented features which require hardware support. */ + p->extd.raw[0xa ].d &= ((1u <<SVM_FEATURE_NPT ) | + (1u <<SVM_FEATURE_LBRV ) | + (1u <<SVM_FEATURE_NRIPS ) | + (1u <<SVM_FEATURE_PAUSEFILTER ) | + (1u <<SVM_FEATURE_DECODEASSISTS )); +/* Enable features which are always emulated. */ + p->extd.raw[0xa ].d |= ((1u <<SVM_FEATURE_VMCBCLEAN ) | + (1u <<SVM_FEATURE_TSCRATEMSR )); + } +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */ p->platform_info.cpuid_faulting = cpu_has_cpuid_faulting;@@ -51,11 +420,88 @@ static void __init calculate_host_policy(void) ARCH_CAPS_PBRSB_NO); } +static void __init guest_common_default_feature_adjustments (uint32_t *fs) +{ +/* +* IvyBridge client parts suffer from leakage of RDRAND data due to SRBDS +* (XSA-320 / CVE-2020-0543), and won't be receiving microcode to +* compensate. +* +* Mitigate by hiding RDRAND from guests by default, unless explicitly +* overridden on the Xen command line (cpuid=rdrand). Irrespective of the +* default setting, guests can use RDRAND if explicitly enabled +* (cpuid="host,rdrand=1") in the VM's config file, and VMs which were +* previously using RDRAND can migrate in. +*/ +if ( boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL && + boot_cpu_data.x86 ==6 && boot_cpu_data.x86_model ==0x3a && + cpu_has_rdrand && !is_forced_cpu_cap (X86_FEATURE_RDRAND ) ) +__clear_bit (X86_FEATURE_RDRAND , fs); + +/* +* On certain hardware, speculative or errata workarounds can result in +* TSX being placed in "force-abort" mode, where it doesn't actually +* function as expected, but is technically compatible with the ISA. +* +* Do not advertise RTM to guests by default if it won't actually work. +*/ +if ( rtm_disabled ) +__clear_bit (X86_FEATURE_RTM , fs); +} + +static void __init guest_common_feature_adjustments (uint32_t *fs) +{ +/* Unconditionally claim to be able to set the hypervisor bit. */ +__set_bit (X86_FEATURE_HYPERVISOR , fs); + +/* +* If IBRS is offered to the guest, unconditionally offer STIBP. It is a +* nop on non-HT hardware, and has this behaviour to make heterogeneous +* setups easier to manage. +*/ +if (test_bit (X86_FEATURE_IBRSB , fs) ) +__set_bit (X86_FEATURE_STIBP , fs); +if (test_bit (X86_FEATURE_IBRS , fs) ) +__set_bit (X86_FEATURE_AMD_STIBP , fs); + +/* +* On hardware which supports IBRS/IBPB, we can offer IBPB independently +* of IBRS by using the AMD feature bit. An administrator may wish for +* performance reasons to offer IBPB without IBRS. +*/ +if ( host_cpu_policy.feat.ibrsb ) +__set_bit (X86_FEATURE_IBPB , fs); +} +static void __init calculate_pv_max_policy (void ) {struct cpu_policy *p = &pv_max_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; *p = host_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= pv_max_featuremask[i]; + +/* +* If Xen isn't virtualising MSR_SPEC_CTRL for PV guests (functional +* availability, or admin choice), hide the feature. +*/ +if ( !boot_cpu_has (X86_FEATURE_SC_MSR_PV ) ) + { +__clear_bit (X86_FEATURE_IBRSB , fs); +__clear_bit (X86_FEATURE_IBRS , fs); + } + +guest_common_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); + + p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */ p->arch_caps.raw =0 ;/* Not supported yet. */ }@@ -63,15 +509,112 @@ static void __init calculate_pv_max_policy(void)static void __init calculate_pv_def_policy (void ) {struct cpu_policy *p = &pv_def_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; *p = pv_max_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= pv_def_featuremask[i]; + +guest_common_feature_adjustments (fs); +guest_common_default_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); }static void __init calculate_hvm_max_policy (void ) {struct cpu_policy *p = &hvm_max_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; +const uint32_t *mask; *p = host_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + + mask =hvm_hap_supported () ? + hvm_hap_max_featuremask : hvm_shadow_max_featuremask; + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= mask[i]; + +/* +* Xen can provide an (x2)APIC emulation to HVM guests even if the host's +* (x2)APIC isn't enabled. +*/ +__set_bit (X86_FEATURE_APIC , fs); +__set_bit (X86_FEATURE_X2APIC , fs); + +/* +* We don't support EFER.LMSLE at all. AMD has dropped the feature from +* hardware and allocated a CPUID bit to indicate its absence. +*/ +__set_bit (X86_FEATURE_NO_LMSL , fs); + +/* +* On AMD, PV guests are entirely unable to use SYSENTER as Xen runs in +* long mode (and init_amd() has cleared it out of host capabilities), but +* HVM guests are able if running in protected mode. +*/ +if ( (boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) && + raw_cpu_policy.basic.sep ) +__set_bit (X86_FEATURE_SEP , fs); + +/* +* VIRT_SSBD is exposed in the default policy as a result of +* amd_virt_spec_ctrl being set, it also needs exposing in the max policy. +*/ +if ( amd_virt_spec_ctrl ) +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +/* +* If Xen isn't virtualising MSR_SPEC_CTRL for HVM guests (functional +* availability, or admin choice), hide the feature. +*/ +if ( !boot_cpu_has (X86_FEATURE_SC_MSR_HVM ) ) + { +__clear_bit (X86_FEATURE_IBRSB , fs); +__clear_bit (X86_FEATURE_IBRS , fs); + } +else if (boot_cpu_has (X86_FEATURE_AMD_SSBD ) ) +/* +* If SPEC_CTRL.SSBD is available VIRT_SPEC_CTRL.SSBD can be exposed +* and implemented using the former. Expose in the max policy only as +* the preference is for guests to use SPEC_CTRL.SSBD if available. +*/ +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +/* +* With VT-x, some features are only supported by Xen if dedicated +* hardware support is also available. +*/ +if ( cpu_has_vmx ) + { +if ( !cpu_has_vmx_mpx ) +__clear_bit (X86_FEATURE_MPX , fs); + +if ( !cpu_has_vmx_xsaves ) +__clear_bit (X86_FEATURE_XSAVES , fs); + } + +/* +* Xen doesn't use PKS, so the guest support for it has opted to not use +* the VMCS load/save controls for efficiency reasons. This depends on +* the exact vmentry/exit behaviour, so don't expose PKS in other +* situations until someone has cross-checked the behaviour for safety. +*/ +if ( !cpu_has_vmx ) +__clear_bit (X86_FEATURE_PKS , fs); + +guest_common_feature_adjustments (fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p);/* It's always possible to emulate CPUID faulting for HVM guests */ p->platform_info.cpuid_faulting = true;@@ -82,8 +625,32 @@ static void __init calculate_hvm_max_policy(void)static void __init calculate_hvm_def_policy (void ) {struct cpu_policy *p = &hvm_def_cpu_policy; +uint32_t fs[FSCAPINTS ]; +unsigned int i; +const uint32_t *mask; *p = hvm_max_cpu_policy; +x86_cpu_policy_to_featureset (p, fs); + + mask =hvm_hap_supported () ? + hvm_hap_def_featuremask : hvm_shadow_def_featuremask; + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + fs[i] &= mask[i]; + +guest_common_feature_adjustments (fs); +guest_common_default_feature_adjustments (fs); + +/* +* Only expose VIRT_SSBD if AMD_SSBD is not available, and thus +* amd_virt_spec_ctrl is set. +*/ +if ( amd_virt_spec_ctrl ) +__set_bit (X86_FEATURE_VIRT_SSBD , fs); + +sanitise_featureset (fs); +x86_cpu_featureset_to_policy (fs, p); +recalculate_xstate (p); }void __init init_guest_cpu_policies (void )
xen/arch/x86/cpu-policy.c int init_domain_cpu_policy(struct domain *d) · line 725
Quoted 10 lines
xen/arch/x86/cpu-policy.c +7
-0@@ -149,3 +716,188 @@ int init_domain_cpu_policy(struct domain *d)return 0 ; } + +void recalculate_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; +const struct cpu_policy *max =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL );
While this is how the original code was, wouldn't this want to use hvm_enabled, just like init_guest_cpu_policies() does (patch 10)?
Quoted 1,117 lines
xen/arch/x86/cpu-policy.c +178
-0+uint32_t fs[FSCAPINTS ], max_fs[FSCAPINTS ]; +unsigned int i; + +if ( !max ) + { +ASSERT_UNREACHABLE (); +return ; + } + + p->x86_vendor =x86_cpuid_lookup_vendor ( + p->basic.vendor_ebx, p->basic.vendor_ecx, p->basic.vendor_edx); + + p->basic.max_leaf =min (p->basic.max_leaf, max->basic.max_leaf); + p->feat.max_subleaf =min (p->feat.max_subleaf, max->feat.max_subleaf); + p->extd.max_leaf =0x80000000 |min (p->extd.max_leaf &0xffff , + ((p->x86_vendor & (X86_VENDOR_AMD | +X86_VENDOR_HYGON )) + ?CPUID_GUEST_NR_EXTD_AMD + :CPUID_GUEST_NR_EXTD_INTEL ) -1 ); + +x86_cpu_policy_to_featureset (p, fs); +x86_cpu_policy_to_featureset (max, max_fs); + +if (is_hvm_domain (d) ) + { +/* +* HVM domains using Shadow paging have further restrictions on their +* available paging features. +*/ +if ( !hap_enabled (d) ) + { +for ( i =0 ; i <ARRAY_SIZE (max_fs); i++ ) + max_fs[i] &= hvm_shadow_max_featuremask[i]; + } + +/* Hide nested-virt if it hasn't been explicitly configured. */ +if ( !nestedhvm_enabled (d) ) + { +__clear_bit (X86_FEATURE_VMX , max_fs); +__clear_bit (X86_FEATURE_SVM , max_fs); + } + } + +/* +* Allow the toolstack to set HTT, X2APIC and CMP_LEGACY. These bits +* affect how to interpret topology information in other cpuid leaves. +*/ +__set_bit (X86_FEATURE_HTT , max_fs); +__set_bit (X86_FEATURE_X2APIC , max_fs); +__set_bit (X86_FEATURE_CMP_LEGACY , max_fs); + +/* +* 32bit PV domains can't use any Long Mode features, and cannot use +* SYSCALL on non-AMD hardware. +*/ +if (is_pv_32bit_domain (d) ) + { +__clear_bit (X86_FEATURE_LM , max_fs); +if ( !(boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) ) +__clear_bit (X86_FEATURE_SYSCALL , max_fs); + } + +/* Clamp the toolstacks choices to reality. */ +for ( i =0 ; i <ARRAY_SIZE (fs); i++ ) + fs[i] &= max_fs[i]; + +if ( p->basic.max_leaf <XSTATE_CPUID ) +__clear_bit (X86_FEATURE_XSAVE , fs); + +sanitise_featureset (fs); + +/* Fold host's FDP_EXCP_ONLY and NO_FPU_SEL into guest's view. */ + fs[FEATURESET_7b0] &= ~(cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | +cpufeat_mask (X86_FEATURE_NO_FPU_SEL )); + fs[FEATURESET_7b0] |= (host_cpu_policy.feat._7b0 & + (cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | +cpufeat_mask (X86_FEATURE_NO_FPU_SEL ))); + +x86_cpu_featureset_to_policy (fs, p); + +/* Pass host cacheline size through to guests. */ + p->basic.clflush_size = max->basic.clflush_size; + + p->extd.maxphysaddr =min (p->extd.maxphysaddr, max->extd.maxphysaddr); + p->extd.maxphysaddr =min_t (uint8_t, p->extd.maxphysaddr, +paging_max_paddr_bits (d)); + p->extd.maxphysaddr =max_t (uint8_t, p->extd.maxphysaddr, + (p->basic.pae || p->basic.pse36) ?36 :32 ); + + p->extd.maxlinaddr = p->extd.lm ?48 :32 ; + +recalculate_xstate (p); +recalculate_misc (p); + +for ( i =0 ; i <ARRAY_SIZE (p->cache.raw); ++i ) + { +if ( p->cache.subleaf[i].type >=1 && + p->cache.subleaf[i].type <=3 ) + { +/* Subleaf has a valid cache type. Zero reserved fields. */ + p->cache.raw[i].a &=0xffffc3ffu ; + p->cache.raw[i].d &=0x00000007u ; + } +else + { +/* Subleaf is not valid. Zero the rest of the union. */ +zero_leaves (p->cache.raw, i,ARRAY_SIZE (p->cache.raw) -1 ); +break ; + } + } + +if ( vpmu_mode ==XENPMU_MODE_OFF || + ((vpmu_mode &XENPMU_MODE_ALL ) && !is_hardware_domain (d)) ) + p->basic.raw[0xa ] =EMPTY_LEAF ; + +if ( !p->extd.svm ) + p->extd.raw[0xa ] =EMPTY_LEAF ; + +if ( !p->extd.page1gb ) + p->extd.raw[0x19 ] =EMPTY_LEAF ; +} + +void __init init_dom0_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; + +/* dom0 can't migrate. Give it ITSC if available. */ +if ( cpu_has_itsc ) + p->extd.itsc = true; + +/* +* Expose the "hardware speculation behaviour" bits of ARCH_CAPS to dom0, +* so dom0 can turn off workarounds as appropriate. Temporary, until the +* domain policy logic gains a better understanding of MSRs. +*/ +if ( cpu_has_arch_caps ) + p->feat.arch_caps = true; + +/* Apply dom0-cpuid= command line settings, if provided. */ +if ( dom0_cpuid_cmdline ) + { +uint32_t fs[FSCAPINTS ]; +unsigned int i; + +x86_cpu_policy_to_featureset (p, fs); + +for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) + { + fs[i] |= dom0_enable_feat [i]; + fs[i] &= ~dom0_disable_feat[i]; + } + +x86_cpu_featureset_to_policy (fs, p); + +recalculate_cpuid_policy (d); + } +} + +static void __init __maybe_unused build_assertions (void ) +{ +BUILD_BUG_ON (ARRAY_SIZE (known_features) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (pv_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (hvm_shadow_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (hvm_hap_max_featuremask) !=FSCAPINTS ); +BUILD_BUG_ON (ARRAY_SIZE (deep_features) !=FSCAPINTS ); + +/* Find some more clever allocation scheme if this trips. */ +BUILD_BUG_ON (sizeof (struct cpu_policy ) >PAGE_SIZE ); + +BUILD_BUG_ON (sizeof (raw_cpu_policy.basic) != +sizeof (raw_cpu_policy.basic.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.feat) != +sizeof (raw_cpu_policy.feat.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.xstate) != +sizeof (raw_cpu_policy.xstate.raw)); +BUILD_BUG_ON (sizeof (raw_cpu_policy.extd) != +sizeof (raw_cpu_policy.extd.raw)); +}xen/arch/x86/cpuid.c +4
-813@@ -1,638 +1,14 @@ -#include <xen/init.h> -#include <xen/lib.h> -#include <xen/param.h> #include <xen/sched.h> -#include <xen/nospec.h> -#include <asm/amd.h> +#include <xen/types.h> + +#include <public/hvm/params.h> +#include <asm/cpu-policy.h> #include <asm/cpuid.h> -#include <asm/hvm/hvm.h> -#include <asm/hvm/nestedhvm.h> -#include <asm/hvm/svm/svm.h> #include <asm/hvm/viridian.h> -#include <asm/hvm/vmx/vmcs.h> -#include <asm/paging.h> -#include <asm/processor.h> #include <asm/xstate.h> -const uint32_t known_features[] =INIT_KNOWN_FEATURES ; - -static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; -static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; -static const uint32_t __initconst hvm_hap_max_featuremask[] = -INIT_HVM_HAP_MAX_FEATURES ; -static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; -static const uint32_t __initconst hvm_shadow_def_featuremask[] = -INIT_HVM_SHADOW_DEF_FEATURES ; -static const uint32_t __initconst hvm_hap_def_featuremask[] = -INIT_HVM_HAP_DEF_FEATURES ; -static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; - -static const struct feature_name { -const char *name; -unsigned int bit; -} feature_names[] __initconstrel = INIT_FEATURE_NAMES; - -/* -* Parse a list of cpuid feature names -> bool, calling the callback for any -* matches found. -* -* always_inline, because this is init code only and we really don't want a -* function pointer call in the middle of the loop. -*/ -static int __init always_inline parse_cpuid( -const char *s,void (*callback)(unsigned int feat,bool val)) -{ -const char *ss; -int val, rc =0 ; - -do { -const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; -const char *feat; - - ss =strchr (s,',' ); -if ( !ss ) - ss =strchr (s,'\0' ); - -/* Skip the 'no-' prefix for name comparisons. */ - feat = s; -if (strncmp (s,"no-" ,3 ) ==0 ) - feat +=3 ; - -/* (Re)initalise lhs and rhs for binary search. */ - lhs = feature_names; - rhs = feature_names +ARRAY_SIZE (feature_names); - -while ( lhs < rhs ) - { -int res; - - mid = lhs + (rhs - lhs) /2 ; - res =cmdline_strcmp (feat, mid->name); - -if ( res <0 ) - { - rhs = mid; -continue ; - } -if ( res >0 ) - { - lhs = mid +1 ; -continue ; - } - -if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) - { -callback (mid->bit, val); - mid =NULL ; - } - -break ; - } - -/* -* Mid being NULL means that the name and boolean were successfully -* identified. Everything else is an error. -*/ -if ( mid ) - rc = -EINVAL ; - - s = ss +1 ; - }while ( *ss ); - -return rc; -} - -static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) -{ -if ( !val ) -setup_clear_cpu_cap (feat); -else if ( feat ==X86_FEATURE_RDRAND && - (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) -setup_force_cpu_cap (X86_FEATURE_RDRAND ); -} - -static int __init cf_check parse_xen_cpuid(const char *s) -{ -return parse_cpuid (s, _parse_xen_cpuid); -} -custom_param ("cpuid" , parse_xen_cpuid); - -static bool __initdata dom0_cpuid_cmdline; -static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; -static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; - -static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) -{ -__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat); -__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); -} - -static int __init cf_check parse_dom0_cpuid(const char *s) -{ - dom0_cpuid_cmdline = true; - -return parse_cpuid (s, _parse_dom0_cpuid); -} -custom_param ("dom0-cpuid" , parse_dom0_cpuid); -#define EMPTY_LEAF ((struct cpuid_leaf){}) -static void zero_leaves (struct cpuid_leaf *l, -unsigned int first,unsigned int last) -{ -memset (&l[first],0 ,sizeof (*l) * (last - first +1 )); -} - -static void sanitise_featureset (uint32_t *fs) -{ -/* for_each_set_bit() uses unsigned longs. Extend with zeroes. */ -uint32_t disabled_features[ -ROUNDUP (FSCAPINTS ,sizeof (unsigned long )/sizeof (uint32_t ))] = {}; -unsigned int i; - -for ( i =0 ; i <FSCAPINTS ; ++i ) - { -/* Clamp to known mask. */ - fs[i] &= known_features[i]; - -/* -* Identify which features with deep dependencies have been -* disabled. -*/ - disabled_features[i] = ~fs[i] & deep_features[i]; - } - -for_each_set_bit (i, (void *)disabled_features, -sizeof (disabled_features) *8 ) - { -const uint32_t *dfs =x86_cpuid_lookup_deep_deps (i); -unsigned int j; - -ASSERT (dfs);/* deep_features[] should guarentee this. */ - -for ( j =0 ; j <FSCAPINTS ; ++j ) - { - fs[j] &= ~dfs[j]; - disabled_features[j] &= ~dfs[j]; - } - } -} - -static void recalculate_xstate (struct cpuid_policy *p) -{ -uint64_t xstates =XSTATE_FP_SSE ; -uint32_t xstate_size =XSTATE_AREA_MIN_SIZE ; -unsigned int i, Da1 = p->xstate.Da1; - -/* -* The Da1 leaf is the only piece of information preserved in the common -* case. Everything else is derived from other feature state. -*/ -memset (&p->xstate,0 ,sizeof (p->xstate)); - -if ( !p->basic.xsave ) -return ; - -if ( p->basic.avx ) - { - xstates |=X86_XCR0_YMM ; - xstate_size =max (xstate_size, - xstate_offsets[X86_XCR0_YMM_POS ] + - xstate_sizes[X86_XCR0_YMM_POS ]); - } - -if ( p->feat.mpx ) - { - xstates |=X86_XCR0_BNDREGS |X86_XCR0_BNDCSR ; - xstate_size =max (xstate_size, - xstate_offsets[X86_XCR0_BNDCSR_POS ] + - xstate_sizes[X86_XCR0_BNDCSR_POS ]); - } - -if ( p->feat.avx512f ) - { - xstates |=X86_XCR0_OPMASK |X86_XCR0_ZMM |X86_XCR0_HI_ZMM ; - xstate_size =max (xstate_size, - xstate_offsets[X86_XCR0_HI_ZMM_POS ] + - xstate_sizes[X86_XCR0_HI_ZMM_POS ]); - } - -if ( p->feat.pku ) - { - xstates |=X86_XCR0_PKRU ; - xstate_size =max (xstate_size, - xstate_offsets[X86_XCR0_PKRU_POS ] + - xstate_sizes[X86_XCR0_PKRU_POS ]); - } - - p->xstate.max_size = xstate_size; - p->xstate.xcr0_low = xstates & ~XSTATE_XSAVES_ONLY ; - p->xstate.xcr0_high = (xstates & ~XSTATE_XSAVES_ONLY ) >>32 ; - - p->xstate.Da1 = Da1; -if ( p->xstate.xsaves ) - { - p->xstate.xss_low = xstates &XSTATE_XSAVES_ONLY ; - p->xstate.xss_high = (xstates &XSTATE_XSAVES_ONLY ) >>32 ; - } -else - xstates &= ~XSTATE_XSAVES_ONLY ; - -for ( i =2 ; i <min (63ul ,ARRAY_SIZE (p->xstate.comp)); ++i ) - { -uint64_t curr_xstate =1ul << i; - -if ( !(xstates & curr_xstate) ) -continue ; - - p->xstate.comp[i].size = xstate_sizes[i]; - p->xstate.comp[i].offset = xstate_offsets[i]; - p->xstate.comp[i].xss = curr_xstate &XSTATE_XSAVES_ONLY ; - p->xstate.comp[i].align = curr_xstate & xstate_align; - } -} - -/* -* Misc adjustments to the policy. Mostly clobbering reserved fields and -* duplicating shared fields. Intentionally hidden fields are annotated. -*/ -static void recalculate_misc (struct cpuid_policy *p) -{ - p->basic.raw_fms &=0x0fff0fff ;/* Clobber Processor Type on Intel. */ - p->basic.apic_id =0 ;/* Dynamic. */ - - p->basic.raw[0x5 ] =EMPTY_LEAF ;/* MONITOR not exposed to guests. */ - p->basic.raw[0x6 ] =EMPTY_LEAF ;/* Therm/Power not exposed to guests. */ - - p->basic.raw[0x8 ] =EMPTY_LEAF ; - -/* TODO: Rework topology logic. */ -memset (p->topo.raw,0 ,sizeof (p->topo.raw)); - - p->basic.raw[0xc ] =EMPTY_LEAF ; - - p->extd.e1d &= ~CPUID_COMMON_1D_FEATURES ; - -/* Most of Power/RAS hidden from guests. */ - p->extd.raw[0x7 ].a = p->extd.raw[0x7 ].b = p->extd.raw[0x7 ].c =0 ; - - p->extd.raw[0x8 ].d =0 ; - -switch ( p->x86_vendor ) - { -case X86_VENDOR_INTEL : - p->basic.l2_nr_queries =1 ;/* Fixed to 1 query. */ - p->basic.raw[0x3 ] =EMPTY_LEAF ;/* PSN - always hidden. */ - p->basic.raw[0x9 ] =EMPTY_LEAF ;/* DCA - always hidden. */ - - p->extd.vendor_ebx =0 ; - p->extd.vendor_ecx =0 ; - p->extd.vendor_edx =0 ; - - p->extd.raw[0x1 ].a = p->extd.raw[0x1 ].b =0 ; - - p->extd.raw[0x5 ] =EMPTY_LEAF ; - p->extd.raw[0x6 ].a = p->extd.raw[0x6 ].b = p->extd.raw[0x6 ].d =0 ; - - p->extd.raw[0x8 ].a &=0x0000ffff ; - p->extd.raw[0x8 ].c =0 ; -break ; - -case X86_VENDOR_AMD : -case X86_VENDOR_HYGON : -zero_leaves (p->basic.raw,0x2 ,0x3 ); -memset (p->cache.raw,0 ,sizeof (p->cache.raw)); -zero_leaves (p->basic.raw,0x9 ,0xa ); - - p->extd.vendor_ebx = p->basic.vendor_ebx; - p->extd.vendor_ecx = p->basic.vendor_ecx; - p->extd.vendor_edx = p->basic.vendor_edx; - - p->extd.raw_fms = p->basic.raw_fms; - p->extd.raw[0x1 ].b &=0xff00ffff ; - p->extd.e1d |= p->basic._1d &CPUID_COMMON_1D_FEATURES ; - - p->extd.raw[0x8 ].a &=0x0000ffff ;/* GuestMaxPhysAddr hidden. */ - p->extd.raw[0x8 ].c &=0x0003f0ff ; - - p->extd.raw[0x9 ] =EMPTY_LEAF ; - -zero_leaves (p->extd.raw,0xb ,0x18 ); - -/* 0x19 - TLB details. Pass through. */ -/* 0x1a - Perf hints. Pass through. */ - - p->extd.raw[0x1b ] =EMPTY_LEAF ;/* IBS - not supported. */ - p->extd.raw[0x1c ] =EMPTY_LEAF ;/* LWP - not supported. */ - p->extd.raw[0x1d ] =EMPTY_LEAF ;/* TopoExt Cache */ - p->extd.raw[0x1e ] =EMPTY_LEAF ;/* TopoExt APIC ID/Core/Node */ - p->extd.raw[0x1f ] =EMPTY_LEAF ;/* SEV */ - p->extd.raw[0x20 ] =EMPTY_LEAF ;/* Platform QoS */ -break ; - } -} - -static void __init calculate_raw_policy (void ) -{ -struct cpuid_policy *p = &raw_cpu_policy; - -x86_cpuid_policy_fill_native (p); - -/* Nothing good will come from Xen and libx86 disagreeing on vendor. */ -ASSERT (p->x86_vendor == boot_cpu_data.x86_vendor); -} - -static void __init calculate_host_policy (void ) -{ -struct cpuid_policy *p = &host_cpu_policy; -unsigned int max_extd_leaf; - - *p = raw_cpu_policy; - - p->basic.max_leaf = -min_t (uint32_t, p->basic.max_leaf,ARRAY_SIZE (p->basic.raw) -1 ); - p->feat.max_subleaf = -min_t (uint32_t, p->feat.max_subleaf,ARRAY_SIZE (p->feat.raw) -1 ); - - max_extd_leaf = p->extd.max_leaf; - -/* -* For AMD/Hygon hardware before Zen3, we unilaterally modify LFENCE to be -* dispatch serialising for Spectre mitigations. Extend max_extd_leaf -* beyond what hardware supports, to include the feature leaf containing -* this information. -*/ -if ( cpu_has_lfence_dispatch ) - max_extd_leaf =max (max_extd_leaf,0x80000021 ); - - p->extd.max_leaf =0x80000000 |min_t (uint32_t, max_extd_leaf &0xffff , -ARRAY_SIZE (p->extd.raw) -1 ); - -x86_cpu_featureset_to_policy (boot_cpu_data.x86_capability, p); -recalculate_xstate (p); -recalculate_misc (p); - -/* When vPMU is disabled, drop it from the host policy. */ -if ( vpmu_mode ==XENPMU_MODE_OFF ) - p->basic.raw[0xa ] =EMPTY_LEAF ; - -if ( p->extd.svm ) - { -/* Clamp to implemented features which require hardware support. */ - p->extd.raw[0xa ].d &= ((1u <<SVM_FEATURE_NPT ) | - (1u <<SVM_FEATURE_LBRV ) | - (1u <<SVM_FEATURE_NRIPS ) | - (1u <<SVM_FEATURE_PAUSEFILTER ) | - (1u <<SVM_FEATURE_DECODEASSISTS )); -/* Enable features which are always emulated. */ - p->extd.raw[0xa ].d |= ((1u <<SVM_FEATURE_VMCBCLEAN ) | - (1u <<SVM_FEATURE_TSCRATEMSR )); - } -} - -static void __init guest_common_default_feature_adjustments (uint32_t *fs) -{ -/* -* IvyBridge client parts suffer from leakage of RDRAND data due to SRBDS -* (XSA-320 / CVE-2020-0543), and won't be receiving microcode to -* compensate. -* -* Mitigate by hiding RDRAND from guests by default, unless explicitly -* overridden on the Xen command line (cpuid=rdrand). Irrespective of the -* default setting, guests can use RDRAND if explicitly enabled -* (cpuid="host,rdrand=1") in the VM's config file, and VMs which were -* previously using RDRAND can migrate in. -*/ -if ( boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL && - boot_cpu_data.x86 ==6 && boot_cpu_data.x86_model ==0x3a && - cpu_has_rdrand && !is_forced_cpu_cap (X86_FEATURE_RDRAND ) ) -__clear_bit (X86_FEATURE_RDRAND , fs); - -/* -* On certain hardware, speculative or errata workarounds can result in -* TSX being placed in "force-abort" mode, where it doesn't actually -* function as expected, but is technically compatible with the ISA. -* -* Do not advertise RTM to guests by default if it won't actually work. -*/ -if ( rtm_disabled ) -__clear_bit (X86_FEATURE_RTM , fs); -} - -static void __init guest_common_feature_adjustments (uint32_t *fs) -{ -/* Unconditionally claim to be able to set the hypervisor bit. */ -__set_bit (X86_FEATURE_HYPERVISOR , fs); - -/* -* If IBRS is offered to the guest, unconditionally offer STIBP. It is a -* nop on non-HT hardware, and has this behaviour to make heterogeneous -* setups easier to manage. -*/ -if (test_bit (X86_FEATURE_IBRSB , fs) ) -__set_bit (X86_FEATURE_STIBP , fs); -if (test_bit (X86_FEATURE_IBRS , fs) ) -__set_bit (X86_FEATURE_AMD_STIBP , fs); - -/* -* On hardware which supports IBRS/IBPB, we can offer IBPB independently -* of IBRS by using the AMD feature bit. An administrator may wish for -* performance reasons to offer IBPB without IBRS. -*/ -if ( host_cpu_policy.feat.ibrsb ) -__set_bit (X86_FEATURE_IBPB , fs); -} - -static void __init calculate_pv_max_policy (void ) -{ -struct cpuid_policy *p = &pv_max_cpu_policy; -uint32_t pv_featureset[FSCAPINTS ]; -unsigned int i; - - *p = host_cpu_policy; -x86_cpu_policy_to_featureset (p, pv_featureset); - -for ( i =0 ; i <ARRAY_SIZE (pv_featureset); ++i ) - pv_featureset[i] &= pv_max_featuremask[i]; - -/* -* If Xen isn't virtualising MSR_SPEC_CTRL for PV guests (functional -* availability, or admin choice), hide the feature. -*/ -if ( !boot_cpu_has (X86_FEATURE_SC_MSR_PV ) ) - { -__clear_bit (X86_FEATURE_IBRSB , pv_featureset); -__clear_bit (X86_FEATURE_IBRS , pv_featureset); - } - -guest_common_feature_adjustments (pv_featureset); - -sanitise_featureset (pv_featureset); -x86_cpu_featureset_to_policy (pv_featureset, p); -recalculate_xstate (p); - - p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */ -} - -static void __init calculate_pv_def_policy (void ) -{ -struct cpuid_policy *p = &pv_def_cpu_policy; -uint32_t pv_featureset[FSCAPINTS ]; -unsigned int i; - - *p = pv_max_cpu_policy; -x86_cpu_policy_to_featureset (p, pv_featureset); - -for ( i =0 ; i <ARRAY_SIZE (pv_featureset); ++i ) - pv_featureset[i] &= pv_def_featuremask[i]; - -guest_common_feature_adjustments (pv_featureset); -guest_common_default_feature_adjustments (pv_featureset); - -sanitise_featureset (pv_featureset); -x86_cpu_featureset_to_policy (pv_featureset, p); -recalculate_xstate (p); -} - -static void __init calculate_hvm_max_policy (void ) -{ -struct cpuid_policy *p = &hvm_max_cpu_policy; -uint32_t hvm_featureset[FSCAPINTS ]; -unsigned int i; -const uint32_t *hvm_featuremask; - - *p = host_cpu_policy; -x86_cpu_policy_to_featureset (p, hvm_featureset); - - hvm_featuremask =hvm_hap_supported () ? - hvm_hap_max_featuremask : hvm_shadow_max_featuremask; - -for ( i =0 ; i <ARRAY_SIZE (hvm_featureset); ++i ) - hvm_featureset[i] &= hvm_featuremask[i]; - -/* -* Xen can provide an (x2)APIC emulation to HVM guests even if the host's -* (x2)APIC isn't enabled. -*/ -__set_bit (X86_FEATURE_APIC , hvm_featureset); -__set_bit (X86_FEATURE_X2APIC , hvm_featureset); - -/* -* We don't support EFER.LMSLE at all. AMD has dropped the feature from -* hardware and allocated a CPUID bit to indicate its absence. -*/ -__set_bit (X86_FEATURE_NO_LMSL , hvm_featureset); - -/* -* On AMD, PV guests are entirely unable to use SYSENTER as Xen runs in -* long mode (and init_amd() has cleared it out of host capabilities), but -* HVM guests are able if running in protected mode. -*/ -if ( (boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) && - raw_cpu_policy.basic.sep ) -__set_bit (X86_FEATURE_SEP , hvm_featureset); - -/* -* VIRT_SSBD is exposed in the default policy as a result of -* amd_virt_spec_ctrl being set, it also needs exposing in the max policy. -*/ -if ( amd_virt_spec_ctrl ) -__set_bit (X86_FEATURE_VIRT_SSBD , hvm_featureset); - -/* -* If Xen isn't virtualising MSR_SPEC_CTRL for HVM guests (functional -* availability, or admin choice), hide the feature. -*/ -if ( !boot_cpu_has (X86_FEATURE_SC_MSR_HVM ) ) - { -__clear_bit (X86_FEATURE_IBRSB , hvm_featureset); -__clear_bit (X86_FEATURE_IBRS , hvm_featureset); - } -else if (boot_cpu_has (X86_FEATURE_AMD_SSBD ) ) -/* -* If SPEC_CTRL.SSBD is available VIRT_SPEC_CTRL.SSBD can be exposed -* and implemented using the former. Expose in the max policy only as -* the preference is for guests to use SPEC_CTRL.SSBD if available. -*/ -__set_bit (X86_FEATURE_VIRT_SSBD , hvm_featureset); - -/* -* With VT-x, some features are only supported by Xen if dedicated -* hardware support is also available. -*/ -if ( cpu_has_vmx ) - { -if ( !cpu_has_vmx_mpx ) -__clear_bit (X86_FEATURE_MPX , hvm_featureset); - -if ( !cpu_has_vmx_xsaves ) -__clear_bit (X86_FEATURE_XSAVES , hvm_featureset); - } - -/* -* Xen doesn't use PKS, so the guest support for it has opted to not use -* the VMCS load/save controls for efficiency reasons. This depends on -* the exact vmentry/exit behaviour, so don't expose PKS in other -* situations until someone has cross-checked the behaviour for safety. -*/ -if ( !cpu_has_vmx ) -__clear_bit (X86_FEATURE_PKS , hvm_featureset); - -guest_common_feature_adjustments (hvm_featureset); - -sanitise_featureset (hvm_featureset); -x86_cpu_featureset_to_policy (hvm_featureset, p); -recalculate_xstate (p); -} - -static void __init calculate_hvm_def_policy (void ) -{ -struct cpuid_policy *p = &hvm_def_cpu_policy; -uint32_t hvm_featureset[FSCAPINTS ]; -unsigned int i; -const uint32_t *hvm_featuremask; - - *p = hvm_max_cpu_policy; -x86_cpu_policy_to_featureset (p, hvm_featureset); - - hvm_featuremask =hvm_hap_supported () ? - hvm_hap_def_featuremask : hvm_shadow_def_featuremask; - -for ( i =0 ; i <ARRAY_SIZE (hvm_featureset); ++i ) - hvm_featureset[i] &= hvm_featuremask[i]; - -guest_common_feature_adjustments (hvm_featureset); -guest_common_default_feature_adjustments (hvm_featureset); - -/* -* Only expose VIRT_SSBD if AMD_SSBD is not available, and thus -* amd_virt_spec_ctrl is set. -*/ -if ( amd_virt_spec_ctrl ) -__set_bit (X86_FEATURE_VIRT_SSBD , hvm_featureset); - -sanitise_featureset (hvm_featureset); -x86_cpu_featureset_to_policy (hvm_featureset, p); -recalculate_xstate (p); -} - -void __init init_guest_cpuid (void ) -{ -calculate_raw_policy (); -calculate_host_policy (); - -if (IS_ENABLED (CONFIG_PV ) ) - { -calculate_pv_max_policy (); -calculate_pv_def_policy (); - } - -if ( hvm_enabled ) - { -calculate_hvm_max_policy (); -calculate_hvm_def_policy (); - } -}bool recheck_cpu_features (unsigned int cpu) {@@ -656,170 +32,6 @@ bool recheck_cpu_features(unsigned int cpu)return okay; } -void recalculate_cpuid_policy (struct domain *d) -{ -struct cpuid_policy *p = d->arch.cpuid; -const struct cpuid_policy *max =is_pv_domain (d) - ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) - : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL ); -uint32_t fs[FSCAPINTS ], max_fs[FSCAPINTS ]; -unsigned int i; - -if ( !max ) - { -ASSERT_UNREACHABLE (); -return ; - } - - p->x86_vendor =x86_cpuid_lookup_vendor ( - p->basic.vendor_ebx, p->basic.vendor_ecx, p->basic.vendor_edx); - - p->basic.max_leaf =min (p->basic.max_leaf, max->basic.max_leaf); - p->feat.max_subleaf =min (p->feat.max_subleaf, max->feat.max_subleaf); - p->extd.max_leaf =0x80000000 |min (p->extd.max_leaf &0xffff , - ((p->x86_vendor & (X86_VENDOR_AMD | -X86_VENDOR_HYGON )) - ?CPUID_GUEST_NR_EXTD_AMD - :CPUID_GUEST_NR_EXTD_INTEL ) -1 ); - -x86_cpu_policy_to_featureset (p, fs); -x86_cpu_policy_to_featureset (max, max_fs); - -if (is_hvm_domain (d) ) - { -/* -* HVM domains using Shadow paging have further restrictions on their -* available paging features. -*/ -if ( !hap_enabled (d) ) - { -for ( i =0 ; i <ARRAY_SIZE (max_fs); i++ ) - max_fs[i] &= hvm_shadow_max_featuremask[i]; - } - -/* Hide nested-virt if it hasn't been explicitly configured. */ -if ( !nestedhvm_enabled (d) ) - { -__clear_bit (X86_FEATURE_VMX , max_fs); -__clear_bit (X86_FEATURE_SVM , max_fs); - } - } - -/* -* Allow the toolstack to set HTT, X2APIC and CMP_LEGACY. These bits -* affect how to interpret topology information in other cpuid leaves. -*/ -__set_bit (X86_FEATURE_HTT , max_fs); -__set_bit (X86_FEATURE_X2APIC , max_fs); -__set_bit (X86_FEATURE_CMP_LEGACY , max_fs); - -/* -* 32bit PV domains can't use any Long Mode features, and cannot use -* SYSCALL on non-AMD hardware. -*/ -if (is_pv_32bit_domain (d) ) - { -__clear_bit (X86_FEATURE_LM , max_fs); -if ( !(boot_cpu_data.x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON )) ) -__clear_bit (X86_FEATURE_SYSCALL , max_fs); - } - -/* Clamp the toolstacks choices to reality. */ -for ( i =0 ; i <ARRAY_SIZE (fs); i++ ) - fs[i] &= max_fs[i]; - -if ( p->basic.max_leaf <XSTATE_CPUID ) -__clear_bit (X86_FEATURE_XSAVE , fs); - -sanitise_featureset (fs); - -/* Fold host's FDP_EXCP_ONLY and NO_FPU_SEL into guest's view. */ - fs[FEATURESET_7b0] &= ~(cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | -cpufeat_mask (X86_FEATURE_NO_FPU_SEL )); - fs[FEATURESET_7b0] |= (host_cpu_policy.feat._7b0 & - (cpufeat_mask (X86_FEATURE_FDP_EXCP_ONLY ) | -cpufeat_mask (X86_FEATURE_NO_FPU_SEL ))); - -x86_cpu_featureset_to_policy (fs, p); - -/* Pass host cacheline size through to guests. */ - p->basic.clflush_size = max->basic.clflush_size; - - p->extd.maxphysaddr =min (p->extd.maxphysaddr, max->extd.maxphysaddr); - p->extd.maxphysaddr =min_t (uint8_t, p->extd.maxphysaddr, -paging_max_paddr_bits (d)); - p->extd.maxphysaddr =max_t (uint8_t, p->extd.maxphysaddr, - (p->basic.pae || p->basic.pse36) ?36 :32 ); - - p->extd.maxlinaddr = p->extd.lm ?48 :32 ; - -recalculate_xstate (p); -recalculate_misc (p); - -for ( i =0 ; i <ARRAY_SIZE (p->cache.raw); ++i ) - { -if ( p->cache.subleaf[i].type >=1 && - p->cache.subleaf[i].type <=3 ) - { -/* Subleaf has a valid cache type. Zero reserved fields. */ - p->cache.raw[i].a &=0xffffc3ffu ; - p->cache.raw[i].d &=0x00000007u ; - } -else - { -/* Subleaf is not valid. Zero the rest of the union. */ -zero_leaves (p->cache.raw, i,ARRAY_SIZE (p->cache.raw) -1 ); -break ; - } - } - -if ( vpmu_mode ==XENPMU_MODE_OFF || - ((vpmu_mode &XENPMU_MODE_ALL ) && !is_hardware_domain (d)) ) - p->basic.raw[0xa ] =EMPTY_LEAF ; - -if ( !p->extd.svm ) - p->extd.raw[0xa ] =EMPTY_LEAF ; - -if ( !p->extd.page1gb ) - p->extd.raw[0x19 ] =EMPTY_LEAF ; -} - -void __init init_dom0_cpuid_policy (struct domain *d) -{ -struct cpuid_policy *p = d->arch.cpuid; - -/* dom0 can't migrate. Give it ITSC if available. */ -if ( cpu_has_itsc ) - p->extd.itsc = true; - -/* -* Expose the "hardware speculation behaviour" bits of ARCH_CAPS to dom0, -* so dom0 can turn off workarounds as appropriate. Temporary, until the -* domain policy logic gains a better understanding of MSRs. -*/ -if ( cpu_has_arch_caps ) - p->feat.arch_caps = true; - -/* Apply dom0-cpuid= command line settings, if provided. */ -if ( dom0_cpuid_cmdline ) - { -uint32_t fs[FSCAPINTS ]; -unsigned int i; - -x86_cpu_policy_to_featureset (p, fs); - -for ( i =0 ; i <ARRAY_SIZE (fs); ++i ) - { - fs[i] |= dom0_enable_feat [i]; - fs[i] &= ~dom0_disable_feat[i]; - } - -x86_cpu_featureset_to_policy (fs, p); - -recalculate_cpuid_policy (d); - } -} -void guest_cpuid (const struct vcpu *v,uint32_t leaf,uint32_t subleaf,struct cpuid_leaf *res) {@@ -1190,27 +402,6 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf, } } -static void __init __maybe_unused build_assertions (void ) -{ -BUILD_BUG_ON (ARRAY_SIZE (known_features) !=FSCAPINTS ); -BUILD_BUG_ON (ARRAY_SIZE (pv_max_featuremask) !=FSCAPINTS ); -BUILD_BUG_ON (ARRAY_SIZE (hvm_shadow_max_featuremask) !=FSCAPINTS ); -BUILD_BUG_ON (ARRAY_SIZE (hvm_hap_max_featuremask) !=FSCAPINTS ); -BUILD_BUG_ON (ARRAY_SIZE (deep_features) !=FSCAPINTS ); - -/* Find some more clever allocation scheme if this trips. */ -BUILD_BUG_ON (sizeof (struct cpuid_policy ) >PAGE_SIZE ); - -BUILD_BUG_ON (sizeof (raw_cpu_policy.basic) != -sizeof (raw_cpu_policy.basic.raw)); -BUILD_BUG_ON (sizeof (raw_cpu_policy.feat) != -sizeof (raw_cpu_policy.feat.raw)); -BUILD_BUG_ON (sizeof (raw_cpu_policy.xstate) != -sizeof (raw_cpu_policy.xstate.raw)); -BUILD_BUG_ON (sizeof (raw_cpu_policy.extd) != -sizeof (raw_cpu_policy.extd.raw)); -} -/* * Local variables: * mode: C xen/arch/x86/hvm/hvm.c +0
-1@@ -77,7 +77,6 @@#include <public/memory.h> #include <public/vm_event.h> #include <public/arch-x86/cpuid.h> -#include <asm/cpuid.h> #include <compat/hvm/hvm_op.h> xen/arch/x86/include/asm/cpu-policy.h +6
-0@@ -18,4 +18,10 @@ void init_guest_cpu_policies(void);/* Allocate and initialise a CPU policy suitable for the domain. */ int init_domain_cpu_policy (struct domain *d); +/* Apply dom0-specific tweaks to the CPUID policy. */ +void init_dom0_cpuid_policy (struct domain *d); + +/* Clamp the CPUID policy to reality. */ +void recalculate_cpuid_policy (struct domain *d); +#endif /* X86_CPU_POLICY_H */ xen/arch/x86/include/asm/cpuid.h +1
-10@@ -8,14 +8,10 @@#include <xen/kernel.h> #include <xen/percpu.h> -#include <xen/lib/x86/cpu-policy.h> -#include <public/sysctl.h> extern const uint32_t known_features[FSCAPINTS ]; -void init_guest_cpuid (void ); -/* * Expected levelling capabilities (given cpuid vendor/family information), * and levelling capabilities actually available (given MSR probing). @@ -49,13 +45,8 @@ extern struct cpuidmasks cpuidmask_defaults;/* Check that all previously present features are still available. */ bool recheck_cpu_features (unsigned int cpu); -/* Apply dom0-specific tweaks to the CPUID policy. */ -void init_dom0_cpuid_policy (struct domain *d); - -/* Clamp the CPUID policy to reality. */ -void recalculate_cpuid_policy (struct domain *d); -struct vcpu ; +struct cpuid_leaf ;void guest_cpuid (const struct vcpu *v,uint32_t leaf,uint32_t subleaf,struct cpuid_leaf *res);xen/arch/x86/pv/domain.c +1
-0@@ -10,6 +10,7 @@#include <xen/param.h> #include <xen/sched.h> +#include <asm/cpu-policy.h> #include <asm/cpufeature.h> #include <asm/invpcid.h> #include <asm/spec_ctrl.h> xen/arch/x86/setup.c +0
-2@@ -51,7 +51,6 @@#include <asm/alternative.h> #include <asm/mc146818rtc.h> #include <asm/cpu-policy.h> -#include <asm/cpuid.h> #include <asm/spec_ctrl.h> #include <asm/guest.h> #include <asm/microcode.h> @@ -1991,7 +1990,6 @@ void __init noreturn __start_xen(unsigned long mbi_p)if ( !tboot_protect_mem_regions () )panic ("Could not protect TXT memory regions\n" ); -init_guest_cpuid ();init_guest_cpu_policies ();if ( xen_cpuidle )
Re: [PATCH v2 11/15] x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c
Quoted 169 lines · Jan Beulich
Quoted 168 lines · Andrew Cooper
Switch to the newer
cpu_policynomenclature. Do some easy cleanup of includes.No practical change.
Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>v2:
* New--- xen/arch/x86/cpu-policy.c | 752 ++++++++++++++++++++++++ xen/arch/x86/cpuid.c | 817 +------------------------- xen/arch/x86/hvm/hvm.c | 1 - xen/arch/x86/include/asm/cpu-policy.h | 6 + xen/arch/x86/include/asm/cpuid.h | 11 +- xen/arch/x86/pv/domain.c | 1 + xen/arch/x86/setup.c | 2 - 7 files changed, 764 insertions(+), 826 deletions(-)xen/arch/x86/cpu-policy.c +123
-0@@ -1,13 +1,19 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ #include <xen/cache.h> #include <xen/kernel.h> +#include <xen/param.h> #include <xen/sched.h> #include <xen/lib/x86/cpu-policy.h> +#include <asm/amd.h> #include <asm/cpu-policy.h> +#include <asm/hvm/nestedhvm.h> +#include <asm/hvm/svm/svm.h> #include <asm/msr-index.h> +#include <asm/paging.h> #include <asm/setup.h> +#include <asm/xstate.h> struct cpu_policy __ro_after_init raw_cpu_policy;struct cpu_policy __ro_after_init host_cpu_policy;@@ -20,10 +26,332 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +const uint32_t known_features[] =INIT_KNOWN_FEATURES ; + +static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; +static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; +static const uint32_t __initconst hvm_hap_max_featuremask[] = +INIT_HVM_HAP_MAX_FEATURES ; +static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; +static const uint32_t __initconst hvm_shadow_def_featuremask[] = +INIT_HVM_SHADOW_DEF_FEATURES ; +static const uint32_t __initconst hvm_hap_def_featuremask[] = +INIT_HVM_HAP_DEF_FEATURES ; +static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; + +static const struct feature_name { +const char *name; +unsigned int bit; +} feature_names[] __initconstrel = INIT_FEATURE_NAMES; + +/* +* Parse a list of cpuid feature names -> bool, calling the callback for any +* matches found. +* +* always_inline, because this is init code only and we really don't want a +* function pointer call in the middle of the loop. +*/ +static int __init always_inline parse_cpuid( +const char *s,void (*callback)(unsigned int feat,bool val)) +{ +const char *ss; +int val, rc =0 ; + +do { +const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; +const char *feat; + + ss =strchr (s,',' ); +if ( !ss ) + ss =strchr (s,'\0' ); + +/* Skip the 'no-' prefix for name comparisons. */ + feat = s; +if (strncmp (s,"no-" ,3 ) ==0 ) + feat +=3 ; + +/* (Re)initalise lhs and rhs for binary search. */ + lhs = feature_names; + rhs = feature_names +ARRAY_SIZE (feature_names); + +while ( lhs < rhs ) + { +int res; + + mid = lhs + (rhs - lhs) /2 ; + res =cmdline_strcmp (feat, mid->name); + +if ( res <0 ) + { + rhs = mid; +continue ; + } +if ( res >0 ) + { + lhs = mid +1 ; +continue ; + } + +if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) + { +callback (mid->bit, val); + mid =NULL ; + } + +break ; + } + +/* +* Mid being NULL means that the name and boolean were successfully +* identified. Everything else is an error. +*/ +if ( mid ) + rc = -EINVAL ; + + s = ss +1 ; + }while ( *ss ); + +return rc; +} + +static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) +{ +if ( !val ) +setup_clear_cpu_cap (feat); +else if ( feat ==X86_FEATURE_RDRAND && + (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) +setup_force_cpu_cap (X86_FEATURE_RDRAND ); +} + +static int __init cf_check parse_xen_cpuid(const char *s) +{ +return parse_cpuid (s, _parse_xen_cpuid); +} +custom_param ("cpuid" , parse_xen_cpuid); + +static bool __initdata dom0_cpuid_cmdline; +static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; +static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; + +static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) +{ +__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat); +__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); +} + +static int __init cf_check parse_dom0_cpuid(const char *s) +{ + dom0_cpuid_cmdline = true; +
Quoted 8 lines
xen/arch/x86/cpu-policy.c struct cpu_policy __ro_after_init hvm_max_cpu_policy; · line 148
Quoted 3 lines
xen/arch/x86/cpu-policy.c +3
-0+return parse_cpuid (s, _parse_dom0_cpuid); +} +custom_param ("dom0-cpuid" , parse_dom0_cpuid);Unless the plan is to completely remove
cpuid.c, this command line handling would imo better fit there. I understand that to keep dom0_{en,dis}able_feat[] static, the_parse_dom0_cpuid()helper would then need to be exposed (under a different name), but I think that's quite okay, the more that it's an__initfunction.
I'm not sure I agree. (I did debate this for a while before moving the cmdline parsing.)
I do have some cleanup plans which will move code into cpuid.c, and guest_cpuid() absolutely still lives there, but for these options specifically, the moment I add MSR_ARCH_CAPS into a featureset, their bit names names will work here too.
So arguably {dom0-}cpuid= don't be a great name moving forwards, but it is is absolutely more cpu-policy.c content than cpuid.c content.
We can't get rid of the existing cmdline names, and I think documenting our way out of the "it's not only CPUID bits any more" is better than adding yet a new name.
Quoted 13 lines
xen/arch/x86/cpu-policy.c int init_domain_cpu_policy(struct domain *d) · line 725
Quoted 11 lines
xen/arch/x86/cpu-policy.c +7
-0@@ -149,3 +716,188 @@ int init_domain_cpu_policy(struct domain *d)return 0 ; } + +void recalculate_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; +const struct cpu_policy *max =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL );While this is how the original code was, wouldn't this want to use
hvm_enabled, just likeinit_guest_cpu_policies()does (patch 10)?
No. That will fail to link.
This trickery is necessary to drop the compiler-visible reference to hvm_max_cpu_policy in !CONFIG_HVM builds.
This function is only called after the domain type has already been established, which precludes calling it in a case where max will evaluate to NULL, hence the ASSERT_UNREACHABLE() just below.
~Andrew
Jan Beulich
Acked-byRe: [PATCH v2 11/15] x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c
Quoted 140 lines · Andrew Cooper
Quoted 122 lines · Jan Beulich
Quoted 121 lines · Andrew Cooper
xen/arch/x86/cpu-policy.c +117
-0@@ -20,10 +26,332 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +const uint32_t known_features[] =INIT_KNOWN_FEATURES ; + +static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; +static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; +static const uint32_t __initconst hvm_hap_max_featuremask[] = +INIT_HVM_HAP_MAX_FEATURES ; +static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; +static const uint32_t __initconst hvm_shadow_def_featuremask[] = +INIT_HVM_SHADOW_DEF_FEATURES ; +static const uint32_t __initconst hvm_hap_def_featuremask[] = +INIT_HVM_HAP_DEF_FEATURES ; +static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; + +static const struct feature_name { +const char *name; +unsigned int bit; +} feature_names[] __initconstrel = INIT_FEATURE_NAMES; + +/* +* Parse a list of cpuid feature names -> bool, calling the callback for any +* matches found. +* +* always_inline, because this is init code only and we really don't want a +* function pointer call in the middle of the loop. +*/ +static int __init always_inline parse_cpuid( +const char *s,void (*callback)(unsigned int feat,bool val)) +{ +const char *ss; +int val, rc =0 ; + +do { +const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; +const char *feat; + + ss =strchr (s,',' ); +if ( !ss ) + ss =strchr (s,'\0' ); + +/* Skip the 'no-' prefix for name comparisons. */ + feat = s; +if (strncmp (s,"no-" ,3 ) ==0 ) + feat +=3 ; + +/* (Re)initalise lhs and rhs for binary search. */ + lhs = feature_names; + rhs = feature_names +ARRAY_SIZE (feature_names); + +while ( lhs < rhs ) + { +int res; + + mid = lhs + (rhs - lhs) /2 ; + res =cmdline_strcmp (feat, mid->name); + +if ( res <0 ) + { + rhs = mid; +continue ; + } +if ( res >0 ) + { + lhs = mid +1 ; +continue ; + } + +if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) + { +callback (mid->bit, val); + mid =NULL ; + } + +break ; + } + +/* +* Mid being NULL means that the name and boolean were successfully +* identified. Everything else is an error. +*/ +if ( mid ) + rc = -EINVAL ; + + s = ss +1 ; + }while ( *ss ); + +return rc; +} + +static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) +{ +if ( !val ) +setup_clear_cpu_cap (feat); +else if ( feat ==X86_FEATURE_RDRAND && + (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) +setup_force_cpu_cap (X86_FEATURE_RDRAND ); +} + +static int __init cf_check parse_xen_cpuid(const char *s) +{ +return parse_cpuid (s, _parse_xen_cpuid); +} +custom_param ("cpuid" , parse_xen_cpuid); + +static bool __initdata dom0_cpuid_cmdline; +static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; +static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; + +static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) +{ +__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat); +__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); +} + +static int __init cf_check parse_dom0_cpuid(const char *s) +{ + dom0_cpuid_cmdline = true; +Quoted 8 lines
xen/arch/x86/cpu-policy.c struct cpu_policy __ro_after_init hvm_max_cpu_policy; · line 148
Quoted 3 lines
xen/arch/x86/cpu-policy.c +3
-0+return parse_cpuid (s, _parse_dom0_cpuid); +} +custom_param ("dom0-cpuid" , parse_dom0_cpuid);Unless the plan is to completely remove
cpuid.c, this command line handling would imo better fit there. I understand that to keep dom0_{en,dis}able_feat[] static, the_parse_dom0_cpuid()helper would then need to be exposed (under a different name), but I think that's quite okay, the more that it's an__initfunction.I'm not sure I agree. (I did debate this for a while before moving the cmdline parsing.)
I do have some cleanup plans which will move code into
cpuid.c, andguest_cpuid()absolutely still lives there, but for these options specifically, the moment I addMSR_ARCH_CAPSinto a featureset, their bit names names will work here too.
Quoted 6 lines
So arguably {dom0-}cpuid= don't be a great name moving forwards, but it is is absolutely more
cpu-policy.ccontent thancpuid.ccontent.We can't get rid of the existing cmdline names, and I think documenting our way out of the "it's not only CPUID bits any more" is better than adding yet a new name.
Hmm, yes:
Quoted 14 lines
Quoted 13 lines
xen/arch/x86/cpu-policy.c int init_domain_cpu_policy(struct domain *d) · line 725
Quoted 11 lines
xen/arch/x86/cpu-policy.c +7
-0@@ -149,3 +716,188 @@ int init_domain_cpu_policy(struct domain *d)return 0 ; } + +void recalculate_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; +const struct cpu_policy *max =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL );While this is how the original code was, wouldn't this want to use
hvm_enabled, just likeinit_guest_cpu_policies()does (patch 10)?
Quoted 1 line
No. That will fail to link.
Why? hvm_enabled is a #define (to false) only when !HVM.
Quoted 6 lines
This trickery is necessary to drop the compiler-visible reference to
hvm_max_cpu_policyin !CONFIG_HVM builds.This function is only called after the domain type has already been established, which precludes calling it in a case where max will evaluate to NULL, hence the
ASSERT_UNREACHABLE()just below.
Right, and this will hold when HVM=y but no VMX/SVM was found.
Jan
Re: [PATCH v2 11/15] x86/boot: Merge CPUID policy initialisation logic into cpu-policy.c
Quoted 140 lines · Jan Beulich
Quoted 139 lines · Andrew Cooper
Quoted 122 lines · Jan Beulich
Quoted 121 lines · Andrew Cooper
xen/arch/x86/cpu-policy.c +117
-0@@ -20,10 +26,332 @@ struct cpu_policy __ro_after_init hvm_max_cpu_policy;struct cpu_policy __ro_after_init hvm_def_cpu_policy;#endif +const uint32_t known_features[] =INIT_KNOWN_FEATURES ; + +static const uint32_t __initconst pv_max_featuremask[] =INIT_PV_MAX_FEATURES ; +static const uint32_t hvm_shadow_max_featuremask[] =INIT_HVM_SHADOW_MAX_FEATURES ; +static const uint32_t __initconst hvm_hap_max_featuremask[] = +INIT_HVM_HAP_MAX_FEATURES ; +static const uint32_t __initconst pv_def_featuremask[] =INIT_PV_DEF_FEATURES ; +static const uint32_t __initconst hvm_shadow_def_featuremask[] = +INIT_HVM_SHADOW_DEF_FEATURES ; +static const uint32_t __initconst hvm_hap_def_featuremask[] = +INIT_HVM_HAP_DEF_FEATURES ; +static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; + +static const struct feature_name { +const char *name; +unsigned int bit; +} feature_names[] __initconstrel = INIT_FEATURE_NAMES; + +/* +* Parse a list of cpuid feature names -> bool, calling the callback for any +* matches found. +* +* always_inline, because this is init code only and we really don't want a +* function pointer call in the middle of the loop. +*/ +static int __init always_inline parse_cpuid( +const char *s,void (*callback)(unsigned int feat,bool val)) +{ +const char *ss; +int val, rc =0 ; + +do { +const struct feature_name *lhs, *rhs, *mid =NULL /* GCC... */ ; +const char *feat; + + ss =strchr (s,',' ); +if ( !ss ) + ss =strchr (s,'\0' ); + +/* Skip the 'no-' prefix for name comparisons. */ + feat = s; +if (strncmp (s,"no-" ,3 ) ==0 ) + feat +=3 ; + +/* (Re)initalise lhs and rhs for binary search. */ + lhs = feature_names; + rhs = feature_names +ARRAY_SIZE (feature_names); + +while ( lhs < rhs ) + { +int res; + + mid = lhs + (rhs - lhs) /2 ; + res =cmdline_strcmp (feat, mid->name); + +if ( res <0 ) + { + rhs = mid; +continue ; + } +if ( res >0 ) + { + lhs = mid +1 ; +continue ; + } + +if ( (val =parse_boolean (mid->name, s, ss)) >=0 ) + { +callback (mid->bit, val); + mid =NULL ; + } + +break ; + } + +/* +* Mid being NULL means that the name and boolean were successfully +* identified. Everything else is an error. +*/ +if ( mid ) + rc = -EINVAL ; + + s = ss +1 ; + }while ( *ss ); + +return rc; +} + +static void __init cf_check _parse_xen_cpuid(unsigned int feat, bool val) +{ +if ( !val ) +setup_clear_cpu_cap (feat); +else if ( feat ==X86_FEATURE_RDRAND && + (cpuid_ecx (1 ) &cpufeat_mask (X86_FEATURE_RDRAND )) ) +setup_force_cpu_cap (X86_FEATURE_RDRAND ); +} + +static int __init cf_check parse_xen_cpuid(const char *s) +{ +return parse_cpuid (s, _parse_xen_cpuid); +} +custom_param ("cpuid" , parse_xen_cpuid); + +static bool __initdata dom0_cpuid_cmdline; +static uint32_t __initdata dom0_enable_feat[FSCAPINTS ]; +static uint32_t __initdata dom0_disable_feat[FSCAPINTS ]; + +static void __init cf_check _parse_dom0_cpuid(unsigned int feat, bool val) +{ +__set_bit (feat, val ? dom0_enable_feat : dom0_disable_feat); +__clear_bit (feat, val ? dom0_disable_feat : dom0_enable_feat ); +} + +static int __init cf_check parse_dom0_cpuid(const char *s) +{ + dom0_cpuid_cmdline = true; +Quoted 8 lines
xen/arch/x86/cpu-policy.c struct cpu_policy __ro_after_init hvm_max_cpu_policy; · line 148
Quoted 3 lines
xen/arch/x86/cpu-policy.c +3
-0+return parse_cpuid (s, _parse_dom0_cpuid); +} +custom_param ("dom0-cpuid" , parse_dom0_cpuid);Unless the plan is to completely remove
cpuid.c, this command line handling would imo better fit there. I understand that to keep dom0_{en,dis}able_feat[] static, the_parse_dom0_cpuid()helper would then need to be exposed (under a different name), but I think that's quite okay, the more that it's an__initfunction.I'm not sure I agree. (I did debate this for a while before moving the cmdline parsing.)
I do have some cleanup plans which will move code into
cpuid.c, andguest_cpuid()absolutely still lives there, but for these options specifically, the moment I addMSR_ARCH_CAPSinto a featureset, their bit names names will work here too.
Quoted 8 lines
Quoted 6 lines
So arguably {dom0-}cpuid= don't be a great name moving forwards, but it is is absolutely more
cpu-policy.ccontent thancpuid.ccontent.We can't get rid of the existing cmdline names, and I think documenting our way out of the "it's not only CPUID bits any more" is better than adding yet a new name.
Hmm, yes:
Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/cpu-policy.c int init_domain_cpu_policy(struct domain *d) · line 725
Quoted 12 lines
xen/arch/x86/cpu-policy.c +7
-0@@ -149,3 +716,188 @@ int init_domain_cpu_policy(struct domain *d)return 0 ; } + +void recalculate_cpuid_policy (struct domain *d) +{ +struct cpu_policy *p = d->arch.cpuid; +const struct cpu_policy *max =is_pv_domain (d) + ? (IS_ENABLED (CONFIG_PV ) ? &pv_max_cpu_policy :NULL ) + : (IS_ENABLED (CONFIG_HVM ) ? &hvm_max_cpu_policy :NULL );While this is how the original code was, wouldn't this want to use
hvm_enabled, just likeinit_guest_cpu_policies()does (patch 10)?
Quoted 2 lines
Quoted 1 line
No. That will fail to link.
Why?
hvm_enabledis a #define (to false) only when !HVM.
Hmm, maybe.
But honestly, I want to keep the code as it is because this is trying to only be code-movement, and because it's currently symmetric between the two cases.
~Andrew
PATCH v2 12/15 x86/emul: Switch x86_emulate_ctxt to cpu_policy
In reply to Andrew CooperAs with struct domain, retain cpuid as a valid alias for local code clarity.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Retain cpuid compatibility alias.
* Split out of RFC patch.
--- tools/fuzz/x86_instruction_emulator/fuzz-emul.c | 2 +- tools/tests/x86_emulator/test_x86_emulator.c | 2 +- tools/tests/x86_emulator/x86-emulate.c | 2 +- xen/arch/x86/hvm/emulate.c | 4 ++-- xen/arch/x86/mm/shadow/hvm.c | 2 +- xen/arch/x86/pv/emul-priv-op.c | 2 +- xen/arch/x86/pv/ro-page-fault.c | 2 +- xen/arch/x86/x86_emulate/private.h | 4 ++-- xen/arch/x86/x86_emulate/x86_emulate.h | 7 +++++-- 9 files changed, 15 insertions(+), 12 deletions(-)
tools/fuzz/x86_instruction_emulator/fuzz-emul.c +1 -1
@@ -893,7 +893,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size)struct x86_emulate_ctxt ctxt = { .data = &state, .regs = &input.regs, - .cpuid = &cp, + .cpu_policy = &cp, .addr_size =8 *sizeof (void *), .sp_size =8 *sizeof (void *), };
tools/tests/x86_emulator/test_x86_emulator.c +1 -1
@@ -909,7 +909,7 @@ int main(int argc, char **argv) ctxt.regs = ®s; ctxt.force_writeback =0 ; - ctxt.cpuid = &cp; + ctxt.cpu_policy = &cp; ctxt.lma =sizeof (void *) ==8 ; ctxt.addr_size =8 *sizeof (void *); ctxt.sp_size =8 *sizeof (void *);
tools/tests/x86_emulator/x86-emulate.c +1 -1
@@ -25,7 +25,7 @@#endif uint32_t mxcsr_mask =0x0000ffbf ; -struct cpuid_policy cp; +struct cpu_policy cp; static char fpu_save_area[0x4000] __attribute__((__aligned__((64))));static bool use_xsave;
xen/arch/x86/hvm/emulate.c +2 -2
@@ -2771,7 +2771,7 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla)void hvm_emulate_one_vm_event (enum emul_kind kind,unsigned int trapnr,unsigned int errcode) { -struct hvm_emulate_ctxt ctx = {{0 }}; +struct hvm_emulate_ctxt ctx = {};int rc;hvm_emulate_init_once (&ctx,NULL ,guest_cpu_user_regs ());
@@ -2846,7 +2846,7 @@ void hvm_emulate_init_once( hvmemul_ctxt->validate = validate; hvmemul_ctxt->ctxt.regs = regs; - hvmemul_ctxt->ctxt.cpuid = curr->domain->arch.cpuid; + hvmemul_ctxt->ctxt.cpu_policy = curr->domain->arch.cpu_policy; hvmemul_ctxt->ctxt.force_writeback = true; }
xen/arch/x86/mm/shadow/hvm.c +1 -1
@@ -319,7 +319,7 @@ const struct x86_emulate_ops *shadow_init_emulation(memset (sh_ctxt,0 ,sizeof (*sh_ctxt)); sh_ctxt->ctxt.regs = regs; - sh_ctxt->ctxt.cpuid = curr->domain->arch.cpuid; + sh_ctxt->ctxt.cpu_policy = curr->domain->arch.cpu_policy; sh_ctxt->ctxt.lma =hvm_long_mode_active (curr);/* Segment cache initialisation. Primed with CS. */
xen/arch/x86/pv/emul-priv-op.c +1 -1
@@ -1327,7 +1327,7 @@ int pv_emulate_privileged_op(struct cpu_user_regs *regs)struct domain *currd = curr->domain;struct priv_op_ctxt ctxt = { .ctxt.regs = regs, - .ctxt.cpuid = currd->arch.cpuid, + .ctxt.cpu_policy = currd->arch.cpu_policy, .ctxt.lma = !is_pv_32bit_domain (currd), };int rc;
xen/arch/x86/pv/ro-page-fault.c +1 -1
@@ -356,7 +356,7 @@ int pv_ro_page_fault(unsigned long addr, struct cpu_user_regs *regs)unsigned int addr_size =is_pv_32bit_domain (currd) ?32 :BITS_PER_LONG ;struct x86_emulate_ctxt ctxt = { .regs = regs, - .cpuid = currd->arch.cpuid, + .cpu_policy = currd->arch.cpu_policy, .addr_size = addr_size, .sp_size = addr_size, .lma = addr_size >32 ,
xen/arch/x86/x86_emulate/private.h +2 -2
@@ -505,7 +505,7 @@ in_protmode( })static inline bool -_amd_like (const struct cpuid_policy *cp) +_amd_like (const struct cpu_policy *cp) {return cp->x86_vendor & (X86_VENDOR_AMD |X86_VENDOR_HYGON ); }
@@ -513,7 +513,7 @@ _amd_like(const struct cpuid_policy *cp)static inline bool amd_like (const struct x86_emulate_ctxt *ctxt) { -return _amd_like (ctxt->cpuid); +return _amd_like (ctxt->cpu_policy); }#define vcpu_has_fpu () (ctxt->cpuid->basic.fpu)
xen/arch/x86/x86_emulate/x86_emulate.h +5 -2
@@ -565,8 +565,11 @@ struct x86_emulate_ctxt* Input-only state: */ -/* CPUID Policy for the domain. */ -const struct cpuid_policy *cpuid; +/* CPU policy for the domain. Allow aliases for local code clarity. */ +union { +struct cpu_policy *cpu_policy; +struct cpu_policy *cpuid; + };/* Set this if writes may have side effects. */ bool force_writeback;
Signature · 1 line
2.30.2
Re: [PATCH v2 12/15] x86/emul: Switch x86_emulate_ctxt to cpu_policy
Quoted 3 lines · Andrew Cooper
As with struct domain, retain cpuid as a valid alias for local code clarity.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 13/15 tools/fuzz: Rework afl-policy-fuzzer
In reply to Andrew CooperWith cpuid_policy and msr_policy merged to form cpu_policy, merge the respective fuzzing logic.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
--- tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 57 ++++++++--------------- 1 file changed, 20 insertions(+), 37 deletions(-)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +20 -37
@@ -16,16 +16,19 @@ static bool debug;#define EMPTY_LEAF ((struct cpuid_leaf){}) -static void check_cpuid (struct cpuid_policy *cp) +static void check_policy (struct cpu_policy *cp) { -struct cpuid_policy new = {}; +struct cpu_policy new = {};size_t data_end;xen_cpuid_leaf_t *leaves =malloc (CPUID_MAX_SERIALISED_LEAVES *sizeof (xen_cpuid_leaf_t)); -unsigned int nr =CPUID_MAX_SERIALISED_LEAVES ; +xen_msr_entry_t *msrs =malloc (MSR_MAX_SERIALISED_ENTRIES * +sizeof (xen_cpuid_leaf_t)); +unsigned int nr_leaves =CPUID_MAX_SERIALISED_LEAVES ; +unsigned int nr_msrs =MSR_MAX_SERIALISED_ENTRIES ;int rc; -if ( !leaves ) +if ( !leaves || !msrs )return ;/*
@@ -49,12 +52,19 @@ static void check_cpuid(struct cpuid_policy *cp)x86_cpuid_policy_recalc_synth (cp);/* Serialise... */ - rc =x86_cpuid_copy_to_buffer (cp, leaves, &nr); + rc =x86_cpuid_copy_to_buffer (cp, leaves, &nr_leaves); +assert (rc ==0 ); +assert (nr_leaves <=CPUID_MAX_SERIALISED_LEAVES ); + + rc =x86_msr_copy_to_buffer (cp, msrs, &nr_msrs);assert (rc ==0 ); -assert (nr <=CPUID_MAX_SERIALISED_LEAVES ); +assert (nr_msrs <=MSR_MAX_SERIALISED_ENTRIES );/* ... and deserialise. */ - rc =x86_cpuid_copy_from_buffer (&new, leaves, nr,NULL ,NULL ); + rc =x86_cpuid_copy_from_buffer (&new, leaves, nr_leaves,NULL ,NULL ); +assert (rc ==0 ); + + rc =x86_msr_copy_from_buffer (&new, msrs, nr_msrs,NULL );assert (rc ==0 );/* The result after serialisation/deserialisaion should be identical... */
@@ -76,28 +86,6 @@ static void check_cpuid(struct cpuid_policy *cp)free (leaves); } -static void check_msr (struct msr_policy *mp) -{ -struct msr_policy new = {}; -xen_msr_entry_t *msrs =malloc (MSR_MAX_SERIALISED_ENTRIES * -sizeof (xen_msr_entry_t)); -unsigned int nr =MSR_MAX_SERIALISED_ENTRIES ; -int rc; - -if ( !msrs ) -return ; - - rc =x86_msr_copy_to_buffer (mp, msrs, &nr); -assert (rc ==0 ); -assert (nr <=MSR_MAX_SERIALISED_ENTRIES ); - - rc =x86_msr_copy_from_buffer (&new, msrs, nr,NULL ); -assert (rc ==0 ); -assert (memcmp (mp, &new,sizeof (*mp)) ==0 ); - -free (msrs); -} -int main (int argc,char **argv) {FILE *fp =NULL ;
@@ -144,8 +132,7 @@ int main(int argc, char **argv) while ( __AFL_LOOP(1000) )#endif { -struct cpuid_policy *cp =NULL ; -struct msr_policy *mp =NULL ; +struct cpu_policy *cp =NULL ;if ( fp != stdin ) {
@@ -160,22 +147,18 @@ int main(int argc, char **argv) } cp =calloc (1 ,sizeof (*cp)); - mp =calloc (1 ,sizeof (*mp)); -if ( !cp || !mp ) +if ( !cp ) goto skip;fread (cp,sizeof (*cp),1 , fp); -fread (mp,sizeof (*mp),1 , fp);if ( !feof (fp) ) goto skip; -check_cpuid (cp); -check_msr (mp); +check_policy (cp); skip:free (cp); -free (mp);if ( fp != stdin ) {
Signature · 1 line
2.30.2
Re: [PATCH v2 13/15] tools/fuzz: Rework afl-policy-fuzzer
Quoted 4 lines · Andrew Cooper
With
cpuid_policyandmsr_policymerged to formcpu_policy, merge the respective fuzzing logic.Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 14/15 libx86: Update library API for cpu_policy
In reply to Andrew CooperAdjust the API and comments appropriately.
x86_cpu_policy_fill_native() will eventually contain MSR reads, but leave a TODO in the short term.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
--- tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 4 +-- tools/tests/cpu-policy/test-cpu-policy.c | 4 +-- tools/tests/x86_emulator/x86-emulate.c | 2 +- xen/arch/x86/cpu-policy.c | 2 +- xen/arch/x86/domctl.c | 2 +- xen/arch/x86/xstate.c | 4 +-- xen/include/xen/lib/x86/cpu-policy.h | 42 ++++++++++++----------- xen/lib/x86/cpuid.c | 24 +++++++------ xen/lib/x86/msr.c | 4 +-- 9 files changed, 46 insertions(+), 42 deletions(-)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +2 -2
@@ -48,8 +48,8 @@ static void check_policy(struct cpu_policy *cp)* Fix up the data in the source policy which isn't expected to survive * serialisation. */ -x86_cpuid_policy_clear_out_of_range_leaves (cp); -x86_cpuid_policy_recalc_synth (cp); +x86_cpu_policy_clear_out_of_range_leaves (cp); +x86_cpu_policy_recalc_synth (cp);/* Serialise... */ rc =x86_cpuid_copy_to_buffer (cp, leaves, &nr_leaves);
tools/tests/cpu-policy/test-cpu-policy.c +2 -2
@@ -105,7 +105,7 @@ static void test_cpuid_current(void)printf ("Testing CPUID on current CPU\n" ); -x86_cpuid_policy_fill_native (&p); +x86_cpu_policy_fill_native (&p); rc =x86_cpuid_copy_to_buffer (&p, leaves, &nr);if ( rc !=0 )
@@ -554,7 +554,7 @@ static void test_cpuid_out_of_range_clearing(void)void *ptr;unsigned int nr_markers; -x86_cpuid_policy_clear_out_of_range_leaves (p); +x86_cpu_policy_clear_out_of_range_leaves (p);/* Count the number of 0xc2's still remaining. */ for ( ptr = p, nr_markers = 0;
tools/tests/x86_emulator/x86-emulate.c +1 -1
@@ -75,7 +75,7 @@ bool emul_test_init(void)unsigned long sp; -x86_cpuid_policy_fill_native (&cp); +x86_cpu_policy_fill_native (&cp);/* * The emulator doesn't use these instructions, so can always emulate
xen/arch/x86/cpu-policy.c +1 -1
@@ -347,7 +347,7 @@ static void __init calculate_raw_policy(void) {struct cpu_policy *p = &raw_cpu_policy; -x86_cpuid_policy_fill_native (p); +x86_cpu_policy_fill_native (p);/* Nothing good will come from Xen and libx86 disagreeing on vendor. */ ASSERT (p->x86_vendor == boot_cpu_data.x86_vendor);
xen/arch/x86/domctl.c +1 -1
@@ -66,7 +66,7 @@ static int update_domain_cpu_policy(struct domain *d, goto out;/* Trim any newly-stale out-of-range leaves. */ -x86_cpuid_policy_clear_out_of_range_leaves (new); +x86_cpu_policy_clear_out_of_range_leaves (new);/* Audit the combined dataset. */ ret =x86_cpu_policies_are_compatible (sys, new, &err);
xen/arch/x86/xstate.c +2 -2
@@ -684,7 +684,7 @@ void xstate_init(struct cpuinfo_x86 *c)int validate_xstate (const struct domain *d,uint64_t xcr0,uint64_t xcr0_accum,const struct xsave_hdr *hdr) { -uint64_t xcr0_max =cpuid_policy_xcr0_max (d->arch.cpuid); +uint64_t xcr0_max =cpu_policy_xcr0_max (d->arch.cpuid);unsigned int i; if ( (hdr->xstate_bv & ~xcr0_accum) ||
@@ -708,7 +708,7 @@ int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum,int handle_xsetbv (u32 index,u64 new_bv) {struct vcpu *curr = current; -uint64_t xcr0_max =cpuid_policy_xcr0_max (curr->domain->arch.cpuid); +uint64_t xcr0_max =cpu_policy_xcr0_max (curr->domain->arch.cpuid);u64 mask;if ( index !=XCR_XFEATURE_ENABLED_MASK )
xen/include/xen/lib/x86/cpu-policy.h +22 -20
122 lines not on this page · raw message
xen/lib/x86/cpuid.c +13 -11
@@ -102,13 +102,13 @@ void x86_cpu_featureset_to_policy( p->feat._7d1 = fs[FEATURESET_7d1]; } -void x86_cpuid_policy_recalc_synth (struct cpuid_policy *p) +void x86_cpu_policy_recalc_synth (struct cpu_policy *p) { p->x86_vendor =x86_cpuid_lookup_vendor ( p->basic.vendor_ebx, p->basic.vendor_ecx, p->basic.vendor_edx); } -void x86_cpuid_policy_fill_native (struct cpuid_policy *p) +void x86_cpu_policy_fill_native (struct cpu_policy *p) {unsigned int i;
@@ -199,7 +199,7 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p)cpuid_count_leaf (0xd ,0 , &p->xstate.raw[0 ]);cpuid_count_leaf (0xd ,1 , &p->xstate.raw[1 ]); - xstates =cpuid_policy_xstates (p); + xstates =cpu_policy_xstates (p);/* This logic will probably need adjusting when XCR0[63] gets used. */ BUILD_BUG_ON (ARRAY_SIZE (p->xstate.raw) >63 );
@@ -222,10 +222,12 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p) p->hv_limit =0 ; p->hv2_limit =0 ; -x86_cpuid_policy_recalc_synth (p); +/* TODO MSRs */ + +x86_cpu_policy_recalc_synth (p); } -void x86_cpuid_policy_clear_out_of_range_leaves (struct cpuid_policy *p) +void x86_cpu_policy_clear_out_of_range_leaves (struct cpu_policy *p) {unsigned int i;
@@ -260,7 +262,7 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p)zero_leaves (p->topo.raw, i,ARRAY_SIZE (p->topo.raw) -1 ); } -if ( p->basic.max_leaf <0xd || !cpuid_policy_xstates (p) ) +if ( p->basic.max_leaf <0xd || !cpu_policy_xstates (p) )memset (p->xstate.raw,0 ,sizeof (p->xstate.raw));else {
@@ -268,7 +270,7 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p)BUILD_BUG_ON (ARRAY_SIZE (p->xstate.raw) >63 );/* First two leaves always valid. Rest depend on xstates. */ - i =max (2 ,64 -__builtin_clzll (cpuid_policy_xstates (p))); + i =max (2 ,64 -__builtin_clzll (cpu_policy_xstates (p)));zero_leaves (p->xstate.raw, i,ARRAY_SIZE (p->xstate.raw) -1 );
@@ -333,7 +335,7 @@ static int copy_leaf_to_buffer(uint32_t leaf, uint32_t subleaf,return 0 ; } -int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +int x86_cpuid_copy_to_buffer (const struct cpu_policy *p,cpuid_leaf_buffer_t leaves,uint32_t *nr_entries_p) {const uint32_t nr_entries = *nr_entries_p;
@@ -383,7 +385,7 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,case 0xd : { -uint64_t xstates =cpuid_policy_xstates (p); +uint64_t xstates =cpu_policy_xstates (p);COPY_LEAF (leaf,0 , &p->xstate.raw[0 ]);COPY_LEAF (leaf,1 , &p->xstate.raw[1 ]);
@@ -419,7 +421,7 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,return 0 ; } -int x86_cpuid_copy_from_buffer (struct cpuid_policy *p, +int x86_cpuid_copy_from_buffer (struct cpu_policy *p,const cpuid_leaf_buffer_t leaves,uint32_t nr_entries,uint32_t *err_leaf,uint32_t *err_subleaf)
@@ -522,7 +524,7 @@ int x86_cpuid_copy_from_buffer(struct cpuid_policy *p, } } -x86_cpuid_policy_recalc_synth (p); +x86_cpu_policy_recalc_synth (p);return 0 ;
xen/lib/x86/msr.c +2 -2
@@ -23,7 +23,7 @@ static int copy_msr_to_buffer(uint32_t idx, uint64_t val,return 0 ; } -int x86_msr_copy_to_buffer (const struct msr_policy *p, +int x86_msr_copy_to_buffer (const struct cpu_policy *p,msr_entry_buffer_t msrs,uint32_t *nr_entries_p) {const uint32_t nr_entries = *nr_entries_p;
@@ -48,7 +48,7 @@ int x86_msr_copy_to_buffer(const struct msr_policy *p,return 0 ; } -int x86_msr_copy_from_buffer (struct msr_policy *p, +int x86_msr_copy_from_buffer (struct cpu_policy *p,const msr_entry_buffer_t msrs,uint32_t nr_entries,uint32_t *err_msr) {
Signature · 1 line
2.30.2
Re: [PATCH v2 14/15] libx86: Update library API for cpu_policy
Quoted 4 lines · Andrew Cooper
Adjust the API and comments appropriately.
x86_cpu_policy_fill_native()will eventually contain MSR reads, but leave a TODO in the short term.
That'll then require passing in a callback function anyway, such that different environments can use different ways of getting at the wanted MSR values. (IOW a bigger change anyway.)
Quoted 3 lines
No practical change.
Signed-off-by Andrew Cooper <[email protected]>
What about x86_cpuid_lookup_deep_deps()? That'll be looking at more than just CPUID bits as well, won't it?
Jan
Re: [PATCH v2 14/15] libx86: Update library API for cpu_policy
Quoted 8 lines · Jan Beulich
Quoted 4 lines · Andrew Cooper
Adjust the API and comments appropriately.
x86_cpu_policy_fill_native()will eventually contain MSR reads, but leave a TODO in the short term.That'll then require passing in a callback function anyway, such that different environments can use different ways of getting at the wanted MSR values. (IOW a bigger change anyway.)
We've already got #if __XEN__'s in there already. I was going to add one more in the short term.
Quoted 6 lines
Quoted 4 lines
No practical change.
Signed-off-by Andrew Cooper <[email protected]>What about
x86_cpuid_lookup_deep_deps()? That'll be looking at more than just CPUID bits as well, won't it?
Good point. I'll adjust.
~Andrew
PATCH v3 1/1 libx86: Update library API for cpu_policy
In reply to Andrew CooperAdjust the API and comments appropriately.
x86_cpu_policy_fill_native() will eventually contain MSR reads, but leave a TODO in the short term.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* New
v3:
* Update x86_cpu_policy_lookup_deep_deps() too and write an API doc.
--- tools/fuzz/cpu-policy/afl-policy-fuzzer.c | 4 +- tools/tests/cpu-policy/test-cpu-policy.c | 4 +- tools/tests/x86_emulator/x86-emulate.c | 2 +- xen/arch/x86/cpu-policy.c | 4 +- xen/arch/x86/cpu/common.c | 2 +- xen/arch/x86/domctl.c | 2 +- xen/arch/x86/xstate.c | 4 +- xen/include/xen/lib/x86/cpu-policy.h | 49 +++++++++++++---------- xen/lib/x86/cpuid.c | 26 ++++++------ xen/lib/x86/msr.c | 4 +- 10 files changed, 55 insertions(+), 46 deletions(-)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +2 -2
@@ -48,8 +48,8 @@ static void check_policy(struct cpu_policy *cp)* Fix up the data in the source policy which isn't expected to survive * serialisation. */ -x86_cpuid_policy_clear_out_of_range_leaves (cp); -x86_cpuid_policy_recalc_synth (cp); +x86_cpu_policy_clear_out_of_range_leaves (cp); +x86_cpu_policy_recalc_synth (cp);/* Serialise... */ rc =x86_cpuid_copy_to_buffer (cp, leaves, &nr_leaves);
tools/tests/cpu-policy/test-cpu-policy.c +2 -2
@@ -105,7 +105,7 @@ static void test_cpuid_current(void)printf ("Testing CPUID on current CPU\n" ); -x86_cpuid_policy_fill_native (&p); +x86_cpu_policy_fill_native (&p); rc =x86_cpuid_copy_to_buffer (&p, leaves, &nr);if ( rc !=0 )
@@ -554,7 +554,7 @@ static void test_cpuid_out_of_range_clearing(void)void *ptr;unsigned int nr_markers; -x86_cpuid_policy_clear_out_of_range_leaves (p); +x86_cpu_policy_clear_out_of_range_leaves (p);/* Count the number of 0xc2's still remaining. */ for ( ptr = p, nr_markers = 0;
tools/tests/x86_emulator/x86-emulate.c +1 -1
@@ -75,7 +75,7 @@ bool emul_test_init(void)unsigned long sp; -x86_cpuid_policy_fill_native (&cp); +x86_cpu_policy_fill_native (&cp);/* * The emulator doesn't use these instructions, so can always emulate
xen/arch/x86/cpu-policy.c +2 -2
@@ -176,7 +176,7 @@ static void sanitise_featureset(uint32_t *fs)for_each_set_bit (i, (void *)disabled_features,sizeof (disabled_features) *8 ) { -const uint32_t *dfs =x86_cpuid_lookup_deep_deps (i); +const uint32_t *dfs =x86_cpu_policy_lookup_deep_deps (i);unsigned int j;ASSERT (dfs);/* deep_features[] should guarentee this. */
@@ -347,7 +347,7 @@ static void __init calculate_raw_policy(void) {struct cpu_policy *p = &raw_cpu_policy; -x86_cpuid_policy_fill_native (p); +x86_cpu_policy_fill_native (p);/* Nothing good will come from Xen and libx86 disagreeing on vendor. */ ASSERT (p->x86_vendor == boot_cpu_data.x86_vendor);
xen/arch/x86/cpu/common.c +1 -1
@@ -75,7 +75,7 @@ void __init setup_clear_cpu_cap(unsigned int cap) __builtin_return_address(0), cap);__clear_bit (cap, boot_cpu_data.x86_capability); - dfs =x86_cpuid_lookup_deep_deps (cap); + dfs =x86_cpu_policy_lookup_deep_deps (cap);if (!dfs)return ;
xen/arch/x86/domctl.c +1 -1
@@ -66,7 +66,7 @@ static int update_domain_cpu_policy(struct domain *d, goto out;/* Trim any newly-stale out-of-range leaves. */ -x86_cpuid_policy_clear_out_of_range_leaves (new); +x86_cpu_policy_clear_out_of_range_leaves (new);/* Audit the combined dataset. */ ret =x86_cpu_policies_are_compatible (sys, new, &err);
xen/arch/x86/xstate.c +2 -2
@@ -684,7 +684,7 @@ void xstate_init(struct cpuinfo_x86 *c)int validate_xstate (const struct domain *d,uint64_t xcr0,uint64_t xcr0_accum,const struct xsave_hdr *hdr) { -uint64_t xcr0_max =cpuid_policy_xcr0_max (d->arch.cpuid); +uint64_t xcr0_max =cpu_policy_xcr0_max (d->arch.cpuid);unsigned int i; if ( (hdr->xstate_bv & ~xcr0_accum) ||
@@ -708,7 +708,7 @@ int validate_xstate(const struct domain *d, uint64_t xcr0, uint64_t xcr0_accum,int handle_xsetbv (u32 index,u64 new_bv) {struct vcpu *curr = current; -uint64_t xcr0_max =cpuid_policy_xcr0_max (curr->domain->arch.cpuid); +uint64_t xcr0_max =cpu_policy_xcr0_max (curr->domain->arch.cpuid);u64 mask;if ( index !=XCR_XFEATURE_ENABLED_MASK )
xen/include/xen/lib/x86/cpu-policy.h +28 -21
131 lines not on this page · raw message
xen/lib/x86/cpuid.c +14 -12
@@ -102,13 +102,13 @@ void x86_cpu_featureset_to_policy( p->feat._7d1 = fs[FEATURESET_7d1]; } -void x86_cpuid_policy_recalc_synth (struct cpuid_policy *p) +void x86_cpu_policy_recalc_synth (struct cpu_policy *p) { p->x86_vendor =x86_cpuid_lookup_vendor ( p->basic.vendor_ebx, p->basic.vendor_ecx, p->basic.vendor_edx); } -void x86_cpuid_policy_fill_native (struct cpuid_policy *p) +void x86_cpu_policy_fill_native (struct cpu_policy *p) {unsigned int i;
@@ -199,7 +199,7 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p)cpuid_count_leaf (0xd ,0 , &p->xstate.raw[0 ]);cpuid_count_leaf (0xd ,1 , &p->xstate.raw[1 ]); - xstates =cpuid_policy_xstates (p); + xstates =cpu_policy_xstates (p);/* This logic will probably need adjusting when XCR0[63] gets used. */ BUILD_BUG_ON (ARRAY_SIZE (p->xstate.raw) >63 );
@@ -222,10 +222,12 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p) p->hv_limit =0 ; p->hv2_limit =0 ; -x86_cpuid_policy_recalc_synth (p); +/* TODO MSRs */ + +x86_cpu_policy_recalc_synth (p); } -void x86_cpuid_policy_clear_out_of_range_leaves (struct cpuid_policy *p) +void x86_cpu_policy_clear_out_of_range_leaves (struct cpu_policy *p) {unsigned int i;
@@ -260,7 +262,7 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p)zero_leaves (p->topo.raw, i,ARRAY_SIZE (p->topo.raw) -1 ); } -if ( p->basic.max_leaf <0xd || !cpuid_policy_xstates (p) ) +if ( p->basic.max_leaf <0xd || !cpu_policy_xstates (p) )memset (p->xstate.raw,0 ,sizeof (p->xstate.raw));else {
@@ -268,7 +270,7 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p)BUILD_BUG_ON (ARRAY_SIZE (p->xstate.raw) >63 );/* First two leaves always valid. Rest depend on xstates. */ - i =max (2 ,64 -__builtin_clzll (cpuid_policy_xstates (p))); + i =max (2 ,64 -__builtin_clzll (cpu_policy_xstates (p)));zero_leaves (p->xstate.raw, i,ARRAY_SIZE (p->xstate.raw) -1 );
@@ -278,7 +280,7 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p) ARRAY_SIZE(p->extd.raw) - 1); } -const uint32_t *x86_cpuid_lookup_deep_deps (uint32_t feature) +const uint32_t *x86_cpu_policy_lookup_deep_deps (uint32_t feature) {static const uint32_t deep_features[] =INIT_DEEP_FEATURES ; static const struct {
@@ -333,7 +335,7 @@ static int copy_leaf_to_buffer(uint32_t leaf, uint32_t subleaf,return 0 ; } -int x86_cpuid_copy_to_buffer (const struct cpuid_policy *p, +int x86_cpuid_copy_to_buffer (const struct cpu_policy *p,cpuid_leaf_buffer_t leaves,uint32_t *nr_entries_p) {const uint32_t nr_entries = *nr_entries_p;
@@ -383,7 +385,7 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,case 0xd : { -uint64_t xstates =cpuid_policy_xstates (p); +uint64_t xstates =cpu_policy_xstates (p);COPY_LEAF (leaf,0 , &p->xstate.raw[0 ]);COPY_LEAF (leaf,1 , &p->xstate.raw[1 ]);
@@ -419,7 +421,7 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,return 0 ; } -int x86_cpuid_copy_from_buffer (struct cpuid_policy *p, +int x86_cpuid_copy_from_buffer (struct cpu_policy *p,const cpuid_leaf_buffer_t leaves,uint32_t nr_entries,uint32_t *err_leaf,uint32_t *err_subleaf)
@@ -522,7 +524,7 @@ int x86_cpuid_copy_from_buffer(struct cpuid_policy *p, } } -x86_cpuid_policy_recalc_synth (p); +x86_cpu_policy_recalc_synth (p);return 0 ;
xen/lib/x86/msr.c +2 -2
@@ -23,7 +23,7 @@ static int copy_msr_to_buffer(uint32_t idx, uint64_t val,return 0 ; } -int x86_msr_copy_to_buffer (const struct msr_policy *p, +int x86_msr_copy_to_buffer (const struct cpu_policy *p,msr_entry_buffer_t msrs,uint32_t *nr_entries_p) {const uint32_t nr_entries = *nr_entries_p;
@@ -48,7 +48,7 @@ int x86_msr_copy_to_buffer(const struct msr_policy *p,return 0 ; } -int x86_msr_copy_from_buffer (struct msr_policy *p, +int x86_msr_copy_from_buffer (struct cpu_policy *p,const msr_entry_buffer_t msrs,uint32_t nr_entries,uint32_t *err_msr) {
Signature · 1 line
2.30.2
Re: [PATCH v3] libx86: Update library API for cpu_policy
Quoted 8 lines · Andrew Cooper
Adjust the API and comments appropriately.
x86_cpu_policy_fill_native()will eventually contain MSR reads, but leave a TODO in the short term.No practical change.
Signed-off-by Andrew Cooper <[email protected]>
PATCH v2 15/15 x86: Remove temporary {cpuid,msr}_policy defines
In reply to Andrew CooperWith all code areas updated, drop the temporary defines and adjust all remaining users.
No practical change.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
v2:
* Split out of RFC patch
--- xen/arch/x86/cpu/mcheck/mce_intel.c | 2 +- xen/arch/x86/cpuid.c | 2 +- xen/arch/x86/domain.c | 2 +- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/svm/svm.c | 2 +- xen/arch/x86/hvm/vlapic.c | 2 +- xen/arch/x86/hvm/vmx/vmx.c | 8 ++++---- xen/arch/x86/include/asm/msr.h | 2 +- xen/arch/x86/msr.c | 20 +++++++++----------- xen/arch/x86/pv/domain.c | 2 +- xen/arch/x86/pv/emul-priv-op.c | 4 ++-- xen/arch/x86/traps.c | 2 +- xen/arch/x86/x86_emulate/x86_emulate.c | 2 +- xen/include/xen/lib/x86/cpu-policy.h | 4 ---- 14 files changed, 26 insertions(+), 32 deletions(-)
xen/arch/x86/cpu/mcheck/mce_intel.c +1 -1
@@ -1008,7 +1008,7 @@ int vmce_intel_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)int vmce_intel_rdmsr (const struct vcpu *v,uint32_t msr,uint64_t *val) { -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;unsigned int bank = msr -MSR_IA32_MC0_CTL2 ; switch ( msr )
xen/arch/x86/cpuid.c +1 -1
@@ -36,7 +36,7 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *res) { const struct domain *d = v->domain; -const struct cpuid_policy *p = d->arch.cpuid; +const struct cpu_policy *p = d->arch.cpu_policy; *res =EMPTY_LEAF ;
xen/arch/x86/domain.c +1 -1
@@ -283,7 +283,7 @@ void update_guest_memory_policy(struct vcpu *v,void domain_cpu_policy_changed (struct domain *d) { -const struct cpuid_policy *p = d->arch.cpuid; +const struct cpu_policy *p = d->arch.cpu_policy;struct vcpu *v;if (is_pv_domain (d) )
xen/arch/x86/hvm/hvm.c +2 -2
@@ -924,7 +924,7 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value, signed int cr0_pg) {const struct domain *d = v->domain; -const struct cpuid_policy *p = d->arch.cpuid; +const struct cpu_policy *p = d->arch.cpu_policy;if ( value & ~EFER_KNOWN_MASK )return "Unknown bits set" ;
@@ -961,7 +961,7 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value,/* These bits in CR4 can be set by the guest. */ unsigned long hvm_cr4_guest_valid_bits (const struct domain *d) { -const struct cpuid_policy *p = d->arch.cpuid; +const struct cpu_policy *p = d->arch.cpu_policy;bool mce, vmxe, cet;/* Logic broken out simply to aid readability below. */
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -583,7 +583,7 @@ static void cf_check svm_cpuid_policy_changed(struct vcpu *v) {struct svm_vcpu *svm = &v->arch.hvm.svm;struct vmcb_struct *vmcb = svm->vmcb; -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;u32 bitmap =vmcb_get_exception_intercepts (vmcb); if ( opt_hvm_fep ||
xen/arch/x86/hvm/vlapic.c +1 -1
@@ -1083,7 +1083,7 @@ static void set_x2apic_id(struct vlapic *vlapic)int guest_wrmsr_apic_base (struct vcpu *v,uint64_t value) { -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;struct vlapic *vlapic =vcpu_vlapic (v);if ( !has_vlapic (v->domain) )
xen/arch/x86/hvm/vmx/vmx.c +4 -4
@@ -710,7 +710,7 @@ static void vmx_restore_host_msrs(void)static void vmx_save_guest_msrs (struct vcpu *v) { -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;struct vcpu_msrs *msrs = v->arch.msrs;/*
@@ -731,7 +731,7 @@ static void vmx_save_guest_msrs(struct vcpu *v)static void vmx_restore_guest_msrs (struct vcpu *v) { -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;const struct vcpu_msrs *msrs = v->arch.msrs;write_gs_shadow (v->arch.hvm.vmx.shadow_gs);
@@ -784,7 +784,7 @@ void vmx_update_exception_bitmap(struct vcpu *v) static void cf_check vmx_cpuid_policy_changed(struct vcpu *v) { -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;int rc =0 ; if ( opt_hvm_fep ||
@@ -3521,7 +3521,7 @@ static int cf_check vmx_msr_write_intercept( unsigned int msr, uint64_t msr_content) {struct vcpu *v = current; -const struct cpuid_policy *cp = v->domain->arch.cpuid; +const struct cpu_policy *cp = v->domain->arch.cpu_policy;HVM_DBG_LOG (DBG_LEVEL_MSR ,"ecx=%#x, msr_value=%#" PRIx64, msr, msr_content);
xen/arch/x86/include/asm/msr.h +1 -1
@@ -290,7 +290,7 @@ static inline void wrmsr_tsc_aux(uint32_t val) } } -uint64_t msr_spec_ctrl_valid_bits (const struct cpuid_policy *cp); +uint64_t msr_spec_ctrl_valid_bits (const struct cpu_policy *cp);/* Container object for per-vCPU MSRs */ struct vcpu_msrs
xen/arch/x86/msr.c +9 -11
@@ -54,8 +54,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) {const struct vcpu *curr = current;const struct domain *d = v->domain; -const struct cpuid_policy *cp = d->arch.cpuid; -const struct msr_policy *mp = d->arch.msr; +const struct cpu_policy *cp = d->arch.cpu_policy;const struct vcpu_msrs *msrs = v->arch.msrs;int ret =X86EMUL_OKAY ;
@@ -139,13 +138,13 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) goto get_reg;case MSR_INTEL_PLATFORM_INFO : - *val = mp->platform_info.raw; + *val = cp->platform_info.raw;break ;case MSR_ARCH_CAPABILITIES :if ( !cp->feat.arch_caps ) goto gp_fault; - *val = mp->arch_caps.raw; + *val = cp->arch_caps.raw;break ;case MSR_INTEL_MISC_FEATURES_ENABLES :
@@ -326,7 +325,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)* separate CPUID features for this functionality, but only set will be * active. */ -uint64_t msr_spec_ctrl_valid_bits (const struct cpuid_policy *cp) +uint64_t msr_spec_ctrl_valid_bits (const struct cpu_policy *cp) {bool ssbd = cp->feat.ssbd || cp->extd.amd_ssbd;bool psfd = cp->feat.intel_psfd || cp->extd.psfd;
@@ -345,8 +344,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) {const struct vcpu *curr = current;struct domain *d = v->domain; -const struct cpuid_policy *cp = d->arch.cpuid; -const struct msr_policy *mp = d->arch.msr; +const struct cpu_policy *cp = d->arch.cpu_policy;struct vcpu_msrs *msrs = v->arch.msrs;int ret =X86EMUL_OKAY ;
@@ -387,7 +385,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)* for backwards compatiblity, the OS should write 0 to it before * trying to access the current microcode version. */ -if ( d->arch.cpuid->x86_vendor !=X86_VENDOR_INTEL || val !=0 ) +if ( cp->x86_vendor !=X86_VENDOR_INTEL || val !=0 ) goto gp_fault;break ;
@@ -397,7 +395,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)* to AMD CPUs as well (at least the architectural/CPUID part does). */ if (is_pv_domain (d) || - d->arch.cpuid->x86_vendor !=X86_VENDOR_AMD ) + cp->x86_vendor !=X86_VENDOR_AMD ) goto gp_fault;break ;
@@ -409,7 +407,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)* by any CPUID bit. */ if (is_pv_domain (d) || - d->arch.cpuid->x86_vendor !=X86_VENDOR_INTEL ) + cp->x86_vendor !=X86_VENDOR_INTEL ) goto gp_fault;break ;
@@ -446,7 +444,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)bool old_cpuid_faulting = msrs->misc_features_enables.cpuid_faulting; rsvd = ~0ull ; -if ( mp->platform_info.cpuid_faulting ) +if ( cp->platform_info.cpuid_faulting ) rsvd &= ~MSR_MISC_FEATURES_CPUID_FAULTING ;if ( val & rsvd )
xen/arch/x86/pv/domain.c +1 -1
@@ -146,7 +146,7 @@ static void release_compat_l4(struct vcpu *v)unsigned long pv_fixup_guest_cr4 (const struct vcpu *v,unsigned long cr4) { -const struct cpuid_policy *p = v->domain->arch.cpuid; +const struct cpu_policy *p = v->domain->arch.cpu_policy;/* Discard attempts to set guest controllable bits outside of the policy. */ cr4 &= ~((p->basic.tsc ? 0 : X86_CR4_TSD) |
xen/arch/x86/pv/emul-priv-op.c +2 -2
@@ -885,7 +885,7 @@ static int cf_check read_msr( {struct vcpu *curr = current;const struct domain *currd = curr->domain; -const struct cpuid_policy *cp = currd->arch.cpuid; +const struct cpu_policy *cp = currd->arch.cpu_policy;bool vpmu_msr = false, warn = false;uint64_t tmp;int ret;
@@ -1034,7 +1034,7 @@ static int cf_check write_msr( {struct vcpu *curr = current;const struct domain *currd = curr->domain; -const struct cpuid_policy *cp = currd->arch.cpuid; +const struct cpu_policy *cp = currd->arch.cpu_policy;bool vpmu_msr = false;int ret;
xen/arch/x86/traps.c +1 -1
@@ -1036,7 +1036,7 @@ void cpuid_hypervisor_leaves(const struct vcpu *v, uint32_t leaf, uint32_t subleaf, struct cpuid_leaf *res) { const struct domain *d = v->domain; -const struct cpuid_policy *p = d->arch.cpuid; +const struct cpu_policy *p = d->arch.cpu_policy;uint32_t base =is_viridian_domain (d) ?0x40000100 :0x40000000 ;uint32_t idx = leaf - base;unsigned int limit =is_viridian_domain (d) ? p->hv2_limit : p->hv_limit;
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -848,7 +848,7 @@ protmode_load_seg(struct x86_emulate_ctxt *ctxt, const struct x86_emulate_ops *ops) { - const struct cpuid_policy *cp = ctxt->cpuid; + const struct cpu_policy *cp = ctxt->cpu_policy;enum x86_segment sel_seg = (sel &4 ) ? x86_seg_ldtr : x86_seg_gdtr;struct {uint32_t a, b; } desc, desc_hi = {};uint8_t dpl, rpl;
xen/include/xen/lib/x86/cpu-policy.h +0 -4
@@ -375,10 +375,6 @@ struct cpu_policyuint8_t x86_vendor; }; -/* Temporary */ -#define cpuid_policy cpu_policy -#define msr_policy cpu_policy -struct cpu_policy_errors {uint32_t leaf, subleaf;
Signature · 1 line
2.30.2
Re: [PATCH v2 15/15] x86: Remove temporary {cpuid,msr}_policy defines
Quoted 6 lines · Andrew Cooper
With all code areas updated, drop the temporary defines and adjust all remaining users.
No practical change.
Signed-off-by Andrew Cooper <[email protected]>