KVM: x86: Advertise EferLmsleUnsupported to userspace
PATCH 1/1 KVM: x86: Advertise EferLmsleUnsupported to userspace
CPUID.80000008H:EBX.EferLmsleUnsupported[bit 20] is a defeature bit. When this bit is clear, EFER.LMSLE is supported. When this bit is set, EFER.LMLSE is unsupported. KVM has never supported EFER.LMSLE, so it cannot support a 0-setting of this bit.
Set the bit in KVM_GET_SUPPORTED_CPUID to advertise the unavailability of EFER.LMSLE to userspace.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kvm/cpuid.c | 1 + 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -338,6 +338,7 @@#define X86_FEATURE_AMD_STIBP (13*32+15)/* Single Thread Indirect Branch Predictors */ #define X86_FEATURE_AMD_STIBP_ALWAYS_ON (13*32+17)/* Single Thread Indirect Branch Predictors always-on preferred */ #define X86_FEATURE_AMD_IBRS_SAME_MODE (13*32+19)/* Indirect Branch Restricted Speculation same mode protection*/ +#define X86_FEATURE_EFER_LMSLE_MBZ (13*32+20)/* EFER.LMSLE must be zero */ #define X86_FEATURE_AMD_PPIN (13*32+23)/* "amd_ppin" Protected Processor Inventory Number */ #define X86_FEATURE_AMD_SSBD (13*32+24)/* Speculative Store Bypass Disable */ #define X86_FEATURE_VIRT_SSBD (13*32+25)/* "virt_ssbd" Virtualized Speculative Store Bypass Disable */
arch/x86/kvm/cpuid.c +1 -0
@@ -1096,6 +1096,7 @@ void kvm_set_cpu_caps(void)F (AMD_STIBP ),F (AMD_STIBP_ALWAYS_ON ),F (AMD_IBRS_SAME_MODE ), +EMULATED_F (EFER_LMSLE_MBZ ),F (AMD_PSFD ), F(AMD_IBPB_RET), );
Signature · 1 line
2.51.0.570.gb178f27e6d-goog
Quoted 7 lines · Jim Mattson
CPUID.80000008H:EBX.EferLmsleUnsupported[bit 20] is a defeature bit. When this bit is clear, EFER.LMSLE is supported. When this bit is set, EFER.LMLSE is unsupported. KVM has never supported EFER.LMSLE, so it cannot support a 0-setting of this bit.
Set the bit in
KVM_GET_SUPPORTED_CPUIDto advertise the unavailability of EFER.LMSLE to userspace.
It seems like KVM allows setting EFER.LMSLE when nested SVM is enabled:
https://elixir.bootlin.com/linux/v6.17/source/arch/x86/kvm/svm/svm.c#L5354
It goes back to commit eec4b140c924 ("KVM: SVM: Allow EFER.LMSLE to be set with nested svm"), the commit log says it was needed for the SLES11 version of Xen 4.0 to boot with nested SVM. Maybe that's no longer the case.
Should KVM advertise EferLmsleUnsupported if it allows setting EFER.LMSLE in some cases?
Quoted 34 lines
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kvm/cpuid.c | 1 + 2 files changed, 2 insertions(+)arch/x86/include/asm/cpufeatures.h +1
-0@@ -338,6 +338,7 @@#define X86_FEATURE_AMD_STIBP (13*32+15)/* Single Thread Indirect Branch Predictors */ #define X86_FEATURE_AMD_STIBP_ALWAYS_ON (13*32+17)/* Single Thread Indirect Branch Predictors always-on preferred */ #define X86_FEATURE_AMD_IBRS_SAME_MODE (13*32+19)/* Indirect Branch Restricted Speculation same mode protection*/ +#define X86_FEATURE_EFER_LMSLE_MBZ (13*32+20)/* EFER.LMSLE must be zero */ #define X86_FEATURE_AMD_PPIN (13*32+23)/* "amd_ppin" Protected Processor Inventory Number */ #define X86_FEATURE_AMD_SSBD (13*32+24)/* Speculative Store Bypass Disable */ #define X86_FEATURE_VIRT_SSBD (13*32+25)/* "virt_ssbd" Virtualized Speculative Store Bypass Disable */ arch/x86/kvm/cpuid.c +1
-0@@ -1096,6 +1096,7 @@ void kvm_set_cpu_caps(void)F (AMD_STIBP ),F (AMD_STIBP_ALWAYS_ON ),F (AMD_IBRS_SAME_MODE ), +EMULATED_F (EFER_LMSLE_MBZ ),F (AMD_PSFD ), F(AMD_IBPB_RET), );Signature · 1 line
2.51.0.570.gb178f27e6d-goog
Quoted 13 lines · Yosry Ahmed
Quoted 7 lines · Jim Mattson
CPUID.80000008H:EBX.EferLmsleUnsupported[bit 20] is a defeature bit. When this bit is clear, EFER.LMSLE is supported. When this bit is set, EFER.LMLSE is unsupported. KVM has never supported EFER.LMSLE, so it cannot support a 0-setting of this bit.
Set the bit in
KVM_GET_SUPPORTED_CPUIDto advertise the unavailability of EFER.LMSLE to userspace.It seems like KVM allows setting EFER.LMSLE when nested SVM is enabled:
https://elixir.bootlin.com/linux/v6.17/source/arch/x86/kvm/svm/svm.c#L5354
Quoted 7 lines
It goes back to commit
eec4b140c924("KVM: SVM: Allow EFER.LMSLE to be set with nested svm"), the commit log says it was needed for the SLES11 version of Xen 4.0 to boot with nested SVM. Maybe that's no longer the case.Should KVM advertise EferLmsleUnsupported if it allows setting EFER.LMSLE in some cases?
I don't think KVM should allow setting the bit if it's not going to actually implement long mode segment limits. That seems like a security issue. The L1 hypervisor thinks that the L2 guest will not be able to access memory above the segment limit, but if there are no segment limit checks, then L2 will be able to access that memory.
It should be possible for KVM to implement long mode segment limits on hardware that supports the feature, but offering the feature on hardware that doesn't support it is infeasible.
Do we really want to implement long mode segment limits in KVM, given that modern CPUs don't support the feature?
Quoted 34 lines
Quoted 34 lines
Signed-off-by Jim Mattson <[email protected]>--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kvm/cpuid.c | 1 + 2 files changed, 2 insertions(+)arch/x86/include/asm/cpufeatures.h +1
-0@@ -338,6 +338,7 @@#define X86_FEATURE_AMD_STIBP (13*32+15)/* Single Thread Indirect Branch Predictors */ #define X86_FEATURE_AMD_STIBP_ALWAYS_ON (13*32+17)/* Single Thread Indirect Branch Predictors always-on preferred */ #define X86_FEATURE_AMD_IBRS_SAME_MODE (13*32+19)/* Indirect Branch Restricted Speculation same mode protection*/ +#define X86_FEATURE_EFER_LMSLE_MBZ (13*32+20)/* EFER.LMSLE must be zero */ #define X86_FEATURE_AMD_PPIN (13*32+23)/* "amd_ppin" Protected Processor Inventory Number */ #define X86_FEATURE_AMD_SSBD (13*32+24)/* Speculative Store Bypass Disable */ #define X86_FEATURE_VIRT_SSBD (13*32+25)/* "virt_ssbd" Virtualized Speculative Store Bypass Disable */ arch/x86/kvm/cpuid.c +1
-1@@ -1096,6 +1096,7 @@ void kvm_set_cpu_caps(void)F (AMD_STIBP ),F (AMD_STIBP_ALWAYS_ON ),F (AMD_IBRS_SAME_MODE ), +EMULATED_F (EFER_LMSLE_MBZ ),F (AMD_PSFD ), F(AMD_IBPB_RET), ); --2.51.0.570.gb178f27e6d-goog
Quoted 19 lines · Jim Mattson
Quoted 18 lines · Yosry Ahmed
Quoted 7 lines · Jim Mattson
CPUID.80000008H:EBX.EferLmsleUnsupported[bit 20] is a defeature bit. When this bit is clear, EFER.LMSLE is supported. When this bit is set, EFER.LMLSE is unsupported. KVM has never supported EFER.LMSLE, so it cannot support a 0-setting of this bit.
Set the bit in
KVM_GET_SUPPORTED_CPUIDto advertise the unavailability of EFER.LMSLE to userspace.It seems like KVM allows setting EFER.LMSLE when nested SVM is enabled:
https://elixir.bootlin.com/linux/v6.17/source/arch/x86/kvm/svm/svm.c#L5354It goes back to commit
eec4b140c924("KVM: SVM: Allow EFER.LMSLE to be set with nested svm"), the commit log says it was needed for the SLES11 version of Xen 4.0 to boot with nested SVM. Maybe that's no longer the case.
Quoted 8 lines
Quoted 2 lines
Should KVM advertise EferLmsleUnsupported if it allows setting EFER.LMSLE in some cases?
I don't think KVM should allow setting the bit if it's not going to actually implement long mode segment limits. That seems like a security issue. The L1 hypervisor thinks that the L2 guest will not be able to access memory above the segment limit, but if there are no segment limit checks, then L2 will be able to access that memory.
Yeah this sounds bad.
On HW that supports EFER.LMSLE we're mostly virtualizing it, but not entirely.
On HW that doesn't support it, we're not advertising EferLmsleUnsupported and we allow the guest to set EFER.LMSLE, so the VMRUN will fail with the invalid EFER (or immediate #VMexit?), and the VM will crash.
Quoted 4 lines
It should be possible for KVM to implement long mode segment limits on hardware that supports the feature, but offering the feature on hardware that doesn't support it is infeasible.
I am not sure what we can and cannot do in terms of backward compatibility. Ideally we'd stop allowing EFER.LMSLE completely, but maybe we cannot break old users like that on HW that supports EFER.LMSLE.
Quoted 3 lines
Do we really want to implement long mode segment limits in KVM, given that modern CPUs don't support the feature?
Probably not, and it causes crashes today anyway so I don't think anyone depends on it.