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 9E7CBAC172F for ; Wed, 31 Jan 2024 12:00:11 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=1T6HzbhW85kvi25MRY9uFBjSCT4Y5T0BYYoiVu4Mdx8=; 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=1706702410; v=1; b=OSDwTuaTaM/Tdj/qUb9vnjq4qKb8ku1n/3wZb5VlfWPjZP4utEHdw6a/Mesy0a0/ECQuZr4y z/YAlRk0mu5ys+vMuCu5hvjS68fM8g3lcg3FAbZqmSreccI4ZASyF0x8DrxfwQMyTMpy5asESIy HwYuME2nKmiTLSb6hF8/RvUA= X-Received: by 127.0.0.2 with SMTP id SBcRYY7687511xnt7xz82SsZ; Wed, 31 Jan 2024 04:00:10 -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.12222.1706702409445988546 for ; Wed, 31 Jan 2024 04:00:09 -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-454-AGXptIUcMdGHbK-NhITwbw-1; Wed, 31 Jan 2024 07:00:03 -0500 X-MC-Unique: AGXptIUcMdGHbK-NhITwbw-1 X-Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 33EF23CBDFA9; Wed, 31 Jan 2024 12:00:03 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.192.243]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 060C4492BE2; Wed, 31 Jan 2024 12:00:03 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 635FC1800DE9; Wed, 31 Jan 2024 13:00:00 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Oliver Steffen , Jiewen Yao , Laszlo Ersek , Ard Biesheuvel , Gerd Hoffmann Subject: [edk2-devel] [PATCH 2/3] OvmfPkg/PlatformPei: rewrite page table calculation Date: Wed, 31 Jan 2024 12:59:59 +0100 Message-ID: <20240131120000.358090-3-kraxel@redhat.com> In-Reply-To: <20240131120000.358090-1-kraxel@redhat.com> References: <20240131120000.358090-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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: nUGqp4HW5obGQUzGifaZKq4ax7686176AA= 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=OSDwTuaT; 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. The new calculation is not 100% exact, but we only need a rough estimate to reserve enough memory. Signed-off-by: Gerd Hoffmann --- OvmfPkg/PlatformPei/MemDetect.c | 42 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c index 338798b54171..e22743b4bfaa 100644 --- a/OvmfPkg/PlatformPei/MemDetect.c +++ b/OvmfPkg/PlatformPei/MemDetect.c @@ -184,8 +184,10 @@ GetPeiMemoryCap ( BOOLEAN Page1GSupport; UINT32 RegEax; UINT32 RegEdx; - UINT32 Pml4Entries; - UINT32 PdpEntries; + UINT32 Level5Pages; + UINT32 Level4Pages; + UINT32 Level3Pages; + UINT32 Level2Pages; UINTN TotalPages; UINTN ApStacks; UINTN MemoryCap; @@ -203,8 +205,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,23 +218,26 @@ GetPeiMemoryCap ( } } - if (PlatformInfoHob->PhysMemAddressWidth <= 39) { - Pml4Entries = 1; - PdpEntries = 1 << (PlatformInfoHob->PhysMemAddressWidth - 30); - ASSERT (PdpEntries <= 0x200); - } else { - if (PlatformInfoHob->PhysMemAddressWidth > 48) { - Pml4Entries = 0x200; - } else { - Pml4Entries = 1 << (PlatformInfoHob->PhysMemAddressWidth - 39); - } - - ASSERT (Pml4Entries <= 0x200); - PdpEntries = 512; + // + // This calculation doesn't return the *exact* number of page table + // pages needed. But we only need a rough estimate to reserve + // enough memory, being off by a few pages isn't much of problem. + // So keep the calculation simple and easy to understand. + // + // Page size is 4k, which needs 12 address bits. + // Page tables on all levels have 512 entries, mapping to 9 address bits. + // We use large pages (2M or 1G), so level 1 never exists. + // Start with level 2, where a page covers 12 + 9 + 9 = 30 address bits. + // + Level2Pages = (1 << (PlatformInfoHob->PhysMemAddressWidth - 30)) + 1; + Level3Pages = (Level2Pages >> 9) + 1; + Level4Pages = (Level3Pages >> 9) + 1; + Level5Pages = (Level4Pages >> 9) + 1; + if (Page1GSupport) { + Level2Pages = 0; } - TotalPages = Page1GSupport ? Pml4Entries + 1 : - (PdpEntries + 1) * Pml4Entries + 1; + TotalPages = Level5Pages + Level4Pages + Level3Pages + Level2Pages; ASSERT (TotalPages <= 0x40201); ApStacks = PlatformInfoHob->PcdCpuMaxLogicalProcessorNumber * PcdGet32 (PcdCpuApStackSize); -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#114884): https://edk2.groups.io/g/devel/message/114884 Mute This Topic: https://groups.io/mt/104073300/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-