Skip to contentSkip to conversation

x86/traps: Simplify exception setup

PATCH In review Andrew Cooper 2 participants 15 messages Mar 17, 2025 – Mar 21, 2025

lore
Share & tools
lore
#0 lore raw

cover x86/traps: Simplify exception setup

This is a small series pulled out of the FRED work.

It is cleanup of our init APIs, moving logic into traps-setup.c, and a simplificiation of early exception setup prior to having exception support.

In the future, it will be traps_init() deciding whether to activate FRED or not, so we'll always be using IDT for early exception handling.

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1720849629
https://cirrus-ci.com/build/6194032705011712

Andrew Cooper (4):
x86/traps: Move cpu_init() out of trap_init() x86/traps: Move percpu_traps_init() into traps-setup.c x86/traps: Move trap_init() into traps-setup.c x86/traps: Introduce early_traps_init() and simplify setup

 xen/arch/x86/acpi/power.c         |   1 +
 xen/arch/x86/include/asm/msr.h    |   2 -
 xen/arch/x86/include/asm/system.h |   3 -
 xen/arch/x86/include/asm/traps.h  |   6 ++
 xen/arch/x86/setup.c              |   8 ++-
 xen/arch/x86/smpboot.c            |   1 +
 xen/arch/x86/traps-setup.c        | 113 ++++++++++++++++++++++++++++++
 xen/arch/x86/traps.c              |  84 ----------------------
 8 files changed, 126 insertions(+), 92 deletions(-)

base-commit: e7e0d485e993e97b1c816adcfc610e7c7258ce96

Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/4 x86/traps: Move cpu_init() out of trap_init()

cpu_init() doesn't particularly belong in trap_init(). This brings the BSP more in line with the APs.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/setup.c | 2 ++
 xen/arch/x86/traps.c | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/setup.c +2 -0
index bde5d75ea6ab..718297ca541a 100644
@@ -2024,6 +2024,8 @@ void asmlinkage __init noreturn __start_xen(void)
 
     trap_init();
 
+    cpu_init();
+
     rcu_init();
 
     early_time_init();
xen/arch/x86/traps.c +0 -2
index df1155bfb673..a89f4f4f34ea 100644
@@ -1938,8 +1938,6 @@ void __init trap_init(void)
             l1e_from_pfn(virt_to_mfn(boot_compat_gdt), __PAGE_HYPERVISOR_RW);
 
     percpu_traps_init();
-
-    cpu_init();
 }
 
 void asm_domain_crash_synchronous(unsigned long addr)
Signature · 1 line
2.39.5
#3 lore raw

PATCH 2/4 x86/traps: Move percpu_traps_init() into traps-setup.c

In reply to Andrew Cooper

Move the declaration from asm/system.h into asm/traps.h.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/acpi/power.c         |  1 +
 xen/arch/x86/include/asm/system.h |  1 -
 xen/arch/x86/include/asm/traps.h  |  2 ++
 xen/arch/x86/setup.c              |  1 +
 xen/arch/x86/smpboot.c            |  1 +
 xen/arch/x86/traps-setup.c        | 15 +++++++++++++++
 xen/arch/x86/traps.c              |  8 --------
 7 files changed, 20 insertions(+), 9 deletions(-)
xen/arch/x86/acpi/power.c +1 -0
index fe70257864e1..3196a33b1918 100644
@@ -31,6 +31,7 @@
 #include <asm/spec_ctrl.h>
 #include <asm/tboot.h>
 #include <asm/trampoline.h>
+#include <asm/traps.h>
 
 #include <public/platform.h>
 
xen/arch/x86/include/asm/system.h +0 -1
index 73cb16ca68d6..73364056c702 100644
@@ -266,7 +266,6 @@ static inline int local_irq_is_enabled(void)
 void trap_init(void);
 void init_idt_traps(void);
 void load_system_tables(void);
-void percpu_traps_init(void);
 void subarch_percpu_traps_init(void);
 
 #endif
xen/arch/x86/include/asm/traps.h +2 -0
index 01ef362edc8c..825f7441c86e 100644
@@ -7,6 +7,8 @@
 #ifndef ASM_TRAP_H
 #define ASM_TRAP_H
 
+void percpu_traps_init(void);
+
 const char *vector_name(unsigned int vec);
 
 #endif /* ASM_TRAP_H */
