From 0a249bcb0279a99b7ddad683fcc84a5e256c13f1 Mon Sep 17 00:00:00 2001 From: Jan Beulich Date: Thu, 13 Aug 2026 15:12:51 +0200 Subject: [PATCH] x86: reduce dependencies on x86_emulate/x86_emulate.h Split out struct x86_event to an entirely separate header, and move a few other items describing the architecture to a new x86-types.h. With a few forward decls of structures and with a fair number of new #include-s in .c files, the inclusion of x86_emulate.h (and hence x86_emulate/x86_emulate.h) can be dropped from all header files except hvm/emulate.h; it needs additionally adding to hvm/ioreq.h though. Signed-off-by: Jan Beulich Reviewed-by: Andrew Cooper --- tools/tests/x86_emulator/Makefile | 2 +- tools/tests/x86_emulator/x86-emulate.h | 2 + xen/arch/x86/emul-i8254.c | 1 + xen/arch/x86/hvm/hpet.c | 2 + xen/arch/x86/hvm/mmio.c | 1 + xen/arch/x86/hvm/pmtimer.c | 2 + xen/arch/x86/hvm/rtc.c | 9 ++- xen/arch/x86/hvm/stdvga.c | 2 + xen/arch/x86/hvm/svm/nestedsvm.c | 1 + xen/arch/x86/hvm/svm/vmcb.h | 2 +- xen/arch/x86/hvm/vioapic.c | 1 + xen/arch/x86/hvm/viridian/private.h | 2 + xen/arch/x86/hvm/vmx/vvmx.c | 1 + xen/arch/x86/hvm/vpic.c | 1 + xen/arch/x86/include/asm/domain.h | 3 +- xen/arch/x86/include/asm/hvm/hvm.h | 4 +- xen/arch/x86/include/asm/hvm/ioreq.h | 2 + xen/arch/x86/include/asm/hvm/vcpu.h | 2 + xen/arch/x86/include/asm/hvm/vmx/vmcs.h | 2 + xen/arch/x86/include/asm/mce.h | 1 + xen/arch/x86/include/asm/mm.h | 1 - xen/arch/x86/include/asm/x86-event.h | 31 ++++++++++ xen/arch/x86/include/asm/x86-types.h | 76 +++++++++++++++++++++++++ xen/arch/x86/msr.c | 1 + xen/arch/x86/pv/misc-hypercalls.c | 1 + xen/arch/x86/x86_emulate/x86_emulate.h | 76 ++----------------------- xen/drivers/vpci/msix.c | 1 + 27 files changed, 151 insertions(+), 79 deletions(-) create mode 100644 xen/arch/x86/include/asm/x86-event.h create mode 100644 xen/arch/x86/include/asm/x86-types.h diff --git a/tools/tests/x86_emulator/Makefile b/tools/tests/x86_emulator/Makefile index 8210e83345..ff85bc88b1 100644 --- a/tools/tests/x86_emulator/Makefile +++ b/tools/tests/x86_emulator/Makefile @@ -305,7 +305,7 @@ $(call cc-option-add,HOSTCFLAGS-x86_64,HOSTCC,-no-integrated-as) HOSTCFLAGS += $(CFLAGS_xeninclude) -I. $(HOSTCFLAGS-$(XEN_COMPILE_ARCH)) x86.h := $(addprefix $(XEN_ROOT)/tools/include/xen/asm/,\ - x86-vendors.h x86-defns.h msr-index.h) \ + x86-vendors.h x86-defns.h x86-types.h x86-event.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) diff --git a/tools/tests/x86_emulator/x86-emulate.h b/tools/tests/x86_emulator/x86-emulate.h index 62ebd881c9..bcb012f155 100644 --- a/tools/tests/x86_emulator/x86-emulate.h +++ b/tools/tests/x86_emulator/x86-emulate.h @@ -38,6 +38,8 @@ #include #include +#include +#include #include #include diff --git a/xen/arch/x86/emul-i8254.c b/xen/arch/x86/emul-i8254.c index ceb0ef721e..012a40fa75 100644 --- a/xen/arch/x86/emul-i8254.c +++ b/xen/arch/x86/emul-i8254.c @@ -37,6 +37,7 @@ #include #include #include +#include #define domain_vpit(x) (&(x)->arch.vpit) #define vcpu_vpit(x) (domain_vpit((x)->domain)) diff --git a/xen/arch/x86/hvm/hpet.c b/xen/arch/x86/hvm/hpet.c index e317ec1f5f..bdd6fa334d 100644 --- a/xen/arch/x86/hvm/hpet.c +++ b/xen/arch/x86/hvm/hpet.c @@ -11,6 +11,8 @@ #include #include #include +#include + #include #include #include diff --git a/xen/arch/x86/hvm/mmio.c b/xen/arch/x86/hvm/mmio.c index 0fc492f854..0c98eb380e 100644 --- a/xen/arch/x86/hvm/mmio.c +++ b/xen/arch/x86/hvm/mmio.c @@ -9,6 +9,7 @@ #include #include +#include static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr) { diff --git a/xen/arch/x86/hvm/pmtimer.c b/xen/arch/x86/hvm/pmtimer.c index 87a7a01c9f..7247043269 100644 --- a/xen/arch/x86/hvm/pmtimer.c +++ b/xen/arch/x86/hvm/pmtimer.c @@ -11,6 +11,8 @@ #include #include #include /* for hvm_acpi_power_button prototype */ +#include + #include /* Slightly more readable port I/O addresses for the registers we intercept */ diff --git a/xen/arch/x86/hvm/rtc.c b/xen/arch/x86/hvm/rtc.c index 9895dd570c..9a53188dac 100644 --- a/xen/arch/x86/hvm/rtc.c +++ b/xen/arch/x86/hvm/rtc.c @@ -23,12 +23,15 @@ */ #include -#include -#include +#include + #include #include +#include #include -#include +#include +#include + #include #define USEC_PER_SEC 1000000UL diff --git a/xen/arch/x86/hvm/stdvga.c b/xen/arch/x86/hvm/stdvga.c index c3c43f59ee..fa426ab8a6 100644 --- a/xen/arch/x86/hvm/stdvga.c +++ b/xen/arch/x86/hvm/stdvga.c @@ -34,6 +34,8 @@ #include #include +#include + #define VGA_MEM_BASE 0xa0000 #define VGA_MEM_SIZE 0x20000 diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nestedsvm.c index b06124c2c9..91c9060720 100644 --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -11,6 +11,7 @@ #include /* paging_mode_hap */ #include /* for local_event_delivery_(en|dis)able */ #include /* p2m_get_pagetable, p2m_get_nestedp2m */ +#include #include "nestedhvm.h" #include "svm.h" diff --git a/xen/arch/x86/hvm/svm/vmcb.h b/xen/arch/x86/hvm/svm/vmcb.h index 13ccfd3ff9..3760f71a86 100644 --- a/xen/arch/x86/hvm/svm/vmcb.h +++ b/xen/arch/x86/hvm/svm/vmcb.h @@ -4,7 +4,7 @@ #include -#include +#include struct vcpu; diff --git a/xen/arch/x86/hvm/vioapic.c b/xen/arch/x86/hvm/vioapic.c index 7c725f9e47..222e59e2c2 100644 --- a/xen/arch/x86/hvm/vioapic.c +++ b/xen/arch/x86/hvm/vioapic.c @@ -38,6 +38,7 @@ #include #include #include +#include /* HACK: Route IRQ0 only to VCPU0 to prevent time jumps. */ #define IRQ0_SPECIAL_ROUTING 1 diff --git a/xen/arch/x86/hvm/viridian/private.h b/xen/arch/x86/hvm/viridian/private.h index 1bf65c761c..8060ce0677 100644 --- a/xen/arch/x86/hvm/viridian/private.h +++ b/xen/arch/x86/hvm/viridian/private.h @@ -5,6 +5,8 @@ #include #include +#include + #include int viridian_synic_wrmsr(struct vcpu *v, uint32_t idx, uint64_t val); diff --git a/xen/arch/x86/hvm/vmx/vvmx.c b/xen/arch/x86/hvm/vmx/vvmx.c index e4cdfe55c1..a5aa5eb163 100644 --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -18,6 +18,7 @@ #include #include #include +#include static DEFINE_PER_CPU(u64 *, vvmcs_buf); diff --git a/xen/arch/x86/hvm/vpic.c b/xen/arch/x86/hvm/vpic.c index 30ce367c08..97698da768 100644 --- a/xen/arch/x86/hvm/vpic.c +++ b/xen/arch/x86/hvm/vpic.c @@ -33,6 +33,7 @@ #include #include #include +#include #define vpic_domain(v) (container_of((v), struct domain, \ arch.hvm.vpic[!(v)->is_master])) diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/domain.h index e0ce8b4c39..50c048adb5 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -8,7 +8,8 @@ #include #include #include -#include +#include + #include #include diff --git a/xen/arch/x86/include/asm/hvm/hvm.h b/xen/arch/x86/include/asm/hvm/hvm.h index e7c1364802..0ce7d5d783 100644 --- a/xen/arch/x86/include/asm/hvm/hvm.h +++ b/xen/arch/x86/include/asm/hvm/hvm.h @@ -16,11 +16,13 @@ #include #include #include -#include +#include +#include struct pirq; /* needed by pi_update_irte */ struct hvm_hw_cpu; struct xen_domctl_createdomain; +struct x86_event; #ifdef CONFIG_HVM_FEP /* Permit use of the Forced Emulation Prefix in HVM guests */ diff --git a/xen/arch/x86/include/asm/hvm/ioreq.h b/xen/arch/x86/include/asm/hvm/ioreq.h index 84be14fd08..9f418c86e1 100644 --- a/xen/arch/x86/include/asm/hvm/ioreq.h +++ b/xen/arch/x86/include/asm/hvm/ioreq.h @@ -8,6 +8,8 @@ #ifndef __ASM_X86_HVM_IOREQ_H__ #define __ASM_X86_HVM_IOREQ_H__ +#include + /* This correlation must not be altered */ #define IOREQ_STATUS_HANDLED X86EMUL_OKAY #define IOREQ_STATUS_UNHANDLED X86EMUL_UNHANDLEABLE diff --git a/xen/arch/x86/include/asm/hvm/vcpu.h b/xen/arch/x86/include/asm/hvm/vcpu.h index 2a14fa0a63..60d1c8f188 100644 --- a/xen/arch/x86/include/asm/hvm/vcpu.h +++ b/xen/arch/x86/include/asm/hvm/vcpu.h @@ -14,6 +14,8 @@ #include #include #include +#include + #include struct hvm_vcpu_asid { diff --git a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h index 88bded5190..f85a8c8bba 100644 --- a/xen/arch/x86/include/asm/hvm/vmx/vmcs.h +++ b/xen/arch/x86/include/asm/hvm/vmx/vmcs.h @@ -9,6 +9,8 @@ #include +#include + extern void vmcs_dump_vcpu(struct vcpu *v); extern int vmx_vmcs_init(void); int cf_check vmx_cpu_up_prepare(unsigned int cpu); diff --git a/xen/arch/x86/include/asm/mce.h b/xen/arch/x86/include/asm/mce.h index 2ec47a71ae..65d8a27eca 100644 --- a/xen/arch/x86/include/asm/mce.h +++ b/xen/arch/x86/include/asm/mce.h @@ -35,6 +35,7 @@ struct vmce { struct domain; struct vcpu; +struct hvm_vmce_vcpu; /* Guest vMCE MSRs virtualization */ extern void vmce_init_vcpu(struct vcpu *v); diff --git a/xen/arch/x86/include/asm/mm.h b/xen/arch/x86/include/asm/mm.h index 06c20ab8de..2254a7e3fe 100644 --- a/xen/arch/x86/include/asm/mm.h +++ b/xen/arch/x86/include/asm/mm.h @@ -8,7 +8,6 @@ #include #include #include -#include /* * Per-page-frame information. diff --git a/xen/arch/x86/include/asm/x86-event.h b/xen/arch/x86/include/asm/x86-event.h new file mode 100644 index 0000000000..a8823c7cfc --- /dev/null +++ b/xen/arch/x86/include/asm/x86-event.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * x86-event.h + * + * Helper definitions for event handling, which aren't prescribed by the + * architecture itself. + */ + +#ifndef X86_X86_EVENT_H +#define X86_X86_EVENT_H + +#ifdef __XEN__ +# include +#else +# include +#endif + +#define X86_EVENT_NO_EC (-1) /* No error code. */ + +struct x86_event { + int16_t vector; + uint8_t type; /* X86_ET_* */ + uint8_t insn_len; /* Instruction length */ + int32_t error_code; /* X86_EVENT_NO_EC if n/a */ + union { + unsigned long cr2; /* #PF */ + unsigned long pending_dbg; /* #DB (new DR6 bits, positive polarity) */ + }; +}; + +#endif /* X86_X86_EVENT_H */ diff --git a/xen/arch/x86/include/asm/x86-types.h b/xen/arch/x86/include/asm/x86-types.h new file mode 100644 index 0000000000..1c43421885 --- /dev/null +++ b/xen/arch/x86/include/asm/x86-types.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * x86-types.h + * + * Type definitions and basic helpers which are more or less directly + * describing aspects of the architecture. + */ + +#ifndef X86_X86_TYPES_H +#define X86_X86_TYPES_H + +#ifdef __XEN__ +# include +#else +# include +#endif + +/* + * Comprehensive enumeration of x86 segment registers. Various bits of code + * rely on this order (general purpose before system, tr at the beginning of + * system). + */ +enum x86_segment { + /* General purpose. Matches the SReg3 encoding in opcode/ModRM bytes. */ + x86_seg_es, + x86_seg_cs, + x86_seg_ss, + x86_seg_ds, + x86_seg_fs, + x86_seg_gs, + /* System: Valid to use for implicit table references. */ + x86_seg_tr, + x86_seg_ldtr, + x86_seg_gdtr, + x86_seg_idtr, + /* No Segment: For (system/normal) accesses which are already linear. */ + x86_seg_sys, + x86_seg_none +}; + +static inline bool is_x86_user_segment(enum x86_segment seg) +{ + unsigned int idx = seg; + + return idx <= x86_seg_gs; +} +static inline bool is_x86_system_segment(enum x86_segment seg) +{ + return seg >= x86_seg_tr && seg < x86_seg_none; +} + +/* + * Full state of a segment register (visible and hidden portions). + * Chosen to match the format of an AMD SVM VMCB. + */ +struct segment_register { + uint16_t sel; + union { + uint16_t attr; + struct { + uint16_t type:4; + uint16_t s: 1; + uint16_t dpl: 2; + uint16_t p: 1; + uint16_t avl: 1; + uint16_t l: 1; + uint16_t db: 1; + uint16_t g: 1; + uint16_t pad: 4; + }; + }; + uint32_t limit; + uint64_t base; +}; + +#endif /* X86_X86_TYPES_H */ diff --git a/xen/arch/x86/msr.c b/xen/arch/x86/msr.c index d10891dcfc..e9fa02a03f 100644 --- a/xen/arch/x86/msr.c +++ b/xen/arch/x86/msr.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/xen/arch/x86/pv/misc-hypercalls.c b/xen/arch/x86/pv/misc-hypercalls.c index 34a0717540..b1fa9685f4 100644 --- a/xen/arch/x86/pv/misc-hypercalls.c +++ b/xen/arch/x86/pv/misc-hypercalls.c @@ -12,6 +12,7 @@ #include #include #include +#include long do_set_debugreg(int reg, unsigned long value) { diff --git a/xen/arch/x86/x86_emulate/x86_emulate.h b/xen/arch/x86/x86_emulate/x86_emulate.h index 0fd20747dc..534b1c46fa 100644 --- a/xen/arch/x86/x86_emulate/x86_emulate.h +++ b/xen/arch/x86/x86_emulate/x86_emulate.h @@ -13,6 +13,11 @@ #include +#ifdef __XEN__ +# include +# include +#endif + #define MAX_INST_LEN 15 #if defined(__i386__) @@ -25,77 +30,6 @@ struct x86_emulate_ctxt; -/* - * Comprehensive enumeration of x86 segment registers. Various bits of code - * rely on this order (general purpose before system, tr at the beginning of - * system). - */ -enum x86_segment { - /* General purpose. Matches the SReg3 encoding in opcode/ModRM bytes. */ - x86_seg_es, - x86_seg_cs, - x86_seg_ss, - x86_seg_ds, - x86_seg_fs, - x86_seg_gs, - /* System: Valid to use for implicit table references. */ - x86_seg_tr, - x86_seg_ldtr, - x86_seg_gdtr, - x86_seg_idtr, - /* No Segment: For (system/normal) accesses which are already linear. */ - x86_seg_sys, - x86_seg_none -}; - -static inline bool is_x86_user_segment(enum x86_segment seg) -{ - unsigned int idx = seg; - - return idx <= x86_seg_gs; -} -static inline bool is_x86_system_segment(enum x86_segment seg) -{ - return seg >= x86_seg_tr && seg < x86_seg_none; -} - -#define X86_EVENT_NO_EC (-1) /* No error code. */ - -struct x86_event { - int16_t vector; - uint8_t type; /* X86_ET_* */ - uint8_t insn_len; /* Instruction length */ - int32_t error_code; /* X86_EVENT_NO_EC if n/a */ - union { - unsigned long cr2; /* #PF */ - unsigned long pending_dbg; /* #DB (new DR6 bits, positive polarity) */ - }; -}; - -/* - * Full state of a segment register (visible and hidden portions). - * Chosen to match the format of an AMD SVM VMCB. - */ -struct segment_register { - uint16_t sel; - union { - uint16_t attr; - struct { - uint16_t type:4; - uint16_t s: 1; - uint16_t dpl: 2; - uint16_t p: 1; - uint16_t avl: 1; - uint16_t l: 1; - uint16_t db: 1; - uint16_t g: 1; - uint16_t pad: 4; - }; - }; - uint32_t limit; - uint64_t base; -}; - struct x86_emul_fpu_aux { unsigned long ip, dp; uint16_t cs, ds; diff --git a/xen/drivers/vpci/msix.c b/xen/drivers/vpci/msix.c index bbe4f213ac..1f93a862da 100644 --- a/xen/drivers/vpci/msix.c +++ b/xen/drivers/vpci/msix.c @@ -25,6 +25,7 @@ #include #include +#include #define VMSIX_ADDR_IN_RANGE(addr, vpci, nr) \ ((addr) >= vmsix_table_addr(vpci, nr) && \