> On Feb 16, 2016, at 12:29 AM, Laszlo Ersek <[email protected]> wrote:
> 
> On 02/16/16 07:26, Bhupesh Sharma wrote:
>> Hi Experts,
>> 
>> I have a doubt regarding memory sharing between PEI and DXE phases.
>> 
>> Let's say I have a PEI library 'NorLib.c' and a DXE driver 'NorDxeDriver.c', 
>> where
>> the DXE driver uses some APIs of the PEI Library to obtain information and 
>> provide
>> functionalities to the upper layers.
>> 

Are you trying to pass function pointers around? That seems like a bad idea. 
What if you PEIM got shadowed? That memory is not going to be around in DXE?

>> Now, if I allocate some memory chunk (let's say for a pointer) in the PEI 
>> phase,
>> how can I use the same in the DXE phase (as normally the PEI pointer 
>> contents become
>> INVALID in the DXE phase).
>> 
>> Are there standard HOB mechanisms available for the same?
>> 

I'm confused the HOBs are designed to make it possible to pass data around 
without having to use pointers. Actually the HOB Construction Rules forbid 
pointers? So just pass the data in the HOB, with no pointers. 

PI Spec Vol 3:
4.5.2 HOB Construction Rules

3. HOBs may be relocated in system memory by the HOB consumer phase. HOBs must 
not contain pointers to other data in the HOB list, including that in other 
HOBs. The table must be able to be copied without requiring internal pointer 
adjustment.


On X86 you can start PEI Phase in Cache, and then turn on memory so the address 
of the HOBs changes. Not to mention it is common for PEI to run in 32-vit mode 
and DXE to run in 64-bit mode. When you launch DXE the memory map starts over 
and the HOBs are copied into the new DXE memory map. So the HOBs could have  3 
different address ranges on a boot. So you just pass the data in HOBs. and 
don't use pointers. 

Thanks,

Andrew Fish

>> Thanks for the help.
> 
> In the PEI phase, use the MemoryAllocationLib API's to allocate memory.
> The library class header is:
> 
>  MdePkg/Include/Library/MemoryAllocationLib.h
> 
> and the library instance you should resolve the class to is
> 
>  MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAllocationLib.inf
> 
> The memory release functions will not ASSERT(), but they are no-ops. You
> can't free memory you allocate during PEI.
> 
> For each pool (i.e., non-pages) allocation, there is a size limit of a
> bit less than 64KB. The reason is that such allocations are internally
> handled with HOBs. If you need more memory, call AllocatePages().
> 
> The memory you allocate with AllocatePages() during PEI will survive
> in-place into DXE, and it will be correctly reflected in the UEFI memmap
> as well. HOBs will be migrated though, so you can't reference them from
> DXE *by address*.
> 
> So, you have two options:
> - If the data is small, create a vendor GUID HOB with the data in it, in
> PEI. In DXE, look up the HOB by GUID.
> - If the data is large, use AllocatePages() -- or one of its friends --
> in PEI, then stash the address in a small vendor GUID HOB.
> 
> For the second option, a dynamic PCD could be considered as well, for
> storing the base address of the allocated pages. However, storing the
> address in a dynamic PCD is in a sense a bit more restricted than
> storing the address in a vendor GUID HOB. For example, if you'd like to
> access the allocated area in a library instance that gets linked into
> the DXE core, then the dynamic PCD is a no-go.
> 
> Thanks
> Laszlo
> _______________________________________________
> edk2-devel mailing list
> [email protected]
> https://lists.01.org/mailman/listinfo/edk2-devel

_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to