2018-10-18 00:13:55 +03:00
|
|
|
#include "MemoryManager.h"
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/kstdio.h>
|
|
|
|
#include <AK/kmalloc.h>
|
|
|
|
#include "i386.h"
|
|
|
|
#include "StdLib.h"
|
2018-11-01 15:15:46 +03:00
|
|
|
#include "Process.h"
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-11-01 13:30:48 +03:00
|
|
|
//#define MM_DEBUG
|
|
|
|
|
2018-10-18 00:13:55 +03:00
|
|
|
static MemoryManager* s_the;
|
|
|
|
|
2018-10-27 15:56:52 +03:00
|
|
|
MemoryManager& MM
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::MemoryManager()
|
|
|
|
{
|
2018-11-02 01:04:34 +03:00
|
|
|
m_kernel_page_directory = (PageDirectory*)0x4000;
|
2018-10-18 00:13:55 +03:00
|
|
|
m_pageTableZero = (dword*)0x6000;
|
2018-10-18 14:05:00 +03:00
|
|
|
m_pageTableOne = (dword*)0x7000;
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-11-01 13:30:48 +03:00
|
|
|
m_next_laddr.set(0xd0000000);
|
|
|
|
|
2018-10-18 00:13:55 +03:00
|
|
|
initializePaging();
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
void MemoryManager::populate_page_directory(Process& process)
|
2018-11-01 11:01:51 +03:00
|
|
|
{
|
2018-11-02 01:04:34 +03:00
|
|
|
memset(process.m_page_directory, 0, sizeof(PageDirectory));
|
|
|
|
process.m_page_directory[0] = m_kernel_page_directory[0];
|
|
|
|
process.m_page_directory[1] = m_kernel_page_directory[1];
|
|
|
|
}
|
2018-11-01 11:01:51 +03:00
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
void MemoryManager::release_page_directory(Process& process)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
for (size_t i = 0; i < 1024; ++i) {
|
|
|
|
auto paddr = process.m_page_directory->physical_addresses[i];
|
|
|
|
if (!paddr.is_null())
|
|
|
|
m_freePages.append(paddr);
|
|
|
|
}
|
2018-11-01 11:01:51 +03:00
|
|
|
}
|
|
|
|
|
2018-10-18 00:13:55 +03:00
|
|
|
void MemoryManager::initializePaging()
|
|
|
|
{
|
|
|
|
static_assert(sizeof(MemoryManager::PageDirectoryEntry) == 4);
|
|
|
|
static_assert(sizeof(MemoryManager::PageTableEntry) == 4);
|
|
|
|
memset(m_pageTableZero, 0, 4096);
|
2018-10-18 14:05:00 +03:00
|
|
|
memset(m_pageTableOne, 0, 4096);
|
2018-11-02 01:04:34 +03:00
|
|
|
memset(m_kernel_page_directory, 0, 8192);
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-10-31 22:10:39 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-01 13:30:48 +03:00
|
|
|
kprintf("MM: Kernel page directory @ %p\n", m_kernel_page_directory);
|
2018-10-31 22:10:39 +03:00
|
|
|
#endif
|
2018-10-18 14:05:00 +03:00
|
|
|
|
2018-10-21 22:57:59 +03:00
|
|
|
// Make null dereferences crash.
|
|
|
|
protectMap(LinearAddress(0), 4 * KB);
|
|
|
|
|
2018-11-01 11:01:51 +03:00
|
|
|
// The bottom 4 MB are identity mapped & supervisor only. Every process shares this mapping.
|
2018-10-21 22:57:59 +03:00
|
|
|
identityMap(LinearAddress(4096), 4 * MB);
|
2018-10-18 14:05:00 +03:00
|
|
|
|
2018-10-27 15:56:52 +03:00
|
|
|
for (size_t i = (4 * MB) + PAGE_SIZE; i < (8 * MB); i += PAGE_SIZE) {
|
2018-10-18 14:05:00 +03:00
|
|
|
m_freePages.append(PhysicalAddress(i));
|
|
|
|
}
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-11-01 13:30:48 +03:00
|
|
|
asm volatile("movl %%eax, %%cr3"::"a"(m_kernel_page_directory));
|
2018-10-18 00:13:55 +03:00
|
|
|
asm volatile(
|
|
|
|
"movl %cr0, %eax\n"
|
|
|
|
"orl $0x80000001, %eax\n"
|
|
|
|
"movl %eax, %cr0\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
void* MemoryManager::allocate_page_table()
|
2018-10-27 15:56:52 +03:00
|
|
|
{
|
|
|
|
auto ppages = allocatePhysicalPages(1);
|
|
|
|
dword address = ppages[0].get();
|
|
|
|
identityMap(LinearAddress(address), 4096);
|
2018-11-01 13:30:48 +03:00
|
|
|
memset((void*)address, 0, 4096);
|
2018-10-27 15:56:52 +03:00
|
|
|
return (void*)address;
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
auto MemoryManager::ensurePTE(PageDirectory* page_directory, LinearAddress laddr) -> PageTableEntry
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-11-02 01:04:34 +03:00
|
|
|
dword page_directory_index = (laddr.get() >> 22) & 0x3ff;
|
|
|
|
dword page_table_index = (laddr.get() >> 12) & 0x3ff;
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
PageDirectoryEntry pde = PageDirectoryEntry(&page_directory->entries[page_directory_index]);
|
2018-10-18 00:13:55 +03:00
|
|
|
if (!pde.isPresent()) {
|
2018-10-31 22:10:39 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-02 01:04:34 +03:00
|
|
|
dbgprintf("MM: PDE %u not present, allocating\n", page_directory_index);
|
2018-10-31 22:10:39 +03:00
|
|
|
#endif
|
2018-11-02 01:04:34 +03:00
|
|
|
if (page_directory_index == 0) {
|
2018-10-18 00:13:55 +03:00
|
|
|
pde.setPageTableBase((dword)m_pageTableZero);
|
2018-11-01 14:45:51 +03:00
|
|
|
pde.setUserAllowed(false);
|
2018-10-18 00:13:55 +03:00
|
|
|
pde.setPresent(true);
|
|
|
|
pde.setWritable(true);
|
2018-11-02 01:04:34 +03:00
|
|
|
} else if (page_directory_index == 1) {
|
2018-10-18 14:05:00 +03:00
|
|
|
pde.setPageTableBase((dword)m_pageTableOne);
|
2018-11-01 14:45:51 +03:00
|
|
|
pde.setUserAllowed(false);
|
2018-10-18 14:05:00 +03:00
|
|
|
pde.setPresent(true);
|
2018-10-18 15:53:00 +03:00
|
|
|
pde.setWritable(true);
|
2018-10-18 00:13:55 +03:00
|
|
|
} else {
|
2018-11-02 01:04:34 +03:00
|
|
|
auto* page_table = allocate_page_table();
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-02 01:04:34 +03:00
|
|
|
dbgprintf("MM: PDE %x allocated page table #%u (for laddr=%p) at %p\n", page_directory, page_directory_index, laddr.get(), page_table);
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
2018-11-02 01:04:34 +03:00
|
|
|
page_directory->physical_addresses[page_directory_index] = PhysicalAddress((dword)page_table);
|
|
|
|
pde.setPageTableBase((dword)page_table);
|
2018-10-27 15:56:52 +03:00
|
|
|
pde.setUserAllowed(true);
|
|
|
|
pde.setPresent(true);
|
|
|
|
pde.setWritable(true);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-02 01:04:34 +03:00
|
|
|
return PageTableEntry(&pde.pageTableBase()[page_table_index]);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2018-10-21 22:57:59 +03:00
|
|
|
void MemoryManager::protectMap(LinearAddress linearAddress, size_t length)
|
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-21 22:57:59 +03:00
|
|
|
// FIXME: ASSERT(linearAddress is 4KB aligned);
|
|
|
|
for (dword offset = 0; offset < length; offset += 4096) {
|
|
|
|
auto pteAddress = linearAddress.offset(offset);
|
2018-11-01 13:30:48 +03:00
|
|
|
auto pte = ensurePTE(m_kernel_page_directory, pteAddress);
|
2018-10-21 22:57:59 +03:00
|
|
|
pte.setPhysicalPageBase(pteAddress.get());
|
|
|
|
pte.setUserAllowed(false);
|
|
|
|
pte.setPresent(false);
|
|
|
|
pte.setWritable(false);
|
2018-10-23 16:53:11 +03:00
|
|
|
flushTLB(pteAddress);
|
2018-10-21 22:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 00:13:55 +03:00
|
|
|
void MemoryManager::identityMap(LinearAddress linearAddress, size_t length)
|
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-18 00:13:55 +03:00
|
|
|
// FIXME: ASSERT(linearAddress is 4KB aligned);
|
|
|
|
for (dword offset = 0; offset < length; offset += 4096) {
|
|
|
|
auto pteAddress = linearAddress.offset(offset);
|
2018-11-01 13:30:48 +03:00
|
|
|
auto pte = ensurePTE(m_kernel_page_directory, pteAddress);
|
2018-10-18 00:13:55 +03:00
|
|
|
pte.setPhysicalPageBase(pteAddress.get());
|
2018-11-01 14:45:51 +03:00
|
|
|
pte.setUserAllowed(false);
|
2018-10-18 00:13:55 +03:00
|
|
|
pte.setPresent(true);
|
|
|
|
pte.setWritable(true);
|
2018-10-23 16:53:11 +03:00
|
|
|
flushTLB(pteAddress);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::initialize()
|
|
|
|
{
|
|
|
|
s_the = new MemoryManager;
|
|
|
|
}
|
|
|
|
|
2018-10-18 14:05:00 +03:00
|
|
|
PageFaultResponse MemoryManager::handlePageFault(const PageFault& fault)
|
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-10-31 22:10:39 +03:00
|
|
|
kprintf("MM: handlePageFault(%w) at laddr=%p\n", fault.code(), fault.address().get());
|
2018-10-18 14:05:00 +03:00
|
|
|
if (fault.isNotPresent()) {
|
|
|
|
kprintf(" >> NP fault!\n");
|
|
|
|
} else if (fault.isProtectionViolation()) {
|
|
|
|
kprintf(" >> PV fault!\n");
|
|
|
|
}
|
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2018-10-28 12:26:07 +03:00
|
|
|
void MemoryManager::registerZone(Zone& zone)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
m_zones.set(&zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregisterZone(Zone& zone)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
m_zones.remove(&zone);
|
|
|
|
m_freePages.append(move(zone.m_pages));
|
|
|
|
}
|
|
|
|
|
|
|
|
Zone::Zone(Vector<PhysicalAddress>&& pages)
|
|
|
|
: m_pages(move(pages))
|
|
|
|
{
|
|
|
|
MM.registerZone(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Zone::~Zone()
|
|
|
|
{
|
|
|
|
MM.unregisterZone(*this);
|
|
|
|
}
|
|
|
|
|
2018-10-18 14:05:00 +03:00
|
|
|
RetainPtr<Zone> MemoryManager::createZone(size_t size)
|
|
|
|
{
|
2018-10-28 12:26:07 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-18 14:05:00 +03:00
|
|
|
auto pages = allocatePhysicalPages(ceilDiv(size, PAGE_SIZE));
|
|
|
|
if (pages.isEmpty()) {
|
2018-10-31 22:10:39 +03:00
|
|
|
kprintf("MM: createZone: no physical pages for size %u\n", size);
|
2018-10-18 14:05:00 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return adopt(*new Zone(move(pages)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<PhysicalAddress> MemoryManager::allocatePhysicalPages(size_t count)
|
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-18 14:05:00 +03:00
|
|
|
if (count > m_freePages.size())
|
|
|
|
return { };
|
|
|
|
|
|
|
|
Vector<PhysicalAddress> pages;
|
|
|
|
pages.ensureCapacity(count);
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
pages.append(m_freePages.takeLast());
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2018-11-01 13:30:48 +03:00
|
|
|
void MemoryManager::enter_kernel_paging_scope()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
current->m_tss.cr3 = (dword)m_kernel_page_directory;
|
|
|
|
asm volatile("movl %%eax, %%cr3"::"a"(m_kernel_page_directory));
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
void MemoryManager::enter_process_paging_scope(Process& process)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2018-11-02 01:04:34 +03:00
|
|
|
current->m_tss.cr3 = (dword)process.m_page_directory;
|
|
|
|
asm volatile("movl %%eax, %%cr3"::"a"(process.m_page_directory));
|
2018-11-01 13:30:48 +03:00
|
|
|
}
|
|
|
|
|
2018-10-23 16:53:11 +03:00
|
|
|
void MemoryManager::flushEntireTLB()
|
2018-10-23 12:03:56 +03:00
|
|
|
{
|
|
|
|
asm volatile(
|
|
|
|
"mov %cr3, %eax\n"
|
|
|
|
"mov %eax, %cr3\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:53:11 +03:00
|
|
|
void MemoryManager::flushTLB(LinearAddress laddr)
|
|
|
|
{
|
|
|
|
asm volatile("invlpg %0": :"m" (*(char*)laddr.get()));
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
void MemoryManager::map_region_at_address(PageDirectory* page_directory, Region& region, LinearAddress laddr, bool user_allowed)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto& zone = *region.zone;
|
|
|
|
for (size_t i = 0; i < zone.m_pages.size(); ++i) {
|
|
|
|
auto page_laddr = laddr.offset(i * PAGE_SIZE);
|
|
|
|
auto pte = ensurePTE(page_directory, page_laddr);
|
|
|
|
pte.setPhysicalPageBase(zone.m_pages[i].get());
|
|
|
|
pte.setPresent(true);
|
|
|
|
pte.setWritable(true);
|
2018-11-01 15:15:46 +03:00
|
|
|
pte.setUserAllowed(user_allowed);
|
2018-11-01 13:30:48 +03:00
|
|
|
flushTLB(page_laddr);
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: >> map_region_at_address (PD=%x) L%x => P%x\n", page_directory, page_laddr, zone.m_pages[i].get());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 01:04:34 +03:00
|
|
|
void MemoryManager::unmap_range(PageDirectory* page_directory, LinearAddress laddr, size_t size)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
ASSERT((size % PAGE_SIZE) == 0);
|
|
|
|
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
size_t numPages = size / 4096;
|
|
|
|
for (size_t i = 0; i < numPages; ++i) {
|
|
|
|
auto page_laddr = laddr.offset(i * PAGE_SIZE);
|
|
|
|
auto pte = ensurePTE(page_directory, page_laddr);
|
|
|
|
pte.setPhysicalPageBase(0);
|
|
|
|
pte.setPresent(false);
|
|
|
|
pte.setWritable(false);
|
|
|
|
pte.setUserAllowed(false);
|
|
|
|
flushTLB(page_laddr);
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: << unmap_range L%x =/> 0\n", page_laddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearAddress MemoryManager::allocate_linear_address_range(size_t size)
|
|
|
|
{
|
|
|
|
ASSERT((size % PAGE_SIZE) == 0);
|
|
|
|
|
|
|
|
// FIXME: Recycle ranges!
|
|
|
|
auto laddr = m_next_laddr;
|
|
|
|
m_next_laddr.set(m_next_laddr.get() + size);
|
|
|
|
return laddr;
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
byte* MemoryManager::create_kernel_alias_for_region(Region& region)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto laddr = allocate_linear_address_range(region.size);
|
2018-11-01 15:15:46 +03:00
|
|
|
map_region_at_address(m_kernel_page_directory, region, laddr, false);
|
2018-11-02 01:04:34 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Created alias L%x for L%x\n", laddr.get(), region.linearAddress.get());
|
|
|
|
#endif
|
2018-11-01 13:30:48 +03:00
|
|
|
return laddr.asPtr();
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
void MemoryManager::remove_kernel_alias_for_region(Region& region, byte* addr)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
unmap_range(m_kernel_page_directory, LinearAddress((dword)addr), region.size);
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
bool MemoryManager::unmapRegion(Process& process, Region& region)
|
2018-10-23 11:12:50 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-23 11:12:50 +03:00
|
|
|
auto& zone = *region.zone;
|
|
|
|
for (size_t i = 0; i < zone.m_pages.size(); ++i) {
|
|
|
|
auto laddr = region.linearAddress.offset(i * PAGE_SIZE);
|
2018-11-02 01:04:34 +03:00
|
|
|
auto pte = ensurePTE(process.m_page_directory, laddr);
|
2018-10-23 11:12:50 +03:00
|
|
|
pte.setPhysicalPageBase(0);
|
|
|
|
pte.setPresent(false);
|
|
|
|
pte.setWritable(false);
|
|
|
|
pte.setUserAllowed(false);
|
2018-10-23 16:53:11 +03:00
|
|
|
flushTLB(laddr);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
//dbgprintf("MM: >> Unmapped L%x => P%x <<\n", laddr, zone.m_pages[i].get());
|
|
|
|
#endif
|
2018-10-27 15:56:52 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
bool MemoryManager::unmapSubregion(Process& process, Subregion& subregion)
|
2018-10-27 15:56:52 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
size_t numPages = subregion.size / 4096;
|
|
|
|
ASSERT(numPages);
|
|
|
|
for (size_t i = 0; i < numPages; ++i) {
|
|
|
|
auto laddr = subregion.linearAddress.offset(i * PAGE_SIZE);
|
2018-11-02 01:04:34 +03:00
|
|
|
auto pte = ensurePTE(process.m_page_directory, laddr);
|
2018-10-27 15:56:52 +03:00
|
|
|
pte.setPhysicalPageBase(0);
|
|
|
|
pte.setPresent(false);
|
|
|
|
pte.setWritable(false);
|
|
|
|
pte.setUserAllowed(false);
|
|
|
|
flushTLB(laddr);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
//dbgprintf("MM: >> Unmapped subregion %s L%x => P%x <<\n", subregion.name.characters(), laddr, zone.m_pages[i].get());
|
|
|
|
#endif
|
2018-10-23 11:12:50 +03:00
|
|
|
}
|
|
|
|
return true;
|
2018-10-27 15:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
bool MemoryManager::mapSubregion(Process& process, Subregion& subregion)
|
2018-10-27 15:56:52 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto& region = *subregion.region;
|
|
|
|
auto& zone = *region.zone;
|
|
|
|
size_t firstPage = subregion.offset / 4096;
|
|
|
|
size_t numPages = subregion.size / 4096;
|
|
|
|
ASSERT(numPages);
|
|
|
|
for (size_t i = 0; i < numPages; ++i) {
|
|
|
|
auto laddr = subregion.linearAddress.offset(i * PAGE_SIZE);
|
2018-11-02 01:04:34 +03:00
|
|
|
auto pte = ensurePTE(process.m_page_directory, laddr);
|
2018-10-27 15:56:52 +03:00
|
|
|
pte.setPhysicalPageBase(zone.m_pages[firstPage + i].get());
|
|
|
|
pte.setPresent(true);
|
|
|
|
pte.setWritable(true);
|
2018-11-01 13:30:48 +03:00
|
|
|
pte.setUserAllowed(true);
|
2018-10-27 15:56:52 +03:00
|
|
|
flushTLB(laddr);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
//dbgprintf("MM: >> Mapped subregion %s L%x => P%x (%u into region)\n", subregion.name.characters(), laddr, zone.m_pages[firstPage + i].get(), subregion.offset);
|
|
|
|
#endif
|
2018-10-27 15:56:52 +03:00
|
|
|
}
|
2018-10-18 14:05:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:21:02 +03:00
|
|
|
bool MemoryManager::mapRegion(Process& process, Region& region)
|
2018-10-22 16:42:39 +03:00
|
|
|
{
|
2018-11-02 01:04:34 +03:00
|
|
|
map_region_at_address(process.m_page_directory, region, region.linearAddress, true);
|
2018-10-22 16:42:39 +03:00
|
|
|
return true;
|
2018-11-01 14:45:51 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
bool MemoryManager::validate_user_read(const Process& process, LinearAddress laddr) const
|
2018-11-01 14:45:51 +03:00
|
|
|
{
|
|
|
|
dword pageDirectoryIndex = (laddr.get() >> 22) & 0x3ff;
|
|
|
|
dword pageTableIndex = (laddr.get() >> 12) & 0x3ff;
|
2018-11-02 01:04:34 +03:00
|
|
|
auto pde = PageDirectoryEntry(&process.m_page_directory->entries[pageDirectoryIndex]);
|
2018-11-01 14:45:51 +03:00
|
|
|
if (!pde.isPresent())
|
|
|
|
return false;
|
|
|
|
auto pte = PageTableEntry(&pde.pageTableBase()[pageTableIndex]);
|
|
|
|
if (!pte.isPresent())
|
|
|
|
return false;
|
|
|
|
if (!pte.isUserAllowed())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
bool MemoryManager::validate_user_write(const Process& process, LinearAddress laddr) const
|
2018-11-01 14:45:51 +03:00
|
|
|
{
|
|
|
|
dword pageDirectoryIndex = (laddr.get() >> 22) & 0x3ff;
|
|
|
|
dword pageTableIndex = (laddr.get() >> 12) & 0x3ff;
|
2018-11-02 01:04:34 +03:00
|
|
|
auto pde = PageDirectoryEntry(&process.m_page_directory->entries[pageDirectoryIndex]);
|
2018-11-01 14:45:51 +03:00
|
|
|
if (!pde.isPresent())
|
|
|
|
return false;
|
|
|
|
auto pte = PageTableEntry(&pde.pageTableBase()[pageTableIndex]);
|
|
|
|
if (!pte.isPresent())
|
|
|
|
return false;
|
|
|
|
if (!pte.isUserAllowed())
|
|
|
|
return false;
|
|
|
|
if (!pte.isWritable())
|
|
|
|
return false;
|
|
|
|
return true;
|
2018-10-22 16:42:39 +03:00
|
|
|
}
|