Align SVM with APM defined behaviors
v2 cover Align SVM with APM defined behaviors
The APM lists the following behaviors
- The VMRUN, VMLOAD, VMSAVE, CLGI, VMMCALL, and INVLPGA instructions can be used when the EFER.SVME is set to 1; otherwise, these instructions generate a #UD exception.
- If VMMCALL instruction is not intercepted, the instruction raises a #UD exception.
- STGI instruction causes a #UD exception if SVM is not enabled and neither SVM Lock nor the device exclusion vector (DEV) are supported.
The patches in this series fix current SVM bugs that do not adhere to the APM listed behaviors.
v1 -> v2:
- Split up the series into smaller more logical changes as suggested by Sean
- Added patch for injecting #UD for STGI under APM defined conditions as suggested by Sean
- Combined EFER.SVME=0 conditional with intel CPU logic in svm_recalc_instruction_intercepts
v1: https://lore.kernel.org/all/[email protected]/
Kevin Cheng (5):
KVM: SVM: Move STGI and CLGI intercept handling
KVM: SVM: Inject #UD for STGI if EFER.SVME=0 and SVM Lock and DEV are
not available
KVM: SVM: Inject #UD for INVLPGA if EFER.SVME=0
KVM: SVM: Recalc instructions intercepts when EFER.SVME is toggled
KVM: SVM: Raise #UD if VMMCALL instruction is not intercepted
arch/x86/kvm/svm/svm.c | 52 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 7 deletions(-)
--
2.52.0.457.g6b5491de43-goog
PATCH v2 1/5 KVM: SVM: Move STGI and CLGI intercept handling
Similar to VMLOAD/VMSAVE intercept handling, move the STGI/CLGI intercept handling to svm_recalc_instruction_intercepts().
--- arch/x86/kvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/svm.c +6 -4
@@ -1010,6 +1010,11 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_clr_intercept (svm,INTERCEPT_VMSAVE ); svm->vmcb->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; } + +if (vgif) { +svm_clr_intercept (svm,INTERCEPT_STGI ); +svm_clr_intercept (svm,INTERCEPT_CLGI ); + } } }
@@ -1147,11 +1152,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)if (vnmi) svm->vmcb->control.int_ctl |=V_NMI_ENABLE_MASK ; -if (vgif) { -svm_clr_intercept (svm,INTERCEPT_STGI ); -svm_clr_intercept (svm,INTERCEPT_CLGI ); +if (vgif) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; - }if (vcpu->kvm->arch.bus_lock_detection_enabled)svm_set_intercept (svm,INTERCEPT_BUSLOCK );
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 11 lines · Kevin Cheng
Similar to VMLOAD/VMSAVE intercept handling, move the STGI/CLGI intercept handling to
svm_recalc_instruction_intercepts().--- arch/x86/kvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)arch/x86/kvm/svm/svm.c +0
-0
arch/x86/kvm/svm/svm.c static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) · line 1015
Quoted 6 lines
arch/x86/kvm/svm/svm.c +3
-0@@ -1010,6 +1010,11 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_clr_intercept (svm,INTERCEPT_VMSAVE ); svm->vmcb->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; } + +if (vgif) { +svm_clr_intercept (svm,INTERCEPT_STGI );
Could this cause a problem with NMI window tracking?
svm_enable_nmi_window() sets INTERCEPT_STGI to detect when NMIs are enabled, and it's later cleared by svm_set_gif(). If we recalc intercepts in between we will clear the intercept here and miss NMI enablement.
We could move the logic to set/clear INTERCEPT_STGI for NMI window tracking here as well, but then we'll need to recalc intercepts in svm_enable_nmi_window() and svm_set_gif(), which could be expensive.
The alternative is perhaps setting a flag when INTERCEPT_STGI is set in svm_enable_nmi_window() and avoid clearing the intercept here if the flag is set.
Not sure what's the best way forward here.
Quoted 21 lines
arch/x86/kvm/svm/svm.c +3
-4+svm_clr_intercept (svm,INTERCEPT_CLGI ); + } } }@@ -1147,11 +1152,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)if (vnmi) svm->vmcb->control.int_ctl |=V_NMI_ENABLE_MASK ; -if (vgif) { -svm_clr_intercept (svm,INTERCEPT_STGI ); -svm_clr_intercept (svm,INTERCEPT_CLGI ); +if (vgif) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; - }if (vcpu->kvm->arch.bus_lock_detection_enabled)svm_set_intercept (svm,INTERCEPT_BUSLOCK );Signature · 1 line
2.52.0.457.g6b5491de43-goog
Sean Christopherson
maintainerRe: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 7 lines · Yosry Ahmed
Quoted 6 lines · Kevin Cheng
Similar to VMLOAD/VMSAVE intercept handling, move the STGI/CLGI intercept handling to
svm_recalc_instruction_intercepts().--- arch/x86/kvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 13 lines
arch/x86/kvm/svm/svm.c static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) · line 1015
Quoted 11 lines
arch/x86/kvm/svm/svm.c +3
-0@@ -1010,6 +1010,11 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_clr_intercept (svm,INTERCEPT_VMSAVE ); svm->vmcb->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; } + +if (vgif) { +svm_clr_intercept (svm,INTERCEPT_STGI );Could this cause a problem with NMI window tracking?
Yes.
Quoted 9 lines
svm_enable_nmi_window()setsINTERCEPT_STGIto detect when NMIs are enabled, and it's later cleared bysvm_set_gif(). If we recalc intercepts in between we will clear the intercept here and miss NMI enablement.We could move the logic to set/clear
INTERCEPT_STGIfor NMI window tracking here as well, but then we'll need to recalc intercepts insvm_enable_nmi_window()andsvm_set_gif(), which could be expensive.
Quoted 5 lines
The alternative is perhaps setting a flag when
INTERCEPT_STGIis set insvm_enable_nmi_window()and avoid clearing the intercept here if the flag is set.Not sure what's the best way forward here.
First things first, the changelog needs to state _why_ the code is being moved. "To be like VMLOAD/VMSAVE" doesn't suffice, because my initial answer was going to be "well just don't move the code" (I already forgot the context of v1).
But moving the code is needed to fix the missing #UD in "Recalc instructions intercepts when EFER.SVME is toggled".
As for how to fix this, a few ideas:
1. Set KVM_REQ_EVENT to force KVM to re-evulate all events. kvm_check_and_inject_events() will see the pending NMI and/or SMI, that the NMI/SMI is not allowed, and re-call enable_{nmi,smi}_window().
2. Manually check for pending+blocked NMI/SMIs.
3. Combine parts of #1 and #2. Set KVM_REQ_EVENT, but only if there's a pending NMI or SMI.
4. Add flags to vcpu_svm to explicitly track if a vCPU has an NMI/SMI window, similar to what we're planning on doing for IRQs[*], and use that to more confidently do the right thing when recomputing intercepts.
I don't love any of those ideas. Ah, at least not until I poke around KVM. In svm_set_gif() there's already this:
if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
So I think it makes sense to bundle that into a helper, e.g. (no idea what to call it)
static bool svm_think_of_a_good_name(struct kvm_vcpu *vcpu)
{
if (svm->vcpu.arch.smi_pending ||
svm->vcpu.arch.nmi_pending ||
kvm_cpu_has_injectable_intr(&svm->vcpu) ||
kvm_apic_has_pending_init_or_sipi(&svm->vcpu))
kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
}And then call that from svm_recalc_instruction_intercepts(). That implements #3 in a fairly maintainable way (we'll hopefully notice sooner than later if we break svm_set_gif()).
Quoted 21 lines
Quoted 21 lines
arch/x86/kvm/svm/svm.c +3
-4+svm_clr_intercept (svm,INTERCEPT_CLGI ); + } } }@@ -1147,11 +1152,8 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)if (vnmi) svm->vmcb->control.int_ctl |=V_NMI_ENABLE_MASK ; -if (vgif) { -svm_clr_intercept (svm,INTERCEPT_STGI ); -svm_clr_intercept (svm,INTERCEPT_CLGI ); +if (vgif) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; - }if (vcpu->kvm->arch.bus_lock_detection_enabled)svm_set_intercept (svm,INTERCEPT_BUSLOCK );Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 73 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Quoted 6 lines · Kevin Cheng
Similar to VMLOAD/VMSAVE intercept handling, move the STGI/CLGI intercept handling to
svm_recalc_instruction_intercepts().--- arch/x86/kvm/svm/svm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)Quoted 13 lines
arch/x86/kvm/svm/svm.c static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) · line 1015
Quoted 11 lines
arch/x86/kvm/svm/svm.c +3
-0@@ -1010,6 +1010,11 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_clr_intercept (svm,INTERCEPT_VMSAVE ); svm->vmcb->control.virt_ext |=VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ; } + +if (vgif) { +svm_clr_intercept (svm,INTERCEPT_STGI );Could this cause a problem with NMI window tracking?
Yes.
Quoted 9 lines
svm_enable_nmi_window()setsINTERCEPT_STGIto detect when NMIs are enabled, and it's later cleared bysvm_set_gif(). If we recalc intercepts in between we will clear the intercept here and miss NMI enablement.We could move the logic to set/clear
INTERCEPT_STGIfor NMI window tracking here as well, but then we'll need to recalc intercepts insvm_enable_nmi_window()andsvm_set_gif(), which could be expensive.Quoted 5 lines
The alternative is perhaps setting a flag when
INTERCEPT_STGIis set insvm_enable_nmi_window()and avoid clearing the intercept here if the flag is set.Not sure what's the best way forward here.
First things first, the changelog needs to state _why_ the code is being moved. "To be like VMLOAD/VMSAVE" doesn't suffice, because my initial answer was going to be "well just don't move the code" (I already forgot the context of v1).
But moving the code is needed to fix the missing #UD in "Recalc instructions intercepts when EFER.SVME is toggled".
As for how to fix this, a few ideas:
1. Set
KVM_REQ_EVENTto force KVM to re-evulate all events.kvm_check_and_inject_events()will see the pending NMI and/or SMI, that the NMI/SMI is not allowed, and re-call enable_{nmi,smi}_window().2. Manually check for pending+blocked NMI/SMIs.
3. Combine parts of #1 and #2. Set
KVM_REQ_EVENT, but only if there's a pending NMI or SMI.4. Add flags to
vcpu_svmto explicitly track if a vCPU has an NMI/SMI window, similar to what we're planning on doing for IRQs[*], and use that to more confidently do the right thing when recomputing intercepts.I don't love any of those ideas. Ah, at least not until I poke around KVM. In
svm_set_gif()there's already this:if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);So I think it makes sense to bundle that into a helper, e.g. (no idea what to call it)
Quoted 8 lines
static bool
svm_think_of_a_good_name(struct kvm_vcpu *vcpu){ if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); }
Maybe svm_check_gif_events() or svm_check_gif_interrupts()?
Or maybe it's clearer if we just put the checks in a helper like svm_waiting_for_gif() or svm_pending_gif_interrupt().
Then in svm_recalc_instruction_intercepts() we do:
/* * If there is a pending interrupt controlled by GIF, set * KVM_REQ_EVENT to re-evaluate if the intercept needs to be set * again to track when GIF is re-enabled (e.g. for NMI * injection). */ svm_clr_intercept(svm, INTERCEPT_STGI); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
and in svm_set_gif() it reads well semantically:
enable_gif(svm); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Quoted 7 lines
And then call that from
svm_recalc_instruction_intercepts(). That implements #3 in a fairly maintainable way (we'll hopefully notice sooner than later if we breaksvm_set_gif()).
Sean Christopherson
maintainerRe: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 38 lines · Yosry Ahmed
Quoted 28 lines · Sean Christopherson
On Mon, Jan 12, 2026, Yosry Ahmed wrote:
As for how to fix this, a few ideas:1. Set
KVM_REQ_EVENTto force KVM to re-evulate all events.kvm_check_and_inject_events()will see the pending NMI and/or SMI, that the NMI/SMI is not allowed, and re-call enable_{nmi,smi}_window().2. Manually check for pending+blocked NMI/SMIs.
3. Combine parts of #1 and #2. Set
KVM_REQ_EVENT, but only if there's a pending NMI or SMI.4. Add flags to
vcpu_svmto explicitly track if a vCPU has an NMI/SMI window, similar to what we're planning on doing for IRQs[*], and use that to more confidently do the right thing when recomputing intercepts.I don't love any of those ideas. Ah, at least not until I poke around KVM. In
svm_set_gif()there's already this:if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);So I think it makes sense to bundle that into a helper, e.g. (no idea what to call it)
Quoted 8 lines
static bool
svm_think_of_a_good_name(struct kvm_vcpu *vcpu){ if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); }
Quoted 4 lines
Maybe
svm_check_gif_events()orsvm_check_gif_interrupts()?Or maybe it's clearer if we just put the checks in a helper like
svm_waiting_for_gif()orsvm_pending_gif_interrupt().
This was my first idea as well, though I would name it svm_has_pending_gif_event() to better align with kvm_vcpu_has_events().
I suggested a single helper because I don't love that how to react to the pending event is duplicated. But I definitely don't object to open coding the request if the consensus is that it's more readable overall.
Quoted 17 lines
Then in
svm_recalc_instruction_intercepts()we do:/* * If there is a pending interrupt controlled by GIF, set * KVM_REQ_EVENT to re-evaluate if the intercept needs to be set * again to track when GIF is re-enabled (e.g. for NMI * injection). */ svm_clr_intercept(svm, INTERCEPT_STGI); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);and in
svm_set_gif()it reads well semantically:enable_gif(svm); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Re: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 39 lines · Sean Christopherson
Quoted 38 lines · Yosry Ahmed
Quoted 36 lines · Sean Christopherson
On Mon, Jan 12, 2026, Yosry Ahmed wrote:
As for how to fix this, a few ideas:1. Set
KVM_REQ_EVENTto force KVM to re-evulate all events.kvm_check_and_inject_events()will see the pending NMI and/or SMI, that the NMI/SMI is not allowed, and re-call enable_{nmi,smi}_window().2. Manually check for pending+blocked NMI/SMIs.
3. Combine parts of #1 and #2. Set
KVM_REQ_EVENT, but only if there's a pending NMI or SMI.4. Add flags to
vcpu_svmto explicitly track if a vCPU has an NMI/SMI window, similar to what we're planning on doing for IRQs[*], and use that to more confidently do the right thing when recomputing intercepts.I don't love any of those ideas. Ah, at least not until I poke around KVM. In
svm_set_gif()there's already this:if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);So I think it makes sense to bundle that into a helper, e.g. (no idea what to call it)
static bool
svm_think_of_a_good_name(struct kvm_vcpu *vcpu){ if (svm->vcpu.arch.smi_pending || svm->vcpu.arch.nmi_pending || kvm_cpu_has_injectable_intr(&svm->vcpu) || kvm_apic_has_pending_init_or_sipi(&svm->vcpu)) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); }
Quoted 7 lines
Quoted 4 lines
Maybe
svm_check_gif_events()orsvm_check_gif_interrupts()?Or maybe it's clearer if we just put the checks in a helper like
svm_waiting_for_gif()orsvm_pending_gif_interrupt().This was my first idea as well, though I would name it
svm_has_pending_gif_event()to better align withkvm_vcpu_has_events().
svm_has_pending_gif_event() sounds good.
Quoted 4 lines
I suggested a single helper because I don't love that how to react to the pending event is duplicated. But I definitely don't object to open coding the request if the consensus is that it's more readable overall.
A single helper is nice, but I can't think of a name that would read well. My first instinct is svm_check_pending_gif_event(), but we are not really checking the event as much as requesting for it to be checked.
We can do svm_request_gif_event(), perhaps? Not sure if that's better or worse than svm_has_pending_gif_event().
Quoted 18 lines
Quoted 18 lines
Then in
svm_recalc_instruction_intercepts()we do:/* * If there is a pending interrupt controlled by GIF, set * KVM_REQ_EVENT to re-evaluate if the intercept needs to be set * again to track when GIF is re-enabled (e.g. for NMI * injection). */ svm_clr_intercept(svm, INTERCEPT_STGI); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);and in
svm_set_gif()it reads well semantically:enable_gif(svm); if (svm_pending_gif_interrupt()) kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Sean Christopherson
maintainerRe: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 11 lines · Yosry Ahmed
Quoted 6 lines · Sean Christopherson
Quoted 2 lines · Yosry Ahmed
Or maybe it's clearer if we just put the checks in a helper like
svm_waiting_for_gif()orsvm_pending_gif_interrupt().This was my first idea as well, though I would name it
svm_has_pending_gif_event()to better align withkvm_vcpu_has_events().
svm_has_pending_gif_event()sounds good.Quoted 1 line
Quoted 7 lines
Quoted 3 lines
I suggested a single helper because I don't love that how to react to the pending event is duplicated. But I definitely don't object to open coding the request if the consensus is that it's more readable overall.
A single helper is nice, but I can't think of a name that would read well. My first instinct is
svm_check_pending_gif_event(), but we are not really checking the event as much as requesting for it to be checked.
Ya, that's the same problem I'm having. I can't even come up with an absurdly verbose name to describe the behavior.
Quoted 2 lines
We can do
svm_request_gif_event(), perhaps? Not sure if that's better or worse thansvm_has_pending_gif_event().
Definitely worse in my opinion. My entire motivation for a single helper would be to avoid bleeding implementation details (use of KVM_REQ_EVENT) to trigger the potential re-evaluation STGI/CLGI intercepts. And then there's the fact that in most cases, there probably isn't a pending event, i.e. not request will be made.
Let's just go with svm_has_pending_gif_event().
Re: [PATCH V2 1/5] KVM: SVM: Move STGI and CLGI intercept handling
Quoted 27 lines · Sean Christopherson
Quoted 11 lines · Yosry Ahmed
Quoted 6 lines · Sean Christopherson
Quoted 2 lines · Yosry Ahmed
Or maybe it's clearer if we just put the checks in a helper like
svm_waiting_for_gif()orsvm_pending_gif_interrupt().This was my first idea as well, though I would name it
svm_has_pending_gif_event()to better align withkvm_vcpu_has_events().
svm_has_pending_gif_event()sounds good.Quoted 1 line
Quoted 7 lines
Quoted 3 lines
I suggested a single helper because I don't love that how to react to the pending event is duplicated. But I definitely don't object to open coding the request if the consensus is that it's more readable overall.
A single helper is nice, but I can't think of a name that would read well. My first instinct is
svm_check_pending_gif_event(), but we are not really checking the event as much as requesting for it to be checked.Ya, that's the same problem I'm having. I can't even come up with an absurdly verbose name to describe the behavior.
Quoted 2 lines
We can do
svm_request_gif_event(), perhaps? Not sure if that's better or worse thansvm_has_pending_gif_event().
Quoted 7 lines
Definitely worse in my opinion. My entire motivation for a single helper would be to avoid bleeding implementation details (use of
KVM_REQ_EVENT) to trigger the potential re-evaluation STGI/CLGI intercepts. And then there's the fact that in most cases, there probably isn't a pending event, i.e. not request will be made.Let's just go with
svm_has_pending_gif_event().
Sounds good. Thanks for the suggestions Yosry and Sean :) And thanks for catching this!
PATCH v2 2/5 KVM: SVM: Inject #UD for STGI if EFER.SVME=0 and SVM Lock and DEV are not available
In reply to Kevin ChengThe AMD APM states that STGI causes a #UD if SVM is not enabled and neither SVM Lock nor the device exclusion vector (DEV) are supported. Fix the STGI exit handler by injecting #UD when these conditions are met.
--- arch/x86/kvm/svm/svm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +11 -1
@@ -2271,8 +2271,18 @@ static int stgi_interception(struct kvm_vcpu *vcpu) {int ret; -if (nested_svm_check_permissions (vcpu)) +if ((!(vcpu->arch.efer &EFER_SVME ) && + !guest_cpu_cap_has (vcpu,X86_FEATURE_SVML ) && + !guest_cpu_cap_has (vcpu,X86_FEATURE_SKINIT )) || + !is_paging (vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +if (to_svm (vcpu)->vmcb->save.cpl) { +kvm_inject_gp (vcpu,0 );return 1 ; + } ret =kvm_skip_emulated_instruction (vcpu);svm_set_gif (to_svm (vcpu), true);
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 2/5] KVM: SVM: Inject #UD for STGI if EFER.SVME=0 and SVM Lock and DEV are not available
Quoted 2 lines · Kevin Cheng
The AMD APM states that STGI causes a #UD if SVM is not enabled and neither SVM Lock nor the device exclusion vector (DEV) are supported.
Might be useful to also mention the following part "Support for DEV is part of the SKINIT architecture".
Quoted 19 lines
Fix the STGI exit handler by injecting #UD when these conditions are met.
Signed-off-by Kevin Cheng <[email protected]>--- arch/x86/kvm/svm/svm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +2
-1@@ -2271,8 +2271,18 @@ static int stgi_interception(struct kvm_vcpu *vcpu) {int ret; -if (nested_svm_check_permissions (vcpu)) + if ((!(vcpu->arch.efer & EFER_SVME) && + !guest_cpu_cap_has(vcpu, X86_FEATURE_SVML) &&
arch/x86/kvm/svm/svm.c static int stgi_interception(struct kvm_vcpu *vcpu) · line 2285
Quoted 10 lines
arch/x86/kvm/svm/svm.c +9
-0+ !guest_cpu_cap_has(vcpu, X86_FEATURE_SKINIT)) || + !is_paging(vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +if (to_svm (vcpu)->vmcb->save.cpl) { +kvm_inject_gp (vcpu,0 );return 1 ; + }
Not a big fan of open-coding nested_svm_check_permissions() here. The checks could get out of sync.
How about refactoring nested_svm_check_permissions() like so:
arch/x86/kvm/svm/nested.c +8 -2
@@ -1520,9 +1520,10 @@ int nested_svm_exit_handled(struct vcpu_svm *svm)return vmexit; } -int nested_svm_check_permissions (struct kvm_vcpu *vcpu) +static int __nested_svm_check_permissions (struct kvm_vcpu *vcpu, +bool insn_allowed) { -if (!(vcpu->arch.efer &EFER_SVME ) || !is_paging (vcpu)) { +if (!insn_allowed || !is_paging (vcpu)) {kvm_queue_exception (vcpu,UD_VECTOR );return 1 ; }
@@ -1535,6 +1536,11 @@ int nested_svm_check_permissions(struct kvm_vcpu *vcpu)return 0 ; } +int nested_svm_check_permissions (struct kvm_vcpu *vcpu) +{ +return __nested_svm_check_permissions (vcpu, vcpu->arch.efer &EFER_SVME ); +} +static bool nested_svm_is_exception_vmexit (struct kvm_vcpu *vcpu,u8 vector,u32 error_code) {
arch/x86/kvm/svm/svm.c +11 -1
@@ -2333,9 +2333,19 @@ void svm_set_gif(struct vcpu_svm *svm, bool value)static int stgi_interception (struct kvm_vcpu *vcpu) { +bool insn_allowed;int ret; -if (nested_svm_check_permissions (vcpu)) +/* +* According to the APM, STGI is allowed even with SVM disabled if SVM +* Lock or device exclusion vector (DEV) are supported. DEV is part of +* the SKINIT architecture. +*/ + insn_allowed = (vcpu->arch.efer &EFER_SVME ) || +guest_cpu_cap_has (vcpu,X86_FEATURE_SVML ) || +guest_cpu_cap_has (vcpu,X86_FEATURE_SKINIT ); + +if (__nested_svm_check_permissions (vcpu, insn_allowed))return 1 ; ret =kvm_skip_emulated_instruction (vcpu);
---
We may also want to rename nested_svm_check_permissions() to nested_svm_insn_check_permissions() or something. Sean, WDYT?
Quoted 6 lines
arch/x86/kvm/svm/svm.c +0
-0ret =kvm_skip_emulated_instruction (vcpu);svm_set_gif (to_svm (vcpu), true);Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 2/5] KVM: SVM: Inject #UD for STGI if EFER.SVME=0 and SVM Lock and DEV are not available
Quoted 100 lines · Yosry Ahmed
Quoted 2 lines · Kevin Cheng
The AMD APM states that STGI causes a #UD if SVM is not enabled and neither SVM Lock nor the device exclusion vector (DEV) are supported.
Might be useful to also mention the following part "Support for DEV is part of the SKINIT architecture".
Quoted 19 lines
Fix the STGI exit handler by injecting #UD when these conditions are met.
Signed-off-by Kevin Cheng <[email protected]>--- arch/x86/kvm/svm/svm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +2
-1@@ -2271,8 +2271,18 @@ static int stgi_interception(struct kvm_vcpu *vcpu) {int ret; -if (nested_svm_check_permissions (vcpu)) + if ((!(vcpu->arch.efer & EFER_SVME) && + !guest_cpu_cap_has(vcpu, X86_FEATURE_SVML) &&arch/x86/kvm/svm/svm.c static int stgi_interception(struct kvm_vcpu *vcpu) · line 2285
Quoted 10 lines
arch/x86/kvm/svm/svm.c +9
-0+ !guest_cpu_cap_has(vcpu, X86_FEATURE_SKINIT)) || + !is_paging(vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +if (to_svm (vcpu)->vmcb->save.cpl) { +kvm_inject_gp (vcpu,0 );return 1 ; + }Not a big fan of open-coding
nested_svm_check_permissions()here. The checks could get out of sync.How about refactoring
nested_svm_check_permissions()like so:arch/x86/kvm/svm/nested.c +8
-2@@ -1520,9 +1520,10 @@ int nested_svm_exit_handled(struct vcpu_svm *svm)return vmexit; } -int nested_svm_check_permissions (struct kvm_vcpu *vcpu) +static int __nested_svm_check_permissions (struct kvm_vcpu *vcpu, +bool insn_allowed) { -if (!(vcpu->arch.efer &EFER_SVME ) || !is_paging (vcpu)) { +if (!insn_allowed || !is_paging (vcpu)) {kvm_queue_exception (vcpu,UD_VECTOR );return 1 ; }@@ -1535,6 +1536,11 @@ int nested_svm_check_permissions(struct kvm_vcpu *vcpu)return 0 ; } +int nested_svm_check_permissions (struct kvm_vcpu *vcpu) +{ +return __nested_svm_check_permissions (vcpu, vcpu->arch.efer &EFER_SVME ); +} +static bool nested_svm_is_exception_vmexit (struct kvm_vcpu *vcpu,u8 vector,u32 error_code) {arch/x86/kvm/svm/svm.c +11
-2@@ -2333,9 +2333,19 @@ void svm_set_gif(struct vcpu_svm *svm, bool value)static int stgi_interception (struct kvm_vcpu *vcpu) { +bool insn_allowed;int ret; -if (nested_svm_check_permissions (vcpu)) +/* +* According to the APM, STGI is allowed even with SVM disabled if SVM +* Lock or device exclusion vector (DEV) are supported. DEV is part of +* the SKINIT architecture. +*/ + insn_allowed = (vcpu->arch.efer &EFER_SVME ) || +guest_cpu_cap_has (vcpu,X86_FEATURE_SVML ) || +guest_cpu_cap_has (vcpu,X86_FEATURE_SKINIT ); + +if (__nested_svm_check_permissions (vcpu, insn_allowed))return 1 ; ret =kvm_skip_emulated_instruction (vcpu); ---
Quoted 2 lines
We may also want to rename
nested_svm_check_permissions()tonested_svm_insn_check_permissions()or something. Sean, WDYT?
I just sent out v3 without the rename for now. Sean, if you prefer nested_svm_insn_check_permissions over nested_svm_check_permissions let me know and I can change along with any final revisions.
PATCH v2 3/5 KVM: SVM: Inject #UD for INVLPGA if EFER.SVME=0
In reply to Kevin ChengINVLPGA should cause a #UD when EFER.SVME is not set. Add a check to properly inject #UD when EFER.SVME=0.
--- arch/x86/kvm/svm/svm.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/svm/svm.c +3 -0
@@ -2306,6 +2306,9 @@ static int invlpga_interception(struct kvm_vcpu *vcpu)gva_t gva =kvm_rax_read (vcpu);u32 asid =kvm_rcx_read (vcpu); +if (nested_svm_check_permissions (vcpu)) +return 1 ; +/* FIXME: Handle an address size prefix. */ if (!is_long_mode (vcpu)) gva = (u32 )gva;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 3/5] KVM: SVM: Inject #UD for INVLPGA if EFER.SVME=0
Quoted 4 lines · Kevin Cheng
INVLPGA should cause a #UD when EFER.SVME is not set. Add a check to properly inject #UD when EFER.SVME=0.
Signed-off-by Kevin Cheng <[email protected]>
Quoted 21 lines
--- arch/x86/kvm/svm/svm.c | 3 +++ 1 file changed, 3 insertions(+)arch/x86/kvm/svm/svm.c +3
-0@@ -2306,6 +2306,9 @@ static int invlpga_interception(struct kvm_vcpu *vcpu)gva_t gva =kvm_rax_read (vcpu);u32 asid =kvm_rcx_read (vcpu); +if (nested_svm_check_permissions (vcpu)) +return 1 ; +/* FIXME: Handle an address size prefix. */ if (!is_long_mode (vcpu)) gva = (u32 )gva;Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v2 4/5 KVM: SVM: Recalc instructions intercepts when EFER.SVME is toggled
In reply to Kevin ChengThe AMD APM states that VMRUN, VMLOAD, VMSAVE, CLGI, VMMCALL, and INVLPGA instructions should generate a #UD when EFER.SVME is cleared. Currently, when VMLOAD, VMSAVE, or CLGI are executed in L1 with EFER.SVME cleared, no #UD is generated in certain cases. This is because the intercepts for these instructions are cleared based on whether or not vls or vgif is enabled. The #UD fails to be generated when the intercepts are absent.
Fix the missing #UD generation by ensuring that all relevant instructions have intercepts set when SVME.EFER is disabled.
VMMCALL is special because KVM's ABI is that VMCALL/VMMCALL are always supported for L1 and never fault.
--- arch/x86/kvm/svm/svm.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +10 -1
@@ -243,6 +243,8 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)if (svm_gp_erratum_intercept && !sev_guest (vcpu->kvm))set_exception_intercept (svm,GP_VECTOR ); } + +kvm_make_request (KVM_REQ_RECALC_INTERCEPTS , vcpu); } svm->vmcb->save.efer = efer |EFER_SVME ;
@@ -976,6 +978,7 @@ void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu)static void svm_recalc_instruction_intercepts (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +u64 efer = vcpu->arch.efer;/* * Intercept INVPCID if shadow paging is enabled to sync/free shadow
@@ -996,7 +999,13 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu)svm_set_intercept (svm,INTERCEPT_RDTSCP ); } -if (guest_cpuid_is_intel_compatible (vcpu)) { +/* +* Intercept instructions that #UD if EFER.SVME=0, as SVME must be set even +* when running the guest, i.e. hardware will only ever see EFER.SVME=1. +*/ +if (guest_cpuid_is_intel_compatible (vcpu) || !(efer &EFER_SVME )) { +svm_set_intercept (svm,INTERCEPT_CLGI ); +svm_set_intercept (svm,INTERCEPT_STGI );svm_set_intercept (svm,INTERCEPT_VMLOAD );svm_set_intercept (svm,INTERCEPT_VMSAVE ); svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK ;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v2 5/5 KVM: SVM: Raise #UD if VMMCALL instruction is not intercepted
In reply to Kevin ChengThe AMD APM states that if VMMCALL instruction is not intercepted, the instruction raises a #UD exception.
Create a vmmcall exit handler that generates a #UD if a VMMCALL exit from L2 is being handled by L0, which means that L1 did not intercept the VMMCALL instruction.
--- arch/x86/kvm/svm/svm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +15 -1
@@ -3180,6 +3180,20 @@ static int bus_lock_exit(struct kvm_vcpu *vcpu)return 0 ; } +static int vmmcall_interception (struct kvm_vcpu *vcpu) +{ +/* +* VMMCALL #UDs if it's not intercepted, and KVM reaches this point if +* and only if the VMMCALL intercept is not set in vmcb12. +*/ +if (is_guest_mode (vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +return kvm_emulate_hypercall (vcpu); +} +static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_READ_CR0 ] = cr_interception, [SVM_EXIT_READ_CR3 ] = cr_interception,
@@ -3230,7 +3244,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_TASK_SWITCH] = task_switch_interception, [SVM_EXIT_SHUTDOWN] = shutdown_interception, [SVM_EXIT_VMRUN] = vmrun_interception, - [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, + [SVM_EXIT_VMMCALL] = vmmcall_interception, [SVM_EXIT_VMLOAD] = vmload_interception, [SVM_EXIT_VMSAVE] = vmsave_interception, [SVM_EXIT_STGI] = stgi_interception,
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 5/5] KVM: SVM: Raise #UD if VMMCALL instruction is not intercepted
Quoted 9 lines · Kevin Cheng
The AMD APM states that if VMMCALL instruction is not intercepted, the instruction raises a #UD exception.
Create a vmmcall exit handler that generates a #UD if a VMMCALL exit from L2 is being handled by L0, which means that L1 did not intercept the VMMCALL instruction.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Kevin Cheng <[email protected]>
Quoted 41 lines
--- arch/x86/kvm/svm/svm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +15
-1@@ -3180,6 +3180,20 @@ static int bus_lock_exit(struct kvm_vcpu *vcpu)return 0 ; } +static int vmmcall_interception (struct kvm_vcpu *vcpu) +{ +/* +* VMMCALL #UDs if it's not intercepted, and KVM reaches this point if +* and only if the VMMCALL intercept is not set in vmcb12. +*/ +if (is_guest_mode (vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +return kvm_emulate_hypercall (vcpu); +} +static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_READ_CR0 ] = cr_interception, [SVM_EXIT_READ_CR3 ] = cr_interception,@@ -3230,7 +3244,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_TASK_SWITCH] = task_switch_interception, [SVM_EXIT_SHUTDOWN] = shutdown_interception, [SVM_EXIT_VMRUN] = vmrun_interception, - [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, + [SVM_EXIT_VMMCALL] = vmmcall_interception, [SVM_EXIT_VMLOAD] = vmload_interception, [SVM_EXIT_VMSAVE] = vmsave_interception, [SVM_EXIT_STGI] = stgi_interception,Signature · 1 line
2.52.0.457.g6b5491de43-goog
Re: [PATCH V2 5/5] KVM: SVM: Raise #UD if VMMCALL instruction is not intercepted
In reply to Kevin ChengQuoted 39 lines · Kevin Cheng
The AMD APM states that if VMMCALL instruction is not intercepted, the instruction raises a #UD exception.
Create a vmmcall exit handler that generates a #UD if a VMMCALL exit from L2 is being handled by L0, which means that L1 did not intercept the VMMCALL instruction.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Kevin Cheng <[email protected]>--- arch/x86/kvm/svm/svm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +14
-0@@ -3180,6 +3180,20 @@ static int bus_lock_exit(struct kvm_vcpu *vcpu)return 0 ; } +static int vmmcall_interception (struct kvm_vcpu *vcpu) +{ +/* +* VMMCALL #UDs if it's not intercepted, and KVM reaches this point if +* and only if the VMMCALL intercept is not set in vmcb12. +*/ +if (is_guest_mode (vcpu)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } + +return kvm_emulate_hypercall (vcpu); +} +static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_READ_CR0 ] = cr_interception, [SVM_EXIT_READ_CR3 ] = cr_interception,
arch/x86/kvm/svm/svm.c static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { · line 3250
Quoted 8 lines
arch/x86/kvm/svm/svm.c +1
-1@@ -3230,7 +3244,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { [SVM_EXIT_TASK_SWITCH] = task_switch_interception, [SVM_EXIT_SHUTDOWN] = shutdown_interception, [SVM_EXIT_VMRUN] = vmrun_interception, - [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall, + [SVM_EXIT_VMMCALL] = vmmcall_interception, [SVM_EXIT_VMLOAD] = vmload_interception, [SVM_EXIT_VMSAVE] = vmsave_interception, [SVM_EXIT_STGI] = stgi_interception,