From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from rn-mailsvcp-ppex-lapp15.apple.com (rn-mailsvcp-ppex-lapp15.apple.com [17.179.253.34]) by mx.groups.io with SMTP id smtpd.web11.18630.1654806420841540119 for ; Thu, 09 Jun 2022 13:27:00 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=MSuOWZYp; spf=pass (domain: apple.com, ip: 17.179.253.34, mailfrom: afish@apple.com) Received: from pps.filterd (rn-mailsvcp-ppex-lapp15.rno.apple.com [127.0.0.1]) by rn-mailsvcp-ppex-lapp15.rno.apple.com (8.16.1.2/8.16.1.2) with SMTP id 259KJwG3011913; Thu, 9 Jun 2022 13:27:00 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=from : content-type : content-transfer-encoding : mime-version : subject : date : references : to : in-reply-to : message-id; s=20180706; bh=zq9Dqt8SoUN9AK/MwE88Tfavi0aowxUhmDx7JMCZ+A4=; b=MSuOWZYplU1/IrVewbefPIv8FbTeTV8xUdvDcBpNC9yw7tIU08DQ/YfO7odBwfcacIUG t6xxSYcMQguLuEloBNlXNph1gVZ53L6JyoidLWHFq0MP1ZtlJW2ugR1kB0bAMgG0S/Vx 2XVSr7R3JrGWJ+wti8gxPPRfhsDM1LJBMf0rUqr5C1Dhe3D/tUASB4/HHWzh3Hi7Jl/W JWz0Q0PEARt0PIYxbYklzDSDVR/2uQbmP23IC8eDmhB5oKqo1vIGAd80JJNf4TfI+/mi DXmniMpsCsqNdMVd9m6crRr9ygW6MWxgpV0MfkSNdv95eltZR4K3RZ/eZyN+ejq5w7uO oQ== Received: from rn-mailsvcp-mta-lapp04.rno.apple.com (rn-mailsvcp-mta-lapp04.rno.apple.com [10.225.203.152]) by rn-mailsvcp-ppex-lapp15.rno.apple.com with ESMTP id 3gg4ydj098-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Thu, 09 Jun 2022 13:27:00 -0700 Received: from rn-mailsvcp-mmp-lapp01.rno.apple.com (rn-mailsvcp-mmp-lapp01.rno.apple.com [17.179.253.14]) by rn-mailsvcp-mta-lapp04.rno.apple.com (Oracle Communications Messaging Server 8.1.0.18.20220407 64bit (built Apr 7 2022)) with ESMTPS id <0RD800ZH98T0XB60@rn-mailsvcp-mta-lapp04.rno.apple.com>; Thu, 09 Jun 2022 13:27:00 -0700 (PDT) Received: from process_milters-daemon.rn-mailsvcp-mmp-lapp01.rno.apple.com by rn-mailsvcp-mmp-lapp01.rno.apple.com (Oracle Communications Messaging Server 8.1.0.18.20220407 64bit (built Apr 7 2022)) id <0RD800O008QBD900@rn-mailsvcp-mmp-lapp01.rno.apple.com>; Thu, 09 Jun 2022 13:26:59 -0700 (PDT) X-Va-A: X-Va-T-CD: c365ff6c1366ae51b74a8a4e717d3d9d X-Va-E-CD: a883af46aa02bf8091f2a6262c209ba2 X-Va-R-CD: 15da91710467abb124c65b2f9a04ce9d X-Va-CD: 0 X-Va-ID: 57c55b75-9098-40a6-ac17-b0f49c105bcf X-V-A: X-V-T-CD: c365ff6c1366ae51b74a8a4e717d3d9d X-V-E-CD: a883af46aa02bf8091f2a6262c209ba2 X-V-R-CD: 15da91710467abb124c65b2f9a04ce9d X-V-CD: 0 X-V-ID: 315e8b58-efbb-4c33-9afa-128f36430e97 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-09_15:2022-06-09,2022-06-09 signatures=0 Received: from smtpclient.apple (unknown [17.235.37.220]) by rn-mailsvcp-mmp-lapp01.rno.apple.com (Oracle Communications Messaging Server 8.1.0.18.20220407 64bit (built Apr 7 2022)) with ESMTPSA id <0RD800SJK8SY3J00@rn-mailsvcp-mmp-lapp01.rno.apple.com>; Thu, 09 Jun 2022 13:26:59 -0700 (PDT) From: "Andrew Fish" MIME-version: 1.0 (Mac OS X Mail 15.0 \(3693.20.0.1.32\)) Subject: Re: [edk2-devel] Clarification of Memory management in PEI phase Date: Thu, 09 Jun 2022 13:26:58 -0700 References: To: devel@edk2.groups.io, ayushdevel1325@gmail.com In-reply-to: Message-id: <500949D4-FFB9-4BC1-8655-430BC788765A@apple.com> X-Mailer: Apple Mail (2.3693.20.0.1.32) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.874 definitions=2022-06-09_15:2022-06-09,2022-06-09 signatures=0 Content-type: text/plain; charset=utf-8 Content-transfer-encoding: quoted-printable > On Jun 9, 2022, at 10:28 AM, Ayush Singh wrote= : >=20 > Hello everyone, >=20 > 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: >=20 > 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.=20 > 2. AllocatePages() > 3. AllocatePool() > 4. CopyMem() > 5. SetMem() > 6. FreePages() >=20 > However, no `FreePool()` service seems to be present. So how is the > memory allocated using `AllocatePool()` freed? >=20 It basically gets Freed when you transition to the DXE phase.=20 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 DX= E IPL so you can load the DXE Phase and start up EFI. Remember PEI is Pre E= FI. 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=E2=80=99t have any DRAM. For x86 not h= aving DRAM means you are using the cache as RAM. For some SoCs there is act= ually an SRAM you can use. Thus the PEI memory allocation scheme is designe= d to deal with this very constrained environment.=20 You start PEI with a heap and stack. You can also allocate HOBs (Hand Off B= locks). 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 i= n the HOB list. But when the OS called gBS->ExitBootServices() that frees a= ll non runtime memory back to the OS.=20 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].=20 TL;DR there is no pool manager in PEI.=20 [1] https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Core/Pei/Mem= ory/MemoryServices.c#L878 Thanks, Andrew Fish > Ayush Singh >=20 >=20 >=20 >=20 >=20