From 2d0a5a9e155d9a0fc2df103128793bf691710233 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 8 May 2023 00:06:56 +0300 Subject: [PATCH] Kernel: FixedWidthAllocator operates on MMU Instead of Process* we use MMU& in FixedWidthAllocator since it is everything it actually needs :) --- .../kernel/Memory/FixedWidthAllocator.h | 9 ++-- kernel/kernel/Memory/FixedWidthAllocator.cpp | 47 +++++++------------ kernel/kernel/Process.cpp | 2 +- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/kernel/include/kernel/Memory/FixedWidthAllocator.h b/kernel/include/kernel/Memory/FixedWidthAllocator.h index b3683603..67a01248 100644 --- a/kernel/include/kernel/Memory/FixedWidthAllocator.h +++ b/kernel/include/kernel/Memory/FixedWidthAllocator.h @@ -1,19 +1,18 @@ #pragma once #include +#include namespace Kernel { - class Process; - class FixedWidthAllocator { BAN_NON_COPYABLE(FixedWidthAllocator); + BAN_NON_MOVABLE(FixedWidthAllocator); public: - FixedWidthAllocator(Process*, uint32_t); - FixedWidthAllocator(FixedWidthAllocator&&); + FixedWidthAllocator(MMU&, uint32_t); ~FixedWidthAllocator(); vaddr_t allocate(); @@ -38,7 +37,7 @@ namespace Kernel private: static constexpr uint32_t m_min_allocation_size = 16; - Process* m_process; + MMU& m_mmu; const uint32_t m_allocation_size; vaddr_t m_nodes_page { 0 }; diff --git a/kernel/kernel/Memory/FixedWidthAllocator.cpp b/kernel/kernel/Memory/FixedWidthAllocator.cpp index e08d36c3..06cded09 100644 --- a/kernel/kernel/Memory/FixedWidthAllocator.cpp +++ b/kernel/kernel/Memory/FixedWidthAllocator.cpp @@ -6,23 +6,23 @@ namespace Kernel { - FixedWidthAllocator::FixedWidthAllocator(Process* process, uint32_t allocation_size) - : m_process(process) + FixedWidthAllocator::FixedWidthAllocator(MMU& mmu, uint32_t allocation_size) + : m_mmu(mmu) , m_allocation_size(BAN::Math::max(allocation_size, m_min_allocation_size)) { ASSERT(BAN::Math::is_power_of_two(allocation_size)); paddr_t nodes_paddr = Heap::get().take_free_page(); - m_nodes_page = m_process->mmu().get_free_page(); - m_process->mmu().map_page_at(nodes_paddr, m_nodes_page, MMU::Flags::ReadWrite | MMU::Flags::Present); + m_nodes_page = m_mmu.get_free_page(); + m_mmu.map_page_at(nodes_paddr, m_nodes_page, MMU::Flags::ReadWrite | MMU::Flags::Present); paddr_t allocated_pages_paddr = Heap::get().take_free_page(); - m_allocated_pages = m_process->mmu().get_free_page(); - m_process->mmu().map_page_at(allocated_pages_paddr, m_allocated_pages, MMU::Flags::ReadWrite | MMU::Flags::Present); + m_allocated_pages = m_mmu.get_free_page(); + m_mmu.map_page_at(allocated_pages_paddr, m_allocated_pages, MMU::Flags::ReadWrite | MMU::Flags::Present); CriticalScope _; - m_process->mmu().load(); + m_mmu.load(); memset((void*)m_nodes_page, 0, PAGE_SIZE); memset((void*)m_allocated_pages, 0, PAGE_SIZE); @@ -42,25 +42,10 @@ namespace Kernel Process::current().mmu().load(); } - FixedWidthAllocator::FixedWidthAllocator(FixedWidthAllocator&& other) - : m_process(other.m_process) - , m_allocation_size(other.m_allocation_size) - , m_nodes_page(other.m_nodes_page) - , m_allocated_pages(other.m_allocated_pages) - , m_free_list(other.m_free_list) - , m_used_list(other.m_used_list) - , m_allocations(other.m_allocations) - { - other.m_process = nullptr; - } - FixedWidthAllocator::~FixedWidthAllocator() { - if (m_process == nullptr) - return; - - Heap::get().release_page(m_process->mmu().physical_address_of(m_nodes_page)); - m_process->mmu().unmap_page(m_nodes_page); + Heap::get().release_page(m_mmu.physical_address_of(m_nodes_page)); + m_mmu.unmap_page(m_nodes_page); for (uint32_t page_index = 0; page_index < PAGE_SIZE / sizeof(vaddr_t); page_index++) { @@ -68,15 +53,15 @@ namespace Kernel if (page_vaddr == 0) continue; - ASSERT(!m_process->mmu().is_page_free(page_vaddr)); - paddr_t page_paddr = m_process->mmu().physical_address_of(page_vaddr); + ASSERT(!m_mmu.is_page_free(page_vaddr)); + paddr_t page_paddr = m_mmu.physical_address_of(page_vaddr); Heap::get().release_page(page_paddr); - m_process->mmu().unmap_page(page_vaddr); + m_mmu.unmap_page(page_vaddr); } - Heap::get().release_page(m_process->mmu().physical_address_of(m_allocated_pages)); - m_process->mmu().unmap_page(m_allocated_pages); + Heap::get().release_page(m_mmu.physical_address_of(m_allocated_pages)); + m_mmu.unmap_page(m_allocated_pages); } paddr_t FixedWidthAllocator::allocate() @@ -202,8 +187,8 @@ namespace Kernel paddr_t page_paddr = Heap::get().take_free_page(); ASSERT(page_paddr); - page_vaddr = m_process->mmu().get_free_page(); - m_process->mmu().map_page_at(page_paddr, page_vaddr, MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present); + page_vaddr = m_mmu.get_free_page(); + m_mmu.map_page_at(page_paddr, page_vaddr, MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present); } } \ No newline at end of file diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index b2c26123..ed300406 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -397,7 +397,7 @@ namespace Kernel if (allocator.allocation_size() == allocation_size && allocator.allocations() < allocator.max_allocations()) return (void*)allocator.allocate(); - MUST(m_fixed_width_allocators.emplace_back(this, allocation_size)); + MUST(m_fixed_width_allocators.emplace_back(mmu(), allocation_size)); return (void*)m_fixed_width_allocators.back().allocate(); } else