From nobody Fri May 17 00:33:11 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 166165515543028.92759740800261; Sat, 27 Aug 2022 19:52:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.394271.633611 (Exim 4.92) (envelope-from ) id 1oS8PB-0001dt-Ny; Sun, 28 Aug 2022 02:52:09 +0000 Received: by outflank-mailman (output) from mailman id 394271.633611; Sun, 28 Aug 2022 02:52:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oS8PB-0001dm-K8; Sun, 28 Aug 2022 02:52:09 +0000 Received: by outflank-mailman (input) for mailman id 394271; Sun, 28 Aug 2022 02:52:08 +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 1oS8PA-0001dg-5h for xen-devel@lists.xenproject.org; Sun, 28 Aug 2022 02:52:08 +0000 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 65c1d989-267c-11ed-bd2e-47488cf2e6aa; Sun, 28 Aug 2022 04:52:05 +0200 (CEST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 020895C00A5; Sat, 27 Aug 2022 22:52:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sat, 27 Aug 2022 22:52:04 -0400 Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 27 Aug 2022 22:52:03 -0400 (EDT) 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: 65c1d989-267c-11ed-bd2e-47488cf2e6aa 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=1661655123; x= 1661741523; bh=G8lfY+HnYPQ+yHzLmohUyXRmKIDZHR6G1hUMBfrHJO4=; b=F 9sv+8hYma89nPnIBgqE14qajzvG2iT24p/XcACk8xtcIg2kyz263vPY37ewp84Yc EU3O9j5UH7Q43aEbQiDQkSwGQ7k3CiQVA7R22g1kvriR5Ssgz9lgPhW/pI8qDDcT 8xA16j0bcmVweJYl25+VNPH5cdUT4bmFey4N8JBKYToR2Q84HksRhesC9metNF+I 3m1BkbY7osFWjB+9AHQ0sOIL1B9ie/uS4KUwhn/VJQxng/3LRMAs0bdeYFABybrF hNtW9eDyfnsBySSfH0OtBAnwhe5392m4L6Y8jUcmlMeWSTixFnhNAMzxZFryaWGu 4q3Gq7pidm+03hzUrQmTQ== 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=fm1; t= 1661655123; x=1661741523; bh=G8lfY+HnYPQ+yHzLmohUyXRmKIDZHR6G1hU MBfrHJO4=; b=yKesZYmxza4UdYJldlVths0Tj3MhSxCOxDHuaMib7E2ly+U7J3U MLF7f3d+mgitXPPyuCQEAd7QGlqptVR9MNQT0dzMEVmaGNfm/zTncLDC3I9ItKTD gJZI9LqUd9BvaoWpZiDHhqY+cSoKRr/kNlN21cAHQeaWdMDjwL2BD5g982RDUbwt SQ8uiECo4Pn5exJ0cWA+hP++58YTNaS1YUzW7a+HtQmlG78yFIwIVRhHUr1prM0d 3GAoFC9vETaZtApC2x+U3xgAi7IUSV3AFtAtVtpVh/X4Dt3QwjhmV4sfl0nkuGnm V2KGsB1v8gKcelGhlDOGCkmceuU/iY50i3A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdejkedgieeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgggfestdekredtredttdenucfhrhhomhepffgvmhhiucfo rghrihgvucfqsggvnhhouhhruceouggvmhhisehinhhvihhsihgslhgvthhhihhnghhslh grsgdrtghomheqnecuggftrfgrthhtvghrnhepvdefgeekvdekgfffgeekhfeijedtffek hefhleehfeejueetgfelgefgtdevieelnecuvehluhhsthgvrhfuihiivgeptdenucfrrg hrrghmpehmrghilhhfrhhomhepuggvmhhisehinhhvihhsihgslhgvthhhihhnghhslhgr sgdrtghomh X-ME-Proxy: Feedback-ID: iac594737:Fastmail From: Demi Marie Obenour To: Ard Biesheuvel , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Cc: Demi Marie Obenour , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org Subject: [PATCH v2] Add support for ESRT loading under Xen Date: Sat, 27 Aug 2022 22:51:58 -0400 Message-Id: <20220828025158.1455-1-demi@invisiblethingslab.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1661655157090100001 Content-Type: text/plain; charset="utf-8" This is needed for fwupd to work in Qubes OS. Signed-off-by: Demi Marie Obenour --- Changes since v1: - Use a different type (struct xen_efi_mem_info) for memory information provided by Xen, as Xen reports it in a different way than the standard Linux functions do. drivers/firmware/efi/esrt.c | 49 +++++++++++++++++++++++++++---------- drivers/xen/efi.c | 32 ++++++++++++++++++++++++++ include/linux/efi.h | 18 ++++++++++++++ 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/drivers/firmware/efi/esrt.c b/drivers/firmware/efi/esrt.c index 2a2f52b017e736dd995c69e8aeb5fbd7761732e5..c0fc149a838044cc16bb08a374a= 0c8ea6b7dcbff 100644 --- a/drivers/firmware/efi/esrt.c +++ b/drivers/firmware/efi/esrt.c @@ -243,27 +243,50 @@ void __init efi_esrt_init(void) void *va; struct efi_system_resource_table tmpesrt; size_t size, max, entry_size, entries_size; - efi_memory_desc_t md; - int rc; phys_addr_t end; - - if (!efi_enabled(EFI_MEMMAP)) - return; + uint32_t type; =20 pr_debug("esrt-init: loading.\n"); if (!esrt_table_exists()) return; =20 - rc =3D efi_mem_desc_lookup(efi.esrt, &md); - if (rc < 0 || - (!(md.attribute & EFI_MEMORY_RUNTIME) && - md.type !=3D EFI_BOOT_SERVICES_DATA && - md.type !=3D EFI_RUNTIME_SERVICES_DATA)) { - pr_warn("ESRT header is not in the memory map.\n"); + if (efi_enabled(EFI_MEMMAP)) { + efi_memory_desc_t md; + + if (efi_mem_desc_lookup(efi.esrt, &md) < 0 || + (!(md.attribute & EFI_MEMORY_RUNTIME) && + md.type !=3D EFI_BOOT_SERVICES_DATA && + md.type !=3D EFI_RUNTIME_SERVICES_DATA)) { + pr_warn("ESRT header is not in the memory map.\n"); + return; + } + + type =3D md.type; + max =3D efi_mem_desc_end(&md); + } else if (IS_ENABLED(CONFIG_XEN_EFI) && efi_enabled(EFI_PARAVIRT)) { + struct xen_efi_mem_info info; + + if (!xen_efi_mem_info_query(efi.esrt, &info)) { + pr_warn("Failed to lookup ESRT header in Xen memory map\n"); + return; + } + + type =3D info.type; + max =3D info.addr + info.size; + + /* Recent Xen versions relocate the ESRT to memory of type + * EfiRuntimeServicesData, which Xen will not reuse. If the ESRT + * is not in EfiRuntimeServicesData memory, it has not been reserved + * by Xen and might be allocated to other guests, so it cannot + * safely be used. */ + if (type !=3D EFI_RUNTIME_SERVICES_DATA) { + pr_warn("Xen did not reserve ESRT, ignoring it\n"); + return; + } + } else { return; } =20 - max =3D efi_mem_desc_end(&md); if (max < efi.esrt) { pr_err("EFI memory descriptor is invalid. (esrt: %p max: %p)\n", (void *)efi.esrt, (void *)max); @@ -333,7 +356,7 @@ void __init efi_esrt_init(void) =20 end =3D esrt_data + size; pr_info("Reserving ESRT space from %pa to %pa.\n", &esrt_data, &end); - if (md.type =3D=3D EFI_BOOT_SERVICES_DATA) + if (type =3D=3D EFI_BOOT_SERVICES_DATA) efi_mem_reserve(esrt_data, esrt_data_size); =20 pr_debug("esrt-init: loaded.\n"); diff --git a/drivers/xen/efi.c b/drivers/xen/efi.c index d1ff2186ebb48a7c0981ecb6d4afcbbb25ffcea0..b313f213822f0fd5ba6448f6f6f= 453cfda4c7e23 100644 --- a/drivers/xen/efi.c +++ b/drivers/xen/efi.c @@ -26,6 +26,7 @@ =20 #include #include +#include #include #include =20 @@ -40,6 +41,37 @@ =20 #define efi_data(op) (op.u.efi_runtime_call) =20 +static_assert(XEN_PAGE_SHIFT =3D=3D EFI_PAGE_SHIFT, + "Mismatch between EFI_PAGE_SHIFT and XEN_PAGE_SHIFT"); + +bool xen_efi_mem_info_query(u64 phys_addr, struct xen_efi_mem_info *md) +{ + struct xen_platform_op op =3D { + .cmd =3D XENPF_firmware_info, + .u.firmware_info =3D { + .type =3D XEN_FW_EFI_INFO, + .index =3D XEN_FW_EFI_MEM_INFO, + .u.efi_info.mem.addr =3D phys_addr, + .u.efi_info.mem.size =3D ((u64)-1ULL) - phys_addr, + } + }; + union xenpf_efi_info *info =3D &op.u.firmware_info.u.efi_info; + int rc; + + memset(md, 0, sizeof(*md)); /* initialize md even on failure */ + rc =3D HYPERVISOR_platform_op(&op); + if (rc) { + pr_warn("Could not obtain information on address %llu from Xen: " + "error %d\n", phys_addr, rc); + return false; + } + md->addr =3D info->mem.addr; + md->size =3D info->mem.size; + md->attr =3D info->mem.attr; + md->type =3D info->mem.type; + return true; +} + static efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) { struct xen_platform_op op =3D INIT_EFI_OP(get_time); diff --git a/include/linux/efi.h b/include/linux/efi.h index d2b84c2fec39f0268324d1a38a73ed67786973c9..0598869cdc924aef0e2b9cacc44= 50b728e1a98c7 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1327,1 +1327,19 @@ struct linux_efi_coco_secret_area { +/* Result of a XEN_FW_EFI_MEM_INFO query */ +struct xen_efi_mem_info { + uint64_t addr; /* address queried */ + uint64_t size; /* remaining bytes in memory region */ + uint64_t attr; /* attributes */ + uint32_t type; /* type */ +}; + +#if IS_ENABLED(CONFIG_XEN_EFI) +extern bool xen_efi_mem_info_query(u64 phys_addr, struct xen_efi_mem_info = *out_md); +#else +static inline bool xen_efi_mem_info_query(u64 phys_addr, struct xen_efi_me= m_info *out_md) +{ + BUILD_BUG(); + return false; +} +#endif + #endif /* _LINUX_EFI_H */ --=20 Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab