2018-10-17 23:13:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-18 13:05:00 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "i386.h"
|
2018-11-01 13:21:02 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2018-10-18 13:05:00 +02:00
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
2018-10-28 10:26:07 +01:00
|
|
|
#include <AK/HashTable.h>
|
2018-11-01 13:21:02 +01:00
|
|
|
#include <AK/String.h>
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-01 13:15:46 +01:00
|
|
|
class Process;
|
2018-11-01 13:21:02 +01:00
|
|
|
extern Process* current;
|
2018-10-18 13:05:00 +02:00
|
|
|
|
|
|
|
enum class PageFaultResponse {
|
|
|
|
ShouldCrash,
|
|
|
|
Continue,
|
2018-10-17 23:13:55 +02:00
|
|
|
};
|
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
class PhysicalPage {
|
|
|
|
friend class MemoryManager;
|
2018-10-17 23:13:55 +02:00
|
|
|
public:
|
2018-11-05 10:23:00 +01:00
|
|
|
~PhysicalPage() { }
|
|
|
|
PhysicalAddress paddr() const { return m_paddr; }
|
|
|
|
|
|
|
|
void retain()
|
|
|
|
{
|
|
|
|
ASSERT(m_retain_count);
|
|
|
|
++m_retain_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release()
|
|
|
|
{
|
|
|
|
ASSERT(m_retain_count);
|
|
|
|
if (!--m_retain_count)
|
|
|
|
return_to_freelist();
|
|
|
|
}
|
2018-10-17 23:13:55 +02:00
|
|
|
|
|
|
|
private:
|
2018-11-05 10:23:00 +01:00
|
|
|
PhysicalPage(PhysicalAddress paddr)
|
|
|
|
: m_paddr(paddr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void return_to_freelist();
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
unsigned m_retain_count { 1 };
|
|
|
|
PhysicalAddress m_paddr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PageDirectory {
|
|
|
|
dword entries[1024];
|
|
|
|
RetainPtr<PhysicalPage> physical_pages[1024];
|
2018-10-17 23:13:55 +02:00
|
|
|
};
|
|
|
|
|
2018-11-01 13:21:02 +01:00
|
|
|
struct Region : public Retainable<Region> {
|
2018-11-05 10:23:00 +01:00
|
|
|
Region(LinearAddress, size_t, Vector<RetainPtr<PhysicalPage>>, String&&, bool r, bool w);
|
2018-11-01 13:21:02 +01:00
|
|
|
~Region();
|
2018-11-02 20:41:58 +01:00
|
|
|
|
|
|
|
RetainPtr<Region> clone();
|
2018-11-01 13:21:02 +01:00
|
|
|
LinearAddress linearAddress;
|
|
|
|
size_t size { 0 };
|
2018-11-05 10:23:00 +01:00
|
|
|
Vector<RetainPtr<PhysicalPage>> physical_pages;
|
2018-11-01 13:21:02 +01:00
|
|
|
String name;
|
2018-11-03 11:36:45 +01:00
|
|
|
bool is_readable { true };
|
|
|
|
bool is_writable { true };
|
2018-11-01 13:21:02 +01:00
|
|
|
};
|
|
|
|
|
2018-10-27 14:56:52 +02:00
|
|
|
#define MM MemoryManager::the()
|
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
class MemoryManager {
|
2018-10-31 23:19:15 +01:00
|
|
|
AK_MAKE_ETERNAL
|
2018-11-05 10:23:00 +01:00
|
|
|
friend class PhysicalPage;
|
2018-10-28 10:26:07 +01:00
|
|
|
friend ByteBuffer procfs$mm();
|
2018-10-17 23:13:55 +02:00
|
|
|
public:
|
2018-10-26 18:43:25 +02:00
|
|
|
static MemoryManager& the() PURE;
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2018-11-01 11:30:48 +01:00
|
|
|
PhysicalAddress pageDirectoryBase() const { return PhysicalAddress(reinterpret_cast<dword>(m_kernel_page_directory)); }
|
2018-10-17 23:13:55 +02:00
|
|
|
|
|
|
|
static void initialize();
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-05 10:29:19 +01:00
|
|
|
PageFaultResponse handle_page_fault(const PageFault&);
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-01 13:21:02 +01:00
|
|
|
bool mapRegion(Process&, Region&);
|
|
|
|
bool unmapRegion(Process&, Region&);
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-03 01:49:40 +01:00
|
|
|
void populate_page_directory(PageDirectory&);
|
|
|
|
void release_page_directory(PageDirectory&);
|
2018-11-01 11:30:48 +01:00
|
|
|
|
2018-11-01 13:21:02 +01:00
|
|
|
byte* create_kernel_alias_for_region(Region&);
|
|
|
|
void remove_kernel_alias_for_region(Region&, byte*);
|
2018-11-01 11:30:48 +01:00
|
|
|
|
|
|
|
void enter_kernel_paging_scope();
|
2018-11-01 13:15:46 +01:00
|
|
|
void enter_process_paging_scope(Process&);
|
2018-11-01 09:01:51 +01:00
|
|
|
|
2018-11-01 13:15:46 +01:00
|
|
|
bool validate_user_read(const Process&, LinearAddress) const;
|
|
|
|
bool validate_user_write(const Process&, LinearAddress) const;
|
2018-11-01 12:45:51 +01:00
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
Vector<RetainPtr<PhysicalPage>> allocate_physical_pages(size_t count);
|
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
private:
|
|
|
|
MemoryManager();
|
|
|
|
~MemoryManager();
|
|
|
|
|
2018-11-01 11:30:48 +01:00
|
|
|
LinearAddress allocate_linear_address_range(size_t);
|
2018-11-01 23:04:34 +01:00
|
|
|
void map_region_at_address(PageDirectory*, Region&, LinearAddress, bool user_accessible);
|
|
|
|
void unmap_range(PageDirectory*, LinearAddress, size_t);
|
2018-11-01 11:30:48 +01:00
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
void initializePaging();
|
2018-10-23 15:53:11 +02:00
|
|
|
void flushEntireTLB();
|
|
|
|
void flushTLB(LinearAddress);
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
RetainPtr<PhysicalPage> allocate_page_table(PageDirectory&, unsigned index);
|
|
|
|
void deallocate_page_table(PageDirectory&, unsigned index);
|
2018-10-27 14:56:52 +02:00
|
|
|
|
2018-10-21 21:57:59 +02:00
|
|
|
void protectMap(LinearAddress, size_t length);
|
2018-11-03 00:31:42 +01:00
|
|
|
|
|
|
|
void create_identity_mapping(LinearAddress, size_t length);
|
|
|
|
void remove_identity_mapping(LinearAddress, size_t);
|
2018-10-17 23:13:55 +02:00
|
|
|
|
|
|
|
struct PageDirectoryEntry {
|
|
|
|
explicit PageDirectoryEntry(dword* pde) : m_pde(pde) { }
|
|
|
|
|
|
|
|
dword* pageTableBase() { return reinterpret_cast<dword*>(raw() & 0xfffff000u); }
|
|
|
|
void setPageTableBase(dword value)
|
|
|
|
{
|
|
|
|
*m_pde &= 0xfff;
|
|
|
|
*m_pde |= value & 0xfffff000;
|
|
|
|
}
|
|
|
|
|
|
|
|
dword raw() const { return *m_pde; }
|
|
|
|
dword* ptr() { return m_pde; }
|
|
|
|
|
|
|
|
enum Flags {
|
|
|
|
Present = 1 << 0,
|
|
|
|
ReadWrite = 1 << 1,
|
|
|
|
UserSupervisor = 1 << 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isPresent() const { return raw() & Present; }
|
|
|
|
void setPresent(bool b) { setBit(Present, b); }
|
|
|
|
|
|
|
|
bool isUserAllowed() const { return raw() & UserSupervisor; }
|
|
|
|
void setUserAllowed(bool b) { setBit(UserSupervisor, b); }
|
|
|
|
|
|
|
|
bool isWritable() const { return raw() & ReadWrite; }
|
|
|
|
void setWritable(bool b) { setBit(ReadWrite, b); }
|
|
|
|
|
|
|
|
void setBit(byte bit, bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
*m_pde |= bit;
|
|
|
|
else
|
|
|
|
*m_pde &= ~bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
dword* m_pde;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PageTableEntry {
|
|
|
|
explicit PageTableEntry(dword* pte) : m_pte(pte) { }
|
|
|
|
|
|
|
|
dword* physicalPageBase() { return reinterpret_cast<dword*>(raw() & 0xfffff000u); }
|
|
|
|
void setPhysicalPageBase(dword value)
|
|
|
|
{
|
|
|
|
*m_pte &= 0xfffu;
|
|
|
|
*m_pte |= value & 0xfffff000u;
|
|
|
|
}
|
|
|
|
|
|
|
|
dword raw() const { return *m_pte; }
|
|
|
|
dword* ptr() { return m_pte; }
|
|
|
|
|
|
|
|
enum Flags {
|
|
|
|
Present = 1 << 0,
|
|
|
|
ReadWrite = 1 << 1,
|
|
|
|
UserSupervisor = 1 << 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isPresent() const { return raw() & Present; }
|
|
|
|
void setPresent(bool b) { setBit(Present, b); }
|
|
|
|
|
|
|
|
bool isUserAllowed() const { return raw() & UserSupervisor; }
|
|
|
|
void setUserAllowed(bool b) { setBit(UserSupervisor, b); }
|
|
|
|
|
|
|
|
bool isWritable() const { return raw() & ReadWrite; }
|
|
|
|
void setWritable(bool b) { setBit(ReadWrite, b); }
|
|
|
|
|
|
|
|
void setBit(byte bit, bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
*m_pte |= bit;
|
|
|
|
else
|
|
|
|
*m_pte &= ~bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
dword* m_pte;
|
|
|
|
};
|
|
|
|
|
2018-11-01 23:04:34 +01:00
|
|
|
PageTableEntry ensurePTE(PageDirectory*, LinearAddress);
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2018-11-01 23:04:34 +01:00
|
|
|
PageDirectory* m_kernel_page_directory;
|
2018-10-17 23:13:55 +02:00
|
|
|
dword* m_pageTableZero;
|
2018-10-18 13:05:00 +02:00
|
|
|
dword* m_pageTableOne;
|
|
|
|
|
2018-11-01 11:30:48 +01:00
|
|
|
LinearAddress m_next_laddr;
|
|
|
|
|
2018-11-05 10:23:00 +01:00
|
|
|
Vector<RetainPtr<PhysicalPage>> m_free_physical_pages;
|
2018-10-17 23:13:55 +02:00
|
|
|
};
|
2018-11-01 11:44:21 +01:00
|
|
|
|
|
|
|
struct KernelPagingScope {
|
|
|
|
KernelPagingScope() { MM.enter_kernel_paging_scope(); }
|
2018-11-01 13:15:46 +01:00
|
|
|
~KernelPagingScope() { MM.enter_process_paging_scope(*current); }
|
2018-11-01 11:44:21 +01:00
|
|
|
};
|
|
|
|
|
2018-11-01 23:04:34 +01:00
|
|
|
struct ProcessPagingScope {
|
|
|
|
ProcessPagingScope(Process& process) { MM.enter_process_paging_scope(process); }
|
|
|
|
~ProcessPagingScope() { MM.enter_process_paging_scope(*current); }
|
2018-11-01 11:44:21 +01:00
|
|
|
};
|