xen/arch/x86/setup.c +1 -0
index 718297ca541a..b092106ccca5 100644
@@ -56,6 +56,7 @@
 #include <asm/spec_ctrl.h>
 #include <asm/tboot.h>
 #include <asm/trampoline.h>
+#include <asm/traps.h>
 
 #include <xsm/xsm.h>
 
xen/arch/x86/smpboot.c +1 -0
index b63a9ce13e0a..54207e6d8830 100644
@@ -41,6 +41,7 @@
 #include <asm/tboot.h>
 #include <asm/time.h>
 #include <asm/trampoline.h>
+#include <asm/traps.h>
 
 uint32_t __ro_after_init trampoline_phys;
 enum ap_boot_method __read_mostly ap_boot_method = AP_BOOT_NORMAL;
xen/arch/x86/traps-setup.c +15 -0
index 3ee28319584d..fa78a35a7ebf 100644
@@ -3,5 +3,20 @@
  * Configuration of event handling for all CPUs.
  */
 #include <asm/idt.h>
+#include <asm/msr.h>
+#include <asm/system.h>
+#include <asm/traps.h>
 
 DEFINE_PER_CPU_READ_MOSTLY(idt_entry_t *, idt);
+
+/*
+ * Set up per-CPU linkage registers for exception, interrupt and syscall
+ * handling.
+ */
+void percpu_traps_init(void)
+{
+    subarch_percpu_traps_init();
+
+    if ( cpu_has_xen_lbr )
+        wrmsrl(MSR_IA32_DEBUGCTLMSR, IA32_DEBUGCTLMSR_LBR);
+}
xen/arch/x86/traps.c +0 -8
index a89f4f4f34ea..29d7e4123f90 100644
@@ -1864,14 +1864,6 @@ void asmlinkage do_entry_CP(struct cpu_user_regs *regs)
     panic("CONTROL-FLOW PROTECTION FAULT: #CP[%04x] %s\n", ec, err);
 }
 
-void percpu_traps_init(void)
-{
-    subarch_percpu_traps_init();
-
-    if ( cpu_has_xen_lbr )
-        wrmsrl(MSR_IA32_DEBUGCTLMSR, IA32_DEBUGCTLMSR_LBR);
-}
-
 void nocall entry_PF(void);
 
 void __init init_idt_traps(void)
Signature · 1 line
2.39.5
Acked-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] x86/traps: Move percpu_traps_init() into traps-setup.c

Quoted 5 lines · Andrew Cooper

Move the declaration from asm/system.h into asm/traps.h.

No functional change.

Quoted 9 lines
xen/arch/x86/traps-setup.c +3 -0
@@ -3,5 +3,20 @@
  * Configuration of event handling for all CPUs.
  */
 #include <asm/idt.h>
+#include <asm/msr.h>
+#include <asm/system.h>
+#include <asm/traps.h>

xen/arch/x86/traps-setup.c line 18

Quoted 10 lines
xen/arch/x86/traps-setup.c +8 -0
 
 DEFINE_PER_CPU_READ_MOSTLY(idt_entry_t *, idt);
