From nobody Thu Nov 28 09:43:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1670972788990285.22458438322826; Tue, 13 Dec 2022 15:06:28 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.461464.719561 (Exim 4.92) (envelope-from ) id 1p5ELg-00045R-K9; Tue, 13 Dec 2022 23:06:08 +0000 Received: by outflank-mailman (output) from mailman id 461464.719561; Tue, 13 Dec 2022 23:06:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1p5ELg-00045K-Ge; Tue, 13 Dec 2022 23:06:08 +0000 Received: by outflank-mailman (input) for mailman id 461464; Tue, 13 Dec 2022 23:06:07 +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 1p5ELf-00044C-QY for xen-devel@lists.xenproject.org; Tue, 13 Dec 2022 23:06:07 +0000 Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id b6f84edd-7b3a-11ed-8fd2-01056ac49cbb; Wed, 14 Dec 2022 00:06:03 +0100 (CET) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 35419320079B; Tue, 13 Dec 2022 18:06:01 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 13 Dec 2022 18:06:01 -0500 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 13 Dec 2022 18:06:00 -0500 (EST) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: b6f84edd-7b3a-11ed-8fd2-01056ac49cbb DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-transfer-encoding:date :date:from:from:in-reply-to:message-id:mime-version:reply-to :sender:subject:subject:to:to; s=fm1; t=1670972760; x= 1671059160; bh=l9in6W1y9JfHnZdCgIaroaKvCryg4beZ3lAXpT8+2CM=; b=T QA3mIgoKHz1RzItf4sf2YefE8oDeKJPyJbAjZ15Ot1Lg5FZpQgaIK/jS6JEEN8gy xhc6hsx6AcI0Mx85/MCoDD/BGf3/fBwttSzwYSAwAvB76/tcLwMGGI7TsMKAQIh0 BUJ1pEedljk0vVlzQBkn5rDHgx+cJca4GsUfzB0cVO1hgHmuQqTATQywZxZDixys 65/dFLTPmsr/rwdzLo/7LAhZVLoCUzAx4id9QiUtIc1vXnm0ocm3silYojCnD847 fn4q3dIdFIG29MStDvJGGyODeij4dEXQjRBC9P4+Jqj7BAD8Sra/3llry3xiNmRJ B/iIZva06EXGLKoQCwEdA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1670972760; x=1671059160; bh=l9in6W1y9JfHnZdCgIaroaKvCryg4beZ3lA XpT8+2CM=; b=PWiaqJurpSo8d5WZdNQaNvRayLFMrVUnWENi2+f9JRTU/ZW1I0d qrsHJMb7PDQ3mVTLDwuNdImkadPM37+spVx/4KIYu0BTi7KBOCGZ746KpSdAJOD4 dBvZKvQ6zxH9MqbjIkztUoOzZVYZCGNZFjkRWgscPbhvxqWuCqcKuQxpEeky31LZ cLwlNMkzhs/wQ9G3+/d66YlgUGsUvoIsNqSJVcH33oJEQ/E8UcnVqH17BT+ef+4a Si4KEUsRvI4jHHXrhqmdL86d4MzRhbvj1ePP8us5pCokn08MU5HKTZqOEv3DFRs4 EgFt4lumSD882mrpiu8YMcCBsmaWLm3v2Sw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedvgddtfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffoggfgsedtkeertdertddtnecuhfhrohhmpeffvghmihcuofgr rhhivgcuqfgsvghnohhurhcuoeguvghmihesihhnvhhishhisghlvghthhhinhhgshhlrg gsrdgtohhmqeenucggtffrrghtthgvrhhnpedvfeegkedvkefgffegkefhieejtdffkeeh hfelheefjeeutefgleeggfdtveeileenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpeguvghmihesihhnvhhishhisghlvghthhhinhhgshhlrggs rdgtohhm X-ME-Proxy: Feedback-ID: iac594737:Fastmail From: Demi Marie Obenour To: xen-devel@lists.xenproject.org Cc: Demi Marie Obenour , =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= , Jan Beulich Subject: [PATCH v2] efi: Relocate the ESRT when booting via multiboot2 Date: Tue, 13 Dec 2022 18:03:20 -0500 Message-Id: X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1670972790951100001 Content-Type: text/plain; charset="utf-8" This was missed in the initial patchset. Signed-off-by: Demi Marie Obenour --- xen/arch/x86/efi/efi-boot.h | 2 + xen/common/efi/boot.c | 136 ++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 68 deletions(-) diff --git a/xen/arch/x86/efi/efi-boot.h b/xen/arch/x86/efi/efi-boot.h index abfc7ab0f31511e2c1ee402a09ac533d260444b2..a9a2991d6462dec9cea695c8b91= 2b72df26bd511 100644 --- a/xen/arch/x86/efi/efi-boot.h +++ b/xen/arch/x86/efi/efi-boot.h @@ -825,6 +825,8 @@ void __init efi_multiboot2(EFI_HANDLE ImageHandle, EFI_= SYSTEM_TABLE *SystemTable if ( gop ) efi_set_gop_mode(gop, gop_mode); =20 + efi_relocate_esrt(SystemTable); + efi_exit_boot(ImageHandle, SystemTable, true); } =20 diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index 32ae6b43bb53448421c908819cda552757157c1f..ea5f010df1c5ce40fb67a91fdbf= e28f40865252c 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -625,6 +625,74 @@ static size_t __init get_esrt_size(const EFI_MEMORY_DE= SCRIPTOR *desc) return esrt_ptr->FwResourceCount * sizeof(esrt_ptr->Entries[0]); } =20 +static EFI_GUID __initdata esrt_guid =3D EFI_SYSTEM_RESOURCE_TABLE_GUID; + +static void __init efi_relocate_esrt(EFI_SYSTEM_TABLE *SystemTable) +{ + EFI_STATUS status; + UINTN info_size =3D 0, map_key, mdesc_size; + void *memory_map =3D NULL; + UINT32 ver; + unsigned int i; + + for ( ; ; ) + { + status =3D efi_bs->GetMemoryMap(&info_size, memory_map, &map_key, + &mdesc_size, &ver); + if ( status =3D=3D EFI_SUCCESS && memory_map !=3D NULL ) + break; + if ( status =3D=3D EFI_BUFFER_TOO_SMALL || memory_map =3D=3D NULL ) + { + info_size +=3D 8 * mdesc_size; + if ( memory_map !=3D NULL ) + efi_bs->FreePool(memory_map); + memory_map =3D NULL; + status =3D efi_bs->AllocatePool(EfiLoaderData, info_size, &mem= ory_map); + if ( status =3D=3D EFI_SUCCESS ) + continue; + PrintErr(L"Cannot allocate memory to relocate ESRT\r\n"); + } + else + PrintErr(L"Cannot obtain memory map to relocate ESRT\r\n"); + return; + } + + /* Try to obtain the ESRT. Errors are not fatal. */ + for ( i =3D 0; i < info_size; i +=3D mdesc_size ) + { + /* + * ESRT needs to be moved to memory of type EfiACPIReclaimMemory + * so that the memory it is in will not be used for other purposes. + */ + void *new_esrt =3D NULL; + const EFI_MEMORY_DESCRIPTOR *desc =3D memory_map + i; + size_t esrt_size =3D get_esrt_size(desc); + + if ( !esrt_size ) + continue; + if ( desc->Type =3D=3D EfiRuntimeServicesData || + desc->Type =3D=3D EfiACPIReclaimMemory ) + break; /* ESRT already safe from reuse */ + status =3D efi_bs->AllocatePool(EfiACPIReclaimMemory, esrt_size, + &new_esrt); + if ( status =3D=3D EFI_SUCCESS && new_esrt ) + { + memcpy(new_esrt, (void *)esrt, esrt_size); + status =3D efi_bs->InstallConfigurationTable(&esrt_guid, new_e= srt); + if ( status !=3D EFI_SUCCESS ) + { + PrintErr(L"Cannot install new ESRT\r\n"); + efi_bs->FreePool(new_esrt); + } + } + else + PrintErr(L"Cannot allocate memory for ESRT\r\n"); + break; + } + + efi_bs->FreePool(memory_map); +} + /* * Include architecture specific implementation here, which references the * static globals defined above. @@ -903,8 +971,6 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTP= UT_PROTOCOL *gop, return gop_mode; } =20 -static EFI_GUID __initdata esrt_guid =3D EFI_SYSTEM_RESOURCE_TABLE_GUID; - static void __init efi_tables(void) { unsigned int i; @@ -1113,72 +1179,6 @@ static void __init efi_set_gop_mode(EFI_GRAPHICS_OUT= PUT_PROTOCOL *gop, UINTN gop #define INVALID_VIRTUAL_ADDRESS (0xBAAADUL << \ (EFI_PAGE_SHIFT + BITS_PER_LONG - 32)) =20 -static void __init efi_relocate_esrt(EFI_SYSTEM_TABLE *SystemTable) -{ - EFI_STATUS status; - UINTN info_size =3D 0, map_key, mdesc_size; - void *memory_map =3D NULL; - UINT32 ver; - unsigned int i; - - for ( ; ; ) - { - status =3D efi_bs->GetMemoryMap(&info_size, memory_map, &map_key, - &mdesc_size, &ver); - if ( status =3D=3D EFI_SUCCESS && memory_map !=3D NULL ) - break; - if ( status =3D=3D EFI_BUFFER_TOO_SMALL || memory_map =3D=3D NULL ) - { - info_size +=3D 8 * mdesc_size; - if ( memory_map !=3D NULL ) - efi_bs->FreePool(memory_map); - memory_map =3D NULL; - status =3D efi_bs->AllocatePool(EfiLoaderData, info_size, &mem= ory_map); - if ( status =3D=3D EFI_SUCCESS ) - continue; - PrintErr(L"Cannot allocate memory to relocate ESRT\r\n"); - } - else - PrintErr(L"Cannot obtain memory map to relocate ESRT\r\n"); - return; - } - - /* Try to obtain the ESRT. Errors are not fatal. */ - for ( i =3D 0; i < info_size; i +=3D mdesc_size ) - { - /* - * ESRT needs to be moved to memory of type EfiACPIReclaimMemory - * so that the memory it is in will not be used for other purposes. - */ - void *new_esrt =3D NULL; - const EFI_MEMORY_DESCRIPTOR *desc =3D memory_map + i; - size_t esrt_size =3D get_esrt_size(desc); - - if ( !esrt_size ) - continue; - if ( desc->Type =3D=3D EfiRuntimeServicesData || - desc->Type =3D=3D EfiACPIReclaimMemory ) - break; /* ESRT already safe from reuse */ - status =3D efi_bs->AllocatePool(EfiACPIReclaimMemory, esrt_size, - &new_esrt); - if ( status =3D=3D EFI_SUCCESS && new_esrt ) - { - memcpy(new_esrt, (void *)esrt, esrt_size); - status =3D efi_bs->InstallConfigurationTable(&esrt_guid, new_e= srt); - if ( status !=3D EFI_SUCCESS ) - { - PrintErr(L"Cannot install new ESRT\r\n"); - efi_bs->FreePool(new_esrt); - } - } - else - PrintErr(L"Cannot allocate memory for ESRT\r\n"); - break; - } - - efi_bs->FreePool(memory_map); -} - static void __init efi_exit_boot(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE = *SystemTable, bool exit_boot_services) { EFI_STATUS status; --=20 Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab