Introduce a few helpers to allocate/map/unmap several pages, which wrap around the MMU functionality

This commit is contained in:
apio 2022-11-19 22:28:45 +01:00
parent c886669d56
commit 1b41a3e9cf
2 changed files with 66 additions and 1 deletions

View File

@ -39,6 +39,8 @@ static void page_bitmap_set(u64 index, bool value)
if (value) { page_virtual_bitmap_addr[byte_index] |= mask; } if (value) { page_virtual_bitmap_addr[byte_index] |= mask; }
} }
#define CHECK_PAGE_ALIGNED(address) check(is_aligned(address, ARCH_PAGE_SIZE))
namespace MemoryManager namespace MemoryManager
{ {
Result<void> protect_kernel_sections() Result<void> protect_kernel_sections()
@ -158,7 +160,7 @@ namespace MemoryManager
Result<void> remap(u64 address, usize count, int flags) Result<void> remap(u64 address, usize count, int flags)
{ {
check(is_aligned(address, ARCH_PAGE_SIZE)); CHECK_PAGE_ALIGNED(address);
while (count--) while (count--)
{ {
@ -169,6 +171,62 @@ namespace MemoryManager
return {}; return {};
} }
Result<void> map(u64 virt, u64 phys, usize count, int flags)
{
CHECK_PAGE_ALIGNED(virt);
CHECK_PAGE_ALIGNED(phys);
while (count--)
{
TRY(MMU::map(virt, phys, flags));
virt += ARCH_PAGE_SIZE;
phys += ARCH_PAGE_SIZE;
}
return {};
}
Result<u64> alloc_at(u64 virt, usize count, int flags)
{
CHECK_PAGE_ALIGNED(virt);
while (count--)
{
u64 frame = TRY(alloc_frame());
TRY(MMU::map(virt, frame, flags));
virt += ARCH_PAGE_SIZE;
}
return virt;
}
Result<void> unmap_owned(u64 virt, usize count)
{
CHECK_PAGE_ALIGNED(virt);
while (count--)
{
u64 frame = TRY(MMU::unmap(virt));
TRY(free_frame(frame));
virt += ARCH_PAGE_SIZE;
}
return {};
}
Result<void> unmap_weak(u64 virt, usize count)
{
CHECK_PAGE_ALIGNED(virt);
while (count--)
{
TRY(MMU::unmap(virt));
virt += ARCH_PAGE_SIZE;
}
return {};
}
Result<void> remap_unaligned(u64 address, usize count, int flags) Result<void> remap_unaligned(u64 address, usize count, int flags)
{ {
if (!is_aligned(address, ARCH_PAGE_SIZE)) count++; if (!is_aligned(address, ARCH_PAGE_SIZE)) count++;

View File

@ -20,6 +20,13 @@ namespace MemoryManager
bool validate_readable_page(u64 address); bool validate_readable_page(u64 address);
bool validate_writable_page(u64 address); bool validate_writable_page(u64 address);
Result<void> map(u64 virt, u64 phys, usize count, int flags);
Result<u64> alloc_at(u64 virt, usize count, int flags);
Result<void> unmap_owned(u64 virt, usize count);
Result<void> unmap_weak(u64 virt, usize count);
u64 free(); u64 free();
u64 used(); u64 used();
u64 reserved(); u64 reserved();