Skip to contentSkip to conversation

x86emul: a few small steps towards disintegration

PATCH In review Jan Beulich 3 participants 22 messages Jun 15, 2022 – Mar 30, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover x86emul: a few small steps towards disintegration

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

It has now been almost a year since the v1 submission, without having had any feedback. Some re-basing was necessary in the meantime, and a new patch (the last one) has been added - even if seemingly unrelated, it was in this context where I did think of that possible adjustment (which may want to be viewed somewhat RFC, as I know there are reservations against the use of -Os).

1: split off opcode 0f01 handling
2: split off opcode 0fae handling
3: split off opcode 0fc7 handling
4: split off FPU opcode handling
5: split off insn decoding
6: move x86_emul_blk() to separate source file
7: move various utility functions to separate source files 8: build with -Os

Jan

#1 lore raw

PATCH v2 1/8 x86emul: split off opcode 0f01 handling

There's a fair amount of sub-cases (with some yet to be implemented), so a separate function seems warranted.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

---
v2: Re-base.

tools/fuzz/x86_instruction_emulator/Makefile +18 -6
@@ -11,10 +11,13 @@ endif
 # Add libx86 to the build
 vpath %.c $(XEN_ROOT)/xen/lib/x86
 
+.PHONY: x86_emulate
 x86_emulate:
-	[ -L $@ ] || ln -sf $(XEN_ROOT)/xen/arch/x86/$@
+	mkdir -p $@
+	ln -sf $(XEN_ROOT)/xen/arch/x86/$@/*.[ch] $@/
 
-x86_emulate/%: x86_emulate ;
+x86_emulate/%.c: x86_emulate ;
+x86_emulate/%.h: x86_emulate ;
 
 x86-emulate.c x86-emulate.h wrappers.c: %:
 	[ -L $* ] || ln -sf $(XEN_ROOT)/tools/tests/x86_emulator/$*
@@ -31,18 +34,27 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i
                      cpuid.h cpuid-autogen.h)
 x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h $(x86.h)
 
+OBJS := fuzz-emul.o x86-emulate.o
+OBJS += x86_emulate/0f01.o
+
 # x86-emulate.c will be implicit for both
-x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h)
+x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h) x86_emulate/private.h
 
 fuzz-emul.o fuzz-emulate-cov.o cpuid.o wrappers.o: $(x86_emulate.h)
 
-x86-insn-fuzzer.a: fuzz-emul.o x86-emulate.o cpuid.o
+$(filter x86_emulate/%.o,$(OBJS)): x86_emulate/%.o: x86_emulate/%.c x86_emulate/private.h $(x86_emulate.h)
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_$*.o) -c -o $@ $< $(APPEND_CFLAGS)
+
+$(patsubst %.o,%-cov.o,$(filter x86_emulate/%.o,$(OBJS))): x86_emulate/%-cov.o: x86_emulate/%.c x86_emulate/private.h $(x86_emulate.h)
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS_$*.o) $(GCOV_FLAGS) -c -o $@ $< $(APPEND_CFLAGS)
+
+x86-insn-fuzzer.a: $(OBJS) cpuid.o
 	$(AR) rc $@ $^
 
-afl-harness: afl-harness.o fuzz-emul.o x86-emulate.o cpuid.o wrappers.o
+afl-harness: afl-harness.o $(OBJS) cpuid.o wrappers.o
 	$(CC) $(CFLAGS) $^ -o $@
 
-afl-harness-cov: afl-harness-cov.o fuzz-emul-cov.o x86-emulate-cov.o cpuid.o wrappers.o
+afl-harness-cov: afl-harness-cov.o $(patsubst %.o,%-cov.o,$(OBJS)) cpuid.o wrappers.o
 	$(CC) $(CFLAGS) $(GCOV_FLAGS) $^ -o $@
 
 # Common targets
tools/tests/x86_emulator/Makefile +11 -6
@@ -29,7 +29,7 @@ OPMASK := avx512f avx512dq avx512bw
 
 ifeq ($(origin XEN_COMPILE_ARCH),override)
 
-HOSTCFLAGS += -m32
+HOSTCFLAGS += -m32 -I..
 
 else
 
@@ -250,7 +250,10 @@ xop.h avx512f.h: simd-fma.c
 
 endif # 32-bit override
 
-$(TARGET): x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
+OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
+OBJS += x86_emulate/0f01.o
+
+$(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
 
 .PHONY: clean
@@ -274,8 +277,10 @@ else
 run32 clean32: %32: %
 endif
 
+.PHONY: x86_emulate
 x86_emulate:
-	[ -L $@ ] || ln -sf $(XEN_ROOT)/xen/arch/x86/$@
+	mkdir -p $@
+	ln -sf $(XEN_ROOT)/xen/arch/x86/$@/*.[ch] $@/
 
 x86_emulate/%: x86_emulate ;
 
@@ -287,13 +292,13 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i
                      x86-vendors.h x86-defns.h msr-index.h) \
          $(addprefix $(XEN_ROOT)/tools/include/xen/lib/x86/, \
                      cpuid.h cpuid-autogen.h)
-x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h $(x86.h)
+x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h x86_emulate/private.h $(x86.h)
 
-x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o: %.o: %.c $(x86_emulate.h)
+$(OBJS): %.o: %.c $(x86_emulate.h)
 	$(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $<
 
 x86-emulate.o: x86_emulate/x86_emulate.c
-x86-emulate.o: HOSTCFLAGS += -D__XEN_TOOLS__
+x86-emulate.o x86_emulate/%.o: HOSTCFLAGS += -D__XEN_TOOLS__
 
 # In order for our custom .type assembler directives to reliably land after
 # gcc's, we need to keep it from re-ordering top-level constructs.
tools/tests/x86_emulator/x86-emulate.c +2 -4
@@ -22,11 +22,9 @@
 
 /* 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
@@ -265,12 +263,12 @@ void emul_test_put_fpu(
 
 static uint32_t pkru;
 
-static unsigned int rdpkru(void)
+unsigned int rdpkru(void)
 {
     return pkru;
 }
 
-static void wrpkru(unsigned int val)
+void wrpkru(unsigned int val)
 {
     pkru = val;
 }
tools/tests/x86_emulator/x86-emulate.h +8 -0
@@ -1,3 +1,6 @@
+#ifndef X86_EMULATE_H
+#define X86_EMULATE_H
+
 #include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -129,6 +132,9 @@ static inline bool xcr0_mask(uint64_t ma
     return cpu_has_xsave && ((xgetbv(0) & mask) == mask);
 }
 
+unsigned int rdpkru(void);
+void wrpkru(unsigned int val);
+
 #define cache_line_size() (cp.basic.clflush_size * 8)
 #define cpu_has_fpu        cp.basic.fpu
 #define cpu_has_mmx        cp.basic.mmx
@@ -206,3 +212,5 @@ void emul_test_put_fpu(
     struct x86_emulate_ctxt *ctxt,
     enum x86_emulate_fpu_type backout,
     const struct x86_emul_fpu_aux *aux);
+
+#endif /* X86_EMULATE_H */
xen/arch/x86/Makefile +1 -0
@@ -9,6 +9,7 @@ obj-y += mm/
 obj-$(CONFIG_XENOPROF) += oprofile/
 obj-$(CONFIG_PV) += pv/
 obj-y += x86_64/
+obj-y += x86_emulate/
 
 alternative-y := alternative.init.o
 alternative-$(CONFIG_LIVEPATCH) :=
xen/arch/x86/x86_emulate/0f01.c new file +349 -0

349 lines not on this page · raw message

xen/arch/x86/x86_emulate/Makefile new file +1 -0
@@ -0,0 +1 @@
+obj-y += 0f01.o
xen/arch/x86/x86_emulate/private.h new file +531 -0

531 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +26 -795

939 lines not on this page · raw message

xen/arch/x86/x86_emulate.c +0 -8
@@ -24,8 +24,6 @@
 #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)
 
@@ -54,12 +52,6 @@
 
 #define FXSAVE_AREA current->arch.fpu_ctxt
 
-#ifndef CONFIG_HVM
-# define X86EMUL_NO_FPU
-# define X86EMUL_NO_MMX
-# define X86EMUL_NO_SIMD
-#endif
-
 #include "x86_emulate/x86_emulate.c"
 
 int cf_check x86emul_read_xcr(
#2 lore raw

Re: [PATCH v2 1/8] x86emul: split off opcode 0f01 handling

Quoted 30 lines · Jan Beulich
xen/arch/x86/x86_emulate/private.h new file +27 -0
@@ -0,0 +1,531 @@
+/******************************************************************************
+ * private.h - interface between x86_emulate.c and its helpers
+ *
+ * Copyright (c) 2005-2007 Keir Fraser
+ * Copyright (c) 2005-2007 XenSource Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef __XEN__
+
+# include <xen/kernel.h>
+# include <asm/msr-index.h>
+# include <asm/x86_emulate.h>
+
+# ifndef CONFIG_HVM

xen/arch/x86/x86_emulate/private.h line 37

Quoted 10 lines
xen/arch/x86/x86_emulate/private.h new file +10 -0
+#  define X86EMUL_NO_FPU
+#  define X86EMUL_NO_MMX
+#  define X86EMUL_NO_SIMD
+# endif
+
+#else /* !__XEN__ */
+# include "x86-emulate.h"
+#endif
+
+#ifdef __i386__

Do we ever build this code in 32bit mode?

Thanks, Roger.

#3 lore raw

Re: [PATCH v2 1/8] x86emul: split off opcode 0f01 handling

Quoted 35 lines · Roger Pau Monné
Quoted 34 lines · Jan Beulich
xen/arch/x86/x86_emulate/private.h new file +31 -0
@@ -0,0 +1,531 @@
+/******************************************************************************
+ * private.h - interface between x86_emulate.c and its helpers
+ *
+ * Copyright (c) 2005-2007 Keir Fraser
+ * Copyright (c) 2005-2007 XenSource Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef __XEN__
+
+# include <xen/kernel.h>
+# include <asm/msr-index.h>
+# include <asm/x86_emulate.h>
+
+# ifndef CONFIG_HVM
+#  define X86EMUL_NO_FPU
+#  define X86EMUL_NO_MMX
+#  define X86EMUL_NO_SIMD
+# endif
Quoted 8 lines

xen/arch/x86/x86_emulate/private.h line 37

Quoted 6 lines
xen/arch/x86/x86_emulate/private.h new file +6 -0
+
+#else /* !__XEN__ */
+# include "x86-emulate.h"
+#endif
+
+#ifdef __i386__

Do we ever build this code in 32bit mode?

Yes, for the emulator test harness, which can be built both 32-bit and 64-bit (like all of the tool stack can be). When the tool stack is otherwise 64-bit we even have Makefile logic there so the test harness can be built both ways in parallel (and hence things can be tested both ways, provided 32-bit runtime packages are installed).

Jan

#4 lore raw

PATCH v2 2/8 x86emul: split off opcode 0fae handling

In reply to Jan Beulich

There's a fair amount of sub-cases (with some yet to be implemented), so a separate function seems warranted.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

tools/fuzz/x86_instruction_emulator/Makefile +1 -1
@@ -35,7 +35,7 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i
 x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h $(x86.h)
 
 OBJS := fuzz-emul.o x86-emulate.o
-OBJS += x86_emulate/0f01.o
+OBJS += x86_emulate/0f01.o x86_emulate/0fae.o
 
 # x86-emulate.c will be implicit for both
 x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h) x86_emulate/private.h
tools/tests/x86_emulator/Makefile +1 -1
@@ -251,7 +251,7 @@ xop.h avx512f.h: simd-fma.c
 endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
-OBJS += x86_emulate/0f01.o
+OBJS += x86_emulate/0f01.o x86_emulate/0fae.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
xen/arch/x86/x86_emulate/0fae.c new file +222 -0

222 lines not on this page · raw message

xen/arch/x86/x86_emulate/Makefile +1 -0
@@ -1 +1,2 @@
 obj-y += 0f01.o
+obj-y += 0fae.o
xen/arch/x86/x86_emulate/private.h +76 -0

