MdeModulePkg: Moved CoreAllocatePoolPagesI() and CoreFreePoolPagesI() to Page.c.

This commit is contained in:
Mikhail Krichanov 2024-02-27 14:43:20 +03:00
parent 241d60775b
commit abdfba8835
3 changed files with 110 additions and 182 deletions

View File

@ -2173,6 +2173,81 @@ Done:
return Status; return Status;
} }
/**
Internal function. Used by the pool functions to allocate pages
to back pool allocation requests.
@param PoolType The type of memory for the new pool pages
@param NoPages No of pages to allocate
@param Granularity Bits to align.
@param NeedGuard Flag to indicate Guard page is needed or not
@return The allocated memory, or NULL
**/
VOID *
CoreAllocatePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN NoPages,
IN UINTN Granularity,
IN BOOLEAN NeedGuard
)
{
VOID *Buffer;
EFI_STATUS Status;
Status = CoreAcquireLockOrFail (&gMemoryLock);
if (EFI_ERROR (Status)) {
return NULL;
}
Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity, NeedGuard);
CoreReleaseMemoryLock ();
if (Buffer != NULL) {
if (NeedGuard) {
SetGuardForMemory ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, NoPages);
}
ApplyMemoryProtectionPolicy (
EfiConventionalMemory,
PoolType,
(EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
EFI_PAGES_TO_SIZE (NoPages)
);
}
return Buffer;
}
/**
Internal function. Frees pool pages allocated via CoreAllocatePoolPagesI().
@param PoolType The type of memory for the pool pages
@param Memory The base address to free
@param NoPages The number of pages to free
**/
VOID
CoreFreePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NoPages
)
{
CoreAcquireMemoryLock ();
CoreFreePoolPages (Memory, NoPages);
CoreReleaseMemoryLock ();
GuardFreedPagesChecked (Memory, NoPages);
ApplyMemoryProtectionPolicy (
PoolType,
EfiConventionalMemory,
(EFI_PHYSICAL_ADDRESS)(UINTN)Memory,
EFI_PAGES_TO_SIZE (NoPages)
);
}
/** /**
Internal function. Used by the pool functions to allocate pages Internal function. Used by the pool functions to allocate pages
to back pool allocation requests. to back pool allocation requests.

View File

@ -10,7 +10,6 @@
#define _INTERNAL_POOL_H_ #define _INTERNAL_POOL_H_
extern BOOLEAN mOnGuarding; extern BOOLEAN mOnGuarding;
extern EFI_LOCK gMemoryLock;
#define GUARD_HEAP_TYPE_FREED BIT4 #define GUARD_HEAP_TYPE_FREED BIT4
@ -103,73 +102,6 @@ InstallMemoryAttributesTableOnMemoryAllocation (
IN EFI_MEMORY_TYPE MemoryType IN EFI_MEMORY_TYPE MemoryType
); );
/**
Internal function. Used by the pool functions to allocate pages
to back pool allocation requests.
@param PoolType The type of memory for the new pool pages
@param NumberOfPages No of pages to allocate
@param Alignment Bits to align.
@param NeedGuard Flag to indicate Guard page is needed or not
@return The allocated memory, or NULL
**/
VOID *
CoreAllocatePoolPages (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN NumberOfPages,
IN UINTN Alignment,
IN BOOLEAN NeedGuard
);
/**
Exit critical section by releasing lock on gMemoryLock.
**/
VOID
CoreReleaseMemoryLock (
VOID
);
/**
Set head Guard and tail Guard for the given memory range.
@param[in] Memory Base address of memory to set guard for.
@param[in] NumberOfPages Memory size in pages.
@return VOID.
**/
VOID
SetGuardForMemory (
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NumberOfPages
);
/**
Manage memory permission attributes on a memory range, according to the
configured DXE memory protection policy.
@param OldType The old memory type of the range
@param NewType The new memory type of the range
@param Memory The base address of the range
@param Length The size of the range (in bytes)
@return EFI_SUCCESS If the the CPU arch protocol is not installed yet
@return EFI_SUCCESS If no DXE memory protection policy has been configured
@return EFI_SUCCESS If OldType and NewType use the same permission attributes
@return other Return value of gCpu->SetMemoryAttributes()
**/
EFI_STATUS
EFIAPI
ApplyMemoryProtectionPolicy (
IN EFI_MEMORY_TYPE OldType,
IN EFI_MEMORY_TYPE NewType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINT64 Length
);
/** /**
Check to see if the heap guard is enabled for page and/or pool allocation. Check to see if the heap guard is enabled for page and/or pool allocation.
@ -214,43 +146,6 @@ CoreAcquireLock (
IN EFI_LOCK *Lock IN EFI_LOCK *Lock
); );
/**
Enter critical section by gaining lock on gMemoryLock.
**/
VOID
CoreAcquireMemoryLock (
VOID
);
/**
Internal function. Frees pool pages allocated via AllocatePoolPages ()
@param Memory The base address to free
@param NumberOfPages The number of pages to free
**/
VOID
CoreFreePoolPages (
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NumberOfPages
);
/**
Record freed pages as well as mark them as not-present, if enabled.
@param[in] BaseAddress Base address of just freed pages.
@param[in] Pages Number of freed pages.
@return VOID.
**/
VOID
EFIAPI
GuardFreedPagesChecked (
IN EFI_PHYSICAL_ADDRESS BaseAddress,
IN UINTN Pages
);
/** /**
Adjust the start address and number of pages to free according to Guard. Adjust the start address and number of pages to free according to Guard.
@ -314,4 +209,39 @@ AdjustPoolHeadF (
IN UINTN Size IN UINTN Size
); );
/**
Internal function. Used by the pool functions to allocate pages
to back pool allocation requests.
@param PoolType The type of memory for the new pool pages
@param NoPages No of pages to allocate
@param Granularity Bits to align.
@param NeedGuard Flag to indicate Guard page is needed or not
@return The allocated memory, or NULL
**/
VOID *
CoreAllocatePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN NoPages,
IN UINTN Granularity,
IN BOOLEAN NeedGuard
);
/**
Internal function. Frees pool pages allocated via CoreAllocatePoolPagesI().
@param PoolType The type of memory for the pool pages
@param Memory The base address to free
@param NoPages The number of pages to free
**/
VOID
CoreFreePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NoPages
);
#endif #endif

View File

@ -294,54 +294,6 @@ CoreAllocatePool (
return Status; return Status;
} }
/**
Internal function. Used by the pool functions to allocate pages
to back pool allocation requests.
@param PoolType The type of memory for the new pool pages
@param NoPages No of pages to allocate
@param Granularity Bits to align.
@param NeedGuard Flag to indicate Guard page is needed or not
@return The allocated memory, or NULL
**/
STATIC
VOID *
CoreAllocatePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN UINTN NoPages,
IN UINTN Granularity,
IN BOOLEAN NeedGuard
)
{
VOID *Buffer;
EFI_STATUS Status;
Status = CoreAcquireLockOrFail (&gMemoryLock);
if (EFI_ERROR (Status)) {
return NULL;
}
Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity, NeedGuard);
CoreReleaseMemoryLock ();
if (Buffer != NULL) {
if (NeedGuard) {
SetGuardForMemory ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, NoPages);
}
ApplyMemoryProtectionPolicy (
EfiConventionalMemory,
PoolType,
(EFI_PHYSICAL_ADDRESS)(UINTN)Buffer,
EFI_PAGES_TO_SIZE (NoPages)
);
}
return Buffer;
}
/** /**
Internal function to allocate pool of a particular type. Internal function to allocate pool of a particular type.
Caller must have the memory lock held Caller must have the memory lock held
@ -617,35 +569,6 @@ CoreFreePool (
return Status; return Status;
} }
/**
Internal function. Frees pool pages allocated via CoreAllocatePoolPagesI().
@param PoolType The type of memory for the pool pages
@param Memory The base address to free
@param NoPages The number of pages to free
**/
STATIC
VOID
CoreFreePoolPagesI (
IN EFI_MEMORY_TYPE PoolType,
IN EFI_PHYSICAL_ADDRESS Memory,
IN UINTN NoPages
)
{
CoreAcquireMemoryLock ();
CoreFreePoolPages (Memory, NoPages);
CoreReleaseMemoryLock ();
GuardFreedPagesChecked (Memory, NoPages);
ApplyMemoryProtectionPolicy (
PoolType,
EfiConventionalMemory,
(EFI_PHYSICAL_ADDRESS)(UINTN)Memory,
EFI_PAGES_TO_SIZE (NoPages)
);
}
/** /**
Internal function. Frees guarded pool pages. Internal function. Frees guarded pool pages.