KVM: x86: nSVM: Improve PAT virtualization
Subsystems (5)
v7 cover KVM: x86: nSVM: Improve PAT virtualization
Currently, KVM's implementation of nested SVM treats the PAT MSR the same way whether or not nested NPT is enabled: L1 and L2 share a single PAT. However, the AMD APM specifies that when nested NPT is enabled, the host (L1) and the guest (L2) should have independent PATs: hPAT for L1 and gPAT for L2.
This patch series implements independent PATs for L1 and L2 when nested NPT is enabled, but only when a new quirk, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT, is disabled. By default, the quirk is enabled, preserving KVM's legacy behavior. When the quirk is disabled, KVM correctly virtualizes a separate PAT register for L2, using the g_pat field in the VMCB.
Guest accesses to the IA32_PAT MSR are redirected to either hPAT or gPAT depending on the current mode and whether nested NPT is enabled. All other accesses, including userspace accesses via KVM_{GET,SET}_MSRS, continue to reference hPAT. L2's gPAT is saved and restored via a new 'gpat' field in kvm_svm_nested_state_hdr, which is within the existing padding of the header to maintain ABI compatibility.
v1: https://lore.kernel.org/kvm/[email protected]/
v2: https://lore.kernel.org/kvm/[email protected]/
v3: https://lore.kernel.org/kvm/[email protected]/
v4: https://lore.kernel.org/kvm/[email protected]/
v5: https://lore.kernel.org/kvm/[email protected]/
v6: https://lore.kernel.org/kvm/[email protected]/
v6 -> v7:
* Drop the patch "KVM: x86: Remove common handling of MSR_IA32_CR_PAT," because TDX still calls the common handler.
* Instead, add a warning to the common PAT handling code if it is called by an SVM-capable vCPU
* Add comments about userspace disabling the quirk while a vCPU is running.
* Cache the value of use_separate_l2_pat in KVM_SET_NESTED_STATE, in case the quirk is disabled concurrently with the execution of that function.
Jim Mattson (9):
KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
KVM: x86: nSVM: Clear VMCB_NPT clean bit when updating hPAT from guest
mode
KVM: x86: nSVM: Cache and validate vmcb12 g_pat
KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT
KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on VMEXIT
KVM: Documentation: document KVM_{GET,SET}_NESTED_STATE for SVM
KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
KVM: selftests: nSVM: Add svm_nested_pat test
Documentation/virt/kvm/api.rst | 26 ++ arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/uapi/asm/kvm.h | 2 + arch/x86/kvm/svm/nested.c | 65 +++- arch/x86/kvm/svm/svm.c | 54 +++- arch/x86/kvm/svm/svm.h | 19 +- arch/x86/kvm/x86.c | 6 + tools/arch/x86/include/uapi/asm/kvm.h | 2 + tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/svm_nested_pat_test.c | 304 ++++++++++++++++++ 10 files changed, 457 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
base-commit: 3d6cdcc8883b5726513d245eef0e91cabfc397f7
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 1/9 KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
Define a quirk to control whether nested SVM shares L1's PAT with L2 (legacy behavior) or gives L2 its own independent gPAT (correct behavior per the APM).
When the quirk is enabled (default), L2 shares L1's PAT, preserving the legacy KVM behavior. When userspace disables the quirk, KVM correctly virtualizes the PAT for nested SVM guests, giving L2 a separate gPAT as specified in the AMD architecture.
--- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/svm.h | 11 +++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +14 -0
@@ -8551,6 +8551,20 @@ KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM By default, KVM relaxes the consisten bit to be cleared. Note that the vmcs02 bit is still completely controlled by the host, regardless of the quirk setting. + +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT By default, KVM for nested SVM guests + shares the IA32_PAT MSR between L1 and + L2. This is legacy behavior and does + not match the AMD architecture + specification. When this quirk is + disabled and nested paging (NPT) is + enabled for L2, KVM correctly + virtualizes a separate guest PAT + register for L2, using the g_pat + field in the VMCB. When NPT is + disabled for L2, L1 and L2 continue + to share the IA32_PAT MSR regardless + of the quirk setting. ======================================== ================================================ 7.32 KVM_CAP_MAX_VCPU_ID
arch/x86/include/asm/kvm_host.h +2 -1
@@ -2511,7 +2511,8 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages); KVM_X86_QUIRK_SLOT_ZAP_ALL | \ KVM_X86_QUIRK_STUFF_FEATURE_MSRS | \ KVM_X86_QUIRK_IGNORE_GUEST_PAT | \ - KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM) + KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM | \ + KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)#define KVM_X86_CONDITIONAL_QUIRKS \ (KVM_X86_QUIRK_CD_NW_CLEARED | \
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -477,6 +477,7 @@ struct kvm_sync_regs {#define KVM_X86_QUIRK_STUFF_FEATURE_MSRS (1 << 8)#define KVM_X86_QUIRK_IGNORE_GUEST_PAT (1 << 9)#define KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM (1 << 10) +#define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT (1 << 11)#define KVM_STATE_NESTED_FORMAT_VMX 0#define KVM_STATE_NESTED_FORMAT_SVM 1
arch/x86/kvm/svm/svm.h +11 -0
@@ -616,6 +616,17 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)return svm->nested.ctl.misc_ctl &SVM_MISC_ENABLE_NP ; } +static inline bool l2_has_separate_pat (struct vcpu_svm *svm) +{ +/* +* If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU +* is running, the L2 IA32_PAT semantics for that vCPU are undefined. +*/ +return nested_npt_enabled (svm) && + !kvm_check_has_quirk (svm->vcpu.kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT ); +} +static inline bool nested_vnmi_enabled (struct vcpu_svm *svm) { return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VNMI) &&
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
kernel test robot
build botRe: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
kernel test robot noticed the following build errors:
Hi Jim,
kernel test robot noticed the following build errors:
[auto build test ERROR on 3d6cdcc8883b5726513d245eef0e91cabfc397f7]
url: https://github.com/intel-lab-lkp/linux/commits/Jim-Mattson/KVM-x86-Define-KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT/20260328-110805 base: 3d6cdcc8883b5726513d245eef0e91cabfc397f7 patch link: https://lore.kernel.org/r/20260327234023.2659476-2-jmattson%40google.com patch subject: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT config: x86_64-randconfig-016-20260330 (https://download.01.org/0day-ci/archive/20260330/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260330/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from arch/x86/kvm/svm/svm_onhyperv.c:11:
arch/x86/kvm/svm/svm.h: In function 'l2_has_separate_pat':
>> arch/x86/kvm/svm/svm.h:626:18: error: implicit declaration of function 'kvm_check_has_quirk'; did you mean 'kvm_check_request'? [-Wimplicit-function-declaration]
626 | !kvm_check_has_quirk(svm->vcpu.kvm,
| ^~~~~~~~~~~~~~~~~~~
| kvm_check_request
In file included from arch/x86/kvm/svm/svm_ops.h:7,
from arch/x86/kvm/svm/svm_onhyperv.c:12:
arch/x86/kvm/x86.h: At top level:
>> arch/x86/kvm/x86.h:429:20: error: conflicting types for 'kvm_check_has_quirk'; have 'bool(struct kvm *, u64)' {aka '_Bool(struct kvm *, long long unsigned int)'}
429 | static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
| ^~~~~~~~~~~~~~~~~~~
arch/x86/kvm/svm/svm.h:626:18: note: previous implicit declaration of 'kvm_check_has_quirk' with type 'int()'
626 | !kvm_check_has_quirk(svm->vcpu.kvm,
| ^~~~~~~~~~~~~~~~~~~--
In file included from kvm/svm/svm_onhyperv.c:11:
kvm/svm/svm.h: In function 'l2_has_separate_pat':
kvm/svm/svm.h:626:18: error: implicit declaration of function 'kvm_check_has_quirk'; did you mean 'kvm_check_request'? [-Wimplicit-function-declaration]
626 | !kvm_check_has_quirk(svm->vcpu.kvm,
| ^~~~~~~~~~~~~~~~~~~
| kvm_check_request
In file included from kvm/svm/svm_ops.h:7,
from kvm/svm/svm_onhyperv.c:12:
arch/x86/kvm/x86.h: At top level:
>> arch/x86/kvm/x86.h:429:20: error: conflicting types for 'kvm_check_has_quirk'; have 'bool(struct kvm *, u64)' {aka '_Bool(struct kvm *, long long unsigned int)'}
429 | static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
| ^~~~~~~~~~~~~~~~~~~
kvm/svm/svm.h:626:18: note: previous implicit declaration of 'kvm_check_has_quirk' with type 'int()'
626 | !kvm_check_has_quirk(svm->vcpu.kvm,
| ^~~~~~~~~~~~~~~~~~~vim +626 arch/x86/kvm/svm/svm.h
618
619 static inline bool l2_has_separate_pat(struct vcpu_svm *svm)
620 {
621 /*
622 * If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU
623 * is running, the L2 IA32_PAT semantics for that vCPU are undefined.
624 */
625 return nested_npt_enabled(svm) &&
> 626 !kvm_check_has_quirk(svm->vcpu.kvm,
627 KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
628 }
629
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
Quoted 31 lines · kernel test robot
Hi Jim,
kernel test robot noticed the following build errors:
[auto build test ERROR on
3d6cdcc8883b5726513d245eef0e91cabfc397f7]url: https://github.com/intel-lab-lkp/linux/commits/Jim-Mattson/KVM-x86-Define-KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT/20260328-110805 base: 3d6cdcc8883b5726513d245eef0e91cabfc397f7 patch link: https://lore.kernel.org/r/20260327234023.2659476-2-jmattson%40google.com patch subject: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT config: x86_64-randconfig-016-20260330 (https://download.01.org/0day-ci/archive/20260330/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260330/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
In file included from
arch/x86/kvm/svm/svm_onhyperv.c:11:arch/x86/kvm/svm/svm.h: In function 'l2_has_separate_pat':Quoted 1 line
Quoted 1 line
arch/x86/kvm/svm/svm.h:626:18: error: implicit declaration of function 'kvm_check_has_quirk'; did you mean 'kvm_check_request'? [-Wimplicit-function-declaration]626 | !kvm_check_has_quirk(svm->vcpu.kvm, | ^~~~~~~~~~~~~~~~~~~ | kvm_check_request In file included from arch/x86/kvm/svm/svm_ops.h:7, from arch/x86/kvm/svm/svm_onhyperv.c:12: arch/x86/kvm/x86.h: At top level:
Quoted 7 lines
Quoted 1 line
Quoted 1 line
arch/x86/kvm/x86.h:429:20: error: conflicting types for 'kvm_check_has_quirk'; have 'bool(struct kvm *, u64)' {aka '_Bool(struct kvm *, long long unsigned int)'}429 | static inline bool
kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
| ^~~~~~~~~~~~~~~~~~~
arch/x86/kvm/svm/svm.h:626:18: note: previous implicit declaration of 'kvm_check_has_quirk' with type 'int()'626 | !kvm_check_has_quirk(svm->vcpu.kvm, | ^~~~~~~~~~~~~~~~~~~--
It looks like svm.h should include x86.h.
Sean: Do you want me to send a new series?
Re: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
Quoted 3 lines
It looks like
svm.hshould includex86.h.Sean: Do you want me to send a new series?
FWIW, this is the same problem as:
https://lore.kernel.org/kvm/CAO9r8zPuDcHMObfzTQVY-P0Z3kXZbw6y5KJizxvpFWXdW7uKbQ@mail.gmail.com/.
So only the first series that gets picked up will need the fixup.
Sean Christopherson
maintainerRe: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
In reply to Jim Mattsonarch/x86/kvm/svm/svm.h static inline bool nested_npt_enabled(struct vcpu_svm *svm) · line 619
Quoted 9 lines · Jim Mattson
arch/x86/kvm/svm/svm.h +1
-0@@ -616,6 +616,17 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)return svm->nested.ctl.misc_ctl &SVM_MISC_ENABLE_NP ; } +static inline bool l2_has_separate_pat (struct vcpu_svm *svm)
Take @vcpu instead of @svm. All of the callers have a "vcpu", but not all have a local "svm". That will shorten the quirk check far enough to let it poke out.
arch/x86/kvm/svm/svm.h static inline bool nested_npt_enabled(struct vcpu_svm *svm) · line 627
Quoted 8 lines
arch/x86/kvm/svm/svm.h +8
-0+{ +/* +* If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU +* is running, the L2 IA32_PAT semantics for that vCPU are undefined. +*/ +return nested_npt_enabled (svm) && + !kvm_check_has_quirk (svm->vcpu.kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );
Align indentation. With the @svm => @vcpu change, this becomes:
return nested_npt_enabled(to_svm(vcpu)) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
Quoted 8 lines
arch/x86/kvm/svm/svm.h +2
-0+} +static inline bool nested_vnmi_enabled (struct vcpu_svm *svm) { return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VNMI) &&Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Jim Mattson
arch/x86/kvm/svm/svm.h +0
-0@@ -616,6 +616,17 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)return svm->nested.ctl.misc_ctl &SVM_MISC_ENABLE_NP ; }
Quoted 4 lines
arch/x86/kvm/svm/svm.h static inline bool nested_npt_enabled(struct vcpu_svm *svm) · line 619
Quoted 1 line
arch/x86/kvm/svm/svm.h +1
-0+static inline bool l2_has_separate_pat (struct vcpu_svm *svm)Take @vcpu instead of @svm. All of the callers have a "vcpu", but not all have a local "svm". That will shorten the quirk check far enough to let it poke out.
What is the actual line length limit?
Quoted 9 lines
arch/x86/kvm/svm/svm.h static inline bool nested_npt_enabled(struct vcpu_svm *svm) · line 627
Quoted 8 lines
arch/x86/kvm/svm/svm.h +8
-0+{ +/* +* If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU +* is running, the L2 IA32_PAT semantics for that vCPU are undefined. +*/ +return nested_npt_enabled (svm) && + !kvm_check_has_quirk (svm->vcpu.kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );
Quoted 4 lines
Align indentation. With the @svm => @vcpu change, this becomes:
return nested_npt_enabled(to_svm(vcpu)) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
You wouldn't happen to know the Emacs configuration for the alignment you like, would you? I asked Gemini, but it lied to me.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/svm.h +2
-1+} +static inline bool nested_vnmi_enabled (struct vcpu_svm *svm) { return guest_cpu_cap_has(&svm->vcpu, X86_FEATURE_VNMI) && --2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v7 1/9] KVM: x86: Define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT
Quoted 11 lines · Jim Mattson
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Jim Mattson
arch/x86/kvm/svm/svm.h +0
-0@@ -616,6 +616,17 @@ static inline bool nested_npt_enabled(struct vcpu_svm *svm)return svm->nested.ctl.misc_ctl &SVM_MISC_ENABLE_NP ; }
Quoted 6 lines
Quoted 4 lines
arch/x86/kvm/svm/svm.h static inline bool nested_npt_enabled(struct vcpu_svm *svm) · line 619
Quoted 1 line
arch/x86/kvm/svm/svm.h +1
-0+static inline bool l2_has_separate_pat (struct vcpu_svm *svm)Take @vcpu instead of @svm. All of the callers have a "vcpu", but not all have a local "svm". That will shorten the quirk check far enough to let it poke out.
What is the actual line length limit?
There's a "medium-firm" limit at 80 and a "mostly-hard" limit at 100. 100 isn't a true hard limit to allow for things like pre-formatted strings, and cases where the only way to stay under 100 chars would (arguably) yield less readable code overall, e.g. msr-index.h deliberately has this
#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI (1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
and not
#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI \
(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
arch/x86/kvm/svm/svm.h +8
-0+{ +/* +* If KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled while a vCPU +* is running, the L2 IA32_PAT semantics for that vCPU are undefined. +*/ +return nested_npt_enabled (svm) && + !kvm_check_has_quirk (svm->vcpu.kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );
Quoted 7 lines
Quoted 4 lines
Align indentation. With the @svm => @vcpu change, this becomes:
return nested_npt_enabled(to_svm(vcpu)) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);You wouldn't happen to know the Emacs configuration for the alignment you like, would you? I asked Gemini, but it lied to me.
Heh, no. Any time I unintentionally end up in Emacs, I have to do a search just to figure out how to save and exit :-)
PATCH v7 2/9 KVM: x86: nSVM: Clear VMCB_NPT clean bit when updating hPAT from guest mode
In reply to Jim MattsonWhen running an L2 guest and writing to MSR_IA32_CR_PAT, the host PAT value is stored in both vmcb01's g_pat field and vmcb02's g_pat field, but the clean bit was only being cleared for vmcb02.
Introduce the helper vmcb_set_gpat() which sets vmcb->save.g_pat and marks the VMCB dirty for VMCB_NPT. Use this helper in both svm_set_msr() for updating vmcb01 and in nested_vmcb02_compute_g_pat() for updating vmcb02, ensuring both VMCBs' NPT fields are properly marked dirty.
--- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/svm/svm.c | 3 +-- arch/x86/kvm/svm/svm.h | 6 ++++++ 3 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +1 -1
@@ -697,7 +697,7 @@ void nested_vmcb02_compute_g_pat(struct vcpu_svm *svm)return ;/* FIXME: merge g_pat from vmcb01 and vmcb12. */ - svm->nested.vmcb02.ptr->save.g_pat = svm->vmcb01.ptr->save.g_pat; +vmcb_set_gpat (svm->nested.vmcb02.ptr, svm->vmcb01.ptr->save.g_pat); }static bool nested_vmcb12_has_lbrv (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.c +1 -2
@@ -2979,10 +2979,9 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (ret)break ; - svm->vmcb01.ptr->save.g_pat = data; +vmcb_set_gpat (svm->vmcb01.ptr, data);if (is_guest_mode (vcpu))nested_vmcb02_compute_g_pat (svm); -vmcb_mark_dirty (svm->vmcb,VMCB_NPT );break ;case MSR_IA32_SPEC_CTRL : if (!msr->host_initiated &&
arch/x86/kvm/svm/svm.h +6 -0
@@ -439,6 +439,12 @@ static inline bool vmcb12_is_dirty(struct vmcb_ctrl_area_cached *control, int bireturn !test_bit (bit, (unsigned long *)&control->clean); } +static inline void vmcb_set_gpat (struct vmcb *vmcb,u64 data) +{ + vmcb->save.g_pat = data; +vmcb_mark_dirty (vmcb,VMCB_NPT ); +} + static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) { return container_of(vcpu, struct vcpu_svm, vcpu);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 3/9 KVM: x86: nSVM: Cache and validate vmcb12 g_pat
In reply to Jim MattsonWhen KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and nested paging is enabled in vmcb12, validate g_pat at emulated VMRUN and cause an immediate VMEXIT with exit code VMEXIT_INVALID if it is invalid, as specified in the APM, volume 2: "Nested Paging and VMRUN/VMEXIT."
--- arch/x86/kvm/svm/nested.c | 23 +++++++++++++++++++---- arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +19 -4
@@ -410,7 +410,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, +bool check_gpat) {if (CC (!(save->efer &EFER_SVME )))return false;
@@ -445,6 +446,15 @@ static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu,if (CC (!kvm_valid_efer (vcpu, save->efer)))return false; +/* +* If userspace contrives to get an invalid g_pat into vmcb02 by +* disabling KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT in a race with +* this check, it should be prepared for the KVM_EXIT_FAIL_ENTRY +* that will follow. +*/ +if (check_gpat &&CC (!kvm_pat_valid (save->g_pat))) +return false; +return true; }
@@ -452,7 +462,8 @@ int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -if (!nested_vmcb_check_save (vcpu, &svm->nested.save) || +if (!nested_vmcb_check_save (vcpu, &svm->nested.save, +l2_has_separate_pat (svm)) || !nested_vmcb_check_controls (vcpu, &svm->nested.ctl))return -EINVAL ;
@@ -562,6 +573,7 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to, to->rax = from->rax; to->cr2 = from->cr2; + to->g_pat = from->g_pat;svm_copy_lbrs (to, from); }
@@ -1971,13 +1983,16 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,/* * Validate host state saved from before VMRUN (see -* nested_svm_check_permissions). +* nested_svm_check_permissions). Note that the g_pat field is not +* validated, because (a) it may have been clobbered by SMM before +* KVM_GET_NESTED_STATE, and (b) it is not loaded at emulated +* #VMEXIT. */ __nested_copy_vmcb_save_to_cache (&save_cached, save);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, false)) goto out_free;
arch/x86/kvm/svm/svm.h +1 -0
@@ -161,6 +161,7 @@ struct vmcb_save_area_cached {u64 isst_addr;u64 rax;u64 cr2; +u64 g_pat;u64 dbgctl;u64 br_from;u64 br_to;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 4/9 KVM: x86: nSVM: Set vmcb02.g_pat correctly for nested NPT
In reply to Jim MattsonWhen KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and nested NPT is enabled in vmcb12, copy the (cached and validated) vmcb12 g_pat field to vmcb02's g_pat, giving L2 its own independent guest PAT register.
When the quirk is enabled (default), or when NPT is enabled but nested NPT is disabled, copy L1's IA32_PAT MSR to the vmcb02 g_pat field, since L2 shares the IA32_PAT MSR with L1.
When NPT is disabled, the g_pat field is ignored by hardware.
--- arch/x86/kvm/svm/nested.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +7 -3
@@ -727,9 +727,6 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)struct 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;
@@ -760,6 +757,13 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)vmcb_mark_dirty (vmcb02,VMCB_CET ); } +if (l2_has_separate_pat (svm)) { +if (unlikely (new_vmcb12 ||vmcb12_is_dirty (control,VMCB_NPT ))) +vmcb_set_gpat (vmcb02, svm->nested.save.g_pat); + }else if (npt_enabled) { +vmcb_set_gpat (vmcb02, vcpu->arch.pat); + } +kvm_set_rflags (vcpu, save->rflags |X86_EFLAGS_FIXED );svm_set_efer (vcpu, svm->nested.save.efer);
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 5/9 KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT
In reply to Jim MattsonWhen KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and the vCPU is in guest mode with nested NPT enabled, guest accesses to IA32_PAT are redirected to the gPAT register, which is stored in VMCB02's g_pat field.
Non-guest accesses (e.g. from userspace) to IA32_PAT are always redirected to hPAT, which is stored in vcpu->arch.pat.
Directing host-initiated accesses to hPAT ensures that KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent of each other and can be ordered arbitrarily during save and restore. gPAT is saved and restored separately via KVM_GET/SET_NESTED_STATE.
Use WARN_ON_ONCE to flag any host-initiated accesses originating from KVM itself rather than userspace.
Use pr_warn_once to flag any use of the common MSR-handling code (now shared by VMX and TDX) for IA32_PAT by a vCPU that is SVM-capable.
--- arch/x86/kvm/svm/nested.c | 9 ------- arch/x86/kvm/svm/svm.c | 53 ++++++++++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 - arch/x86/kvm/x86.c | 6 +++++ 4 files changed, 53 insertions(+), 16 deletions(-)
arch/x86/kvm/svm/nested.c +0 -9
@@ -703,15 +703,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. */ -vmcb_set_gpat (svm->nested.vmcb02.ptr, svm->vmcb01.ptr->save.g_pat); -} -static bool nested_vmcb12_has_lbrv (struct kvm_vcpu *vcpu) { return guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
arch/x86/kvm/svm/svm.c +47 -6
72 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +0 -1
@@ -868,7 +868,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;
arch/x86/kvm/x86.c +6 -0
@@ -4025,6 +4025,9 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }break ;case MSR_IA32_CR_PAT : +if (!(efer_reserved_bits &EFER_SVME )) +pr_warn_once ("%s: MSR_IA32_CR_PAT should be handled by AMD vendor-specific code\n" , __func__); +if (!kvm_pat_valid (data))return 1 ;
@@ -4436,6 +4439,9 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ; }case MSR_IA32_CR_PAT : +if (!(efer_reserved_bits &EFER_SVME )) +pr_warn_once ("%s: MSR_IA32_CR_PAT should be handled by AMD vendor-specific code\n" , __func__); + msr_info->data = vcpu->arch.pat;break ;case MSR_MTRRcap:
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v7 5/9] KVM: x86: nSVM: Redirect IA32_PAT accesses to either hPAT or gPAT
Quoted 54 lines · Jim Mattson
When
KVM_X86_QUIRK_NESTED_SVM_SHARED_PATis disabled and the vCPU is in guest mode with nested NPT enabled, guest accesses toIA32_PATare redirected to the gPAT register, which is stored in VMCB02'sg_patfield.Non-guest accesses (e.g. from userspace) to
IA32_PATare always redirected to hPAT, which is stored invcpu->arch.pat.Directing host-initiated accesses to hPAT ensures that KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent of each other and can be ordered arbitrarily during save and restore. gPAT is saved and restored separately via KVM_GET/SET_NESTED_STATE.
Use
WARN_ON_ONCEto flag any host-initiated accesses originating from KVM itself rather than userspace.Use
pr_warn_onceto flag any use of the common MSR-handling code (now shared by VMX and TDX) forIA32_PATby a vCPU that is SVM-capable.Fixes 15038e147247 ("KVM: SVM: obey guest PAT")Signed-off-by Jim Mattson <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/svm/nested.c | 9 ------- arch/x86/kvm/svm/svm.c | 53 ++++++++++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 - arch/x86/kvm/x86.c | 6 +++++ 4 files changed, 53 insertions(+), 16 deletions(-)arch/x86/kvm/svm/nested.c +0
-9@@ -703,15 +703,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. */ -vmcb_set_gpat (svm->nested.vmcb02.ptr, svm->vmcb01.ptr->save.g_pat); -} -static bool nested_vmcb12_has_lbrv (struct kvm_vcpu *vcpu) { return guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&arch/x86/kvm/svm/svm.c +0
-0
arch/x86/kvm/svm/svm.c static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu, · line 2784
Quoted 9 lines
arch/x86/kvm/svm/svm.c +6
-0@@ -2776,6 +2776,47 @@ static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu, !msr_write_intercepted (vcpu, msr_info->index); } +static bool svm_pat_accesses_gpat (struct kvm_vcpu *vcpu,bool from_host) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +/* +* When nested NPT is enabled, L2 has a separate PAT from L1. Guest
Hmm, do we also want to add "and KVM's quirk is disabled"?
/*
* When nested NPT is enabled and KVM's quirk is disabled, L2 has a
* separate PAT from L1. Guest accesses to IA32_PAT while running L2
* target L2's gPAT; host-initiated accesses always target L1's hPAT so
* that KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent
* of each other and can be ordered arbitrarily during save and restore. */
Quoted 82 lines
arch/x86/kvm/svm/svm.c +41
-6+* accesses to IA32_PAT while running L2 target L2's gPAT; +* host-initiated accesses always target L1's hPAT so that +* KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent of +* each other and can be ordered arbitrarily during save and restore. +*/ +WARN_ON_ONCE (from_host && vcpu->wants_to_run); +return !from_host &&is_guest_mode (vcpu) &&l2_has_separate_pat (svm); +} + +static u64 svm_get_pat (struct kvm_vcpu *vcpu,bool from_host) +{ +if (svm_pat_accesses_gpat (vcpu, from_host)) +return to_svm (vcpu)->vmcb->save.g_pat; +else +return vcpu->arch.pat; +} + +static void svm_set_pat (struct kvm_vcpu *vcpu,bool from_host,u64 data) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +if (svm_pat_accesses_gpat (vcpu, from_host)) { +vmcb_set_gpat (svm->vmcb, data); +return ; + } + + svm->vcpu.arch.pat = data; + +if (npt_enabled) { +vmcb_set_gpat (svm->vmcb01.ptr, data); +if (is_guest_mode (&svm->vcpu) && !l2_has_separate_pat (svm)) +vmcb_set_gpat (svm->vmcb, data); + } +} +static int svm_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {struct vcpu_svm *svm =to_svm (vcpu);@@ -2892,6 +2933,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_AMD64_DE_CFG : msr_info->data = svm->msr_decfg;break ; +case MSR_IA32_CR_PAT : + msr_info->data =svm_get_pat (vcpu, msr_info->host_initiated); +break ;default :return kvm_get_msr_common (vcpu, msr_info); }@@ -2975,13 +3019,10 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)break ;case MSR_IA32_CR_PAT : - ret =kvm_set_msr_common (vcpu, msr); -if (ret) -break ; +if (!kvm_pat_valid (data)) +return 1 ; -vmcb_set_gpat (svm->vmcb01.ptr, data); -if (is_guest_mode (vcpu)) -nested_vmcb02_compute_g_pat (svm); +svm_set_pat (vcpu, msr->host_initiated, data);break ;case MSR_IA32_SPEC_CTRL : if (!msr->host_initiated &&arch/x86/kvm/svm/svm.h +0
-1@@ -868,7 +868,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;arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 4029
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -4025,6 +4025,9 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }break ;case MSR_IA32_CR_PAT : +if (!(efer_reserved_bits &EFER_SVME )) +pr_warn_once ("%s: MSR_IA32_CR_PAT should be handled by AMD vendor-specific code\n" , __func__);
If we want to add a sanity check, simply do
WARN_ON_ONCE(efer_reserved_bits & EFER_SVME)?
so that bugs fail loudly.
Hrm. Though I'm leaning towards saying we should keep the "common" behavior in x86.c. If VMX and TDX didn't need to add get() APIs, I would say move the common code to vmx/common.h, but that ends up being pretty ugly.
If we do that, the total diff for svm.c is quite nice. Duplicating the kvm_pat_valid() sucks, but it seems like the lesser of all evils :-/
arch/x86/kvm/svm/svm.c +34 -4
@@ -2767,6 +2767,21 @@ static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu, !msr_write_intercepted (vcpu, msr_info->index); } +static bool svm_pat_accesses_gpat (struct kvm_vcpu *vcpu,bool from_host) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +/* +* When nested NPT is enabled, L2 has a separate PAT from L1. Guest +* accesses to IA32_PAT while running L2 target L2's gPAT; +* host-initiated accesses always target L1's hPAT so that +* KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent of +* each other and can be ordered arbitrarily during save and restore. +*/ +WARN_ON_ONCE (from_host && vcpu->wants_to_run); +return !from_host &&is_guest_mode (vcpu) &&l2_has_separate_pat (svm); +} +static int svm_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -2883,6 +2898,12 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_AMD64_DE_CFG : msr_info->data = svm->msr_decfg;break ; +case MSR_IA32_CR_PAT : +if (svm_pat_accesses_gpat (vcpu, msr_info->host_initiated)) { + msr_info->data =to_svm (vcpu)->vmcb->save.g_pat; +break ; + } +return kvm_get_msr_common (vcpu, msr_info);default :return kvm_get_msr_common (vcpu, msr_info); }
@@ -2966,14 +2987,23 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)break ;case MSR_IA32_CR_PAT : +if (svm_pat_accesses_gpat (vcpu, msr->host_initiated)) { +if (kvm_pat_valid (data)) +return 1 ; + +vmcb_set_gpat (svm->vmcb, data); +break ; + } + ret =kvm_set_msr_common (vcpu, msr);if (ret)break ; - svm->vmcb01.ptr->save.g_pat = data; -if (is_guest_mode (vcpu)) -nested_vmcb02_compute_g_pat (svm); -vmcb_mark_dirty (svm->vmcb,VMCB_NPT ); +if (npt_enabled) { +vmcb_set_gpat (svm->vmcb01.ptr, data); +if (is_guest_mode (&svm->vcpu) && !l2_has_separate_pat (svm)) +vmcb_set_gpat (svm->vmcb, data); + }break ;case MSR_IA32_SPEC_CTRL : if (!msr->host_initiated &&
arch/x86/kvm/x86.c int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 4443
Quoted 10 lines
arch/x86/kvm/x86.c +3
-0+if (!kvm_pat_valid (data))return 1 ;@@ -4436,6 +4439,9 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ; }case MSR_IA32_CR_PAT : +if (!(efer_reserved_bits &EFER_SVME )) +pr_warn_once ("%s: MSR_IA32_CR_PAT should be handled by AMD vendor-specific code\n" , __func__);
And obviously the same here..
Quoted 7 lines
arch/x86/kvm/x86.c +1
-0+ msr_info->data = vcpu->arch.pat;break ;case MSR_MTRRcap:Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 6/9 KVM: x86: nSVM: Save gPAT to vmcb12.g_pat on VMEXIT
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 emulated VMEXIT.
When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and the vCPU is in guest mode with nested NPT enabled, save the vmcb02 g_pat field to the vmcb12 g_pat field on emulated VMEXIT.
--- arch/x86/kvm/svm/nested.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/nested.c +3 -0
@@ -1248,6 +1248,9 @@ static int nested_svm_vmexit_update_vmcb12(struct kvm_vcpu *vcpu) vmcb12->save.dr6 = svm->vcpu.arch.dr6; vmcb12->save.cpl = vmcb02->save.cpl; +if (l2_has_separate_pat (svm)) + vmcb12->save.g_pat = vmcb02->save.g_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.53.0.1018.g2bb0e51243-goog
PATCH v7 7/9 KVM: Documentation: document KVM_{GET,SET}_NESTED_STATE for SVM
In reply to Jim MattsonDocument the nested state constants and structures for SVM that were added by commit cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE").
--- Documentation/virt/kvm/api.rst | 11 +++++++++++ 1 file changed, 11 insertions(+)
Documentation/virt/kvm/api.rst +11 -0
@@ -4942,10 +4942,13 @@ Errors: #define KVM_STATE_NESTED_FORMAT_SVM 1 #define KVM_STATE_NESTED_VMX_VMCS_SIZE 0x1000 + #define KVM_STATE_NESTED_SVM_VMCB_SIZE 0x1000 #define KVM_STATE_NESTED_VMX_SMM_GUEST_MODE 0x00000001 #define KVM_STATE_NESTED_VMX_SMM_VMXON 0x00000002 + #define KVM_STATE_NESTED_GIF_SET 0x00000100 + #define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001 struct kvm_vmx_nested_state_hdr {
@@ -4960,11 +4963,19 @@ Errors: __u64 preemption_timer_deadline; }; + struct kvm_svm_nested_state_hdr { + __u64 vmcb_pa; + }; + struct kvm_vmx_nested_state_data { __u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE]; __u8 shadow_vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE]; }; + struct kvm_svm_nested_state_data { + __u8 vmcb12[KVM_STATE_NESTED_SVM_VMCB_SIZE]; + }; + This ioctl copies the vcpu's nested virtualization state from the kernel to userspace.
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v7 8/9 KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
In reply to Jim MattsonAdd a 'gpat' field to kvm_svm_nested_state_hdr to carry L2's guest PAT value across save and restore.
When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and the vCPU is in guest mode with nested NPT enabled, save vmcb02's g_pat into the header on KVM_GET_NESTED_STATE, and restore it on KVM_SET_NESTED_STATE.
Host-initiated accesses to IA32_PAT (via KVM_GET/SET_MSRS) always target L1's hPAT, so they cannot be used to save or restore gPAT. The separate header field ensures that KVM_GET/SET_MSRS and KVM_GET/SET_NESTED_STATE are independent and can be ordered arbitrarily during save and restore.
Note that struct kvm_svm_nested_state_hdr is included in a union padded to 120 bytes, so there is room to add the gpat field without changing any offsets.
--- Documentation/virt/kvm/api.rst | 1 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/nested.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+)
Documentation/virt/kvm/api.rst +1 -0
@@ -4965,6 +4965,7 @@ Errors: struct kvm_svm_nested_state_hdr { __u64 vmcb_pa; + __u64 gpat; }; struct kvm_vmx_nested_state_data {
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -533,6 +533,7 @@ struct kvm_svm_nested_state_data {struct kvm_svm_nested_state_hdr {__u64 vmcb_pa; +__u64 gpat; };/* for KVM_CAP_NESTED_STATE */
arch/x86/kvm/svm/nested.c +20 -0
@@ -1865,6 +1865,9 @@ 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.gpat =0 ; +if (l2_has_separate_pat (svm)) + kvm_state.hdr.svm.gpat = svm->vmcb->save.g_pat; kvm_state.size +=KVM_STATE_NESTED_SVM_VMCB_SIZE ; kvm_state.flags |=KVM_STATE_NESTED_GUEST_MODE ;
@@ -1918,6 +1921,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct vmcb_save_area_cached save_cached;struct vmcb_ctrl_area_cached ctl_cached;unsigned long cr0; +bool use_separate_l2_pat;int ret; BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >
@@ -1993,6 +1997,18 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free; +/* +* Validate gPAT when the shared PAT quirk is disabled (i.e. L2 +* has its own gPAT). This is done separately from the +* vmcb_save_area_cached validation above, because gPAT is L2 +* state, but the vmcb_save_area_cached is populated with L1 state. +*/ + use_separate_l2_pat = + (ctl_cached.misc_ctl &SVM_MISC_ENABLE_NP ) && + !kvm_check_has_quirk (vcpu->kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT ); +if (use_separate_l2_pat && !kvm_pat_valid (kvm_state->hdr.svm.gpat)) + goto out_free;/* * All checks done, we can enter guest mode. Userspace provides
@@ -2017,6 +2033,10 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,nested_copy_vmcb_control_to_cache (svm, ctl);svm_switch_vmcb (svm, &svm->nested.vmcb02); + +if (use_separate_l2_pat) +vmcb_set_gpat (svm->vmcb, kvm_state->hdr.svm.gpat); +nested_vmcb02_prepare_control (svm);/*
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v7 8/9] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1924
Quoted 5 lines · Jim Mattson
arch/x86/kvm/svm/nested.c +1
-0@@ -1918,6 +1921,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct vmcb_save_area_cached save_cached;struct vmcb_ctrl_area_cached ctl_cached;unsigned long cr0; +bool use_separate_l2_pat;
Land this above "cr0" to preserve the inverted fir tree.
Quoted 7 lines
arch/x86/kvm/svm/nested.c +0
-0int ret; BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >@@ -1993,6 +1997,18 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free;
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 2009
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+/* +* Validate gPAT when the shared PAT quirk is disabled (i.e. L2 +* has its own gPAT). This is done separately from the +* vmcb_save_area_cached validation above, because gPAT is L2 +* state, but the vmcb_save_area_cached is populated with L1 state. +*/ + use_separate_l2_pat = + (ctl_cached.misc_ctl &SVM_MISC_ENABLE_NP ) && + !kvm_check_has_quirk (vcpu->kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );
I vote for either:
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);
or
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP); if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)) use_separate_l2_pat = false;
Quoted 19 lines
arch/x86/kvm/svm/nested.c +6
-0+if (use_separate_l2_pat && !kvm_pat_valid (kvm_state->hdr.svm.gpat)) + goto out_free;/* * All checks done, we can enter guest mode. Userspace provides @@ -2017,6 +2033,10 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,nested_copy_vmcb_control_to_cache (svm, ctl);svm_switch_vmcb (svm, &svm->nested.vmcb02); + +if (use_separate_l2_pat) +vmcb_set_gpat (svm->vmcb, kvm_state->hdr.svm.gpat); +nested_vmcb02_prepare_control (svm);/* Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Re: [PATCH v7 8/9] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
Quoted 30 lines · Sean Christopherson
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1924
Quoted 5 lines · Jim Mattson
arch/x86/kvm/svm/nested.c +1
-0@@ -1918,6 +1921,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct vmcb_save_area_cached save_cached;struct vmcb_ctrl_area_cached ctl_cached;unsigned long cr0; +bool use_separate_l2_pat;Land this above "cr0" to preserve the inverted fir tree.
Quoted 7 lines
arch/x86/kvm/svm/nested.c +0
-0int ret; BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >@@ -1993,6 +1997,18 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free;arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 2009
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+/* +* Validate gPAT when the shared PAT quirk is disabled (i.e. L2 +* has its own gPAT). This is done separately from the +* vmcb_save_area_cached validation above, because gPAT is L2 +* state, but the vmcb_save_area_cached is populated with L1 state. +*/ + use_separate_l2_pat = + (ctl_cached.misc_ctl &SVM_MISC_ENABLE_NP ) && + !kvm_check_has_quirk (vcpu->kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );I vote for either:
Quoted 4 lines
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP) &&
!kvm_check_has_quirk(vcpu->kvm,
KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);LOL! Aren't you the one who keeps complaining that my indentation doesn't line up? Are you schizophrenic?
Quoted 5 lines
or
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP); if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)) use_separate_l2_pat = false;
Wow. I really have no idea how to predict what you're going to want the code to look like. How is this better than the original?!?
Quoted 20 lines
Quoted 20 lines
arch/x86/kvm/svm/nested.c +6
-1+if (use_separate_l2_pat && !kvm_pat_valid (kvm_state->hdr.svm.gpat)) + goto out_free;/* * All checks done, we can enter guest mode. Userspace provides @@ -2017,6 +2033,10 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,nested_copy_vmcb_control_to_cache (svm, ctl);svm_switch_vmcb (svm, &svm->nested.vmcb02); + +if (use_separate_l2_pat) +vmcb_set_gpat (svm->vmcb, kvm_state->hdr.svm.gpat); +nested_vmcb02_prepare_control (svm);/* -- 2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v7 8/9] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
Quoted 31 lines · Jim Mattson
Quoted 30 lines · Sean Christopherson
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1924
Quoted 5 lines · Jim Mattson
arch/x86/kvm/svm/nested.c +1
-0@@ -1918,6 +1921,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct vmcb_save_area_cached save_cached;struct vmcb_ctrl_area_cached ctl_cached;unsigned long cr0; +bool use_separate_l2_pat;Land this above "cr0" to preserve the inverted fir tree.
Quoted 7 lines
arch/x86/kvm/svm/nested.c +0
-0int ret; BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >@@ -1993,6 +1997,18 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free;arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 2009
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+/* +* Validate gPAT when the shared PAT quirk is disabled (i.e. L2 +* has its own gPAT). This is done separately from the +* vmcb_save_area_cached validation above, because gPAT is L2 +* state, but the vmcb_save_area_cached is populated with L1 state. +*/ + use_separate_l2_pat = + (ctl_cached.misc_ctl &SVM_MISC_ENABLE_NP ) && + !kvm_check_has_quirk (vcpu->kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );I vote for either:
Quoted 6 lines
Quoted 4 lines
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);LOL! Aren't you the one who keeps complaining that my indentation doesn't line up? Are you schizophrenic?
Huh? That is aligned. Perhaps it's whitespace damaged by your MUA?
Quoted 8 lines
Quoted 5 lines
or
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP); if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)) use_separate_l2_pat = false;Wow. I really have no idea how to predict what you're going to want the code to look like. How is this better than the original?!?
It doesn't immediately wrap after the "=". Similar to my view on wrapping before function names[*], I find wrapping immediately after an assignment operator to be unnecessarily difficult to read as it doesn't provide any context for single-line searches.
I'm pretty darn consistent in my dislike for that style: I count 26 instances in arch/x86/kvm that match "\s=\n", and only two of those carry my SoB or R-b. I simply missed the wrap in kvm_vcpu_apicv_activated() that was added by commit 896046474f8d ("KVM: x86: Introduce kvm_x86_call() to simplify static calls of kvm_x86_ops"), and I'll give myself a pass for commit 8764ed55c970 ("KVM: x86:
Whitelist port 0x7e for pre-incrementing %rip") as that predates treating checkpatch's 80 char limit as a soft limit.
[*] https://lore.kernel.org/all/CAHk-=wjoLAYG446ZNHfg=GhjSY6nFmuB_wA8fYd5iLBNXjo9Bw@mail.gmail.com
Re: [PATCH v7 8/9] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
Quoted 33 lines · Sean Christopherson
Quoted 31 lines · Jim Mattson
Quoted 30 lines · Sean Christopherson
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1924
Quoted 5 lines · Jim Mattson
arch/x86/kvm/svm/nested.c +1
-0@@ -1918,6 +1921,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,struct vmcb_save_area_cached save_cached;struct vmcb_ctrl_area_cached ctl_cached;unsigned long cr0; +bool use_separate_l2_pat;Land this above "cr0" to preserve the inverted fir tree.
Quoted 7 lines
arch/x86/kvm/svm/nested.c +0
-0int ret; BUILD_BUG_ON(sizeof(struct vmcb_control_area) + sizeof(struct vmcb_save_area) >@@ -1993,6 +1997,18 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu, !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free;arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 2009
Quoted 10 lines
arch/x86/kvm/svm/nested.c +10
-0+/* +* Validate gPAT when the shared PAT quirk is disabled (i.e. L2 +* has its own gPAT). This is done separately from the +* vmcb_save_area_cached validation above, because gPAT is L2 +* state, but the vmcb_save_area_cached is populated with L1 state. +*/ + use_separate_l2_pat = + (ctl_cached.misc_ctl &SVM_MISC_ENABLE_NP ) && + !kvm_check_has_quirk (vcpu->kvm, +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT );I vote for either:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP) && !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);LOL! Aren't you the one who keeps complaining that my indentation doesn't line up? Are you schizophrenic?
Huh? That is aligned. Perhaps it's whitespace damaged by your MUA?
Indeed. It was.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
or
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP); if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)) use_separate_l2_pat = false;
Quoted 7 lines
Quoted 2 lines
Wow. I really have no idea how to predict what you're going to want the code to look like. How is this better than the original?!?
It doesn't immediately wrap after the "=". Similar to my view on wrapping before function names[*], I find wrapping immediately after an assignment operator to be unnecessarily difficult to read as it doesn't provide any context for single-line searches.
That's actually a good argument to *never* wrap a line. If a line is broken at all, the interesting context might follow the line break.
Quoted 7 lines
I'm pretty darn consistent in my dislike for that style: I count 26 instances in
arch/x86/kvmthat match "\s=\n", and only two of those carry my SoB or R-b. I simply missed the wrap inkvm_vcpu_apicv_activated()that was added by commit896046474f8d("KVM: x86: Introducekvm_x86_call()to simplify static calls ofkvm_x86_ops"), and I'll give myself a pass for commit8764ed55c970("KVM: x86:
Whitelist port0x7efor pre-incrementing %rip") as that predates treating checkpatch's 80 char limit as a soft limit.
Might I suggest that you should provide a tool—something like checkpatch.pl—that flags style violations?
Sean Christopherson
maintainerRe: [PATCH v7 8/9] KVM: x86: nSVM: Save/restore gPAT with KVM_{GET,SET}_NESTED_STATE
Quoted 8 lines · Jim Mattson
Quoted 7 lines · Sean Christopherson
Quoted 6 lines
Quoted 3 lines
use_separate_l2_pat = (ctl_cached.misc_ctl & SVM_MISC_ENABLE_NP); if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT)) use_separate_l2_pat = false;Wow. I really have no idea how to predict what you're going to want the code to look like. How is this better than the original?!?
Quoted 7 lines
Quoted 4 lines
It doesn't immediately wrap after the "=". Similar to my view on wrapping before function names[*], I find wrapping immediately after an assignment operator to be unnecessarily difficult to read as it doesn't provide any context for single-line searches.
That's actually a good argument to *never* wrap a line. If a line is broken at all, the interesting context might follow the line break.
Don't let perfect be the enemy of good. :-)
Quoted 10 lines
Quoted 7 lines
I'm pretty darn consistent in my dislike for that style: I count 26 instances in
arch/x86/kvmthat match "\s=\n", and only two of those carry my SoB or R-b. I simply missed the wrap inkvm_vcpu_apicv_activated()that was added by commit896046474f8d("KVM: x86: Introducekvm_x86_call()to simplify static calls ofkvm_x86_ops"), and I'll give myself a pass for commit8764ed55c970("KVM: x86:
Whitelist port0x7efor pre-incrementing %rip") as that predates treating checkpatch's 80 char limit as a soft limit.Might I suggest that you should provide a tool—something like checkpatch.pl—that flags style violations?
Or maybe extend checkpatch with an optional "feature"? Or subsystem-specific rules?
PATCH v7 9/9 KVM: selftests: nSVM: Add svm_nested_pat test
In reply to Jim MattsonWhen KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled, verify that KVM correctly virtualizes the host PAT MSR and the guest PAT register for nested SVM guests.
With nested NPT disabled:
* L1 and L2 share the same PAT
* The vmcb12.g_pat is ignored
With nested NPT enabled:
* An invalid g_pat in vmcb12 causes VMEXIT_INVALID
* RDMSR(IA32_PAT) from L2 returns the value of the guest PAT register
* WRMSR(IA32_PAT) from L2 is reflected in vmcb12's g_pat on VMEXIT
* RDMSR(IA32_PAT) from L1 returns the value of the host PAT MSR
* Save/restore with the vCPU in guest mode preserves both hPAT and gPAT
--- tools/arch/x86/include/uapi/asm/kvm.h | 2 + tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/svm_nested_pat_test.c | 304 ++++++++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
tools/arch/x86/include/uapi/asm/kvm.h +2 -0
@@ -476,6 +476,7 @@ struct kvm_sync_regs {#define KVM_X86_QUIRK_SLOT_ZAP_ALL (1 << 7)#define KVM_X86_QUIRK_STUFF_FEATURE_MSRS (1 << 8)#define KVM_X86_QUIRK_IGNORE_GUEST_PAT (1 << 9) +#define KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT (1 << 11)#define KVM_STATE_NESTED_FORMAT_VMX 0#define KVM_STATE_NESTED_FORMAT_SVM 1
@@ -530,6 +531,7 @@ struct kvm_svm_nested_state_data {struct kvm_svm_nested_state_hdr {__u64 vmcb_pa; +__u64 gpat; };/* for KVM_CAP_NESTED_STATE */
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -113,6 +113,7 @@ TEST_GEN_PROGS_x86 += x86/svm_vmcall_test TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme TEST_GEN_PROGS_x86 += x86/svm_nested_invalid_vmcb12_gpa +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/svm_lbr_nested_state
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +304 -0
304 lines not on this page · raw message
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v7 9/9] KVM: selftests: nSVM: Add svm_nested_pat test
Quoted 15 lines · Jim Mattson
When
KVM_X86_QUIRK_NESTED_SVM_SHARED_PATis disabled, verify that KVM correctly virtualizes the host PAT MSR and the guest PAT register for nested SVM guests.With nested NPT disabled:
* L1 and L2 share the same PAT
* The vmcb12.g_pat is ignoredWith nested NPT enabled:
* An invalidg_patin vmcb12 causesVMEXIT_INVALID
*RDMSR(IA32_PAT)from L2 returns the value of the guest PAT register
*WRMSR(IA32_PAT)from L2 is reflected in vmcb12'sg_paton VMEXIT
*RDMSR(IA32_PAT)from L1 returns the value of the host PAT MSR
* Save/restore with the vCPU in guest mode preserves both hPAT and gPAT
Quoted 3 lines
Signed-off-by Jim Mattson <[email protected]>--- tools/arch/x86/include/uapi/asm/kvm.h | 2 +
Don't update uAPI headers in tools/, they're not used by KVM selftests (perf folks will sync them as needed).
Quoted 8 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +8
-0+#define PAT_DEFAULT 0x0007040600070406ULL +#define L1_PAT_VALUE 0x0007040600070404ULL/* Change PA0 to WT */ +#define L2_VMCB12_PAT 0x0606060606060606ULL/* All WB */ +#define L2_PAT_MODIFIED 0x0606060606060604ULL/* Change PA0 to WT */ +#define INVALID_PAT_VALUE 0x0808080808080808ULL/* 8 is reserved */ + +/* +* Shared state between L1 and L2 for verification.
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 49
Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10
-0+*/ +struct pat_test_data { +uint64_t l2_pat_read; +uint64_t l2_pat_after_write; +uint64_t l1_pat_after_vmexit; +uint64_t vmcb12_gpat_after_exit; +bool l2_done; +}; + +static struct pat_test_data *pat_data;
This is ridiculous. Whatever AI you're using is reinventing sync_global_to_guest() in a very obfuscated way. Drop the indirection along with the params and the full page allocation, and just sync the damn struct.
Actually, this is even dumber than that. The "data" is only ever accessed from within the guest; it's used to pass info between L1 and L2. Drop the struct entirely and just write global variables.
In general, please clean this test up before submitting v8. All of the L2 code is basically copy+paste of itself. This is the second vibe coded selftest (AFAIK)
you've posted, and it has many of the same flaws as the first one[*]. While I'm
not opposed to using fancy tools, and the bar is generally lower for selftests, the code still needs to be readable and maintainable. This ain't.
Quoted 8 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +8
-0+static void l2_guest_code (void ) +{ + pat_data->l2_pat_read =rdmsr (MSR_IA32_CR_PAT ); +wrmsr (MSR_IA32_CR_PAT ,L2_PAT_MODIFIED ); + pat_data->l2_pat_after_write =rdmsr (MSR_IA32_CR_PAT ); + pat_data->l2_done = true; +vmmcall (); +}
...
Quoted 13 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +13
-0+static void run_test (void *l1_code,const char *test_name,bool npt_enabled, +bool do_save_restore) +{ +struct pat_test_data *data_hva; +vm_vaddr_t svm_gva, data_gva; +struct kvm_x86_state *state; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; + +pr_info ("Testing: %s\n" , test_name); + + vm =vm_create_with_one_vcpu (&vcpu, l1_code);
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 247
Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10
-0+vm_enable_cap (vm,KVM_CAP_DISABLE_QUIRKS2 , +KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT ); +if (npt_enabled) +vm_enable_npt (vm); + +vcpu_alloc_svm (vm, &svm_gva); + + data_gva =vm_vaddr_alloc_page (vm); + data_hva =addr_gva2hva (vm, data_gva); +memset (data_hva,0 ,sizeof (*data_hva));
Ugh.
Quoted 5 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +5
-0+ +if (npt_enabled) +tdp_identity_map_default_memslots (vm); + +vcpu_args_set (vcpu,2 , svm_gva, data_gva);
Re: [PATCH v7 9/9] KVM: selftests: nSVM: Add svm_nested_pat test
Quoted 51 lines · Sean Christopherson
Quoted 15 lines · Jim Mattson
When
KVM_X86_QUIRK_NESTED_SVM_SHARED_PATis disabled, verify that KVM correctly virtualizes the host PAT MSR and the guest PAT register for nested SVM guests.With nested NPT disabled:
* L1 and L2 share the same PAT
* The vmcb12.g_pat is ignoredWith nested NPT enabled:
* An invalidg_patin vmcb12 causesVMEXIT_INVALID
*RDMSR(IA32_PAT)from L2 returns the value of the guest PAT register
*WRMSR(IA32_PAT)from L2 is reflected in vmcb12'sg_paton VMEXIT
*RDMSR(IA32_PAT)from L1 returns the value of the host PAT MSR
* Save/restore with the vCPU in guest mode preserves both hPAT and gPATQuoted 3 lines
Signed-off-by Jim Mattson <[email protected]>--- tools/arch/x86/include/uapi/asm/kvm.h | 2 +Don't update uAPI headers in
tools/, they're not used by KVM selftests (perf folks will sync them as needed).Quoted 8 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +8
-0+#define PAT_DEFAULT 0x0007040600070406ULL +#define L1_PAT_VALUE 0x0007040600070404ULL/* Change PA0 to WT */ +#define L2_VMCB12_PAT 0x0606060606060606ULL/* All WB */ +#define L2_PAT_MODIFIED 0x0606060606060604ULL/* Change PA0 to WT */ +#define INVALID_PAT_VALUE 0x0808080808080808ULL/* 8 is reserved */ + +/* +* Shared state between L1 and L2 for verification. tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 49
Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10
-0+*/ +struct pat_test_data { +uint64_t l2_pat_read; +uint64_t l2_pat_after_write; +uint64_t l1_pat_after_vmexit; +uint64_t vmcb12_gpat_after_exit; +bool l2_done; +}; + +static struct pat_test_data *pat_data;This is ridiculous. Whatever AI you're using is reinventing
sync_global_to_guest()in a very obfuscated way. Drop the indirection along with the params and the full page allocation, and just sync the damn struct.Actually, this is even dumber than that. The "data" is only ever accessed from within the guest; it's used to pass info between L1 and L2. Drop the struct entirely and just write global variables.
Quoted 7 lines
In general, please clean this test up before submitting v8. All of the L2 code is basically copy+paste of itself. This is the second vibe coded selftest (AFAIK)
you've posted, and it has many of the same flaws as the first one[*]. While I'm
not opposed to using fancy tools, and the bar is generally lower for selftests, the code still needs to be readable and maintainable. This ain't.
I contend that the kvm selftests infrastructure already fails to meet that bar, but I will toss this in the bit bucket and write a selftest by hand for the next version.