Kernel: Add GeneralAllocator::paddr_of

Yoy can now query physical address of a virtual address for general
allocator allocation
This commit is contained in:
Bananymous 2023-06-18 23:25:51 +03:00
parent d354cccd37
commit 388f530edd
2 changed files with 30 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <BAN/LinkedList.h>
#include <BAN/Optional.h>
#include <BAN/UniqPtr.h>
#include <kernel/Memory/Heap.h>
#include <kernel/Memory/PageTable.h>
@ -19,6 +20,8 @@ namespace Kernel
BAN::ErrorOr<BAN::UniqPtr<GeneralAllocator>> clone(PageTable&);
BAN::Optional<paddr_t> paddr_of(vaddr_t);
vaddr_t allocate(size_t);
bool deallocate(vaddr_t);
@ -30,12 +33,14 @@ namespace Kernel
{
vaddr_t address { 0 };
BAN::Vector<paddr_t> pages;
bool contains(vaddr_t);
};
private:
const vaddr_t m_first_vaddr;
PageTable& m_page_table;
BAN::LinkedList<Allocation> m_allocations;
const vaddr_t m_first_vaddr;
};
}

View File

@ -51,9 +51,11 @@ namespace Kernel
{
vaddr_t vaddr = allocation.address + i * PAGE_SIZE;
m_page_table.map_page_at(allocation.pages[i], vaddr, PageTable::Flags::UserSupervisor | PageTable::Flags::ReadWrite | PageTable::Flags::Present);
m_page_table.invalidate(vaddr);
}
if (&m_page_table == &PageTable::current())
m_page_table.load();
m_page_table.unlock();
MUST(m_allocations.push_back(BAN::move(allocation)));
@ -121,4 +123,25 @@ namespace Kernel
return allocator;
}
BAN::Optional<paddr_t> GeneralAllocator::paddr_of(vaddr_t vaddr)
{
for (auto& allocation : m_allocations)
{
if (!allocation.contains(vaddr))
continue;
size_t offset = vaddr - allocation.address;
size_t page_index = offset / PAGE_SIZE;
size_t page_offset = offset % PAGE_SIZE;
return allocation.pages[page_index] + page_offset;
}
return {};
}
bool GeneralAllocator::Allocation::contains(vaddr_t vaddr)
{
return this->address <= vaddr && vaddr < this->address + this->pages.size() * PAGE_SIZE;
}
}