From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 0D168AC0E0C for ; Mon, 5 Feb 2024 08:19:09 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=JuUK5Sd7qU29m46DiDeE3C2HfQ6HrheWg8hgPtmh1A8=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1707121148; v=1; b=rWJi/m1KVC1bq1JKsGyhJCn6Zedw+v26HTd034XIHVS/l2K6DgAGqogbyPqSiTJoiDFqJ7NK LByEACAANcAh7B0WSwmpVjEVEGqbyAITWDkrdfcExOFF9PMe6YSCu5A9/yg48MPCAVzLepeK5aG w3D+DciZUdsyN85X0xA/bB/M= X-Received: by 127.0.0.2 with SMTP id L7YCYY7687511xaMGzGTkXre; Mon, 05 Feb 2024 00:19:08 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.groups.io with SMTP id smtpd.web11.58343.1707121147935766832 for ; Mon, 05 Feb 2024 00:19:08 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-1gkxw61ENEqmVMFm98Pk0Q-1; Mon, 05 Feb 2024 03:19:03 -0500 X-MC-Unique: 1gkxw61ENEqmVMFm98Pk0Q-1 X-Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5CCE283B82B; Mon, 5 Feb 2024 08:19:03 +0000 (UTC) X-Received: from [10.39.192.216] (unknown [10.39.192.216]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6AC30492BC6; Mon, 5 Feb 2024 08:19:02 +0000 (UTC) Message-ID: <17d3e527-4a6b-be34-2cdb-ccf1841ee717@redhat.com> Date: Mon, 5 Feb 2024 09:19:01 +0100 MIME-Version: 1.0 Subject: Re: [edk2-devel] [PATCH v2 3/4] OvmfPkg/PlatformPei: rewrite page table calculation From: "Laszlo Ersek" To: devel@edk2.groups.io, kraxel@redhat.com Cc: Jiewen Yao , Oliver Steffen , Ard Biesheuvel References: <20240202104720.1275308-1-kraxel@redhat.com> <20240202104720.1275308-4-kraxel@redhat.com> <6a5ece37-2164-e207-7779-6a1258c7445e@redhat.com> In-Reply-To: <6a5ece37-2164-e207-7779-6a1258c7445e@redhat.com> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,lersek@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 3vaGW54LGttJBxfYqJ8AcfvIx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b="rWJi/m1K"; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io On 2/5/24 09:14, Laszlo Ersek wrote: > On 2/2/24 11:47, Gerd Hoffmann wrote: >> Consider 5-level paging. Simplify calculation to make it easier >> to understand. Add some comments, improve ASSERTs. >> >> Signed-off-by: Gerd Hoffmann >> --- >> OvmfPkg/PlatformPei/MemDetect.c | 56 ++++++++++++++++++++------------- >> 1 file changed, 35 insertions(+), 21 deletions(-) >> >> diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDe= tect.c >> index 83f1c1d02a26..48ad0b83a55e 100644 >> --- a/OvmfPkg/PlatformPei/MemDetect.c >> +++ b/OvmfPkg/PlatformPei/MemDetect.c >> @@ -184,9 +184,12 @@ GetPeiMemoryCap ( >> BOOLEAN Page1GSupport; >> UINT32 RegEax; >> UINT32 RegEdx; >> - UINT32 Pml4Entries; >> - UINT32 PdpEntries; >> - UINTN TotalPages; >> + UINT64 MaxAddr; >> + UINT32 Level5Pages; >> + UINT32 Level4Pages; >> + UINT32 Level3Pages; >> + UINT32 Level2Pages; >> + UINT32 TotalPages; >> UINT64 ApStacks; >> UINT64 MemoryCap; >> =20 >> @@ -203,8 +206,7 @@ GetPeiMemoryCap ( >> // >> // Dependent on physical address width, PEI memory allocations can be >> // dominated by the page tables built for 64-bit DXE. So we key the c= ap off >> - // of those. The code below is based on CreateIdentityMappingPageTabl= es() in >> - // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c". >> + // of those. >> // >> Page1GSupport =3D FALSE; >> if (PcdGetBool (PcdUse1GPageTable)) { >> @@ -217,25 +219,37 @@ GetPeiMemoryCap ( >> } >> } >> =20 >> - if (PlatformInfoHob->PhysMemAddressWidth <=3D 39) { >> - Pml4Entries =3D 1; >> - PdpEntries =3D 1 << (PlatformInfoHob->PhysMemAddressWidth - 30); >> - ASSERT (PdpEntries <=3D 0x200); >> + // >> + // - A 4KB page accommodates the least significant 12 bits of the >> + // virtual address. >> + // - A page table entry at any level consumes 8 bytes, so a 4KB page >> + // table page (at any level) contains 512 entries, and >> + // accommodates 9 bits of the virtual address. >> + // - we minimally cover the phys address space with 2MB pages, so >> + // level 1 never exists. >> + // - If 1G paging is available, then level 2 doesn't exist either. >> + // - Start with level 2, where a page table page accommodates >> + // 9 + 9 + 12 =3D 30 bits of the virtual address (and covers 1GB of >> + // physical address space). >> + // >> + >> + MaxAddr =3D LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth); >> + Level2Pages =3D (UINT32)RShiftU64 (MaxAddr, 30); >> + Level3Pages =3D MAX (Level2Pages >> 9, 1); >=20 > (1) The MAX() macro in "MdePkg/Include/Base.h" is documented like this: >=20 > /** > Return the maximum of two operands. >=20 > This macro returns the maximum of two operand specified by a and b. > Both a and b must be the same numerical types, signed or unsigned. >=20 > @param a The first operand with any numerical type. > @param b The second operand. Can be any numerical type as long= as is > the same type as a. >=20 > @return Maximum of two operands. >=20 > **/ >=20 > This does not hold to the above expression. (Level2Pages >> 9) is a UINT3= 2, but "1" is an INT32. Please use "1u". >=20 > (Note that the replacement text of the macro, namely (((a) > (b)) ? (a) := (b)), would work fine as-is, but we still should not break the documented = interface.) >=20 >> + Level4Pages =3D MAX (Level3Pages >> 9, 1); >=20 > (2) Same comment as above, plus another one: >=20 > (3) I'm slightly disturbed by the fact that here we don't shift the origi= nal MaxAddr by 48 bits, but Level3Pages by 9 bits. Namely, if Level3Pages w= as set to 1 by the MAX (i.e., because the >> 39 resulted in zero), then the= input of *this* bit shift is nonsensical. It's a happenstance that 1 >> 9 = is zero too, for Level4Pages, and we're just exploiting that practical resu= lt here. >=20 > I think this is semantically unclean, especially without a comment, but I= don't want to insist anymore. >=20 >> + Level5Pages =3D 1; >> + >> + if (Page1GSupport) { >> + Level2Pages =3D 0; >> + TotalPages =3D Level5Pages + Level4Pages + Level3Pages; >> + ASSERT (TotalPages <=3D 0x40201); >> } else { >> - if (PlatformInfoHob->PhysMemAddressWidth > 48) { >> - Pml4Entries =3D 0x200; >> - } else { >> - Pml4Entries =3D 1 << (PlatformInfoHob->PhysMemAddressWidth - 39); >> - } >> - >> - ASSERT (Pml4Entries <=3D 0x200); >> - PdpEntries =3D 512; >> + TotalPages =3D Level5Pages + Level4Pages + Level3Pages + Level2Page= s; >> + // PlatformAddressWidthFromCpuid() caps at 40 phys bits without 1G = pages. >> + ASSERT (PlatformInfoHob->PhysMemAddressWidth <=3D 40); >> + ASSERT (TotalPages <=3D 0x404); >> } >> =20 >> - TotalPages =3D Page1GSupport ? Pml4Entries + 1 : >> - (PdpEntries + 1) * Pml4Entries + 1; >> - ASSERT (TotalPages <=3D 0x40201); >> - >> // >> // With 32k stacks and 4096 vcpus this lands at 128 MB (far away >> // from MAX_UINT32). >=20 > With (1) and (2) updated (feel free to ignore (3)): >=20 > Reviewed-by: Laszlo Ersek >=20 Sorry, just noticed: (4) after this patch, TotalPages is UINT32, but in the subsequent context, = we have: MemoryCap =3D EFI_PAGES_TO_SIZE (TotalPages) + ApStacks + SIZE_64MB; Per "MdePkg/Include/Uefi/UefiBaseType.h": /** Macro that converts a number of EFI_PAGEs to a size in bytes. @param Pages The number of EFI_PAGES. This parameter is assumed to = be type UINTN. Passing in a parameter that is larger than UINTN may produce unexpected results. @return The number of bytes associated with the number of EFI_PAGEs spec= ified by Pages. **/ So in this patch, please also cast TotalPages to UINTN inside the argument = of EFI_PAGES_TO_SIZE(). Thanks Laszlo -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#115105): https://edk2.groups.io/g/devel/message/115105 Mute This Topic: https://groups.io/mt/104117101/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-