PatchworkOS
Loading...
Searching...
No Matches

Physical Memory Manager (PMM). More...

Functions

void pmm_init (const boot_memory_map_t *map)
 Initializes the Physical Memory Manager.
 
void * pmm_alloc (void)
 Allocates a single physical page.
 
uint64_t pmm_alloc_pages (void **addresses, uint64_t count)
 Allocates multiple physical pages.
 
void * pmm_alloc_bitmap (uint64_t count, uintptr_t maxAddr, uint64_t alignment)
 Allocates a contiguous region of physical pages managed by the bitmap.
 
void pmm_free (void *address)
 Frees a single physical page.
 
void pmm_free_pages (void **addresses, uint64_t count)
 Frees multiple physical pages.
 
void pmm_free_region (void *address, uint64_t count)
 Frees a contiguous region of physical pages.
 
uint64_t pmm_total_amount (void)
 Retrieves the total amount of physical memory managed by the PMM.
 
uint64_t pmm_free_amount (void)
 Retrieves the amount of free physical memory.
 
uint64_t pmm_reserved_amount (void)
 Retrieves the amount of reserved physical memory.
 

Detailed Description

Physical Memory Manager (PMM).

The Physical Memory Manager (PMM) is responsible for managing physical memory pages. It uses a free stack for allocating single pages in constant-time, and for more specialized allocations (requiring a specific address range or alignment) a bitmap allocator is used. The bitmap allocator should only be used when no other option is available.

All physical memory is identity mapped to the beginning of the higher half of the address space. This means that for example NULL is always an invalid address and that the PMM returns addresses in the higher half.

The free stack provides some advantages over for instance a free list, mainly due to cache improvements.

Function Documentation

◆ pmm_alloc()

void * pmm_alloc ( void  )

Allocates a single physical page.

The returned page will not be zeroed.

Returns
On success, returns the higher half physical address of the allocated page. On failure, returns NULL.

Definition at line 162 of file pmm.c.

References address, ENOMEM, errno, pmm_stack_t::free, lock, LOCK_SCOPE, LOG_WARN, NULL, pmm_stack_alloc(), and stack.

Referenced by pipe_open(), pipe_open2(), shmem_object_allocate_pages(), simd_ctx_init(), space_populate_user_region(), and trampoline_init().

◆ pmm_alloc_bitmap()

void * pmm_alloc_bitmap ( uint64_t  count,
uintptr_t  maxAddr,
uint64_t  alignment 
)

Allocates a contiguous region of physical pages managed by the bitmap.

The pmm_alloc_bitmap function allocates a contiguous block of count physical pages from the memory region managed by the bitmap. It also enforces a maximum address and alignment for the allocation.

The returned pages will not be zeroed.

Parameters
countThe number of contiguous pages to allocate.
maxAddrThe maximum physical address (exclusive) for the allocation.
alignmentThe required alignment for the allocated region, in bytes.
Returns
On success, returns the higher half physical address of the allocated region. On failure, returns NULL.

Definition at line 198 of file pmm.c.

References address, bitmap, count, ENOMEM, errno, pmm_bitmap_t::free, lock, LOCK_SCOPE, LOG_WARN, NULL, and pmm_bitmap_alloc().

Referenced by space_pmm_bitmap_alloc_pages().

◆ pmm_alloc_pages()

uint64_t pmm_alloc_pages ( void **  addresses,
uint64_t  count 
)

Allocates multiple physical pages.

The pmm_alloc_pages function allocates count non-contiguous physical pages from the free stack, by using this function its possible to avoid holding the lock for each page allocation, improving performance when allocating many pages at once.

The returned pages will not be zeroed.

Parameters
addressesAn array where the higher half physical addresses of the allocated pages will be stored.
countThe number of pages to allocate.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 175 of file pmm.c.

References address, count, ENOMEM, ERR, errno, pmm_stack_t::free, lock, LOCK_SCOPE, LOG_WARN, NULL, pmm_stack_alloc(), pmm_stack_free(), and stack.

Referenced by space_init(), and vmm_alloc().

◆ pmm_free()

void pmm_free ( void *  address)

Frees a single physical page.

The pmm_free function frees a page returning ownership of it to the PMM. The PMM will determine based on the address if it's owned by the bitmap or the free stack.

Parameters
addressThe higher half physical address of the page to free.

Definition at line 211 of file pmm.c.

References address, lock, LOCK_SCOPE, and pmm_free_unlocked().

Referenced by pipe_close(), shmem_object_allocate_pages(), shmem_object_free(), simd_ctx_deinit(), space_pmm_bitmap_alloc_pages(), space_populate_user_region(), and trampoline_deinit().

◆ pmm_free_amount()

uint64_t pmm_free_amount ( void  )

Retrieves the amount of free physical memory.

Returns
The amount of currently free physical memory in pages.

Definition at line 238 of file pmm.c.

References bitmap, pmm_bitmap_t::free, pmm_stack_t::free, lock, LOCK_SCOPE, and stack.

Referenced by panic(), pmm_load_memory(), and statistics_mem_read().

◆ pmm_free_pages()

void pmm_free_pages ( void **  addresses,
uint64_t  count 
)

Frees multiple physical pages.

The pmm_free_pages function frees count physical pages returning ownership of them to the PMM. The PMM will determine based on the addresses if they're owned by the bitmap or the free stack.

Parameters
addressesAn array containing the higher half physical addresses of the pages to free.
countThe number of pages to free.

Definition at line 217 of file pmm.c.

References count, lock, LOCK_SCOPE, and pmm_free_unlocked().

Referenced by space_init().

◆ pmm_free_region()

void pmm_free_region ( void *  address,
uint64_t  count 
)

Frees a contiguous region of physical pages.

The pmm_free_region function frees a contiguous block of count physical pages, returning ownership of them to the PMM. The PMM will determine based on the address if it's owned by the bitmap or the free stack.

Parameters
addressThe higher half physical address of the first page in the region to free.
countThe number of pages to free.

Definition at line 226 of file pmm.c.

References address, count, lock, LOCK_SCOPE, and pmm_free_pages_unlocked().

Referenced by acpi_reclaim_memory(), and init_free_loader_data().

◆ pmm_init()

void pmm_init ( const boot_memory_map_t map)

Initializes the Physical Memory Manager.

Parameters
mapThe EFI memory map provided by the bootloader.

Definition at line 152 of file pmm.c.

References bitmap, CONFIG_PMM_BITMAP_MAX_ADDR, map, mapBuffer, pmm_bitmap_init(), PMM_BITMAP_SIZE, pmm_detect_memory(), pmm_load_memory(), pmm_stack_init(), and stack.

Referenced by init_early().

◆ pmm_reserved_amount()

uint64_t pmm_reserved_amount ( void  )

Retrieves the amount of reserved physical memory.

Reserved memory includes memory that is not available for allocation (e.g., kernel code, hardware regions).

Returns
The amount of reserved physical memory in pages.

Definition at line 244 of file pmm.c.

References bitmap, pmm_bitmap_t::free, pmm_stack_t::free, lock, LOCK_SCOPE, pageAmount, and stack.

Referenced by init_finalize(), panic(), and statistics_mem_read().

◆ pmm_total_amount()

uint64_t pmm_total_amount ( void  )

Retrieves the total amount of physical memory managed by the PMM.

Returns
The total amount of physical memory in pages.

Definition at line 232 of file pmm.c.

References lock, LOCK_SCOPE, and pageAmount.

Referenced by statistics_mem_read().