diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 33935d26..b410cc0d 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -247,6 +247,32 @@ namespace MemoryManager return start; } + Result get_kernel_mapping_for_frames(u64 phys, usize count, int flags) + { + u64 start = TRY(KernelVM::alloc_several_pages(count)); + + usize pages_mapped = 0; + + auto guard = make_scope_guard([=, &pages_mapped] { + KernelVM::free_several_pages(start, pages_mapped); + unmap_weak(start, pages_mapped); + }); + + u64 virt = start; + + while (pages_mapped < count) + { + TRY(MMU::map(virt, phys, flags)); + virt += ARCH_PAGE_SIZE; + phys += ARCH_PAGE_SIZE; + pages_mapped++; + } + + guard.deactivate(); + + return start; + } + Result unmap_owned(u64 virt, usize count) { CHECK_PAGE_ALIGNED(virt); diff --git a/kernel/src/memory/MemoryManager.h b/kernel/src/memory/MemoryManager.h index cf3ed2ce..a5570157 100644 --- a/kernel/src/memory/MemoryManager.h +++ b/kernel/src/memory/MemoryManager.h @@ -25,6 +25,8 @@ namespace MemoryManager Result alloc_at(u64 virt, usize count, int flags); Result alloc_for_kernel(usize count, int flags); + Result get_kernel_mapping_for_frames(u64 phys, usize count, int flags); + Result unmap_owned(u64 virt, usize count); Result unmap_owned_and_free_vm(u64 virt, usize count); Result unmap_weak(u64 virt, usize count);