Skip to contentSkip to conversation

XSA-191 followup

PATCH Superseded Andrew Cooper 7 participants 91 messages Nov 23, 2016 – Nov 25, 2016

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover XSA-191 followup

This is partly RFC as there is a whole lot of rebasing happened recently and I haven't run thorough tests yet.

This is the quantity of changes required to fix some edgecases in XSA-191 which were ultimately chosen not to go out in the security fix. The main purpose of this series is to fix emulation sufficiently to allow patch 15 to avoid opencoding all of the segmenation logic.

Andrew Cooper (15):
x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity x86/emul: Simplfy emulation state setup
x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC
x86/emul: Remove opencoded exception generation
x86/emul: Rework emulator event injection
x86/vmx: Use hvm_{get,set}_segment_register() rather than vmx_{get,set}_segment_register() x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS x86/emul: Avoid raising faults behind the emulators back
x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer
x86/hvm: Reimplement hvm_copy_*_nofault() in terms of no pagefault_info x86/hvm: Rename hvm_copy_*_guest_virt() to hvm_copy_*_guest_linear() x86/hvm: Avoid __hvm_copy() raising #PF behind the emulators back
x86/hvm: Prepare to allow use of system segments for memory references x86/hvm: Use system-segment relative memory accesses

 tools/tests/x86_emulator/test_x86_emulator.c |   1 +
 xen/arch/x86/hvm/emulate.c                   | 238 +++++++----------
 xen/arch/x86/hvm/hvm.c                       | 366 +++++++++++++++++++--------
 xen/arch/x86/hvm/io.c                        |   6 +-
 xen/arch/x86/hvm/ioreq.c                     |   2 +-
 xen/arch/x86/hvm/nestedhvm.c                 |   2 +-
 xen/arch/x86/hvm/svm/emulate.c               |   4 +-
 xen/arch/x86/hvm/svm/nestedsvm.c             |  13 +-
 xen/arch/x86/hvm/svm/svm.c                   | 102 ++++----
 xen/arch/x86/hvm/vmx/intr.c                  |   2 +-
 xen/arch/x86/hvm/vmx/realmode.c              |  18 +-
 xen/arch/x86/hvm/vmx/vmx.c                   | 107 ++++----
 xen/arch/x86/hvm/vmx/vvmx.c                  |  44 ++--
 xen/arch/x86/mm.c                            |   8 +-
 xen/arch/x86/mm/shadow/common.c              |  29 ++-
 xen/arch/x86/mm/shadow/multi.c               |   4 +-
 xen/arch/x86/x86_emulate/x86_emulate.c       | 328 +++++++++++++-----------
 xen/arch/x86/x86_emulate/x86_emulate.h       | 149 +++++++++--
 xen/include/asm-x86/desc.h                   |   6 +
 xen/include/asm-x86/hvm/emulate.h            |   9 +-
 xen/include/asm-x86/hvm/hvm.h                |  86 +++----
 xen/include/asm-x86/hvm/support.h            |  42 ++-
 xen/include/asm-x86/hvm/svm/nestedsvm.h      |   6 +-
 xen/include/asm-x86/hvm/vcpu.h               |   2 +-
 xen/include/asm-x86/hvm/vmx/vmx.h            |   2 -
 xen/include/asm-x86/hvm/vmx/vvmx.h           |   4 +-
 26 files changed, 901 insertions(+), 679 deletions(-)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 1/15 x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

As hvm_emulate_prepare() was new in 4.8, it would be a good idea to take this patch to avoid future confusion on the stable-4.8 branch

---
 xen/arch/x86/hvm/emulate.c        | 111 +++++++++++++++++++-------------------
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/hvm/io.c             |   2 +-
 xen/arch/x86/hvm/ioreq.c          |   2 +-
 xen/arch/x86/hvm/svm/emulate.c    |   4 +-
 xen/arch/x86/hvm/vmx/realmode.c   |   2 +-
 xen/include/asm-x86/hvm/emulate.h |   6 ++-
 7 files changed, 66 insertions(+), 63 deletions(-)
xen/arch/x86/hvm/emulate.c +56 -55
index e9b8f8c..3ab0e8e 100644
@@ -1755,57 +1755,6 @@ static const struct x86_emulate_ops hvm_emulate_ops_no_write = {
     .vmfunc        = hvmemul_vmfunc,
 };
 
-void hvm_emulate_init(
-    struct hvm_emulate_ctxt *hvmemul_ctxt,
-    const unsigned char *insn_buf,
-    unsigned int insn_bytes)
-{
-    struct vcpu *curr = current;
-    unsigned int pfec = PFEC_page_present;
-    unsigned long addr;
-
-    if ( hvm_long_mode_enabled(curr) &&
-         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
-    {
-        hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
-    }
-    else
-    {
-        hvmemul_ctxt->ctxt.addr_size =
-            hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.db ? 32 : 16;
-        hvmemul_ctxt->ctxt.sp_size =
-            hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db ? 32 : 16;
-    }
-
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
-        pfec |= PFEC_user_mode;
-
-    hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->eip;
-    if ( !insn_bytes )
-    {
-        hvmemul_ctxt->insn_buf_bytes =
-            hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?:
-            (hvm_virtual_to_linear_addr(x86_seg_cs,
-                                        &hvmemul_ctxt->seg_reg[x86_seg_cs],
-                                        hvmemul_ctxt->insn_buf_eip,
-                                        sizeof(hvmemul_ctxt->insn_buf),
-                                        hvm_access_insn_fetch,
-                                        hvmemul_ctxt->ctxt.addr_size,
-                                        &addr) &&
-             hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf, addr,
-                                               sizeof(hvmemul_ctxt->insn_buf),
-                                               pfec) == HVMCOPY_okay) ?
-            sizeof(hvmemul_ctxt->insn_buf) : 0;
-    }
-    else
-    {
-        hvmemul_ctxt->insn_buf_bytes = insn_bytes;
-        memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes);
-    }
-
-    hvmemul_ctxt->exn_pending = 0;
-}
-
 static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
     const struct x86_emulate_ops *ops)
 {
@@ -1815,7 +1764,8 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     int rc;
 
-    hvm_emulate_init(hvmemul_ctxt, vio->mmio_insn, vio->mmio_insn_bytes);
+    hvm_emulate_init_per_insn(hvmemul_ctxt, vio->mmio_insn,
+                              vio->mmio_insn_bytes);
 
     vio->mmio_retry = 0;
 
@@ -1915,7 +1865,7 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla)
     else
         ops = &hvm_ro_emulate_ops_mmio;
 
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
     ctxt.ctxt.data = &mmio_ro_ctxt;
     rc = _hvm_emulate_one(&ctxt, ops);
     switch ( rc )
@@ -1940,7 +1890,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
     struct hvm_emulate_ctxt ctx = {{ 0 }};
     int rc;
 
-    hvm_emulate_prepare(&ctx, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctx, guest_cpu_user_regs());
 
     switch ( kind )
     {
@@ -1992,7 +1942,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
     hvm_emulate_writeback(&ctx);
 }
 
-void hvm_emulate_prepare(
+void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)
 {
@@ -2006,6 +1956,57 @@ void hvm_emulate_prepare(
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
 }
 
+void hvm_emulate_init_per_insn(
+    struct hvm_emulate_ctxt *hvmemul_ctxt,
+    const unsigned char *insn_buf,
+    unsigned int insn_bytes)
+{
+    struct vcpu *curr = current;
+    unsigned int pfec = PFEC_page_present;
+    unsigned long addr;
+
+    if ( hvm_long_mode_enabled(curr) &&
+         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
+    {
+        hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
+    }
+    else
+    {
+        hvmemul_ctxt->ctxt.addr_size =
+            hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.db ? 32 : 16;
+        hvmemul_ctxt->ctxt.sp_size =
+            hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db ? 32 : 16;
+    }
+
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+        pfec |= PFEC_user_mode;
+
+    hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->eip;
+    if ( !insn_bytes )
+    {
+        hvmemul_ctxt->insn_buf_bytes =
+            hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?:
+            (hvm_virtual_to_linear_addr(x86_seg_cs,
+                                        &hvmemul_ctxt->seg_reg[x86_seg_cs],
+                                        hvmemul_ctxt->insn_buf_eip,
+                                        sizeof(hvmemul_ctxt->insn_buf),
+                                        hvm_access_insn_fetch,
+                                        hvmemul_ctxt->ctxt.addr_size,
+                                        &addr) &&
+             hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf, addr,
+                                               sizeof(hvmemul_ctxt->insn_buf),
+                                               pfec) == HVMCOPY_okay) ?
+            sizeof(hvmemul_ctxt->insn_buf) : 0;
+    }
+    else
+    {
+        hvmemul_ctxt->insn_buf_bytes = insn_bytes;
+        memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes);
+    }
+
+    hvmemul_ctxt->exn_pending = 0;
+}
+
 void hvm_emulate_writeback(
     struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
xen/arch/x86/hvm/hvm.c +1 -1
index f76dd90..25dc759 100644
@@ -4058,7 +4058,7 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
 {
     struct hvm_emulate_ctxt ctxt;
 
-    hvm_emulate_prepare(&ctxt, regs);
+    hvm_emulate_init_once(&ctxt, regs);
 
     if ( opt_hvm_fep )
     {
xen/arch/x86/hvm/io.c +1 -1
index 1e7a5f9..7305801 100644
@@ -87,7 +87,7 @@ int handle_mmio(void)
 
     ASSERT(!is_pvh_vcpu(curr));
 
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
 
     rc = hvm_emulate_one(&ctxt);
 
xen/arch/x86/hvm/ioreq.c +1 -1
index d2245e2..88071ab 100644
@@ -167,7 +167,7 @@ bool_t handle_hvm_io_completion(struct vcpu *v)
     {
         struct hvm_emulate_ctxt ctxt;
 
-        hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+        hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
         vmx_realmode_emulate_one(&ctxt);
         hvm_emulate_writeback(&ctxt);
 
xen/arch/x86/hvm/svm/emulate.c +2 -2
index a5545ea..9cdbe9e 100644
@@ -107,8 +107,8 @@ int __get_instruction_length_from_list(struct vcpu *v,
 #endif
 
     ASSERT(v == current);
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
-    hvm_emulate_init(&ctxt, NULL, 0);
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_per_insn(&ctxt, NULL, 0);
     state = x86_decode_insn(&ctxt.ctxt, hvmemul_insn_fetch);
     if ( IS_ERR_OR_NULL(state) )
         return 0;
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index e83a61f..9002638 100644
@@ -179,7 +179,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
-    hvm_emulate_prepare(&hvmemul_ctxt, regs);
+    hvm_emulate_init_once(&hvmemul_ctxt, regs);
 
     /* Only deliver interrupts into emulated real mode. */
     if ( !(curr->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) &&
xen/include/asm-x86/hvm/emulate.h +4 -2
index f610673..d4186a2 100644
@@ -51,10 +51,12 @@ int hvm_emulate_one_no_write(
 void hvm_emulate_one_vm_event(enum emul_kind kind,
     unsigned int trapnr,
     unsigned int errcode);
-void hvm_emulate_prepare(
+/* Must be called once to set up hvmemul state. */
+void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs);
-void hvm_emulate_init(
+/* Must be called once before each instruction emulated. */
+void hvm_emulate_init_per_insn(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     const unsigned char *insn_buf,
     unsigned int insn_bytes);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#8 lore raw

PATCH 2/15 x86/emul: Simplfy emulation state setup

In reply to Andrew Cooper

The current code to set up emulation state is ad-hoc and error prone.

* Consistently zero all emulation state structures.
* Avoid explicitly initialising some state to 0.
* Explicitly identify all input and output state in x86_emulate_ctxt. This involves rearanging some fields.
* Have x86_decode() explicitly initalise all output state at its start.

In addition, move the calculation of hvmemul_ctxt->ctxt.swint_emulate from _hvm_emulate_one() to hvm_emulate_init_once(), as it doesn't need recalculating for each instruction.

---
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>
CC: George Dunlap <[email protected]>
CC: Paul Durrant <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 28 +++++++++++++++-------------
 xen/arch/x86/mm.c                      |  3 ++-
 xen/arch/x86/mm/shadow/common.c        |  4 ++--
 xen/arch/x86/x86_emulate/x86_emulate.c |  2 ++
 xen/arch/x86/x86_emulate/x86_emulate.h | 22 +++++++++++++++-------
 5 files changed, 36 insertions(+), 23 deletions(-)
xen/arch/x86/hvm/emulate.c +15 -13
index 3ab0e8e..f24e211 100644
@@ -1769,13 +1769,6 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
 
     vio->mmio_retry = 0;
 
-    if ( cpu_has_vmx )
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
-    else if ( cpu_has_svm_nrips )
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_icebp;
-    else
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_all;
-
     rc = x86_emulate(&hvmemul_ctxt->ctxt, ops);
 
     if ( rc == X86EMUL_OKAY && vio->mmio_retry )
@@ -1946,14 +1939,23 @@ void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)
 {
-    hvmemul_ctxt->intr_shadow = hvm_funcs.get_interrupt_shadow(current);
-    hvmemul_ctxt->ctxt.regs = regs;
-    hvmemul_ctxt->ctxt.force_writeback = 1;
-    hvmemul_ctxt->seg_reg_accessed = 0;
-    hvmemul_ctxt->seg_reg_dirty = 0;
-    hvmemul_ctxt->set_context = 0;
+    struct vcpu *curr = current;
+
+    memset(hvmemul_ctxt, 0, sizeof(*hvmemul_ctxt));
+
+    hvmemul_ctxt->intr_shadow = hvm_funcs.get_interrupt_shadow(curr);
     hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt);
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
+
+    hvmemul_ctxt->ctxt.regs = regs;
+    hvmemul_ctxt->ctxt.force_writeback = true;
+
+    if ( cpu_has_vmx )
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
+    else if ( cpu_has_svm_nrips )
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_icebp;
+    else
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_all;
 }
 
 void hvm_emulate_init_per_insn(
xen/arch/x86/mm.c +2 -1
index 03dcd71..9901f6f 100644
@@ -5363,8 +5363,9 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
         goto bail;
     }
 
+    memset(&ptwr_ctxt, 0, sizeof(ptwr_ctxt));
+
     ptwr_ctxt.ctxt.regs = regs;
-    ptwr_ctxt.ctxt.force_writeback = 0;
     ptwr_ctxt.ctxt.addr_size = ptwr_ctxt.ctxt.sp_size =
         is_pv_32bit_domain(d) ? 32 : BITS_PER_LONG;
     ptwr_ctxt.ctxt.swint_emulate = x86_swint_emulate_none;
xen/arch/x86/mm/shadow/common.c +2 -2
index ced2313..6f6668d 100644
@@ -385,8 +385,9 @@ const struct x86_emulate_ops *shadow_init_emulation(
     struct vcpu *v = current;
     unsigned long addr;
 
+    memset(sh_ctxt, 0, sizeof(*sh_ctxt));
+
     sh_ctxt->ctxt.regs = regs;
-    sh_ctxt->ctxt.force_writeback = 0;
     sh_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
 
     if ( is_pv_vcpu(v) )
@@ -396,7 +397,6 @@ const struct x86_emulate_ops *shadow_init_emulation(
     }
 
     /* Segment cache initialisation. Primed with CS. */
-    sh_ctxt->valid_seg_regs = 0;
     creg = hvm_get_seg_reg(x86_seg_cs, sh_ctxt);
 
     /* Work out the emulation mode. */
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
index 04f0dac..c5d9664 100644
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;
     ctxt->retire.byte = 0;
 
     op_bytes = def_op_bytes = ad_bytes = def_ad_bytes = ctxt->addr_size/8;
xen/arch/x86/x86_emulate/x86_emulate.h +15 -7
index 993c576..93b268e 100644
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
     /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY). */
     union {
@@ -439,9 +450,6 @@ struct x86_emulate_ctxt
         } flags;
         uint8_t byte;
     } retire;
-
-    /* Caller data that can be used by x86_emulate_ops' routines. */
-    void *data;
 };
 
 /*
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#16 lore raw

PATCH 3/15 x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper

The x86 emulator needs to gain an understanding of interrupts and exceptions generated by its actions. The naming choice is to match both the Intel and AMD terms, and to avoid 'trap' specifically as it has an architectural meaning different to its current usage.

While making this change, make other changes for consistency

* Rename *_trap() infrastructure to *_event()
* Rename trapnr/trap parameters to vector
* Convert hvm_inject_hw_exception() and hvm_inject_page_fault() to being static inlines, as they are only thin wrappers around hvm_inject_event()

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/emulate.c              |  6 +--
 xen/arch/x86/hvm/hvm.c                  | 33 ++++------------
 xen/arch/x86/hvm/io.c                   |  2 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        |  7 ++--
 xen/arch/x86/hvm/svm/svm.c              | 62 ++++++++++++++---------------
 xen/arch/x86/hvm/vmx/vmx.c              | 66 +++++++++++++++----------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 11 +++---
 xen/arch/x86/x86_emulate/x86_emulate.c  | 11 ++++++
 xen/arch/x86/x86_emulate/x86_emulate.h  | 22 +++++++++++
 xen/include/asm-x86/hvm/emulate.h       |  2 +-
 xen/include/asm-x86/hvm/hvm.h           | 69 ++++++++++++++++-----------------
 xen/include/asm-x86/hvm/svm/nestedsvm.h |  6 +--
 xen/include/asm-x86/hvm/vcpu.h          |  2 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |  4 +-
 14 files changed, 159 insertions(+), 144 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -3
index f24e211..d0c3185 100644
@@ -1679,7 +1679,7 @@ static int hvmemul_invlpg(
          * violations, so squash them.
          */
         hvmemul_ctxt->exn_pending = 0;
-        hvmemul_ctxt->trap = (struct hvm_trap){};
+        hvmemul_ctxt->trap = (struct x86_event){};
         rc = X86EMUL_OKAY;
     }
 
@@ -1868,7 +1868,7 @@ int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla)
         break;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         /* fallthrough */
     default:
         hvm_emulate_writeback(&ctxt);
@@ -1928,7 +1928,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
         break;
     case X86EMUL_EXCEPTION:
         if ( ctx.exn_pending )
-            hvm_inject_trap(&ctx.trap);
+            hvm_inject_event(&ctx.trap);
         break;
     }
 
xen/arch/x86/hvm/hvm.c +7 -26
index 25dc759..7b434aa 100644
@@ -535,7 +535,7 @@ void hvm_do_resume(struct vcpu *v)
     /* Inject pending hw/sw trap */
     if ( v->arch.hvm_vcpu.inject_trap.vector != -1 )
     {
-        hvm_inject_trap(&v->arch.hvm_vcpu.inject_trap);
+        hvm_inject_event(&v->arch.hvm_vcpu.inject_trap);
         v->arch.hvm_vcpu.inject_trap.vector = -1;
     }
 }
@@ -1676,19 +1676,19 @@ void hvm_triple_fault(void)
     domain_shutdown(d, reason);
 }
 
-void hvm_inject_trap(const struct hvm_trap *trap)
+void hvm_inject_event(const struct x86_event *event)
 {
     struct vcpu *curr = current;
 
     if ( nestedhvm_enabled(curr->domain) &&
          !nestedhvm_vmswitch_in_progress(curr) &&
          nestedhvm_vcpu_in_guestmode(curr) &&
-         nhvm_vmcx_guest_intercepts_trap(
-             curr, trap->vector, trap->error_code) )
+         nhvm_vmcx_guest_intercepts_event(
+             curr, event->vector, event->error_code) )
     {
         enum nestedhvm_vmexits nsret;
 
-        nsret = nhvm_vcpu_vmexit_trap(curr, trap);
+        nsret = nhvm_vcpu_vmexit_event(curr, event);
 
         switch ( nsret )
         {
@@ -1704,26 +1704,7 @@ void hvm_inject_trap(const struct hvm_trap *trap)
         }
     }
 
-    hvm_funcs.inject_trap(trap);
-}
-
-void hvm_inject_hw_exception(unsigned int trapnr, int errcode)
-{
-    struct hvm_trap trap = {
-        .vector = trapnr,
-        .type = X86_EVENTTYPE_HW_EXCEPTION,
-        .error_code = errcode };
-    hvm_inject_trap(&trap);
-}
-
-void hvm_inject_page_fault(int errcode, unsigned long cr2)
-{
-    struct hvm_trap trap = {
-        .vector = TRAP_page_fault,
-        .type = X86_EVENTTYPE_HW_EXCEPTION,
-        .error_code = errcode,
-        .cr2 = cr2 };
-    hvm_inject_trap(&trap);
+    hvm_funcs.inject_event(event);
 }
 
 int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
@@ -4096,7 +4077,7 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
         break;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         /* fall through */
     default:
         hvm_emulate_writeback(&ctxt);
xen/arch/x86/hvm/io.c +1 -1
index 7305801..1279f68 100644
@@ -103,7 +103,7 @@ int handle_mmio(void)
         return 0;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         break;
     default:
         break;
xen/arch/x86/hvm/svm/nestedsvm.c +4 -3
index f9b38ab..b6b8526 100644
@@ -821,7 +821,7 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
 }
 
 int
-nsvm_vcpu_vmexit_trap(struct vcpu *v, const struct hvm_trap *trap)
+nsvm_vcpu_vmexit_event(struct vcpu *v, const struct x86_event *trap)
 {
     ASSERT(vcpu_nestedhvm(v).nv_vvmcx != NULL);
 
@@ -994,10 +994,11 @@ nsvm_vmcb_guest_intercepts_exitcode(struct vcpu *v,
 }
 
 bool_t
-nsvm_vmcb_guest_intercepts_trap(struct vcpu *v, unsigned int trapnr, int errcode)
+nsvm_vmcb_guest_intercepts_event(
+    struct vcpu *v, unsigned int vector, int errcode)
 {
     return nsvm_vmcb_guest_intercepts_exitcode(v,
-        guest_cpu_user_regs(), VMEXIT_EXCEPTION_DE + trapnr);
+        guest_cpu_user_regs(), VMEXIT_EXCEPTION_DE + vector);
 }
 
 static int
xen/arch/x86/hvm/svm/svm.c +31 -31

126 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vmx.c +33 -33

146 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +6 -5
index bed2e0a..b5837d4 100644
@@ -491,18 +491,19 @@ static void vmreturn(struct cpu_user_regs *regs, enum vmx_ops_result ops_res)
     regs->eflags = eflags;
 }
 
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code)
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code)
 {
     u32 exception_bitmap, pfec_match=0, pfec_mask=0;
     int r;
 
-    ASSERT ( trap < 32 );
+    ASSERT(vector < 32);
 
     exception_bitmap = get_vvmcs(v, EXCEPTION_BITMAP);
-    r = exception_bitmap & (1 << trap) ? 1: 0;
+    r = exception_bitmap & (1 << vector) ? 1: 0;
 
-    if ( trap == TRAP_page_fault ) {
+    if ( vector == TRAP_page_fault )
+    {
         pfec_match = get_vvmcs(v, PAGE_FAULT_ERROR_CODE_MATCH);
         pfec_mask  = get_vvmcs(v, PAGE_FAULT_ERROR_CODE_MASK);
         if ( (error_code & pfec_mask) != pfec_match )
xen/arch/x86/x86_emulate/x86_emulate.c +11 -0
index c5d9664..2e65322 100644
@@ -5453,6 +5453,17 @@ static void __init __maybe_unused build_assertions(void)
     BUILD_BUG_ON(x86_seg_ds != 3);
     BUILD_BUG_ON(x86_seg_fs != 4);
     BUILD_BUG_ON(x86_seg_gs != 5);
+
+    /*
+     * Check X86_EVENTTYPE_* against VMCB EVENTINJ and VMCS INTR_INFO type
+     * fields.
+     */
+    BUILD_BUG_ON(X86_EVENTTYPE_EXT_INTR != 0);
+    BUILD_BUG_ON(X86_EVENTTYPE_NMI != 2);
+    BUILD_BUG_ON(X86_EVENTTYPE_HW_EXCEPTION != 3);
+    BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
+    BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
+    BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
 }
 
 #ifdef __XEN__
xen/arch/x86/x86_emulate/x86_emulate.h +22 -0
index 93b268e..b0d8e6f 100644
@@ -67,6 +67,28 @@ enum x86_swint_emulation {
     x86_swint_emulate_all,  /* Help needed with all software events */
 };
 
+/*
+ * x86 event types. This enumeration is valid for:
+ *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
+ *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
+ */
+enum x86_event_type {
+    X86_EVENTTYPE_EXT_INTR,         /* External interrupt */
+    X86_EVENTTYPE_NMI = 2,          /* NMI */
+    X86_EVENTTYPE_HW_EXCEPTION,     /* Hardware exception */
+    X86_EVENTTYPE_SW_INTERRUPT,     /* Software interrupt (CD nn) */
+    X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
+    X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
+};
+
+struct x86_event {
+    int16_t       vector;
+    uint8_t       type;         /* X86_EVENTTYPE_* */
+    uint8_t       insn_len;     /* Instruction length */
+    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
+};
+
 /* 
  * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
  * segment descriptor. It happens to match the format of an AMD SVM VMCB.
xen/include/asm-x86/hvm/emulate.h +1 -1
index d4186a2..3b7ec33 100644
@@ -30,7 +30,7 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_dirty;
 
     bool_t exn_pending;
-    struct hvm_trap trap;
+    struct x86_event trap;
 
     uint32_t intr_shadow;
 
xen/include/asm-x86/hvm/hvm.h +34 -35

114 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/nestedsvm.h +3 -3
index 0dbc5ec..4b36c25 100644
@@ -110,10 +110,10 @@ void nsvm_vcpu_destroy(struct vcpu *v);
 int nsvm_vcpu_initialise(struct vcpu *v);
 int nsvm_vcpu_reset(struct vcpu *v);
 int nsvm_vcpu_vmrun(struct vcpu *v, struct cpu_user_regs *regs);
-int nsvm_vcpu_vmexit_trap(struct vcpu *v, const struct hvm_trap *trap);
+int nsvm_vcpu_vmexit_event(struct vcpu *v, const struct x86_event *event);
 uint64_t nsvm_vcpu_hostcr3(struct vcpu *v);
-bool_t nsvm_vmcb_guest_intercepts_trap(struct vcpu *v, unsigned int trapnr,
-                                       int errcode);
+bool_t nsvm_vmcb_guest_intercepts_event(
+    struct vcpu *v, unsigned int vector, int errcode);
 bool_t nsvm_vmcb_hap_enabled(struct vcpu *v);
 enum hvm_intblk nsvm_intr_blocked(struct vcpu *v);
 
xen/include/asm-x86/hvm/vcpu.h +1 -1
index 84d9406..d485536 100644
@@ -206,7 +206,7 @@ struct hvm_vcpu {
     void *fpu_exception_callback_arg;
 
     /* Pending hw/sw interrupt (.vector = -1 means nothing pending). */
-    struct hvm_trap     inject_trap;
+    struct x86_event     inject_trap;
 
     struct viridian_vcpu viridian;
 };
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -2
index aca8b4b..ead586e 100644
@@ -112,8 +112,8 @@ void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
 uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code);
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code);
 void nvmx_domain_relinquish_resources(struct domain *d);
 
 bool_t nvmx_ept_enabled(struct vcpu *v);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#24 lore raw

PATCH 4/15 x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

In reply to Andrew Cooper

and move it to live with the other x86_event infrastructure in x86_emulate.h. Switch it and x86_event.error_code to being signed, matching the rest of the code.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             |  2 +-
 xen/arch/x86/hvm/hvm.c                 |  6 +++---
 xen/arch/x86/hvm/nestedhvm.c           |  2 +-
 xen/arch/x86/hvm/svm/nestedsvm.c       |  6 +++---
 xen/arch/x86/hvm/svm/svm.c             | 22 +++++++++++-----------
 xen/arch/x86/hvm/vmx/intr.c            |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c             | 23 ++++++++++++-----------
 xen/arch/x86/hvm/vmx/vvmx.c            |  2 +-
 xen/arch/x86/x86_emulate/x86_emulate.h |  3 ++-
 xen/include/asm-x86/hvm/support.h      |  2 --
 10 files changed, 35 insertions(+), 35 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
index d0c3185..790e9c1 100644
@@ -1609,7 +1609,7 @@ static int hvmemul_inject_sw_interrupt(
 
     hvmemul_ctxt->exn_pending = 1;
     hvmemul_ctxt->trap.vector = vector;
-    hvmemul_ctxt->trap.error_code = HVM_DELIVER_NO_ERROR_CODE;
+    hvmemul_ctxt->trap.error_code = X86_EVENT_NO_EC;
     hvmemul_ctxt->trap.insn_len = insn_len;
 
     return X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +3 -3
index 7b434aa..b950842 100644
@@ -502,7 +502,7 @@ void hvm_do_resume(struct vcpu *v)
                 kind = EMUL_KIND_SET_CONTEXT_INSN;
 
             hvm_emulate_one_vm_event(kind, TRAP_invalid_op,
-                                     HVM_DELIVER_NO_ERROR_CODE);
+                                     X86_EVENT_NO_EC);
 
             v->arch.vm_event->emulate_flags = 0;
         }
@@ -3054,7 +3054,7 @@ void hvm_task_switch(
     }
 
     if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 
  out:
     hvm_unmap_entry(optss_desc);
@@ -4073,7 +4073,7 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
     switch ( hvm_emulate_one(&ctxt) )
     {
     case X86EMUL_UNHANDLEABLE:
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
xen/arch/x86/hvm/nestedhvm.c +1 -1
index caad525..c4671d8 100644
@@ -17,7 +17,7 @@
  */
 
 #include <asm/msr.h>
-#include <asm/hvm/support.h>	/* for HVM_DELIVER_NO_ERROR_CODE */
+#include <asm/hvm/support.h>
 #include <asm/hvm/hvm.h>
 #include <asm/p2m.h>    /* for struct p2m_domain */
 #include <asm/hvm/nestedhvm.h>
xen/arch/x86/hvm/svm/nestedsvm.c +3 -3
index b6b8526..8c9b073 100644
@@ -756,7 +756,7 @@ nsvm_vcpu_vmrun(struct vcpu *v, struct cpu_user_regs *regs)
     default:
         gdprintk(XENLOG_ERR,
             "nsvm_vcpu_vmentry failed, injecting #UD\n");
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         /* Must happen after hvm_inject_hw_exception or it doesn't work right. */
         nv->nv_vmswitch_in_progress = 0;
         return 1;
@@ -1581,7 +1581,7 @@ void svm_vmexit_do_stgi(struct cpu_user_regs *regs, struct vcpu *v)
     unsigned int inst_len;
 
     if ( !nestedhvm_enabled(v->domain) ) {
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
 
@@ -1601,7 +1601,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v)
     vintr_t intr;
 
     if ( !nestedhvm_enabled(v->domain) ) {
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
 
xen/arch/x86/hvm/svm/svm.c +11 -11
index 66eb30b..fb4fd0b 100644
@@ -89,7 +89,7 @@ static DEFINE_SPINLOCK(osvw_lock);
 static void svm_crash_or_fault(struct vcpu *v)
 {
     if ( vmcb_get_cpl(v->arch.hvm_svm.vmcb) )
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
     else
         domain_crash(v->domain);
 }
@@ -116,7 +116,7 @@ void __update_guest_eip(struct cpu_user_regs *regs, unsigned int inst_len)
     curr->arch.hvm_svm.vmcb->interrupt_shadow = 0;
 
     if ( regs->eflags & X86_EFLAGS_TF )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 }
 
 static void svm_cpu_down(void)
@@ -1285,7 +1285,7 @@ static void svm_inject_event(const struct x86_event *event)
 
     default:
         eventinj.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
-        eventinj.fields.ev = (_event.error_code != HVM_DELIVER_NO_ERROR_CODE);
+        eventinj.fields.ev = (_event.error_code != X86_EVENT_NO_EC);
         eventinj.fields.errorcode = _event.error_code;
         break;
     }
@@ -1553,7 +1553,7 @@ static void svm_fpu_dirty_intercept(void)
     {
        /* Check if l1 guest must make FPU ready for the l2 guest */
        if ( v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_TS )
-           hvm_inject_hw_exception(TRAP_no_device, HVM_DELIVER_NO_ERROR_CODE);
+           hvm_inject_hw_exception(TRAP_no_device, X86_EVENT_NO_EC);
        else
            vmcb_set_cr0(n1vmcb, vmcb_get_cr0(n1vmcb) & ~X86_CR0_TS);
        return;
@@ -2022,14 +2022,14 @@ svm_vmexit_do_vmrun(struct cpu_user_regs *regs,
     if ( !nsvm_efer_svm_enabled(v) )
     {
         gdprintk(XENLOG_ERR, "VMRUN: nestedhvm disabled, injecting #UD\n");
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
 
     if ( !nestedsvm_vmcb_map(v, vmcbaddr) )
     {
         gdprintk(XENLOG_ERR, "VMRUN: mapping vmcb failed, injecting #GP\n");
-        hvm_inject_hw_exception(TRAP_gp_fault, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_gp_fault, X86_EVENT_NO_EC);
         return;
     }
 
@@ -2101,7 +2101,7 @@ svm_vmexit_do_vmload(struct vmcb_struct *vmcb,
     return;
 
  inject:
-    hvm_inject_hw_exception(ret, HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(ret, X86_EVENT_NO_EC);
     return;
 }
 
@@ -2139,7 +2139,7 @@ svm_vmexit_do_vmsave(struct vmcb_struct *vmcb,
     return;
 
  inject:
-    hvm_inject_hw_exception(ret, HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(ret, X86_EVENT_NO_EC);
     return;
 }
 
@@ -2428,7 +2428,7 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
 
     case VMEXIT_EXCEPTION_DB:
         if ( !v->domain->debugger_attached )
-            hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+            hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
         else
             domain_pause_for_debugger();
         break;
@@ -2616,7 +2616,7 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
 
     case VMEXIT_MONITOR:
     case VMEXIT_MWAIT:
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;
 
     case VMEXIT_VMRUN:
@@ -2635,7 +2635,7 @@ void svm_vmexit_handler(struct cpu_user_regs *regs)
         svm_vmexit_do_clgi(regs, v);
         break;
     case VMEXIT_SKINIT:
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;
 
     case VMEXIT_XSETBV:
xen/arch/x86/hvm/vmx/intr.c +1 -1
index 8fca08c..639a705 100644
@@ -302,7 +302,7 @@ void vmx_intr_assist(void)
     }
     else if ( intack.source == hvm_intsrc_mce )
     {
-        hvm_inject_hw_exception(TRAP_machine_check, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_machine_check, X86_EVENT_NO_EC);
     }
     else if ( cpu_has_vmx_virtual_intr_delivery &&
               intack.source != hvm_intsrc_pic &&
xen/arch/x86/hvm/vmx/vmx.c +12 -11
index b1d8a0b..eb7c902 100644
@@ -1646,7 +1646,8 @@ static void __vmx_inject_exception(int trap, int type, int error_code)
     intr_fields = INTR_INFO_VALID_MASK |
                   MASK_INSR(type, INTR_INFO_INTR_TYPE_MASK) |
                   MASK_INSR(trap, INTR_INFO_VECTOR_MASK);
-    if ( error_code != HVM_DELIVER_NO_ERROR_CODE ) {
+    if ( error_code != X86_EVENT_NO_EC )
+    {
         __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
         intr_fields |= INTR_INFO_DELIVER_CODE_MASK;
     }
@@ -1671,12 +1672,12 @@ void vmx_inject_extint(int trap, uint8_t source)
                INTR_INFO_VALID_MASK |
                MASK_INSR(X86_EVENTTYPE_EXT_INTR, INTR_INFO_INTR_TYPE_MASK) |
                MASK_INSR(trap, INTR_INFO_VECTOR_MASK),
-               HVM_DELIVER_NO_ERROR_CODE, source);
+               X86_EVENT_NO_EC, source);
             return;
         }
     }
     __vmx_inject_exception(trap, X86_EVENTTYPE_EXT_INTR,
-                           HVM_DELIVER_NO_ERROR_CODE);
+                           X86_EVENT_NO_EC);
 }
 
 void vmx_inject_nmi(void)
@@ -1691,12 +1692,12 @@ void vmx_inject_nmi(void)
                INTR_INFO_VALID_MASK |
                MASK_INSR(X86_EVENTTYPE_NMI, INTR_INFO_INTR_TYPE_MASK) |
                MASK_INSR(TRAP_nmi, INTR_INFO_VECTOR_MASK),