+
+/*
+ * Set up per-CPU linkage registers for exception, interrupt and syscall
+ * handling.
+ */
+void percpu_traps_init(void)
+{
+    subarch_percpu_traps_init();

At some point this may then want expanding into here, seeing that we don't have any subarch anymore, and don't expect to re-gain one any time soon?

Jan

#5 lore raw

Re: [PATCH 2/4] x86/traps: Move percpu_traps_init() into traps-setup.c

Quoted 7 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper

Move the declaration from asm/system.h into asm/traps.h.

No functional change.

Thanks.

Quoted 11 lines
Quoted 11 lines
xen/arch/x86/traps-setup.c +3 -0
@@ -3,5 +3,20 @@
  * Configuration of event handling for all CPUs.
  */
 #include <asm/idt.h>
+#include <asm/msr.h>
+#include <asm/system.h>
+#include <asm/traps.h>
 
Quoted 11 lines

xen/arch/x86/traps-setup.c line 18

Quoted 9 lines
xen/arch/x86/traps-setup.c +8 -0
 DEFINE_PER_CPU_READ_MOSTLY(idt_entry_t *, idt);
+
+/*
+ * Set up per-CPU linkage registers for exception, interrupt and syscall
+ * handling.
+ */
+void percpu_traps_init(void)
+{
+    subarch_percpu_traps_init();

At some point this may then want expanding into here, seeing that we don't have any subarch anymore, and don't expect to re-gain one any time soon?

I have folded it later in the FRED series.

This, and load_system_tables() need non-trivial alterations to support FRED, and I haven't got that part of the work cleaned up suitable to post yet.

~Andrew

#6 lore raw

PATCH 3/4 x86/traps: Move trap_init() into traps-setup.c

In reply to Andrew Cooper

... and rename to traps_init() for consistency. Move the declaration from asm/system.h into asm/traps.h.

This also involves moving init_ler() and variables. Move the declaration of ler_msr from asm/msr.h to asm/traps.h.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/include/asm/msr.h    |  2 -
 xen/arch/x86/include/asm/system.h |  1 -
 xen/arch/x86/include/asm/traps.h  |  3 ++
 xen/arch/x86/setup.c              |  2 +-
 xen/arch/x86/traps-setup.c        | 66 +++++++++++++++++++++++++++++++
 xen/arch/x86/traps.c              | 61 ----------------------------
 6 files changed, 70 insertions(+), 65 deletions(-)
xen/arch/x86/include/asm/msr.h +0 -2
index 7b00a4db5d48..549d40b404cb 100644
@@ -279,8 +279,6 @@ static inline void write_efer(uint64_t val)
     wrmsrl(MSR_EFER, val);
 }
 
-extern unsigned int ler_msr;
-
 DECLARE_PER_CPU(uint32_t, tsc_aux);
 
 /* Lazy update of MSR_TSC_AUX */
xen/arch/x86/include/asm/system.h +0 -1
index 73364056c702..1bf6bd026852 100644
@@ -263,7 +263,6 @@ static inline int local_irq_is_enabled(void)
 #define BROKEN_ACPI_Sx          0x0001
 #define BROKEN_INIT_AFTER_S1    0x0002
 
-void trap_init(void);
 void init_idt_traps(void);
 void load_system_tables(void);
 void subarch_percpu_traps_init(void);
xen/arch/x86/include/asm/traps.h +3 -0
index 825f7441c86e..3d30aa6738d4 100644
@@ -7,8 +7,11 @@
 #ifndef ASM_TRAP_H
 #define ASM_TRAP_H
 
+void traps_init(void);
 void percpu_traps_init(void);
 
+extern unsigned int ler_msr;
+
 const char *vector_name(unsigned int vec);
 
 #endif /* ASM_TRAP_H */
xen/arch/x86/setup.c +1 -1
index b092106ccca5..5e2411a008f5 100644
@@ -2023,7 +2023,7 @@ void asmlinkage __init noreturn __start_xen(void)
                                            &this_cpu(stubs).mfn);
     BUG_ON(!this_cpu(stubs.addr));
 
-    trap_init();
+    traps_init(); /* Needs stubs allocated. */
 
     cpu_init();
 
xen/arch/x86/traps-setup.c +66 -0

78 lines not on this page · raw message

xen/arch/x86/traps.c +0 -61

79 lines not on this page · raw message

Signature · 1 line
2.39.5
Acked-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] x86/traps: Move trap_init() into traps-setup.c

Quoted 9 lines · Andrew Cooper

... and rename to traps_init() for consistency. Move the declaration from asm/system.h into asm/traps.h.

This also involves moving init_ler() and variables. Move the declaration of ler_msr from asm/msr.h to asm/traps.h.

No functional change.

#8 lore raw

PATCH 4/4 x86/traps: Introduce early_traps_init() and simplify setup

In reply to Andrew Cooper

Something I overlooked when last cleaning up exception handling is that a TSS is not necessary if IST isn't configured, and IST isn't necessary until we're running guest code.

Introduce early_traps_init() which is far more minimal than init_idt_traps(); bsp_ist[] is constructed without IST settings, so all early_traps_init() needs to do is load the IDT, and invalidate TR/LDTR.

Put the remaining logic into traps_init(), later on boot. Note that load_system_tables() already contains enable_each_ist(), so the call is simply dropped.

This removes some complexity prior to having exception support, and lays the groundwork to not even allocate a TSS when using FRED.

No practical change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/include/asm/system.h |  1 -
 xen/arch/x86/include/asm/traps.h  |  1 +
 xen/arch/x86/setup.c              |  3 +--
 xen/arch/x86/traps-setup.c        | 32 +++++++++++++++++++++++++++++++
 xen/arch/x86/traps.c              | 13 -------------
 5 files changed, 34 insertions(+), 16 deletions(-)
