From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57F6A606A32D for ; Fri, 4 Dec 2026 20:51:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Yy5ocVp4 Received: from list by lists.xenproject.org with outflank-mailman.1514952.1705685 (Exim 4.92) (envelope-from ) id 1xZaFB-00050c-T4; Fri, 04 Dec 2026 20:51:01 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1514952.1705685; Fri, 04 Dec 2026 20:51:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZaFB-0004z1-Jh; Fri, 04 Dec 2026 20:51:01 +0000 Received: by outflank-mailman (input) for mailman id 1514952; Fri, 04 Dec 2026 20:51:00 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZaFA-0004Qz-7p for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 20:51:00 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xZaF9-00GVsz-Kj for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 21:50:59 +0100 Received: from [10.42.69.11] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b13278a-bab6-0a2a0a5309dd-0a2a450bc204-40 for ; Fri, 04 Dec 2026 21:50:59 +0100 Received: from [209.85.128.51] (helo=mail-wm1-f51.google.com) by tlsNG-42698a.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b1327b3-b7e8-0a2a450b0019-d1558033f134-3 for ; Fri, 04 Dec 2026 21:50:59 +0100 Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4bb9d8e8e90so12536645e9.3 for ; Fri, 04 Dec 2026 12:50:59 -0800 (PST) Received: from localhost.localdomain (89.246.200.146.dyn.plus.net. [146.200.246.89]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d17426821asm6895228f8f.45.2026.12.04.12.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:50:58 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20260707 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796417459; x=1797022259; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qNACBJCaJ1FbrUA3ZbAm8dSfsFPeaqwDoQNUCCPatoY=; b=Yy5ocVp4Pg+VI8aWEDl+oN8KWztNKw7Uoz0U3IUjFPjtwjSdb9b5RmFmx5f7jUybNt AJusBst1PerUipI0tiP1i9HkURgxLPnT7fO5oSxWP1JN2qrlZR85o5OrSOPa/EBblBQV 6NuAMfIqdmj1L0A+WXhsH4swnrp4ZXgqit+KDOdnrUi8JKOBqDdBlbV/ekblDJq3QY0h QRJOLv4A94zR4e2Nvvk0qXoOQ1zTl/tlrgZRUJpI/7rUAzRyLNhZPSR26hCCMZp7yo9o XDhCD0BVhBO7q2CgCPpYCKevo86BKdqWSF9/efHIuffYoqIBhTLS0wZq8k6k7vO8NtE0 VUaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796417459; x=1797022259; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=qNACBJCaJ1FbrUA3ZbAm8dSfsFPeaqwDoQNUCCPatoY=; b=bt6LAx0EvJwn0bv+J0xXlFSRhSzgsDCNUM5CzNzgivSonRHLHSz4gGOPHL3gYbRvWZ TWjpv+lQiImjm+XxYksHhqArA+eq4dsed/cNFvCGspAfy+Zm8Iqqk4FIKDC1dsflOonT Nr1Cly7R9cg0OPmkExfjkbrCXEST7+o8zvDHGSpAy2R5V89jrzspHThpnr3z5wMdzCI+ IGQqOGz9khXkFkNcQQolYG/rgFKhQR9OdQGktVMJR9+1fJL4G4MpiRTRRxuIaGalh+rC wO27pC+chb3Jt/q0OzqmVQdgGf9ptF9FYT+P5AJ4zdXEEdR+SkexIOzAg+UZUzYjSWIu KfWg== X-Gm-Message-State: APuU4Z+4jhj8rjxoo08vFytZJNwHv8qNdtfywhJtLzSubopmC/tmMJ7J w3ALi2ySxn9ZhyfIjrqS2ROSdk51ECLt/qEjaDogMWAL3JpbubJd5HXYEXXx0Mxj X-Gm-Gg: AZFdTZemvyc6zb+nwRdCb0HDW0Ve0vhWYU7CUDRnjjmsgoox3xUHCN5sklzFpONkhQt z5CDpqCkGOJF7YfZUftadSh/c7eYEVvnszMzAdA6DxR6Wly0jhPqptdOYOaxd46Q8XNbt8scH2f sNqXRdqoYSqZxhxgsz3O7+gxAX886kxDIgETKIleUa4Y0VSj1bBP0O4Vgx/1M56rrla8ZqNMhB1 3K1BZvUTqyaOucXpJQKG2WaoPBDXPtfoZPIi0AliqKmtvCRXu+tUVrceKkDYDDOuPAXbTLVbo8G 1hPGCQy+AIJzPf6IpWXIElxoGpySegGo4jrsGAiozsR86p7DGxYqAUhm1ka2myBzlrqFaIPwa+G Fm7XQx7uwEV1Jqxbttwr3llHbPLPIHijYC58pT8MjHapwtpaG09PHj1yDvXTo9eubvMDP0DczFX BgGHYSshCatjLOsgXQzUkkQYVuDwq8NWDQ/TQinJwE+mfWQ7aTgfVKP03ClUxwABncI1QUUXSzJ fq0DFAteBFqIxAnJd0PqGkYYtoeJG0bv1kwROIISyeDCdd6DCTpAru5nN1TM9VkBPe0lxMZF59c 1KdOSxkvIquFaIURGQ4yYm4F6RI/JmkeUXChKGATCP+HUbzjSbwq9kfcX/s7Vp2H0iTSJk/xMlr P9D5KfyuMajQar/MOuNE0xUs= X-Received: by 2002:a05:600c:3b04:b0:4bb:6118:4c9a with SMTP id 5b1f17b1804b1-4bbeb8cf058mr58979475e9.30.1796417458944; Fri, 04 Dec 2026 12:50:58 -0800 (PST) Sender: Frediano Ziglio From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , "Daniel P. Smith" , =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Subject: [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy Date: Fri, 4 Dec 2026 20:50:40 +0000 Message-ID: <20261204205045.596093-8-freddy77@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204205045.596093-1-freddy77@gmail.com> References: <20261204205045.596093-1-freddy77@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-purgate-ID: tlsNG-42698a/1796417459-198C19EA-3C0EF9EC/0/0 X-purgate-type: clean X-purgate-size: 10934 X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 --- 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 +#include + +#include +#include + +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 -#include - -#include -#include - -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