-               HVM_DELIVER_NO_ERROR_CODE, hvm_intsrc_nmi);
+               X86_EVENT_NO_EC, hvm_intsrc_nmi);
             return;
         }
     }
     __vmx_inject_exception(2, X86_EVENTTYPE_NMI,
-                           HVM_DELIVER_NO_ERROR_CODE);
+                           X86_EVENT_NO_EC);
 }
 
 /*
@@ -2111,7 +2112,7 @@ static bool_t vmx_vcpu_emulate_ve(struct vcpu *v)
     vmx_vmcs_exit(v);
 
     hvm_inject_hw_exception(TRAP_virtualisation,
-                            HVM_DELIVER_NO_ERROR_CODE);
+                            X86_EVENT_NO_EC);
 
  out:
     hvm_unmap_guest_frame(veinfo, 0);
@@ -2387,7 +2388,7 @@ void update_guest_eip(void)
     }
 
     if ( regs->eflags & X86_EFLAGS_TF )
-        hvm_inject_hw_exception(TRAP_debug, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 }
 
 static void vmx_fpu_dirty_intercept(void)
@@ -3213,7 +3214,7 @@ static void vmx_propagate_intr(unsigned long intr)
         event.error_code = tmp;
     }
     else
-        event.error_code = HVM_DELIVER_NO_ERROR_CODE;
+        event.error_code = X86_EVENT_NO_EC;
 
     if ( event.type >= X86_EVENTTYPE_SW_INTERRUPT )
     {
@@ -3770,7 +3771,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
 
     case EXIT_REASON_VMFUNC:
         if ( vmx_vmfunc_intercept(regs) != X86EMUL_OKAY )
-            hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         else
             update_guest_eip();
         break;
@@ -3784,7 +3785,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
          * as far as vmexit.
          */
         WARN_ON(exit_reason == EXIT_REASON_GETSEC);
-        hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;
 
     case EXIT_REASON_TPR_BELOW_THRESHOLD:
@@ -3909,7 +3910,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             vmx_get_segment_register(v, x86_seg_ss, &ss);
             if ( ss.attr.fields.dpl )
                 hvm_inject_hw_exception(TRAP_invalid_op,
-                                        HVM_DELIVER_NO_ERROR_CODE);
+                                        X86_EVENT_NO_EC);
             else
                 domain_crash(v->domain);
         }
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
index b5837d4..efaf54c 100644
@@ -380,7 +380,7 @@ static int vmx_inst_check_privilege(struct cpu_user_regs *regs, int vmxop_check)
     
 invalid_op:
     gdprintk(XENLOG_ERR, "vmx_inst_check_privilege: invalid_op\n");
