Skip to contentSkip to conversation

x86_emulate adjustments

PATCH In review Jan Beulich 2 participants 7 messages Jan 5, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_emulate adjustments

- don't provide cmpxchg8b emulation where not needed (i.e. page table ops on 64-bit hv)
- properly deal with stack operands (push/pop)
- synchronize prefix handling with hvm's instrlen determination and about to be committed privileged op decoder (changes coming with the 32on64 patches)
- support cmpxchg16b if the CPU supports it
- a couple of minor adjustments

xen/arch/x86/mm.c +5 -1
Index: 2006-12-18/xen/arch/x86/mm.c
===================================================================
@@ -3240,6 +3240,7 @@ static int ptwr_emulated_cmpxchg(
         container_of(ctxt, struct ptwr_emulate_ctxt, ctxt));
 }
 
+#ifdef __i386__
 static int ptwr_emulated_cmpxchg8b(
     enum x86_segment seg,
     unsigned long offset,
@@ -3255,13 +3256,16 @@ static int ptwr_emulated_cmpxchg8b(
         offset, ((u64)old_hi << 32) | old, ((u64)new_hi << 32) | new, 8, 1,
         container_of(ctxt, struct ptwr_emulate_ctxt, ctxt));
 }
+#endif
 
 static struct x86_emulate_ops ptwr_emulate_ops = {
     .read       = ptwr_emulated_read,
     .insn_fetch = ptwr_emulated_read,
     .write      = ptwr_emulated_write,
     .cmpxchg    = ptwr_emulated_cmpxchg,
-    .cmpxchg8b  = ptwr_emulated_cmpxchg8b
+#ifdef __i386__
+    .cmpxchg2   = ptwr_emulated_cmpxchg8b
+#endif
 };
 
 /* Write page fault handler: check if guest is trying to modify a PTE. */
xen/arch/x86/mm/shadow/common.c +25 -2
Index: 2006-12-18/xen/arch/x86/mm/shadow/common.c
===================================================================
@@ -291,6 +291,7 @@ hvm_emulate_cmpxchg(enum x86_segment seg
         v, addr, old, new, bytes, sh_ctxt);
 }
 
+#ifdef __i386__
 static int 
 hvm_emulate_cmpxchg8b(enum x86_segment seg,
                       unsigned long offset,
@@ -314,13 +315,31 @@ hvm_emulate_cmpxchg8b(enum x86_segment s
     return v->arch.shadow.mode->x86_emulate_cmpxchg8b(
         v, addr, old_lo, old_hi, new_lo, new_hi, sh_ctxt);
 }
+#endif
+
+static unsigned int
+hvm_stack_word_size(struct x86_emulate_ctxt *ctxt)
+{
+#ifdef __x86_64__
+    if ( ctxt->mode == X86EMUL_MODE_PROT64 )
+        return 8;
+#endif
+
+    return hvm_get_seg_reg(x86_seg_ss,
+                           container_of(ctxt,
+                                        struct sh_emulate_ctxt,
+                                        ctxt))->attr.fields.db ? 4 : 2;
+}
 
 static struct x86_emulate_ops hvm_shadow_emulator_ops = {
     .read       = hvm_emulate_read,
     .insn_fetch = hvm_emulate_insn_fetch,
     .write      = hvm_emulate_write,
     .cmpxchg    = hvm_emulate_cmpxchg,
-    .cmpxchg8b  = hvm_emulate_cmpxchg8b,
+#ifdef __i386__
+    .cmpxchg2   = hvm_emulate_cmpxchg8b,
+#endif
+    .stksz      = hvm_stack_word_size
 };
 
 static int
@@ -371,6 +390,7 @@ pv_emulate_cmpxchg(enum x86_segment seg,
         v, offset, old, new, bytes, sh_ctxt);
 }
 
