From patchwork Wed Aug 28 09:19:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13780945 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 755CDC54757 for ; Wed, 28 Aug 2024 09:20:20 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.784560.1193940 (Exim 4.92) (envelope-from ) id 1sjEqb-00053t-Ba; Wed, 28 Aug 2024 09:20:13 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 784560.1193940; Wed, 28 Aug 2024 09:20:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sjEqb-00053m-7O; Wed, 28 Aug 2024 09:20:13 +0000 Received: by outflank-mailman (input) for mailman id 784560; Wed, 28 Aug 2024 09:20:12 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sjEqa-00052T-36 for xen-devel@lists.xenproject.org; Wed, 28 Aug 2024 09:20:12 +0000 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [2607:f8b0:4864:20::f2f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id b93be8dd-651e-11ef-a0b0-8be0dac302b0; Wed, 28 Aug 2024 11:20:10 +0200 (CEST) Received: by mail-qv1-xf2f.google.com with SMTP id 6a1803df08f44-6bf6beda0c0so35099476d6.1 for ; Wed, 28 Aug 2024 02:20:10 -0700 (PDT) Received: from fziglio-xenia-fedora.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c162d21710sm63763296d6.11.2024.08.28.02.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 02:20:09 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b93be8dd-651e-11ef-a0b0-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1724836809; x=1725441609; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4RGhBHc552V5isJ2kp8ZF0VtasdROqoNs8OcgQhRR7c=; b=OM2YtuxsRZBYxMM/2nC1oTfCSgSkVpSwrQlziQYVtKxwho5Q+WnFoc+hYdmM7THtmZ Hj1b6dqObWJQGbQI9pAF5m+7NL03OTyPWSoZOvzdnly0uLGgd2ZpXkANcfmo2oPJqEb8 vnxFXCt2Z+8Scb2uJ8gyVtKPoKYL9Od75QJN4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724836809; x=1725441609; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4RGhBHc552V5isJ2kp8ZF0VtasdROqoNs8OcgQhRR7c=; b=EK+q+xIqD7is6BtPHAnBD+JbbbG02yDmpRBcpCpkvqKt+BiRH4bHpEdta5k+wPWpCh hLrO3H8GovFMIJh5PsLVB+RxKPrq7FsZ7JAc+/1fma08/1TON6gvHhnG3m2vsnGVLQWi zEYQv93UqCt4SUoegcb2ux5mDMAnzBNJHVXuZtrgWKWQvJXwVeHiaUs9fyKGHYA9UxBw 0MYOUc+AtTSPmUbbDB0ufEQScw7PbeT5Q7WTEZqTMbeHEbcUnDdijRspAv1WVTZKvdgC /yUuS/6J7nYl5bVs9eV7ZD7O9As8idf9ak+qkcefYrLrc4fB4eNQFqwWRsSct07YBn/7 WWYA== X-Gm-Message-State: AOJu0YzAFoMcNjStMWKrWvYPAbX0O9RlzEjhFqbylJBR86gtwcAqhINJ 9lWHYmm/5dV0mNQHafhlHtmHrBjALTX8GdNJkdwMjLNsZi32CaJ2WvjdbXTCK2Zo/HqZ+mqSzXj K X-Google-Smtp-Source: AGHT+IHxRkJdClhb6Gw3jEDOTUxCeI/er0Xo4kT911pY124/51QCpurE13swPk/asc4qF65QJgh7wg== X-Received: by 2002:a05:6214:5987:b0:6b5:e2da:8bec with SMTP id 6a1803df08f44-6c16deb85f1mr194733766d6.55.1724836809441; Wed, 28 Aug 2024 02:20:09 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v2 1/2] x86/boot: Avoid relocations in trampoline code to physical addresses Date: Wed, 28 Aug 2024 10:19:53 +0100 Message-ID: <20240828091956.127760-1-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 The trampoline could have "manual" relocation entries (created by assembly macros and some code to use them) and (in case of PE) normal executable relocations. Remove some normal executable ones. In this way we don't have to worry about applying both correctly (they need proper order which is hard to spot looking at the code). Specifically in efi_arch_post_exit_boot trampoline is copied after fixing relocations with efi_arch_relocate_image. These time dependencies between different part of code are hard to spot making hard to change code. In this case the copy is done in a state where code should be run at higher locations so it would be better to reduce the code between calling efi_arch_relocate_image and jumping to higher location. Absolute symbols are defined by linker in order to avoid relocations. These symbols use a "_PA" suffix to avoid possible clashes. phys_addr macro is used to make more clear the address we want and making symbol search easier. sym_offs and sym_esi are undefined to avoid the usage of them which would lead to relocations. Signed-off-by: Frediano Ziglio --- xen/arch/x86/boot/head.S | 6 ++++-- xen/arch/x86/boot/trampoline.S | 9 ++++++++- xen/arch/x86/boot/wakeup.S | 4 ++-- xen/arch/x86/xen.lds.S | 9 +++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) --- Changes since v1: - add subject prefix to commit messages; - more explanation of the improvement; - use upper case for suffix to avoid clashes; - undefine some macros to avoid usage of them; - use some macros to make code more clear; - use hidden symbols; - split change in 2 commits. diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index d8ac0f0494..a8a14060b6 100644 --- a/xen/arch/x86/boot/head.S +++ b/xen/arch/x86/boot/head.S @@ -870,8 +870,10 @@ cmdline_parse_early: reloc: .incbin "reloc.bin" +#include "x86_64.S" + + .section .init.text, "ax", @progbits + ENTRY(trampoline_start) #include "trampoline.S" ENTRY(trampoline_end) - -#include "x86_64.S" diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S index b8ab0ffdcb..3a6eb942a7 100644 --- a/xen/arch/x86/boot/trampoline.S +++ b/xen/arch/x86/boot/trampoline.S @@ -16,6 +16,11 @@ * not guaranteed to persist. */ +/* We don't want relocation in the trampoline. + * Undefine some macros generating relocations. */ +#undef sym_offs +#undef sym_esi + /* NB. bootsym() is only usable in real mode, or via BOOT_PSEUDORM_DS. */ #undef bootsym #define bootsym(s) ((s)-trampoline_start) @@ -34,6 +39,8 @@ .long 111b - (off) - .; \ .popsection +#define phys_addr(sym) sym ## _PA + /* Start of the permanent trampoline code. */ .code16 @@ -73,7 +80,7 @@ trampoline_protmode_entry: mov %ecx,%cr4 /* Load pagetable base register. */ - mov $sym_offs(idle_pg_table),%eax + mov $phys_addr(idle_pg_table), %eax add bootsym_rel(trampoline_xen_phys_start,4,%eax) mov %eax,%cr3 diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S index 08447e1934..ae4dd5eb40 100644 --- a/xen/arch/x86/boot/wakeup.S +++ b/xen/arch/x86/boot/wakeup.S @@ -99,7 +99,7 @@ wakeup_32: mov $bootsym_rel(wakeup_stack, 4, %esp) # check saved magic again - mov $sym_offs(saved_magic),%eax + mov $phys_addr(saved_magic), %eax add bootsym_rel(trampoline_xen_phys_start, 4, %eax) mov (%eax), %eax cmp $0x9abcdef0, %eax @@ -112,7 +112,7 @@ wakeup_32: mov %ecx, %cr4 /* Load pagetable base register */ - mov $sym_offs(idle_pg_table),%eax + mov $phys_addr(idle_pg_table), %eax add bootsym_rel(trampoline_xen_phys_start,4,%eax) mov %eax,%cr3 diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S index 9a1dfe1b34..5cfbd2524a 100644 --- a/xen/arch/x86/xen.lds.S +++ b/xen/arch/x86/xen.lds.S @@ -22,7 +22,7 @@ ENTRY(efi_start) #define FORMAT "elf64-x86-64" -ENTRY(start_pa) +ENTRY(start_PA) #endif /* EFI */ @@ -71,7 +71,12 @@ SECTIONS __2M_text_start = .; /* Start of 2M superpages, mapped RX. */ #endif - start_pa = ABSOLUTE(start - __XEN_VIRT_START); +#define DEFINE_PA_ADDRESS(sym) \ + HIDDEN(sym ## _PA = ABSOLUTE(sym - __XEN_VIRT_START)) + + DEFINE_PA_ADDRESS(start); + DEFINE_PA_ADDRESS(saved_magic); + DEFINE_PA_ADDRESS(idle_pg_table); . = __XEN_VIRT_START + XEN_IMG_OFFSET; _start = .; From patchwork Wed Aug 28 09:19:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frediano Ziglio X-Patchwork-Id: 13780946 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4677FC54799 for ; Wed, 28 Aug 2024 09:20:23 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.784561.1193950 (Exim 4.92) (envelope-from ) id 1sjEqc-0005KM-Jg; Wed, 28 Aug 2024 09:20:14 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 784561.1193950; Wed, 28 Aug 2024 09:20:14 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sjEqc-0005KD-Fp; Wed, 28 Aug 2024 09:20:14 +0000 Received: by outflank-mailman (input) for mailman id 784561; Wed, 28 Aug 2024 09:20:13 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sjEqb-0003rJ-So for xen-devel@lists.xenproject.org; Wed, 28 Aug 2024 09:20:13 +0000 Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [2607:f8b0:4864:20::82a]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id b9ea46e9-651e-11ef-99a0-01e77a169b0f; Wed, 28 Aug 2024 11:20:12 +0200 (CEST) Received: by mail-qt1-x82a.google.com with SMTP id d75a77b69052e-456768fb0a6so813951cf.1 for ; Wed, 28 Aug 2024 02:20:12 -0700 (PDT) Received: from fziglio-xenia-fedora.eng.citrite.net ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c162d21710sm63763296d6.11.2024.08.28.02.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 02:20:10 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b9ea46e9-651e-11ef-99a0-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1724836811; x=1725441611; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0JSvtCmO9jAkp0KnDfC0SlX5scV2vhq50nOcVowwJtE=; b=eCGnQkJWObvfewn799KREz1s3pjIqYVJxzkmhma20q6vB7IboFZMsXwTYhpRxpEF5S k0qLsdX3EDjidTYTkdSc4VrchIr9p5GyJ+j8OkQ9jXVK/fCzM96bG6dMHLYq7KLPhesu VKfGqLS/3CMQJdgV3IpHVHwnrBMO3MppEQ+RI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724836811; x=1725441611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0JSvtCmO9jAkp0KnDfC0SlX5scV2vhq50nOcVowwJtE=; b=G1F3egVCV4X0K6JNyeMz1RdciyGIW3yXQGtrBh1h9ergkJ6NHbFro/sQCD2NS3eXti eEkFlkU8qv0r06cl52dsYHAB177rARPU0MeLH9WR0Xm6Yxx0Nhwt2d+OG0jirMyINKIA 7dJvNlk4IiaKYrQ16EfQXkg1riaMWXMJ0nZfUX6LKlh6mhaGXSHaJKDIIMht6zbdAu0K VCP+mryNg6gw91iCggQuukq3EcneWNs65C/Lbzn82XhMgv/Ln6ANv7RMWtSvItMtGsat ow7tCGnEbhAFb356OVh037ynaFYg+ioLHvtBpKogSPHKqPSwI3K/658jVuTeAcZktyZH Vjxw== X-Gm-Message-State: AOJu0Yx1YS6GHEYLbj4tNVl5GAB01tizOCU/WERx3TzxwPCwnRw+Qil4 Rny94bAYFQqln4QlP1XEZf5UU6fKIITbhhN7IRU1rjNsYQKHnBg4cVkg6ITpFZ3zbBsO/7IcLuD h X-Google-Smtp-Source: AGHT+IE5mp8V1Sj6xT5ZqXrshqa1ZdKt2HKcZZ8KJuKayVeJWYDbIBov1bGRqRh6mk8q5R28JWO/RQ== X-Received: by 2002:a05:6214:2b82:b0:6b7:4712:c878 with SMTP id 6a1803df08f44-6c16dcbcb08mr191690446d6.41.1724836810614; Wed, 28 Aug 2024 02:20:10 -0700 (PDT) From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Subject: [PATCH v2 2/2] x86/boot: Avoid relocations in trampoline code to virtual addresses Date: Wed, 28 Aug 2024 10:19:54 +0100 Message-ID: <20240828091956.127760-2-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240828091956.127760-1-frediano.ziglio@cloud.com> References: <20240828091956.127760-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Add virt_addr macro to make more clear the address we want and making symbol search easier. We don't generate direct absolute symbols to virtual addresses to avoid linker warnings. Signed-off-by: Frediano Ziglio --- xen/arch/x86/boot/trampoline.S | 3 ++- xen/arch/x86/boot/wakeup.S | 2 +- xen/arch/x86/xen.lds.S | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/boot/trampoline.S b/xen/arch/x86/boot/trampoline.S index 3a6eb942a7..02c713af91 100644 --- a/xen/arch/x86/boot/trampoline.S +++ b/xen/arch/x86/boot/trampoline.S @@ -40,6 +40,7 @@ .popsection #define phys_addr(sym) sym ## _PA +#define virt_addr(sym) (sym ## _PA + __XEN_VIRT_START) /* Start of the permanent trampoline code. */ @@ -120,7 +121,7 @@ trampoline_protmode_entry: .code64 start64: /* Jump to high mappings. */ - movabs $__high_start, %rdi + movabs $virt_addr(__high_start), %rdi jmpq *%rdi #include "video.h" diff --git a/xen/arch/x86/boot/wakeup.S b/xen/arch/x86/boot/wakeup.S index ae4dd5eb40..5ac8ba1fb6 100644 --- a/xen/arch/x86/boot/wakeup.S +++ b/xen/arch/x86/boot/wakeup.S @@ -156,7 +156,7 @@ wakeup_32: .code64 wakeup_64: /* Jump to high mappings and the higher-level wakeup code. */ - movabs $s3_resume, %rbx + movabs $virt_addr(s3_resume), %rbx jmp *%rbx bogus_saved_magic: diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S index 5cfbd2524a..b200606278 100644 --- a/xen/arch/x86/xen.lds.S +++ b/xen/arch/x86/xen.lds.S @@ -77,6 +77,8 @@ SECTIONS DEFINE_PA_ADDRESS(start); DEFINE_PA_ADDRESS(saved_magic); DEFINE_PA_ADDRESS(idle_pg_table); + DEFINE_PA_ADDRESS(__high_start); + DEFINE_PA_ADDRESS(s3_resume); . = __XEN_VIRT_START + XEN_IMG_OFFSET; _start = .;