|
miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
|
#include <miniOS/mm/pmm.h>#include <miniOS/mm/vmm.h>#include <miniOS/types.h>#include <miniOS/arch/x86_64/spinlock.h>
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_t * | g_pmm_bitmap = NULL |
| static uint64_t | g_max_frames = 0 |
| static uint64_t | g_bitmap_bytes = 0 |
| static uint8_t * | g_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 |
| #define MB2_TAG_MMAP 6 |
| #define MULTIBOOT_MEMORY_AVAILABLE 1 |

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.


| 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.


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.


| uint64_t pmm_free_count | ( | void | ) |
| 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.




| 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.


| void pmm_ref_frame | ( | uint64_t | phys | ) |
pmm_ref_frame() - Add an extra owner to an already-allocated frame.

|
static |

|
static |
| 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).

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


|
extern |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |