Skip to contentSkip to conversation

x86/hvm: Start unifying VMEXIT and emulated paths

RFC In review Ross Lagerwall 4 participants 21 messages May 18 – May 22

lore
Share & tools
lore
#0 lore raw

cover x86/hvm: Start unifying VMEXIT and emulated paths

Hi,

There are differences in instruction execution when coming from a VMEXIT vs a fully emulated path. This can result in subtle bugs, e.g. injecting #GP with trap rather than fault semantics. When using nested virt, these bugs may become security issues if it allows L2 to attack L1 (because L0 has done something unexpected).

To fix this, we plan to unify the VMEXIT (fast) path and emulated instruction paths with everything going through the emulator but the fast path short-circuiting some parts. This patch series adds the basic infrastructure and converts two SVM VMEXITs: halt and read/write CR

The other motivation to do this work is to arrange it so that there is only a single place where Xen needs to consider whether an instruction/event is something that L1 cares about.

This is very much an RFC at this point so I'm mainly looking for feedback on the overall approach.

Thanks,
Ross

Ross Lagerwall (7):
x86/hvm: Move description into hvm_emulate_ctxt
x86/hvm: Introduce hvm_emulate_one_ctxt
x86/emul: Separate out instruction completion
x86/hvm: Support a fast path during emulation
x86/hvm: Move INSTR_* constants to hvm.h
x86/svm: Use the emulator path for VMEXIT_HLT
x86/svm: Use the emulator path for VMEXIT_CR*

 xen/arch/x86/hvm/emulate.c             | 180 +++++++++++++++++++------
 xen/arch/x86/hvm/hvm.c                 |   2 +-
 xen/arch/x86/hvm/io.c                  |  19 ++-
 xen/arch/x86/hvm/ioreq.c               |   2 +-
 xen/arch/x86/hvm/svm/emulate.c         |   6 +-
 xen/arch/x86/hvm/svm/svm.c             |  52 +++----
 xen/arch/x86/hvm/svm/svm.h             |  30 +----
 xen/arch/x86/hvm/vmx/realmode.c        |   2 +-
 xen/arch/x86/include/asm/hvm/emulate.h |  13 ++
 xen/arch/x86/include/asm/hvm/hvm.h     |  31 +++++
 xen/arch/x86/x86_emulate/x86_emulate.c |  48 ++++---
 xen/arch/x86/x86_emulate/x86_emulate.h |   3 +
 12 files changed, 252 insertions(+), 136 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

RFC 1/7 x86/hvm: Move description into hvm_emulate_ctxt

This will be needed to split up hvm_emulate_one_insn().

---
 xen/arch/x86/hvm/emulate.c             | 4 +++-
 xen/arch/x86/hvm/hvm.c                 | 2 +-
 xen/arch/x86/hvm/io.c                  | 6 +++---
 xen/arch/x86/hvm/ioreq.c               | 2 +-
 xen/arch/x86/hvm/svm/emulate.c         | 4 ++--
 xen/arch/x86/hvm/vmx/realmode.c        | 2 +-
 xen/arch/x86/include/asm/hvm/emulate.h | 2 ++
 7 files changed, 13 insertions(+), 9 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -1
index bb1bb03ac4e9..a96d7814f91c 100644
@@ -2868,7 +2868,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
     struct hvm_emulate_ctxt ctx = {};
     int rc;
 
-    hvm_emulate_init_once(&ctx, NULL, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctx, NULL, NULL, guest_cpu_user_regs());
 
     switch ( kind )
     {
@@ -2929,6 +2929,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind, unsigned int trapnr,
 void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     hvm_emulate_validate_t *validate,
+    const char *descr,
     struct cpu_user_regs *regs)
 {
     struct vcpu *curr = current;
@@ -2941,6 +2942,7 @@ void hvm_emulate_init_once(
     hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt);
 
     hvmemul_ctxt->validate = validate;
+    hvmemul_ctxt->descr = descr;
     hvmemul_ctxt->ctxt.regs = regs;
     hvmemul_ctxt->ctxt.cpu_policy = curr->domain->arch.cpu_policy;
     hvmemul_ctxt->ctxt.force_writeback = true;
xen/arch/x86/hvm/hvm.c +1 -1
index f759a397c557..de399bd9024a 100644
@@ -3835,7 +3835,7 @@ void hvm_ud_intercept(struct cpu_user_regs *regs)
     bool should_emulate = false;
     struct hvm_emulate_ctxt ctxt;
 
-    hvm_emulate_init_once(&ctxt, NULL, regs);
+    hvm_emulate_init_once(&ctxt, NULL, NULL, regs);
 
     if ( opt_hvm_fep )
     {
xen/arch/x86/hvm/io.c +3 -3
index 23a5ea0e6197..d9838c36d330 100644
@@ -55,16 +55,16 @@ bool hvm_emulate_one_insn(hvm_emulate_validate_t *validate, const char *descr)
     struct hvm_emulate_ctxt ctxt;
     int rc;
 
-    hvm_emulate_init_once(&ctxt, validate, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, validate, descr, guest_cpu_user_regs());
 
     switch ( rc = hvm_emulate_one(&ctxt, VIO_no_completion) )
     {
     case X86EMUL_UNHANDLEABLE:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
         return false;
 
     case X86EMUL_UNRECOGNIZED:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
         hvm_inject_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC);
         break;
 
xen/arch/x86/hvm/ioreq.c +1 -1
index a5fa97e149ba..42ff796c6cb9 100644
@@ -39,7 +39,7 @@ bool arch_vcpu_ioreq_completion(enum vio_completion completion)
     {
         struct hvm_emulate_ctxt ctxt;
 
-        hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
+        hvm_emulate_init_once(&ctxt, NULL, NULL, guest_cpu_user_regs());
         vmx_realmode_emulate_one(&ctxt);
         hvm_emulate_writeback(&ctxt);
 
xen/arch/x86/hvm/svm/emulate.c +2 -2
index 1f5436c8ea5d..1dd830a31bd7 100644
@@ -71,7 +71,7 @@ unsigned int svm_get_insn_len(struct vcpu *v, unsigned int instr_enc)
 #endif
 
     ASSERT(v == current);
-    hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, NULL, NULL, 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) )
@@ -116,7 +116,7 @@ unsigned int svm_get_task_switch_insn_len(void)
     struct x86_emulate_state *state;
     unsigned int emul_len, modrm_reg;
 
-    hvm_emulate_init_once(&ctxt, NULL, guest_cpu_user_regs());
+    hvm_emulate_init_once(&ctxt, NULL, NULL, 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) )
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index ff44ddcfa627..9879533280d0 100644
@@ -163,7 +163,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, 0);
 