99 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +13 -236

310 lines not on this page · raw message

#5 lore raw

PATCH v2 3/8 x86emul: split off opcode 0fc7 handling

In reply to Jan Beulich

There's a fair amount of sub-cases (with some yet to be implemented), so a separate function seems warranted.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

tools/fuzz/x86_instruction_emulator/Makefile +1 -1
@@ -35,7 +35,7 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i
 x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h $(x86.h)
 
 OBJS := fuzz-emul.o x86-emulate.o
-OBJS += x86_emulate/0f01.o x86_emulate/0fae.o
+OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
 
 # x86-emulate.c will be implicit for both
 x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h) x86_emulate/private.h
tools/tests/x86_emulator/Makefile +1 -1
@@ -251,7 +251,7 @@ xop.h avx512f.h: simd-fma.c
 endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
-OBJS += x86_emulate/0f01.o x86_emulate/0fae.o
+OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
xen/arch/x86/x86_emulate/0fc7.c new file +210 -0

210 lines not on this page · raw message

xen/arch/x86/x86_emulate/Makefile +1 -0
@@ -1,2 +1,3 @@
 obj-y += 0f01.o
 obj-y += 0fae.o
+obj-y += 0fc7.o
xen/arch/x86/x86_emulate/private.h +14 -0
@@ -308,6 +308,14 @@ struct x86_emulate_state {
 #endif
 };
 
+typedef union {
+    uint64_t mmx;
+    uint64_t __attribute__ ((aligned(16))) xmm[2];
+    uint64_t __attribute__ ((aligned(32))) ymm[4];
+    uint64_t __attribute__ ((aligned(64))) zmm[8];
+    uint32_t data32[16];
+} mmval_t;
+
 struct x86_fxsr {
     uint16_t fcw;
     uint16_t fsw;
@@ -558,6 +566,12 @@ int x86emul_0fae(struct x86_emulate_stat
                  struct x86_emulate_ctxt *ctxt,
                  const struct x86_emulate_ops *ops,
                  enum x86_emulate_fpu_type *fpu_type);
+int x86emul_0fc7(struct x86_emulate_state *s,
+                 struct cpu_user_regs *regs,
+                 struct operand *dst,
+                 struct x86_emulate_ctxt *ctxt,
+                 const struct x86_emulate_ops *ops,
+                 mmval_t *mmvalp);
 
 static inline bool is_aligned(enum x86_segment seg, unsigned long offs,
                               unsigned int size, struct x86_emulate_ctxt *ctxt,
xen/arch/x86/x86_emulate/x86_emulate.c +4 -178

195 lines not on this page · raw message

#6 lore raw

PATCH v2 4/8 x86emul: split off FPU opcode handling

In reply to Jan Beulich

Some of the helper functions/macros are needed only for this, and the code is otherwise relatively independent of other parts of the emulator.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

---
v2: Re-base.

tools/fuzz/x86_instruction_emulator/Makefile +1 -0
@@ -36,6 +36,7 @@ x86_emulate.h := x86-emulate.h x86_emula
 
 OBJS := fuzz-emul.o x86-emulate.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
+OBJS += x86_emulate/fpu.o
 
 # x86-emulate.c will be implicit for both
 x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h) x86_emulate/private.h
tools/tests/x86_emulator/Makefile +1 -0
@@ -252,6 +252,7 @@ endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
+OBJS += x86_emulate/fpu.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
tools/tests/x86_emulator/x86-emulate.c +0 -6
@@ -29,12 +29,6 @@
 # define __OP          "r"  /* Operand Prefix */
 #endif
 
-#define get_stub(stb) ({                         \
-    assert(!(stb).addr);                         \
-    (void *)((stb).addr = (uintptr_t)(stb).buf); \
-})
-#define put_stub(stb) ((stb).addr = 0)
-
 uint32_t mxcsr_mask = 0x0000ffbf;
 struct cpuid_policy cp;
 
xen/arch/x86/x86_emulate/Makefile +1 -0
@@ -1,3 +1,4 @@
 obj-y += 0f01.o
 obj-y += 0fae.o
 obj-y += 0fc7.o
+obj-$(CONFIG_HVM) += fpu.o
xen/arch/x86/x86_emulate/fpu.c new file +491 -0

491 lines not on this page · raw message

xen/arch/x86/x86_emulate/private.h +172 -0

202 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +10 -542

606 lines not on this page · raw message

xen/arch/x86/x86_emulate.c +0 -25
@@ -9,7 +9,6 @@
  *    Keir Fraser <[email protected]>
  */
 
-#include <xen/domain_page.h>
 #include <xen/err.h>
 #include <xen/event.h>
 #include <asm/x86_emulate.h>
@@ -17,7 +16,6 @@
 #include <asm/xstate.h>
 #include <asm/amd.h> /* cpu_has_amd_erratum() */
 #include <asm/debugreg.h>
-#include <asm/endbr.h>
 
 /* Avoid namespace pollution. */
 #undef cmpxchg
@@ -27,29 +25,6 @@
 #define cpu_has_amd_erratum(nr) \
         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
 
-#define get_stub(stb) ({                                        \
-    void *ptr;                                                  \
-    BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX_INST_LEN + 1);         \
-    ASSERT(!(stb).ptr);                                         \
-    (stb).addr = this_cpu(stubs.addr) + STUB_BUF_SIZE / 2;      \
-    (stb).ptr = map_domain_page(_mfn(this_cpu(stubs.mfn))) +    \
-        ((stb).addr & ~PAGE_MASK);                              \
-    ptr = memset((stb).ptr, 0xcc, STUB_BUF_SIZE / 2);           \
-    if ( cpu_has_xen_ibt )                                      \
-    {                                                           \
-        place_endbr64(ptr);                                     \
-        ptr += 4;                                               \
-    }                                                           \
-    ptr;                                                        \
-})
-#define put_stub(stb) ({                                   \
-    if ( (stb).ptr )                                       \
-    {                                                      \
-        unmap_domain_page((stb).ptr);                      \
-        (stb).ptr = NULL;                                  \
-    }                                                      \
-})
-
 #define FXSAVE_AREA current->arch.fpu_ctxt
 
 #include "x86_emulate/x86_emulate.c"
#7 lore raw

PATCH v2 5/8 x86emul: split off insn decoding

In reply to Jan Beulich

This is a fair chunk of code and data and can easily live separate from the main emulation function.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

---
v2: Re-base.

tools/fuzz/x86_instruction_emulator/Makefile +1 -1
@@ -36,7 +36,7 @@ x86_emulate.h := x86-emulate.h x86_emula
 
 OBJS := fuzz-emul.o x86-emulate.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
-OBJS += x86_emulate/fpu.o
+OBJS += x86_emulate/decode.o x86_emulate/fpu.o
 
 # x86-emulate.c will be implicit for both
 x86-emulate.o x86-emulate-cov.o: x86_emulate/x86_emulate.c $(x86_emulate.h) x86_emulate/private.h
tools/tests/x86_emulator/Makefile +1 -1
@@ -252,7 +252,7 @@ endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
-OBJS += x86_emulate/fpu.o
+OBJS += x86_emulate/decode.o x86_emulate/fpu.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
tools/tests/x86_emulator/x86-emulate.c +0 -5
@@ -3,11 +3,6 @@
 #include <errno.h>
 #include <sys/mman.h>
 
-#define DEFINE_PER_CPU(type, var) type per_cpu_##var
-#define this_cpu(var) per_cpu_##var
-
-#define ERR_PTR(val) NULL
-
 /* See gcc bug 100680, but here don't bother making this version dependent. */
 #define gcc11_wrap(x) ({                  \
     unsigned long x_;                     \
tools/tests/x86_emulator/x86-emulate.h +3 -0
@@ -48,6 +48,9 @@
 #define ASSERT assert
 #define ASSERT_UNREACHABLE() assert(!__LINE__)
 
+#define DEFINE_PER_CPU(type, var) type per_cpu_##var
+#define this_cpu(var) per_cpu_##var
+
 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
 
xen/arch/x86/x86_emulate.c +0 -1
@@ -9,7 +9,6 @@
  *    Keir Fraser <[email protected]>
  */
 
-#include <xen/err.h>
 #include <xen/event.h>
 #include <asm/x86_emulate.h>
 #include <asm/processor.h> /* current_cpu_info */
xen/arch/x86/x86_emulate/Makefile +1 -0
@@ -1,4 +1,5 @@
 obj-y += 0f01.o
 obj-y += 0fae.o
 obj-y += 0fc7.o
+obj-y += decode.o
 obj-$(CONFIG_HVM) += fpu.o
xen/arch/x86/x86_emulate/decode.c new file +1749 -0

1,749 lines not on this page · raw message

xen/arch/x86/x86_emulate/private.h +33 -0
@@ -38,9 +38,11 @@
 #ifdef __i386__
 # define mode_64bit() false
 # define r(name) e ## name
+# define PTR_POISON NULL /* 32-bit builds are for user-space, so NULL is OK. */
 #else
 # define mode_64bit() (ctxt->addr_size == 64)
 # define r(name) r ## name
+# define PTR_POISON ((void *)0x8086000000008086UL) /* non-canonical */
 #endif
 
 /* Operand sizes: 8-bit operands or specified/overridden size. */
@@ -77,6 +79,23 @@
 
 typedef uint8_t opcode_desc_t;
 
+enum disp8scale {
+    /* Values 0 ... 4 are explicit sizes. */
+    d8s_bw = 5,
+    d8s_dq,
+    /* EVEX.W ignored outside of 64-bit mode */
+    d8s_dq64,
+    /*
+     * All further values must strictly be last and in the order
+     * given so that arithmetic on the values works.
+     */
+    d8s_vl,
+    d8s_vl_by_2,
+    d8s_vl_by_4,
+    d8s_vl_by_8,
+};
+typedef uint8_t disp8scale_t;
+
 /* Type, address-of, and value of an instruction's operand. */
 struct operand {
     enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type;
@@ -183,6 +202,9 @@ enum vex_pfx {
     vex_f2
 };
 
+#define VEX_PREFIX_DOUBLE_MASK 0x1
+#define VEX_PREFIX_SCALAR_MASK 0x2
+
 union vex {
     uint8_t raw[2];
     struct {             /* SDM names */
@@ -716,6 +738,10 @@ do {
     if ( rc ) goto done;                                        \
 } while (0)
 
+int x86emul_decode(struct x86_emulate_state *s,
+                   struct x86_emulate_ctxt *ctxt,
+                   const struct x86_emulate_ops *ops);
+
 int x86emul_fpu(struct x86_emulate_state *s,
                 struct cpu_user_regs *regs,
                 struct operand *dst,
@@ -745,6 +771,13 @@ int x86emul_0fc7(struct x86_emulate_stat
                  const struct x86_emulate_ops *ops,
                  mmval_t *mmvalp);
 
+/* Initialise output state in x86_emulate_ctxt */
+static inline void init_context(struct x86_emulate_ctxt *ctxt)
+{
+    ctxt->retire.raw = 0;
+    x86_emul_reset_event(ctxt);
+}
+
 static inline bool is_aligned(enum x86_segment seg, unsigned long offs,
                               unsigned int size, struct x86_emulate_ctxt *ctxt,
                               const struct x86_emulate_ops *ops)
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1759

1,137 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#8 lore raw

PATCH v2 6/8 x86emul: move x86_emul_blk() to separate source file

In reply to Jan Beulich

The function is already non-trivial and is expected to further grow.

Code moved gets slightly adjusted in a few places, e.g. replacing EXC_* by X86_EXC_* (such that EXC_* don't need to move as well; we want these to be phased out anyway).

---
v2: Re-base.

tools/tests/x86_emulator/Makefile +1 -1
@@ -252,7 +252,7 @@ endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
-OBJS += x86_emulate/decode.o x86_emulate/fpu.o
+OBJS += x86_emulate/blk.o x86_emulate/decode.o x86_emulate/fpu.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
tools/tests/x86_emulator/x86-emulate.h +2 -0
@@ -85,6 +85,8 @@ bool emul_test_init(void);
 void emul_save_fpu_state(void);
 void emul_restore_fpu_state(void);
 
+struct x86_fxsr *get_fpu_save_area(void);
+
 /*
  * In order to reasonably use the above, wrap library calls we use and which we
  * think might access any of the FPU state into wrappers saving/restoring state
xen/arch/x86/x86_emulate.c +0 -2
@@ -24,8 +24,6 @@
 #define cpu_has_amd_erratum(nr) \
         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
 
-#define FXSAVE_AREA current->arch.fpu_ctxt
-
 #include "x86_emulate/x86_emulate.c"
 
 int cf_check x86emul_read_xcr(
xen/arch/x86/x86_emulate/Makefile +1 -0
@@ -1,5 +1,6 @@
 obj-y += 0f01.o
 obj-y += 0fae.o
 obj-y += 0fc7.o
+obj-y += blk.o
 obj-y += decode.o
 obj-$(CONFIG_HVM) += fpu.o
xen/arch/x86/x86_emulate/blk.c new file +396 -0

396 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +0 -365

371 lines not on this page · raw message

tools/tests/x86_emulator/x86-emulate.c +4 -1
@@ -35,7 +35,10 @@ static bool use_xsave;
  * (When debugging the emulator, care needs to be taken when inserting
  * printf() or alike function calls into regions using this.)
  */
-#define FXSAVE_AREA ((struct x86_fxsr *)fpu_save_area)
+struct x86_fxsr *get_fpu_save_area(void)
+{
+    return (void *)fpu_save_area;
+}
 
 void emul_save_fpu_state(void)
 {
#9 lore raw

PATCH v2 7/8 x86emul: move various utility functions to separate source files

In reply to Jan Beulich

Many are needed by the hypervisor only - have one file for this purpose. Some are also needed by the harness (but not the fuzzer) - have another file for these.

Code moved gets slightly adjusted in a few places, e.g. replacing "state" by "s" (like was done for other that has been split off).

---
v2: Re-base.

tools/tests/x86_emulator/Makefile +1 -1
@@ -252,7 +252,7 @@ endif # 32-bit override
 
 OBJS := x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o
 OBJS += x86_emulate/0f01.o x86_emulate/0fae.o x86_emulate/0fc7.o
-OBJS += x86_emulate/blk.o x86_emulate/decode.o x86_emulate/fpu.o
+OBJS += x86_emulate/blk.o x86_emulate/decode.o x86_emulate/fpu.o x86_emulate/util.o
 
 $(TARGET): $(OBJS)
 	$(HOSTCC) $(HOSTCFLAGS) -o $@ $^
xen/arch/x86/x86_emulate.c +0 -124

136 lines not on this page · raw message

xen/arch/x86/x86_emulate/Makefile +2 -0
@@ -4,3 +4,5 @@ obj-y += 0fc7.o
 obj-y += blk.o
 obj-y += decode.o
 obj-$(CONFIG_HVM) += fpu.o
+obj-y += util.o
+obj-y += util-xen.o
xen/arch/x86/x86_emulate/private.h +7 -0
@@ -331,6 +331,13 @@ struct x86_emulate_state {
 #endif
 };
 
+static inline void check_state(const struct x86_emulate_state *s)
+{
+#if defined(__XEN__) && !defined(NDEBUG)
+    ASSERT(s->caller);
+#endif
+}
+
 typedef union {
     uint64_t mmx;
     uint64_t __attribute__ ((aligned(16))) xmm[2];
xen/arch/x86/x86_emulate/util.c new file +298 -0

298 lines not on this page · raw message

xen/arch/x86/x86_emulate/util-xen.c new file +250 -0

250 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.c +0 -390

393 lines not on this page · raw message

#10 lore raw

PATCH v2 8/8 x86emul: build with -Os

In reply to Jan Beulich

Emulator code is large and involving it in guest operations cannot be expected to be fast anyway. Help binary size as well as, for release builds at least, compile time by building all involved code with size optimization, independent of the build being a debug or a release one.

The size savings observed in a release build (with AMX and KeyLocker code in place on top of what's upstream) are above 48k of .text, with gcc 11.

---
v2: New.

xen/arch/x86/Makefile +1 -0
@@ -89,6 +89,7 @@ hostprogs-y += efi/mkreloc
 # Allows usercopy.c to include itself
 $(obj)/usercopy.o: CFLAGS-y += -iquote .
 
+$(obj)/x86_emulate.o: CFLAGS-y += -Os
 ifneq ($(CONFIG_HVM),y)
 $(obj)/x86_emulate.o: CFLAGS-y += -Wno-unused-label
 endif
xen/arch/x86/x86_emulate/Makefile +2 -0
@@ -1,3 +1,5 @@
+CFLAGS-y += -Os
+
 obj-y += 0f01.o
 obj-y += 0fae.o
 obj-y += 0fc7.o
#11 lore raw
In reply to Jan Beulich

Hi,

It seems that this series has been stale for nearly a month, with nothing heard from maintainers. So I am sending this email as a gentle reminder for maintainers. Thanks!

Kind regards,
Henry

Quoted 24 lines

-----Original Message-----
Subject: [PATCH v2 0/8] x86emul: a few small steps towards disintegration

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

It has now been almost a year since the v1 submission, without having had any feedback. Some re-basing was necessary in the meantime, and a new patch (the last one) has been added - even if seemingly unrelated, it was in this context where I did think of that possible adjustment (which may want to be viewed somewhat RFC, as I know there are reservations against the use of -Os).

1: split off opcode 0f01 handling
2: split off opcode 0fae handling
3: split off opcode 0fc7 handling
4: split off FPU opcode handling
5: split off insn decoding
6: move x86_emul_blk() to separate source file
7: move various utility functions to separate source files 8: build with -Os

Jan

#12 lore raw
Quoted 2 lines · Henry Wang

It seems that this series has been stale for nearly a month, with nothing heard from maintainers. So I am sending this email as a gentle reminder for maintainers.

A month? That's only since v2 submission. See ...

Quoted 9 lines
Quoted 9 lines

-----Original Message-----
Subject: [PATCH v2 0/8] x86emul: a few small steps towards disintegration

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

It has now been almost a year since the v1 submission, without having had any feedback.

... here.

Jan

Quoted 17 lines
Quoted 16 lines

Some re-basing was necessary in the meantime, and a
new patch (the last one) has been added - even if seemingly unrelated, it was in this context where I did think of that possible adjustment (which may want to be viewed somewhat RFC, as I know there are reservations against the use of -Os).

1: split off opcode 0f01 handling
2: split off opcode 0fae handling
3: split off opcode 0fc7 handling
4: split off FPU opcode handling
5: split off insn decoding
6: move x86_emul_blk() to separate source file
7: move various utility functions to separate source files 8: build with -Os

Jan

#13 lore raw

Hi Jan,

Quoted 10 lines

-----Original Message-----
From: Jan Beulich <[email protected]>

Quoted 1 line · Henry Wang

It seems that this series has been stale for nearly a month, with nothing

heard

Quoted 1 line

from maintainers. So I am sending this email as a gentle reminder for

maintainers.

A month? That's only since v2 submission. See ...

Oh, yes indeed. Thank you for the background information, normally I use patchwork to track series I did missed the v1 as v1 is superseded by v2 there (thus "a month", but honestly this series is stale for more than a year...).

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

-----Original Message-----
Subject: [PATCH v2 0/8] x86emul: a few small steps towards disintegration

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

It has now been almost a year since the v1 submission, without having had any feedback.

To possibly make the ping better, I will try add Wei Liu's another 2 emails in the CC list (I was once told by him in this way he will be more likely to notice the email) and put Andrew as TO.

Kind regards,
Henry

Quoted 22 lines

... here.

Jan

Quoted 17 lines
Quoted 16 lines

Some re-basing was necessary in the meantime, and a
new patch (the last one) has been added - even if seemingly unrelated, it was in this context where I did think of that possible adjustment (which may want to be viewed somewhat RFC, as I know there are reservations against the use of -Os).

1: split off opcode 0f01 handling
2: split off opcode 0fae handling
3: split off opcode 0fc7 handling
4: split off FPU opcode handling
5: split off insn decoding
6: move x86_emul_blk() to separate source file
7: move various utility functions to separate source files 8: build with -Os

Jan

#14 lore raw
In reply to Jan Beulich
Quoted 3 lines · Jan Beulich

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

I take the intention is to make code simpler and easier to follow by splitting it up into smaller files?

Thanks, Roger.

#15 lore raw
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

I take the intention is to make code simpler and easier to follow by splitting it up into smaller files?

Well, I can't say yes or no to "simpler" or "easier to follow", but splitting is the goal, in the hope that these may end up as a side effects. There's always the risk that scattering things around may also make things less obvious. My main motivation, however, is the observation that this huge source file alone consumes a fair part of (non-parallelizable) build time. To the degree that with older gcc building this one file takes ten (or so) times as long as the entire rest of the hypervisor.

Jan

#16 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

I take the intention is to make code simpler and easier to follow by splitting it up into smaller files?

Quoted 7 lines

Well, I can't say yes or no to "simpler" or "easier to follow", but splitting is the goal, in the hope that these may end up as a side effects. There's always the risk that scattering things around may also make things less obvious. My main motivation, however, is the observation that this huge source file alone consumes a fair part of (non-parallelizable) build time. To the degree that with older gcc building this one file takes ten (or so) times as long as the

I wouldn't really trade compiler speed for clarity in a piece of code like the x86 emulator, which is already very complex.

Do you have some figures of the performance difference this series makes when building the emulator?

A couple of notes from someone that's not familiar with the x86 emulator. It would be clearer if the split files where prefixed with opcode_ for those that deal with emulation (blk.c, 0f01.c, ...) IMO, so that you clearly see this is an opcode family that has been split into a separate file, or maybe insn_{opcode,blk,fpu,...}?

I've noticed in some of the newly introduced files the original copyright notice from Keir is lost, I assume that's on purpose because none of the code was contributed by Kier in that file? (see 0f01.c vs 0fae.c for example).

Do we expect to be able to split other opcode handling into separate files?

I wonder how tied together are the remaining cases, and whether we will be able to split more of them into separate units?

Overall I don't think the disintegration makes the code harder, as the split cases seems to be fully isolated, I do however wonder whether further splits might not be so isolated?

Thanks, Roger.

#17 lore raw
Quoted 18 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

I take the intention is to make code simpler and easier to follow by splitting it up into smaller files?

Quoted 7 lines

Well, I can't say yes or no to "simpler" or "easier to follow", but splitting is the goal, in the hope that these may end up as a side effects. There's always the risk that scattering things around may also make things less obvious. My main motivation, however, is the observation that this huge source file alone consumes a fair part of (non-parallelizable) build time. To the degree that with older gcc building this one file takes ten (or so) times as long as the

Quoted 2 lines

I wouldn't really trade compiler speed for clarity in a piece of code like the x86 emulator, which is already very complex.

Of course, and I specifically said "main" motivation. The hope is that by splitting things become less entangled / convoluted. I guess fpu.c is a good example where certain non-trivial macros have isolated use, and hence are no longer cluttering other parts of the emulator sources.

Quoted 2 lines

Do you have some figures of the performance difference this series makes when building the emulator?

No, I don't. And the difference isn't going to be significant, I expect, as the build being slow is - from all I can tell - directly connected to the huge switch() statement. Yet the number of cases there shrinks only marginally for now. The series is named "a few small steps" for this reason, along with others. See below for a first bigger step, which may then make a noticeable difference.

Quoted 5 lines

A couple of notes from someone that's not familiar with the x86 emulator. It would be clearer if the split files where prefixed with opcode_ for those that deal with emulation (blk.c, 0f01.c, ...) IMO, so that you clearly see this is an opcode family that has been split into a separate file, or maybe insn_{opcode,blk,fpu,...}?

Hmm. For one, "blk" isn't really dealing with any opcode family in particular. It contains a helper function for code using the emulator. So it falls more in the group of util*.c. For the others may main objection would be that I'd prefer to keep file names short. At least at this point of splitting I think file names are sufficiently descriptive. Nevertheless, insn-0f01.c or opc-0f01.c may be options, if we really think we want/need to group files visually. However, I don't expect there are going to be more files paralleling 0f01.c et al: The opcode groups split out are the ones that are large/heterogeneous enough to warrant doing it on this basis. Of course new such groups may appear in the ISA down the road.

FPU is isolated functionally, and I'd expect a simd.c to appear once it becomes clear if/how to sensibly split out SIMD code. Unlike fpu.c I'd further expect this to (long term) consist of more than just a single function, hopefully replacing the massive use of "goto" within that big switch statement by function calls (but as said, plans here
- if one can call it that way in the first place - are very vague at this point).

Quoted 4 lines

I've noticed in some of the newly introduced files the original copyright notice from Keir is lost, I assume that's on purpose because none of the code was contributed by Kier in that file? (see 0f01.c vs 0fae.c for example).

Right - 0fae.c contains only code which was added later (mostly by me), if I'm not mistaken.

Quoted 2 lines

Do we expect to be able to split other opcode handling into separate files?

As per above, "expect" is perhaps too optimistic. I'd say "hope", in particular for SIMD code (which I guess is now the main part of the ISA as well as the sources, at least number-of-insns-wise). One possible (likely intermediate) approach might be to move all SIMD code from the huge switch() statement to its own file/function, invoked from that huge switch()'s default: case. It may then still be a big switch() statement in (e.g.) simd.c, but we'd then at least have two of them, each about half the size of what we have right now. Plus it may allow some (most?) of the X86EMUL_NO_SIMD #ifdef-ary to be dropped, as the new file - like fpu.c - could then itself be built only conditionally.

Quoted 2 lines

I wonder how tied together are the remaining cases, and whether we will be able to split more of them into separate units?

That's the big open question indeed. As per above - with some effort I expect all SIMD code could collectively be moved out; further splitting would likely end up more involved.

Quoted 3 lines

Overall I don't think the disintegration makes the code harder, as the split cases seems to be fully isolated, I do however wonder whether further splits might not be so isolated?

And again - indeed. This series, while already a lot of code churn, is only collecting some of the very low hanging fruit. But at least I hope that the pieces now separated out won't need a lot of touching later on, except of course to add support for new insns.

Jan

#18 lore raw
Quoted 63 lines · Jan Beulich
Quoted 18 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

... of the huge monolithic source file. The series is largely code movement and hence has the intention of not incurring any functional change.

I take the intention is to make code simpler and easier to follow by splitting it up into smaller files?

Quoted 7 lines

Well, I can't say yes or no to "simpler" or "easier to follow", but splitting is the goal, in the hope that these may end up as a side effects. There's always the risk that scattering things around may also make things less obvious. My main motivation, however, is the observation that this huge source file alone consumes a fair part of (non-parallelizable) build time. To the degree that with older gcc building this one file takes ten (or so) times as long as the

Quoted 2 lines

I wouldn't really trade compiler speed for clarity in a piece of code like the x86 emulator, which is already very complex.

Of course, and I specifically said "main" motivation. The hope is that by splitting things become less entangled / convoluted. I guess fpu.c is a good example where certain non-trivial macros have isolated use, and hence are no longer cluttering other parts of the emulator sources.

Quoted 2 lines

Do you have some figures of the performance difference this series makes when building the emulator?

No, I don't. And the difference isn't going to be significant, I expect, as the build being slow is - from all I can tell - directly connected to the huge switch() statement. Yet the number of cases there shrinks only marginally for now. The series is named "a few small steps" for this reason, along with others. See below for a first bigger step, which may then make a noticeable difference.

Quoted 5 lines

A couple of notes from someone that's not familiar with the x86 emulator. It would be clearer if the split files where prefixed with opcode_ for those that deal with emulation (blk.c, 0f01.c, ...) IMO, so that you clearly see this is an opcode family that has been split into a separate file, or maybe insn_{opcode,blk,fpu,...}?

Hmm. For one, "blk" isn't really dealing with any opcode family in particular. It contains a helper function for code using the emulator. So it falls more in the group of util*.c. For the others may main objection would be that I'd prefer to keep file names short. At least at this point of splitting I think file names are sufficiently descriptive. Nevertheless, insn-0f01.c or opc-0f01.c may be options, if we really think we want/need to group files visually. However, I don't expect there are going to be more files paralleling 0f01.c et al: The opcode groups split out are the ones that are large/heterogeneous enough to warrant doing it on this basis. Of course new such groups may appear in the ISA down the road.

FPU is isolated functionally, and I'd expect a simd.c to appear once it becomes clear if/how to sensibly split out SIMD code. Unlike fpu.c I'd further expect this to (long term) consist of more than just a single function, hopefully replacing the massive use of "goto" within that big switch statement by function calls (but as said, plans here
- if one can call it that way in the first place - are very vague at this point).

Quoted 7 lines
Quoted 4 lines

I've noticed in some of the newly introduced files the original copyright notice from Keir is lost, I assume that's on purpose because none of the code was contributed by Kier in that file? (see 0f01.c vs 0fae.c for example).

Right - 0fae.c contains only code which was added later (mostly by me), if I'm not mistaken.

OK, just wanted to make sure this wasn't an oversight.

Quoted 14 lines
Quoted 2 lines

Do we expect to be able to split other opcode handling into separate files?

As per above, "expect" is perhaps too optimistic. I'd say "hope", in particular for SIMD code (which I guess is now the main part of the ISA as well as the sources, at least number-of-insns-wise). One possible (likely intermediate) approach might be to move all SIMD code from the huge switch() statement to its own file/function, invoked from that huge switch()'s default: case. It may then still be a big switch() statement in (e.g.) simd.c, but we'd then at least have two of them, each about half the size of what we have right now. Plus it may allow some (most?) of the X86EMUL_NO_SIMD #ifdef-ary to be dropped, as the new file - like fpu.c - could then itself be built only conditionally.

I don't like the handling of SIMD from a default case in the global switch much, as we then could end up chaining all kind of random handling in the default case. It's IMO clearer if we can detect and forward insn to the SIMD code when we know it's a SIMD instruction.

I guess that's for another series anyway, so not really the point here.

Quoted 7 lines
Quoted 2 lines

I wonder how tied together are the remaining cases, and whether we will be able to split more of them into separate units?

That's the big open question indeed. As per above - with some effort I expect all SIMD code could collectively be moved out; further splitting would likely end up more involved.

Quoted 8 lines
Quoted 3 lines

Overall I don't think the disintegration makes the code harder, as the split cases seems to be fully isolated, I do however wonder whether further splits might not be so isolated?

And again - indeed. This series, while already a lot of code churn, is only collecting some of the very low hanging fruit. But at least I hope that the pieces now separated out won't need a lot of touching later on, except of course to add support for new insns.

OK, I'm a bit concerned that we end up growing duplicated switch cases, in the sense that we will have the following:

switch ( insn )
{
case 0x100:
case 0x1f0:
case 0x200:

	x86emul_foo();

...
}

x86emul_foo()

{
    switch (insn )
    {
    case 0x100:
        /* Handle. */
	break;

    case 0x1f0:
        /* Handle. */
	break;

    case 0x200:
        ...
    }
}

So that we would end up having to add the opcodes twice, once in the generic switch, and then again at place the insn are actually handled.

So far the introduced splits seems fine in that they deal with a contiguous range of opcodes.

For patches 1-7:

Patch 8 I'm unsure, I guess it should be up to the user to decide whether to use -Os or some other optimization?

I expect introspection users likely care way more about the speed rather than the size of the generated x86 emulator code?

Thanks, Roger.

#19 lore raw
Quoted 17 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Roger Pau Monné

Do we expect to be able to split other opcode handling into separate files?

Quoted 11 lines

As per above, "expect" is perhaps too optimistic. I'd say "hope", in particular for SIMD code (which I guess is now the main part of the ISA as well as the sources, at least number-of-insns-wise). One possible (likely intermediate) approach might be to move all SIMD code from the huge switch() statement to its own file/function, invoked from that huge switch()'s default: case. It may then still be a big switch() statement in (e.g.) simd.c, but we'd then at least have two of them, each about half the size of what we have right now. Plus it may allow some (most?) of the X86EMUL_NO_SIMD #ifdef-ary to be dropped, as the new file - like fpu.c - could then itself be built only conditionally.

Quoted 4 lines

I don't like the handling of SIMD from a default case in the global switch much, as we then could end up chaining all kind of random handling in the default case. It's IMO clearer if we can detect and forward insn to the SIMD code when we know it's a SIMD instruction.

Avoiding of which would require ...

Quoted 45 lines
Quoted 7 lines
Quoted 2 lines

I wonder how tied together are the remaining cases, and whether we will be able to split more of them into separate units?

That's the big open question indeed. As per above - with some effort I expect all SIMD code could collectively be moved out; further splitting would likely end up more involved.

Quoted 8 lines
Quoted 3 lines

Overall I don't think the disintegration makes the code harder, as the split cases seems to be fully isolated, I do however wonder whether further splits might not be so isolated?

And again - indeed. This series, while already a lot of code churn, is only collecting some of the very low hanging fruit. But at least I hope that the pieces now separated out won't need a lot of touching later on, except of course to add support for new insns.

OK, I'm a bit concerned that we end up growing duplicated switch cases, in the sense that we will have the following:

switch ( insn )
{
case 0x100:
case 0x1f0:
case 0x200:

	x86emul_foo();

...
}

x86emul_foo()

{
    switch (insn )
    {
    case 0x100:
        /* Handle. */
	break;

    case 0x1f0:
        /* Handle. */
	break;

    case 0x200:
        ...
    }
}
Quoted 2 lines

So that we would end up having to add the opcodes twice, once in the generic switch, and then again at place the insn are actually handled.

... doing exactly this, which looks like we a agree we'd like to avoid. I'm also not really concerned of "chaining all kind of random handling in the default case" - where things are terminated with an error code doesn't really matter. As first step here I'm literally thinking of splitting the huge switch() into two. The only thing that I can see may get in the way is the resulting SIMD function requiring like 20 parameters.

Quoted 6 lines

So far the introduced splits seems fine in that they deal with a contiguous range of opcodes.

For patches 1-7:

Thanks!

Quoted 5 lines

Patch 8 I'm unsure, I guess it should be up to the user to decide whether to use -Os or some other optimization?

I expect introspection users likely care way more about the speed rather than the size of the generated x86 emulator code?

Perhaps. But do we want to introduce another Kconfig control just for that? And wouldn't there likely be other performance concerns, if performance really mattered in the introspection case?

Jan

#20 lore raw
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monné
Quoted 1 line · Jan Beulich

On 29.03.2023 16:17, Roger Pau Monné wrote:

Patch 8 I'm unsure, I guess it should be up to the user to decide whether to use -Os or some other optimization?

Quoted 6 lines
Quoted 2 lines

I expect introspection users likely care way more about the speed rather than the size of the generated x86 emulator code?

Perhaps. But do we want to introduce another Kconfig control just for that? And wouldn't there likely be other performance concerns, if performance really mattered in the introspection case?

I don't really have a strong opinion on the usage of -Os or not. It's likely fine as long as we also stay consistent with the flag we use when building the test harness and the fuzzer, just in case.

Thanks, Roger.

#21 lore raw
Quoted 10 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 1 line · Jan Beulich

On 29.03.2023 16:17, Roger Pau Monné wrote:

Patch 8 I'm unsure, I guess it should be up to the user to decide whether to use -Os or some other optimization?

I expect introspection users likely care way more about the speed rather than the size of the generated x86 emulator code?

Quoted 7 lines
Quoted 3 lines

Perhaps. But do we want to introduce another Kconfig control just for that? And wouldn't there likely be other performance concerns, if performance really mattered in the introspection case?

I don't really have a strong opinion on the usage of -Os or not. It's likely fine as long as we also stay consistent with the flag we use when building the test harness and the fuzzer, just in case.

While they might be built with different compilers (and hence things can't be compared directly anyway), I can certainly make those two use -Os as well.

Jan