Refresh queued CET virtualization series
Subsystems (6)
cover Refresh queued CET virtualization series
The purpose of this patch series is to refresh the queued CET KVM patches[1] with the latest dependent CET native patches, pursuing the result that whole series could be merged ahead of CET native series[2] [3].
The patchset has been tested on Skylake(none-CET) and Sapphire Rapids (CET capable) platforms, didn't find breakages to KVM basic functions and KVM unit-tests/selftests.
----------------------------------------------------------------------
The motivations are:
1) Customers are interested in developing CET related applications, they especially desire to set up CET development environments in VM, but suffered by non-trivial native and KVM patch rebasing work. If this series could be merged early, it'll save them tons of energy.
2) The kernel and KVM have evolved significantly since the queued day, it’s necessary to fix up the KVM patches to make them adapted to the recent mainline code.
3) CET native patch series refactored a lot per maintainers’ review and some of the patches can be reused by KVM enabling patches.
4) PeterZ’s supervisor IBT patch series got merged in 5.18, it requires additional KVM patch to support it in guest kernel.
----------------------------------------------------------------------
Guest CET states in KVM:
CET user mode states(MSR_IA32_U_CET,MSR_IA32_PL3_SSP) counts on xsaves/xrstors and CET user bit of MSR_IA32_XSS to save/restor when thread/process context switch happens. In virtulization world, after vm-exit and before vcpu thread exits to user mode, the guest user mode states are swapped to guest fpu area and host user mode states are loaded, vice-versa on vm-entry. See details in kvm_load_guest_fpu() and kvm_put_guest_fpu(). With this design, guest CET xsave-supported states retain while the vcpu thread keeps in ring-0 vmx root mode, the instantaneous guest states are not expected to impact host side.
Moveover, VMCS includes new fields for CET states, i.e.,GUEST_S_CET, GUEST_SSP, GUEST_INTR_SSP_TABLE for guest and HOST_S_CET, HOST_SSP, HOST_INTR_SSP_TABLE for host, when loading guest/host state bits set in VMCS, the guest/host MSRs are swapped at vm-exit/entry, therefore these guest/host CET states are strictly isolated. All CET supervisor states map to one of the fields. With the new fields, current guest supervisor IBT enalbing doesn't depend on xsaves/xrstors and CET supervisor bit of MSR_IA32_XSS.
---------------------------------------------------------------------
Impact to existing kernel/KVM:
To minimize the impact to exiting kernel/KVM code, most of KVM patch code can be bypassed during runtime.Uncheck "CONFIG_X86_KERNEL_IBT" and "CONFIG_X86_SHADOW_STACK" in Kconfig before kernel build to get rid of CET featrures in KVM. If both of them are not enabled, KVM clears related feature bits as well as CET user bit in supported_xss, this makes CET related checks stop at the first points. Since most of the patch code runs on the none-hot path of KVM, it's expected to introduce little impact to existing code.
On legacy platforms, CET feature is not available by nature, therefore the execution flow just like that on CET capable platform with features disabled at build time.
One known downside of early merge is thread fpu area size expands by 16 bytes due to enabling XFEATURE_MASK_CET_USER bit on CET capable platforms.
Although native SHSTK and IBT patch series are split off, but we don't want to do the same for KVM patches since supervisor IBT has been merged and customers desire full user mode features in guest.
We'd like to get your comments on the practice and patches, thanks!
Patch 1-5: Dependent CET native patches. Patch 6-7: KVM XSS Supporting patches from kvm/queue. Patch 8-18: Enabling patches for CET user mode. Patch 19: Enabling patch for supervisor IBT.
Change logs:
1. Removed XFEATURE_MASK_CET_KERNEL, MSR_IA32_PL{0,1,2}_SSP and MSR_IA32_INT_SSP_TAB related code since supervisor SHSTK design is still open.
2. Added support for guest kernel supervisor IBT.
3. Refactored some of previous helpers due to change 1) and 2).
4. Refactored control logic between XSS CET user bit and user mode SHSTK/IBT, make supervisor IBT support independent to XSS user bit.
5. Rebased the patch series onto kvm/queue:
8baacf67c76c ("KVM: SEV-ES: reuse advance_sev_es_emulated_ins for OUT too")
[1]: https://git.kernel.org/pub/scm/virt/kvm/kvm.git/log/?h=intel
[2]: SHSTK: https://lore.kernel.org/all/[email protected]/
[3]: old IBT: https://lore.kernel.org/all/[email protected]/
Rick Edgecombe (1):
x86/fpu: Add helper for modifying xstate
Sean Christopherson (2):
KVM: x86: Report XSS as an MSR to be saved if there are supported
features
KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
Yang Weijiang (12):
KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
KVM: x86: Add #CP support in guest exception classification.
KVM: VMX: Introduce CET VMCS fields and flags
KVM: x86: Add fault checks for CR4.CET
KVM: VMX: Emulate reads and writes to CET MSRs
KVM: VMX: Add a synthetic MSR to allow userspace VMM to access
GUEST_SSP
KVM: x86: Report CET MSRs as to-be-saved if CET is supported
KVM: x86: Save/Restore GUEST_SSP to/from SMM state save area
KVM: x86: Enable CET virtualization for VMX and advertise CET to
userspace
KVM: VMX: Pass through CET MSRs to the guest when supported
KVM: nVMX: Enable CET support for nested VMX
KVM: x86: Enable supervisor IBT support for guest
Yu-cheng Yu (4):
x86/cet/shstk: Add Kconfig option for Shadow Stack x86/cpufeatures: Add CPU feature flags for shadow stacks x86/cpufeatures: Enable CET CR4 bit for shadow stack x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states
arch/x86/Kconfig | 17 +++ arch/x86/Kconfig.assembler | 1 + arch/x86/include/asm/cpu.h | 2 +- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/fpu/api.h | 7 +- arch/x86/include/asm/fpu/types.h | 14 ++- arch/x86/include/asm/fpu/xstate.h | 6 +- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/asm/vmx.h | 8 ++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/include/uapi/asm/kvm_para.h | 1 + arch/x86/kernel/cpu/common.c | 14 +-- arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/fpu/core.c | 19 ++++ arch/x86/kernel/fpu/xstate.c | 93 ++++++++-------- arch/x86/kernel/machine_kexec_64.c | 2 +- arch/x86/kvm/cpuid.c | 21 +++- arch/x86/kvm/cpuid.h | 5 + arch/x86/kvm/emulate.c | 11 ++ arch/x86/kvm/vmx/capabilities.h | 4 + arch/x86/kvm/vmx/nested.c | 19 +++- arch/x86/kvm/vmx/vmcs12.c | 6 + arch/x86/kvm/vmx/vmcs12.h | 14 ++- arch/x86/kvm/vmx/vmx.c | 134 ++++++++++++++++++++++- arch/x86/kvm/x86.c | 95 ++++++++++++++-- arch/x86/kvm/x86.h | 47 +++++++- 27 files changed, 468 insertions(+), 86 deletions(-)
base-commit: 8baacf67c76c560fed954ac972b63e6e59a6fba0
Signature · 1 line
2.27.0
PATCH 1/19 x86/cet/shstk: Add Kconfig option for Shadow Stack
From: Yu-cheng Yu <[email protected]>
Shadow Stack provides protection against function return address corruption. It is active when the processor supports it, the kernel has CONFIG_X86_SHADOW_STACK enabled, and the application is built for the feature. This is only implemented for the 64-bit kernel. When it is enabled, legacy non-Shadow Stack applications continue to work, but without protection.
---
v2:
- Remove already wrong kernel size increase info (tlgx)
- Change prompt to remove "Intel" (tglx)
- Update line about what CPUs are supported (Dave)
Yu-cheng v25:
- Remove X86_CET and use X86_SHADOW_STACK directly.
Yu-cheng v24:
- Update for the splitting X86_CET to X86_SHADOW_STACK and X86_IBT.
arch/x86/Kconfig | 17 +++++++++++++++++ arch/x86/Kconfig.assembler | 1 + 2 files changed, 18 insertions(+)
arch/x86/Kconfig +17 -0
@@ -26,6 +26,7 @@ config X86_64 depends on 64BIT # Options that are inherently 64-bit kernel only: select ARCH_HAS_GIGANTIC_PAGE + select ARCH_HAS_SHADOW_STACK select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 select ARCH_USE_CMPXCHG_LOCKREF select HAVE_ARCH_SOFT_DIRTY
@@ -1969,6 +1970,22 @@ config X86_SGX If unsure, say N. +config ARCH_HAS_SHADOW_STACK + def_bool n + +config X86_SHADOW_STACK + prompt "X86 Shadow Stack" + def_bool n + depends on ARCH_HAS_SHADOW_STACK + help + Shadow Stack protection is a hardware feature that detects function + return address corruption. Today the kernel's support is limited to + virtualizing it in KVM guests. + + CPUs supporting shadow stacks were first released in 2020. + + If unsure, say N. + config EFI bool "EFI runtime service support" depends on ACPI
arch/x86/Kconfig.assembler +1 -0
@@ -19,3 +19,4 @@ config AS_TPAUSE def_bool $(as-instr,tpause %ecx) help Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7 +
Signature · 1 line
2.27.0
PATCH 2/19 x86/cpufeatures: Add CPU feature flags for shadow stacks
In reply to Yang WeijiangFrom: Yu-cheng Yu <[email protected]>
The Control-Flow Enforcement Technology contains two related features, one of which is Shadow Stacks. Future patches will utilize this feature for shadow stack support in KVM, so add a CPU feature flags for Shadow Stacks (CPUID.(EAX=7,ECX=0):ECX[bit 7]).
To protect shadow stack state from malicious modification, the registers are only accessible in supervisor mode. This implementation context-switches the registers with XSAVES. Make X86_FEATURE_SHSTK depend on XSAVES.
---
v2:
- Remove IBT reference in commit log (Kees)
- Describe xsaves dependency using text from (Dave)
v1:
- Remove IBT, can be added in a follow on IBT series.
Yu-cheng v25:
- Make X86_FEATURE_IBT depend on X86_FEATURE_SHSTK.
Yu-cheng v24:
- Update for splitting CONFIG_X86_CET to CONFIG_X86_SHADOW_STACK and CONFIG_X86_IBT.
- Move DISABLE_IBT definition to the IBT series.
arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +++++++- arch/x86/kernel/cpu/cpuid-deps.c | 1 + 3 files changed, 9 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -355,6 +355,7 @@#define X86_FEATURE_OSPKE (16*32+ 4)/* OS Protection Keys Enable */ #define X86_FEATURE_WAITPKG (16*32+ 5)/* UMONITOR/UMWAIT/TPAUSE Instructions */ #define X86_FEATURE_AVX512_VBMI2 (16*32+ 6)/* Additional AVX512 Vector Bit Manipulation Instructions */ +#define X86_FEATURE_SHSTK (16*32+ 7)/* Shadow Stack */ #define X86_FEATURE_GFNI (16*32+ 8)/* Galois Field New Instructions */ #define X86_FEATURE_VAES (16*32+ 9)/* Vector AES */ #define X86_FEATURE_VPCLMULQDQ (16*32+10)/* Carry-Less Multiplication Double Quadword */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -68,6 +68,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_X86_SHADOW_STACK +#define DISABLE_SHSTK 0 +#else +#define DISABLE_SHSTK (1 << (X86_FEATURE_SHSTK & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -88,7 +94,7 @@#define DISABLED_MASK14 0#define DISABLED_MASK15 0#define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ - DISABLE_ENQCMD) + DISABLE_ENQCMD|DISABLE_SHSTK)#define DISABLED_MASK17 0#define DISABLED_MASK18 0#define DISABLED_MASK19 0
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -78,6 +78,7 @@ static const struct cpuid_dep cpuid_deps[] = { { X86_FEATURE_XFD, X86_FEATURE_XSAVES }, { X86_FEATURE_XFD, X86_FEATURE_XGETBV1 }, { X86_FEATURE_AMX_TILE, X86_FEATURE_XFD }, + { X86_FEATURE_SHSTK, X86_FEATURE_XSAVES }, {} };
Signature · 1 line
2.27.0
PATCH 3/19 x86/cpufeatures: Enable CET CR4 bit for shadow stack
In reply to Yang WeijiangFrom: Yu-cheng Yu <[email protected]>
Utilizing CET features requires a CR4 bit to be enabled as well as bits to be set in CET MSRs. Setting the CR4 bit does two things:
1. Enables the usage of WRUSS instruction, which the kernel can use to write to userspace shadow stacks.
2. Allows those individual aspects of CET to be enabled later via the MSR.
While future patches will allow the MSR values to be saved and restored per task, the CR4 bit will allow for WRUSS to be used regardless of if a tasks CET MSRs have been restored.
Kernel IBT already enables the CR4 bit. Modify the logic to enable it for when the kernel is configured with and detects shadow stack support, as well.
Rename cet_disable() to ibt_disable() since it no longer applies to all CET features in the kernel.
---
v2:
- Drop no_user_shstk (Dave Hansen)
- Elaborate on what the CR4 bit does in the commit log
- Integrate with Kernel IBT logic
v1:
- Moved kernel-parameters.txt changes here from patch 1.
Yu-cheng v25:
- Remove software-defined X86_FEATURE_CET.
Yu-cheng v24:
- Update #ifdef placement to reflect Kconfig changes of splitting shadow stack and ibt.
arch/x86/include/asm/cpu.h | 2 +- arch/x86/kernel/cpu/common.c | 14 +++++++------- arch/x86/kernel/machine_kexec_64.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-)
arch/x86/include/asm/cpu.h +1 -1
@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);static inline void init_ia32_feat_ctl (struct cpuinfo_x86 *c) {}#endif -extern __noendbr void cet_disable(void); +extern __noendbr void ibt_disable(void);struct ucode_cpu_info ;
arch/x86/kernel/cpu/common.c +7 -7
@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save) static __always_inline void setup_cet(struct cpuinfo_x86 *c) { +bool kernel_ibt =HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT );u64 msr =CET_ENDBR_EN ; -if (!HAS_KERNEL_IBT || - !cpu_feature_enabled (X86_FEATURE_IBT )) -return ; +if (kernel_ibt) +wrmsrl (MSR_IA32_S_CET , msr); -wrmsrl (MSR_IA32_S_CET , msr); -cr4_set_bits (X86_CR4_CET ); +if (kernel_ibt ||cpu_feature_enabled (X86_FEATURE_SHSTK )) +cr4_set_bits (X86_CR4_CET ); -if (!ibt_selftest ()) { +if (kernel_ibt && !ibt_selftest ()) {pr_err ("IBT selftest: Failed!\n" );setup_clear_cpu_cap (X86_FEATURE_IBT );return ; } } -__noendbr void cet_disable(void) +__noendbr void ibt_disable(void) {if (cpu_feature_enabled (X86_FEATURE_IBT ))wrmsrl (MSR_IA32_S_CET ,0 );
arch/x86/kernel/machine_kexec_64.c +1 -1
@@ -311,7 +311,7 @@ void machine_kexec(struct kimage *image)/* Interrupts aren't acceptable while we reboot */ local_irq_disable ();hw_breakpoint_disable (); -cet_disable (); +ibt_disable ();if (image->preserve_context) {#ifdef CONFIG_X86_IO_APIC
Signature · 1 line
2.27.0
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
Quoted 16 lines · Yang Weijiang
arch/x86/include/asm/cpu.h +1
-1@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);static inline void init_ia32_feat_ctl (struct cpuinfo_x86 *c) {}#endif -extern __noendbr void cet_disable(void); +extern __noendbr void ibt_disable(void);struct ucode_cpu_info ;arch/x86/kernel/cpu/common.c +0
-0
arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 603
Quoted 7 lines
arch/x86/kernel/cpu/common.c +1
-1@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save) -__noendbr void cet_disable(void) +__noendbr void ibt_disable(void) {if (cpu_feature_enabled (X86_FEATURE_IBT ))wrmsrl (MSR_IA32_S_CET ,0 );
Not sure about this rename; it really disables all of (S) CET.
Specifically, once we do S-SHSTK (after FRED) we might also very much need to kill that for kexec.
Quoted 16 lines
arch/x86/kernel/machine_kexec_64.c +1
-1@@ -311,7 +311,7 @@ void machine_kexec(struct kimage *image)/* Interrupts aren't acceptable while we reboot */ local_irq_disable ();hw_breakpoint_disable (); -cet_disable (); +ibt_disable ();if (image->preserve_context) {#ifdef CONFIG_X86_IO_APIC Signature · 1 line
2.27.0
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
Quoted 27 lines · Peter Zijlstra
Quoted 17 lines · Yang Weijiang
arch/x86/include/asm/cpu.h +1
-1@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);static inline void init_ia32_feat_ctl (struct cpuinfo_x86 *c) {}#endif -extern __noendbr void cet_disable(void); +extern __noendbr void ibt_disable(void);struct ucode_cpu_info ;arch/x86/kernel/cpu/common.c +0
-0arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 602
Quoted 6 lines
arch/x86/kernel/cpu/common.c +1
-1@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save) -__noendbr void cet_disable(void) +__noendbr void ibt_disable(void) {if (cpu_feature_enabled (X86_FEATURE_IBT ))wrmsrl (MSR_IA32_S_CET ,0 );Not sure about this rename; it really disables all of (S) CET.
Quoted 2 lines
Specifically, once we do S-SHSTK (after FRED) we might also very much need to kill that for kexec.
Sure, what about something like sup_cet_disable()?
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
Quoted 28 lines · Rick P
Quoted 27 lines · Peter Zijlstra
Quoted 17 lines · Yang Weijiang
arch/x86/include/asm/cpu.h +1
-1@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);static inline void init_ia32_feat_ctl (struct cpuinfo_x86 *c) {}#endif -extern __noendbr void cet_disable(void); +extern __noendbr void ibt_disable(void);struct ucode_cpu_info ;arch/x86/kernel/cpu/common.c +0
-0arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 602
Quoted 6 lines
arch/x86/kernel/cpu/common.c +1
-1@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save) -__noendbr void cet_disable(void) +__noendbr void ibt_disable(void) {if (cpu_feature_enabled (X86_FEATURE_IBT ))wrmsrl (MSR_IA32_S_CET ,0 );Not sure about this rename; it really disables all of (S) CET.
Quoted 4 lines
Quoted 2 lines
Specifically, once we do S-SHSTK (after FRED) we might also very much need to kill that for kexec.
Sure, what about something like
sup_cet_disable()?
Why bother? Arguably kexec should clear U_CET too.
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
+kexec people
Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · Rick P
Quoted 29 lines · Peter Zijlstra
Quoted 19 lines · Yang Weijiang
arch/x86/include/asm/cpu.h +1
-1@@ -74,7 +74,7 @@ void init_ia32_feat_ctl(struct cpuinfo_x86 *c);static inline void init_ia32_feat_ctl (struct cpuinfo_x86 *c) {}#endif -extern __noendbr void cet_disable(void); +extern __noendbr void ibt_disable(void);struct ucode_cpu_info;arch/x86/kernel/cpu/common.c +0
-0arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 602
Quoted 6 lines
arch/x86/kernel/cpu/common.c +1
-1@@ -598,23 +598,23 @@ __noendbr void ibt_restore(u64 save) -__noendbr void cet_disable(void) +__noendbr void ibt_disable(void) {if (cpu_feature_enabled (X86_FEATURE_IBT ))wrmsrl (MSR_IA32_S_CET ,0 );Not sure about this rename; it really disables all of (S) CET.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Specifically, once we do S-SHSTK (after FRED) we might also very much
need to kill that for kexec.Sure, what about something like
sup_cet_disable()?Why bother? Arguably kexec should clear
U_CETtoo.
Hmm, I think you're right. It doesn't look the fpu stuff actually would reset unknown xfeatures to init. So kernels with Kernel IBT would set CR4.CET and then MSR_IA32_U_CET might make it to the point where userspace would run with CET enabled.
It seems like a general kexec problem for when the kernel enables new xfeatures. I suppose having vector instruction type data stick around is not going to show up the same way as having new enforcement rules applied.
But also, looking at this, the existing clearing of MSR_IA32_S_CET is not sufficient, since it only does it on the cpu doing the kexec. I think something like the below might be needed. Since per the other discussion we are going to need to start setting CR4.CET whenever the HW supports it, for KVM's benefit. So those other CPUs might get supervisor IBT enabled if we don't clear the msr from every CPU.
arch/x86/kernel/crash.c +0 -0
@@ -96,6 +96,12 @@ static void kdump_nmi_callback(int cpu, struct
pt_regs *regs)
cpu_emergency_stop_pt();
disable_local_APIC();+
+ /*
+ * Make sure to disable CET features before kexec so the new
kernel
+ * doesn't get surprised by the enforcement.
+ */
+ cet_disable();
}
void kdump_nmi_shootdown_cpus(void)
arch/x86/kernel/process.c +3 -0
@@ -787,6 +787,12 @@ void __noreturn stop_this_cpu(void *dummy)*/ if (cpuid_eax (0x8000001f ) &BIT (0 ))native_wbinvd (); + +/* +* Make sure to disable CET features before kexec so the new
kernel
+ * doesn't get surprised by the enforcement.
+ */
+ cet_disable();
for (;;) {
/*
* Use native_halt() so that memory contents don'tchange
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
In reply to Yang Weijiangarch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 608
Quoted 10 lines · Yang Weijiang
static
__always_inlinevoidsetup_cet(struct cpuinfo_x86 *c){arch/x86/kernel/cpu/common.c +5
-0+bool kernel_ibt =HAS_KERNEL_IBT &&cpu_feature_enabled (X86_FEATURE_IBT );u64 msr =CET_ENDBR_EN ; +if (kernel_ibt) +wrmsrl (MSR_IA32_S_CET , msr); +if (kernel_ibt ||cpu_feature_enabled (X86_FEATURE_SHSTK )) +cr4_set_bits (X86_CR4_CET );
Does flipping the CR4 and S_CET MSR write not result in simpler code?
arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 612
Quoted 4 lines
arch/x86/kernel/cpu/common.c +1
-0+if (kernel_ibt && !ibt_selftest ()) {pr_err ("IBT selftest: Failed!\n" );setup_clear_cpu_cap (X86_FEATURE_IBT );
Looking at this error path; I think I forgot to clear S_CET here.
Quoted 3 lines
(quoted diff) +0
-0return; } }
Re: [PATCH 03/19] x86/cpufeatures: Enable CET CR4 bit for shadow stack
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Yang Weijiang
static
__always_inlinevoidsetup_cet(struct cpuinfo_x86 *c){
+ boolkernel_ibt=HAS_KERNEL_IBT&&cpu_feature_enabled(X86_FEATURE_IBT); u64 msr = CET_ENDBR_EN;
Quoted 7 lines
arch/x86/kernel/cpu/common.c __noendbr void ibt_restore(u64 save) · line 608
Quoted 5 lines
arch/x86/kernel/cpu/common.c +4
-0+if (kernel_ibt) +wrmsrl (MSR_IA32_S_CET , msr); +if (kernel_ibt ||cpu_feature_enabled (X86_FEATURE_SHSTK )) +cr4_set_bits (X86_CR4_CET );Does flipping the CR4 and
S_CETMSR write not result in simpler code?
I thought it was more defensive to reset S_CET before turning it on with CR4. Of course CR4.CET could have been left on as well, but if CET features were actually fully turned on, then we probably wouldn't have gotten here. Seem reasonable?
Quoted 8 lines
Quoted 5 lines
+ if (kernel_ibt && !ibt_selftest()) { pr_err("IBT selftest: Failed!\n"); setup_clear_cpu_cap(X86_FEATURE_IBT);Looking at this error path; I think I forgot to clear
S_CEThere.
Yea. I can fix it in the next version of this if you want.
Quoted 3 lines
Quoted 3 lines
return; }}
PATCH 4/19 x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states
In reply to Yang WeijiangFrom: Yu-cheng Yu <[email protected]>
Shadow stack register state can be managed with XSAVE. The registers can logically be separated into two groups:
* Registers controlling user-mode operation
* Registers controlling kernel-mode operation
The architecture has two new XSAVE state components: one for each group of those groups of registers. This lets an OS manage them separately if it chooses. Future patches for host userspace and KVM guests will only utilize the user-mode registers, so only configure XSAVE to save user-mode registers. This state will add 16 bytes to the xsave buffer size.
Future patches will use the user-mode XSAVE area to save guest user-mode CET state. However, VMCS includes new fields for guest CET supervisor states. KVM can use these to save and restore guest supervisor state, so host supervisor XSAVE support is not required.
Adding this exacerbates the already unwieldy if statement in check_xstate_against_struct() that handles warning about un-implemented xfeatures. So refactor these check's by having XCHECK_SZ() set a bool when it actually check's the xfeature. This ends up exceeding 80 chars, but was better on balance than other options explored. Pass the bool as pointer to make it clear that XCHECK_SZ() can change the variable.
While configuring user-mode XSAVE, clarify kernel-mode registers are not managed by XSAVE by defining the xfeature in XFEATURE_MASK_SUPERVISOR_UNSUPPORTED, like is done for XFEATURE_MASK_PT. This serves more of a documentation as code purpose, and functionally, only enables a few safety checks.
Both XSAVE state components are supervisor states, even the state controlling user-mode operation. This is a departure from earlier features like protection keys where the PKRU state a normal user (non-supervisor) state. Having the user state be supervisor-managed ensures there is no direct, unprivileged access to it, making it harder for an attacker to subvert CET.
To facilitate this privileged access, define the two user-mode CET MSRs, and the bits defined in those MSRs relevant to future shadow stack enablement patches.
---
v2:
- Reword commit log using some verbiage posted by Dave Hansen
- Remove unlikely to be used supervisor cet xsave struct
- Clarify that supervisor cet state is not saved by xsave
- Remove unused supervisor MSRs
v1:
- Remove outdated reference to sigreturn checks on msr's.
Yu-cheng v29:
- Move CET MSR definition up in msr-index.h.
Yu-cheng v28:
- Add XFEATURE_MASK_CET_USER to XFEATURES_INIT_FPSTATE_HANDLED.
Yu-cheng v25:
- Update xsave_cpuid_features[]. Now CET XSAVES features depend on X86_FEATURE_SHSTK (vs. the software-defined X86_FEATURE_CET).
arch/x86/include/asm/fpu/types.h | 14 ++++- arch/x86/include/asm/fpu/xstate.h | 6 +- arch/x86/kernel/fpu/xstate.c | 93 ++++++++++++++++--------------- 3 files changed, 63 insertions(+), 50 deletions(-)
arch/x86/include/asm/fpu/types.h +12 -2
@@ -115,8 +115,8 @@ enum xfeature {XFEATURE_PT_UNIMPLEMENTED_SO_FAR ,XFEATURE_PKRU ,XFEATURE_PASID , -XFEATURE_RSRVD_COMP_11 , -XFEATURE_RSRVD_COMP_12 , +XFEATURE_CET_USER , +XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR ,XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 , XFEATURE_LBR,
@@ -138,6 +138,8 @@ enum xfeature {#define XFEATURE_MASK_PT (1 << XFEATURE_PT_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID) +#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) +#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR)#define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG)#define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA)
@@ -252,6 +254,14 @@ struct pkru_state {u32 pad; } __packed; +/* +* State component 11 is Control-flow Enforcement user states +*/ +struct cet_user_state { +u64 user_cet;/* user control-flow settings */ +u64 user_ssp;/* user shadow stack pointer */ +}; +/* * State component 15: Architectural LBR configuration state. * The size of Arch LBR state depends on the number of LBRs (lbr_depth).
arch/x86/include/asm/fpu/xstate.h +4 -2
@@ -50,7 +50,8 @@#define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA/* All currently supported supervisor features */ -#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID) +#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \ + XFEATURE_MASK_CET_USER)/* * A supervisor state component may not always contain valuable information,
@@ -77,7 +78,8 @@* Unsupported supervisor features. When a supervisor feature in this mask is * supported in the future, move it to the supported supervisor feature mask. */ -#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT) +#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT | \ + XFEATURE_MASK_CET_KERNEL)/* All supervisor states including supported and unsupported states. */ #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
arch/x86/kernel/fpu/xstate.c +47 -46
137 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH 04/19] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states
Quoted 16 lines · Yang Weijiang
arch/x86/include/asm/fpu/types.h +2
-2@@ -115,8 +115,8 @@ enum xfeature {XFEATURE_PT_UNIMPLEMENTED_SO_FAR ,XFEATURE_PKRU ,XFEATURE_PASID , -XFEATURE_RSRVD_COMP_11 , -XFEATURE_RSRVD_COMP_12 , +XFEATURE_CET_USER , +XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR ,XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 , XFEATURE_LBR,
arch/x86/include/asm/fpu/types.h enum xfeature { · line 145
Quoted 8 lines
arch/x86/include/asm/fpu/types.h +2
-0@@ -138,6 +138,8 @@ enum xfeature {#define XFEATURE_MASK_PT (1 << XFEATURE_PT_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID) +#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) +#define XFEATURE_MASK_CET_KERNEL (1 << XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR)#define XFEATURE_MASK_XTILE_CFG (1 << XFEATURE_XTILE_CFG)#define XFEATURE_MASK_XTILE_DATA (1 << XFEATURE_XTILE_DATA)
I'm not sure about that UNIMPLEMENTED_SO_FAR thing, that is, I'm thinking we *never* want XSAVE managed S_CET.
Re: [PATCH 04/19] x86/fpu/xstate: Introduce CET MSR and XSAVES supervisor states
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Yang Weijiang
arch/x86/include/asm/fpu/types.h +4
-2@@ -115,8 +115,8 @@ enum xfeature {XFEATURE_PT_UNIMPLEMENTED_SO_FAR ,XFEATURE_PKRU ,XFEATURE_PASID , -XFEATURE_RSRVD_COMP_11 , -XFEATURE_RSRVD_COMP_12 , +XFEATURE_CET_USER , +XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR ,XFEATURE_RSRVD_COMP_13 ,XFEATURE_RSRVD_COMP_14 , XFEATURE_LBR,@@ -138,6 +138,8 @@ enum xfeature {#define XFEATURE_MASK_PT (1 << XFEATURE_PT_UNIMPLEMENTED_SO_FAR)#define XFEATURE_MASK_PKRU (1 << XFEATURE_PKRU)#define XFEATURE_MASK_PASID (1 << XFEATURE_PASID) +#define XFEATURE_MASK_CET_USER (1 << XFEATURE_CET_USER) +#define XFEATURE_MASK_CET_KERNEL (1 <<
Quoted 8 lines
Quoted 5 lines
arch/x86/include/asm/fpu/types.h +0
-0XFEATURE_CET_KERNEL_UNIMPLEMENTED_SO_FAR)#define
XFEATURE_MASK_LBR(1 <<XFEATURE_LBR)
#defineXFEATURE_MASK_XTILE_CFG(1 <<XFEATURE_XTILE_CFG)
#defineXFEATURE_MASK_XTILE_DATA(1 <<XFEATURE_XTILE_DATA)I'm not sure about that
UNIMPLEMENTED_SO_FARthing, that is, I'm thinking we *never* want XSAVE managedS_CET.
Hmm, yes. I mostly was just keeping the pattern with XFEATURE_PT_UNIMPLEMENTED_SO_FAR.
How about XFEATURE_CET_KERNEL_UNUSED?
PATCH 5/19 x86/fpu: Add helper for modifying xstate
In reply to Yang WeijiangFrom: Rick Edgecombe <[email protected]>
Just like user xfeatures, supervisor xfeatures can be active in the registers or present in the task FPU buffer. If the registers are active, the registers can be modified directly. If the registers are not active, the modification must be performed on the task FPU buffer.
When the state is not active, the kernel could perform modifications directly to the buffer. But in order for it to do that, it needs to know where in the buffer the specific state it wants to modify is located. Doing this is not robust against optimizations that compact the FPU buffer, as each access would require computing where in the buffer it is.
The easiest way to modify supervisor xfeature data is to force restore the registers and write directly to the MSRs. Often times this is just fine anyway as the registers need to be restored before returning to userspace. Do this for now, leaving buffer writing optimizations for the future.
Add a new function fpregs_lock_and_load() that can simultaneously call fpregs_lock() and do this restore. Also perform some extra sanity checks in this function since this will be used in non-fpu focused code.
---
v2:
- Drop optimization of writing directly the buffer, and change API accordingly.
- fpregs_lock_and_load() suggested by tglx
- Some commit log verbiage from dhansen
v1:
- New patch.
arch/x86/include/asm/fpu/api.h | 7 ++++++- arch/x86/kernel/fpu/core.c | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/fpu/api.h +6 -1
@@ -82,6 +82,12 @@ static inline void fpregs_unlock(void)preempt_enable (); } +/* +* Lock and load the fpu state into the registers, if they are not already +* loaded. +*/ +void fpu_lock_and_load (void ); +#ifdef CONFIG_X86_DEBUG_FPU extern void fpregs_assert_state_consistent (void );#else
@@ -163,5 +169,4 @@ static inline bool fpstate_is_confidential(struct fpu_guest *gfpu)/* prctl */ extern long fpu_xstate_prctl (int option,unsigned long arg2); -#endif /* _ASM_X86_FPU_API_H */
arch/x86/kernel/fpu/core.c +19 -0
@@ -756,6 +756,25 @@ void switch_fpu_return(void) }EXPORT_SYMBOL_GPL (switch_fpu_return); +void fpu_lock_and_load (void ) +{ +/* +* fpregs_lock() only disables preemption (mostly). So modifing state +* in an interrupt could screw up some in progress fpregs operation, +* but appear to work. Warn about it. +*/ +WARN_ON_ONCE (!irq_fpu_usable ()); +WARN_ON_ONCE (current->flags &PF_KTHREAD ); + +fpregs_lock (); + +fpregs_assert_state_consistent (); + +if (test_thread_flag (TIF_NEED_FPU_LOAD )) +fpregs_restore_userregs (); +} +EXPORT_SYMBOL_GPL (fpu_lock_and_load); +#ifdef CONFIG_X86_DEBUG_FPU /* * If current FPU state according to its tracking (loaded FPU context on this
Signature · 1 line
2.27.0
PATCH 6/19 KVM: x86: Report XSS as an MSR to be saved if there are supported features
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
Add MSR_IA32_XSS to the list of MSRs reported to userspace if supported_xss is non-zero, i.e. KVM supports at least one XSS based feature.
Message-Id: <[email protected]>
--- arch/x86/kvm/x86.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kvm/x86.c +5 -0
@@ -1446,6 +1446,7 @@ static const u32 msrs_to_save_all[] = {MSR_F15H_PERF_CTR0 ,MSR_F15H_PERF_CTR1 ,MSR_F15H_PERF_CTR2 ,MSR_F15H_PERF_CTR3 ,MSR_F15H_PERF_CTR4 ,MSR_F15H_PERF_CTR5 ,MSR_IA32_XFD ,MSR_IA32_XFD_ERR , + MSR_IA32_XSS, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6780,6 +6781,10 @@ static void kvm_init_msr_list(void)if (!kvm_cpu_cap_has (X86_FEATURE_XFD ))continue ;break ; +case MSR_IA32_XSS : +if (!kvm_caps.supported_xss) +continue ; +break ;default :break ; }
Signature · 1 line
2.27.0
PATCH 7/19 KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
In reply to Yang WeijiangUpdated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time.
Message-Id: <[email protected]>
--- arch/x86/kvm/cpuid.c | 16 +++++++++++++--- arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-)
arch/x86/kvm/cpuid.c +13 -3
@@ -240,9 +240,19 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =cpuid_entry2_find (entries, nent,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) { +if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ; + } + } best =__kvm_find_kvm_cpuid_features (vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
arch/x86/kvm/x86.c +4 -2
@@ -3605,8 +3605,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)*/ if (data & ~kvm_caps.supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; -kvm_update_cpuid_runtime (vcpu); +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)
Signature · 1 line
2.27.0
PATCH 8/19 KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
If new feature MSRs are supported in XSS and passed through to the guest they are saved and restored by XSAVES/XRSTORS, i.e. in the guest's FPU state.
Load the guest's FPU state if userspace is accessing MSRs whose values are managed by XSAVES so that the MSR helper, e.g. kvm_{get,set}_xsave_msr(), can simply do {RD,WR}MSR to access the guest's value.
Because is also used for the KVM_GET_MSRS device ioctl(), explicitly check that @vcpu is non-null before attempting to load guest state. The XSS supporting MSRs cannot be retrieved via the device ioctl() without loading guest FPU state (which doesn't exist).
Note that guest_cpuid_has() is not queried as host userspace is allowed to access MSRs that have not been exposed to the guest, e.g. it might do KVM_SET_MSRS prior to KVM_SET_CPUID2.
--- arch/x86/kvm/x86.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +18 -1
@@ -130,6 +130,9 @@ static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu);static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2);static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); +static void kvm_load_guest_fpu (struct kvm_vcpu *vcpu); +static void kvm_put_guest_fpu (struct kvm_vcpu *vcpu); +struct kvm_x86_ops kvm_x86_ops__read_mostly ;#define KVM_X86_OP (func) \
@@ -4138,6 +4141,11 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) }EXPORT_SYMBOL_GPL (kvm_get_msr_common); +static bool is_xsaves_msr (u32 index) +{ +return index ==MSR_IA32_U_CET || index ==MSR_IA32_PL3_SSP ; +} +/* * Read or write a bunch of msrs. All parameters are kernel addresses. *
@@ -4148,11 +4156,20 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,int (*do_msr)(struct kvm_vcpu *vcpu, unsigned index, u64 *data)) { +bool fpu_loaded = false;int i; -for (i =0 ; i < msrs->nmsrs; ++i) +for (i =0 ; i < msrs->nmsrs; ++i) { +if (vcpu && !fpu_loaded && kvm_caps.supported_xss && +is_xsaves_msr (entries[i].index)) { +kvm_load_guest_fpu (vcpu); + fpu_loaded = true; + }if (do_msr (vcpu, entries[i].index, &entries[i].data))break ; + } +if (fpu_loaded) +kvm_put_guest_fpu (vcpu);return i; }
Signature · 1 line
2.27.0
PATCH 9/19 KVM: x86: Add #CP support in guest exception classification.
In reply to Yang WeijiangAdd handling for Control Protection (#CP) exceptions, vector 21, used and introduced by Intel's Control-Flow Enforcement Technology (CET). relevant CET violation case. See Intel's SDM for details.
Message-Id: <[email protected]>
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/x86.c | 10 +++++++--- arch/x86/kvm/x86.h | 13 ++++++++++--- 4 files changed, 19 insertions(+), 7 deletions(-)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -32,6 +32,7 @@#define MC_VECTOR 18#define XM_VECTOR 19#define VE_VECTOR 20 +#define CP_VECTOR 21/* Select x86 specific features in <linux/kvm.h> */ #define __KVM_HAVE_PIT
arch/x86/kvm/vmx/nested.c +1 -1
@@ -2819,7 +2819,7 @@ static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu,/* VM-entry interruption-info field: deliver error code */ should_have_error_code = intr_type ==INTR_TYPE_HARD_EXCEPTION && prot_mode && -x86_exception_has_error_code (vector); +x86_exception_has_error_code (vcpu, vector);if (CC (has_error_code != should_have_error_code))return -EINVAL ;
arch/x86/kvm/x86.c +7 -3
@@ -511,11 +511,15 @@ EXPORT_SYMBOL_GPL(kvm_spurious_fault);#define EXCPT_CONTRIBUTORY 1#define EXCPT_PF 2 -static int exception_class (int vector) +static int exception_class (struct kvm_vcpu *vcpu,int vector) {switch (vector) {case PF_VECTOR :return EXCPT_PF ; +case CP_VECTOR : +if (vcpu->arch.cr4_guest_rsvd_bits &X86_CR4_CET ) +return EXCPT_BENIGN ; +return EXCPT_CONTRIBUTORY ;case DE_VECTOR :case TS_VECTOR :case NP_VECTOR :
@@ -659,8 +663,8 @@ static void kvm_multiple_exception(struct kvm_vcpu *vcpu,kvm_make_request (KVM_REQ_TRIPLE_FAULT , vcpu);return ; } - class1 =exception_class (prev_nr); - class2 =exception_class (nr); + class1 =exception_class (vcpu, prev_nr); + class2 =exception_class (vcpu, nr);if ((class1 ==EXCPT_CONTRIBUTORY && class2 ==EXCPT_CONTRIBUTORY ) || (class1 ==EXCPT_PF && class2 !=EXCPT_BENIGN )) {/*
arch/x86/kvm/x86.h +10 -3
@@ -148,13 +148,20 @@ static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)return vcpu->arch.guest_state_protected ||is_64_bit_mode (vcpu); } -static inline bool x86_exception_has_error_code (unsigned int vector) +static inline bool x86_exception_has_error_code (struct kvm_vcpu *vcpu, +unsigned int vector) {static u32 exception_has_error_code =BIT (DF_VECTOR ) |BIT (TS_VECTOR ) |BIT (NP_VECTOR ) |BIT (SS_VECTOR ) |BIT (GP_VECTOR ) | -BIT (PF_VECTOR ) |BIT (AC_VECTOR ); +BIT (PF_VECTOR ) |BIT (AC_VECTOR ) |BIT (CP_VECTOR ); -return (1U << vector) & exception_has_error_code; +if (!((1U << vector) & exception_has_error_code)) +return false; + +if (vector ==CP_VECTOR ) +return !(vcpu->arch.cr4_guest_rsvd_bits &X86_CR4_CET ); + +return true; }static inline bool mmu_is_nested (struct kvm_vcpu *vcpu)
Signature · 1 line
2.27.0
PATCH 10/19 KVM: VMX: Introduce CET VMCS fields and flags
In reply to Yang WeijiangCET (Control-flow Enforcement Technology) is a CPU feature used to prevent Return/Jump-Oriented Programming (ROP/JOP) attacks. CET introduces a new exception type, Control Protection (#CP), and two sub-features to defend against ROP/JOP style control-flow subversion attacks:
Shadow Stack (SHSTK):
A shadow stack is a second stack used exclusively for control transfer operations. The shadow stack is separate from the data/normal stack and can be enabled individually in user and kernel mode. When shadow stacks are enabled, CALL pushes the return address on both the data and shadow stack. RET pops the return address from both stacks and compares them. If the return addresses from the two stacks do not match, the processor signals a #CP.
Indirect Branch Tracking (IBT):
IBT adds a new instrution, ENDBRANCH, that is used to mark valid target addresses of indirect branches (CALL, JMP, ENCLU[EEXIT], etc...). If an indirect branch is executed and the next instruction is _not_ an ENDBRANCH, the processor signals a #CP.
Several new CET MSRs are defined to support CET:
MSR_IA32_{U,S}_CET: Controls the CET settings for user mode and kernel
mode respectively.
MSR_IA32_PL{0,1,2,3}_SSP: Stores shadow stack pointers for CPL-0,1,2,3
protection respectively.
MSR_IA32_INT_SSP_TAB: Stores base address of shadow stack pointer table.
Two XSAVES state bits are introduced for CET:
IA32_XSS:[bit 11]: Control saving/restoring user mode CET states IA32_XSS:[bit 12]: Control saving/restoring kernel mode CET states.
Six VMCS fields are introduced for CET:
{HOST,GUEST}_S_CET: Stores CET settings for kernel mode. {HOST,GUEST}_SSP: Stores shadow stack pointer of current task/thread. {HOST,GUEST}_INTR_SSP_TABLE: Stores base address of shadow stack pointer table.
If VM_EXIT_LOAD_HOST_CET_STATE = 1, the host CET states are restored from the following VMCS fields at VM-Exit:
HOST_S_CET
HOST_SSP
HOST_INTR_SSP_TABLE
If VM_ENTRY_LOAD_GUEST_CET_STATE = 1, the guest CET states are loaded from the following VMCS fields at VM-Entry:
GUEST_S_CET
GUEST_SSP
GUEST_INTR_SSP_TABLE
Message-Id: <[email protected]>
--- arch/x86/include/asm/vmx.h | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/include/asm/vmx.h +8 -0
@@ -102,6 +102,7 @@#define VM_EXIT_CLEAR_BNDCFGS 0x00800000#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000 +#define VM_EXIT_LOAD_CET_STATE 0x10000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -115,6 +116,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_CET_STATE 0x00100000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
@@ -343,6 +345,9 @@ enum vmcs_field {GUEST_PENDING_DBG_EXCEPTIONS =0x00006822 ,GUEST_SYSENTER_ESP =0x00006824 ,GUEST_SYSENTER_EIP =0x00006826 , +GUEST_S_CET =0x00006828 , +GUEST_SSP =0x0000682a , +GUEST_INTR_SSP_TABLE =0x0000682c ,HOST_CR0 =0x00006c00 ,HOST_CR3 =0x00006c02 , HOST_CR4 = 0x00006c04,
@@ -355,6 +360,9 @@ enum vmcs_field {HOST_IA32_SYSENTER_EIP =0x00006c12 ,HOST_RSP =0x00006c14 ,HOST_RIP =0x00006c16 , +HOST_S_CET =0x00006c18 , +HOST_SSP =0x00006c1a , + HOST_INTR_SSP_TABLE = 0x00006c1c };/*
Signature · 1 line
2.27.0
PATCH 11/19 KVM: x86: Add fault checks for CR4.CET
In reply to Yang WeijiangAdd the fault checks for CR4.CET, which is the master control for all CET features (SHSTK and IBT). In addition to basic support checks, CET can be enabled if and only if CR0.WP==1, i.e. setting CR4.CET=1 faults if CR0.WP==0 and setting CR0.WP=0 fails if CR4.CET==1.
Message-Id: <[email protected]>
--- arch/x86/kvm/x86.c | 6 ++++++ arch/x86/kvm/x86.h | 3 +++ 2 files changed, 9 insertions(+)
arch/x86/kvm/x86.c +6 -0
@@ -952,6 +952,9 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) (is_64_bit_mode(vcpu) || kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)))return 1 ; +if (!(cr0 &X86_CR0_WP ) &&kvm_read_cr4_bits (vcpu,X86_CR4_CET )) +return 1 ; +static_call (kvm_x86_set_cr0)(vcpu, cr0);kvm_post_set_cr0 (vcpu, old_cr0, cr0);
@@ -1168,6 +1171,9 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } +if ((cr4 &X86_CR4_CET ) && !(kvm_read_cr0 (vcpu) &X86_CR0_WP )) +return 1 ; +static_call (kvm_x86_set_cr4)(vcpu, cr4);kvm_post_set_cr4 (vcpu, old_cr4, cr4);
arch/x86/kvm/x86.h +3 -0
@@ -477,6 +477,9 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type); __reserved_bits |=X86_CR4_VMXE ; \if (!__cpu_has (__c,X86_FEATURE_PCID )) \ __reserved_bits |=X86_CR4_PCIDE ; \ +if (!__cpu_has (__c,X86_FEATURE_SHSTK ) && \ + !__cpu_has (__c,X86_FEATURE_IBT )) \ + __reserved_bits |=X86_CR4_CET ; \ __reserved_bits; \ })
Signature · 1 line
2.27.0
PATCH 12/19 KVM: VMX: Emulate reads and writes to CET MSRs
In reply to Yang WeijiangAdd support for emulating read and write accesses to CET MSRs. CET MSRs are universally "special" as they are either context switched via dedicated VMCS fields or via XSAVES, i.e. no additional in-memory tracking is needed, but emulated reads/writes are more expensive.
--- arch/x86/kvm/vmx/vmx.c | 42 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+)
arch/x86/kvm/vmx/vmx.c +42 -0
@@ -1767,6 +1767,26 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr) } } +static bool cet_is_msr_accessible (struct kvm_vcpu *vcpu, +struct msr_data *msr) +{ +if (!kvm_cet_user_supported ()) +return false; + +if (msr->host_initiated) +return true; + +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) && + !guest_cpuid_has (vcpu,X86_FEATURE_IBT )) +return false; + +if (msr->index ==MSR_IA32_PL3_SSP && + !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; + +return true; +} +/* * Reads an msr value (of 'msr_info->index') into 'msr_info->data'. * Returns 0 on success, non-0 otherwise.
@@ -1906,6 +1926,12 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)else msr_info->data = vmx->pt_desc.guest.addr_a[index /2 ];break ; +case MSR_IA32_U_CET : +case MSR_IA32_PL3_SSP : +if (!cet_is_msr_accessible (vcpu, msr_info)) +return 1 ; +kvm_get_xsave_msr (msr_info); +break ;case MSR_IA32_DEBUGCTLMSR : msr_info->data =vmcs_read64 (GUEST_IA32_DEBUGCTL );break ;
@@ -2238,6 +2264,22 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)else vmx->pt_desc.guest.addr_a[index /2 ] = data;break ; +case MSR_IA32_U_CET : +if (!cet_is_msr_accessible (vcpu, msr_info)) +return 1 ; +if ((data &GENMASK (9 ,6 )) || +is_noncanonical_address (data, vcpu)) +return 1 ; +kvm_set_xsave_msr (msr_info); +break ; +case MSR_IA32_PL3_SSP : +if (!cet_is_msr_accessible (vcpu, msr_info)) +return 1 ; +if ((data &GENMASK (2 ,0 )) || +is_noncanonical_address (data, vcpu)) +return 1 ; +kvm_set_xsave_msr (msr_info); +break ;case MSR_IA32_PERF_CAPABILITIES :if (data && !vcpu_to_pmu (vcpu)->version)return 1 ;
arch/x86/kvm/x86.h +31 -0
@@ -2,6 +2,7 @@#ifndef ARCH_X86_KVM_X86_H #define ARCH_X86_KVM_X86_H +#include <asm/fpu/api.h> #include <linux/kvm_host.h> #include <asm/mce.h> #include <asm/pvclock.h>
@@ -323,6 +324,16 @@ static inline bool kvm_mpx_supported(void) == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); } +/* +* Guest CET user mode states depend on host XSAVES/XRSTORS to save/restore +* when vCPU enter/exit user space. If host doesn't support CET user bit in +* XSS msr, then treat this case as KVM doesn't support CET user mode. +*/ +static inline bool kvm_cet_user_supported (void ) +{ +return !!(kvm_caps.supported_xss &XFEATURE_MASK_CET_USER ); +} +extern unsigned int min_timer_period_us;extern bool enable_vmware_backdoor;
@@ -491,4 +502,24 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count, int in); +/* +* We've already loaded guest MSRs in __msr_io() when check the MSR index. +* In case vcpu has been preempted, we need to disable preemption, check +* and reload the guest fpu states before issue MSR read/write, +* fpu_lock_and_load() serves the purpose well. +*/ +static inline void kvm_get_xsave_msr (struct msr_data *msr_info) +{ +fpu_lock_and_load (); +rdmsrl (msr_info->index, msr_info->data); +fpregs_unlock (); +} + +static inline void kvm_set_xsave_msr (struct msr_data *msr_info) +{ +fpu_lock_and_load (); +wrmsrl (msr_info->index, msr_info->data); +fpregs_unlock (); +} +#endif
Signature · 1 line
2.27.0
PATCH 13/19 KVM: VMX: Add a synthetic MSR to allow userspace VMM to access GUEST_SSP
In reply to Yang WeijiangIntroduce a host-only synthetic MSR, MSR_KVM_GUEST_SSP so that the VMM can read/write the guest's SSP, e.g. to migrate CET state. Use a synthetic MSR, e.g. as opposed to a VCPU_REG_, as GUEST_SSP is subject to the same consistency checks as the PL*_SSP MSRs, i.e. can share code.
--- arch/x86/include/uapi/asm/kvm_para.h | 1 + arch/x86/kvm/vmx/vmx.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
arch/x86/include/uapi/asm/kvm_para.h +1 -0
@@ -58,6 +58,7 @@#define MSR_KVM_ASYNC_PF_INT 0x4b564d06#define MSR_KVM_ASYNC_PF_ACK 0x4b564d07#define MSR_KVM_MIGRATION_CONTROL 0x4b564d08 +#define MSR_KVM_GUEST_SSP 0x4b564d09struct kvm_steal_time {__u64 steal;
arch/x86/kvm/vmx/vmx.c +12 -3
@@ -1780,7 +1780,8 @@ static bool cet_is_msr_accessible(struct kvm_vcpu *vcpu, !guest_cpuid_has(vcpu, X86_FEATURE_IBT))return false; -if (msr->index ==MSR_IA32_PL3_SSP && +if ((msr->index ==MSR_IA32_PL3_SSP || + msr->index ==MSR_KVM_GUEST_SSP ) && !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ))return false;
@@ -1928,9 +1929,13 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_IA32_U_CET :case MSR_IA32_PL3_SSP : +case MSR_KVM_GUEST_SSP :if (!cet_is_msr_accessible (vcpu, msr_info))return 1 ; -kvm_get_xsave_msr (msr_info); +if (msr_info->index ==MSR_KVM_GUEST_SSP ) + msr_info->data =vmcs_readl (GUEST_SSP ); +else +kvm_get_xsave_msr (msr_info);break ;case MSR_IA32_DEBUGCTLMSR : msr_info->data =vmcs_read64 (GUEST_IA32_DEBUGCTL );
@@ -2273,12 +2278,16 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)kvm_set_xsave_msr (msr_info);break ;case MSR_IA32_PL3_SSP : +case MSR_KVM_GUEST_SSP :if (!cet_is_msr_accessible (vcpu, msr_info))return 1 ;if ((data &GENMASK (2 ,0 )) ||is_noncanonical_address (data, vcpu))return 1 ; -kvm_set_xsave_msr (msr_info); +if (msr_index ==MSR_KVM_GUEST_SSP ) +vmcs_writel (GUEST_SSP , data); +else +kvm_set_xsave_msr (msr_info);break ;case MSR_IA32_PERF_CAPABILITIES :if (data && !vcpu_to_pmu (vcpu)->version)
Signature · 1 line
2.27.0
PATCH 14/19 KVM: x86: Report CET MSRs as to-be-saved if CET is supported
In reply to Yang WeijiangReport all CET MSRs, including the synthetic GUEST_SSP MSR, as to-be-saved, e.g. for migration, if CET is supported by KVM.
--- arch/x86/kvm/x86.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/x86.c +7 -0
@@ -1460,6 +1460,7 @@ static const u32 msrs_to_save_all[] = {MSR_F15H_PERF_CTR3 ,MSR_F15H_PERF_CTR4 ,MSR_F15H_PERF_CTR5 ,MSR_IA32_XFD ,MSR_IA32_XFD_ERR ,MSR_IA32_XSS , + MSR_IA32_U_CET, MSR_IA32_PL3_SSP, MSR_KVM_GUEST_SSP, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6814,6 +6815,12 @@ static void kvm_init_msr_list(void)if (!kvm_caps.supported_xss)continue ;break ; +case MSR_KVM_GUEST_SSP : +case MSR_IA32_U_CET : +case MSR_IA32_PL3_SSP : +if (!kvm_cet_user_supported ()) +continue ; +break ;default :break ; }
Signature · 1 line
2.27.0
PATCH 15/19 KVM: x86: Save/Restore GUEST_SSP to/from SMM state save area
In reply to Yang WeijiangSave GUEST_SSP in the SMM state save area when guest exits to SMM due to SMI and restore it when guest exits SMM.
Message-Id: <[email protected]>
--- arch/x86/kvm/emulate.c | 11 +++++++++++ arch/x86/kvm/x86.c | 10 ++++++++++ 2 files changed, 21 insertions(+)
arch/x86/kvm/emulate.c +11 -0
@@ -2558,6 +2558,17 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, return r; } +if (kvm_cet_user_supported ()) { +struct msr_data msr; + + val =GET_SMSTATE (u64, smstate,0x7f08 ); + msr.index =MSR_KVM_GUEST_SSP ; + msr.host_initiated = true; + msr.data = val; +/* Mimic host_initiated access to bypass ssp access check. */ + kvm_x86_ops.set_msr (ctxt->vcpu, &msr); + } +return X86EMUL_CONTINUE ; }#endif
arch/x86/kvm/x86.c +10 -0
@@ -9833,6 +9833,16 @@ static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)for (i =0 ; i <6 ; i++)enter_smm_save_seg_64 (vcpu, buf, i); + +if (kvm_cet_user_supported ()) { +struct msr_data msr; + + msr.index =MSR_KVM_GUEST_SSP ; + msr.host_initiated = true; +/* GUEST_SSP is stored in VMCS at vm-exit. */ + kvm_x86_ops.get_msr (vcpu, &msr); +put_smstate (u64, buf,0x7f08 , msr.data); + } }#endif
Signature · 1 line
2.27.0
PATCH 16/19 KVM: x86: Enable CET virtualization for VMX and advertise CET to userspace
In reply to Yang WeijiangSet the feature bits so that CET capabilities can be seen in guest via CPUID enumeration. Add CR4.CET bit support in order to allow guest set CET master control bit(CR4.CET).
Disable KVM CET feature if unrestricted_guest is unsupported/disabled as KVM does not support emulating CET.
Don't expose CET feature if dependent CET bits are cleared in host XSS, or if XSAVES isn't supported. Updating the CET features in common x86 is a little ugly, but there is no clean solution without risking breakage of SVM if SVM hardware ever gains support for CET, e.g. moving everything to common x86 would prematurely expose CET on SVM. The alternative is to put all the logic in VMX, but that means rereading host_xss in VMX and duplicating the XSAVES check across VMX and SVM.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/cpuid.c | 5 +++-- arch/x86/kvm/vmx/capabilities.h | 4 ++++ arch/x86/kvm/vmx/vmx.c | 37 ++++++++++++++++++++++++++++----- arch/x86/kvm/x86.c | 21 ++++++++++++++++++- 5 files changed, 61 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -120,7 +120,8 @@ | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \ | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \ | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \ - | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP)) + | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP \ + | X86_CR4_CET))#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
arch/x86/kvm/cpuid.c +3 -2
@@ -581,7 +581,7 @@ void kvm_set_cpu_caps(void)F (AVX512_VPOPCNTDQ ) |F (UMIP ) |F (AVX512_VBMI2 ) |F (GFNI ) |F (VAES ) |F (VPCLMULQDQ ) |F (AVX512_VNNI ) |F (AVX512_BITALG ) |F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |0 /*WAITPKG*/ | -F (SGX_LC ) |F (BUS_LOCK_DETECT ) +F (SGX_LC ) |F (BUS_LOCK_DETECT ) |F (SHSTK ) );/* Set LA57 based on hardware capability. */ if (cpuid_ecx (7 ) &F (LA57 ))
@@ -599,7 +599,8 @@ void kvm_set_cpu_caps(void)F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) |F (MD_CLEAR ) |F (AVX512_VP2INTERSECT ) |F (FSRM ) |F (SERIALIZE ) |F (TSXLDTRK ) |F (AVX512_FP16 ) | -F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) +F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) | +F (IBT ) );/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
arch/x86/kvm/vmx/capabilities.h +4 -0
@@ -106,6 +106,10 @@ static inline bool cpu_has_load_perf_global_ctrl(void)return vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; } +static inline bool cpu_has_load_cet_ctrl (void ) +{ +return (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_CET_STATE ); +}static inline bool cpu_has_vmx_mpx (void ) {return vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_BNDCFGS ;
arch/x86/kvm/vmx/vmx.c +32 -5
@@ -2516,6 +2516,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, { VM_ENTRY_LOAD_IA32_EFER, VM_EXIT_LOAD_IA32_EFER }, { VM_ENTRY_LOAD_BNDCFGS, VM_EXIT_CLEAR_BNDCFGS }, { VM_ENTRY_LOAD_IA32_RTIT_CTL, VM_EXIT_CLEAR_IA32_RTIT_CTL }, + { VM_ENTRY_LOAD_CET_STATE, VM_EXIT_LOAD_CET_STATE }, };memset (vmcs_conf,0 ,sizeof (*vmcs_conf));
@@ -2636,7 +2637,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,VM_EXIT_LOAD_IA32_EFER |VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_PT_CONCEAL_PIP | -VM_EXIT_CLEAR_IA32_RTIT_CTL ; +VM_EXIT_CLEAR_IA32_RTIT_CTL | +VM_EXIT_LOAD_CET_STATE ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_EXIT_CTLS , &_vmexit_control) <0 )return -EIO ;
@@ -2660,7 +2662,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,VM_ENTRY_LOAD_IA32_EFER |VM_ENTRY_LOAD_BNDCFGS |VM_ENTRY_PT_CONCEAL_PIP | -VM_ENTRY_LOAD_IA32_RTIT_CTL ; +VM_ENTRY_LOAD_IA32_RTIT_CTL | +VM_ENTRY_LOAD_CET_STATE ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_ENTRY_CTLS , &_vmentry_control) <0 )return -EIO ;
@@ -2705,7 +2708,6 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, } } -rdmsr (MSR_IA32_VMX_BASIC , vmx_msr_low, vmx_msr_high);/* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
@@ -6159,6 +6161,12 @@ void dump_vmcs(struct kvm_vcpu *vcpu)if (vmcs_read32 (VM_EXIT_MSR_STORE_COUNT ) >0 )vmx_dump_msrs ("guest autostore" , &vmx->msr_autostore.guest); +if (vmentry_ctl &VM_ENTRY_LOAD_CET_STATE ) { +pr_err ("S_CET = 0x%016lx\n" ,vmcs_readl (GUEST_S_CET )); +pr_err ("SSP = 0x%016lx\n" ,vmcs_readl (GUEST_SSP )); +pr_err ("SSP TABLE = 0x%016lx\n" , +vmcs_readl (GUEST_INTR_SSP_TABLE )); + }pr_err ("*** Host State ***\n" );pr_err ("RIP = 0x%016lx RSP = 0x%016lx\n" ,vmcs_readl (HOST_RIP ),vmcs_readl (HOST_RSP ));
@@ -6236,6 +6244,12 @@ void dump_vmcs(struct kvm_vcpu *vcpu)if (secondary_exec_control &SECONDARY_EXEC_ENABLE_VPID )pr_err ("Virtual processor ID = 0x%04x\n" ,vmcs_read16 (VIRTUAL_PROCESSOR_ID )); +if (vmexit_ctl &VM_EXIT_LOAD_CET_STATE ) { +pr_err ("S_CET = 0x%016lx\n" ,vmcs_readl (HOST_S_CET )); +pr_err ("SSP = 0x%016lx\n" ,vmcs_readl (HOST_SSP )); +pr_err ("SSP TABLE = 0x%016lx\n" , +vmcs_readl (HOST_INTR_SSP_TABLE )); + } }/*
@@ -7679,9 +7693,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - kvm_caps.supported_xss =0 ; -if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + kvm_caps.supported_xss =0 ; + }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {
@@ -7691,6 +7706,18 @@ static __init void vmx_set_cpu_caps(void)if (cpu_has_vmx_waitpkg ())kvm_cpu_cap_check_and_set (X86_FEATURE_WAITPKG ); + +if (!cpu_has_load_cet_ctrl () || !enable_unrestricted_guest) { +kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); +kvm_cpu_cap_clear (X86_FEATURE_IBT ); + kvm_caps.supported_xss &= ~XFEATURE_MASK_CET_USER ; + } + +#ifndef CONFIG_X86_SHADOW_STACK +if (boot_cpu_has (X86_FEATURE_SHSTK )) +kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); +#endif + }static void vmx_request_immediate_exit (struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c +20 -1
@@ -217,6 +217,8 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) +#define KVM_SUPPORTED_XSS (XFEATURE_MASK_CET_USER) +u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);
@@ -11807,8 +11809,10 @@ int kvm_arch_hardware_setup(void *opaque)rdmsrl_safe (MSR_EFER , &host_efer); -if (boot_cpu_has (X86_FEATURE_XSAVES )) +if (boot_cpu_has (X86_FEATURE_XSAVES )) {rdmsrl (MSR_IA32_XSS , host_xss); + kvm_caps.supported_xss = host_xss &KVM_SUPPORTED_XSS ; + }kvm_init_pmu_capability ();
@@ -11823,6 +11827,21 @@ int kvm_arch_hardware_setup(void *opaque)if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) kvm_caps.supported_xss =0 ; +/* Update CET features now as kvm_caps.supported_xss is finalized. */ +if (!kvm_cet_user_supported ()) { +kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); +kvm_cpu_cap_clear (X86_FEATURE_IBT ); + } + +/* +* If SHSTK and IBT are disabled either by user space or unselection in +* Kconfig,then the feature bits should have been removed from KVM caps +* by this point, clear CET user bit in kvm_caps.supported_xss too. +*/ +if (!kvm_cpu_cap_has (X86_FEATURE_SHSTK ) && + !kvm_cpu_cap_has (X86_FEATURE_IBT )) + kvm_caps.supported_xss &= ~XFEATURE_CET_USER ; +#define __kvm_cpu_cap_has (UNUSED_ , f) kvm_cpu_cap_has(f) cr4_reserved_bits =__cr4_reserved_bits (__kvm_cpu_cap_has,UNUSED_ );#undef __kvm_cpu_cap_has
Signature · 1 line
2.27.0
Re: [PATCH 16/19] KVM: x86: Enable CET virtualization for VMX and advertise CET to userspace
Quoted 7 lines · Yang Weijiang
Set the feature bits so that CET capabilities can be seen in guest via CPUID enumeration. Add CR4.CET bit support in order to allow guest set CET master control bit(CR4.CET).
Disable KVM CET feature if
unrestricted_guestis unsupported/disabled as KVM does not support emulating CET.
Quoted 7 lines
Don't expose CET feature if dependent CET bits are cleared in host XSS, or if XSAVES isn't supported. Updating the CET features in common x86 is a little ugly, but there is no clean solution without risking breakage of SVM if SVM hardware ever gains support for CET, e.g. moving everything to common x86 would prematurely expose CET on SVM. The alternative is to put all the logic in VMX, but that means rereading
host_xssin VMX and duplicating the XSAVES check across VMX and SVM.
Doesn't Zen3 already have SHSTK ?
Re: [PATCH 16/19] KVM: x86: Enable CET virtualization for VMX and advertise CET to userspace
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Yang Weijiang
Set the feature bits so that CET capabilities can be seen in guest via CPUID enumeration. Add CR4.CET bit support in order to allow guest set CET master control bit(CR4.CET).
Disable KVM CET feature if
unrestricted_guestis unsupported/disabled as KVM does not support emulating CET.
Quoted 8 lines
Quoted 7 lines
Don't expose CET feature if dependent CET bits are cleared in host XSS, or if XSAVES isn't supported. Updating the CET features in common x86 is a little ugly, but there is no clean solution without risking breakage of SVM if SVM hardware ever gains support for CET, e.g. moving everything to common x86 would prematurely expose CET on SVM. The alternative is to put all the logic in VMX, but that means rereading
host_xssin VMX and duplicating the XSAVES check across VMX and SVM.Doesn't Zen3 already have SHSTK ?
Hmm, you remind me of reading more specs from AMD... I'll check their HW solution
if it's available.
Re: [PATCH 16/19] KVM: x86: Enable CET virtualization for VMX and advertise CET to userspace
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Quoted 7 lines · Yang Weijiang
Set the feature bits so that CET capabilities can be seen in guest via CPUID enumeration. Add CR4.CET bit support in order to allow guest set CET master control bit(CR4.CET).
Disable KVM CET feature if
unrestricted_guestis unsupported/disabled as KVM does not support emulating CET.
Quoted 8 lines
Quoted 7 lines
Don't expose CET feature if dependent CET bits are cleared in host XSS, or if XSAVES isn't supported. Updating the CET features in common x86 is a little ugly, but there is no clean solution without risking breakage of SVM if SVM hardware ever gains support for CET, e.g. moving everything to common x86 would prematurely expose CET on SVM. The alternative is to put all the logic in VMX, but that means rereading
host_xssin VMX and duplicating the XSAVES check across VMX and SVM.Doesn't Zen3 already have SHSTK ?
From what I read, AMD only supports SHSTK now, IBT is not available. Given
possible implementation difference and the enabling code in vmx is shared by
SHSTK and IBT, I'd like to keep guest CET enabling code specific to vmx now.
In the future, part of the code could be hoisted to x86 common to support both.
PATCH 17/19 KVM: VMX: Pass through CET MSRs to the guest when supported
In reply to Yang WeijiangPass through CET user mode MSRs when the associated CET component is enabled to improve guest performance. All CET MSRs are context switched, either via dedicated VMCS fields or XSAVES.
--- arch/x86/kvm/vmx/vmx.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
arch/x86/kvm/vmx/vmx.c +23 -0
@@ -583,6 +583,9 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8:/* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ return true; +case MSR_IA32_U_CET : +case MSR_IA32_PL3_SSP : +return true; } r =possible_passthrough_msr_slot (msr) != -ENOENT ;
@@ -7595,6 +7598,23 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i *4 )); } +static bool is_cet_state_supported (struct kvm_vcpu *vcpu,u32 xss_state) +{ +return (kvm_caps.supported_xss & xss_state) && + (guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) || +guest_cpuid_has (vcpu,X86_FEATURE_IBT )); +} + +static void vmx_update_intercept_for_cet_msr (struct kvm_vcpu *vcpu) +{ +bool incpt = !is_cet_state_supported (vcpu,XFEATURE_MASK_CET_USER ); + +vmx_set_intercept_for_msr (vcpu,MSR_IA32_U_CET ,MSR_TYPE_RW , incpt); + + incpt |= !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ); +vmx_set_intercept_for_msr (vcpu,MSR_IA32_PL3_SSP ,MSR_TYPE_RW , incpt); +} +static void vmx_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -7657,6 +7677,9 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)/* Refresh #PF interception to account for MAXPHYADDR changes. */ vmx_update_exception_bitmap (vcpu); + +if (kvm_cet_user_supported ()) +vmx_update_intercept_for_cet_msr (vcpu); }static __init void vmx_set_cpu_caps (void )
Signature · 1 line
2.27.0
PATCH 18/19 KVM: nVMX: Enable CET support for nested VMX
In reply to Yang WeijiangAdd vmcs12 fields for all CET fields, pass-through CET MSRs to L2 when possible, and enumerate the VMCS controls and CR4 bit as supported.
--- arch/x86/kvm/vmx/nested.c | 14 ++++++++++++-- arch/x86/kvm/vmx/vmcs12.c | 6 ++++++ arch/x86/kvm/vmx/vmcs12.h | 14 +++++++++++++- arch/x86/kvm/vmx/vmx.c | 2 ++ 4 files changed, 33 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +12 -2
@@ -684,6 +684,13 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0,MSR_IA32_PRED_CMD ,MSR_TYPE_W ); +/* Pass CET MSRs to nested VM if L0 and L1 are set to pass-through. */ +nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, +MSR_IA32_U_CET ,MSR_TYPE_RW ); + +nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, +MSR_IA32_PL3_SSP ,MSR_TYPE_RW ); +kvm_vcpu_unmap (vcpu, &vmx->nested.msr_bitmap_map, false); vmx->nested.force_msr_bitmap_recalc = false;
@@ -6593,7 +6600,9 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)VM_EXIT_HOST_ADDR_SPACE_SIZE | #endifVM_EXIT_LOAD_IA32_PAT |VM_EXIT_SAVE_IA32_PAT | -VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; +VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_LOAD_CET_STATE ; + msrs->exit_ctls_high |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
@@ -6613,7 +6622,8 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)VM_ENTRY_IA32E_MODE | #endifVM_ENTRY_LOAD_IA32_PAT |VM_ENTRY_LOAD_BNDCFGS | -VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; +VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |VM_ENTRY_LOAD_CET_STATE ; + msrs->entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |VM_ENTRY_LOAD_IA32_EFER );
arch/x86/kvm/vmx/vmcs12.c +6 -0
@@ -138,6 +138,9 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (GUEST_PENDING_DBG_EXCEPTIONS , guest_pending_dbg_exceptions),FIELD (GUEST_SYSENTER_ESP , guest_sysenter_esp),FIELD (GUEST_SYSENTER_EIP , guest_sysenter_eip), +FIELD (GUEST_S_CET , guest_s_cet), +FIELD (GUEST_SSP , guest_ssp), +FIELD (GUEST_INTR_SSP_TABLE , guest_ssp_tbl),FIELD (HOST_CR0 , host_cr0),FIELD (HOST_CR3 , host_cr3), FIELD(HOST_CR4, host_cr4),
@@ -150,5 +153,8 @@ const unsigned short vmcs12_field_offsets[] = {FIELD (HOST_IA32_SYSENTER_EIP , host_ia32_sysenter_eip),FIELD (HOST_RSP , host_rsp),FIELD (HOST_RIP , host_rip), +FIELD (HOST_S_CET , host_s_cet), +FIELD (HOST_SSP , host_ssp), + FIELD(HOST_INTR_SSP_TABLE, host_ssp_tbl), };const unsigned int nr_vmcs12_fields =ARRAY_SIZE (vmcs12_field_offsets);
arch/x86/kvm/vmx/vmcs12.h +13 -1
@@ -117,7 +117,13 @@ struct __packed vmcs12 {natural_width host_ia32_sysenter_eip;natural_width host_rsp;natural_width host_rip; -natural_width paddingl[8 ];/* room for future expansion */ +natural_width host_s_cet; +natural_width host_ssp; +natural_width host_ssp_tbl; +natural_width guest_s_cet; +natural_width guest_ssp; +natural_width guest_ssp_tbl; +natural_width paddingl[2 ];/* room for future expansion */ u32 pin_based_vm_exec_control;u32 cpu_based_vm_exec_control;u32 exception_bitmap;
@@ -293,6 +299,12 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (host_ia32_sysenter_eip,656 );CHECK_OFFSET (host_rsp,664 );CHECK_OFFSET (host_rip,672 ); +CHECK_OFFSET (host_s_cet,680 ); +CHECK_OFFSET (host_ssp,688 ); +CHECK_OFFSET (host_ssp_tbl,696 ); +CHECK_OFFSET (guest_s_cet,704 ); +CHECK_OFFSET (guest_ssp,712 ); +CHECK_OFFSET (guest_ssp_tbl,720 );CHECK_OFFSET (pin_based_vm_exec_control,744 );CHECK_OFFSET (cpu_based_vm_exec_control,748 );CHECK_OFFSET (exception_bitmap,752 );
arch/x86/kvm/vmx/vmx.c +2 -0
@@ -7508,6 +7508,8 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)cr4_fixed1_update (X86_CR4_PKE , ecx,feature_bit (PKU ));cr4_fixed1_update (X86_CR4_UMIP , ecx,feature_bit (UMIP ));cr4_fixed1_update (X86_CR4_LA57 , ecx,feature_bit (LA57 )); +cr4_fixed1_update (X86_CR4_CET , ecx,feature_bit (SHSTK )); +cr4_fixed1_update (X86_CR4_CET , edx,feature_bit (IBT ));#undef cr4_fixed1_update }
Signature · 1 line
2.27.0
PATCH 19/19 KVM: x86: Enable supervisor IBT support for guest
In reply to Yang WeijiangMainline kernel now supports supervisor IBT for kernel code, to make s-IBT work in guest(nested guest), pass through MSR_IA32_S_CET to guest(nested guest) if host kernel and KVM enabled IBT. Note, s-IBT can work independent to host xsaves support because guest MSR_IA32_S_CET can be stored/loaded from specific VMCS field.
--- arch/x86/kvm/cpuid.h | 5 +++++ arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 27 ++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 13 ++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-)
arch/x86/kvm/cpuid.h +5 -0
@@ -230,4 +230,9 @@ static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,return vcpu->arch.pv_cpuid.features & (1u << kvm_feature); } +static __always_inline bool cet_kernel_ibt_supported(void) +{ +return HAS_KERNEL_IBT &&kvm_cpu_cap_has (X86_FEATURE_IBT ); +} +#endif
arch/x86/kvm/vmx/nested.c +3 -0
@@ -688,6 +688,9 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0,MSR_IA32_U_CET ,MSR_TYPE_RW ); +nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0, +MSR_IA32_S_CET ,MSR_TYPE_RW ); +nested_vmx_set_intercept_for_msr (vmx, msr_bitmap_l1, msr_bitmap_l0,MSR_IA32_PL3_SSP ,MSR_TYPE_RW );
arch/x86/kvm/vmx/vmx.c +24 -3
@@ -585,6 +585,7 @@ static bool is_valid_passthrough_msr(u32 msr)return true;case MSR_IA32_U_CET :case MSR_IA32_PL3_SSP : +case MSR_IA32_S_CET :return true; }
@@ -1773,7 +1774,8 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)static bool cet_is_msr_accessible (struct kvm_vcpu *vcpu,struct msr_data *msr) { -if (!kvm_cet_user_supported ()) +if (!kvm_cet_user_supported () && + !cet_kernel_ibt_supported ())return false;if (msr->host_initiated)
@@ -1783,6 +1785,10 @@ static bool cet_is_msr_accessible(struct kvm_vcpu *vcpu, !guest_cpuid_has(vcpu, X86_FEATURE_IBT))return false; +if (msr->index ==MSR_IA32_S_CET && +guest_cpuid_has (vcpu,X86_FEATURE_IBT )) +return true; +if ((msr->index ==MSR_IA32_PL3_SSP || msr->index ==MSR_KVM_GUEST_SSP ) && !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ))
@@ -1933,10 +1939,13 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_U_CET :case MSR_IA32_PL3_SSP :case MSR_KVM_GUEST_SSP : +case MSR_IA32_S_CET :if (!cet_is_msr_accessible (vcpu, msr_info))return 1 ;if (msr_info->index ==MSR_KVM_GUEST_SSP ) msr_info->data =vmcs_readl (GUEST_SSP ); +else if (msr_info->index ==MSR_IA32_S_CET ) + msr_info->data =vmcs_readl (GUEST_S_CET );else kvm_get_xsave_msr (msr_info);break ;
@@ -2273,12 +2282,16 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vmx->pt_desc.guest.addr_a[index /2 ] = data;break ;case MSR_IA32_U_CET : +case MSR_IA32_S_CET :if (!cet_is_msr_accessible (vcpu, msr_info))return 1 ;if ((data &GENMASK (9 ,6 )) ||is_noncanonical_address (data, vcpu))return 1 ; -kvm_set_xsave_msr (msr_info); +if (msr_index ==MSR_IA32_S_CET ) +vmcs_writel (GUEST_S_CET , data); +else +kvm_set_xsave_msr (msr_info);break ;case MSR_IA32_PL3_SSP :case MSR_KVM_GUEST_SSP :
@@ -7615,6 +7628,9 @@ static void vmx_update_intercept_for_cet_msr(struct kvm_vcpu *vcpu) incpt |= !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK );vmx_set_intercept_for_msr (vcpu,MSR_IA32_PL3_SSP ,MSR_TYPE_RW , incpt); + + incpt |= !guest_cpuid_has (vcpu,X86_FEATURE_IBT ); +vmx_set_intercept_for_msr (vcpu,MSR_IA32_S_CET ,MSR_TYPE_RW , incpt); }static void vmx_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu)
@@ -7680,7 +7696,7 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)/* Refresh #PF interception to account for MAXPHYADDR changes. */ vmx_update_exception_bitmap (vcpu); -if (kvm_cet_user_supported ()) +if (kvm_cet_user_supported () ||cet_kernel_ibt_supported ())vmx_update_intercept_for_cet_msr (vcpu); }
@@ -7743,6 +7759,11 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_clear (X86_FEATURE_SHSTK );#endif +#ifndef CONFIG_X86_KERNEL_IBT +if (boot_cpu_has (X86_FEATURE_IBT )) +kvm_cpu_cap_clear (X86_FEATURE_IBT ); +#endif + }static void vmx_request_immediate_exit (struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c +12 -1
@@ -1463,6 +1463,7 @@ static const u32 msrs_to_save_all[] = {MSR_IA32_XFD ,MSR_IA32_XFD_ERR ,MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_PL3_SSP ,MSR_KVM_GUEST_SSP , + MSR_IA32_S_CET, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6823,6 +6824,10 @@ static void kvm_init_msr_list(void)if (!kvm_cet_user_supported ())continue ;break ; +case MSR_IA32_S_CET : +if (!cet_kernel_ibt_supported ()) +continue ; +break ;default :break ; }
@@ -11830,7 +11835,13 @@ int kvm_arch_hardware_setup(void *opaque)/* Update CET features now as kvm_caps.supported_xss is finalized. */ if (!kvm_cet_user_supported ()) {kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); -kvm_cpu_cap_clear (X86_FEATURE_IBT ); +/* If CET user bit is disabled due to cmdline option such as +* noxsaves, but kernel IBT is on, this means we can expose +* kernel IBT alone to guest since CET user mode msrs are not +* passed through to guest. +*/ +if (!cet_kernel_ibt_supported ()) +kvm_cpu_cap_clear (X86_FEATURE_IBT ); }/*
Signature · 1 line
2.27.0
Re: [PATCH 19/19] KVM: x86: Enable supervisor IBT support for guest
Quoted 20 lines · Yang Weijiang
Mainline kernel now supports supervisor IBT for kernel code, to make s-IBT work in guest(nested guest), pass through
MSR_IA32_S_CETto guest(nested guest) if host kernel and KVM enabled IBT. Note, s-IBT can work independent to host xsaves support because guestMSR_IA32_S_CETcan be stored/loaded from specific VMCS field.Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/cpuid.h | 5 +++++ arch/x86/kvm/vmx/nested.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 27 ++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 13 ++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-)arch/x86/kvm/cpuid.h +0
-0
arch/x86/kvm/cpuid.h static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu, · line 236
Quoted 7 lines
arch/x86/kvm/cpuid.h +4
-0@@ -230,4 +230,9 @@ static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu,return vcpu->arch.pv_cpuid.features & (1u << kvm_feature); } +static __always_inline bool cet_kernel_ibt_supported(void) +{ +return HAS_KERNEL_IBT &&kvm_cpu_cap_has (X86_FEATURE_IBT ); +}
As stated before; I would much rather it expose S_CET unconditional of host kernel config.
Re: [PATCH 19/19] KVM: x86: Enable supervisor IBT support for guest
In reply to Yang WeijiangQuoted 13 lines · Yang Weijiang
Mainline kernel now supports supervisor IBT for kernel code, to make s-IBT work in guest(nested guest), pass through
MSR_IA32_S_CETto guest(nested guest) if host kernel and KVM enabled IBT. Note, s-IBT can work independent to host xsaves support because guestMSR_IA32_S_CETcan be stored/loaded from specific VMCS field.arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static const u32 msrs_to_save_all[] = { · line 1467
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0@@ -1463,6 +1463,7 @@ static const u32 msrs_to_save_all[] = {MSR_IA32_XFD ,MSR_IA32_XFD_ERR ,MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_PL3_SSP ,MSR_KVM_GUEST_SSP , + MSR_IA32_S_CET, };
So much like my local kvm/qemu hacks; this patch suffers the problem of not exposing S_SHSTK. What happens if a guest tries to use that?
Should we intercept and reject setting those bits or complete this patch and support full S_SHSTK? (with all the warts and horrors that entails)
I don't think throwing this out in this half-finished state makes much sense (which is why I never much shared my hacks).
arch/x86/kvm/x86.c int kvm_arch_hardware_setup(void *opaque) · line 11842
Quoted 10 lines
arch/x86/kvm/x86.c +5
-1@@ -11830,7 +11835,13 @@ int kvm_arch_hardware_setup(void *opaque)/* Update CET features now as kvm_caps.supported_xss is finalized. */ if (!kvm_cet_user_supported ()) {kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); -kvm_cpu_cap_clear (X86_FEATURE_IBT ); +/* If CET user bit is disabled due to cmdline option such as +* noxsaves, but kernel IBT is on, this means we can expose +* kernel IBT alone to guest since CET user mode msrs are not +* passed through to guest. +*/
Invalid multi-line comment style.
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+if (!cet_kernel_ibt_supported ()) +kvm_cpu_cap_clear (X86_FEATURE_IBT );
Re: [PATCH 19/19] KVM: x86: Enable supervisor IBT support for guest
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Yang Weijiang
Mainline kernel now supports supervisor IBT for kernel code, to make s-IBT work in guest(nested guest), pass through
MSR_IA32_S_CETto guest(nested guest) if host kernel and KVM enabled IBT. Note, s-IBT can work independent to host xsaves support because guestMSR_IA32_S_CETcan be stored/loaded from specific VMCS field.
Quoted 13 lines
arch/x86/kvm/x86.c static const u32 msrs_to_save_all[] = { · line 1467
Quoted 10 lines
arch/x86/kvm/x86.c +1
-0@@ -1463,6 +1463,7 @@ static const u32 msrs_to_save_all[] = {MSR_IA32_XFD ,MSR_IA32_XFD_ERR ,MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_PL3_SSP ,MSR_KVM_GUEST_SSP , + MSR_IA32_S_CET, };So much like my local kvm/qemu hacks; this patch suffers the problem of not exposing
S_SHSTK. What happens if a guest tries to use that?
With current solution, I think guest kernel will hit #GP while reading/writing PL0_SSP.
Quoted 6 lines
Should we intercept and reject setting those bits or complete this patch and support full
S_SHSTK? (with all the warts and horrors that entails)I don't think throwing this out in this half-finished state makes much sense (which is why I never much shared my hacks).
You reminded me to think over these cases even I don't have a solution now,
thank you!
Quoted 13 lines
arch/x86/kvm/x86.c int kvm_arch_hardware_setup(void *opaque) · line 11842
Quoted 12 lines
arch/x86/kvm/x86.c +5
-1@@ -11830,7 +11835,13 @@ int kvm_arch_hardware_setup(void *opaque)/* Update CET features now as kvm_caps.supported_xss is finalized. */ if (!kvm_cet_user_supported ()) {kvm_cpu_cap_clear (X86_FEATURE_SHSTK ); -kvm_cpu_cap_clear (X86_FEATURE_IBT ); +/* If CET user bit is disabled due to cmdline option such as +* noxsaves, but kernel IBT is on, this means we can expose +* kernel IBT alone to guest since CET user mode msrs are not +* passed through to guest. +*/ Invalid multi-line comment style.
Oops, last minute change messed it up :-(
Quoted 3 lines
Quoted 3 lines
arch/x86/kvm/x86.c +2
-0+if (!cet_kernel_ibt_supported ()) +kvm_cpu_cap_clear (X86_FEATURE_IBT );
Quoted 4 lines · Yang Weijiang
The purpose of this patch series is to refresh the queued CET KVM patches[1] with the latest dependent CET native patches, pursuing the result that whole series could be merged ahead of CET native series[2] [3].
It might be helpful to explain what CET is here..
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Yang Weijiang
The purpose of this patch series is to refresh the queued CET KVM patches[1] with the latest dependent CET native patches, pursuing the result that whole series could be merged ahead of CET native series[2] [3].
It might be helpful to explain what CET is here..
Central European Time ofc :-)
I think it stands for Control-flow Enforcement Technology or something along those lines, but this being Intel it loves to obfuscate stuff and make it impossible to understand what's being said to increase the buzzword bong hits.
Its a mostly pointless umbrella term for IBT (Indirect Branch Tracking) and SHSTK (SHadow STacK), the first of which covers forward edge control flow and the second covers backward edge control flow.
Quoted 8 lines · Yang Weijiang
To minimize the impact to exiting kernel/KVM code, most of KVM patch code can be bypassed during runtime.Uncheck "
CONFIG_X86_KERNEL_IBT" and "CONFIG_X86_SHADOW_STACK" inKconfigbefore kernel build to get rid of CET featrures in KVM. If both of them are not enabled, KVM clears related feature bits as well as CET user bit insupported_xss, this makes CET related checks stop at the first points. Since most of the patch code runs on the none-hot path of KVM, it's expected to introduce little impact to existing code.
Do I understand this right in that a host without X86_KERNEL_IBT cannot run a guest with X86_KERNEL_IBT on? That seems unfortunate, since that was exactly what I did while developing the X86_KERNEL_IBT patches.
I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
Paolo Bonzini
maintainerQuoted 6 lines · Peter Zijlstra
Do I understand this right in that a host without
X86_KERNEL_IBTcannot run a guest withX86_KERNEL_IBTon? That seems unfortunate, since that was exactly what I did while developing theX86_KERNEL_IBTpatches.I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
For IBT in particular, I think all processor state is only loaded and stored at vmentry/vmexit (does not need XSAVES), so it should be feasible.
Paolo
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Peter Zijlstra
Do I understand this right in that a host without
X86_KERNEL_IBTcannot run a guest withX86_KERNEL_IBTon? That seems unfortunate, since that was exactly what I did while developing theX86_KERNEL_IBTpatches.I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
For IBT in particular, I think all processor state is only loaded and stored at vmentry/vmexit (does not need XSAVES), so it should be feasible.
That would be the S_CET stuff, yeah, that's VMCS managed. The U_CET stuff is all XSAVE though.
But funny thing, CPUID doesn't enumerate {U,S}_CET separately. It *does* enumerate IBT and SS separately, but for each IBT/SS you have to implement both U and S.
That was a problem with the first series, which only implemented support for U_CET while advertising IBT and SS (very much including S_CET), and still is a problem with this series because S_SS is missing while advertised.
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Peter Zijlstra
Do I understand this right in that a host without
X86_KERNEL_IBTcannot run a guest withX86_KERNEL_IBTon? That seems unfortunate, since that was exactly what I did while developing theX86_KERNEL_IBTpatches.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
For IBT in particular, I think all processor state is only loaded and stored at vmentry/vmexit (does not need XSAVES), so it should be feasible.
That would be the
S_CETstuff, yeah, that's VMCS managed. TheU_CETstuff is all XSAVE though.
Thank you Peter and Paolo!
In this version, I referenced host kernel settings when expose X86_KERNEL_IBT to guest.
The reason would be _IF_ host, for whatever reason, disabled the IBT feature, exposing the
feature blindly to guest could be risking, e.g., hitting some issues host wants to mitigate.
The actual implementation depends on the agreement we got :-)
Quoted 4 lines
But funny thing, CPUID doesn't enumerate {U,S}_CET separately. It *does* enumerate IBT and SS separately, but for each IBT/SS you have to implement both U and S.
Exactly, the CPUID enumeration could be a pain point for the KVM solution.
It makes {U,S}_CET feature control harder for guest.
Quoted 5 lines
That was a problem with the first series, which only implemented support for
U_CETwhile advertising IBT and SS (very much includingS_CET), and still is a problem with this series becauseS_SSis missing while advertised.
KVM has problem advertising S_SS alone to guest when U_CET(both SS and IBT) are
not available to guest. I would like to hear the voice from community on how to
make the features control straightforward and reasonable. Existing CPUID enumeration
cannot advertise {U, S}_SS and {U,S}_IBT well.
Quoted 1 line
Paolo Bonzini
maintainerQuoted 6 lines · Peter Zijlstra
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Peter Zijlstra
Do I understand this right in that a host without
X86_KERNEL_IBTcannot run a guest withX86_KERNEL_IBTon? That seems unfortunate, since that was exactly what I did while developing theX86_KERNEL_IBTpatches.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
For IBT in particular, I think all processor state is only loaded and stored at vmentry/vmexit (does not need XSAVES), so it should be feasible.
That would be the
S_CETstuff, yeah, that's VMCS managed. TheU_CETstuff is all XSAVE though.
What matters is whether XFEATURE_MASK_USER_SUPPORTED includes XFEATURE_CET_USER. If you build with !X86_KERNEL_IBT, KVM can still rely on the FPU state for U_CET state, and S_CET is saved/restored via the VMCS independent of X86_KERNEL_IBT.
Paolo
Quoted 8 lines
But funny thing, CPUID doesn't enumerate {U,S}_CET separately. It *does* enumerate IBT and SS separately, but for each IBT/SS you have to implement both U and S.
That was a problem with the first series, which only implemented support for
U_CETwhile advertising IBT and SS (very much includingS_CET), and still is a problem with this series becauseS_SSis missing while advertised.
Quoted 10 lines · Paolo Bonzini
Quoted 9 lines · Peter Zijlstra
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Peter Zijlstra
Do I understand this right in that a host without
X86_KERNEL_IBTcannot run a guest withX86_KERNEL_IBTon? That seems unfortunate, since that was exactly what I did while developing theX86_KERNEL_IBTpatches.I'm thinking that if the hardware supports it, KVM should expose it, irrespective of the host kernel using it.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
For IBT in particular, I think all processor state is only loaded and stored at vmentry/vmexit (does not need XSAVES), so it should be feasible.
That would be the
S_CETstuff, yeah, that's VMCS managed. TheU_CETstuff is all XSAVE though.What matters is whether
XFEATURE_MASK_USER_SUPPORTEDincludesXFEATURE_CET_USER.
Small correction, XFEATURE_CET_USER belongs to XFEATURE_MASK_SUPERVISOR_SUPPORTED, the name is misleading.
Quoted 1 line
If you build with !X86_KERNEL_IBT, KVM can still rely on the FPU state for
U_CETstate, andS_CETis saved/restored via the VMCS independent ofX86_KERNEL_IBT.
A fundamental question is, should KVM always honor host CET enablement before expose the feature to guest? i.e., check X86_KERNEL_IBT and X86_SHADOW_STACK.
Quoted 12 lines
Paolo
Quoted 8 lines
But funny thing, CPUID doesn't enumerate {U,S}_CET separately. It *does* enumerate IBT and SS separately, but for each IBT/SS you have to implement both U and S.
That was a problem with the first series, which only implemented support for
U_CETwhile advertising IBT and SS (very much includingS_CET), and still is a problem with this series becauseS_SSis missing while advertised.
Sean Christopherson
maintainerQuoted 9 lines · Weijiang
Quoted 3 lines · Paolo Bonzini
If you build with !X86_KERNEL_IBT, KVM can still rely on the FPU state for
U_CETstate, andS_CETis saved/restored via the VMCS independent ofX86_KERNEL_IBT.A fundamental question is, should KVM always honor host CET enablement before expose the feature to guest? i.e., check
X86_KERNEL_IBTandX86_SHADOW_STACK.
If there is a legitimate use case to NOT require host enablement and it's 100% safe to do so (within requiring hacks to the core kernel), then there's no hard requirement that says KVM can't virtualize a feature that's not used by the host.
It's definitely uncommon; unless I'm forgetting features, LA57 is the only feature that KVM fully virtualizes (as opposed to emulates in software) without requiring host enablement. Ah, and good ol' MPX, which is probably the best prior are since it shares the same XSAVE+VMCS for user+supervisor state management. So more than one, but still not very many.
But, requiring host "support" is the de facto standard largely because features tend to fall into one of three categories:
1. The feature is always available, i.e. doesn't have a software enable/disable flag.
2. The feature isn't explicitly disabled in cpufeatures / x86_capability even if it's not used by the host. E.g. MONITOR/MWAIT comes to mind where the host can be configured to not use MWAIT for idle, but it's still reported as supported (and for that case, KVM does have to explicitly guard against X86_BUG_MONITOR).
3. Require some amount of host support, e.g. exposing XSAVE without the kernel knowing how to save/restore all that state wouldn't end well.
In other words, virtualizing a feature if it's disabled in the host is allowed, but it's rare because there just aren't many features where doing so is possible _and_ necessary.
Quoted 11 lines · Sean Christopherson
Quoted 7 lines · Weijiang
Quoted 3 lines · Paolo Bonzini
If you build with !X86_KERNEL_IBT, KVM can still rely on the FPU state for
U_CETstate, andS_CETis saved/restored via the VMCS independent ofX86_KERNEL_IBT.A fundamental question is, should KVM always honor host CET enablement before expose the feature to guest? i.e., check
X86_KERNEL_IBTandX86_SHADOW_STACK.If there is a legitimate use case to NOT require host enablement and it's 100% safe to do so (within requiring hacks to the core kernel), then there's no hard requirement that says KVM can't virtualize a feature that's not used by the host.
Yeah, CET definitely can be virtualized without considering host usages, but to make things
easier, still back on some kind of host side support, e.g., xsaves.
Quoted 6 lines
It's definitely uncommon; unless I'm forgetting features, LA57 is the only feature that KVM fully virtualizes (as opposed to emulates in software) without requiring host enablement. Ah, and good ol' MPX, which is probably the best prior are since it shares the same XSAVE+VMCS for user+supervisor state management. So more than one, but still not very many.
Speaking of MPX, is it really active in recent kernel? I can find little piece of code at native side,
instead, more code in KVM.
Quoted 7 lines
But, requiring host "support" is the de facto standard largely because features tend to fall into one of three categories:
1. The feature is always available, i.e. doesn't have a software enable/disable flag.
Quoted 8 lines
2. The feature isn't explicitly disabled in cpufeatures /
x86_capabilityeven if it's not used by the host. E.g. MONITOR/MWAIT comes to mind where the host can be configured to not use MWAIT for idle, but it's still reported as supported (and for that case, KVM does have to explicitly guard againstX86_BUG_MONITOR).3. Require some amount of host support, e.g. exposing XSAVE without the kernel knowing how to save/restore all that state wouldn't end well.
CET may fall into one of the three or combination of them :-), depending on the complexity
of the implementation.
Quoted 4 lines
In other words, virtualizing a feature if it's disabled in the host is allowed, but it's rare because there just aren't many features where doing so is possible _and_ necessary.
I'm thinking of tweaking the patches to construct a safe yet flexible solution based on
a bunch of MSRs/CPUIDs/VMCS fields/XSAVES elements + a few host side constraints.
Thanks for the enlightenment!
Sean Christopherson
maintainerQuoted 10 lines · Weijiang
Quoted 5 lines · Sean Christopherson
It's definitely uncommon; unless I'm forgetting features, LA57 is the only feature that KVM fully virtualizes (as opposed to emulates in software) without requiring host enablement. Ah, and good ol' MPX, which is probably the best prior are since it shares the same XSAVE+VMCS for user+supervisor state management. So more than one, but still not very many.
Speaking of MPX, is it really active in recent kernel? I can find little piece of code at native side, instead, more code in KVM.
Nope, native MPX support was ripped out a year or two ago. The kernel provides just enough save+restore support so that KVM can continue to virtualize MPX.