-    hvm_emulate_init_once(&hvmemul_ctxt, NULL, regs);
+    hvm_emulate_init_once(&hvmemul_ctxt, NULL, NULL, regs);
 
     /* Only deliver interrupts into emulated real mode. */
     if ( !(curr->arch.hvm.guest_cr[0] & X86_CR0_PE) &&
xen/arch/x86/include/asm/hvm/emulate.h +2 -0
index 178ac32e151f..2801d14d5455 100644
@@ -30,6 +30,7 @@ struct hvm_emulate_ctxt {
      * filtering.
      */
     hvm_emulate_validate_t *validate;
+    const char *descr;
 
     /* Cache of 16 bytes of instruction. */
     uint8_t insn_buf[16];
@@ -74,6 +75,7 @@ void hvm_emulate_one_vm_event(enum emul_kind kind,
 void hvm_emulate_init_once(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     hvm_emulate_validate_t *validate,
+    const char *descr,
     struct cpu_user_regs *regs);
 /* Must be called once before each instruction emulated. */
 void hvm_emulate_init_per_insn(
Signature · 1 line
2.53.0
#2 lore raw

RFC 2/7 x86/hvm: Introduce hvm_emulate_one_ctxt

In reply to Ross Lagerwall

This is like hvm_emulate_one_insn() but takes an existing hvm_emulate_ctxt as a parameter. This will be needed to merge the instruction emulator and VMEXIT fast paths.

---
 xen/arch/x86/hvm/io.c                  | 17 +++++++++++------
 xen/arch/x86/include/asm/hvm/emulate.h |  1 +
 2 files changed, 12 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/io.c +11 -6
index d9838c36d330..7dc6f64b646e 100644
@@ -53,27 +53,32 @@ void send_timeoffset_req(unsigned long timeoff)
 bool hvm_emulate_one_insn(hvm_emulate_validate_t *validate, const char *descr)
 {
     struct hvm_emulate_ctxt ctxt;
-    int rc;
 
     hvm_emulate_init_once(&ctxt, validate, descr, guest_cpu_user_regs());
+    return hvm_emulate_one_ctxt(&ctxt);
+}
 
-    switch ( rc = hvm_emulate_one(&ctxt, VIO_no_completion) )
+bool hvm_emulate_one_ctxt(struct hvm_emulate_ctxt *ctxt)
+{
+    int rc = hvm_emulate_one(ctxt, VIO_no_completion);
+
+    switch ( rc )
     {
     case X86EMUL_UNHANDLEABLE:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
         return false;
 
     case X86EMUL_UNRECOGNIZED:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
         hvm_inject_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC);
         break;
 
     case X86EMUL_EXCEPTION:
-        hvm_inject_event(&ctxt.ctxt.event);
+        hvm_inject_event(&ctxt->ctxt.event);
         break;
     }
 
-    hvm_emulate_writeback(&ctxt);
+    hvm_emulate_writeback(ctxt);
 
     return true;
 }
xen/arch/x86/include/asm/hvm/emulate.h +1 -0
index 2801d14d5455..084e2bd90588 100644
@@ -65,6 +65,7 @@ enum emul_kind {
 bool __nonnull(1, 2) hvm_emulate_one_insn(
     hvm_emulate_validate_t *validate,
     const char *descr);
+bool hvm_emulate_one_ctxt(struct hvm_emulate_ctxt *ctxt);
 int hvm_emulate_one(
     struct hvm_emulate_ctxt *hvmemul_ctxt,
     enum vio_completion completion);
Signature · 1 line
2.53.0
#3 lore raw

Re: [RFC v1 2/7] x86/hvm: Introduce hvm_emulate_one_ctxt

Quoted 3 lines · Ross Lagerwall

This is like hvm_emulate_one_insn() but takes an existing hvm_emulate_ctxt as a parameter. This will be needed to merge the instruction emulator and VMEXIT fast paths.

I'm yet to understand why this is necessary, but I'm afraid I don't consider the name very descriptive. Even hvm_emulate_one_insn2() or hvm_emulate_one_insn_ex() would come closer: You don't "emulate a context".

Quoted 19 lines
xen/arch/x86/hvm/io.c +7 -2
@@ -53,27 +53,32 @@ void send_timeoffset_req(unsigned long timeoff)
 bool hvm_emulate_one_insn(hvm_emulate_validate_t *validate, const char *descr)
 {
     struct hvm_emulate_ctxt ctxt;
-    int rc;
 
     hvm_emulate_init_once(&ctxt, validate, descr, guest_cpu_user_regs());
+    return hvm_emulate_one_ctxt(&ctxt);
+}
 
-    switch ( rc = hvm_emulate_one(&ctxt, VIO_no_completion) )
+bool hvm_emulate_one_ctxt(struct hvm_emulate_ctxt *ctxt)
+{
+    int rc = hvm_emulate_one(ctxt, VIO_no_completion);
+
+    switch ( rc )
     {

xen/arch/x86/hvm/io.c void send_timeoffset_req(unsigned long timeoff) · line 74

Quoted 10 lines
xen/arch/x86/hvm/io.c +2 -2
     case X86EMUL_UNHANDLEABLE:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
         return false;
 
     case X86EMUL_UNRECOGNIZED:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
         hvm_inject_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC);
         break;

These two lines were already touched by patch 1. Afaict leaving the change out there will be fine, and hence will be overall less churn (plus following the lines' histories will be one step shorter).

Jan

#4 lore raw

Re: [RFC v1 2/7] x86/hvm: Introduce hvm_emulate_one_ctxt

Quoted 8 lines · Jan Beulich
Quoted 3 lines · Ross Lagerwall

This is like hvm_emulate_one_insn() but takes an existing hvm_emulate_ctxt as a parameter. This will be needed to merge the instruction emulator and VMEXIT fast paths.

I'm yet to understand why this is necessary, but I'm afraid I don't consider the name very descriptive. Even hvm_emulate_one_insn2() or hvm_emulate_one_insn_ex() would come closer: You don't "emulate a context".

Of those, I'd prefer hvm_emulate_one_insn_ex(), though it still doesn't give much away as to how it differs from the proliferation of similarly named functions (i.e. _hvm_emulate_one(), hvm_emulate_one(), hvm_emulate_one_insn()).

Quoted 25 lines
Quoted 25 lines
xen/arch/x86/hvm/io.c +8 -3
@@ -53,27 +53,32 @@ void send_timeoffset_req(unsigned long timeoff)
  bool hvm_emulate_one_insn(hvm_emulate_validate_t *validate, const char *descr)
  {
      struct hvm_emulate_ctxt ctxt;
-    int rc;
 
      hvm_emulate_init_once(&ctxt, validate, descr, guest_cpu_user_regs());
+    return hvm_emulate_one_ctxt(&ctxt);
+}
 
-    switch ( rc = hvm_emulate_one(&ctxt, VIO_no_completion) )
+bool hvm_emulate_one_ctxt(struct hvm_emulate_ctxt *ctxt)
+{
+    int rc = hvm_emulate_one(ctxt, VIO_no_completion);
+
+    switch ( rc )
      {
      case X86EMUL_UNHANDLEABLE:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
          return false;
 
Quoted 10 lines

xen/arch/x86/hvm/io.c void send_timeoffset_req(unsigned long timeoff) · line 74

Quoted 5 lines
xen/arch/x86/hvm/io.c +1 -1
      case X86EMUL_UNRECOGNIZED:
-        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt.descr, &ctxt, rc);
+        hvm_dump_emulation_state(XENLOG_G_WARNING, ctxt->descr, ctxt, rc);
          hvm_inject_hw_exception(X86_EXC_UD, X86_EVENT_NO_EC);
          break;

These two lines were already touched by patch 1. Afaict leaving the change out there will be fine, and hence will be overall less churn (plus following the lines' histories will be one step shorter).

OK.

Thanks,
Ross

#5 lore raw

RFC 3/7 x86/emul: Separate out instruction completion

In reply to Ross Lagerwall

To support merging the emulated instruction and VMEXIT paths, split out instruction completion from the core x86 emulation so it can be called separately.

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 48 +++++++++++++++-----------
 xen/arch/x86/x86_emulate/x86_emulate.h |  3 ++
 2 files changed, 30 insertions(+), 21 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +27 -21
index 364e6c092ded..76fbebf96b4d 100644
@@ -320,7 +320,7 @@ do {                                                                    \
     rc = ops->insn_fetch(ip, NULL, 0, ctxt);                            \
     if ( rc ) goto done;                                                \
     _regs.r(ip) = ip;                                                   \
-    singlestep = _regs.eflags & X86_EFLAGS_TF;                          \
+    ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;                    \
 } while (0)
 
 #define validate_far_branch(cs, ip) ({                                  \
@@ -337,7 +337,7 @@ do {                                                                    \
         ({                                                              \
             validate_far_branch(cs, newip);                             \
             _regs.r(ip) = (newip);                                      \
-            singlestep = _regs.eflags & X86_EFLAGS_TF;                  \
+            ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;            \
         }),                                                             \
         ops->write_segment(x86_seg_cs, cs, ctxt)                        \
     )
@@ -1234,6 +1234,22 @@ int cf_check x86emul_unhandleable_rw(
     return X86EMUL_UNHANDLEABLE;
 }
 
+void x86_emulate_complete(struct x86_emulate_ctxt *ctxt, int rc)
+{
+    /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
+    if ( !mode_64bit() )
+        ctxt->regs->r(ip) = (uint32_t)ctxt->regs->r(ip);
+
+    /* Should a singlestep #DB be raised? */
+    if ( rc == X86EMUL_OKAY && ctxt->singlestep && !ctxt->retire.mov_ss )
+    {
+        ctxt->retire.singlestep = true;
+        ctxt->retire.sti = false;
+    }
+
+    ctxt->regs->eflags &= ~X86_EFLAGS_RF;
+}
+
 /* Helper definitions. */
 #define op_bytes (state->op_bytes)
 #define ad_bytes (state->ad_bytes)
@@ -1265,8 +1281,6 @@ x86_emulate(
     uint8_t b, d, *opc = NULL;
     unsigned int first_byte = 0, elem_bytes, insn_bytes = 0;
     uint64_t op_mask = ~0ULL;
-    bool singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
-	    !is_branch_step(ctxt, ops);
     bool sfence = false, fault_suppression = false;
     struct operand src = { .reg = PTR_POISON };
     struct operand dst = { .reg = PTR_POISON };
@@ -1280,6 +1294,9 @@ x86_emulate(
 
     init_context(ctxt);
 
+    ctxt->singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
+	    !is_branch_step(ctxt, ops);
+
     generate_exception_if((mode_vif() &&
                            (_regs.eflags & X86_EFLAGS_VIF) &&
                            (_regs.eflags & X86_EFLAGS_VIP)),
@@ -2064,7 +2081,7 @@ x86_emulate(
             goto done;
 
         _regs.r(ip) = imm1;
-        singlestep = _regs.eflags & X86_EFLAGS_TF;
+        ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;
         break;
 
 #ifndef X86EMUL_NO_FPU
@@ -3103,7 +3120,7 @@ x86_emulate(
          * Their only mitigation is to use a task gate for handling
          * #DB (or to not use enable EFER.SCE to start with).
          */
-        singlestep = _regs.eflags & X86_EFLAGS_TF;
+        ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;
         break;
 
     case X86EMUL_OPC(0x0f, 0x06): /* clts */
@@ -3189,7 +3206,7 @@ x86_emulate(
                                       ctxt)) != X86EMUL_OKAY )
             goto done;
 
-        singlestep = _regs.eflags & X86_EFLAGS_TF;
+        ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;
         break;
 
     case X86EMUL_OPC(0x0f, 0x08): /* invd */
@@ -3918,7 +3935,7 @@ x86_emulate(
             /* In particular mode_64bit() needs to return true from here on. */
             ctxt->addr_size = ctxt->sp_size = 64;
 
-        singlestep = _regs.eflags & X86_EFLAGS_TF;
+        ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;
         break;
 
     case X86EMUL_OPC(0x0f, 0x35): /* sysexit */
@@ -3958,7 +3975,7 @@ x86_emulate(
         _regs.r(ip) = op_bytes == 8 ? _regs.r(dx) : _regs.edx;
         _regs.r(sp) = op_bytes == 8 ? _regs.r(cx) : _regs.ecx;
 
-        singlestep = _regs.eflags & X86_EFLAGS_TF;
+        ctxt->singlestep = _regs.eflags & X86_EFLAGS_TF;
         break;
 
     case X86EMUL_OPC(0x0f, 0x40) ... X86EMUL_OPC(0x0f, 0x4f): /* cmovcc */
@@ -8347,17 +8364,6 @@ x86_emulate(
     put_fpu(fpu_type, false, state, ctxt, ops);
     fpu_type = X86EMUL_FPU_none;
 
-    /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
-    if ( !mode_64bit() )
-        _regs.r(ip) = (uint32_t)_regs.r(ip);
-
-    /* Should a singlestep #DB be raised? */
-    if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss )
-    {
-        ctxt->retire.singlestep = true;
-        ctxt->retire.sti = false;
-    }
-
     if ( rc != X86EMUL_DONE )
         *ctxt->regs = _regs;
     else
@@ -8366,7 +8372,7 @@ x86_emulate(
         rc = X86EMUL_OKAY;
     }
 
-    ctxt->regs->eflags &= ~X86_EFLAGS_RF;
+    x86_emulate_complete(ctxt, rc);
 
  done:
     put_fpu(fpu_type, insn_bytes > 0 && dst.type == OP_MEM, state, ctxt, ops);
xen/arch/x86/x86_emulate/x86_emulate.h +3 -0
index 0fd20747dc43..17bf14455cf3 100644
@@ -594,6 +594,7 @@ struct x86_emulate_ctxt
     } retire;
 
     bool event_pending;
+    bool singlestep;
     struct x86_event event;
 };
 
@@ -680,6 +681,8 @@ x86_emulate(
     struct x86_emulate_ctxt *ctxt,
     const struct x86_emulate_ops *ops);
 
+void x86_emulate_complete(struct x86_emulate_ctxt *ctxt, int rc);
+
 #ifndef NDEBUG
 /*
  * In debug builds, wrap x86_emulate() with some assertions about its expected
Signature · 1 line
2.53.0
#6 lore raw

Re: [RFC v1 3/7] x86/emul: Separate out instruction completion

Quoted 3 lines · Ross Lagerwall

To support merging the emulated instruction and VMEXIT paths, split out instruction completion from the core x86 emulation so it can be called separately.

You don't mean to use a full-fledged struct x86_emulate_ctxt on the VMEXIT paths, do you?

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 1269

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -2
@@ -1265,8 +1281,6 @@ x86_emulate(
     uint8_t b, d, *opc = NULL;
     unsigned int first_byte = 0, elem_bytes, insn_bytes = 0;
     uint64_t op_mask = ~0ULL;
-    bool singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
-	    !is_branch_step(ctxt, ops);

Nit: Yes, indentation was screwed here. Please ...

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 1298

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -1280,6 +1294,9 @@ x86_emulate(
 
     init_context(ctxt);
 
+    ctxt->singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
+	    !is_branch_step(ctxt, ops);

... correct such when moving code around.

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 8352

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -3
@@ -8347,17 +8364,6 @@ x86_emulate(
     put_fpu(fpu_type, false, state, ctxt, ops);
     fpu_type = X86EMUL_FPU_none;
 
-    /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
-    if ( !mode_64bit() )
-        _regs.r(ip) = (uint32_t)_regs.r(ip);

While, because you have the new helper update ctxt->regs, the removal of this update of _regs looks technically okay, ...

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 8368

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -7
-    /* Should a singlestep #DB be raised? */
-    if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss )
-    {
-        ctxt->retire.singlestep = true;
-        ctxt->retire.sti = false;
-    }
-
     if ( rc != X86EMUL_DONE )
         *ctxt->regs = _regs;

... further uses of _regs here (not this one, but in general) would be at risk of no longer working as expected (yet only in a corner case, so possibly not covered by testing).

Jan

#7 lore raw

Re: [RFC v1 3/7] x86/emul: Separate out instruction completion

Quoted 7 lines · Jan Beulich
Quoted 3 lines · Ross Lagerwall

To support merging the emulated instruction and VMEXIT paths, split out instruction completion from the core x86 emulation so it can be called separately.

You don't mean to use a full-fledged struct x86_emulate_ctxt on the VMEXIT paths, do you?

For VMEXITs it uses a struct hvm_emulate_ctxt which contains struct x86_emulate_ctxt but without going through the full emulator. See patch 4 for details (probably makes sense to discuss this in the context of that patch).

Quoted 10 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 1269

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -2
@@ -1265,8 +1281,6 @@ x86_emulate(
      uint8_t b, d, *opc = NULL;
      unsigned int first_byte = 0, elem_bytes, insn_bytes = 0;
      uint64_t op_mask = ~0ULL;
-    bool singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
-	    !is_branch_step(ctxt, ops);

Nit: Yes, indentation was screwed here. Please ...

Quoted 8 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 1298

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -0
@@ -1280,6 +1294,9 @@ x86_emulate(
 
      init_context(ctxt);
 
+    ctxt->singlestep = (_regs.eflags & X86_EFLAGS_TF) &&
+	    !is_branch_step(ctxt, ops);

... correct such when moving code around.

OK.

Quoted 22 lines

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 8352

Quoted 8 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -3
@@ -8347,17 +8364,6 @@ x86_emulate(
      put_fpu(fpu_type, false, state, ctxt, ops);
      fpu_type = X86EMUL_FPU_none;
 
-    /* Zero the upper 32 bits of %rip if not in 64-bit mode. */
-    if ( !mode_64bit() )
-        _regs.r(ip) = (uint32_t)_regs.r(ip);

While, because you have the new helper update ctxt->regs, the removal of this update of _regs looks technically okay, ...

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 8368

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -7
-    /* Should a singlestep #DB be raised? */
-    if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss )
-    {
-        ctxt->retire.singlestep = true;
-        ctxt->retire.sti = false;
-    }
-
      if ( rc != X86EMUL_DONE )
          *ctxt->regs = _regs;
Quoted 3 lines

... further uses of _regs here (not this one, but in general) would be at risk of no longer working as expected (yet only in a corner case, so possibly not covered by testing).

Any uses of _regs after assignment back to the context is surely broken anyway because nothing outside of this function would see the result? Not that I would expect that to happen anyway since this is essentially the end of the function.

Ross

#8 lore raw

RFC 4/7 x86/hvm: Support a fast path during emulation

In reply to Ross Lagerwall

Information to complete the instruction may be provided by the processor during a VMEXIT. Add a fast path during emulation to use this information and avoid going through the full x86 emulator. Plumbing the fast path through the emulator ensures consistent behaviour.

---
 xen/arch/x86/hvm/emulate.c             | 160 ++++++++++++++++++-------
 xen/arch/x86/include/asm/hvm/emulate.h |   3 +
 2 files changed, 123 insertions(+), 40 deletions(-)
xen/arch/x86/hvm/emulate.c +120 -40
index a96d7814f91c..c9553cd28238 100644
@@ -2724,6 +2724,52 @@ static const struct x86_emulate_ops hvm_emulate_ops = {
     .vmfunc        = hvmemul_vmfunc,
 };
 
+static int hvm_emulate_insn_len(struct hvm_emulate_ctxt *hvmemul_ctxt)
+{
+    struct x86_emulate_state *state;
+    unsigned long emul_len;
+    unsigned int instr_opcode, instr_modrm;
+    unsigned int modrm_rm, modrm_reg;
+    int modrm_mod;
+
+    state = x86_decode_insn(&hvmemul_ctxt->ctxt, hvmemul_insn_fetch);
+    if ( IS_ERR_OR_NULL(state) )
+        return X86EMUL_EXCEPTION;
+
+    emul_len = x86_insn_length(state, &hvmemul_ctxt->ctxt);
+    modrm_mod = x86_insn_modrm(state, &modrm_rm, &modrm_reg);
+    x86_emulate_free_state(state);
+
+    /* Extract components from instr_enc. */
+    instr_modrm  = hvmemul_ctxt->insn & 0xff;
+    instr_opcode = hvmemul_ctxt->insn >> 8;
+
+    if ( instr_opcode == hvmemul_ctxt->ctxt.opcode )
+    {
+        if ( !instr_modrm )
+        {
+            hvmemul_ctxt->insn_len = emul_len;
+            return X86EMUL_OKAY;
+        }
+
+        if ( modrm_mod       == MASK_EXTR(instr_modrm, 0300) && /* octal-ok */
+             (modrm_reg & 7) == MASK_EXTR(instr_modrm, 0070) && /* octal-ok */
+             (modrm_rm  & 7) == MASK_EXTR(instr_modrm, 0007) )  /* octal-ok */
+        {
+            hvmemul_ctxt->insn_len = emul_len;
+            return X86EMUL_OKAY;
+        }
+    }
+
+    printk(XENLOG_G_WARNING
+           "Insn mismatch: Expected opcode %#x, modrm %#x, got emul_len %lu\n",
+           instr_opcode, instr_modrm, emul_len);
+    hvm_dump_emulation_state(XENLOG_G_WARNING, "Insn len",
+                             hvmemul_ctxt, X86EMUL_UNHANDLEABLE);
+
+    return X86EMUL_EXCEPTION;
+}
+
 /*
  * Note that passing VIO_no_completion into this function serves as kind
  * of (but not fully) an "auto select completion" indicator.  When there's
@@ -2733,63 +2779,97 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
     const struct x86_emulate_ops *ops,
     enum vio_completion completion)
 {
-    const struct cpu_user_regs *regs = hvmemul_ctxt->ctxt.regs;
+    struct cpu_user_regs *regs = hvmemul_ctxt->ctxt.regs;
     struct vcpu *curr = current;
     uint32_t new_intr_shadow;
     struct hvm_vcpu_io *hvio = &curr->arch.hvm.hvm_io;
     int rc;
 
-    /*
-     * Enable caching if it's currently disabled, but leave the cache
-     * untouched if it's already enabled, for re-execution to consume
-     * entries populated by an earlier pass.
-     */
-    if ( hvio->cache->num_ents > hvio->cache->max_ents )
+    if ( hvmemul_ctxt->insn )
     {
-        ASSERT(curr->io.req.state == STATE_IOREQ_NONE);
-        hvio->cache->num_ents = 0;
+        hvm_emulate_init_per_insn(hvmemul_ctxt, NULL, 0);
+        if ( !hvmemul_ctxt->insn_len )
+        {
+            rc = hvm_emulate_insn_len(hvmemul_ctxt);
+            if ( rc == X86EMUL_EXCEPTION )
+            {
+                x86_emul_hw_exception(X86_EXC_GP, 0, &hvmemul_ctxt->ctxt);
+                goto complete_insn;
+            }
+        }
+
+        switch ( hvmemul_ctxt->insn )
+        {
+        default:
+            ASSERT_UNREACHABLE();
+            rc = X86EMUL_UNHANDLEABLE;
+            break;
+        }
+
+        if ( rc == X86EMUL_OKAY )
+        {
+            regs->rip += hvmemul_ctxt->insn_len;
+            hvmemul_ctxt->ctxt.singlestep = regs->eflags & X86_EFLAGS_TF;
+            x86_emulate_complete(&hvmemul_ctxt->ctxt, rc);
+        }
     }
     else
-        ASSERT(curr->io.req.state == STATE_IORESP_READY);
+    {
+        /* No fast path. Go through full emulator */
+        /*
+         * Enable caching if it's currently disabled, but leave the cache
+         * untouched if it's already enabled, for re-execution to consume
+         * entries populated by an earlier pass.
+         */
+        if ( hvio->cache->num_ents > hvio->cache->max_ents )
+        {
+            ASSERT(curr->io.req.state == STATE_IOREQ_NONE);
+            hvio->cache->num_ents = 0;
+        }
+        else
+            ASSERT(curr->io.req.state == STATE_IORESP_READY);
 
-    hvm_emulate_init_per_insn(hvmemul_ctxt, hvio->mmio_insn,
-                              hvio->mmio_insn_bytes);
+        hvm_emulate_init_per_insn(hvmemul_ctxt, hvio->mmio_insn,
+                                  hvio->mmio_insn_bytes);
 
-    hvio->mmio_retry = 0;
+        hvio->mmio_retry = 0;
 
-    rc = x86_emulate(&hvmemul_ctxt->ctxt, ops);
-    if ( rc == X86EMUL_OKAY && hvio->mmio_retry )
-        rc = X86EMUL_RETRY;
+        rc = x86_emulate(&hvmemul_ctxt->ctxt, ops);
+        if ( rc == X86EMUL_OKAY && hvio->mmio_retry )
+            rc = X86EMUL_RETRY;
 
-    if ( !ioreq_needs_completion(&curr->io.req) )
-        completion = VIO_no_completion;
-    else if ( completion == VIO_no_completion )
-        completion = (curr->io.req.type != IOREQ_TYPE_PIO ||
-                      hvmemul_ctxt->is_mem_access) ? VIO_mmio_completion
-                                                   : VIO_pio_completion;
+        if ( !ioreq_needs_completion(&curr->io.req) )
+            completion = VIO_no_completion;
+        else if ( completion == VIO_no_completion )
+            completion = (curr->io.req.type != IOREQ_TYPE_PIO ||
+                          hvmemul_ctxt->is_mem_access) ? VIO_mmio_completion
+                                                       : VIO_pio_completion;
 
-    switch ( curr->io.completion = completion )
-    {
-    case VIO_no_completion:
-    case VIO_pio_completion:
-        hvio->mmio_cache_count = 0;
-        hvio->mmio_insn_bytes = 0;
-        hvio->mmio_access = (struct npfec){};
-        hvmemul_cache_disable(curr);
-        break;
+        switch ( curr->io.completion = completion )
+        {
+        case VIO_no_completion:
+        case VIO_pio_completion:
+            hvio->mmio_cache_count = 0;
+            hvio->mmio_insn_bytes = 0;
+            hvio->mmio_access = (struct npfec){};
+            hvmemul_cache_disable(curr);
+            break;
 
-    case VIO_mmio_completion:
-    case VIO_realmode_completion:
-        BUILD_BUG_ON(sizeof(hvio->mmio_insn) < sizeof(hvmemul_ctxt->insn_buf));
-        hvio->mmio_insn_bytes = hvmemul_ctxt->insn_buf_bytes;
-        memcpy(hvio->mmio_insn, hvmemul_ctxt->insn_buf, hvio->mmio_insn_bytes);
-        break;
+        case VIO_mmio_completion:
+        case VIO_realmode_completion:
+            BUILD_BUG_ON(sizeof(hvio->mmio_insn) < sizeof(hvmemul_ctxt->insn_buf));
+            hvio->mmio_insn_bytes = hvmemul_ctxt->insn_buf_bytes;
+            memcpy(hvio->mmio_insn, hvmemul_ctxt->insn_buf, hvio->mmio_insn_bytes);
+            break;
 
-    default:
-        ASSERT_UNREACHABLE();
-        return X86EMUL_UNHANDLEABLE;
+        default:
+            ASSERT_UNREACHABLE();
+            return X86EMUL_UNHANDLEABLE;
+        }
     }
 
+ complete_insn:
+
     if ( hvmemul_ctxt->ctxt.retire.singlestep )
         hvm_inject_hw_exception(X86_EXC_DB, X86_EVENT_NO_EC);
 
xen/arch/x86/include/asm/hvm/emulate.h +3 -0
index 084e2bd90588..1e626f8af8f2 100644
@@ -53,6 +53,9 @@ struct hvm_emulate_ctxt {
     bool is_mem_access;
 
     bool set_context;
+
+    unsigned int insn;
+    unsigned int insn_len;
 };
 
 enum emul_kind {
Signature · 1 line
2.53.0
#9 lore raw

RFC 5/7 x86/hvm: Move INSTR_* constants to hvm.h

In reply to Ross Lagerwall

These aren't specific to SVM and will be used for emulator fast path so move them to hvm.h.

---
 xen/arch/x86/hvm/svm/svm.h         | 29 -----------------------------
 xen/arch/x86/include/asm/hvm/hvm.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 29 deletions(-)
xen/arch/x86/hvm/svm/svm.h +0 -29
index cfa411ad5ae1..f75bca7c5f66 100644
@@ -36,35 +36,6 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
     asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
 }
 
-/*
- * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
- * opcode, shifted left to make room for the ModRM byte.
- *
- * The Grp7 instructions have their ModRM byte expressed in octal for easier
- * cross referencing with the opcode extension table.
- */
-#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
-
-#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
-#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
-#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
-#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
-#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
-#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
-#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
-#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
-#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
-#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
-#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
-#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
-#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
-#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
-#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
-#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
-#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
-#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
-#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
-
 unsigned int svm_get_insn_len(struct vcpu *v, unsigned int instr_enc);
 unsigned int svm_get_task_switch_insn_len(void);
 
xen/arch/x86/include/asm/hvm/hvm.h +29 -0
index e7c1364802f8..a9b64814ca71 100644
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
         alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
 }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.
+ *
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
 #else  /* CONFIG_HVM */
 
 #define hvm_enabled false
Signature · 1 line
2.53.0
#10 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

Quoted 2 lines · Ross Lagerwall

These aren't specific to SVM and will be used for emulator fast path so move them to hvm.h.

Some are SVM-specific, e.g. ...

Quoted 17 lines
xen/arch/x86/hvm/svm/svm.h +0 -11
@@ -36,35 +36,6 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
     asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
 }
 
-/*
- * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
- * opcode, shifted left to make room for the ModRM byte.
- *
- * The Grp7 instructions have their ModRM byte expressed in octal for easier
- * cross referencing with the opcode extension table.
- */
-#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
-
-#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
-#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)

xen/arch/x86/hvm/svm/svm.h static inline void svm_invlpga(unsigned long linear, uint32_t asid) · line 59

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.h +0 -10
-#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
-#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
-#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
-#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
-#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
-#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
-#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
-#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
-#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
-#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */

... the 7 ones above.

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 856

Quoted 9 lines
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
         alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
 }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.

With all of this moved, the comment wants adjusting, at the very least by putting "e.g." in front of the function name.

Quoted 16 lines
xen/arch/x86/include/asm/hvm/hvm.h +16 -0
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 883

Quoted 10 lines
xen/arch/x86/include/asm/hvm/hvm.h +9 -0
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
 #else  /* CONFIG_HVM */

I further wonder whether putting this in hvm.h is a good idea. Is there anything wrong with using a brand new header, e.g. instr-enc.h?

Jan

#11 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

Quoted 70 lines · Jan Beulich
Quoted 2 lines · Ross Lagerwall

These aren't specific to SVM and will be used for emulator fast path so move them to hvm.h.

Some are SVM-specific, e.g. ...

Quoted 17 lines
xen/arch/x86/hvm/svm/svm.h +0 -11
@@ -36,35 +36,6 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
      asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
  }
 
-/*
- * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
- * opcode, shifted left to make room for the ModRM byte.
- *
- * The Grp7 instructions have their ModRM byte expressed in octal for easier
- * cross referencing with the opcode extension table.
- */
-#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
-
-#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
-#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)

xen/arch/x86/hvm/svm/svm.h static inline void svm_invlpga(unsigned long linear, uint32_t asid) · line 59

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.h +0 -10
-#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
-#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
-#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
-#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
-#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
-#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
-#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
-#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
-#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
-#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */

... the 7 ones above.

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 856

Quoted 9 lines
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
          alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
  }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.

With all of this moved, the comment wants adjusting, at the very least by putting "e.g." in front of the function name.

Quoted 21 lines
xen/arch/x86/include/asm/hvm/hvm.h +21 -0
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
Quoted 8 lines

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 883

Quoted 5 lines
xen/arch/x86/include/asm/hvm/hvm.h +4 -0
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
  #else  /* CONFIG_HVM */

I further wonder whether putting this in hvm.h is a good idea. Is there anything wrong with using a brand new header, e.g. instr-enc.h?

No objection to that. I do wonder though if using the instruction encoding like this is the best way of passing through the instruction to the fast path in hvm_emulate_one_ctxt() since I think in some cases the instruction encoding may not match the actual instruction that triggered the VMEXIT.

Ross

#12 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

Quoted 45 lines · Ross Lagerwall
Quoted 35 lines · Jan Beulich

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 856

Quoted 9 lines · Ross Lagerwall
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
          alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
  }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.

With all of this moved, the comment wants adjusting, at the very least by putting "e.g." in front of the function name.

Quoted 21 lines
xen/arch/x86/include/asm/hvm/hvm.h +21 -0
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
Quoted 8 lines

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 883

Quoted 5 lines
xen/arch/x86/include/asm/hvm/hvm.h +4 -0
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
  #else  /* CONFIG_HVM */

I further wonder whether putting this in hvm.h is a good idea. Is there anything wrong with using a brand new header, e.g. instr-enc.h?

Quoted 4 lines

No objection to that. I do wonder though if using the instruction encoding like this is the best way of passing through the instruction to the fast path in hvm_emulate_one_ctxt() since I think in some cases the instruction encoding may not match the actual instruction that triggered the VMEXIT.

Do you have an example? If so, that would indeed be at risk of being misleading or actively confusing. (Of course INSTR_VMCALL wants renaming, as was already suggested.)

Jan

#13 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

Quoted 46 lines · Jan Beulich
Quoted 45 lines · Ross Lagerwall
Quoted 43 lines · Jan Beulich

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 856

Quoted 9 lines · Ross Lagerwall
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
           alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
   }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.

With all of this moved, the comment wants adjusting, at the very least by putting "e.g." in front of the function name.

Quoted 16 lines
xen/arch/x86/include/asm/hvm/hvm.h +16 -0
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 883

Quoted 10 lines
xen/arch/x86/include/asm/hvm/hvm.h +9 -0
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
   #else  /* CONFIG_HVM */

I further wonder whether putting this in hvm.h is a good idea. Is there anything wrong with using a brand new header, e.g. instr-enc.h?

Quoted 9 lines
Quoted 4 lines

No objection to that. I do wonder though if using the instruction encoding like this is the best way of passing through the instruction to the fast path in hvm_emulate_one_ctxt() since I think in some cases the instruction encoding may not match the actual instruction that triggered the VMEXIT.

Do you have an example? If so, that would indeed be at risk of being misleading or actively confusing. (Of course INSTR_VMCALL wants renaming, as was already suggested.)

VMEXIT_CR0_READ may be triggered by MOV-from-CR or SMSW. There are probably other examples...

Ross

#14 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

Quoted 52 lines · Ross Lagerwall
Quoted 51 lines · Jan Beulich
Quoted 49 lines · Ross Lagerwall
Quoted 35 lines · Jan Beulich

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 856

Quoted 9 lines · Ross Lagerwall
xen/arch/x86/include/asm/hvm/hvm.h +3 -0
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
           alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
   }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.

With all of this moved, the comment wants adjusting, at the very least by putting "e.g." in front of the function name.

Quoted 21 lines
xen/arch/x86/include/asm/hvm/hvm.h +21 -0
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
Quoted 8 lines

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 883

Quoted 5 lines
xen/arch/x86/include/asm/hvm/hvm.h +4 -0
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
   #else  /* CONFIG_HVM */

I further wonder whether putting this in hvm.h is a good idea. Is there anything wrong with using a brand new header, e.g. instr-enc.h?

No objection to that. I do wonder though if using the instruction encoding like this is the best way of passing through the instruction to the fast path in hvm_emulate_one_ctxt() since I think in some cases the instruction encoding may not match the actual instruction that triggered the VMEXIT.

Quoted 7 lines
Quoted 4 lines

Do you have an example? If so, that would indeed be at risk of being misleading or actively confusing. (Of course INSTR_VMCALL wants renaming, as was already suggested.)

VMEXIT_CR0_READ may be triggered by MOV-from-CR or SMSW. There are probably other examples...

Like LMSW, then also relevant to VMX. I think these indeed better wouldn't use the wrong INSTR_*. On SVM with decode assists, SMSW can be identified, but LMSW / CLTS are ambiguous without fetching the insn.

Jan

#15 lore raw

Re: [RFC v1 5/7] x86/hvm: Move INSTR_* constants to hvm.h

In reply to Ross Lagerwall

Hello,

Le 18/05/2026 à 15:14, Ross Lagerwall a écrit :

Quoted 63 lines

These aren't specific to SVM and will be used for emulator fast path so move them to hvm.h.

---
  xen/arch/x86/hvm/svm/svm.h         | 29 -----------------------------
  xen/arch/x86/include/asm/hvm/hvm.h | 29 +++++++++++++++++++++++++++++
  2 files changed, 29 insertions(+), 29 deletions(-)
xen/arch/x86/hvm/svm/svm.h +0 -29
index cfa411ad5ae1..f75bca7c5f66 100644
@@ -36,35 +36,6 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
      asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
  }
 
-/*
- * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
- * opcode, shifted left to make room for the ModRM byte.
- *
- * The Grp7 instructions have their ModRM byte expressed in octal for easier
- * cross referencing with the opcode extension table.
- */
-#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
-
-#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
-#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
-#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
-#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
-#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
-#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
-#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */
-#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
-#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
-#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
-#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
-#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
-#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */
-#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
-#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
-#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
-#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
-#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
-#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
-
  unsigned int svm_get_insn_len(struct vcpu *v, unsigned int instr_enc);
  unsigned int svm_get_task_switch_insn_len(void);
 
xen/arch/x86/include/asm/hvm/hvm.h +6 -0
index e7c1364802f8..a9b64814ca71 100644
@@ -851,6 +851,35 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
          alternative_vcall(hvm_funcs.sync_pir_to_irr, v);
  }
 
+/*
+ * Encoding for svm_get_insn_len().  We take X86EMUL_OPC() for the main
+ * opcode, shifted left to make room for the ModRM byte.
+ *
+ * The Grp7 instructions have their ModRM byte expressed in octal for easier
+ * cross referencing with the opcode extension table.

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 869

Quoted 10 lines
xen/arch/x86/include/asm/hvm/hvm.h +10 -0
+ */
+#define INSTR_ENC(opc, modrm) (((opc) << 8) | (modrm))
+
+#define INSTR_PAUSE      INSTR_ENC(X86EMUL_OPC_F3(0, 0x90), 0)
+#define INSTR_INT3       INSTR_ENC(X86EMUL_OPC(   0, 0xcc), 0)
+#define INSTR_ICEBP      INSTR_ENC(X86EMUL_OPC(   0, 0xf1), 0)
+#define INSTR_HLT        INSTR_ENC(X86EMUL_OPC(   0, 0xf4), 0)
+#define INSTR_XSETBV     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0321) /* octal-ok */
+#define INSTR_VMRUN      INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0330) /* octal-ok */
+#define INSTR_VMCALL     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0331) /* octal-ok */

I assume a part of the plan is to use these constants for VMX as well.
Hence, you very likely want to split VMCALL (VMX) and VMMCALL (SVM) encodings (which is oddly named INSTR_VMCALL despite actually being actually vmmcall encoding).

Quoted 6 lines
xen/arch/x86/include/asm/hvm/hvm.h +6 -0
+#define INSTR_VMLOAD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0332) /* octal-ok */
+#define INSTR_VMSAVE     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0333) /* octal-ok */
+#define INSTR_STGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0334) /* octal-ok */
+#define INSTR_CLGI       INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0335) /* octal-ok */
+#define INSTR_INVLPGA    INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0337) /* octal-ok */
+#define INSTR_RDTSCP     INSTR_ENC(X86EMUL_OPC(0x0f, 0x01), 0371) /* octal-ok */

xen/arch/x86/include/asm/hvm/hvm.h static inline void hvm_sync_pir_to_irr(struct vcpu *v) · line 885

Quoted 10 lines
xen/arch/x86/include/asm/hvm/hvm.h +7 -0
+#define INSTR_INVD       INSTR_ENC(X86EMUL_OPC(0x0f, 0x08), 0)
+#define INSTR_WBINVD     INSTR_ENC(X86EMUL_OPC(0x0f, 0x09), 0)
+#define INSTR_WRMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x30), 0)
+#define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
+#define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
+#define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+
  #else  /* CONFIG_HVM */
 
  #define hvm_enabled false

Teddy

#16 lore raw

RFC 6/7 x86/svm: Use the emulator path for VMEXIT_HLT

In reply to Ross Lagerwall
---
 xen/arch/x86/hvm/emulate.c     |  5 +++++
 xen/arch/x86/hvm/svm/emulate.c |  2 +-
 xen/arch/x86/hvm/svm/svm.c     | 24 +++++++++++-------------
 xen/arch/x86/hvm/svm/svm.h     |  1 +
 4 files changed, 18 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/emulate.c +5 -0
index c9553cd28238..471c032c1e9c 100644
@@ -2800,6 +2800,11 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
 
         switch ( hvmemul_ctxt->insn )
         {
+        case INSTR_HLT:
+            hvmemul_ctxt->ctxt.retire.hlt = true;
+            rc = X86EMUL_OKAY;
+            break;
+
         default:
             ASSERT_UNREACHABLE();
             rc = X86EMUL_UNHANDLEABLE;
xen/arch/x86/hvm/svm/emulate.c +1 -1
index 1dd830a31bd7..31f3cd88a858 100644
@@ -16,7 +16,7 @@
 #include "svm.h"
 #include "vmcb.h"
 
-static unsigned long svm_nextrip_insn_length(struct vcpu *v)
+unsigned long svm_nextrip_insn_length(struct vcpu *v)
 {
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
 
xen/arch/x86/hvm/svm/svm.c +11 -13
index f49d2ebbfdd5..2d6022d6238c 100644
@@ -2051,18 +2051,6 @@ static void svm_do_msr_access(struct cpu_user_regs *regs)
         hvm_inject_hw_exception(X86_EXC_GP, 0);
 }
 
-static void svm_vmexit_do_hlt(struct vmcb_struct *vmcb,
-                              struct cpu_user_regs *regs)
-{
-    unsigned int inst_len;
-
-    if ( (inst_len = svm_get_insn_len(current, INSTR_HLT)) == 0 )
-        return;
-    __update_guest_eip(regs, inst_len);
-
-    hvm_hlt(regs->eflags);
-}
-
 static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs, bool rdtscp)
 {
     struct vcpu *curr = current;
@@ -2363,6 +2351,13 @@ static void cf_check svm_set_reg(struct vcpu *v, unsigned int reg, uint64_t val)
     }
 }
 
+static void svm_emulate_one(struct hvm_emulate_ctxt *ctxt)
+{
+    ctxt->insn_len = svm_nextrip_insn_length(current);
+    if ( !hvm_emulate_one_ctxt(ctxt) )
+        hvm_inject_hw_exception(X86_EXC_GP, 0);
+}
+
 static struct hvm_function_table __initdata_cf_clobber svm_function_table = {
     .name                 = "SVM",
     .cpu_up_prepare       = svm_cpu_up_prepare,
@@ -2496,6 +2491,7 @@ void asmlinkage svm_vmexit_handler(void)
     vintr_t intr;
     bool vcpu_guestmode = false;
     struct vlapic *vlapic = vcpu_vlapic(v);
+    struct hvm_emulate_ctxt ctxt;
 
     regs->rax = vmcb->rax;
     regs->rip = vmcb->rip;
@@ -2840,7 +2836,9 @@ void asmlinkage svm_vmexit_handler(void)
     }
 
     case VMEXIT_HLT:
-        svm_vmexit_do_hlt(vmcb, regs);
+        hvm_emulate_init_once(&ctxt, NULL, NULL, regs);
+        ctxt.insn = INSTR_HLT;
+        svm_emulate_one(&ctxt);
         break;
 
     case VMEXIT_IOIO:
xen/arch/x86/hvm/svm/svm.h +1 -0
index f75bca7c5f66..9422dbd38a78 100644
@@ -36,6 +36,7 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
     asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
 }
 
+unsigned long svm_nextrip_insn_length(struct vcpu *v);
 unsigned int svm_get_insn_len(struct vcpu *v, unsigned int instr_enc);
 unsigned int svm_get_task_switch_insn_len(void);
 
Signature · 1 line
2.53.0
#17 lore raw

Re: [RFC v1 6/7] x86/svm: Use the emulator path for VMEXIT_HLT

Hello,

Here is my feedback from a SEV-ES implementation perspective, as it has specific needs related to some VMEXIT handling.

Le 18/05/2026 à 15:14, Ross Lagerwall a écrit :

Quoted 83 lines
---
  xen/arch/x86/hvm/emulate.c     |  5 +++++
  xen/arch/x86/hvm/svm/emulate.c |  2 +-
  xen/arch/x86/hvm/svm/svm.c     | 24 +++++++++++-------------
  xen/arch/x86/hvm/svm/svm.h     |  1 +
  4 files changed, 18 insertions(+), 14 deletions(-)
xen/arch/x86/hvm/emulate.c +5 -0
index c9553cd28238..471c032c1e9c 100644
@@ -2800,6 +2800,11 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
 
          switch ( hvmemul_ctxt->insn )
          {
+        case INSTR_HLT:
+            hvmemul_ctxt->ctxt.retire.hlt = true;
+            rc = X86EMUL_OKAY;
+            break;
+
          default:
              ASSERT_UNREACHABLE();
              rc = X86EMUL_UNHANDLEABLE;
xen/arch/x86/hvm/svm/emulate.c +1 -1
index 1dd830a31bd7..31f3cd88a858 100644
@@ -16,7 +16,7 @@
  #include "svm.h"
  #include "vmcb.h"
 
-static unsigned long svm_nextrip_insn_length(struct vcpu *v)
+unsigned long svm_nextrip_insn_length(struct vcpu *v)
  {
      struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
 
xen/arch/x86/hvm/svm/svm.c +8 -12
index f49d2ebbfdd5..2d6022d6238c 100644
@@ -2051,18 +2051,6 @@ static void svm_do_msr_access(struct cpu_user_regs *regs)
          hvm_inject_hw_exception(X86_EXC_GP, 0);
  }
 
-static void svm_vmexit_do_hlt(struct vmcb_struct *vmcb,
-                              struct cpu_user_regs *regs)
-{
-    unsigned int inst_len;
-
-    if ( (inst_len = svm_get_insn_len(current, INSTR_HLT)) == 0 )
-        return;
-    __update_guest_eip(regs, inst_len);
-
-    hvm_hlt(regs->eflags);
-}
-
  static void svm_vmexit_do_rdtsc(struct cpu_user_regs *regs, bool rdtscp)
  {
      struct vcpu *curr = current;
@@ -2363,6 +2351,13 @@ static void cf_check svm_set_reg(struct vcpu *v, unsigned int reg, uint64_t val)
      }
  }
 
+static void svm_emulate_one(struct hvm_emulate_ctxt *ctxt)
+{
+    ctxt->insn_len = svm_nextrip_insn_length(current);
+    if ( !hvm_emulate_one_ctxt(ctxt) )
+        hvm_inject_hw_exception(X86_EXC_GP, 0);
+}
+
  static struct hvm_function_table __initdata_cf_clobber svm_function_table = {
      .name                 = "SVM",
      .cpu_up_prepare       = svm_cpu_up_prepare,
@@ -2496,6 +2491,7 @@ void asmlinkage svm_vmexit_handler(void)
      vintr_t intr;
      bool vcpu_guestmode = false;
      struct vlapic *vlapic = vcpu_vlapic(v);
+    struct hvm_emulate_ctxt ctxt;
 
      regs->rax = vmcb->rax;
      regs->rip = vmcb->rip;

xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2842

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +3 -1
@@ -2840,7 +2836,9 @@ void asmlinkage svm_vmexit_handler(void)
      }
 
      case VMEXIT_HLT:
-        svm_vmexit_do_hlt(vmcb, regs);
+        hvm_emulate_init_once(&ctxt, NULL, NULL, regs);
+        ctxt.insn = INSTR_HLT;
+        svm_emulate_one(&ctxt);
          break;

In this case, most of the logic is hidden behind svm_emulate_one(); however, SEV-ES changes some aspects of the VMEXIT_HLT behavior (this is the same for e.g VMEXIT_PAUSE).

With SEV-ES, we can't access the CPU registers anymore but hlt can still be intercepted (it's a "Automatic Exit (AE)"), in this case, the CPU increases RIP itself (this is documented in SEV-ES section of the APM), and we just have to emulate the HLT behavior. How would that specific behavior fit in this new design ?

