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 C1280AC129A for ; Fri, 2 Feb 2024 10:47:41 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=O0QnfkwmMdHBfTELL4qCN285rzvzVsxm5nu2GGe4GnI=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1706870860; v=1; b=QvAxLQGgpN7e5cB/yxwomDdYVztl09oHolh5LQ9vizlcEAXPOV6VYtyH1sMF8FAu1IV3LHBF YsVhKqGoCl929z+jFAMFgldZr+E/UWe5OadTSnxLWiXPLyuADv9pRHy5yuduDastyERNs6zkuIR p22/+ntMG7yE1493IBgz3jRg= X-Received: by 127.0.0.2 with SMTP id aLaPYY7687511xtN9AAZB4tp; Fri, 02 Feb 2024 02:47:40 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web11.20039.1706870859557835374 for ; Fri, 02 Feb 2024 02:47:39 -0800 X-Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-442-c-21VpWBOVWJjLcOgj2s_w-1; Fri, 02 Feb 2024 05:47:37 -0500 X-MC-Unique: c-21VpWBOVWJjLcOgj2s_w-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 0F40A3C000BE; Fri, 2 Feb 2024 10:47:37 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.193.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB167492BC6; Fri, 2 Feb 2024 10:47:36 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id DF5A81800DE5; Fri, 2 Feb 2024 11:47:20 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Jiewen Yao , Laszlo Ersek , Gerd Hoffmann , Oliver Steffen , Ard Biesheuvel Subject: [edk2-devel] [PATCH v2 3/4] OvmfPkg/PlatformPei: rewrite page table calculation Date: Fri, 2 Feb 2024 11:47:19 +0100 Message-ID: <20240202104720.1275308-4-kraxel@redhat.com> In-Reply-To: <20240202104720.1275308-1-kraxel@redhat.com> References: <20240202104720.1275308-1-kraxel@redhat.com> MIME-Version: 1.0 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,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 7MAvbo09dARqJkrp1LItqzQcx7686176AA= Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=QvAxLQGg; 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 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/MemDetect.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; @@ -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 cap off - // of those. The code below is based on CreateIdentityMappingPageTables() in - // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c". + // of those. // Page1GSupport = FALSE; if (PcdGetBool (PcdUse1GPageTable)) { @@ -217,25 +219,37 @@ GetPeiMemoryCap ( } } - if (PlatformInfoHob->PhysMemAddressWidth <= 39) { - Pml4Entries = 1; - PdpEntries = 1 << (PlatformInfoHob->PhysMemAddressWidth - 30); - ASSERT (PdpEntries <= 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 = 30 bits of the virtual address (and covers 1GB of + // physical address space). + // + + MaxAddr = LShiftU64 (1, PlatformInfoHob->PhysMemAddressWidth); + Level2Pages = (UINT32)RShiftU64 (MaxAddr, 30); + Level3Pages = MAX (Level2Pages >> 9, 1); + Level4Pages = MAX (Level3Pages >> 9, 1); + Level5Pages = 1; + + if (Page1GSupport) { + Level2Pages = 0; + TotalPages = Level5Pages + Level4Pages + Level3Pages; + ASSERT (TotalPages <= 0x40201); } else { - if (PlatformInfoHob->PhysMemAddressWidth > 48) { - Pml4Entries = 0x200; - } else { - Pml4Entries = 1 << (PlatformInfoHob->PhysMemAddressWidth - 39); - } - - ASSERT (Pml4Entries <= 0x200); - PdpEntries = 512; + TotalPages = Level5Pages + Level4Pages + Level3Pages + Level2Pages; + // PlatformAddressWidthFromCpuid() caps at 40 phys bits without 1G pages. + ASSERT (PlatformInfoHob->PhysMemAddressWidth <= 40); + ASSERT (TotalPages <= 0x404); } - TotalPages = Page1GSupport ? Pml4Entries + 1 : - (PdpEntries + 1) * Pml4Entries + 1; - ASSERT (TotalPages <= 0x40201); - // // With 32k stacks and 4096 vcpus this lands at 128 MB (far away // from MAX_UINT32). -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#115039): https://edk2.groups.io/g/devel/message/115039 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] -=-=-=-=-=-=-=-=-=-=-=-