From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web10.68444.1675722075988001796 for ; Mon, 06 Feb 2023 14:21:16 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pBhGUd7o; spf=pass (domain: kernel.org, ip: 145.40.68.75, 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 ams.source.kernel.org (Postfix) with ESMTPS id 721D2B81630; Mon, 6 Feb 2023 22:21:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB469C4339C; Mon, 6 Feb 2023 22:21:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675722073; bh=1quc6OfBcSCLc8SNz3M9yWHGbLwcd3YOupMO/u4uWSM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pBhGUd7oc9tUyf3v38A6XYfO94cCetXK3KeHqytkExrFemQV88YONeD75zUAuFhvR DT8MZqcA8SxXqD2sZNLEnwUgczYGbsWnIel+QF/0DKIfq/FwmxkfqX5pO8aamLNyfJ f2k0MA0ksO26CoSASsNU5/i7BI0gRuR6jsc3lqh1pC+/+E7KKAKfZPNzcq6KwUD04O LOdbdXIh/szB69yuOid5SDHYjYCNITQPkemXW71btFDzXNd0OU4cpe4wce8Ow2p8Ln VSkfZmPQLnYa1/CgP8EIqT3DvJ0hu+q8hCQYX1m+bb7tauy4qAE1aVt5zcBnjR2A3S MtfZBcX1MzSjw== 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 Subject: [PATCH v3 2/5] ArmPkg/CpuDxe ARM: Fix page-to-section attribute conversion Date: Mon, 6 Feb 2023 23:20:57 +0100 Message-Id: <20230206222100.411169-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230206222100.411169-1-ardb@kernel.org> References: <20230206222100.411169-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The section-to-page attribute conversion takes the shareability and execute-never attributes into account, whereas the page-to-section counterpart does not. The result is that GetMemoryRegionPage () -which takes a section attribute argument (via *RegionAttributes) that is ostensibly based on the first page in the range, but differs from the actual page attributes when converted back- may return with a RegionLength of zero. This is incorrect, and confuses code that scans a region by calling GetMemoryRegion () in sequence. So fix the conversion, and ASSERT () on a non-zero region length. Signed-off-by: Ard Biesheuvel --- ArmPkg/Drivers/CpuDxe/Arm/Mmu.c | 3 +++ ArmPkg/Include/Chipset/ArmV7Mmu.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c b/ArmPkg/Drivers/CpuDxe/Arm/Mm= u.c index 2daf47ba6fe5..e7acd84b8af9 100644 --- a/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c +++ b/ArmPkg/Drivers/CpuDxe/Arm/Mmu.c @@ -480,6 +480,8 @@ GetMemoryRegion ( =0D PageAttributes =3D PageTable[PageTableIndex] & TT_DESCRIPTOR_PAGE_A= TTRIBUTE_MASK;=0D *RegionAttributes =3D TT_DESCRIPTOR_CONVERT_TO_SECTION_CACHE_POLICY (P= ageAttributes, 0) |=0D + TT_DESCRIPTOR_CONVERT_TO_SECTION_S (PageAttributes= ) |=0D + TT_DESCRIPTOR_CONVERT_TO_SECTION_XN (PageAttribute= s) |=0D TT_DESCRIPTOR_CONVERT_TO_SECTION_AP (PageAttribute= s);=0D }=0D =0D @@ -494,6 +496,7 @@ GetMemoryRegion ( =0D // Scan the page table to find the end of the region.=0D Status =3D GetMemoryRegionPage (PageTable, BaseAddress, RegionLength= , RegionAttributes);=0D + ASSERT (*RegionLength > 0);=0D =0D // If we have found the end of the region (Status =3D=3D EFI_SUCCESS= ) then we exit the for-loop=0D if (Status =3D=3D EFI_SUCCESS) {=0D diff --git a/ArmPkg/Include/Chipset/ArmV7Mmu.h b/ArmPkg/Include/Chipset/Arm= V7Mmu.h index db99527d6efa..4f51041e29ed 100644 --- a/ArmPkg/Include/Chipset/ArmV7Mmu.h +++ b/ArmPkg/Include/Chipset/ArmV7Mmu.h @@ -144,6 +144,8 @@ (((((D= esc) & (0x3 << 12)) >> 6) | (Desc & (0x3 << 2)))))=0D =0D #define TT_DESCRIPTOR_CONVERT_TO_SECTION_AP(Desc) ((((Desc) & TT_DESCRIPT= OR_PAGE_AP_MASK) << 6) & TT_DESCRIPTOR_SECTION_AP_MASK)=0D +#define TT_DESCRIPTOR_CONVERT_TO_SECTION_S(Desc) ((((Desc) & TT_DESCRIPT= OR_PAGE_S_MASK) << 6) & TT_DESCRIPTOR_SECTION_S_MASK)=0D +#define TT_DESCRIPTOR_CONVERT_TO_SECTION_XN(Desc) ((((Desc) & TT_DESCRIPT= OR_PAGE_XN_MASK) << 4) & TT_DESCRIPTOR_SECTION_XN_MASK)=0D =0D #define TT_DESCRIPTOR_CONVERT_TO_SECTION_CACHE_POLICY(Desc, IsLargePage) = (IsLargePage? \=0D (((Des= c) & TT_DESCRIPTOR_LARGEPAGE_CACHE_POLICY_MASK) & TT_DESCRIPTOR_SECTION_CAC= HE_POLICY_MASK): \=0D --=20 2.39.1