We can skip increasing rip in this specific case, but it's now common code.

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
      case VMEXIT_IOIO:
xen/arch/x86/hvm/svm/svm.h +0 -0
index f75bca7c5f66..9422dbd38a78 100644

xen/arch/x86/hvm/svm/svm.h static inline void svm_invlpga(unsigned long linear, uint32_t asid) · line 42

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.h +1 -0
@@ -36,6 +36,7 @@ static inline void svm_invlpga(unsigned long linear, uint32_t asid)
      asm volatile ( "invlpga" :: "a" (linear), "c" (asid) );
  }
 
+unsigned long svm_nextrip_insn_length(struct vcpu *v);
  unsigned int svm_get_insn_len(struct vcpu *v, unsigned int instr_enc);
  unsigned int svm_get_task_switch_insn_len(void);
 

Teddy

#18 lore raw

Re: [RFC v1 6/7] x86/svm: Use the emulator path for VMEXIT_HLT

Quoted 24 lines · Teddy Astie

Le 18/05/2026 à 15:14, Ross Lagerwall a écrit :

Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2496,6 +2491,7 @@ void asmlinkage svm_vmexit_handler(void)
      vintr_t intr;
      bool vcpu_guestmode = false;
      struct vlapic *vlapic = vcpu_vlapic(v);
+    struct hvm_emulate_ctxt ctxt;
 
      regs->rax = vmcb->rax;
      regs->rip = vmcb->rip;

xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2842

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +3 -1
@@ -2840,7 +2836,9 @@ void asmlinkage svm_vmexit_handler(void)
      }
 
      case VMEXIT_HLT:
-        svm_vmexit_do_hlt(vmcb, regs);
+        hvm_emulate_init_once(&ctxt, NULL, NULL, regs);
+        ctxt.insn = INSTR_HLT;
+        svm_emulate_one(&ctxt);
          break;

In this case, most of the logic is hidden behind svm_emulate_one(); however, SEV-ES changes some aspects of the VMEXIT_HLT behavior (this is the same for e.g VMEXIT_PAUSE).

Quoted 7 lines

With SEV-ES, we can't access the CPU registers anymore but hlt can still be intercepted (it's a "Automatic Exit (AE)"), in this case, the CPU increases RIP itself (this is documented in SEV-ES section of the APM), and we just have to emulate the HLT behavior. How would that specific behavior fit in this new design ?

We can skip increasing rip in this specific case, but it's now common code.

I guess we may need to somehow reflect fault vs trap semantics in the context, for RIP updating to only occur in the former case.

Jan

#19 lore raw

Re: [RFC v1 6/7] x86/svm: Use the emulator path for VMEXIT_HLT

