Skip to contentSkip to conversation

x86/IDT: Generate the IDT at build time

PATCH In review Andrew Cooper 2 participants 12 messages Mar 5, 2025 – Mar 7, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover x86/IDT: Generate the IDT at build time

This is a chunk of the FRED work split out because it's pretty self contained.

It's mostly cleanup/refactoring, although patch

traps.c is already overly large and going to get larger with FRED, so I'm splitting traps-setup.c out of it, as the two have reasonably-different logic. That's implemented in this series but not used in anger in this series.

Since v1:
* First 3 patches committed
* Minor adjustments. See individual patches for details

Andrew Cooper (5):
x86/IDT: Rename idt_table[] to bsp_idt[] x86/IDT: Make idt_tables[] be per_cpu(idt) x86/IDT: Generate bsp_idt[] at build time x86/IDT: Don't rewrite bsp_idt[] at boot time x86/traps: Convert pv_trap_init() to being an initcall

 xen/arch/x86/Makefile                  |   1 +
 xen/arch/x86/cpu/common.c              |   7 +-
 xen/arch/x86/crash.c                   |   8 +-
 xen/arch/x86/domain.c                  |   2 +-
 xen/arch/x86/hvm/svm/svm.c             |   4 +-
 xen/arch/x86/hvm/vmx/vmcs.c            |   2 +-
 xen/arch/x86/include/asm/gen-idt.h     | 122 +++++++++++++++++++++++++
 xen/arch/x86/include/asm/gen-idt.lds.h |  27 ++++++
 xen/arch/x86/include/asm/idt.h         |  22 +----
 xen/arch/x86/include/asm/pv/traps.h    |   4 -
 xen/arch/x86/machine_kexec.c           |   7 +-
 xen/arch/x86/pv/traps.c                |  18 +---
 xen/arch/x86/smpboot.c                 |  14 +--
 xen/arch/x86/traps-setup.c             |   7 ++
 xen/arch/x86/traps.c                   |  89 +-----------------
 xen/arch/x86/x86_64/entry.S            | 106 ++++++++++++---------
 xen/arch/x86/xen.lds.S                 |   2 +
 17 files changed, 254 insertions(+), 188 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/gen-idt.h
 create mode 100644 xen/arch/x86/include/asm/gen-idt.lds.h
 create mode 100644 xen/arch/x86/traps-setup.c
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 1/5 x86/IDT: Rename idt_table[] to bsp_idt[]

Having variables named idt_table[] and idt_tables[] is not ideal.

Use X86_IDT_VECTORS and remove IDT_ENTRIES. State the size of bsp_idt[] in idt.h so that load_system_tables() and cpu_smpboot_alloc() can use sizeof() rather than opencoding the calculation.

Move the variable into a new traps-setup.c, to make a start at splitting traps.c in half.

No functional change.

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

v2:
* Rename traps-init.c to traps-setup.c

---
 xen/arch/x86/Makefile          |  1 +
 xen/arch/x86/cpu/common.c      |  2 +-
 xen/arch/x86/include/asm/idt.h |  3 +--
 xen/arch/x86/pv/traps.c        |  4 ++--
 xen/arch/x86/smpboot.c         |  2 +-
 xen/arch/x86/traps-setup.c     |  9 +++++++++
 xen/arch/x86/traps.c           | 14 +++++---------
 7 files changed, 20 insertions(+), 15 deletions(-)
 create mode 100644 xen/arch/x86/traps-setup.c
xen/arch/x86/Makefile +1 -0
index b35fd5196ce2..c763f80b0b22 100644
@@ -65,6 +65,7 @@ obj-y += spec_ctrl.o
 obj-y += srat.o
 obj-y += string.o
 obj-y += time.o
+obj-y += traps-setup.o
 obj-y += traps.o
 obj-$(CONFIG_INTEL) += tsx.o
 obj-y += usercopy.o
xen/arch/x86/cpu/common.c +1 -1
index 1540ab0007a0..e8b355ebcf36 100644
@@ -831,7 +831,7 @@ void load_system_tables(void)
 	};
 	const struct desc_ptr idtr = {
 		.base = (unsigned long)idt_tables[cpu],
-		.limit = (IDT_ENTRIES * sizeof(idt_entry_t)) - 1,
+		.limit = sizeof(bsp_idt) - 1,
 	};
 
 	/*
xen/arch/x86/include/asm/idt.h +1 -2
index 3689fdecbec3..f00368f28c86 100644
@@ -28,8 +28,7 @@ typedef union {
     };
 } idt_entry_t;
 
-#define IDT_ENTRIES 256
-extern idt_entry_t idt_table[];
+extern idt_entry_t bsp_idt[X86_IDT_VECTORS];
 extern idt_entry_t *idt_tables[];
 
 /*
xen/arch/x86/pv/traps.c +2 -2
index 77b034e4dc73..4aeb6cab5238 100644
@@ -148,12 +148,12 @@ void __init pv_trap_init(void)
 {
 #ifdef CONFIG_PV32
     /* The 32-on-64 hypercall vector is only accessible from ring 1. */
-    _set_gate(idt_table + HYPERCALL_VECTOR,
+    _set_gate(bsp_idt + HYPERCALL_VECTOR,
               SYS_DESC_irq_gate, 1, entry_int82);
 #endif
 
     /* Fast trap for int80 (faster than taking the #GP-fixup path). */
-    _set_gate(idt_table + LEGACY_SYSCALL_VECTOR, SYS_DESC_irq_gate, 3,
+    _set_gate(bsp_idt + LEGACY_SYSCALL_VECTOR, SYS_DESC_irq_gate, 3,
               &entry_int80);
 
     open_softirq(NMI_SOFTIRQ, nmi_softirq);
xen/arch/x86/smpboot.c +1 -1
index f3d60d5bae35..dc65f9e45269 100644
@@ -1080,7 +1080,7 @@ static int cpu_smpboot_alloc(unsigned int cpu)
         idt_tables[cpu] = alloc_xenheap_pages(0, memflags);
     if ( idt_tables[cpu] == NULL )
         goto out;
-    memcpy(idt_tables[cpu], idt_table, IDT_ENTRIES * sizeof(idt_entry_t));
+    memcpy(idt_tables[cpu], bsp_idt, sizeof(bsp_idt));
     disable_each_ist(idt_tables[cpu]);
 
     for ( stub_page = 0, i = cpu & ~(STUBS_PER_PAGE - 1);
xen/arch/x86/traps-setup.c new file +9 -0
new file mode 100644
index 000000000000..b172ea933607
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Configuration of event handling for all CPUs.
+ */
+#include <asm/idt.h>
+#include <asm/page.h>
+
+idt_entry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
+    bsp_idt[X86_IDT_VECTORS];
xen/arch/x86/traps.c +5 -9
index 4d1aaa78e711..7a68996b02f7 100644
@@ -98,10 +98,6 @@ DEFINE_PER_CPU_READ_MOSTLY(seg_desc_t *, compat_gdt);
 DEFINE_PER_CPU_READ_MOSTLY(l1_pgentry_t, compat_gdt_l1e);
 #endif
 
-/* Master table, used by CPU0. */
-idt_entry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    idt_table[IDT_ENTRIES];
-
 /* Pointer to the IDT of every CPU. */
 idt_entry_t *idt_tables[NR_CPUS] __read_mostly;
 
@@ -1874,7 +1870,7 @@ void asmlinkage do_entry_CP(struct cpu_user_regs *regs)
 static void __init noinline __set_intr_gate(unsigned int n,
                                             uint32_t dpl, void *addr)
 {
-    _set_gate(&idt_table[n], SYS_DESC_irq_gate, dpl, addr);
+    _set_gate(&bsp_idt[n], SYS_DESC_irq_gate, dpl, addr);
 }
 
 static void __init set_swint_gate(unsigned int n, void *addr)
@@ -1940,10 +1936,10 @@ void __init init_idt_traps(void)
     set_intr_gate (X86_EXC_CP,  entry_CP);
 
     /* Specify dedicated interrupt stacks for NMI, #DF, and #MC. */
-    enable_each_ist(idt_table);
+    enable_each_ist(bsp_idt);
 
     /* CPU0 uses the master IDT. */
-    idt_tables[0] = idt_table;
+    idt_tables[0] = bsp_idt;
 
     this_cpu(gdt) = boot_gdt;
     if ( IS_ENABLED(CONFIG_PV32) )
@@ -2001,13 +1997,13 @@ void __init trap_init(void)
         if ( autogen_entrypoints[vector] )
         {
             /* Found autogen entry: check we won't clobber an existing trap. */
-            ASSERT(idt_table[vector].b == 0);
+            ASSERT(bsp_idt[vector].b == 0);
             set_intr_gate(vector, autogen_entrypoints[vector]);
         }
         else
         {
             /* No entry point: confirm we have an existing trap in place. */
-            ASSERT(idt_table[vector].b != 0);
+            ASSERT(bsp_idt[vector].b != 0);
         }
     }
 
Signature · 1 line
2.39.5
#2 lore raw

PATCH v2 2/5 x86/IDT: Make idt_tables[] be per_cpu(idt)

In reply to Andrew Cooper

This can be a plain per_cpu() variable, and __read_mostly seeing as it's allocated once and never touched again.

This removes a NR_CPU's sized structure, and improves NUMA locality of access for both the the VT-x and SVM context switch paths.

No functional change.

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

---
 xen/arch/x86/cpu/common.c      |  5 +++--
 xen/arch/x86/crash.c           |  8 ++++----
 xen/arch/x86/domain.c          |  2 +-
 xen/arch/x86/hvm/svm/svm.c     |  4 ++--
 xen/arch/x86/hvm/vmx/vmcs.c    |  2 +-
 xen/arch/x86/include/asm/idt.h |  3 ++-
 xen/arch/x86/machine_kexec.c   |  7 +++++--
 xen/arch/x86/smpboot.c         | 14 +++++++-------
 xen/arch/x86/traps-setup.c     |  2 ++
 xen/arch/x86/traps.c           |  5 +----
 10 files changed, 28 insertions(+), 24 deletions(-)
xen/arch/x86/cpu/common.c +3 -2
index e8b355ebcf36..e8d4ca3203be 100644
@@ -819,6 +819,7 @@ void load_system_tables(void)
 	 * support using ARRAY_SIZE against per-cpu variables.
 	 */
 	struct tss_page *tss_page = &this_cpu(tss_page);
+	idt_entry_t *idt = this_cpu(idt);
 
 	/* The TSS may be live.	 Disuade any clever optimisations. */
 	volatile struct tss64 *tss = &tss_page->tss;
@@ -830,7 +831,7 @@ void load_system_tables(void)
 		.limit = LAST_RESERVED_GDT_BYTE,
 	};
 	const struct desc_ptr idtr = {
-		.base = (unsigned long)idt_tables[cpu],
+		.base = (unsigned long)idt,
 		.limit = sizeof(bsp_idt) - 1,
 	};
 
@@ -914,7 +915,7 @@ void load_system_tables(void)
 	ltr(TSS_SELECTOR);
 	lldt(0);
 
-	enable_each_ist(idt_tables[cpu]);
+	enable_each_ist(idt);
 
 	/*
 	 * Bottom-of-stack must be 16-byte aligned!
xen/arch/x86/crash.c +4 -4
index 5f7d7b392a1f..1e4b0eeff21b 100644
@@ -63,7 +63,7 @@ static int noreturn cf_check do_nmi_crash(
          * This update is safe from a security point of view, as this
          * pcpu is never going to try to sysret back to a PV vcpu.
          */
-        set_ist(&idt_tables[cpu][X86_EXC_MC], IST_NONE);
+        set_ist(&per_cpu(idt, cpu)[X86_EXC_MC], IST_NONE);
 
         kexec_crash_save_cpu();
         __stop_this_cpu();
@@ -120,6 +120,7 @@ static void nmi_shootdown_cpus(void)
 {
     unsigned long msecs;
     unsigned int cpu = smp_processor_id();
+    idt_entry_t *idt = this_cpu(idt);
 
     disable_lapic_nmi_watchdog();
     local_irq_disable();
@@ -133,9 +134,8 @@ static void nmi_shootdown_cpus(void)
      * Disable IST for MCEs to avoid stack corruption race conditions, and
      * change the NMI handler to a nop to avoid deviation from this codepath.
      */
-    _set_gate_lower(&idt_tables[cpu][X86_EXC_NMI],
-                    SYS_DESC_irq_gate, 0, &trap_nop);
-    set_ist(&idt_tables[cpu][X86_EXC_MC], IST_NONE);
+    _set_gate_lower(&idt[X86_EXC_NMI], SYS_DESC_irq_gate, 0, &trap_nop);
+    set_ist(&idt[X86_EXC_MC], IST_NONE);
 
     set_nmi_callback(do_nmi_crash);
     smp_send_nmi_allbutself();
xen/arch/x86/domain.c +1 -1
index d3db76833f3c..a42fa5480593 100644
@@ -116,7 +116,7 @@ void play_dead(void)
     local_irq_disable();
 
     /* Change the NMI handler to a nop (see comment below). */
-    _set_gate_lower(&idt_tables[cpu][X86_EXC_NMI], SYS_DESC_irq_gate, 0,
+    _set_gate_lower(&this_cpu(idt)[X86_EXC_NMI], SYS_DESC_irq_gate, 0,
                     &trap_nop);
 
     /*
xen/arch/x86/hvm/svm/svm.c +2 -2
index ea78da4f4210..4eac89964f61 100644
@@ -915,7 +915,7 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
     svm_vmload_pa(per_cpu(host_vmcb, cpu));
 
     /* Resume use of ISTs now that the host TR is reinstated. */
-    enable_each_ist(idt_tables[cpu]);
+    enable_each_ist(per_cpu(idt, cpu));
 
     /*
      * Possibly clear previous guest selection of SSBD if set.  Note that
@@ -944,7 +944,7 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
      * Cannot use ISTs for NMI/#MC/#DF while we are running with the guest TR.
      * But this doesn't matter: the IST is only req'd to handle SYSCALL/SYSRET.
      */
-    disable_each_ist(idt_tables[cpu]);
+    disable_each_ist(per_cpu(idt, cpu));
 
     svm_restore_dr(v);
 
xen/arch/x86/hvm/vmx/vmcs.c +1 -1
index 20ab2d0f266f..e47a6e1542b7 100644
@@ -917,7 +917,7 @@ static void vmx_set_host_env(struct vcpu *v)
 
     __vmwrite(HOST_GDTR_BASE,
               (unsigned long)(this_cpu(gdt) - FIRST_RESERVED_GDT_ENTRY));
-    __vmwrite(HOST_IDTR_BASE, (unsigned long)idt_tables[cpu]);
+    __vmwrite(HOST_IDTR_BASE, (unsigned long)per_cpu(idt, cpu));
 
     __vmwrite(HOST_TR_BASE, (unsigned long)&per_cpu(tss_page, cpu).tss);
 
xen/arch/x86/include/asm/idt.h +2 -1
index f00368f28c86..d795798d3eca 100644
@@ -3,6 +3,7 @@
 #define X86_ASM_IDT_H
 
 #include <xen/bug.h>
+#include <xen/percpu.h>
 
 #include <asm/x86-defns.h>
 
@@ -29,7 +30,7 @@ typedef union {
 } idt_entry_t;
 
 extern idt_entry_t bsp_idt[X86_IDT_VECTORS];
-extern idt_entry_t *idt_tables[];
+DECLARE_PER_CPU(idt_entry_t *, idt);
 
 /*
  * Set the Interrupt Stack Table used by a particular IDT entry.  Typically
xen/arch/x86/machine_kexec.c +5 -2
index f775e526d59b..35fa5c82e9c2 100644
@@ -170,9 +170,12 @@ void machine_kexec(struct kexec_image *image)
      */
     for ( i = 0; i < nr_cpu_ids; i++ )
     {
-        if ( idt_tables[i] == NULL )
+        idt_entry_t *idt = per_cpu(idt, i);
+
+        if ( !idt )
             continue;
-        _update_gate_addr_lower(&idt_tables[i][X86_EXC_MC], &trap_nop);
+
+        _update_gate_addr_lower(&idt[X86_EXC_MC], &trap_nop);
     }
 
     /* Reset CPUID masking and faulting to the host's default. */
xen/arch/x86/smpboot.c +7 -7
index dc65f9e45269..4e9f9ac4b2ee 100644
@@ -863,7 +863,7 @@ int setup_cpu_root_pgt(unsigned int cpu)
         rc = clone_mapping(__va(__pa(stack_base[cpu])) + off, rpt);
 
     if ( !rc )
-        rc = clone_mapping(idt_tables[cpu], rpt);
+        rc = clone_mapping(per_cpu(idt, cpu), rpt);
     if ( !rc )
     {
         struct tss_page *ptr = &per_cpu(tss_page, cpu);
@@ -1009,7 +1009,7 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
-        FREE_XENHEAP_PAGE(idt_tables[cpu]);
+        FREE_XENHEAP_PAGE(per_cpu(idt, cpu));
 
         if ( stack_base[cpu] )
         {
@@ -1076,12 +1076,12 @@ static int cpu_smpboot_alloc(unsigned int cpu)
     gdt[PER_CPU_GDT_ENTRY - FIRST_RESERVED_GDT_ENTRY].a = cpu;
 #endif
 
-    if ( idt_tables[cpu] == NULL )
-        idt_tables[cpu] = alloc_xenheap_pages(0, memflags);
-    if ( idt_tables[cpu] == NULL )
+    if ( per_cpu(idt, cpu) == NULL )
+        per_cpu(idt, cpu) = alloc_xenheap_pages(0, memflags);
+    if ( per_cpu(idt, cpu) == NULL )
         goto out;
-    memcpy(idt_tables[cpu], bsp_idt, sizeof(bsp_idt));
-    disable_each_ist(idt_tables[cpu]);
+    memcpy(per_cpu(idt, cpu), bsp_idt, sizeof(bsp_idt));
+    disable_each_ist(per_cpu(idt, cpu));
 
     for ( stub_page = 0, i = cpu & ~(STUBS_PER_PAGE - 1);
           i < nr_cpu_ids && i <= (cpu | (STUBS_PER_PAGE - 1)); ++i )
xen/arch/x86/traps-setup.c +2 -0
index b172ea933607..ae600526cbe3 100644
@@ -7,3 +7,5 @@
 
 idt_entry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
     bsp_idt[X86_IDT_VECTORS];
+
+DEFINE_PER_CPU_READ_MOSTLY(idt_entry_t *, idt);
xen/arch/x86/traps.c +1 -4
index 7a68996b02f7..d52840848d30 100644
@@ -98,9 +98,6 @@ DEFINE_PER_CPU_READ_MOSTLY(seg_desc_t *, compat_gdt);
 DEFINE_PER_CPU_READ_MOSTLY(l1_pgentry_t, compat_gdt_l1e);
 #endif
 
-/* Pointer to the IDT of every CPU. */
-idt_entry_t *idt_tables[NR_CPUS] __read_mostly;
-
 /*
  * The TSS is smaller than a page, but we give it a full page to avoid
  * adjacent per-cpu data leaking via Meltdown when XPTI is in use.
@@ -1939,7 +1936,7 @@ void __init init_idt_traps(void)
     enable_each_ist(bsp_idt);
 
     /* CPU0 uses the master IDT. */
-    idt_tables[0] = bsp_idt;
+    this_cpu(idt) = bsp_idt;
 
     this_cpu(gdt) = boot_gdt;
     if ( IS_ENABLED(CONFIG_PV32) )
Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH v2 2/5] x86/IDT: Make idt_tables[] be per_cpu(idt)

Quoted 2 lines · Andrew Cooper

This can be a plain per_cpu() variable, and __read_mostly seeing as it's allocated once and never touched again.

cpu_smpboot_free() certainly touches (really: modifies) it again. Just that ...

xen/arch/x86/smpboot.c static void cpu_smpboot_free(unsigned int cpu, bool remove) · line 1012

Quoted 6 lines
xen/arch/x86/smpboot.c +1 -1
@@ -1009,7 +1009,7 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
-        FREE_XENHEAP_PAGE(idt_tables[cpu]);
+        FREE_XENHEAP_PAGE(per_cpu(idt, cpu));

... this function-like macro hides that the macro argument is written to. (This is the kind of hiding that elsewhere I think you dislike, yet there macros were an idea of yours, iirc.)

Jan

#4 lore raw

Re: [PATCH v2 2/5] x86/IDT: Make idt_tables[] be per_cpu(idt)

Quoted 4 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

This can be a plain per_cpu() variable, and __read_mostly seeing as it's allocated once and never touched again.

cpu_smpboot_free() certainly touches (really: modifies) it again.

Not really.  That's a dead path because we always park CPUs.

But fine, I'll rephrase to "not touched again during it's lifetime".

~Andrew

#5 lore raw

Re: [PATCH v2 2/5] x86/IDT: Make idt_tables[] be per_cpu(idt)

Quoted 7 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

This can be a plain per_cpu() variable, and __read_mostly seeing as it's allocated once and never touched again.

cpu_smpboot_free() certainly touches (really: modifies) it again.

Not really.  That's a dead path because we always park CPUs.

On Intel hardware; not (yet) on AMD / Hygon iirc.

Jan

Quoted 3 lines

But fine, I'll rephrase to "not touched again during it's lifetime".

~Andrew

#6 lore raw

PATCH v2 3/5 x86/IDT: Generate bsp_idt[] at build time

In reply to Andrew Cooper

... rather than dynamically at boot time. Aside from less runtime overhead, this approach is less fragile than the preexisting autogen stubs mechanism.

We can manage this with some linker calculations. See patch comments for full details.

For simplicity, we create a new set of entry stubs here, and clean up the old ones in the subsequent patch. bsp_idt[] needs to move from .bss to .data.

No functional change yet; the boot path still (re)writes bsp_idt[] at this juncture.

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

v2:
* Shuffle semicolon position
* Eclair magic comment for multi-include files

There's something differnet about LLD vs LD. Without the ABSOLUTE() in gen-idt.lds.h, LD is fine but LLD puts out symbols in the form:

  x86_IDT_entry_0xff_ADDR1|0000000000002fb0|   t  |            NOTYPE|                |     |.text
  x86_IDT_entry_0xff_ADDR2|0000000000004020|   a  |            NOTYPE|                |     |*ABS*

which causes a slew of errors making symbols for xen-syms:

.xen-syms.0.S:20:8: error: out of range literal value
.long 0x15a0 - (((((((261 >> 8) * 0xffff000000000000) | (261 << 39))) + ((1 << 39) / 2)) + (64 << 30)) + (1 << 30))

         ^

owing to half the symbols being t rather than a. Moreover, this is reliable for the full FreeBSD builds, but interminttent on randconfig. I haven't figured out which other option is having an effect.

Forcing them all to absolute works in both toolchains.

---
 xen/arch/x86/include/asm/gen-idt.h     | 122 +++++++++++++++++++++++++
 xen/arch/x86/include/asm/gen-idt.lds.h |  27 ++++++
 xen/arch/x86/traps-setup.c             |   4 -
 xen/arch/x86/x86_64/entry.S            |  76 +++++++++++++++
 xen/arch/x86/xen.lds.S                 |   2 +
 5 files changed, 227 insertions(+), 4 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/gen-idt.h
 create mode 100644 xen/arch/x86/include/asm/gen-idt.lds.h
xen/arch/x86/include/asm/gen-idt.h new file +122 -0
new file mode 100644
index 000000000000..9c8810edf9d7
@@ -0,0 +1,122 @@
+/* This file is intended to be included multiple times. */
+/*
+ * Generator for IDT entries.
+ *
+ * Caller to provide GEN(vector, symbol, dpl, autogen) macro
+ *
+ * Symbols are 'entry_0xYY' if there is no better name available.  Regular
+ * handlers set autogen=1, while manual (autogen=0) require the symbol to be
+ * implemented somewhere else.
+ */
+
+#define DPL0 0
+#define DPL1 1
+#define DPL3 3
+
+#define manual 0
+#define autogen 1
+
+#define GEN16(i) \
+    GEN(0x ## i ## 0, entry_0x ## i ## 0, DPL0, autogen); \
+    GEN(0x ## i ## 1, entry_0x ## i ## 1, DPL0, autogen); \
+    GEN(0x ## i ## 2, entry_0x ## i ## 2, DPL0, autogen); \
+    GEN(0x ## i ## 3, entry_0x ## i ## 3, DPL0, autogen); \
+    GEN(0x ## i ## 4, entry_0x ## i ## 4, DPL0, autogen); \
+    GEN(0x ## i ## 5, entry_0x ## i ## 5, DPL0, autogen); \
+    GEN(0x ## i ## 6, entry_0x ## i ## 6, DPL0, autogen); \
+    GEN(0x ## i ## 7, entry_0x ## i ## 7, DPL0, autogen); \
+    GEN(0x ## i ## 8, entry_0x ## i ## 8, DPL0, autogen); \
+    GEN(0x ## i ## 9, entry_0x ## i ## 9, DPL0, autogen); \
+    GEN(0x ## i ## a, entry_0x ## i ## a, DPL0, autogen); \
+    GEN(0x ## i ## b, entry_0x ## i ## b, DPL0, autogen); \
+    GEN(0x ## i ## c, entry_0x ## i ## c, DPL0, autogen); \
+    GEN(0x ## i ## d, entry_0x ## i ## d, DPL0, autogen); \
+    GEN(0x ## i ## e, entry_0x ## i ## e, DPL0, autogen); \
+    GEN(0x ## i ## f, entry_0x ## i ## f, DPL0, autogen)
+
+
+GEN(0x00, entry_DE,         DPL0, manual);
+GEN(0x01, entry_DB,         DPL0, manual);
+GEN(0x02, entry_NMI,        DPL0, manual);
+GEN(0x03, entry_BP,         DPL3, manual);
+GEN(0x04, entry_OF,         DPL3, manual);
+GEN(0x05, entry_BR,         DPL0, manual);
+GEN(0x06, entry_UD,         DPL0, manual);
+GEN(0x07, entry_NM,         DPL0, manual);
+GEN(0x08, entry_DF,         DPL0, manual);
+GEN(0x09, entry_0x09,       DPL0, autogen); /* Coprocessor Segment Overrun */
+GEN(0x0a, entry_TS,         DPL0, manual);
+GEN(0x0b, entry_NP,         DPL0, manual);
+GEN(0x0c, entry_SS,         DPL0, manual);
+GEN(0x0d, entry_GP,         DPL0, manual);
+GEN(0x0e, early_page_fault, DPL0, manual);
+GEN(0x0f, entry_0x0f,       DPL0, autogen); /* PIC Spurious Interrupt Vector */
+
+GEN(0x10, entry_MF,         DPL0, manual);
+GEN(0x11, entry_AC,         DPL0, manual);
+GEN(0x12, entry_MC,         DPL0, manual);
+GEN(0x13, entry_XM,         DPL0, manual);
+GEN(0x14, entry_VE,         DPL0, autogen);
+GEN(0x15, entry_CP,         DPL0, manual);
+GEN(0x16, entry_0x16,       DPL0, autogen);
+GEN(0x17, entry_0x17,       DPL0, autogen);
+GEN(0x18, entry_0x18,       DPL0, autogen);
+GEN(0x19, entry_0x19,       DPL0, autogen);
+GEN(0x1a, entry_0x1a,       DPL0, autogen);
+GEN(0x1b, entry_0x1b,       DPL0, autogen);
+GEN(0x1c, entry_HV,         DPL0, autogen);
+GEN(0x1d, entry_VC,         DPL0, autogen);
+GEN(0x1e, entry_SX,         DPL0, autogen);
+GEN(0x1f, entry_0x1f,       DPL0, autogen);
+
+GEN16(2);
+GEN16(3);
+GEN16(4);
+GEN16(5);
+GEN16(6);
+GEN16(7);
+
+#ifdef CONFIG_PV
+GEN(0x80, entry_int80,      DPL0, manual);
+#else
+GEN(0x80, entry_0x80,       DPL0, autogen);
+#endif
+
+GEN(0x81, entry_0x81,       DPL0, autogen);
+
+#ifdef CONFIG_PV32
+GEN(0x82, entry_int82,      DPL1, manual);
+#else
+GEN(0x82, entry_0x82,       DPL0, autogen);
+#endif
+
+GEN(0x83, entry_0x83,       DPL0, autogen);
+GEN(0x84, entry_0x84,       DPL0, autogen);
+GEN(0x85, entry_0x85,       DPL0, autogen);
+GEN(0x86, entry_0x86,       DPL0, autogen);
+GEN(0x87, entry_0x87,       DPL0, autogen);
+GEN(0x88, entry_0x88,       DPL0, autogen);
+GEN(0x89, entry_0x89,       DPL0, autogen);
+GEN(0x8a, entry_0x8a,       DPL0, autogen);
+GEN(0x8b, entry_0x8b,       DPL0, autogen);
+GEN(0x8c, entry_0x8c,       DPL0, autogen);
+GEN(0x8d, entry_0x8d,       DPL0, autogen);
+GEN(0x8e, entry_0x8e,       DPL0, autogen);
+GEN(0x8f, entry_0x8f,       DPL0, autogen);
+
+GEN16(9);
+GEN16(a);
+GEN16(b);
+GEN16(c);
+GEN16(d);
+GEN16(e);
+GEN16(f);
+
+#undef autogen
+#undef manual
+
+#undef DPL3
+#undef DPL1
+#undef DPL0
+
+#undef GEN16
xen/arch/x86/include/asm/gen-idt.lds.h new file +27 -0
new file mode 100644
index 000000000000..a11ef7dd1c0a
@@ -0,0 +1,27 @@
+/*
+ * Linker file fragment to help format the IDT correctly
+ *
+ * The IDT, having grown compatibly since the 16 bit days, has the entrypoint
+ * address field split into 3.  x86 ELF lacks the @lo/@hi/etc relocation forms
+ * commonly found in other architectures for accessing a part of a resolved
+ * symbol address.
+ *
+ * However, the linker can perform the necessary calculations and provide them
+ * under new symbol names.  We use this to generate the low and next 16 bits
+ * of the address for each handler.
+ *
+ * The upper 32 bits are always a constant as Xen's .text/data/rodata sits in
+ * a single aligned 1G range, so do not need calculating in this manner.
+ */
+#ifndef X86_IDT_GEN_LDS_H
+#define X86_IDT_GEN_LDS_H
+
+#define GEN(vec, sym, dpl, auto)                                        \
+    PROVIDE_HIDDEN(IDT_ ## sym ## _ADDR1 = ABSOLUTE(((sym) & 0xffff))); \
+    PROVIDE_HIDDEN(IDT_ ## sym ## _ADDR2 = ABSOLUTE((((sym) >> 16) & 0xffff)))
+
+#include <asm/gen-idt.h>
+
+#undef GEN
+
+#endif /* X86_IDT_GEN_LDS_H */
xen/arch/x86/traps-setup.c +0 -4
index ae600526cbe3..3ee28319584d 100644
@@ -3,9 +3,5 @@
  * Configuration of event handling for all CPUs.
  */
 #include <asm/idt.h>
-#include <asm/page.h>
-
-idt_entry_t __section(".bss.page_aligned") __aligned(PAGE_SIZE)
-    bsp_idt[X86_IDT_VECTORS];
 
 DEFINE_PER_CPU_READ_MOSTLY(idt_entry_t *, idt);
xen/arch/x86/x86_64/entry.S +76 -0

82 lines not on this page · raw message

xen/arch/x86/xen.lds.S +2 -0
index 42217eaf2485..d4dd6434c466 100644
@@ -8,6 +8,8 @@
 # define DECL_SECTION_WITH_LADDR
 #endif
 #include <xen/xen.lds.h>
+
+#include <asm/gen-idt.lds.h>
 #include <asm/page.h>
 #include <asm/trampoline.h>
 
Signature · 1 line
2.39.5
Reviewed-by on v2 3/5
#7 lore raw

Re: [PATCH v2 3/5] x86/IDT: Generate bsp_idt[] at build time

Quoted 31 lines · Andrew Cooper

... rather than dynamically at boot time. Aside from less runtime overhead, this approach is less fragile than the preexisting autogen stubs mechanism.

We can manage this with some linker calculations. See patch comments for full details.

For simplicity, we create a new set of entry stubs here, and clean up the old ones in the subsequent patch. bsp_idt[] needs to move from .bss to .data.

No functional change yet; the boot path still (re)writes bsp_idt[] at this juncture.

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

v2:
* Shuffle semicolon position
* Eclair magic comment for multi-include files

There's something differnet about LLD vs LD. Without the ABSOLUTE() in gen-idt.lds.h, LD is fine but LLD puts out symbols in the form:

  x86_IDT_entry_0xff_ADDR1|0000000000002fb0|   t  |            NOTYPE|                |     |.text
  x86_IDT_entry_0xff_ADDR2|0000000000004020|   a  |            NOTYPE|                |     |*ABS*

which causes a slew of errors making symbols for xen-syms:

.xen-syms.0.S:20:8: error: out of range literal value
.long 0x15a0 - (((((((261 >> 8) * 0xffff000000000000) | (261 << 39))) + ((1 << 39) / 2)) + (64 << 30)) + (1 << 30))

Quoted 7 lines
         ^

owing to half the symbols being t rather than a. Moreover, this is reliable for the full FreeBSD builds, but interminttent on randconfig. I haven't figured out which other option is having an effect.

Forcing them all to absolute works in both toolchains.

Just to double-check: Making these symbols absolute does not collide with the .reloc section generation that we do for xen.efi, does it? That is, the absence of relocations for the IDT merely means that we must not use the IDT or any of its entries prior to relocating Xen back to its linked addresses. Or, if e.g. we fetched an entry's address from the IDT earlier on, we'd need to be aware that it's the linked address we fetch, not the one matching where we execute. If that's a correct understanding of mine and also matches your intentions:

Jan

#8 lore raw

Re: [PATCH v2 3/5] x86/IDT: Generate bsp_idt[] at build time

Quoted 34 lines · Jan Beulich
Quoted 33 lines · Andrew Cooper

... rather than dynamically at boot time. Aside from less runtime overhead, this approach is less fragile than the preexisting autogen stubs mechanism.

We can manage this with some linker calculations. See patch comments for full details.

For simplicity, we create a new set of entry stubs here, and clean up the old ones in the subsequent patch. bsp_idt[] needs to move from .bss to .data.

No functional change yet; the boot path still (re)writes bsp_idt[] at this juncture.

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

v2:
* Shuffle semicolon position
* Eclair magic comment for multi-include files

There's something differnet about LLD vs LD. Without the ABSOLUTE() in gen-idt.lds.h, LD is fine but LLD puts out symbols in the form:

  x86_IDT_entry_0xff_ADDR1|0000000000002fb0|   t  |            NOTYPE|                |     |.text
  x86_IDT_entry_0xff_ADDR2|0000000000004020|   a  |            NOTYPE|                |     |*ABS*

which causes a slew of errors making symbols for xen-syms:

.xen-syms.0.S:20:8: error: out of range literal value
.long 0x15a0 - (((((((261 >> 8) * 0xffff000000000000) | (261 << 39))) + ((1 << 39) / 2)) + (64 << 30)) + (1 << 30))

         ^
Quoted 7 lines
Quoted 5 lines

owing to half the symbols being t rather than a. Moreover, this is reliable for the full FreeBSD builds, but interminttent on randconfig. I haven't figured out which other option is having an effect.

Forcing them all to absolute works in both toolchains.

Just to double-check: Making these symbols absolute does not collide with the .reloc section generation that we do for xen.efi, does it?

I don't know for sure.  I'd be lying if I said I understood how that really worked.

But, the boot of a real xen.efi in GitlabCI is happy with the series, including the recovery selftests.

Quoted 6 lines

That is, the
absence of relocations for the IDT merely means that we must not use the IDT or any of its entries prior to relocating Xen back to its linked addresses. Or, if e.g. we fetched an entry's address from the IDT earlier on, we'd need to be aware that it's the linked address we fetch, not the one matching where we execute.

Today, the IDT is a block of zeroes until we get into __start_xen() and construct it by hand.

Safe use of the exception handlers also relies on the percpu areas, so use of the IDT isn't viably moving any earlier.

Even with FRED, I haven't moved it earlier, because there's no point. We're only a handful of instructions ahead of being in 32bit mode, where it would be useless to us.

For EFI boot, we run on the system IDT and are expected to keep it intact (probably until EBS), so I don't see any point using it earlier there either.

Quoted 3 lines

If that's a correct understanding of mine
and also matches your intentions:

Thanks.  I'm pretty confident we're not going to be inclined to use it earlier than __start_xen().

~Andrew

#9 lore raw

PATCH v2 4/5 x86/IDT: Don't rewrite bsp_idt[] at boot time

In reply to Andrew Cooper

Now that bsp_idt[] is constructed at build time, we do not need to manually initialise it in init_idt_traps() and trap_init().

When swapping the early pagefault handler for the normal one, switch to using _update_gate_addr_lower() as we do on the kexec path for NMI and #MC.

This in turn allows us to drop set_{intr,swint}_gate() and the underlying infrastructure. It also lets us drop autogen_entrypoints[] and that underlying infrastructure.

No functional change.

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

v2:
* Adjust the commit message to not give the impression that there's only a single edit on boot.

Bloat-o-meter reports:

add/remove: 0/3 grow/shrink: 1/2 up/down: 9/-6482 (-6473)

  Function                                     old     new   delta
  trap_init                                    425     434      +9
  __set_intr_gate                               84       -     -84
  pv_trap_init                                 163      17    -146
  init_idt_traps                               469     105    -364
  autogen_entrypoints                         2048       -   -2048
  autogen_stubs                               3840       -   -3840

The 3840 for autogen_stubs isn't really a saving here; it was introduced under different names in the prior patch. We do safe 2k on autogen_entrypoints by having the linker complete the work at build time.

---
 xen/arch/x86/include/asm/idt.h | 16 -------
 xen/arch/x86/pv/traps.c        | 13 ------
 xen/arch/x86/traps.c           | 76 +---------------------------------
 xen/arch/x86/x86_64/entry.S    | 60 ---------------------------
 4 files changed, 1 insertion(+), 164 deletions(-)
xen/arch/x86/include/asm/idt.h +0 -16
index d795798d3eca..f613d5693e0e 100644
@@ -60,22 +60,6 @@ static inline void disable_each_ist(idt_entry_t *idt)
     set_ist(&idt[X86_EXC_DB],  IST_NONE);
 }
 
-#define _set_gate(gate_addr,type,dpl,addr)               \
-do {                                                     \
-    (gate_addr)->a = 0;                                  \
-    smp_wmb(); /* disable gate /then/ rewrite */         \
-    (gate_addr)->b =                                     \
-        ((unsigned long)(addr) >> 32);                   \
-    smp_wmb(); /* rewrite /then/ enable gate */          \
-    (gate_addr)->a =                                     \
-        (((unsigned long)(addr) & 0xFFFF0000UL) << 32) | \
-        ((unsigned long)(dpl) << 45) |                   \
-        ((unsigned long)(type) << 40) |                  \
-        ((unsigned long)(addr) & 0xFFFFUL) |             \
-        ((unsigned long)__HYPERVISOR_CS << 16) |         \
-        (1UL << 47);                                     \
-} while (0)
-
 /*
  * Write the lower 64 bits of an IDT Entry. This relies on the upper 32
  * bits of the address not changing, which is a safe assumption as all
xen/arch/x86/pv/traps.c +0 -13
index 4aeb6cab5238..932800555bca 100644
@@ -141,21 +141,8 @@ static void cf_check nmi_softirq(void)
     *v_ptr = NULL;
 }
 
-void nocall entry_int80(void);
-void nocall entry_int82(void);
-
 void __init pv_trap_init(void)
 {
-#ifdef CONFIG_PV32
-    /* The 32-on-64 hypercall vector is only accessible from ring 1. */
-    _set_gate(bsp_idt + HYPERCALL_VECTOR,
-              SYS_DESC_irq_gate, 1, entry_int82);
-#endif
-
-    /* Fast trap for int80 (faster than taking the #GP-fixup path). */
-    _set_gate(bsp_idt + LEGACY_SYSCALL_VECTOR, SYS_DESC_irq_gate, 3,
-              &entry_int80);
-
     open_softirq(NMI_SOFTIRQ, nmi_softirq);
 }
 
xen/arch/x86/traps.c +1 -75

104 lines not on this page · raw message

xen/arch/x86/x86_64/entry.S +0 -60

63 lines not on this page · raw message

Signature · 1 line
2.39.5
#10 lore raw

PATCH v2 5/5 x86/traps: Convert pv_trap_init() to being an initcall

In reply to Andrew Cooper

With most of pv_trap_init() being done at build time, opening of NMI_SOFTIRQ can be a regular initcall, simplifying trap_init().

No functional change.

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

For Branch:

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1684170631
https://cirrus-ci.com/build/6590097610506240

---
 xen/arch/x86/include/asm/pv/traps.h | 4 ----
 xen/arch/x86/pv/traps.c             | 5 ++++-
 xen/arch/x86/traps.c                | 2 --
 3 files changed, 4 insertions(+), 7 deletions(-)
xen/arch/x86/include/asm/pv/traps.h +0 -4
index 404f5b169ca8..8c31d5a793c5 100644
@@ -14,8 +14,6 @@
 
 #include <public/xen.h>
 
-void pv_trap_init(void);
-
 int pv_raise_nmi(struct vcpu *v);
 
 int pv_emulate_privileged_op(struct cpu_user_regs *regs);
@@ -32,8 +30,6 @@ static inline bool pv_trap_callback_registered(const struct vcpu *v,
 
 #include <xen/errno.h>
 
-static inline void pv_trap_init(void) {}
-
 static inline int pv_raise_nmi(struct vcpu *v) { return -EOPNOTSUPP; }
 
 static inline int pv_emulate_privileged_op(struct cpu_user_regs *regs) { return 0; }
xen/arch/x86/pv/traps.c +4 -1
index 932800555bca..c3c0976c440f 100644
@@ -141,10 +141,13 @@ static void cf_check nmi_softirq(void)
     *v_ptr = NULL;
 }
 
-void __init pv_trap_init(void)
+static int __init cf_check pv_trap_init(void)
 {
     open_softirq(NMI_SOFTIRQ, nmi_softirq);
+
+    return 0;
 }
+__initcall(pv_trap_init);
 
 /*
  * Deliver NMI to PV guest. Return 0 on success.
xen/arch/x86/traps.c +0 -2
index 7698fa580ef7..5addb1f903d3 100644
@@ -1928,8 +1928,6 @@ void __init trap_init(void)
     /* Replace early pagefault with real pagefault handler. */
     _update_gate_addr_lower(&bsp_idt[X86_EXC_PF], entry_PF);
 
-    pv_trap_init();
-
     init_ler();
 
     /* Cache {,compat_}gdt_l1e now that physically relocation is done. */
Signature · 1 line
2.39.5
Acked-by on the v2 series
#11 lore raw
In reply to Andrew Cooper
Quoted 12 lines · Andrew Cooper

This is a chunk of the FRED work split out because it's pretty self contained.

It's mostly cleanup/refactoring, although patch

traps.c is already overly large and going to get larger with FRED, so I'm splitting traps-setup.c out of it, as the two have reasonably-different logic. That's implemented in this series but not used in anger in this series.

Since v1:
* First 3 patches committed
* Minor adjustments. See individual patches for details

Quoted 6 lines

Andrew Cooper (5):
x86/IDT: Rename idt_table[] to bsp_idt[] x86/IDT: Make idt_tables[] be per_cpu(idt) x86/IDT: Generate bsp_idt[] at build time x86/IDT: Don't rewrite bsp_idt[] at boot time x86/traps: Convert pv_trap_init() to being an initcall

For the two not having R-b already:

perhaps with patch 2's description slightly adjusted (see separate mail).

Jan