Skip to contentSkip to conversation

x86: use unambiguous register names

PATCH In review Jan Beulich 3 participants 5 messages Jan 5, 2017 – Jan 6, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: use unambiguous register names

Eliminate the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc). To ensure no piece of code was missed, transiently use the underscore prefixed names only for 32-bit register accesses. This will be cleaned up subsequently.

---
This is part II of the register renaming project. My intention is for things to stay this way for a couple of weeks, so people having patches in progress can update accordingly before we switch from the _e* names to the proper e* ones.

tools/include/xen-foreign/mkheader.py +12 -0
@@ -67,10 +67,14 @@ inttypes["x86_32"] = {
     "xen_ulong_t"   : "uint32_t",
 };
 header["x86_32"] = """
+#define __DECL_REG_LO8(which) uint32_t e ## which ## x
+#define __DECL_REG_LO16(name) uint32_t e ## name
 #define __i386___X86_32 1
 #pragma pack(4)
 """;
 footer["x86_32"] = """
+#undef __DECL_REG_LO8
+#undef __DECL_REG_LO16
 #pragma pack()
 """;
 
@@ -89,10 +93,18 @@ header["x86_64"] = """
 # define __DECL_REG(name) uint64_t r ## name
 # define __align8__ FIXME
 #endif
+#define __DECL_REG_LOHI(name) __DECL_REG(name ## x)
+#define __DECL_REG_LO8        __DECL_REG
+#define __DECL_REG_LO16       __DECL_REG
+#define __DECL_REG_HI         __DECL_REG
 #define __x86_64___X86_64 1
 """;
 footer["x86_64"] = """
 #undef __DECL_REG
+#undef __DECL_REG_LOHI
+#undef __DECL_REG_LO8
+#undef __DECL_REG_LO16
+#undef __DECL_REG_HI
 """
 
 ###########################################################################
tools/tests/x86_emulator/Makefile +1 -1
@@ -46,7 +46,7 @@ x86_emulate/x86_emulate.c x86_emulate/x8
 HOSTCFLAGS += $(CFLAGS_xeninclude)
 
 x86_emulate.o: x86_emulate.c x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h
