All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frediano Ziglio <freddy77@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: "Frediano Ziglio" <freddy77@gmail.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Roger Pau Monné" <roger@xenproject.org>,
	"Teddy Astie" <teddy.astie@vates.tech>
Subject: [PATCH 04/11] x86/boot: Reduce relocations in the trampoline
Date: Fri,  4 Dec 2026 20:50:37 +0000	[thread overview]
Message-ID: <20261204205045.596093-5-freddy77@gmail.com> (raw)
In-Reply-To: <20261204205045.596093-1-freddy77@gmail.com>

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



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

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour Frediano Ziglio
2026-12-04 20:50 ` [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations Frediano Ziglio
2026-12-04 20:50 ` Frediano Ziglio [this message]
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

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20261204205045.596093-5-freddy77@gmail.com \
    --to=freddy77@gmail.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=roger@xenproject.org \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

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

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