+#ifdef __i386__
 static int 
 pv_emulate_cmpxchg8b(enum x86_segment seg,
                      unsigned long offset,
@@ -386,13 +406,16 @@ pv_emulate_cmpxchg8b(enum x86_segment se
     return v->arch.shadow.mode->x86_emulate_cmpxchg8b(
         v, offset, old_lo, old_hi, new_lo, new_hi, sh_ctxt);
 }
+#endif
 
 static struct x86_emulate_ops pv_shadow_emulator_ops = {
     .read       = pv_emulate_read,
     .insn_fetch = pv_emulate_read,
     .write      = pv_emulate_write,
     .cmpxchg    = pv_emulate_cmpxchg,
-    .cmpxchg8b  = pv_emulate_cmpxchg8b,
+#ifdef __i386__
+    .cmpxchg2   = pv_emulate_cmpxchg8b,
+#endif
 };
 
 struct x86_emulate_ops *shadow_init_emulation(
xen/arch/x86/mm/shadow/multi.c +5 -1
Index: 2006-12-18/xen/arch/x86/mm/shadow/multi.c
===================================================================
@@ -3960,7 +3960,8 @@ sh_x86_emulate_cmpxchg(struct vcpu *v, u
     return rv;
 }
 
-int
+#ifdef __i386__
+static int
 sh_x86_emulate_cmpxchg8b(struct vcpu *v, unsigned long vaddr, 
                           unsigned long old_lo, unsigned long old_hi,
                           unsigned long new_lo, unsigned long new_hi,
@@ -3999,6 +4000,7 @@ sh_x86_emulate_cmpxchg8b(struct vcpu *v,
     shadow_audit_tables(v);
     return rv;
 }
+#endif
 
 
 /**************************************************************************/
@@ -4286,7 +4288,9 @@ struct shadow_paging_mode sh_paging_mode
     .detach_old_tables      = sh_detach_old_tables,
     .x86_emulate_write      = sh_x86_emulate_write,
     .x86_emulate_cmpxchg    = sh_x86_emulate_cmpxchg,
+#ifdef __i386__
     .x86_emulate_cmpxchg8b  = sh_x86_emulate_cmpxchg8b,
+#endif
     .make_monitor_table     = sh_make_monitor_table,
     .destroy_monitor_table  = sh_destroy_monitor_table,
     .guest_map_l1e          = sh_guest_map_l1e,
xen/arch/x86/x86_emulate.c +116 -68

310 lines not on this page · raw message

xen/include/asm-x86/cpufeature.h +2 -0
Index: 2006-12-18/xen/include/asm-x86/cpufeature.h
===================================================================
@@ -118,6 +118,7 @@
 #define cpu_has_cyrix_arr	boot_cpu_has(X86_FEATURE_CYRIX_ARR)
 #define cpu_has_centaur_mcr	boot_cpu_has(X86_FEATURE_CENTAUR_MCR)
 #define cpu_has_clflush		boot_cpu_has(X86_FEATURE_CLFLSH)
+#define cpu_has_cmpxchg16b	0
 #else /* __x86_64__ */
 #define cpu_has_vme		0
 #define cpu_has_de		1
@@ -140,6 +141,7 @@
 #define cpu_has_cyrix_arr	0
 #define cpu_has_centaur_mcr	0
 #define cpu_has_clflush		boot_cpu_has(X86_FEATURE_CLFLSH)
+#define cpu_has_cmpxchg16b	boot_cpu_has(X86_FEATURE_CX16)
 #endif
 
 #endif /* __ASM_I386_CPUFEATURE_H */
xen/include/asm-x86/shadow.h +2 -0
Index: 2006-12-18/xen/include/asm-x86/shadow.h
===================================================================
@@ -157,12 +157,14 @@ struct shadow_paging_mode {
                                             unsigned long new,
                                             unsigned int bytes,
                                             struct sh_emulate_ctxt *sh_ctxt);
+#ifdef __i386__
     int           (*x86_emulate_cmpxchg8b )(struct vcpu *v, unsigned long va,
                                             unsigned long old_lo, 
                                             unsigned long old_hi, 
                                             unsigned long new_lo,
                                             unsigned long new_hi,
                                             struct sh_emulate_ctxt *sh_ctxt);
+#endif
     mfn_t         (*make_monitor_table    )(struct vcpu *v);
     void          (*destroy_monitor_table )(struct vcpu *v, mfn_t mmfn);
     void *        (*guest_map_l1e         )(struct vcpu *v, unsigned long va,
xen/include/asm-x86/x86_emulate.h +19 -7
Index: 2006-12-18/xen/include/asm-x86/x86_emulate.h
===================================================================
@@ -39,8 +39,9 @@ enum x86_segment {
  *     some out-of-band mechanism, unknown to the emulator. The memop signals
  *     failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will
  *     then immediately bail.
- *  2. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
- *     cmpxchg8b_emulated need support 8-byte accesses.
+ *  2. Valid access sizes are 1, 2, 4, 8, and 16 bytes. On x86/32 systems only
+ *     cmpxchg2_emulated need support 8-byte accesses. On x86/64 systems only
+ *     cmpxchg2_emulated need support 16-byte accesses.
  *  3. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
  */
 /* Access completed successfully: continue emulation as normal. */
@@ -110,16 +111,17 @@ struct x86_emulate_ops
         struct x86_emulate_ctxt *ctxt);
 
     /*
-     * cmpxchg8b: Emulate an atomic (LOCKed) CMPXCHG8B operation.
+     * cmpxchg2: Emulate an atomic (LOCKed) CMPXCHG{8,16}B operation.
      *  @old:   [IN ] Value expected to be current at @addr.
      *  @new:   [IN ] Value to write to @addr.
      * NOTES:
-     *  1. This function is only ever called when emulating a real CMPXCHG8B.
-     *  2. This function is *never* called on x86/64 systems.
-     *  2. Not defining this function (i.e., specifying NULL) is equivalent
+     *  1. This function is only ever called when emulating a real CMPXCHG{8,16}B.
+     *  2. This function is *never* called on x86/64 systems for emulating
+     *     CMPXCHG8B.
+     *  3. Not defining this function (i.e., specifying NULL) is equivalent
      *     to defining a function that always returns X86EMUL_UNHANDLEABLE.
      */
-    int (*cmpxchg8b)(
+    int (*cmpxchg2)(
         enum x86_segment seg,
         unsigned long offset,
         unsigned long old_lo,
@@ -127,6 +129,16 @@ struct x86_emulate_ops
         unsigned long new_lo,
         unsigned long new_hi,
         struct x86_emulate_ctxt *ctxt);
+
+    /*
+     * stksz: Determine the item size of the guest stack.
+     * NOTE:
+     *  Not defining this function (i.e., specifying NULL) is equivalent
+     *  to defining a function that returns the default address size of
+     *  the execution mode (X86EMUL_MODE_xxx).
+     */
+    unsigned int (*stksz)(
+        struct x86_emulate_ctxt *ctxt);
 };
 
 struct cpu_user_regs;
#1 lore raw
Quoted 2 lines · Jan Beulich

- don't provide cmpxchg8b emulation where not needed (i.e. page table ops on 64-bit hv)

With the cmpxchg16b changes this probably makes sense, despite my dislike of ifdef if it can be avoided. I'll take a look.

Quoted 1 line

- properly deal with stack operands (push/pop)

I already got the mis-emulation of x86/64 PUSH/POP with operand-size override. The stacksz thing I would do differently -- extend the mode input field to have an extra stack-address-size field. There's another thing that's not right at the moment -- I think on POP we have to calculate the operand effective address after adjusting the stack pointer? That is broken right now which is not a good thing. :-)

Quoted 2 lines

- synchronize prefix handling with hvm's instrlen determination and about to be committed privileged op decoder (changes coming with the 32on64 patches)

The whole lot of hvm_instrlen/mmio/privop emulators are all going to get merged into x86_emulate(). So sync'ing is not really worthwhile, unless there are bugs in x86_emulate()'s prefix decoder.

Quoted 1 line

- support cmpxchg16b if the CPU supports it

I'll take a look at this part. Sounds sensible.

Thanks,
Keir

#2 lore raw
Quoted 6 lines

I already got the mis-emulation of x86/64 PUSH/POP with operand-size override. The stacksz thing I would do differently -- extend the mode input field to have an extra stack-address-size field. There's another thing that's not right at the moment -- I think on POP we have to calculate the operand effective address after adjusting the stack pointer? That is broken right now which is not a good thing. :-)

The patch sent actually fixes that.

Quoted 6 lines
Quoted 2 lines

- synchronize prefix handling with hvm's instrlen determination and about to be committed privileged op decoder (changes coming with the 32on64 patches)

The whole lot of hvm_instrlen/mmio/privop emulators are all going to get merged into x86_emulate(). So sync'ing is not really worthwhile, unless there are bugs in x86_emulate()'s prefix decoder.

That's actually part of the point of the syncing - after having changed the others, I finally also want the main one fixed. And yes, there are problems with the prefix decoding, which can possibly be ignored when emulation pv guest insns, but which (in my opinion) should match hardware behavior 1:1 for hvm guests.

I can't see, however, what value there would particularly be in merging the privop decoder into x86_emulate - it's pretty orthogonal to the current functionality there, except for the memory accessed resulting from ins/outs. To a lesser degree, merging the hvm_instrlen into the main code seems not desirable for performance reasons, unless you mean to share the lookup table, but keep the routine separate.

Jan

#3 lore raw
Quoted 6 lines · Jan Beulich

I can't see, however, what value there would particularly be in merging the privop decoder into x86_emulate - it's pretty orthogonal to the current functionality there, except for the memory accessed resulting from ins/outs. To a lesser degree, merging the hvm_instrlen into the main code seems not desirable for performance reasons, unless you mean to share the lookup table, but keep the routine separate.

Yes, the table-driven hvm_instrlen will stay I think. The generic hvm_instrlen() is only used by the mmio emulator, and that's going away.

-- Keir

#4 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 6 lines

I already got the mis-emulation of x86/64 PUSH/POP with operand-size override. The stacksz thing I would do differently -- extend the mode input field to have an extra stack-address-size field. There's another thing that's not right at the moment -- I think on POP we have to calculate the operand effective address after adjusting the stack pointer? That is broken right now which is not a good thing. :-)

The patch sent actually fixes that.

Oh yes, that's neat. But it should increment the effective address by op_bytes not by stacksz. stacksz only specifies the stack pointer's width, not stack data size.

Quoted 3 lines

I finally also want the main one fixed. And yes, there are problems with the prefix decoding, which can possibly be ignored when emulation pv guest insns, but which (in my opinion) should match hardware behavior 1:1 for hvm guests.

I don't see any problem with the existing code w.r.t. what is architecturally supported. REX byte must come after all prefix bytes, and Intel says that only 4 prefix bytes are allowed. REPNE is not a valid prefix on any instruction that the emulator currently supports. If instructions are outside these defined boundaries we must have some scope for interpretation?

-- Keir

#5 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 05.01.07 15:34 >>>

Quoted 7 lines · Jan Beulich
Quoted 6 lines

I already got the mis-emulation of x86/64 PUSH/POP with operand-size override. The stacksz thing I would do differently -- extend the mode input field to have an extra stack-address-size field. There's another thing that's not right at the moment -- I think on POP we have to calculate the operand effective address after adjusting the stack pointer? That is broken right now which is not a good thing. :-)