-	$(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $<
+	$(HOSTCC) $(HOSTCFLAGS) -D__XEN_TOOLS__ -c -g -o $@ $<
 
 test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate/x86_emulate.h
 	$(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $<
xen/arch/x86/domctl.c +2 -2
@@ -1530,8 +1530,8 @@ void arch_get_info_guest(struct vcpu *v,
         }
 
         /* IOPL privileges are virtualised: merge back into returned eflags. */
-        BUG_ON((c(user_regs.eflags) & X86_EFLAGS_IOPL) != 0);
-        c(user_regs.eflags |= v->arch.pv_vcpu.iopl);
+        BUG_ON((c(user_regs._eflags) & X86_EFLAGS_IOPL) != 0);
+        c(user_regs._eflags |= v->arch.pv_vcpu.iopl);
 
         if ( !compat )
         {
xen/arch/x86/hvm/hvm.c +1 -1
@@ -3093,7 +3093,7 @@ void hvm_task_switch(
         if ( segr.attr.fields.db )
             sp = regs->_esp -= opsz;
         else
-            sp = *(uint16_t *)&regs->esp -= opsz;
+            sp = regs->sp -= opsz;
         if ( hvm_virtual_to_linear_addr(x86_seg_ss, &segr, sp, opsz,
                                         hvm_access_write,
                                         16 << segr.attr.fields.db,
xen/arch/x86/hvm/vmx/realmode.c +6 -13
@@ -66,30 +66,23 @@ static void realmode_deliver_exception(
         }
     }
 
-    frame[0] = regs->eip + insn_len;
+    frame[0] = regs->ip + insn_len;
     frame[1] = csr->sel;
-    frame[2] = regs->eflags & ~X86_EFLAGS_RF;
+    frame[2] = regs->flags & ~X86_EFLAGS_RF;
 
     /* We can't test hvmemul_ctxt->ctxt.sp_size: it may not be initialised. */
     if ( hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.db )
-    {
-        regs->esp -= 6;
-        pstk = regs->esp;
-    }
+        pstk = regs->_esp -= 6;
     else
-    {
-        pstk = (uint16_t)(regs->esp - 6);
-        regs->esp &= ~0xffff;
-        regs->esp |= pstk;
-    }
+        pstk = regs->sp -= 6;
 
     pstk += hvmemul_get_seg_reg(x86_seg_ss, hvmemul_ctxt)->base;
     (void)hvm_copy_to_guest_phys(pstk, frame, sizeof(frame));
 
     csr->sel  = cs_eip >> 16;
     csr->base = (uint32_t)csr->sel << 4;
-    regs->eip = (uint16_t)cs_eip;
-    regs->eflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_RF);
+    regs->ip = (uint16_t)cs_eip;
+    regs->_eflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF | X86_EFLAGS_RF);
 
     /* Exception delivery clears STI and MOV-SS blocking. */
     if ( hvmemul_ctxt->intr_shadow &
xen/arch/x86/ioport_emulate.c +2 -2
@@ -12,8 +12,8 @@
 static void ioemul_handle_proliant_quirk(
     u8 opcode, char *io_emul_stub, struct cpu_user_regs *regs)
 {
-    uint16_t port = regs->edx;
-    uint8_t value = regs->eax;
+    uint16_t port = regs->dx;
+    uint8_t value = regs->al;
 
     if ( (opcode != 0xee) || (port != 0xcd4) || !(value & 0x80) )
         return;
xen/arch/x86/x86_emulate/x86_emulate.c +232 -248

1,238 lines not on this page · raw message

xen/include/Makefile +1 -1
@@ -30,7 +30,7 @@ headers-$(CONFIG_X86)     += compat/hvm/
 headers-y                 += compat/arch-$(compat-arch-y).h compat/pmu.h compat/xlat.h
 headers-$(CONFIG_FLASK)   += compat/xsm/flask_op.h
 
-cppflags-y                := -include public/xen-compat.h
+cppflags-y                := -include public/xen-compat.h -DXEN_GENERATING_COMPAT_HEADERS
 cppflags-$(CONFIG_X86)    += -m32
 
 # 8-byte types are 4-byte aligned on x86_32 ...
xen/include/public/arch-x86/xen-x86_32.h +35 -10

63 lines not on this page · raw message

xen/include/public/arch-x86/xen-x86_64.h +58 -19

101 lines not on this page · raw message

xen/include/public/arch-x86/xen.h +10 -0
@@ -57,7 +57,17 @@
 #define set_xen_guest_handle(hnd, val) set_xen_guest_handle_raw(hnd, val)
 
 #if defined(__i386__)
+# ifdef __XEN__
+__DeFiNe__ __DECL_REG_LO8(which) uint32_t _e ## which ## x
+__DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name, _e ## name; }
+# endif
 #include "xen-x86_32.h"
+# ifdef __XEN__
+__UnDeF__ __DECL_REG_LO8
+__UnDeF__ __DECL_REG_LO16
+__DeFiNe__ __DECL_REG_LO8(which) _e ## which ## x
+__DeFiNe__ __DECL_REG_LO16(name) _e ## name
+# endif
 #elif defined(__x86_64__)
 #include "xen-x86_64.h"
 #endif
xen/tools/compat-build-header.py +2 -0
@@ -7,6 +7,8 @@ pats = [
  [ r"__IfDeF__ (XEN_HAVE.*)", r"#ifdef \1" ],
  [ r"__ElSe__", r"#else" ],
  [ r"__EnDif__", r"#endif" ],
+ [ r"__DeFiNe__", r"#define" ],
+ [ r"__UnDeF__", r"#undef" ],
  [ r"\"xen-compat.h\"", r"<public/xen-compat.h>" ],
  [ r"(struct|union|enum)\s+(xen_?)?(\w)", r"\1 compat_\3" ],
  [ r"@KeeP@", r"" ],

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

Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Jan Beulich

Eliminate the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc). To ensure no piece of code was missed, transiently use the underscore prefixed names only for 32-bit register accesses. This will be cleaned up subsequently.

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

Acked-by on v1 1/1
#2 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

Eliminate the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc). To ensure no piece of code was missed, transiently use the underscore prefixed names only for 32-bit register accesses. This will be cleaned up subsequently.

Quoted 5 lines

---
This is part II of the register renaming project. My intention is for things to stay this way for a couple of weeks, so people having patches in progress can update accordingly before we switch from the _e* names to the proper e* ones.

I bet that noone is going to notice that they even need make changes until the build breaks.

I'd recommend getting part 3 done sooner rather than later, leaving a some commented-out code to reintroduce the _e* names, which people can patch locally if they can't get get around to updating the names at once. That commented-out code can then stay for a couple of weeks, as a grace period.

~Andrew

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

#3 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 06.01.17 at 12:08, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

Eliminate the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc). To ensure no piece of code was missed, transiently use the underscore prefixed names only for 32-bit register accesses. This will be cleaned up subsequently.

Thanks.

Quoted 8 lines
Quoted 5 lines

---
This is part II of the register renaming project. My intention is for things to stay this way for a couple of weeks, so people having patches in progress can update accordingly before we switch from the _e* names to the proper e* ones.

I bet that noone is going to notice that they even need make changes until the build breaks.

But that's the point of requiring the use of _eax for the time being after this patch: Anyone still using eax would run into build failure.

Quoted 5 lines

I'd recommend getting part 3 done sooner rather than later, leaving a some commented-out code to reintroduce the _e* names, which people can patch locally if they can't get get around to updating the names at once. That commented-out code can then stay for a couple of weeks, as a grace period.

I don't really see the point of this, considering that with the move here we're forcing people to update their code immediately, too.

Jan

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

#4 lore raw
Quoted 16 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 06.01.17 at 12:08, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich

Eliminate the mis-naming of 64-bit fields with 32-bit register names (eflags instead of rflags etc). To ensure no piece of code was missed, transiently use the underscore prefixed names only for 32-bit register accesses. This will be cleaned up subsequently.

Thanks.

Quoted 5 lines
Quoted 5 lines

---
This is part II of the register renaming project. My intention is for things to stay this way for a couple of weeks, so people having patches in progress can update accordingly before we switch from the _e* names to the proper e* ones.

Quoted 5 lines
Quoted 2 lines

I bet that noone is going to notice that they even need make changes until the build breaks.

But that's the point of requiring the use of _eax for the time being after this patch: Anyone still using eax would run into build failure.

Ah. Yes - I missed this point.

Fine then.

~Andrew

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