Quoted 30 lines · Jan Beulich
Quoted 29 lines · Teddy Astie

Le 18/05/2026 à 15:14, Ross Lagerwall a écrit :

Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +1 -0
@@ -2496,6 +2491,7 @@ void asmlinkage svm_vmexit_handler(void)
      vintr_t intr;
      bool vcpu_guestmode = false;
      struct vlapic *vlapic = vcpu_vlapic(v);
+    struct hvm_emulate_ctxt ctxt;
 
      regs->rax = vmcb->rax;
      regs->rip = vmcb->rip;

xen/arch/x86/hvm/svm/svm.c void asmlinkage svm_vmexit_handler(void) · line 2842

Quoted 8 lines
xen/arch/x86/hvm/svm/svm.c +3 -1
@@ -2840,7 +2836,9 @@ void asmlinkage svm_vmexit_handler(void)
      }
 
      case VMEXIT_HLT:
-        svm_vmexit_do_hlt(vmcb, regs);
+        hvm_emulate_init_once(&ctxt, NULL, NULL, regs);
+        ctxt.insn = INSTR_HLT;
+        svm_emulate_one(&ctxt);
          break;

In this case, most of the logic is hidden behind svm_emulate_one(); however, SEV-ES changes some aspects of the VMEXIT_HLT behavior (this is the same for e.g VMEXIT_PAUSE).

