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 5C189606A32C for ; Fri, 4 Dec 2026 20:51:20 +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=bPJoTTc9 Received: from list by lists.xenproject.org with outflank-mailman.1514954.1705706 (Exim 4.92) (envelope-from ) id 1xZaFE-0005ci-6y; Fri, 04 Dec 2026 20:51:04 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1514954.1705706; Fri, 04 Dec 2026 20:51:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZaFE-0005by-1L; Fri, 04 Dec 2026 20:51:04 +0000 Received: by outflank-mailman (input) for mailman id 1514954; Fri, 04 Dec 2026 20:51:02 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZaFB-00050w-Vv for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 20:51:02 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xZaFB-0097Kn-CD for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 21:51:01 +0100 Received: from [10.42.69.6] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b1327a9-2eae-0a2a0a5409dd-0a2a4506d68e-12 for ; Fri, 04 Dec 2026 21:51:01 +0100 Received: from [209.85.221.44] (helo=mail-wr1-f44.google.com) by tlsNG-16d1c6.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b1327b5-195a-0a2a45060019-d155dd2ca5c9-3 for ; Fri, 04 Dec 2026 21:51:01 +0100 Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-490d59c3edaso909812f8f.1 for ; Fri, 04 Dec 2026 12:51:01 -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.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:51:00 -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=1796417461; x=1797022261; 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=RBk0BYmPR1q/xseS20QHK2y8R22x7rdfunE2DMQSuCc=; b=bPJoTTc94O9PIhnMvGYmxjLU+Y9ZHW0wqr33I/y2K0k5CuQNDzG1KU2/Aj/faCXmZd vEYR0c7/tKIgOHJ1/iYbUOr9rItR+/hCs9qy5U3i/A+VZ/CSmmui3aCEwmi734RtOiji Jpmo1+HRbWs3sFCC6XxxHUSgTWqO5MkJGv/H85DflV7mwK86c6he2QdREbw3qIjjSo1v lhHR4dw0ASgRyPJgmD/S3SbiZyeIC1qX6K8zfEAoElnjXalFWb3hsLeImXjRByfspf1M S9++Vfz/x3+axptEabhbD9n4tvQm50o7zpRbY45CfKJ37jxldUKH43WQHhodXZgjZG78 TVrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796417461; x=1797022261; 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=RBk0BYmPR1q/xseS20QHK2y8R22x7rdfunE2DMQSuCc=; b=sbLcyMKr/S3DPIMmY+H/z37AWcdTSK9WTPJDlV/Em08i9KZpamFyq8cpwt5DGMaZGv lLHStK2NxjStnKjUZR3s9EHWzhi83sYtBN7G+GcXjS0hkCLq9aWqKo1ch2IT0qbzQ35Z Qg3Zt3Wmxcfbe4Q6S1uBaNNUulDBuNNT87o4YkOpXCFQ/rBdnBGdro2mPNKrsKep078F AdRczIi10tpA7nNOz34YnBL3lJKkTn3pOEJ5D96PC7NPNXjCwJbjmrgvq++3egcvjCmE qfw/GtVnYg1eR3qP5sMnKJS2eXs+IT3D2SPZ3yRs0wqbFo+Suq1XK3iPwCDe0tKlm18i vM4w== X-Gm-Message-State: AB4q+uGaq0wrDBbmKsIK4dWy31b1/HXZehsJJdNmCTtkzHaFt+2OM4nc gQeALEb80hUP3u5vN8wWfnDpRN6efbzC1+/bnIO/usJ/q/YjDBDMWJZQBO9bLGnq X-Gm-Gg: AZFdTZepTQQbLf0GACVpEoeMmVlGB2eD34G5vThqku5Ry6EBWPYdfGfmt4LI7+/KkC1 LSBsMctwgCh8RTnfHcwaaIjBIPJ//s1RFgyfRtU5eLFWMu/2mi2GC5XS6Yp+5PXxYPGfFVBO1L3 FdoGBWWkQSG15jcdylHWLsWQ0TSpO6VKUbz+HTqZukqFPomA2WY3LFOa7khTqe7L2Z99OjodYu1 sLljcMj0xnW70JTRv03R5F2wEcDMCKvBTWnKQUpXML7CNxKPFr0aczxff/WUfsX8PvtDNU0Polr HF8U6ceQl6MzUmayWzAPPslhgqkfWQ2Q7TRlqFlzymDAg4kcq++r7HaCYqWyi2KNIdZshbHgF8R L7qde50QZvkL9yEMiCtuzP8lNoj8tbXMZAPvTyWN9bOfJ7v0Y4DLc1CLzMOBC3khNUKCkl+0Ny5 KJbL98GfgLTsQIqdHBikyzaQXwnFPtnXifFepe80vPdcFA7YSXcJHVpZ6D9x1ehOg+55xfoHG2m lEJ8tmdLSGnwyeLvexG/xJakAhviwPSDVFjFSgxNen1sTT6k7NSNdaKPTcdVerahVif/MyaTzXb QlWlYzpV/ttX+uZS+0lUxAmFDFsTCOMjXMi5jWcmv+QhlUwOpCAZU1lWwZIzlNwL/lc6bMOKlnW zkuTdqXISHxARvgcj8u9vl/0= X-Received: by 2002:a05:6000:18a5:b0:4b8:6c3e:95f5 with SMTP id ffacd0b85a97d-4cf4cb7f9fbmr14669046f8f.30.1796417460394; Fri, 04 Dec 2026 12:51:00 -0800 (PST) Sender: Frediano Ziglio From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , 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?= , "Claude Opus 5 . 5" Subject: [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data Date: Fri, 4 Dec 2026 20:50:42 +0000 Message-ID: <20261204205045.596093-10-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-16d1c6/1796417461-FCC0777B-50D6E878/0/0 X-purgate-type: clean X-purgate-size: 29458 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 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 Co-Authored-By: Claude Opus 5.5 --- 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 #include #include +#include #include 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