[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 2/2] x86/nestedhvm: Don't treat an emulated INT $n as an exception


  • To: xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Date: Fri, 20 Nov 2026 17:00:13 +0800
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5TRhWD1Ujks8mNxichRYb4a82YhbY3PV06BDqVi/KvE=; b=AUDIuyPZ7RAi9PYlkv0SZ9qSjskfmF9DgTmrIfegD9V0z/xRPxOkHnMmsu0VNbZeSIZj71zlLV1BwW1URuSRDR+qVFfo2NZUR8Z6W6GuKhbIslBTRQm6Ao8iwYebA02cv7NTXa92V/pZ/qasrF3Jj+GL8XuFSDDzfPcWOE1+mvDQlOIExgCcLB1tOl/knQERPNDi/xI5Z0+plWJM+pgWANiCpi+pPOyYXAQf9dydJ2o6buNyl/DiNJzTNrMkntYLH8s9Kygt3EupRf8MraRsoT3sF+lRfWr1RVLsi464Rh/KGTfHG5N90QiKLI0I4G5hCCp4VkjyLpZ4MaW8UzHWKw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=adL7xdR07f1ZD5y/P0wfXtk7VV23JKbRYQYs0NW4Nc3EFeADBSsLL2vk4zXf0jK3ty0zns7xeKlums1405u6lWLuemSf0q6F2eWgyyVCSjW7pyRoG+SUy0AO1tQcTP/wcIOx+WPOUx1Ja0msdq8ftPWULxdciL8XUEFEVX8XOYK8IcLhd8s0rhL6IsbAiGFuDdljsn3hpgdfAkSE0v/5Z4WsPoSmR5rBfBEGZHb9oNBrrF3RBXLtu3KtBjtg+hGQqGkoTo5wXTMn0O3eSQr8RPhMnzOXVW8KKqTpgyh9hyJXOAE9VnLX4FiT7sigKS7RDGd3EVDkh9wa0HRJeGVmVA==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Jason Andryuk <jason.andryuk@xxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Stephen Cheng <stephen.cheng@xxxxxxxxxx>
  • Delivery-date: Fri, 20 Nov 2026 09:02:48 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

hvm_inject_event() asks whether L1 intercepts an event raised in L2, but
passes the hook only the vector and error code, so an emulated INT $n is
checked as if it were exception n.

On SVM, L1's intercept for exception n is checked instead of its
software interrupt intercept (INTn in the APM), and the #VMEXIT is
VMEXIT_EXCEPTION_DE + n instead of VMEXIT_SWINT.  If L1 intercepts that
exit code, it gets an exception exit for n < 0x14 and an unrelated exit
for n in 0x20-0x4d.  For other n, L1 is crashed with "Illegal exitcode".
On VMX, where INT $n cannot cause a VM exit, n < 32 is checked against
the exception bitmap and larger n hits ASSERT(vector < 32).

Pass the whole event to the hook.  On SVM, check INT $n against the
software interrupt intercept and exit with VMEXIT_SWINT, EXITINFO1 = n
and NextRIP after the instruction (AMD64 APM vol 2, publication 24593
rev 3.44, sections 15.7.1, 15.8.2 and 15.12).  On VMX, never intercept
INT $n (Intel SDM 325462-092US vol 3C, section 28.2).

Fixes: 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")
Fixes: 479d4619fd5b ("Nested VMX: interrupt/exception handling for n2 guest")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Cheng <stephen.cheng@xxxxxxxxxx>
---
v2:
 - New patch.

Tested on AMD with an XTF test which runs INT $0x00, $0x03 and $0x80 in
L2 with each combination of the software interrupt and exception
intercepts, natively and with the forced emulation prefix.  The emulated
results now match the native ones, including EXITINFO1 and NextRIP.
INT $0x80 no longer crashes L1.

About the Fixes: tags: the two commits added the check which this
patch changes.  At that time it was not wrong, because Xen injected an
emulated INT $n as exception n for every guest.  It became wrong with
commit ecf5678200a ("x86/hvm: don't discard the SW/HW event distinction
from the emulator"), which made normal event injection keep the event
type, but did not change the nested check.  I can use
Fixes: ecf5678200a instead, if that is preferred.

 xen/arch/x86/hvm/hvm.c                  |  3 +-
 xen/arch/x86/hvm/svm/nestedhvm.h        |  2 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        | 42 ++++++++++++++++++-------
 xen/arch/x86/hvm/vmx/vmx.c              |  6 ++--
 xen/arch/x86/hvm/vmx/vvmx.c             | 10 ++++--
 xen/arch/x86/include/asm/hvm/hvm.h      |  8 ++---
 xen/arch/x86/include/asm/hvm/vmx/vvmx.h |  6 ++--
 7 files changed, 52 insertions(+), 25 deletions(-)

diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index 4334ff678d3..863c3666d25 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -1774,8 +1774,7 @@ void hvm_inject_event(const struct x86_event *event)
     if ( nestedhvm_enabled(curr->domain) &&
          !nestedhvm_vmswitch_in_progress(curr) &&
          nestedhvm_vcpu_in_guestmode(curr) &&
-         nhvm_vmcx_guest_intercepts_event(
-             curr, event->vector, event->error_code) )
+         nhvm_vmcx_guest_intercepts_event(curr, event) )
     {
         enum nestedhvm_vmexits nsret;
 
diff --git a/xen/arch/x86/hvm/svm/nestedhvm.h b/xen/arch/x86/hvm/svm/nestedhvm.h
index 963390e8a99..a45694457b9 100644
--- a/xen/arch/x86/hvm/svm/nestedhvm.h
+++ b/xen/arch/x86/hvm/svm/nestedhvm.h
@@ -46,7 +46,7 @@ int cf_check nsvm_vcpu_vmexit_event(struct vcpu *v,
                                     const struct x86_event *event);
 uint64_t cf_check nsvm_vcpu_hostcr3(struct vcpu *v);
 bool cf_check nsvm_vmcb_guest_intercepts_event(
-    struct vcpu *v, unsigned int vector, int errcode);
+    struct vcpu *v, const struct x86_event *event);
 bool cf_check nsvm_vmcb_hap_enabled(struct vcpu *v);
 enum hvm_intblk cf_check nsvm_intr_blocked(struct vcpu *v);
 void cf_check nsvm_domain_relinquish_resources(struct domain *d);
diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nestedsvm.c
index 70ef50eb845..05c450f9fd3 100644
--- a/xen/arch/x86/hvm/svm/nestedsvm.c
+++ b/xen/arch/x86/hvm/svm/nestedsvm.c
@@ -753,6 +753,7 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct 
cpu_user_regs *regs,
         case VMEXIT_EXCEPTION_15:
         case VMEXIT_EXCEPTION_MF:
         case VMEXIT_EXCEPTION_AC:
+        case VMEXIT_SWINT:
             ns_vmcb->exitinfo1 = svm->ns_vmexit.exitinfo1;
             break;
         default:
@@ -766,29 +767,48 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct 
cpu_user_regs *regs,
     return 0;
 }
 
