feat(kernel): implement Phase 3 - PMM bitmap allocator, VMM #PF handler & dynamic kernel heap (kmalloc/kfree)
This commit is contained in:
1 parent
a8486515cc
commit
7a584ea6e6
10 files changed
+489
-5
No files matched your search
@@ -0,0 +1,29 @@
|
||||
#ifndef AIRLYS_HEAP_H
|
||||
#define AIRLYS_HEAP_H
|
||||
|
||||
#include <airlys/types.h>
|
||||
|
||||
#define KERNEL_HEAP_START 0x01000000ULL /* 16 MB boundary */
|
||||
#define KERNEL_HEAP_SIZE (32ULL * 1024 * 1024) /* 32 MB heap pool */
|
||||
|
||||
/* Initialize dynamic kernel heap allocator */
|
||||
void heap_init(uintptr_t start_addr, size_t size);
|
||||
|
||||
/* Allocate dynamic heap memory */
|
||||
void* kmalloc(size_t size);
|
||||
|
||||
/* Free previously allocated heap memory */
|
||||
void kfree(void* ptr);
|
||||
|
||||
/* Allocate zero-initialized dynamic heap memory */
|
||||
void* kcalloc(size_t num, size_t size);
|
||||
|
||||
/* Reallocate dynamic heap memory with new size */
|
||||
void* krealloc(void* ptr, size_t new_size);
|
||||
|
||||
/* Heap Statistics */
|
||||
size_t heap_get_total_bytes(void);
|
||||
size_t heap_get_used_bytes(void);
|
||||
size_t heap_get_free_bytes(void);
|
||||
|
||||
#endif /* AIRLYS_HEAP_H */
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef AIRLYS_PMM_H
|
||||
#define AIRLYS_PMM_H
|
||||
|
||||
#include <airlys/types.h>
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
#define PMM_MAX_MEMORY (1024ULL * 1024 * 1024) /* 1 GB */
|
||||
#define PMM_MAX_FRAMES (PMM_MAX_MEMORY / PAGE_SIZE) /* 262,144 frames */
|
||||
#define PMM_BITMAP_SIZE (PMM_MAX_FRAMES / 8) /* 32,768 bytes */
|
||||
|
||||
/* Initialize Physical Memory Manager above kernel image */
|
||||
void pmm_init(uintptr_t mem_size);
|
||||
|
||||
/* Allocate a single 4KB physical frame (returns physical address or 0 on failure) */
|
||||
void* pmm_alloc_frame(void);
|
||||
|
||||
/* Free an allocated 4KB physical frame */
|
||||
void pmm_free_frame(void* frame);
|
||||
|
||||
/* Allocate multiple contiguous physical frames */
|
||||
void* pmm_alloc_frames(size_t count);
|
||||
|
||||
/* Free multiple contiguous physical frames */
|
||||
void pmm_free_frames(void* frame, size_t count);
|
||||
|
||||
/* Statistics */
|
||||
size_t pmm_get_total_frames(void);
|
||||
size_t pmm_get_used_frames(void);
|
||||
size_t pmm_get_free_frames(void);
|
||||
uint64_t pmm_get_free_memory_kb(void);
|
||||
|
||||
#endif /* AIRLYS_PMM_H */
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef AIRLYS_VMM_H
|
||||
#define AIRLYS_VMM_H
|
||||
|
||||
#include <airlys/types.h>
|
||||
#include <airlys/idt.h>
|
||||
|
||||
/* Page Table Entry Flags (x86_64) */
|
||||
#define PAGE_PRESENT (1ULL << 0)
|
||||
#define PAGE_WRITABLE (1ULL << 1)
|
||||
#define PAGE_USER (1ULL << 2)
|
||||
#define PAGE_WRITETHROUGH (1ULL << 3)
|
||||
#define PAGE_NOCACHE (1ULL << 4)
|
||||
#define PAGE_ACCESSED (1ULL << 5)
|
||||
#define PAGE_DIRTY (1ULL << 6)
|
||||
#define PAGE_HUGE (1ULL << 7)
|
||||
#define PAGE_GLOBAL (1ULL << 8)
|
||||
#define PAGE_NOEXECUTE (1ULL << 63)
|
||||
|
||||
/* Read CR2 register (Linear faulting virtual address) */
|
||||
static inline uintptr_t read_cr2(void) {
|
||||
uintptr_t val;
|
||||
__asm__ volatile ("mov %%cr2, %0" : "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Read CR3 register (PML4 base address) */
|
||||
static inline uintptr_t read_cr3(void) {
|
||||
uintptr_t val;
|
||||
__asm__ volatile ("mov %%cr3, %0" : "=r"(val));
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Invalidate TLB entry for a single virtual address */
|
||||
static inline void invlpg(uintptr_t vaddr) {
|
||||
__asm__ volatile ("invlpg (%0)" : : "r"(vaddr) : "memory");
|
||||
}
|
||||
|
||||
/* Initialize Virtual Memory Manager & register #PF handler */
|
||||
void vmm_init(void);
|
||||
|
||||
#endif /* AIRLYS_VMM_H */
|
||||
Reference in new issue
Block a user