All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception
@ 2026-12-02  8:02 Sohil Mehta
  2026-12-02  8:02 ` [PATCH 1/4] KVM: x86: Track the event type of a queued exception Sohil Mehta
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Sohil Mehta @ 2026-12-02  8:02 UTC (permalink / raw)
  To: kvm
  Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin,
	Xin Li, Peter Zijlstra, Andrew Cooper, Tom Lendacky,
	Nikunj A Dadhania, Shivansh Dhiman, Neeraj Upadhyay,
	David Woodhouse, Chao Gao, Binbin Wu, Maciej Wieczor-Retman,
	Chang S . Bae, Sohil Mehta, Yosry Ahmed, David Matlack, x86,
	linux-kernel, linux-kselftest

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/20260911213659.2025974-1-sohil.mehta@intel.com/
[2]: https://lore.kernel.org/all/7f93eb25874ddd13a1ad6e3c75785f11041c8b7f.camel@infradead.org/
[3]: https://lore.kernel.org/lkml/fcfea4a5e25a9f828de317cc2c0b133eabd91745.camel@infradead.org/
[4]: https://lore.kernel.org/all/f4cb5f8e-caf5-4513-9538-edaaea20de2d@citrix.com/
[5]: https://lore.kernel.org/all/20260925114017.2208531-1-shivansh.dhiman@amd.com/


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
-- 
2.43.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-02  8:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  8:02 [PATCH 0/4] KVM: VMX: Handle the #DB of an ICEBP as a privileged software exception Sohil Mehta
2026-12-02  8:02 ` [PATCH 1/4] KVM: x86: Track the event type of a queued exception Sohil Mehta
2026-12-02  8:02 ` [PATCH 2/4] KVM: nVMX: Don't drop an L1-injected INT1 whose delivery is interrupted Sohil Mehta
2026-12-02  8:02 ` [PATCH 3/4] KVM: VMX: Re-inject an ICEBP #DB as a privileged software exception Sohil Mehta
2026-12-02  8:35   ` sashiko-bot
2026-12-02  8:02 ` [PATCH 4/4] KVM: selftests: Test how L1 sees the #DB of an ICEBP in L2 Sohil Mehta

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.