Skip to contentSkip to conversation

x86emul: use unambiguous register names

PATCH In review Jan Beulich 2 participants 10 messages Jan 4, 2017

lore
Share & tools
lore
#0 lore raw

v2 cover x86emul: use unambiguous register names

This (together with the still pending VMX patch) concludes part I of the renaming project. The split of the changes here from what will be in part II is to limit code churn, i.e. namely to avoid touching the same line of code twice where possible.

1: make _PRE_EFLAGS() tolerate first argument being 32-bit 2: use unambiguous register names

---
v2: New patch 1, leading to reduced patch 2.

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

#1 lore raw

PATCH v2 1/2 x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -530,7 +530,8 @@ typedef union {
 "pop  %"_tmp"; "                                                \
 "orl  %"_LO32 _tmp",("_STK"); "                                 \
 "popf; "                                                        \
-"pop  %"_sav"; "
+"pop  %"_tmp"; "                                                \
+"movl %"_LO32 _tmp",%"_LO32 _sav"; "
 
 /* After executing instruction: write-back necessary bits in EFLAGS. */
 #define _POST_EFLAGS(_sav, _msk, _tmp)          \

x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
@@ -530,7 +530,8 @@ typedef union {
 "pop  %"_tmp"; "                                                \
 "orl  %"_LO32 _tmp",("_STK"); "                                 \
 "popf; "                                                        \
-"pop  %"_sav"; "
+"pop  %"_tmp"; "                                                \
+"movl %"_LO32 _tmp",%"_LO32 _sav"; "
 
 /* After executing instruction: write-back necessary bits in EFLAGS. */
 #define _POST_EFLAGS(_sav, _msk, _tmp)          \

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

#2 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 6 lines · Jan Beulich

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

Quoted 8 lines

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

And the actual masking calculations can be done in C.

I'd suggest putting this patch in as-is to unblock the register renaming, and defer any potential improvements to separate work.

~Andrew

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

#3 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 11:56, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

Quoted 8 lines

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

Quoted 7 lines

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

And the actual masking calculations can be done in C.

I did think about this yesterday, but came to the conclusion that it can't be easily converted. Yet now that I look at the sketched out code above, I can't see why I came to that conclusion.

Quoted 2 lines

I'd suggest putting this patch in as-is to unblock the register renaming, and defer any potential improvements to separate work.

May I take this as an ack then?

Jan

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

Reviewed-by
#4 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 11:56, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

Quoted 8 lines

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

Quoted 8 lines
Quoted 1 line

And the actual masking calculations can be done in C.

I did think about this yesterday, but came to the conclusion that it can't be easily converted. Yet now that I look at the sketched out code above, I can't see why I came to that conclusion.

Quoted 2 lines

I'd suggest putting this patch in as-is to unblock the register renaming, and defer any potential improvements to separate work.

May I take this as an ack then?

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

#5 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 28 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 11:56, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

Quoted 8 lines

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

And the actual masking calculations can be done in C.

I did think about this yesterday, but came to the conclusion that it can't be easily converted. Yet now that I look at the sketched out code above, I can't see why I came to that conclusion.

It is always possible that my 30s thinking about this is subtly wrong...

~Andrew

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

#6 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 29 lines · Andrew Cooper
Quoted 28 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 11:56, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

While this may appear to introduce a truncation issue, the high 32 bits get zapped already anyway (early in _PRE_EFLAGS() as well as in _POST_EFLAGS()). Once a subsequent patch switches to use proper 32-bit EFLAGS operands, we'll in fact end up with more correct code, as that zeroing of the upper halves will then go away.

Quoted 8 lines

---
As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

And the actual masking calculations can be done in C.

I did think about this yesterday, but came to the conclusion that it can't be easily converted. Yet now that I look at the sketched out code above, I can't see why I came to that conclusion.

It is always possible that my 30s thinking about this is subtly wrong...

Or even, better yet with LAHF/SAHF which avoids all the stack operations. (Needing alternatives to cover the slim case of running on the few 64bit processors without these instructions.)

~Andrew

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

#7 lore raw

Re: [PATCH v2 1/2] x86emul: make _PRE_EFLAGS() tolerate first argument being 32-bit

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 12:51, <[email protected]> wrote:
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 04.01.17 at 11:56, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

As this adds an instruction, the question is whether it would be worth forking _PRE_EFLAGS() into two flavors: One dealing with _sav in a register (allowing several instructions to be dropped) and another dealing with it being on the stack (in which case the logic needs to remain as is, since between the first PUSH and the last POP we mustn't access variables possibly living on the stack).

Looking at the code, why does so much of this need to be written in ASM? Most looks like it could be moved into C.

All that is needed in ASM is something like:

push %[flags_before]
popf
... op ...
pushf
pop %[flags_after]

And the actual masking calculations can be done in C.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

I did think about this yesterday, but came to the conclusion that it can't be easily converted. Yet now that I look at the sketched out code above, I can't see why I came to that conclusion.

It is always possible that my 30s thinking about this is subtly wrong...

Or even, better yet with LAHF/SAHF which avoids all the stack operations. (Needing alternatives to cover the slim case of running on the few 64bit processors without these instructions.)

No - that wouldn't cover EFLAGS.OF.

Jan

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

#8 lore raw

PATCH v2 2/2 x86emul: use unambiguous register names

In reply to 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).

---
v2: Move eflags -> r(flags) change in emulate_<n>op...() macro
invocations to a later patch, where they can now be changed to the more efficient _eflags. Retain a comment.

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 +146 -107

657 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +2 -30
@@ -587,37 +587,9 @@ x86_emulate(
  * 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).

---
v2: Move eflags -> r(flags) change in emulate_<n>op...() macro
invocations to a later patch, where they can now be changed to the more efficient _eflags. Retain a comment.

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 +146 -107

657 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +2 -30
@@ -587,37 +587,9 @@ x86_emulate(
  * 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

Reviewed-by on v2 2/2
#9 lore raw
Quoted 8 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).

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