+/*
+ * INT $n (CD nn) is checked against the software interrupt intercept (INTn),
+ * never the exception intercepts, even when n < 32 (AMD APM vol 2 rev 3.44,
+ * 15.12).  INT3 and INTO raise exceptions.  ICEBP should check the ICEBP
+ * intercept, not #DB (15.12.2); not handled yet.
+ */
+static uint64_t nsvm_event_exitcode(const struct x86_event *event)
+{
+    if ( event->type == X86_ET_SW_INT )
+        return VMEXIT_SWINT;
+
+    return VMEXIT_EXCEPTION_DE + event->vector;
+}
+
 int cf_check nsvm_vcpu_vmexit_event(
     struct vcpu *v, const struct x86_event *event)
 {
     const struct vmcb *vmcb = v->arch.hvm.svm.vmcb;
-    uint64_t nextrip = 0;
+    uint64_t exitinfo1 = event->error_code, nextrip = 0;
 
     ASSERT(vcpu_nestedhvm(v).nv_vvmcx != NULL);
 
     /*
-     * An exception intercept saves NextRIP only for INT3, INTO and BOUND
-     * (AMD APM vol 2 rev 3.44, 15.7.1).  The emulator raises BOUND's #BR as
-     * a plain hardware exception, so only INT3/INTO can be recognised here.
-     * As in svm_inject_event(), truncate it outside of 64bit mode.
+     * NextRIP is saved for the software interrupt intercept (INTn), and for
+     * an exception intercept only for INT3, INTO and BOUND (AMD APM vol 2
+     * rev 3.44, 15.7.1).  The emulator raises BOUND's #BR as a plain hardware
+     * exception, so only INT $n, INT3 and INTO can be recognised here.  As in
+     * svm_inject_event(), truncate it outside of 64bit mode.
      */
-    if ( event->type == X86_ET_SW_EXC )
+    if ( event->type == X86_ET_SW_INT || event->type == X86_ET_SW_EXC )
     {
         nextrip = guest_cpu_user_regs()->rip + event->insn_len;
         if ( !((vmcb_get_efer(vmcb) & EFER_LMA) && vmcb->cs.l) )
             nextrip = (uint32_t)nextrip;
     }
 
-    nestedsvm_vmexit_defer(v, VMEXIT_EXCEPTION_DE + event->vector,
-                           event->error_code, event->data, nextrip);
+    /* With DecodeAssists, EXITINFO1 for INTn is the vector (15.8.2). */
+    if ( event->type == X86_ET_SW_INT )
+        exitinfo1 = event->vector;
+
+    nestedsvm_vmexit_defer(v, nsvm_event_exitcode(event), exitinfo1,
+                           event->data, nextrip);
     return NESTEDHVM_VMEXIT_DONE;
 }
 
@@ -955,10 +975,10 @@ nsvm_vmcb_guest_intercepts_exitcode(struct vcpu *v,
 }
 
 bool cf_check nsvm_vmcb_guest_intercepts_event(
-    struct vcpu *v, unsigned int vector, int errcode)
+    struct vcpu *v, const struct x86_event *event)
 {
-    return nsvm_vmcb_guest_intercepts_exitcode(v,
-        guest_cpu_user_regs(), VMEXIT_EXCEPTION_DE + vector);
+    return nsvm_vmcb_guest_intercepts_exitcode(v, guest_cpu_user_regs(),
+                                               nsvm_event_exitcode(event));
 }
 
 static int
diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
index 3c7374a9ff4..2a760e9d161 100644
--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -2083,7 +2083,7 @@ static void cf_check vmx_inject_event(const struct 
x86_event *event)
             write_debugreg(6, read_debugreg(6) | DR_STEP);
         }
         if ( !nestedhvm_vcpu_in_guestmode(curr) ||
-             !nvmx_intercepts_exception(curr, X86_EXC_DB, _event.error_code) )
+             !nvmx_intercepts_event(curr, &_event) )
         {
             unsigned long val;
 
@@ -2128,7 +2128,7 @@ static void cf_check vmx_inject_event(const struct 
x86_event *event)
         __vmwrite(VM_ENTRY_INSTRUCTION_LEN, _event.insn_len);
 
     if ( nestedhvm_vcpu_in_guestmode(curr) &&
-         nvmx_intercepts_exception(curr, _event.vector, _event.error_code) )
+         nvmx_intercepts_event(curr, &_event) )
     {
         nvmx_enqueue_n2_exceptions (curr, 
             INTR_INFO_VALID_MASK |
@@ -2886,7 +2886,7 @@ static struct hvm_function_table __initdata_cf_clobber 
vmx_function_table = {
     .nhvm_vcpu_reset      = nvmx_vcpu_reset,
     .nhvm_vcpu_p2m_base   = nvmx_vcpu_eptp_base,
     .nhvm_vmcx_hap_enabled = nvmx_ept_enabled,
-    .nhvm_vmcx_guest_intercepts_event = nvmx_intercepts_exception,
+    .nhvm_vmcx_guest_intercepts_event = nvmx_intercepts_event,
     .nhvm_vcpu_vmexit_event = nvmx_vmexit_event,
     .nhvm_intr_blocked    = nvmx_intr_blocked,
     .nhvm_domain_relinquish_resources = nvmx_domain_relinquish_resources,
diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c
index 29e48387a11..2f1788d1737 100644
--- a/xen/arch/x86/hvm/vmx/vvmx.c
+++ b/xen/arch/x86/hvm/vmx/vvmx.c
@@ -506,12 +506,18 @@ static void vmfail(struct cpu_user_regs *regs, enum 
vmx_insn_errno errno)
         vmfail_invalid(regs);
 }
 
-bool cf_check nvmx_intercepts_exception(
-    struct vcpu *v, unsigned int vector, int error_code)
+bool cf_check nvmx_intercepts_event(
+    struct vcpu *v, const struct x86_event *event)
 {
+    const unsigned int vector = event->vector;
+    const int error_code = event->error_code;
     u32 exception_bitmap, pfec_match=0, pfec_mask=0;
     int r;
 
+    /* INT $n is not an exception, and is never in the exception bitmap. */
+    if ( event->type == X86_ET_SW_INT )
+        return false;
+
     ASSERT(vector < 32);
 
     exception_bitmap = get_vvmcs(v, EXCEPTION_BITMAP);
diff --git a/xen/arch/x86/include/asm/hvm/hvm.h 
b/xen/arch/x86/include/asm/hvm/hvm.h
index 9bf2064925f..0168d85f733 100644
--- a/xen/arch/x86/include/asm/hvm/hvm.h
+++ b/xen/arch/x86/include/asm/hvm/hvm.h
@@ -206,7 +206,7 @@ struct hvm_function_table {
     int (*nhvm_vcpu_vmexit_event)(struct vcpu *v, const struct x86_event 
*event);
     uint64_t (*nhvm_vcpu_p2m_base)(struct vcpu *v);
     bool (*nhvm_vmcx_guest_intercepts_event)(
-        struct vcpu *v, unsigned int vector, int errcode);
+        struct vcpu *v, const struct x86_event *event);
 
     bool (*nhvm_vmcx_hap_enabled)(struct vcpu *v);
 
@@ -663,12 +663,12 @@ static inline uint64_t nhvm_vcpu_p2m_base(struct vcpu *v)
     return alternative_call(hvm_funcs.nhvm_vcpu_p2m_base, v);
 }
 
-/* returns true, when l1 guest intercepts the specified trap */
+/* returns true, when l1 guest intercepts the specified event */
 static inline bool nhvm_vmcx_guest_intercepts_event(
-    struct vcpu *v, unsigned int vector, int errcode)
+    struct vcpu *v, const struct x86_event *event)
 {
     return alternative_call(hvm_funcs.nhvm_vmcx_guest_intercepts_event, v,
-                            vector, errcode);
+                            event);
 }
 
 /* returns true when l1 guest wants to use hap to run l2 guest */
diff --git a/xen/arch/x86/include/asm/hvm/vmx/vvmx.h 
b/xen/arch/x86/include/asm/hvm/vmx/vvmx.h
index da10d3fa961..ec725dcc71b 100644
--- a/xen/arch/x86/include/asm/hvm/vmx/vvmx.h
+++ b/xen/arch/x86/include/asm/hvm/vmx/vvmx.h
@@ -11,6 +11,8 @@
 #ifndef __ASM_X86_HVM_VVMX_H__
 #define __ASM_X86_HVM_VVMX_H__
 
+struct x86_event;
+
 struct vvmcs_list {
     unsigned long vvmcs_mfn;
     struct list_head node;
@@ -78,8 +80,8 @@ void cf_check nvmx_vcpu_destroy(struct vcpu *v);
 int cf_check nvmx_vcpu_reset(struct vcpu *v);
 uint64_t cf_check nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk cf_check nvmx_intr_blocked(struct vcpu *v);
-bool cf_check nvmx_intercepts_exception(
-    struct vcpu *v, unsigned int vector, int error_code);
+bool cf_check nvmx_intercepts_event(
+    struct vcpu *v, const struct x86_event *event);
 void cf_check nvmx_domain_relinquish_resources(struct domain *d);
 
 bool cf_check nvmx_ept_enabled(struct vcpu *v);
-- 
2.49.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.