Relax canonical checks on some arch msrs
v3 cover Relax canonical checks on some arch msrs
Recently we came up upon a failure where likely the guest writes 0xff4547ceb1600000 to MSR_KERNEL_GS_BASE and later on, qemu sets this value via KVM_PUT_MSRS, and is rejected by the kernel, likely due to not being canonical in 4 level paging.
One of the way to trigger this is to make the guest enter SMM, which causes paging to be disabled, which SMM bios re-enables but not the whole 5 level. MSR_KERNEL_GS_BASE on the other hand continues to contain old value.
I did some reverse engineering and to my surprise I found out that both Intel and AMD indeed ignore CR4.LA57 when doing canonical checks on this and other msrs and/or other arch registers (like GDT base) which contain linear addresses.
V2: addressed a very good feedback from Chao Gao. Thanks!
V3: also fix the nested VMX, and also fix theMSR_IA32_SYSENTER_EIP / MSR_IA32_SYSENTER_ESP
Best regards,
Maxim Levitsky
Maxim Levitsky (4):
KVM: x86: relax canonical check for some x86 architectural msrs
KVM: x86: add X86_FEATURE_LA57 to governed_features
KVM: nVMX: relax canonical checks on some x86 registers in vmx host
state
KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and
MSR_GS_BASE
arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/governed_features.h | 1 + arch/x86/kvm/svm/svm.c | 12 ++++++++++++ arch/x86/kvm/vmx/nested.c | 30 +++++++++++++++++++++++------- arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++-- 5 files changed, 65 insertions(+), 9 deletions(-)
Signature · 1 line
2.40.1
PATCH v3 1/4 KVM: x86: relax canonical check for some x86 architectural msrs
Several x86's arch msrs contain a linear base, and thus must contain a canonical address.
This includes FS/GS base, addresses used for SYSENTER and SYSCALL instructions and probably more.
As it turns out, when x86 architecture was updated to 5 level paging / 57 bit virtual addresses, these MSRs were allowed to contain a full 57 bit address regardless of the state of CR4.LA57.
The main reason behind this decision is that 5 level paging, and even paging itself can be temporarily disabled (e.g by SMM entry) leaving non canonical values in these fields.
Another reason is that OS might prepare these fields before it switches to 5 level paging.
Experemental tests on a Sapphire Rapids CPU and on a Zen4 CPU confirm this behavior.
In addition to that, the Intel ISA extension manual mentions that this might be the architectural behavior:
Architecture Instruction Set Extensions and Future Features Programming Reference [1], Chapter 6.4:
"CANONICALITY CHECKING FOR DATA ADDRESSES WRITTEN TO CONTROL REGISTERS AND MSRS"
"In Processors that support LAM continue to require the addresses written tocontrol registers or MSRs to be 57-bit canonical if the processor supports 5-level paging or 48-bit canonical if it supports only 4-level paging"
[1]: https://cdrdv2.intel.com/v1/dl/getContent/671368
--- arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +27 -2
@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; + +/* +* Most x86 arch MSR values which contain linear addresses like +* segment bases, addresses that are used in instructions (e.g SYSENTER), +* have static canonicality checks, +* size of whose depends only on CPU's support for 5-level +* paging, rather than state of CR4.LA57. +* +* In addition to that, some of these MSRS are directly passed +* to the guest (e.g MSR_KERNEL_GS_BASE) thus even if the guest +* doen't have LA57 enabled in its CPUID, for consistency with +* CPUs' ucode, it is better to pivot the check around host +* support for 5 level paging. +*/ + +static u8 max_host_supported_virt_addr_bits (void ) +{ +return kvm_cpu_cap_has (X86_FEATURE_LA57 ) ?57 :48 ; +} + +static bool is_host_noncanonical_msr_value (u64 la) +{ +return !__is_canonical_address (la,max_host_supported_virt_addr_bits ()); +} +static struct kmem_cache *x86_emulator_cache;/*
@@ -1829,7 +1854,7 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,case MSR_KERNEL_GS_BASE :case MSR_CSTAR :case MSR_LSTAR : -if (is_noncanonical_address (data, vcpu)) +if (is_host_noncanonical_msr_value (data))return 1 ;break ;case MSR_IA32_SYSENTER_EIP :
@@ -1846,7 +1871,7 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,* value, and that something deterministic happens if the guest * invokes 64-bit SYSENTER. */ - data =__canonical_address (data,vcpu_virt_addr_bits (vcpu)); + data =__canonical_address (data,max_host_supported_virt_addr_bits ());break ;case MSR_TSC_AUX :if (!kvm_is_supported_user_return_msr (MSR_TSC_AUX ))
Signature · 1 line
2.40.1
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 307
Quoted 11 lines · Maxim Levitsky
arch/x86/kvm/x86.c +3
-0@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; + +/* +* Most x86 arch MSR values which contain linear addresses like
Is it most, or all? I'm guessing all?
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 309
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+* segment bases, addresses that are used in instructions (e.g SYSENTER), +* have static canonicality checks,
Weird and early line breaks.
How about this?
/*
* The canonicality checks for MSRs that hold linear addresses, e.g. segment
* bases, SYSENTER targets, etc., are static, in the sense that they are based
* on CPU _support_ for 5-level paging, not the state of CR4.LA57.
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 317
Quoted 8 lines
arch/x86/kvm/x86.c +8
-0+* size of whose depends only on CPU's support for 5-level +* paging, rather than state of CR4.LA57. +* +* In addition to that, some of these MSRS are directly passed +* to the guest (e.g MSR_KERNEL_GS_BASE) thus even if the guest +* doen't have LA57 enabled in its CPUID, for consistency with +* CPUs' ucode, it is better to pivot the check around host +* support for 5 level paging.
I think we should elaborate on why it's better. It only takes another line or two, and that way we don't forget the edge cases that make properly emulating guest CPUID a bad idea.
* This creates a virtualization hole where a guest writes to passthrough MSRs
* may incorrectly succeed if the CPU supports LA57, but the vCPU does not
* (because hardware has no awareness of guest CPUID). Do not try to plug this
* hole, i.e. emulate the behavior for intercepted accesses, as injecting #GP
* depending on whether or not KVM happens to emulate a WRMSR would result in
* non-deterministic behavior, and could even allow L2 to crash L1, e.g. if L1
* passes through an MSR to L2, and then tries to save+restore L2's value. */
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 319
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+ +static u8 max_host_supported_virt_addr_bits (void )
Any objection to dropping the "supported", i.e. going with max_host_virt_addr_bits()? Mostly to shorten the name, but also because "supported" suggests there's software involvement, e.g. the max supported by the kernel/KVM, which isn't the case.
If you're ok with the above, I'll fixup when applying.
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
Quoted 30 lines · Sean Christopherson
On Thu, Aug 15, 2024, Maxim Levitsky wrote:
How about this?/*
* The canonicality checks for MSRs that hold linear addresses, e.g. segment
* bases, SYSENTER targets, etc., are static, in the sense that they are based
* on CPU _support_ for 5-level paging, not the state of CR4.LA57.Quoted 8 lines
+ * size of whose depends only on CPU's support for 5-level
+ * paging, rather than state of CR4.LA57.
+ *
+ * In addition to that, some of these MSRS are directly passed
+ * to the guest (e.gMSR_KERNEL_GS_BASE) thus even if the guest
+ * doen't have LA57 enabled in its CPUID, for consistency with
+ * CPUs' ucode, it is better to pivot the check around host
+ * support for 5 level paging.I think we should elaborate on why it's better. It only takes another line or two, and that way we don't forget the edge cases that make properly emulating guest CPUID a bad idea.
* This creates a virtualization hole where a guest writes to passthrough MSRs
* may incorrectly succeed if the CPU supports LA57, but the vCPU does not
* (because hardware has no awareness of guest CPUID). Do not try to plug this
* hole, i.e. emulate the behavior for intercepted accesses, as injecting #GP
* depending on whether or not KVM happens to emulate a WRMSR would result in
* non-deterministic behavior, and could even allow L2 to crash L1, e.g. if L1
* passes through an MSR to L2, and then tries to save+restore L2's value. */
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static u8 max_host_supported_virt_addr_bits(void)Any objection to dropping the "supported", i.e. going with
max_host_virt_addr_bits()? Mostly to shorten the name, but also because "supported" suggests there's software involvement, e.g. the max supported by the kernel/KVM, which isn't the case.If you're ok with the above, I'll fixup when applying.
I take that back, I think we're going to need a v4 (see patch 3).
Re: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
In reply to Sean ChristophersonУ пт, 2024-08-16 у 14:49 -0700, Sean Christopherson пише:
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines · Maxim Levitsky
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/x86.c +0
-0@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 307
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0sizeof(kvm_vcpu_stats_desc), };arch/x86/kvm/x86.c +3
-0+ +/* +* Most x86 arch MSR values which contain linear addresses like Is it most, or all? I'm guessing all?
I can't be sure that all of them are like that - there could be some outliers that behave differently.
One of the things my work at Intel taught me is that there is nothing consistent in x86 spec, anything is possible and nothing can be assumed.
I dealt only with those msrs, that KVM checks for canonicality, therefore I use the word 'most'. There could be other msrs that are not known to me and/or to KVM.
I can write 'some' if you prefer.
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ * segment bases, addresses that are used in instructions (e.g SYSENTER),
+ * have static canonicality checks,Weird and early line breaks.
How about this?
/*
* The canonicality checks for MSRs that hold linear addresses, e.g. segment
* bases, SYSENTER targets, etc., are static, in the sense that they are based
* on CPU _support_ for 5-level paging, not the state of CR4.LA57.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
+ * size of whose depends only on CPU's support for 5-level
+ * paging, rather than state of CR4.LA57.
+ *
+ * In addition to that, some of these MSRS are directly passed
+ * to the guest (e.gMSR_KERNEL_GS_BASE) thus even if the guest
+ * doen't have LA57 enabled in its CPUID, for consistency with
+ * CPUs' ucode, it is better to pivot the check around host
+ * support for 5 level paging.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
I think we should elaborate on why it's better. It only takes another line or two, and that way we don't forget the edge cases that make properly emulating guest CPUID a bad idea.
OK, will do.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
* This creates a virtualization hole where a guest writes to passthrough MSRs
* may incorrectly succeed if the CPU supports LA57, but the vCPU does not
* (because hardware has no awareness of guest CPUID). Do not try to plug this
* hole, i.e. emulate the behavior for intercepted accesses, as injecting #GP
* depending on whether or not KVM happens to emulate a WRMSR would result in
* non-deterministic behavior, and could even allow L2 to crash L1, e.g. if L1
* passes through an MSR to L2, and then tries to save+restore L2's value. */
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/x86/kvm/x86.c
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+ +static u8 max_host_supported_virt_addr_bits(void)Any objection to dropping the "supported", i.e. going with
max_host_virt_addr_bits()? Mostly to shorten the name, but also because "supported" suggests there's software involvement, e.g. the max supported by the kernel/KVM, which isn't the case.
Doesn't matter to me.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
If you're ok with the above, I'll fixup when applying.
Best regards,
Maxim Levitsky
Re: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
У вт, 2024-08-20 у 15:13 +0300, [email protected] пише:
Quoted 18 lines
У пт, 2024-08-16 у 14:49 -0700, Sean Christopherson пише:
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines · Maxim Levitsky
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/x86.c +0
-0@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c const struct kvm_stats_header kvm_vcpu_stats_header = { · line 307
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0sizeof(kvm_vcpu_stats_desc), };arch/x86/kvm/x86.c +3
-0+ +/* +* Most x86 arch MSR values which contain linear addresses like Is it most, or all? I'm guessing all?
I can't be sure that all of them are like that - there could be some outliers that behave differently.
Quoted 7 lines
One of the things my work at Intel taught me is that there is nothing consistent in x86 spec, anything is possible and nothing can be assumed.
I dealt only with those msrs, that KVM checks for canonicality, therefore I use the word 'most'. There could be other msrs that are not known to me and/or to KVM.
I can write 'some' if you prefer.
Hi,
So I did some more reverse engineering and indeed, 'some' is the right word:
I audited all places in KVM which check an linear address for being canonical and this is what I found:
- MSR_IA32_BNDCFGS - since it is not supported on CPUs with 5 level paging, its not possible to know what the hardware does.
- MSR_IA32_DS_AREA: - Ignores CR4.LA57 as expected. Tested by booting into kernel with 5 level paging disabled and then using userspace 'wrmsr' program to set this msr.
I attached the bash script that I used
- MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: - Exactly the same story, but for some reason the host doesn't suport (not even read) from MSR_IA32_RTIT_ADDR2_*, MSR_IA32_RTIT_ADDR3_*. Probably the system is not new enough for these.
- invpcid instruction. It is exposed to the guest without interception (unless !npt or !ept), and yes, it works just fine on 57-canonical address without CR4.LA57 set....
- invvpid - this one belongs to VMX set, so technically its for nesting although it is run by L1, it is always emulated by KVM, but still executed on the host just with different vpid, so I booted the host without 5 level paging, and patched KVM to avoid canonical check.
Also 57-canonical adddress worked just fine, and fully non canonical address failed. and gave a warning in 'invvpid_error'
Should I fix all of these too?
About fixing the emulator this is what see:
emul_is_noncanonical_address
__load_segment_descriptor
load_segment_descriptor
em_lldt
em_ltr
em_lgdt_lidt
While em_lgdt_lidt should be easy to fix because it calls emul_is_noncanonical_address directly, the em_lldt, em_ltr will be harder because these use load_segment_descriptor which calls __load_segment_descriptor which in turn is also used for emulating of far jumps/calls/rets, for which I do believe that canonical check does respect CR4.LA57, but can't be sure either.
It is possible that far jumps/calls/rets also ignore CR4.LA57, and instead set RIP to non canonical instruction, and then on first fetch, #GP happens.
I'll setup another unit test for this. RIP of the #GP will determine if the instruction failed or the next fetch.
Best regards,
Maxim Levitsky
Quoted 54 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+ * segment bases, addresses that are used in instructions (e.g SYSENTER),
+ * have static canonicality checks,Weird and early line breaks.
How about this?
/*
* The canonicality checks for MSRs that hold linear addresses, e.g. segment
* bases, SYSENTER targets, etc., are static, in the sense that they are based
* on CPU _support_ for 5-level paging, not the state of CR4.LA57.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
+ * size of whose depends only on CPU's support for 5-level
+ * paging, rather than state of CR4.LA57.
+ *
+ * In addition to that, some of these MSRS are directly passed
+ * to the guest (e.gMSR_KERNEL_GS_BASE) thus even if the guest
+ * doen't have LA57 enabled in its CPUID, for consistency with
+ * CPUs' ucode, it is better to pivot the check around host
+ * support for 5 level paging.Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
I think we should elaborate on why it's better. It only takes another line or two, and that way we don't forget the edge cases that make properly emulating guest CPUID a bad idea.
OK, will do.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
* This creates a virtualization hole where a guest writes to passthrough MSRs
* may incorrectly succeed if the CPU supports LA57, but the vCPU does not
* (because hardware has no awareness of guest CPUID). Do not try to plug this
* hole, i.e. emulate the behavior for intercepted accesses, as injecting #GP
* depending on whether or not KVM happens to emulate a WRMSR would result in
* non-deterministic behavior, and could even allow L2 to crash L1, e.g. if L1
* passes through an MSR to L2, and then tries to save+restore L2's value. */Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
arch/x86/kvm/x86.c
Quoted 2 lines
arch/x86/kvm/x86.c +2
-0+ +static u8 max_host_supported_virt_addr_bits(void)Any objection to dropping the "supported", i.e. going with
max_host_virt_addr_bits()? Mostly to shorten the name, but also because "supported" suggests there's software involvement, e.g. the max supported by the kernel/KVM, which isn't the case.Doesn't matter to me.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
If you're ok with the above, I'll fixup when applying.
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
Quoted 27 lines · On Wed, Aug 21, 2024, [email protected] wrote:
У вт, 2024-08-20 у 15:13 +0300, [email protected] пише:
Quoted 26 lines
У пт, 2024-08-16 у 14:49 -0700, Sean Christopherson пише:
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 6 lines · Maxim Levitsky
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/x86.c +0
-0@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/x86.c
Quoted 6 lines
arch/x86/kvm/x86.c +0
-0sizeof(kvm_vcpu_stats_desc), };arch/x86/kvm/x86.c +3
-0+ +/* +* Most x86 arch MSR values which contain linear addresses like Is it most, or all? I'm guessing all?
I can't be sure that all of them are like that - there could be some outliers that behave differently.
One of the things my work at Intel taught me is that there is nothing consistent in x86 spec, anything is possible and nothing can be assumed.
I dealt only with those msrs, that KVM checks for canonicality, therefore I use the word 'most'. There could be other msrs that are not known to me and/or to KVM.
Quoted 6 lines
Quoted 1 line
I can write 'some' if you prefer.
Hi,
So I did some more reverse engineering and indeed, 'some' is the right word:
Is it? IIUC, we have yet to find an MSR that honors, CR4.LA57, i.e. it really is "all", so far as we know.
Quoted 5 lines
I audited all places in KVM which check an linear address for being canonical and this is what I found:
-
MSR_IA32_BNDCFGS- since it is not supported on CPUs with 5 level paging, its not possible to know what the hardware does.
Heh, yeah, but I would be very surprised if MSR_IA32_BNDCFGS didn't follow all other system-ish MSRs.
Quoted 4 lines
-
MSR_IA32_DS_AREA: - Ignores CR4.LA57 as expected. Tested by booting into kernel with 5 level paging disabled and then using userspace 'wrmsr' program to set this msr. I attached the bash script that I used
Quoted 8 lines
-
MSR_IA32_RTIT_ADDR0_A...MSR_IA32_RTIT_ADDR3_B: - Exactly the same story, but for some reason the host doesn't suport (not even read) from MSR_IA32_RTIT_ADDR2_*, MSR_IA32_RTIT_ADDR3_*. Probably the system is not new enough for these.- invpcid instruction. It is exposed to the guest without interception (unless !npt or !ept), and yes, it works just fine on 57-canonical address without CR4.LA57 set....
Did you verify the behavior for the desciptor, the target, or both? I assume the memory operand, i.e. the address of the _descriptor_, honors CR4.LA57, but the target within the descriptor does not.
If that assumption is correct, then this code in vm_mmu_invalidate_addr() is broken, as KVM actually needs to do a TLB flush if the address is canonical for the vCPU model, even if it's non-canonical for the current vCPU state.
/* It's actually a GPA for vcpu->arch.guest_mmu. */
if (mmu != &vcpu->arch.guest_mmu) {
/* INVLPG on a non-canonical address is a NOP according to the SDM. */
if (is_noncanonical_address(addr, vcpu))
return;
kvm_x86_call(flush_tlb_gva)(vcpu, addr);
}Assuming INVPCID is indicative of how INVLPG and INVVPID behave (they are nops if the _target_ is non-canonical), then the code is broken for INVLPG and INVVPID.
And I think it's probably a safe assumption that a TLB flush is needed. E.g. the primary (possible only?) use case for INVVPID with a linear address is to flush TLB entries for a specific GVA=>HPA mapping, and honoring CR4.LA57 would prevent shadowing 5-level paging with a hypervisor that is using 4-level paging for itself.
Quoted 9 lines
- invvpid - this one belongs to VMX set, so technically its for nesting although it is run by L1, it is always emulated by KVM, but still executed on the host just with different vpid, so I booted the host without 5 level paging, and patched KVM to avoid canonical check.
Also 57-canonical adddress worked just fine, and fully non canonical address failed. and gave a warning in '
invvpid_error'Should I fix all of these too?
Yeah, though I believe we're at the point where we need to figure out a better naming scheme, because usage of what is currently is_noncanonical_address() will be, by far, in the minority.
Hmm, actually, what if we extend the X86EMUL_F_* flags that were added for LAM (and eventually for LASS) to handle the non-canonical checks? That's essentially what LAM does already, there are just a few more flavors we now need to handle.
E.g. I think we just need flags for MSRs and segment/DT bases. The only (or at least, most) confusing thing is that LAM/LASS do NOT apply to INVPLG accesses, but are exempt from LA57. But that's an arch oddity, not a problem KVM can solve.
arch/x86/kvm/kvm_emulate.h +2 -0
@@ -94,6 +94,8 @@ struct x86_instruction_info {#define X86EMUL_F_FETCH BIT(1)#define X86EMUL_F_IMPLICIT BIT(2)#define X86EMUL_F_INVLPG BIT(3) +#define X86EMUL_F_MSR BIT(4) +#define X86EMUL_F_BASE BIT(5)struct x86_emulate_ops {void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
---
And then with that, we can do the below, and have emul_is_noncanonical_address() redirect to is_noncanonical_address() instead of being an open coded equivalent.
---
static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
{
return kvm_is_cr4_bit_set(vcpu, X86_CR4_LA57) ? 57 : 48;
}static inline u8 max_host_virt_addr_bits(void)
{
return kvm_cpu_cap_has(X86_FEATURE_LA57) ? 57 : 48;
}static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu,
unsigned int flags)
{
if (flags & (X86EMUL_F_INVLPG | X86EMUL_F_MSR | X86EMUL_F_DT_LOAD))
return !__is_canonical_address(la, max_host_virt_addr_bits());
else
return !__is_canonical_address(la, vcpu_virt_addr_bits(vcpu));
}---
That will make it _much_ harder to incorrectly use is_noncanonical_address(), as all callers will be forced to specify the emulation type, i.e. there is no automatic, implied default type.
Line lengths could get annoying, but with per-type flags, we could do selectively add a few wrappers, e.g.
---
static inline bool is_noncanonical_msr_address(u64 la, struct kvm_vcpu *vcpu)
{
return is_noncanonical_address(la, vcpu, X86EMUL_F_MSR);
}static inline bool is_noncanonical_base_address(u64 la, struct kvm_vcpu *vcpu)
{
return is_noncanonical_address(la, vcpu, X86EMUL_F_BASE);
}static inline bool is_noncanonical_invlpg_address(u64 la, struct kvm_vcpu *vcpu)
{
return is_noncanonical_address(la, vcpu, X86EMUL_F_INVLPG);
}---
We wouldn't want wrapper for everything, e.g. to minimize the risk of creating a de factor implicit default, but I think those three, and maybe a code/fetch variant, will cover all but a few users.
Quoted 8 lines
About fixing the emulator this is what see:
emul_is_noncanonical_address
__load_segment_descriptor
load_segment_descriptor
em_lldt
em_ltr
Quoted 6 lines
em_lgdt_lidtWhile
em_lgdt_lidtshould be easy to fix because it callsemul_is_noncanonical_addressdirectly,
Those don't need to be fixed, they are validating the memory operand, not the base of the descriptor, i.e. aren't exempt from CR4.LA57.
Quoted 5 lines
the
em_lldt,em_ltrwill be harder
because these useload_segment_descriptorwhich calls__load_segment_descriptorwhich in turn is also used for emulating of far jumps/calls/rets, for which I do believe that canonical check does respect CR4.LA57, but can't be sure either.
I'm fairly certain this is a non-issue. CS, DS, ES, and SS have a fixed base of '0' in 64-bit mode, i.e. are completely exempt from canonical checks because the base address is always ignored. And while FS and GS do have base addresses, the segment descriptors themselves can only load 32-bit bases, i.e. _can't_ generate non-canonical addresses.
There _is_ an indirect canonical check on the _descriptor_ (the actual descriptor pointed at by the selector, not the memory operand). The SDM is calls this case out in the LFS/LDS docs:
If the FS, or GS register is being loaded with a non-NULL segment selector and any of the following is true: the segment selector index is not within descriptor table limits, the memory address of the descriptor is non-canonical
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and similarly, when using a CALL GATE, the far transfer docs say:
If the segment descriptor from a 64-bit call gate is in non-canonical space.
And given that those implicit accesses are not subjected to LAM/LASS, I strongly suspect they honor CR4.LA57. So the explicit emul_is_noncanonical_address() check in __load_segment_descriptor() needs to be tagged X86EMUL_F_BASE, but otherwise it all should Just Work (knock wood).
Quoted 2 lines
It is possible that far jumps/calls/rets also ignore CR4.LA57, and instead set RIP to non canonical instruction, and then on first fetch, #GP happens.
I doubt this is the case for the final RIP check, especially since ucode does check vmcs.HOST_RIP against vmcs.HOST_CR4.LA57, but it's worth testing to confirm.
Quoted 2 lines
I'll setup another unit test for this. RIP of the #GP will determine if the instruction failed or the next fetch.
Re: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
У ср, 2024-08-21 у 09:04 -0700, Sean Christopherson пише:
Quoted 30 lines
Quoted 30 lines · On Wed, Aug 21, 2024, [email protected] wrote:
Quoted 29 lines
Quoted 29 lines
У вт, 2024-08-20 у 15:13 +0300, [email protected] пише:
Quoted 27 lines
Quoted 27 lines
У пт, 2024-08-16 у 14:49 -0700, Sean Christopherson пише:
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
On Thu, Aug 15, 2024, Maxim Levitsky wrote:
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ce7c00894f32..2e83f7d74591 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -302,6 +302,31 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {
sizeof(kvm_vcpu_stats_desc),
};
+
+/*
+ * Most x86 arch MSR values which contain linear addresses likeIs it most, or all? I'm guessing all?
I can't be sure that all of them are like that - there could be some outliers that behave differently.
One of the things my work at Intel taught me is that there is nothing consistent in x86 spec, anything is possible and nothing can be assumed.
I dealt only with those msrs, that KVM checks for canonicality, therefore I use the word 'most'. There could be other msrs that are not known to me and/or to KVM.
I can write 'some' if you prefer.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Hi,
So I did some more reverse engineering and indeed, 'some' is the right word:
Is it? IIUC, we have yet to find an MSR that honors, CR4.LA57, i.e. it really is "all", so far as we know.
This is more or less what I meant to say: I meant to say that I verified only 'some' of the msrs/instructions, and for others I can't know, although it's likely that indeed our theory is correct.
Quoted 19 lines
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
I audited all places in KVM which check an linear address for being canonical and this is what I found:
-
MSR_IA32_BNDCFGS- since it is not supported on CPUs with 5 level paging, its not possible to know what the hardware does.Heh, yeah, but I would be very surprised if
MSR_IA32_BNDCFGSdidn't follow all other system-ish MSRs.Quoted 9 lines
Quoted 9 lines
-
MSR_IA32_DS_AREA: - Ignores CR4.LA57 as expected. Tested by booting into kernel with 5 level paging disabled and then using userspace 'wrmsr' program to set this msr. I attached the bash script that I used-
MSR_IA32_RTIT_ADDR0_A...MSR_IA32_RTIT_ADDR3_B: - Exactly the same story, but for some reason the host doesn't suport (not even read) from MSR_IA32_RTIT_ADDR2_*, MSR_IA32_RTIT_ADDR3_*. Probably the system is not new enough for these.
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
- invpcid instruction. It is exposed to the guest without interception (unless !npt or !ept), and yes, it works just fine on 57-canonical address without CR4.LA57 set....
Did you verify the behavior for the desciptor, the target, or both? I assume the memory operand, i.e. the address of the _descriptor_, honors CR4.LA57, but the target within the descriptor does not.
I verified only the target address. I assume that memory fetches do have to honor the CR.LA57.
Quoted 14 lines
Quoted 14 lines
If that assumption is correct, then this code in
vm_mmu_invalidate_addr()is broken, as KVM actually needs to do a TLB flush if the address is canonical for the vCPU model, even if it's non-canonical for the current vCPU state./* It's actually a GPA for vcpu->arch.guest_mmu. */
if (mmu !=&vcpu->arch.guest_mmu) {
/* INVLPG on a non-canonical address is a NOP according to the SDM. */ if (is_noncanonical_address(addr, vcpu))
return;kvm_x86_call(flush_tlb_gva)(vcpu, addr); }
Quoted 7 lines
Quoted 7 lines
Assuming INVPCID is indicative of how INVLPG and INVVPID behave (they are nops if the _target_ is non-canonical), then the code is broken for INVLPG and INVVPID.
And I think it's probably a safe assumption that a TLB flush is needed. E.g. the primary (possible only?) use case for INVVPID with a linear address is to flush TLB entries for a specific GVA=>HPA mapping, and honoring CR4.LA57 would prevent shadowing 5-level paging with a hypervisor that is using 4-level paging for itself.
I also think so.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
- invvpid - this one belongs to VMX set, so technically its for nesting although it is run by L1, it is always emulated by KVM, but still executed on the host just with different vpid, so I booted the host without 5 level paging, and patched KVM to avoid canonical check.
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Also 57-canonical adddress worked just fine, and fully non canonical address failed. and gave a warning in '
invvpid_error'Should I fix all of these too?
Yeah, though I believe we're at the point where we need to figure out a better naming scheme, because usage of what is currently
is_noncanonical_address()will be, by far, in the minority.
Yes, I will take this into account.
Quoted 33 lines
Quoted 33 lines
Hmm, actually, what if we extend the X86EMUL_F_* flags that were added for LAM (and eventually for LASS) to handle the non-canonical checks? That's essentially what LAM does already, there are just a few more flavors we now need to handle.
E.g. I think we just need flags for MSRs and segment/DT bases. The only (or at least, most) confusing thing is that LAM/LASS do NOT apply to INVPLG accesses, but are exempt from LA57. But that's an arch oddity, not a problem KVM can solve.
arch/x86/kvm/kvm_emulate.h +2
-0@@ -94,6 +94,8 @@ struct x86_instruction_info { #define X86EMUL_F_FETCH BIT(1) #define X86EMUL_F_IMPLICIT BIT(2) #define X86EMUL_F_INVLPG BIT(3) +#define X86EMUL_F_MSR BIT(4) +#define X86EMUL_F_BASE BIT(5)
structx86_emulate_ops{
void (*vm_bugged)(structx86_emulate_ctxt*ctxt);
---And then with that, we can do the below, and have
emul_is_noncanonical_address()redirect tois_noncanonical_address()instead of being an open coded equivalent.---
static inline u8vcpu_virt_addr_bits(struct kvm_vcpu *vcpu){
returnkvm_is_cr4_bit_set(vcpu, X86_CR4_LA57)? 57 : 48; }
Quoted 4 lines
Quoted 4 lines
static inline u8
max_host_virt_addr_bits(void)
{
returnkvm_cpu_cap_has(X86_FEATURE_LA57)? 57 : 48; }
This is a good name for this function, thanks.
Quoted 9 lines
Quoted 9 lines
static inline bool
is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu, unsigned int flags)
{
if (flags & (X86EMUL_F_INVLPG|X86EMUL_F_MSR|X86EMUL_F_DT_LOAD)) return !__is_canonical_address(la,max_host_virt_addr_bits()); else
return !__is_canonical_address(la,vcpu_virt_addr_bits(vcpu)); }
This can work in principle, although are you OK with using these emulator flags outside of the emulator code?
I am asking because the is_noncanonical_address is used in various places across KVM.
Quoted 42 lines
Quoted 42 lines
---
That will make it _much_ harder to incorrectly use
is_noncanonical_address(), as all callers will be forced to specify the emulation type, i.e. there is no automatic, implied default type.Line lengths could get annoying, but with per-type flags, we could do selectively add a few wrappers, e.g.
---
static inline boolis_noncanonical_msr_address(u64 la, struct kvm_vcpu *vcpu){
returnis_noncanonical_address(la, vcpu, X86EMUL_F_MSR); }static inline bool
is_noncanonical_base_address(u64 la, struct kvm_vcpu *vcpu){
returnis_noncanonical_address(la, vcpu, X86EMUL_F_BASE); }static inline bool
is_noncanonical_invlpg_address(u64 la, struct kvm_vcpu *vcpu){
returnis_noncanonical_address(la, vcpu, X86EMUL_F_INVLPG); }
---We wouldn't want wrapper for everything, e.g. to minimize the risk of creating a de factor implicit default, but I think those three, and maybe a code/fetch variant, will cover all but a few users.
Quoted 12 lines
Quoted 12 lines
About fixing the emulator this is what see:
emul_is_noncanonical_address
__load_segment_descriptor
load_segment_descriptor
em_lldt
em_ltrem_lgdt_lidt
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
While
em_lgdt_lidtshould be easy to fix because it callsemul_is_noncanonical_addressdirectly,Those don't need to be fixed, they are validating the memory operand, not the base of the descriptor, i.e. aren't exempt from CR4.LA57.
Are you sure?
em_lgdt_lidt reads its memory operands (and checks that it is canonical through linearize) with read_descriptor and that is fine because this is memory fetch, and then it checks that the base address within the operand is canonical.
This check needs to be updated, as it is possible to load non canonical GDT and IDT base via lgdt/lidt (I tested this).
For em_lldt, em_ltr, the check on the system segment descriptor base is in __load_segment_descriptor:
...
ret = linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3)); if (ret != X86EMUL_CONTINUE)
return ret;
if (emul_is_noncanonical_address(get_desc_base(&seg_desc) | ((u64)base3 << 32), ctxt))
return emulate_gp(ctxt, err_code);
...
64 bases are possible only for system segments, which are TSS, LDT, and call gates/IDT descriptors.
We don't emulate IDT fetches in protected mode, and as I found out the hard way after I wrote a unit test to do a call through a call gate, the emulator doesn't support call gates either)
Thus I can safely patch __load_segment_descriptor.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
the
em_lldt,em_ltrwill be harder
because these useload_segment_descriptorwhich calls__load_segment_descriptorwhich in turn is also used for emulating of far jumps/calls/rets, for which I do believe that canonical check does respect CR4.LA57, but can't be sure either.
Quoted 5 lines
Quoted 5 lines
I'm fairly certain this is a non-issue. CS, DS, ES, and SS have a fixed base of '0' in 64-bit mode, i.e. are completely exempt from canonical checks because the base address is always ignored. And while FS and GS do have base addresses, the segment descriptors themselves can only load 32-bit bases, i.e. _can't_ generate non-canonical addresses.
Yes - all data segments in 64 bit mode, still have 32 bit base in the GDT/LDT, so this is indeed not an issue.
Quoted 10 lines
Quoted 10 lines
There _is_ an indirect canonical check on the _descriptor_ (the actual descriptor pointed at by the selector, not the memory operand). The SDM is calls this case out in the LFS/LDS docs:
If the FS, or GS register is being loaded with a non-NULL segment selector and any of the following is true: the segment selector index is not within descriptor table limits, the memory address of the descriptor is non-canonical ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I tested that both ltr and lldt do ignore CR4.LA57 by loading from a descriptor which had a non canonical value and CR4.LA57 clear.
Quoted 3 lines
Quoted 3 lines
and similarly, when using a CALL GATE, the far transfer docs say:
If the segment descriptor from a 64-bit call gate is in non-canonical space.
For the academic reference, when loading offset from a call gate descriptor, the CPU does the canonical check, and it does honour the CR4.LA57.
A variation of the below test can prove it:
void set_callgate_entry(u16 sel, void *offset, int dpl)
{
idt_entry_t *e = (idt_entry_t *)&gdt[sel >> 3];
set_desc_entry(e, sizeof *e, offset, read_cs(), 12, dpl);
}int main(int argc, char **argv)
{
u64 gate_offset = (u64)&&gate_entry_point;
// toggle below to check various cases:
//setup_5level_page_table();
//gate_offset = 0xff4547ceb1600000;
set_callgate_entry(FIRST_SPARE_SEL, (void *)gate_offset, 0);
struct { u64 offset; u16 sel; } call_target =
{ 0 /* ignored */,
FIRST_SPARE_SEL};
// Perform the far call
asm volatile goto (
// KVM_FEP
".byte 0x48; rex.w\n" // optional, just for fun
"lcall *%0\n"
"jmp %l1\n"
:
: "m" (call_target)
:
: return_address
);
gate_entry_point:
printf("Call gate worked\n");
asm volatile (
"retfq\n"
);
return_address:
printf("Exit from call gate worked\n");
return 0;
}Quoted 11 lines
Quoted 11 lines
And given that those implicit accesses are not subjected to LAM/LASS, I strongly suspect they honor CR4.LA57. So the explicit
emul_is_noncanonical_address()check in__load_segment_descriptor()needs to be taggedX86EMUL_F_BASE, but otherwise it all should Just Work (knock wood).Quoted 2 lines
Quoted 2 lines
It is possible that far jumps/calls/rets also ignore CR4.LA57, and instead set RIP to non canonical instruction, and then on first fetch, #GP happens.
I doubt this is the case for the final RIP check, especially since ucode does check vmcs.HOST_RIP against vmcs.HOST_CR4.LA57, but it's worth testing to confirm.
See above.
So now I'll try to do a v4 of the patches with all of the feedback included. Thanks!
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
I'll setup another unit test for this. RIP of the #GP will determine if the instruction failed or the next fetch.
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
Quoted 10 lines · On Fri, Aug 23, 2024, [email protected] wrote:
У ср, 2024-08-21 у 09:04 -0700, Sean Christopherson пише:
Quoted 8 lines
Quoted 8 lines
static inline bool
is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu, unsigned int flags)
{
if (flags & (X86EMUL_F_INVLPG|X86EMUL_F_MSR|X86EMUL_F_DT_LOAD)) return !__is_canonical_address(la,max_host_virt_addr_bits()); else
return !__is_canonical_address(la,vcpu_virt_addr_bits(vcpu)); }
Quoted 2 lines
This can work in principle, although are you OK with using these emulator flags outside of the emulator code?
Yep, they're already used in VMX's vmx_get_untagged_addr().
Quoted 1 line
I am asking because the
is_noncanonical_addressis used in various places across KVM.
...
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
We wouldn't want wrapper for everything, e.g. to minimize the risk of creating a de factor implicit default, but I think those three, and maybe a code/fetch variant, will cover all but a few users.
Quoted 12 lines
Quoted 12 lines
About fixing the emulator this is what see:
emul_is_noncanonical_address
__load_segment_descriptor
load_segment_descriptor
em_lldt
em_ltrem_lgdt_lidt
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
While
em_lgdt_lidtshould be easy to fix because it callsemul_is_noncanonical_addressdirectly,Those don't need to be fixed, they are validating the memory operand, not the base of the descriptor, i.e. aren't exempt from CR4.LA57.
Are you sure?
Nope. Re-reading what I wrote, I have no idea what code past me was looking at.
I even contradicted myself later on:
So the explicit emul_is_noncanonical_address() check in __load_segment_descriptor() needs to be tagged X86EMUL_F_BASE, but otherwise it all should Just Work (knock wood).
so yeah, ignore this.
Quoted 25 lines
em_lgdt_lidtreads its memory operands (and checks that it is canonical through linearize) withread_descriptorand that is fine because this is memory fetch, and then it checks that the base address within the operand is canonical.This check needs to be updated, as it is possible to load non canonical GDT and IDT base via lgdt/lidt (I tested this).
For
em_lldt,em_ltr, the check on the system segment descriptor base is in__load_segment_descriptor:...
ret =linear_read_system(ctxt, desc_addr+8, &base3, sizeof(base3)); if (ret !=X86EMUL_CONTINUE)
return ret;
if (emul_is_noncanonical_address(get_desc_base(&seg_desc) | ((u64)base3 << 32), ctxt))
returnemulate_gp(ctxt, err_code);...
64 bases are possible only for system segments, which are TSS, LDT, and call gates/IDT descriptors.
Quoted 5 lines
We don't emulate IDT fetches in protected mode, and as I found out the hard way after I wrote a unit test to do a call through a call gate, the emulator doesn't support call gates either)
Thus I can safely patch
__load_segment_descriptor.
Agreed.
And thinking more about how this is likely implemented in ucode, this is probably working as intended. The the SDM gives CPUs a _lot_ of leeway:
In 64-bit mode, an address is considered to be in canonical form if address bits 63 through to the most-significant implemented bit by the microarchitecture are set to either all ones or all zeros.
as does the APM:
Long mode defines 64 bits of virtual address, but implementations of the AMD64 architecture may support fewer bits of virtual address. Although implementations might not use all 64 bits of the virtual address, they check bits 63 through the most-significant implemented bit to see if those bits are all zeros or all ones. An address that complies with this property is said to be in canonical address form. If a virtual-memory reference is not in canonical form, the implementation causes a general-protection exception or stack fault.
I suspect that CR4.LA_57 is only consulted when the CPU is actually consuming the address, i.e. is (or is about to, e.g. for code fetches) generating a memory access.
Heh, and for MPX, the SDM kinda sorta confirms that LA57 is ignored, though I doubt the author of this section intended their words to be taken this way :-)
WRMSR to BNDCFGS will #GP if any of the reserved bits of BNDCFGS is not zero or if the base address of the bound directory is not canonical. XRSTOR of BNDCFGU ignores the reserved bits and does not fault if any is non-zero; similarly, it ignores the upper bits of the base address of the bound directory and sign-extends the highest implemented bit of the linear address to guarantee the canonicality ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ of this address.
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
There _is_ an indirect canonical check on the _descriptor_ (the actual descriptor pointed at by the selector, not the memory operand). The SDM is calls this case out in the LFS/LDS docs:
If the FS, or GS register is being loaded with a non-NULL segment selector and any of the following is true: the segment selector index is not within descriptor table limits, the memory address of the descriptor is non-canonical ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I tested that both ltr and lldt do ignore CR4.LA57 by loading from a descriptor which had a non canonical value and CR4.LA57 clear.
Re: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
Quoted 10 lines · Sean Christopherson
Heh, and for MPX, the SDM kinda sorta confirms that LA57 is ignored, though I doubt the author of this section intended their words to be taken this way :-)
WRMSR to BNDCFGS will #GP if any of the reserved bits of BNDCFGS is not zero or if the base address of the bound directory is not canonical. XRSTOR of BNDCFGU ignores the reserved bits and does not fault if any is non-zero; similarly, it ignores the upper bits of the base address of the bound directory and sign-extends the highest implemented bit of the linear address to guarantee the canonicality ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ of this address.
I don't believe there was ever a CPU that supported both MPX and LA57. :)
Late to the party, as usual, but my interest was piqued by the failure of KVM_SET_NESTED_STATE prior to v6.13 if L1 had CR4.LA57 set, L2 did not, and the VMCS12.HOST_GSBASE had a kernel address > 48 bits wide. The canonicalization checks for the *host* state in the VMCS were done using the guest's CR4.LA57.
Shouldn't this series have been cc'd to stable?
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: x86: relax canonical check for some x86 architectural msrs
Quoted 14 lines · Jim Mattson
Quoted 10 lines · Sean Christopherson
Heh, and for MPX, the SDM kinda sorta confirms that LA57 is ignored, though I doubt the author of this section intended their words to be taken this way :-)
WRMSR to BNDCFGS will #GP if any of the reserved bits of BNDCFGS is not zero or if the base address of the bound directory is not canonical. XRSTOR of BNDCFGU ignores the reserved bits and does not fault if any is non-zero; similarly, it ignores the upper bits of the base address of the bound directory and sign-extends the highest implemented bit of the linear address to guarantee the canonicality ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ of this address.
I don't believe there was ever a CPU that supported both MPX and LA57. :)
Quoted 7 lines
Late to the party, as usual, but my interest was piqued by the failure of
KVM_SET_NESTED_STATEprior to v6.13 if L1 had CR4.LA57 set, L2 did not, and the VMCS12.HOST_GSBASE had a kernel address > 48 bits wide. The canonicalization checks for the *host* state in the VMCS were done using the guest's CR4.LA57.Shouldn't this series have been cc'd to stable?
Yes :-(
That's my fault. I balked at the size/scope of the changes, but in hindsight that was a mistake.
PATCH v3 2/4 KVM: x86: add X86_FEATURE_LA57 to governed_features
In reply to Maxim LevitskyGuest support for X86_FEATURE_LA57 will need to be tested to determine canonical width check for nested VMX VM entries.
--- arch/x86/kvm/cpuid.c | 2 ++ arch/x86/kvm/governed_features.h | 1 + 2 files changed, 3 insertions(+)
arch/x86/kvm/cpuid.c +2 -0
@@ -373,6 +373,8 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)if (allow_gbpages)kvm_governed_feature_set (vcpu,X86_FEATURE_GBPAGES ); +kvm_governed_feature_check_and_set (vcpu,X86_FEATURE_LA57 ); + best =kvm_find_cpuid_entry (vcpu,1 );if (best && apic) {if (cpuid_entry_has (best,X86_FEATURE_TSC_DEADLINE_TIMER ))
arch/x86/kvm/governed_features.h +1 -0
@@ -17,6 +17,7 @@ KVM_GOVERNED_X86_FEATURE(PFTHRESHOLD) KVM_GOVERNED_X86_FEATURE(VGIF) KVM_GOVERNED_X86_FEATURE(VNMI) KVM_GOVERNED_X86_FEATURE(LAM) +KVM_GOVERNED_X86_FEATURE(LA57)#undef KVM_GOVERNED_X86_FEATURE#undef KVM_GOVERNED_FEATURE
Signature · 1 line
2.40.1
PATCH v3 3/4 KVM: nVMX: relax canonical checks on some x86 registers in vmx host state
In reply to Maxim LevitskySeveral x86's architecture registers contain a linear base, and thus must contain a canonical address.
This includes segment and segment like bases (FS/GS base, GDT,IDT,LDT,TR), addresses used for SYSENTER and SYSCALL instructions and probably more.
As it turns out, when x86 architecture was updated to 5 level paging / 57 bit virtual addresses, these fields were allowed to contain a full 57 bit address regardless of the state of CR4.LA57.
The main reason behind this decision is that 5 level paging, and even paging itself can be temporarily disabled (e.g by SMM entry) leaving non canonical values in these fields.
Another reason is that OS might prepare these fields before it switches to 5 level paging.
--- arch/x86/kvm/vmx/nested.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/nested.c +23 -7
@@ -2969,6 +2969,22 @@ static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu,return 0 ; } +static bool is_l1_noncanonical_address_static (u64 la,struct kvm_vcpu *vcpu) +{ +u8 max_guest_address_bits =guest_can_use (vcpu,X86_FEATURE_LA57 ) ?57 :48 ; +/* +* Most x86 arch registers which contain linear addresses like +* segment bases, addresses that are used in instructions (e.g SYSENTER), +* have static canonicality checks, +* size of whose depends only on CPU's support for 5-level +* paging, rather than state of CR4.LA57. +* +* In other words the check only depends on the CPU model, +* rather than on runtime state. +*/ +return !__is_canonical_address (la, max_guest_address_bits); +} +static int nested_vmx_check_host_state (struct kvm_vcpu *vcpu,struct vmcs12 *vmcs12) {
@@ -2979,8 +2995,8 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3)))return -EINVAL ; -if (CC (is_noncanonical_address (vmcs12->host_ia32_sysenter_esp, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_ia32_sysenter_eip, vcpu))) +if (CC (is_l1_noncanonical_address_static (vmcs12->host_ia32_sysenter_esp, vcpu)) || +CC (is_l1_noncanonical_address_static (vmcs12->host_ia32_sysenter_eip, vcpu)))return -EINVAL ; if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) &&
@@ -3014,11 +3030,11 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(vmcs12->host_ss_selector == 0 && !ia32e))return -EINVAL ; -if (CC (is_noncanonical_address (vmcs12->host_fs_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_gs_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_gdtr_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_idtr_base, vcpu)) || -CC (is_noncanonical_address (vmcs12->host_tr_base, vcpu)) || +if (CC (is_l1_noncanonical_address_static (vmcs12->host_fs_base, vcpu)) || +CC (is_l1_noncanonical_address_static (vmcs12->host_gs_base, vcpu)) || +CC (is_l1_noncanonical_address_static (vmcs12->host_gdtr_base, vcpu)) || +CC (is_l1_noncanonical_address_static (vmcs12->host_idtr_base, vcpu)) || +CC (is_l1_noncanonical_address_static (vmcs12->host_tr_base, vcpu)) ||CC (is_noncanonical_address (vmcs12->host_rip, vcpu)))return -EINVAL ;
Signature · 1 line
2.40.1
Re: [PATCH v3 3/4] KVM: nVMX: relax canonical checks on some x86 registers in vmx host state
У чт, 2024-08-15 у 15:33 +0300, Maxim Levitsky пише:
Quoted 10 lines
Several x86's architecture registers contain a linear base, and thus must contain a canonical address.
This includes segment and segment like bases (FS/GS base, GDT,IDT,LDT,TR), addresses used for SYSENTER and SYSCALL instructions and probably more.
As it turns out, when x86 architecture was updated to 5 level paging / 57 bit virtual addresses, these fields were allowed to contain a full 57 bit address regardless of the state of CR4.LA57.
Quoted 5 lines
The main reason behind this decision is that 5 level paging, and even paging itself can be temporarily disabled (e.g by SMM entry) leaving non canonical values in these fields.
Another reason is that OS might prepare these fields before it switches to 5 level paging.
Hi,
Note that I haven't included a fix for HOST_RIP. I did today a bare metal check and indeed the microcode does check CR4.LA57, the one that is stored in the vmcs as you suspected.
I add a patch to this patch series with this mostly theoretical fix, when I send a new revision.
Second thing, I kept the canonical check on 'vmcs12->guest_bndcfgs because Intel deprecated this feature and none of CPUs which support 5 level paging support MPX.
Also I think that since this is a guest state field, it might be possible to just remove the check, because the value of this field is copied to vmcs02 and the CPU's microcode should do the same check that KVM does.
Best regards,
Maxim Levitsky
Quoted 61 lines
Signed-off-by Maxim Levitsky <[email protected]>---
arch/x86/kvm/vmx/nested.c | 30 +++++++++++++++++++++++-------1 file changed, 23 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/nested.c +0
-0@@ -2969,6 +2969,22 @@ static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, return 0; }arch/x86/kvm/vmx/nested.c +16
-0+static bool is_l1_noncanonical_address_static (u64 la,struct kvm_vcpu *vcpu) +{ + u8 max_guest_address_bits = guest_can_use(vcpu, X86_FEATURE_LA57) ? 57 : 48; + /* + * Most x86 arch registers which contain linear addresses like + * segment bases, addresses that are used in instructions (e.g SYSENTER), + * have static canonicality checks, + * size of whose depends only on CPU's support for 5-level + * paging, rather than state of CR4.LA57. + * + * In other words the check only depends on the CPU model, + * rather than on runtime state. + */ + return !__is_canonical_address(la, max_guest_address_bits); +} +static int
nested_vmx_check_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12){arch/x86/kvm/vmx/nested.c +0
-0@@ -2979,8 +2995,8 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3))) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) + if (CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_esp, vcpu))|| +CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_eip, vcpu))) return-EINVAL;
if ((vmcs12->vm_exit_controls&VM_EXIT_LOAD_IA32_PAT) &&arch/x86/kvm/vmx/nested.c +0
-0@@ -3014,11 +3030,11 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(vmcs12->host_ss_selector == 0 && !ia32e)) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu))||- CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || - CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || - CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || + if (CC(is_l1_noncanonical_address_static(vmcs12->host_fs_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_gs_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_gdtr_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_idtr_base, vcpu)) ||+
CC(is_l1_noncanonical_address_static(vmcs12->host_tr_base, vcpu))||CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) return-EINVAL;
Sean Christopherson
maintainerRe: [PATCH v3 3/4] KVM: nVMX: relax canonical checks on some x86 registers in vmx host state
Quoted 5 lines · On Fri, Aug 16, 2024, [email protected] wrote:
Quoted 5 lines
Signed-off-by Maxim Levitsky <[email protected]>---
arch/x86/kvm/vmx/nested.c | 30 +++++++++++++++++++++++-------1 file changed, 23 insertions(+), 7 deletions(-)
Quoted 11 lines
arch/x86/kvm/vmx/nested.c static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, · line 2974
Quoted 11 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -2969,6 +2969,22 @@ static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, return 0; }arch/x86/kvm/vmx/nested.c +3
-0+static bool is_l1_noncanonical_address_static (u64 la,struct kvm_vcpu *vcpu) +{ + u8 max_guest_address_bits = guest_can_use(vcpu, X86_FEATURE_LA57) ? 57 : 48;
I don't see any reason to use LA57 support from guest CPUID for the VMCS checks. The virtualization hole exists can't be safely plugged for all cases, so why bother trying to plug it only for some cases?
It'd be very odd that an L1 could set a "bad" value via WRMSR, but then couldn't load that same value on VM-Exit, e.g. if L1 gets the VMCS value by doing RDMSR.
Quoted 33 lines
Quoted 33 lines
+ /*
+ * Most x86 arch registers which contain linear addresses like + * segment bases, addresses that are used in instructions (e.g SYSENTER), + * have static canonicality checks,
+ * size of whose depends only on CPU's support for 5-level
+ * paging, rather than state of CR4.LA57.
+ *
+ * In other words the check only depends on the CPU model,
+ * rather than on runtime state.
+ */
+ return !__is_canonical_address(la,max_guest_address_bits);arch/x86/kvm/vmx/nested.c +2
-0+} +static int
nested_vmx_check_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12){arch/x86/kvm/vmx/nested.c +0
-0@@ -2979,8 +2995,8 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3))) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) + if (CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_esp, vcpu))|| +CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_eip, vcpu))) return-EINVAL;
if ((vmcs12->vm_exit_controls&VM_EXIT_LOAD_IA32_PAT) &&arch/x86/kvm/vmx/nested.c +0
-0@@ -3014,11 +3030,11 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(vmcs12->host_ss_selector == 0 && !ia32e)) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu))||
Quoted 8 lines
Quoted 8 lines
- CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || - CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || - CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || + if (CC(is_l1_noncanonical_address_static(vmcs12->host_fs_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_gs_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_gdtr_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_idtr_base, vcpu)) ||+
CC(is_l1_noncanonical_address_static(vmcs12->host_tr_base, vcpu))||
If loads via LTR, LLDT, and LGDT are indeed exempt, then we need to update emul_is_noncanonical_address() too.
The best idea I have is to have a separate flow for system registers (not a great name, but I can't think of anything better), and the
E.g. s/is_host_noncanonical_msr_value/is_non_canonical_system_reg, and then wire that up to the emulator.
Quoted 4 lines
Quoted 3 lines
CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) return-EINVAL;
Re: [PATCH v3 3/4] KVM: nVMX: relax canonical checks on some x86 registers in vmx host state
У пт, 2024-08-16 у 15:03 -0700, Sean Christopherson пише:
Quoted 6 lines · On Fri, Aug 16, 2024, [email protected] wrote:
Quoted 5 lines
Quoted 5 lines
Signed-off-by Maxim Levitsky <[email protected]>---
arch/x86/kvm/vmx/nested.c | 30 +++++++++++++++++++++++-------1 file changed, 23 insertions(+), 7 deletions(-)
Quoted 15 lines
Quoted 11 lines
arch/x86/kvm/vmx/nested.c
Quoted 11 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -2969,6 +2969,22 @@ static int nested_vmx_check_address_space_size(struct kvm_vcpu *vcpu, return 0; }arch/x86/kvm/vmx/nested.c +3
-0+static bool is_l1_noncanonical_address_static (u64 la,struct kvm_vcpu *vcpu) +{ + u8 max_guest_address_bits = guest_can_use(vcpu, X86_FEATURE_LA57) ? 57 : 48;I don't see any reason to use LA57 support from guest CPUID for the VMCS checks. The virtualization hole exists can't be safely plugged for all cases, so why bother trying to plug it only for some cases?
I also thought like that but then there is another argument:
My idea was that the guest really ought to not put non canonical values if its CPUID doesn't support 5 level paging. There is absolutely no reason for doing so.
If the guest does this though via WRMSR, most of the time the MSR is not intercepted, thus it makes sense to allow this in emulation patch as well, as we discussed to be consistent.
But when VMRESUME/VMLAUNCH instruction, which is *always* emulated, writes those MSRS on VM exit, then I don't see a reason to allow a virtualization hole.
But then as it turns out (I didn't expect that) that instructions like LGDT also don't check CR4.LA57, and these are also passed through, then I guess singling out the VMX instructions is no longer better.
Quoted 40 lines
It'd be very odd that an L1 could set a "bad" value via WRMSR, but then couldn't load that same value on VM-Exit, e.g. if L1 gets the VMCS value by doing RDMSR.
Quoted 36 lines
Quoted 36 lines
+ /*
+ * Most x86 arch registers which contain linear addresses like + * segment bases, addresses that are used in instructions (e.g SYSENTER), + * have static canonicality checks,
+ * size of whose depends only on CPU's support for 5-level
+ * paging, rather than state of CR4.LA57.
+ *
+ * In other words the check only depends on the CPU model,
+ * rather than on runtime state.
+ */
+ return !__is_canonical_address(la,max_guest_address_bits);arch/x86/kvm/vmx/nested.c +2
-0+} +static int
nested_vmx_check_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12){arch/x86/kvm/vmx/nested.c +0
-0@@ -2979,8 +2995,8 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(!kvm_vcpu_is_legal_cr3(vcpu, vmcs12->host_cr3))) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) + if (CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_esp, vcpu))|| +CC(is_l1_noncanonical_address_static(vmcs12->host_ia32_sysenter_eip, vcpu))) return-EINVAL;
if ((vmcs12->vm_exit_controls&VM_EXIT_LOAD_IA32_PAT) &&arch/x86/kvm/vmx/nested.c +0
-0@@ -3014,11 +3030,11 @@ static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, CC(vmcs12->host_ss_selector == 0 && !ia32e)) return -EINVAL;
- if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu))|| -CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu))||- CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || - CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) ||-
CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu))||
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
+ if (
CC(is_l1_noncanonical_address_static(vmcs12->host_fs_base, vcpu))|| +CC(is_l1_noncanonical_address_static(vmcs12->host_gs_base, vcpu))||+ CC(is_l1_noncanonical_address_static(vmcs12->host_gdtr_base, vcpu)) || + CC(is_l1_noncanonical_address_static(vmcs12->host_idtr_base, vcpu)) ||+
CC(is_l1_noncanonical_address_static(vmcs12->host_tr_base, vcpu))||If loads via LTR, LLDT, and LGDT are indeed exempt, then we need to update
emul_is_noncanonical_address()too.
Sadly the answer to this is yes, at least on Intel. I will test on AMD soon, as soon as I grab a Zen4 machine again.
And since these instructions are also all unintercepted, it also makes sense to use host cpuid for them as well.
I attached two kvm unit tests, which I will hopefully polish for publishing soon, which pass with flying colors with this patch series, and unless I made a mistake prove most of my research.
The HOST_RIP field I checked separately by patching the L0 kernel, and observing it either hang/crash or fail VM entry of the first guest.
Best regards,
Maxim Levitsky
Quoted 14 lines
The best idea I have is to have a separate flow for system registers (not a great name, but I can't think of anything better), and the
E.g. s/is_host_noncanonical_msr_value/is_non_canonical_system_reg, and then wire that up to the emulator.
Quoted 3 lines
Quoted 3 lines
CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) return-EINVAL;
From e6d7bd2aa4f185881714a6103e9e672b0dbd12ab Mon Sep 17 00:00:00 2001
From: Maxim Levitsky <[email protected]>
Date: Fri, 16 Aug 2024 12:40:20 +0300
Subject: [PATCH 1/2] Add test for writing canonical values to various msrs
--- lib/x86/asm/setup.h | 1 + lib/x86/processor.h | 5 + lib/x86/vm.h | 1 + x86/Makefile.x86_64 | 1 + x86/cstart64.S | 35 +++++++ x86/msr_canonical.c | 236 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 x86/msr_canonical.c
lib/x86/asm/setup.h +1 -0
@@ -14,6 +14,7 @@ unsigned long setup_tss(u8 *stacktop);efi_status_t setup_efi (efi_bootinfo_t *efi_bootinfo);void setup_5level_page_table (void ); +void setup_4level_page_table (void );#endif /* CONFIG_EFI */ void save_id (void );
lib/x86/processor.h +5 -0
@@ -468,6 +468,11 @@ static inline int rdmsr_safe(u32 index, uint64_t *val)return rdreg64_safe ("rdmsr" , index, val); } +static inline int rdmsr_fep_safe (u32 index,uint64_t *val) +{ +return __rdreg64_safe (KVM_FEP ,"rdmsr" , index, val); +} +static inline int wrmsr_safe (u32 index,u64 val) {return wrreg64_safe ("wrmsr" , index, val);
lib/x86/vm.h +1 -0
@@ -8,6 +8,7 @@#include "asm/bitops.h" void setup_5level_page_table (void ); +void setup_4level_page_table (void );struct pte_search {int level;
x86/Makefile.x86_64 +1 -0
@@ -38,6 +38,7 @@ tests += $(TEST_DIR)/rdpru.$(exe) tests += $(TEST_DIR)/pks.$(exe) tests += $(TEST_DIR)/pmu_lbr.$(exe) tests += $(TEST_DIR)/pmu_pebs.$(exe) +tests += $(TEST_DIR)/msr_canonical.$(exe) ifeq ($(CONFIG_EFI),y) tests += $(TEST_DIR)/amd_sev.$(exe)
x86/cstart64.S +35 -0
@@ -92,6 +92,27 @@ switch_to_5level: call enter_long_mode jmpl $8, $lvl5 + +switch_to_4level: + mov %cr0, %eax + btr $31, %eax + mov %eax, %cr0 + + mov $ptl4, %eax + mov %eax, pt_root + + /* Disable CR4.LA57 */ + mov %cr4, %eax + btr $12, %eax + mov %eax, %cr4 + + mov $0x10, %ax + mov %ax, %ss + + call enter_long_mode + jmpl $8, $lvl5 + + smp_stacktop: .long stacktop - 4096 .align 16
@@ -139,3 +160,17 @@ setup_5level_page_table: lretq lvl5: retq + + +.globl setup_4level_page_table +setup_4level_page_table: + /* Check if 4-level paging has already enabled */ + mov %cr4, %rax + test $0x1000, %eax + jz lvl4 + + pushq $32 + pushq $switch_to_4level + lretq +lvl4: + retq
x86/msr_canonical.c new file +236 -0
236 lines not on this page · raw message
Signature · 1 line
2.40.1
From e14288f50896caaafbdd8e66d58fe757f237b13c Mon Sep 17 00:00:00 2001
From: Maxim Levitsky <[email protected]>
Date: Mon, 22 Jul 2024 11:09:40 -0400
Subject: [PATCH 2/2] vmx: add test for canonical checks on various fields
--- x86/vmx_tests.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+)
x86/vmx_tests.c +151 -0
168 lines not on this page · raw message
Signature · 1 line
2.40.1
PATCH v3 4/4 KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and MSR_GS_BASE
In reply to Maxim LevitskyIf these msrs are read by the emulator (e.g due to 'force emulation' prefix), SVM code currently fails to extract the corresponding segment bases, and return them to the emulator.
Fix that.
--- arch/x86/kvm/svm/svm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kvm/svm/svm.c +12 -0
@@ -2876,6 +2876,12 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_CSTAR : msr_info->data = svm->vmcb01.ptr->save.cstar;break ; +case MSR_GS_BASE : + msr_info->data = svm->vmcb01.ptr->save.gs.base; +break ; +case MSR_FS_BASE : + msr_info->data = svm->vmcb01.ptr->save.fs.base; +break ;case MSR_KERNEL_GS_BASE : msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;break ;
@@ -3101,6 +3107,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_CSTAR : svm->vmcb01.ptr->save.cstar = data;break ; +case MSR_GS_BASE : + svm->vmcb01.ptr->save.gs.base = data; +break ; +case MSR_FS_BASE : + svm->vmcb01.ptr->save.fs.base = data; +break ;case MSR_KERNEL_GS_BASE : svm->vmcb01.ptr->save.kernel_gs_base = data;break ;
Signature · 1 line
2.40.1
Sean Christopherson
maintainerRe: [PATCH v3 4/4] KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and MSR_GS_BASE
Quoted 3 lines · Maxim Levitsky
If these msrs are read by the emulator (e.g due to 'force emulation' prefix), SVM code currently fails to extract the corresponding segment bases, and return them to the emulator.
I'll apply this one for 6.11 and tag it for stable, i.e. no need to include this patch in v4.
Sean Christopherson
maintainerRe: [PATCH v3 4/4] KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and MSR_GS_BASE
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Maxim Levitsky
If these msrs are read by the emulator (e.g due to 'force emulation' prefix), SVM code currently fails to extract the corresponding segment bases, and return them to the emulator.
I'll apply this one for 6.11 and tag it for stable, i.e. no need to include this patch in v4.
I appear to have missed my normal "thank you" for this, so here it is, in kvm-x86 fixes:
[1/4] KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and MSR_GS_BASE
https://github.com/kvm-x86/linux/commit/dad1613e0533