KVM: x86: nSVM: Improve PAT virtualization
cover KVM: x86: nSVM: Improve PAT virtualization
KVM's implementation of nested SVM treats PAT the same way whether or not nested NPT is enabled: L1 and L2 share a PAT.
This is correct when nested NPT is disabled, but incorrect when nested NPT is enabled. When nested NPT is enabled, L1 and L2 have independent PATs.
The architectural specification for this separation is unusual. There is a "guest PAT register" that is accessed by references to the PAT MSR in guest mode, but it is different from the (host) PAT MSR. Other resources that have distinct host and guest values have a shared storage location, and the values are swapped on VM-entry/VM-exit.
In
https://lore.kernel.org/kvm/[email protected]/,
I proposed an implementation that adhered to the architectural specification. It had a few warts. The worst was the necessity of "fixing up" KVM_SET_MSRS when executing KVM_SET_NESTED_STATE if L2 was active and nested NPT was enabled when a snapshot was taken. Aside from Yosry's clarification, no one has responded. I will take silence to imply rejection. That's okay; I wasn't fond of that implementation myself.
The current series treats PAT just like any other resource with distinct host and guest values. There is a single shared storage location (vcpu->arch.pat), and the values are swapped on VM-entry/VM-exit. Though this implementation doesn't precisely follow the architectural specification, the guest visible behavior is the same as architected.
The first three patches ensure that the vmcb01.g_pat value at VMRUN is preserved through virtual SMM and serialization. When NPT is enabled, this field holds the host (L1) hPAT value from emulated VMRUN to emulated #VMEXIT.
The fourth patch restores (L1) hPAT value from vmcb01.g_pat at emulated #VMEXIT. Note that this is not architected, but it is required for this implementation, because hPAT and gPAT occupy the same storage location.
The next three patches handle loading vmcb12.g_pat into the (L2) guest PAT register at VMRUN. Most of this behavior is architected, but the architectural specification states that the value is loaded into the guest PAT register, leaving the hPAT register unchanged.
The eighth patch stores the (L2) guest PAT register into vmcb12_g_pat on emulated #VMEXIT, as architected.
The ninth patch fixes the emulation of WRMSR(IA32_PAT) when nested NPT is enabled.
The tenth patch introduces a new KVM selftest to validate virtualized PAT behavior.
Jim Mattson (10):
KVM: x86: nSVM: Add g_pat to fields copied by svm_copy_vmrun_state()
KVM: x86: nSVM: Add VALID_GPAT flag to kvm_svm_nested_state_hdr
KVM: x86: nSVM: Handle legacy SVM nested state in SET_NESTED_STATE
KVM: x86: nSVM: Restore L1's PAT on emulated #VMEXIT from L2 to L1
KVM: x86: nSVM: Cache g_pat in vmcb_save_area_cached
KVM: x86: nSVM: Add validity check for VMCB12 g_pat
KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on emulated #VMEXIT from L2
to L1
KVM: x86: nSVM: Fix assignment to IA32_PAT from L2
KVM: selftests: nSVM: Add svm_nested_pat test
arch/x86/include/uapi/asm/kvm.h | 3 + arch/x86/kvm/svm/nested.c | 74 +++- arch/x86/kvm/svm/svm.c | 14 +- arch/x86/kvm/svm/svm.h | 2 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/svm_nested_pat_test.c | 357 ++++++++++++++++++ 6 files changed, 432 insertions(+), 19 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
base-commit: f62b64b970570c92fe22503b0cdc65be7ce7fc7c
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 1/10 KVM: x86: nSVM: Add g_pat to fields copied by svm_copy_vmrun_state()
The vmcb01 g_pat field holds the value of L1's IA32_PAT MSR. To preserve this value through virtual SMM and serialization, add g_pat to the fields copied by svm_copy_vmrun_state().
--- arch/x86/kvm/svm/nested.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/svm/nested.c +1 -0
@@ -1090,6 +1090,7 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save, to_save->gdtr = from_save->gdtr; to_save->idtr = from_save->idtr; to_save->rflags = from_save->rflags |X86_EFLAGS_FIXED ; + to_save->g_pat = from_save->g_pat; to_save->efer = from_save->efer; to_save->cr0 = from_save->cr0; to_save->cr3 = from_save->cr3;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 2/10 KVM: x86: nSVM: Add VALID_GPAT flag to kvm_svm_nested_state_hdr
In reply to Jim MattsonNow that the svm_copy_vmrun_state() copies the g_pat field, L1's g_pat will be stored in the serialized nested state. Add a 'flags' field to the SVM nested state header, define a VALID_GPAT flag, and start reporting this flag in the serialized nested state populated by KVM_GET_NESTED_STATE.
Note that struct kvm_svm_nested_state_hdr is included in a union padded to 120 bytes, so there is room to add the flags field without changing any offsets.
--- arch/x86/include/uapi/asm/kvm.h | 3 +++ arch/x86/kvm/svm/nested.c | 1 + 2 files changed, 4 insertions(+)
arch/x86/include/uapi/asm/kvm.h +3 -0
@@ -495,6 +495,8 @@ struct kvm_sync_regs {#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001 +#define KVM_STATE_SVM_VALID_GPAT 0x00000001 +/* vendor-independent attributes for system fd (group 0) */ #define KVM_X86_GRP_SYSTEM 0# define KVM_X86_XCOMP_GUEST_SUPP 0
@@ -530,6 +532,7 @@ struct kvm_svm_nested_state_data {struct kvm_svm_nested_state_hdr {__u64 vmcb_pa; +__u32 flags; };/* for KVM_CAP_NESTED_STATE */
arch/x86/kvm/svm/nested.c +1 -0
@@ -1769,6 +1769,7 @@ static int svm_get_nested_state(struct kvm_vcpu *vcpu,/* First fill in the header and copy it out. */ if (is_guest_mode (vcpu)) { kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa; + kvm_state.hdr.svm.flags =KVM_STATE_SVM_VALID_GPAT ; kvm_state.size +=KVM_STATE_NESTED_SVM_VMCB_SIZE ; kvm_state.flags |=KVM_STATE_NESTED_GUEST_MODE ;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Sean Christopherson
maintainerRe: [PATCH 02/10] KVM: x86: nSVM: Add VALID_GPAT flag to kvm_svm_nested_state_hdr
arch/x86/kvm/svm/nested.c static int svm_get_nested_state(struct kvm_vcpu *vcpu, · line 1772
Quoted 10 lines · Jim Mattson
/* for
KVM_CAP_NESTED_STATE*/arch/x86/kvm/svm/nested.c +1
-0@@ -1769,6 +1769,7 @@ static int svm_get_nested_state(struct kvm_vcpu *vcpu,/* First fill in the header and copy it out. */ if (is_guest_mode (vcpu)) { kvm_state.hdr.svm.vmcb_pa = svm->nested.vmcb12_gpa; + kvm_state.hdr.svm.flags =KVM_STATE_SVM_VALID_GPAT ;
Assuming this survives to v2, make this:
kvm_state.hdr.svm.flags |= KVM_STATE_SVM_VALID_GPAT;
both to communicate that there's no pre-existing bug where KVM doesn't initialize flags, and so that there's less risk of clobbering others flags, e.g. if a new flag is added in the future and is set before this point.
Quoted 6 lines
arch/x86/kvm/svm/nested.c +0
-0kvm_state.size +=KVM_STATE_NESTED_SVM_VMCB_SIZE ; kvm_state.flags |=KVM_STATE_NESTED_GUEST_MODE ;Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 3/10 KVM: x86: nSVM: Handle legacy SVM nested state in SET_NESTED_STATE
In reply to Jim MattsonPreviously, KVM didn't record the vmcb01 G_PAT (i.e. the IA32_PAT MSR) in the serialized nested state. It didn't have to, because it ignored the vmcb12 g_pat field entirely. L1 and L2 simply shared the same PAT.
To preserve legacy behavior, copy the current value of the IA32_PAT MSR to the location of the vmcb01 G_PAT in the serialized nested state. (On restore, KVM_SET_MSRS should be called before KVM_SET_NESTED_STATE, so the value of the shared IA32_PAT MSR should be available.)
--- arch/x86/kvm/svm/nested.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/svm/nested.c +7 -0
@@ -1884,6 +1884,13 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,if (((cr0 &X86_CR0_CD ) ==0 ) && (cr0 &X86_CR0_NW )) goto out_free; +/* +* If kvm_state doesn't have a valid saved L1 g_pat, use the +* PAT MSR instead. This preserves the legacy behavior. +*/ +if (!(kvm_state->hdr.svm.flags &KVM_STATE_SVM_VALID_GPAT )) + save->g_pat = vcpu->arch.pat; +/* * Validate host state saved from before VMRUN (see * nested_svm_check_permissions).
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 4/10 KVM: x86: nSVM: Restore L1's PAT on emulated #VMEXIT from L2 to L1
In reply to Jim MattsonKVM doesn't implement a separate G_PAT register to hold the guest's PAT in guest mode with nested NPT enabled. Consequently, L1's IA32_PAT MSR must be restored on emulated #VMEXIT from L2 to L1.
Note: if L2 uses shadow paging, L1 and L2 share the same IA32_PAT MSR.
--- arch/x86/kvm/svm/nested.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/svm/nested.c +10 -0
@@ -1292,6 +1292,16 @@ int nested_svm_vmexit(struct vcpu_svm *svm)kvm_rsp_write (vcpu, vmcb01->save.rsp);kvm_rip_write (vcpu, vmcb01->save.rip); +/* +* KVM doesn't implement a separate guest PAT +* register. Instead, the guest PAT lives in vcpu->arch.pat +* while in guest mode with nested NPT enabled. Hence, the +* IA32_PAT MSR has to be restored from the vmcb01 g_pat at +* #VMEXIT. +*/ +if (nested_npt_enabled (svm)) + vcpu->arch.pat = vmcb01->save.g_pat; + svm->vcpu.arch.dr7 =DR7_FIXED_1 ;kvm_update_dr7 (&svm->vcpu);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Sean Christopherson
maintainerRe: [PATCH 04/10] KVM: x86: nSVM: Restore L1's PAT on emulated #VMEXIT from L2 to L1
Quoted 16 lines · Jim Mattson
KVM doesn't implement a separate
G_PATregister to hold the guest's PAT in guest mode with nested NPT enabled. Consequently, L1'sIA32_PATMSR must be restored on emulated #VMEXIT from L2 to L1.Note: if L2 uses shadow paging, L1 and L2 share the same
IA32_PATMSR.Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/svm/nested.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/svm/nested.c +0
-0
arch/x86/kvm/svm/nested.c int nested_svm_vmexit(struct vcpu_svm *svm) · line 1300
Quoted 9 lines
arch/x86/kvm/svm/nested.c +6
-0@@ -1292,6 +1292,16 @@ int nested_svm_vmexit(struct vcpu_svm *svm)kvm_rsp_write (vcpu, vmcb01->save.rsp);kvm_rip_write (vcpu, vmcb01->save.rip); +/* +* KVM doesn't implement a separate guest PAT +* register. Instead, the guest PAT lives in vcpu->arch.pat +* while in guest mode with nested NPT enabled. Hence, the +* IA32_PAT MSR has to be restored from the vmcb01 g_pat at +* #VMEXIT.
Wrap closer to 80 chars.
Quoted 10 lines
arch/x86/kvm/svm/nested.c +4
-0+*/ +if (nested_npt_enabled (svm)) + vcpu->arch.pat = vmcb01->save.g_pat; + svm->vcpu.arch.dr7 =DR7_FIXED_1 ;kvm_update_dr7 (&svm->vcpu);Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 5/10 KVM: x86: nSVM: Cache g_pat in vmcb_save_area_cached
In reply to Jim MattsonThe g_pat field from the vmcb12 save state area must be validated. To accommodate validation without TOCTTOU issues, add a g_pat field to the vmcb_save_area_cached struct, and include it in the fields copied by __nested_copy_vmcb_save_to_cache().
--- arch/x86/kvm/svm/nested.c | 2 ++ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 3 insertions(+)
arch/x86/kvm/svm/nested.c +2 -0
@@ -506,6 +506,8 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to, to->dr6 = from->dr6; to->dr7 = from->dr7; + + to->g_pat = from->g_pat; } void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,
arch/x86/kvm/svm/svm.h +1 -0
@@ -145,6 +145,7 @@ struct vmcb_save_area_cached {u64 cr0;u64 dr7;u64 dr6; +u64 g_pat; };struct vmcb_ctrl_area_cached {
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 6/10 KVM: x86: nSVM: Add validity check for VMCB12 g_pat
In reply to Jim MattsonWhen nested paging is enabled for VMCB12, an invalid g_pat causes an immediate #VMEXIT with exit code VMEXIT_INVALID, as specified in the APM, volume 2: "Nested Paging and VMRUN/#VMEXIT."
--- arch/x86/kvm/svm/nested.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +9 -3
@@ -369,7 +369,8 @@ static bool __nested_vmcb_check_controls(struct kvm_vcpu *vcpu,/* Common checks that apply to both L1 and L2 state. */ static bool __nested_vmcb_check_save (struct kvm_vcpu *vcpu, -struct vmcb_save_area_cached *save) +struct vmcb_save_area_cached *save, +struct vmcb_ctrl_area_cached *control) {if (CC (!(save->efer &EFER_SVME )))return false;
@@ -400,6 +401,10 @@ static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,if (CC (!kvm_valid_efer (vcpu, save->efer)))return false; +if (CC ((control->nested_ctl &SVM_NESTED_CTL_NP_ENABLE ) && + npt_enabled && !kvm_pat_valid (save->g_pat))) +return false; +return true; }
@@ -407,8 +412,9 @@ static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);struct vmcb_save_area_cached *save = &svm->nested.save; +struct vmcb_ctrl_area_cached *ctl = &svm->nested.ctl; -return __nested_vmcb_check_save (vcpu, save); +return __nested_vmcb_check_save (vcpu, save, ctl); }static bool nested_vmcb_check_controls (struct kvm_vcpu *vcpu)
@@ -1911,7 +1917,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_check_save (vcpu, &save_cached)) + !__nested_vmcb_check_save (vcpu, &save_cached, &ctl_cached)) goto out_free;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 7/10 KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
In reply to Jim MattsonWhen nested NPT is enabled in VMCB12, copy the (cached and validated) VMCB12 g_pat field to the IA32_PAT MSR and to the VMCB02 g_pat field. (The latter can be skipped if the VMCB02 g_pat field already has the correct value.)
When NPT is enabled, but nested NPT is disabled, copy L1's IA32_PAT MSR to the VMCB02 g_pat field (L1 and L2 share the same IA32_PAT MSR in this scenario).
When NPT is disabled, the VMCB02 g_pat field is ignored by hardware.
--- arch/x86/kvm/svm/nested.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +20 -3
@@ -656,9 +656,6 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;struct kvm_vcpu *vcpu = &svm->vcpu; -nested_vmcb02_compute_g_pat (svm); -vmcb_mark_dirty (vmcb02,VMCB_NPT ); -/* Load the nested guest state */ if (svm->nested.vmcb12_gpa != svm->nested.last_vmcb12_gpa) { new_vmcb12 = true;
@@ -666,6 +663,26 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12 svm->nested.force_msr_bitmap_recalc = true; } +if (npt_enabled) { +if (nested_npt_enabled (svm)) { +/* +* KVM doesn't implement a separate guest PAT +* register. Instead, the guest PAT lives in +* vcpu->arch.pat while in guest mode with +* nested NPT enabled. +*/ + vcpu->arch.pat = svm->nested.save.g_pat; +if (unlikely (new_vmcb12 || +vmcb_is_dirty (vmcb12,VMCB_NPT ))) { + vmcb02->save.g_pat = svm->nested.save.g_pat; +vmcb_mark_dirty (vmcb02,VMCB_NPT ); + } + }else { + vmcb02->save.g_pat = vcpu->arch.pat; +vmcb_mark_dirty (vmcb02,VMCB_NPT ); + } + } +if (unlikely (new_vmcb12 ||vmcb_is_dirty (vmcb12,VMCB_SEG ))) { vmcb02->save.es = vmcb12->save.es; vmcb02->save.cs = vmcb12->save.cs;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 8/10 KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on emulated #VMEXIT from L2 to L1
In reply to Jim MattsonAccording to the APM volume 3 pseudo-code for "VMRUN," when nested paging is enabled in the VMCB, the guest PAT register (gPAT) is saved to the VMCB on #VMEXIT.
KVM doesn't implement a separate gPAT register. Instead, the guest PAT is stored in the IA32_PAT MSR while in guest mode (L2) and nested NPT is enabled in vmcs02.
Save the current IA32_PAT MSR to the vmcb12 g_pat field on emulated #VMEXIT from L2 to L1.
--- arch/x86/kvm/svm/nested.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/kvm/svm/nested.c +9 -0
@@ -1197,6 +1197,15 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12->save.dr6 = svm->vcpu.arch.dr6; vmcb12->save.cpl = vmcb02->save.cpl; +/* +* KVM stores the guest PAT in the IA32_PAT register while in +* guest mode with nested NPT enabled (rather than in a +* separate G_PAT register). Hence, the IA32_PAT MSR is stored +* in the VMCB12 g_pat field on #VMEXIT. +*/ +if (nested_npt_enabled (svm)) + vmcb12->save.g_pat = vcpu->arch.pat; +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SHSTK )) { vmcb12->save.s_cet = vmcb02->save.s_cet; vmcb12->save.isst_addr = vmcb02->save.isst_addr;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH 9/10 KVM: x86: nSVM: Fix assignment to IA32_PAT from L2
In reply to Jim MattsonIn svm_set_msr(), when the IA32_PAT MSR is updated, up to two vmcb g_pat fields must be updated.
When NPT is disabled, no g_pat fields have to be updated, as they are ignored by hardware.
When NPT is enabled, the current VMCB (either VMCB01 or VMCB02) g_pat field must be updated.
In addition, when in guest mode and nested NPT is disabled, the VMCB01 g_pat field must be updated. In this scenario, L1 and L2 share the same IA32_PAT MSR.
--- arch/x86/kvm/svm/nested.c | 9 --------- arch/x86/kvm/svm/svm.c | 14 +++++++++++--- arch/x86/kvm/svm/svm.h | 1 - 3 files changed, 11 insertions(+), 13 deletions(-)
arch/x86/kvm/svm/nested.c +0 -9
@@ -640,15 +640,6 @@ 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) -{ -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; -} -static void nested_vmcb02_prepare_save (struct vcpu_svm *svm,struct vmcb *vmcb12) {bool new_vmcb12 = false;
arch/x86/kvm/svm/svm.c +11 -3
@@ -2933,10 +2933,18 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (ret)break ; - svm->vmcb01.ptr->save.g_pat = data; -if (is_guest_mode (vcpu)) -nested_vmcb02_compute_g_pat (svm); +if (!npt_enabled) +break ; + + svm->vmcb->save.g_pat = data;vmcb_mark_dirty (svm->vmcb,VMCB_NPT ); + +if (!is_guest_mode (vcpu) ||nested_npt_enabled (svm)) +break ; + + svm->vmcb01.ptr->save.g_pat = data; +vmcb_mark_dirty (svm->vmcb01.ptr,VMCB_NPT ); +break ;case MSR_IA32_SPEC_CTRL : if (!msr->host_initiated &&
arch/x86/kvm/svm/svm.h +0 -1
@@ -801,7 +801,6 @@ void nested_copy_vmcb_control_to_cache(struct vcpu_svm *svm,void nested_copy_vmcb_save_to_cache (struct vcpu_svm *svm,struct vmcb_save_area *save);void nested_sync_control_from_vmcb02 (struct vcpu_svm *svm); -void nested_vmcb02_compute_g_pat (struct vcpu_svm *svm);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.52.0.457.g6b5491de43-goog
PATCH 10/10 KVM: selftests: nSVM: Add svm_nested_pat test
In reply to Jim MattsonVerify KVM's virtualization of the PAT MSR and--when nested NPT is enabled--the VMCB12 g_pat field and the guest PAT register.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/svm_nested_pat_test.c | 357 ++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -109,6 +109,7 @@ TEST_GEN_PROGS_x86 += x86/state_test TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test TEST_GEN_PROGS_x86 += x86/svm_vmcall_test TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test +TEST_GEN_PROGS_x86 += x86/svm_nested_pat_test TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +357 -0
357 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Quoted 7 lines · Jim Mattson
KVM's implementation of nested SVM treats PAT the same way whether or not nested NPT is enabled: L1 and L2 share a PAT.
This is correct when nested NPT is disabled, but incorrect when nested NPT is enabled. When nested NPT is enabled, L1 and L2 have independent PATs.
Yosry points out that this series does not correctly handle saving a checkpoint on a new kernel and restoring it on an old kernel. In that scenario, KVM_SET_MSRS will restore the L2 PAT, and the old kernel will not restore L1's PAT on emulated #VMEXIT.
I have also discovered that not all userspace VMMs restore MSRs before nested state.
Ironically, I think the way to correctly deal with compatibility in both directions is to go back to the architected separation of hPAT and gPAT. Accesses to IA32_PAT from userspace will always have to reference hPAT to properly restore a new checkpoint on an old kernel.
Cooking up v2...
Sean Christopherson
maintainerTrimmed the Cc.
Please adjust whatever script (or script invocation) you're using to generate the Cc list. AFAIK, Alex hasn't for SUSE for years, Radim hasn't worked for Red Hat for years, and Avi hasn't worked on KVM at all for even longer :-)