EmulatorPkg/Host: Consume MemoryAllocationLib

This commit is contained in:
Marvin Häuser 2023-03-26 19:20:28 +02:00 committed by Mikhail Krichanov
parent 6cf6d8247a
commit e23139949b
4 changed files with 203 additions and 210 deletions

View File

@ -60,6 +60,7 @@
ThunkProtocolList
PpiListLib
PeiServicesLib
CommonMemoryAllocationLib
[Ppis]
gEfiPeiStatusCodePpiGuid # PPI ALWAYS_PRODUCED

View File

@ -6,19 +6,111 @@
**/
#include "Base.h"
#include "Library/BaseMemoryLib.h"
#include "Library/MemoryAllocationLib.h"
#include "Uefi.h"
#include "Library/PhaseMemoryAllocationLib.h"
#include <sys/mman.h>
#include <stdlib.h>
/**
Allocates a buffer of type EfiBootServicesData.
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData;
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode;
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
/**
Allocates one or more 4KB pages of a certain memory type.
Allocates the number of 4KB pages of a certain memory type and returns a pointer
to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
@param Type The type of allocation to perform.
@param MemoryType The type of memory to allocate.
@param Pages The number of 4 KB pages to allocate.
@param Memory The pointer to a physical address. On input, the
way in which the address is used depends on the
value of Type.
@retval EFI_SUCCESS The requested pages were allocated.
@retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
@retval EFI_NOT_FOUND The requested pages could not be found.
**/
EFI_STATUS
EFIAPI
PhaseAllocatePages (
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages,
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
UINTN DestSize;
VOID *Buffer;
DestSize = EFI_PAGES_TO_SIZE (Pages);
Buffer = mmap (
NULL,
DestSize,
PROT_EXEC | PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON,
-1,
0
);
if (Buffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*Memory = (UINTN)Buffer;
return EFI_SUCCESS;
}
/**
Frees one or more 4KB pages that were previously allocated with one of the page allocation
functions in the Memory Allocation Library.
Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.
Buffer must have been allocated on a previous call to the page allocation services
of the Memory Allocation Library. If it is not possible to free allocated pages,
then this function will perform no actions.
If Buffer was not allocated with a page allocation function in the Memory Allocation
Library, then ASSERT().
If Pages is zero, then ASSERT().
@param Memory The base physical address of the pages to be freed.
@param Pages The number of 4 KB pages to free.
@retval EFI_SUCCESS The requested pages were freed.
@retval EFI_NOT_FOUND The requested memory pages were not allocated with
PhaseAllocatePages().
**/
EFI_STATUS
EFIAPI
PhaseFreePages (
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN Pages
)
{
UINTN DestSize;
INT32 Result;
DestSize = EFI_PAGES_TO_SIZE (Pages);
Result = munmap ((VOID *)(UINTN)Memory, DestSize);
if (Result != 0) {
return EFI_NOT_FOUND;
}
return EFI_SUCCESS;
}
/**
Allocates a buffer of a certain pool type.
Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
@param MemoryType The type of memory to allocate.
@param AllocationSize The number of bytes to allocate.
@return A pointer to the allocated buffer or NULL if allocation fails.
@ -26,91 +118,14 @@
**/
VOID *
EFIAPI
AllocatePool (
IN UINTN AllocationSize
PhaseAllocatePool (
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN AllocationSize
)
{
return (VOID *)malloc (AllocationSize);
}
/**
Allocates and zeros a buffer of type EfiBootServicesData.
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
request, then NULL is returned.
@param AllocationSize The number of bytes to allocate and zero.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocateZeroPool (
IN UINTN AllocationSize
)
{
VOID *Buffer;
Buffer = AllocatePool (AllocationSize);
if (Buffer == NULL) {
return NULL;
}
ZeroMem (Buffer, AllocationSize);
return Buffer;
}
/**
Reallocates a buffer of type EfiBootServicesData.
Allocates and zeros the number bytes specified by NewSize from memory of type
EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
OldBuffer is freed. A pointer to the newly allocated buffer is returned.
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
enough memory remaining to satisfy the request, then NULL is returned.
If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
@param OldSize The size, in bytes, of OldBuffer.
@param NewSize The size, in bytes, of the buffer to reallocate.
@param OldBuffer The buffer to copy to the allocated buffer. This is an optional
parameter that may be NULL.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
ReallocatePool (
IN UINTN OldSize,
IN UINTN NewSize,
IN VOID *OldBuffer OPTIONAL
)
{
VOID *NewBuffer;
NewBuffer = AllocatePool (NewSize);
if (NewBuffer == NULL) {
return NULL;
}
if (OldBuffer != NULL) {
if (OldSize > 0) {
CopyMem (NewBuffer, OldBuffer, OldSize);
}
FreePool (OldBuffer);
}
return NewBuffer;
}
/**
Frees a buffer that was previously allocated with one of the pool allocation functions in the
Memory Allocation Library.
@ -122,12 +137,12 @@ ReallocatePool (
If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
then ASSERT().
@param Buffer Pointer to the buffer to free.
@param Buffer The pointer to the buffer to free.
**/
VOID
EFIAPI
FreePool (
PhaseFreePool (
IN VOID *Buffer
)
{

View File

@ -55,6 +55,7 @@
PpiListLib
PeiServicesLib
FrameBufferBltLib
CommonMemoryAllocationLib
[Ppis]
gEmuThunkPpiGuid

View File

@ -6,19 +6,106 @@
**/
#include <Base.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include "WinHost.h"
#include <Library/PhaseMemoryAllocationLib.h>
#include <stdlib.h>
/**
Allocates a buffer of type EfiBootServicesData.
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultDataType = EfiBootServicesData;
GLOBAL_REMOVE_IF_UNREFERENCED CONST EFI_MEMORY_TYPE gPhaseDefaultCodeType = EfiBootServicesCode;
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
/**
Allocates one or more 4KB pages of a certain memory type.
Allocates the number of 4KB pages of a certain memory type and returns a pointer
to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
@param Type The type of allocation to perform.
@param MemoryType The type of memory to allocate.
@param Pages The number of 4 KB pages to allocate.
@param Memory The pointer to a physical address. On input, the
way in which the address is used depends on the
value of Type.
@retval EFI_SUCCESS The requested pages were allocated.
@retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
@retval EFI_NOT_FOUND The requested pages could not be found.
**/
EFI_STATUS
EFIAPI
PhaseAllocatePages (
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages,
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
UINTN DestSize;
VOID *Buffer;
ASSERT (Type == AllocateAnyPages);
DestSize = EFI_PAGES_TO_SIZE (Pages);
Buffer = VirtualAlloc (NULL, (SIZE_T) DestSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (Buffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
*Memory = (UINTN)Buffer;
return EFI_SUCCESS;
}
/**
Frees one or more 4KB pages that were previously allocated with one of the page allocation
functions in the Memory Allocation Library.
Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.
Buffer must have been allocated on a previous call to the page allocation services
of the Memory Allocation Library. If it is not possible to free allocated pages,
then this function will perform no actions.
If Buffer was not allocated with a page allocation function in the Memory Allocation
Library, then ASSERT().
If Pages is zero, then ASSERT().
@param Memory The base physical address of the pages to be freed.
@param Pages The number of 4 KB pages to free.
@retval EFI_SUCCESS The requested pages were freed.
@retval EFI_NOT_FOUND The requested memory pages were not allocated with
PhaseAllocatePages().
**/
EFI_STATUS
EFIAPI
PhaseFreePages (
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN Pages
)
{
UINTN DestSize;
INT32 Success;
DestSize = EFI_PAGES_TO_SIZE (Pages);
Success = VirtualFree ((VOID *)(UINTN)Memory, (SIZE_T) DestSize, MEM_DECOMMIT);
if (!Success) {
return EFI_NOT_FOUND;
}
return EFI_SUCCESS;
}
/**
Allocates a buffer of a certain pool type.
Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
@param MemoryType The type of memory to allocate.
@param AllocationSize The number of bytes to allocate.
@return A pointer to the allocated buffer or NULL if allocation fails.
@ -26,125 +113,14 @@
**/
VOID *
EFIAPI
AllocatePool (
IN UINTN AllocationSize
PhaseAllocatePool (
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN AllocationSize
)
{
return (VOID *)malloc (AllocationSize);
}
/**
Allocates and zeros a buffer of type EfiBootServicesData.
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
request, then NULL is returned.
@param AllocationSize The number of bytes to allocate and zero.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocateZeroPool (
IN UINTN AllocationSize
)
{
VOID *Buffer;
Buffer = AllocatePool (AllocationSize);
if (Buffer == NULL) {
return NULL;
}
ZeroMem (Buffer, AllocationSize);
return Buffer;
}
/**
Reallocates a buffer of type EfiBootServicesData.
Allocates and zeros the number bytes specified by NewSize from memory of type
EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
OldBuffer is freed. A pointer to the newly allocated buffer is returned.
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
enough memory remaining to satisfy the request, then NULL is returned.
If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
@param OldSize The size, in bytes, of OldBuffer.
@param NewSize The size, in bytes, of the buffer to reallocate.
@param OldBuffer The buffer to copy to the allocated buffer. This is an optional
parameter that may be NULL.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
ReallocatePool (
IN UINTN OldSize,
IN UINTN NewSize,
IN VOID *OldBuffer OPTIONAL
)
{
VOID *NewBuffer;
NewBuffer = AllocatePool (NewSize);
if (NewBuffer == NULL) {
return NULL;
}
if (OldBuffer != NULL) {
if (OldSize > 0) {
CopyMem (NewBuffer, OldBuffer, OldSize);
}
FreePool (OldBuffer);
}
return NewBuffer;
}
/**
Copies a buffer to an allocated buffer of type EfiBootServicesData.
Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
is not enough memory remaining to satisfy the request, then NULL is returned.
If Buffer is NULL, then ASSERT().
If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
@param AllocationSize The number of bytes to allocate and zero.
@param Buffer The buffer to copy to the allocated buffer.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocateCopyPool (
IN UINTN AllocationSize,
IN CONST VOID *Buffer
)
{
VOID *Memory;
Memory = AllocatePool (AllocationSize);
if (Memory != NULL) {
Memory = CopyMem (Memory, Buffer, AllocationSize);
}
return Memory;
}
/**
Frees a buffer that was previously allocated with one of the pool allocation functions in the
Memory Allocation Library.
@ -156,12 +132,12 @@ AllocateCopyPool (
If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
then ASSERT().
@param Buffer Pointer to the buffer to free.
@param Buffer The pointer to the buffer to free.
**/
VOID
EFIAPI
FreePool (
PhaseFreePool (
IN VOID *Buffer
)
{