|
PatchworkOS
19e446b
A non-POSIX operating system.
|
Physical Memory Manager (PMM). More...
Physical Memory Manager (PMM).
The Physical Memory Manager (PMM) is responsible for allocating and freeing physical memory pages.
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.
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.
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.
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. | |
| void pmm_init | ( | void | ) |
| 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.
ERR. Definition at line 249 of file pmm.c.
Allocate multiple pages of physical memory.
Usefull for reducing overhead from locking when allocating many pages.
| pfns | Array to store the allocated page PFNs. |
| count | Number of pages to allocate. |
0. On failure, ERR and no pages are allocated. Definition at line 275 of file pmm.c.
Allocate a contiguous region of physical memory using the bitmap.
| count | Number of pages to allocate. |
| maxPfn | Maximum PFN to allocate up to (exclusive). |
| alignPfn | Alignment of the region in pages. |
ERR. Definition at line 317 of file pmm.c.
| void pmm_free | ( | pfn_t | pfn | ) |
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.
| pfns | Array of PFNs to free. |
| count | Number of pages to free. |
Definition at line 348 of file pmm.c.
Free a contiguous region of physical memory.
The pages will only be reclaimed if its reference count reaches zero.
| pfn | The PFN of the first page of the region to free. |
| count | Number of pages to free. |
Definition at line 358 of file pmm.c.
Increment the reference count of a physical region.
Will fail if any of the pages are not allocated.
| pfn | The PFN of the first physical page. |
| count | Number of pages to increment the reference count of. |
ERR. Definition at line 365 of file pmm.c.
Decrement the reference count of a physical region.
If the reference count reaches zero, the pages will be freed.
| pfn | The PFN of the first physical page. |
| count | Number of pages to decrement the reference count of. |
Definition at line 149 of file pmm.h.
| size_t pmm_total_pages | ( | void | ) |
| size_t pmm_avail_pages | ( | void | ) |