Skip to contentSkip to conversation

x86emul: use unambiguous register names

PATCH Superseded Jan Beulich 2 participants 5 messages Jan 3, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: use unambiguous register names

This is in preparation of eliminating the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc).

Note that the result is not fully consistent until after at least one more patch is in place, primarily to limit patch size (by trying to not touch the same line twice).

tools/tests/x86_emulator/x86_emulate.c +2 -0
@@ -10,9 +10,11 @@
 
 /* For generic assembly code: use macros to define operation/operand sizes. */
 #ifdef __i386__
+# define r(name)       e ## name
 # define __OS          "l"  /* Operation Suffix */
 # define __OP          "e"  /* Operand Prefix */
 #else
+# define r(name)       r ## name
 # define __OS          "q"  /* Operation Suffix */
 # define __OP          "r"  /* Operand Prefix */
 #endif
xen/arch/x86/x86_emulate.c +2 -0
@@ -21,6 +21,8 @@
 #undef cpuid
 #undef wbinvd
 
+#define r(name) r ## name
+
 #define cpu_has_amd_erratum(nr) \
         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
 
xen/arch/x86/x86_emulate/x86_emulate.c +175 -136

813 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +2 -34
@@ -583,41 +583,9 @@ x86_emulate(
     const struct x86_emulate_ops *ops);
 
 #ifndef NDEBUG
-/*
- * In debug builds, wrap x86_emulate() with some assertions about its expected
- * behaviour.
- */
-static inline int x86_emulate_wrapper(
+int x86_emulate_wrapper(
     struct x86_emulate_ctxt *ctxt,
-    const struct x86_emulate_ops *ops)
-{
-    unsigned long orig_eip = ctxt->regs->eip;
-    int rc = x86_emulate(ctxt, ops);
-
-    /* Retire flags should only be set for successful instruction emulation. */
-    if ( rc != X86EMUL_OKAY )
-        ASSERT(ctxt->retire.raw == 0);
-
-    /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
-    if ( rc == X86EMUL_EXCEPTION )
-        ASSERT(ctxt->regs->eip == orig_eip);
-
-    /*
-     * TODO: Make this true:
-     *
-    ASSERT(ctxt->event_pending == (rc == X86EMUL_EXCEPTION));
-     *
-     * Some codepaths still raise exceptions behind the back of the
-     * emulator. (i.e. return X86EMUL_EXCEPTION but without
-     * event_pending being set).  In the meantime, use a slightly
-     * relaxed check...
-     */
-    if ( ctxt->event_pending )
-        ASSERT(rc == X86EMUL_EXCEPTION);
-
-    return rc;
-}
-
+    const struct x86_emulate_ops *ops);
 #define x86_emulate x86_emulate_wrapper
 #endif
 

x86emul: use unambiguous register names

This is in preparation of eliminating the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc).

Note that the result is not fully consistent until after at least one more patch is in place, primarily to limit patch size (by trying to not touch the same line twice).

tools/tests/x86_emulator/x86_emulate.c +2 -0
@@ -10,9 +10,11 @@
 
 /* For generic assembly code: use macros to define operation/operand sizes. */
 #ifdef __i386__
+# define r(name)       e ## name
 # define __OS          "l"  /* Operation Suffix */
 # define __OP          "e"  /* Operand Prefix */
 #else
+# define r(name)       r ## name
 # define __OS          "q"  /* Operation Suffix */
 # define __OP          "r"  /* Operand Prefix */
 #endif
xen/arch/x86/x86_emulate.c +2 -0
@@ -21,6 +21,8 @@
 #undef cpuid
 #undef wbinvd
 
+#define r(name) r ## name
+
 #define cpu_has_amd_erratum(nr) \
         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
 
xen/arch/x86/x86_emulate/x86_emulate.c +175 -136

813 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +2 -34
@@ -583,41 +583,9 @@ x86_emulate(
     const struct x86_emulate_ops *ops);
 
 #ifndef NDEBUG
-/*
- * In debug builds, wrap x86_emulate() with some assertions about its expected
- * behaviour.
- */
-static inline int x86_emulate_wrapper(
+int x86_emulate_wrapper(
     struct x86_emulate_ctxt *ctxt,
-    const struct x86_emulate_ops *ops)
-{
-    unsigned long orig_eip = ctxt->regs->eip;
-    int rc = x86_emulate(ctxt, ops);
-
-    /* Retire flags should only be set for successful instruction emulation. */
-    if ( rc != X86EMUL_OKAY )
-        ASSERT(ctxt->retire.raw == 0);
-
-    /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
-    if ( rc == X86EMUL_EXCEPTION )
-        ASSERT(ctxt->regs->eip == orig_eip);
-
-    /*
-     * TODO: Make this true:
-     *
-    ASSERT(ctxt->event_pending == (rc == X86EMUL_EXCEPTION));
-     *
-     * Some codepaths still raise exceptions behind the back of the
-     * emulator. (i.e. return X86EMUL_EXCEPTION but without
-     * event_pending being set).  In the meantime, use a slightly
-     * relaxed check...
-     */
-    if ( ctxt->event_pending )
-        ASSERT(rc == X86EMUL_EXCEPTION);
-
-    return rc;
-}
-
+    const struct x86_emulate_ops *ops);
 #define x86_emulate x86_emulate_wrapper
 #endif
 

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

#1 lore raw
Quoted 26 lines · Jan Beulich

This is in preparation of eliminating the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc).

Note that the result is not fully consistent until after at least one more patch is in place, primarily to limit patch size (by trying to not touch the same line twice).

tools/tests/x86_emulator/x86_emulate.c +2 -0
@@ -10,9 +10,11 @@
 
 /* For generic assembly code: use macros to define operation/operand sizes. */
 #ifdef __i386__
+# define r(name)       e ## name
 # define __OS          "l"  /* Operation Suffix */
 # define __OP          "e"  /* Operand Prefix */
 #else
+# define r(name)       r ## name
 # define __OS          "q"  /* Operation Suffix */
 # define __OP          "r"  /* Operand Prefix */
 #endif

xen/arch/x86/x86_emulate.c line 25

Quoted 5 lines
xen/arch/x86/x86_emulate.c +2 -0
@@ -21,6 +21,8 @@
 #undef cpuid
 #undef wbinvd
 
+#define r(name) r ## name
+

Hmm. I am no overwhelmed with this syntax, but I can't propose an alternative, so ok.

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

Quoted 8 lines
xen/arch/x86/x86_emulate.c +1 -1
@@ -2716,36 +2716,36 @@ x86_emulate(
         struct segment_register cs, sreg;
 
     case 0x00 ... 0x05: add: /* add */
-        emulate_2op_SrcV("add", src, dst, _regs.eflags);
+        emulate_2op_SrcV("add", src, dst, _regs.r(flags));
         break;
 

All of these types of operations only adjust the arithmetic flags, so could legitimately use eflags alone. Is it worth reducing?

xen/arch/x86/x86_emulate/x86_emulate.h x86_emulate( · line 589

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
@@ -583,41 +583,9 @@ x86_emulate(
     const struct x86_emulate_ops *ops);
 
 #ifndef NDEBUG
-/*
- * In debug builds, wrap x86_emulate() with some assertions about its expected
- * behaviour.
- */

I'd leave this comment here as well.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

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

#2 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 03.01.17 at 14:30, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
xen/arch/x86/x86_emulate.c +0 -0
@@ -21,6 +21,8 @@
 #undef cpuid
 #undef wbinvd
 
Quoted 5 lines

xen/arch/x86/x86_emulate.c line 25

Quoted 2 lines
xen/arch/x86/x86_emulate.c +2 -0
+#define r(name) r ## name
+

Hmm. I am no overwhelmed with this syntax, but I can't propose an alternative, so ok.

I kind of suspected such a response.

Quoted 11 lines

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

Quoted 8 lines
xen/arch/x86/x86_emulate.c +1 -1
@@ -2716,36 +2716,36 @@ x86_emulate(
         struct segment_register cs, sreg;
 
     case 0x00 ... 0x05: add: /* add */
-        emulate_2op_SrcV("add", src, dst, _regs.eflags);
+        emulate_2op_SrcV("add", src, dst, _regs.r(flags));
         break;
 

All of these types of operations only adjust the arithmetic flags, so could legitimately use eflags alone. Is it worth reducing?

Yes, but not now and here: Using _eflags breaks the inline asm which these macros resolve to.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
@@ -583,41 +583,9 @@ x86_emulate(
     const struct x86_emulate_ops *ops);
 
Quoted 7 lines

xen/arch/x86/x86_emulate/x86_emulate.h x86_emulate( · line 589

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
 #ifndef NDEBUG
-/*
- * In debug builds, wrap x86_emulate() with some assertions about its expected
- * behaviour.
- */

I'd leave this comment here as well.

Hmm, in that case I'd drop it at the definition site. I don't think we need to have the comment in both places. What do you think?

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks (but I'll wait for your feedback to the above), Jan

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

#3 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -0
@@ -583,41 +583,9 @@ x86_emulate(
     const struct x86_emulate_ops *ops);
 
Quoted 8 lines
Quoted 6 lines

xen/arch/x86/x86_emulate/x86_emulate.h x86_emulate( · line 589

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.h +0 -4
 #ifndef NDEBUG
-/*
- * In debug builds, wrap x86_emulate() with some assertions about its expected
- * behaviour.
- */

I'd leave this comment here as well.

Hmm, in that case I'd drop it at the definition site. I don't think we need to have the comment in both places. What do you think?

If you insist on only having it in one place, I'd err on having it in the header file.

~Andrew

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

#4 lore raw
In reply to Jan Beulich
Quoted 15 lines
Quoted 6 lines
Quoted 2 lines · On 03.01.17 at 15:12, <[email protected]> wrote:
Quoted 1 line · On 03.01.17 at 14:30, <[email protected]> wrote:
Quoted 3 lines · Jan Beulich
(quoted diff) +0 -0
@@ -2716,36 +2716,36 @@ x86_emulate(
         struct segment_register cs, sreg;
 
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
     case 0x00 ... 0x05: add: /* add */
-        emulate_2op_SrcV("add", src, dst, _regs.eflags);
+        emulate_2op_SrcV("add", src, dst, _regs.r(flags));
         break;
 

All of these types of operations only adjust the arithmetic flags, so could legitimately use eflags alone. Is it worth reducing?

Quoted 2 lines

Yes, but not now and here: Using _eflags breaks the inline asm which these macros resolve to.

Actually, after some staring at those macros I think this might be relatively simple to deal with, so let me see if I can put together a prereq patch allowing _eflags to be used here instead.

Jan

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