[edk2-devel] Clarification of Memory management in PEI phase

Nate DeSimone nathaniel.l.desimone at intel.com
Wed Jun 22 22:58:24 UTC 2022


Hi Ayush,

For your work to make Rust run in PEI I would recommend writing a generic heap manager that uses the PEI services AllocatePage() and FreePage(). PEI does allow you to truly free up memory but only when allocated in 4KB increments. Your heap manager can allow the Rust program to go to a smaller granularity.

In parallel, I can see merit in the argument for adding proper heap management to PEI, but that would be a PI specification update that is way outside the scope of your GSoC project and won’t happen fast enough for the work you need to do this summer 😊.

Thanks,
Nate

From: devel at edk2.groups.io <devel at edk2.groups.io> On Behalf Of Ayush Singh
Sent: Thursday, June 9, 2022 10:23 PM
To: Andrew Fish <afish at apple.com>
Cc: devel at edk2.groups.io
Subject: Re: [edk2-devel] Clarification of Memory management in PEI phase

Thanks for the wonderful answer.

Ayush Singh

On Thu, Jun 9 2022 at 01:26:58 PM -0700, Andrew Fish <afish at apple.com<mailto:afish at apple.com>> wrote:

On Jun 9, 2022, at 10:28 AM, Ayush Singh <ayushdevel1325 at gmail.com<mailto:ayushdevel1325 at gmail.com>> wrote: Hello everyone, Can anyone help me with understanding dynamic memory management in PEI phase? In the UEFI Platform Integration Specification, version 1.7 Errata A, Section 4.6, PEI Memory services are given which include: 1. InstallPeiMemory()
This is basically: (*PeiServices)->InstallPeiMemory (PeiServices, MemoryBegin, MemoryLength); This is how you tell the PEI Core the location of the memory that will can be used in PEI.
2. AllocatePages() 3. AllocatePool() 4. CopyMem() 5. SetMem() 6. FreePages() However, no `FreePool()` service seems to be present. So how is the memory allocated using `AllocatePool()` freed?
It basically gets Freed when you transition to the DXE phase. To step back for a minute I think it is important to remember that the main job of PEI is to initialize DRAM, and deal with S3 (resuming from suspend to RAM). So as soon as you have DRAM you are kind done and ready for the DXE IPL so you can load the DXE Phase and start up EFI. Remember PEI is Pre EFI. The reality is programming DRAM is complex and lots of code got written, then lots more code got written and PEI has become large for some ports. That was never the intent. PEI is designed as a way to run C code when you code is running from ROM and you donā��t have any DRAM. For x86 not having DRAM means you are using the cache as RAM. For some SoCs there is actually an SRAM you can use. Thus the PEI memory allocation scheme is designed to deal with this very constrained environment. You start PEI with a heap and stack. You can also allocate HOBs (Hand Off Blocks). A pool allocation in PEI is just a HOB. See [1]. There is no way to free a HOB. So the AllocatePool() kind of leaks into DXE too as an entry in the HOB list. But when the OS called gBS->ExitBootServices() that frees all non runtime memory back to the OS. If you look a AllocatePages/FreePages you will see AllocatePages creates a HOB that points to the memory region, and FreePages just marks that HOB as not used. That code is also in this file [1]. TL;DR there is no pool manager in PEI. [1] https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878 Thanks, Andrew Fish
Ayush Singh



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#90702): https://edk2.groups.io/g/devel/message/90702
Mute This Topic: https://groups.io/mt/91651322/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/edk2-devel-archive/attachments/20220622/fc4221c0/attachment-0001.htm>


More information about the edk2-devel-archive mailing list