Skip to content

KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception

Sohil Mehta kvm linux-kselftest lkml sashiko-reviews 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest4 patches 3 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4KVM: x86: Track the event type of a queued exceptionarch/x86/include/asm/kvm_host.h arch/x86/kvm/svm/svm.c arch/x86/kvm/vmx/vmx.c +2 more+39 -130 replies
2/4KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interruptedarch/x86/kvm/vmx/nested.c arch/x86/kvm/vmx/vmx.c arch/x86/kvm/x86.c+26 -101 reply
3/4KVM: VMX: Re-inject an ICEBP #DB as a privileged software exceptionarch/x86/kvm/vmx/vmx.c arch/x86/kvm/x86.c arch/x86/kvm/x86.h+30 -152 replies
4/4KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2tools/testing/selftests/kvm/Makefile.kvm tools/testing/selftests/kvm/include/x86/vmx.h tools/testing/selftests/kvm/x86/vmx_icebp_test.c+182 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception

TL;DR
-----
This series tracks the event type of a queued exception, and uses it to requeue and re-inject a privileged software exception as one. This lays groundwork for FRED guest support [1] and fixes nested-VMX edge cases.

Background
----------
David's int1_fred_test [2] found the issue by checking the event type in the FRED stack frame. The issue isn't specific to FRED but it becomes highly visible with FRED virtualization.

ICEBP (INT1) generates a #DB with the event type of a privileged software exception. KVM loses that type in two ways. It re-injects the #DB of an intercepted ICEBP as a hardware exception. And it drops a privileged software exception altogether if a VM-exit interrupts its delivery.

The existing impact described below is limited, but it's still worth addressing. As fixing this seems necessary [3] for the upcoming FRED guest support, I am posting it separately for easier review and merge.

Impact
------
Without FRED, a guest can't see the event type through the IDT, so currently only nested guests are affected:

- If the delivery of an ICEBP's #DB to L2 causes a VM-exit to L1, L1 finds a hardware exception in the IDT-vectoring information, with no instruction length and L2's RIP already past the ICEBP. An L1 that re-injects that event still delivers the #DB with the right return RIP.
- An INT1 that L1 injects into L2 is dropped if a VM-exit interrupts its delivery. KVM doesn't re-inject it, and if the VM-exit goes to L1, L1 doesn't find it in the IDT-vectoring information. If L1 injected it at an ICEBP, L2 re-executes the ICEBP and takes the #DB late. Otherwise, the #DB is lost.

Neither issue breaks a common L1. An L1 normally injects an INT1 only to re-inject the #DB of an ICEBP, at the ICEBP, where a dropped INT1 is only delayed. So the series has no Fixes tags.

However, with upcoming FRED guest support [1], every ICEBP in a FRED guest shows event type 3 instead of 5 in the FRED stack frame. Unlike the IDT, FRED gives the guest the type of every event. Linux's FRED entry code dispatches on the type before the vector, so KVM should deliver the type that bare metal would. Currently, Linux sends both types of #DB to the same handler, but other guests may not.

Patches
-------
Patch 1 adds the event type to the queued exception, with no functional change.

Patch 2 uses the event type to requeue a privileged software exception whose delivery is interrupted, rather than drop it, and to inject it with its own type and length.

Patch 3 builds on that to fix the other issue. KVM now re-injects the #DB of an intercepted ICEBP as a privileged software exception, and VM-entry advances RIP past the ICEBP, as Andy Cooper suggested [4].

Patch 4 adds a nested VMX selftest for both fixes. It doesn't need FRED.

AI was used for the initial prototype but the patches were heavily modified before submission. The series has no dependencies and is based on kvm-x86-next (kvm-x86-next-2026.11.11).

Testing
-------
Tested the series on a Clearwater Forest system (with ept=1 and ept=0):

New L2 ICEBP selftest:
It passes. Without patch 3, L1 sees a hardware exception for the ICEBP's #DB. Without patch 2, L2 loops on the ICEBP and the test hangs.
Throwaway save/restore selftest:
It saves and restores a vCPU while the #DB of an ICEBP is in flight, and checks that the guest takes exactly one #DB. It passes. If KVM reported the queued INT1 in the vCPU events, the guest would take the #DB twice. I can post it if anyone wants it upstream.
KVM unit tests:
The debug, realmode, vmx and i386 taskswitch tests show no regressions. They cover ICEBP with single-step and other paths that the series touches.
KVM selftests:
They show no regressions.
David's FRED event type test [2]:
It passes with the FRED guest support series on top.

Notes/Opens
-----------
Save/restore: The vCPU events uAPI carries neither the event type nor the instruction length, so patch 2 leaves a queued INT1 out, like INT3 and INTO, and the guest re-executes the ICEBP. In a rare case, an INT1 that L1 injects at a RIP without an ICEBP can be lost across save/restore. It's the same as a software exception that L1 injects. Adding the type and length to the uAPI would fix both, but that doesn't seem worth a uAPI change.

vm86: Without unrestricted guest, KVM runs a real-mode guest in vm86 mode and forwards the #DB of an ICEBP to it without advancing RIP, so the guest executes the ICEBP forever. This predates the series, as fd2a445a94d2 ("KVM: VMX: Advance rip to after an ICEBP instruction") advanced RIP only outside vm86 mode. The series leaves vm86 mode alone, as nothing queues a privileged software exception there today. I can post a fix separately if someone says it's worth it.

SVM: SVM can inject a privileged software exception only for FRED guests. So for now, SVM keeps injecting a hardware exception for the #DB of an ICEBP. The SVM FRED series [5] asked for a generic fix for KVM-injected INT1s. Now, SVM FRED can inject type 5 based on the event type that this series adds.

Links
-----
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/
[5]: https://lore.kernel.org/all/[email protected]/

Sohil Mehta (4):
KVM: x86: Track the event type of a queued exception
KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is
interrupted
KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception
KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2

 arch/x86/include/asm/kvm_host.h               |   2 +
 arch/x86/kvm/svm/svm.c                        |   2 +-
 arch/x86/kvm/vmx/nested.c                     |  13 +-
 arch/x86/kvm/vmx/vmx.c                        |  52 ++++--
 arch/x86/kvm/x86.c                            |  50 +++--
 arch/x86/kvm/x86.h                            |  14 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/include/x86/vmx.h |   7 +
 .../selftests/kvm/x86/vmx_icebp_test.c        | 174 ++++++++++++++++++
 9 files changed, 277 insertions(+), 38 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/vmx_icebp_test.c

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7

Signature · 1 line
2.43.0