From patchwork Wed Feb 11 14:32:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1814 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770820453; bh=v0whPuZwojqBtwpMmNv+qkRr8QyoHiE5fmzEh3HhLdE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=DIaiXjdbtu10gBpmOa2P38kEPdWjOWzt6Cn1uA7atWzw+arvoYKeKiJmbmkHm2xWy kvoPpJ7libYfX9fMCe9IKa46BoGpBa/oO1HXBrkf+EwtZdrN0jUXmxokSnmTqp912o xWGgUMwvjaoE/9lW2j7Ka0CgSdGS/iqpiA29YZcsbsXWatDU5dOHnsaRKQYqfl8M0Q ZwSM/P5T9+E2A5mISj9W2ApLT1f6ao8LgI4Xs27vfvtvJvaUifbYlkJa12/xqolQ4z QUgo+9Q3kl8dqxnVfw5byRLQNaFc/DLSEzhHX2YIkT+qMtGs6BlNNQUh+FzroPydfA 5N1vxEvb2EuPg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3586A699D6 for ; Wed, 11 Feb 2026 07:34:13 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id M5hgdzzQSDjk for ; Wed, 11 Feb 2026 07:34:13 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770820453; bh=v0whPuZwojqBtwpMmNv+qkRr8QyoHiE5fmzEh3HhLdE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=DIaiXjdbtu10gBpmOa2P38kEPdWjOWzt6Cn1uA7atWzw+arvoYKeKiJmbmkHm2xWy kvoPpJ7libYfX9fMCe9IKa46BoGpBa/oO1HXBrkf+EwtZdrN0jUXmxokSnmTqp912o xWGgUMwvjaoE/9lW2j7Ka0CgSdGS/iqpiA29YZcsbsXWatDU5dOHnsaRKQYqfl8M0Q ZwSM/P5T9+E2A5mISj9W2ApLT1f6ao8LgI4Xs27vfvtvJvaUifbYlkJa12/xqolQ4z QUgo+9Q3kl8dqxnVfw5byRLQNaFc/DLSEzhHX2YIkT+qMtGs6BlNNQUh+FzroPydfA 5N1vxEvb2EuPg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 235CF69A89 for ; Wed, 11 Feb 2026 07:34:13 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770820451; bh=F7kE+uEWpCkOIq87evM+vZO8ouYgJPtd/5JJAFRimv0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nPp9rX4gJzqreVmRtkVCIeaBSPPODx5bZCFvZMXx1MxNF2WIrX15WFShLyA9+tJ+U 1AwWIZbDS75PH9VNoiIO7ZB7jwWPiHf8WGQC2EiwBqJUmIom8CTPSNkqgQFq4ONos1 C5wTGbZ9KBIixc1Wz5c+CPJ/9g8Iey5/TB2wMVG51LbIL4aEAdQqGnCDOSqNtVHzI0 +0H88FJIcaV7ytNkNhdYZuggmf0vCIighF9Nves0t51nlB+jiaIpspZiih0InboyQv oHlC7r0z/EdWPtKZD2gBrTWB9kHNPzacwW3FIHpKbyQvNNPHJJyjrMZFwnbeA8F+Hx pQI7F4bU1AP+A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 101F869A8E; Wed, 11 Feb 2026 07:34:11 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id 9DPGLsywK8wE; Wed, 11 Feb 2026 07:34:10 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1770820446; bh=IH8LMyexKzKDfJLrshz0nIOTeMybvHJdZPzlUmynU1Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N4bsO3x626TPoL6Cj+b+1dz5pLdt8d9fccIabx8hxo2rlXyCr6f/GN/IDBj0joQ2T 0woCCoDYgTjWUmAE9xxVF1vu1AZGDQr52j/irdSRPxSY1Nkoto4tgalhFWRp3VJLGN aBbWNTpbCAho5IRlVzuCNvJJHLuQnm4V+W9x3wMRzsX9aXgDA9Z1/anVyzbDTElzQh RMQ249m4EIndoSlN0wSojHy+GdYd9hyLRbB8oN5OdsnGuCfa+iPXoRMUpW15ywsyih Yt7qAQM8v7NcJCrbOG/MVivrRfombpXqugS0mdtUXe7N57GrPJqOx9SkRU19zsIwIO dCfoFGeudBajw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id C9026699D6; Wed, 11 Feb 2026 07:34:05 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Wed, 11 Feb 2026 07:32:51 -0700 Message-ID: <20260211143309.1183113-4-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260211143309.1183113-1-sjg@u-boot.org> References: <20260211143309.1183113-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: PSENFYYWE6SSQNVK76NFPNK7Z2I7BH2E X-Message-ID-Hash: PSENFYYWE6SSQNVK76NFPNK7Z2I7BH2E X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass , "Claude Opus 4 . 5" X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 03/12] efi: Cache gd->relocaddr for EFI runtime services List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass The EFI runtime service SetVirtualAddressMap() and the functions it calls access gd->relocaddr. On architectures where 'gd' is accessed through a CPU register (ARM via r9/x18, RISC-V via tp, x86_64 via MSR_FS_BASE), the register is repurposed by the OS after ExitBootServices(). When the OS later calls SetVirtualAddressMap(), the gd pointer is invalid, causing a page fault. On x86_64, the recent switch to MSR_FS_BASE for the gd pointer means that %fs:0 now reads from the Linux kernel's per-CPU data area instead of U-Boot's global_data, resulting in a bogus relocaddr value and a crash during the EFI virtual-address relocation. Cache gd->relocaddr in an __efi_runtime_data variable during the initial relocation call from board_r.c (when gd is still valid) and use this cached value in all three runtime-time references. This is correct for all architectures since EFI runtime code should not depend on U-Boot's gd after ExitBootServices(). Fix a checkpatch warning while here. Co-developed-by: Claude Opus 4.5 Signed-off-by: Simon Glass --- lib/efi_loader/efi_runtime.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c index 0a6004f404d..bd09d78d047 100644 --- a/lib/efi_loader/efi_runtime.c +++ b/lib/efi_loader/efi_runtime.c @@ -97,6 +97,7 @@ struct elf_rela { static __efi_runtime_data struct efi_mem_desc *efi_virtmap; static __efi_runtime_data efi_uintn_t efi_descriptor_count; static __efi_runtime_data efi_uintn_t efi_descriptor_size; +static __efi_runtime_data ulong efi_relocaddr; /* * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI @@ -684,7 +685,7 @@ static __efi_runtime void efi_relocate_runtime_table(ulong offset) void **pos; /* Relocate the runtime services pointers */ - patchoff = offset - gd->relocaddr; + patchoff = offset - efi_relocaddr; for (pos = (void **)&efi_runtime_services.get_time; pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) { if (*pos) @@ -721,6 +722,18 @@ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) if (gd_ulib()) return; + /* + * Cache gd->relocaddr for use by the EFI runtime services after + * the OS has taken over. On architectures where 'gd' is accessed + * through a register (ARM, RISC-V, x86_64), it becomes invalid + * once the OS overwrites that register. + * + * The first call (map == NULL) comes from board_r.c during + * U-Boot init, when gd is still valid. + */ + if (!map) + efi_relocaddr = gd->relocaddr; + #ifdef IS_RELA struct elf_rela *rel = (void *)__efi_runtime_rel_start; #else @@ -734,7 +747,7 @@ void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) ulong *p; ulong newaddr; - p = (void*)((ulong)rel->offset - base) + gd->relocaddr; + p = (void *)((ulong)rel->offset - base) + efi_relocaddr; /* * The runtime services table is updated in @@ -910,7 +923,7 @@ static efi_status_t EFIAPI efi_set_virtual_address_map( map = (void*)virtmap + (descriptor_size * i); if (map->type == EFI_RUNTIME_SERVICES_CODE) { ulong new_offset = map->virtual_start - - map->physical_start + gd->relocaddr; + map->physical_start + efi_relocaddr; efi_relocate_runtime_table(new_offset); efi_runtime_relocate(new_offset, map);