All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frediano Ziglio <freddy77@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: "Frediano Ziglio" <freddy77@gmail.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Roger Pau Monné" <roger@xenproject.org>,
	"Teddy Astie" <teddy.astie@vates.tech>,
	"Anthony PERARD" <anthony.perard@vates.tech>,
	"Michal Orzel" <michal.orzel@amd.com>,
	"Julien Grall" <julien@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Claude Opus 5 . 5" <noreply@anthropic.com>
Subject: [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S
Date: Fri,  4 Dec 2026 20:50:43 +0000	[thread overview]
Message-ID: <20261204205045.596093-11-freddy77@gmail.com> (raw)
In-Reply-To: <20261204205045.596093-1-freddy77@gmail.com>

The trampoline is included in head.S, so head.S can reference any of
its symbols, including local labels, and the two share all the
assembler state.

Assemble trampoline.S as its own object, so only its global symbols are
visible outside it.  The definitions used by both (ENTRY(), GLOBAL()
and the boot GDT selectors usable by head.S) move to a new boot.h
header.

head.S uses trampoline_gdt while the trampoline is still part of the
Xen image, where the descriptors with a base relocated to the
trampoline are not valid yet.  Define their selectors only in the
trampoline, after BOOT_GDT_LAST, and limit the GDT loaded by head.S to
the valid descriptors, so using them there faults.  A build time check
keeps the GDT layout in sync with the selectors.

trampoline_gdt is now a global symbol, and the offset of the boot code
head.S jumps to is exported as an absolute symbol, as head.S can no
longer compute label differences inside the trampoline.

The build time checks now run on trampoline.o, which contains both the
trampoline and the references to the abs_* symbols.

No change in the generated trampoline.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/x86/Makefile                |  2 +-
 xen/arch/x86/boot/.gitignore         |  2 +-
 xen/arch/x86/boot/Makefile           |  7 ++--
 xen/arch/x86/boot/boot.h             | 26 +++++++++++++++
 xen/arch/x86/boot/head.S             | 33 +++----------------
 xen/arch/x86/boot/trampoline.S       | 48 +++++++++++++++++++++++++++-
 xen/tools/check-abs-symbols.sh       | 10 +++---
 xen/tools/check-trampoline-relocs.sh |  2 +-
 8 files changed, 89 insertions(+), 41 deletions(-)
 create mode 100644 xen/arch/x86/boot/boot.h

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index a90d15748d..7ce9e5fd94 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -117,7 +117,7 @@ LAST_LINKING_PASS := 2
 
 # The trampoline relies on the abs_* symbols being absolute.
 final-image-check-y = \
-    $(SHELL) $(srctree)/tools/check-abs-symbols.sh $(1) $(obj)/boot/head.o
+    $(SHELL) $(srctree)/tools/check-abs-symbols.sh $(1) $(obj)/boot/trampoline.o
 final-image-check-$(CONFIG_XEN_IBT) += && $(SHELL) $(srctree)/tools/check-endbr.sh $(1)
 
 include scripts/Makefile.link
diff --git a/xen/arch/x86/boot/.gitignore b/xen/arch/x86/boot/.gitignore
index fb669b2eae..e983602db0 100644
--- a/xen/arch/x86/boot/.gitignore
+++ b/xen/arch/x86/boot/.gitignore
@@ -3,4 +3,4 @@
 /built-in-32.*.bin
 /built-in-32.*.map
 /built-in-32.S
-/head.trampoline-checked
+/trampoline.checked
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index 2e2e903a88..7600226398 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -1,4 +1,5 @@
 obj-bin-y += head.o
+obj-bin-y += trampoline.o
 obj-bin-y += built-in-32.o
 obj-bin-y += $(obj64)
 
@@ -21,11 +22,11 @@ cmd_check_trampoline = \
     OBJDUMP="$(OBJDUMP)" $(SHELL) $(srctree)/tools/check-trampoline-relocs.sh $< && \
     touch $@
 
-$(obj)/head.trampoline-checked: $(obj)/head.o \
-                                $(srctree)/tools/check-trampoline-relocs.sh FORCE
+$(obj)/trampoline.checked: $(obj)/trampoline.o \
+                           $(srctree)/tools/check-trampoline-relocs.sh FORCE
 	$(call if_changed,check_trampoline)
 
-extra-y += head.trampoline-checked
+extra-y += trampoline.checked
 
 CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
diff --git a/xen/arch/x86/boot/boot.h b/xen/arch/x86/boot/boot.h
new file mode 100644
index 0000000000..e388250050
--- /dev/null
+++ b/xen/arch/x86/boot/boot.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Definitions shared between head.S and the trampoline, which are assembled
+ * separately.
+ */
+#ifndef X86_BOOT_BOOT_H
+#define X86_BOOT_BOOT_H
+
+#define ENTRY(name)                             \
+  .align CONFIG_FUNCTION_ALIGNMENT, CODE_FILL;  \
+  GLOBAL(name)
+#define GLOBAL(name)                            \
+  .globl name;                                  \
+  .hidden name;                                 \
+  name:
+
+/*
+ * Selectors in trampoline_gdt usable while it is still part of the Xen image,
+ * as head.S does.
+ */
+#define BOOT_CS32        0x0008
+#define BOOT_CS64        0x0010
+#define BOOT_DS          0x0018
+#define BOOT_GDT_LAST    BOOT_DS
+
+#endif /* X86_BOOT_BOOT_H */
diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 853bf27a59..8253b68158 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -12,13 +12,7 @@
 
 #include <public/elfnote.h>
 
-#define ENTRY(name)                             \
-  .align CONFIG_FUNCTION_ALIGNMENT, CODE_FILL;  \
-  GLOBAL(name)
-#define GLOBAL(name)                            \
-  .globl name;                                  \
-  .hidden name;                                 \
-  name:
+#include "boot.h"
 
         .section .text.header, "ax", @progbits
         .code32
@@ -26,14 +20,6 @@
 #define sym_offs(sym)     ((sym) - __XEN_VIRT_START)
 #define sym_esi(sym)      sym_offs(sym)(%esi)
 
-#define BOOT_CS32        0x0008
-#define BOOT_CS64        0x0010
-#define BOOT_DS          0x0018
-#define BOOT_PSEUDORM_CS 0x0020
-#define BOOT_PSEUDORM_DS 0x0028
-#define BOOT_CS32_TRAMP  0x0030
-#define BOOT_DS_TRAMP    0x0038
-
 #define MB2_HT(name)      (MULTIBOOT2_HEADER_TAG_##name)
 #define MB2_TT(name)      (MULTIBOOT2_TAG_TYPE_##name)
 
@@ -144,7 +130,8 @@ multiboot2_header:
 
         .word   0
 gdt_boot_descr:
-        .word   .Ltrampoline_gdt_end - trampoline_gdt - 1
+        /* Only the descriptors usable while part of the Xen image. */
+        .word   BOOT_GDT_LAST + 8 - 1
 gdt_boot_base:
         .long   sym_offs(trampoline_gdt)
         .long   0 /* Needed for 64-bit lgdt */
@@ -703,23 +690,11 @@ trampoline_setup:
         /* Switch to low-memory stack which lives at the end of trampoline region. */
         mov     sym_esi(trampoline_phys), %edi
         lea     TRAMPOLINE_SIZE(%edi), %esp
-        lea     trampoline_boot_cpu_entry-trampoline_start(%edi), %eax
+        lea     trampoline_boot_cpu_entry_offset(%edi), %eax
         pushl   $BOOT_CS32
         push    %eax
 
         /* Jump into the relocated trampoline. */
         lret
 
-        /*
-         * The trampoline is never written to while part of the Xen image: the
-         * data also used from outside has a writable copy (see GLOBAL_COPY()),
-         * and relocations are applied when it is placed in low memory.  This
-         * allows it to live in .init.text.
-         */
-        .section .init.trampoline, "ax", @progbits
-ENTRY(trampoline_start)
-#include "trampoline.S"
-        .subsection TRAMP_LAST
-ENTRY(trampoline_end)
-
 #include "x86_64.S"
diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index df193fb67f..d389694162 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -14,7 +14,28 @@
  * after suspend, bringup code for secondary cpus). The permanent parts should
  * not reference any temporary low memory trampoline parts as those parts are
  * not guaranteed to persist.