Quoted 5 lines
Quoted 1 line

The patch sent actually fixes that.

Oh yes, that's neat. But it should increment the effective address by op_bytes not by stacksz. stacksz only specifies the stack pointer's width, not stack data size.

Indeed, I mixed this up. Makes the code even a little simpler.

Quoted 9 lines
Quoted 3 lines

I finally also want the main one fixed. And yes, there are problems with the prefix decoding, which can possibly be ignored when emulation pv guest insns, but which (in my opinion) should match hardware behavior 1:1 for hvm guests.

I don't see any problem with the existing code w.r.t. what is architecturally supported. REX byte must come after all prefix bytes, and Intel says that only 4 prefix bytes are allowed. REPNE is not a valid prefix on any instruction that the emulator currently supports. If instructions are outside these defined boundaries we must have some scope for interpretation?

Okay, I view this differently - if CPUs always behaved a certain way (i.e. REPNE and REPE being interchangeable as long as the instruction doesn't involve a comparison) or if behavior is clearly defined (REX followed by non-REX prefix, where the REX then simply has no effect), the decoder should behave as real hardware would.

Jan

#6 lore raw
Quoted 6 lines · Jan Beulich

Okay, I view this differently - if CPUs always behaved a certain way (i.e. REPNE
and REPE being interchangeable as long as the instruction doesn't involve a comparison) or if behavior is clearly defined (REX followed by non-REX prefix, where the REX then simply has no effect), the decoder should behave as real hardware would.

Okay, I didn't realise the latter was defined behaviour but it does indeed appear to be. And the REPE/REPNE thing makes sense.

Thanks,
Keir