xen/arch/x86/include/asm/system.h +0 -1
index 1bf6bd026852..8ceaaf45d1a0 100644
@@ -263,7 +263,6 @@ static inline int local_irq_is_enabled(void)
 #define BROKEN_ACPI_Sx          0x0001
 #define BROKEN_INIT_AFTER_S1    0x0002
 
-void init_idt_traps(void);
 void load_system_tables(void);
 void subarch_percpu_traps_init(void);
 
xen/arch/x86/include/asm/traps.h +1 -0
index 3d30aa6738d4..72c33a33e283 100644
@@ -7,6 +7,7 @@
 #ifndef ASM_TRAP_H
 #define ASM_TRAP_H
 
+void early_traps_init(void);
 void traps_init(void);
 void percpu_traps_init(void);
 
xen/arch/x86/setup.c +1 -2
index 5e2411a008f5..6a6ddc569500 100644
@@ -1093,8 +1093,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     percpu_init_areas();
 
-    init_idt_traps();
-    load_system_tables();
+    early_traps_init();
 
     smp_prepare_boot_cpu();
     sort_exception_tables();
xen/arch/x86/traps-setup.c +32 -0
index 1a7b42c14bf2..67a6fda89ad9 100644
@@ -55,6 +55,31 @@ static void __init init_ler(void)
     setup_force_cpu_cap(X86_FEATURE_XEN_LBR);
 }
 
+/*
+ * Configure basic exception handling.  This is prior to parsing the command
+ * line or configuring a console, and needs to be as simple as possible.
+ *
+ * boot_gdt is already loaded, and bsp_idt[] is constructed without IST
+ * settings, so we don't need a TSS configured yet.
+ *
+ * Load bsp_idt[], and invalidate the TSS and LDT.
+ */
+void __init early_traps_init(void)
+{
+    const struct desc_ptr idtr = {
+        .base = (unsigned long)bsp_idt,
+        .limit = sizeof(bsp_idt) - 1,
+    };
+
+    lidt(&idtr);
+
+    _set_tssldt_desc(boot_gdt + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
+                     0, 0, SYS_DESC_tss_avail);
+
+    ltr(TSS_SELECTOR);
+    lldt(0);
+}
+
 /*
  * Configure complete exception, interrupt and syscall handling.
  */
@@ -63,6 +88,13 @@ void __init traps_init(void)
     /* Replace early pagefault with real pagefault handler. */
     _update_gate_addr_lower(&bsp_idt[X86_EXC_PF], entry_PF);
 
+    this_cpu(idt) = bsp_idt;
+    this_cpu(gdt) = boot_gdt;
+    if ( IS_ENABLED(CONFIG_PV32) )
+        this_cpu(compat_gdt) = boot_compat_gdt;
+
+    load_system_tables();
+
     init_ler();
 
     /* Cache {,compat_}gdt_l1e now that physically relocation is done. */
xen/arch/x86/traps.c +0 -13
index 79d92f21acf5..25e0d5777e6e 100644
@@ -1858,19 +1858,6 @@ void asmlinkage do_entry_CP(struct cpu_user_regs *regs)
     panic("CONTROL-FLOW PROTECTION FAULT: #CP[%04x] %s\n", ec, err);
 }
 
-void __init init_idt_traps(void)
-{
-    /* Specify dedicated interrupt stacks for NMI, #DF, and #MC. */
-    enable_each_ist(bsp_idt);
-
-    /* CPU0 uses the master IDT. */
-    this_cpu(idt) = bsp_idt;
-
-    this_cpu(gdt) = boot_gdt;
-    if ( IS_ENABLED(CONFIG_PV32) )
-        this_cpu(compat_gdt) = boot_compat_gdt;
-}
-
 void asm_domain_crash_synchronous(unsigned long addr)
 {
     /*
Signature · 1 line
2.39.5
Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] x86/traps: Introduce early_traps_init() and simplify setup

Quoted 12 lines · Andrew Cooper

Something I overlooked when last cleaning up exception handling is that a TSS is not necessary if IST isn't configured, and IST isn't necessary until we're running guest code.

Introduce early_traps_init() which is far more minimal than init_idt_traps(); bsp_ist[] is constructed without IST settings, so all early_traps_init() needs to do is load the IDT, and invalidate TR/LDTR.

Put the remaining logic into traps_init(), later on boot. Note that load_system_tables() already contains enable_each_ist(), so the call is simply dropped.

Quoted 6 lines

This removes some complexity prior to having exception support, and lays the groundwork to not even allocate a TSS when using FRED.

No practical change.

Given the present state of thing:

What worries me slightly is that ...

xen/arch/x86/traps-setup.c void __init traps_init(void) · line 94

Quoted 8 lines
xen/arch/x86/traps-setup.c +4 -0
@@ -63,6 +88,13 @@ void __init traps_init(void)
     /* Replace early pagefault with real pagefault handler. */
     _update_gate_addr_lower(&bsp_idt[X86_EXC_PF], entry_PF);
 
+    this_cpu(idt) = bsp_idt;
+    this_cpu(gdt) = boot_gdt;
+    if ( IS_ENABLED(CONFIG_PV32) )
+        this_cpu(compat_gdt) = boot_compat_gdt;

... this being done later now requires more care with e.g. play_dead(). Yet if and when needed, this setting up could of course be moved earlier again. It's not entirely clear to me why you specifically want it here and not in early_traps_init(). The sole dependency is percpu_init_areas(), which runs - as even visible from patch context here - ahead of early_traps_init(). My take is - the earlier we do things that can be done easily, the less we have to worry about ordering and dependencies.

Jan

#10 lore raw

Re: [PATCH 4/4] x86/traps: Introduce early_traps_init() and simplify setup

Quoted 13 lines · Jan Beulich
Quoted 12 lines · Andrew Cooper

Something I overlooked when last cleaning up exception handling is that a TSS is not necessary if IST isn't configured, and IST isn't necessary until we're running guest code.

Introduce early_traps_init() which is far more minimal than init_idt_traps(); bsp_ist[] is constructed without IST settings, so all early_traps_init() needs to do is load the IDT, and invalidate TR/LDTR.

Put the remaining logic into traps_init(), later on boot. Note that load_system_tables() already contains enable_each_ist(), so the call is simply dropped.

Quoted 8 lines
Quoted 6 lines

This removes some complexity prior to having exception support, and lays the groundwork to not even allocate a TSS when using FRED.

No practical change.

Given the present state of thing:

Thanks.

Quoted 11 lines

What worries me slightly is that ...

xen/arch/x86/traps-setup.c void __init traps_init(void) · line 94

Quoted 8 lines
xen/arch/x86/traps-setup.c +4 -0
@@ -63,6 +88,13 @@ void __init traps_init(void)
     /* Replace early pagefault with real pagefault handler. */
     _update_gate_addr_lower(&bsp_idt[X86_EXC_PF], entry_PF);
 
+    this_cpu(idt) = bsp_idt;
+    this_cpu(gdt) = boot_gdt;
+    if ( IS_ENABLED(CONFIG_PV32) )
+        this_cpu(compat_gdt) = boot_compat_gdt;

... this being done later now requires more care with e.g. play_dead().

Hmm.  There are multiple things at play here.

load_system_tables() is the important one to defer, and when FRED is active there really is no IDT.  However, when we're using IDT for early handing, we probably ought to have the pointer set up.

So I think this block wants to move back early.  It's also used in NMI crash shootdown too.

Quoted 3 lines

Yet if and when needed, this setting up could of course be moved earlier again. It's not entirely clear to me why you specifically want it here and not in early_traps_init().

Because I was looking to not even set up an IDT when FRED is in use.

But as said, we probably want to set it up, and then invalidate it when turning on FRED.

Quoted 3 lines

The sole dependency is percpu_init_areas(),
which runs - as even visible from patch context here - ahead of early_traps_init().

percpu_init_areas() isn't even a dependency.  Notice how it only starts from CPU1.  CPU0 (which is what this_cpu() refers to in both of these functions) is correct (i.e. 0) at build time.

I still intend to remove percpu_init_areas().  It's all build-time constant; it just needs doing by the assembler, rather than the compiler, as (-(long)__per_cpu_start) is not an integer constant expression.

~Andrew

#11 lore raw

Re: [PATCH 4/4] x86/traps: Introduce early_traps_init() and simplify setup

Quoted 8 lines · Andrew Cooper
Quoted 3 lines · Jan Beulich

The sole dependency is percpu_init_areas(),
which runs - as even visible from patch context here - ahead of early_traps_init().

percpu_init_areas() isn't even a dependency.  Notice how it only starts from CPU1.  CPU0 (which is what this_cpu() refers to in both of these functions) is correct (i.e. 0) at build time.

Oh, right.

Quoted 3 lines

I still intend to remove percpu_init_areas().  It's all build-time constant; it just needs doing by the assembler, rather than the compiler, as (-(long)__per_cpu_start) is not an integer constant expression.

Except that this value also cannot be expressed by a relocation, afaict, so the assembler would choke when trying to write the object file. (Same for x86'es alternative initializer.)

Jan

#12 lore raw

Re: [PATCH 4/4] x86/traps: Introduce early_traps_init() and simplify setup

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 3 lines · Jan Beulich

The sole dependency is percpu_init_areas(),
which runs - as even visible from patch context here - ahead of early_traps_init().

percpu_init_areas() isn't even a dependency.  Notice how it only starts from CPU1.  CPU0 (which is what this_cpu() refers to in both of these functions) is correct (i.e. 0) at build time.

Quoted 8 lines

Oh, right.

Quoted 3 lines

I still intend to remove percpu_init_areas().  It's all build-time constant; it just needs doing by the assembler, rather than the compiler, as (-(long)__per_cpu_start) is not an integer constant expression.

Except that this value also cannot be expressed by a relocation, afaict, so the assembler would choke when trying to write the object file. (Same for x86'es alternative initializer.)

Yeah, I found that while experimenting.

However, the PROVIDE_HIDDEN() trick I used for IDT autogen does work.

There are a couple of nasty thing.  While we have ASM_INT() now, we don't have ASM_PTR() or equivalent, which is needed because this is a common file and we can't just use .quad.

Also, because this is an asm() block in a C file, we can't use any of linkage.h; opencoding it isn't too bad, but it also appears that __read_mostly doesn't honour -fdata-sections.

~Andrew

#13 lore raw

PATCH v2 4/4 x86/traps: Introduce early_traps_init() and simplify setup

In reply to Andrew Cooper

Something I overlooked when last cleaning up exception handling is that a TSS is not necessary if IST isn't configured, and IST isn't necessary until we're running guest code.

Introduce early_traps_init(), and rearrange the existing logic between this and traps_init() later on boot, to allow defering TSS and IST setup.

In early_traps_init(), load the IDT and invalidate TR/LDTR; this sufficient system-table setup to make exception handling work. The setup of the BSPs per-cpu variables stay early too; they're used on certain error paths.

Move load_system_tables() later into traps_init(). Note that it already contains enable_each_ist(), so the call is simply dropped.

This removes some complexity prior to having exception support, and lays the groundwork to not even allocate a TSS when using FRED.

No practical change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

v2:
* Keep this_cpu(idt) early.

---
 xen/arch/x86/include/asm/system.h |  1 -
 xen/arch/x86/include/asm/traps.h  |  1 +
 xen/arch/x86/setup.c              |  5 ++---
 xen/arch/x86/traps-setup.c        | 32 +++++++++++++++++++++++++++++++
 xen/arch/x86/traps.c              | 13 -------------
 5 files changed, 35 insertions(+), 17 deletions(-)
xen/arch/x86/include/asm/system.h +0 -1
index 1bf6bd026852..8ceaaf45d1a0 100644
@@ -263,7 +263,6 @@ static inline int local_irq_is_enabled(void)
 #define BROKEN_ACPI_Sx          0x0001
 #define BROKEN_INIT_AFTER_S1    0x0002
 
-void init_idt_traps(void);
 void load_system_tables(void);
 void subarch_percpu_traps_init(void);
 
xen/arch/x86/include/asm/traps.h +1 -0
index 3d30aa6738d4..72c33a33e283 100644
@@ -7,6 +7,7 @@
 #ifndef ASM_TRAP_H
 #define ASM_TRAP_H
 
+void early_traps_init(void);
 void traps_init(void);
 void percpu_traps_init(void);
 
xen/arch/x86/setup.c +2 -3
index 993a6202a398..7d6e52a55f2f 100644
@@ -1087,14 +1087,13 @@ void asmlinkage __init noreturn __start_xen(void)
     };
     const char *hypervisor_name;
 
-    /* Critical region without IDT or TSS.  Any fault is deadly! */
+    /* Critical region without exception handling.  Any fault is deadly! */
 
     init_shadow_spec_ctrl_state();
 
     percpu_init_areas();
 
-    init_idt_traps();
-    load_system_tables();
+    early_traps_init();
 
     smp_prepare_boot_cpu();
     sort_exception_tables();
xen/arch/x86/traps-setup.c +32 -0
index 1a7b42c14bf2..a8385b26ae9b 100644
@@ -55,6 +55,36 @@ static void __init init_ler(void)
     setup_force_cpu_cap(X86_FEATURE_XEN_LBR);
 }
 
+/*
+ * Configure basic exception handling.  This is prior to parsing the command
+ * line or configuring a console, and needs to be as simple as possible.
+ *
+ * boot_gdt is already loaded, and bsp_idt[] is constructed without IST
+ * settings, so we don't need a TSS configured yet.
+ */
+void __init early_traps_init(void)
+{
+    const struct desc_ptr idtr = {
+        .base = (unsigned long)bsp_idt,
+        .limit = sizeof(bsp_idt) - 1,
+    };
+
+    lidt(&idtr);
+
+    /* Invalidate TR/LDTR as they're not set up yet. */
+    _set_tssldt_desc(boot_gdt + TSS_ENTRY - FIRST_RESERVED_GDT_ENTRY,
+                     0, 0, SYS_DESC_tss_avail);
+
+    ltr(TSS_SELECTOR);
+    lldt(0);
+
+    /* Set up the BSPs per-cpu references. */
+    this_cpu(idt) = bsp_idt;
+    this_cpu(gdt) = boot_gdt;
+    if ( IS_ENABLED(CONFIG_PV32) )
+        this_cpu(compat_gdt) = boot_compat_gdt;
+}
+
 /*
  * Configure complete exception, interrupt and syscall handling.
  */
@@ -63,6 +93,8 @@ void __init traps_init(void)
     /* Replace early pagefault with real pagefault handler. */
     _update_gate_addr_lower(&bsp_idt[X86_EXC_PF], entry_PF);
 
+    load_system_tables();
+
     init_ler();
 
     /* Cache {,compat_}gdt_l1e now that physically relocation is done. */
xen/arch/x86/traps.c +0 -13
index 79d92f21acf5..25e0d5777e6e 100644
@@ -1858,19 +1858,6 @@ void asmlinkage do_entry_CP(struct cpu_user_regs *regs)
     panic("CONTROL-FLOW PROTECTION FAULT: #CP[%04x] %s\n", ec, err);
 }
 
