miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
pmm.c File Reference
Include dependency graph for pmm.c:

Classes

struct  mb2_tag
struct  mb2_tag_mmap
struct  mb2_mmap_entry

Macros

#define MB2_TAG_MMAP   6
#define MULTIBOOT_MEMORY_AVAILABLE   1

Functions

static uint64_t align_up (uint64_t v, uint64_t align)
static void pmm_set_free (uint64_t frame_idx)
static void pmm_set_used (uint64_t frame_idx)
static void pmm_free_range (uint64_t base, uint64_t length)
void pmm_init (uint64_t mb_info_phys)
uint64_t pmm_alloc_frame (void)
void pmm_free_frame (uint64_t phys)
void pmm_ref_frame (uint64_t phys)
void pmm_unref_frame (uint64_t phys)
uint64_t pmm_alloc_contiguous (size_t n_frames)
void pmm_free_contiguous (uint64_t phys, size_t n_frames)
uint64_t pmm_free_count (void)
uint64_t pmm_total_count (void)

Variables

static uint8_tg_pmm_bitmap = NULL
static uint64_t g_max_frames = 0
static uint64_t g_bitmap_bytes = 0
static uint8_tg_pmm_refcount = NULL
static uint64_t g_refcount_bytes = 0
static uint64_t total_free = 0
static uint64_t total_frames = 0
static spinlock_t pmm_lock = SPINLOCK_INIT
uint64_t __image_end

Macro Definition Documentation

◆ MB2_TAG_MMAP

#define MB2_TAG_MMAP   6

◆ MULTIBOOT_MEMORY_AVAILABLE

#define MULTIBOOT_MEMORY_AVAILABLE   1

Function Documentation

◆ align_up()

uint64_t align_up ( uint64_t v,
uint64_t align )
inlinestatic
Here is the caller graph for this function:

◆ pmm_alloc_contiguous()

uint64_t pmm_alloc_contiguous ( size_t n_frames)

pmm_alloc_contiguous() - Allocate n_frames physically contiguous frames. @n_frames: Number of consecutive 4 KiB frames required (must be > 0).

Linear scan for the first run of n_frames consecutive free bits. O(n) in the bitmap size — suitable for infrequent DMA allocations.

Returns
: Physical address of the first frame, or 0 on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_alloc_frame()

uint64_t pmm_alloc_frame ( void )

pmm_alloc_frame() - Return the physical address of a free frame.

Linear scan of g_pmm_bitmap looking for any non-zero byte. Within the found byte, bit-scans to the lowest set bit, clears it (USED), and returns frame_idx * PAGE_SIZE.

Returns
: 4 KiB-aligned physical address, or 0 on OOM.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_free_contiguous()

void pmm_free_contiguous ( uint64_t phys,
size_t n_frames )

pmm_free_contiguous() - Return n_frames contiguous frames to the free pool. @phys: Physical address of the first frame (4 KiB-aligned). @n_frames: Number of frames to free.

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

◆ pmm_free_count()

uint64_t pmm_free_count ( void )

pmm_free_count() - Return the number of free frames.

Here is the caller graph for this function:

◆ pmm_free_frame()

void pmm_free_frame ( uint64_t phys)

pmm_free_frame() - Mark a physical frame as free. @phys: Physical address of frame (4 KiB-aligned).

Guards against double-free: if the bit is already set, returns without modifying total_free.

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

◆ pmm_free_range()

void pmm_free_range ( uint64_t base,
uint64_t length )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pmm_init()

void pmm_init ( uint64_t mb_info_phys)

pmm_init() - Parse Multiboot 2 memory map and initialise the bitmap. @mb_info_phys: Physical address of MB2 struct; KERNEL_VMA added internally.

Two-pass init: Pass 1 — scan mmap to find the highest available physical address, then size and place the bitmap in physical RAM right after the kernel image (within the boot identity map, so the virtual address phys+KERNEL_VMA is immediately valid). Pass 2 — mark available regions free; mark kernel+bitmap frames used.

For a 64 MiB VM the bitmap is ~2 KiB instead of the 128 KiB that a static 4 GiB-ceiling array would require.

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

◆ pmm_ref_frame()

void pmm_ref_frame ( uint64_t phys)

pmm_ref_frame() - Add an extra owner to an already-allocated frame.

Here is the call graph for this function:

◆ pmm_set_free()

void pmm_set_free ( uint64_t frame_idx)
static
Here is the caller graph for this function:

◆ pmm_set_used()

void pmm_set_used ( uint64_t frame_idx)
static

◆ pmm_total_count()

uint64_t pmm_total_count ( void )

pmm_total_count() - Query the total number of physical frames discovered at boot.

Returns total_frames, incremented once per frame during pmm_init(). Does not change after boot. O(1).

Returns
: Total 4 KiB frame count (all usable RAM, including kernel-reserved frames).
Here is the caller graph for this function:

◆ pmm_unref_frame()

void pmm_unref_frame ( uint64_t phys)

pmm_unref_frame() - Drop one owner of a frame; frees it at refcount 0.

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

Variable Documentation

◆ __image_end

uint64_t __image_end
extern

◆ g_bitmap_bytes

uint64_t g_bitmap_bytes = 0
static

◆ g_max_frames

uint64_t g_max_frames = 0
static

◆ g_pmm_bitmap

uint8_t* g_pmm_bitmap = NULL
static

◆ g_pmm_refcount

uint8_t* g_pmm_refcount = NULL
static

◆ g_refcount_bytes

uint64_t g_refcount_bytes = 0
static

◆ pmm_lock

spinlock_t pmm_lock = SPINLOCK_INIT
static

◆ total_frames

uint64_t total_frames = 0
static

◆ total_free

uint64_t total_free = 0
static