The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pmm.c File Reference
#include "pmm.h"
#include "pmm_layout.h"
#include "region.h"
#include "kernel/panic.h"
#include "kernel/sync/spinlock.h"
#include "common/memory.h"
#include "common/stdio.h"

Functions

void pmm_init (boot_info_t *boot)
 physical memory manager: a bitmap of 4 KiB page frames.
void * pmm_alloc_page (void)
 allocate a single 4 KiB physical page frame.
void * pmm_alloc_contiguous (uint32_t count)
 allocate a run of physically contiguous 4 KiB page frames.
void pmm_free_page (void *address)
 drop one reference to a page frame.
void pmm_page_ref (void *address)
 add a reference to an allocated page frame, so that it stays allocated until pmm_free_page has been called once more.
uint32_t pmm_page_refcount (void *address)
 read the reference count of a page frame.
uint32_t pmm_free_count (void)
 return the number of free page frames available for allocation.
uint32_t pmm_total_count (void)
 return the total number of page frames managed by the PMM.
bool pmm_owns (phys_addr_t phys)
 check whether a physical address falls within the RAM the PMM manages.
void pmm_mark_region_used (phys_addr_t start, phys_addr_t end)
 mark all page frames within a physical address range as used.
bool pmm_is_page_free (uint32_t page_index)
 check whether a specific page frame is free.
bool pmm_claim_pages (uint32_t first_page, uint32_t count)
 claim a run of specific page frames, only if every one of them is free.

Function Documentation

◆ pmm_init()

void pmm_init ( boot_info_t * boot)

physical memory manager: a bitmap of 4 KiB page frames.

every function is safe to call from preemptible kernel code: the bitmap and counters are guarded by a spinlock that masks interrupts, so a process switch can never land in the middle of an update. initialize the physical memory manager. must be called after pmm_layout_init() and all memory reservations. walks the E820 map and marks usable, non-reserved pages as free.

Parameters
bootpointer to boot info containing the E820 memory map.

◆ pmm_alloc_page()

void * pmm_alloc_page ( void )

allocate a single 4 KiB physical page frame.

Returns
direct-map address of the page (one reference), or NULL on failure.

◆ pmm_alloc_contiguous()

void * pmm_alloc_contiguous ( uint32_t count)

allocate a run of physically contiguous 4 KiB page frames.

pages are freed individually with pmm_free_page().

Parameters
countnumber of pages (must be > 0).
Returns
a kernel pointer (direct map) to the first page, or NULL if no free run of that length exists.

◆ pmm_free_page()

void pmm_free_page ( void * address)

drop one reference to a page frame.

the frame becomes free when its last reference goes. a frame from an allocation has one reference.

Parameters
addressdirect-map address of the page (must be page-aligned).

◆ pmm_page_ref()

void pmm_page_ref ( void * address)

add a reference to an allocated page frame, so that it stays allocated until pmm_free_page has been called once more.

used to share a frame between address spaces (copy-on-write).

Parameters
addressdirect-map address of the page (must be page-aligned).

◆ pmm_page_refcount()

uint32_t pmm_page_refcount ( void * address)

read the reference count of a page frame.

Parameters
addressdirect-map address of the page (must be page-aligned).
Returns
the number of references, 0 for a free or reserved frame.

◆ pmm_free_count()

uint32_t pmm_free_count ( void )

return the number of free page frames available for allocation.

Returns
the number of free page frames.

◆ pmm_total_count()

uint32_t pmm_total_count ( void )

return the total number of page frames managed by the PMM.

Returns
the total number of page frames.

◆ pmm_owns()

bool pmm_owns ( phys_addr_t phys)

check whether a physical address falls within the RAM the PMM manages.

device memory (e.g. a linear framebuffer mapped high) is not owned by the PMM, so it must not be handed to pmm_free_page. address-space teardown uses this to skip freeing such mappings.

Parameters
physthe physical address to test.
Returns
true if the address is a PMM-managed frame, false otherwise.

◆ pmm_mark_region_used()

void pmm_mark_region_used ( phys_addr_t start,
phys_addr_t end )

mark all page frames within a physical address range as used.

used by subsystems that claim memory after pmm_init() (e.g., kernel heap).

Parameters
startfirst byte of the region (inclusive, page-aligned).
endfirst byte past the region (exclusive, page-aligned).

◆ pmm_is_page_free()

bool pmm_is_page_free ( uint32_t page_index)

check whether a specific page frame is free.

the answer can be stale as soon as it returns; to take pages, use pmm_claim_pages().

Parameters
page_indexindex of the page frame to check.
Returns
true if the page is free, false if used or out of range.

◆ pmm_claim_pages()

bool pmm_claim_pages ( uint32_t first_page,
uint32_t count )

claim a run of specific page frames, only if every one of them is free.

the check and the claim are one atomic step.

Parameters
first_pageindex of the first page frame.
countnumber of page frames.
Returns
true if all pages were free and are now used; false (nothing claimed) otherwise.