-void __init init_idt_traps(void)
-{
-    /* Specify dedicated interrupt stacks for NMI, #DF, and #MC. */
-    enable_each_ist(bsp_idt);
-
-    /* CPU0 uses the master IDT. */
-    this_cpu(idt) = bsp_idt;
-
-    this_cpu(gdt) = boot_gdt;
-    if ( IS_ENABLED(CONFIG_PV32) )
-        this_cpu(compat_gdt) = boot_compat_gdt;
-}
-
 void asm_domain_crash_synchronous(unsigned long addr)
 {
     /*

base-commit: 6e5fed7cb67c9f84653cdbd3924b8a119ef653be
prerequisite-patch-id: 832a3f5dd12d56454ae74d4ae08d1893c672733e
prerequisite-patch-id: 2f5a1dc0cf663d27c66076f809063764e7a99e61
prerequisite-patch-id: 1f25c130d5a1ee15884dfebad9f5d2a4e69f673a

Signature · 1 line
2.39.5
Reviewed-by on v2 4/4
#14 lore raw

Re: [PATCH v2 4/4] x86/traps: Introduce early_traps_init() and simplify setup

Quoted 14 lines · Andrew Cooper

Something I overlooked when last cleaning up exception handling is that a TSS is not necessary if IST isn't configured, and IST isn't necessary until we're running guest code.

Introduce early_traps_init(), and rearrange the existing logic between this and traps_init() later on boot, to allow defering TSS and IST setup.

In early_traps_init(), load the IDT and invalidate TR/LDTR; this sufficient system-table setup to make exception handling work. The setup of the BSPs per-cpu variables stay early too; they're used on certain error paths.

Move load_system_tables() later into traps_init(). Note that it already contains enable_each_ist(), so the call is simply dropped.

Quoted 6 lines

This removes some complexity prior to having exception support, and lays the groundwork to not even allocate a TSS when using FRED.

No practical change.