From patchwork Wed May 18 17:32:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Demi Marie Obenour X-Patchwork-Id: 12853917 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 490F5C433F5 for ; Wed, 18 May 2022 17:33:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.332412.556104 (Exim 4.92) (envelope-from ) id 1nrNXX-0004Yj-2M; Wed, 18 May 2022 17:32:51 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 332412.556104; Wed, 18 May 2022 17:32:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nrNXW-0004Yc-VT; Wed, 18 May 2022 17:32:50 +0000 Received: by outflank-mailman (input) for mailman id 332412; Wed, 18 May 2022 17:32:49 +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 1nrNXV-0004YW-4c for xen-devel@lists.xenproject.org; Wed, 18 May 2022 17:32:49 +0000 Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 86dc5a81-d6d0-11ec-837e-e5687231ffcc; Wed, 18 May 2022 19:32:46 +0200 (CEST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 2482A32008FD; Wed, 18 May 2022 13:32:43 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 18 May 2022 13:32:43 -0400 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 18 May 2022 13:32:42 -0400 (EDT) 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: 86dc5a81-d6d0-11ec-837e-e5687231ffcc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= invisiblethingslab.com; h=cc:cc:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:sender:subject :subject:to:to; s=fm1; t=1652895162; x=1652981562; bh=l3nshceWNm Nh4sbzOcO5cSmfwPWblYE/KIfax+ALspQ=; b=QFMkLX1r/cgDfiTupBMI9D2l31 PjmfEwzC9UAj9TQUVEOL+TOhfwN8ZhZ3/V285O7oAL1Ep2Gl1IMg5mNYugM5neqM g2JPcHp1ognEGx+sJvT7GXOx2hLycscrutbVX9P0wD+EXZ4owvuGzAmFFzLMzJbc B749DQMC0J+8hF8tmA4hf3yU0ioGTwcujI17Y8nP4Jtz+TjpAhL16fcyj2l90/5u tOfoefErVJVLKq5FPuboMhAUmosxWfHK5CPxMCttoW7HZIxOu5J2rBghI22H6+wU J9wsWUxfoWg0Q2+U+keIl+o/RjSHMBwaf9fJ4XTLsOixEnk16KI9Q8AV+Ixg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type: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=fm1; t=1652895162; x= 1652981562; bh=l3nshceWNmNh4sbzOcO5cSmfwPWblYE/KIfax+ALspQ=; b=w P6lquywixaX+sjALnu9g8Q++0Slfj8N2ePxrRJYitdMrxBfTgrLgbTtUal7mYtfz MHyjvCB73phWO5UZDJJujGCJL/+FOLtKlweM8LV6ekM/N/tr8mc+zG9LPnFqwLqq t6ms+4bX0OYL4RFScNSJRQElJNndpBm9WMO1AejHxszvAKQj0FFAHyHzUgdYj49w oVut+Sv3Dl9RtIzRjFRdmSHnqZSlYJeDzX2us9tkdqfrNkwfve7vEutSIS2dE/HA vZ2Tcw2Ha0EgDcNaolEuciyd7b3O2pVKSGUHjYa29sXJvQ+R2OdQxDGHWVe+Z81I YnXx/LbLXSEwpdS8mkFKw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrheelgdduuddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugesghdtreertddtjeenucfhrhhomhepffgvmhhiucfo rghrihgvucfqsggvnhhouhhruceouggvmhhisehinhhvihhsihgslhgvthhhihhnghhslh grsgdrtghomheqnecuggftrfgrthhtvghrnhepudekteekkeekkedvteegtdektefftdfh tddtgfeifffhkeehhfejhfevgefgieehnecuffhomhgrihhnpehkvghrnhgvlhdrohhrgh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeguvghm ihesihhnvhhishhisghlvghthhhinhhgshhlrggsrdgtohhm X-ME-Proxy: Feedback-ID: iac594737:Fastmail Date: Wed, 18 May 2022 13:32:33 -0400 From: Demi Marie Obenour To: Xen developer discussion Cc: Jan Beulich Subject: [PATCH v6] Preserve the EFI System Resource Table for dom0 Message-ID: MIME-Version: 1.0 Content-Disposition: inline The EFI System Resource Table (ESRT) is necessary for fwupd to identify firmware updates to install. According to the UEFI specification ยง23.4, the ESRT shall be stored in memory of type EfiBootServicesData. However, memory of type EfiBootServicesData is considered general-purpose memory by Xen, so the ESRT needs to be moved somewhere where Xen will not overwrite it. Copy the ESRT to memory of type EfiRuntimeServicesData, which Xen will not reuse. dom0 can use the ESRT if (and only if) it is in memory of type EfiRuntimeServicesData. Earlier versions of this patch reserved the memory in which the ESRT was located. This created awkward alignment problems, and required either splitting the E820 table or wasting memory. It also would have required a new platform op for dom0 to use to indicate if the ESRT is reserved. By copying the ESRT into EfiRuntimeServicesData memory, the E820 table does not need to be modified, and dom0 can just check the type of the memory region containing the ESRT. The copy is only done if the ESRT is not already in EfiRuntimeServicesData memory, avoiding memory leaks on repeated kexec. See https://lore.kernel.org/xen-devel/20200818184018.GN1679@mail-itl/T/ for details. Signed-off-by: Demi Marie Obenour --- Changes since v5: - Use type and field names from EFI specification. - Remove tags from EFI types that do not have them. - Use PrintErr() instead of PrintStr() for error conditions. Changes since v4: - Do not call blexit() (via PrintErrMsg()) if the new ESRT cannot be allocated or installed. - Use the correct comment style. - Remove leftover change from earlier version. - Add a blank line between declarations and statements. - Re-fetch the memory map after allocating the ESRT copy. Changes since v3: - Do not modify the memory map. - Allocate a copy of the ESRT in EfiRuntimeServicesData() memory. xen/common/efi/boot.c | 108 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) diff --git a/xen/common/efi/boot.c b/xen/common/efi/boot.c index a25e1d29f1..6a829b8278 100644 --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -39,6 +39,26 @@ { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} } #define APPLE_PROPERTIES_PROTOCOL_GUID \ { 0x91bd12fe, 0xf6c3, 0x44fb, { 0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0} } +#define EFI_SYSTEM_RESOURCE_TABLE_GUID \ + { 0xb122a263, 0x3661, 0x4f68, {0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80} } +#define EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION 1 + +typedef struct { + EFI_GUID FwClass; + UINT32 FwType; + UINT32 FwVersion; + UINT32 LowestSupportedFwVersion; + UINT32 CapsuleFlags; + UINT32 LastAttemptVersion; + UINT32 LastAttemptStatus; +} EFI_SYSTEM_RESOURCE_ENTRY; + +typedef struct { + UINT32 FwResourceCount; + UINT32 FwResourceCountMax; + UINT64 FwResourceVersion; + EFI_SYSTEM_RESOURCE_ENTRY Entries[]; +} EFI_SYSTEM_RESOURCE_TABLE; typedef EFI_STATUS (/* _not_ EFIAPI */ *EFI_SHIM_LOCK_VERIFY) ( @@ -567,6 +587,39 @@ static int __init efi_check_dt_boot(const EFI_LOADED_IMAGE *loaded_image) } #endif +static UINTN __initdata esrt = EFI_INVALID_TABLE_ADDR; + +static size_t __init get_esrt_size(const EFI_MEMORY_DESCRIPTOR *desc) +{ + size_t available_len, len; + const UINTN physical_start = desc->PhysicalStart; + const EFI_SYSTEM_RESOURCE_TABLE *esrt_ptr; + + len = desc->NumberOfPages << EFI_PAGE_SHIFT; + if ( esrt == EFI_INVALID_TABLE_ADDR ) + return 0; + if ( physical_start > esrt || esrt - physical_start >= len ) + return 0; + /* + * The specification requires EfiBootServicesData, but accept + * EfiRuntimeServicesData, which is a more logical choice. + */ + if ( (desc->Type != EfiRuntimeServicesData) && + (desc->Type != EfiBootServicesData) ) + return 0; + available_len = len - (esrt - physical_start); + if ( available_len <= offsetof(EFI_SYSTEM_RESOURCE_TABLE, Entries) ) + return 0; + available_len -= offsetof(EFI_SYSTEM_RESOURCE_TABLE, Entries); + esrt_ptr = (const EFI_SYSTEM_RESOURCE_TABLE *)esrt; + if ( esrt_ptr->FwResourceVersion != EFI_SYSTEM_RESOURCE_TABLE_FIRMWARE_RESOURCE_VERSION || + !esrt_ptr->FwResourceCount ) + return 0; + if ( esrt_ptr->FwResourceCount > available_len / sizeof(esrt_ptr->Entries[0]) ) + return 0; + return esrt_ptr->FwResourceCount * sizeof(esrt_ptr->Entries[0]); +} + /* * Include architecture specific implementation here, which references the * static globals defined above. @@ -845,6 +898,8 @@ static UINTN __init efi_find_gop_mode(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop, return gop_mode; } +static EFI_GUID __initdata esrt_guid = EFI_SYSTEM_RESOURCE_TABLE_GUID; + static void __init efi_tables(void) { unsigned int i; @@ -868,6 +923,8 @@ static void __init efi_tables(void) efi.smbios = (unsigned long)efi_ct[i].VendorTable; if ( match_guid(&smbios3_guid, &efi_ct[i].VendorGuid) ) efi.smbios3 = (unsigned long)efi_ct[i].VendorTable; + if ( match_guid(&esrt_guid, &efi_ct[i].VendorGuid) ) + esrt = (UINTN)efi_ct[i].VendorTable; } #ifndef CONFIG_ARM /* TODO - disabled until implemented on ARM */ @@ -1056,9 +1113,7 @@ static void __init efi_exit_boot(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *Syste EFI_STATUS status; UINTN info_size = 0, map_key; bool retry; -#ifdef CONFIG_EFI_SET_VIRTUAL_ADDRESS_MAP unsigned int i; -#endif efi_bs->GetMemoryMap(&info_size, NULL, &map_key, &efi_mdesc_size, &mdesc_ver); @@ -1067,6 +1122,46 @@ static void __init efi_exit_boot(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *Syste if ( !efi_memmap ) blexit(L"Unable to allocate memory for EFI memory map"); + efi_memmap_size = info_size; + status = SystemTable->BootServices->GetMemoryMap(&efi_memmap_size, + efi_memmap, &map_key, + &efi_mdesc_size, + &mdesc_ver); + if ( EFI_ERROR(status) ) + PrintErrMesg(L"Cannot obtain memory map", status); + + /* Try to obtain the ESRT. Errors are not fatal. */ + for ( i = 0; i < efi_memmap_size; i += efi_mdesc_size ) + { + /* + * ESRT needs to be moved to memory of type EfiRuntimeServicesData + * so that the memory it is in will not be used for other purposes. + */ + void *new_esrt = NULL; + size_t esrt_size = get_esrt_size(efi_memmap + i); + + if ( !esrt_size ) + continue; + if ( ((EFI_MEMORY_DESCRIPTOR *)(efi_memmap + i))->Type == + EfiRuntimeServicesData ) + break; /* ESRT already safe from reuse */ + status = efi_bs->AllocatePool(EfiRuntimeServicesData, esrt_size, + &new_esrt); + if ( status == EFI_SUCCESS && new_esrt ) + { + memcpy(new_esrt, (void *)esrt, esrt_size); + status = efi_bs->InstallConfigurationTable(&esrt_guid, new_esrt); + if ( status != 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; + } + for ( retry = false; ; retry = true ) { efi_memmap_size = info_size; @@ -1753,3 +1848,12 @@ void __init efi_init_memory(void) unmap_domain_page(efi_l4t); } #endif + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */