From 11a6be89b6c75b31f29c86136d53aac5044da7a2 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sat, 2 May 2026 23:33:11 +0800 Subject: 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 --- na/mm/slab.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'na/mm/slab.cc') diff --git a/na/mm/slab.cc b/na/mm/slab.cc index ef15b6a..98029aa 100644 --- a/na/mm/slab.cc +++ b/na/mm/slab.cc @@ -36,7 +36,7 @@ */ static inline void* frame_to_page_va(Frame* f) { - return reinterpret_cast(pa_to_va(frame_to_pa(f))); + return reinterpret_cast(pa_to_va(frame_to_pa(f)).raw); } /* Round up x to the next multiple of align (align must be power of 2) */ @@ -193,9 +193,9 @@ void KmemCache::free(void* obj) uint64 obj_addr = reinterpret_cast(obj); /* Convert VA to PA. Like pmm.cc, handle both PA and VA cases. */ - uint64 obj_pa = ensure_pa(obj_addr); + uint64 obj_pa = va_to_pa(virt_addr(obj_addr)).raw; uint64 page_pa = obj_pa & ~(kPageSize - 1); - Frame* f = pa_to_frame(page_pa); + Frame* f = pa_to_frame(phys_addr(page_pa)); if (f->state != FrameState::Slab || f->slab.cache != this) kernel_panic("slab free: object does not belong to this cache"); @@ -255,7 +255,7 @@ void* kmalloc(uint32 size) Frame* f = pmm_alloc_frames(order); if (!f) return nullptr; - return reinterpret_cast(pa_to_va(frame_to_pa(f))); + return reinterpret_cast(pa_to_va(frame_to_pa(f)).raw); } KmemCache* cache = find_cache(size); @@ -270,9 +270,9 @@ void kfree(void* ptr) return; uint64 addr = reinterpret_cast(ptr); - uint64 pa = ensure_pa(addr); + uint64 pa = va_to_pa(virt_addr(addr)).raw; uint64 page_pa = pa & ~(kPageSize - 1); - Frame* f = pa_to_frame(page_pa); + Frame* f = pa_to_frame(phys_addr(page_pa)); if (f->state == FrameState::Slab) { KmemCache* cache = f->slab.cache; -- cgit v1.2.3