With SEV-ES, we can't access the CPU registers anymore but hlt can still be intercepted (it's a "Automatic Exit (AE)"), in this case, the CPU increases RIP itself (this is documented in SEV-ES section of the APM), and we just have to emulate the HLT behavior. How would that specific behavior fit in this new design ?

Quoted 3 lines
Quoted 1 line

We can skip increasing rip in this specific case, but it's now common code.

I guess we may need to somehow reflect fault vs trap semantics in the context, for RIP updating to only occur in the former case.

Indeed.  There are APIC acceleration exits which have trap semantics after the write into the APIC page has completed.

For these, we need to drive the LAPIC machinery, not move RIP, but still deal with things like singlestep.

I'm not even sure what the correct behaviour for blocked-by-sti/movss would be here but I suspect they can be set and would want dropping too.  The point of these VMEXIT is to let the hypervisor take an action that's still logically part of executing the instruction, and that completes prior to the shadows dropping.

~Andrew

#20 lore raw

RFC 7/7 x86/svm: Use the emulator path for VMEXIT_CR*

In reply to Ross Lagerwall

If the processor has provided extra information, use that to accelerate the emulator path.

The hvmemul_{read,write}_cr callbacks are used for consistent behaviour between emulated and fast paths.

---
 xen/arch/x86/hvm/emulate.c             | 11 ++++++++++
 xen/arch/x86/hvm/svm/svm.c             | 28 +++++++++-----------------
 xen/arch/x86/include/asm/hvm/emulate.h |  7 +++++++
 xen/arch/x86/include/asm/hvm/hvm.h     |  2 ++
 4 files changed, 29 insertions(+), 19 deletions(-)
