From 3f1c0ec91b421f2f9aa0e74a46df2ac061c1117e Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 22 Apr 2023 16:54:46 +0300 Subject: [PATCH] Kernel: Process now frees up its pages on destruction --- kernel/include/kernel/Process.h | 2 ++ kernel/kernel/Process.cpp | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index ab0fa707..f6584669 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -78,6 +79,7 @@ namespace Kernel BAN::ErrorOr get_free_fd(); BAN::Vector m_open_files; + BAN::Vector m_allocated_pages; mutable RecursiveSpinLock m_lock; diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index f07f4138..56394ae5 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -66,10 +66,12 @@ namespace Kernel { size_t page_start = elf_program_header.p_vaddr / 4096; size_t page_end = BAN::Math::div_round_up(elf_program_header.p_vaddr + elf_program_header.p_memsz, 4096); + MUST(process->m_allocated_pages.reserve(page_end - page_start + 1)); for (size_t page = page_start; page <= page_end; page++) { - auto addr = Memory::Heap::get().take_free_page(); - process->m_mmu->map_page_at(addr, page * 4096, MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present); + auto paddr = Memory::Heap::get().take_free_page(); + MUST(process->m_allocated_pages.push_back(paddr)); + process->m_mmu->map_page_at(paddr, page * 4096, MMU::Flags::UserSupervisor | MMU::Flags::ReadWrite | MMU::Flags::Present); } process->m_mmu->load(); memcpy((void*)elf_program_header.p_vaddr, elf->data() + elf_program_header.p_offset, elf_program_header.p_filesz); @@ -98,8 +100,11 @@ namespace Kernel Process::~Process() { - if (!m_threads.empty()) - exit(); + ASSERT(m_threads.empty()); + if (m_mmu) + delete m_mmu; + for (auto paddr : m_allocated_pages) + Memory::Heap::get().release_page(paddr); } void Process::add_thread(Thread* thread)