1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 02:58:12 +00:00

Kernel: Let PageDirectory own the associated RangeAllocator.

Since we transition to a new PageDirectory on exec(), we need a matching
RangeAllocator to go with the new directory. Instead of juggling this in
Process and MemoryManager, simply attach the RangeAllocator to the
PageDirectory instead.

Fixes #61.
This commit is contained in:
Andreas Kling 2019-05-20 04:46:29 +02:00
parent d65114afd7
commit bcc6ddfb6b
6 changed files with 16 additions and 16 deletions

View file

@ -75,8 +75,8 @@ Range Process::allocate_range(LinearAddress laddr, size_t size)
laddr.mask(PAGE_MASK); laddr.mask(PAGE_MASK);
size = PAGE_ROUND_UP(size); size = PAGE_ROUND_UP(size);
if (laddr.is_null()) if (laddr.is_null())
return m_range_allocator.allocate_anywhere(size); return page_directory().range_allocator().allocate_anywhere(size);
return m_range_allocator.allocate_specific(laddr, size); return page_directory().range_allocator().allocate_specific(laddr, size);
} }
Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name, bool is_readable, bool is_writable, bool commit) Region* Process::allocate_region(LinearAddress laddr, size_t size, String&& name, bool is_readable, bool is_writable, bool commit)
@ -117,7 +117,7 @@ bool Process::deallocate_region(Region& region)
InterruptDisabler disabler; InterruptDisabler disabler;
for (int i = 0; i < m_regions.size(); ++i) { for (int i = 0; i < m_regions.size(); ++i) {
if (m_regions[i] == &region) { if (m_regions[i] == &region) {
m_range_allocator.deallocate({ region.laddr(), region.size() }); page_directory().range_allocator().deallocate({ region.laddr(), region.size() });
MM.unmap_region(region); MM.unmap_region(region);
m_regions.remove(i); m_regions.remove(i);
return true; return true;
@ -313,7 +313,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
dword entry_eip = 0; dword entry_eip = 0;
// FIXME: Is there a race here? // FIXME: Is there a race here?
auto old_page_directory = move(m_page_directory); auto old_page_directory = move(m_page_directory);
m_page_directory = PageDirectory::create(); m_page_directory = PageDirectory::create_for_userspace();
#ifdef MM_DEBUG #ifdef MM_DEBUG
dbgprintf("Process %u exec: PD=%x created\n", pid(), m_page_directory.ptr()); dbgprintf("Process %u exec: PD=%x created\n", pid(), m_page_directory.ptr());
#endif #endif
@ -542,9 +542,6 @@ Process* Process::create_kernel_process(String&& name, void (*e)())
return process; return process;
} }
static const dword userspace_range_base = 0x01000000;
static const dword kernelspace_range_base = 0xc0000000;
Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RetainPtr<Inode>&& cwd, RetainPtr<Inode>&& executable, TTY* tty, Process* fork_parent) Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RetainPtr<Inode>&& cwd, RetainPtr<Inode>&& executable, TTY* tty, Process* fork_parent)
: m_name(move(name)) : m_name(move(name))
, m_pid(next_pid++) // FIXME: RACE: This variable looks racy! , m_pid(next_pid++) // FIXME: RACE: This variable looks racy!
@ -557,11 +554,10 @@ Process::Process(String&& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring
, m_executable(move(executable)) , m_executable(move(executable))
, m_tty(tty) , m_tty(tty)
, m_ppid(ppid) , m_ppid(ppid)
, m_range_allocator(LinearAddress(userspace_range_base), kernelspace_range_base - userspace_range_base)
{ {
dbgprintf("Process: New process PID=%u with name=%s\n", m_pid, m_name.characters()); dbgprintf("Process: New process PID=%u with name=%s\n", m_pid, m_name.characters());
m_page_directory = PageDirectory::create(); m_page_directory = PageDirectory::create_for_userspace();
#ifdef MM_DEBUG #ifdef MM_DEBUG
dbgprintf("Process %u ctor: PD=%x created\n", pid(), m_page_directory.ptr()); dbgprintf("Process %u ctor: PD=%x created\n", pid(), m_page_directory.ptr());
#endif #endif

View file

@ -331,7 +331,6 @@ private:
RetainPtr<ProcessTracer> m_tracer; RetainPtr<ProcessTracer> m_tracer;
OwnPtr<ELFLoader> m_elf_loader; OwnPtr<ELFLoader> m_elf_loader;
RangeAllocator m_range_allocator;
Lock m_big_lock { "Process" }; Lock m_big_lock { "Process" };
}; };

View file

@ -20,7 +20,6 @@ MemoryManager& MM
} }
MemoryManager::MemoryManager() MemoryManager::MemoryManager()
: m_range_allocator(LinearAddress(0xc0000000), 0x3f000000)
{ {
// FIXME: This is not the best way to do memory map detection. // FIXME: This is not the best way to do memory map detection.
// Rewrite to use BIOS int 15,e820 once we have VM86 support. // Rewrite to use BIOS int 15,e820 once we have VM86 support.
@ -402,7 +401,7 @@ RetainPtr<Region> MemoryManager::allocate_kernel_region(size_t size, String&& na
InterruptDisabler disabler; InterruptDisabler disabler;
ASSERT(!(size % PAGE_SIZE)); ASSERT(!(size % PAGE_SIZE));
auto range = m_range_allocator.allocate_anywhere(size); auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
ASSERT(range.is_valid()); ASSERT(range.is_valid());
auto region = adopt(*new Region(range, move(name), true, true, false)); auto region = adopt(*new Region(range, move(name), true, true, false));
MM.map_region_at_address(*m_kernel_page_directory, *region, range.base(), false); MM.map_region_at_address(*m_kernel_page_directory, *region, range.base(), false);

View file

@ -13,7 +13,6 @@
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <Kernel/LinearAddress.h> #include <Kernel/LinearAddress.h>
#include <Kernel/VM/PhysicalPage.h> #include <Kernel/VM/PhysicalPage.h>
#include <Kernel/VM/RangeAllocator.h>
#include <Kernel/VM/Region.h> #include <Kernel/VM/Region.h>
#include <Kernel/VM/VMObject.h> #include <Kernel/VM/VMObject.h>
#include <Kernel/FileSystem/InodeIdentifier.h> #include <Kernel/FileSystem/InodeIdentifier.h>
@ -217,8 +216,6 @@ private:
HashTable<Region*> m_user_regions; HashTable<Region*> m_user_regions;
HashTable<Region*> m_kernel_regions; HashTable<Region*> m_kernel_regions;
RangeAllocator m_range_allocator;
size_t m_ram_size { 0 }; size_t m_ram_size { 0 };
bool m_quickmap_in_use { false }; bool m_quickmap_in_use { false };
}; };

View file

@ -3,12 +3,17 @@
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/Thread.h> #include <Kernel/Thread.h>
static const dword userspace_range_base = 0x01000000;
static const dword kernelspace_range_base = 0xc0000000;
PageDirectory::PageDirectory(PhysicalAddress paddr) PageDirectory::PageDirectory(PhysicalAddress paddr)
: m_range_allocator(LinearAddress(0xc0000000), 0x3f000000)
{ {
m_directory_page = PhysicalPage::create_eternal(paddr, true); m_directory_page = PhysicalPage::create_eternal(paddr, true);
} }
PageDirectory::PageDirectory() PageDirectory::PageDirectory()
: m_range_allocator(LinearAddress(userspace_range_base), kernelspace_range_base - userspace_range_base)
{ {
MM.populate_page_directory(*this); MM.populate_page_directory(*this);
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <Kernel/VM/PhysicalPage.h> #include <Kernel/VM/PhysicalPage.h>
#include <Kernel/VM/RangeAllocator.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/Retainable.h> #include <AK/Retainable.h>
#include <AK/RetainPtr.h> #include <AK/RetainPtr.h>
@ -8,7 +9,7 @@
class PageDirectory : public Retainable<PageDirectory> { class PageDirectory : public Retainable<PageDirectory> {
friend class MemoryManager; friend class MemoryManager;
public: public:
static Retained<PageDirectory> create() { return adopt(*new PageDirectory); } static Retained<PageDirectory> create_for_userspace() { return adopt(*new PageDirectory); }
static Retained<PageDirectory> create_at_fixed_address(PhysicalAddress paddr) { return adopt(*new PageDirectory(paddr)); } static Retained<PageDirectory> create_at_fixed_address(PhysicalAddress paddr) { return adopt(*new PageDirectory(paddr)); }
~PageDirectory(); ~PageDirectory();
@ -17,10 +18,13 @@ public:
void flush(LinearAddress); void flush(LinearAddress);
RangeAllocator& range_allocator() { return m_range_allocator; }
private: private:
PageDirectory(); PageDirectory();
explicit PageDirectory(PhysicalAddress); explicit PageDirectory(PhysicalAddress);
RangeAllocator m_range_allocator;
RetainPtr<PhysicalPage> m_directory_page; RetainPtr<PhysicalPage> m_directory_page;
HashMap<unsigned, RetainPtr<PhysicalPage>> m_physical_pages; HashMap<unsigned, RetainPtr<PhysicalPage>> m_physical_pages;
}; };