xen/arch/x86/hvm/emulate.c +11 -0
index 471c032c1e9c..0f7507575884 100644
@@ -2804,6 +2804,17 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
             hvmemul_ctxt->ctxt.retire.hlt = true;
             rc = X86EMUL_OKAY;
             break;
+        case INSTR_MOV_FROM_CR:
+        case INSTR_MOV_TO_CR:
+        {
+            unsigned long *val = decode_gpr(regs, hvmemul_ctxt->u.cr.gp);
+
+            if ( hvmemul_ctxt->insn == INSTR_MOV_TO_CR )
+                rc = hvmemul_write_cr(hvmemul_ctxt->u.cr.cr, *val, &hvmemul_ctxt->ctxt);
+            else
+                rc = hvmemul_read_cr(hvmemul_ctxt->u.cr.cr, val, &hvmemul_ctxt->ctxt);
+            break;
+        }
 
         default:
             ASSERT_UNREACHABLE();
xen/arch/x86/hvm/svm/svm.c +9 -19
index 2d6022d6238c..3ba852f52666 100644
@@ -1625,22 +1625,6 @@ static void svm_do_nested_pgfault(struct vcpu *v,
     domain_crash(v->domain);
 }
 
-static void svm_vmexit_do_cr_access(
-    struct vmcb_struct *vmcb, struct cpu_user_regs *regs)
-{
-    int gp, cr, dir, rc;
-
-    cr = vmcb->exitcode - VMEXIT_CR0_READ;
-    dir = (cr > 15);
-    cr &= 0xf;
-    gp = vmcb->ei.mov_cr.gpr;
-
-    rc = dir ? hvm_mov_to_cr(cr, gp) : hvm_mov_from_cr(cr, gp);
-
-    if ( rc == X86EMUL_OKAY )
-        __update_guest_eip(regs, vmcb->nextrip - vmcb->rip);
-}
-
 static void svm_dr_access(struct vcpu *v, struct cpu_user_regs *regs)
 {
     struct vmcb_struct *vmcb = vcpu_nestedhvm(v).nv_n1vmcx;
@@ -2869,10 +2853,16 @@ void asmlinkage svm_vmexit_handler(void)
 
     case VMEXIT_CR0_READ ... VMEXIT_CR15_READ:
     case VMEXIT_CR0_WRITE ... VMEXIT_CR15_WRITE:
+        hvm_emulate_init_once(&ctxt, x86_insn_is_cr_access, "CR access", regs);
+
         if ( cpu_has_svm_decode && vmcb->ei.mov_cr.mov_insn )
-            svm_vmexit_do_cr_access(vmcb, regs);
-        else if ( !hvm_emulate_one_insn(x86_insn_is_cr_access, "CR access") )
-            hvm_inject_hw_exception(X86_EXC_GP, 0);
+        {
+            ctxt.u.cr.cr = vmcb->exitcode - VMEXIT_CR0_READ;
+            ctxt.insn = (ctxt.u.cr.cr > 15) ? INSTR_MOV_TO_CR : INSTR_MOV_FROM_CR;
+            ctxt.u.cr.cr = ctxt.u.cr.cr & 0xf;
+            ctxt.u.cr.gp = vmcb->ei.mov_cr.gpr;
+        }
+        svm_emulate_one(&ctxt);
         break;
 
     case VMEXIT_INVLPG:
xen/arch/x86/include/asm/hvm/emulate.h +7 -0
index 1e626f8af8f2..3661ca28d6f4 100644
@@ -56,6 +56,13 @@ struct hvm_emulate_ctxt {
 
     unsigned int insn;
     unsigned int insn_len;
+
+    union {
+        struct {
+            int cr;
+            int gp;
+        } cr;
+    } u;
 };
 
 enum emul_kind {
xen/arch/x86/include/asm/hvm/hvm.h +2 -0
index a9b64814ca71..7732217e279a 100644
@@ -879,6 +879,8 @@ static inline void hvm_sync_pir_to_irr(struct vcpu *v)
 #define INSTR_RDTSC      INSTR_ENC(X86EMUL_OPC(0x0f, 0x31), 0)
 #define INSTR_RDMSR      INSTR_ENC(X86EMUL_OPC(0x0f, 0x32), 0)
 #define INSTR_CPUID      INSTR_ENC(X86EMUL_OPC(0x0f, 0xa2), 0)
+#define INSTR_MOV_FROM_CR INSTR_ENC(X86EMUL_OPC(0x0f, 0x20), 0)
+#define INSTR_MOV_TO_CR  INSTR_ENC(X86EMUL_OPC(0x0f, 0x22), 0)
 
 #else  /* CONFIG_HVM */
 
Signature · 1 line
2.53.0