-    hvm_inject_hw_exception(TRAP_invalid_op, HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
     return X86EMUL_EXCEPTION;
 
 gp_fault:
xen/arch/x86/x86_emulate/x86_emulate.h +2 -1
index b0d8e6f..9df083e 100644
@@ -80,12 +80,13 @@ enum x86_event_type {
     X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
     X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
 };
+#define X86_EVENT_NO_EC (-1)        /* No error code. */
 
 struct x86_event {
     int16_t       vector;
     uint8_t       type;         /* X86_EVENTTYPE_* */
     uint8_t       insn_len;     /* Instruction length */
-    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    int32_t       error_code;   /* X86_EVENT_NO_EC if n/a */
     unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
 };
 
xen/include/asm-x86/hvm/support.h +0 -2
index 2984abc..9938450 100644
@@ -25,8 +25,6 @@
 #include <xen/hvm/save.h>
 #include <asm/processor.h>
 
-#define HVM_DELIVER_NO_ERROR_CODE  (~0U)
-
 #ifndef NDEBUG
 #define DBG_LEVEL_0                 (1 << 0)
 #define DBG_LEVEL_1                 (1 << 1)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#29 lore raw

PATCH 5/15 x86/emul: Remove opencoded exception generation

In reply to Andrew Cooper

Introduce generate_exception() for unconditional exception generation, and replace existing uses. Both generate_exception() and generate_exception_if() are updated to make their error code parameters optional, which removes the use of the -1 sentinal.

The ioport_access_check() check loses the presence check for %tr, as the x86 architecture has no concept of a non-usable task register.

No functional change.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 196 +++++++++++++++++----------------
 1 file changed, 100 insertions(+), 96 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +100 -96
index 2e65322..f8271b3 100644
@@ -457,14 +457,20 @@ typedef union {
 #define EXC_BR  5
 #define EXC_UD  6
 #define EXC_NM  7
+#define EXC_DF  8
 #define EXC_TS 10
 #define EXC_NP 11
 #define EXC_SS 12
 #define EXC_GP 13
 #define EXC_PF 14
 #define EXC_MF 16
+#define EXC_AC 17
 #define EXC_XM 19
 
+#define EXC_HAS_EC                                                      \
+    ((1u << EXC_DF) | (1u << EXC_TS) | (1u << EXC_NP) |                 \
+     (1u << EXC_SS) | (1u << EXC_GP) | (1u << EXC_PF) | (1u << EXC_AC))
+
 /* Segment selector error code bits. */
 #define ECODE_EXT (1 << 0)
 #define ECODE_IDT (1 << 1)
@@ -667,14 +673,22 @@ do {                                                    \
     if ( rc ) goto done;                                \
 } while (0)
 
-#define generate_exception_if(p, e, ec)                                   \
+static inline int mkec(uint8_t e, int32_t ec, ...)
+{
+    return (e < 32 && (1u << e) & EXC_HAS_EC) ? ec : X86_EVENT_NO_EC;
+}
+
+#define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
         fail_if(ops->inject_hw_exception == NULL);                        \
-        rc = ops->inject_hw_exception(e, ec, ctxt) ? : X86EMUL_EXCEPTION; \
+        rc = ops->inject_hw_exception(e, mkec(e, ##ec, 0), ctxt)          \
+            ? : X86EMUL_EXCEPTION;                                        \
         goto done;                                                        \
     }                                                                     \
 })
 
+#define generate_exception(e, ec...) generate_exception_if(true, e, ##ec)
+
 /*
  * Given byte has even parity (even number of 1s)? SDM Vol. 1 Sec. 3.4.3.1,
  * "Status Flags": EFLAGS.PF reflects parity of least-sig. byte of result only.
@@ -785,7 +799,7 @@ static int _get_fpu(
                 return rc;
             generate_exception_if(!(cr4 & ((type == X86EMUL_FPU_xmm)
                                            ? CR4_OSFXSR : CR4_OSXSAVE)),
-                                  EXC_UD, -1);
+                                  EXC_UD);
         }
 
         rc = ops->read_cr(0, &cr0, ctxt);
@@ -798,13 +812,13 @@ static int _get_fpu(
         }
         if ( cr0 & CR0_EM )
         {
-            generate_exception_if(type == X86EMUL_FPU_fpu, EXC_NM, -1);
-            generate_exception_if(type == X86EMUL_FPU_mmx, EXC_UD, -1);
-            generate_exception_if(type == X86EMUL_FPU_xmm, EXC_UD, -1);
+            generate_exception_if(type == X86EMUL_FPU_fpu, EXC_NM);
+            generate_exception_if(type == X86EMUL_FPU_mmx, EXC_UD);
+            generate_exception_if(type == X86EMUL_FPU_xmm, EXC_UD);
         }
         generate_exception_if((cr0 & CR0_TS) &&
                               (type != X86EMUL_FPU_wait || (cr0 & CR0_MP)),
-                              EXC_NM, -1);
+                              EXC_NM);
     }
 
  done:
@@ -832,7 +846,7 @@ do {                                                            \
             (_fic)->exn_raised = EXC_UD;                        \
     }                                                           \
     generate_exception_if((_fic)->exn_raised >= 0,              \
-                          (_fic)->exn_raised, -1);              \
+                          (_fic)->exn_raised);                  \
 } while (0)
 
 #define emulate_fpu_insn(_op)                           \
@@ -1167,11 +1181,9 @@ static int ioport_access_check(
     if ( (rc = ops->read_segment(x86_seg_tr, &tr, ctxt)) != 0 )
         return rc;
 
-    /* Ensure that the TSS is valid and has an io-bitmap-offset field. */
-    if ( !tr.attr.fields.p ||
-         ((tr.attr.fields.type & 0xd) != 0x9) ||
-         (tr.limit < 0x67) )
-        goto raise_exception;
+    /* Ensure the TSS has an io-bitmap-offset field. */
+    generate_exception_if(tr.attr.fields.type != 0xb ||
+                          tr.limit < 0x67, EXC_GP, 0);
 
     if ( (rc = read_ulong(x86_seg_none, tr.base + 0x66,
                           &iobmp, 2, ctxt, ops)) )
@@ -1179,21 +1191,16 @@ static int ioport_access_check(
 
     /* Ensure TSS includes two bytes including byte containing first port. */
     iobmp += first_port / 8;
-    if ( tr.limit <= iobmp )
-        goto raise_exception;
+    generate_exception_if(tr.limit <= iobmp, EXC_GP, 0);
 
     if ( (rc = read_ulong(x86_seg_none, tr.base + iobmp,
                           &iobmp, 2, ctxt, ops)) )
         return rc;
-    if ( (iobmp & (((1<<bytes)-1) << (first_port&7))) != 0 )
-        goto raise_exception;
+    generate_exception_if(iobmp & (((1 << bytes) - 1) << (first_port & 7)),
+                          EXC_GP, 0);
 
  done:
     return rc;
-
- raise_exception:
-    fail_if(ops->inject_hw_exception == NULL);
-    return ops->inject_hw_exception(EXC_GP, 0, ctxt) ? : X86EMUL_EXCEPTION;
 }
 
 static bool_t
@@ -1262,7 +1269,7 @@ static bool_t vcpu_has(
 #define vcpu_has_rtm()   vcpu_has(0x00000007, EBX, 11, ctxt, ops)
 
 #define vcpu_must_have(leaf, reg, bit) \
-    generate_exception_if(!vcpu_has(leaf, reg, bit, ctxt, ops), EXC_UD, -1)
+    generate_exception_if(!vcpu_has(leaf, reg, bit, ctxt, ops), EXC_UD)
 #define vcpu_must_have_fpu()  vcpu_must_have(0x00000001, EDX, 0)
 #define vcpu_must_have_cmov() vcpu_must_have(0x00000001, EDX, 15)
 #define vcpu_must_have_mmx()  vcpu_must_have(0x00000001, EDX, 23)
@@ -1282,7 +1289,7 @@ static bool_t vcpu_has(
  * the actual operation.
  */
 #define host_and_vcpu_must_have(feat) ({ \
-    generate_exception_if(!cpu_has_##feat, EXC_UD, -1); \
+    generate_exception_if(!cpu_has_##feat, EXC_UD); \
     vcpu_must_have_##feat(); \
 })
 #else
@@ -1485,11 +1492,9 @@ protmode_load_seg(
     return X86EMUL_OKAY;
 
  raise_exn:
-    if ( ops->inject_hw_exception == NULL )
-        return X86EMUL_UNHANDLEABLE;
-    if ( (rc = ops->inject_hw_exception(fault_type, sel & 0xfffc, ctxt)) )
-        return rc;
-    return X86EMUL_EXCEPTION;
+    generate_exception(fault_type, sel & 0xfffc);
+ done:
+    return rc;
 }
 
 static int
@@ -1704,7 +1709,7 @@ static int inject_swint(enum x86_swint_type type,
     return rc;
 
  raise_exn:
-    return ops->inject_hw_exception(fault_type, error_code, ctxt);
+    generate_exception(fault_type, error_code);
 }
 
 int x86emul_unhandleable_rw(
@@ -1795,7 +1800,7 @@ x86_decode_onebyte(
 
     case 0x9a: /* call (far, absolute) */
     case 0xea: /* jmp (far, absolute) */
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
 
         imm1 = insn_fetch_bytes(op_bytes);
         imm2 = insn_fetch_type(uint16_t);
@@ -2024,7 +2029,7 @@ x86_decode(
                 /* fall through */
             case 8:
                 /* VEX / XOP / EVEX */
-                generate_exception_if(rex_prefix || vex.pfx, EXC_UD, -1);
+                generate_exception_if(rex_prefix || vex.pfx, EXC_UD);
 
                 vex.raw[0] = modrm;
                 if ( b == 0xc5 )
@@ -2514,12 +2519,12 @@ x86_emulate(
             (ext != ext_0f ||
              (((b < 0x20) || (b > 0x23)) && /* MOV CRn/DRn */
               (b != 0xc7))),                /* CMPXCHG{8,16}B */
-            EXC_UD, -1);
+            EXC_UD);
         dst.type = OP_NONE;
         break;
 
     case DstReg:
-        generate_exception_if(lock_prefix, EXC_UD, -1);
+        generate_exception_if(lock_prefix, EXC_UD);
         dst.type = OP_REG;
         if ( d & ByteOp )
         {
@@ -2575,7 +2580,7 @@ x86_emulate(
         dst = ea;
         if ( dst.type == OP_REG )
         {
-            generate_exception_if(lock_prefix, EXC_UD, -1);
+            generate_exception_if(lock_prefix, EXC_UD);
             switch ( dst.bytes )
             {
             case 1: dst.val = *(uint8_t  *)dst.reg; break;
@@ -2592,7 +2597,7 @@ x86_emulate(
             dst.orig_val = dst.val;
         }
         else /* Lock prefix is allowed only on RMW instructions. */
-            generate_exception_if(lock_prefix, EXC_UD, -1);
+            generate_exception_if(lock_prefix, EXC_UD);
         break;
     }
 
@@ -2630,7 +2635,7 @@ x86_emulate(
         break;
 
     case 0x38 ... 0x3d: cmp: /* cmp */
-        generate_exception_if(lock_prefix, EXC_UD, -1);
+        generate_exception_if(lock_prefix, EXC_UD);
         emulate_2op_SrcV("cmp", src, dst, _regs.eflags);
         dst.type = OP_NONE;
         break;
@@ -2638,7 +2643,7 @@ x86_emulate(
     case 0x06: /* push %%es */
         src.val = x86_seg_es;
     push_seg:
-        generate_exception_if(mode_64bit() && !ext, EXC_UD, -1);
+        generate_exception_if(mode_64bit() && !ext, EXC_UD);
         fail_if(ops->read_segment == NULL);
         if ( (rc = ops->read_segment(src.val, &sreg, ctxt)) != 0 )
             goto done;
@@ -2648,7 +2653,7 @@ x86_emulate(
     case 0x07: /* pop %%es */
         src.val = x86_seg_es;
     pop_seg:
-        generate_exception_if(mode_64bit() && !ext, EXC_UD, -1);
+        generate_exception_if(mode_64bit() && !ext, EXC_UD);
         fail_if(ops->write_segment == NULL);
         /* 64-bit mode: POP defaults to a 64-bit operand. */
         if ( mode_64bit() && (op_bytes == 4) )
@@ -2685,7 +2690,7 @@ x86_emulate(
         uint8_t al = _regs.eax;
         unsigned long eflags = _regs.eflags;
 
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         _regs.eflags &= ~(EFLG_CF|EFLG_AF|EFLG_SF|EFLG_ZF|EFLG_PF);
         if ( ((al & 0x0f) > 9) || (eflags & EFLG_AF) )
         {
@@ -2707,7 +2712,7 @@ x86_emulate(
 
     case 0x37: /* aaa */
     case 0x3f: /* aas */
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         _regs.eflags &= ~EFLG_CF;
         if ( ((uint8_t)_regs.eax > 9) || (_regs.eflags & EFLG_AF) )
         {
@@ -2751,7 +2756,7 @@ x86_emulate(
         unsigned long regs[] = {
             _regs.eax, _regs.ecx, _regs.edx, _regs.ebx,
             _regs.esp, _regs.ebp, _regs.esi, _regs.edi };
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         for ( i = 0; i < 8; i++ )
             if ( (rc = ops->write(x86_seg_ss, sp_pre_dec(op_bytes),
                                   &regs[i], op_bytes, ctxt)) != 0 )
@@ -2766,7 +2771,7 @@ x86_emulate(
             (unsigned long *)&_regs.ebp, (unsigned long *)&dummy_esp,
             (unsigned long *)&_regs.ebx, (unsigned long *)&_regs.edx,
             (unsigned long *)&_regs.ecx, (unsigned long *)&_regs.eax };
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         for ( i = 0; i < 8; i++ )
         {
             if ( (rc = read_ulong(x86_seg_ss, sp_post_inc(op_bytes),
@@ -2784,14 +2789,14 @@ x86_emulate(
         unsigned long src_val2;
         int lb, ub, idx;
         generate_exception_if(mode_64bit() || (src.type != OP_MEM),
-                              EXC_UD, -1);
+                              EXC_UD);
         if ( (rc = read_ulong(src.mem.seg, src.mem.off + op_bytes,
                               &src_val2, op_bytes, ctxt, ops)) )
             goto done;
         ub  = (op_bytes == 2) ? (int16_t)src_val2 : (int32_t)src_val2;
         lb  = (op_bytes == 2) ? (int16_t)src.val  : (int32_t)src.val;
         idx = (op_bytes == 2) ? (int16_t)dst.val  : (int32_t)dst.val;
-        generate_exception_if((idx < lb) || (idx > ub), EXC_BR, -1);
+        generate_exception_if((idx < lb) || (idx > ub), EXC_BR);
         dst.type = OP_NONE;
         break;
     }
@@ -2829,7 +2834,7 @@ x86_emulate(
                 _regs.eflags &= ~EFLG_ZF;
                 dst.type = OP_NONE;
             }
-            generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
+            generate_exception_if(!in_protmode(ctxt, ops), EXC_UD);
         }
         break;
 
@@ -2920,7 +2925,7 @@ x86_emulate(
         break;
 
     case 0x82: /* Grp1 (x86/32 only) */
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
     case 0x80: case 0x81: case 0x83: /* Grp1 */
         switch ( modrm_reg & 7 )
         {
@@ -2971,7 +2976,7 @@ x86_emulate(
             dst.type = OP_NONE;
             break;
         }
-        generate_exception_if((modrm_reg & 7) != 0, EXC_UD, -1);
+        generate_exception_if((modrm_reg & 7) != 0, EXC_UD);
     case 0x88 ... 0x8b: /* mov */
     case 0xa0 ... 0xa1: /* mov mem.offs,{%al,%ax,%eax,%rax} */
     case 0xa2 ... 0xa3: /* mov {%al,%ax,%eax,%rax},mem.offs */
@@ -2980,7 +2985,7 @@ x86_emulate(
 
     case 0x8c: /* mov Sreg,r/m */
         seg = modrm_reg & 7; /* REX.R is ignored. */
-        generate_exception_if(!is_x86_user_segment(seg), EXC_UD, -1);
+        generate_exception_if(!is_x86_user_segment(seg), EXC_UD);
     store_selector:
         fail_if(ops->read_segment == NULL);
         if ( (rc = ops->read_segment(seg, &sreg, ctxt)) != 0 )
@@ -2993,7 +2998,7 @@ x86_emulate(
     case 0x8e: /* mov r/m,Sreg */
         seg = modrm_reg & 7; /* REX.R is ignored. */
         generate_exception_if(!is_x86_user_segment(seg) ||
-                              seg == x86_seg_cs, EXC_UD, -1);
+                              seg == x86_seg_cs, EXC_UD);
         if ( (rc = load_seg(seg, src.val, 0, NULL, ctxt, ops)) != 0 )
             goto done;
         if ( seg == x86_seg_ss )
@@ -3002,12 +3007,12 @@ x86_emulate(
         break;
 
     case 0x8d: /* lea */
-        generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+        generate_exception_if(ea.type != OP_MEM, EXC_UD);
         dst.val = ea.mem.off;
         break;
 
     case 0x8f: /* pop (sole member of Grp1a) */
-        generate_exception_if((modrm_reg & 7) != 0, EXC_UD, -1);
+        generate_exception_if((modrm_reg & 7) != 0, EXC_UD);
         /* 64-bit mode: POP defaults to a 64-bit operand. */
         if ( mode_64bit() && (dst.bytes == 4) )
             dst.bytes = 8;
@@ -3284,8 +3289,8 @@ x86_emulate(
         unsigned long sel;
         dst.val = x86_seg_es;
     les: /* dst.val identifies the segment */
-        generate_exception_if(mode_64bit() && !ext, EXC_UD, -1);
-        generate_exception_if(src.type != OP_MEM, EXC_UD, -1);
+        generate_exception_if(mode_64bit() && !ext, EXC_UD);
+        generate_exception_if(src.type != OP_MEM, EXC_UD);
         if ( (rc = read_ulong(src.mem.seg, src.mem.off + src.bytes,
                               &sel, 2, ctxt, ops)) != 0 )
             goto done;
@@ -3375,7 +3380,7 @@ x86_emulate(
         goto done;
 
     case 0xce: /* into */
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         if ( !(_regs.eflags & EFLG_OF) )
             break;
         src.val = EXC_OF;
@@ -3417,7 +3422,7 @@ x86_emulate(
     case 0xd5: /* aad */ {
         unsigned int base = (uint8_t)src.val;
 
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         if ( b & 0x01 )
         {
             uint16_t ax = _regs.eax;
@@ -3428,7 +3433,7 @@ x86_emulate(
         {
             uint8_t al = _regs.eax;
 
-            generate_exception_if(!base, EXC_DE, -1);
+            generate_exception_if(!base, EXC_DE);
             *(uint16_t *)&_regs.eax = ((al / base) << 8) | (al % base);
         }
         _regs.eflags &= ~(EFLG_SF|EFLG_ZF|EFLG_PF);
@@ -3439,7 +3444,7 @@ x86_emulate(
     }
 
     case 0xd6: /* salc */
-        generate_exception_if(mode_64bit(), EXC_UD, -1);
+        generate_exception_if(mode_64bit(), EXC_UD);
         *(uint8_t *)&_regs.eax = (_regs.eflags & EFLG_CF) ? 0xff : 0x00;
         break;
 
@@ -4047,7 +4052,7 @@ x86_emulate(
             unsigned long u[2], v;
 
         case 0 ... 1: /* test */
-            generate_exception_if(lock_prefix, EXC_UD, -1);
+            generate_exception_if(lock_prefix, EXC_UD);
             goto test;
         case 2: /* not */
             dst.val = ~dst.val;
@@ -4145,7 +4150,7 @@ x86_emulate(
                 v    = (uint8_t)src.val;
                 generate_exception_if(
                     div_dbl(u, v) || ((uint8_t)u[0] != (uint16_t)u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val = (uint8_t)u[0];
                 ((uint8_t *)&_regs.eax)[1] = u[1];
                 break;
@@ -4155,7 +4160,7 @@ x86_emulate(
                 v    = (uint16_t)src.val;
                 generate_exception_if(
                     div_dbl(u, v) || ((uint16_t)u[0] != (uint32_t)u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val = (uint16_t)u[0];
                 *(uint16_t *)&_regs.edx = u[1];
                 break;
@@ -4166,7 +4171,7 @@ x86_emulate(
                 v    = (uint32_t)src.val;
                 generate_exception_if(
                     div_dbl(u, v) || ((uint32_t)u[0] != u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val   = (uint32_t)u[0];
                 _regs.edx = (uint32_t)u[1];
                 break;
@@ -4175,7 +4180,7 @@ x86_emulate(
                 u[0] = _regs.eax;
                 u[1] = _regs.edx;
                 v    = src.val;
-                generate_exception_if(div_dbl(u, v), EXC_DE, -1);
+                generate_exception_if(div_dbl(u, v), EXC_DE);
                 dst.val   = u[0];
                 _regs.edx = u[1];
                 break;
@@ -4191,7 +4196,7 @@ x86_emulate(
                 v    = (int8_t)src.val;
                 generate_exception_if(
                     idiv_dbl(u, v) || ((int8_t)u[0] != (int16_t)u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val = (int8_t)u[0];
                 ((int8_t *)&_regs.eax)[1] = u[1];
                 break;
@@ -4201,7 +4206,7 @@ x86_emulate(
                 v    = (int16_t)src.val;
                 generate_exception_if(
                     idiv_dbl(u, v) || ((int16_t)u[0] != (int32_t)u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val = (int16_t)u[0];
                 *(int16_t *)&_regs.edx = u[1];
                 break;
@@ -4212,7 +4217,7 @@ x86_emulate(
                 v    = (int32_t)src.val;
                 generate_exception_if(
                     idiv_dbl(u, v) || ((int32_t)u[0] != u[0]),
-                    EXC_DE, -1);
+                    EXC_DE);
                 dst.val   = (int32_t)u[0];
                 _regs.edx = (uint32_t)u[1];
                 break;
@@ -4221,7 +4226,7 @@ x86_emulate(
                 u[0] = _regs.eax;
                 u[1] = _regs.edx;
                 v    = src.val;
-                generate_exception_if(idiv_dbl(u, v), EXC_DE, -1);
+                generate_exception_if(idiv_dbl(u, v), EXC_DE);
                 dst.val   = u[0];
                 _regs.edx = u[1];
                 break;
@@ -4261,7 +4266,7 @@ x86_emulate(
         break;
 
     case 0xfe: /* Grp4 */
-        generate_exception_if((modrm_reg & 7) >= 2, EXC_UD, -1);
+        generate_exception_if((modrm_reg & 7) >= 2, EXC_UD);
     case 0xff: /* Grp5 */
         switch ( modrm_reg & 7 )
         {
@@ -4286,7 +4291,7 @@ x86_emulate(
             break;
         case 3: /* call (far, absolute indirect) */
         case 5: /* jmp (far, absolute indirect) */
-            generate_exception_if(src.type != OP_MEM, EXC_UD, -1);
+            generate_exception_if(src.type != OP_MEM, EXC_UD);
 
             if ( (rc = read_ulong(src.mem.seg, src.mem.off + op_bytes,
                                   &imm2, 2, ctxt, ops)) )
@@ -4298,13 +4303,13 @@ x86_emulate(
         case 6: /* push */
             goto push;
         case 7:
-            generate_exception_if(1, EXC_UD, -1);
+            generate_exception(EXC_UD);
         }
         break;
 
     case X86EMUL_OPC(0x0f, 0x00): /* Grp6 */
         seg = (modrm_reg & 1) ? x86_seg_tr : x86_seg_ldtr;
-        generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
+        generate_exception_if(!in_protmode(ctxt, ops), EXC_UD);
         switch ( modrm_reg & 6 )
         {
         case 0: /* sldt / str */
@@ -4316,7 +4321,7 @@ x86_emulate(
                 goto done;
             break;
         default:
-            generate_exception_if(true, EXC_UD, -1);
+            generate_exception_if(true, EXC_UD);
             break;
         }
         break;
@@ -4331,10 +4336,10 @@ x86_emulate(
         {
             unsigned long cr4;
 
-            generate_exception_if(vex.pfx, EXC_UD, -1);
+            generate_exception_if(vex.pfx, EXC_UD);
             if ( !ops->read_cr || ops->read_cr(4, &cr4, ctxt) != X86EMUL_OKAY )
                 cr4 = 0;
-            generate_exception_if(!(cr4 & X86_CR4_OSXSAVE), EXC_UD, -1);
+            generate_exception_if(!(cr4 & X86_CR4_OSXSAVE), EXC_UD);
             generate_exception_if(!mode_ring0() ||
                                   handle_xsetbv(_regs._ecx,
                                                 _regs._eax | (_regs.rdx << 32)),
@@ -4345,28 +4350,28 @@ x86_emulate(
 
         case 0xd4: /* vmfunc */
             generate_exception_if(lock_prefix | rep_prefix() | (vex.pfx == vex_66),
-                                  EXC_UD, -1);
+                                  EXC_UD);
             fail_if(!ops->vmfunc);
             if ( (rc = ops->vmfunc(ctxt) != X86EMUL_OKAY) )
                 goto done;
             goto no_writeback;
 
         case 0xd5: /* xend */
-            generate_exception_if(vex.pfx, EXC_UD, -1);
-            generate_exception_if(!vcpu_has_rtm(), EXC_UD, -1);
+            generate_exception_if(vex.pfx, EXC_UD);
+            generate_exception_if(!vcpu_has_rtm(), EXC_UD);
             generate_exception_if(vcpu_has_rtm(), EXC_GP, 0);
             break;
 
         case 0xd6: /* xtest */
-            generate_exception_if(vex.pfx, EXC_UD, -1);
+            generate_exception_if(vex.pfx, EXC_UD);
             generate_exception_if(!vcpu_has_rtm() && !vcpu_has_hle(),
-                                  EXC_UD, -1);
+                                  EXC_UD);
             /* Neither HLE nor RTM can be active when we get here. */
             _regs.eflags |= EFLG_ZF;
             goto no_writeback;
 
         case 0xdf: /* invlpga */
-            generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
+            generate_exception_if(!in_protmode(ctxt, ops), EXC_UD);
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
             fail_if(ops->invlpg == NULL);
             if ( (rc = ops->invlpg(x86_seg_none, truncate_ea(_regs.eax),
@@ -4396,7 +4401,7 @@ x86_emulate(
                  ops->cpuid(&eax, &ebx, &dummy, &dummy, ctxt) == X86EMUL_OKAY )
                 limit = ((ebx >> 8) & 0xff) * 8;
             generate_exception_if(limit < sizeof(long) ||
-                                  (limit & (limit - 1)), EXC_UD, -1);
+                                  (limit & (limit - 1)), EXC_UD);
             base &= ~(limit - 1);
             if ( override_seg == -1 )
                 override_seg = x86_seg_ds;
@@ -4432,7 +4437,7 @@ x86_emulate(
         {
         case 0: /* sgdt */
         case 1: /* sidt */
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+            generate_exception_if(ea.type != OP_MEM, EXC_UD);
             generate_exception_if(umip_active(ctxt, ops), EXC_GP, 0);
             fail_if(ops->read_segment == NULL);
             if ( (rc = ops->read_segment(seg, &sreg, ctxt)) )
@@ -4453,7 +4458,7 @@ x86_emulate(
         case 2: /* lgdt */
         case 3: /* lidt */
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+            generate_exception_if(ea.type != OP_MEM, EXC_UD);
             fail_if(ops->write_segment == NULL);
             memset(&sreg, 0, sizeof(sreg));
             if ( (rc = read_ulong(ea.mem.seg, ea.mem.off+0,
@@ -4496,7 +4501,7 @@ x86_emulate(
             break;
         case 7: /* invlpg */
             generate_exception_if(!mode_ring0(), EXC_GP, 0);
-            generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+            generate_exception_if(ea.type != OP_MEM, EXC_UD);
             fail_if(ops->invlpg == NULL);
             if ( (rc = ops->invlpg(ea.mem.seg, ea.mem.off, ctxt)) )
                 goto done;
@@ -4510,13 +4515,13 @@ x86_emulate(
     case X86EMUL_OPC(0x0f, 0x05): /* syscall */ {
         uint64_t msr_content;
 
-        generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, -1);
+        generate_exception_if(!in_protmode(ctxt, ops), EXC_UD);
 
         /* Inject #UD if syscall/sysret are disabled. */
         fail_if(ops->read_msr == NULL);
         if ( (rc = ops->read_msr(MSR_EFER, &msr_content, ctxt)) != 0 )
             goto done;
-        generate_exception_if((msr_content & EFER_SCE) == 0, EXC_UD, -1);
+        generate_exception_if((msr_content & EFER_SCE) == 0, EXC_UD);
 
         if ( (rc = ops->read_msr(MSR_STAR, &msr_content, ctxt)) != 0 )
             goto done;
@@ -4585,7 +4590,7 @@ x86_emulate(
     case X86EMUL_OPC(0x0f, 0x0b): /* ud2 */
     case X86EMUL_OPC(0x0f, 0xb9): /* ud1 */
     case X86EMUL_OPC(0x0f, 0xff): /* ud0 */
-        generate_exception_if(1, EXC_UD, -1);
+        generate_exception(EXC_UD);
 
     case X86EMUL_OPC(0x0f, 0x0d): /* GrpP (prefetch) */
     case X86EMUL_OPC(0x0f, 0x18): /* Grp16 (prefetch/nop) */
@@ -4704,7 +4709,7 @@ x86_emulate(
     case X86EMUL_OPC(0x0f, 0x21): /* mov dr,reg */
     case X86EMUL_OPC(0x0f, 0x22): /* mov reg,cr */
     case X86EMUL_OPC(0x0f, 0x23): /* mov reg,dr */
-        generate_exception_if(ea.type != OP_REG, EXC_UD, -1);
+        generate_exception_if(ea.type != OP_REG, EXC_UD);
         generate_exception_if(!mode_ring0(), EXC_GP, 0);
         modrm_reg |= lock_prefix << 3;
         if ( b & 2 )
@@ -4941,11 +4946,11 @@ x86_emulate(
         switch ( b )
         {
         case 0x7e:
-            generate_exception_if(vex.l, EXC_UD, -1);
+            generate_exception_if(vex.l, EXC_UD);
             ea.bytes = op_bytes;
             break;
         case 0xd6:
-            generate_exception_if(vex.l, EXC_UD, -1);
+            generate_exception_if(vex.l, EXC_UD);
             ea.bytes = 8;
             break;
         }
@@ -5035,7 +5040,7 @@ x86_emulate(
     case X86EMUL_OPC(0x0f, 0xad): /* shrd %%cl,r,r/m */ {
         uint8_t shift, width = dst.bytes << 3;
 
-        generate_exception_if(lock_prefix, EXC_UD, -1);
+        generate_exception_if(lock_prefix, EXC_UD);
         if ( b & 1 )
             shift = _regs.ecx;
         else
@@ -5150,7 +5155,7 @@ x86_emulate(
         case 5: goto bts;
         case 6: goto btr;
         case 7: goto btc;
-        default: generate_exception_if(1, EXC_UD, -1);
+        default: generate_exception(EXC_UD);
         }
         break;
 
@@ -5251,15 +5256,15 @@ x86_emulate(
     case X86EMUL_OPC(0x0f, 0xc3): /* movnti */
         /* Ignore the non-temporal hint for now. */
         vcpu_must_have_sse2();
-        generate_exception_if(dst.bytes <= 2, EXC_UD, -1);
+        generate_exception_if(dst.bytes <= 2, EXC_UD);
         dst.val = src.val;
         break;
 
     case X86EMUL_OPC(0x0f, 0xc7): /* Grp9 (cmpxchg8b/cmpxchg16b) */ {
         unsigned long old[2], exp[2], new[2];
 
-        generate_exception_if((modrm_reg & 7) != 1, EXC_UD, -1);
-        generate_exception_if(ea.type != OP_MEM, EXC_UD, -1);
+        generate_exception_if((modrm_reg & 7) != 1, EXC_UD);
+        generate_exception_if(ea.type != OP_MEM, EXC_UD);
         if ( op_bytes == 8 )
             host_and_vcpu_must_have(cx16);
         op_bytes *= 2;
@@ -5416,8 +5421,7 @@ x86_emulate(
     *ctxt->regs = _regs;
 
     /* Inject #DB if single-step tracing was enabled at instruction start. */
-    if ( tf && (rc == X86EMUL_OKAY) && ops->inject_hw_exception )
-        rc = ops->inject_hw_exception(EXC_DB, -1, ctxt) ? : X86EMUL_EXCEPTION;
+    generate_exception_if(tf && (rc == X86EMUL_OKAY), EXC_DB);
 
  done:
     _put_fpu();
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#34 lore raw

PATCH 6/15 x86/emul: Rework emulator event injection

In reply to Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so they are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending event to fall back into the previous codepath.

No overall functional change.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 tools/tests/x86_emulator/test_x86_emulator.c |  1 +
 xen/arch/x86/hvm/emulate.c                   | 81 ++++------------------------
 xen/arch/x86/hvm/hvm.c                       |  4 +-
 xen/arch/x86/hvm/io.c                        |  4 +-
 xen/arch/x86/hvm/vmx/realmode.c              | 16 +++---
 xen/arch/x86/mm.c                            |  5 +-
 xen/arch/x86/mm/shadow/multi.c               |  4 +-
 xen/arch/x86/x86_emulate/x86_emulate.c       | 12 ++---
 xen/arch/x86/x86_emulate/x86_emulate.h       | 67 ++++++++++++++++++-----
 xen/include/asm-x86/hvm/emulate.h            |  3 --
 10 files changed, 86 insertions(+), 111 deletions(-)
tools/tests/x86_emulator/test_x86_emulator.c +1 -0
index 948ee8d..146e15e 100644
@@ -1,3 +1,4 @@
+#include <assert.h>
 #include <errno.h>
 #include <limits.h>
 #include <stdbool.h>
xen/arch/x86/hvm/emulate.c +9 -72

135 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +2 -2
index b950842..ef83100 100644
@@ -4076,8 +4076,8 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
         hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;
     case X86EMUL_EXCEPTION:
-        if ( ctxt.exn_pending )
-            hvm_inject_event(&ctxt.trap);
+        if ( ctxt.ctxt.event_pending )
+            hvm_inject_event(&ctxt.ctxt.event);
         /* fall through */
     default:
         hvm_emulate_writeback(&ctxt);
xen/arch/x86/hvm/io.c +2 -2
index 1279f68..abb9d51 100644
@@ -102,8 +102,8 @@ int handle_mmio(void)
         hvm_dump_emulation_state(XENLOG_G_WARNING "MMIO", &ctxt);
         return 0;
     case X86EMUL_EXCEPTION:
-        if ( ctxt.exn_pending )
-            hvm_inject_event(&ctxt.trap);
+        if ( ctxt.ctxt.event_pending )
+            hvm_inject_event(&ctxt.ctxt.event);
         break;
     default:
         break;
xen/arch/x86/hvm/vmx/realmode.c +8 -8
index 9002638..dc3ab44 100644
@@ -122,7 +122,7 @@ void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
 
     if ( rc == X86EMUL_EXCEPTION )
     {
-        if ( !hvmemul_ctxt->exn_pending )
+        if ( !hvmemul_ctxt->ctxt.event_pending )
         {
             unsigned long intr_info;
 
@@ -133,27 +133,27 @@ void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
                 gdprintk(XENLOG_ERR, "Exception pending but no info.\n");
                 goto fail;
             }
-            hvmemul_ctxt->trap.vector = (uint8_t)intr_info;
-            hvmemul_ctxt->trap.insn_len = 0;
+            hvmemul_ctxt->ctxt.event.vector = (uint8_t)intr_info;
+            hvmemul_ctxt->ctxt.event.insn_len = 0;
         }
 
         if ( unlikely(curr->domain->debugger_attached) &&
-             ((hvmemul_ctxt->trap.vector == TRAP_debug) ||
-              (hvmemul_ctxt->trap.vector == TRAP_int3)) )
+             ((hvmemul_ctxt->ctxt.event.vector == TRAP_debug) ||
+              (hvmemul_ctxt->ctxt.event.vector == TRAP_int3)) )
         {
             domain_pause_for_debugger();
         }
         else if ( curr->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE )
         {
             gdprintk(XENLOG_ERR, "Exception %02x in protected mode.\n",
-                     hvmemul_ctxt->trap.vector);
+                     hvmemul_ctxt->ctxt.event.vector);
             goto fail;
         }
         else
         {
             realmode_deliver_exception(
-                hvmemul_ctxt->trap.vector,
-                hvmemul_ctxt->trap.insn_len,
+                hvmemul_ctxt->ctxt.event.vector,
+                hvmemul_ctxt->ctxt.event.insn_len,
                 hvmemul_ctxt);
         }
     }
xen/arch/x86/mm.c +3 -2
index 9901f6f..66ecdab 100644
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }
 
 void *alloc_xen_pagetable(void)
xen/arch/x86/mm/shadow/multi.c +2 -2
index d70b1c6..84cb6b6 100644
@@ -3378,7 +3378,7 @@ static int sh_page_fault(struct vcpu *v,
      * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
      * then it must be 'failable': we cannot require the unshadow to succeed.
      */
-    if ( r == X86EMUL_UNHANDLEABLE )
+    if ( r == X86EMUL_UNHANDLEABLE || emul_ctxt.ctxt.event_pending )
     {
         perfc_incr(shadow_fault_emulate_failed);
 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )
             {
                 emulation_count++;
                 if ( v->arch.paging.last_write_was_pt )
xen/arch/x86/x86_emulate/x86_emulate.c +5 -7
index f8271b3..768a436 100644
@@ -680,9 +680,8 @@ static inline int mkec(uint8_t e, int32_t ec, ...)
 
 #define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
-        fail_if(ops->inject_hw_exception == NULL);                        \
-        rc = ops->inject_hw_exception(e, mkec(e, ##ec, 0), ctxt)          \
-            ? : X86EMUL_EXCEPTION;                                        \
+        x86_emul_hw_exception(e, mkec(e, ##ec, 0), ctxt);                 \
+        rc = X86EMUL_EXCEPTION;                                           \
         goto done;                                                        \
     }                                                                     \
 })
@@ -1604,9 +1603,6 @@ static int inject_swint(enum x86_swint_type type,
 {
     int rc, error_code, fault_type = EXC_GP;
 
-    fail_if(ops->inject_sw_interrupt == NULL);
-    fail_if(ops->inject_hw_exception == NULL);
-
     /*
      * Without hardware support, injecting software interrupts/exceptions is
      * problematic.
@@ -1703,7 +1699,8 @@ static int inject_swint(enum x86_swint_type type,
         ctxt->regs->eip += insn_len;
     }
 
-    rc = ops->inject_sw_interrupt(type, vector, insn_len, ctxt);
+    x86_emul_software_event(type, vector, insn_len, ctxt);
+    rc = X86EMUL_OKAY;
 
  done:
     return rc;
@@ -1912,6 +1909,7 @@ x86_decode(
     /* Initialise output state in x86_emulate_ctxt */
     ctxt->opcode = ~0u;
     ctxt->retire.byte = 0;
+    x86_emul_reset_event(ctxt);
 
     op_bytes = def_op_bytes = ad_bytes = def_ad_bytes = ctxt->addr_size/8;
     if ( op_bytes == 8 )
xen/arch/x86/x86_emulate/x86_emulate.h +54 -13

83 lines not on this page · raw message

xen/include/asm-x86/hvm/emulate.h +0 -3
index 3b7ec33..d64d834 100644
@@ -29,9 +29,6 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_accessed;
     unsigned long seg_reg_dirty;
 
-    bool_t exn_pending;
-    struct x86_event trap;
-
     uint32_t intr_shadow;
 
     bool_t set_context;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#50 lore raw

PATCH 7/15 x86/vmx: Use hvm_{get, set}_segment_register() rather than vmx_{get, set}_segment_register()

In reply to Andrew Cooper

No functional change at this point, but this is a prerequisite for forthcoming functional changes.

Make vmx_get_segment_register() private to vmx.c like all the other Vendor get/set functions.

---
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/vmx/vmx.c        | 14 +++++++-------
 xen/arch/x86/hvm/vmx/vvmx.c       |  6 +++---
 xen/include/asm-x86/hvm/vmx/vmx.h |  2 --
 3 files changed, 10 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +7 -7
index eb7c902..29c6088 100644
@@ -940,8 +940,8 @@ static void vmx_ctxt_switch_to(struct vcpu *v)
         .fields = { .type = 0xb, .s = 0, .dpl = 0, .p = 1, .avl = 0,    \
                     .l = 0, .db = 0, .g = 0, .pad = 0 } }).bytes)
 
-void vmx_get_segment_register(struct vcpu *v, enum x86_segment seg,
-                              struct segment_register *reg)
+static void vmx_get_segment_register(struct vcpu *v, enum x86_segment seg,
+                                     struct segment_register *reg)
 {
     unsigned long attr = 0, sel = 0, limit;
 
@@ -1504,19 +1504,19 @@ static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr)
              * Need to read them all either way, as realmode reads can update
              * the saved values we'll use when returning to prot mode. */
             for ( s = 0; s < ARRAY_SIZE(reg); s++ )
-                vmx_get_segment_register(v, s, &reg[s]);
+                hvm_get_segment_register(v, s, &reg[s]);
             v->arch.hvm_vmx.vmx_realmode = realmode;
             
             if ( realmode )
             {
                 for ( s = 0; s < ARRAY_SIZE(reg); s++ )
-                    vmx_set_segment_register(v, s, &reg[s]);
+                    hvm_set_segment_register(v, s, &reg[s]);
             }
             else 
             {
                 for ( s = 0; s < ARRAY_SIZE(reg); s++ )
                     if ( !(v->arch.hvm_vmx.vm86_segment_mask & (1<<s)) )
-                        vmx_set_segment_register(
+                        hvm_set_segment_register(
                             v, s, &v->arch.hvm_vmx.vm86_saved_seg[s]);
             }
 
@@ -3907,7 +3907,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
             gdprintk(XENLOG_WARNING, "Bad vmexit (reason %#lx)\n",
                      exit_reason);
 
-            vmx_get_segment_register(v, x86_seg_ss, &ss);
+            hvm_get_segment_register(v, x86_seg_ss, &ss);
             if ( ss.attr.fields.dpl )
                 hvm_inject_hw_exception(TRAP_invalid_op,
                                         X86_EVENT_NO_EC);
@@ -3939,7 +3939,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)
 
         gprintk(XENLOG_WARNING, "Bad rIP %lx for mode %u\n", regs->rip, mode);
 
-        vmx_get_segment_register(v, x86_seg_ss, &ss);
+        hvm_get_segment_register(v, x86_seg_ss, &ss);
         if ( ss.attr.fields.dpl )
         {
             __vmread(VM_ENTRY_INTR_INFO, &intr_info);
xen/arch/x86/hvm/vmx/vvmx.c +3 -3
index efaf54c..bcc4a97 100644
@@ -360,7 +360,7 @@ static int vmx_inst_check_privilege(struct cpu_user_regs *regs, int vmxop_check)
     else if ( !vcpu_2_nvmx(v).vmxon_region_pa )
         goto invalid_op;
 
-    vmx_get_segment_register(v, x86_seg_cs, &cs);
+    hvm_get_segment_register(v, x86_seg_cs, &cs);
 
     if ( (regs->eflags & X86_EFLAGS_VM) ||
          (hvm_long_mode_enabled(v) && cs.attr.fields.l == 0) )
@@ -419,13 +419,13 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
 
         if ( hvm_long_mode_enabled(v) )
         {
-            vmx_get_segment_register(v, x86_seg_cs, &seg);
+            hvm_get_segment_register(v, x86_seg_cs, &seg);
             mode_64bit = seg.attr.fields.l;
         }
 
         if ( info.fields.segment > VMX_SREG_GS )
             goto gp_fault;
-        vmx_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
+        hvm_get_segment_register(v, sreg_to_index[info.fields.segment], &seg);
         seg_base = seg.base;
 
         base = info.fields.base_reg_invalid ? 0 :
xen/include/asm-x86/hvm/vmx/vmx.h +0 -2
index 4cdd9b1..0e5902d 100644
@@ -550,8 +550,6 @@ static inline int __vmxon(u64 addr)
     return rc;
 }
 
-void vmx_get_segment_register(struct vcpu *, enum x86_segment,
-                              struct segment_register *);
 void vmx_inject_extint(int trap, uint8_t source);
 void vmx_inject_nmi(void);
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#52 lore raw

PATCH 8/15 x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Andrew Cooper

Intel VT-x and AMD SVM provide access to the full segment descriptor cache via fields in the VMCB/VMCS. However, the bits which are actually checked by hardware and preserved across vmentry/exit are inconsistent, and the vendor accessor functions perform inconsistent modification to the raw values.

Convert {svm,vmx}_{get,set}_segment_register() into raw accessors, and alter hvm_{get,set}_segment_register() to cook the values consistently. This allows the common emulation code to better rely on finding architecturally-expected values.

This does cause some functional changes because of the modifications being applied uniformly. A side effect of this fixes latent bugs where vmx_set_segment_register() didn't correctly fix up .G for segments, and inconsistent fixing up of the GDTR/IDTR limits.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/hvm.c        | 151 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/svm.c    |  20 +-----
 xen/arch/x86/hvm/vmx/vmx.c    |   6 +-
 xen/include/asm-x86/desc.h    |   6 ++
 xen/include/asm-x86/hvm/hvm.h |  17 ++---
 5 files changed, 164 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/hvm.c +151 -0
index ef83100..804cd88 100644
@@ -6051,6 +6051,157 @@ void hvm_domain_soft_reset(struct domain *d)
 }
 
 /*
+ * Segment caches in VMCB/VMCS are inconsistent about which bits are checked,
+ * important, and preserved across vmentry/exit.  Cook the values to make them
+ * closer to what is architecturally expected from entries in the segment
+ * cache.
+ */
+void hvm_get_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg)
+{
+    hvm_funcs.get_segment_register(v, seg, reg);
+
+    switch ( seg )
+    {
+    case x86_seg_ss:
+        /* SVM may retain %ss.DB when %ss is loaded with a NULL selector. */
+        if ( !reg->attr.fields.p )
+            reg->attr.fields.db = 0;
+        break;
+
+    case x86_seg_tr:
+        /*
+         * SVM doesn't track %tr.B. Architecturally, a loaded TSS segment will
+         * always be busy.
+         */
+        reg->attr.fields.type |= 0x2;
+
+        /*
+         * %cs and %tr are unconditionally present.  SVM ignores these present
+         * bits and will happily run without them set.
+         */
+    case x86_seg_cs:
+        reg->attr.fields.p = 1;
+        break;
+
+    case x86_seg_gdtr:
+    case x86_seg_idtr:
+        /*
+         * Treat GDTR/IDTR as being present system segments.  This avoids them
+         * needing special casing for segmentation checks.
+         */
+        reg->attr.bytes = 0x80;
+        break;
+
+    default: /* Avoid triggering -Werror=switch */
+        break;
+    }
+
+    if ( reg->attr.fields.p )
+    {
+        /*
+         * For segments which are present/usable, cook the system flag.  SVM
+         * ignores the S bit on all segments and will happily run with them in
+         * any state.
+         */
+        reg->attr.fields.s = is_x86_user_segment(seg);
+
+        /*
+         * SVM discards %cs.G on #VMEXIT.  Other user segments do have .G
+         * tracked, but Linux commit 80112c89ed87 "KVM: Synthesize G bit for
+         * all segments." indicates that this isn't necessarily the case when
+         * nested under ESXi.
+         *
+         * Unconditionally recalculate G.
+         */
+        reg->attr.fields.g = !!(reg->limit >> 20);
+
+        /*
+         * SVM doesn't track the Accessed flag.  It will always be set for
+         * usable user segments loaded into the descriptor cache.
+         */
+        if ( is_x86_user_segment(seg) )
+            reg->attr.fields.type |= 0x1;
+    }
+}
+
+void hvm_set_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg)
+{
+    /* Set G to match the limit field.  VT-x cares, while SVM doesn't. */
+    if ( reg->attr.fields.p )
+        reg->attr.fields.g = !!(reg->limit >> 20);
+
+    switch ( seg )
+    {
+    case x86_seg_cs:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(reg->attr.fields.s);                  /* User segment. */
+        ASSERT((reg->base >> 32) == 0);              /* Upper bits clear. */
+        break;
+
+    case x86_seg_ss:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(reg->attr.fields.s);              /* User segment. */
+            ASSERT(!(reg->attr.fields.type & 0x8));  /* Data segment. */
+            ASSERT(reg->attr.fields.type & 0x2);     /* Writeable. */
+            ASSERT((reg->base >> 32) == 0);          /* Upper bits clear. */
+        }
+        break;
+
+    case x86_seg_ds:
+    case x86_seg_es:
+    case x86_seg_fs:
+    case x86_seg_gs:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(reg->attr.fields.s);              /* User segment. */
+
+            if ( reg->attr.fields.type & 0x8 )
+                ASSERT(reg->attr.fields.type & 0x2); /* Readable. */
+
+            if ( seg == x86_seg_fs || seg == x86_seg_gs )
+                ASSERT(is_canonical_address(reg->base));
+            else
+                ASSERT((reg->base >> 32) == 0);      /* Upper bits clear. */
+        }
+        break;
+
+    case x86_seg_tr:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(!reg->attr.fields.s);                 /* System segment. */
+        ASSERT(!(reg->sel & 0x4));                   /* !TI. */
+        ASSERT(reg->attr.fields.type == SYS_DESC_tss16_busy ||
+               reg->attr.fields.type == SYS_DESC_tss_busy);
+        ASSERT(is_canonical_address(reg->base));
+        break;
+
+    case x86_seg_ldtr:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(!reg->attr.fields.s);             /* System segment. */
+            ASSERT(!(reg->sel & 0x4));               /* !TI. */
+            ASSERT(reg->attr.fields.type == SYS_DESC_ldt);
+            ASSERT(is_canonical_address(reg->base));
+        }
+        break;
+
+    case x86_seg_gdtr:
+    case x86_seg_idtr:
+        ASSERT(is_canonical_address(reg->base));
+        ASSERT((reg->limit >> 16) == 0);             /* Upper bits clear. */
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        break;
+    }
+
+    hvm_funcs.set_segment_register(v, seg, reg);
+}
+
+/*
  * Local variables:
  * mode: C
  * c-file-style: "BSD"
xen/arch/x86/hvm/svm/svm.c +2 -18
index fb4fd0b..c3c759f 100644
@@ -627,50 +627,34 @@ static void svm_get_segment_register(struct vcpu *v, enum x86_segment seg,
     {
     case x86_seg_cs:
         memcpy(reg, &vmcb->cs, sizeof(*reg));
-        reg->attr.fields.p = 1;
-        reg->attr.fields.g = reg->limit > 0xFFFFF;
         break;
     case x86_seg_ds:
         memcpy(reg, &vmcb->ds, sizeof(*reg));
-        if ( reg->attr.fields.type != 0 )
-            reg->attr.fields.type |= 0x1;
         break;
     case x86_seg_es:
         memcpy(reg, &vmcb->es, sizeof(*reg));
-        if ( reg->attr.fields.type != 0 )
-            reg->attr.fields.type |= 0x1;
         break;
     case x86_seg_fs:
         svm_sync_vmcb(v);
         memcpy(reg, &vmcb->fs, sizeof(*reg));
-        if ( reg->attr.fields.type != 0 )
-            reg->attr.fields.type |= 0x1;
         break;
     case x86_seg_gs:
         svm_sync_vmcb(v);
         memcpy(reg, &vmcb->gs, sizeof(*reg));
-        if ( reg->attr.fields.type != 0 )
-            reg->attr.fields.type |= 0x1;
         break;
     case x86_seg_ss:
         memcpy(reg, &vmcb->ss, sizeof(*reg));
         reg->attr.fields.dpl = vmcb->_cpl;
-        if ( reg->attr.fields.type == 0 )
-            reg->attr.fields.db = 0;
         break;
     case x86_seg_tr:
         svm_sync_vmcb(v);
         memcpy(reg, &vmcb->tr, sizeof(*reg));
-        reg->attr.fields.p = 1;
-        reg->attr.fields.type |= 0x2;
         break;
     case x86_seg_gdtr:
         memcpy(reg, &vmcb->gdtr, sizeof(*reg));
-        reg->attr.bytes = 0x80;
         break;
     case x86_seg_idtr:
         memcpy(reg, &vmcb->idtr, sizeof(*reg));
-        reg->attr.bytes = 0x80;
         break;
     case x86_seg_ldtr:
         svm_sync_vmcb(v);
@@ -740,11 +724,11 @@ static void svm_set_segment_register(struct vcpu *v, enum x86_segment seg,
         break;
     case x86_seg_gdtr:
         vmcb->gdtr.base = reg->base;
-        vmcb->gdtr.limit = (uint16_t)reg->limit;
+        vmcb->gdtr.limit = reg->limit;
         break;
     case x86_seg_idtr:
         vmcb->idtr.base = reg->base;
-        vmcb->idtr.limit = (uint16_t)reg->limit;
+        vmcb->idtr.limit = reg->limit;
         break;
     case x86_seg_ldtr:
         memcpy(&vmcb->ldtr, reg, sizeof(*reg));
xen/arch/x86/hvm/vmx/vmx.c +1 -5
index 29c6088..0fd4b5c 100644
@@ -1126,9 +1126,6 @@ static void vmx_set_segment_register(struct vcpu *v, enum x86_segment seg,
      */
     attr = (!(attr & (1u << 7)) << 16) | ((attr & 0xf00) << 4) | (attr & 0xff);
 
-    /* VMX has strict consistency requirement for flag G. */
-    attr |= !!(limit >> 20) << 15;
-
     vmx_vmcs_enter(v);
 
     switch ( seg )
@@ -1173,8 +1170,7 @@ static void vmx_set_segment_register(struct vcpu *v, enum x86_segment seg,
         __vmwrite(GUEST_TR_SELECTOR, sel);
         __vmwrite(GUEST_TR_LIMIT, limit);
         __vmwrite(GUEST_TR_BASE, base);
-        /* VMX checks that the the busy flag (bit 1) is set. */
-        __vmwrite(GUEST_TR_AR_BYTES, attr | 2);
+        __vmwrite(GUEST_TR_AR_BYTES, attr);
         break;
     case x86_seg_gdtr:
         __vmwrite(GUEST_GDTR_LIMIT, limit);
xen/include/asm-x86/desc.h +6 -0
index 0e2d97f..da924bf 100644
@@ -89,7 +89,13 @@
 #ifndef __ASSEMBLY__
 
 /* System Descriptor types for GDT and IDT entries. */
+#define SYS_DESC_tss16_avail  1
 #define SYS_DESC_ldt          2
+#define SYS_DESC_tss16_busy   3
+#define SYS_DESC_call_gate16  4
+#define SYS_DESC_task_gate    5
+#define SYS_DESC_irq_gate16   6
+#define SYS_DESC_trap_gate16  7
 #define SYS_DESC_tss_avail    9
 #define SYS_DESC_tss_busy     11
 #define SYS_DESC_call_gate    12
xen/include/asm-x86/hvm/hvm.h +4 -13
index 51a64f7..b37b335 100644
@@ -358,19 +358,10 @@ static inline void hvm_flush_guest_tlbs(void)
 void hvm_hypercall_page_initialise(struct domain *d,
                                    void *hypercall_page);
 
-static inline void
-hvm_get_segment_register(struct vcpu *v, enum x86_segment seg,
-                         struct segment_register *reg)
-{
-    hvm_funcs.get_segment_register(v, seg, reg);
-}
-
-static inline void
-hvm_set_segment_register(struct vcpu *v, enum x86_segment seg,
-                         struct segment_register *reg)
-{
-    hvm_funcs.set_segment_register(v, seg, reg);
-}
+void hvm_get_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg);
+void hvm_set_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg);
 
 static inline unsigned long hvm_get_shadow_gs_base(struct vcpu *v)
 {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#61 lore raw

PATCH 9/15 x86/emul: Avoid raising faults behind the emulators back

In reply to Andrew Cooper

Introduce a new x86_emul_pagefault() similar to x86_emul_hw_exception(), and use this instead of hvm_inject_page_fault() from emulation codepaths.

Replace one hvm_inject_hw_exception() in the shadow emulation codepaths.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: George Dunlap <[email protected]>

NOTE: this is a functional change for the shadow code, as a #PF previously raised properly with the guest will now cause X86EMUL_UNHANDLABLE. It is my understanding after a discusion with Tim that this is ok, but I haven't done extenstive testing yet.

---
 xen/arch/x86/hvm/emulate.c             |  4 ++--
 xen/arch/x86/mm/shadow/common.c        |  5 +++--
 xen/arch/x86/x86_emulate/x86_emulate.h | 13 +++++++++++++
 3 files changed, 18 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -2
index c0fbde1..3ebb200 100644
@@ -459,7 +459,7 @@ static int hvmemul_linear_to_phys(
     {
         if ( pfec & (PFEC_page_paged | PFEC_page_shared) )
             return X86EMUL_RETRY;
-        hvm_inject_page_fault(pfec, addr);
+        x86_emul_pagefault(pfec, addr, &hvmemul_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     }
 
@@ -483,7 +483,7 @@ static int hvmemul_linear_to_phys(
                 ASSERT(!reverse);
                 if ( npfn != gfn_x(INVALID_GFN) )
                     return X86EMUL_UNHANDLEABLE;
-                hvm_inject_page_fault(pfec, addr & PAGE_MASK);
+                x86_emul_pagefault(pfec, addr & PAGE_MASK, &hvmemul_ctxt->ctxt);
                 return X86EMUL_EXCEPTION;
             }
             *reps = done;
xen/arch/x86/mm/shadow/common.c +3 -2
index 6f6668d..c8b61b9 100644
@@ -162,8 +162,9 @@ static int hvm_translate_linear_addr(
 
     if ( !okay )
     {
-        hvm_inject_hw_exception(
-            (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault, 0);
+        x86_emul_hw_exception(
+            (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,
+            0, &sh_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     }
 
xen/arch/x86/x86_emulate/x86_emulate.h +13 -0
index ddcd93c..cc26e9d 100644
@@ -600,6 +600,19 @@ static inline void x86_emul_hw_exception(
     ctxt->event_pending = true;
 }
 
+static inline void x86_emul_pagefault(
+    unsigned int error_code, unsigned long cr2, struct x86_emulate_ctxt *ctxt)
+{
+    ASSERT(!ctxt->event_pending);
+
+    ctxt->event.vector = 14; /* TRAP_page_fault */
+    ctxt->event.type = X86_EVENTTYPE_HW_EXCEPTION;
+    ctxt->event.error_code = error_code;
+    ctxt->event.cr2 = cr2;
+
+    ctxt->event_pending = true;
+}
+
 static inline void x86_emul_software_event(
     enum x86_swint_type type, uint8_t vector, uint8_t insn_len,
     struct x86_emulate_ctxt *ctxt)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#69 lore raw

PATCH 10/15 x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer

In reply to Andrew Cooper

which is filled with pagefault information should one occur.

No functional change.

---
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/emulate.c        |  8 ++++---
 xen/arch/x86/hvm/hvm.c            | 49 +++++++++++++++++++++++++--------------
 xen/arch/x86/hvm/vmx/vvmx.c       |  9 ++++---
 xen/arch/x86/mm/shadow/common.c   |  5 ++--
 xen/include/asm-x86/hvm/support.h | 23 +++++++++++++-----
 5 files changed, 63 insertions(+), 31 deletions(-)
xen/arch/x86/hvm/emulate.c +5 -3
index 3ebb200..e50ee24 100644
@@ -770,6 +770,7 @@ static int __hvmemul_read(
     struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     struct vcpu *curr = current;
+    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
@@ -790,8 +791,8 @@ static int __hvmemul_read(
         pfec |= PFEC_user_mode;
 
     rc = ((access_type == hvm_access_insn_fetch) ?
-          hvm_fetch_from_guest_virt(p_data, addr, bytes, pfec) :
-          hvm_copy_from_guest_virt(p_data, addr, bytes, pfec));
+          hvm_fetch_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo) :
+          hvm_copy_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo));
 
     switch ( rc )
     {
@@ -878,6 +879,7 @@ static int hvmemul_write(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     struct vcpu *curr = current;
+    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present | PFEC_write_access;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
@@ -896,7 +898,7 @@ static int hvmemul_write(
          (hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3) )
         pfec |= PFEC_user_mode;
 
-    rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec);
+    rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec, &pfinfo);
 
     switch ( rc )
     {
xen/arch/x86/hvm/hvm.c +32 -17

144 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +6 -3
index bcc4a97..7342d12 100644
@@ -396,6 +396,7 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
     struct vcpu *v = current;
     union vmx_inst_info info;
     struct segment_register seg;
+    pagefault_info_t pfinfo;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
 
@@ -451,7 +452,7 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
             goto gp_fault;
 
         if ( poperandS != NULL &&
-             hvm_copy_from_guest_virt(poperandS, base, size, 0)
+             hvm_copy_from_guest_virt(poperandS, base, size, 0, &pfinfo)
                   != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         decode->mem = base;
@@ -1611,6 +1612,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)
     struct vcpu *v = current;
     struct vmx_inst_decoded decode;
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
+    pagefault_info_t pfinfo;
     unsigned long gpa = 0;
     int rc;
 
@@ -1620,7 +1622,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)
 
     gpa = nvcpu->nv_vvmcxaddr;
 
-    rc = hvm_copy_to_guest_virt(decode.mem, &gpa, decode.len, 0);
+    rc = hvm_copy_to_guest_virt(decode.mem, &gpa, decode.len, 0, &pfinfo);
     if ( rc != HVMCOPY_okay )
         return X86EMUL_EXCEPTION;
 
@@ -1679,6 +1681,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
     struct vmx_inst_decoded decode;
+    pagefault_info_t pfinfo;
     u64 value = 0;
     int rc;
 
@@ -1690,7 +1693,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)
 
     switch ( decode.type ) {
     case VMX_INST_MEMREG_TYPE_MEMORY:
-        rc = hvm_copy_to_guest_virt(decode.mem, &value, decode.len, 0);
+        rc = hvm_copy_to_guest_virt(decode.mem, &value, decode.len, 0, &pfinfo);
         if ( rc != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         break;
xen/arch/x86/mm/shadow/common.c +3 -2
index c8b61b9..d28eae1 100644
@@ -179,6 +179,7 @@ hvm_read(enum x86_segment seg,
          enum hvm_access_type access_type,
          struct sh_emulate_ctxt *sh_ctxt)
 {
+    pagefault_info_t pfinfo;
     unsigned long addr;
     int rc;
 
@@ -188,9 +189,9 @@ hvm_read(enum x86_segment seg,
         return rc;
 
     if ( access_type == hvm_access_insn_fetch )
-        rc = hvm_fetch_from_guest_virt(p_data, addr, bytes, 0);
+        rc = hvm_fetch_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
     else
-        rc = hvm_copy_from_guest_virt(p_data, addr, bytes, 0);
+        rc = hvm_copy_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
 
     switch ( rc )
     {
xen/include/asm-x86/hvm/support.h +17 -6
index 9938450..4aa5a36 100644
@@ -83,16 +83,27 @@ enum hvm_copy_result hvm_copy_from_guest_phys(
  *  HVMCOPY_bad_gfn_to_mfn: Some guest physical address did not map to
  *                          ordinary machine memory.
  *  HVMCOPY_bad_gva_to_gfn: Some guest virtual address did not have a valid
- *                          mapping to a guest physical address. In this case
- *                          a page fault exception is automatically queued
- *                          for injection into the current HVM VCPU.
+ *                          mapping to a guest physical address.  The
+ *                          pagefault_info_t structure will be filled in if
+ *                          provided, and a page fault exception is
+ *                          automatically queued for injection into the
+ *                          current HVM VCPU.
  */
+typedef struct pagefault_info
+{
+    unsigned long linear;
+    int ec;
+} pagefault_info_t;
+
 enum hvm_copy_result hvm_copy_to_guest_virt(
-    unsigned long vaddr, void *buf, int size, uint32_t pfec);
+    unsigned long vaddr, void *buf, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 enum hvm_copy_result hvm_copy_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 enum hvm_copy_result hvm_fetch_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 
 /*
  * As above (copy to/from a guest virtual address), but no fault is generated
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#73 lore raw

PATCH 11/15 x86/hvm: Reimplement hvm_copy_*_nofault() in terms of no pagefault_info

In reply to Andrew Cooper

No functional change.

---
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>

---
 xen/arch/x86/hvm/emulate.c        |  6 ++---
 xen/arch/x86/hvm/hvm.c            | 56 +++++++++------------------------------
 xen/arch/x86/mm/shadow/common.c   |  8 +++---
 xen/include/asm-x86/hvm/support.h | 11 --------
 4 files changed, 19 insertions(+), 62 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -3
index e50ee24..34c9d77 100644
@@ -1936,9 +1936,9 @@ void hvm_emulate_init_per_insn(
                                         hvm_access_insn_fetch,
                                         hvmemul_ctxt->ctxt.addr_size,
                                         &addr) &&
-             hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf, addr,
-                                               sizeof(hvmemul_ctxt->insn_buf),
-                                               pfec) == HVMCOPY_okay) ?
+             hvm_fetch_from_guest_virt(hvmemul_ctxt->insn_buf, addr,
+                                       sizeof(hvmemul_ctxt->insn_buf),
+                                       pfec, NULL) == HVMCOPY_okay) ?
             sizeof(hvmemul_ctxt->insn_buf) : 0;
     }
     else
xen/arch/x86/hvm/hvm.c +12 -44

120 lines not on this page · raw message

xen/arch/x86/mm/shadow/common.c +4 -4
index d28eae1..bfd76af 100644
@@ -419,8 +419,8 @@ const struct x86_emulate_ops *shadow_init_emulation(
         (!hvm_translate_linear_addr(
             x86_seg_cs, regs->eip, sizeof(sh_ctxt->insn_buf),
             hvm_access_insn_fetch, sh_ctxt, &addr) &&
-         !hvm_fetch_from_guest_virt_nofault(
-             sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0))
+         !hvm_fetch_from_guest_virt(
+             sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0, NULL))
         ? sizeof(sh_ctxt->insn_buf) : 0;
 
     return &hvm_shadow_emulator_ops;
@@ -447,8 +447,8 @@ void shadow_continue_emulation(struct sh_emulate_ctxt *sh_ctxt,
                 (!hvm_translate_linear_addr(
                     x86_seg_cs, regs->eip, sizeof(sh_ctxt->insn_buf),
                     hvm_access_insn_fetch, sh_ctxt, &addr) &&
-                 !hvm_fetch_from_guest_virt_nofault(
-                     sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0))
+                 !hvm_fetch_from_guest_virt(
+                     sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0, NULL))
                 ? sizeof(sh_ctxt->insn_buf) : 0;
             sh_ctxt->insn_buf_eip = regs->eip;
         }
xen/include/asm-x86/hvm/support.h +0 -11
index 4aa5a36..114aa04 100644
@@ -105,17 +105,6 @@ enum hvm_copy_result hvm_fetch_from_guest_virt(
     void *buf, unsigned long vaddr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
 
-/*
- * As above (copy to/from a guest virtual address), but no fault is generated
- * when HVMCOPY_bad_gva_to_gfn is returned.
- */
-enum hvm_copy_result hvm_copy_to_guest_virt_nofault(
-    unsigned long vaddr, void *buf, int size, uint32_t pfec);
-enum hvm_copy_result hvm_copy_from_guest_virt_nofault(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
-enum hvm_copy_result hvm_fetch_from_guest_virt_nofault(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
-
 #define HVM_HCALL_completed  0 /* hypercall completed - no further action */
 #define HVM_HCALL_preempted  1 /* hypercall preempted - re-execute VMCALL */
 #define HVM_HCALL_invalidate 2 /* invalidate ioemu-dm memory cache        */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#77 lore raw

PATCH 12/15 x86/hvm: Rename hvm_copy_*_guest_virt() to hvm_copy_*_guest_linear()

In reply to Andrew Cooper

The functions use linear addresses, not virtual addresses, as no segmentation is used. (Lots of other code in Xen makes this mistake.)

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/emulate.c        | 12 ++++----
 xen/arch/x86/hvm/hvm.c            | 60 +++++++++++++++++++--------------------
 xen/arch/x86/hvm/vmx/vvmx.c       |  6 ++--
 xen/arch/x86/mm/shadow/common.c   |  8 +++---
 xen/include/asm-x86/hvm/support.h | 14 ++++-----
 5 files changed, 50 insertions(+), 50 deletions(-)
xen/arch/x86/hvm/emulate.c +6 -6
index 34c9d77..dd24979 100644
@@ -791,8 +791,8 @@ static int __hvmemul_read(
         pfec |= PFEC_user_mode;
 
     rc = ((access_type == hvm_access_insn_fetch) ?
-          hvm_fetch_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo) :
-          hvm_copy_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo));
+          hvm_fetch_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo) :
+          hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo));
 
     switch ( rc )
     {
@@ -898,7 +898,7 @@ static int hvmemul_write(
          (hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3) )
         pfec |= PFEC_user_mode;
 
-    rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec, &pfinfo);
+    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
 
     switch ( rc )
     {
@@ -1936,9 +1936,9 @@ void hvm_emulate_init_per_insn(
                                         hvm_access_insn_fetch,
                                         hvmemul_ctxt->ctxt.addr_size,
                                         &addr) &&
-             hvm_fetch_from_guest_virt(hvmemul_ctxt->insn_buf, addr,
-                                       sizeof(hvmemul_ctxt->insn_buf),
-                                       pfec, NULL) == HVMCOPY_okay) ?
+             hvm_fetch_from_guest_linear(hvmemul_ctxt->insn_buf, addr,
+                                         sizeof(hvmemul_ctxt->insn_buf),
+                                         pfec, NULL) == HVMCOPY_okay) ?
             sizeof(hvmemul_ctxt->insn_buf) : 0;
     }
     else
xen/arch/x86/hvm/hvm.c +30 -30

135 lines not on this page · raw message

xen/arch/x86/hvm/vmx/vvmx.c +3 -3
index 7342d12..fd7ea0a 100644
@@ -452,7 +452,7 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
             goto gp_fault;
 
         if ( poperandS != NULL &&
-             hvm_copy_from_guest_virt(poperandS, base, size, 0, &pfinfo)
+             hvm_copy_from_guest_linear(poperandS, base, size, 0, &pfinfo)
                   != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         decode->mem = base;
@@ -1622,7 +1622,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)
 
     gpa = nvcpu->nv_vvmcxaddr;
 
-    rc = hvm_copy_to_guest_virt(decode.mem, &gpa, decode.len, 0, &pfinfo);
+    rc = hvm_copy_to_guest_linear(decode.mem, &gpa, decode.len, 0, &pfinfo);
     if ( rc != HVMCOPY_okay )
         return X86EMUL_EXCEPTION;
 
@@ -1693,7 +1693,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)
 
     switch ( decode.type ) {
     case VMX_INST_MEMREG_TYPE_MEMORY:
-        rc = hvm_copy_to_guest_virt(decode.mem, &value, decode.len, 0, &pfinfo);
+        rc = hvm_copy_to_guest_linear(decode.mem, &value, decode.len, 0, &pfinfo);
         if ( rc != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         break;
xen/arch/x86/mm/shadow/common.c +4 -4
index bfd76af..afacd5f 100644
@@ -189,9 +189,9 @@ hvm_read(enum x86_segment seg,
         return rc;
 
     if ( access_type == hvm_access_insn_fetch )
-        rc = hvm_fetch_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
+        rc = hvm_fetch_from_guest_linear(p_data, addr, bytes, 0, &pfinfo);
     else
-        rc = hvm_copy_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, 0, &pfinfo);
 
     switch ( rc )
     {
@@ -419,7 +419,7 @@ const struct x86_emulate_ops *shadow_init_emulation(
         (!hvm_translate_linear_addr(
             x86_seg_cs, regs->eip, sizeof(sh_ctxt->insn_buf),
             hvm_access_insn_fetch, sh_ctxt, &addr) &&
-         !hvm_fetch_from_guest_virt(
+         !hvm_fetch_from_guest_linear(
              sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0, NULL))
         ? sizeof(sh_ctxt->insn_buf) : 0;
 
@@ -447,7 +447,7 @@ void shadow_continue_emulation(struct sh_emulate_ctxt *sh_ctxt,
                 (!hvm_translate_linear_addr(
                     x86_seg_cs, regs->eip, sizeof(sh_ctxt->insn_buf),
                     hvm_access_insn_fetch, sh_ctxt, &addr) &&
-                 !hvm_fetch_from_guest_virt(
+                 !hvm_fetch_from_guest_linear(
                      sh_ctxt->insn_buf, addr, sizeof(sh_ctxt->insn_buf), 0, NULL))
                 ? sizeof(sh_ctxt->insn_buf) : 0;
             sh_ctxt->insn_buf_eip = regs->eip;
xen/include/asm-x86/hvm/support.h +7 -7
index 114aa04..78349f8 100644
@@ -73,7 +73,7 @@ enum hvm_copy_result hvm_copy_from_guest_phys(
     void *buf, paddr_t paddr, int size);
 
 /*
- * Copy to/from a guest virtual address. @pfec should include PFEC_user_mode
+ * Copy to/from a guest linear address. @pfec should include PFEC_user_mode
  * if emulating a user-mode access (CPL=3). All other flags in @pfec are
  * managed by the called function: it is therefore optional for the caller
  * to set them.
@@ -95,14 +95,14 @@ typedef struct pagefault_info
     int ec;
 } pagefault_info_t;
 
-enum hvm_copy_result hvm_copy_to_guest_virt(
-    unsigned long vaddr, void *buf, int size, uint32_t pfec,
+enum hvm_copy_result hvm_copy_to_guest_linear(
+    unsigned long addr, void *buf, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
-enum hvm_copy_result hvm_copy_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+enum hvm_copy_result hvm_copy_from_guest_linear(
+    void *buf, unsigned long addr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
-enum hvm_copy_result hvm_fetch_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+enum hvm_copy_result hvm_fetch_from_guest_linear(
+    void *buf, unsigned long addr, int size, uint32_t pfec,
     pagefault_info_t *pfinfo);
 
 #define HVM_HCALL_completed  0 /* hypercall completed - no further action */
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#81 lore raw

PATCH 13/15 x86/hvm: Avoid __hvm_copy() raising #PF behind the emulators back

In reply to Andrew Cooper

Drop the call to hvm_inject_page_fault() in __hvm_copy(), and require callers to inject the pagefault themselves.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/emulate.c        |  2 ++
 xen/arch/x86/hvm/hvm.c            | 11 +++++++++--
 xen/arch/x86/hvm/vmx/vvmx.c       | 20 +++++++++++++++-----
 xen/arch/x86/mm/shadow/common.c   |  1 +
 xen/include/asm-x86/hvm/support.h |  4 +---
 5 files changed, 28 insertions(+), 10 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -0
index dd24979..c248eca 100644
@@ -799,6 +799,7 @@ static int __hvmemul_read(
     case HVMCOPY_okay:
         break;
     case HVMCOPY_bad_gva_to_gfn:
+        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
         if ( access_type == hvm_access_insn_fetch )
@@ -905,6 +906,7 @@ static int hvmemul_write(
     case HVMCOPY_okay:
         break;
     case HVMCOPY_bad_gva_to_gfn:
+        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
         return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
xen/arch/x86/hvm/hvm.c +9 -2
index d0c4129..e1f2c9e 100644
@@ -2927,6 +2927,8 @@ void hvm_task_switch(
 
     rc = hvm_copy_from_guest_linear(
         &tss, prev_tr.base, sizeof(tss), PFEC_page_present, &pfinfo);
+    if ( rc == HVMCOPY_bad_gva_to_gfn )
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
     if ( rc != HVMCOPY_okay )
         goto out;
 
@@ -2965,11 +2967,15 @@ void hvm_task_switch(
                                   offsetof(typeof(tss), trace) -
                                   offsetof(typeof(tss), eip),
                                   PFEC_page_present, &pfinfo);
+    if ( rc == HVMCOPY_bad_gva_to_gfn )
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
     if ( rc != HVMCOPY_okay )
         goto out;
 
     rc = hvm_copy_from_guest_linear(
         &tss, tr.base, sizeof(tss), PFEC_page_present, &pfinfo);
+    if ( rc == HVMCOPY_bad_gva_to_gfn )
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
     /*
      * Note: The HVMCOPY_gfn_shared case could be optimised, if the callee
      * functions knew we want RO access.
@@ -3012,7 +3018,10 @@ void hvm_task_switch(
                                       &tss.back_link, sizeof(tss.back_link), 0,
                                       &pfinfo);
         if ( rc == HVMCOPY_bad_gva_to_gfn )
+        {
+            hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
             exn_raised = 1;
+        }
         else if ( rc != HVMCOPY_okay )
             goto out;
     }
@@ -3114,8 +3123,6 @@ static enum hvm_copy_result __hvm_copy(
                 {
                     pfinfo->linear = addr;
                     pfinfo->ec = pfec;
-
-                    hvm_inject_page_fault(pfec, addr);
                 }
                 return HVMCOPY_bad_gva_to_gfn;
             }
xen/arch/x86/hvm/vmx/vvmx.c +15 -5
index fd7ea0a..e6e9ebd 100644
@@ -396,7 +396,6 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
     struct vcpu *v = current;
     union vmx_inst_info info;
     struct segment_register seg;
-    pagefault_info_t pfinfo;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
 
@@ -451,10 +450,17 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,
               offset + size - 1 > seg.limit) )
             goto gp_fault;
 
-        if ( poperandS != NULL &&
-             hvm_copy_from_guest_linear(poperandS, base, size, 0, &pfinfo)
-                  != HVMCOPY_okay )
-            return X86EMUL_EXCEPTION;
+        if ( poperandS != NULL )
+        {
+            pagefault_info_t pfinfo;
+            int rc = hvm_copy_from_guest_linear(poperandS, base, size,
+                                                0, &pfinfo);
+
+            if ( rc == HVMCOPY_bad_gva_to_gfn )
+                hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
+            if ( rc != HVMCOPY_okay )
+                return X86EMUL_EXCEPTION;
+        }
         decode->mem = base;
         decode->len = size;
     }
@@ -1623,6 +1629,8 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)
     gpa = nvcpu->nv_vvmcxaddr;
 
     rc = hvm_copy_to_guest_linear(decode.mem, &gpa, decode.len, 0, &pfinfo);
+    if ( rc == HVMCOPY_bad_gva_to_gfn )
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
     if ( rc != HVMCOPY_okay )
         return X86EMUL_EXCEPTION;
 
@@ -1694,6 +1702,8 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)
     switch ( decode.type ) {
     case VMX_INST_MEMREG_TYPE_MEMORY:
         rc = hvm_copy_to_guest_linear(decode.mem, &value, decode.len, 0, &pfinfo);
+        if ( rc == HVMCOPY_bad_gva_to_gfn )
+            hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
         if ( rc != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         break;
xen/arch/x86/mm/shadow/common.c +1 -0
index afacd5f..88d4642 100644
@@ -198,6 +198,7 @@ hvm_read(enum x86_segment seg,
     case HVMCOPY_okay:
         return X86EMUL_OKAY;
     case HVMCOPY_bad_gva_to_gfn:
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
     case HVMCOPY_unhandleable:
xen/include/asm-x86/hvm/support.h +1 -3
index 78349f8..3d767d7 100644
@@ -85,9 +85,7 @@ enum hvm_copy_result hvm_copy_from_guest_phys(
  *  HVMCOPY_bad_gva_to_gfn: Some guest virtual address did not have a valid
  *                          mapping to a guest physical address.  The
  *                          pagefault_info_t structure will be filled in if
- *                          provided, and a page fault exception is
- *                          automatically queued for injection into the
- *                          current HVM VCPU.
+ *                          provided.
  */
 typedef struct pagefault_info
 {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#85 lore raw

PATCH 14/15 x86/hvm: Prepare to allow use of system segments for memory references

In reply to Andrew Cooper

All system segments (GDT/IDT/LDT and TR) describe a linear address and limit, and act similarly to user segments. However all current uses of these tables in the emulator opencode the address calculations and limit checks. In particular, no care is taken for access which wrap around the 4GB or non-canonical boundaries.

Alter hvm_virtual_to_linear_addr() to cope with performing segmentation checks on system segments. This involves restricting access checks in the 32bit case to user segments only, and adding presence/limit checks in the 64bit case.

When suffering a segmentation fault for a system segments, return X86EMUL_EXCEPTION but leave the fault injection to the caller. The fault type depends on the higher level action being performed.

---
CC: Paul Durrant <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 14 ++++++++----
 xen/arch/x86/hvm/hvm.c                 | 40 ++++++++++++++++++++++------------
 xen/arch/x86/mm/shadow/common.c        | 12 +++++++---
 xen/arch/x86/x86_emulate/x86_emulate.h | 26 ++++++++++++++--------
 4 files changed, 62 insertions(+), 30 deletions(-)
xen/arch/x86/hvm/emulate.c +10 -4
index c248eca..3a7d1f3 100644
@@ -567,10 +567,16 @@ static int hvmemul_virtual_to_linear(
     if ( *reps != 1 )
         return X86EMUL_UNHANDLEABLE;
 
-    /* This is a singleton operation: fail it with an exception. */
-    x86_emul_hw_exception((seg == x86_seg_ss)
-                          ? TRAP_stack_error
-                          : TRAP_gp_fault, 0, &hvmemul_ctxt->ctxt);
+    /*
+     * Leave exception injection to the caller for non-user segments: We
+     * neither know the exact error code to be used, nor can we easily
+     * determine the kind of exception (#GP or #TS) in that case.
+     */
+    if ( is_x86_user_segment(seg) )
+        x86_emul_hw_exception((seg == x86_seg_ss)
+                              ? TRAP_stack_error
+                              : TRAP_gp_fault, 0, &hvmemul_ctxt->ctxt);
+
     return X86EMUL_EXCEPTION;
 }
 
xen/arch/x86/hvm/hvm.c +26 -14
index e1f2c9e..2bcef1f 100644
@@ -2497,24 +2497,28 @@ bool_t hvm_virtual_to_linear_addr(
         if ( !reg->attr.fields.p )
             goto out;
 
-        switch ( access_type )
+        /* Read/write restrictions only exist for user segments. */
+        if ( reg->attr.fields.s )
         {
-        case hvm_access_read:
-            if ( (reg->attr.fields.type & 0xa) == 0x8 )
-                goto out; /* execute-only code segment */
-            break;
-        case hvm_access_write:
-            if ( (reg->attr.fields.type & 0xa) != 0x2 )
-                goto out; /* not a writable data segment */
-            break;
-        default:
-            break;
+            switch ( access_type )
+            {
+            case hvm_access_read:
+                if ( (reg->attr.fields.type & 0xa) == 0x8 )
+                    goto out; /* execute-only code segment */
+                break;
+            case hvm_access_write:
+                if ( (reg->attr.fields.type & 0xa) != 0x2 )
+                    goto out; /* not a writable data segment */
+                break;
+            default:
+                break;
+            }
         }
 
         last_byte = (uint32_t)offset + bytes - !!bytes;
 
         /* Is this a grows-down data segment? Special limit check if so. */
-        if ( (reg->attr.fields.type & 0xc) == 0x4 )
+        if ( reg->attr.fields.s && (reg->attr.fields.type & 0xc) == 0x4 )
         {
             /* Is upper limit 0xFFFF or 0xFFFFFFFF? */
             if ( !reg->attr.fields.db )
@@ -2530,10 +2534,18 @@ bool_t hvm_virtual_to_linear_addr(
     else
     {
         /*
-         * LONG MODE: FS and GS add segment base. Addresses must be canonical.
+         * User segments are always treated as present.  System segment may
+         * not be, and also incur limit checks.
          */
+        if ( is_x86_system_segment(seg) &&
+             (!reg->attr.fields.p || (offset + bytes - !!bytes) > reg->limit) )
+            goto out;
 
-        if ( (seg == x86_seg_fs) || (seg == x86_seg_gs) )
+        /*
+         * LONG MODE: FS, GS and system segments: add segment base. All
+         * addresses must be canonical.
+         */
+        if ( seg >= x86_seg_fs )
             addr += reg->base;
 
         last_byte = addr + bytes - !!bytes;
xen/arch/x86/mm/shadow/common.c +9 -3
index 88d4642..954c157 100644
@@ -162,9 +162,15 @@ static int hvm_translate_linear_addr(
 
     if ( !okay )
     {
-        x86_emul_hw_exception(
-            (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,
-            0, &sh_ctxt->ctxt);
+        /*
+         * Leave exception injection to the caller for non-user segments: We
+         * neither know the exact error code to be used, nor can we easily
+         * determine the kind of exception (#GP or #TS) in that case.
+         */
+        if ( is_x86_user_segment(seg) )
+            x86_emul_hw_exception(
+                (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,
+                0, &sh_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     }
 
xen/arch/x86/x86_emulate/x86_emulate.h +17 -9
index cc26e9d..4d18623 100644
@@ -27,7 +27,11 @@
 
 struct x86_emulate_ctxt;
 
-/* Comprehensive enumeration of x86 segment registers. */
+/*
+ * Comprehensive enumeration of x86 segment registers.  Various bits of code
+ * rely on this order (general purpose before system, tr at the beginning of
+ * system).
+ */
 enum x86_segment {
     /* General purpose.  Matches the SReg3 encoding in opcode/ModRM bytes. */
     x86_seg_es,
@@ -36,21 +40,25 @@ enum x86_segment {
     x86_seg_ds,
     x86_seg_fs,
     x86_seg_gs,
-    /* System. */
+    /* System: Valid to use for implicit table references. */
     x86_seg_tr,
     x86_seg_ldtr,
     x86_seg_gdtr,
     x86_seg_idtr,
-    /*
-     * Dummy: used to emulate direct processor accesses to management
-     * structures (TSS, GDT, LDT, IDT, etc.) which use linear addressing
-     * (no segment component) and bypass usual segment- and page-level
-     * protection checks.
-     */
+    /* No Segment: For accesses which are already linear. */
     x86_seg_none
 };
 
-#define is_x86_user_segment(seg) ((unsigned)(seg) <= x86_seg_gs)
+static inline bool is_x86_user_segment(enum x86_segment seg)
+{
+    unsigned int idx = seg;
+
+    return idx <= x86_seg_gs;
+}
+static inline bool is_x86_system_segment(enum x86_segment seg)
+{
+    return seg >= x86_seg_tr && seg < x86_seg_none;
+}
 
 /* Classification of the types of software generated interrupts/exceptions. */
 enum x86_swint_type {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#88 lore raw

PATCH 15/15 x86/hvm: Use system-segment relative memory accesses

In reply to Andrew Cooper

With hvm_virtual_to_linear_addr() capable of doing proper system-segment relative memory accesses, avoid open-coding the address and limit calculations locally.

When a table spans the 4GB boundary (32bit) or non-canonical boundary (64bit), segmentation errors are now raised. Previously, the use of x86_seg_none resulted in segmentation being skipped, and the linear address being truncated through the pagewalk, and possibly coming out valid on the far side.

---
 xen/arch/x86/hvm/hvm.c                 |   8 +++
 xen/arch/x86/x86_emulate/x86_emulate.c | 117 ++++++++++++++++++++-------------
 2 files changed, 79 insertions(+), 46 deletions(-)
xen/arch/x86/hvm/hvm.c +8 -0
index 2bcef1f..fbdb8dd 100644
@@ -2470,6 +2470,14 @@ bool_t hvm_virtual_to_linear_addr(
     unsigned long addr = offset, last_byte;
     bool_t okay = 0;
 
+    /*
+     * These checks are for a memory access through an active segment.
+     *
+     * It is expected that the access rights of reg are suitable for seg (and
+     * that this is enforced at the point that seg is loaded).
+     */
+    ASSERT(seg < x86_seg_none);
+
     if ( !(current->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) )
     {
         /*
xen/arch/x86/x86_emulate/x86_emulate.c +71 -46

189 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 1/15
#2 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Andrew Cooper
Quoted 20 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as
they are
very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer
how to
and when to use them.

Quoted 3 lines

No functional change.

Quoted 280 lines

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

As hvm_emulate_prepare() was new in 4.8, it would be a good idea to take this
patch to avoid future confusion on the stable-4.8 branch

---
 xen/arch/x86/hvm/emulate.c        | 111 +++++++++++++++++++---------------
----
 xen/arch/x86/hvm/hvm.c            |   2 +-
 xen/arch/x86/hvm/io.c             |   2 +-
 xen/arch/x86/hvm/ioreq.c          |   2 +-
 xen/arch/x86/hvm/svm/emulate.c    |   4 +-
 xen/arch/x86/hvm/vmx/realmode.c   |   2 +-
 xen/include/asm-x86/hvm/emulate.h |   6 ++-
 7 files changed, 66 insertions(+), 63 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -51
index e9b8f8c..3ab0e8e 100644
@@ -1755,57 +1755,6 @@ static const struct x86_emulate_ops
 hvm_emulate_ops_no_write = {
     .vmfunc        = hvmemul_vmfunc,
 };
 
-void hvm_emulate_init(
-    struct hvm_emulate_ctxt *hvmemul_ctxt,
-    const unsigned char *insn_buf,
-    unsigned int insn_bytes)
-{
-    struct vcpu *curr = current;
-    unsigned int pfec = PFEC_page_present;
-    unsigned long addr;
-
-    if ( hvm_long_mode_enabled(curr) &&
-         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
-    {
-        hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
-    }
-    else
-    {
-        hvmemul_ctxt->ctxt.addr_size =
-            hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.db ? 32 : 16;
-        hvmemul_ctxt->ctxt.sp_size =
-            hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db ? 32 : 16;
-    }
-
-    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
-        pfec |= PFEC_user_mode;
-
-    hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->eip;
-    if ( !insn_bytes )
-    {
-        hvmemul_ctxt->insn_buf_bytes =
-            hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?:
-            (hvm_virtual_to_linear_addr(x86_seg_cs,
-                                        &hvmemul_ctxt->seg_reg[x86_seg_cs],
-                                        hvmemul_ctxt->insn_buf_eip,
-                                        sizeof(hvmemul_ctxt->insn_buf),
-                                        hvm_access_insn_fetch,
-                                        hvmemul_ctxt->ctxt.addr_size,
-                                        &addr) &&
-             hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf,
 addr,
-                                               sizeof(hvmemul_ctxt->insn_buf),
-                                               pfec) == HVMCOPY_okay) ?
-            sizeof(hvmemul_ctxt->insn_buf) : 0;
-    }
-    else
-    {
-        hvmemul_ctxt->insn_buf_bytes = insn_bytes;
-        memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes);
-    }
-
-    hvmemul_ctxt->exn_pending = 0;
-}
-
 static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,

const struct x86_emulate_ops *ops)
{

xen/arch/x86/hvm/emulate.c +2 -1
@@ -1815,7 +1764,8 @@ static int _hvm_emulate_one(struct
 hvm_emulate_ctxt *hvmemul_ctxt,
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     int rc;
 
-    hvm_emulate_init(hvmemul_ctxt, vio->mmio_insn, vio-
 >mmio_insn_bytes);
+    hvm_emulate_init_per_insn(hvmemul_ctxt, vio->mmio_insn,
+                              vio->mmio_insn_bytes);
 
     vio->mmio_retry = 0;
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1915,7 +1865,7 @@ int hvm_emulate_one_mmio(unsigned long mfn,
 unsigned long gla)
     else
         ops = &hvm_ro_emulate_ops_mmio;
 
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
     ctxt.ctxt.data = &mmio_ro_ctxt;
     rc = _hvm_emulate_one(&ctxt, ops);

switch ( rc )

xen/arch/x86/hvm/emulate.c +1 -1
@@ -1940,7 +1890,7 @@ void hvm_emulate_one_vm_event(enum
 emul_kind kind, unsigned int trapnr,
     struct hvm_emulate_ctxt ctx = {{ 0 }};
     int rc;
 
-    hvm_emulate_prepare(&ctx, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctx, guest_cpu_user_regs());
 
     switch ( kind )
     {
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1992,7 +1942,7 @@ void hvm_emulate_one_vm_event(enum
 emul_kind kind, unsigned int trapnr,
     hvm_emulate_writeback(&ctx);
 }
 
-void hvm_emulate_prepare(
+void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)

{

xen/arch/x86/hvm/emulate.c +51 -0
@@ -2006,6 +1956,57 @@ void hvm_emulate_prepare(
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
 }
 
+void hvm_emulate_init_per_insn(
+    struct hvm_emulate_ctxt *hvmemul_ctxt,
+    const unsigned char *insn_buf,
+    unsigned int insn_bytes)
+{
+    struct vcpu *curr = current;
+    unsigned int pfec = PFEC_page_present;
+    unsigned long addr;
+
+    if ( hvm_long_mode_enabled(curr) &&
+         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
+    {
+        hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
+    }
+    else
+    {
+        hvmemul_ctxt->ctxt.addr_size =
+            hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.db ? 32 : 16;
+        hvmemul_ctxt->ctxt.sp_size =
+            hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db ? 32 : 16;
+    }
+
+    if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3 )
+        pfec |= PFEC_user_mode;
+
+    hvmemul_ctxt->insn_buf_eip = hvmemul_ctxt->ctxt.regs->eip;
+    if ( !insn_bytes )
+    {
+        hvmemul_ctxt->insn_buf_bytes =
+            hvm_get_insn_bytes(curr, hvmemul_ctxt->insn_buf) ?:
+            (hvm_virtual_to_linear_addr(x86_seg_cs,
+                                        &hvmemul_ctxt->seg_reg[x86_seg_cs],
+                                        hvmemul_ctxt->insn_buf_eip,
+                                        sizeof(hvmemul_ctxt->insn_buf),
+                                        hvm_access_insn_fetch,
+                                        hvmemul_ctxt->ctxt.addr_size,
+                                        &addr) &&
+             hvm_fetch_from_guest_virt_nofault(hvmemul_ctxt->insn_buf,
 addr,
+                                               sizeof(hvmemul_ctxt->insn_buf),
+                                               pfec) == HVMCOPY_okay) ?
+            sizeof(hvmemul_ctxt->insn_buf) : 0;
+    }
+    else
+    {
+        hvmemul_ctxt->insn_buf_bytes = insn_bytes;
+        memcpy(hvmemul_ctxt->insn_buf, insn_buf, insn_bytes);
+    }
+
+    hvmemul_ctxt->exn_pending = 0;
+}
+
 void hvm_emulate_writeback(
     struct hvm_emulate_ctxt *hvmemul_ctxt)

{

xen/arch/x86/hvm/hvm.c +1 -1
index f76dd90..25dc759 100644
@@ -4058,7 +4058,7 @@ void hvm_ud_intercept(struct cpu_user_regs
 *regs)
 {
     struct hvm_emulate_ctxt ctxt;
 
-    hvm_emulate_prepare(&ctxt, regs);
+    hvm_emulate_init_once(&ctxt, regs);
 
     if ( opt_hvm_fep )
     {
xen/arch/x86/hvm/io.c +1 -1
index 1e7a5f9..7305801 100644
@@ -87,7 +87,7 @@ int handle_mmio(void)
 
     ASSERT(!is_pvh_vcpu(curr));
 
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
 
     rc = hvm_emulate_one(&ctxt);
 
xen/arch/x86/hvm/ioreq.c +1 -1
index d2245e2..88071ab 100644
@@ -167,7 +167,7 @@ bool_t handle_hvm_io_completion(struct vcpu *v)
     {
         struct hvm_emulate_ctxt ctxt;
 
-        hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+        hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
         vmx_realmode_emulate_one(&ctxt);
         hvm_emulate_writeback(&ctxt);
 
xen/arch/x86/hvm/svm/emulate.c +2 -2
index a5545ea..9cdbe9e 100644
@@ -107,8 +107,8 @@ int __get_instruction_length_from_list(struct vcpu
 *v,
 #endif
 
     ASSERT(v == current);
-    hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
-    hvm_emulate_init(&ctxt, NULL, 0);
+    hvm_emulate_init_once(&ctxt, guest_cpu_user_regs());
+    hvm_emulate_init_per_insn(&ctxt, NULL, 0);
     state = x86_decode_insn(&ctxt.ctxt, hvmemul_insn_fetch);
     if ( IS_ERR_OR_NULL(state) )
         return 0;
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index e83a61f..9002638 100644
@@ -179,7 +179,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
-    hvm_emulate_prepare(&hvmemul_ctxt, regs);
+    hvm_emulate_init_once(&hvmemul_ctxt, regs);
 
     /* Only deliver interrupts into emulated real mode. */
     if ( !(curr->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) &&
xen/include/asm- renamed from xen/include/asm-x86/hvm/emulate.h +0 -0

x86/hvm/emulate.h
index f610673..d4186a2 100644

xen/include/asm-x86/hvm/emulate.h +4 -3
@@ -51,10 +51,12 @@ int hvm_emulate_one_no_write(
 void hvm_emulate_one_vm_event(enum emul_kind kind,
     unsigned int trapnr,
     unsigned int errcode);
-void hvm_emulate_prepare(
+/* Must be called once to set up hvmemul state. */
+void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs);
-void hvm_emulate_init(
+/* Must be called once before each instruction emulated. */
+void hvm_emulate_init_per_insn(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     const unsigned char *insn_buf,
     unsigned int insn_bytes);
--
Quoted 1 line

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/15
#3 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

In reply to Andrew Cooper
Quoted 134 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 02/15] x86/emul: Simplfy emulation state setup

The current code to set up emulation state is ad-hoc and error prone.

* Consistently zero all emulation state structures.
* Avoid explicitly initialising some state to 0.
* Explicitly identify all input and output state in x86_emulate_ctxt. This involves rearanging some fields.
* Have x86_decode() explicitly initalise all output state at its start.

In addition, move the calculation of hvmemul_ctxt->ctxt.swint_emulate from
_hvm_emulate_one() to hvm_emulate_init_once(), as it doesn't need recalculating for each instruction.

---
CC: Jan Beulich <[email protected]>
CC: Tim Deegan <[email protected]>
CC: George Dunlap <[email protected]>
CC: Paul Durrant <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             | 28 +++++++++++++++-------------
 xen/arch/x86/mm.c                      |  3 ++-
 xen/arch/x86/mm/shadow/common.c        |  4 ++--
 xen/arch/x86/x86_emulate/x86_emulate.c |  2 ++
 xen/arch/x86/x86_emulate/x86_emulate.h | 22 +++++++++++++++-------
 5 files changed, 36 insertions(+), 23 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -7
index 3ab0e8e..f24e211 100644
@@ -1769,13 +1769,6 @@ static int _hvm_emulate_one(struct
 hvm_emulate_ctxt *hvmemul_ctxt,
 
     vio->mmio_retry = 0;
 
-    if ( cpu_has_vmx )
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
-    else if ( cpu_has_svm_nrips )
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_icebp;
-    else
-        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_all;
-
     rc = x86_emulate(&hvmemul_ctxt->ctxt, ops);
 
     if ( rc == X86EMUL_OKAY && vio->mmio_retry )
xen/arch/x86/hvm/emulate.c +15 -6
@@ -1946,14 +1939,23 @@ void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     struct cpu_user_regs *regs)
 {
-    hvmemul_ctxt->intr_shadow =
 hvm_funcs.get_interrupt_shadow(current);
-    hvmemul_ctxt->ctxt.regs = regs;
-    hvmemul_ctxt->ctxt.force_writeback = 1;
-    hvmemul_ctxt->seg_reg_accessed = 0;
-    hvmemul_ctxt->seg_reg_dirty = 0;
-    hvmemul_ctxt->set_context = 0;
+    struct vcpu *curr = current;
+
+    memset(hvmemul_ctxt, 0, sizeof(*hvmemul_ctxt));
+
+    hvmemul_ctxt->intr_shadow = hvm_funcs.get_interrupt_shadow(curr);
     hvmemul_get_seg_reg(x86_seg_cs, hvmemul_ctxt);
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
+
+    hvmemul_ctxt->ctxt.regs = regs;
+    hvmemul_ctxt->ctxt.force_writeback = true;
+
+    if ( cpu_has_vmx )
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
+    else if ( cpu_has_svm_nrips )
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_icebp;
+    else
+        hvmemul_ctxt->ctxt.swint_emulate = x86_swint_emulate_all;
 }
 

void hvm_emulate_init_per_insn(

xen/arch/x86/mm.c +2 -1
index 03dcd71..9901f6f 100644
@@ -5363,8 +5363,9 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned
 long addr,
         goto bail;
     }
 
+    memset(&ptwr_ctxt, 0, sizeof(ptwr_ctxt));
+
     ptwr_ctxt.ctxt.regs = regs;
-    ptwr_ctxt.ctxt.force_writeback = 0;
     ptwr_ctxt.ctxt.addr_size = ptwr_ctxt.ctxt.sp_size =
         is_pv_32bit_domain(d) ? 32 : BITS_PER_LONG;
     ptwr_ctxt.ctxt.swint_emulate = x86_swint_emulate_none;
xen/arch/x86/mm/shadow/common.c +2 -1
index ced2313..6f6668d 100644
@@ -385,8 +385,9 @@ const struct x86_emulate_ops
 *shadow_init_emulation(
     struct vcpu *v = current;
     unsigned long addr;
 
+    memset(sh_ctxt, 0, sizeof(*sh_ctxt));
+
     sh_ctxt->ctxt.regs = regs;
-    sh_ctxt->ctxt.force_writeback = 0;
     sh_ctxt->ctxt.swint_emulate = x86_swint_emulate_none;
 
     if ( is_pv_vcpu(v) )
xen/arch/x86/mm/shadow/common.c +0 -1
@@ -396,7 +397,6 @@ const struct x86_emulate_ops
 *shadow_init_emulation(
     }
 
     /* Segment cache initialisation. Primed with CS. */
-    sh_ctxt->valid_seg_regs = 0;
     creg = hvm_get_seg_reg(x86_seg_cs, sh_ctxt);
 
     /* Work out the emulation mode. */
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
index 04f0dac..c5d9664 100644

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1909

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;
     ctxt->retire.byte = 0;

In the commit message you state that x86_decode() will "explicitly initalise all output state at its start". This doesn't seem to be all the output state. In fact you appear to be removing some initialization.

Quoted 22 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
     op_bytes = def_op_bytes = ad_bytes = def_ad_bytes = ctxt-
 >addr_size/8;
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
index 993c576..93b268e 100644
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 432

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -4
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;

Is this type change intentional? I assume it is, but you didn't call it out.

Paul

Quoted 25 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
     /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY).
 */
     union {
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
@@ -439,9 +450,6 @@ struct x86_emulate_ctxt
         } flags;
         uint8_t byte;
     } retire;
-
-    /* Caller data that can be used by x86_emulate_ops' routines. */
-    void *data;
 };
 
 /*
--
Quoted 1 line

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#10 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

Quoted 9 lines · Paul Durrant
Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
index 04f0dac..c5d9664 100644
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
Quoted 4 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1909

Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;
     ctxt->retire.byte = 0;

In the commit message you state that x86_decode() will "explicitly initalise all output state at its start". This doesn't seem to be all the output state. In fact you appear to be removing some initialization.

There are only two fields of output state, as delineated by the extra comments in x86_emulate_ctxt. Most of x86_emulate_ctxt is input state.

Quoted 29 lines
Quoted 29 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
     op_bytes = def_op_bytes = ad_bytes = def_ad_bytes = ctxt-
 > addr_size/8;
xen/arch/x86/x86_emulate/x86_emulate.h +4 -3
index 993c576..93b268e 100644
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
Quoted 4 lines

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 432

Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -1
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;

Is this type change intentional? I assume it is, but you didn't call it out.

Yes. I thought I had it in the commit message, but will update for v2.

~Andrew

Quoted 26 lines

Paul

Quoted 23 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
     /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY).
 */
     union {
xen/arch/x86/x86_emulate/x86_emulate.h +0 -3
@@ -439,9 +450,6 @@ struct x86_emulate_ctxt
         } flags;
         uint8_t byte;
     } retire;
-
-    /* Caller data that can be used by x86_emulate_ops' routines. */
-    void *data;
 };
 
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -1
 /*
--

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

Quoted 23 lines

-----Original Message-----
From: Andrew Cooper
Sent: 23 November 2016 16:01
To: Paul Durrant <[email protected]>; Xen-devel <xen- [email protected]>
Cc: Jan Beulich <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>
Subject: Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

Quoted 9 lines · Paul Durrant
Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
index 04f0dac..c5d9664 100644
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
Quoted 4 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1909

Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;
     ctxt->retire.byte = 0;

In the commit message you state that x86_decode() will "explicitly initalise

Quoted 5 lines

all output state at its start". This doesn't seem to be all the output state. In fact you appear to be removing some initialization.

There are only two fields of output state, as delineated by the extra comments in x86_emulate_ctxt. Most of x86_emulate_ctxt is input state.

D'oh, yes. Sorry, got confused by the field movements... my eyes were seeing '+' as '-' for some reason.

Paul

Quoted 66 lines
Quoted 30 lines
Quoted 30 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
     op_bytes = def_op_bytes = ad_bytes = def_ad_bytes = ctxt-
 > addr_size/8;
xen/arch/x86/x86_emulate/x86_emulate.h +4 -3
index 993c576..93b268e 100644
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
Quoted 4 lines

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 432

Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.h +1 -1
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;

Is this type change intentional? I assume it is, but you didn't call it out.

Yes. I thought I had it in the commit message, but will update for v2.

~Andrew

Quoted 14 lines

Paul

Quoted 11 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
Quoted 1 line

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 444

Quoted 1 line
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
     /* Retirement state, set by the emulator (valid only on

X86EMUL_OKAY).

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
 */
     union {
xen/arch/x86/x86_emulate/x86_emulate.h +0 -3
@@ -439,9 +450,6 @@ struct x86_emulate_ctxt
         } flags;
         uint8_t byte;
     } retire;
-
-    /* Caller data that can be used by x86_emulate_ops' routines. */
-    void *data;
 };
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -1
 /*
--

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

The current code to set up emulation state is ad-hoc and error prone.

* Consistently zero all emulation state structures.
* Avoid explicitly initialising some state to 0.
* Explicitly identify all input and output state in x86_emulate_ctxt. This involves rearanging some fields.
* Have x86_decode() explicitly initalise all output state at its start.

Quoted 5 lines

In addition, move the calculation of hvmemul_ctxt->ctxt.swint_emulate from _hvm_emulate_one() to hvm_emulate_init_once(), as it doesn't need recalculating for each instruction.

Shadow code changes Acked-by: Tim Deegan <[email protected]>

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/15
#17 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper
Quoted 59 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>
Subject: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

The x86 emulator needs to gain an understanding of interrupts and exceptions
generated by its actions. The naming choice is to match both the Intel and AMD terms, and to avoid 'trap' specifically as it has an architectural meaning different to its current usage.

While making this change, make other changes for consistency

* Rename *_trap() infrastructure to *_event()
* Rename trapnr/trap parameters to vector
* Convert hvm_inject_hw_exception() and hvm_inject_page_fault() to
being
static inlines, as they are only thin wrappers around hvm_inject_event()

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/emulate.c              |  6 +--
 xen/arch/x86/hvm/hvm.c                  | 33 ++++------------
 xen/arch/x86/hvm/io.c                   |  2 +-
 xen/arch/x86/hvm/svm/nestedsvm.c        |  7 ++--
 xen/arch/x86/hvm/svm/svm.c              | 62 ++++++++++++++---------------
 xen/arch/x86/hvm/vmx/vmx.c              | 66 +++++++++++++++----------------
 xen/arch/x86/hvm/vmx/vvmx.c             | 11 +++---
 xen/arch/x86/x86_emulate/x86_emulate.c  | 11 ++++++
 xen/arch/x86/x86_emulate/x86_emulate.h  | 22 +++++++++++
 xen/include/asm-x86/hvm/emulate.h       |  2 +-
 xen/include/asm-x86/hvm/hvm.h           | 69 ++++++++++++++++--------------
---
 xen/include/asm-x86/hvm/svm/nestedsvm.h |  6 +--
 xen/include/asm-x86/hvm/vcpu.h          |  2 +-
 xen/include/asm-x86/hvm/vmx/vvmx.h      |  4 +-
 14 files changed, 159 insertions(+), 144 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index f24e211..d0c3185 100644

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1682

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1679,7 +1679,7 @@ static int hvmemul_invlpg(
          * violations, so squash them.
          */
         hvmemul_ctxt->exn_pending = 0;
-        hvmemul_ctxt->trap = (struct hvm_trap){};
+        hvmemul_ctxt->trap = (struct x86_event){};

Can't say I like that way of initializing but...

Quoted 753 lines
xen/arch/x86/hvm/emulate.c +1 -1
         rc = X86EMUL_OKAY;
     }
 
@@ -1868,7 +1868,7 @@ int hvm_emulate_one_mmio(unsigned long mfn,
 unsigned long gla)
         break;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         /* fallthrough */
     default:
         hvm_emulate_writeback(&ctxt);
xen/arch/x86/hvm/emulate.c +1 -1
@@ -1928,7 +1928,7 @@ void hvm_emulate_one_vm_event(enum
 emul_kind kind, unsigned int trapnr,
         break;
     case X86EMUL_EXCEPTION:
         if ( ctx.exn_pending )
-            hvm_inject_trap(&ctx.trap);
+            hvm_inject_event(&ctx.trap);
         break;
     }
xen/arch/x86/hvm/hvm.c +6 -25
index 25dc759..7b434aa 100644
@@ -535,7 +535,7 @@ void hvm_do_resume(struct vcpu *v)
     /* Inject pending hw/sw trap */
     if ( v->arch.hvm_vcpu.inject_trap.vector != -1 )
     {
-        hvm_inject_trap(&v->arch.hvm_vcpu.inject_trap);
+        hvm_inject_event(&v->arch.hvm_vcpu.inject_trap);
         v->arch.hvm_vcpu.inject_trap.vector = -1;
     }
 }
@@ -1676,19 +1676,19 @@ void hvm_triple_fault(void)
     domain_shutdown(d, reason);
 }
 
-void hvm_inject_trap(const struct hvm_trap *trap)
+void hvm_inject_event(const struct x86_event *event)
 {
     struct vcpu *curr = current;
 
     if ( nestedhvm_enabled(curr->domain) &&
          !nestedhvm_vmswitch_in_progress(curr) &&
          nestedhvm_vcpu_in_guestmode(curr) &&
-         nhvm_vmcx_guest_intercepts_trap(
-             curr, trap->vector, trap->error_code) )
+         nhvm_vmcx_guest_intercepts_event(
+             curr, event->vector, event->error_code) )
     {
         enum nestedhvm_vmexits nsret;
 
-        nsret = nhvm_vcpu_vmexit_trap(curr, trap);
+        nsret = nhvm_vcpu_vmexit_event(curr, event);
 
         switch ( nsret )
         {
@@ -1704,26 +1704,7 @@ void hvm_inject_trap(const struct hvm_trap
 *trap)
         }
     }
 
-    hvm_funcs.inject_trap(trap);
-}
-
-void hvm_inject_hw_exception(unsigned int trapnr, int errcode)
-{
-    struct hvm_trap trap = {
-        .vector = trapnr,
-        .type = X86_EVENTTYPE_HW_EXCEPTION,
-        .error_code = errcode };
-    hvm_inject_trap(&trap);
-}
-
-void hvm_inject_page_fault(int errcode, unsigned long cr2)
-{
-    struct hvm_trap trap = {
-        .vector = TRAP_page_fault,
-        .type = X86_EVENTTYPE_HW_EXCEPTION,
-        .error_code = errcode,
-        .cr2 = cr2 };
-    hvm_inject_trap(&trap);
+    hvm_funcs.inject_event(event);
 }
 

int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,

xen/arch/x86/hvm/hvm.c +1 -1
@@ -4096,7 +4077,7 @@ void hvm_ud_intercept(struct cpu_user_regs
 *regs)
         break;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         /* fall through */
     default:
         hvm_emulate_writeback(&ctxt);
xen/arch/x86/hvm/io.c +1 -1
index 7305801..1279f68 100644
@@ -103,7 +103,7 @@ int handle_mmio(void)
         return 0;
     case X86EMUL_EXCEPTION:
         if ( ctxt.exn_pending )
-            hvm_inject_trap(&ctxt.trap);
+            hvm_inject_event(&ctxt.trap);
         break;
     default:
         break;
xen/arch/x86/hvm/svm/nestedsvm.c +4 -3
index f9b38ab..b6b8526 100644
@@ -821,7 +821,7 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct
 cpu_user_regs *regs,
 }
 
 int
-nsvm_vcpu_vmexit_trap(struct vcpu *v, const struct hvm_trap *trap)
+nsvm_vcpu_vmexit_event(struct vcpu *v, const struct x86_event *trap)
 {
     ASSERT(vcpu_nestedhvm(v).nv_vvmcx != NULL);
@@ -994,10 +994,11 @@ nsvm_vmcb_guest_intercepts_exitcode(struct
 vcpu *v,
 }
 
 bool_t
-nsvm_vmcb_guest_intercepts_trap(struct vcpu *v, unsigned int trapnr, int
 errcode)
+nsvm_vmcb_guest_intercepts_event(
+    struct vcpu *v, unsigned int vector, int errcode)
 {
     return nsvm_vmcb_guest_intercepts_exitcode(v,
-        guest_cpu_user_regs(), VMEXIT_EXCEPTION_DE + trapnr);
+        guest_cpu_user_regs(), VMEXIT_EXCEPTION_DE + vector);
 }

static int

xen/arch/x86/hvm/svm/svm.c +14 -14
index f737d8c..66eb30b 100644
@@ -1203,15 +1203,15 @@ static void svm_vcpu_destroy(struct vcpu *v)
     passive_domain_destroy(v);
 }
 
-static void svm_inject_trap(const struct hvm_trap *trap)
+static void svm_inject_event(const struct x86_event *event)
 {
     struct vcpu *curr = current;
     struct vmcb_struct *vmcb = curr->arch.hvm_svm.vmcb;
-    eventinj_t event = vmcb->eventinj;
-    struct hvm_trap _trap = *trap;
+    eventinj_t eventinj = vmcb->eventinj;
+    struct x86_event _event = *event;
     const struct cpu_user_regs *regs = guest_cpu_user_regs();
 
-    switch ( _trap.vector )
+    switch ( _event.vector )
     {
     case TRAP_debug:
         if ( regs->eflags & X86_EFLAGS_TF )
@@ -1229,21 +1229,21 @@ static void svm_inject_trap(const struct
 hvm_trap *trap)
         }
     }
 
-    if ( unlikely(event.fields.v) &&
-         (event.fields.type == X86_EVENTTYPE_HW_EXCEPTION) )
+    if ( unlikely(eventinj.fields.v) &&
+         (eventinj.fields.type == X86_EVENTTYPE_HW_EXCEPTION) )
     {
-        _trap.vector = hvm_combine_hw_exceptions(
-            event.fields.vector, _trap.vector);
-        if ( _trap.vector == TRAP_double_fault )
-            _trap.error_code = 0;
+        _event.vector = hvm_combine_hw_exceptions(
+            eventinj.fields.vector, _event.vector);
+        if ( _event.vector == TRAP_double_fault )
+            _event.error_code = 0;
     }
 
-    event.bytes = 0;
-    event.fields.v = 1;
-    event.fields.vector = _trap.vector;
+    eventinj.bytes = 0;
+    eventinj.fields.v = 1;
+    eventinj.fields.vector = _event.vector;
 
     /* Refer to AMD Vol 2: System Programming, 15.20 Event Injection. */
-    switch ( _trap.type )
+    switch ( _event.type )
     {
     case X86_EVENTTYPE_SW_INTERRUPT: /* int $n */
         /*
xen/arch/x86/hvm/svm/svm.c +2 -2
@@ -1253,8 +1253,8 @@ static void svm_inject_trap(const struct hvm_trap
 *trap)
          * moved eip forward if appropriate.
          */
         if ( cpu_has_svm_nrips )
-            vmcb->nextrip = regs->eip + _trap.insn_len;
-        event.fields.type = X86_EVENTTYPE_SW_INTERRUPT;
+            vmcb->nextrip = regs->eip + _event.insn_len;
+        eventinj.fields.type = X86_EVENTTYPE_SW_INTERRUPT;
         break;
 

case X86_EVENTTYPE_PRI_SW_EXCEPTION: /* icebp */

xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -1265,7 +1265,7 @@ static void svm_inject_trap(const struct hvm_trap
 *trap)
          */
         if ( cpu_has_svm_nrips )
             vmcb->nextrip = regs->eip;
-        event.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
+        eventinj.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
         break;
 

case X86_EVENTTYPE_SW_EXCEPTION: /* int3, into */

xen/arch/x86/hvm/svm/svm.c +10 -10
@@ -1279,28 +1279,28 @@ static void svm_inject_trap(const struct
 hvm_trap *trap)
          * the correct faulting eip should a fault occur.
          */
         if ( cpu_has_svm_nrips )
-            vmcb->nextrip = regs->eip + _trap.insn_len;
-        event.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
+            vmcb->nextrip = regs->eip + _event.insn_len;
+        eventinj.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
         break;
 
     default:
-        event.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
-        event.fields.ev = (_trap.error_code !=
 HVM_DELIVER_NO_ERROR_CODE);
-        event.fields.errorcode = _trap.error_code;
+        eventinj.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
+        eventinj.fields.ev = (_event.error_code !=
 HVM_DELIVER_NO_ERROR_CODE);
+        eventinj.fields.errorcode = _event.error_code;
         break;
     }
 
-    vmcb->eventinj = event;
+    vmcb->eventinj = eventinj;
 
-    if ( _trap.vector == TRAP_page_fault )
+    if ( _event.vector == TRAP_page_fault )
     {
-        curr->arch.hvm_vcpu.guest_cr[2] = _trap.cr2;
-        vmcb_set_cr2(vmcb, _trap.cr2);
-        HVMTRACE_LONG_2D(PF_INJECT, _trap.error_code,
 TRC_PAR_LONG(_trap.cr2));
+        curr->arch.hvm_vcpu.guest_cr[2] = _event.cr2;
+        vmcb_set_cr2(vmcb, _event.cr2);
+        HVMTRACE_LONG_2D(PF_INJECT, _event.error_code,
 TRC_PAR_LONG(_event.cr2));
     }
     else
     {
xen/arch/x86/hvm/svm/svm.c +2 -2
-        HVMTRACE_2D(INJ_EXC, _trap.vector, _trap.error_code);
+        HVMTRACE_2D(INJ_EXC, _event.vector, _event.error_code);
     }
 }
@@ -2250,7 +2250,7 @@ static struct hvm_function_table __initdata
 svm_function_table = {
     .set_guest_pat        = svm_set_guest_pat,
     .get_guest_pat        = svm_get_guest_pat,
     .set_tsc_offset       = svm_set_tsc_offset,
-    .inject_trap          = svm_inject_trap,
+    .inject_event         = svm_inject_event,
     .init_hypercall_page  = svm_init_hypercall_page,
     .event_pending        = svm_event_pending,

.invlpg = svm_invlpg,

xen/arch/x86/hvm/svm/svm.c +2 -2
@@ -2265,9 +2265,9 @@ static struct hvm_function_table __initdata
 svm_function_table = {
     .nhvm_vcpu_initialise = nsvm_vcpu_initialise,
     .nhvm_vcpu_destroy = nsvm_vcpu_destroy,
     .nhvm_vcpu_reset = nsvm_vcpu_reset,
-    .nhvm_vcpu_vmexit_trap = nsvm_vcpu_vmexit_trap,
+    .nhvm_vcpu_vmexit_event = nsvm_vcpu_vmexit_event,
     .nhvm_vcpu_p2m_base = nsvm_vcpu_hostcr3,
-    .nhvm_vmcx_guest_intercepts_trap =
 nsvm_vmcb_guest_intercepts_trap,
+    .nhvm_vmcx_guest_intercepts_event =
 nsvm_vmcb_guest_intercepts_event,

.nhvm_vmcx_hap_enabled = nsvm_vmcb_hap_enabled, .nhvm_intr_blocked = nsvm_intr_blocked, .nhvm_hap_walk_L1_p2m = nsvm_hap_walk_L1_p2m,

xen/arch/x86/hvm/vmx/vmx.c +2 -2
index 0a52624..b1d8a0b 100644
@@ -1623,9 +1623,9 @@ void nvmx_enqueue_n2_exceptions(struct vcpu
 *v,
                  nvmx->intr.intr_info, nvmx->intr.error_code);
 }
 
-static int nvmx_vmexit_trap(struct vcpu *v, const struct hvm_trap *trap)
+static int nvmx_vmexit_event(struct vcpu *v, const struct x86_event
 *event)
 {
-    nvmx_enqueue_n2_exceptions(v, trap->vector, trap->error_code,
+    nvmx_enqueue_n2_exceptions(v, event->vector, event->error_code,
                                hvm_intsrc_none);
     return NESTEDHVM_VMEXIT_DONE;

}

xen/arch/x86/hvm/vmx/vmx.c +3 -3
@@ -1707,13 +1707,13 @@ void vmx_inject_nmi(void)
  *  - #DB is X86_EVENTTYPE_HW_EXCEPTION, except when generated by
  *    opcode 0xf1 (which is X86_EVENTTYPE_PRI_SW_EXCEPTION)
  */
-static void vmx_inject_trap(const struct hvm_trap *trap)
+static void vmx_inject_event(const struct x86_event *event)
 {
     unsigned long intr_info;
     struct vcpu *curr = current;
-    struct hvm_trap _trap = *trap;
+    struct x86_event _event = *event;
 
-    switch ( _trap.vector | -(_trap.type == X86_EVENTTYPE_SW_INTERRUPT) )
+    switch ( _event.vector | -(_event.type ==
 X86_EVENTTYPE_SW_INTERRUPT) )
     {
     case TRAP_debug:
         if ( guest_cpu_user_regs()->eflags & X86_EFLAGS_TF )
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -1722,7 +1722,7 @@ static void vmx_inject_trap(const struct hvm_trap
 *trap)
             write_debugreg(6, read_debugreg(6) | DR_STEP);
         }
         if ( !nestedhvm_vcpu_in_guestmode(curr) ||
-             !nvmx_intercepts_exception(curr, TRAP_debug, _trap.error_code) )
+             !nvmx_intercepts_exception(curr, TRAP_debug, _event.error_code)
 )
         {
             unsigned long val;
xen/arch/x86/hvm/vmx/vmx.c +17 -17
@@ -1744,8 +1744,8 @@ static void vmx_inject_trap(const struct hvm_trap
 *trap)
         break;
 
     case TRAP_page_fault:
-        ASSERT(_trap.type == X86_EVENTTYPE_HW_EXCEPTION);
-        curr->arch.hvm_vcpu.guest_cr[2] = _trap.cr2;
+        ASSERT(_event.type == X86_EVENTTYPE_HW_EXCEPTION);
+        curr->arch.hvm_vcpu.guest_cr[2] = _event.cr2;
         break;
     }
@@ -1758,34 +1758,34 @@ static void vmx_inject_trap(const struct
 hvm_trap *trap)
          (MASK_EXTR(intr_info, INTR_INFO_INTR_TYPE_MASK) ==
           X86_EVENTTYPE_HW_EXCEPTION) )
     {
-        _trap.vector = hvm_combine_hw_exceptions(
-            (uint8_t)intr_info, _trap.vector);
-        if ( _trap.vector == TRAP_double_fault )
-            _trap.error_code = 0;
+        _event.vector = hvm_combine_hw_exceptions(
+            (uint8_t)intr_info, _event.vector);
+        if ( _event.vector == TRAP_double_fault )
+            _event.error_code = 0;
     }
 
-    if ( _trap.type >= X86_EVENTTYPE_SW_INTERRUPT )
-        __vmwrite(VM_ENTRY_INSTRUCTION_LEN, _trap.insn_len);
+    if ( _event.type >= X86_EVENTTYPE_SW_INTERRUPT )
+        __vmwrite(VM_ENTRY_INSTRUCTION_LEN, _event.insn_len);
 
     if ( nestedhvm_vcpu_in_guestmode(curr) &&
-         nvmx_intercepts_exception(curr, _trap.vector, _trap.error_code) )
+         nvmx_intercepts_exception(curr, _event.vector, _event.error_code) )
     {
         nvmx_enqueue_n2_exceptions (curr,
             INTR_INFO_VALID_MASK |
-            MASK_INSR(_trap.type, INTR_INFO_INTR_TYPE_MASK) |
-            MASK_INSR(_trap.vector, INTR_INFO_VECTOR_MASK),
-            _trap.error_code, hvm_intsrc_none);
+            MASK_INSR(_event.type, INTR_INFO_INTR_TYPE_MASK) |
+            MASK_INSR(_event.vector, INTR_INFO_VECTOR_MASK),
+            _event.error_code, hvm_intsrc_none);
         return;
     }
     else
-        __vmx_inject_exception(_trap.vector, _trap.type, _trap.error_code);
+        __vmx_inject_exception(_event.vector, _event.type,
 _event.error_code);
 
-    if ( (_trap.vector == TRAP_page_fault) &&
-         (_trap.type == X86_EVENTTYPE_HW_EXCEPTION) )
-        HVMTRACE_LONG_2D(PF_INJECT, _trap.error_code,
+    if ( (_event.vector == TRAP_page_fault) &&
+         (_event.type == X86_EVENTTYPE_HW_EXCEPTION) )
+        HVMTRACE_LONG_2D(PF_INJECT, _event.error_code,
                          TRC_PAR_LONG(curr->arch.hvm_vcpu.guest_cr[2]));
     else
-        HVMTRACE_2D(INJ_EXC, _trap.vector, _trap.error_code);
+        HVMTRACE_2D(INJ_EXC, _event.vector, _event.error_code);
 }

static int vmx_event_pending(struct vcpu *v)

xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2162,7 +2162,7 @@ static struct hvm_function_table __initdata
 vmx_function_table = {
     .set_guest_pat        = vmx_set_guest_pat,
     .get_guest_pat        = vmx_get_guest_pat,
     .set_tsc_offset       = vmx_set_tsc_offset,
-    .inject_trap          = vmx_inject_trap,
+    .inject_event         = vmx_inject_event,
     .init_hypercall_page  = vmx_init_hypercall_page,
     .event_pending        = vmx_event_pending,

.invlpg = vmx_invlpg,

xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -2182,8 +2182,8 @@ static struct hvm_function_table __initdata
 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_trap = nvmx_intercepts_exception,
-    .nhvm_vcpu_vmexit_trap = nvmx_vmexit_trap,
+    .nhvm_vmcx_guest_intercepts_event = nvmx_intercepts_exception,
+    .nhvm_vcpu_vmexit_event = nvmx_vmexit_event,
     .nhvm_intr_blocked    = nvmx_intr_blocked,
     .nhvm_domain_relinquish_resources =

nvmx_domain_relinquish_resources,
.update_eoi_exit_bitmap = vmx_update_eoi_exit_bitmap,

xen/arch/x86/hvm/vmx/vmx.c +7 -7
@@ -3201,7 +3201,7 @@ static int vmx_handle_eoi_write(void)
  */
 static void vmx_propagate_intr(unsigned long intr)
 {
-    struct hvm_trap trap = {
+    struct x86_event event = {
         .vector = MASK_EXTR(intr, INTR_INFO_VECTOR_MASK),
         .type = MASK_EXTR(intr, INTR_INFO_INTR_TYPE_MASK),
     };
@@ -3210,20 +3210,20 @@ static void vmx_propagate_intr(unsigned long
 intr)
     if ( intr & INTR_INFO_DELIVER_CODE_MASK )
     {
         __vmread(VM_EXIT_INTR_ERROR_CODE, &tmp);
-        trap.error_code = tmp;
+        event.error_code = tmp;
     }
     else
-        trap.error_code = HVM_DELIVER_NO_ERROR_CODE;
+        event.error_code = HVM_DELIVER_NO_ERROR_CODE;
 
-    if ( trap.type >= X86_EVENTTYPE_SW_INTERRUPT )
+    if ( event.type >= X86_EVENTTYPE_SW_INTERRUPT )
     {
         __vmread(VM_EXIT_INSTRUCTION_LEN, &tmp);
-        trap.insn_len = tmp;
+        event.insn_len = tmp;
     }
     else
-        trap.insn_len = 0;
+        event.insn_len = 0;
 
-    hvm_inject_trap(&trap);
+    hvm_inject_event(&event);
 }
 

static void vmx_idtv_reinject(unsigned long idtv_info)

xen/arch/x86/hvm/vmx/vvmx.c +6 -5
index bed2e0a..b5837d4 100644
@@ -491,18 +491,19 @@ static void vmreturn(struct cpu_user_regs *regs,
 enum vmx_ops_result ops_res)
     regs->eflags = eflags;
 }
 
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code)
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code)
 {
     u32 exception_bitmap, pfec_match=0, pfec_mask=0;
     int r;
 
-    ASSERT ( trap < 32 );
+    ASSERT(vector < 32);
 
     exception_bitmap = get_vvmcs(v, EXCEPTION_BITMAP);
-    r = exception_bitmap & (1 << trap) ? 1: 0;
+    r = exception_bitmap & (1 << vector) ? 1: 0;
 
-    if ( trap == TRAP_page_fault ) {
+    if ( vector == TRAP_page_fault )
+    {
         pfec_match = get_vvmcs(v, PAGE_FAULT_ERROR_CODE_MATCH);
         pfec_mask  = get_vvmcs(v, PAGE_FAULT_ERROR_CODE_MASK);
         if ( (error_code & pfec_mask) != pfec_match )
xen/arch/x86/x86_emulate/x86_emulate.c +3 -0
index c5d9664..2e65322 100644
@@ -5453,6 +5453,17 @@ static void __init __maybe_unused
 build_assertions(void)
     BUILD_BUG_ON(x86_seg_ds != 3);
     BUILD_BUG_ON(x86_seg_fs != 4);
     BUILD_BUG_ON(x86_seg_gs != 5);
+
+    /*
+     * Check X86_EVENTTYPE_* against VMCB EVENTINJ and VMCS

INTR_INFO type

xen/arch/x86/x86_emulate/x86_emulate.c +8 -0
+     * fields.
+     */
+    BUILD_BUG_ON(X86_EVENTTYPE_EXT_INTR != 0);
+    BUILD_BUG_ON(X86_EVENTTYPE_NMI != 2);
+    BUILD_BUG_ON(X86_EVENTTYPE_HW_EXCEPTION != 3);
+    BUILD_BUG_ON(X86_EVENTTYPE_SW_INTERRUPT != 4);
+    BUILD_BUG_ON(X86_EVENTTYPE_PRI_SW_EXCEPTION != 5);
+    BUILD_BUG_ON(X86_EVENTTYPE_SW_EXCEPTION != 6);
 }
 
 #ifdef __XEN__
xen/arch/x86/x86_emulate/x86_emulate.h +22 -0
index 93b268e..b0d8e6f 100644
@@ -67,6 +67,28 @@ enum x86_swint_emulation {
     x86_swint_emulate_all,  /* Help needed with all software events */
 };
 
+/*
+ * x86 event types. This enumeration is valid for:
+ *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
+ *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
+ */
+enum x86_event_type {
+    X86_EVENTTYPE_EXT_INTR,         /* External interrupt */
+    X86_EVENTTYPE_NMI = 2,          /* NMI */
+    X86_EVENTTYPE_HW_EXCEPTION,     /* Hardware exception */
+    X86_EVENTTYPE_SW_INTERRUPT,     /* Software interrupt (CD nn) */
+    X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
+    X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
+};
+
+struct x86_event {
+    int16_t       vector;
+    uint8_t       type;         /* X86_EVENTTYPE_* */
+    uint8_t       insn_len;     /* Instruction length */
+    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
+};
+
 /*
  * Attribute for segment selector. This is a copy of bit 40:47 & 52:55 of the
  * segment descriptor. It happens to match the format of an AMD SVM

VMCB.

xen/include/asm- renamed from xen/include/asm-x86/hvm/emulate.h +0 -0

x86/hvm/emulate.h
index d4186a2..3b7ec33 100644

xen/include/asm-x86/hvm/emulate.h +1 -1
@@ -30,7 +30,7 @@ struct hvm_emulate_ctxt {
     unsigned long seg_reg_dirty;
 
     bool_t exn_pending;
-    struct hvm_trap trap;
+    struct x86_event trap;
 
     uint32_t intr_shadow;
 
xen/include/asm- renamed from xen/include/asm-x86/hvm/hvm.h +0 -0

x86/hvm/hvm.h
index 7e7462e..51a64f7 100644

xen/include/asm-x86/hvm/hvm.h +4 -13
@@ -77,14 +77,6 @@ enum hvm_intblk {
 #define HVM_HAP_SUPERPAGE_2MB   0x00000001
 #define HVM_HAP_SUPERPAGE_1GB   0x00000002
 
-struct hvm_trap {
-    int16_t       vector;
-    uint8_t       type;         /* X86_EVENTTYPE_* */
-    uint8_t       insn_len;     /* Instruction length */
-    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
-    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
-};
-
 /*
  * The hardware virtual machine (HVM) interface abstracts away from the
  * x86/x86_64 CPU virtualization assist specifics. Currently this interface
@@ -152,7 +144,7 @@ struct hvm_function_table {
 
     void (*set_tsc_offset)(struct vcpu *v, u64 offset, u64 at_tsc);
 
-    void (*inject_trap)(const struct hvm_trap *trap);
+    void (*inject_event)(const struct x86_event *event);
 
     void (*init_hypercall_page)(struct domain *d, void *hypercall_page);
 
@@ -185,11 +177,10 @@ struct hvm_function_table {
     int (*nhvm_vcpu_initialise)(struct vcpu *v);
     void (*nhvm_vcpu_destroy)(struct vcpu *v);
     int (*nhvm_vcpu_reset)(struct vcpu *v);
-    int (*nhvm_vcpu_vmexit_trap)(struct vcpu *v, const struct hvm_trap
 *trap);
+    int (*nhvm_vcpu_vmexit_event)(struct vcpu *v, const struct x86_event
 *event);
     uint64_t (*nhvm_vcpu_p2m_base)(struct vcpu *v);
-    bool_t (*nhvm_vmcx_guest_intercepts_trap)(struct vcpu *v,
-                                              unsigned int trapnr,
-                                              int errcode);
+    bool_t (*nhvm_vmcx_guest_intercepts_event)(
+        struct vcpu *v, unsigned int vector, int errcode);
 
     bool_t (*nhvm_vmcx_hap_enabled)(struct vcpu *v);
xen/include/asm-x86/hvm/hvm.h +24 -3
@@ -419,9 +410,30 @@ void hvm_migrate_timers(struct vcpu *v);
 void hvm_do_resume(struct vcpu *v);
 void hvm_migrate_pirqs(struct vcpu *v);
 
-void hvm_inject_trap(const struct hvm_trap *trap);
-void hvm_inject_hw_exception(unsigned int trapnr, int errcode);
-void hvm_inject_page_fault(int errcode, unsigned long cr2);
+void hvm_inject_event(const struct x86_event *event);
+
+static inline void hvm_inject_hw_exception(unsigned int vector, int
 errcode)
+{
+    struct x86_event event = {
+        .vector = vector,
+        .type = X86_EVENTTYPE_HW_EXCEPTION,
+        .error_code = errcode,
+    };
+
+    hvm_inject_event(&event);
+}
+
+static inline void hvm_inject_page_fault(int errcode, unsigned long cr2)
+{
+    struct x86_event event = {
+        .vector = TRAP_page_fault,
+        .type = X86_EVENTTYPE_HW_EXCEPTION,
+        .error_code = errcode,
+        .cr2 = cr2,
+    };
+
+    hvm_inject_event(&event);
+}
 
 static inline int hvm_event_pending(struct vcpu *v)

{

xen/include/asm-x86/hvm/hvm.h +0 -12
@@ -437,18 +449,6 @@ static inline int hvm_event_pending(struct vcpu *v)
                        (1U << TRAP_alignment_check) | \
                        (1U << TRAP_machine_check))
 
-/*
- * x86 event types. This enumeration is valid for:
- *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
- *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
- */
-#define X86_EVENTTYPE_EXT_INTR         0 /* external interrupt */
-#define X86_EVENTTYPE_NMI              2 /* NMI */
-#define X86_EVENTTYPE_HW_EXCEPTION     3 /* hardware exception */
-#define X86_EVENTTYPE_SW_INTERRUPT     4 /* software interrupt (CD nn)
 */
-#define X86_EVENTTYPE_PRI_SW_EXCEPTION 5 /* ICEBP (F1) */
-#define X86_EVENTTYPE_SW_EXCEPTION     6 /* INT3 (CC), INTO (CE) */
-
 int hvm_event_needs_reinjection(uint8_t type, uint8_t vector);
 

uint8_t hvm_combine_hw_exceptions(uint8_t vec1, uint8_t vec2);

xen/include/asm-x86/hvm/hvm.h +3 -3
@@ -542,10 +542,10 @@ int hvm_x2apic_msr_write(struct vcpu *v,
 unsigned int msr, uint64_t msr_content)
 /* inject vmexit into l1 guest. l1 guest will see a VMEXIT due to
  * 'trapnr' exception.
  */
-static inline int nhvm_vcpu_vmexit_trap(struct vcpu *v,
-                                        const struct hvm_trap *trap)
+static inline int nhvm_vcpu_vmexit_event(
+    struct vcpu *v, const struct x86_event *event)
 {
-    return hvm_funcs.nhvm_vcpu_vmexit_trap(v, trap);
+    return hvm_funcs.nhvm_vcpu_vmexit_event(v, event);
 }
 

/* returns l1 guest's cr3 that points to the page table used to

xen/include/asm-x86/hvm/hvm.h +3 -4
@@ -557,11 +557,10 @@ static inline uint64_t nhvm_vcpu_p2m_base(struct
 vcpu *v)
 }
 
 /* returns true, when l1 guest intercepts the specified trap */
-static inline bool_t nhvm_vmcx_guest_intercepts_trap(struct vcpu *v,
-                                                     unsigned int trap,
-                                                     int errcode)
+static inline bool_t nhvm_vmcx_guest_intercepts_event(
+    struct vcpu *v, unsigned int vector, int errcode)
 {
-    return hvm_funcs.nhvm_vmcx_guest_intercepts_trap(v, trap, errcode);
+    return hvm_funcs.nhvm_vmcx_guest_intercepts_event(v, vector,
 errcode);
 }

/* returns true when l1 guest wants to use hap to run l2 guest */

xen/include/asm-x86/hvm/svm/nestedsvm.h +3 -3
index 0dbc5ec..4b36c25 100644
@@ -110,10 +110,10 @@ void nsvm_vcpu_destroy(struct vcpu *v);
 int nsvm_vcpu_initialise(struct vcpu *v);
 int nsvm_vcpu_reset(struct vcpu *v);
 int nsvm_vcpu_vmrun(struct vcpu *v, struct cpu_user_regs *regs);
-int nsvm_vcpu_vmexit_trap(struct vcpu *v, const struct hvm_trap *trap);
+int nsvm_vcpu_vmexit_event(struct vcpu *v, const struct x86_event
 *event);
 uint64_t nsvm_vcpu_hostcr3(struct vcpu *v);
-bool_t nsvm_vmcb_guest_intercepts_trap(struct vcpu *v, unsigned int
 trapnr,
-                                       int errcode);
+bool_t nsvm_vmcb_guest_intercepts_event(
+    struct vcpu *v, unsigned int vector, int errcode);
 bool_t nsvm_vmcb_hap_enabled(struct vcpu *v);

enum hvm_intblk nsvm_intr_blocked(struct vcpu *v);

xen/include/asm- renamed from xen/include/asm-x86/hvm/vcpu.h +0 -0

x86/hvm/vcpu.h
index 84d9406..d485536 100644

xen/include/asm-x86/hvm/vcpu.h +1 -1
@@ -206,7 +206,7 @@ struct hvm_vcpu {
     void *fpu_exception_callback_arg;
 
     /* Pending hw/sw interrupt (.vector = -1 means nothing pending). */
-    struct hvm_trap     inject_trap;
+    struct x86_event     inject_trap;
 
     struct viridian_vcpu viridian;
 };
xen/include/asm- renamed from xen/include/asm-x86/hvm/vmx/vvmx.h +0 -0

x86/hvm/vmx/vvmx.h
index aca8b4b..ead586e 100644

xen/include/asm-x86/hvm/vmx/vvmx.h +2 -3
@@ -112,8 +112,8 @@ void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
 uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code);
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code);
 void nvmx_domain_relinquish_resources(struct domain *d);
 
 bool_t nvmx_ept_enabled(struct vcpu *v);
--
Quoted 1 line

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#82 lore raw

Re: [PATCH 13/15] x86/hvm: Avoid __hvm_copy() raising #PF behind the emulators back

In reply to Andrew Cooper

xen/arch/x86/mm/shadow/common.c hvm_read(enum x86_segment seg, · line 204

Quoted 12 lines · Andrew Cooper
xen/arch/x86/mm/shadow/common.c +1 -0
index afacd5f..88d4642 100644
@@ -198,6 +198,7 @@ hvm_read(enum x86_segment seg,
     case HVMCOPY_okay:
         return X86EMUL_OKAY;
     case HVMCOPY_bad_gva_to_gfn:
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
     case HVMCOPY_unhandleable:

I realise I have forgotten to adjust this change to being x86_emul_pagefault(). emulate_gva_to_mfn() also needs modifying in a similar vein.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#35 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper

Hi,

Quoted 8 lines · Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so they are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

Quoted 6 lines

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending event to fall back into the previous codepath.

No overall functional change.

AIUI this does have a change in the shadow callers in the case where the emulated instruction would inject an event. Previously we would have failed the emulation, perhaps unshadowed something, and returned to the guest to retry.

Now the emulator records the event in the context struct, updates the register state and returns success, so we'll return on the *next* instruction. I think that's OK, though.

Also, handle_mmio() and other callers of the emulator check for that pending event and pass it to the hardware but you haven't added anything in the shadow code to do that. Does the event get dropped?

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 4/15
#25 lore raw

Re: [PATCH 04/15] x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

In reply to Andrew Cooper
Quoted 12 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Boris Ostrovsky <[email protected]>; Suravee Suthikulpanit <[email protected]>
Subject: [PATCH 04/15] x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

Quoted 6 lines

and move it to live with the other x86_event infrastructure in x86_emulate.h.
Switch it and x86_event.error_code to being signed, matching the rest of the code.

Quoted 415 lines

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/emulate.c             |  2 +-
 xen/arch/x86/hvm/hvm.c                 |  6 +++---
 xen/arch/x86/hvm/nestedhvm.c           |  2 +-
 xen/arch/x86/hvm/svm/nestedsvm.c       |  6 +++---
 xen/arch/x86/hvm/svm/svm.c             | 22 +++++++++++-----------
 xen/arch/x86/hvm/vmx/intr.c            |  2 +-
 xen/arch/x86/hvm/vmx/vmx.c             | 23 ++++++++++++-----------
 xen/arch/x86/hvm/vmx/vvmx.c            |  2 +-
 xen/arch/x86/x86_emulate/x86_emulate.h |  3 ++-
 xen/include/asm-x86/hvm/support.h      |  2 --
 10 files changed, 35 insertions(+), 35 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
index d0c3185..790e9c1 100644
@@ -1609,7 +1609,7 @@ static int hvmemul_inject_sw_interrupt(
 
     hvmemul_ctxt->exn_pending = 1;
     hvmemul_ctxt->trap.vector = vector;
-    hvmemul_ctxt->trap.error_code = HVM_DELIVER_NO_ERROR_CODE;
+    hvmemul_ctxt->trap.error_code = X86_EVENT_NO_EC;
     hvmemul_ctxt->trap.insn_len = insn_len;
 
     return X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +2 -2
index 7b434aa..b950842 100644
@@ -502,7 +502,7 @@ void hvm_do_resume(struct vcpu *v)
                 kind = EMUL_KIND_SET_CONTEXT_INSN;
 
             hvm_emulate_one_vm_event(kind, TRAP_invalid_op,
-                                     HVM_DELIVER_NO_ERROR_CODE);
+                                     X86_EVENT_NO_EC);
 
             v->arch.vm_event->emulate_flags = 0;
         }
@@ -3054,7 +3054,7 @@ void hvm_task_switch(
     }
 
     if ( (tss.trace & 1) && !exn_raised )
-        hvm_inject_hw_exception(TRAP_debug,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 
  out:
     hvm_unmap_entry(optss_desc);
xen/arch/x86/hvm/hvm.c +1 -1
@@ -4073,7 +4073,7 @@ void hvm_ud_intercept(struct cpu_user_regs
 *regs)
     switch ( hvm_emulate_one(&ctxt) )
     {
     case X86EMUL_UNHANDLEABLE:
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;

case X86EMUL_EXCEPTION:
if ( ctxt.exn_pending )

xen/arch/x86/hvm/nestedhvm.c +0 -1
index caad525..c4671d8 100644
@@ -17,7 +17,7 @@
  */
 
 #include <asm/msr.h>
-#include <asm/hvm/support.h>	/* for

HVM_DELIVER_NO_ERROR_CODE */

xen/arch/x86/hvm/nestedhvm.c +1 -0
+#include <asm/hvm/support.h>
 #include <asm/hvm/hvm.h>
 #include <asm/p2m.h>    /* for struct p2m_domain */
 #include <asm/hvm/nestedhvm.h>
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index b6b8526..8c9b073 100644
@@ -756,7 +756,7 @@ nsvm_vcpu_vmrun(struct vcpu *v, struct
 cpu_user_regs *regs)
     default:
         gdprintk(XENLOG_ERR,
             "nsvm_vcpu_vmentry failed, injecting #UD\n");
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         /* Must happen after hvm_inject_hw_exception or it doesn't work

right. */

         nv->nv_vmswitch_in_progress = 0;
         return 1;
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1581,7 +1581,7 @@ void svm_vmexit_do_stgi(struct cpu_user_regs
 *regs, struct vcpu *v)
     unsigned int inst_len;
 
     if ( !nestedhvm_enabled(v->domain) ) {
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
@@ -1601,7 +1601,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs
 *regs, struct vcpu *v)
     vintr_t intr;
 
     if ( !nestedhvm_enabled(v->domain) ) {
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
xen/arch/x86/hvm/svm/svm.c +1 -1
index 66eb30b..fb4fd0b 100644
@@ -89,7 +89,7 @@ static DEFINE_SPINLOCK(osvw_lock);
 static void svm_crash_or_fault(struct vcpu *v)
 {
     if ( vmcb_get_cpl(v->arch.hvm_svm.vmcb) )
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
     else
         domain_crash(v->domain);

}

xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -116,7 +116,7 @@ void __update_guest_eip(struct cpu_user_regs
 *regs, unsigned int inst_len)
     curr->arch.hvm_svm.vmcb->interrupt_shadow = 0;
 
     if ( regs->eflags & X86_EFLAGS_TF )
-        hvm_inject_hw_exception(TRAP_debug,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 }

static void svm_cpu_down(void)

xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -1285,7 +1285,7 @@ static void svm_inject_event(const struct
 x86_event *event)
 
     default:
         eventinj.fields.type = X86_EVENTTYPE_HW_EXCEPTION;
-        eventinj.fields.ev = (_event.error_code !=
 HVM_DELIVER_NO_ERROR_CODE);
+        eventinj.fields.ev = (_event.error_code != X86_EVENT_NO_EC);
         eventinj.fields.errorcode = _event.error_code;
         break;
     }
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -1553,7 +1553,7 @@ static void svm_fpu_dirty_intercept(void)
     {
        /* Check if l1 guest must make FPU ready for the l2 guest */
        if ( v->arch.hvm_vcpu.guest_cr[0] & X86_CR0_TS )
-           hvm_inject_hw_exception(TRAP_no_device,
 HVM_DELIVER_NO_ERROR_CODE);
+           hvm_inject_hw_exception(TRAP_no_device, X86_EVENT_NO_EC);
        else
            vmcb_set_cr0(n1vmcb, vmcb_get_cr0(n1vmcb) & ~X86_CR0_TS);
        return;
xen/arch/x86/hvm/svm/svm.c +1 -2
@@ -2022,14 +2022,14 @@ svm_vmexit_do_vmrun(struct cpu_user_regs
 *regs,
     if ( !nsvm_efer_svm_enabled(v) )
     {
         gdprintk(XENLOG_ERR, "VMRUN: nestedhvm disabled, injecting
 #UD\n");
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         return;
     }
 
     if ( !nestedsvm_vmcb_map(v, vmcbaddr) )
     {
         gdprintk(XENLOG_ERR, "VMRUN: mapping vmcb failed, injecting
 #GP\n");
-        hvm_inject_hw_exception(TRAP_gp_fault,

HVM_DELIVER_NO_ERROR_CODE);

xen/arch/x86/hvm/svm/svm.c +4 -3
+        hvm_inject_hw_exception(TRAP_gp_fault, X86_EVENT_NO_EC);
         return;
     }
@@ -2101,7 +2101,7 @@ svm_vmexit_do_vmload(struct vmcb_struct
 *vmcb,
     return;
 
  inject:
-    hvm_inject_hw_exception(ret, HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(ret, X86_EVENT_NO_EC);
     return;
 }
@@ -2139,7 +2139,7 @@ svm_vmexit_do_vmsave(struct vmcb_struct
 *vmcb,
     return;
 
  inject:
-    hvm_inject_hw_exception(ret, HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(ret, X86_EVENT_NO_EC);
     return;
 }
@@ -2428,7 +2428,7 @@ void svm_vmexit_handler(struct cpu_user_regs
 *regs)
 
     case VMEXIT_EXCEPTION_DB:
         if ( !v->domain->debugger_attached )
-            hvm_inject_hw_exception(TRAP_debug,
 HVM_DELIVER_NO_ERROR_CODE);
+            hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
         else
             domain_pause_for_debugger();
         break;
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -2616,7 +2616,7 @@ void svm_vmexit_handler(struct cpu_user_regs
 *regs)
 
     case VMEXIT_MONITOR:
     case VMEXIT_MWAIT:
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;

case VMEXIT_VMRUN:

xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -2635,7 +2635,7 @@ void svm_vmexit_handler(struct cpu_user_regs
 *regs)
         svm_vmexit_do_clgi(regs, v);
         break;
     case VMEXIT_SKINIT:
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;

case VMEXIT_XSETBV:

xen/arch/x86/hvm/vmx/intr.c +1 -1
index 8fca08c..639a705 100644
@@ -302,7 +302,7 @@ void vmx_intr_assist(void)
     }
     else if ( intack.source == hvm_intsrc_mce )
     {
-        hvm_inject_hw_exception(TRAP_machine_check,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_machine_check,
 X86_EVENT_NO_EC);
     }
     else if ( cpu_has_vmx_virtual_intr_delivery &&
               intack.source != hvm_intsrc_pic &&
xen/arch/x86/hvm/vmx/vmx.c +2 -1
index b1d8a0b..eb7c902 100644
@@ -1646,7 +1646,8 @@ static void __vmx_inject_exception(int trap, int
 type, int error_code)
     intr_fields = INTR_INFO_VALID_MASK |
                   MASK_INSR(type, INTR_INFO_INTR_TYPE_MASK) |
                   MASK_INSR(trap, INTR_INFO_VECTOR_MASK);
-    if ( error_code != HVM_DELIVER_NO_ERROR_CODE ) {
+    if ( error_code != X86_EVENT_NO_EC )
+    {
         __vmwrite(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
         intr_fields |= INTR_INFO_DELIVER_CODE_MASK;
     }
xen/arch/x86/hvm/vmx/vmx.c +0 -0
@@ -1671,12 +1672,12 @@ void vmx_inject_extint(int trap, uint8_t source)
                INTR_INFO_VALID_MASK |
                MASK_INSR(X86_EVENTTYPE_EXT_INTR,

INTR_INFO_INTR_TYPE_MASK) |
MASK_INSR(trap, INTR_INFO_VECTOR_MASK),

xen/arch/x86/hvm/vmx/vmx.c +5 -5
-               HVM_DELIVER_NO_ERROR_CODE, source);
+               X86_EVENT_NO_EC, source);
             return;
         }
     }
     __vmx_inject_exception(trap, X86_EVENTTYPE_EXT_INTR,
-                           HVM_DELIVER_NO_ERROR_CODE);
+                           X86_EVENT_NO_EC);
 }
 
 void vmx_inject_nmi(void)
@@ -1691,12 +1692,12 @@ void vmx_inject_nmi(void)
                INTR_INFO_VALID_MASK |
                MASK_INSR(X86_EVENTTYPE_NMI, INTR_INFO_INTR_TYPE_MASK) |
                MASK_INSR(TRAP_nmi, INTR_INFO_VECTOR_MASK),
-               HVM_DELIVER_NO_ERROR_CODE, hvm_intsrc_nmi);
+               X86_EVENT_NO_EC, hvm_intsrc_nmi);
             return;
         }
     }
     __vmx_inject_exception(2, X86_EVENTTYPE_NMI,
-                           HVM_DELIVER_NO_ERROR_CODE);
+                           X86_EVENT_NO_EC);
 }
 
 /*
@@ -2111,7 +2112,7 @@ static bool_t vmx_vcpu_emulate_ve(struct vcpu
 *v)
     vmx_vmcs_exit(v);
 
     hvm_inject_hw_exception(TRAP_virtualisation,
-                            HVM_DELIVER_NO_ERROR_CODE);
+                            X86_EVENT_NO_EC);
 
  out:
     hvm_unmap_guest_frame(veinfo, 0);
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -2387,7 +2388,7 @@ void update_guest_eip(void)
     }
 
     if ( regs->eflags & X86_EFLAGS_TF )
-        hvm_inject_hw_exception(TRAP_debug,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_debug, X86_EVENT_NO_EC);
 }
 

static void vmx_fpu_dirty_intercept(void)

xen/arch/x86/hvm/vmx/vmx.c +2 -2
@@ -3213,7 +3214,7 @@ static void vmx_propagate_intr(unsigned long intr)
         event.error_code = tmp;
     }
     else
-        event.error_code = HVM_DELIVER_NO_ERROR_CODE;
+        event.error_code = X86_EVENT_NO_EC;
 
     if ( event.type >= X86_EVENTTYPE_SW_INTERRUPT )
     {
@@ -3770,7 +3771,7 @@ void vmx_vmexit_handler(struct cpu_user_regs
 *regs)
 
     case EXIT_REASON_VMFUNC:
         if ( vmx_vmfunc_intercept(regs) != X86EMUL_OKAY )
-            hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+            hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         else
             update_guest_eip();
         break;
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -3784,7 +3785,7 @@ void vmx_vmexit_handler(struct cpu_user_regs
 *regs)
          * as far as vmexit.
          */
         WARN_ON(exit_reason == EXIT_REASON_GETSEC);
-        hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+        hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
         break;

case EXIT_REASON_TPR_BELOW_THRESHOLD:

xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -3909,7 +3910,7 @@ void vmx_vmexit_handler(struct cpu_user_regs
 *regs)
             vmx_get_segment_register(v, x86_seg_ss, &ss);
             if ( ss.attr.fields.dpl )
                 hvm_inject_hw_exception(TRAP_invalid_op,
-                                        HVM_DELIVER_NO_ERROR_CODE);
+                                        X86_EVENT_NO_EC);
             else
                 domain_crash(v->domain);
         }
xen/arch/x86/hvm/vmx/vvmx.c +1 -1
index b5837d4..efaf54c 100644
@@ -380,7 +380,7 @@ static int vmx_inst_check_privilege(struct
 cpu_user_regs *regs, int vmxop_check)
 
 invalid_op:
     gdprintk(XENLOG_ERR, "vmx_inst_check_privilege: invalid_op\n");
-    hvm_inject_hw_exception(TRAP_invalid_op,
 HVM_DELIVER_NO_ERROR_CODE);
+    hvm_inject_hw_exception(TRAP_invalid_op, X86_EVENT_NO_EC);
     return X86EMUL_EXCEPTION;

gp_fault:

xen/arch/x86/x86_emulate/x86_emulate.h +2 -1
index b0d8e6f..9df083e 100644
@@ -80,12 +80,13 @@ enum x86_event_type {
     X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
     X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
 };
+#define X86_EVENT_NO_EC (-1)        /* No error code. */
 
 struct x86_event {
     int16_t       vector;
     uint8_t       type;         /* X86_EVENTTYPE_* */
     uint8_t       insn_len;     /* Instruction length */
-    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    int32_t       error_code;   /* X86_EVENT_NO_EC if n/a */
     unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
 };
 
xen/include/asm- renamed from xen/include/asm-x86/hvm/support.h +0 -0

x86/hvm/support.h
index 2984abc..9938450 100644

xen/include/asm-x86/hvm/support.h +0 -3
@@ -25,8 +25,6 @@
 #include <xen/hvm/save.h>
 #include <asm/processor.h>
 
-#define HVM_DELIVER_NO_ERROR_CODE  (~0U)
-
 #ifndef NDEBUG
 #define DBG_LEVEL_0                 (1 << 0)
 #define DBG_LEVEL_1                 (1 << 1)
--
Quoted 1 line

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#18 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Paul Durrant
Quoted 12 lines · Paul Durrant

xen/arch/x86/hvm/emulate.c static int hvmemul_invlpg( · line 1682

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -1
index f24e211..d0c3185 100644
@@ -1679,7 +1679,7 @@ static int hvmemul_invlpg(
          * violations, so squash them.
          */
         hvmemul_ctxt->exn_pending = 0;
-        hvmemul_ctxt->trap = (struct hvm_trap){};
+        hvmemul_ctxt->trap = (struct x86_event){};

Can't say I like that way of initializing but...

Quoted 1 line

Think of it like memcpy/memset, but typesafe. Either way, this is a strict renaming patch, I wouldn't want to change the method of initialisation here anyway.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#62 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper

Introduce a new x86_emul_pagefault() similar to x86_emul_hw_exception(), and use this instead of hvm_inject_page_fault() from emulation codepaths.

Replace one hvm_inject_hw_exception() in the shadow emulation codepaths.

NOTE: this is a functional change for the shadow code, as a #PF previously raised properly with the guest will now cause X86EMUL_UNHANDLABLE. It is my understanding after a discusion with Tim that this is ok, but I haven't done extenstive testing yet.

Do you plan to? I think this is indeed OK, but there may be some edge case, e.g. an instruction that writes to both the current top-level pagetable (which can't be unshadowed) and an unmapped virtual address. That ought to raise #PF in the guest but might now spin retrying?

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by on v1 10/15
#70 lore raw

Re: [PATCH 10/15] x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer

In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper

which is filled with pagefault information should one occur.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#36 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Tim Deegan
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 17:19, <[email protected]> wrote:

Hi,

Quoted 4 lines · Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so

they

Quoted 7 lines

are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending

event

Quoted 3 lines

to fall back into the previous codepath.

No overall functional change.

Quoted 8 lines

AIUI this does have a change in the shadow callers in the case where the emulated instruction would inject an event. Previously we would have failed the emulation, perhaps unshadowed something, and returned to the guest to retry.

Now the emulator records the event in the context struct, updates the register state and returns success, so we'll return on the *next* instruction. I think that's OK, though.

Not exactly - instead of success, X86EMUL_EXCEPTION is being returned, which would suppress register updates. Also I don't think continuing on the next instruction would be okay, as we'd then basically have skipped the one having caused the (not delivered) exception.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by on v1 12/15
#78 lore raw

Re: [PATCH 12/15] x86/hvm: Rename hvm_copy_*_guest_virt() to hvm_copy_*_guest_linear()

In reply to Andrew Cooper
Quoted 4 lines · Andrew Cooper

The functions use linear addresses, not virtual addresses, as no segmentation is used. (Lots of other code in Xen makes this mistake.)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 10/15
#71 lore raw

Re: [PATCH 10/15] x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer

In reply to Andrew Cooper
Quoted 10 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>; Tim (Xen.org) <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>
Subject: [PATCH 10/15] x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer

Quoted 6 lines

which is filled with pagefault information should one occur.

No functional change.

Quoted 351 lines

---
CC: Paul Durrant <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>

---
 xen/arch/x86/hvm/emulate.c        |  8 ++++---
 xen/arch/x86/hvm/hvm.c            | 49 +++++++++++++++++++++++++---------
-----
 xen/arch/x86/hvm/vmx/vvmx.c       |  9 ++++---
 xen/arch/x86/mm/shadow/common.c   |  5 ++--
 xen/include/asm-x86/hvm/support.h | 23 +++++++++++++-----
 5 files changed, 63 insertions(+), 31 deletions(-)
xen/arch/x86/hvm/emulate.c +5 -3
index 3ebb200..e50ee24 100644
@@ -770,6 +770,7 @@ static int __hvmemul_read(
     struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     struct vcpu *curr = current;
+    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
@@ -790,8 +791,8 @@ static int __hvmemul_read(
         pfec |= PFEC_user_mode;
 
     rc = ((access_type == hvm_access_insn_fetch) ?
-          hvm_fetch_from_guest_virt(p_data, addr, bytes, pfec) :
-          hvm_copy_from_guest_virt(p_data, addr, bytes, pfec));
+          hvm_fetch_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo) :
+          hvm_copy_from_guest_virt(p_data, addr, bytes, pfec, &pfinfo));
 
     switch ( rc )
     {
@@ -878,6 +879,7 @@ static int hvmemul_write(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     struct vcpu *curr = current;
+    pagefault_info_t pfinfo;
     unsigned long addr, reps = 1;
     uint32_t pfec = PFEC_page_present | PFEC_write_access;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
@@ -896,7 +898,7 @@ static int hvmemul_write(
          (hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3) )
         pfec |= PFEC_user_mode;
 
-    rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec);
+    rc = hvm_copy_to_guest_virt(addr, p_data, bytes, pfec, &pfinfo);
 
     switch ( rc )
     {
xen/arch/x86/hvm/hvm.c +18 -6
index 804cd88..afba51f 100644
@@ -2859,6 +2859,7 @@ void hvm_task_switch(
     struct desc_struct *optss_desc = NULL, *nptss_desc = NULL, tss_desc;
     bool_t otd_writable, ntd_writable;
     unsigned long eflags;
+    pagefault_info_t pfinfo;
     int exn_raised, rc;
     struct {
         u16 back_link,__blh;
@@ -2925,7 +2926,7 @@ void hvm_task_switch(
     }
 
     rc = hvm_copy_from_guest_virt(
-        &tss, prev_tr.base, sizeof(tss), PFEC_page_present);
+        &tss, prev_tr.base, sizeof(tss), PFEC_page_present, &pfinfo);
     if ( rc != HVMCOPY_okay )
         goto out;
 
@@ -2963,12 +2964,12 @@ void hvm_task_switch(
                                 &tss.eip,
                                 offsetof(typeof(tss), trace) -
                                 offsetof(typeof(tss), eip),
-                                PFEC_page_present);
+                                PFEC_page_present, &pfinfo);
     if ( rc != HVMCOPY_okay )
         goto out;
 
     rc = hvm_copy_from_guest_virt(
-        &tss, tr.base, sizeof(tss), PFEC_page_present);
+        &tss, tr.base, sizeof(tss), PFEC_page_present, &pfinfo);
     /*
      * Note: The HVMCOPY_gfn_shared case could be optimised, if the callee
      * functions knew we want RO access.
@@ -3008,7 +3009,8 @@ void hvm_task_switch(
         tss.back_link = prev_tr.sel;
 
         rc = hvm_copy_to_guest_virt(tr.base + offsetof(typeof(tss), back_link),
-                                    &tss.back_link, sizeof(tss.back_link), 0);
+                                    &tss.back_link, sizeof(tss.back_link), 0,
+                                    &pfinfo);
         if ( rc == HVMCOPY_bad_gva_to_gfn )
             exn_raised = 1;
         else if ( rc != HVMCOPY_okay )
@@ -3045,7 +3047,8 @@ void hvm_task_switch(
                                         16 << segr.attr.fields.db,
                                         &linear_addr) )
         {
-            rc = hvm_copy_to_guest_virt(linear_addr, &errcode, opsz, 0);
+            rc = hvm_copy_to_guest_virt(linear_addr, &errcode, opsz, 0,
+                                        &pfinfo);
             if ( rc == HVMCOPY_bad_gva_to_gfn )
                 exn_raised = 1;
             else if ( rc != HVMCOPY_okay )
@@ -3068,7 +3071,8 @@ void hvm_task_switch(
 #define HVMCOPY_phys       (0u<<2)
 #define HVMCOPY_virt       (1u<<2)
 static enum hvm_copy_result __hvm_copy(
-    void *buf, paddr_t addr, int size, unsigned int flags, uint32_t pfec)
+    void *buf, paddr_t addr, int size, unsigned int flags, uint32_t pfec,
+    pagefault_info_t *pfinfo)
 {
     struct vcpu *curr = current;
     unsigned long gfn;
@@ -3109,7 +3113,15 @@ static enum hvm_copy_result __hvm_copy(
                 if ( pfec & PFEC_page_shared )
                     return HVMCOPY_gfn_shared;
                 if ( flags & HVMCOPY_fault )
+                {
+                    if ( pfinfo )
+                    {
+                        pfinfo->linear = addr;
+                        pfinfo->ec = pfec;
+                    }
+
                     hvm_inject_page_fault(pfec, addr);
+                }
                 return HVMCOPY_bad_gva_to_gfn;
             }
             gpa |= (paddr_t)gfn << PAGE_SHIFT;
@@ -3279,7 +3291,7 @@ enum hvm_copy_result

hvm_copy_to_guest_phys(
{
return __hvm_copy(buf, paddr, size,
HVMCOPY_to_guest | HVMCOPY_fault | HVMCOPY_phys,

xen/arch/x86/hvm/hvm.c +1 -1
-                      0);
+                      0, NULL);
 }
 
 enum hvm_copy_result hvm_copy_from_guest_phys(
@@ -3287,31 +3299,34 @@ enum hvm_copy_result

hvm_copy_from_guest_phys(
{
return __hvm_copy(buf, paddr, size,
HVMCOPY_from_guest | HVMCOPY_fault | HVMCOPY_phys,

xen/arch/x86/hvm/hvm.c +10 -7
-                      0);
+                      0, NULL);
 }
 
 enum hvm_copy_result hvm_copy_to_guest_virt(
-    unsigned long vaddr, void *buf, int size, uint32_t pfec)
+    unsigned long vaddr, void *buf, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo)
 {
     return __hvm_copy(buf, vaddr, size,
                       HVMCOPY_to_guest | HVMCOPY_fault | HVMCOPY_virt,
-                      PFEC_page_present | PFEC_write_access | pfec);
+                      PFEC_page_present | PFEC_write_access | pfec, pfinfo);
 }
 
 enum hvm_copy_result hvm_copy_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec)
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo)
 {
     return __hvm_copy(buf, vaddr, size,
                       HVMCOPY_from_guest | HVMCOPY_fault | HVMCOPY_virt,
-                      PFEC_page_present | pfec);
+                      PFEC_page_present | pfec, pfinfo);
 }
 
 enum hvm_copy_result hvm_fetch_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec)
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo)
 {
     return __hvm_copy(buf, vaddr, size,
                       HVMCOPY_from_guest | HVMCOPY_fault | HVMCOPY_virt,
-                      PFEC_page_present | PFEC_insn_fetch | pfec);
+                      PFEC_page_present | PFEC_insn_fetch | pfec, pfinfo);
 }
 
 enum hvm_copy_result hvm_copy_to_guest_virt_nofault(
@@ -3319,7 +3334,7 @@ enum hvm_copy_result

hvm_copy_to_guest_virt_nofault(
{
return __hvm_copy(buf, vaddr, size,
HVMCOPY_to_guest | HVMCOPY_no_fault | HVMCOPY_virt,

xen/arch/x86/hvm/hvm.c +1 -1
-                      PFEC_page_present | PFEC_write_access | pfec);
+                      PFEC_page_present | PFEC_write_access | pfec, NULL);
 }
 
 enum hvm_copy_result hvm_copy_from_guest_virt_nofault(
@@ -3327,7 +3342,7 @@ enum hvm_copy_result

hvm_copy_from_guest_virt_nofault(
{
return __hvm_copy(buf, vaddr, size,
HVMCOPY_from_guest | HVMCOPY_no_fault | HVMCOPY_virt,

xen/arch/x86/hvm/hvm.c +1 -1
-                      PFEC_page_present | pfec);
+                      PFEC_page_present | pfec, NULL);
 }
 
 enum hvm_copy_result hvm_fetch_from_guest_virt_nofault(
@@ -3335,7 +3350,7 @@ enum hvm_copy_result

hvm_fetch_from_guest_virt_nofault(
{
return __hvm_copy(buf, vaddr, size,
HVMCOPY_from_guest | HVMCOPY_no_fault | HVMCOPY_virt,

xen/arch/x86/hvm/hvm.c +1 -1
-                      PFEC_page_present | PFEC_insn_fetch | pfec);
+                      PFEC_page_present | PFEC_insn_fetch | pfec, NULL);
 }
 
 unsigned long copy_to_user_hvm(void *to, const void *from, unsigned int

len)

xen/arch/x86/hvm/vmx/vvmx.c +2 -1
index bcc4a97..7342d12 100644
@@ -396,6 +396,7 @@ static int decode_vmx_inst(struct cpu_user_regs
 *regs,
     struct vcpu *v = current;
     union vmx_inst_info info;
     struct segment_register seg;
+    pagefault_info_t pfinfo;
     unsigned long base, index, seg_base, disp, offset;
     int scale, size;
@@ -451,7 +452,7 @@ static int decode_vmx_inst(struct cpu_user_regs
 *regs,
             goto gp_fault;
 
         if ( poperandS != NULL &&
-             hvm_copy_from_guest_virt(poperandS, base, size, 0)
+             hvm_copy_from_guest_virt(poperandS, base, size, 0, &pfinfo)
                   != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         decode->mem = base;
xen/arch/x86/hvm/vmx/vvmx.c +2 -1
@@ -1611,6 +1612,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs
 *regs)
     struct vcpu *v = current;
     struct vmx_inst_decoded decode;
     struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
+    pagefault_info_t pfinfo;
     unsigned long gpa = 0;
     int rc;
@@ -1620,7 +1622,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs
 *regs)
 
     gpa = nvcpu->nv_vvmcxaddr;
 
-    rc = hvm_copy_to_guest_virt(decode.mem, &gpa, decode.len, 0);
+    rc = hvm_copy_to_guest_virt(decode.mem, &gpa, decode.len, 0,
 &pfinfo);
     if ( rc != HVMCOPY_okay )
         return X86EMUL_EXCEPTION;
xen/arch/x86/hvm/vmx/vvmx.c +2 -1
@@ -1679,6 +1681,7 @@ int nvmx_handle_vmread(struct cpu_user_regs
 *regs)
 {
     struct vcpu *v = current;
     struct vmx_inst_decoded decode;
+    pagefault_info_t pfinfo;
     u64 value = 0;
     int rc;
@@ -1690,7 +1693,7 @@ int nvmx_handle_vmread(struct cpu_user_regs
 *regs)
 
     switch ( decode.type ) {
     case VMX_INST_MEMREG_TYPE_MEMORY:
-        rc = hvm_copy_to_guest_virt(decode.mem, &value, decode.len, 0);
+        rc = hvm_copy_to_guest_virt(decode.mem, &value, decode.len, 0,
 &pfinfo);
         if ( rc != HVMCOPY_okay )
             return X86EMUL_EXCEPTION;
         break;
xen/arch/x86/mm/shadow/common.c +3 -2
index c8b61b9..d28eae1 100644
@@ -179,6 +179,7 @@ hvm_read(enum x86_segment seg,
          enum hvm_access_type access_type,
          struct sh_emulate_ctxt *sh_ctxt)
 {
+    pagefault_info_t pfinfo;
     unsigned long addr;
     int rc;
 
@@ -188,9 +189,9 @@ hvm_read(enum x86_segment seg,
         return rc;
 
     if ( access_type == hvm_access_insn_fetch )
-        rc = hvm_fetch_from_guest_virt(p_data, addr, bytes, 0);
+        rc = hvm_fetch_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
     else
-        rc = hvm_copy_from_guest_virt(p_data, addr, bytes, 0);
+        rc = hvm_copy_from_guest_virt(p_data, addr, bytes, 0, &pfinfo);
 
     switch ( rc )
     {
xen/include/asm- renamed from xen/include/asm-x86/hvm/support.h +0 -0

x86/hvm/support.h
index 9938450..4aa5a36 100644

xen/include/asm-x86/hvm/support.h +0 -0
@@ -83,16 +83,27 @@ enum hvm_copy_result

hvm_copy_from_guest_phys(
* HVMCOPY_bad_gfn_to_mfn: Some guest physical address did not map to
* ordinary machine memory.
* HVMCOPY_bad_gva_to_gfn: Some guest virtual address did not have a
valid

- *                          mapping to a guest physical address. In this case
- *                          a page fault exception is automatically queued
- *                          for injection into the current HVM VCPU.
+ *                          mapping to a guest physical address.  The
+ *                          pagefault_info_t structure will be filled in if
+ *                          provided, and a page fault exception is
+ *                          automatically queued for injection into the
+ *                          current HVM VCPU.

*/

xen/include/asm-x86/hvm/support.h +12 -4
+typedef struct pagefault_info
+{
+    unsigned long linear;
+    int ec;
+} pagefault_info_t;
+
 enum hvm_copy_result hvm_copy_to_guest_virt(
-    unsigned long vaddr, void *buf, int size, uint32_t pfec);
+    unsigned long vaddr, void *buf, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 enum hvm_copy_result hvm_copy_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 enum hvm_copy_result hvm_fetch_from_guest_virt(
-    void *buf, unsigned long vaddr, int size, uint32_t pfec);
+    void *buf, unsigned long vaddr, int size, uint32_t pfec,
+    pagefault_info_t *pfinfo);
 
 /*
  * As above (copy to/from a guest virtual address), but no fault is generated
--
Quoted 1 line

2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#75 lore raw

Re: [PATCH 11/15] x86/hvm: Reimplement hvm_copy_*_nofault() in terms of no pagefault_info

In reply to Tim Deegan
Quoted 6 lines · Tim Deegan
Quoted 3 lines · Andrew Cooper

No functional change.

Shouldn't this also update the comments to describe the new semantics of hvm_copy_*()?

I couldn't find an easy way of phrasing it which didn't get deleted by the subsequent patch.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#38 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Tim Deegan
Quoted 16 lines · Tim Deegan

Hi,

Quoted 13 lines · Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so they are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending event to fall back into the previous codepath.

Quoted 8 lines
Quoted 1 line

No overall functional change.

AIUI this does have a change in the shadow callers in the case where the emulated instruction would inject an event. Previously we would have failed the emulation, perhaps unshadowed something, and returned to the guest to retry.
Now the emulator records the event in the context struct, updates the register state and returns success, so we'll return on the *next* instruction. I think that's OK, though.

We are still passing X86EMUL_EXCEPTION back into the emulator, so nothing changes immediately from that point of view. It will still "goto done" and skip the writeback phase.

Quoted 3 lines

Also, handle_mmio() and other callers of the emulator check for that pending event and pass it to the hardware but you haven't added anything in the shadow code to do that. Does the event get dropped?

Yes. That was the intended purpose of these hunks:

xen/arch/x86/mm/shadow/multi.c +2 -2
index d70b1c6..84cb6b6 100644
@@ -3378,7 +3378,7 @@ static int sh_page_fault(struct vcpu *v,
      * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
      * then it must be 'failable': we cannot require the unshadow to succeed.
      */
-    if ( r == X86EMUL_UNHANDLEABLE )
+    if ( r == X86EMUL_UNHANDLEABLE || emul_ctxt.ctxt.event_pending )
     {
         perfc_incr(shadow_fault_emulate_failed);
 #if SHADOW_OPTIMIZATIONS & SHOPT_FAST_EMULATION
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )
             {
                 emulation_count++;
                 if ( v->arch.paging.last_write_was_pt )

To take the failure path any time an event is seen pending.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#83 lore raw

Re: [PATCH 13/15] x86/hvm: Avoid __hvm_copy() raising #PF behind the emulators back

In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper

Drop the call to hvm_inject_page_fault() in __hvm_copy(), and require callers to inject the pagefault themselves.

No functional change.

index afacd5f..88d4642 100644

xen/arch/x86/mm/shadow/common.c hvm_read(enum x86_segment seg, · line 204

Quoted 7 lines
xen/arch/x86/mm/shadow/common.c +1 -0
@@ -198,6 +198,7 @@ hvm_read(enum x86_segment seg,
     case HVMCOPY_okay:
         return X86EMUL_OKAY;
     case HVMCOPY_bad_gva_to_gfn:
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
     case HVMCOPY_unhandleable:

Should this also be converted to pass the injection to the emulator rather than injecting it directly?

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#76 lore raw

Re: [PATCH 11/15] x86/hvm: Reimplement hvm_copy_*_nofault() in terms of no pagefault_info

In reply to Tim Deegan
Quoted 7 lines · Tim Deegan
Quoted 3 lines · Andrew Cooper

No functional change.

Shouldn't this also update the comments to describe the new semantics of hvm_copy_*()?

Right, now I see that this all goes away later in the series. So, Ack.

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/15
#4 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Andrew Cooper
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

No functional change.

with one further cosmetic request:

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +4 -0
@@ -2006,6 +1956,57 @@ void hvm_emulate_prepare(
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
 }
 
+void hvm_emulate_init_per_insn(
+    struct hvm_emulate_ctxt *hvmemul_ctxt,
+    const unsigned char *insn_buf,
+    unsigned int insn_bytes)

xen/arch/x86/hvm/emulate.c void hvm_emulate_prepare( · line 1972

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+{
+    struct vcpu *curr = current;
+    unsigned int pfec = PFEC_page_present;
+    unsigned long addr;
+
+    if ( hvm_long_mode_enabled(curr) &&
+         hvmemul_ctxt->seg_reg[x86_seg_cs].attr.fields.l )
+    {
+        hvmemul_ctxt->ctxt.addr_size = hvmemul_ctxt->ctxt.sp_size = 64;
+    }

Please consider dropping the stray braces here.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#63 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

In reply to Tim Deegan
Quoted 11 lines · Tim Deegan
Quoted 10 lines · Andrew Cooper

Introduce a new x86_emul_pagefault() similar to x86_emul_hw_exception(), and use this instead of hvm_inject_page_fault() from emulation codepaths.

Replace one hvm_inject_hw_exception() in the shadow emulation codepaths.

NOTE: this is a functional change for the shadow code, as a #PF previously raised properly with the guest will now cause X86EMUL_UNHANDLABLE. It is my understanding after a discusion with Tim that this is ok, but I haven't done extenstive testing yet.

Quoted 4 lines

Do you plan to? I think this is indeed OK, but there may be some edge case, e.g. an instruction that writes to both the current top-level pagetable (which can't be unshadowed) and an unmapped virtual address. That ought to raise #PF in the guest but might now spin retrying?

That is a devious corner case. I take it you have been there before?

The more I think about these changes, the more I think that the shadow code would be better by selectively looking a pending event, injecting pagefaults, but rejecting and retrying if any other event shows up.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Boris Ostrovsky
Quoted 18 lines · Boris Ostrovsky
Quoted 9 lines · Andrew Cooper

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

No functional change.

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

Quoted 4 lines

(although I am having trouble parsing the first bullet in the commit message)

Ah "to immediately after". I will fix up the text.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/15
#5 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

No functional change.

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Wei Liu <[email protected]>

(although I am having trouble parsing the first bullet in the commit message)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 14/15
#86 lore raw

Re: [PATCH 14/15] x86/hvm: Prepare to allow use of system segments for memory references

In reply to Andrew Cooper
Quoted 26 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 23 November 2016 15:39
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 14/15] x86/hvm: Prepare to allow use of system segments for memory references

All system segments (GDT/IDT/LDT and TR) describe a linear address and limit,
and act similarly to user segments. However all current uses of these tables in the emulator opencode the address calculations and limit checks. In particular, no care is taken for access which wrap around the 4GB or non-canonical boundaries.

Alter hvm_virtual_to_linear_addr() to cope with performing segmentation checks
on system segments. This involves restricting access checks in the 32bit case to user segments only, and adding presence/limit checks in the 64bit case.

When suffering a segmentation fault for a system segments, return X86EMUL_EXCEPTION but leave the fault injection to the caller. The fault type
depends on the higher level action being performed.

Quoted 5 lines

---
CC: Paul Durrant <[email protected]>

Quoted 178 lines
---
 xen/arch/x86/hvm/emulate.c             | 14 ++++++++----
 xen/arch/x86/hvm/hvm.c                 | 40 ++++++++++++++++++++++-----------
(quoted diff) +0 -1
-
 xen/arch/x86/mm/shadow/common.c        | 12 +++++++---
 xen/arch/x86/x86_emulate/x86_emulate.h | 26 ++++++++++++++--------
 4 files changed, 62 insertions(+), 30 deletions(-)
xen/arch/x86/hvm/emulate.c +10 -4
index c248eca..3a7d1f3 100644
@@ -567,10 +567,16 @@ static int hvmemul_virtual_to_linear(
     if ( *reps != 1 )
         return X86EMUL_UNHANDLEABLE;
 
-    /* This is a singleton operation: fail it with an exception. */
-    x86_emul_hw_exception((seg == x86_seg_ss)
-                          ? TRAP_stack_error
-                          : TRAP_gp_fault, 0, &hvmemul_ctxt->ctxt);
+    /*
+     * Leave exception injection to the caller for non-user segments: We
+     * neither know the exact error code to be used, nor can we easily
+     * determine the kind of exception (#GP or #TS) in that case.
+     */
+    if ( is_x86_user_segment(seg) )
+        x86_emul_hw_exception((seg == x86_seg_ss)
+                              ? TRAP_stack_error
+                              : TRAP_gp_fault, 0, &hvmemul_ctxt->ctxt);
+
     return X86EMUL_EXCEPTION;
 }
 
xen/arch/x86/hvm/hvm.c +16 -13
index e1f2c9e..2bcef1f 100644
@@ -2497,24 +2497,28 @@ bool_t hvm_virtual_to_linear_addr(
         if ( !reg->attr.fields.p )
             goto out;
 
-        switch ( access_type )
+        /* Read/write restrictions only exist for user segments. */
+        if ( reg->attr.fields.s )
         {
-        case hvm_access_read:
-            if ( (reg->attr.fields.type & 0xa) == 0x8 )
-                goto out; /* execute-only code segment */
-            break;
-        case hvm_access_write:
-            if ( (reg->attr.fields.type & 0xa) != 0x2 )
-                goto out; /* not a writable data segment */
-            break;
-        default:
-            break;
+            switch ( access_type )
+            {
+            case hvm_access_read:
+                if ( (reg->attr.fields.type & 0xa) == 0x8 )
+                    goto out; /* execute-only code segment */
+                break;
+            case hvm_access_write:
+                if ( (reg->attr.fields.type & 0xa) != 0x2 )
+                    goto out; /* not a writable data segment */
+                break;
+            default:
+                break;
+            }
         }
 
         last_byte = (uint32_t)offset + bytes - !!bytes;
 
         /* Is this a grows-down data segment? Special limit check if so. */
-        if ( (reg->attr.fields.type & 0xc) == 0x4 )
+        if ( reg->attr.fields.s && (reg->attr.fields.type & 0xc) == 0x4 )
         {
             /* Is upper limit 0xFFFF or 0xFFFFFFFF? */
             if ( !reg->attr.fields.db )
@@ -2530,10 +2534,18 @@ bool_t hvm_virtual_to_linear_addr(
     else
     {
         /*
-         * LONG MODE: FS and GS add segment base. Addresses must be

canonical.

xen/arch/x86/hvm/hvm.c +10 -1
+         * User segments are always treated as present.  System segment may
+         * not be, and also incur limit checks.
          */
+        if ( is_x86_system_segment(seg) &&
+             (!reg->attr.fields.p || (offset + bytes - !!bytes) > reg->limit) )
+            goto out;
 
-        if ( (seg == x86_seg_fs) || (seg == x86_seg_gs) )
+        /*
+         * LONG MODE: FS, GS and system segments: add segment base. All
+         * addresses must be canonical.
+         */
+        if ( seg >= x86_seg_fs )
             addr += reg->base;
 
         last_byte = addr + bytes - !!bytes;
xen/arch/x86/mm/shadow/common.c +9 -3
index 88d4642..954c157 100644
@@ -162,9 +162,15 @@ static int hvm_translate_linear_addr(
 
     if ( !okay )
     {
-        x86_emul_hw_exception(
-            (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,
-            0, &sh_ctxt->ctxt);
+        /*
+         * Leave exception injection to the caller for non-user segments: We
+         * neither know the exact error code to be used, nor can we easily
+         * determine the kind of exception (#GP or #TS) in that case.
+         */
+        if ( is_x86_user_segment(seg) )
+            x86_emul_hw_exception(
+                (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,
+                0, &sh_ctxt->ctxt);
         return X86EMUL_EXCEPTION;
     }
 
xen/arch/x86/x86_emulate/x86_emulate.h +2 -1
index cc26e9d..4d18623 100644
@@ -27,7 +27,11 @@
 
 struct x86_emulate_ctxt;
 
-/* Comprehensive enumeration of x86 segment registers. */
+/*
+ * Comprehensive enumeration of x86 segment registers.  Various bits of

code
+ * rely on this order (general purpose before system, tr at the beginning of
+ * system).
+ */

 enum x86_segment {
     /* General purpose.  Matches the SReg3 encoding in opcode/ModRM
bytes. */
     x86_seg_es,
xen/arch/x86/x86_emulate/x86_emulate.h +12 -8
@@ -36,21 +40,25 @@ enum x86_segment {
     x86_seg_ds,
     x86_seg_fs,
     x86_seg_gs,
-    /* System. */
+    /* System: Valid to use for implicit table references. */
     x86_seg_tr,
     x86_seg_ldtr,
     x86_seg_gdtr,
     x86_seg_idtr,
-    /*
-     * Dummy: used to emulate direct processor accesses to management
-     * structures (TSS, GDT, LDT, IDT, etc.) which use linear addressing
-     * (no segment component) and bypass usual segment- and page-level
-     * protection checks.
-     */
+    /* No Segment: For accesses which are already linear. */
     x86_seg_none
 };
 
-#define is_x86_user_segment(seg) ((unsigned)(seg) <= x86_seg_gs)
+static inline bool is_x86_user_segment(enum x86_segment seg)
+{
+    unsigned int idx = seg;
+
+    return idx <= x86_seg_gs;
+}
+static inline bool is_x86_system_segment(enum x86_segment seg)
+{
+    return seg >= x86_seg_tr && seg < x86_seg_none;
+}
 
 /* Classification of the types of software generated interrupts/exceptions.
 */
Quoted 3 lines

enum x86_swint_type {
--
2.1.4

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#37 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Jan Beulich
Quoted 26 lines · Jan Beulich
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 17:19, <[email protected]> wrote:

Hi,

Quoted 4 lines · Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so

they

Quoted 7 lines

are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending

event

Quoted 3 lines

to fall back into the previous codepath.

No overall functional change.

AIUI this does have a change in the shadow callers in the case where the emulated instruction would inject an event. Previously we would have failed the emulation, perhaps unshadowed something, and returned to the guest to retry.

Quoted 6 lines
Quoted 3 lines

Now the emulator records the event in the context struct, updates the register state and returns success, so we'll return on the *next* instruction. I think that's OK, though.

Not exactly - instead of success, X86EMUL_EXCEPTION is being returned, which would suppress register updates.

Oh right. In that case AFAICS neither invocation of x86_emulate() in shadow/multi.c needs any adjustment.

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#64 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

In reply to Andrew Cooper
Quoted 12 lines · Andrew Cooper
Quoted 11 lines · Tim Deegan
Quoted 10 lines · Andrew Cooper

Introduce a new x86_emul_pagefault() similar to x86_emul_hw_exception(), and use this instead of hvm_inject_page_fault() from emulation codepaths.

Replace one hvm_inject_hw_exception() in the shadow emulation codepaths.

NOTE: this is a functional change for the shadow code, as a #PF previously raised properly with the guest will now cause X86EMUL_UNHANDLABLE. It is my understanding after a discusion with Tim that this is ok, but I haven't done extenstive testing yet.

Quoted 6 lines
Quoted 4 lines

Do you plan to? I think this is indeed OK, but there may be some edge case, e.g. an instruction that writes to both the current top-level pagetable (which can't be unshadowed) and an unmapped virtual address. That ought to raise #PF in the guest but might now spin retrying?

That is a devious corner case. I take it you have been there before?

In similar situations, yes. :)

Quoted 3 lines

The more I think about these changes, the more I think that the shadow code would be better by selectively looking a pending event, injecting pagefaults, but rejecting and retrying if any other event shows up.

That sounds like a good idea, and seems like the smallest deviation from the current behaviour. It might also be OK to inject any event that the emulator raises. That's a bigger change but maybe a more coherent end result?

Cheers,

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#65 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

Quoted 20 lines · Tim Deegan
Quoted 12 lines · Andrew Cooper
Quoted 11 lines · Tim Deegan
Quoted 10 lines · Andrew Cooper

Introduce a new x86_emul_pagefault() similar to x86_emul_hw_exception(), and use this instead of hvm_inject_page_fault() from emulation codepaths.

Replace one hvm_inject_hw_exception() in the shadow emulation codepaths.

NOTE: this is a functional change for the shadow code, as a #PF previously raised properly with the guest will now cause X86EMUL_UNHANDLABLE. It is my understanding after a discusion with Tim that this is ok, but I haven't done extenstive testing yet.

Quoted 5 lines
Quoted 4 lines

Do you plan to? I think this is indeed OK, but there may be some edge case, e.g. an instruction that writes to both the current top-level pagetable (which can't be unshadowed) and an unmapped virtual address. That ought to raise #PF in the guest but might now spin retrying?

That is a devious corner case. I take it you have been there before?

In similar situations, yes. :)

Quoted 7 lines
Quoted 3 lines

The more I think about these changes, the more I think that the shadow code would be better by selectively looking a pending event, injecting pagefaults, but rejecting and retrying if any other event shows up.

That sounds like a good idea, and seems like the smallest deviation from the current behaviour. It might also be OK to inject any event that the emulator raises. That's a bigger change but maybe a more coherent end result?

Well - now this isn't hidden in a security fix, I am less averse to functional changes.

My only concern is that the previous lack of the ->inject_hw_exception() hook cut off large chunks of functionality from the shadow and PV PT emulation paths, and I am not sure opening this up in general is a good idea.

Longterm the plan is to fully split the decode and emulate calls even for external callers, at which point the the pagetable code could check that the instruction is write which matches %cr2 before proceeding with emulation. Even then however, I am not sure it would be a good idea to follow anything other than a pagefault which surfaces from emulation.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/15
#19 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper

The x86 emulator needs to gain an understanding of interrupts and exceptions generated by its actions. The naming choice is to match both the Intel and AMD terms, and to avoid 'trap' specifically as it has an architectural meaning different to its current usage.

While making this change, make other changes for consistency

Quoted 9 lines

* Rename *_trap() infrastructure to *_event()
* Rename trapnr/trap parameters to vector
* Convert hvm_inject_hw_exception() and hvm_inject_page_fault() to being static inlines, as they are only thin wrappers around hvm_inject_event()

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 4/15
#26 lore raw

Re: [PATCH 04/15] x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

and move it to live with the other x86_event infrastructure in x86_emulate.h. Switch it and x86_event.error_code to being signed, matching the rest of the code.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#84 lore raw

Re: [PATCH 13/15] x86/hvm: Avoid __hvm_copy() raising #PF behind the emulators back

In reply to Tim Deegan
Quoted 6 lines · Tim Deegan
Quoted 5 lines · Andrew Cooper

Drop the call to hvm_inject_page_fault() in __hvm_copy(), and require callers to inject the pagefault themselves.

No functional change.

Quoted 14 lines

xen/arch/x86/mm/shadow/common.c hvm_read(enum x86_segment seg, · line 204

Quoted 12 lines

index afacd5f..88d4642 100644

xen/arch/x86/mm/shadow/common.c +1 -0
@@ -198,6 +198,7 @@ hvm_read(enum x86_segment seg,
     case HVMCOPY_okay:
         return X86EMUL_OKAY;
     case HVMCOPY_bad_gva_to_gfn:
+        hvm_inject_page_fault(pfinfo.ec, pfinfo.linear);
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
     case HVMCOPY_unhandleable:

Should this also be converted to pass the injection to the emulator rather than injecting it directly?

Yes.

emulate_gva_to_mfn() also needs similar treatment, but will include some PV pagetable fun as well.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 6/15
#39 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so they are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

Quoted 9 lines

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending event to fall back into the previous codepath.

No overall functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#53 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Andrew Cooper
Quoted 44 lines · Andrew Cooper

Intel VT-x and AMD SVM provide access to the full segment descriptor cache via fields in the VMCB/VMCS. However, the bits which are actually checked by hardware and preserved across vmentry/exit are inconsistent, and the vendor accessor functions perform inconsistent modification to the raw values.

Convert {svm,vmx}_{get,set}_segment_register() into raw accessors, and alter hvm_{get,set}_segment_register() to cook the values consistently. This allows the common emulation code to better rely on finding architecturally-expected values.

This does cause some functional changes because of the modifications being applied uniformly. A side effect of this fixes latent bugs where vmx_set_segment_register() didn't correctly fix up .G for segments, and inconsistent fixing up of the GDTR/IDTR limits.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/hvm.c        | 151 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/svm.c    |  20 +-----
 xen/arch/x86/hvm/vmx/vmx.c    |   6 +-
 xen/include/asm-x86/desc.h    |   6 ++
 xen/include/asm-x86/hvm/hvm.h |  17 ++---
 5 files changed, 164 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/hvm.c +6 -0
index ef83100..804cd88 100644
@@ -6051,6 +6051,157 @@ void hvm_domain_soft_reset(struct domain *d)
 }
 
 /*
+ * Segment caches in VMCB/VMCS are inconsistent about which bits are checked,
+ * important, and preserved across vmentry/exit.  Cook the values to make them
+ * closer to what is architecturally expected from entries in the segment
+ * cache.
+ */
+void hvm_get_segment_register(struct vcpu *v, enum x86_segment seg,

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6069

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+                              struct segment_register *reg)
+{
+    hvm_funcs.get_segment_register(v, seg, reg);
+
+    switch ( seg )
+    {
+    case x86_seg_ss:
+        /* SVM may retain %ss.DB when %ss is loaded with a NULL selector. */
+        if ( !reg->attr.fields.p )
+            reg->attr.fields.db = 0;

Removed SVM code relied on type being zero to indicate a NULL segment. Looking at P bit is the correct way and I think it's worth mentioning this in the commit message.

Quoted 16 lines
xen/arch/x86/hvm/hvm.c +16 -0
+}
+
+void hvm_set_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg)
+{
+    /* Set G to match the limit field.  VT-x cares, while SVM doesn't. */
+    if ( reg->attr.fields.p )
+        reg->attr.fields.g = !!(reg->limit >> 20);
+
+    switch ( seg )
+    {
+    case x86_seg_cs:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(reg->attr.fields.s);                  /* User segment. */
+        ASSERT((reg->base >> 32) == 0);              /* Upper bits clear. */
+        break;

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6151

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+
+    case x86_seg_ss:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(reg->attr.fields.s);              /* User segment. */
+            ASSERT(!(reg->attr.fields.type & 0x8));  /* Data segment. */
+            ASSERT(reg->attr.fields.type & 0x2);     /* Writeable. */
+            ASSERT((reg->base >> 32) == 0);          /* Upper bits clear. */
+        }
+        break;

SVM requires attributes of any NULL segment to be zero. I don't know about Intel but if it's the same then should we ASSERT this as well?

-boris

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#54 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

Quoted 50 lines · Boris Ostrovsky
Quoted 49 lines · Andrew Cooper

Intel VT-x and AMD SVM provide access to the full segment descriptor cache via fields in the VMCB/VMCS. However, the bits which are actually checked by hardware and preserved across vmentry/exit are inconsistent, and the vendor accessor functions perform inconsistent modification to the raw values.

Convert {svm,vmx}_{get,set}_segment_register() into raw accessors, and alter hvm_{get,set}_segment_register() to cook the values consistently. This allows the common emulation code to better rely on finding architecturally-expected values.

This does cause some functional changes because of the modifications being applied uniformly. A side effect of this fixes latent bugs where vmx_set_segment_register() didn't correctly fix up .G for segments, and inconsistent fixing up of the GDTR/IDTR limits.

---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>

---
 xen/arch/x86/hvm/hvm.c        | 151 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/svm/svm.c    |  20 +-----
 xen/arch/x86/hvm/vmx/vmx.c    |   6 +-
 xen/include/asm-x86/desc.h    |   6 ++
 xen/include/asm-x86/hvm/hvm.h |  17 ++---
 5 files changed, 164 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/hvm.c +11 -0
index ef83100..804cd88 100644
@@ -6051,6 +6051,157 @@ void hvm_domain_soft_reset(struct domain *d)
 }
 
 /*
+ * Segment caches in VMCB/VMCS are inconsistent about which bits are checked,
+ * important, and preserved across vmentry/exit.  Cook the values to make them
+ * closer to what is architecturally expected from entries in the segment
+ * cache.
+ */
+void hvm_get_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg)
+{
+    hvm_funcs.get_segment_register(v, seg, reg);
+
+    switch ( seg )
Quoted 8 lines

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6069

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5 -0
+    {
+    case x86_seg_ss:
+        /* SVM may retain %ss.DB when %ss is loaded with a NULL selector. */
+        if ( !reg->attr.fields.p )
+            reg->attr.fields.db = 0;

Removed SVM code relied on type being zero to indicate a NULL segment. Looking at P bit is the correct way and I think it's worth mentioning this in the commit message.

Oh yes. As far as I can tell, that was simply broken before.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/hvm/hvm.c +19 -0
+}
+
+void hvm_set_segment_register(struct vcpu *v, enum x86_segment seg,
+                              struct segment_register *reg)
+{
+    /* Set G to match the limit field.  VT-x cares, while SVM doesn't. */
+    if ( reg->attr.fields.p )
+        reg->attr.fields.g = !!(reg->limit >> 20);
+
+    switch ( seg )
+    {
+    case x86_seg_cs:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(reg->attr.fields.s);                  /* User segment. */
+        ASSERT((reg->base >> 32) == 0);              /* Upper bits clear. */
+        break;
+
+    case x86_seg_ss:
+        if ( reg->attr.fields.p )
Quoted 8 lines

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6151

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7 -0
+        {
+            ASSERT(reg->attr.fields.s);              /* User segment. */
+            ASSERT(!(reg->attr.fields.type & 0x8));  /* Data segment. */
+            ASSERT(reg->attr.fields.type & 0x2);     /* Writeable. */
+            ASSERT((reg->base >> 32) == 0);          /* Upper bits clear. */
+        }
+        break;

SVM requires attributes of any NULL segment to be zero.

Where is this claim made? Vol2 recommends that the VMM clear all attributes, but the wording of the previous paragraph suggests that the
attributes would be ignored in this case. The %ss bug, and some
experimentation on my behalf also indicate that they are ignored.

Quoted 1 line

I don't know about Intel but if it's the same then should we ASSERT this as well?

On Intel if unusable is set, all other bits are ignored.

However, the behaviour of both Intel and AMD is to occasionally set upper attribute bits. At some point I intend to make emulated segment loading match d->arch.vendor's behaviour, at which point such an ASSERT() would definitely trip.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#55 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

Quoted 6 lines · Andrew Cooper
Quoted 2 lines

SVM requires attributes of any NULL segment to be zero.

Where is this claim made? Vol2 recommends that the VMM clear all attributes, but the wording of the previous paragraph suggests that the
attributes would be ignored in this case. The %ss bug, and some
experimentation on my behalf also indicate that they are ignored.

15.5.1 Basic Operation, Segment State in the VMCB:

The VMM should follow these rules when storing segment attributes into the VMCB
* For NULL segments, set all attribute bits to zero; otherwise, write the concatenation of bits 55:52 and 47:40 from the original 64-bit (in-memory) segment descriptors.

I guess the preceding text is indeed unclear as to whether this is somehow enforced (in which case I am not sure I see the point of having this rule).

-boris

Quoted 10 lines
Quoted 2 lines

I don't know about Intel but if it's the same then should we ASSERT this as well?

On Intel if unusable is set, all other bits are ignored.

However, the behaviour of both Intel and AMD is to occasionally set upper attribute bits. At some point I intend to make emulated segment loading match d->arch.vendor's behaviour, at which point such an ASSERT() would definitely trip.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#56 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

Quoted 14 lines · Boris Ostrovsky
Quoted 5 lines · Andrew Cooper
Quoted 1 line

SVM requires attributes of any NULL segment to be zero.

Where is this claim made? Vol2 recommends that the VMM clear all attributes, but the wording of the previous paragraph suggests that the
attributes would be ignored in this case. The %ss bug, and some
experimentation on my behalf also indicate that they are ignored.

15.5.1 Basic Operation, Segment State in the VMCB:

The VMM should follow these rules when storing segment attributes into the VMCB
* For NULL segments, set all attribute bits to zero; otherwise, write the concatenation of bits 55:52 and 47:40 from the original 64-bit (in-memory) segment descriptors.

Quoted 3 lines

I guess the preceding text is indeed unclear as to whether this is somehow enforced (in which case I am not sure I see the point of having this rule).

The quality if information in this regard is very poor. Both Intel and AMD expose the segment cache to hypervisors, without actually documenting the behaviour and expectations. I spent several weeks during the development of XSA-191 trying to divide what actually happens, especially in terms of what a guest can do to the segment cache without suffering a VMEXIT.

One point I deliberately didn't highlight at the time in c/s 12bc22f791 is that such an emulated instruction, on AMD, discards the non-canonical part of the base, and happily runs with a truncated address implicitly sign extended. If you happen to look back through my submissions, you will find quite a few patches drip-feeding segmentation fixes and improvements. c/s ed00f17616 was another curious issue to stumble across.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 1/15
#7 lore raw

Re: [PATCH 01/15] x86/hvm: Rename hvm_emulate_init() and hvm_emulate_prepare() for clarity

In reply to Andrew Cooper
Quoted 9 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

* Move hvm_emulate_init() to immediately hvm_emulate_prepare(), as they are very closely related.
* Rename hvm_emulate_prepare() to hvm_emulate_init_once() and hvm_emulate_init() to hvm_emulate_init_per_insn() to make it clearer how to and when to use them.

Quoted 3 lines

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/15
#20 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper
Quoted 10 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

The x86 emulator needs to gain an understanding of interrupts and exceptions generated by its actions. The naming choice is to match both the Intel and AMD terms, and to avoid 'trap' specifically as it has an architectural meaning different to its current usage.

While making this change, make other changes for consistency

Quoted 8 lines

* Rename *_trap() infrastructure to *_event()
* Rename trapnr/trap parameters to vector
* Convert hvm_inject_hw_exception() and hvm_inject_page_fault() to being static inlines, as they are only thin wrappers around hvm_inject_event()

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 4/15
#27 lore raw

Re: [PATCH 04/15] x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

In reply to Andrew Cooper
Quoted 8 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

and move it to live with the other x86_event infrastructure in x86_emulate.h. Switch it and x86_event.error_code to being signed, matching the rest of the code.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 6/15
#40 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 11 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

The emulator needs to gain an understanding of interrupts and exceptions generated by its actions.

Move hvm_emulate_ctxt.{exn_pending,trap} into struct x86_emulate_ctxt so they are visible to the emulator. This removes the need for the inject_{hw,sw}_interrupt() hooks, which are dropped and replaced with x86_emul_{hw_exception,software_event}() instead.

Quoted 8 lines

The shadow pagetable and PV uses of x86_emulate() previously failed with X86EMUL_UNHANDLEABLE due to the lack of inject_*() hooks, but this behaviour has subtly changed. Adjust the return value checking to cause a pending event to fall back into the previous codepath.

No overall functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by on v1 7/15
#51 lore raw

Re: [PATCH 07/15] x86/vmx: Use hvm_{get, set}_segment_register() rather than vmx_{get, set}_segment_register()

In reply to Andrew Cooper
Quoted 9 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

No functional change at this point, but this is a prerequisite for forthcoming functional changes.

Make vmx_get_segment_register() private to vmx.c like all the other Vendor get/set functions.

Quoted 7 lines

---
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 8/15
#57 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Andrew Cooper
Quoted 13 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

Intel VT-x and AMD SVM provide access to the full segment descriptor cache via fields in the VMCB/VMCS. However, the bits which are actually checked by hardware and preserved across vmentry/exit are inconsistent, and the vendor accessor functions perform inconsistent modification to the raw values.

Convert {svm,vmx}_{get,set}_segment_register() into raw accessors, and alter hvm_{get,set}_segment_register() to cook the values consistently. This allows the common emulation code to better rely on finding architecturally-expected values.

Quoted 6 lines

This does cause some functional changes because of the modifications being applied uniformly. A side effect of this fixes latent bugs where vmx_set_segment_register() didn't correctly fix up .G for segments, and inconsistent fixing up of the GDTR/IDTR limits.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 10/15
#72 lore raw

Re: [PATCH 10/15] x86/hvm: Extend the hvm_copy_*() API with a pagefault_info pointer

In reply to Andrew Cooper
Quoted 9 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

which is filled with pagefault information should one occur.

No functional change.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 12/15
#79 lore raw

Re: [PATCH 12/15] x86/hvm: Rename hvm_copy_*_guest_virt() to hvm_copy_*_guest_linear()

In reply to Andrew Cooper
Quoted 7 lines

From: Andrew Cooper [mailto:[email protected]]
Sent: Wednesday, November 23, 2016 11:39 PM

The functions use linear addresses, not virtual addresses, as no segmentation is used. (Lots of other code in Xen makes this mistake.)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#66 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

In reply to Andrew Cooper
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 17:58, <[email protected]> wrote:

Longterm the plan is to fully split the decode and emulate calls even for external callers,

This is news to me - why would that be? My PV priv-op series will add a hook called between decode and execute, but I don't see why we should bother all callers with invoking decode and execute separately.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#67 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

Quoted 7 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 17:58, <[email protected]> wrote:

Longterm the plan is to fully split the decode and emulate calls even for external callers,

This is news to me - why would that be? My PV priv-op series will add a hook called between decode and execute, but I don't see why we should bother all callers with invoking decode and execute separately.

Having an audit hook is also fine. It achieves the same overall effect.

Without seeing how you planned to do this, I was referring back to the discussion which was had at the April Hackathon.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#68 lore raw

Re: [PATCH 09/15] x86/emul: Avoid raising faults behind the emulators back

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 11:46, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 17:58, <[email protected]> wrote:

Longterm the plan is to fully split the decode and emulate calls even for external callers,

This is news to me - why would that be? My PV priv-op series will add a hook called between decode and execute, but I don't see why we should bother all callers with invoking decode and execute separately.

Quoted 4 lines

Having an audit hook is also fine. It achieves the same overall effect.

Without seeing how you planned to do this, I was referring back to the discussion which was had at the April Hackathon.

Oh, I see. We actually did briefly discuss the hook later (see e.g. lists.xenproject.org/archives/html/xen-devel/2016-09/msg03367.html), and it doesn't make much sense for me to post v3 without your XSA-191 follow-up finished and in place.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#13 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

In reply to Andrew Cooper
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:

xen/arch/x86/mm.c int ptwr_do_page_fault(struct vcpu *v, unsigned long addr, · line 5371

Quoted 10 lines
xen/arch/x86/mm.c +2 -1
@@ -5363,8 +5363,9 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
         goto bail;
     }
 
+    memset(&ptwr_ctxt, 0, sizeof(ptwr_ctxt));
+
     ptwr_ctxt.ctxt.regs = regs;
-    ptwr_ctxt.ctxt.force_writeback = 0;
     ptwr_ctxt.ctxt.addr_size = ptwr_ctxt.ctxt.sp_size =
         is_pv_32bit_domain(d) ? 32 : BITS_PER_LONG;
     ptwr_ctxt.ctxt.swint_emulate = x86_swint_emulate_none;

Mind using an explicit initializer instead, moving everything there that's already known at function start?

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1908

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;

I have to admit that I don't see the point of this.

Quoted 29 lines
xen/arch/x86/x86_emulate/x86_emulate.h +8 -4
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 445

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
     /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY). */
     union {

Hmm, this separation looks to be correct for the current state of the emulator, but I don't think it is architecturally so (and hence it would become wrong in the course of us completing it): Both addr_size and sp_size are not necessarily inputs only. Also keeping regs in the input only group is slightly misleading - the pointer itself surely is input only, but the pointed to data isn't.

So I'd suggest to have three groups: input, input/output, output, even if for your purpose here you only want to tell apart fields which need to be initialized before calling x86_emulate() / x86_decode() (the first two groups) from those which don't (the last group).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 3/15
#21 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
@@ -67,6 +67,28 @@ enum x86_swint_emulation {
     x86_swint_emulate_all,  /* Help needed with all software events */
 };
 
+/*
+ * x86 event types. This enumeration is valid for:
+ *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
+ *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
+ */
+enum x86_event_type {
+    X86_EVENTTYPE_EXT_INTR,         /* External interrupt */

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_swint_emulation { · line 86

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+    X86_EVENTTYPE_NMI = 2,          /* NMI */
+    X86_EVENTTYPE_HW_EXCEPTION,     /* Hardware exception */
+    X86_EVENTTYPE_SW_INTERRUPT,     /* Software interrupt (CD nn) */
+    X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
+    X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
+};
+
+struct x86_event {
+    int16_t       vector;
+    uint8_t       type;         /* X86_EVENTTYPE_* */

Do we perhaps want to make the compiler warn about possibly incomplete switch statements, but making this an 8-bit field of type enum x86_event_type? (That would perhaps imply making vector and insn_len bitfields too; see also below.)

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_swint_emulation { · line 90

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+    uint8_t       insn_len;     /* Instruction length */
+    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
+};

Also I have to admit I'm not really happy about the mixing of fixed width and fundamental types. Can I talk you into using only the latter?

xen/include/asm-x86/hvm/vmx/vvmx.h void nvmx_vcpu_destroy(struct vcpu *v); · line 116

Quoted 10 lines
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -2
@@ -112,8 +112,8 @@ void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
 uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code);
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code);

This reformatting doesn't appear to be in line with other nearby code. But iirc you've got an ack from the VMX side already...

Anyway, with or without the comments addressed,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#14 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +0 -0
@@ -5363,8 +5363,9 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
         goto bail;
     }
 
Quoted 9 lines

xen/arch/x86/mm.c int ptwr_do_page_fault(struct vcpu *v, unsigned long addr, · line 5371

Quoted 7 lines
xen/arch/x86/mm.c +2 -1
+    memset(&ptwr_ctxt, 0, sizeof(ptwr_ctxt));
+
     ptwr_ctxt.ctxt.regs = regs;
-    ptwr_ctxt.ctxt.force_writeback = 0;
     ptwr_ctxt.ctxt.addr_size = ptwr_ctxt.ctxt.sp_size =
         is_pv_32bit_domain(d) ? 32 : BITS_PER_LONG;
     ptwr_ctxt.ctxt.swint_emulate = x86_swint_emulate_none;

Mind using an explicit initializer instead, moving everything there that's already known at function start?

Done.

Quoted 10 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1908

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;

I have to admit that I don't see the point of this.

There are early exit paths which leave it uninitialised. An alternative would be explicitly document that it is only valid for X86EMUL_OKAY?

Quoted 47 lines
Quoted 30 lines
xen/arch/x86/x86_emulate/x86_emulate.h +8 -4
@@ -412,6 +412,10 @@ struct cpu_user_regs;
 
 struct x86_emulate_ctxt
 {
+    /*
+     * Input state:
+     */
+
     /* Register state before/after emulation. */
     struct cpu_user_regs *regs;
 
@@ -421,14 +425,21 @@ struct x86_emulate_ctxt
     /* Stack pointer width in bits (16, 32 or 64). */
     unsigned int sp_size;
 
-    /* Canonical opcode (see below). */
-    unsigned int opcode;
-
     /* Software event injection support. */
     enum x86_swint_emulation swint_emulate;
 
     /* Set this if writes may have side effects. */
-    uint8_t force_writeback;
+    bool force_writeback;
+
+    /* Caller data that can be used by x86_emulate_ops' routines. */
+    void *data;

xen/arch/x86/x86_emulate/x86_emulate.h struct x86_emulate_ctxt · line 445

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
+
+    /*
+     * Output state:
+     */
+
+    /* Canonical opcode (see below). */
+    unsigned int opcode;
 
     /* Retirement state, set by the emulator (valid only on X86EMUL_OKAY). */
     union {

Hmm, this separation looks to be correct for the current state of the emulator, but I don't think it is architecturally so (and hence it would become wrong in the course of us completing it): Both addr_size and sp_size are not necessarily inputs only. Also keeping regs in the input only group is slightly misleading - the pointer itself surely is input only, but the pointed to data isn't.

Quoted 4 lines

So I'd suggest to have three groups: input, input/output, output, even if for your purpose here you only want to tell apart fields which need to be initialized before calling x86_emulate() / x86_decode() (the first two groups) from those which don't (the last group).

Right - proposed net result looks like:

struct x86_emulate_ctxt

{
    /*
     * Input-only state:
     */

    /* Software event injection support. */
    enum x86_swint_emulation swint_emulate;

    /* Set this if writes may have side effects. */
    bool force_writeback;

    /* Caller data that can be used by x86_emulate_ops' routines. */
    void *data;

    /*
     * Input/output state:
     */

    /* Register state before/after emulation. */
    struct cpu_user_regs *regs;

    /* Default address size in current execution mode (16, 32, or 64). */
    unsigned int addr_size;

    /* Stack pointer width in bits (16, 32 or 64). */
    unsigned int sp_size;

    /*
     * Output-only state:
     */

    /* Canonical opcode (see below) (valid only on X86EMUL_OKAY). */
    unsigned int opcode;

    /* Retirement state, set by the emulator (valid only on

X86EMUL_OKAY). */
union {
struct {
uint8_t hlt:1; /* Instruction HLTed. */
uint8_t mov_ss:1; /* Instruction sets MOV-SS irq
shadow. */

            uint8_t sti:1;          /* Instruction sets STI irq shadow. */
        } flags;
        uint8_t byte;
    } retire;
};

If that is ok?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by
#15 lore raw

Re: [PATCH 02/15] x86/emul: Simplfy emulation state setup

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 14:59, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -1904,6 +1904,8 @@ x86_decode(
     state->regs = ctxt->regs;
     state->eip = ctxt->regs->eip;
 
Quoted 6 lines
Quoted 3 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_decode( · line 1908

Quoted 2 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
+    /* Initialise output state in x86_emulate_ctxt */
+    ctxt->opcode = ~0u;

I have to admit that I don't see the point of this.

There are early exit paths which leave it uninitialised. An alternative would be explicitly document that it is only valid for X86EMUL_OKAY?

Well, to me that goes without saying, but I'm fine with it being added.

Quoted 49 lines
Quoted 4 lines

So I'd suggest to have three groups: input, input/output, output, even if for your purpose here you only want to tell apart fields which need to be initialized before calling x86_emulate() / x86_decode() (the first two groups) from those which don't (the last group).

Right - proposed net result looks like:

struct x86_emulate_ctxt

{
    /*
     * Input-only state:
     */

    /* Software event injection support. */
    enum x86_swint_emulation swint_emulate;

    /* Set this if writes may have side effects. */
    bool force_writeback;

    /* Caller data that can be used by x86_emulate_ops' routines. */
    void *data;

    /*
     * Input/output state:
     */

    /* Register state before/after emulation. */
    struct cpu_user_regs *regs;

    /* Default address size in current execution mode (16, 32, or 64). */
    unsigned int addr_size;

    /* Stack pointer width in bits (16, 32 or 64). */
    unsigned int sp_size;

    /*
     * Output-only state:
     */

    /* Canonical opcode (see below) (valid only on X86EMUL_OKAY). */
    unsigned int opcode;

    /* Retirement state, set by the emulator (valid only on

X86EMUL_OKAY). */
union {
struct {
uint8_t hlt:1; /* Instruction HLTed. */
uint8_t mov_ss:1; /* Instruction sets MOV-SS irq
shadow. */

Quoted 7 lines
            uint8_t sti:1;          /* Instruction sets STI irq shadow. */
        } flags;
        uint8_t byte;
    } retire;
};

If that is ok?

Looks good, thanks. With that

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 4/15
#28 lore raw

Re: [PATCH 04/15] x86/emul: Rename HVM_DELIVER_NO_ERROR_CODE to X86_EVENT_NO_EC

In reply to Andrew Cooper
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:

and move it to live with the other x86_event infrastructure in x86_emulate.h. Switch it and x86_event.error_code to being signed, matching the rest of the code.

Preferrably re-based on top of the SVM patch just sent,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#30 lore raw

Re: [PATCH 05/15] x86/emul: Remove opencoded exception generation

In reply to Andrew Cooper

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 678

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +3 -0
+static inline int mkec(uint8_t e, int32_t ec, ...)
+{
+    return (e < 32 && (1u << e) & EXC_HAS_EC) ? ec : X86_EVENT_NO_EC;

Please parenthesize the operands of &.

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 684

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -1
+}
+
+#define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
         fail_if(ops->inject_hw_exception == NULL);                        \
-        rc = ops->inject_hw_exception(e, ec, ctxt) ? : X86EMUL_EXCEPTION; \
+        rc = ops->inject_hw_exception(e, mkec(e, ##ec, 0), ctxt)          \

Did you notice that with the 0 used here, ...

xen/arch/x86/x86_emulate/x86_emulate.c static int ioport_access_check( · line 1186

Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +3 -5
@@ -1167,11 +1181,9 @@ static int ioport_access_check(
     if ( (rc = ops->read_segment(x86_seg_tr, &tr, ctxt)) != 0 )
         return rc;
 
-    /* Ensure that the TSS is valid and has an io-bitmap-offset field. */
-    if ( !tr.attr.fields.p ||
-         ((tr.attr.fields.type & 0xd) != 0x9) ||
-         (tr.limit < 0x67) )
-        goto raise_exception;
+    /* Ensure the TSS has an io-bitmap-offset field. */
+    generate_exception_if(tr.attr.fields.type != 0xb ||
+                          tr.limit < 0x67, EXC_GP, 0);

... invocations like this one don't really need their error code specified anymore either?

With you having added my S-o-b (not really sure why), I'm not sure it makes a whole lot of sense to give my R-b as well (but feel free to add it).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#22 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Jan Beulich
Quoted 29 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
@@ -67,6 +67,28 @@ enum x86_swint_emulation {
     x86_swint_emulate_all,  /* Help needed with all software events */
 };
 
+/*
+ * x86 event types. This enumeration is valid for:
+ *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
+ *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
+ */
+enum x86_event_type {
+    X86_EVENTTYPE_EXT_INTR,         /* External interrupt */

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_swint_emulation { · line 86

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+    X86_EVENTTYPE_NMI = 2,          /* NMI */
+    X86_EVENTTYPE_HW_EXCEPTION,     /* Hardware exception */
+    X86_EVENTTYPE_SW_INTERRUPT,     /* Software interrupt (CD nn) */
+    X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
+    X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
+};
+
+struct x86_event {
+    int16_t       vector;
+    uint8_t       type;         /* X86_EVENTTYPE_* */

Do we perhaps want to make the compiler warn about possibly incomplete switch statements, but making this an 8-bit field of type enum x86_event_type? (That would perhaps imply making vector and insn_len bitfields too; see also below.)

Quoted 7 lines

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_swint_emulation { · line 90

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+    uint8_t       insn_len;     /* Instruction length */
+    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
+};

Also I have to admit I'm not really happy about the mixing of fixed width and fundamental types. Can I talk you into using only the latter?

I am open to idea of swapping things around, but wonder whether this would be better done in a separate patch to avoid interfering with this mechanical movement.

Quoted 13 lines

xen/include/asm-x86/hvm/vmx/vvmx.h void nvmx_vcpu_destroy(struct vcpu *v); · line 116

Quoted 11 lines
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -2
@@ -112,8 +112,8 @@ void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
 uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code);
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code);

This reformatting doesn't appear to be in line with other nearby code. But iirc you've got an ack from the VMX side already...

The first version also had an int => unsigned int change for error_code.

Now, the only difference is trap => vector, but I would like to keep it for consistency with the other changes.

~Andrew

Quoted 6 lines

Anyway, with or without the comments addressed,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#41 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 7 lines
xen/arch/x86/mm.c +2 -1
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

xen/arch/x86/mm/shadow/multi.c static int sh_page_fault(struct vcpu *v, · line 3381

Quoted 8 lines
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -3378,7 +3378,7 @@ static int sh_page_fault(struct vcpu *v,
      * would be a good unshadow hint. If we *do* decide to unshadow-on-fault
      * then it must be 'failable': we cannot require the unshadow to succeed.
      */
-    if ( r == X86EMUL_UNHANDLEABLE )
+    if ( r == X86EMUL_UNHANDLEABLE || emul_ctxt.ctxt.event_pending )

Same here then.

xen/arch/x86/mm/shadow/multi.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm/shadow/multi.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case. But wouldn't you then need to add similar checks in OKAY paths elsewhere? Or alternatively, wouldn't it be better to have x86_emulate() return EXCEPTION also for successful swint emulation (albeit that would likely require other not very nice adjustments)?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#23 lore raw

Re: [PATCH 03/15] x86/emul: Rename hvm_trap to x86_event and move it into the emulation infrastructure

In reply to Andrew Cooper
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 15:42, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.h +7 -0
@@ -67,6 +67,28 @@ enum x86_swint_emulation {
     x86_swint_emulate_all,  /* Help needed with all software events */
 };
 
+/*
+ * x86 event types. This enumeration is valid for:
+ *  Intel VMX: {VM_ENTRY,VM_EXIT,IDT_VECTORING}_INTR_INFO[10:8]
+ *  AMD SVM: eventinj[10:8] and exitintinfo[10:8] (types 0-4 only)
+ */
+enum x86_event_type {
+    X86_EVENTTYPE_EXT_INTR,         /* External interrupt */

xen/arch/x86/x86_emulate/x86_emulate.h enum x86_swint_emulation { · line 86

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +10 -0
+    X86_EVENTTYPE_NMI = 2,          /* NMI */
+    X86_EVENTTYPE_HW_EXCEPTION,     /* Hardware exception */
+    X86_EVENTTYPE_SW_INTERRUPT,     /* Software interrupt (CD nn) */
+    X86_EVENTTYPE_PRI_SW_EXCEPTION, /* ICEBP (F1) */
+    X86_EVENTTYPE_SW_EXCEPTION,     /* INT3 (CC), INTO (CE) */
+};
+
+struct x86_event {
+    int16_t       vector;
+    uint8_t       type;         /* X86_EVENTTYPE_* */

Do we perhaps want to make the compiler warn about possibly incomplete switch statements, but making this an 8-bit field of type enum x86_event_type? (That would perhaps imply making vector and insn_len bitfields too; see also below.)

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.h +4 -0
+    uint8_t       insn_len;     /* Instruction length */
+    uint32_t      error_code;   /* HVM_DELIVER_NO_ERROR_CODE if n/a */
+    unsigned long cr2;          /* Only for TRAP_page_fault h/w exception */
+};
Quoted 7 lines
Quoted 3 lines

Also I have to admit I'm not really happy about the mixing of fixed width and fundamental types. Can I talk you into using only the latter?

I am open to idea of swapping things around, but wonder whether this would be better done in a separate patch to avoid interfering with this mechanical movement.

Okay then.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/include/asm-x86/hvm/vmx/vvmx.h +0 -0
@@ -112,8 +112,8 @@ void nvmx_vcpu_destroy(struct vcpu *v);
 int nvmx_vcpu_reset(struct vcpu *v);
Quoted 8 lines

xen/include/asm-x86/hvm/vmx/vvmx.h void nvmx_vcpu_destroy(struct vcpu *v); · line 116

Quoted 6 lines
xen/include/asm-x86/hvm/vmx/vvmx.h +2 -2
 uint64_t nvmx_vcpu_eptp_base(struct vcpu *v);
 enum hvm_intblk nvmx_intr_blocked(struct vcpu *v);
-bool_t nvmx_intercepts_exception(struct vcpu *v, unsigned int trap,
-                                 int error_code);
+bool_t nvmx_intercepts_exception(
+    struct vcpu *v, unsigned int vector, int error_code);

This reformatting doesn't appear to be in line with other nearby code. But iirc you've got an ack from the VMX side already...

Quoted 4 lines

The first version also had an int => unsigned int change for error_code.

Now, the only difference is trap => vector, but I would like to keep it for consistency with the other changes.

Consistency? My comment was solely about the change in indentation and breaking between lines, and there I think consistency with surrounding code is more important than consistency with other changes.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 8/15
#58 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Andrew Cooper

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6178

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +8 -0
+    case x86_seg_tr:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(!reg->attr.fields.s);                 /* System segment. */
+        ASSERT(!(reg->sel & 0x4));                   /* !TI. */
+        ASSERT(reg->attr.fields.type == SYS_DESC_tss16_busy ||
+               reg->attr.fields.type == SYS_DESC_tss_busy);
+        ASSERT(is_canonical_address(reg->base));
+        break;

I can't help thinking that the slightly more strict

+ if ( reg->attr.fields.type == SYS_DESC_tss_busy )
+ ASSERT(is_canonical_address(reg->base));
+ else if ( reg->attr.fields.type == SYS_DESC_tss16_busy )
+ ASSERT(!(reg->base >> 32));
+ else
+ ASSERT_UNREACHABLE();

would be better, even if that's going to make TR checking look a little different than the others (but we should leverage the information we have).

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    case x86_seg_ldtr:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(!reg->attr.fields.s);             /* System segment. */
+            ASSERT(!(reg->sel & 0x4));               /* !TI. */
+            ASSERT(reg->attr.fields.type == SYS_DESC_ldt);
+            ASSERT(is_canonical_address(reg->base));
+        }
+        break;
+

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6199

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10 -0
+    case x86_seg_gdtr:
+    case x86_seg_idtr:
+        ASSERT(is_canonical_address(reg->base));
+        ASSERT((reg->limit >> 16) == 0);             /* Upper bits clear. */
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        break;
+    }

Didn't you agree to change this last "break" to "return"?

Quoted 6 lines
xen/include/asm-x86/desc.h +0 -0
@@ -89,7 +89,13 @@
 #ifndef __ASSEMBLY__
 
 /* System Descriptor types for GDT and IDT entries. */

xen/include/asm-x86/desc.h line 101

Quoted 10 lines
xen/include/asm-x86/desc.h +6 -0
+#define SYS_DESC_tss16_avail  1
 #define SYS_DESC_ldt          2
+#define SYS_DESC_tss16_busy   3
+#define SYS_DESC_call_gate16  4
+#define SYS_DESC_task_gate    5
+#define SYS_DESC_irq_gate16   6
+#define SYS_DESC_trap_gate16  7
 #define SYS_DESC_tss_avail    9
 #define SYS_DESC_tss_busy     11
 #define SYS_DESC_call_gate    12

Thanks for completing the set.

Regardless of how you decide on the two earlier remarks,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 12/15
#80 lore raw

Re: [PATCH 12/15] x86/hvm: Rename hvm_copy_*_guest_virt() to hvm_copy_*_guest_linear()

In reply to Andrew Cooper
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:

The functions use linear addresses, not virtual addresses, as no segmentation is used. (Lots of other code in Xen makes this mistake.)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v1 14/15
#87 lore raw

Re: [PATCH 14/15] x86/hvm: Prepare to allow use of system segments for memory references

In reply to Andrew Cooper
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:

All system segments (GDT/IDT/LDT and TR) describe a linear address and limit, and act similarly to user segments. However all current uses of these tables in the emulator opencode the address calculations and limit checks. In particular, no care is taken for access which wrap around the 4GB or non-canonical boundaries.

Alter hvm_virtual_to_linear_addr() to cope with performing segmentation checks on system segments. This involves restricting access checks in the 32bit case to user segments only, and adding presence/limit checks in the 64bit case.

Quoted 6 lines

When suffering a segmentation fault for a system segments, return X86EMUL_EXCEPTION but leave the fault injection to the caller. The fault type depends on the higher level action being performed.

I think the code that this covered has been moved elsewhere, so please use

here instead.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#89 lore raw

Re: [PATCH 15/15] x86/hvm: Use system-segment relative memory accesses

In reply to Andrew Cooper
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +1 -2
@@ -1181,20 +1181,38 @@ static int ioport_access_check(
         return rc;
 
     /* Ensure the TSS has an io-bitmap-offset field. */
-    generate_exception_if(tr.attr.fields.type != 0xb ||
-                          tr.limit < 0x67, EXC_GP, 0);
+    generate_exception_if(tr.attr.fields.type != 0xb, EXC_GP, 0);
 

xen/arch/x86/x86_emulate/x86_emulate.c static int ioport_access_check( · line 1193

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8 -2
-    if ( (rc = read_ulong(x86_seg_none, tr.base + 0x66,
-                          &iobmp, 2, ctxt, ops)) )
+    switch ( rc = read_ulong(x86_seg_tr, 0x66, &iobmp, 2, ctxt, ops) )
+    {
+    case X86EMUL_OKAY:
+        break;
+
+    case X86EMUL_EXCEPTION:
+        if ( !ctxt->event_pending )
+            generate_exception_if(true, EXC_GP, 0);

generate_exception_if(!ctxt->event_pending, EXC_GP, 0) ?

xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 1496

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
@@ -1471,9 +1490,12 @@ protmode_load_seg(
     {
         uint32_t new_desc_b = desc.b | a_flag;
 
-        if ( (rc = ops->cmpxchg(x86_seg_none, desctab.base + (sel & 0xfff8) + 4,
-                                &desc.b, &new_desc_b, 4, ctxt)) != 0 )
+        if ( (rc = ops->cmpxchg(sel_seg, (sel & 0xfff8) + 4, &desc.b,
+                                &new_desc_b, 4, ctxt) != X86EMUL_OKAY) )
+        {
+            ASSERT(rc != X86EMUL_EXCEPTION);

Hmm, now that I look at this again I don't think it's right: Why did we think there can't be any exception here? What if the descriptor table page is write protected? Or page tables have been changed behind our back after the earlier read?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#90 lore raw

Re: [PATCH 15/15] x86/hvm: Use system-segment relative memory accesses

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +1 -2
@@ -1181,20 +1181,38 @@ static int ioport_access_check(
         return rc;
 
     /* Ensure the TSS has an io-bitmap-offset field. */
-    generate_exception_if(tr.attr.fields.type != 0xb ||
-                          tr.limit < 0x67, EXC_GP, 0);
+    generate_exception_if(tr.attr.fields.type != 0xb, EXC_GP, 0);
 
Quoted 11 lines

xen/arch/x86/x86_emulate/x86_emulate.c static int ioport_access_check( · line 1193

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8 -2
-    if ( (rc = read_ulong(x86_seg_none, tr.base + 0x66,
-                          &iobmp, 2, ctxt, ops)) )
+    switch ( rc = read_ulong(x86_seg_tr, 0x66, &iobmp, 2, ctxt, ops) )
+    {
+    case X86EMUL_OKAY:
+        break;
+
+    case X86EMUL_EXCEPTION:
+        if ( !ctxt->event_pending )
+            generate_exception_if(true, EXC_GP, 0);

generate_exception_if(!ctxt->event_pending, EXC_GP, 0) ?

Already noticed and fixed in v2.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -1471,9 +1490,12 @@ protmode_load_seg(
     {
         uint32_t new_desc_b = desc.b | a_flag;
 
Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.c protmode_load_seg( · line 1496

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
-        if ( (rc = ops->cmpxchg(x86_seg_none, desctab.base + (sel & 0xfff8) + 4,
-                                &desc.b, &new_desc_b, 4, ctxt)) != 0 )
+        if ( (rc = ops->cmpxchg(sel_seg, (sel & 0xfff8) + 4, &desc.b,
+                                &new_desc_b, 4, ctxt) != X86EMUL_OKAY) )
+        {
+            ASSERT(rc != X86EMUL_EXCEPTION);

Hmm, now that I look at this again I don't think it's right: Why did we think there can't be any exception here?

Hmm. I cant remember either.

Quoted 1 line

What if the descriptor table page is write protected?

Architecturally, a #PF is raised.

Quoted 1 line

Or page tables have been changed behind our back after the earlier read?

Currently nothing because ops->cmpxchg() doesn't have atomic or xchg propertied.

I will drop the assertion, because it is definitely wrong for the pagefault case.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#31 lore raw

Re: [PATCH 05/15] x86/emul: Remove opencoded exception generation

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 678

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +3 -0
+static inline int mkec(uint8_t e, int32_t ec, ...)
+{
+    return (e < 32 && (1u << e) & EXC_HAS_EC) ? ec : X86_EVENT_NO_EC;

Please parenthesize the operands of &.

Fixed.

Quoted 14 lines

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 684

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +4 -1
+}
+
+#define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
         fail_if(ops->inject_hw_exception == NULL);                        \
-        rc = ops->inject_hw_exception(e, ec, ctxt) ? : X86EMUL_EXCEPTION; \
+        rc = ops->inject_hw_exception(e, mkec(e, ##ec, 0), ctxt)          \

Did you notice that with the 0 used here, ...

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -1167,11 +1181,9 @@ static int ioport_access_check(
     if ( (rc = ops->read_segment(x86_seg_tr, &tr, ctxt)) != 0 )
         return rc;
 
Quoted 10 lines

xen/arch/x86/x86_emulate/x86_emulate.c static int ioport_access_check( · line 1186

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +3 -5
-    /* Ensure that the TSS is valid and has an io-bitmap-offset field. */
-    if ( !tr.attr.fields.p ||
-         ((tr.attr.fields.type & 0xd) != 0x9) ||
-         (tr.limit < 0x67) )
-        goto raise_exception;
+    /* Ensure the TSS has an io-bitmap-offset field. */
+    generate_exception_if(tr.attr.fields.type != 0xb ||
+                          tr.limit < 0x67, EXC_GP, 0);

... invocations like this one don't really need their error code specified anymore either?

Yes, but I chose not to visually remove the error code from EXC_GP.

I attempted to get the compiler to force the presence or absence of the error code parameter based on (e & EXC_HAS_EC) but failed to get something working. I doubt the C preprocessor is sufficiently expressive for this use.

Quoted 4 lines

With you having added my S-o-b (not really sure why), I'm not sure it makes a whole lot of sense to give my R-b as well (but feel free to add it).

Your code was originally the few bits replacing opencoded "goto raise_exception" with generate_exception_if(), but the patch has morphed a long way since then. I am happy to exchange your S-o-B for R-b if you would prefer?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#32 lore raw

Re: [PATCH 05/15] x86/emul: Remove opencoded exception generation

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 17:24, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

xen/arch/x86/x86_emulate/x86_emulate.c do { \ · line 684

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
+#define generate_exception_if(p, e, ec...)                                \
 ({  if ( (p) ) {                                                          \
         fail_if(ops->inject_hw_exception == NULL);                        \
-        rc = ops->inject_hw_exception(e, ec, ctxt) ? : X86EMUL_EXCEPTION; \
+        rc = ops->inject_hw_exception(e, mkec(e, ##ec, 0), ctxt)          \

Did you notice that with the 0 used here, ...

Quoted 4 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
@@ -1167,11 +1181,9 @@ static int ioport_access_check(
     if ( (rc = ops->read_segment(x86_seg_tr, &tr, ctxt)) != 0 )
         return rc;
 
Quoted 10 lines

xen/arch/x86/x86_emulate/x86_emulate.c static int ioport_access_check( · line 1186

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +3 -5
-    /* Ensure that the TSS is valid and has an io-bitmap-offset field. */
-    if ( !tr.attr.fields.p ||
-         ((tr.attr.fields.type & 0xd) != 0x9) ||
-         (tr.limit < 0x67) )
-        goto raise_exception;
+    /* Ensure the TSS has an io-bitmap-offset field. */
+    generate_exception_if(tr.attr.fields.type != 0xb ||
+                          tr.limit < 0x67, EXC_GP, 0);

... invocations like this one don't really need their error code specified anymore either?

Quoted 1 line

Yes, but I chose not to visually remove the error code from EXC_GP.

Well, let's keep it that way then for now, but I think I will be tempted to get rid of those zeros down the road.

Quoted 4 lines

I attempted to get the compiler to force the presence or absence of the error code parameter based on (e & EXC_HAS_EC) but failed to get something working. I doubt the C preprocessor is sufficiently expressive for this use.

Right, I had tried to think of something like that too when working out how that mkec() could be made work, but couldn't think of anything. After all, us using the ellipsis is pretty much heading into the opposite direction (and that part of why I think dropping the zeros too would be a good idea, because that will make more visible when we actually care about a _specific_ error code).

Quoted 8 lines
Quoted 3 lines

With you having added my S-o-b (not really sure why), I'm not sure it makes a whole lot of sense to give my R-b as well (but feel free to add it).

Your code was originally the few bits replacing opencoded "goto raise_exception" with generate_exception_if(), but the patch has morphed a long way since then. I am happy to exchange your S-o-B for R-b if you would prefer?

Yes, it's basically all your work, so I think R-b is more appropriate.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#42 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
Quoted 7 lines

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 4 lines
xen/arch/x86/mm.c +2 -1
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

In this patch, the key point I am guarding against is that, without the ->inject_*() hooks, some actions which previously took a fail_if() path now succeed and latch an event.

From that point of view, it doesn't matter how the event became pending, but the fact that one is means that it was a codepath which would previously have returned UNHANDLEABLE.

Later patches, which stop raising faults behind the back of emulator, are the ones where new consideration is needed towards the handling of EXCEPTION/event_pending. Following Tim's feedback, I have more work to do in patch 9, as propagate_page_fault() raises #PF behind the back of the emulator for PV guests.

In other words, I think this patch wants to stay like this, and a later one change to be better accommodating.

Quoted 7 lines

xen/arch/x86/mm.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case.

Why? software interrupts were never previously tolerated in shadow emulation.

Quoted 1 line

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

I don't see why I would. Does my explanation above resolve your concern?

Quoted 3 lines

Or alternatively, wouldn't it be better to have x86_emulate() return EXCEPTION also for successful swint emulation (albeit that would likely require other not very nice adjustments)?

That would indeed be nasty. If we were to go down that route, it would be better to swap X86EMUL_EXCEPTION for X86EMUL_EVENT which explicitly also includes traps where a register writeback has been completed.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#33 lore raw

Re: [PATCH 05/15] x86/emul: Remove opencoded exception generation

In reply to Jan Beulich
Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 4 lines

With you having added my S-o-b (not really sure why), I'm not sure it makes a whole lot of sense to give my R-b as well (but feel free to add it).

Your code was originally the few bits replacing opencoded "goto raise_exception" with generate_exception_if(), but the patch has morphed a long way since then. I am happy to exchange your S-o-B for R-b if you would prefer?

Yes, it's basically all your work, so I think R-b is more appropriate.

Ok done.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#43 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:00, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
Quoted 7 lines

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 4 lines
xen/arch/x86/mm.c +2 -1
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

In this patch, the key point I am guarding against is that, without the ->inject_*() hooks, some actions which previously took a fail_if() path now succeed and latch an event.

From that point of view, it doesn't matter how the event became pending, but the fact that one is means that it was a codepath which would previously have returned UNHANDLEABLE.

Quoted 8 lines

Later patches, which stop raising faults behind the back of emulator, are the ones where new consideration is needed towards the handling of EXCEPTION/event_pending. Following Tim's feedback, I have more work to do in patch 9, as propagate_page_fault() raises #PF behind the back of the emulator for PV guests.

In other words, I think this patch wants to stay like this, and a later one change to be better accommodating.

Okay.

Quoted 10 lines
Quoted 7 lines

xen/arch/x86/mm.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case.

Why? software interrupts were never previously tolerated in shadow emulation.

Then why would you expect OKAY together with event_pending set? I'm not saying swint handling needs to succeed here, but I can't see anything else to cause that particular state to occur.

Quoted 3 lines
Quoted 1 line

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

I don't see why I would. Does my explanation above resolve your concern?

I'm afraid not: On the same basis as above, code not expecting to handle swint may now see OKAY together with event_pending set, and would need to indicate failure to their callers just like you do in sh_page_fault().

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#44 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

Quoted 51 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:00, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
Quoted 7 lines

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 4 lines
xen/arch/x86/mm.c +2 -1
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

In this patch, the key point I am guarding against is that, without the ->inject_*() hooks, some actions which previously took a fail_if() path now succeed and latch an event.

From that point of view, it doesn't matter how the event became pending, but the fact that one is means that it was a codepath which would previously have returned UNHANDLEABLE.

Quoted 8 lines

Later patches, which stop raising faults behind the back of emulator, are the ones where new consideration is needed towards the handling of EXCEPTION/event_pending. Following Tim's feedback, I have more work to do in patch 9, as propagate_page_fault() raises #PF behind the back of the emulator for PV guests.

In other words, I think this patch wants to stay like this, and a later one change to be better accommodating.

Okay.

Quoted 7 lines
Quoted 7 lines

xen/arch/x86/mm.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case.

Quoted 5 lines
Quoted 2 lines

Why? software interrupts were never previously tolerated in shadow emulation.

Then why would you expect OKAY together with event_pending set? I'm not saying swint handling needs to succeed here, but I can't see anything else to cause that particular state to occur.

Before this patch, a VM playing race conditions with the emulator could cause this case to emulate 0xcc, which would fail because of the lack of ->inject_sw_interrupt() hook, and return X86_UNHANDLEABLE.

The changes in this patch now mean that the same case would properly latch #BP, returning OKAY because its a trap not an exception.

By not explicitly failing the OKAY case with an event pending, we are suddenly opening up rather more functionality than previously existed.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

I don't see why I would. Does my explanation above resolve your concern?

I'm afraid not: On the same basis as above, code not expecting to handle swint may now see OKAY together with event_pending set, and would need to indicate failure to their callers just like you do in sh_page_fault().

That is my intent with the current code. I have double checked it, and it still looks correct.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#59 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Jan Beulich
Quoted 18 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6178

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +8 -0
+    case x86_seg_tr:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(!reg->attr.fields.s);                 /* System segment. */
+        ASSERT(!(reg->sel & 0x4));                   /* !TI. */
+        ASSERT(reg->attr.fields.type == SYS_DESC_tss16_busy ||
+               reg->attr.fields.type == SYS_DESC_tss_busy);
+        ASSERT(is_canonical_address(reg->base));
+        break;

I can't help thinking that the slightly more strict

xen/arch/x86/hvm/hvm.c +6 -0
+        if ( reg->attr.fields.type == SYS_DESC_tss_busy )
+            ASSERT(is_canonical_address(reg->base));
+        else if ( reg->attr.fields.type == SYS_DESC_tss16_busy )
+            ASSERT(!(reg->base >> 32));
+        else
+            ASSERT_UNREACHABLE();
Quoted 3 lines

would be better, even if that's going to make TR checking look a little different than the others (but we should leverage the information we have).

I still don't like the use of ASSERT_UNREACHABLE(); as the "you failed the typecheck" case.

Would ASSERT(!"%tr typecheck failure") be acceptable?

Quoted 14 lines
Quoted 14 lines
xen/arch/x86/hvm/hvm.c +13 -0
+    case x86_seg_ldtr:
+        if ( reg->attr.fields.p )
+        {
+            ASSERT(!reg->attr.fields.s);             /* System segment. */
+            ASSERT(!(reg->sel & 0x4));               /* !TI. */
+            ASSERT(reg->attr.fields.type == SYS_DESC_ldt);
+            ASSERT(is_canonical_address(reg->base));
+        }
+        break;
+
+    case x86_seg_gdtr:
+    case x86_seg_idtr:
+        ASSERT(is_canonical_address(reg->base));
Quoted 8 lines

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6199

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +7 -0
+        ASSERT((reg->limit >> 16) == 0);             /* Upper bits clear. */
+        break;
+
+    default:
+        ASSERT_UNREACHABLE();
+        break;
+    }

Didn't you agree to change this last "break" to "return"?

Yes. Sorry. Fixed.

Quoted 19 lines
Quoted 7 lines
xen/include/asm-x86/desc.h +0 -0
@@ -89,7 +89,13 @@
 #ifndef __ASSEMBLY__
 
 /* System Descriptor types for GDT and IDT entries. */

xen/include/asm-x86/desc.h line 101

Quoted 10 lines
xen/include/asm-x86/desc.h +6 -0
+#define SYS_DESC_tss16_avail  1
 #define SYS_DESC_ldt          2
+#define SYS_DESC_tss16_busy   3
+#define SYS_DESC_call_gate16  4
+#define SYS_DESC_task_gate    5
+#define SYS_DESC_irq_gate16   6
+#define SYS_DESC_trap_gate16  7
 #define SYS_DESC_tss_avail    9
 #define SYS_DESC_tss_busy     11
 #define SYS_DESC_call_gate    12

Thanks for completing the set.

Quoted 5 lines

Regardless of how you decide on the two earlier remarks,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#45 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 70 lines · Andrew Cooper
Quoted 51 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:00, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
Quoted 7 lines

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 4 lines
xen/arch/x86/mm.c +2 -1
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

In this patch, the key point I am guarding against is that, without the ->inject_*() hooks, some actions which previously took a fail_if() path now succeed and latch an event.

From that point of view, it doesn't matter how the event became pending, but the fact that one is means that it was a codepath which would previously have returned UNHANDLEABLE.

Quoted 8 lines

Later patches, which stop raising faults behind the back of emulator, are the ones where new consideration is needed towards the handling of EXCEPTION/event_pending. Following Tim's feedback, I have more work to do in patch 9, as propagate_page_fault() raises #PF behind the back of the emulator for PV guests.

In other words, I think this patch wants to stay like this, and a later one change to be better accommodating.

Okay.

Quoted 7 lines
Quoted 7 lines

xen/arch/x86/mm.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case.

Quoted 5 lines
Quoted 2 lines

Why? software interrupts were never previously tolerated in shadow emulation.

Then why would you expect OKAY together with event_pending set? I'm not saying swint handling needs to succeed here, but I can't see anything else to cause that particular state to occur.

Before this patch, a VM playing race conditions with the emulator could cause this case to emulate 0xcc, which would fail because of the lack of ->inject_sw_interrupt() hook, and return X86_UNHANDLEABLE.

The changes in this patch now mean that the same case would properly latch #BP, returning OKAY because its a trap not an exception.

By not explicitly failing the OKAY case with an event pending, we are suddenly opening up rather more functionality than previously existed.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I don't see why I would. Does my explanation above resolve your concern?

I'm afraid not: On the same basis as above, code not expecting to handle swint may now see OKAY together with event_pending set, and would need to indicate failure to their callers just like you do in sh_page_fault().

That is my intent with the current code. I have double checked it, and it still looks correct.

So is that not the case I was worried about, where the emulator updates register state but we then drop the expected event on the floor?

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#46 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

Quoted 74 lines · Tim Deegan
Quoted 73 lines · Andrew Cooper
Quoted 51 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:00, <[email protected]> wrote:
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/mm.c +1 -1
@@ -5377,7 +5377,7 @@ int ptwr_do_page_fault(struct vcpu *v, unsigned long addr,
     page_unlock(page);
     put_page(page);
 
-    if ( rc == X86EMUL_UNHANDLEABLE )
+    if ( rc == X86EMUL_UNHANDLEABLE || ptwr_ctxt.ctxt.event_pending )
         goto bail;
 
     perfc_incr(ptwr_emulations);
@@ -5501,7 +5501,8 @@ int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr,
     else
         rc = x86_emulate(&ctxt, &mmio_ro_emulate_ops);
 
Quoted 7 lines

xen/arch/x86/mm.c int mmio_ro_do_page_fault(struct vcpu *v, unsigned long addr, · line 5506

Quoted 4 lines
xen/arch/x86/mm.c +2 -1
-    return rc != X86EMUL_UNHANDLEABLE ? EXCRET_fault_fixed : 0;
+    return ((rc != X86EMUL_UNHANDLEABLE && !ctxt.event_pending)
+            ? EXCRET_fault_fixed : 0);
 }

Wouldn't these two better be adjusted to check for OKAY and RETRY, the more that iirc we had settled on it not (yet) being guaranteed to see event_pending set whenever getting back EXCEPTION?

In this patch, the key point I am guarding against is that, without the ->inject_*() hooks, some actions which previously took a fail_if() path now succeed and latch an event.

From that point of view, it doesn't matter how the event became pending, but the fact that one is means that it was a codepath which would previously have returned UNHANDLEABLE.

Quoted 8 lines

Later patches, which stop raising faults behind the back of emulator, are the ones where new consideration is needed towards the handling of EXCEPTION/event_pending. Following Tim's feedback, I have more work to do in patch 9, as propagate_page_fault() raises #PF behind the back of the emulator for PV guests.

In other words, I think this patch wants to stay like this, and a later one change to be better accommodating.

Okay.

Quoted 7 lines
Quoted 7 lines

xen/arch/x86/mm.c static int sh_page_fault(struct vcpu *v, · line 3436

Quoted 6 lines
xen/arch/x86/mm.c +1 -1
@@ -3433,7 +3433,7 @@ static int sh_page_fault(struct vcpu *v,
             shadow_continue_emulation(&emul_ctxt, regs);
             v->arch.paging.last_write_was_pt = 0;
             r = x86_emulate(&emul_ctxt.ctxt, emul_ops);
-            if ( r == X86EMUL_OKAY )
+            if ( r == X86EMUL_OKAY && !emul_ctxt.ctxt.event_pending )

Aiui you need this for the swint case.

Quoted 5 lines
Quoted 2 lines

Why? software interrupts were never previously tolerated in shadow emulation.

Then why would you expect OKAY together with event_pending set? I'm not saying swint handling needs to succeed here, but I can't see anything else to cause that particular state to occur.

Before this patch, a VM playing race conditions with the emulator could cause this case to emulate 0xcc, which would fail because of the lack of ->inject_sw_interrupt() hook, and return X86_UNHANDLEABLE.

The changes in this patch now mean that the same case would properly latch #BP, returning OKAY because its a trap not an exception.

By not explicitly failing the OKAY case with an event pending, we are suddenly opening up rather more functionality than previously existed.

Quoted 6 lines
Quoted 2 lines
Quoted 1 line

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

I don't see why I would. Does my explanation above resolve your concern?

I'm afraid not: On the same basis as above, code not expecting to handle swint may now see OKAY together with event_pending set, and would need to indicate failure to their callers just like you do in sh_page_fault().

Quoted 5 lines
Quoted 2 lines

That is my intent with the current code. I have double checked it, and it still looks correct.

So is that not the case I was worried about, where the emulator updates register state but we then drop the expected event on the floor?

Oh right, yes. Sorry for being dense.

As an interim between now and getting a proper audit hook, would a bool permit_traps in x86_emulate_ctxt suffice?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#47 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:37, <[email protected]> wrote:

As an interim between now and getting a proper audit hook, would a bool permit_traps in x86_emulate_ctxt suffice?

That's one option; the other would be to do away with only the exception injection hook for now, and keep the swint one. That's effectively the same as the boolean flag, just with less overall code churn (and keeping the hook would not undermine this series' goal of eliminating event injection behind the back of the emulator).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#49 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Andrew Cooper
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:19, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · On 24.11.16 at 18:00, <[email protected]> wrote:

But wouldn't you then need to add similar checks in OKAY paths elsewhere?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I don't see why I would. Does my explanation above resolve your concern?

I'm afraid not: On the same basis as above, code not expecting to handle swint may now see OKAY together with event_pending set, and would need to indicate failure to their callers just like you do in sh_page_fault().

That is my intent with the current code. I have double checked it, and it still looks correct.

Then what about the handling immediately after the x86_emulate() invocation in _hvm_emulate_one()? Apart from that I've now also convinced myself that you handle all existing callers.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#60 lore raw

Re: [PATCH 08/15] x86/hvm: Reposition the modification of raw segment data from the VMCB/VMCS

In reply to Andrew Cooper
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:22, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich

xen/arch/x86/hvm/hvm.c void hvm_domain_soft_reset(struct domain *d) · line 6178

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 23.11.16 at 16:38, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +8 -0
+    case x86_seg_tr:
+        ASSERT(reg->attr.fields.p);                  /* Usable. */
+        ASSERT(!reg->attr.fields.s);                 /* System segment. */
+        ASSERT(!(reg->sel & 0x4));                   /* !TI. */
+        ASSERT(reg->attr.fields.type == SYS_DESC_tss16_busy ||
+               reg->attr.fields.type == SYS_DESC_tss_busy);
+        ASSERT(is_canonical_address(reg->base));
+        break;

I can't help thinking that the slightly more strict

xen/arch/x86/hvm/hvm.c +6 -0
+        if ( reg->attr.fields.type == SYS_DESC_tss_busy )
+            ASSERT(is_canonical_address(reg->base));
+        else if ( reg->attr.fields.type == SYS_DESC_tss16_busy )
+            ASSERT(!(reg->base >> 32));
+        else
+            ASSERT_UNREACHABLE();
Quoted 8 lines
Quoted 3 lines

would be better, even if that's going to make TR checking look a little different than the others (but we should leverage the information we have).

I still don't like the use of ASSERT_UNREACHABLE(); as the "you failed the typecheck" case.

Would ASSERT(!"%tr typecheck failure") be acceptable?

Sure.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#48 lore raw

Re: [PATCH 06/15] x86/emul: Rework emulator event injection

In reply to Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 24.11.16 at 18:37, <[email protected]> wrote:

As an interim between now and getting a proper audit hook, would a bool permit_traps in x86_emulate_ctxt suffice?

That's one option; the other would be to do away with only the exception injection hook for now, and keep the swint one. That's effectively the same as the boolean flag, just with less overall code churn (and keeping the hook would not undermine this series' goal of eliminating event injection behind the back of the emulator).

I'd be OK with either of those. Or indeed with
- adjusting the emulator so it always returns a non-OK value when it needs the caller to do something to make the state consistent; or
- keeping the injection hook but moving it to the end of the emulation, as a sort of write-back of the emulator's internal event state to the guest. That would let the emulator DTRT about tracking events internally but still avoid any risk that the caller forgets to inject the event.

Tim.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel