[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy



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@xxxxxxxxx>
---
 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




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.