nSVM: Fix PAT in VMCB02 and check PAT in VMCB12
cover nSVM: Fix PAT in VMCB02 and check PAT in VMCB12
According to section "Nested Paging and VMRUN/#VMEXIT" in APM vol 2:
"When VMRUN is executed with nested paging enabled (NP_ENABLE = 1),
the paging registers are affected as follows:
• VMRUN loads the guest paging state from the guest VMCB into the guest registers (i.e., VMRUN loads CR3 with the VMCB CR3 field, etc.). The guest PAT register is loaded from G_PAT field in the VMCB.
The following guest state is illegal:
• Any G_PAT.PA field has an unsupported type encoding or any reserved field in G_PAT has a nonzero value."
Patch# 1 does the following:
i) Fixes the PAT value in VMCB02 before launching nested guests as follows:
If nested paging is enabled in VMCB12, use PAT from VMCB12. Otherwise, use PAT from VMCB01.
ii) When nested guests attempt to write MSR_IA32_CR_PAT, the register
is updated only when nested paging is disabled and PAT from VMCB12 is used to update it.
iii) Adds checks for the PAT fields in VMCB12.
Patch# 2 adds a helper to check if PAT is supported by the VCPU. Patch# 3 adds tests for all the PAT fields.
[PATCH KVM 1/3] nSVM: Fix PAT value in VMCB02
[PATCH kvm-unit-tests 2/3] SVM: Add a helpter function for checking if PAT is [PATCH kvm-unit-tests 3/3] nSVM: Test G_PAT fields
arch/x86/kvm/svm/nested.c | 34 +++++++++++++++++++++++++++------- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 31 insertions(+), 9 deletions(-)
Krish Sadhukhan (1):
nSVM: Fix PAT value in VMCB02
lib/x86/asm/page.h | 11 +++++++++ lib/x86/processor.h | 1 + x86/svm.c | 13 +++++++++++ x86/svm.h | 2 ++ x86/svm_tests.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+)
Krish Sadhukhan (2):
SVM: Add a helpter function for checking if PAT is supported by the VCPU
nSVM: Test G_PAT fields
PATCH 1/3 nSVM: Fix PAT in VMCB02 and check PAT in VMCB12
Currently, KVM uses PAT from VMCB01 when launching nested guests, as well as, when nested guests write MSR_IA32_CR_PAT. But section "Nested Paging and VMRUN/#VMEXIT" in APM vol 2 states the following:
"When VMRUN is executed with nested paging enabled (NP_ENABLE = 1),
the paging registers are affected as follows:
• VMRUN loads the guest paging state from the guest VMCB into the guest registers (i.e., VMRUN loads CR3 with the VMCB CR3 field, etc.). The guest PAT register is loaded from G_PAT field in the VMCB."
Therefore, if we are launching nested guests, the PAT value from VMCB12 needs to be use in VMCB02 if nested paging is enabled in VMCB12 whereas the PAT value from VMCB01 needs to used in VMCB02 if nested paging is disabled in VMCB12.
However, when nested guets write MSR_IA32_CR_PAT, that register needs to be updated only if nested paging is disabled in nested guests and the PAT to be used is the one from VMCB12.
According to the same section in APM vol 2, the following guest state is illegal:
• Any G_PAT.PA field has an unsupported type encoding or any reserved field in G_PAT has a nonzero value.
So, add checks for the PAT fields in VMCB12.
--- arch/x86/kvm/svm/nested.c | 34 +++++++++++++++++++++++++++------- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 31 insertions(+), 9 deletions(-)
arch/x86/kvm/svm/nested.c +27 -7
@@ -326,6 +326,17 @@ static bool nested_vmcb_valid_sregs(struct kvm_vcpu *vcpu,return true; } +noinline bool nested_vmcb_check_save_area(struct kvm_vcpu *vcpu, +struct vmcb_control_area *control, +struct vmcb_save_area *save) +{ +if (CC ((control->nested_ctl &SVM_NESTED_CTL_NP_ENABLE ) && + !kvm_pat_valid (save->g_pat))) +return false; + +return nested_vmcb_valid_sregs (vcpu, save); +} +void nested_load_control_from_vmcb12 (struct vcpu_svm *svm,struct vmcb_control_area *control) {
@@ -452,21 +463,28 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,return 0 ; } -void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm) +void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm,u64 g_pat, +u64 nested_ctl,bool from_vmrun) {if (!svm->nested.vmcb02.ptr)return ; -/* FIXME: merge g_pat from vmcb01 and vmcb12. */ - svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat; +if (from_vmrun) { +if (nested_ctl &SVM_NESTED_CTL_NP_ENABLE ) + svm->nested.vmcb02.ptr->save.g_pat = g_pat; +else + svm->nested.vmcb02.ptr->save.g_pat = + svm->vmcb01.ptr->save.g_pat; + }else { +if (!(nested_ctl &SVM_NESTED_CTL_NP_ENABLE )) + svm->nested.vmcb02.ptr->save.g_pat = g_pat; + } }static void nested_vmcb02_prepare_save (struct vcpu_svm *svm,struct vmcb *vmcb12) {bool new_vmcb12 = false; -nested_vmcb02_compute_g_pat (svm); -/* Load the nested guest state */ if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) { new_vmcb12 = true;
@@ -679,8 +697,10 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)return -EINVAL ;nested_load_control_from_vmcb12 (svm, &vmcb12->control); +nested_vmcb02_compute_g_pat (svm, vmcb12->save.g_pat, + vmcb12->control.nested_ctl, true); -if (!nested_vmcb_valid_sregs (vcpu, &vmcb12->save) || +if (!nested_vmcb_check_save_area (vcpu, &vmcb12->control, &vmcb12->save) || !nested_vmcb_check_controls (vcpu, &svm->nested.ctl)) { vmcb12->control.exit_code =SVM_EXIT_ERR ; vmcb12->control.exit_code_hi =0 ;
@@ -1386,7 +1406,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,if (!(save->cr0 &X86_CR0_PG ) || !(save->cr0 &X86_CR0_PE ) || (save->rflags &X86_EFLAGS_VM ) || - !nested_vmcb_valid_sregs (vcpu, save)) + !nested_vmcb_check_save_area (vcpu, ctl, save)) goto out_free;/*
arch/x86/kvm/svm/svm.c +2 -1
@@ -2907,7 +2907,8 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) vcpu->arch.pat = data; svm->vmcb01.ptr->save.g_pat = data;if (is_guest_mode (vcpu)) -nested_vmcb02_compute_g_pat (svm); +nested_vmcb02_compute_g_pat (svm, data, + svm->vmcb->control.nested_ctl, false);vmcb_mark_dirty (svm->vmcb,VMCB_NPT );break ;case MSR_IA32_SPEC_CTRL :
arch/x86/kvm/svm/svm.h +2 -1
@@ -497,7 +497,8 @@ void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier);void nested_load_control_from_vmcb12 (struct vcpu_svm *svm,struct vmcb_control_area *control);void nested_sync_control_from_vmcb02 (struct vcpu_svm *svm); -void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm); +void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm,u64 g_pat, +u64 nested_ctl,bool from_vmrun);void svm_switch_vmcb (struct vcpu_svm *svm,struct kvm_vmcb_info *target_vmcb);extern struct kvm_x86_nested_ops svm_nested_ops;
Signature · 1 line
2.27.0
PATCH 2/3 SVM: Add a helpter function for checking if PAT is supported by the VCPU
In reply to Krish Sadhukhan--- lib/x86/processor.h | 1 + x86/svm.c | 5 +++++ x86/svm.h | 1 + 3 files changed, 7 insertions(+)
lib/x86/processor.h +1 -0
@@ -180,6 +180,7 @@ static inline bool is_intel(void)* Extended Leafs, a.k.a. AMD defined */ #define X86_FEATURE_SVM (CPUID(0x80000001, 0, ECX, 2)) +#define X86_FEATURE_PAT (CPUID(0x80000001, 0, EDX, 16))#define X86_FEATURE_NX (CPUID(0x80000001, 0, EDX, 20))#define X86_FEATURE_GBPAGES (CPUID(0x80000001, 0, EDX, 26))#define X86_FEATURE_RDTSCP (CPUID(0x80000001, 0, EDX, 27))
x86/svm.c +5 -0
@@ -89,6 +89,11 @@ bool npt_supported(void)return this_cpu_has (X86_FEATURE_NPT ); } +bool pat_supported (void ) +{ +return this_cpu_has (X86_FEATURE_PAT ); +} +int get_test_stage (struct svm_test *test) {barrier ();
x86/svm.h +1 -0
@@ -409,6 +409,7 @@ void default_prepare(struct svm_test *test);void default_prepare_gif_clear (struct svm_test *test);bool default_finished (struct svm_test *test);bool npt_supported (void ); +bool pat_supported (void );int get_test_stage (struct svm_test *test);void set_test_stage (struct svm_test *test,int s);void inc_test_stage (struct svm_test *test);
Signature · 1 line
2.27.0
PATCH 3/3 nSVM: Test G_PAT fields
In reply to Krish SadhukhanAccording to section "Nested Paging and VMRUN/#VMEXIT" in APM vol 2, the following guest state is illegal:
"Any G_PAT.PA field has an unsupported type encoding or any
reserved field in G_PAT has a nonzero value."
--- lib/x86/asm/page.h | 11 ++++++++ x86/svm.c | 8 ++++++ x86/svm.h | 1 + x86/svm_tests.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+)
lib/x86/asm/page.h +11 -0
@@ -57,5 +57,16 @@ typedef unsigned long pgd_t;#define PGDIR_BITS (lvl) (((lvl) - 1) * PGDIR_WIDTH + PAGE_SHIFT)#define PGDIR_OFFSET (va, lvl) (((va) >> PGDIR_BITS(lvl)) & PGDIR_MASK) +#ifdef __x86_64__ +enum { +PAT_UC =0 ,/* uncached */ +PAT_WC =1 ,/* Write combining */ +PAT_WT =4 ,/* Write Through */ +PAT_WP =5 ,/* Write Protected */ +PAT_WB =6 ,/* Write Back (default) */ +PAT_UC_MINUS =7 ,/* UC, but can be overridden by MTRR */ +}; +#endif +#endif /* !__ASSEMBLY__ */ #endif
x86/svm.c +8 -0
@@ -94,6 +94,14 @@ bool pat_supported(void)return this_cpu_has (X86_FEATURE_PAT ); } +bool pat_valid (u64 data) +{ +if (data &0xF8F8F8F8F8F8F8F8ull ) +return false; +/* 0, 1, 4, 5, 6, 7 are valid values. */ +return (data | ((data &0x0202020202020202ull ) <<1 )) == data; +} +int get_test_stage (struct svm_test *test) {barrier ();
x86/svm.h +1 -0
@@ -410,6 +410,7 @@ void default_prepare_gif_clear(struct svm_test *test);bool default_finished (struct svm_test *test);bool npt_supported (void );bool pat_supported (void ); +bool pat_valid (u64 data);int get_test_stage (struct svm_test *test);void set_test_stage (struct svm_test *test,int s);void inc_test_stage (struct svm_test *test);
x86/svm_tests.c +66 -0
78 lines not on this page · raw message
Signature · 1 line
2.27.0