VMX MSRs policy for Nested Virt: part 1
v2 cover VMX MSRs policy for Nested Virt: part 1
The end goal of having VMX MSRs policy is to be able to manage L1 VMX features. This patch series is the first part of this work. There is no functional change to what L1 sees in VMX MSRs at this point. But each domain will have a policy object which allows to sensibly query what VMX features the domain has. This will unblock some other nested virtualization work items.
Currently, when nested virt is enabled, the set of L1 VMX features is fixed and calculated by nvmx_msr_read_intercept() as an intersection between the full set of Xen's supported L1 VMX features, the set of actual H/W features and, for MSR_IA32_VMX_EPT_VPID_CAP, the set of features that Xen uses.
The above makes L1 VMX feature set inconsistent between different H/W and there is no ability to control what features are available to L1. The overall set of issues has much in common with CPUID policy.
Part 1 introduces struct vmx_msr_policy and the following instances:
* Raw policy (raw_vmx_msr_policy) -- the actual contents of H/W VMX MSRs
* VVMX max policy (vvmx_max_msr_policy) -- the end result of nvmx_msr_read_intercept() on current H/W
* Per-domain policy (d->arch.vmx_msr) -- the copy of VVMX max policy (for now)
In the future it should be possible to independently configure the VMX policy for each domain using some new domctl.
There is no "Host policy" object because Xen already has a set of variables (vmx_pin_based_exec_control and others) which represent the set of VMX features that Xen uses. There are features that Xen doesn't use (e.g. CPU_BASED_PAUSE_EXITING) but they are available to L1. This makes it not worthy to introduce "Host policy" at this stage.
v1 --> v2:
- Rebased to the latest master
- hvm_max_vmx_msr_policy is renamed to vvmx_max_msr_policy
- Dropped the debug patch
- Other changes are available on a per-patch basis
Sergey Dyasli (5):
x86/vmx: add struct vmx_msr_policy
x86/vmx: add raw_vmx_msr_policy
x86/vmx: refactor vmx_init_vmcs_config()
x86/vvmx: add vvmx_max_msr_policy
x86/vvmx: add per domain vmx msr policy
xen/arch/x86/domain.c | 6 + xen/arch/x86/hvm/vmx/vmcs.c | 269 +++++++++++++++++--------- xen/arch/x86/hvm/vmx/vmx.c | 2 + xen/arch/x86/hvm/vmx/vvmx.c | 296 ++++++++++++++-------------- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/hvm/vmx/vmcs.h | 383 +++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/vmx/vvmx.h | 3 + xen/include/asm-x86/msr-index.h | 1 + 8 files changed, 722 insertions(+), 240 deletions(-)
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v2 1/5 x86/vmx: add struct vmx_msr_policy
This structure provides a convenient way of accessing contents of VMX MSRs: every bit value is accessible by its name. Bit names match existing Xen's definitions as close as possible. The structure also contains the bitmap of available MSRs since not all of them may be available on a particular H/W.
A set of helper functions is introduced to provide a simple way of interacting with the new structure.
---
v1 --> v2:
- Replaced MSR indices with MSR names in struct vmx_msr_policy's comments
- Named "always zero bit" 31 of basic msr as mbz
- Added placeholder bits into union vmfunc
- Added structures cr0_bits and cr4_bits
- Added MSR_IA32_VMX_LAST define to use instead of MSR_IA32_VMX_VMFUNC
- vmx_msr_available() now uses pointer to const struct vmx_msr_policy
- build_assertions() now uses local struct vmx_msr_policy
- Added BUILD_BUG_ON to check that width of vmx_msr_policy::available bitmap is enough for all existing VMX MSRs
- Helpers get_vmx_msr_val(), get_vmx_msr_ptr() and gen_vmx_msr_mask() are added
xen/arch/x86/hvm/vmx/vmcs.c | 78 ++++++++ xen/include/asm-x86/hvm/vmx/vmcs.h | 380 +++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/msr-index.h | 1 + 3 files changed, 459 insertions(+)
xen/arch/x86/hvm/vmx/vmcs.c +78 -0
@@ -144,6 +144,40 @@ static void __init vmx_display_features(void)printk (" - none\n" ); } +bool vmx_msr_available (const struct vmx_msr_policy *p,uint32_t msr) +{ +if ( msr <MSR_IA32_VMX_BASIC || msr >MSR_IA32_VMX_LAST ) +return 0 ; + +return p->available & (1u << (msr -MSR_IA32_VMX_BASIC )); +} + +uint64_t get_vmx_msr_val (const struct vmx_msr_policy *p,uint32_t msr) +{ +if ( !vmx_msr_available (p, msr)) +return 0 ; + +return p->msr[msr -MSR_IA32_VMX_BASIC ]; +} + +uint64_t *get_vmx_msr_ptr (struct vmx_msr_policy *p,uint32_t msr) +{ +if ( !vmx_msr_available (p, msr)) +return NULL ; + +return &p->msr[msr -MSR_IA32_VMX_BASIC ]; +} + +uint32_t gen_vmx_msr_mask (uint32_t start_msr,uint32_t end_msr) +{ +if ( start_msr <MSR_IA32_VMX_BASIC || start_msr >MSR_IA32_VMX_LAST || + end_msr <MSR_IA32_VMX_BASIC || end_msr >MSR_IA32_VMX_LAST ) +return 0 ; + +return ((1u << (end_msr - start_msr +1 )) -1 ) << + (start_msr -MSR_IA32_VMX_BASIC ); +} +static u32 adjust_vmx_controls (const char *name,u32 ctl_min,u32 ctl_opt,u32 msr,bool_t *mismatch) {
@@ -1956,6 +1990,50 @@ void __init setup_vmcs_dump(void)register_keyhandler ('v' , vmcs_dump,"dump VT-x VMCSs" ,1 ); } +static void __init __maybe_unused build_assertions (void ) +{ +struct vmx_msr_policy policy; + +BUILD_BUG_ON (sizeof (policy.basic) != +sizeof (policy.basic.raw)); +BUILD_BUG_ON (sizeof (policy.pinbased_ctls) != +sizeof (policy.pinbased_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.procbased_ctls) != +sizeof (policy.procbased_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.exit_ctls) != +sizeof (policy.exit_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.entry_ctls) != +sizeof (policy.entry_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.misc) != +sizeof (policy.misc.raw)); +BUILD_BUG_ON (sizeof (policy.cr0_fixed_0) != +sizeof (policy.cr0_fixed_0.raw)); +BUILD_BUG_ON (sizeof (policy.cr0_fixed_1) != +sizeof (policy.cr0_fixed_1.raw)); +BUILD_BUG_ON (sizeof (policy.cr4_fixed_0) != +sizeof (policy.cr4_fixed_0.raw)); +BUILD_BUG_ON (sizeof (policy.cr4_fixed_1) != +sizeof (policy.cr4_fixed_1.raw)); +BUILD_BUG_ON (sizeof (policy.vmcs_enum) != +sizeof (policy.vmcs_enum.raw)); +BUILD_BUG_ON (sizeof (policy.procbased_ctls2) != +sizeof (policy.procbased_ctls2.raw)); +BUILD_BUG_ON (sizeof (policy.ept_vpid_cap) != +sizeof (policy.ept_vpid_cap.raw)); +BUILD_BUG_ON (sizeof (policy.true_pinbased_ctls) != +sizeof (policy.true_pinbased_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.true_procbased_ctls) != +sizeof (policy.true_procbased_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.true_exit_ctls) != +sizeof (policy.true_exit_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.true_entry_ctls) != +sizeof (policy.true_entry_ctls.raw)); +BUILD_BUG_ON (sizeof (policy.vmfunc) != +sizeof (policy.vmfunc.raw)); + +BUILD_BUG_ON (MSR_IA32_VMX_LAST -MSR_IA32_VMX_BASIC +1 > +sizeof (policy.available) *8 ); +}/* * Local variables:
xen/include/asm-x86/hvm/vmx/vmcs.h +380 -0
386 lines not on this page · raw message
xen/include/asm-x86/msr-index.h +1 -0
@@ -139,6 +139,7 @@#define MSR_IA32_VMX_TRUE_EXIT_CTLS 0x48f#define MSR_IA32_VMX_TRUE_ENTRY_CTLS 0x490#define MSR_IA32_VMX_VMFUNC 0x491 +#define MSR_IA32_VMX_LAST MSR_IA32_VMX_VMFUNC/* K7/K8 MSRs. Not complete. See the architecture manual for a more complete list. */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v2 1/5] x86/vmx: add struct vmx_msr_policy
Quoted 40 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, July 24, 2017 9:48 PMThis structure provides a convenient way of accessing contents of VMX MSRs: every bit value is accessible by its name. Bit names match existing Xen's definitions as close as possible. The structure also contains the bitmap of available MSRs since not all of them may be available on a particular H/W.
A set of helper functions is introduced to provide a simple way of interacting with the new structure.
Signed-off-by Sergey Dyasli <[email protected]>---
v1 --> v2:
- Replaced MSR indices with MSR names in struct vmx_msr_policy's comments
- Named "always zero bit" 31 of basic msr as mbz
- Added placeholder bits into union vmfunc
- Added structurescr0_bitsandcr4_bits
- AddedMSR_IA32_VMX_LASTdefine to use instead ofMSR_IA32_VMX_VMFUNC
-vmx_msr_available()now uses pointer to const structvmx_msr_policy
-build_assertions()now uses local structvmx_msr_policy
- AddedBUILD_BUG_ONto check that width ofvmx_msr_policy::availablebitmap is enough for all existing VMX MSRs
- Helpersget_vmx_msr_val(),get_vmx_msr_ptr()andgen_vmx_msr_mask()are addedxen/arch/x86/hvm/vmx/vmcs.c | 78 ++++++++ xen/include/asm-x86/hvm/vmx/vmcs.h | 380 +++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/msr-index.h | 1 + 3 files changed, 459 insertions(+)xen/arch/x86/hvm/vmx/vmcs.c +0
-0
xen/arch/x86/hvm/vmx/vmcs.c static void __init vmx_display_features(void) · line 147
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmcs.c +1
-0@@ -144,6 +144,40 @@ static void __init vmx_display_features(void)printk (" - none\n" ); } +bool vmx_msr_available (const struct vmx_msr_policy *p,uint32_t msr)
regarding to naming, many functions use vmx_ as prefix with later strings represent the actual purpose e.g. vmx_msr_read_intercept which is about general msr read interception. while here your intention is whether "vmx_msr" is available, which is for specific VMX MSRs. similar for vmx_msr_policy which reads more general than the real intention here. Can we find a way to differentiate? SDM calls this category as "VMX CAPABILITY REPORTING FACILITY", maybe using vmxcap_msr?
xen/arch/x86/hvm/vmx/vmcs.c static void __init vmx_display_features(void) · line 150
Quoted 3 lines
xen/arch/x86/hvm/vmx/vmcs.c +3
-0+{ +if ( msr <MSR_IA32_VMX_BASIC || msr >MSR_IA32_VMX_LAST ) +return 0 ;
then why not also introducing MSR_IA32_VMX_FIRST for better readability?
xen/arch/x86/hvm/vmx/vmcs.c static void __init vmx_display_features(void) · line 158
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmcs.c +8
-0+ +return p->available & (1u << (msr -MSR_IA32_VMX_BASIC )); +} + +uint64_t get_vmx_msr_val (const struct vmx_msr_policy *p,uint32_t msr) +{ +if ( !vmx_msr_available (p, msr)) +return 0 ;
0 is a valid MSR value. better return value in a pointer parameter and then return error number here.
xen/arch/x86/hvm/vmx/vmcs.c static void __init vmx_display_features(void) · line 161
Quoted 3 lines
xen/arch/x86/hvm/vmx/vmcs.c +3
-0+ +return p->msr[msr -MSR_IA32_VMX_BASIC ]; +}
Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 2/5 x86/vmx: add raw_vmx_msr_policy
In reply to Sergey DyasliAdd calculate_vmx_raw_policy() which fills the raw_vmx_msr_policy object (the actual contents of H/W VMX MSRs) on the boot CPU. On secondary CPUs, this function checks that contents of VMX MSRs match the boot CPU's contents.
Remove lesser version of same-contents-check from vmx_init_vmcs_config().
---
v1 --> v2:
- calculate_raw_policy() is renamed to calculate_vmx_raw_policy() to avoid clash with the same-name function in cpuid.c
- Declaration of calculate_vmx_raw_policy() is removed from vmx.c and added to vmcs.h
- msr variable is now unsigned in calculate_vmx_raw_policy()
- "\n" moved to the same line as the printk format string
- Replaced magic constants for available bitmap with gen_vmx_msr_mask()
- get_vmx_msr_ptr() and get_vmx_msr_val() helpers are used instead of accessing MSR array directly
xen/arch/x86/hvm/vmx/vmcs.c | 134 +++++++++++++++++++++---------------- xen/arch/x86/hvm/vmx/vmx.c | 2 + xen/include/asm-x86/hvm/vmx/vmcs.h | 3 + 3 files changed, 82 insertions(+), 57 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +77 -57
@@ -144,6 +144,8 @@ static void __init vmx_display_features(void)printk (" - none\n" ); } +struct vmx_msr_policy __read_mostly raw_vmx_msr_policy; +bool vmx_msr_available (const struct vmx_msr_policy *p,uint32_t msr) {if ( msr <MSR_IA32_VMX_BASIC || msr >MSR_IA32_VMX_LAST )
@@ -178,6 +180,78 @@ uint32_t gen_vmx_msr_mask(uint32_t start_msr, uint32_t end_msr) (start_msr -MSR_IA32_VMX_BASIC ); } +int calculate_vmx_raw_policy (bool bsp) +{ +struct vmx_msr_policy policy; +struct vmx_msr_policy *p = &policy; +unsigned int msr; + +/* Raw policy is filled only on boot CPU */ +if ( bsp ) + p = &raw_vmx_msr_policy; +else +memset (&policy,0 ,sizeof (policy)); + + p->available =gen_vmx_msr_mask (MSR_IA32_VMX_BASIC ,MSR_IA32_VMX_VMCS_ENUM ); +for ( msr =MSR_IA32_VMX_BASIC ; msr <=MSR_IA32_VMX_VMCS_ENUM ; msr++ ) +rdmsrl (msr, *get_vmx_msr_ptr (p, msr)); + +if ( p->basic.default1_zero ) + { + p->available |=gen_vmx_msr_mask (MSR_IA32_VMX_TRUE_PINBASED_CTLS , +MSR_IA32_VMX_TRUE_ENTRY_CTLS ); +for ( msr =MSR_IA32_VMX_TRUE_PINBASED_CTLS ; + msr <=MSR_IA32_VMX_TRUE_ENTRY_CTLS ; msr++ ) +rdmsrl (msr, *get_vmx_msr_ptr (p, msr)); + } + +if ( p->procbased_ctls.allowed_1.activate_secondary_controls ) + { + p->available |=gen_vmx_msr_mask (MSR_IA32_VMX_PROCBASED_CTLS2 , +MSR_IA32_VMX_PROCBASED_CTLS2 ); + msr =MSR_IA32_VMX_PROCBASED_CTLS2 ; +rdmsrl (msr, *get_vmx_msr_ptr (p, msr)); + +if ( p->procbased_ctls2.allowed_1.enable_ept || + p->procbased_ctls2.allowed_1.enable_vpid ) + { + p->available |=gen_vmx_msr_mask (MSR_IA32_VMX_EPT_VPID_CAP , +MSR_IA32_VMX_EPT_VPID_CAP ); + msr =MSR_IA32_VMX_EPT_VPID_CAP ; +rdmsrl (msr, *get_vmx_msr_ptr (p, msr)); + } + +if ( p->procbased_ctls2.allowed_1.enable_vm_functions ) + { + p->available |=gen_vmx_msr_mask (MSR_IA32_VMX_VMFUNC , +MSR_IA32_VMX_VMFUNC ); + msr =MSR_IA32_VMX_VMFUNC ; +rdmsrl (msr, *get_vmx_msr_ptr (p, msr)); + } + } + +/* Check that secondary CPUs have exactly the same bits in VMX MSRs */ +if ( !bsp &&memcmp (p, &raw_vmx_msr_policy,sizeof (*p)) !=0 ) + { +for ( msr =MSR_IA32_VMX_BASIC ; msr <=MSR_IA32_VMX_LAST ; msr++ ) + { +if (get_vmx_msr_val (p, msr) != +get_vmx_msr_val (&raw_vmx_msr_policy, msr) ) + { +printk ("VMX msr %#x: saw 0x%016" PRIx64" expected 0x%016" PRIx64"\n" , + msr,get_vmx_msr_val (p, msr), +get_vmx_msr_val (&raw_vmx_msr_policy, msr)); + } + } + +printk ("VMX: Capabilities fatally differ between CPU%d and boot CPU\n" , +smp_processor_id ()); +return -EINVAL ; + } + +return 0 ; +} +static u32 adjust_vmx_controls (const char *name,u32 ctl_min,u32 ctl_opt,u32 msr,bool_t *mismatch) {
@@ -199,13 +273,6 @@ static u32 adjust_vmx_controls(return ctl; } -static bool_t cap_check (const char *name,u32 expected,u32 saw) -{ -if ( saw != expected ) -printk ("VMX %s: saw %#x expected %#x\n" , name, saw, expected); -return saw != expected; -} -static int vmx_init_vmcs_config (void ) {u32 vmx_basic_msr_low, vmx_basic_msr_high, min, opt;
@@ -438,56 +505,6 @@ static int vmx_init_vmcs_config(void)return -EINVAL ; } } -else - { -/* Globals are already initialised: re-check them. */ - mismatch |=cap_check ( -"VMCS revision ID" , - vmcs_revision_id, vmx_basic_msr_low &VMX_BASIC_REVISION_MASK ); - mismatch |=cap_check ( -"Pin-Based Exec Control" , - vmx_pin_based_exec_control, _vmx_pin_based_exec_control); - mismatch |=cap_check ( -"CPU-Based Exec Control" , - vmx_cpu_based_exec_control, _vmx_cpu_based_exec_control); - mismatch |=cap_check ( -"Secondary Exec Control" , - vmx_secondary_exec_control, _vmx_secondary_exec_control); - mismatch |=cap_check ( -"VMExit Control" , - vmx_vmexit_control, _vmx_vmexit_control); - mismatch |=cap_check ( -"VMEntry Control" , - vmx_vmentry_control, _vmx_vmentry_control); - mismatch |=cap_check ( -"EPT and VPID Capability" , - vmx_ept_vpid_cap, _vmx_ept_vpid_cap); - mismatch |=cap_check ( -"VMFUNC Capability" , - vmx_vmfunc, _vmx_vmfunc); -if ( cpu_has_vmx_ins_outs_instr_info != - !!(vmx_basic_msr_high & (VMX_BASIC_INS_OUT_INFO >>32 )) ) - { -printk ("VMX INS/OUTS Instruction Info: saw %d expected %d\n" , - !!(vmx_basic_msr_high & (VMX_BASIC_INS_OUT_INFO >>32 )), - cpu_has_vmx_ins_outs_instr_info); - mismatch =1 ; - } -if ( (vmx_basic_msr_high & (VMX_BASIC_VMCS_SIZE_MASK >>32 )) != - ((vmx_basic_msr &VMX_BASIC_VMCS_SIZE_MASK ) >>32 ) ) - { -printk ("VMX: CPU%d unexpected VMCS size %Lu\n" , -smp_processor_id (), - vmx_basic_msr_high & (VMX_BASIC_VMCS_SIZE_MASK >>32 )); - mismatch =1 ; - } -if ( mismatch ) - { -printk ("VMX: Capabilities fatally differ between CPU%d and CPU0\n" , -smp_processor_id ()); -return -EINVAL ; - } - }/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ if ( vmx_basic_msr_high & (VMX_BASIC_32BIT_ADDRESSES >>32 ) )
@@ -637,6 +654,9 @@ int vmx_cpu_up(void)BUG_ON (!(read_cr4 () &X86_CR4_VMXE )); +if ( (rc =calculate_vmx_raw_policy (false)) !=0 ) +return rc; +/* * Ensure the current processor operating mode meets * the requred CRO fixed bits in VMX operation.
xen/arch/x86/hvm/vmx/vmx.c +2 -0
@@ -2440,6 +2440,8 @@ const struct hvm_function_table * __init start_vmx(void) {set_in_cr4 (X86_CR4_VMXE ); +calculate_vmx_raw_policy (true); +if (vmx_cpu_up () ) {printk ("VMX: failed to initialise.\n" );
xen/include/asm-x86/hvm/vmx/vmcs.h +3 -0
@@ -942,6 +942,9 @@ uint64_t get_vmx_msr_val(const struct vmx_msr_policy *p, uint32_t msr);uint64_t *get_vmx_msr_ptr (struct vmx_msr_policy *p,uint32_t msr);uint32_t gen_vmx_msr_mask (uint32_t start_msr,uint32_t end_msr); +extern struct vmx_msr_policy raw_vmx_msr_policy; +int calculate_vmx_raw_policy (bool bsp); +#endif /* ASM_X86_HVM_VMX_VMCS_H__ */ /*
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v2 2/5] x86/vmx: add raw_vmx_msr_policy
Quoted 43 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, July 24, 2017 9:48 PMAdd
calculate_vmx_raw_policy()which fills theraw_vmx_msr_policyobject (the actual contents of H/W VMX MSRs) on the boot CPU. On secondary CPUs, this function checks that contents of VMX MSRs match the boot CPU's contents.Remove lesser version of same-contents-check from
vmx_init_vmcs_config().Signed-off-by Sergey Dyasli <[email protected]>---
v1 --> v2:
-calculate_raw_policy()is renamed tocalculate_vmx_raw_policy()to avoid clash with the same-name function incpuid.c
- Declaration ofcalculate_vmx_raw_policy()is removed fromvmx.cand added tovmcs.h
- msr variable is now unsigned incalculate_vmx_raw_policy()
- "\n" moved to the same line as the printk format string
- Replaced magic constants for available bitmap withgen_vmx_msr_mask()
-get_vmx_msr_ptr()andget_vmx_msr_val()helpers are used instead of accessing MSR array directlyxen/arch/x86/hvm/vmx/vmcs.c | 134 +++++++++++++++++++++------------ ---- xen/arch/x86/hvm/vmx/vmx.c | 2 + xen/include/asm-x86/hvm/vmx/vmcs.h | 3 + 3 files changed, 82 insertions(+), 57 deletions(-)xen/arch/x86/hvm/vmx/vmcs.c +2
-0@@ -144,6 +144,8 @@ static void __init vmx_display_features(void)printk (" - none\n" ); } +struct vmx_msr_policy __read_mostly raw_vmx_msr_policy; +bool vmx_msr_available (const struct vmx_msr_policy *p,uint32_t msr) {if ( msr <MSR_IA32_VMX_BASIC || msr >MSR_IA32_VMX_LAST )
xen/arch/x86/hvm/vmx/vmcs.c uint32_t gen_vmx_msr_mask(uint32_t start_msr, · line 184
Quoted 5 lines
xen/arch/x86/hvm/vmx/vmcs.c +1
-0@@ -178,6 +180,78 @@ uint32_t gen_vmx_msr_mask(uint32_t start_msr, uint32_t end_msr) (start_msr - MSR_IA32_VMX_BASIC); } +int calculate_vmx_raw_policy (bool bsp)
calculate_raw_vmxcap_policy
xen/arch/x86/hvm/vmx/vmcs.c uint32_t gen_vmx_msr_mask(uint32_t start_msr, uint32_t end_msr) · line 193
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10
-0+{ +struct vmx_msr_policy policy; +struct vmx_msr_policy *p = &policy; +unsigned int msr; + +/* Raw policy is filled only on boot CPU */ +if ( bsp ) + p = &raw_vmx_msr_policy; +else +memset (&policy,0 ,sizeof (policy));
memset(p, 0, sizeof(*p));
xen/arch/x86/hvm/vmx/vmcs.c uint32_t gen_vmx_msr_mask(uint32_t start_msr, · line 200
Quoted 6 lines
xen/arch/x86/hvm/vmx/vmcs.c +4
-0+ + p->available =gen_vmx_msr_mask (MSR_IA32_VMX_BASIC ,MSR_IA32_VMX_VMCS_ENUM ); +for ( msr =MSR_IA32_VMX_BASIC ; msr <=MSR_IA32_VMX_VMCS_ENUM ; msr++ ) +rdmsrl (msr, *get_vmx_msr_ptr (p, msr));
move above into a function since quite some duplication below.
Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 3/5 x86/vmx: refactor vmx_init_vmcs_config()
In reply to Sergey Dyasli1. Remove RDMSRs of VMX MSRs since all values are already available in raw_vmx_msr_policy.
2. Replace bit operations involving VMX bitmasks with accessing VMX features by name and using vmx_msr_available() where appropriate.
---
v1 --> v2:
- get_vmx_msr_val() is used instead of accessing policy's msr array directly
xen/arch/x86/hvm/vmx/vmcs.c | 56 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 30 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +26 -30
@@ -257,7 +257,8 @@ static u32 adjust_vmx_controls( {u32 vmx_msr_low, vmx_msr_high, ctl = ctl_min | ctl_opt; -rdmsr (msr, vmx_msr_low, vmx_msr_high); + vmx_msr_low =get_vmx_msr_val (&raw_vmx_msr_policy, msr); + vmx_msr_high =get_vmx_msr_val (&raw_vmx_msr_policy, msr) >>32 ; ctl &= vmx_msr_high;/* bit == 0 in high word ==> must be zero */ ctl |= vmx_msr_low;/* bit == 1 in low word ==> must be one */
@@ -275,19 +276,16 @@ static u32 adjust_vmx_controls(static int vmx_init_vmcs_config (void ) { -u32 vmx_basic_msr_low, vmx_basic_msr_high, min, opt; +u32 min, opt;u32 _vmx_pin_based_exec_control;u32 _vmx_cpu_based_exec_control;u32 _vmx_secondary_exec_control =0 ;u64 _vmx_ept_vpid_cap =0 ; -u64 _vmx_misc_cap =0 ;u32 _vmx_vmexit_control;u32 _vmx_vmentry_control;u64 _vmx_vmfunc =0 ;bool_t mismatch =0 ; -rdmsr (MSR_IA32_VMX_BASIC , vmx_basic_msr_low, vmx_basic_msr_high); - min = (PIN_BASED_EXT_INTR_MASK |PIN_BASED_NMI_EXITING ); opt = (PIN_BASED_VIRTUAL_NMIS |
@@ -321,7 +319,7 @@ static int vmx_init_vmcs_config(void) _vmx_cpu_based_exec_control &= ~(CPU_BASED_CR8_LOAD_EXITING |CPU_BASED_CR8_STORE_EXITING ); -if ( _vmx_cpu_based_exec_control &CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ) +if (vmx_msr_available (&raw_vmx_msr_policy,MSR_IA32_VMX_PROCBASED_CTLS2 ) ) { min =0 ; opt = (SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
@@ -335,8 +333,7 @@ static int vmx_init_vmcs_config(void)SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS |SECONDARY_EXEC_XSAVES | SECONDARY_EXEC_TSC_SCALING); -rdmsrl (MSR_IA32_VMX_MISC , _vmx_misc_cap); -if ( _vmx_misc_cap &VMX_MISC_VMWRITE_ALL ) +if ( raw_vmx_msr_policy.misc.vmwrite_all ) opt |=SECONDARY_EXEC_ENABLE_VMCS_SHADOWING ;if ( opt_vpid_enabled ) opt |=SECONDARY_EXEC_ENABLE_VPID ;
@@ -361,10 +358,9 @@ static int vmx_init_vmcs_config(void) }/* The IA32_VMX_EPT_VPID_CAP MSR exists only when EPT or VPID available */ -if ( _vmx_secondary_exec_control & (SECONDARY_EXEC_ENABLE_EPT | -SECONDARY_EXEC_ENABLE_VPID ) ) +if (vmx_msr_available (&raw_vmx_msr_policy,MSR_IA32_VMX_EPT_VPID_CAP ) ) { -rdmsrl (MSR_IA32_VMX_EPT_VPID_CAP , _vmx_ept_vpid_cap); + _vmx_ept_vpid_cap = raw_vmx_msr_policy.ept_vpid_cap.raw;if ( !opt_ept_ad ) _vmx_ept_vpid_cap &= ~VMX_EPT_AD_BIT ;
@@ -409,10 +405,14 @@ static int vmx_init_vmcs_config(void)* To use EPT we expect to be able to clear certain intercepts. * We check VMX_BASIC_MSR[55] to correctly handle default controls. */ -uint32_t must_be_one, must_be_zero, msr =MSR_IA32_VMX_PROCBASED_CTLS ; -if ( vmx_basic_msr_high & (VMX_BASIC_DEFAULT1_ZERO >>32 ) ) - msr =MSR_IA32_VMX_TRUE_PROCBASED_CTLS ; -rdmsr (msr, must_be_one, must_be_zero); +uint32_t must_be_one = raw_vmx_msr_policy.procbased_ctls.allowed_0.raw; +uint32_t must_be_zero = raw_vmx_msr_policy.procbased_ctls.allowed_1.raw; +if (vmx_msr_available (&raw_vmx_msr_policy, +MSR_IA32_VMX_TRUE_PROCBASED_CTLS ) ) + { + must_be_one = raw_vmx_msr_policy.true_procbased_ctls.allowed_0.raw; + must_be_zero = raw_vmx_msr_policy.true_procbased_ctls.allowed_1.raw; + }if ( must_be_one & (CPU_BASED_INVLPG_EXITING |CPU_BASED_CR3_LOAD_EXITING |CPU_BASED_CR3_STORE_EXITING ) )
@@ -453,9 +453,9 @@ static int vmx_init_vmcs_config(void) _vmx_pin_based_exec_control &= ~PIN_BASED_POSTED_INTERRUPT ;/* The IA32_VMX_VMFUNC MSR exists only when VMFUNC is available */ -if ( _vmx_secondary_exec_control &SECONDARY_EXEC_ENABLE_VM_FUNCTIONS ) +if (vmx_msr_available (&raw_vmx_msr_policy,MSR_IA32_VMX_VMFUNC ) ) { -rdmsrl (MSR_IA32_VMX_VMFUNC , _vmx_vmfunc); + _vmx_vmfunc = raw_vmx_msr_policy.vmfunc.raw;/* * VMFUNC leaf 0 (EPTP switching) must be supported.
@@ -481,33 +481,31 @@ static int vmx_init_vmcs_config(void)if ( !vmx_pin_based_exec_control ) {/* First time through. */ - vmcs_revision_id = vmx_basic_msr_low &VMX_BASIC_REVISION_MASK ; + vmcs_revision_id = raw_vmx_msr_policy.basic.vmcs_revision_id; vmx_pin_based_exec_control = _vmx_pin_based_exec_control; vmx_cpu_based_exec_control = _vmx_cpu_based_exec_control; vmx_secondary_exec_control = _vmx_secondary_exec_control; vmx_ept_vpid_cap = _vmx_ept_vpid_cap; vmx_vmexit_control = _vmx_vmexit_control; vmx_vmentry_control = _vmx_vmentry_control; - vmx_basic_msr = ((u64 )vmx_basic_msr_high <<32 ) | - vmx_basic_msr_low; + vmx_basic_msr = raw_vmx_msr_policy.basic.raw; vmx_vmfunc = _vmx_vmfunc; vmx_virt_exception = !!(_vmx_secondary_exec_control &SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS );vmx_display_features ();/* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ -if ( (vmx_basic_msr_high & (VMX_BASIC_VMCS_SIZE_MASK >>32 )) > -PAGE_SIZE ) +if ( raw_vmx_msr_policy.basic.vmcs_region_size >PAGE_SIZE ) { -printk ("VMX: CPU%d VMCS size is too big (%Lu bytes)\n" , +printk ("VMX: CPU%d VMCS size is too big (%d bytes)\n" ,smp_processor_id (), - vmx_basic_msr_high & (VMX_BASIC_VMCS_SIZE_MASK >>32 )); + raw_vmx_msr_policy.basic.vmcs_region_size);return -EINVAL ; } }/* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ -if ( vmx_basic_msr_high & (VMX_BASIC_32BIT_ADDRESSES >>32 ) ) +if ( raw_vmx_msr_policy.basic.addresses_32bit ) {printk ("VMX: CPU%d limits VMX structure pointers to 32 bits\n" ,smp_processor_id ());
@@ -515,9 +513,7 @@ static int vmx_init_vmcs_config(void) }/* Require Write-Back (WB) memory type for VMCS accesses. */ - opt = (vmx_basic_msr_high & (VMX_BASIC_MEMORY_TYPE_MASK >>32 )) / - ((VMX_BASIC_MEMORY_TYPE_MASK & -VMX_BASIC_MEMORY_TYPE_MASK ) >>32 ); -if ( opt !=MTRR_TYPE_WRBACK ) +if ( raw_vmx_msr_policy.basic.memory_type !=MTRR_TYPE_WRBACK ) {printk ("VMX: CPU%d has unexpected VMCS access type %u\n" ,smp_processor_id (), opt);
@@ -662,8 +658,8 @@ int vmx_cpu_up(void)* the requred CRO fixed bits in VMX operation. */ cr0 =read_cr0 (); -rdmsrl (MSR_IA32_VMX_CR0_FIXED0 , vmx_cr0_fixed0); -rdmsrl (MSR_IA32_VMX_CR0_FIXED1 , vmx_cr0_fixed1); + vmx_cr0_fixed0 = raw_vmx_msr_policy.cr0_fixed_0.raw; + vmx_cr0_fixed1 = raw_vmx_msr_policy.cr0_fixed_1.raw;if ( (~cr0 & vmx_cr0_fixed0) || (cr0 & ~vmx_cr0_fixed1) ) { printk("CPU%d: some settings of host CR0 are "
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v2 3/5] x86/vmx: refactor vmx_init_vmcs_config()
Quoted 9 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, July 24, 2017 9:48 PM1. Remove RDMSRs of VMX MSRs since all values are already available in
raw_vmx_msr_policy.
2. Replace bit operations involving VMX bitmasks with accessing VMX features by name and usingvmx_msr_available()where appropriate.Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 4/5 x86/vvmx: add vvmx_max_msr_policy
In reply to Sergey DyasliCurrently, when nested virt is enabled, the set of L1 VMX features is fixed and calculated by nvmx_msr_read_intercept() as an intersection between the full set of Xen's supported L1 VMX features, the set of actual H/W features and, for MSR_IA32_VMX_EPT_VPID_CAP, the set of features that Xen uses.
Add vvmx_max_msr_policy object which represents the end result of nvmx_msr_read_intercept() on current H/W. Most of the code is moved from nvmx_msr_read_intercept() to calculate_vvmx_max_policy() which is called only once during the startup.
There is no functional change to what L1 sees in VMX MSRs.
---
v1 --> v2:
- Renamed hvm_max_vmx_msr_policy to vvmx_max_msr_policy and made it static
- calculate_hvm_max_policy() is renamed to calculate_vvmx_max_policy()
- Declaration of calculate_vvmx_max_policy() is removed from vmcs.c and added to vvmx.h
- Removed comment "XXX: vmcs_revision_id for nested virt"
- nvmx_msr_read_intercept() now uses const struct vmx_msr_policy *
- Shortened "msr = *msr &" to "*msr &="
- Removed usage of "data" as an intermediate variable for 4 MSRs
- Replaced magic constant for disabling MSR_IA32_VMX_VMFUNC with gen_vmx_msr_mask()
- get_vmx_msr_ptr() and get_vmx_msr_val() helpers are used instead of accessing MSR array directly
xen/arch/x86/hvm/vmx/vmcs.c | 1 + xen/arch/x86/hvm/vmx/vvmx.c | 284 +++++++++++++++++-------------------- xen/include/asm-x86/hvm/vmx/vvmx.h | 2 + 3 files changed, 134 insertions(+), 153 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
@@ -493,6 +493,7 @@ static int vmx_init_vmcs_config(void) vmx_virt_exception = !!(_vmx_secondary_exec_control &SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS );vmx_display_features (); +calculate_vvmx_max_policy ();/* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ if ( raw_vmx_msr_policy.basic.vmcs_region_size >PAGE_SIZE )
xen/arch/x86/hvm/vmx/vvmx.c +131 -153
310 lines not on this page · raw message
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -0
@@ -244,5 +244,7 @@ int nept_translate_l2ga(struct vcpu *v, paddr_t l2ga, uint64_t *exit_qual, uint32_t *exit_reason);int nvmx_cpu_up_prepare (unsigned int cpu);void nvmx_cpu_dead (unsigned int cpu); + +void calculate_vvmx_max_policy (void );#endif /* __ASM_X86_HVM_VVMX_H__ */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v2 5/5 x86/vvmx: add per domain vmx msr policy
In reply to Sergey DyasliHaving a policy per domain allows to sensibly query what VMX features the domain has, which unblocks some other nested virt work items.
For now, make policy for each domain equal to vvmx_max_msr_policy. In the future it should be possible to independently configure the policy for each domain.
---
v1 --> v2:
- nvmx_msr_read_intercept() now uses const struct vmx_msr_policy * (starting from patch #4)
- Added Reviewed-by: Jan Beulich <[email protected]>
xen/arch/x86/domain.c | 6 ++++++ xen/arch/x86/hvm/vmx/vvmx.c | 14 +++++++++++++- xen/include/asm-x86/domain.h | 2 ++ xen/include/asm-x86/hvm/vmx/vvmx.h | 1 + 4 files changed, 22 insertions(+), 1 deletion(-)
xen/arch/x86/domain.c +6 -0
@@ -425,6 +425,7 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags, { d->arch.emulation_flags =0 ; d->arch.cpuid =ZERO_BLOCK_PTR ;/* Catch stray misuses. */ + d->arch.vmx_msr =ZERO_BLOCK_PTR ; } else {
@@ -470,6 +471,9 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,if ( (rc =init_domain_cpuid_policy (d)) ) goto fail; +if ( (rc =init_domain_vmx_msr_policy (d)) ) + goto fail; + d->arch.ioport_caps =rangeset_new (d,"I/O Ports" , RANGESETF_prettyprint_hex); rc = -ENOMEM ;
@@ -541,6 +545,7 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,cleanup_domain_irq_mapping (d);free_xenheap_page (d->shared_info);xfree (d->arch.cpuid); +xfree (d->arch.vmx_msr);if ( paging_initialised )paging_final_teardown (d);free_perdomain_mappings (d);
@@ -555,6 +560,7 @@ void arch_domain_destroy(struct domain *d)xfree (d->arch.e820);xfree (d->arch.cpuid); +xfree (d->arch.vmx_msr);free_domain_pirqs (d);if ( !is_idle_domain (d) )
xen/arch/x86/hvm/vmx/vvmx.c +13 -1
@@ -2072,6 +2072,18 @@ void __init calculate_vvmx_max_policy(void) MSR_IA32_VMX_VMFUNC); } +int init_domain_vmx_msr_policy (struct domain *d) +{ + d->arch.vmx_msr = xmalloc(struct vmx_msr_policy); + +if ( !d->arch.vmx_msr ) +return -ENOMEM ; + + *d->arch.vmx_msr = vvmx_max_msr_policy; + +return 0 ; +} +/* * Capability reporting */
@@ -2079,7 +2091,7 @@ int nvmx_msr_read_intercept(unsigned int msr, u64 *msr_content) {struct vcpu *v = current;struct domain *d = v->domain; -const struct vmx_msr_policy *p = &vvmx_max_msr_policy; +const struct vmx_msr_policy *p = d->arch.vmx_msr;int r =1 ;/* VMX capablity MSRs are available only when guest supports VMX. */
xen/include/asm-x86/domain.h +2 -0
@@ -359,6 +359,8 @@ struct arch_domain/* CPUID Policy. */ struct cpuid_policy *cpuid; +struct vmx_msr_policy *vmx_msr; +struct PITState vpit;/* TSC management (emulation, pv, scaling, stats) */
xen/include/asm-x86/hvm/vmx/vvmx.h +1 -0
@@ -246,5 +246,6 @@ int nvmx_cpu_up_prepare(unsigned int cpu);void nvmx_cpu_dead (unsigned int cpu);void calculate_vvmx_max_policy (void ); +int init_domain_vmx_msr_policy (struct domain *d);#endif /* __ASM_X86_HVM_VVMX_H__ */
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v2 5/5] x86/vvmx: add per domain vmx msr policy
xen/arch/x86/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags, · line 475
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Sergey Dyasli <[email protected]> 07/24/17 3:48 PM >>>
xen/arch/x86/domain.c +2
-0@@ -470,6 +471,9 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,if ( (rc =init_domain_cpuid_policy (d)) ) goto fail; +if ( (rc =init_domain_vmx_msr_policy (d)) ) + goto fail;
There should not be a direct call from here to VMX-specific code - an intermediate HVM layer is needed.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 20 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, July 24, 2017 9:48 PMThe end goal of having VMX MSRs policy is to be able to manage L1 VMX features. This patch series is the first part of this work. There is no functional change to what L1 sees in VMX MSRs at this point. But each domain will have a policy object which allows to sensibly query what VMX features the domain has. This will unblock some other nested virtualization work items.
Currently, when nested virt is enabled, the set of L1 VMX features is fixed and calculated by
nvmx_msr_read_intercept()as an intersection between the full set of Xen's supported L1 VMX features, the set of actual H/W features and, forMSR_IA32_VMX_EPT_VPID_CAP, the set of features that Xen uses.The above makes L1 VMX feature set inconsistent between different H/W and there is no ability to control what features are available to L1. The overall set of issues has much in common with CPUID policy.
Quoted 5 lines
Part 1 introduces struct
vmx_msr_policyand the following instances:* Raw policy (
raw_vmx_msr_policy) -- the actual contents of H/W VMX MSRs
* VVMX max policy (vvmx_max_msr_policy) -- the end result ofnvmx_msr_read_intercept()on current H/W
it's clearer to call it max_vvmx_msr_policy
Quoted 36 lines
* Per-domain policy (
d->arch.vmx_msr) -- the copy of VVMX max policy (for now)In the future it should be possible to independently configure the VMX policy for each domain using some new domctl.
There is no "Host policy" object because Xen already has a set of variables (
vmx_pin_based_exec_controland others) which represent the set of VMX features that Xen uses. There are features that Xen doesn't use (e.g.CPU_BASED_PAUSE_EXITING) but they are available to L1. This makes it not worthy to introduce "Host policy" at this stage.v1 --> v2:
- Rebased to the latest master
-hvm_max_vmx_msr_policyis renamed tovvmx_max_msr_policy
- Dropped the debug patch
- Other changes are available on a per-patch basisSergey Dyasli (5):
x86/vmx: add structvmx_msr_policy
x86/vmx: addraw_vmx_msr_policy
x86/vmx: refactorvmx_init_vmcs_config()
x86/vvmx: addvvmx_max_msr_policy
x86/vvmx: add per domain vmx msr policyxen/arch/x86/domain.c | 6 + xen/arch/x86/hvm/vmx/vmcs.c | 269 +++++++++++++++++--------- xen/arch/x86/hvm/vmx/vmx.c | 2 + xen/arch/x86/hvm/vmx/vvmx.c | 296 ++++++++++++++-------------- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/hvm/vmx/vmcs.h | 383 +++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/vmx/vvmx.h | 3 + xen/include/asm-x86/msr-index.h | 1 + 8 files changed, 722 insertions(+), 240 deletions(-)
Quoted 2 lines
--
2.11.0
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel