: Check "load IA32_PAT" VM-exit{entry} controls on vmentry of L2 guests (v4)
PATCH v4 4/6 : nested_check_guest_cregs_dregs_msrs() should return -EINVAL for error conditions
In reply to Krish Sadhukhan..to match the error return type of other similar functions.
--- arch/x86/kvm/vmx/nested.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +2 -2
@@ -2683,11 +2683,11 @@ static int nested_check_guest_cregs_dregs_msrs(struct kvm_vcpu *vcpu, {if (!nested_guest_cr0_valid (vcpu, vmcs12->guest_cr0) || !nested_guest_cr4_valid (vcpu, vmcs12->guest_cr4)) -return 1 ; +return -EINVAL ;if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PAT ) && !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->guest_ia32_pat)) -return 1 ; +return -EINVAL ;return 0 ; }
Signature · 1 line
2.17.2
PATCH v4 5/6 : nested_vmx_check_vmentry_postreqs() should return VMX_EXIT_REASONS_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE for error conditions
In reply to Krish Sadhukhan ..to reflect the architectural Exit Reason for VM-entry failures due to
invalid guest state.
--- arch/x86/kvm/vmx/nested.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +9 -3
@@ -2705,7 +2705,9 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu,if (nested_vmx_check_vmcs_link_ptr (vcpu, vmcs12)) { *exit_qual =ENTRY_FAIL_VMCS_LINK_PTR ; -return 1 ; + +return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ; }/*
@@ -2724,13 +2726,17 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, ia32e != !!(vmcs12->guest_ia32_efer &EFER_LMA ) || ((vmcs12->guest_cr0 &X86_CR0_PG ) && ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) - return 1; + + return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ; }if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ) && (is_noncanonical_address (vmcs12->guest_bndcfgs &PAGE_MASK , vcpu) || (vmcs12->guest_bndcfgs &MSR_IA32_BNDCFGS_RSVD ))) -return 1 ; + +return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ;return 0 ; }
Signature · 1 line
2.17.2
PATCH v4 6/6 : Check "load IA32_PAT" on vmentry of L2 guests
In reply to Krish Sadhukhan.to verify KVM performs the appropriate consistency checks for loading IA32_PAT as part of running a nested guest.
According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry:
If the “load IA32_PAT” VM-exit control is 1, the value of the field for the IA32_PAT MSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-).
Since a PAT value higher than 8 will yield the same test result as that of 8, we want to confine our tests only up to 8 in order to reduce redundancy of tests and to avoid too many vmentries.
--- x86/vmx_tests.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
x86/vmx_tests.c +72 -0
@@ -4995,6 +4995,76 @@ static void test_sysenter_field(u32 field, const char *name)vmcs_write (field, addr_saved); } +/* +* Since a PAT value higher than 8 will yield the same test result as that +* of 8, we want to confine our tests only up to 8 in order to reduce +* redundancy of tests and to avoid too many vmentries. +*/ +#define PAT_VAL_LIMIT 8 + +static void test_pat (u32 field,const char * field_name,u32 ctrl_field, +u64 ctrl_bit) +{ +u32 ctrl_saved =vmcs_read (ctrl_field); +u64 pat_saved =vmcs_read (field); +u64 i, val; +u32 j; +int error; + +vmcs_write (ctrl_field, ctrl_saved & ~ctrl_bit); +for (i =0 ; i <=PAT_VAL_LIMIT ; i++) { +/* Test PAT0..PAT7 fields */ +for (j =0 ; j <8 ; j++) { + val = i << j *8 ; +vmcs_write (field, val); +report_prefix_pushf ("%s %lx" , field_name, val); +test_vmx_vmlaunch (0 , false); +report_prefix_pop (); + } + } + +vmcs_write (ctrl_field, ctrl_saved | ctrl_bit); +for (i =0 ; i <=PAT_VAL_LIMIT ; i++) { +/* Test PAT0..PAT7 fields */ +for (j =0 ; j <8 ; j++) { + val = i << j *8 ; +vmcs_write (field, val); +report_prefix_pushf ("%s %lx" , field_name, val); +if (i ==0x2 || i ==0x3 || i >=0x8 ) + error =VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ; +else + error =0 ; +test_vmx_vmlaunch (error, false); +report_prefix_pop (); + } + } + +vmcs_write (ctrl_field, ctrl_saved); +vmcs_write (field, pat_saved); +} + +/* +* If the "load IA32_PAT" VM-exit control is 1, the value of the field +* for the IA32_PAT MSR must be one that could be written by WRMSR +* without fault at CPL 0. Specifically, each of the 8 bytes in the +* field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), +* 6 (WB), or 7 (UC-). +* +* [Intel SDM] +*/ +static void test_load_host_pat (void ) +{ +/* +* "load IA32_PAT" VM-exit control +*/ +if (!(ctrl_exit_rev.clr &EXI_LOAD_PAT )) { +printf ("\"Load-IA32-PAT\" exit control not supported\n" ); +return ; + } + +test_pat (HOST_PAT ,"HOST_PAT" ,EXI_CONTROLS ,EXI_LOAD_PAT ); +} +/* * Check that the virtual CPU checks the VMX Host State Area as * documented in the Intel SDM.
@@ -5010,6 +5080,8 @@ static void vmx_host_state_area_test(void)test_sysenter_field (HOST_SYSENTER_ESP ,"HOST_SYSENTER_ESP" );test_sysenter_field (HOST_SYSENTER_EIP ,"HOST_SYSENTER_EIP" ); + +test_load_host_pat (); }static bool valid_vmcs_for_vmentry (void )
Signature · 1 line
2.17.2
PATCH v4 2/6 : Check "load IA32_PAT" VM-entry control on vmentry
In reply to Krish SadhukhanAccording to section "Checking and Loading Guest State" in Intel SDM vol 3C, the following check is performed on vmentry:
If the "load IA32_PAT" VM-entry control is 1, the value of the field for the IA32_PAT MSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-).
--- arch/x86/kvm/vmx/nested.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/vmx/nested.c +4 -0
@@ -2686,6 +2686,10 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))return 1 ; +if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PAT ) && + !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->guest_ia32_pat)) +return 1 ; +if (nested_vmx_check_vmcs_link_ptr (vcpu, vmcs12)) { *exit_qual =ENTRY_FAIL_VMCS_LINK_PTR ;return 1 ;
Signature · 1 line
2.17.2
PATCH v4 3/6 : Move the checks for Guest Control Registers and Guest MSRs to a separate function
In reply to Krish Sadhukhan ..in order to align the checks to the order in which they are described in
Intel SDM vol 3C.
--- arch/x86/kvm/vmx/nested.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/nested.c +19 -6
@@ -2674,6 +2674,24 @@ static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,return r; } +/* +* Checks related to Control Registers, Debug Registers and MSRs in +* Guest State Area. +*/ +static int nested_check_guest_cregs_dregs_msrs (struct kvm_vcpu *vcpu, +struct vmcs12 *vmcs12) +{ +if (!nested_guest_cr0_valid (vcpu, vmcs12->guest_cr0) || + !nested_guest_cr4_valid (vcpu, vmcs12->guest_cr4)) +return 1 ; + +if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PAT ) && + !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->guest_ia32_pat)) +return 1 ; + +return 0 ; +} +static int nested_vmx_check_vmentry_postreqs (struct kvm_vcpu *vcpu,struct vmcs12 *vmcs12,u32 *exit_qual)
@@ -2682,12 +2700,7 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, *exit_qual =ENTRY_FAIL_DEFAULT ; -if (!nested_guest_cr0_valid (vcpu, vmcs12->guest_cr0) || - !nested_guest_cr4_valid (vcpu, vmcs12->guest_cr4)) -return 1 ; - -if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_IA32_PAT ) && - !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->guest_ia32_pat)) +if (nested_check_guest_cregs_dregs_msrs (vcpu, vmcs12))return 1 ;if (nested_vmx_check_vmcs_link_ptr (vcpu, vmcs12)) {
Signature · 1 line
2.17.2
v3 -> v4:
1. Two new patches, patch# 4 and 5, have been added. These patches change the error return in nested_check_guest_cregs_dregs_msrs() and nested_vmx_check_vmentry_postreqs().
2. In patch# 6 (which was patch# 4 in v4), names of some of the parameters and variables have been expanded.
[PATCH 1/6 v4][KVM nVMX]: Check "load IA32_PAT" VM-exit control on vmentry [PATCH 2/6 v4][KVM nVMX]: Check "load IA32_PAT" VM-entry control on vmentry [PATCH 3/6 v4][KVM nVMX]: Move the checks for Guest Control Registers and [PATCH 4/6 v4][KVM nVMX]: nested_check_guest_cregs_dregs_msrs() should return [PATCH 5/6 v4][KVM nVMX]: nested_vmx_check_vmentry_postreqs() should return [PATCH 6/6 v4][kvm-unit-test nVMX]: Check "load IA32_PAT" on vmentry of L2 guests
arch/x86/kvm/vmx/nested.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-)
Krish Sadhukhan (5):
nVMX: Check "load IA32_PAT" VM-exit control on vmentry
nVMX: Check "load IA32_PAT" VM-entry control on vmentry
nVMX: Move the checks for Guest Control Registers and Guest MSRs to a separate function
nVMX: nested_check_guest_cregs_dregs_msrs() should return -EINVAL for error conditions
nVMX: nested_vmx_check_vmentry_postreqs() should return VMX_EXIT_REASONS_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE for error conditions
x86/vmx_tests.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
Krish Sadhukhan (1):
nVMX: Check "load IA32_PAT" on vmentry of L2 guests
PATCH v4 1/6 : Check "load IA32_PAT" VM-exit control on vmentry
According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry:
If the "load IA32_PAT" VM-exit control is 1, the value of the field for the IA32_PAT MSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-).
--- arch/x86/kvm/vmx/nested.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kvm/vmx/nested.c +5 -0
@@ -2595,6 +2595,11 @@ static int nested_check_host_control_regs(struct kvm_vcpu *vcpu, !nested_host_cr4_valid (vcpu, vmcs12->host_cr4) || !nested_cr3_valid(vcpu, vmcs12->host_cr3)) return -EINVAL; + +if ((vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PAT ) && + !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->host_ia32_pat)) +return -EINVAL ; +/* * If the load IA32_EFER VM-exit control is 1, bits reserved in the * IA32_EFER MSR must be 0 in the field for that register. In addition,
Signature · 1 line
2.17.2
Re: [PATCH 1/6 v4][KVM nVMX]: Check "load IA32_PAT" VM-exit control on vmentry
Quoted 23 lines · Krish Sadhukhan
According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry:
If the "load
IA32_PAT" VM-exit control is 1, the value of the field for theIA32_PATMSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-).Signed-off-by Krish Sadhukhan <[email protected]>Reviewed-by Karl Heubaum <[email protected]>Suggested-by Sean Christopherson <[email protected]>Reviewed-by Sean Christopherson <[email protected]>Reviewed-by Jim Mattson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 5 +++++ 1 file changed, 5 insertions(+)arch/x86/kvm/vmx/nested.c +0
-0
arch/x86/kvm/vmx/nested.c static int nested_check_host_control_regs(struct kvm_vcpu *vcpu, · line 2601
Quoted 7 lines
arch/x86/kvm/vmx/nested.c +4
-0@@ -2595,6 +2595,11 @@ static int nested_check_host_control_regs(struct kvm_vcpu *vcpu, !nested_host_cr4_valid (vcpu, vmcs12->host_cr4) || !nested_cr3_valid(vcpu, vmcs12->host_cr3)) return -EINVAL; + +if ((vmcs12->vm_exit_controls &VM_EXIT_LOAD_IA32_PAT ) && + !kvm_mtrr_valid (vcpu,MSR_IA32_CR_PAT , vmcs12->host_ia32_pat)) +return -EINVAL ;
Doh, "return -EINVAL" is indented too far, only noticed it now because this conflicts with the latest kvm/queue. Paolo/Radim can likely fix it up while applying since they'll need to deal with the conflict anyways.
Quoted 7 lines
arch/x86/kvm/vmx/nested.c +1
-0+/* * If the load IA32_EFER VM-exit control is 1, bits reserved in the * IA32_EFER MSR must be 0 in the field for that register. In addition, Signature · 1 line
2.17.2
Re: [PATCH 4/6 v4][KVM nVMX]: nested_check_guest_cregs_dregs_msrs() should return -EINVAL for error conditions
In reply to Krish SadhukhanQuoted 5 lines · Krish Sadhukhan
..to match the error return type of other similar functions.
Signed-off-by Krish Sadhukhan <[email protected]>Suggested-by Sean Christopherson <[email protected]>---
Re: [PATCH 5/6 v4][KVM nVMX]: nested_vmx_check_vmentry_postreqs() should return VMX_EXIT_REASONS_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE for error conditions
In reply to Krish SadhukhanQuoted 14 lines · Krish Sadhukhan
..to reflect the architectural Exit Reason for VM-entry failures due to
invalid guest state.Signed-off-by Krish Sadhukhan <[email protected]>Suggested-by Sean Christopherson <[email protected]>--- arch/x86/kvm/vmx/nested.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/nested.c +0
-0
arch/x86/kvm/vmx/nested.c static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, · line 2705
Quoted 1 line
arch/x86/kvm/vmx/nested.c +0
-0@@ -2705,7 +2705,9 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu,
You missed the newly added call to nested_check_guest_cregs_dregs_msrs(). Assuming you spin v5, please fix the indentation issue in patch 1/6 as well.
Quoted 32 lines
arch/x86/kvm/vmx/nested.c +9
-3if (nested_vmx_check_vmcs_link_ptr (vcpu, vmcs12)) { *exit_qual =ENTRY_FAIL_VMCS_LINK_PTR ; -return 1 ; + +return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ; }/* @@ -2724,13 +2726,17 @@ static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, ia32e != !!(vmcs12->guest_ia32_efer &EFER_LMA ) || ((vmcs12->guest_cr0 &X86_CR0_PG ) && ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) - return 1; + + return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ; }if ((vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ) && (is_noncanonical_address (vmcs12->guest_bndcfgs &PAGE_MASK , vcpu) || (vmcs12->guest_bndcfgs &MSR_IA32_BNDCFGS_RSVD ))) -return 1 ; + +return VMX_EXIT_REASONS_FAILED_VMENTRY | +EXIT_REASON_INVALID_STATE ;return 0 ; }Signature · 1 line
2.17.2
Re: [PATCH 6/6 v4][kvm-unit-test nVMX]: Check "load IA32_PAT" on vmentry of L2 guests
In reply to Krish SadhukhanQuoted 12 lines · Krish Sadhukhan
.to verify KVM performs the appropriate consistency checks for loading
IA32_PATas part of running a nested guest.According to section "Checks on Host Control Registers and MSRs" in Intel SDM vol 3C, the following check is performed on vmentry:
If the “load IA32_PAT” VM-exit control is 1, the value of the field for the
IA32_PATMSR must be one that could be written by WRMSR without fault at CPL 0. Specifically, each of the 8 bytes in the field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 6 (WB), or 7 (UC-).
Quoted 7 lines
Since a PAT value higher than 8 will yield the same test result as that of 8, we want to confine our tests only up to 8 in order to reduce redundancy of tests and to avoid too many vmentries.
Signed-off-by Krish Sadhukhan <[email protected]>Reviewed-by Karl Heubaum <[email protected]>---
One nit below.
Quoted 8 lines
x86/vmx_tests.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)x86/vmx_tests.c +0
-0
x86/vmx_tests.c static void test_sysenter_field(u32 field, const char *name) · line 5002
Quoted 8 lines
x86/vmx_tests.c +5
-0@@ -4995,6 +4995,76 @@ static void test_sysenter_field(u32 field, const char *name)vmcs_write (field, addr_saved); } +/* +* Since a PAT value higher than 8 will yield the same test result as that +* of 8, we want to confine our tests only up to 8 in order to reduce +* redundancy of tests and to avoid too many vmentries. +*/
For this type of comment, it's better to say that those cases are "uninteresting" as opposed to "will yield the same test result", as we can't actually assume or guarantee anything about KVM's behavior. E.g.:
/*
* PAT values higher than 8 are uninteresting since they're likely lumped in
* with "8", e.g. "val >= 8". Cap the tests at "8" to reduce the number of
* VM-Entries and keep the runtime reasonable.
*/
Quoted 80 lines
x86/vmx_tests.c +67
-0+#define PAT_VAL_LIMIT 8 + +static void test_pat (u32 field,const char * field_name,u32 ctrl_field, +u64 ctrl_bit) +{ +u32 ctrl_saved =vmcs_read (ctrl_field); +u64 pat_saved =vmcs_read (field); +u64 i, val; +u32 j; +int error; + +vmcs_write (ctrl_field, ctrl_saved & ~ctrl_bit); +for (i =0 ; i <=PAT_VAL_LIMIT ; i++) { +/* Test PAT0..PAT7 fields */ +for (j =0 ; j <8 ; j++) { + val = i << j *8 ; +vmcs_write (field, val); +report_prefix_pushf ("%s %lx" , field_name, val); +test_vmx_vmlaunch (0 , false); +report_prefix_pop (); + } + } + +vmcs_write (ctrl_field, ctrl_saved | ctrl_bit); +for (i =0 ; i <=PAT_VAL_LIMIT ; i++) { +/* Test PAT0..PAT7 fields */ +for (j =0 ; j <8 ; j++) { + val = i << j *8 ; +vmcs_write (field, val); +report_prefix_pushf ("%s %lx" , field_name, val); +if (i ==0x2 || i ==0x3 || i >=0x8 ) + error =VMXERR_ENTRY_INVALID_HOST_STATE_FIELD ; +else + error =0 ; +test_vmx_vmlaunch (error, false); +report_prefix_pop (); + } + } + +vmcs_write (ctrl_field, ctrl_saved); +vmcs_write (field, pat_saved); +} + +/* +* If the "load IA32_PAT" VM-exit control is 1, the value of the field +* for the IA32_PAT MSR must be one that could be written by WRMSR +* without fault at CPL 0. Specifically, each of the 8 bytes in the +* field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), +* 6 (WB), or 7 (UC-). +* +* [Intel SDM] +*/ +static void test_load_host_pat (void ) +{ +/* +* "load IA32_PAT" VM-exit control +*/ +if (!(ctrl_exit_rev.clr &EXI_LOAD_PAT )) { +printf ("\"Load-IA32-PAT\" exit control not supported\n" ); +return ; + } + +test_pat (HOST_PAT ,"HOST_PAT" ,EXI_CONTROLS ,EXI_LOAD_PAT ); +} +/* * Check that the virtual CPU checks the VMX Host State Area as * documented in the Intel SDM. @@ -5010,6 +5080,8 @@ static void vmx_host_state_area_test(void)test_sysenter_field (HOST_SYSENTER_ESP ,"HOST_SYSENTER_ESP" );test_sysenter_field (HOST_SYSENTER_EIP ,"HOST_SYSENTER_EIP" ); + +test_load_host_pat (); }static bool valid_vmcs_for_vmentry (void )Signature · 1 line
2.17.2