All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/11] x86/boot: Improve trampoline code
@ 2026-12-04 20:50 Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour Frediano Ziglio
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Daniel P. Smith, Marek Marczykowski-Górecki,
	Anthony PERARD, Michal Orzel, Julien Grall, Stefano Stabellini,
	Bertrand Marquis, Volodymyr Babchuk, Lukasz Hawrylko,
	Mateusz Mówka

This series collect different attempts and idea to improve trampoline
code.

Most of the changes improve relocation handling:
- binary relocations on the final binary causes issues with PE format
  (used for EFI firmwares) as our code need to relocate from loader
  position to final one. To fix that use absolute symbols generated by
  the linker script. Checks are added to avoid adding back other
  relocations;
- apply relocations after the copy to lower memory. This avoids writing
  into code section;
- use a copy of the data section inside the trampoline in the final binary
  executable to avoid changing the original trampoline;
- trampoline code is back in the final binary code section for EFI builds.

General improvements:
- add some checks and cleanup in the code handling S3 resume;
- reuse code to switch to 64 bit, almost identical besides useless
  differences.

Added safety:
- assembly bootsym macro now assure symbols are in the same section;
- C bootsym* macros check for symbol type;
- check there are no relocation inside the trampoline in the final
  binary (mentioned above);
- compile trampoline in a separate object to avoid head.S to access
  unwanted symbols in the trampoline.

Frediano Ziglio (11):
  x86/boot: Prevent some possible firmware misbehaviour
  x86/boot: Avoid PE relocations in the trampoline
  x86/boot: Check at build time that the trampoline needs no relocations
  x86/boot: Reduce relocations in the trampoline
  x86/boot: Share the switch to 64-bit mode between SIPI and S3 paths
  x86/boot: Use stack to remove a relocation from trampoline code
  x86/boot: Apply relocations in the trampoline after the copy
  x86/boot: Group trampoline code and data using subsections
  x86/boot: Use a writable copy of the trampoline data
  x86/boot: Assemble the trampoline separately from head.S
  x86/boot: Check the kind of symbols passed to the bootsym helpers

 xen/arch/arm/efi/efi-boot.h             |   4 +
 xen/arch/x86/Makefile                   |   5 +-
 xen/arch/x86/boot/.gitignore            |   1 +
 xen/arch/x86/boot/Makefile              |  20 +-
 xen/arch/x86/boot/boot.h                |  26 +++
 xen/arch/x86/boot/build32.lds.S         |   8 +-
 xen/arch/x86/boot/cmdline.c             |   2 +-
 xen/arch/x86/boot/copy-trampoline.c     |  67 +++++++
 xen/arch/x86/boot/edd.S                 |  26 +--
 xen/arch/x86/boot/head.S                |  52 +----
 xen/arch/x86/boot/mem.S                 |  12 +-
 xen/arch/x86/boot/reloc-trampoline.c    |  34 ----
 xen/arch/x86/boot/trampoline.S          | 242 +++++++++++++++++++-----
 xen/arch/x86/boot/video.S               |  24 ++-
 xen/arch/x86/boot/wakeup.S              | 114 ++++-------
 xen/arch/x86/efi/efi-boot.h             |  21 +-
 xen/arch/x86/efi/mbi2.c                 |   4 +
 xen/arch/x86/include/asm/boot-helpers.h |   3 +-
 xen/arch/x86/include/asm/trampoline.h   |  51 ++++-
 xen/arch/x86/setup.c                    |   6 +-
 xen/arch/x86/tboot.c                    |   2 +-
 xen/arch/x86/xen.lds.S                  |  33 +++-
 xen/common/efi/boot.c                   |   1 +
 xen/tools/check-abs-symbols.sh          |  50 +++++
 xen/tools/check-trampoline-relocs.sh    |  47 +++++
 25 files changed, 604 insertions(+), 251 deletions(-)
 create mode 100644 xen/arch/x86/boot/boot.h
 create mode 100644 xen/arch/x86/boot/copy-trampoline.c
 delete mode 100644 xen/arch/x86/boot/reloc-trampoline.c
 create mode 100755 xen/tools/check-abs-symbols.sh
 create mode 100755 xen/tools/check-trampoline-relocs.sh

-- 
2.43.0



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
@ 2026-12-04 20:50 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline Frediano Ziglio
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Claude Opus 5 . 5

Check the option ROM has a valid header before calling it.
Reset flags after calling the option ROM.

These changes were made after talking with AI about 0xc000:3 address.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/x86/boot/wakeup.S | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index 654e97005f..1f5af77745 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -39,11 +39,18 @@ ENTRY(entry_S3)
         # for acpi_sleep=s3_bios
         testb   $1, wakesym(video_flags)
         jz      1f
+        mov     $0xc000, %ax
+        mov     %ax, %fs
+        cmpw    $0xaa55, %fs:0  # check BIOS signature
+        jne     1f
         lcall   $0xc000, $3
         movw    %cs, %ax        # In case messed by BIOS
         movw    %ax, %ds
         movw    %ax, %ss        # Need this? How to ret if clobbered?
 
+        pushl   $0              # Flags may have been messed by BIOS too
+        popfl
+
 1:      # for acpi_sleep=s3_mode
         testb   $2, wakesym(video_flags)
         jz      1f
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline
  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 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations Frediano Ziglio
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Daniel P. Smith, Marek Marczykowski-Górecki

Use linker script to generate absolute symbols which do not
need relocations in the final binary.

In debug mode check the trampoline has no relocations for PE
executables.

This avoids having to write into text section for relocating to higher
addresses.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 xen/arch/x86/boot/trampoline.S |  4 ++--
 xen/arch/x86/boot/wakeup.S     |  6 +++---
 xen/arch/x86/efi/efi-boot.h    |  8 ++++++++
 xen/arch/x86/xen.lds.S         | 15 +++++++++++++++
 4 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index a92e399fbe..0be6fe0006 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -72,7 +72,7 @@ trampoline_protmode_entry:
         mov     %ecx,%cr4
 
         /* Load pagetable base register. */
-        mov     $sym_offs(idle_pg_table),%eax
+        mov     $abs_idle_pg_table, %eax
         add     bootsym_rel(trampoline_xen_phys_start,4,%eax)
         mov     %eax,%cr3
 
@@ -112,7 +112,7 @@ trampoline_protmode_entry:
         .code64
 start64:
         /* Jump to high mappings. */
-        movabs  $__high_start, %rdi
+        movabs  $(abs___high_start + XEN_VIRT_START), %rdi
         jmpq    *%rdi
 
 #include "video.h"
diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index 1f5af77745..fb9b3da205 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -116,7 +116,7 @@ wakeup_32:
         mov     $bootsym_rel(wakeup_stack, 4, %esp)
 
         # check saved magic again
-        mov     $sym_offs(saved_magic),%eax
+        mov     $abs_saved_magic, %eax
         add     bootsym_rel(trampoline_xen_phys_start, 4, %eax)
         mov     (%eax), %eax
         cmp     $0x9abcdef0, %eax
@@ -129,7 +129,7 @@ wakeup_32:
         mov     %ecx, %cr4
 
         /* Load pagetable base register */
-        mov     $sym_offs(idle_pg_table),%eax
+        mov     $abs_idle_pg_table, %eax
         add     bootsym_rel(trampoline_xen_phys_start,4,%eax)
         mov     %eax,%cr3
 
@@ -178,5 +178,5 @@ END(bogus_saved_magic)
         .code64
 wakeup_64:
         /* Jump to high mappings and the higher-level wakeup code. */
-        movabs  $s3_resume, %rbx
+        movabs  $(abs_s3_resume + XEN_VIRT_START), %rbx
         jmp     *%rbx
diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h
index 8bca03a66b..2baecb20b4 100644
--- a/xen/arch/x86/efi/efi-boot.h
+++ b/xen/arch/x86/efi/efi-boot.h
@@ -82,6 +82,14 @@ static void __init efi_arch_relocate_image(unsigned long delta)
             unsigned long addr = xen_phys_start + base_relocs->rva +
                                  (base_relocs->entries[i] & 0xfff);
 
+#ifdef CONFIG_DEBUG
+            char *pp = (char *) addr;
+
+            if ( (base_relocs->entries[i] >> 12) != PE_BASE_RELOC_ABS &&
+                 pp >= trampoline_start && pp < trampoline_end )
+                blexit(L"Relocations in trampoline");
+#endif
+
             switch ( base_relocs->entries[i] >> 12 )
             {
             case PE_BASE_RELOC_ABS:
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index e25863f331..9d8da60455 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -328,6 +328,21 @@ SECTIONS
   } PHDR(text)
   _end = . ;
 
+  /*
+   * Symbols used by the trampoline, as offsets from the start of the image.
+   *
+   * Being absolute, references to them are fully resolved at link time and
+   * don't produce base relocations in the PE binary, so the trampoline (which
+   * is in a text section) doesn't need to be written to when Xen is relocated.
+   *
+   * 32-bit unpaged code has to add trampoline_xen_phys_start to get a physical
+   * address, 64-bit code has to add XEN_VIRT_START to get a virtual one.
+   */
+  abs_idle_pg_table = ABSOLUTE(idle_pg_table - __XEN_VIRT_START);
+  abs_saved_magic = ABSOLUTE(saved_magic - __XEN_VIRT_START);
+  abs_s3_resume = ABSOLUTE(s3_resume - __XEN_VIRT_START);
+  abs___high_start = ABSOLUTE(__high_start - __XEN_VIRT_START);
+
   __2M_rwdata_end = ALIGN(SECTION_ALIGN);
 
 #ifdef EFI
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations
  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 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 04/11] x86/boot: Reduce relocations in the trampoline Frediano Ziglio
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Anthony PERARD, Michal Orzel, Julien Grall,
	Stefano Stabellini, Claude Opus 5 . 5

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, so relocations in the trampoline would have to be reverted
by hand when it is placed in low memory.  That means patching code,
which is unusual and unwanted on x86-64, where code is normally
%rip-relative.  The only relocations allowed are against the abs_*
symbols, which are absolute and so fully resolved at link time.

Add two checks:
- after assembling head.o, the trampoline section must have no
  relocations other than non PC-relative ones against abs_* symbols;
- after linking xen-syms and xen.efi, every abs_* symbol referenced by
  head.o must be present with absolute type.  Checking presence is
  needed for xen.efi, as ld silently drops non-absolute symbols with a
  value below the image base, while still emitting base relocations
  for their references.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/x86/Makefile                |  5 ++-
 xen/arch/x86/boot/.gitignore         |  1 +
 xen/arch/x86/boot/Makefile           | 13 ++++++++
 xen/tools/check-abs-symbols.sh       | 50 ++++++++++++++++++++++++++++
 xen/tools/check-trampoline-relocs.sh | 47 ++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100755 xen/tools/check-abs-symbols.sh
 create mode 100755 xen/tools/check-trampoline-relocs.sh

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 320593a1e8..a90d15748d 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -115,7 +115,10 @@ CFLAGS-$(XEN_BUILD_EFI) += -DXEN_BUILD_EFI
 
 LAST_LINKING_PASS := 2
 
-final-image-check-$(CONFIG_XEN_IBT) = $(SHELL) $(srctree)/tools/check-endbr.sh $(1)
+# 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
+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 595cef6a2c..fb669b2eae 100644
--- a/xen/arch/x86/boot/.gitignore
+++ b/xen/arch/x86/boot/.gitignore
@@ -3,3 +3,4 @@
 /built-in-32.*.bin
 /built-in-32.*.map
 /built-in-32.S
+/head.trampoline-checked
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index ff0d61d7ac..8411535397 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -14,6 +14,19 @@ targets   += $(obj32)
 
 obj32 := $(addprefix $(obj)/,$(obj32))
 
+# The trampoline must not contain relocations, apart from the ones against the
+# abs_* symbols, which are resolved at link time.
+quiet_cmd_check_trampoline = CHECK   $<
+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
+	$(call if_changed,check_trampoline)
+
+extra-y += head.trampoline-checked
+
 CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
 CFLAGS_x86_32 += -Werror -fno-builtin -g0 -msoft-float -mregparm=3
diff --git a/xen/tools/check-abs-symbols.sh b/xen/tools/check-abs-symbols.sh
new file mode 100755
index 0000000000..3db5a8e01d
--- /dev/null
+++ b/xen/tools/check-abs-symbols.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+#
+# Usage ./$0 image head.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.
+#
+set -e
+
+# Pretty-print parameters a little for message
+MSG_PFX="${0##*/} ${1##*/}"
+
+NM="${NM:-nm}"
+AWK="${AWK:-awk}"
+
+error() {
+    printf '%s\n' "$MSG_PFX Error: $*" >&2
+    exit 1
+}
+
+# Undefined symbols are listed as "U name".
+NEEDED=$($NM -u "$2" | $AWK '$1 == "U" && $2 ~ /^abs_/ { print $2 }')
+[ -n "$NEEDED" ] || error "no abs_* symbols referenced by ${2##*/}"
+
+SYMS=$($NM -p "$1")
+
+# Defined symbols are listed as "value type name".
+BAD=$(printf '%s\n%s\n' "$NEEDED" "--" "$SYMS" | $AWK '
+    !syms && $0 == "--" { syms = 1; next }
+    !syms { needed[$1] = 1; next }
+    NF == 3 && $3 ~ /^abs_/ {
+        seen[$3] = 1
+        if ($2 != "A")
+            printf "  %s: type %s, not absolute\n", $3, $2
+    }
+    END {
+        for (s in needed)
+            if (!(s in seen))
+                printf "  %s: missing (not absolute?)\n", s
+    }')
+
+[ -z "$BAD" ] || error "bad abs_* symbols:
+$BAD"
diff --git a/xen/tools/check-trampoline-relocs.sh b/xen/tools/check-trampoline-relocs.sh
new file mode 100755
index 0000000000..5b79f88698
--- /dev/null
+++ b/xen/tools/check-trampoline-relocs.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# Usage ./$0 head.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,
+# so relocations in the trampoline would have to be reverted by hand when it is
+# placed in low memory, which means patching code.  The only relocations
+# allowed are against the abs_* symbols, which are ABSOLUTE linker script
+# symbols and so are fully resolved at link time.
+#
+# Check that the trampoline section of the object file containing the
+# trampoline has no other relocations.
+#
+set -e
+
+# Pretty-print parameters a little for message
+MSG_PFX="${0##*/} ${1##*/}"
+
+OBJDUMP="${OBJDUMP:-objdump}"
+AWK="${AWK:-awk}"
+
+error() {
+    printf '%s\n' "$MSG_PFX Error: $*" >&2
+    exit 1
+}
+
+SECTION=.init.trampoline
+
+# Make sure the section exists, so a rename doesn't silently skip the check.
+$OBJDUMP -h "$1" | grep -qF " $SECTION " || error "section $SECTION not found"
+
+# Don't use -j, llvm-objdump doesn't apply it to relocations.
+RELOCS=$($OBJDUMP -r "$1")
+
+# Relocations are listed as "OFFSET TYPE SYMBOL[+-ADDEND]" after a
+# "RELOCATION RECORDS FOR [section]:" header.  PC-relative references are
+# wrong even against abs_* symbols, as the code is moved.
+BAD=$(echo "$RELOCS" | $AWK -v sect=$SECTION '
+    /^RELOCATION RECORDS FOR / { in_sect = ($4 == "[" sect "]:"); next }
+    in_sect && NF >= 3 && $1 ~ /^[0-9a-fA-F]+$/ {
+        if ($3 !~ /^abs_/ || $2 ~ /PC|PLT|GOT/)
+            printf "  %s+0x%s: %s %s\n", sect, $1, $2, $3
+    }')
+
+[ -z "$BAD" ] || error "unexpected relocations in the trampoline:
+$BAD"
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 04/11] x86/boot: Reduce relocations in the trampoline
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (2 preceding siblings ...)
  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 ` 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
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie

Use code and data selectors based on the trampoline to avoid relocations
to simple symbols and for far jumps from 16 bits code to 32 bits code.
Avoid wakesym macro reusing bootsym one.

This also remove the need to pass the relative position of the
relocation (second argument of bootsym_rel) keeping normal parameter
separation.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 xen/arch/x86/boot/head.S       |  6 +++--
 xen/arch/x86/boot/trampoline.S | 31 +++++++++++++++---------
 xen/arch/x86/boot/wakeup.S     | 43 ++++++++++++++++++++--------------
 3 files changed, 49 insertions(+), 31 deletions(-)

diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 77e5a86a1d..0e1388ac1f 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -31,6 +31,8 @@
 #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)
@@ -699,8 +701,8 @@ 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
-        pushl   $BOOT_CS32
+        mov     $(trampoline_boot_cpu_entry-trampoline_start), %eax
+        pushl   $BOOT_CS32_TRAMP
         push    %eax
 
         /* Copy bootstrap trampoline to low memory, below 1MB. */
diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index 0be6fe0006..c34ca9beae 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -16,7 +16,7 @@
  * not guaranteed to persist.
  */
 
-/* NB. bootsym() is only usable in real mode, or via BOOT_PSEUDORM_DS. */
+/* NB. bootsym() is usable in real mode, via BOOT_PSEUDORM_DS or via BOOT_DS_TRAMP. */
 #undef bootsym
 #define bootsym(s) ((s)-trampoline_start)
 
@@ -55,14 +55,13 @@ GLOBAL(entry_SIPI16)
         mov     $X86_CR0_PE, %ebx         # EBX != 0 indicates we are an AP
         mov     %ebx, %cr0                # Alias with CR0.PE for brevity
 
-        ljmpl   $BOOT_CS32,$bootsym_rel(trampoline_protmode_entry,6)
+        ljmp    $BOOT_CS32_TRAMP, $bootsym(trampoline_protmode_entry)
 
         .code32
 trampoline_protmode_entry:
         /* Set up a few descriptors: on entry only CS is guaranteed good. */
-        mov     $BOOT_DS,%eax
+        mov     $BOOT_DS_TRAMP, %eax
         mov     %eax,%ds
-        mov     %eax,%es
 
         /* Set up FPU. */
         fninit
@@ -73,12 +72,12 @@ trampoline_protmode_entry:
 
         /* Load pagetable base register. */
         mov     $abs_idle_pg_table, %eax
-        add     bootsym_rel(trampoline_xen_phys_start,4,%eax)
+        add     bootsym(trampoline_xen_phys_start), %eax
         mov     %eax,%cr3
 
         /* Adjust IA32_MISC_ENABLE if needed (for NX enabling below). */
-        mov     bootsym_rel(trampoline_misc_enable_off,4,%esi)
-        mov     bootsym_rel(trampoline_misc_enable_off+4,4,%edi)
+        mov     bootsym(trampoline_misc_enable_off), %esi
+        mov     bootsym(trampoline_misc_enable_off+4), %edi
         mov     %esi,%eax
         or      %edi,%eax
         jz      1f
@@ -99,7 +98,7 @@ trampoline_protmode_entry:
         /* Set up EFER (Extended Feature Enable Register). */
         movl    $MSR_EFER,%ecx
         rdmsr
-        or      bootsym_rel(trampoline_efer, 4, %eax)
+        or      bootsym(trampoline_efer), %eax
         wrmsr
 
         mov     $(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_NE |\
@@ -107,6 +106,9 @@ trampoline_protmode_entry:
         mov     %eax,%cr0
 
         /* Now in compatibility mode. Long-jump into 64-bit mode. */
+        mov     $BOOT_DS, %eax
+        mov     %eax, %ds
+        mov     %eax, %es
         ljmp    $BOOT_CS64,$bootsym_rel(start64,6)
 
         .code64
@@ -133,12 +135,16 @@ gdt_48:
         .quad   0x00cf93000000ffff /* 0x0018: ring 0 data */
         .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:
 
         /* Relocations for trampoline Real Mode segments. */
         .pushsection .trampoline_rel, "a"
         .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
         .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
+        .long   trampoline_gdt + BOOT_DS_TRAMP+ 2 - .
+        .long   trampoline_gdt + BOOT_CS32_TRAMP + 2 - .
         .popsection
 
 GLOBAL(trampoline_misc_enable_off)
@@ -162,9 +168,12 @@ LABEL(trampoline_perm_end, 0)
         .code32
 trampoline_boot_cpu_entry:
         /* Switch to relocated trampoline GDT. */
-        lgdt    bootsym_rel(gdt_48, 4)
+        lgdt    %cs:bootsym(gdt_48)
+
+        mov     $BOOT_DS_TRAMP, %eax
+        mov     %eax, %ds
 
-        cmpb    $0,bootsym_rel(skip_realmode,5)
+        cmpb    $0, bootsym(skip_realmode)
         jnz     .Lskip_realmode
 
         /* Load pseudo-real-mode segments. */
@@ -236,7 +245,7 @@ trampoline_boot_cpu_entry:
         mov     %eax, %cr0
 
         /* Load proper protected-mode values into all segment registers. */
-        ljmpl   $BOOT_CS32,$bootsym_rel(1f,6)
+        ljmp    $BOOT_CS32_TRAMP, $bootsym(1f)
         .code32
 1:      mov     $BOOT_DS,%eax
         mov     %eax,%ds
diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index fb9b3da205..6d24ab2bdd 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -7,8 +7,6 @@
         .equ    wakeup_stack, trampoline_start + PAGE_SIZE
         .local  wakeup_stack
 
-#define wakesym(sym) (sym - entry_S3)
-
 /*
  * acpi_sleep_prepare() programs the S3 wakeup vector to point here.
  *
@@ -24,20 +22,26 @@ ENTRY(entry_S3)
 
         # setup data segment
         movw    %cs, %ax
+        sub     $bootsym(entry_S3) / 16, %ax
         movw    %ax, %ds
         movw    %ax, %ss        # A stack required for BIOS call
-        movw    $wakesym(wakeup_stack), %sp
+        movw    $bootsym(wakeup_stack), %sp
 
         pushl   $0              # Kill dangerous flag early
         popfl
 
         # check magic number
-        movl    wakesym(real_magic), %eax
+        movl    bootsym(real_magic), %eax
         cmpl    $0x12345678, %eax
         jne     bogus_real_magic
 
+        push    %ds
+        push    $bootsym(1f)
+        lret
+1:
+
         # for acpi_sleep=s3_bios
-        testb   $1, wakesym(video_flags)
+        testb   $1, bootsym(video_flags)
         jz      1f
         mov     $0xc000, %ax
         mov     %ax, %fs
@@ -52,9 +56,9 @@ ENTRY(entry_S3)
         popfl
 
 1:      # for acpi_sleep=s3_mode
-        testb   $2, wakesym(video_flags)
+        testb   $2, bootsym(video_flags)
         jz      1f
-        movw    wakesym(video_mode), %ax
+        movw    bootsym(video_mode), %ax
         call    mode_setw
 
 1:      # Show some progress if VGA is resumed
@@ -62,13 +66,13 @@ ENTRY(entry_S3)
         movw    %ax, %fs
         movw    $0x0e00 + 'L', %fs:(0x10)
 
-        lidt    wakesym(idt_48)
-        lgdt    wakesym(gdt_48)
+        lidt    bootsym(idt_48)
+        lgdt    bootsym(gdt_48)
 
         mov     $X86_CR0_PE, %eax
         mov     %eax, %cr0
 
-        ljmpl   $BOOT_CS32, $bootsym_rel(wakeup_32, 6)
+        ljmp    $BOOT_CS32_TRAMP, $bootsym(wakeup_32)
 
 # Setting of user mode (AX=mode ID) => CF=success
 mode_setw:
@@ -110,15 +114,16 @@ GLOBAL(video_flags)
 
 wakeup_32:
         /* Set up segment registers and initial stack for protected mode */
-        mov     $BOOT_DS, %eax
+        mov     $BOOT_DS_TRAMP, %eax
         mov     %eax, %ds
+        mov     $BOOT_DS, %eax
         mov     %eax, %ss
         mov     $bootsym_rel(wakeup_stack, 4, %esp)
 
         # check saved magic again
         mov     $abs_saved_magic, %eax
-        add     bootsym_rel(trampoline_xen_phys_start, 4, %eax)
-        mov     (%eax), %eax
+        add     bootsym(trampoline_xen_phys_start), %eax
+        mov     %ss:(%eax), %eax
         cmp     $0x9abcdef0, %eax
         jne     bogus_saved_magic
         
@@ -130,12 +135,12 @@ wakeup_32:
 
         /* Load pagetable base register */
         mov     $abs_idle_pg_table, %eax
-        add     bootsym_rel(trampoline_xen_phys_start,4,%eax)
+        add     bootsym(trampoline_xen_phys_start), %eax
         mov     %eax,%cr3
 
         /* Reapply IA32_MISC_ENABLE modifications from early_init_intel(). */
-        mov     bootsym_rel(trampoline_misc_enable_off, 4, %esi)
-        mov     bootsym_rel(trampoline_misc_enable_off + 4, 4, %edi)
+        mov     bootsym(trampoline_misc_enable_off), %esi
+        mov     bootsym(trampoline_misc_enable_off + 4), %edi
         mov     %esi, %eax
         or      %edi, %eax
         jz      1f
@@ -156,7 +161,7 @@ wakeup_32:
         /* Set up EFER (Extended Feature Enable Register). */
         movl    $MSR_EFER,%ecx
         rdmsr
-        or      bootsym_rel(trampoline_efer, 4, %eax)
+        or      bootsym(trampoline_efer), %eax
         wrmsr
 
         wbinvd
@@ -168,10 +173,12 @@ wakeup_32:
 1:
 
         /* Now in compatibility mode. Long-jump to 64-bit mode */
+        mov     $BOOT_DS, %eax
+        mov     %eax, %ds
         ljmp    $BOOT_CS64, $bootsym_rel(wakeup_64,6)
 
 FUNC_LOCAL(bogus_saved_magic, 0)
-        movw    $0x0e00 + 'S', 0xb8014
+        movw    $0x0e00 + 'S', %ss:0xb8014
         jmp     bogus_saved_magic
 END(bogus_saved_magic)
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 05/11] x86/boot: Share the switch to 64-bit mode between SIPI and S3 paths
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (3 preceding siblings ...)
  2026-12-04 20:50 ` [PATCH 04/11] x86/boot: Reduce relocations in the trampoline Frediano Ziglio
@ 2026-12-04 20:50 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 06/11] x86/boot: Use stack to remove a relocation from trampoline code Frediano Ziglio
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie

The S3 wakeup path duplicated the protected mode to 64-bit mode switch
of the SIPI path: CR4, CR3, IA32_MISC_ENABLE, PAT, EFER and CR0 are set
up the same way, with the same values (0x80050033 is the same CR0 value
the SIPI path builds from the X86_CR0_* constants).

Move this into common_64, entered with DS set to BOOT_DS_TRAMP and EBP
pointing to the 64-bit address to jump to, which is the only difference
left between the two paths.  The S3 data is moved after the code.

There are two functional changes for S3:

- The FPU is now initialised, as on the SIPI path.  Firmware can leave
  it in any state, and the old code even had a "fpu init?" note.

- WBINVD before enabling paging is dropped.  Caches are flushed with
  ACPI_FLUSH_CPU_CACHE() before entering S3, and CR0.CD/NW are already
  cleared by the first write to CR0 in real mode, so caches are enabled
  with nothing stale to write back.  The SIPI path, which has the same
  requirements, never had it.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 xen/arch/x86/boot/trampoline.S | 38 ++++++++++++++++---
 xen/arch/x86/boot/wakeup.S     | 69 +++++-----------------------------
 2 files changed, 41 insertions(+), 66 deletions(-)

diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index c34ca9beae..c52c27795d 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -59,10 +59,20 @@ GLOBAL(entry_SIPI16)
 
         .code32
 trampoline_protmode_entry:
+        mov     $bootsym(high_start_address), %ebp
+
         /* Set up a few descriptors: on entry only CS is guaranteed good. */
         mov     $BOOT_DS_TRAMP, %eax
         mov     %eax,%ds
 
+        /*
+         * Common path to 64-bit mode, used by SIPI and S3.
+         * On entry:
+         * - DS is BOOT_DS_TRAMP;
+         * - EBP is the trampoline offset of the 64-bit address to jump to;
+         * - EBX is passed on unchanged.
+         */
+common_64:
         /* Set up FPU. */
         fninit
 
@@ -101,26 +111,42 @@ trampoline_protmode_entry:
         or      bootsym(trampoline_efer), %eax
         wrmsr
 
+        /* Enable paging */
         mov     $(X86_CR0_PG | X86_CR0_AM | X86_CR0_WP | X86_CR0_NE |\
                   X86_CR0_ET | X86_CR0_MP | X86_CR0_PE), %eax
         mov     %eax,%cr0
 
-        /* Now in compatibility mode. Long-jump into 64-bit mode. */
-        mov     $BOOT_DS, %eax
-        mov     %eax, %ds
-        mov     %eax, %es
+        /*
+         * Now in compatibility mode.  Load the target address before
+         * switching DS: (%ebp) would use SS, which isn't set up on the SIPI
+         * path.
+         */
+        mov     %ebp, %eax
+        mov     4(%eax), %edx
+        mov     (%eax), %eax
+
+        /* Load correct selectors */
+        mov     $BOOT_DS, %ecx
+        mov     %ecx, %ds
+        mov     %ecx, %es
+
+        /* Long-jump into 64-bit mode. */
         ljmp    $BOOT_CS64,$bootsym_rel(start64,6)
 
         .code64
 start64:
+        movl    %eax, %eax
+        shl     $32, %rdx
+        or      %rdx, %rax
         /* Jump to high mappings. */
-        movabs  $(abs___high_start + XEN_VIRT_START), %rdi
-        jmpq    *%rdi
+        jmpq    *%rax
 
 #include "video.h"
 #include "wakeup.S"
 
         .balign 8
+high_start_address:
+        .quad   abs___high_start + XEN_VIRT_START
         .word   0
 idt_48: .word   0, 0, 0 # base = limit = 0
 
diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index 6d24ab2bdd..ecbf2852d8 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -101,12 +101,6 @@ bogus_real_magic:
         movw    $0x0e00 + 'B', %fs:(0x12)
         jmp     1b
 
-        .align 4
-real_magic:     .long 0x12345678
-video_mode:     .word 0
-GLOBAL(video_flags)
-        .byte 0
-
         .code32
 
         # Now in protect mode, with paging disabled
@@ -126,64 +120,19 @@ wakeup_32:
         mov     %ss:(%eax), %eax
         cmp     $0x9abcdef0, %eax
         jne     bogus_saved_magic
-        
-        /* fpu init? */
-
-        /* Initialise CR4. */
-        mov     $X86_CR4_PAE, %ecx
-        mov     %ecx, %cr4
-
-        /* Load pagetable base register */
-        mov     $abs_idle_pg_table, %eax
-        add     bootsym(trampoline_xen_phys_start), %eax
-        mov     %eax,%cr3
-
-        /* Reapply IA32_MISC_ENABLE modifications from early_init_intel(). */
-        mov     bootsym(trampoline_misc_enable_off), %esi
-        mov     bootsym(trampoline_misc_enable_off + 4), %edi
-        mov     %esi, %eax
-        or      %edi, %eax
-        jz      1f
-        mov     $MSR_IA32_MISC_ENABLE, %ecx
-        rdmsr
-        not     %esi
-        not     %edi
-        and     %esi, %eax
-        and     %edi, %edx
-        wrmsr
-1:
-        /* Set up PAT before enabling paging. */
-        mov     $XEN_MSR_PAT & 0xffffffff, %eax
-        mov     $XEN_MSR_PAT >> 32, %edx
-        mov     $MSR_IA32_CR_PAT, %ecx
-        wrmsr
-
-        /* Set up EFER (Extended Feature Enable Register). */
-        movl    $MSR_EFER,%ecx
-        rdmsr
-        or      bootsym(trampoline_efer), %eax
-        wrmsr
-
-        wbinvd
-
-        /* Enable paging and flush prefetch queue */
-        mov     $0x80050033,%eax /* hi-to-lo: PG,AM,WP,NE,ET,MP,PE */
-        mov     %eax,%cr0
-        jmp     1f
-1:
 
-        /* Now in compatibility mode. Long-jump to 64-bit mode */
-        mov     $BOOT_DS, %eax
-        mov     %eax, %ds
-        ljmp    $BOOT_CS64, $bootsym_rel(wakeup_64,6)
+        mov     $bootsym(s3_start_address), %ebp
+        jmp     common_64
 
 FUNC_LOCAL(bogus_saved_magic, 0)
         movw    $0x0e00 + 'S', %ss:0xb8014
         jmp     bogus_saved_magic
 END(bogus_saved_magic)
 
-        .code64
-wakeup_64:
-        /* Jump to high mappings and the higher-level wakeup code. */
-        movabs  $(abs_s3_resume + XEN_VIRT_START), %rbx
-        jmp     *%rbx
+        .align 8
+s3_start_address:
+        .quad   abs_s3_resume + XEN_VIRT_START
+real_magic:     .long 0x12345678
+video_mode:     .word 0
+GLOBAL(video_flags)
+        .byte 0
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 06/11] x86/boot: Use stack to remove a relocation from trampoline code
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (4 preceding siblings ...)
  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 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy Frediano Ziglio
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie

The destination selector is computed from a GDT entry.
The stack is setup anyway to allow calling real mode interrupts.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 xen/arch/x86/boot/trampoline.S | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index c52c27795d..faa5bb9e30 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -27,13 +27,6 @@
         .long 111b - (off) - .;            \
         .popsection
 
-#define bootsym_segrel(sym, off)           \
-        $0,$bootsym(sym);                  \
-111:;                                      \
-        .pushsection .trampoline_seg, "a"; \
-        .long 111b - (off) - .;            \
-        .popsection
-
 /* Start of the permanent trampoline code. */
 
         .code16
@@ -210,6 +203,9 @@ trampoline_boot_cpu_entry:
         mov     %eax,%gs
         mov     %eax,%ss
 
+        /* Initialise stack pointer. */
+        xor     %esp, %esp
+
         /* Switch to pseudo-rm CS, enter real mode, and flush insn queue. */
         mov     %cr0,%eax
         dec     %eax
@@ -218,14 +214,16 @@ trampoline_boot_cpu_entry:
 1:      mov     %eax,%cr0                 # CR0.PE = 0 (leave protected mode)
 
         /* Load proper real-mode values into %cs, %ds, %es and %ss. */
-        ljmp    bootsym_segrel(1f,2)
-1:      mov     %cs,%ax
-        mov     %ax,%ds
+        mov     bootsym(trampoline_gdt)+BOOT_PSEUDORM_DS+3, %ax
+        shl     $4, %ax
+        pushw   %ax
+        pushw   $bootsym(1f)
+        lretw
+1:      mov     %ax,%ds
         mov     %ax,%es
         mov     %ax,%ss
 
-        /* Initialise stack pointer and IDT, and enable irqs. */
-        xor     %esp,%esp
+        /* Initialise IDT, and enable irqs. */
         lidt    bootsym(rm_idt)
         sti
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (5 preceding siblings ...)
  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 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 08/11] x86/boot: Group trampoline code and data using subsections Frediano Ziglio
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Daniel P. Smith, Marek Marczykowski-Górecki

The trampoline relocations were applied to the trampoline inside the
Xen image, before copying it to low memory.

Move the copy into C, together with the relocations, and apply them to
the low memory copy, reading the original values from the image, so
the image is never relocated.  The relocation entries are now 16-bit
offsets from trampoline_start, and the segment relocations, no longer
used, are removed.

