From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by mx.groups.io with SMTP id smtpd.web09.23543.1654838647515966894 for ; Thu, 09 Jun 2022 22:24:07 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=MXeE/Mu9; spf=pass (domain: gmail.com, ip: 209.85.210.169, mailfrom: ayushdevel1325@gmail.com) Received: by mail-pf1-f169.google.com with SMTP id e11so22913641pfj.5 for ; Thu, 09 Jun 2022 22:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:subject:to:cc:message-id:in-reply-to:references :mime-version; bh=XudlVuWDuhSC6QYeYQM052xhcAclwpQlVQgccLJ7aes=; b=MXeE/Mu9mML3vFn3MC0mLtiDVMFBcvm6YcLXiL47+MFm8LkYzOvEy8kd/ENfSvKG7H y+l8QzXmKSoZvBHfP0cI3I8IvlwCQt6dlF6Lg5b/z9Ka8LSzcLg1h52RDgc7fawa5Wod bAe+2/HNjvx5hQDhN4Gsht2O7wr/URvIlkVSdJFnZNWVHe4ZmnWkGem8lFqIXMUCl5Hu 0ApiH1D3TYjwr8qTarCgeM75Zc/IliqvkQ57g/3lP9lBo7RxzgA6jRnhPCXMGICNL/zD Xcf785ikK+acgq3K+fMtONlLhChIuG3gQOAJAD/jGsDKj8dkPx2NwVXTcJfohgr/jrYP jlUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:subject:to:cc:message-id:in-reply-to :references:mime-version; bh=XudlVuWDuhSC6QYeYQM052xhcAclwpQlVQgccLJ7aes=; b=VZLPsIxXO4fX7GqkopxnSkH49tCjWToW8xfExfdmPd68wknM+4rwRJIEhoEsW0D9i7 Uujd7AIAew6jg0ORK++HaJAME9+UKLAFQSl/ZW/FZ8UK1NbsiUotVKBnAivSxIGLnHt1 BLwlhJ5LP82lUd6wnVOINd+qHpGrgSGTWT4RTZdFzB8CEzbsVqIIZFLoAgDBm8ilsfle O+aRian+R3X8rfaB3SUIX3N7l0HWpvNpb0f6Pv8WA/QjvLkWS00Es4bEF6H6AYdH6HJs Gekqe3eVI9ogryFIxFxlv/07MF5PE+ICIFGsQLr7BeDvKI7oOg92tTE0TsTVLcjJFFxJ VcmA== X-Gm-Message-State: AOAM533DZ1LtEXZNJJpnjMaXurn/laBuOKk9cUe3ZyAgsMd9EBMzI34X xVplyoxPQdcfVYyEP1ySQEQ= X-Google-Smtp-Source: ABdhPJwTwCXgV3uhBuI6F15e1R9J5BqFRei6kLGsDAxqpdb0L4Sa12T2WVEi9RZtGTioZE6ofDyiVA== X-Received: by 2002:a63:8942:0:b0:3fc:ffa6:378c with SMTP id v63-20020a638942000000b003fcffa6378cmr34652615pgd.305.1654838646867; Thu, 09 Jun 2022 22:24:06 -0700 (PDT) Return-Path: Received: from laptopfedora ([2401:4900:1f3e:100:f45d:8c38:7c9e:a43e]) by smtp.gmail.com with ESMTPSA id co23-20020a17090afe9700b001ea808094fcsm326735pjb.49.2022.06.09.22.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 22:24:06 -0700 (PDT) Date: Fri, 10 Jun 2022 10:52:42 +0530 From: "Ayush Singh" Subject: Re: [edk2-devel] Clarification of Memory management in PEI phase To: Andrew Fish Cc: devel@edk2.groups.io Message-Id: In-Reply-To: <500949D4-FFB9-4BC1-8655-430BC788765A@apple.com> References: <500949D4-FFB9-4BC1-8655-430BC788765A@apple.com> X-Mailer: geary/40.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=-tP8bGomRuTCR8qFa6+er" --=-tP8bGomRuTCR8qFa6+er Content-Type: text/plain; charset=iso-8859-13; format=flowed Content-Transfer-Encoding: quoted-printable Thanks for the wonderful answer. Ayush Singh On Thu, Jun 9 2022 at 01:26:58 PM -0700, Andrew Fish =20 wrote: >=20 >> 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=20 >> 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() >=20 > This is basically: > (*PeiServices)->InstallPeiMemory (PeiServices, MemoryBegin,=20 > MemoryLength); >=20 > This is how you tell the PEI Core the location of the memory that=20 > 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 >=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=20 > the main job of PEI is to initialize DRAM, and deal with S3 (resuming=20 > from suspend to RAM). So as soon as you have DRAM you are kind done=20 > and ready for the DXE IPL so you can load the DXE Phase and start up=20 > EFI. Remember PEI is Pre EFI. The reality is programming DRAM is=20 > complex and lots of code got written, then lots more code got written=20 > and PEI has become large for some ports. That was never the intent.=20 > PEI is designed as a way to run C code when you code is running from=20 > ROM and you don=E2��t have any DRAM. For x86 not having DRA= M means you=20 > are using the cache as RAM. For some SoCs there is actually an SRAM=20 > you can use. Thus the PEI memory allocation scheme is designed to=20 > deal with this very constrained environment. >=20 > You start PEI with a heap and stack. You can also allocate HOBs (Hand=20 > Off Blocks). A pool allocation in PEI is just a HOB. See [1]. There=20 > is no way to free a HOB. So the AllocatePool() kind of leaks into DXE=20 > too as an entry in the HOB list. But when the OS called=20 > gBS->ExitBootServices() that frees all non runtime memory back to the=20 > OS. >=20 > If you look a AllocatePages/FreePages you will see AllocatePages=20 > creates a HOB that points to the memory region, and FreePages just=20 > 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]=20 > >=20 >=20 > Thanks, >=20 > Andrew Fish >=20 >> Ayush Singh >>=20 >>=20 >> =20 >>=20 >>=20 >=20 --=-tP8bGomRuTCR8qFa6+er Content-Type: text/html; charset=iso-8859-13 Content-Transfer-Encoding: quoted-printable
Thanks for the wonderful answer.&n= bsp;

Ayush Singh

On Thu, Jun 9 2022 at 01:26:58 PM -0700, Andrew Fish <= ;afish@apple.com> wrote:
On Jun 9, 2022, at 10:28 AM, Ayush Singh <ayushdevel1325@gmail.com> 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��t have any DRAM. For= x86 not having DRAM means you are using the cache as RAM. For some SoCs th= ere is actually an SRAM you can use. Thus the PEI memory allocation scheme = is designed 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 free= s all 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/blo= b/master/MdeModulePkg/Core/Pei/Memory/MemoryServices.c#L878 Thanks, Andrew Fish
Ayush Singh =20 =20 =20 =20 =20
--=-tP8bGomRuTCR8qFa6+er--