Introduce support for guest CET feature
v8 cover Introduce support for guest CET feature
Control-flow Enforcement Technology (CET) provides protection against Return/Jump-Oriented Programming (ROP/JOP) attack. It includes two
sub-features: Shadow Stack (SHSTK) and Indirect Branch Tracking (IBT).
KVM change is required to support guest CET feature.
This patch serial implemented CET related CPUID/XSAVES enumeration, MSRs and vmentry/vmexit configuration etc.so that guest kernel can setup CET runtime infrastructure based on them. Some CET MSRs and related feature flags used reference the definitions in kernel patchset.
CET kernel patches is here:
https://lkml.org/lkml/2019/8/13/1110
https://lkml.org/lkml/2019/8/13/1109
v7 -> v8:
- Addressed Jim and Sean's feedback on: 1) CPUID(0xD,i) enumeration. 2) sanity check when configure guest CET. 3) function improvement.
- Added more sanity check functions.
- Set host vmexit default status so that guest won't leak CET status to host when vmexit.
- Added CR0.WP vs. CR4.CET mutual constrains.
v6 -> v7:
- Rebased patch to kernel v5.3
- Sean suggested to change CPUID(0xd, n) enumeration code as alined with existing one, and I think it's better to make the fix as an independent patch since XSS MSR are being used widely on X86 platforms.
- Check more host and guest status before configure guest CET
per Sean's feedback.
- Add error-check before guest accesses CET MSRs per Sean's feedback.
- Other minor fixes suggested by Sean.
v5 -> v6:
- Rebase patch to kernel v5.2.
- Move CPUID(0xD, n>=1) helper to a seperate patch.
- Merge xsave size fix with other patch.
- Other minor fixes per community feedback.
v4 -> v5:
- Rebase patch to kernel v5.1.
- Wrap CPUID(0xD, n>=1) code to a helper function.
- Pass through MSR_IA32_PL1_SSP and MSR_IA32_PL2_SSP to Guest.
- Add Co-developed-by expression in patch description.
- Refine patch description.
v3 -> v4:
- Add Sean's patch for loading Guest fpu state before access XSAVES managed CET MSRs.
- Melt down CET bits setting into CPUID configuration patch.
- Add VMX interface to query Host XSS.
- Check Host and Guest XSS support bits before set Guest XSS.
- Make Guest SHSTK and IBT feature enabling independent.
- Do not report CET support to Guest when Host CET feature is Disabled.
v2 -> v3:
- Modified patches to make Guest CET independent to Host enabling.
- Added patch 8 to add user space access for Guest CET MSR access.
- Modified code comments and patch description to reflect changes.
v1 -> v2:
- Re-ordered patch sequence, combined one patch.
- Added more description for CET related VMCS fields.
- Added Host CET capability check while enabling Guest CET loading bit.
- Added Host CET capability check while reporting Guest CPUID(EAX=7, EXC=0).
- Modified code in reporting Guest CPUID(EAX=D,ECX>=1), make it clearer.
- Added Host and Guest XSS mask check while setting bits for Guest XSS.
PATCH 1 : Fix CPUID(0xD, n) enumeration to support XSS MSR.
PATCH 2 : Define CET VMCS fields and bits.
PATCH 3 : Pass through CET MSRs to guest.
PATCH 4 : Load guest/host CET states on vmentry/vmexit.
PATCH 5 : Enable update to IA32_XSS for guest.
PATCH 6 : Load Guest FPU states for XSAVES managed MSRs.
PATCH 7 : Add user-space CET MSR access interface.
Sean Christopherson (1):
KVM: X86: Load guest fpu state when accessing MSRs managed by XSAVES
Yang Weijiang (6):
KVM: CPUID: Fix IA32_XSS support in CPUID(0xd,i) enumeration
KVM: VMX: Define CET VMCS fields and #CP flag
KVM: VMX: Pass through CET related MSRs
KVM: VMX: Load CET states on vmentry/vmexit
KVM: X86: Enable CET bits update in IA32_XSS
KVM: X86: Add user-space access interface for CET MSRs
arch/x86/include/asm/kvm_host.h | 5 +- arch/x86/include/asm/vmx.h | 8 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/cpuid.c | 121 +++++++++----- arch/x86/kvm/cpuid.h | 2 + arch/x86/kvm/svm.c | 7 + arch/x86/kvm/vmx/capabilities.h | 10 ++ arch/x86/kvm/vmx/vmx.c | 272 +++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 25 ++- arch/x86/kvm/x86.h | 10 +- 10 files changed, 415 insertions(+), 46 deletions(-)
Signature · 1 line
2.17.2
PATCH v8 1/7 KVM: CPUID: Fix IA32_XSS support in CPUID(0xd,i) enumeration
The control bits in IA32_XSS MSR are being used for new features, but current CPUID(0xd,i) enumeration code doesn't support them, so fix existing code first.
The supervisor states in IA32_XSS haven't been used in public KVM code, so set KVM_SUPPORTED_XSS to 0 now, anyone who's developing IA32_XSS related feature may expand the macro to add the CPUID support, otherwise, CPUID(0xd,i>1) always reports 0 of the subleaf to guest.
Extracted old code into a new filter and keep it same flavor as others.
This patch passed selftest on a few Intel platforms.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 105 ++++++++++++++++++++++---------- arch/x86/kvm/svm.c | 7 +++ arch/x86/kvm/vmx/vmx.c | 6 ++ arch/x86/kvm/x86.h | 7 +++ 5 files changed, 94 insertions(+), 32 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1209,6 +1209,7 @@ struct kvm_x86_ops {uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu);bool (*need_emulation_on_page_fault)(struct kvm_vcpu *vcpu); +u64 (*supported_xss)(void); };struct kvm_arch_async_pf {
arch/x86/kvm/cpuid.c +73 -32
140 lines not on this page · raw message
arch/x86/kvm/svm.c +7 -0
@@ -7193,6 +7193,11 @@ static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)return false; } +static u64 svm_supported_xss (void ) +{ +return 0 ; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -7329,6 +7334,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .nested_get_evmcs_version = NULL, .need_emulation_on_page_fault = svm_need_emulation_on_page_fault, + + .supported_xss = svm_supported_xss, };static int __init svm_init (void )
arch/x86/kvm/vmx/vmx.c +6 -0
@@ -1651,6 +1651,11 @@ static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,return !(val & ~valid_bits); } +static inline u64 vmx_supported_xss (void ) +{ +return host_xss; +} +static int vmx_get_msr_feature (struct kvm_msr_entry *msr) {switch (msr->index) {
@@ -7799,6 +7804,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .nested_enable_evmcs = NULL, .nested_get_evmcs_version = NULL, .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault, + .supported_xss = vmx_supported_xss, };static void vmx_cleanup_l1d_flush (void )
arch/x86/kvm/x86.h +7 -0
@@ -293,6 +293,13 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU) + +/* +* Right now, no XSS states are used on x86 platform, +* expand the macro for new features. +*/ +#define KVM_SUPPORTED_XSS 0 +extern u64 host_xcr0;extern u64 kvm_supported_xcr0 (void );
Signature · 1 line
2.17.2
PATCH v8 2/7 KVM: VMX: Define CET VMCS fields and #CP flag
In reply to Yang WeijiangCET(Control-flow Enforcement Technology) is an upcoming Intel(R) processor feature that blocks Return/Jump-Oriented Programming(ROP) attacks. It provides the following capabilities to defend against ROP/JOP style control-flow subversion attacks:
Shadow Stack (SHSTK):
A second stack for program which is used exclusively for control transfer operations.
Indirect Branch Tracking (IBT):
Code branching protection to defend against jump/call oriented programming.
Several new CET MSRs are defined in kernel to support CET:
MSR_IA32_{U,S}_CET: Controls the CET settings for user
mode and suervisor mode respectively.
MSR_IA32_PL{0,1,2,3}_SSP: Stores shadow stack pointers for
CPL-0,1,2,3 level 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]: For saving/restoring user mode CET states IA32_XSS:[bit 12]: For saving/restoring supervisor mode CET states.
Six VMCS fields are introduced for CET:
{HOST,GUEST}_S_CET: Stores CET settings for supervisor mode. {HOST,GUEST}_SSP: Stores shadow stack pointer for supervisor mode. {HOST,GUEST}_INTR_SSP_TABLE: Stores base address of shadow stack pointer
table.
If VM_EXIT_LOAD_HOST_CET_STATE = 1, the host's CET MSRs are restored from below VMCS fields at VM-Exit:
HOST_S_CET
HOST_SSP
HOST_INTR_SSP_TABLE
If VM_ENTRY_LOAD_GUEST_CET_STATE = 1, the guest's CET MSRs are loaded from below VMCS fields at VM-Entry:
GUEST_S_CET
GUEST_SSP
GUEST_INTR_SSP_TABLE
--- arch/x86/include/asm/vmx.h | 8 ++++++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/x86.c | 1 + arch/x86/kvm/x86.h | 5 +++-- 4 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +8 -0
@@ -90,6 +90,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_HOST_CET_STATE 0x10000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -103,6 +104,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_GUEST_CET_STATE 0x00100000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
@@ -321,6 +323,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,
@@ -333,6 +338,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 };/*
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -31,6 +31,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/x86.c +1 -0
@@ -378,6 +378,7 @@ static int exception_class(int vector)case NP_VECTOR :case SS_VECTOR :case GP_VECTOR : +case CP_VECTOR :return EXCPT_CONTRIBUTORY ;default :break ;
arch/x86/kvm/x86.h +3 -2
@@ -114,7 +114,7 @@ static inline bool x86_exception_has_error_code(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; }
@@ -298,7 +298,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,* Right now, no XSS states are used on x86 platform, * expand the macro for new features. */ -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS (XFEATURE_MASK_CET_USER \ + | XFEATURE_MASK_CET_KERNEL)extern u64 host_xcr0;
Signature · 1 line
2.17.2
Re: [PATCH v8 2/7] KVM: VMX: Define CET VMCS fields and #CP flag
Quoted 133 lines · Yang Weijiang
CET(Control-flow Enforcement Technology) is an upcoming Intel(R) processor feature that blocks Return/Jump-Oriented Programming(ROP) attacks. It provides the following capabilities to defend against ROP/JOP style control-flow subversion attacks:
Shadow Stack (SHSTK):
A second stack for program which is used exclusively for control transfer operations.Indirect Branch Tracking (IBT):
Code branching protection to defend against jump/call oriented programming.Several new CET MSRs are defined in kernel to support CET:
MSR_IA32_{U,S}_CET: Controls the CET settings for user
mode and suervisor mode respectively.MSR_IA32_PL{0,1,2,3}_SSP: Stores shadow stack pointers for
CPL-0,1,2,3 level 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]: For saving/restoring user mode CET states IA32_XSS:[bit 12]: For saving/restoring supervisor mode CET states.Six VMCS fields are introduced for CET:
{HOST,GUEST}_S_CET: Stores CET settings for supervisor mode. {HOST,GUEST}_SSP: Stores shadow stack pointer for supervisor mode. {HOST,GUEST}_INTR_SSP_TABLE: Stores base address of shadow stack pointer
table.If
VM_EXIT_LOAD_HOST_CET_STATE= 1, the host's CET MSRs are restored from below VMCS fields at VM-Exit:
HOST_S_CET
HOST_SSP
HOST_INTR_SSP_TABLEIf
VM_ENTRY_LOAD_GUEST_CET_STATE= 1, the guest's CET MSRs are loaded from below VMCS fields at VM-Entry:
GUEST_S_CET
GUEST_SSP
GUEST_INTR_SSP_TABLECo-developed-by Zhang Yi Z <[email protected]>Signed-off-by Zhang Yi Z <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/include/asm/vmx.h | 8 ++++++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/x86.c | 1 + arch/x86/kvm/x86.h | 5 +++-- 4 files changed, 13 insertions(+), 2 deletions(-)arch/x86/include/asm/vmx.h +8
-0@@ -90,6 +90,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_HOST_CET_STATE 0x10000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff@@ -103,6 +104,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_GUEST_CET_STATE 0x00100000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff@@ -321,6 +323,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,@@ -333,6 +338,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 };/* arch/x86/include/uapi/asm/kvm.h +1
-0@@ -31,6 +31,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_PITarch/x86/kvm/x86.c +1
-0@@ -378,6 +378,7 @@ static int exception_class(int vector)case NP_VECTOR :case SS_VECTOR :case GP_VECTOR : +case CP_VECTOR :return EXCPT_CONTRIBUTORY ;default :break ;arch/x86/kvm/x86.h +1
-1@@ -114,7 +114,7 @@ static inline bool x86_exception_has_error_code(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; }
arch/x86/kvm/x86.h int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, · line 299
Quoted 2 lines
arch/x86/kvm/x86.h +0
-0@@ -298,7 +298,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,* Right now, no XSS states are used on x86 platform, * expand the macro for new features.
I assume this comment needs to be updated?
Quoted 10 lines
arch/x86/kvm/x86.h +2
-1*/ -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS (XFEATURE_MASK_CET_USER \ + | XFEATURE_MASK_CET_KERNEL)extern u64 host_xcr0;Signature · 1 line
2.17.2
Re: [PATCH v8 2/7] KVM: VMX: Define CET VMCS fields and #CP flag
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Yang Weijiang
CET(Control-flow Enforcement Technology) is an upcoming Intel(R) processor feature that blocks Return/Jump-Oriented Programming(ROP) attacks. It provides the following capabilities to defend against ROP/JOP style control-flow subversion attacks:
Quoted 8 lines
arch/x86/kvm/x86.h int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2, · line 299
Quoted 5 lines
arch/x86/kvm/x86.h +0
-0return (1U << vector) & exception_has_error_code; }@@ -298,7 +298,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,* Right now, no XSS states are used on x86 platform, * expand the macro for new features. I assume this comment needs to be updated?
I'm not sure which features in upstream code are using xsaves bits, should I go like this:
In future, other XSS state bits can be added here to make them available to guest?
Quoted 10 lines
Quoted 10 lines
arch/x86/kvm/x86.h +2
-1*/ -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS (XFEATURE_MASK_CET_USER \ + | XFEATURE_MASK_CET_KERNEL)extern u64 host_xcr0;Signature · 1 line
2.17.2
PATCH v8 3/7 KVM: VMX: Pass through CET related MSRs
In reply to Yang WeijiangCET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.
MSR_IA32_U_CET and MSR_IA32_PL3_SSP are used for user mode protection, the contents could differ from process to process, therefore, kernel needs to save/restore them during context switch, it makes sense to pass through them so that the guest kernel can
use xsaves/xrstors to operate them efficiently. Other MSRs are used for non-user mode protection. See CET spec for detailed info.
The difference between CET VMCS state fields and xsave components is that, the former used for CET state storage during VMEnter/VMExit, whereas the latter used for state retention between Guest task/process switch.
--- arch/x86/kvm/cpuid.c | 4 +-- arch/x86/kvm/cpuid.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 65 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +2 -2
@@ -371,13 +371,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)F (AVX512VBMI ) |F (LA57 ) |F (PKU ) |0 /*OSPKE*/ |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 ); +F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |F (SHSTK );/* cpuid 7.0.edx*/ const u32 kvm_cpuid_7_0_edx_x86_features =F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) | -F (MD_CLEAR ); +F (MD_CLEAR ) |F (IBT );/* cpuid 7.1.eax */ const u32 kvm_cpuid_7_1_eax_x86_features =
arch/x86/kvm/cpuid.h +2 -0
@@ -27,6 +27,8 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,int cpuid_query_maxphyaddr (struct kvm_vcpu *vcpu); +u64 kvm_supported_xss (void ); +static inline int cpuid_maxphyaddr (struct kvm_vcpu *vcpu) {return vcpu->arch.maxphyaddr;
arch/x86/kvm/vmx/vmx.c +65 -0
83 lines not on this page · raw message
Signature · 1 line
2.17.2
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 34 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.
MSR_IA32_U_CETandMSR_IA32_PL3_SSPare used for user mode protection, the contents could differ from process to process, therefore, kernel needs to save/restore them during context switch, it makes sense to pass through them so that the guest kernel can
use xsaves/xrstors to operate them efficiently. Other MSRs are used for non-user mode protection. See CET spec for detailed info.The difference between CET VMCS state fields and xsave components is that, the former used for CET state storage during VMEnter/VMExit, whereas the latter used for state retention between Guest task/process switch.
Co-developed-by Zhang Yi Z <[email protected]>Signed-off-by Zhang Yi Z <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/cpuid.c | 4 +-- arch/x86/kvm/cpuid.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 65 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-)arch/x86/kvm/cpuid.c +0
-0@@ -371,13 +371,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)F (AVX512VBMI ) |F (LA57 ) |F (PKU ) |0 /*OSPKE*/ | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 380
Quoted 10 lines
arch/x86/kvm/cpuid.c +2
-2F (VAES ) |F (VPCLMULQDQ ) |F (AVX512_VNNI ) |F (AVX512_BITALG ) | -F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ); +F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |F (SHSTK );/* cpuid 7.0.edx*/ const u32 kvm_cpuid_7_0_edx_x86_features =F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) | -F (MD_CLEAR ); +F (MD_CLEAR ) |F (IBT );
Advertising CET to userspace/guest needs to be done at the end of the series, or at least after CR4.CET is no longer reserved, e.g. KVM_SET_SREGS will fail and the guest will get a #GP when trying to set CR4.CET.
I'm pretty sure I've said this at least twice in previous versions of this series...
Quoted 46 lines
arch/x86/kvm/cpuid.c +0
-0/* cpuid 7.1.eax */ const u32 kvm_cpuid_7_1_eax_x86_features =arch/x86/kvm/cpuid.h +2
-0@@ -27,6 +27,8 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,int cpuid_query_maxphyaddr (struct kvm_vcpu *vcpu); +u64 kvm_supported_xss (void ); +static inline int cpuid_maxphyaddr (struct kvm_vcpu *vcpu) {return vcpu->arch.maxphyaddr;arch/x86/kvm/vmx/vmx.c +18
-0@@ -2918,6 +2918,24 @@ void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)vmcs_writel (GUEST_CR3 , guest_cr3); } +static bool guest_cet_allowed (struct kvm_vcpu *vcpu,u32 feature,u32 mode) +{ +u64 kvm_xss =kvm_supported_xss (); + +/* +* Sanity check for guest CET dependencies, guest_cpu_has(SHSTK|IBT) has +* implied corresponding host CET status check. +*/ +if (feature ==X86_FEATURE_SHSTK ) +return guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) && + (kvm_xss & mode); +else if (feature ==X86_FEATURE_IBT ) +return guest_cpuid_has (vcpu,X86_FEATURE_IBT ) && + (kvm_xss & mode); + +return false; +} +int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {struct vcpu_vmx *vmx =to_vmx (vcpu);
arch/x86/kvm/vmx/vmx.c static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) · line 7022
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -7001,6 +7019,50 @@ static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i *4 )); } +static void vmx_pass_cet_msrs (struct kvm_vcpu *vcpu)
"pass" isn't accurate, this function also does the opposite. Maybe vmx_update_intercept_for_cet_msr()? Or reuse the PT naming and go with cet_update_intercept_for_msr()?
arch/x86/kvm/vmx/vmx.c static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) · line 7032
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+{ +struct vcpu_vmx *vmx =to_vmx (vcpu); +unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; + +/* +* U_CET is required for USER CET, per CET spec., meanwhile U_CET and +* PL3_SPP are a bundle for USER CET xsaves. +*/ +if (guest_cet_allowed (vcpu,X86_FEATURE_SHSTK ,XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT ,XFEATURE_MASK_CET_USER )) {
IMO, the guest_cet_allowed() wrappers do more harm than good, e.g. I find this easier to understand because it doesn't require digging into a random helper.
if ((kvm_supported_xss() & XFEATURE_MASK_CET_USER) && (guest_cpuid_has(vcpu, X86_FEATURE_SHSTK) || guest_cpuid_has(vcpu, X86_FEATURE_IBT)))
Quoted 37 lines
arch/x86/kvm/vmx/vmx.c +33
-0+vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_U_CET ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL3_SSP ,MSR_TYPE_RW ); + }else { +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_U_CET ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL3_SSP ,MSR_TYPE_RW , true); + } +/* +* S_CET is required for KERNEL CET, meanwhile PL0_SSP ... PL2_SSP are a bundle +* for CET KERNEL xsaves. +*/ +if (guest_cet_allowed (vcpu,X86_FEATURE_SHSTK ,XFEATURE_MASK_CET_KERNEL ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT ,XFEATURE_MASK_CET_KERNEL )) { +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_S_CET ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL0_SSP ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL1_SSP ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL2_SSP ,MSR_TYPE_RW ); + +/* SSP_TAB only available for KERNEL SHSTK.*/ +if (guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB , +MSR_TYPE_RW ); +else +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB , +MSR_TYPE_RW , true); + }else { +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_S_CET ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL0_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL1_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL2_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB ,MSR_TYPE_RW , true); + } +} +static void vmx_cpuid_update (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);
Hmm, this looks insufficent, e.g. deliberately toggling CET from on->off while in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with -EINVAL?
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0
-0}static void vmx_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry)Signature · 1 line
2.17.2
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 29 lines · Sean Christopherson
Quoted 13 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.arch/x86/kvm/cpuid.c +0
-0@@ -371,13 +371,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)F (AVX512VBMI ) |F (LA57 ) |F (PKU ) |0 /*OSPKE*/ | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 380
Quoted 10 lines
arch/x86/kvm/cpuid.c +2
-2F (VAES ) |F (VPCLMULQDQ ) |F (AVX512_VNNI ) |F (AVX512_BITALG ) | -F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ); +F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |F (SHSTK );/* cpuid 7.0.edx*/ const u32 kvm_cpuid_7_0_edx_x86_features =F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) | -F (MD_CLEAR ); +F (MD_CLEAR ) |F (IBT );Advertising CET to userspace/guest needs to be done at the end of the series, or at least after CR4.CET is no longer reserved, e.g.
KVM_SET_SREGSwill fail and the guest will get a #GP when trying to set CR4.CET.
Quoted 2 lines
I'm pretty sure I've said this at least twice in previous versions of this series...
Thanks Sean for picking these up!
The reason is, starting from this patch, I'm using guest_cpuid_has(CET) to check the availability of guest CET CPUID, so logically I would like to let the readers understand CET related CPUID word is
defined as above. But no problem, I can move these definitions to a latter patch as the patchset only meaningful as a whole.
Quoted 10 lines
Quoted 10 lines
arch/x86/kvm/cpuid.c +0
-0/* cpuid 7.1.eax */ const u32 kvm_cpuid_7_1_eax_x86_features =arch/x86/kvm/cpuid.h +0
-0@@ -27,6 +27,8 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Quoted 6 lines
arch/x86/kvm/cpuid.h bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, · line 28
Quoted 1 line
arch/x86/kvm/cpuid.h +1
-0+static void vmx_pass_cet_msrs (struct kvm_vcpu *vcpu)"pass" isn't accurate, this function also does the opposite. Maybe
vmx_update_intercept_for_cet_msr()? Or reuse the PT naming and go withcet_update_intercept_for_msr()?
Sure, will change it.
Quoted 15 lines
arch/x86/kvm/cpuid.h bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx,
Quoted 10 lines
arch/x86/kvm/cpuid.h +10
-0+{ +struct vcpu_vmx *vmx =to_vmx (vcpu); +unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; + +/* +* U_CET is required for USER CET, per CET spec., meanwhile U_CET and +* PL3_SPP are a bundle for USER CET xsaves. +*/ +if (guest_cet_allowed (vcpu,X86_FEATURE_SHSTK ,XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT ,XFEATURE_MASK_CET_USER )) {IMO, the
guest_cet_allowed()wrappers do more harm than good, e.g. I find this easier to understand because it doesn't require digging into a random helper.
Quoted 4 lines
if ((kvm_supported_xss() & XFEATURE_MASK_CET_USER) && (guest_cpuid_has(vcpu, X86_FEATURE_SHSTK) || guest_cpuid_has(vcpu, X86_FEATURE_IBT)))
Hmm, sounds like it's an unnecessary wrapper, will remove it, thanks!
Quoted 39 lines
Quoted 39 lines
arch/x86/kvm/cpuid.h +33
-0+vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_U_CET ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL3_SSP ,MSR_TYPE_RW ); + }else { +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_U_CET ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL3_SSP ,MSR_TYPE_RW , true); + } +/* +* S_CET is required for KERNEL CET, meanwhile PL0_SSP ... PL2_SSP are a bundle +* for CET KERNEL xsaves. +*/ +if (guest_cet_allowed (vcpu,X86_FEATURE_SHSTK ,XFEATURE_MASK_CET_KERNEL ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT ,XFEATURE_MASK_CET_KERNEL )) { +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_S_CET ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL0_SSP ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL1_SSP ,MSR_TYPE_RW ); +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_PL2_SSP ,MSR_TYPE_RW ); + +/* SSP_TAB only available for KERNEL SHSTK.*/ +if (guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +vmx_disable_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB , +MSR_TYPE_RW ); +else +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB , +MSR_TYPE_RW , true); + }else { +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_S_CET ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL0_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL1_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_PL2_SSP ,MSR_TYPE_RW , true); +vmx_set_intercept_for_msr (msr_bitmap,MSR_IA32_INT_SSP_TAB ,MSR_TYPE_RW , true); + } +} +static void vmx_cpuid_update (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))
Quoted 8 lines
arch/x86/kvm/cpuid.h static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 4 lines
arch/x86/kvm/cpuid.h +3
-0update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);Hmm, this looks insufficent, e.g. deliberately toggling CET from
on->offwhile in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.
Not sure I understand correctly, guest_cpu_has(CET) implies the check of host CET status, if CET is off in host, CET MSRs won't exposed to L1 guest.
Quoted 4 lines
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with
-EINVAL?
Do you mean prevent L1 using KVM_SET_CPUID{2} to expose CET feature bits to L2?
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/cpuid.h +0
-0}static void vmx_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry)Signature · 1 line
2.17.2
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 33 lines · Yang Weijiang
Quoted 29 lines · Sean Christopherson
Quoted 13 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.arch/x86/kvm/cpuid.c +0
-0@@ -371,13 +371,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)F (AVX512VBMI ) |F (LA57 ) |F (PKU ) |0 /*OSPKE*/ | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 380
Quoted 10 lines
arch/x86/kvm/cpuid.c +2
-2F (VAES ) |F (VPCLMULQDQ ) |F (AVX512_VNNI ) |F (AVX512_BITALG ) | -F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ); +F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |F (SHSTK );/* cpuid 7.0.edx*/ const u32 kvm_cpuid_7_0_edx_x86_features =F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) | -F (MD_CLEAR ); +F (MD_CLEAR ) |F (IBT );Advertising CET to userspace/guest needs to be done at the end of the series, or at least after CR4.CET is no longer reserved, e.g.
KVM_SET_SREGSwill fail and the guest will get a #GP when trying to set CR4.CET.Quoted 2 lines
I'm pretty sure I've said this at least twice in previous versions of this series...
Quoted 6 lines
Thanks Sean for picking these up!
The reason is, starting from this patch, I'm usingguest_cpuid_has(CET)to check the availability of guest CET CPUID, so logically I would like to let the readers understand CET related CPUID word is
defined as above. But no problem, I can move these definitions to a latter patch as the patchset only meaningful as a whole.
Adding usage of guest_cpuid_has(CET) without advertising CET is perfectly ok from a functionality perspective. Having a user without a consumer isn't ideal, but it's better than having one gigantic patch.
The problem with advertising CET when it's not fully supported is that it will break bisection, e.g. trying to boot a CET-enabled guest would get a #GP during boot and likely crash. Whether or not a series is useful when taken as a whole is orthogonal to the integrity of each invidiual patch.
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 41 lines · Sean Christopherson
Quoted 33 lines · Yang Weijiang
Quoted 29 lines · Sean Christopherson
Quoted 13 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.arch/x86/kvm/cpuid.c +0
-0@@ -371,13 +371,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)F (AVX512VBMI ) |F (LA57 ) |F (PKU ) |0 /*OSPKE*/ | F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 380
Quoted 10 lines
arch/x86/kvm/cpuid.c +2
-2F (VAES ) |F (VPCLMULQDQ ) |F (AVX512_VNNI ) |F (AVX512_BITALG ) | -F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ); +F (CLDEMOTE ) |F (MOVDIRI ) |F (MOVDIR64B ) |F (SHSTK );/* cpuid 7.0.edx*/ const u32 kvm_cpuid_7_0_edx_x86_features =F (AVX512_4VNNIW ) |F (AVX512_4FMAPS ) |F (SPEC_CTRL ) |F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) | -F (MD_CLEAR ); +F (MD_CLEAR ) |F (IBT );Advertising CET to userspace/guest needs to be done at the end of the series, or at least after CR4.CET is no longer reserved, e.g.
KVM_SET_SREGSwill fail and the guest will get a #GP when trying to set CR4.CET.Quoted 2 lines
I'm pretty sure I've said this at least twice in previous versions of this series...
Quoted 6 lines
Thanks Sean for picking these up!
The reason is, starting from this patch, I'm usingguest_cpuid_has(CET)to check the availability of guest CET CPUID, so logically I would like to let the readers understand CET related CPUID word is
defined as above. But no problem, I can move these definitions to a latter patch as the patchset only meaningful as a whole.
Quoted 8 lines
Adding usage of
guest_cpuid_has(CET)without advertising CET is perfectly ok from a functionality perspective. Having a user without a consumer isn't ideal, but it's better than having one gigantic patch.The problem with advertising CET when it's not fully supported is that it will break bisection, e.g. trying to boot a CET-enabled guest would get a #GP during boot and likely crash. Whether or not a series is useful when taken as a whole is orthogonal to the integrity of each invidiual patch.
Oh, I omitted case likes bisection, you're right, I'll change it, thanks a lot!
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Quoted 6 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.+
Quoted 15 lines
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 10 lines
static void
vmx_cpuid_update(struct kvm_vcpu *vcpu){arch/x86/kvm/vmx/vmx.c +3
-0struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);Hmm, this looks insufficent, e.g. deliberately toggling CET from
on->offwhile in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.
Hi, Sean,
I don't get you, there's guest mode check before access CET msrs, it'll fail if it's in guest mode.
Quoted 3 lines
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with
-EINVAL?
Do you mean don't expose CET cpuids to L2 guest?
thanks!
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0
-0}static void vmx_set_supported_cpuid (u32 func,struct kvm_cpuid_entry2 *entry)Signature · 1 line
2.17.2
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 24 lines · Yang Weijiang
Quoted 8 lines · Sean Christopherson
Quoted 6 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.+
Quoted 15 lines
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 10 lines
static void
vmx_cpuid_update(struct kvm_vcpu *vcpu){arch/x86/kvm/vmx/vmx.c +3
-0struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);Hmm, this looks insufficent, e.g. deliberately toggling CET from
on->offwhile in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.
Quoted 3 lines
Hi, Sean,
I don't get you, there's guest mode check before access CET msrs, it'll fail if it's in guest mode.
KVM can exit to userspae while L2 is active. If userspace then did a KVM_SET_CPUID2, e.g. instead of KVM_RUN, vmx_cpuid_update() would skip vmx_pass_cet_msrs() and KVM would never update L1's MSR bitmaps.
Quoted 4 lines
Quoted 3 lines
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with
-EINVAL?Do you mean don't expose CET cpuids to L2 guest?
I mean completely disallow KVM_SET_CPUID and KVM_SET_CPUID2 if is_guest_mode() is true. My question is mostly directed at Paolo and anyone else that has an opinion on whether we can massage the ABI to retroactively change KVM_SET_CPUID{2} behavior.
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 25 lines · Sean Christopherson
Quoted 24 lines · Yang Weijiang
Quoted 23 lines · Sean Christopherson
Quoted 6 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.+
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 10 lines
static void
vmx_cpuid_update(struct kvm_vcpu *vcpu){arch/x86/kvm/vmx/vmx.c +3
-0struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);Hmm, this looks insufficent, e.g. deliberately toggling CET from
on->offwhile in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.
Quoted 8 lines
Quoted 3 lines
Hi, Sean,
I don't get you, there's guest mode check before access CET msrs, it'll fail if it's in guest mode.KVM can exit to userspae while L2 is active. If userspace then did a
KVM_SET_CPUID2, e.g. instead ofKVM_RUN,vmx_cpuid_update()would skipvmx_pass_cet_msrs()and KVM would never update L1's MSR bitmaps.
Thanks, it makes sense to me. Given current implementation, how about removing above check and adding it in CET CPUID enumeration for L2 so that no CET msrs passed through to L2 when is_guest_mode() is true?
Quoted 9 lines
Quoted 4 lines
Quoted 3 lines
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with
-EINVAL?Do you mean don't expose CET cpuids to L2 guest?
I mean completely disallow
KVM_SET_CPUIDandKVM_SET_CPUID2ifis_guest_mode()is true. My question is mostly directed at Paolo and anyone else that has an opinion on whether we can massage the ABI to retroactively change KVM_SET_CPUID{2} behavior.
This sounds like something deserving an individual patch after get agreement in community. I'll put it aside right now.
Re: [PATCH v8 3/7] KVM: VMX: Pass through CET related MSRs
Quoted 30 lines · Yang Weijiang
Quoted 29 lines · Sean Christopherson
Quoted 27 lines · Yang Weijiang
Quoted 8 lines · Sean Christopherson
Quoted 6 lines · Yang Weijiang
CET MSRs pass through Guest directly to enhance performance. CET runtime control settings are stored in MSR_IA32_{U,S}_CET, Shadow Stack Pointer(SSP) are stored in MSR_IA32_PL{0,1,2,3}_SSP, SSP table base address is stored in
MSR_IA32_INT_SSP_TAB, these MSRs are defined in kernel and re-used here.+
Quoted 15 lines
arch/x86/kvm/vmx/vmx.c static void vmx_cpuid_update(struct kvm_vcpu *vcpu) · line 7092
Quoted 10 lines
static void
vmx_cpuid_update(struct kvm_vcpu *vcpu){arch/x86/kvm/vmx/vmx.c +3
-0struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -7025,6 +7087,9 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_INTEL_PT ) &&guest_cpuid_has (vcpu,X86_FEATURE_INTEL_PT ))update_intel_pt_cfg (vcpu); + +if (!is_guest_mode (vcpu)) +vmx_pass_cet_msrs (vcpu);Hmm, this looks insufficent, e.g. deliberately toggling CET from
on->offwhile in guest mode would put KVM in a weird state as the msr bitmap for L1 would still allow L1 to access the CET MSRs.Hi, Sean,
I don't get you, there's guest mode check before access CET msrs, it'll fail if it's in guest mode.
Quoted 8 lines
Quoted 4 lines
KVM can exit to userspae while L2 is active. If userspace then did a
KVM_SET_CPUID2, e.g. instead ofKVM_RUN,vmx_cpuid_update()would skipvmx_pass_cet_msrs()and KVM would never update L1's MSR bitmaps.Thanks, it makes sense to me. Given current implementation, how about removing above check and adding it in CET CPUID enumeration for L2 so that no CET msrs passed through to L2 when
is_guest_mode()is true?
But you still need to update L1's MSR bitmaps. That can obviously be done all at once, but it's annoying and IMO unnecessarily complex.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
Allowing KVM_SET_CPUID{2} while running a nested guest seems bogus, can we kill that path entirely with
-EINVAL?Do you mean don't expose CET cpuids to L2 guest?
Quoted 7 lines
Quoted 4 lines
I mean completely disallow
KVM_SET_CPUIDandKVM_SET_CPUID2ifis_guest_mode()is true. My question is mostly directed at Paolo and anyone else that has an opinion on whether we can massage the ABI to retroactively change KVM_SET_CPUID{2} behavior.This sounds like something deserving an individual patch after get agreement in community. I'll put it aside right now.
Normally I would agree, but I think in this case it would significantly reduce the complexity and implementation cost of CET support. I'll send a patch to kickstart the conversation, it's a tiny change in terms of code.
PATCH v8 4/7 KVM: VMX: Load CET states on vmentry/vmexit
In reply to Yang Weijiang"Load {guest,host} CET state" bit controls whether guest/host CET states will be loaded at VM entry/exit. Before doing that, KVM needs to check if CET is both enabled on host and guest.
Note: SHSTK and IBT features share one control MSR:
MSR_IA32_{U,S}_CET, which means it's difficult to hide one feature from another in the case of SHSTK != IBT, after discussed in community, it's agreed to allow Guest control two features independently as it won't introduce security hole.
--- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/vmx/capabilities.h | 10 ++++++ arch/x86/kvm/vmx/vmx.c | 55 +++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -90,7 +90,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/vmx/capabilities.h +10 -0
@@ -106,6 +106,16 @@ static inline bool vmx_mpx_supported(void) (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_BNDCFGS ); } +static inline bool cpu_has_load_guest_cet_states_ctrl (void ) +{ +return ((vmcs_config.vmentry_ctrl) &VM_ENTRY_LOAD_GUEST_CET_STATE ); +} + +static inline bool cpu_has_load_host_cet_states_ctrl (void ) +{ +return ((vmcs_config.vmexit_ctrl) &VM_EXIT_LOAD_HOST_CET_STATE ); +} +static inline bool cpu_has_vmx_tpr_shadow (void ) {return vmcs_config.cpu_based_exec_ctrl &CPU_BASED_TPR_SHADOW ;
arch/x86/kvm/vmx/vmx.c +53 -2
109 lines not on this page · raw message
Signature · 1 line
2.17.2
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 15 lines · Yang Weijiang
"Load {guest,host} CET state" bit controls whether guest/host CET states will be loaded at VM entry/exit. Before doing that, KVM needs to check if CET is both enabled on host and guest.
Note: SHSTK and IBT features share one control MSR:
MSR_IA32_{U,S}_CET, which means it's difficult to hide one feature from another in the case of SHSTK != IBT, after discussed in community, it's agreed to allow Guest control two features independently as it won't introduce security hole.Co-developed-by Zhang Yi Z <[email protected]>Signed-off-by Zhang Yi Z <[email protected]>Signed-off-by Yang Weijiang <[email protected]>---
...
Quoted 33 lines
arch/x86/kvm/vmx/vmx.c +5
-2@@ -44,6 +44,7 @@#include <asm/spec-ctrl.h> #include <asm/virtext.h> #include <asm/vmx.h> +#include <asm/cet.h> #include "capabilities.h" #include "cpuid.h" @@ -2336,7 +2337,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_HOST_CET_STATE ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_EXIT_CTLS , &_vmexit_control) <0 )return -EIO ;@@ -2360,7 +2362,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_GUEST_CET_STATE ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_ENTRY_CTLS , &_vmentry_control) <0 )return -EIO ;
arch/x86/kvm/vmx/vmx.c void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 2841
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -2834,6 +2837,9 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long hw_cr0; +if (!(cr0 &X86_CR0_WP ) &&kvm_read_cr4_bits (vcpu,X86_CR4_CET )) + cr0 |=X86_CR0_WP ;
Huh? What's the interaction between CR4.CET and CR0.WP? If there really is some non-standard interaction then it needs to be documented in at least the changelog and probably with a comment as well.
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +3
-0+ hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF );if (enable_unrestricted_guest) hw_cr0 |=KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST ;@@ -2936,6 +2942,22 @@ static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode)return false; } +bool is_cet_bit_allowed (struct kvm_vcpu *vcpu) +{
arch/x86/kvm/vmx/vmx.c static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode) · line 2956
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+unsigned long cr0; +bool cet_allowed; + + cr0 =kvm_read_cr0 (vcpu); + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if ((cr0 &X86_CR0_WP ) && cet_allowed) +return true;
So, attempting to set CR4.CET if CR0.WP=0 takes a #GP? But attempting to clear CR0.WP if CR4.CET=1 is ignored?
Quoted 67 lines
arch/x86/kvm/vmx/vmx.c +33
-0+ +return false; +} +int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -2976,6 +2998,9 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } +if ((cr4 &X86_CR4_CET ) && !is_cet_bit_allowed (vcpu)) +return 1 ; +if (vmx->nested.vmxon && !nested_cr4_valid (vcpu, cr4))return 1 ;@@ -3839,6 +3864,12 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)if (cpu_has_load_ia32_efer ())vmcs_write64 (HOST_IA32_EFER , host_efer); + +if (cpu_has_load_host_cet_states_ctrl ()) { +vmcs_writel (HOST_S_CET ,0 ); +vmcs_writel (HOST_INTR_SSP_TABLE ,0 ); +vmcs_writel (HOST_SSP ,0 ); + } }void set_cr4_guest_host_mask (struct vcpu_vmx *vmx)@@ -6436,6 +6467,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long cr3, cr4; +bool cet_allowed;/* Record the guest's net vcpu time for enforced NMI injections. */ if (unlikely(!enable_vnmi &&@@ -6466,6 +6498,25 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->loaded_vmcs->host_state.cr3 = cr3; } +/* To be aligned with kernel code, only user mode is supported now. */ + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if (cpu_has_load_guest_cet_states_ctrl () && cet_allowed) +vmcs_set_bits (VM_ENTRY_CONTROLS , +VM_ENTRY_LOAD_GUEST_CET_STATE ); +else +vmcs_clear_bits (VM_ENTRY_CONTROLS , +VM_ENTRY_LOAD_GUEST_CET_STATE ); + +if (cpu_has_load_host_cet_states_ctrl () && cet_allowed) +vmcs_set_bits (VM_EXIT_CONTROLS , +VM_EXIT_LOAD_HOST_CET_STATE ); +else +vmcs_clear_bits (VM_EXIT_CONTROLS , +VM_EXIT_LOAD_HOST_CET_STATE ); + cr4 =cr4_read_shadow ();if (unlikely (cr4 != vmx->loaded_vmcs->host_state.cr4)) {vmcs_writel (HOST_CR4 , cr4);Signature · 1 line
2.17.2
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +0
-0@@ -2834,6 +2837,9 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long hw_cr0;
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 2841
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+if (!(cr0 &X86_CR0_WP ) &&kvm_read_cr4_bits (vcpu,X86_CR4_CET )) + cr0 |=X86_CR0_WP ;Huh? What's the interaction between CR4.CET and CR0.WP? If there really is some non-standard interaction then it needs to be documented in at least the changelog and probably with a comment as well.
The processor does not allow CR4.CET to be set if CR0.WP = 0 (similarly, it does not allow CR0.WP to be cleared while CR4.CET = 1).
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0+ hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF );if (enable_unrestricted_guest) hw_cr0 |=KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST ;@@ -2936,6 +2942,22 @@ static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode)return false; }
Quoted 16 lines
arch/x86/kvm/vmx/vmx.c static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode) · line 2956
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +12
-0+bool is_cet_bit_allowed (struct kvm_vcpu *vcpu) +{ +unsigned long cr0; +bool cet_allowed; + + cr0 =kvm_read_cr0 (vcpu); + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if ((cr0 &X86_CR0_WP ) && cet_allowed) +return true;So, attempting to set CR4.CET if CR0.WP=0 takes a #GP? But attempting to clear CR0.WP if CR4.CET=1 is ignored?
Per above words in spec., inject #GP to guest in either case?
Quoted 67 lines
Quoted 67 lines
arch/x86/kvm/vmx/vmx.c +33
-0+ +return false; +} +int vmx_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {struct vcpu_vmx *vmx =to_vmx (vcpu);@@ -2976,6 +2998,9 @@ int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } +if ((cr4 &X86_CR4_CET ) && !is_cet_bit_allowed (vcpu)) +return 1 ; +if (vmx->nested.vmxon && !nested_cr4_valid (vcpu, cr4))return 1 ;@@ -3839,6 +3864,12 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)if (cpu_has_load_ia32_efer ())vmcs_write64 (HOST_IA32_EFER , host_efer); + +if (cpu_has_load_host_cet_states_ctrl ()) { +vmcs_writel (HOST_S_CET ,0 ); +vmcs_writel (HOST_INTR_SSP_TABLE ,0 ); +vmcs_writel (HOST_SSP ,0 ); + } }void set_cr4_guest_host_mask (struct vcpu_vmx *vmx)@@ -6436,6 +6467,7 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long cr3, cr4; +bool cet_allowed;/* Record the guest's net vcpu time for enforced NMI injections. */ if (unlikely(!enable_vnmi &&@@ -6466,6 +6498,25 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) vmx->loaded_vmcs->host_state.cr3 = cr3; } +/* To be aligned with kernel code, only user mode is supported now. */ + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if (cpu_has_load_guest_cet_states_ctrl () && cet_allowed) +vmcs_set_bits (VM_ENTRY_CONTROLS , +VM_ENTRY_LOAD_GUEST_CET_STATE ); +else +vmcs_clear_bits (VM_ENTRY_CONTROLS , +VM_ENTRY_LOAD_GUEST_CET_STATE ); + +if (cpu_has_load_host_cet_states_ctrl () && cet_allowed) +vmcs_set_bits (VM_EXIT_CONTROLS , +VM_EXIT_LOAD_HOST_CET_STATE ); +else +vmcs_clear_bits (VM_EXIT_CONTROLS , +VM_EXIT_LOAD_HOST_CET_STATE ); + cr4 =cr4_read_shadow ();if (unlikely (cr4 != vmx->loaded_vmcs->host_state.cr4)) {vmcs_writel (HOST_CR4 , cr4);Signature · 1 line
2.17.2
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 13 lines · Yang Weijiang
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +0
-0@@ -2834,6 +2837,9 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long hw_cr0;Quoted 7 lines
arch/x86/kvm/vmx/vmx.c void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 2841
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2
-0+if (!(cr0 &X86_CR0_WP ) &&kvm_read_cr4_bits (vcpu,X86_CR4_CET )) + cr0 |=X86_CR0_WP ;Huh? What's the interaction between CR4.CET and CR0.WP? If there really is some non-standard interaction then it needs to be documented in at least the changelog and probably with a comment as well.
Quoted 2 lines
The processor does not allow CR4.CET to be set if CR0.WP = 0 (similarly, it does not allow CR0.WP to be cleared while CR4.CET = 1).
Ya, as you surmised below, this needs to be a #GP condition.
Have you tested SMM at all? The interaction between CR0 and CR4 may be problematic for em_rsm() and/or rsm_enter_protected_mode().
Quoted 30 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0+ hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF );if (enable_unrestricted_guest) hw_cr0 |=KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST ;@@ -2936,6 +2942,22 @@ static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode)return false; }Quoted 16 lines
arch/x86/kvm/vmx/vmx.c static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode) · line 2956
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +12
-0+bool is_cet_bit_allowed (struct kvm_vcpu *vcpu) +{ +unsigned long cr0; +bool cet_allowed; + + cr0 =kvm_read_cr0 (vcpu); + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if ((cr0 &X86_CR0_WP ) && cet_allowed) +return true;So, attempting to set CR4.CET if CR0.WP=0 takes a #GP? But attempting to clear CR0.WP if CR4.CET=1 is ignored?
Per above words in spec., inject #GP to guest in either case?
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +4
-0+ +return false; +} +
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Yang Weijiang
Quoted 12 lines · Sean Christopherson
arch/x86/kvm/vmx/vmx.c void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) · line 2841
Quoted 6 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +2
-0@@ -2834,6 +2837,9 @@ void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)struct vcpu_vmx *vmx =to_vmx (vcpu);unsigned long hw_cr0; +if (!(cr0 &X86_CR0_WP ) &&kvm_read_cr4_bits (vcpu,X86_CR4_CET )) + cr0 |=X86_CR0_WP ;Huh? What's the interaction between CR4.CET and CR0.WP? If there really is some non-standard interaction then it needs to be documented in at least the changelog and probably with a comment as well.
Quoted 5 lines
Quoted 2 lines
The processor does not allow CR4.CET to be set if CR0.WP = 0 (similarly, it does not allow CR0.WP to be cleared while CR4.CET = 1).
Ya, as you surmised below, this needs to be a #GP condition.
OK, will do it.
Quoted 3 lines
Have you tested SMM at all? The interaction between CR0 and CR4 may be problematic for
em_rsm()and/orrsm_enter_protected_mode().
Not yet, what's an easy way to test code in SMM mode? Thanks!
Quoted 30 lines
Quoted 30 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0+ hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF );if (enable_unrestricted_guest) hw_cr0 |=KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST ;@@ -2936,6 +2942,22 @@ static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode)return false; }Quoted 16 lines
arch/x86/kvm/vmx/vmx.c static bool guest_cet_allowed(struct kvm_vcpu *vcpu, u32 feature, u32 mode) · line 2956
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +12
-0+bool is_cet_bit_allowed (struct kvm_vcpu *vcpu) +{ +unsigned long cr0; +bool cet_allowed; + + cr0 =kvm_read_cr0 (vcpu); + cet_allowed =guest_cet_allowed (vcpu,X86_FEATURE_SHSTK , +XFEATURE_MASK_CET_USER ) || +guest_cet_allowed (vcpu,X86_FEATURE_IBT , +XFEATURE_MASK_CET_USER ); +if ((cr0 &X86_CR0_WP ) && cet_allowed) +return true;So, attempting to set CR4.CET if CR0.WP=0 takes a #GP? But attempting to clear CR0.WP if CR4.CET=1 is ignored?
Per above words in spec., inject #GP to guest in either case?
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +4
-0+ +return false; +} +
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 5 lines · Yang Weijiang
Quoted 3 lines · Sean Christopherson
Have you tested SMM at all? The interaction between CR0 and CR4 may be problematic for
em_rsm()and/orrsm_enter_protected_mode().Not yet, what's an easy way to test code in SMM mode?
IIRC, SeaBIOS does SMM stuff by default.
Re: [PATCH v8 4/7] KVM: VMX: Load CET states on vmentry/vmexit
Quoted 8 lines · Sean Christopherson
Quoted 5 lines · Yang Weijiang
Quoted 3 lines · Sean Christopherson
Have you tested SMM at all? The interaction between CR0 and CR4 may be problematic for
em_rsm()and/orrsm_enter_protected_mode().Not yet, what's an easy way to test code in SMM mode?
IIRC, SeaBIOS does SMM stuff by default.
Thanks Sean. I'll check this part.
PATCH v8 5/7 KVM: X86: Enable CET bits update in IA32_XSS
In reply to Yang WeijiangPreviously, KVM did not support setting any bits in XSS so it's hardcoded to check and inject a #GP if Guest attempted to write a non-zero value to XSS, now it supports CET related bits setting.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 12 ++++++++++-- arch/x86/kvm/vmx/vmx.c | 6 +----- 3 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -624,6 +624,7 @@ struct kvm_vcpu_arch {u64 xcr0;u64 guest_supported_xcr0; +u64 guest_supported_xss;u32 guest_xstate_size;struct kvm_pio_request pio;
arch/x86/kvm/cpuid.c +10 -2
@@ -125,8 +125,16 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) } best =kvm_find_cpuid_entry (vcpu,0xD ,1 ); -if (best && (best->eax & (F (XSAVES ) |F (XSAVEC )))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best && (best->eax & (F (XSAVES ) |F (XSAVEC )))) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + vcpu->arch.guest_supported_xss = + (best->ecx | ((u64 )best->edx <<32 )) & +kvm_supported_xss (); + }else { + vcpu->arch.guest_supported_xss =0 ; + }/* * The existing code assumes virtual address is 48-bit or 57-bit in the
arch/x86/kvm/vmx/vmx.c +1 -5
@@ -1973,11 +1973,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))return 1 ; -/* -* The only supported bit as of Skylake is bit 8, but -* it is not supported on KVM. -*/ -if (data !=0 ) +if (data & ~vcpu->arch.guest_supported_xss)return 1 ; vcpu->arch.ia32_xss = data;if (vcpu->arch.ia32_xss != host_xss)
Signature · 1 line
2.17.2
PATCH v8 6/7 KVM: X86: Load guest fpu state when accessing MSRs managed by XSAVES
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
A handful of CET MSRs are not context switched through "traditional" methods, e.g. VMCS or manual switching, but rather are passed through to the guest and are saved and restored by XSAVES/XRSTORS, i.e. 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. vmx_{get,set}_msr(), can simply do {RD,WR}MSR to access the guest's value.
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 | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +20 -1
@@ -104,6 +104,8 @@ static void enter_smm(struct kvm_vcpu *vcpu);static void __kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu); +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 ;EXPORT_SYMBOL_GPL (kvm_x86_ops);
@@ -3000,6 +3002,12 @@ 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_PL0_SSP && index <=MSR_IA32_PL3_SSP ); +} +/* * Read or write a bunch of msrs. All parameters are kernel addresses. *
@@ -3010,11 +3018,22 @@ 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; +const u64 cet_bits =XFEATURE_MASK_CET_USER |XFEATURE_MASK_CET_KERNEL ; +bool cet_xss =kvm_supported_xss () & cet_bits; -for (i =0 ; i < msrs->nmsrs; ++i) +for (i =0 ; i < msrs->nmsrs; ++i) { +if (!fpu_loaded && cet_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.17.2
Re: [PATCH v8 6/7] KVM: X86: Load guest fpu state when accessing MSRs managed by XSAVES
Quoted 52 lines · Yang Weijiang
From: Sean Christopherson <[email protected]>
A handful of CET MSRs are not context switched through "traditional" methods, e.g. VMCS or manual switching, but rather are passed through to the guest and are saved and restored by XSAVES/XRSTORS, i.e. 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. vmx_{get,set}_msr(), can simply do {RD,WR}MSR to access the guest's value.
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 doKVM_SET_MSRSprior toKVM_SET_CPUID2.Signed-off-by Sean Christopherson <[email protected]>Co-developed-by Yang Weijiang <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/x86.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)arch/x86/kvm/x86.c +8
-0@@ -104,6 +104,8 @@ static void enter_smm(struct kvm_vcpu *vcpu);static void __kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);static void store_regs (struct kvm_vcpu *vcpu);static int sync_regs (struct kvm_vcpu *vcpu); +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 ;EXPORT_SYMBOL_GPL (kvm_x86_ops);@@ -3000,6 +3002,12 @@ 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_PL0_SSP && index <=MSR_IA32_PL3_SSP ); +} +/* * Read or write a bunch of msrs. All parameters are kernel addresses. * @@ -3010,11 +3018,22 @@ static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,int (*do_msr)(struct kvm_vcpu *vcpu, unsigned index, u64 *data)) {
arch/x86/kvm/x86.c static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, · line 3029
Quoted 10 lines
arch/x86/kvm/x86.c +7
-1+bool fpu_loaded = false;int i; +const u64 cet_bits =XFEATURE_MASK_CET_USER |XFEATURE_MASK_CET_KERNEL ; +bool cet_xss =kvm_supported_xss () & cet_bits; -for (i =0 ; i < msrs->nmsrs; ++i) +for (i =0 ; i < msrs->nmsrs; ++i) { +if (!fpu_loaded && cet_xss && +is_xsaves_msr (entries[i].index)) { +kvm_load_guest_fpu (vcpu);
This needs to also check for a non-NULL @vcpu. KVM_GET_MSR can be called on the VM to invoke do_get_msr_feature().
Quoted 13 lines
arch/x86/kvm/x86.c +5
-0+ 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.17.2
Re: [PATCH v8 6/7] KVM: X86: Load guest fpu state when accessing MSRs managed by XSAVES
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Yang Weijiang
From: Sean Christopherson <[email protected]>
Quoted 9 lines
arch/x86/kvm/x86.c static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, · line 3029
Quoted 5 lines
arch/x86/kvm/x86.c +4
-1-for (i =0 ; i < msrs->nmsrs; ++i) +for (i =0 ; i < msrs->nmsrs; ++i) { +if (!fpu_loaded && cet_xss && +is_xsaves_msr (entries[i].index)) { +kvm_load_guest_fpu (vcpu);This needs to also check for a non-NULL @vcpu.
KVM_GET_MSRcan be called on the VM to invokedo_get_msr_feature().
Yeah, I need to add the check, thanks!
Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/x86.c +5
-0+ 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.17.2
PATCH v8 7/7 KVM: X86: Add user-space access interface for CET MSRs
In reply to Yang WeijiangThere're two different places storing Guest CET states, states managed with XSAVES/XRSTORS, as restored/saved
in previous patch, can be read/write directly from/to the MSRs. For those stored in VMCS fields, they're access via vmcs_read/ vmcs_write.
--- arch/x86/kvm/vmx/vmx.c | 140 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 3 + 2 files changed, 143 insertions(+)
arch/x86/kvm/vmx/vmx.c +140 -0
@@ -1671,6 +1671,98 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)return 0 ; } +#define CET_MSR_RSVD_BITS_1 0x3 +#define CET_MSR_RSVD_BITS_2 (0xF << 6) + +static bool cet_msr_write_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u32 index = msr->index; +u64 data = msr->data; +u32 high_word = data >>32 ; + +if ((index ==MSR_IA32_U_CET || index ==MSR_IA32_S_CET ) && + (data &CET_MSR_RSVD_BITS_2 )) +return false; + +if (is_64_bit_mode (vcpu)) { +if (is_noncanonical_address (data &PAGE_MASK , vcpu)) +return false; +else if ((index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index ==MSR_IA32_PL3_SSP ) && + (data &CET_MSR_RSVD_BITS_1 )) +return false; + }else { +if (msr->index ==MSR_IA32_INT_SSP_TAB ) +return false; +else if ((index ==MSR_IA32_U_CET || + index ==MSR_IA32_S_CET || + index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index ==MSR_IA32_PL3_SSP ) && + (high_word & ~0ul )) +return false; + } + +return true; +} + +static bool cet_msr_access_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u64 kvm_xss; +u32 index = msr->index; + +if (is_guest_mode (vcpu)) +return false; + + kvm_xss =kvm_supported_xss (); + +switch (index) { + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return false; +if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; + }else { +if (index ==MSR_IA32_PL3_SSP ) { +if (!(kvm_xss &XFEATURE_MASK_CET_USER )) +return false; + }else { +if (!(kvm_xss &XFEATURE_MASK_CET_KERNEL )) +return false; + } + } +break ; +case MSR_IA32_U_CET : +case MSR_IA32_S_CET : +if (!boot_cpu_has (X86_FEATURE_SHSTK ) && + !boot_cpu_has (X86_FEATURE_IBT )) +return false; + +if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) && + !guest_cpuid_has (vcpu,X86_FEATURE_IBT )) +return false; + }else if (index ==MSR_IA32_U_CET && + !(kvm_xss &XFEATURE_MASK_CET_USER )) +return false; +break ; +case MSR_IA32_INT_SSP_TAB : +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return false; + +if (!msr->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; +break ; +default : +return false; + } +return true; +}/* * Reads an msr value (of 'msr_index') into 'pdata'. * Returns 0 on success, non-0 otherwise.
@@ -1788,6 +1880,26 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_S_CET ); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_INTR_SSP_TABLE ); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (MSR_IA32_U_CET , msr_info->data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (msr_info->index, msr_info->data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))
@@ -2039,6 +2151,34 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_S_CET , data); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_INTR_SSP_TABLE , data); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (MSR_IA32_U_CET , data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (msr_info->index, data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))
arch/x86/kvm/x86.c +3 -0
@@ -1143,6 +1143,9 @@ static u32 msrs_to_save[] = {MSR_IA32_RTIT_ADDR1_A ,MSR_IA32_RTIT_ADDR1_B ,MSR_IA32_RTIT_ADDR2_A ,MSR_IA32_RTIT_ADDR2_B ,MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B , +MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_S_CET , +MSR_IA32_PL0_SSP ,MSR_IA32_PL1_SSP ,MSR_IA32_PL2_SSP , + MSR_IA32_PL3_SSP, MSR_IA32_INT_SSP_TAB, };static unsigned num_msrs_to_save;
Signature · 1 line
2.17.2
Re: [PATCH v8 7/7] KVM: X86: Add user-space access interface for CET MSRs
Quoted 25 lines · Yang Weijiang
There're two different places storing Guest CET states, states managed with XSAVES/XRSTORS, as restored/saved
in previous patch, can be read/write directly from/to the MSRs. For those stored in VMCS fields, they're access via vmcs_read/vmcs_write.Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 140 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 3 + 2 files changed, 143 insertions(+)arch/x86/kvm/vmx/vmx.c +5
-0@@ -1671,6 +1671,98 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)return 0 ; } +#define CET_MSR_RSVD_BITS_1 0x3 +#define CET_MSR_RSVD_BITS_2 (0xF << 6) + +static bool cet_msr_write_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1688
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+u32 index = msr->index; +u64 data = msr->data; +u32 high_word = data >>32 ; + +if ((index ==MSR_IA32_U_CET || index ==MSR_IA32_S_CET ) && + (data &CET_MSR_RSVD_BITS_2 )) +return false; + +if (is_64_bit_mode (vcpu)) { +if (is_noncanonical_address (data &PAGE_MASK , vcpu))
I don't think this is correct. MSRs that contain an address usually only fault on a non-canonical value and do the non-canonical check regardless of mode. E.g. VM-Enter's consistency checks on SYSENTER_E{I,S}P only care about a canonical address and are not dependent on mode, and SYSENTER itself states that bits 63:32 are ignored in 32-bit mode. I assume the same is true here.
If that is indeed the case, what about adding these to the common canonical check in __kvm_set_msr()? That'd cut down on the boilerplate here and might make it easier to audit KVM's canonical checks.
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +12
-0+return false; + else if ((index == MSR_IA32_PL0_SSP || + index == MSR_IA32_PL1_SSP || + index == MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (data & CET_MSR_RSVD_BITS_1)) + return false; + }else { +if (msr->index ==MSR_IA32_INT_SSP_TAB ) +return false; + else if ((index == MSR_IA32_U_CET || + index == MSR_IA32_S_CET ||
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1710
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (high_word & ~0ul)) + return false; + } + +return true; +}
This helper seems like overkill, e.g. it's filled with index-specific checks, but is called from code that has already switched on the index. Open coding the individual checks is likely more readable and would require less code, especially if the canonical checks are cleaned up.
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1718
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+ +static bool cet_msr_access_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u64 kvm_xss; +u32 index = msr->index; + +if (is_guest_mode (vcpu)) +return false;
I may have missed this in an earlier discussion, does CET not support nesting?
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1729
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +11
-0+ + kvm_xss =kvm_supported_xss (); + +switch (index) { + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return false; +if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; + }else {
This looks wrong, WRMSR from the guest only checks CPUID, it doesn't check kvm_xss.
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1740
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +11
-0+if (index ==MSR_IA32_PL3_SSP ) { +if (!(kvm_xss &XFEATURE_MASK_CET_USER )) +return false; + }else { +if (!(kvm_xss &XFEATURE_MASK_CET_KERNEL )) +return false; + } + } +break ; +case MSR_IA32_U_CET : +case MSR_IA32_S_CET :
Rather than bundle everything in a single access_allowed() helper, it might
be easier to have separate helpers for each class of MSR. Except for theguest_mode() check, there's no overlap between the classes.
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1750
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+if (!boot_cpu_has (X86_FEATURE_SHSTK ) && + !boot_cpu_has (X86_FEATURE_IBT )) +return false; + +if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) && + !guest_cpuid_has (vcpu,X86_FEATURE_IBT )) +return false; + }else if (index ==MSR_IA32_U_CET && + !(kvm_xss &XFEATURE_MASK_CET_USER ))
Same comment about guest not checking kvm_xss.
Quoted 97 lines
arch/x86/kvm/vmx/vmx.c +63
-0+return false; +break ; +case MSR_IA32_INT_SSP_TAB : +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return false; + +if (!msr->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; +break ; +default : +return false; + } +return true; +}/* * Reads an msr value (of 'msr_index') into 'pdata'. * Returns 0 on success, non-0 otherwise. @@ -1788,6 +1880,26 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_S_CET ); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_INTR_SSP_TABLE ); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (MSR_IA32_U_CET , msr_info->data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (msr_info->index, msr_info->data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))@@ -2039,6 +2151,34 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_S_CET , data); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_INTR_SSP_TABLE , data); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (MSR_IA32_U_CET , data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (msr_info->index, data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))arch/x86/kvm/x86.c +3
-0@@ -1143,6 +1143,9 @@ static u32 msrs_to_save[] = {MSR_IA32_RTIT_ADDR1_A ,MSR_IA32_RTIT_ADDR1_B ,MSR_IA32_RTIT_ADDR2_A ,MSR_IA32_RTIT_ADDR2_B ,MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B , +MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_S_CET , +MSR_IA32_PL0_SSP ,MSR_IA32_PL1_SSP ,MSR_IA32_PL2_SSP , + MSR_IA32_PL3_SSP, MSR_IA32_INT_SSP_TAB, };static unsigned num_msrs_to_save;Signature · 1 line
2.17.2
Re: [PATCH v8 7/7] KVM: X86: Add user-space access interface for CET MSRs
Quoted 22 lines · Sean Christopherson
Quoted 21 lines · Yang Weijiang
There're two different places storing Guest CET states, states managed with XSAVES/XRSTORS, as restored/saved
in previous patch, can be read/write directly from/to the MSRs. For those stored in VMCS fields, they're access via vmcs_read/vmcs_write.arch/x86/kvm/vmx/vmx.c +14
-0+#define CET_MSR_RSVD_BITS_1 0x3 +#define CET_MSR_RSVD_BITS_2 (0xF << 6) + +static bool cet_msr_write_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u32 index = msr->index; +u64 data = msr->data; +u32 high_word = data >>32 ; + +if ((index ==MSR_IA32_U_CET || index ==MSR_IA32_S_CET ) && + (data &CET_MSR_RSVD_BITS_2 )) +return false; + +if (is_64_bit_mode (vcpu)) {
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1688
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+if (is_noncanonical_address (data &PAGE_MASK , vcpu))I don't think this is correct. MSRs that contain an address usually only fault on a non-canonical value and do the non-canonical check regardless of mode. E.g. VM-Enter's consistency checks on SYSENTER_E{I,S}P only care about a canonical address and are not dependent on mode, and SYSENTER itself states that bits 63:32 are ignored in 32-bit mode. I assume the same is true here.
The spec. reads like this: Must be machine canonical when written on
parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are reserved and must be 0.
Quoted 23 lines
If that is indeed the case, what about adding these to the common canonical check in
__kvm_set_msr()? That'd cut down on the boilerplate here and might make it easier to audit KVM's canonical checks.Quoted 19 lines
arch/x86/kvm/vmx/vmx.c +19
-0+return false; + else if ((index == MSR_IA32_PL0_SSP || + index == MSR_IA32_PL1_SSP || + index == MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (data & CET_MSR_RSVD_BITS_1)) + return false; + }else { +if (msr->index ==MSR_IA32_INT_SSP_TAB ) +return false; +else if ((index ==MSR_IA32_U_CET || + index ==MSR_IA32_S_CET || + index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index ==MSR_IA32_PL3_SSP ) && + (high_word & ~0ul )) +return false; + }
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1710
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+ +return true; +}This helper seems like overkill, e.g. it's filled with index-specific checks, but is called from code that has already switched on the index. Open coding the individual checks is likely more readable and would require less code, especially if the canonical checks are cleaned up.
I'm afraid if the checks are not wrapped in a helper, there're many repeat checking-code, that's why I'm using a wrapper.
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1718
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+ +static bool cet_msr_access_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u64 kvm_xss; +u32 index = msr->index; + +if (is_guest_mode (vcpu)) +return false;I may have missed this in an earlier discussion, does CET not support nesting?
I don't want to make CET avaible to nested guest at time being, first to make it available to L1 guest first. So I need to avoid exposing any CET CPUID/MSRs to a nested guest.
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6
-0+ + kvm_xss =kvm_supported_xss (); + +switch (index) { + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!boot_cpu_has (X86_FEATURE_SHSTK ))
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1729
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +5
-0+return false; +if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; + }else {This looks wrong, WRMSR from the guest only checks CPUID, it doesn't check
kvm_xss.
OOPs, I need to add the check, thank you!
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +7
-0+if (index ==MSR_IA32_PL3_SSP ) { +if (!(kvm_xss &XFEATURE_MASK_CET_USER )) +return false; + }else { +if (!(kvm_xss &XFEATURE_MASK_CET_KERNEL )) +return false; + }
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1740
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +4
-0+ } +break ; +case MSR_IA32_U_CET : +case MSR_IA32_S_CET :Rather than bundle everything in a single
access_allowed()helper, it might
be easier to have separate helpers for each class of MSR. Except for theguest_mode()check, there's no overlap between the classes.
Sure, let me double check the code.
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +4
-0+if (!boot_cpu_has (X86_FEATURE_SHSTK ) && + !boot_cpu_has (X86_FEATURE_IBT )) +return false; +
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1750
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6
-0+if (!msr->host_initiated) { +if (!guest_cpuid_has (vcpu,X86_FEATURE_SHSTK ) && + !guest_cpuid_has (vcpu,X86_FEATURE_IBT )) +return false; + }else if (index ==MSR_IA32_U_CET && + !(kvm_xss &XFEATURE_MASK_CET_USER ))Same comment about guest not checking
kvm_xss.
OK.
Quoted 97 lines
Quoted 97 lines
arch/x86/kvm/vmx/vmx.c +63
-0+return false; +break ; +case MSR_IA32_INT_SSP_TAB : +if (!boot_cpu_has (X86_FEATURE_SHSTK )) +return false; + +if (!msr->host_initiated && + !guest_cpuid_has (vcpu,X86_FEATURE_SHSTK )) +return false; +break ; +default : +return false; + } +return true; +}/* * Reads an msr value (of 'msr_index') into 'pdata'. * Returns 0 on success, non-0 otherwise. @@ -1788,6 +1880,26 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_S_CET ); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; + msr_info->data =vmcs_readl (GUEST_INTR_SSP_TABLE ); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (MSR_IA32_U_CET , msr_info->data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +rdmsrl (msr_info->index, msr_info->data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))@@ -2039,6 +2151,34 @@ 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_S_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_S_CET , data); +break ; +case MSR_IA32_INT_SSP_TAB : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +vmcs_writel (GUEST_INTR_SSP_TABLE , data); +break ; +case MSR_IA32_U_CET : +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (MSR_IA32_U_CET , data); +break ; + case MSR_IA32_PL0_SSP ... MSR_IA32_PL3_SSP: +if (!cet_msr_access_allowed (vcpu, msr_info)) +return 1 ; +if (!cet_msr_write_allowed (vcpu, msr_info)) +return 1 ; +wrmsrl (msr_info->index, data); +break ;case MSR_TSC_AUX :if (!msr_info->host_initiated && !guest_cpuid_has (vcpu,X86_FEATURE_RDTSCP ))arch/x86/kvm/x86.c +3
-0@@ -1143,6 +1143,9 @@ static u32 msrs_to_save[] = {MSR_IA32_RTIT_ADDR1_A ,MSR_IA32_RTIT_ADDR1_B ,MSR_IA32_RTIT_ADDR2_A ,MSR_IA32_RTIT_ADDR2_B ,MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B , +MSR_IA32_XSS ,MSR_IA32_U_CET ,MSR_IA32_S_CET , +MSR_IA32_PL0_SSP ,MSR_IA32_PL1_SSP ,MSR_IA32_PL2_SSP , + MSR_IA32_PL3_SSP, MSR_IA32_INT_SSP_TAB, };static unsigned num_msrs_to_save;Signature · 1 line
2.17.2
Re: [PATCH v8 7/7] KVM: X86: Add user-space access interface for CET MSRs
Quoted 31 lines · Yang Weijiang
Quoted 22 lines · Sean Christopherson
Quoted 21 lines · Yang Weijiang
There're two different places storing Guest CET states, states managed with XSAVES/XRSTORS, as restored/saved
in previous patch, can be read/write directly from/to the MSRs. For those stored in VMCS fields, they're access via vmcs_read/vmcs_write.arch/x86/kvm/vmx/vmx.c +14
-0+#define CET_MSR_RSVD_BITS_1 0x3 +#define CET_MSR_RSVD_BITS_2 (0xF << 6) + +static bool cet_msr_write_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u32 index = msr->index; +u64 data = msr->data; +u32 high_word = data >>32 ; + +if ((index ==MSR_IA32_U_CET || index ==MSR_IA32_S_CET ) && + (data &CET_MSR_RSVD_BITS_2 )) +return false; + +if (is_64_bit_mode (vcpu)) {Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1688
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+if (is_noncanonical_address (data &PAGE_MASK , vcpu))I don't think this is correct. MSRs that contain an address usually only fault on a non-canonical value and do the non-canonical check regardless of mode. E.g. VM-Enter's consistency checks on SYSENTER_E{I,S}P only care about a canonical address and are not dependent on mode, and SYSENTER itself states that bits 63:32 are ignored in 32-bit mode. I assume the same is true here.
Quoted 3 lines
The spec. reads like this: Must be machine canonical when written on parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are reserved and must be 0.
Yes, that agrees with me. The key word is "support", i.e. "on parts that support 64 bit mode" means "on parts with CPUID.0x80000001.EDX.LM=1."
The reason the architecture works this way is that unless hardware clears the MSRs on transition from 64->32, bits 63:32 need to be ignored on the way out instead of being validated on the way in, e.g. software writes a 64-bit value to the MSR and then transitions to 32-bit mode. Clearing the MSRs would be painful, slow and error prone, so it's easier for hardware to simply ignore bits 63:32 in 32-bit mode.
Quoted 32 lines
Quoted 23 lines
If that is indeed the case, what about adding these to the common canonical check in
__kvm_set_msr()? That'd cut down on the boilerplate here and might make it easier to audit KVM's canonical checks.Quoted 19 lines
arch/x86/kvm/vmx/vmx.c +19
-0+return false; + else if ((index == MSR_IA32_PL0_SSP || + index == MSR_IA32_PL1_SSP || + index == MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (data & CET_MSR_RSVD_BITS_1)) + return false; + }else { +if (msr->index ==MSR_IA32_INT_SSP_TAB ) +return false; +else if ((index ==MSR_IA32_U_CET || + index ==MSR_IA32_S_CET || + index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index ==MSR_IA32_PL3_SSP ) && + (high_word & ~0ul )) +return false; + }Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1710
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3
-0+ +return true; +}This helper seems like overkill, e.g. it's filled with index-specific checks, but is called from code that has already switched on the index. Open coding the individual checks is likely more readable and would require less code, especially if the canonical checks are cleaned up.
Quoted 2 lines
I'm afraid if the checks are not wrapped in a helper, there're many repeat checking-code, that's why I'm using a wrapper.
But you're adding almost as much, if not more, code to re-split the checks in this helper.
Quoted 15 lines
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1718
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+ +static bool cet_msr_access_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u64 kvm_xss; +u32 index = msr->index; + +if (is_guest_mode (vcpu)) +return false;I may have missed this in an earlier discussion, does CET not support nesting?
I don't want to make CET avaible to nested guest at time being, first to make it available to L1 guest first. So I need to avoid exposing any CET CPUID/MSRs to a nested guest.
Re: [PATCH v8 7/7] KVM: X86: Add user-space access interface for CET MSRs
Quoted 39 lines · Sean Christopherson
Quoted 31 lines · Yang Weijiang
Quoted 22 lines · Sean Christopherson
Quoted 21 lines · Yang Weijiang
There're two different places storing Guest CET states, states managed with XSAVES/XRSTORS, as restored/saved
in previous patch, can be read/write directly from/to the MSRs. For those stored in VMCS fields, they're access via vmcs_read/vmcs_write.arch/x86/kvm/vmx/vmx.c +14
-0+#define CET_MSR_RSVD_BITS_1 0x3 +#define CET_MSR_RSVD_BITS_2 (0xF << 6) + +static bool cet_msr_write_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u32 index = msr->index; +u64 data = msr->data; +u32 high_word = data >>32 ; + +if ((index ==MSR_IA32_U_CET || index ==MSR_IA32_S_CET ) && + (data &CET_MSR_RSVD_BITS_2 )) +return false; + +if (is_64_bit_mode (vcpu)) {Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1688
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+if (is_noncanonical_address (data &PAGE_MASK , vcpu))I don't think this is correct. MSRs that contain an address usually only fault on a non-canonical value and do the non-canonical check regardless of mode. E.g. VM-Enter's consistency checks on SYSENTER_E{I,S}P only care about a canonical address and are not dependent on mode, and SYSENTER itself states that bits 63:32 are ignored in 32-bit mode. I assume the same is true here.
Quoted 3 lines
The spec. reads like this: Must be machine canonical when written on parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are reserved and must be 0.
Yes, that agrees with me. The key word is "support", i.e. "on parts that support 64 bit mode" means "on parts with CPUID.0x80000001.EDX.LM=1."
Quoted 7 lines
The reason the architecture works this way is that unless hardware clears the MSRs on transition from 64->32, bits 63:32 need to be ignored on the way out instead of being validated on the way in, e.g. software writes a 64-bit value to the MSR and then transitions to 32-bit mode. Clearing the MSRs would be painful, slow and error prone, so it's easier for hardware to simply ignore bits 63:32 in 32-bit mode.
Make sense, I'll move the canonical check up to kvm_set_msr() like other MSRs, thanks!
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
If that is indeed the case, what about adding these to the common canonical check in
__kvm_set_msr()? That'd cut down on the boilerplate here and might make it easier to audit KVM's canonical checks.Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +12
-0+return false; + else if ((index == MSR_IA32_PL0_SSP || + index == MSR_IA32_PL1_SSP || + index == MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (data & CET_MSR_RSVD_BITS_1)) + return false; + }else { +if (msr->index ==MSR_IA32_INT_SSP_TAB ) +return false; + else if ((index == MSR_IA32_U_CET || + index == MSR_IA32_S_CET ||arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1710
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+ index ==MSR_IA32_PL0_SSP || + index ==MSR_IA32_PL1_SSP || + index ==MSR_IA32_PL2_SSP || + index == MSR_IA32_PL3_SSP) && + (high_word & ~0ul)) + return false; + } + +return true; +}This helper seems like overkill, e.g. it's filled with index-specific checks, but is called from code that has already switched on the index. Open coding the individual checks is likely more readable and would require less code, especially if the canonical checks are cleaned up.
Quoted 6 lines
Quoted 2 lines
I'm afraid if the checks are not wrapped in a helper, there're many repeat checking-code, that's why I'm using a wrapper.
But you're adding almost as much, if not more, code to re-split the checks in this helper.
Sure, thanks!
Quoted 15 lines
Quoted 15 lines
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c static int vmx_get_msr_feature(struct kvm_msr_entry *msr) · line 1718
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+ +static bool cet_msr_access_allowed (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +u64 kvm_xss; +u32 index = msr->index; + +if (is_guest_mode (vcpu)) +return false;I may have missed this in an earlier discussion, does CET not support nesting?
I don't want to make CET avaible to nested guest at time being, first to make it available to L1 guest first. So I need to avoid exposing any CET CPUID/MSRs to a nested guest.
Quoted 10 lines · Yang Weijiang
Control-flow Enforcement Technology (CET) provides protection against Return/Jump-Oriented Programming (ROP/JOP) attack. It includes two
sub-features: Shadow Stack (SHSTK) and Indirect Branch Tracking (IBT).KVM change is required to support guest CET feature.
This patch serial implemented CET related CPUID/XSAVES enumeration, MSRs and vmentry/vmexit configuration etc.so that guest kernel can setup CET runtime infrastructure based on them. Some CET MSRs and related feature flags used reference the definitions in kernel patchset.
Quoted 3 lines
CET kernel patches is here:
https://lkml.org/lkml/2019/8/13/1110
https://lkml.org/lkml/2019/8/13/1109
Is there a git tree with all of them against v5.5-rc1 (so all three series)? I tried your github tree: https://github.com/yyu168/linux_cet.git #cet but sadly that does not apply against 5.5-rc1 :-(
Thanks!
Quoted 15 lines · Konrad Rzeszutek Wilk
Quoted 10 lines · Yang Weijiang
Control-flow Enforcement Technology (CET) provides protection against Return/Jump-Oriented Programming (ROP/JOP) attack. It includes two
sub-features: Shadow Stack (SHSTK) and Indirect Branch Tracking (IBT).KVM change is required to support guest CET feature.
This patch serial implemented CET related CPUID/XSAVES enumeration, MSRs and vmentry/vmexit configuration etc.so that guest kernel can setup CET runtime infrastructure based on them. Some CET MSRs and related feature flags used reference the definitions in kernel patchset.Quoted 3 lines
CET kernel patches is here:
https://lkml.org/lkml/2019/8/13/1110
https://lkml.org/lkml/2019/8/13/1109
Quoted 5 lines
Is there a git tree with all of them against v5.5-rc1 (so all three series)? I tried your github tree: https://github.com/yyu168/linux_cet.git #cet but sadly that does not apply against 5.5-rc1 :-(
Thanks!
Hi,
The CET patch includes two parts: one from kernel side the other from KVM, the kernel patch in github is maintained by my peer, he'll rebase it to the latest kernel tree shortly after resolve some issues. Thank you for having interest!