PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches

Physical Memory Manager (PMM). More...

Collaboration diagram for PMM:

Detailed Description

Physical Memory Manager (PMM).

The Physical Memory Manager (PMM) is responsible for allocating and freeing physical memory pages.

The Free Page Stack

For most allocations, the PMM uses a fast O(1) stack-based allocator to manage free pages, with the limitation that only single pages can be allocated or freed at a time.

The Bitmap Allocator

For larger, contiguous or aligned allocations, the PMM uses a bitmap allocator. This allocator is slower (O(n)) but is usefull for more specialized allocations.

Reference Counting

All allocations from the PMM are referenced counted, meaning that a page is only freed when its reference count reaches zero. This allows pages to be passed around between subsystems without fear of double frees or use-after-frees.

See also
kernel_mem_mem_desc

Data Structures

struct  page_t
 Page metadata structure. More...
 
struct  page_stack_t
 Stored in free pages to form a stack of free pages. More...
 

Macros

#define FREE_PAGE_MAX   (PAGE_SIZE / sizeof(pfn_t) - 1)
 Maximum number of free pages that can be stored in a free page.
 

Functions

void pmm_init (void)
 Read the boot info memory map and initialize the PMM.
 
pfn_t pmm_alloc (void)
 Allocate a single page of physical memory.
 
uint64_t pmm_alloc_pages (pfn_t *pfns, size_t count)
 Allocate multiple pages of physical memory.
 
pfn_t pmm_alloc_bitmap (size_t count, pfn_t maxPfn, pfn_t alignPfn)
 Allocate a contiguous region of physical memory using the bitmap.
 
void pmm_free (pfn_t pfn)
 Free a single page of physical memory.
 
void pmm_free_pages (pfn_t *pfns, size_t count)
 Free multiple pages of physical memory.
 
void pmm_free_region (pfn_t pfn, size_t count)
 Free a contiguous region of physical memory.
 
uint64_t pmm_ref_inc (pfn_t pfn, size_t count)
 Increment the reference count of a physical region.
 
static void pmm_ref_dec (pfn_t pfn, size_t count)
 Decrement the reference count of a physical region.
 
size_t pmm_total_pages (void)
 Get the total number of physical pages.
 
size_t pmm_avail_pages (void)
 Get the number of available physical pages.
 
size_t pmm_used_pages (void)
 Get the number of used physical pages.
 

Macro Definition Documentation

◆ FREE_PAGE_MAX

#define FREE_PAGE_MAX   (PAGE_SIZE / sizeof(pfn_t) - 1)

Maximum number of free pages that can be stored in a free page.

Definition at line 49 of file pmm.h.

Function Documentation

◆ pmm_init()

void pmm_init ( void  )

Read the boot info memory map and initialize the PMM.

Definition at line 239 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_alloc()

pfn_t pmm_alloc ( void  )

Allocate a single page of physical memory.

Will by default use the free stack allocator, but if no pages are available there, it will fall back to the bitmap allocator.

Returns
On success, the PFN of the allocated page. On failure, ERR.

Definition at line 249 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_alloc_pages()

uint64_t pmm_alloc_pages ( pfn_t pfns,
size_t  count 
)

Allocate multiple pages of physical memory.

Usefull for reducing overhead from locking when allocating many pages.

Parameters
pfnsArray to store the allocated page PFNs.
countNumber of pages to allocate.
Returns
On success, 0. On failure, ERR and no pages are allocated.

Definition at line 275 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_alloc_bitmap()

pfn_t pmm_alloc_bitmap ( size_t  count,
pfn_t  maxPfn,
pfn_t  alignPfn 
)

Allocate a contiguous region of physical memory using the bitmap.

Parameters
countNumber of pages to allocate.
maxPfnMaximum PFN to allocate up to (exclusive).
alignPfnAlignment of the region in pages.
Returns
On success, the PFN of the first page of the allocated region. On failure, ERR.

Definition at line 317 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_free()

void pmm_free ( pfn_t  pfn)

Free a single page of physical memory.

The page will only be reclaimed if its reference count reaches zero.

Parameters
pfnThe PFN of the page to free.

Definition at line 341 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_free_pages()

void pmm_free_pages ( pfn_t pfns,
size_t  count 
)

Free multiple pages of physical memory.

Useful for reducing overhead from locking when freeing many pages.

The pages will only be reclaimed if its reference count reaches zero.

Parameters
pfnsArray of PFNs to free.
countNumber of pages to free.

Definition at line 348 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_free_region()

void pmm_free_region ( pfn_t  pfn,
size_t  count 
)

Free a contiguous region of physical memory.

The pages will only be reclaimed if its reference count reaches zero.

Parameters
pfnThe PFN of the first page of the region to free.
countNumber of pages to free.

Definition at line 358 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_ref_inc()

uint64_t pmm_ref_inc ( pfn_t  pfn,
size_t  count 
)

Increment the reference count of a physical region.

Will fail if any of the pages are not allocated.

Parameters
pfnThe PFN of the first physical page.
countNumber of pages to increment the reference count of.
Returns
On success, the new reference count. On failure, ERR.

Definition at line 365 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_ref_dec()

static void pmm_ref_dec ( pfn_t  pfn,
size_t  count 
)
inlinestatic

Decrement the reference count of a physical region.

If the reference count reaches zero, the pages will be freed.

Parameters
pfnThe PFN of the first physical page.
countNumber of pages to decrement the reference count of.

Definition at line 149 of file pmm.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_total_pages()

size_t pmm_total_pages ( void  )

Get the total number of physical pages.

Returns
Total number of physical pages.

Definition at line 387 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_avail_pages()

size_t pmm_avail_pages ( void  )

Get the number of available physical pages.

Returns
Number of available physical pages.

Definition at line 395 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_used_pages()

size_t pmm_used_pages ( void  )

Get the number of used physical pages.

Returns
Number of used physical pages.

Definition at line 403 of file pmm.c.

Here is the call graph for this function:
Here is the caller graph for this function: