Skip to contentSkip to conversation

x86emul: lift macro machinery from XTF to aid EFLAGS handling

PATCH New Jan Beulich 1 participant 1 message Jul 29

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86emul: lift macro machinery from XTF to aid EFLAGS handling

Sequences of X86_EFLAGS_* ORed together aren't very readable. XTF has a nice way of improving this - adopt this for the eumlator as well, with a few minor tweaks.

No change to generated code, except of course for embedded line numbers.

---
Of course in principle this could be put in xen/include/macros.h for use elsewhere in the hypervisor. That would, however, require duplicating everything into e.g. tools/include/xen-tools/common-macros.h, for test and fuzzing harnesses to still build.

tools/tests/x86_emulator/Makefile +2 -1
@@ -308,7 +308,8 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i
                      x86-vendors.h x86-defns.h msr-index.h) \
          $(addprefix $(XEN_ROOT)/tools/include/xen/lib/x86/, \
                      cpu-policy.h cpuid-autogen.h)
-x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h x86_emulate/private.h $(x86.h)
+x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h \
+                 x86_emulate/macros.h x86_emulate/private.h $(x86.h)
 
 $(OBJS): %.o: %.c $(x86_emulate.h)
 	$(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $<
tools/tests/x86_emulator/test_x86_emulator.c +34 -53

235 lines not on this page · raw message

tools/tests/x86_emulator/x86-emulate.h +4 -0
@@ -72,6 +72,10 @@
 # define ASM_FLAG_OUT(yes, no) no
 #endif
 
+#define count_args_(dot, a1, a2, a3, a4, a5, a6, a7, a8, x, ...) x
+#define count_args(args...) \
+    count_args_(., ## args, 8, 7, 6, 5, 4, 3, 2, 1, 0)
+
 #define hweight32 __builtin_popcount
 #define hweight64 __builtin_popcountll
 
xen/arch/x86/x86_emulate/0f01.c +1 -0
@@ -8,6 +8,7 @@
  * Copyright (c) 2005-2007 XenSource Inc.
  */
 
+#include "macros.h"
 #include "private.h"
 
 #ifdef __XEN__
xen/arch/x86/x86_emulate/0fc7.c +1 -0
@@ -8,6 +8,7 @@
  * Copyright (c) 2005-2007 XenSource Inc.
  */
 
+#include "macros.h"
 #include "private.h"
 
 /* Avoid namespace pollution. */
xen/arch/x86/x86_emulate/blk.c +1 -0
@@ -5,6 +5,7 @@
  * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
  */
 
+#include "macros.h"
 #include "private.h"
 
 #if !defined(X86EMUL_NO_FPU) || !defined(X86EMUL_NO_MMX) || \
xen/arch/x86/x86_emulate/fpu.c +2 -1
@@ -8,6 +8,7 @@
  * Copyright (c) 2005-2007 XenSource Inc.
  */
 
+#include "macros.h"
 #include "private.h"
 
 #ifdef __XEN__
@@ -71,7 +72,7 @@ do {
     invoke_stub(_PRE_EFLAGS("[eflags]", "[mask]", "[tmp]"),             \
                 _POST_EFLAGS("[eflags]", "[mask]", "[tmp]"),            \
                 [eflags] "+g" (regs->eflags), [tmp] "=&r" (tmp_)        \
-                : [mask] "i" (X86_EFLAGS_ZF|X86_EFLAGS_PF|X86_EFLAGS_CF)); \
+                : [mask] "i" (X86_EFLAGS(ZF, PF, CF)));                 \
     put_stub(stub);                                                     \
 } while (0)
 
xen/arch/x86/x86_emulate/macros.h new file +26 -0
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/******************************************************************************
+ * macros.h - helpers for the emulator as well as the test and fuzzing harnesses
+ */
+
+#ifndef X86_EMULATE_MACROS_H
+#define X86_EMULATE_MACROS_H
+
+#define VAR_MACRO__(m, c, count, args...) m##count(c, ## args)
+#define VAR_MACRO_(m, c, count, args...) VAR_MACRO__(m, c, count, ## args)
+#define VAR_MACRO(m, c, args...) VAR_MACRO_(m, c, count_args(args), ## args)
+
+#define TOK_OR1(t, x)       (t ## x)
+#define TOK_OR2(t, x, y)    ((t ## x) | TOK_OR1(t, y))
+#define TOK_OR3(t, x, y...) ((t ## x) | TOK_OR2(t, ## y))
+#define TOK_OR4(t, x, y...) ((t ## x) | TOK_OR3(t, ## y))
+#define TOK_OR5(t, x, y...) ((t ## x) | TOK_OR4(t, ## y))
+#define TOK_OR6(t, x, y...) ((t ## x) | TOK_OR5(t, ## y))
+#define TOK_OR7(t, x, y...) ((t ## x) | TOK_OR6(t, ## y))
+#define TOK_OR8(t, x, y...) ((t ## x) | TOK_OR7(t, ## y))
+
+#define TOK_OR(stem, tok...) VAR_MACRO(TOK_OR, stem, ## tok)
+
+#define X86_EFLAGS(flg...) TOK_OR(X86_EFLAGS_, ## flg)
+
+#endif /* X86_EMULATE_MACROS_H */
xen/arch/x86/x86_emulate/private.h +3 -5
@@ -439,16 +439,14 @@ struct x87_env32 {
  * These EFLAGS bits are restored from saved value during emulation, and
  * any changes are written back to the saved value after emulation.
  */
-#define EFLAGS_MASK (X86_EFLAGS_OF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
-                     X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
+#define EFLAGS_MASK X86_EFLAGS(OF, SF, ZF, AF, PF, CF)
 
 /*
  * These EFLAGS bits are modifiable (by POPF and IRET), possibly subject
  * to further CPL and IOPL constraints.
  */
-#define EFLAGS_MODIFIABLE (X86_EFLAGS_ID | X86_EFLAGS_AC | X86_EFLAGS_RF | \
-                           X86_EFLAGS_NT | X86_EFLAGS_IOPL | X86_EFLAGS_DF | \
-                           X86_EFLAGS_IF | X86_EFLAGS_TF | EFLAGS_MASK)
+#define EFLAGS_MODIFIABLE (X86_EFLAGS(ID, AC, RF, NT, IOPL, DF, IF, TF) | \
+                           EFLAGS_MASK)
 
 #define truncate_word(ea, byte_width)           \
 ({  unsigned long __ea = (ea);                  \
xen/arch/x86/x86_emulate/x86_emulate.c +23 -23

183 lines not on this page · raw message