diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-05-02 23:33:11 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-05-02 23:33:11 +0800 |
| commit | 11a6be89b6c75b31f29c86136d53aac5044da7a2 (patch) | |
| tree | 0ca910710dea9169be608966b03ce5581926f797 /na/include | |
| parent | 6907a90418ba856ff9331e6b4ba5d26b76034c2d (diff) | |
| download | lume-11a6be89b6c75b31f29c86136d53aac5044da7a2.tar.gz lume-11a6be89b6c75b31f29c86136d53aac5044da7a2.tar.xz | |
feat: refactor memory management and integrate memblock
- Updated physical memory management (PMM) to utilize memblock for memory layout and frame allocation.
- Enhanced the virtual memory manager (VMM) to allocate the frame map from memblock, ensuring it starts at the correct virtual address without relocation.
- Improved frame allocation and deallocation functions to handle physical addresses more consistently.
- Adjusted the slab allocator to correctly convert between physical and virtual addresses.
- Refined page fault handling to ensure proper frame allocation and address translation.
- Streamlined the initialization process for PMM and VMM, ensuring a more robust setup of memory structures.
Co-authored-by: Copilot <copilot@github.com>
Diffstat (limited to 'na/include')
| -rw-r--r-- | na/include/lume/memblock.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/na/include/lume/memblock.h b/na/include/lume/memblock.h new file mode 100644 index 0000000..41baf34 --- /dev/null +++ b/na/include/lume/memblock.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * memblock — Boot-time physical memory allocator + * + * A simple region-based allocator active from early boot until + * the buddy system (PMM) takes over. Inspired by Linux's memblock. + * + * Lifetime: + * 1. memblock_init() — discovers RAM via FDT, reserves kernel image + * 2. vmm_init() — uses memblock_alloc for page table pages & frame_map + * 3. pmm_init() — absorbs remaining free regions into buddy + * 4. memblock_retire() — marks memblock inactive; all alloc via PMM + * + * This eliminates the need for post-init pointer relocation when + * rebinding frame_map to a VMEMMAP virtual address. + */ + +#include <lume/types.h> + +inline constexpr int kMemblockMaxRegions = 128; + +/* Initialize memblock: scan FDT for RAM, reserve kernel image + firmware. */ +void memblock_init(uint64 fdt_paddr); + +/* Mark a physical range as reserved (will not be handed to buddy). */ +void memblock_reserve(uint64 base, uint64 size); + +/* Allocate `size` bytes aligned to `align` from top of free memory. + * Returns PA, or 0 on failure. Used for frame_map. */ +uint64 memblock_alloc_top(uint64 size, uint64 align); + +/* Allocate `size` bytes aligned to `align` from bottom of free memory. + * Returns PA, or 0 on failure. Used for page table pages. */ +uint64 memblock_alloc(uint64 size, uint64 align); + +/* Query memory layout (same info as FDT). */ +uint64 memblock_get_mem_base(); +uint64 memblock_get_mem_size(); + +/* Is memblock still the active allocator? */ +bool memblock_is_active(); + +/* Mark memblock as retired (PMM takes over). */ +void memblock_retire(); + +/* Iterate all free (unreserved) regions in ascending order. + * Used by pmm_init() to feed free pages into the buddy system. */ +void memblock_for_each_free(void (*cb)(uint64 base, uint64 size, void* ctx), + void* ctx); |