As the GDT in the image is no longer relocated, jump to the low memory
copy using the flat BOOT_CS32 selector.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
---
 xen/arch/x86/boot/Makefile              |  6 ++--
 xen/arch/x86/boot/build32.lds.S         |  4 +--
 xen/arch/x86/boot/copy-trampoline.c     | 42 +++++++++++++++++++++++++
 xen/arch/x86/boot/head.S                | 14 +++------
 xen/arch/x86/boot/reloc-trampoline.c    | 34 --------------------
 xen/arch/x86/boot/trampoline.S          | 19 +++++++----
 xen/arch/x86/efi/efi-boot.h             |  7 +----
 xen/arch/x86/include/asm/boot-helpers.h |  2 +-
 xen/arch/x86/xen.lds.S                  |  5 +--
 9 files changed, 67 insertions(+), 66 deletions(-)
 create mode 100644 xen/arch/x86/boot/copy-trampoline.c
 delete mode 100644 xen/arch/x86/boot/reloc-trampoline.c

diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index 8411535397..06fef25d22 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -4,9 +4,9 @@ obj-bin-y += $(obj64)
 
 obj32 := cmdline.32.o
 obj32 += reloc.32.o
-obj32 += reloc-trampoline.32.o
+obj32 += copy-trampoline.32.o
 
-obj64 := reloc-trampoline.o
+obj64 := copy-trampoline.o
 
 nocov-y   += $(obj32) $(obj64)
 noubsan-y += $(obj32) $(obj64)
@@ -94,7 +94,7 @@ cmd_combine = \
               --bin1      $(obj)/built-in-32.base.bin \
               --bin2      $(obj)/built-in-32.offset.bin \
               --map       $(obj)/built-in-32.base.map \
-              --exports   cmdline_parse_early,reloc,reloc_trampoline32 \
+              --exports   cmdline_parse_early,reloc,copy_trampoline32 \
               --output    $@
 
 targets += built-in-32.S
diff --git a/xen/arch/x86/boot/build32.lds.S b/xen/arch/x86/boot/build32.lds.S
index e4f7c7fc2f..5cb6f0162b 100644
--- a/xen/arch/x86/boot/build32.lds.S
+++ b/xen/arch/x86/boot/build32.lds.S
@@ -45,10 +45,10 @@ SECTIONS
          */
         DECLARE_IMPORT(__trampoline_rel_start);
         DECLARE_IMPORT(__trampoline_rel_stop);
-        DECLARE_IMPORT(__trampoline_seg_start);
-        DECLARE_IMPORT(__trampoline_seg_stop);
         DECLARE_IMPORT(trampoline_phys);
         DECLARE_IMPORT(boot_vid_info);
+        DECLARE_IMPORT(trampoline_start);
+        DECLARE_IMPORT(trampoline_end);
         . = . + GAP;
         *(.text)
         *(.text.*)
diff --git a/xen/arch/x86/boot/copy-trampoline.c b/xen/arch/x86/boot/copy-trampoline.c
new file mode 100644
index 0000000000..280b24eabe
--- /dev/null
+++ b/xen/arch/x86/boot/copy-trampoline.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/compiler.h>
+#include <xen/stdint.h>
+
+#include <asm/boot-helpers.h>
+#include <asm/trampoline.h>
+
+extern const uint16_t __trampoline_rel_start[], __trampoline_rel_stop[];
+
+#if defined(__i386__)
+void asmlinkage copy_trampoline32(void)
+#elif defined (__x86_64__)
+void copy_trampoline64(void)
+#else
+#error Unknown architecture
+#endif
+{
+    const unsigned long dst_phys = (unsigned long)trampoline_phys;
+    const unsigned long src_phys = (unsigned long)trampoline_start;
+    const uint32_t *src = (const uint32_t *)trampoline_start;
+    uint32_t *dst = (uint32_t *)dst_phys;
+    const uint16_t *ptr;
+
+    /*
+     * Copy bootstrap trampoline to low memory, below 1MB.
+     *
+     * Don't use memcpy(): the 32-bit version has no library to link against
+     * and, unlike GCC, Clang emits a call for __builtin_memcpy() with a
+     * variable size.  Both start and end are 4-byte aligned.
+     */
+    while ( src < (const uint32_t *)trampoline_end )
+        *dst++ = *src++;
+
+    /*
+     * Apply relocations to trampoline.
+     *
+     * This modifies the final copy of the trampoline.
+     */
+    for ( ptr = __trampoline_rel_start; ptr < __trampoline_rel_stop; ++ptr )
+        *(uint32_t *)(*ptr + dst_phys) = *(uint32_t *)(*ptr + src_phys) + dst_phys;
+}
diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 0e1388ac1f..88c9639f32 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -680,9 +680,6 @@ trampoline_setup:
         shr     $PAGE_SHIFT, %ecx                /* %ecx = Slot to write */
         mov     %edx, sym_offs(l1_bootmap)(%esi, %ecx, 8)
 
-        /* Apply relocations to bootstrap trampoline. */
-        call    reloc_trampoline32
-
         /* Do not parse command line on EFI platform here. */
         cmpb    $0, sym_esi(efi_platform)
         jnz     1f
@@ -698,18 +695,15 @@ trampoline_setup:
         call    cmdline_parse_early
 
 1:
+        call    copy_trampoline32
+
         /* Switch to low-memory stack which lives at the end of trampoline region. */
         mov     sym_esi(trampoline_phys), %edi
         lea     TRAMPOLINE_SIZE(%edi), %esp
-        mov     $(trampoline_boot_cpu_entry-trampoline_start), %eax
-        pushl   $BOOT_CS32_TRAMP
+        lea     trampoline_boot_cpu_entry-trampoline_start(%edi), %eax
+        pushl   $BOOT_CS32
         push    %eax
 
-        /* Copy bootstrap trampoline to low memory, below 1MB. */
-        lea     sym_esi(trampoline_start), %esi
-        mov     $((trampoline_end - trampoline_start) / 4),%ecx
-        rep movsl
-
         /* Jump into the relocated trampoline. */
         lret
 
diff --git a/xen/arch/x86/boot/reloc-trampoline.c b/xen/arch/x86/boot/reloc-trampoline.c
deleted file mode 100644
index f52b9b265b..0000000000
--- a/xen/arch/x86/boot/reloc-trampoline.c
+++ /dev/null
@@ -1,34 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-#include <xen/compiler.h>
-#include <xen/stdint.h>
-
-#include <asm/boot-helpers.h>
-#include <asm/trampoline.h>
-
-extern const int32_t __trampoline_rel_start[], __trampoline_rel_stop[];
-extern const int32_t __trampoline_seg_start[], __trampoline_seg_stop[];
-
-#if defined(__i386__)
-void asmlinkage reloc_trampoline32(void)
-#elif defined (__x86_64__)
-void reloc_trampoline64(void)
-#else
-#error Unknown architecture
-#endif
-{
-    uint32_t phys = trampoline_phys;
-    const int32_t *ptr;
-
-    /*
-     * Apply relocations to trampoline.
-     *
-     * This modifies the trampoline in place within Xen, so that it will
-     * operate correctly when copied into place.
-     */
-    for ( ptr = __trampoline_rel_start; ptr < __trampoline_rel_stop; ++ptr )
-        *(uint32_t *)(*ptr + (long)ptr) += phys;
-
-    for ( ptr = __trampoline_seg_start; ptr < __trampoline_seg_stop; ++ptr )
-        *(uint16_t *)(*ptr + (long)ptr) = phys >> 4;
-}
diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index faa5bb9e30..c9c5d6c27e 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -24,7 +24,7 @@
         bootsym(sym),##opnd;               \
 111:;                                      \
         .pushsection .trampoline_rel, "a"; \
-        .long 111b - (off) - .;            \
+        .word bootsym(111b) - (off);       \
         .popsection
 
 /* Start of the permanent trampoline code. */
@@ -52,6 +52,8 @@ GLOBAL(entry_SIPI16)
 
         .code32
 trampoline_protmode_entry:
+        /* This code will use %ebx to detect if BSP or not. */
+
         mov     $bootsym(high_start_address), %ebp
 
         /* Set up a few descriptors: on entry only CS is guaranteed good. */
@@ -160,10 +162,10 @@ gdt_48:
 
         /* Relocations for trampoline Real Mode segments. */
         .pushsection .trampoline_rel, "a"
-        .long   trampoline_gdt + BOOT_PSEUDORM_CS + 2 - .
-        .long   trampoline_gdt + BOOT_PSEUDORM_DS + 2 - .
-        .long   trampoline_gdt + BOOT_DS_TRAMP+ 2 - .
-        .long   trampoline_gdt + BOOT_CS32_TRAMP + 2 - .
+        .word   bootsym(trampoline_gdt) + BOOT_PSEUDORM_CS + 2
+        .word   bootsym(trampoline_gdt) + BOOT_PSEUDORM_DS + 2
+        .word   bootsym(trampoline_gdt) + BOOT_DS_TRAMP + 2
+        .word   bootsym(trampoline_gdt) + BOOT_CS32_TRAMP + 2
         .popsection
 
 GLOBAL(trampoline_misc_enable_off)
@@ -186,8 +188,13 @@ LABEL(trampoline_perm_end, 0)
 
         .code32
 trampoline_boot_cpu_entry:
+        call    1f
+1:
+        pop     %ebx
+        sub     $bootsym(1b), %ebx
+
         /* Switch to relocated trampoline GDT. */
-        lgdt    %cs:bootsym(gdt_48)
+        lgdt    bootsym(gdt_48)(%ebx)
 
         mov     $BOOT_DS_TRAMP, %eax
         mov     %eax, %ds
diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h
index 2baecb20b4..0f4598a0ca 100644
--- a/xen/arch/x86/efi/efi-boot.h
+++ b/xen/arch/x86/efi/efi-boot.h
@@ -121,11 +121,6 @@ static void __init efi_arch_relocate_image(unsigned long delta)
 static void __init relocate_trampoline(unsigned long phys)
 {
     trampoline_phys = phys;
-
-    if ( !efi_enabled(EFI_LOADER) )
-        return;
-
-    reloc_trampoline64();
 }
 
 DEFINE_BRK(efi, MB(1));
@@ -260,7 +255,7 @@ static void __init noreturn efi_arch_post_exit_boot(void)
     u64 cr4 = XEN_MINIMAL_CR4 & ~X86_CR4_PGE, efer;
 
     efi_arch_relocate_image(__XEN_VIRT_START - xen_phys_start);
-    memcpy(_p(trampoline_phys), trampoline_start, cfg.size);
+    copy_trampoline64();
 
     /*
      * We're in physical mode right now (i.e. identity map), so a regular
diff --git a/xen/arch/x86/include/asm/boot-helpers.h b/xen/arch/x86/include/asm/boot-helpers.h
index 391e16a730..8d9774860a 100644
--- a/xen/arch/x86/include/asm/boot-helpers.h
+++ b/xen/arch/x86/include/asm/boot-helpers.h
@@ -8,6 +8,6 @@
 #ifndef X86_BOOT_HELPERS_H
 #define X86_BOOT_HELPERS_H
 
-void reloc_trampoline64(void);
+void copy_trampoline64(void);
 
 #endif /* X86_BOOT_HELPERS_H */
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index 9d8da60455..6b80de3955 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -243,13 +243,10 @@ SECTIONS
 #endif
        *(.init.data.rel)
        *(.init.data.rel.*)
-       . = ALIGN(4);
+       . = ALIGN(2);
        __trampoline_rel_start = .;
        *(.trampoline_rel)
        __trampoline_rel_stop = .;
-       __trampoline_seg_start = .;
-       *(.trampoline_seg)
-       __trampoline_seg_stop = .;
        /*
         * struct alt_inst entries. From the header (alternative.h):
         * "Alternative instructions for different CPU types or capabilities"
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 08/11] x86/boot: Group trampoline code and data using subsections
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (6 preceding siblings ...)
  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 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data Frediano Ziglio
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Claude Opus 5 . 5

The trampoline mixes code, data used only by the trampoline itself and
data also used from outside it (head.S, or C code via bootsym()).

Use subsections of .init.trampoline to lay it out as:
- permanent code;
- permanent data used only by the trampoline;
- data also used from outside the trampoline, the permanent part
  followed, from trampoline_perm_end, by the boot-only part;
- boot-only code;
- boot-only data used only by the trampoline.

Data is kept next to the code using it in the sources, and placed in
its group with .pushsection/.popsection.  All the data used from
outside is now contiguous.

Move kbd_shift_flags into the padding byte of early_boot_opts, and
mark the byte as reserved in the C view of the structure.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/x86/boot/cmdline.c    |  2 +-
 xen/arch/x86/boot/edd.S        |  2 ++
 xen/arch/x86/boot/head.S       |  1 +
 xen/arch/x86/boot/mem.S        |  2 ++
 xen/arch/x86/boot/trampoline.S | 44 +++++++++++++++++++++++++++++-----
 xen/arch/x86/boot/video.S      |  8 +++++++
 xen/arch/x86/boot/wakeup.S     |  5 ++++
 7 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/xen/arch/x86/boot/cmdline.c b/xen/arch/x86/boot/cmdline.c
index cf9e32b6d9..310acee544 100644
--- a/xen/arch/x86/boot/cmdline.c
+++ b/xen/arch/x86/boot/cmdline.c
@@ -30,7 +30,7 @@ typedef struct __packed {
     uint8_t skip_realmode;
     uint8_t opt_edd;
     uint8_t opt_edid;
-    uint8_t padding;
+    uint8_t reserved;           /* Used by the trampoline. */
 #ifdef CONFIG_VIDEO
     uint16_t boot_vid_mode;
     uint16_t vesa_width;
diff --git a/xen/arch/x86/boot/edd.S b/xen/arch/x86/boot/edd.S
index 02437511b2..58af7744e4 100644
--- a/xen/arch/x86/boot/edd.S
+++ b/xen/arch/x86/boot/edd.S
@@ -163,6 +163,7 @@ edd_next:
 .Ledd_done:
         ret
 
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
 GLOBAL(boot_edd_info_nr)
         .byte   0
 GLOBAL(boot_mbr_signature_nr)
@@ -171,3 +172,4 @@ GLOBAL(boot_mbr_signature)
         .fill   EDD_MBR_SIG_MAX*8,1,0
 GLOBAL(boot_edd_info)
         .fill   EDD_INFO_MAX * (EDDEXTSIZE + EDDPARMSIZE), 1, 0
+        .popsection
diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 88c9639f32..0e055ff070 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -725,6 +725,7 @@ trampoline_setup:
         .section .init.trampoline, "a", @progbits
 ENTRY(trampoline_start)
 #include "trampoline.S"
+        .subsection TRAMP_LAST
 ENTRY(trampoline_end)
 
 #include "x86_64.S"
diff --git a/xen/arch/x86/boot/mem.S b/xen/arch/x86/boot/mem.S
index 94ffc87d50..96b03a00c8 100644
--- a/xen/arch/x86/boot/mem.S
+++ b/xen/arch/x86/boot/mem.S
@@ -31,8 +31,10 @@ get_memory_map:
 .Ldone:
         ret
 
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
         .align  4
 GLOBAL(bios_e820map)
         .fill   E820_BIOS_MAX*20,1,0
 GLOBAL(bios_e820nr)
         .long   0
+        .popsection
diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index c9c5d6c27e..2898e5da77 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -27,6 +27,27 @@
         .word bootsym(111b) - (off);       \
         .popsection
 
+/*
+ * The trampoline is assembled in subsections of .init.trampoline, laid out in
+ * this order:
+ * - permanent code;
+ * - permanent data used only by the trampoline;
+ * - data also used from outside the trampoline (head.S, or C code via
+ *   bootsym()): first the permanent part, then, starting at
+ *   trampoline_perm_end, the boot-only one;
+ * - boot-only code;
+ * - boot-only data used only by the trampoline.
+ * Code is in the current subsection; data is put in its group with
+ * .pushsection/.popsection.
+ */
+#define TRAMP_PERM_CODE      0
+#define TRAMP_PERM_DATA      1
+#define TRAMP_PERM_EXT_DATA  2
+#define TRAMP_BOOT_EXT_DATA  3
+#define TRAMP_BOOT_CODE      4
+#define TRAMP_BOOT_DATA      5
+#define TRAMP_LAST           TRAMP_BOOT_DATA
+
 /* Start of the permanent trampoline code. */
 
         .code16
@@ -139,6 +160,7 @@ start64:
 #include "video.h"
 #include "wakeup.S"
 
+        .pushsection .init.trampoline, TRAMP_PERM_DATA
         .balign 8
 high_start_address:
         .quad   abs___high_start + XEN_VIRT_START
@@ -167,7 +189,10 @@ gdt_48:
         .word   bootsym(trampoline_gdt) + BOOT_DS_TRAMP + 2
         .word   bootsym(trampoline_gdt) + BOOT_CS32_TRAMP + 2
         .popsection
+        .popsection
 
+        .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
+        .balign 8
 GLOBAL(trampoline_misc_enable_off)
         .quad   0
 
@@ -181,11 +206,16 @@ GLOBAL(trampoline_xen_phys_start)
 
 GLOBAL(trampoline_cpu_started)
         .byte   0
-
-LABEL(trampoline_perm_end, 0)
+        .popsection
 
 /* From here on early boot only. */
 
+        /* Must be the first thing in TRAMP_BOOT_EXT_DATA. */
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
+LABEL(trampoline_perm_end, 0)
+        .popsection
+
+        .subsection TRAMP_BOOT_CODE
         .code32
 trampoline_boot_cpu_entry:
         call    1f
@@ -292,6 +322,7 @@ trampoline_boot_cpu_entry:
         /* Jump to the common bootstrap entry point. */
         jmp     trampoline_protmode_entry
 
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
         .align  2
 /* Keep in sync with cmdline.c:early_boot_opts_t type! */
 early_boot_opts:
@@ -301,7 +332,8 @@ opt_edd:
         .byte   0                               /* edd=on/off/skipmbr */
 opt_edid:
         .byte   0                               /* EDID parsing option (force/no/default). */
-/* Padding. */
+
+GLOBAL(kbd_shift_flags)
         .byte   0
 
 #ifdef CONFIG_VIDEO
@@ -310,11 +342,11 @@ boot_vid_mode:
 vesa_size:
         .word   0,0,0                           /* width x depth x height */
 #endif
+        .popsection
 
-GLOBAL(kbd_shift_flags)
-        .byte   0
-
+        .pushsection .init.trampoline, TRAMP_BOOT_DATA
 rm_idt: .word   256*4-1, 0, 0
+        .popsection
 
 #include "mem.S"
 #include "edd.S"
diff --git a/xen/arch/x86/boot/video.S b/xen/arch/x86/boot/video.S
index f78906878a..57c8e790e8 100644
--- a/xen/arch/x86/boot/video.S
+++ b/xen/arch/x86/boot/video.S
@@ -495,6 +495,7 @@ setvesabysize:
         movw    %bx,bootsym(boot_vid_mode)
         jmp     check_vesa
 
+        .pushsection .init.trampoline, TRAMP_BOOT_DATA
 # Table of routines for setting of the special modes.
 spec_inits:
         .word   bootsym(set_80x25)
@@ -505,6 +506,7 @@ spec_inits:
         .word   bootsym(set_80x30)
         .word   bootsym(set_80x34)
         .word   bootsym(set_80x60)
+        .popsection
 
 # Set the 80x25 mode. If already set, do nothing.
 set_80x25:
@@ -649,6 +651,7 @@ mode_table:
 mtab1:  movw    $modelist, %si          # SI=mode list, DI=list end
 ret0:   ret
 
+        .pushsection .init.trampoline, TRAMP_BOOT_DATA
 # Modes usable on all standard VGAs
 vga_modes:
         .word   VIDEO_80x25, 0x50,0x19,0        # 80x25
@@ -659,6 +662,7 @@ vga_modes:
         .word   VIDEO_80x34, 0x50,0x22,0        # 80x34
         .word   VIDEO_80x60, 0x50,0x3c,0        # 80x60
 vga_modes_end:
+        .popsection
 
 # If the current mode is a VESA graphics one, obtain its parameters.
 set_current:
@@ -946,6 +950,7 @@ store_edid:
 #endif
         ret
 
+        .pushsection .init.trampoline, TRAMP_BOOT_DATA
                 .p2align 1
 mt_end:         .word   0       # End of video mode table if built
 edit_buf:       .space  6       # Line editor buffer
@@ -992,7 +997,9 @@ name_bann:      .asciz  "Video adapter: "
 
                 .p2align 1
 force_size:     .word   0       # Use this size instead of the one in BIOS vars
+        .popsection
 
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
 GLOBAL(boot_vid_info)
         .byte   0, 0    /* orig_x, orig_y */
         .byte   3       /* text mode 3    */
@@ -1004,3 +1011,4 @@ GLOBAL(boot_edid_info)
         .fill   128,1,0x13
 GLOBAL(boot_edid_caps)
         .word   0x1313
+        .popsection
diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index ecbf2852d8..f602541972 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -129,10 +129,15 @@ FUNC_LOCAL(bogus_saved_magic, 0)
         jmp     bogus_saved_magic
 END(bogus_saved_magic)
 
+        .pushsection .init.trampoline, TRAMP_PERM_DATA
         .align 8
 s3_start_address:
         .quad   abs_s3_resume + XEN_VIRT_START
 real_magic:     .long 0x12345678
 video_mode:     .word 0
+        .popsection
+
+        .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
 GLOBAL(video_flags)
         .byte 0
+        .popsection
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (7 preceding siblings ...)
  2026-12-04 20:50 ` [PATCH 08/11] x86/boot: Group trampoline code and data using subsections Frediano Ziglio
@ 2026-12-04 20:50 ` Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S Frediano Ziglio
  2026-12-04 20:50 ` [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers Frediano Ziglio
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Stefano Stabellini, Julien Grall,
	Bertrand Marquis, Michal Orzel, Volodymyr Babchuk, Jan Beulich,
	Andrew Cooper, Roger Pau Monné, Teddy Astie, Daniel P. Smith,
	Marek Marczykowski-Górecki, Claude Opus 5 . 5

The trampoline data also used from outside it (command line options,
EFER mask, data collected from EFI or the BIOS, ...) is written while
the trampoline is still part of the Xen image.  For xen.efi this forces
the whole trampoline into a data section, for StrictNX compatibility.

As this data is now contiguous, give it a writable copy in
.init.bss.trampoline, placed in .init.data by the linker script.
Symbols are defined in both using GLOBAL_COPY(): "sym" in the copy, used
from outside the trampoline, and "l_sym" in the trampoline, used by
trampoline code via bootlsym().  bootsym() in assembly now multiplies by
1, so that using a symbol from another section, such as the copy, fails
to assemble.

The copy is filled very early, before anything writes to it: in __start
for BIOS, Multiboot and PVH boots, in efi_multiboot2_prelude() for
MB2+EFI, and in a new efi_arch_early_init() hook, called first in
efi_start() and empty on Arm, for xen.efi.  When the trampoline is
placed in low memory, the copy replaces the original data.

bootsym() in C now addresses the copy, using bootsym_data_phys(), while
bootsym_phys() is still used for code symbols.  The linker script
defines trampoline_data_base, where trampoline_start would be if the
copy was part of a whole trampoline, so that bootsym_data_phys() needs
a single difference, like bootsym_phys().

As the trampoline in the image is never written to, place it in
.init.text for xen.efi too, and mark its section as code.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/arm/efi/efi-boot.h             |  4 ++
 xen/arch/x86/boot/Makefile              |  2 +-
 xen/arch/x86/boot/build32.lds.S         |  4 ++
 xen/arch/x86/boot/copy-trampoline.c     | 53 ++++++++++++++------
 xen/arch/x86/boot/edd.S                 | 24 ++++-----
 xen/arch/x86/boot/head.S                | 22 +++------
 xen/arch/x86/boot/mem.S                 | 10 ++--
 xen/arch/x86/boot/trampoline.S          | 66 +++++++++++++++++++------
 xen/arch/x86/boot/video.S               | 16 +++---
 xen/arch/x86/boot/wakeup.S              |  8 +--
 xen/arch/x86/efi/efi-boot.h             |  6 +++
 xen/arch/x86/efi/mbi2.c                 |  4 ++
 xen/arch/x86/include/asm/boot-helpers.h |  1 +
 xen/arch/x86/include/asm/trampoline.h   | 42 ++++++++++++++--
 xen/arch/x86/xen.lds.S                  | 13 +++--
 xen/common/efi/boot.c                   |  1 +
 16 files changed, 193 insertions(+), 83 deletions(-)

diff --git a/xen/arch/arm/efi/efi-boot.h b/xen/arch/arm/efi/efi-boot.h
index 069cc68b0a..4f39537c1c 100644
--- a/xen/arch/arm/efi/efi-boot.h
+++ b/xen/arch/arm/efi/efi-boot.h
@@ -924,6 +924,10 @@ static void noreturn __init efi_arch_halt(void)
     stop_cpu();
 }
 
+static void __init efi_arch_early_init(void)
+{
+}
+
 static void __init efi_arch_load_addr_check(const EFI_LOADED_IMAGE *loaded_image)
 {
     if ( (unsigned long)loaded_image->ImageBase & ((1 << 12) - 1) )
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index 06fef25d22..2e2e903a88 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -94,7 +94,7 @@ cmd_combine = \
               --bin1      $(obj)/built-in-32.base.bin \
               --bin2      $(obj)/built-in-32.offset.bin \
               --map       $(obj)/built-in-32.base.map \
-              --exports   cmdline_parse_early,reloc,copy_trampoline32 \
+              --exports   cmdline_parse_early,reloc,init_trampoline_data32,copy_trampoline32 \
               --output    $@
 
 targets += built-in-32.S
diff --git a/xen/arch/x86/boot/build32.lds.S b/xen/arch/x86/boot/build32.lds.S
index 5cb6f0162b..e6ec3b2fa3 100644
--- a/xen/arch/x86/boot/build32.lds.S
+++ b/xen/arch/x86/boot/build32.lds.S
@@ -49,6 +49,10 @@ SECTIONS
         DECLARE_IMPORT(boot_vid_info);
         DECLARE_IMPORT(trampoline_start);
         DECLARE_IMPORT(trampoline_end);
+        DECLARE_IMPORT(l_trampoline_data_start);
+        DECLARE_IMPORT(l_trampoline_data_end);
+        DECLARE_IMPORT(trampoline_data_start);
+        DECLARE_IMPORT(trampoline_data_end);
         . = . + GAP;
         *(.text)
         *(.text.*)
diff --git a/xen/arch/x86/boot/copy-trampoline.c b/xen/arch/x86/boot/copy-trampoline.c
index 280b24eabe..188599c52e 100644
--- a/xen/arch/x86/boot/copy-trampoline.c
+++ b/xen/arch/x86/boot/copy-trampoline.c
@@ -8,34 +8,59 @@
 
 extern const uint16_t __trampoline_rel_start[], __trampoline_rel_stop[];
 
+/*
+ * Don't use memcpy(): the 32-bit version has no library to link against and,
+ * unlike GCC, Clang emits a call for __builtin_memcpy() with a variable size.
+ * All the areas copied are 4-byte aligned.
+ */
+static void copy_words(void *dst, const void *src, const void *src_end)
+{
+    uint32_t *d = dst;
+    const uint32_t *s = src;
+
+    while ( s < (const uint32_t *)src_end )
+        *d++ = *s++;
+}
+
 #if defined(__i386__)
-void asmlinkage copy_trampoline32(void)
+void asmlinkage init_trampoline_data32(void)
 #elif defined (__x86_64__)
-void copy_trampoline64(void)
+void init_trampoline_data64(void)
 #else
 #error Unknown architecture
 #endif
+{
+    /*
+     * Fill the writable copy of the trampoline data.  This must happen before
+     * anything writes to it.
+     */
+    copy_words(trampoline_data_start, l_trampoline_data_start,
+               l_trampoline_data_end);
+}
+
+#if defined(__i386__)
+void asmlinkage copy_trampoline32(void)
+#elif defined (__x86_64__)
+void copy_trampoline64(void)
+#endif
 {
     const unsigned long dst_phys = (unsigned long)trampoline_phys;
     const unsigned long src_phys = (unsigned long)trampoline_start;
-    const uint32_t *src = (const uint32_t *)trampoline_start;
-    uint32_t *dst = (uint32_t *)dst_phys;
     const uint16_t *ptr;
 
-    /*
-     * Copy bootstrap trampoline to low memory, below 1MB.
-     *
-     * Don't use memcpy(): the 32-bit version has no library to link against
-     * and, unlike GCC, Clang emits a call for __builtin_memcpy() with a
-     * variable size.  Both start and end are 4-byte aligned.
-     */
-    while ( src < (const uint32_t *)trampoline_end )
-        *dst++ = *src++;
+    /* Copy bootstrap trampoline to low memory, below 1MB. */
+    copy_words((void *)dst_phys, trampoline_start, trampoline_end);
+
+    /* Replace the original data with the writable copy. */
+    copy_words((void *)(dst_phys + (l_trampoline_data_start - trampoline_start)),
+               trampoline_data_start, trampoline_data_end);
 
     /*
      * Apply relocations to trampoline.
      *
-     * This modifies the final copy of the trampoline.
+     * This modifies the final copy of the trampoline.  The relocated values
+     * are read from the original, so there must be no relocations in the
+     * data with a writable copy.
      */
     for ( ptr = __trampoline_rel_start; ptr < __trampoline_rel_stop; ++ptr )
         *(uint32_t *)(*ptr + dst_phys) = *(uint32_t *)(*ptr + src_phys) + dst_phys;
diff --git a/xen/arch/x86/boot/edd.S b/xen/arch/x86/boot/edd.S
index 58af7744e4..58e2443d0b 100644
--- a/xen/arch/x86/boot/edd.S
+++ b/xen/arch/x86/boot/edd.S
@@ -41,7 +41,7 @@ get_edd:
 # This code is sensitive to the size of the structs in edd.h
 edd_start:
         /* ds:si points at fn48 results. Fn41 results go immediately before. */
-        movw    $bootsym(boot_edd_info)+EDDEXTSIZE, %si
+        movw    $bootlsym(boot_edd_info)+EDDEXTSIZE, %si
         movb    $0x80, %dl                      # BIOS device 0x80
 
 edd_check_ext:
@@ -56,7 +56,7 @@ edd_check_ext:
         movb    %dl, %ds:-8(%si)                # store device number
         movb    %ah, %ds:-7(%si)                # store version
         movw    %cx, %ds:-6(%si)                # store extensions
-        incb    bootsym(boot_edd_info_nr)       # note that we stored something
+        incb    bootlsym(boot_edd_info_nr)      # note that we stored something
 
 edd_get_device_params:
         movw    $EDDPARMSIZE, %ds:(%si)         # put size
@@ -97,7 +97,7 @@ edd_legacy_done:
 edd_next:
         incb    %dl                             # increment to next device
         jz      .Ledd_mbr_sig
-        cmpb    $EDD_INFO_MAX,bootsym(boot_edd_info_nr)
+        cmpb    $EDD_INFO_MAX, bootlsym(boot_edd_info_nr)
         jb      edd_check_ext
 
 .Ledd_mbr_sig:
@@ -108,11 +108,11 @@ edd_next:
 .Ledd_mbr_sig_start:
         pushw   %es
         movb    $0x80, %dl                      # from device 80
-        movw    $bootsym(boot_mbr_signature), %bx # store buffer ptr in bx
+        movw    $bootlsym(boot_mbr_signature), %bx # store buffer ptr in bx
 .Ledd_mbr_sig_read:
         pushw   %bx
-        movw    $bootsym(boot_edd_info), %bx
-        movzbw  bootsym(boot_edd_info_nr), %cx
+        movw    $bootlsym(boot_edd_info), %bx
+        movzbw  bootlsym(boot_edd_info_nr), %cx
         jcxz    .Ledd_mbr_sig_default
 .Ledd_mbr_sig_find_info:
         cmpb    %dl, (%bx)
@@ -151,12 +151,12 @@ edd_next:
         jne     .Ledd_mbr_sig_next
         movb    %dl, (%bx)                      # store BIOS drive number
         movl    %ecx, 4(%bx)                    # store signature from MBR
-        incb    bootsym(boot_mbr_signature_nr)  # note that we stored something
+        incb    bootlsym(boot_mbr_signature_nr) # note that we stored something
         addw    $8, %bx                         # increment sig buffer ptr
 .Ledd_mbr_sig_next:
         incb    %dl                             # increment to next device
         jz      .Ledd_mbr_sig_done
-        cmpb    $EDD_MBR_SIG_MAX, bootsym(boot_mbr_signature_nr)
+        cmpb    $EDD_MBR_SIG_MAX, bootlsym(boot_mbr_signature_nr)
         jb      .Ledd_mbr_sig_read
 .Ledd_mbr_sig_done:
         popw    %es
@@ -164,12 +164,12 @@ edd_next:
         ret
 
         .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
-GLOBAL(boot_edd_info_nr)
+GLOBAL_COPY(boot_edd_info_nr)
         .byte   0
-GLOBAL(boot_mbr_signature_nr)
+GLOBAL_COPY(boot_mbr_signature_nr)
         .byte   0
-GLOBAL(boot_mbr_signature)
+GLOBAL_COPY(boot_mbr_signature)
         .fill   EDD_MBR_SIG_MAX*8,1,0
-GLOBAL(boot_edd_info)
+GLOBAL_COPY(boot_edd_info)
         .fill   EDD_INFO_MAX * (EDDEXTSIZE + EDDPARMSIZE), 1, 0
         .popsection
diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S
index 0e055ff070..853bf27a59 100644
--- a/xen/arch/x86/boot/head.S
+++ b/xen/arch/x86/boot/head.S
@@ -398,6 +398,9 @@ __start:
         shr     $2, %ecx
         rep stosl
 
+        /* Fill the writable copy of the trampoline data. */
+        call    init_trampoline_data32
+
         mov     %ebp, %eax
 
 #ifdef CONFIG_PVH_GUEST
@@ -708,21 +711,12 @@ trampoline_setup:
         lret
 
         /*
-         * In an ideal world, the trampoline would live in .init.text, so that
-         * it can be disassembled during development.
-         *
-         * However, xen.efi writes into the trampoline while it's part of the
-         * main Xen image.  Xen.efi needs to be StrictNX compatible
-         * (i.e. writable sections must not be executable), so must have the
-         * trampoline in a data section.
-         *
-         * Put the trampoline in a dedicated section so the final link can
-         * place it appropriately.
-         *
-         * TODO: Rework xen.efi to not write into the trampoline while it is
-         * part of the main Xen image.
+         * 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, "a", @progbits
+        .section .init.trampoline, "ax", @progbits
 ENTRY(trampoline_start)
 #include "trampoline.S"
         .subsection TRAMP_LAST
diff --git a/xen/arch/x86/boot/mem.S b/xen/arch/x86/boot/mem.S
index 96b03a00c8..cb304155cc 100644
--- a/xen/arch/x86/boot/mem.S
+++ b/xen/arch/x86/boot/mem.S
@@ -5,7 +5,7 @@
 
 get_memory_map:
         xorl    %ebx, %ebx                      # continuation counter
-        movw    $bootsym(bios_e820map), %di     # point into the whitelist
+        movw    $bootlsym(bios_e820map), %di    # point into the whitelist
                                                 # so we can have the bios
                                                 # directly write into it.
 
@@ -20,8 +20,8 @@ get_memory_map:
         cmpl    $SMAP,%eax                      # check the return is `SMAP'
         jne     .Ldone
 
-        incw    bootsym(bios_e820nr)
-        cmpw    $E820_BIOS_MAX, bootsym(bios_e820nr) # up to this many entries
+        incw    bootlsym(bios_e820nr)
+        cmpw    $E820_BIOS_MAX, bootlsym(bios_e820nr) # up to this many entries
         jae     .Ldone
 
         addw    $20,%di
@@ -33,8 +33,8 @@ get_memory_map:
 
         .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
         .align  4
-GLOBAL(bios_e820map)
+GLOBAL_COPY(bios_e820map)
         .fill   E820_BIOS_MAX*20,1,0
-GLOBAL(bios_e820nr)
+GLOBAL_COPY(bios_e820nr)
         .long   0
         .popsection
diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S
index 2898e5da77..df193fb67f 100644
--- a/xen/arch/x86/boot/trampoline.S
+++ b/xen/arch/x86/boot/trampoline.S
@@ -17,8 +17,24 @@
  */
 
 /* 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. */
 #undef bootsym
-#define bootsym(s) ((s)-trampoline_start)
+#define bootsym(s) (((s)-trampoline_start) * 1)
+
+/* Offset of data defined with GLOBAL_COPY(). */
+#define bootlsym(sym) bootsym(l_ ## sym)
+
+/*
+ * Define sym in the writable copy of the data (see asm/trampoline.h), and
+ * l_sym at the same offset in the trampoline.  Trampoline code must use the
+ * latter, via bootlsym().
+ */
+#define GLOBAL_COPY(sym) \
+        l_ ## sym: ; \
+        .pushsection .init.bss.trampoline, "aw", @nobits; \
+        .org trampoline_data_start + (l_ ## sym - l_trampoline_data_start); \
+        GLOBAL(sym); \
+        .popsection
 
 #define bootsym_rel(sym, off, opnd...)     \
         bootsym(sym),##opnd;               \
@@ -48,6 +64,16 @@
 #define TRAMP_BOOT_DATA      5
 #define TRAMP_LAST           TRAMP_BOOT_DATA
 
+        /* Start of the data with a writable copy, see GLOBAL_COPY(). */
+        .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
+        .balign 8
+GLOBAL(l_trampoline_data_start)
+        .popsection
+        .pushsection .init.bss.trampoline, "aw", @nobits
+        .balign 8
+GLOBAL(trampoline_data_start)
+        .popsection
+
 /* Start of the permanent trampoline code. */
 
         .code16
@@ -61,7 +87,7 @@
 GLOBAL(entry_SIPI16)
         mov     %cs,%ax
         mov     %ax,%ds
-        movb    $0xA5,bootsym(trampoline_cpu_started)
+        movb    $0xA5, bootlsym(trampoline_cpu_started)
         cld
         cli
         lidt    bootsym(idt_48)
@@ -98,12 +124,12 @@ common_64:
 
         /* Load pagetable base register. */
         mov     $abs_idle_pg_table, %eax
-        add     bootsym(trampoline_xen_phys_start), %eax
+        add     bootlsym(trampoline_xen_phys_start), %eax
         mov     %eax,%cr3
 
         /* Adjust IA32_MISC_ENABLE if needed (for NX enabling below). */
-        mov     bootsym(trampoline_misc_enable_off), %esi
-        mov     bootsym(trampoline_misc_enable_off+4), %edi
+        mov     bootlsym(trampoline_misc_enable_off), %esi
+        mov     bootlsym(trampoline_misc_enable_off+4), %edi
         mov     %esi,%eax
         or      %edi,%eax
         jz      1f
@@ -124,7 +150,7 @@ common_64:
         /* Set up EFER (Extended Feature Enable Register). */
         movl    $MSR_EFER,%ecx
         rdmsr
-        or      bootsym(trampoline_efer), %eax
+        or      bootlsym(trampoline_efer), %eax
         wrmsr
 
         /* Enable paging */
@@ -193,18 +219,18 @@ gdt_48:
 
         .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
         .balign 8
-GLOBAL(trampoline_misc_enable_off)
+GLOBAL_COPY(trampoline_misc_enable_off)
         .quad   0
 
 /* EFER OR-mask for boot paths.  SCE conditional on PV support, NX added when available. */
-GLOBAL(trampoline_efer)
+GLOBAL_COPY(trampoline_efer)
         .long   EFER_LME | (EFER_SCE * IS_ENABLED(CONFIG_PV)) | \
                 (EFER_NXE * IS_ENABLED(CONFIG_REQUIRE_NX))
 
-GLOBAL(trampoline_xen_phys_start)
+GLOBAL_COPY(trampoline_xen_phys_start)
         .long   0
 
-GLOBAL(trampoline_cpu_started)
+GLOBAL_COPY(trampoline_cpu_started)
         .byte   0
         .popsection
 
@@ -229,7 +255,7 @@ trampoline_boot_cpu_entry:
         mov     $BOOT_DS_TRAMP, %eax
         mov     %eax, %ds
 
-        cmpb    $0, bootsym(skip_realmode)
+        cmpb    $0, bootlsym(skip_realmode)
         jnz     .Lskip_realmode
 
         /* Load pseudo-real-mode segments. */
@@ -292,7 +318,7 @@ trampoline_boot_cpu_entry:
 
         mov     $0x0200,%ax
         int     $0x16
-        mov     %al,bootsym(kbd_shift_flags)
+        mov     %al, bootlsym(kbd_shift_flags)
 
         /* Disable irqs before returning to protected mode. */
         cli
@@ -325,15 +351,15 @@ trampoline_boot_cpu_entry:
         .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
         .align  2
 /* Keep in sync with cmdline.c:early_boot_opts_t type! */
-early_boot_opts:
-skip_realmode:
+GLOBAL_COPY(early_boot_opts)
+GLOBAL_COPY(skip_realmode)
         .byte   0
 opt_edd:
         .byte   0                               /* edd=on/off/skipmbr */
 opt_edid:
         .byte   0                               /* EDID parsing option (force/no/default). */
 
-GLOBAL(kbd_shift_flags)
+GLOBAL_COPY(kbd_shift_flags)
         .byte   0
 
 #ifdef CONFIG_VIDEO
@@ -353,3 +379,13 @@ rm_idt: .word   256*4-1, 0, 0
 #ifdef CONFIG_VIDEO
 #include "video.S"
 #endif
+
+        /* End of the data with a writable copy. */
+        .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
+        .balign 4
+GLOBAL(l_trampoline_data_end)
+        .popsection
+        .pushsection .init.bss.trampoline, "aw", @nobits
+        .org trampoline_data_start + (l_trampoline_data_end - l_trampoline_data_start)
+GLOBAL(trampoline_data_end)
+        .popsection
diff --git a/xen/arch/x86/boot/video.S b/xen/arch/x86/boot/video.S
index 57c8e790e8..0e34a515a4 100644
--- a/xen/arch/x86/boot/video.S
+++ b/xen/arch/x86/boot/video.S
@@ -26,7 +26,7 @@
 /* Force 400 scan lines for standard modes (hack to fix bad BIOS behaviour */
 #undef CONFIG_VIDEO_400_HACK
 
-#define _param(param) bootsym(boot_vid_info)+(param)
+#define _param(param) bootlsym(boot_vid_info)+(param)
 
 video:  xorw    %ax, %ax
         movw    %ax, %gs        # GS is zero
@@ -893,7 +893,7 @@ store_edid:
         pushw   %di
 
         movb    bootsym(opt_edid), %al
-        cmpw    $0x1313, bootsym(boot_edid_caps) # Data already retrieved?
+        cmpw    $0x1313, bootlsym(boot_edid_caps) # Data already retrieved?
         je      .Lcheck_edid
         cmpb    $2, %al                 # EDID forced on cmdline (edid=force)?
         jne     .Lno_edid
@@ -922,7 +922,7 @@ store_edid:
         cmpw    $0x004f, %ax            # Call failed?
         jne     .Lno_edid
 
-        movw    %bx, bootsym(boot_edid_caps)
+        movw    %bx, bootlsym(boot_edid_caps)
 
         cmpb    $2, bootsym(opt_edid)   # EDID forced on cmdline (edid=force)?
         je      .Lforce_edid
@@ -938,7 +938,7 @@ store_edid:
         movw    $0x01, %bx
         xorw    %cx, %cx
         xorw    %dx, %dx
-        movw    $bootsym(boot_edid_info), %di
+        movw    $bootlsym(boot_edid_info), %di
         int     $0x10
 
 .Lno_edid:
@@ -1000,15 +1000,15 @@ force_size:     .word   0       # Use this size instead of the one in BIOS vars
         .popsection
 
         .pushsection .init.trampoline, TRAMP_BOOT_EXT_DATA
-GLOBAL(boot_vid_info)
+GLOBAL_COPY(boot_vid_info)
         .byte   0, 0    /* orig_x, orig_y */
         .byte   3       /* text mode 3    */
         .byte   80, 25  /* 80x25          */
         .byte   1       /* isVGA          */
         .word   16      /* 8x16 font      */
-        .space  BVI_size - (. - boot_vid_info)
-GLOBAL(boot_edid_info)
+        .space  BVI_size - (. - l_boot_vid_info)
+GLOBAL_COPY(boot_edid_info)
         .fill   128,1,0x13
-GLOBAL(boot_edid_caps)
+GLOBAL_COPY(boot_edid_caps)
         .word   0x1313
         .popsection
diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S
index f602541972..5841aeea29 100644
--- a/xen/arch/x86/boot/wakeup.S
+++ b/xen/arch/x86/boot/wakeup.S
@@ -41,7 +41,7 @@ ENTRY(entry_S3)
 1:
 
         # for acpi_sleep=s3_bios
-        testb   $1, bootsym(video_flags)
+        testb   $1, bootlsym(video_flags)
         jz      1f
         mov     $0xc000, %ax
         mov     %ax, %fs
@@ -56,7 +56,7 @@ ENTRY(entry_S3)
         popfl
 
 1:      # for acpi_sleep=s3_mode
-        testb   $2, bootsym(video_flags)
+        testb   $2, bootlsym(video_flags)
         jz      1f
         movw    bootsym(video_mode), %ax
         call    mode_setw
@@ -116,7 +116,7 @@ wakeup_32:
 
         # check saved magic again
         mov     $abs_saved_magic, %eax
-        add     bootsym(trampoline_xen_phys_start), %eax
+        add     bootlsym(trampoline_xen_phys_start), %eax
         mov     %ss:(%eax), %eax
         cmp     $0x9abcdef0, %eax
         jne     bogus_saved_magic
@@ -138,6 +138,6 @@ video_mode:     .word 0
         .popsection
 
         .pushsection .init.trampoline, TRAMP_PERM_EXT_DATA
-GLOBAL(video_flags)
+GLOBAL_COPY(video_flags)
         .byte 0
         .popsection
diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h
index 0f4598a0ca..2e0d88ffc3 100644
--- a/xen/arch/x86/efi/efi-boot.h
+++ b/xen/arch/x86/efi/efi-boot.h
@@ -806,6 +806,12 @@ static void noreturn __init efi_arch_halt(void)
         halt();
 }
 
+static void __init efi_arch_early_init(void)
+{
+    /* Before anything writes to the trampoline data. */
+    init_trampoline_data64();
+}
+
 static void __init efi_arch_load_addr_check(const EFI_LOADED_IMAGE *loaded_image)
 {
     xen_phys_start = (UINTN)loaded_image->ImageBase;
diff --git a/xen/arch/x86/efi/mbi2.c b/xen/arch/x86/efi/mbi2.c
index 996eafa03d..3240a6c69a 100644
--- a/xen/arch/x86/efi/mbi2.c
+++ b/xen/arch/x86/efi/mbi2.c
@@ -4,6 +4,7 @@
 #include <xen/init.h>
 #include <xen/multiboot2.h>
 #include <asm/asm_defns.h>
+#include <asm/boot-helpers.h>
 #include <asm/efi.h>
 
 const char * asmlinkage __init
@@ -16,6 +17,9 @@ efi_multiboot2_prelude(uint32_t magic, const multiboot2_fixed_t *mbi)
     unsigned long mbi_raw = (unsigned long)mbi;
     bool have_bs = false;
 
+    /* Before anything writes to the trampoline data. */
+    init_trampoline_data64();
+
     if ( magic != MULTIBOOT2_BOOTLOADER_MAGIC )
         return "ERR: Not a Multiboot2 bootloader!";
 
diff --git a/xen/arch/x86/include/asm/boot-helpers.h b/xen/arch/x86/include/asm/boot-helpers.h
index 8d9774860a..ee158299b4 100644
--- a/xen/arch/x86/include/asm/boot-helpers.h
+++ b/xen/arch/x86/include/asm/boot-helpers.h
@@ -8,6 +8,7 @@
 #ifndef X86_BOOT_HELPERS_H
 #define X86_BOOT_HELPERS_H
 
+void init_trampoline_data64(void);
 void copy_trampoline64(void);
 
 #endif /* X86_BOOT_HELPERS_H */
diff --git a/xen/arch/x86/include/asm/trampoline.h b/xen/arch/x86/include/asm/trampoline.h
index deed2679d9..37a91c28ca 100644
--- a/xen/arch/x86/include/asm/trampoline.h
+++ b/xen/arch/x86/include/asm/trampoline.h
@@ -90,6 +90,18 @@
  *
  * Note: trampoline_start[] and trampoline_end[] represent the shown
  * boundaries, but are addresses as linked into Xen's .init section.
+ *
+ * The trampoline is never written to while part of the Xen image.  The data
+ * in it also used from outside (e.g. command line options, EFER mask, data
+ * collected from EFI or the BIOS) is contiguous, between
+ * l_trampoline_data_start[] and l_trampoline_data_end[], and has a writable
+ * copy in .init.data, between trampoline_data_start[] and
+ * trampoline_data_end[].  The copy is filled very early during boot, all
+ * modifications before the trampoline is placed in low memory are done to it,
+ * and it is placed over the original data when copying the trampoline.
+ *
+ * Such symbols are defined in both, using GLOBAL_COPY(): "sym" is in the copy
+ * while "l_sym" is in the trampoline.
  */
 
 #define TRAMPOLINE_SIZE         KB(64)
@@ -108,6 +120,20 @@
 /* SAF-0-safe */
 extern char trampoline_start[], trampoline_end[];
 
+/* The data used from outside the trampoline, and its writable copy. */
+/* SAF-0-safe */
+extern char l_trampoline_data_start[], l_trampoline_data_end[];
+/* SAF-0-safe */
+extern char trampoline_data_start[], trampoline_data_end[];
+
+/*
+ * Where trampoline_start[] would be if the writable copy of the data was part
+ * of a whole trampoline.  Defined by the linker script, it allows locating
+ * data symbols in the trampoline using a single difference.
+ */
+/* SAF-0-safe */
+extern char trampoline_data_base[];
+
 /*
  * The physical address of trampoline_start[] in low memory.  It must be below
  * the 1M boundary (as the trampoline contains 16-bit code), and must be 4k
@@ -118,16 +144,22 @@ extern uint32_t trampoline_phys;
 /*
  * Calculate the physical address of a symbol in the trampoline.
  *
- * Should only be used on symbols declared later in this header.  Specifying
- * other symbols will compile but malfunction when used, as will using this
- * helper before the trampoline is placed.
+ * bootsym_phys() is for code symbols (entry points, trampoline_start[] and
+ * trampoline_end[]), bootsym_data_phys() for data with a writable copy
+ * (defined with GLOBAL_COPY()).  Specifying other symbols will compile but
+ * malfunction when used, as will using these helpers before the trampoline
+ * is placed.
  */
 #define bootsym_phys(sym)                                       \
     (trampoline_phys + ((unsigned long)&(sym) -                 \
                         (unsigned long)trampoline_start))
 
-/* Given a trampoline symbol, construct a pointer to it in the directmap. */
-#define bootsym(sym) (*((typeof(sym) *)__va(bootsym_phys(sym))))
+#define bootsym_data_phys(sym)                                  \
+    (trampoline_phys + ((unsigned long)&(sym) -                 \
+                        (unsigned long)trampoline_data_base))
+
+/* Given a trampoline data symbol, construct a pointer to it in the directmap. */
+#define bootsym(sym) (*((typeof(sym) *)__va(bootsym_data_phys(sym))))
 
 /* The INIT-SIPI-SIPI entrypoint.  16-bit code. */
 void nocall entry_SIPI16(void);
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index 6b80de3955..96b469b2bd 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -196,9 +196,7 @@ SECTIONS
        _sinittext = .;
        *(.init.multiboot)
        *(.init.text)
-#ifndef EFI /* Non-EFI wants the trampoline in .init.text for disassembly. */
        *(.init.trampoline)
-#endif
        *(.text.startup)
        _einittext = .;
        /*
@@ -238,11 +236,13 @@ SECTIONS
 
        *(.init.data.multiboot)
        *(.init.data)
-#ifdef EFI /* EFI needs the trampoline in .init.data for StrictNX compatibility, for now. */
-       *(.init.trampoline)
-#endif
        *(.init.data.rel)
        *(.init.data.rel.*)
+       . = ALIGN(8);
+       *(.init.bss.trampoline)
+       /* See trampoline.h. */
+       trampoline_data_base = trampoline_data_start -
+                              (l_trampoline_data_start - trampoline_start);
        . = ALIGN(2);
        __trampoline_rel_start = .;
        *(.trampoline_rel)
@@ -425,6 +425,9 @@ ASSERT(IS_ALIGNED(__init_end,   PAGE_SIZE), "__init_end misaligned")
 
 ASSERT(IS_ALIGNED(trampoline_start, 4), "trampoline_start misaligned")
 ASSERT(IS_ALIGNED(trampoline_end,   4), "trampoline_end misaligned")
+ASSERT(IS_ALIGNED(l_trampoline_data_start, 4), "l_trampoline_data_start misaligned")
+ASSERT(IS_ALIGNED(trampoline_data_start, 4), "trampoline_data_start misaligned")
+ASSERT(IS_ALIGNED(trampoline_data_end, 4), "trampoline_data_end misaligned")
 ASSERT(IS_ALIGNED(__bss_start,      8), "__bss_start misaligned")
 ASSERT(IS_ALIGNED(__bss_end,        8), "__bss_end misaligned")
 
diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c
index 345dcc0bd4..f54c757f1a 100644
--- a/xen/common/efi/boot.c
+++ b/xen/common/efi/boot.c
@@ -1458,6 +1458,7 @@ void EFIAPI __init noreturn efi_start(EFI_HANDLE ImageHandle,
     bool kernel_verified = false;
     int dt_modules_found;
 
+    efi_arch_early_init();
     __set_bit(EFI_BOOT, &efi_flags);
     __set_bit(EFI_LOADER, &efi_flags);
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (8 preceding siblings ...)
  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
  2026-12-04 20:50 ` [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers Frediano Ziglio
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Anthony PERARD, Michal Orzel, Julien Grall,
	Stefano Stabellini, Claude Opus 5 . 5

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



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers
  2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
                   ` (9 preceding siblings ...)
  2026-12-04 20:50 ` [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S Frediano Ziglio
@ 2026-12-04 20:50 ` Frediano Ziglio
  10 siblings, 0 replies; 12+ messages in thread
From: Frediano Ziglio @ 2026-12-04 20:50 UTC (permalink / raw)
  To: xen-devel
  Cc: Frediano Ziglio, Jan Beulich, Andrew Cooper, Roger Pau Monné,
	Teddy Astie, Lukasz Hawrylko, Daniel P. Smith, Mateusz Mówka,
	Claude Opus 5 . 5

bootsym_phys() must be used for code in the trampoline, while
bootsym_data_phys(), and so bootsym(), must be used for data with a
writable copy.  Using the wrong one compiles, but gives a wrong address.

Add is_function(), comparing the type of the address of a symbol with
the type of the symbol after decay, which only match for functions, and
check the symbols at build time.

bootsym_phys() was also used for trampoline_start and trampoline_end,
which are arrays; use trampoline_phys directly for them.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 xen/arch/x86/include/asm/trampoline.h | 17 ++++++++++++-----
 xen/arch/x86/setup.c                  |  6 ++++--
 xen/arch/x86/tboot.c                  |  2 +-
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/xen/arch/x86/include/asm/trampoline.h b/xen/arch/x86/include/asm/trampoline.h
index 37a91c28ca..d15d3c61a1 100644
--- a/xen/arch/x86/include/asm/trampoline.h
+++ b/xen/arch/x86/include/asm/trampoline.h
@@ -111,6 +111,7 @@
 #ifndef __ASSEMBLER__
 
 #include <xen/compiler.h>
+#include <xen/macros.h>
 #include <xen/types.h>
 
 /*
@@ -141,21 +142,27 @@ extern char trampoline_data_base[];
  */
 extern uint32_t trampoline_phys;
 
+/* Whether x is a function, as opposed to an object. */
+#define is_function(x) \
+    __builtin_types_compatible_p(typeof(&(x)), typeof((void)0, (x)))
+
 /*
  * Calculate the physical address of a symbol in the trampoline.
  *
- * bootsym_phys() is for code symbols (entry points, trampoline_start[] and
- * trampoline_end[]), bootsym_data_phys() for data with a writable copy
- * (defined with GLOBAL_COPY()).  Specifying other symbols will compile but
+ * bootsym_phys() is for functions (the entry points), bootsym_data_phys() for
+ * data with a writable copy (defined with GLOBAL_COPY()), which is checked at
+ * build time.  Specifying other symbols of the right kind will compile but
  * malfunction when used, as will using these helpers before the trampoline
  * is placed.
  */
 #define bootsym_phys(sym)                                       \
-    (trampoline_phys + ((unsigned long)&(sym) -                 \
+    (BUILD_BUG_ON_ZERO(!is_function(sym)) +                     \
+     trampoline_phys + ((unsigned long)&(sym) -                 \
                         (unsigned long)trampoline_start))
 
 #define bootsym_data_phys(sym)                                  \
-    (trampoline_phys + ((unsigned long)&(sym) -                 \
+    (BUILD_BUG_ON_ZERO(is_function(sym)) +                      \
+     trampoline_phys + ((unsigned long)&(sym) -                 \
                         (unsigned long)trampoline_data_base))
 
 /* Given a trampoline data symbol, construct a pointer to it in the directmap. */
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index 17f4f9de4f..33f975c328 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -2266,11 +2266,13 @@ __initcall(init_xen_cap_info);
 
 int __hwdom_init remove_xen_ranges(struct rangeset *r)
 {
+    unsigned long tramp_end =
+        trampoline_phys + (trampoline_end - trampoline_start);
     int rc;
 
     /* S3 resume code (and other real mode trampoline code) */
-    rc = rangeset_remove_range(r, PFN_DOWN(bootsym_phys(trampoline_start)),
-                               PFN_DOWN(bootsym_phys(trampoline_end) - 1));
+    rc = rangeset_remove_range(r, PFN_DOWN(trampoline_phys),
+                               PFN_DOWN(tramp_end - 1));
     if ( rc )
         return rc;
 
diff --git a/xen/arch/x86/tboot.c b/xen/arch/x86/tboot.c
index 180da67d6c..576131603d 100644
--- a/xen/arch/x86/tboot.c
+++ b/xen/arch/x86/tboot.c
@@ -328,7 +328,7 @@ void tboot_shutdown(uint32_t shutdown_type)
          */
         g_tboot_shared->num_mac_regions = 3;
         /* S3 resume code (and other real mode trampoline code) */
-        g_tboot_shared->mac_regions[0].start = bootsym_phys(trampoline_start);
+        g_tboot_shared->mac_regions[0].start = trampoline_phys;
         g_tboot_shared->mac_regions[0].size = trampoline_end - trampoline_start;
         /* hypervisor .text + .rodata */
         g_tboot_shared->mac_regions[1].start = (uint64_t)__pa(&_stext);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-12-04 20:51 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S Frediano Ziglio
2026-12-04 20:50 ` [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers Frediano Ziglio

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.