From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web10.16804.1676301514769873645 for ; Mon, 13 Feb 2023 07:18:34 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Vfwk/LNR; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4610961176; Mon, 13 Feb 2023 15:18:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14EB3C4339B; Mon, 13 Feb 2023 15:18:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1676301513; bh=AN4l/0FDwIHKGofFu143x6zVYicApYdvC3vk3gx5vyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vfwk/LNROZyq2BWMLtKnUgxj7veWed2q2QmvW8ajBQqJ/LPe1YiW5GGoJz4Ot30sD GZOM354Xb2XOXCX5mR6L1/uFa7hmhFxVGmIEFHZ0moVGicJGxtu/hZokLF+44PQI2Z Xj3MkNLupHUz0qQfmhAcciDDthY4s/03h/QIEPlKRj2guBK5PtqFtTt1mey9QVEVhp twOOIX5n1vCJIgurURSo1b0I+WOEJKneTbEgFLWECm4EuQmB8YJFYSWu+pvZckKNax AWgxOPJorvdsLVX7HXf15PTHiUwZY5ZSXvZFVx8YkfmhJ4zUl0heIlnaD76hznNCZk uPLT77EgKXhFw== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , Matthew Garrett , Peter Jones , Kees Cook Subject: [RFC 02/13] ArmPkg/ArmMmuLib: Introduce region types for RO/XP WB cached memory Date: Mon, 13 Feb 2023 16:17:59 +0100 Message-Id: <20230213151810.2301480-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230213151810.2301480-1-ardb@kernel.org> References: <20230213151810.2301480-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable To prepare for the enablement of booting EFI with the SCTLR.WXN control enabled, which makes all writeable memory regions non-executable by default, introduce a memory type that we will use to describe the flash region that carries the SEC and PEIM modules that execute in place. Even if these are implicitly read-only due to the ROM nature, they need to be mapped with read-only attributes in the page tables to be able to execute from them. Also add the XP counterpart which will be used for all normal DRAM right at the outset. Signed-off-by: Ard Biesheuvel --- ArmPkg/Include/Library/ArmLib.h | 6 ++++ ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c | 29 ++++++++++++++++---- ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibCore.c | 16 +++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/ArmPkg/Include/Library/ArmLib.h b/ArmPkg/Include/Library/ArmLi= b.h index a53f60d98852..fb1ae57b3522 100644 --- a/ArmPkg/Include/Library/ArmLib.h +++ b/ArmPkg/Include/Library/ArmLib.h @@ -35,6 +35,12 @@ typedef enum { // Do NOT use below two attributes if you are not sure.=0D ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_NONSHAREABLE,=0D =0D + // Special region types for memory that must be mapped with read-only or= =0D + // non-execute permissions from the very start, e.g., to support the use= =0D + // of the WXN virtual memory control.=0D + ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_RO,=0D + ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_XP,=0D +=0D ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH,=0D ARM_MEMORY_REGION_ATTRIBUTE_DEVICE,=0D } ARM_MEMORY_REGION_ATTRIBUTES;=0D diff --git a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c b/ArmPkg/Libr= ary/ArmMmuLib/AArch64/ArmMmuLibCore.c index ee4c5c995ce8..9cdaa8b32c62 100644 --- a/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c +++ b/ArmPkg/Library/ArmMmuLib/AArch64/ArmMmuLibCore.c @@ -37,12 +37,33 @@ ArmMemoryAttributeToPageAttribute ( IN ARM_MEMORY_REGION_ATTRIBUTES Attributes=0D )=0D {=0D + UINT64 Permissions =3D 0;=0D +=0D + switch (Attributes) {=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_RO:=0D + Permissions =3D TT_AP_NO_RO;=0D + break;=0D +=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_XP:=0D + case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:=0D + if (ArmReadCurrentEL () =3D=3D AARCH64_EL2) {=0D + Permissions =3D TT_XN_MASK;=0D + } else {=0D + Permissions =3D TT_UXN_MASK | TT_PXN_MASK;=0D + }=0D + break;=0D + default:=0D + break;=0D + }=0D +=0D switch (Attributes) {=0D case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_NONSHAREABLE:=0D return TT_ATTR_INDX_MEMORY_WRITE_BACK;=0D =0D case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK:=0D - return TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE;=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_RO:=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_XP:=0D + return TT_ATTR_INDX_MEMORY_WRITE_BACK | TT_SH_INNER_SHAREABLE | Perm= issions;=0D =0D case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:=0D return TT_ATTR_INDX_MEMORY_WRITE_THROUGH | TT_SH_INNER_SHAREABLE;=0D @@ -54,11 +75,7 @@ ArmMemoryAttributeToPageAttribute ( default:=0D ASSERT (0);=0D case ARM_MEMORY_REGION_ATTRIBUTE_DEVICE:=0D - if (ArmReadCurrentEL () =3D=3D AARCH64_EL2) {=0D - return TT_ATTR_INDX_DEVICE_MEMORY | TT_XN_MASK;=0D - } else {=0D - return TT_ATTR_INDX_DEVICE_MEMORY | TT_UXN_MASK | TT_PXN_MASK;=0D - }=0D + return TT_ATTR_INDX_DEVICE_MEMORY | Permissions;=0D }=0D }=0D =0D diff --git a/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibCore.c b/ArmPkg/Library/= ArmMmuLib/Arm/ArmMmuLibCore.c index 154298357460..00c5f42cd91a 100644 --- a/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibCore.c +++ b/ArmPkg/Library/ArmMmuLib/Arm/ArmMmuLibCore.c @@ -106,6 +106,14 @@ PopulateLevel2PageTable ( PageAttributes =3D TT_DESCRIPTOR_PAGE_WRITE_BACK;=0D PageAttributes &=3D ~TT_DESCRIPTOR_PAGE_S_SHARED;=0D break;=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_RO:=0D + PageAttributes =3D TT_DESCRIPTOR_PAGE_WRITE_BACK;=0D + PageAttributes |=3D TT_DESCRIPTOR_PAGE_AP_NO_RO;=0D + break;=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_XP:=0D + PageAttributes =3D TT_DESCRIPTOR_PAGE_WRITE_BACK;=0D + PageAttributes |=3D TT_DESCRIPTOR_PAGE_XN_MASK;=0D + break;=0D case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:=0D PageAttributes =3D TT_DESCRIPTOR_PAGE_WRITE_THROUGH;=0D break;=0D @@ -240,6 +248,14 @@ FillTranslationTable ( Attributes =3D TT_DESCRIPTOR_SECTION_WRITE_BACK;=0D Attributes &=3D ~TT_DESCRIPTOR_SECTION_S_SHARED;=0D break;=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_RO:=0D + Attributes =3D TT_DESCRIPTOR_SECTION_WRITE_BACK;=0D + Attributes |=3D TT_DESCRIPTOR_SECTION_AP_NO_RO;=0D + break;=0D + case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK_XP:=0D + Attributes =3D TT_DESCRIPTOR_SECTION_WRITE_BACK;=0D + Attributes |=3D TT_DESCRIPTOR_SECTION_XN_MASK;=0D + break;=0D case ARM_MEMORY_REGION_ATTRIBUTE_WRITE_THROUGH:=0D Attributes =3D TT_DESCRIPTOR_SECTION_WRITE_THROUGH;=0D break;=0D --=20 2.39.1