+ *
+ * The trampoline is assembled separately from head.S, so only its global
+ * symbols are visible outside.
+ */
+
+#include <asm/asm_defns.h>
+#include <asm/page.h>
+#include <asm/processor.h>
+#include <asm/msr-index.h>
+#include <asm/trampoline.h>
+
+#include "boot.h"
+
+/*
+ * Selectors in trampoline_gdt following the ones in boot.h.  These descriptors
+ * have a base relocated to the trampoline, so they are only valid once it is
+ * placed, and only usable from the trampoline.
  */
+#define BOOT_PSEUDORM_CS (BOOT_GDT_LAST + 0x08)
+#define BOOT_PSEUDORM_DS (BOOT_GDT_LAST + 0x10)
+#define BOOT_CS32_TRAMP  (BOOT_GDT_LAST + 0x18)
+#define BOOT_DS_TRAMP    (BOOT_GDT_LAST + 0x20)
 
 /* NB. bootsym() is usable in real mode, via BOOT_PSEUDORM_DS or via BOOT_DS_TRAMP. */
 /* The weird multiplication is to catch symbols from other sections. */
@@ -64,6 +85,15 @@
 #define TRAMP_BOOT_DATA      5
 #define TRAMP_LAST           TRAMP_BOOT_DATA
 
+        /*
+         * The trampoline is never written to while part of the Xen image: the
+         * data also used from outside has a writable copy (see GLOBAL_COPY()),
+         * and relocations are applied when it is placed in low memory.  This
+         * allows it to live in .init.text.
+         */
+        .section .init.trampoline, "ax", @progbits
+ENTRY(trampoline_start)
+
         /* Start of the data with a writable copy, see GLOBAL_COPY(). */
         .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
         .balign 8
@@ -193,7 +223,7 @@ high_start_address:
         .word   0
 idt_48: .word   0, 0, 0 # base = limit = 0
 
-trampoline_gdt:
+GLOBAL(trampoline_gdt)
         .word   0                  /* 0x0000: unused (reused for GDTR) */
 gdt_48:
         .word   .Ltrampoline_gdt_end - trampoline_gdt - 1
@@ -202,11 +232,18 @@ gdt_48:
         .quad   0x00cf9b000000ffff /* 0x0008: ring 0 code, 32-bit mode */
         .quad   0x00af9b000000ffff /* 0x0010: ring 0 code, 64-bit mode */
         .quad   0x00cf93000000ffff /* 0x0018: ring 0 data */
+.Ltrampoline_gdt_image_end:
         .quad   0x00009b000000ffff /* 0x0020: real-mode code @ BOOT_TRAMPOLINE */
         .quad   0x000093000000ffff /* 0x0028: real-mode data @ BOOT_TRAMPOLINE */
         .quad   0x00cf9b000000ffff /* 0x0030: ring 0 code, 32-bit mode @ BOOT_TRAMPOLINE */
         .quad   0x00cf93000000ffff /* 0x0038: ring 0 data @ BOOT_TRAMPOLINE */
 .Ltrampoline_gdt_end:
+        .if .Ltrampoline_gdt_image_end - trampoline_gdt != BOOT_GDT_LAST + 8
+        .error "trampoline_gdt doesn't match BOOT_GDT_LAST"
+        .endif
+        .if .Ltrampoline_gdt_end - trampoline_gdt != BOOT_DS_TRAMP + 8
+        .error "trampoline_gdt doesn't match the trampoline selectors"
+        .endif
 
         /* Relocations for trampoline Real Mode segments. */
         .pushsection .trampoline_rel, "a"
@@ -389,3 +426,12 @@ GLOBAL(l_trampoline_data_end)
         .org trampoline_data_start + (l_trampoline_data_end - l_trampoline_data_start)
 GLOBAL(trampoline_data_end)
         .popsection
+
+        .subsection TRAMP_LAST
+ENTRY(trampoline_end)
+
+/* For head.S, to jump into the boot code once the trampoline is placed. */
+        .globl trampoline_boot_cpu_entry_offset
+        .hidden trampoline_boot_cpu_entry_offset
+        .set trampoline_boot_cpu_entry_offset, \
+             trampoline_boot_cpu_entry - trampoline_start
diff --git a/xen/tools/check-abs-symbols.sh b/xen/tools/check-abs-symbols.sh
index 3db5a8e01d..f3801dd50d 100755
--- a/xen/tools/check-abs-symbols.sh
+++ b/xen/tools/check-abs-symbols.sh
@@ -1,16 +1,16 @@
 #!/bin/sh
 #
-# Usage ./$0 image head.o
+# Usage ./$0 image trampoline.o
 #
 # The trampoline references Xen symbols only through the abs_* symbols defined
 # in the linker script.  These must be ABSOLUTE symbols, so that references to
 # them are fully resolved at link time and don't produce base relocations in
 # xen.efi.
 #
-# Check that every abs_* symbol referenced by head.o is present in the final
-# image with absolute type.  Checking presence matters for PE images, where a
-# non-absolute symbol with a value below the image base isn't representable and
-# is silently dropped from the symbol table.
+# Check that every abs_* symbol referenced by trampoline.o is present in the
+# final image with absolute type.  Checking presence matters for PE images,
+# where a non-absolute symbol with a value below the image base isn't
+# representable and is silently dropped from the symbol table.
 #
 set -e
 
diff --git a/xen/tools/check-trampoline-relocs.sh b/xen/tools/check-trampoline-relocs.sh
index 5b79f88698..85085e51f5 100755
--- a/xen/tools/check-trampoline-relocs.sh
+++ b/xen/tools/check-trampoline-relocs.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Usage ./$0 head.o
+# Usage ./$0 trampoline.o
 #
 # The trampoline must not need relocations in the final binary.  For PE, the
 # loader applies base relocations to the whole image before any Xen code runs,
-- 
2.43.0



  parent reply	other threads:[~2026-12-04 20:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour Frediano Ziglio
2026-12-04 20:50 ` [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations Frediano Ziglio
2026-12-04 20:50 ` [PATCH 04/11] x86/boot: Reduce relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` [PATCH 05/11] x86/boot: Share the switch to 64-bit mode between SIPI and S3 paths Frediano Ziglio
2026-12-04 20:50 ` [PATCH 06/11] x86/boot: Use stack to remove a relocation from trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy Frediano Ziglio
2026-12-04 20:50 ` [PATCH 08/11] x86/boot: Group trampoline code and data using subsections Frediano Ziglio
2026-12-04 20:50 ` [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data Frediano Ziglio
2026-12-04 20:50 ` Frediano Ziglio [this message]
2026-12-04 20:50 ` [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers Frediano Ziglio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204205045.596093-11-freddy77@gmail.com \
    --to=freddy77@gmail.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=noreply@anthropic.com \
    --cc=roger@xenproject.org \
    --cc=sstabellini@kernel.org \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.