From 388f530edd0fc1234824541c7b13535e70d69d0a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 18 Jun 2023 23:25:51 +0300 Subject: [PATCH] Kernel: Add GeneralAllocator::paddr_of Yoy can now query physical address of a virtual address for general allocator allocation --- .../include/kernel/Memory/GeneralAllocator.h | 7 +++++- kernel/kernel/Memory/GeneralAllocator.cpp | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/kernel/include/kernel/Memory/GeneralAllocator.h b/kernel/include/kernel/Memory/GeneralAllocator.h index 6ec2f4c8..f96d0f92 100644 --- a/kernel/include/kernel/Memory/GeneralAllocator.h +++ b/kernel/include/kernel/Memory/GeneralAllocator.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -19,6 +20,8 @@ namespace Kernel BAN::ErrorOr> clone(PageTable&); + BAN::Optional 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 pages; + + bool contains(vaddr_t); }; private: - const vaddr_t m_first_vaddr; PageTable& m_page_table; BAN::LinkedList m_allocations; + const vaddr_t m_first_vaddr; }; } \ No newline at end of file diff --git a/kernel/kernel/Memory/GeneralAllocator.cpp b/kernel/kernel/Memory/GeneralAllocator.cpp index 2bf4ab73..21a37773 100644 --- a/kernel/kernel/Memory/GeneralAllocator.cpp +++ b/kernel/kernel/Memory/GeneralAllocator.cpp @@ -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 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; + } + } \ No newline at end of file