diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-01-03 02:19:57 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-01-03 02:19:57 +0800 |
| commit | 210025560c80b48af4875af199f183ad709e3bf0 (patch) | |
| tree | eb11aeb7b641187f207e3b572c5184cf3526b463 /include/kernel/pmm.h | |
| parent | bde87490d61135dcaa94d9b1489ff6c39876d169 (diff) | |
| parent | 10614e04929a6d109a5e90524e446b637aaf0c13 (diff) | |
| download | lume-210025560c80b48af4875af199f183ad709e3bf0.tar.gz lume-210025560c80b48af4875af199f183ad709e3bf0.tar.xz | |
Merge pull request #4 from Richard-Qin-001/dev/shell
Rebuilt the system's underlying structure; it is now stable enough to merge.
The system now supports a read-only file system and has a simple shell implemented.
Diffstat (limited to 'include/kernel/pmm.h')
| -rw-r--r-- | include/kernel/pmm.h | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/include/kernel/pmm.h b/include/kernel/pmm.h index 3328450..d31473f 100644 --- a/include/kernel/pmm.h +++ b/include/kernel/pmm.h @@ -1,13 +1,50 @@ #pragma once #include "common/types.h" +#include "kernel/spinlock.h" -#define PHYSTOP (0x80000000 + 128 * 1024 * 1024) -#define PGSIZE 4096 +// Physical Memory Layout Definition +#define KERNBASE 0x80000000 +// Set the memory size to 128MB +#define PHYSIZE (128 * 1024 * 1024) +#define PHYSTOP (KERNBASE + PHYSIZE) -namespace PMM{ +// Buddy System Max Order +// Order 10 => 2^10 pages = 1024 * 4KB = 4MB contiguous block +// Order 11 => 2^11 pages = 8MB +#define MAX_ORDER 11 + +// Page flag +#define PG_used 0x01 // This page is occupied +#define PG_reserved 0x02 // This page is reserved (kernel code/data, DTB, MemMap) + +// Physical Page Metadata Structure +struct Page +{ + uint8 flags; // Page Status (PG_used, PG_reserved) + uint8 order; // If it is the first page of a free block, record the order of the current block + uint16 refcnt; // Reference Counting + struct Page *next; // Free list pointer (points to the next free block of the same size) + struct Page *prev; // Doubly linked list (easy to remove) +}; + +namespace PMM +{ + // Initialize physical memory management void init(); - void* alloc_page(); + // Core Allocation Interface (Buddy System) + // order: 0 ~ MAX_ORDER-1 + void *alloc_pages(int order); + void free_pages(void *pa, int order); + + // Compatible with old interface (allocate single page with order=0 and automatically reset to zero) + void *alloc_page(); + void free_page(void *pa); + + // Helper function + struct Page *pa_to_page(uint64 pa); + uint64 page_to_pa(struct Page *page); - void free_page(void* pa); + // Debug information + void print_free_lists(); }
\ No newline at end of file |
