2019-06-07 09:43:58 +00:00
|
|
|
#include "CMOS.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "StdLib.h"
|
2018-10-17 21:13:55 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/kstdio.h>
|
2019-06-07 18:02:01 +00:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-06-09 09:48:58 +00:00
|
|
|
#include <Kernel/Multiboot.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2018-11-01 10:30:48 +00:00
|
|
|
//#define MM_DEBUG
|
2018-11-05 12:48:07 +00:00
|
|
|
//#define PAGE_FAULT_DEBUG
|
2018-11-01 10:30:48 +00:00
|
|
|
|
2018-10-17 21:13:55 +00:00
|
|
|
static MemoryManager* s_the;
|
|
|
|
|
2018-10-27 12:56:52 +00:00
|
|
|
MemoryManager& MM
|
2018-10-17 21:13:55 +00:00
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::MemoryManager()
|
|
|
|
{
|
2019-01-22 04:01:00 +00:00
|
|
|
m_kernel_page_directory = PageDirectory::create_at_fixed_address(PhysicalAddress(0x4000));
|
2018-12-03 00:38:22 +00:00
|
|
|
m_page_table_zero = (dword*)0x6000;
|
2019-06-09 09:48:58 +00:00
|
|
|
m_page_table_one = (dword*)0x7000;
|
2018-11-01 10:30:48 +00:00
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
initialize_paging();
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
kprintf("MM initialized.\n");
|
2018-10-17 21:13:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
void MemoryManager::populate_page_directory(PageDirectory& page_directory)
|
|
|
|
{
|
2019-01-01 01:09:43 +00:00
|
|
|
page_directory.m_directory_page = allocate_supervisor_physical_page();
|
2018-12-31 13:58:03 +00:00
|
|
|
page_directory.entries()[0] = kernel_page_directory().entries()[0];
|
2019-06-09 09:48:58 +00:00
|
|
|
page_directory.entries()[1] = kernel_page_directory().entries()[1];
|
2019-03-11 10:11:29 +00:00
|
|
|
// Defer to the kernel page tables for 0xC0000000-0xFFFFFFFF
|
|
|
|
for (int i = 768; i < 1024; ++i)
|
|
|
|
page_directory.entries()[i] = kernel_page_directory().entries()[i];
|
2018-11-01 08:01:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
void MemoryManager::initialize_paging()
|
2018-10-17 21:13:55 +00:00
|
|
|
{
|
|
|
|
static_assert(sizeof(MemoryManager::PageDirectoryEntry) == 4);
|
|
|
|
static_assert(sizeof(MemoryManager::PageTableEntry) == 4);
|
2018-12-03 00:38:22 +00:00
|
|
|
memset(m_page_table_zero, 0, PAGE_SIZE);
|
2019-06-09 09:48:58 +00:00
|
|
|
memset(m_page_table_one, 0, PAGE_SIZE);
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2018-10-31 19:10:39 +00:00
|
|
|
#ifdef MM_DEBUG
|
2018-12-31 13:58:03 +00:00
|
|
|
dbgprintf("MM: Kernel page directory @ %p\n", kernel_page_directory().cr3());
|
2018-10-31 19:10:39 +00:00
|
|
|
#endif
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Protect against null dereferences\n");
|
|
|
|
#endif
|
2018-10-21 19:57:59 +00:00
|
|
|
// Make null dereferences crash.
|
2019-06-07 10:56:50 +00:00
|
|
|
map_protected(VirtualAddress(0), PAGE_SIZE);
|
2018-10-21 19:57:59 +00:00
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-09 09:48:58 +00:00
|
|
|
dbgprintf("MM: Identity map bottom 5MB\n");
|
2018-12-31 13:58:03 +00:00
|
|
|
#endif
|
2019-06-09 09:48:58 +00:00
|
|
|
// The bottom 5 MB (except for the null page) are identity mapped & supervisor only.
|
2019-01-01 01:09:43 +00:00
|
|
|
// Every process shares these mappings.
|
2019-06-09 09:48:58 +00:00
|
|
|
create_identity_mapping(kernel_page_directory(), VirtualAddress(PAGE_SIZE), (5 * MB) - PAGE_SIZE);
|
2019-01-15 23:44:09 +00:00
|
|
|
|
|
|
|
// Basic memory map:
|
|
|
|
// 0 -> 512 kB Kernel code. Root page directory & PDE 0.
|
2019-01-31 02:57:06 +00:00
|
|
|
// (last page before 1MB) Used by quickmap_page().
|
2019-06-09 09:48:58 +00:00
|
|
|
// 1 MB -> 3 MB kmalloc_eternal() space.
|
|
|
|
// 3 MB -> 4 MB kmalloc() space.
|
|
|
|
// 4 MB -> 5 MB Supervisor physical pages (available for allocation!)
|
|
|
|
// 5 MB -> 0xc0000000 Userspace physical pages (available for allocation!)
|
2019-06-13 19:42:12 +00:00
|
|
|
// 0xc0000000-0xffffffff Kernel-only virtual address space
|
2019-05-14 09:51:00 +00:00
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Quickmap will use %p\n", m_quickmap_addr.get());
|
|
|
|
#endif
|
|
|
|
m_quickmap_addr = VirtualAddress((1 * MB) - PAGE_SIZE);
|
|
|
|
|
|
|
|
RetainPtr<PhysicalRegion> region = nullptr;
|
|
|
|
bool region_is_super = false;
|
|
|
|
|
2019-06-09 09:48:58 +00:00
|
|
|
for (auto* mmap = (multiboot_memory_map_t*)multiboot_info_ptr->mmap_addr; (unsigned long)mmap < multiboot_info_ptr->mmap_addr + multiboot_info_ptr->mmap_length; mmap = (multiboot_memory_map_t*)((unsigned long)mmap + mmap->size + sizeof(mmap->size))) {
|
|
|
|
kprintf("MM: Multiboot mmap: base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n",
|
|
|
|
(dword)(mmap->addr >> 32),
|
|
|
|
(dword)(mmap->addr & 0xffffffff),
|
|
|
|
(dword)(mmap->len >> 32),
|
|
|
|
(dword)(mmap->len & 0xffffffff),
|
|
|
|
(dword)mmap->type);
|
|
|
|
|
|
|
|
if (mmap->type != MULTIBOOT_MEMORY_AVAILABLE)
|
|
|
|
continue;
|
2019-06-11 11:13:02 +00:00
|
|
|
|
2019-06-09 09:48:58 +00:00
|
|
|
// FIXME: Maybe make use of stuff below the 1MB mark?
|
|
|
|
if (mmap->addr < (1 * MB))
|
|
|
|
continue;
|
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
kprintf("MM: considering memory at %p - %p\n",
|
|
|
|
(dword)mmap->addr, (dword)(mmap->addr + mmap->len));
|
|
|
|
#endif
|
|
|
|
|
2019-06-09 09:48:58 +00:00
|
|
|
for (size_t page_base = mmap->addr; page_base < (mmap->addr + mmap->len); page_base += PAGE_SIZE) {
|
2019-06-11 11:13:02 +00:00
|
|
|
auto addr = PhysicalAddress(page_base);
|
|
|
|
|
|
|
|
if (page_base < 4 * MB) {
|
|
|
|
// nothing
|
|
|
|
} else if (page_base >= 4 * MB && page_base < 5 * MB) {
|
|
|
|
if (region.is_null() || !region_is_super || region->upper().offset(PAGE_SIZE) != addr) {
|
|
|
|
m_super_physical_regions.append(PhysicalRegion::create(addr, addr));
|
|
|
|
region = m_super_physical_regions.last();
|
|
|
|
region_is_super = true;
|
|
|
|
} else {
|
|
|
|
region->expand(region->lower(), addr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (region.is_null() || region_is_super || region->upper().offset(PAGE_SIZE) != addr) {
|
|
|
|
m_user_physical_regions.append(PhysicalRegion::create(addr, addr));
|
|
|
|
region = m_user_physical_regions.last();
|
|
|
|
region_is_super = false;
|
|
|
|
} else {
|
|
|
|
region->expand(region->lower(), addr);
|
|
|
|
}
|
2019-06-09 09:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 13:58:03 +00:00
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
for (auto& region : m_super_physical_regions)
|
|
|
|
m_super_physical_pages += region->finalize_capacity();
|
|
|
|
|
|
|
|
for (auto& region : m_user_physical_regions)
|
|
|
|
m_user_physical_pages += region->finalize_capacity();
|
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Installing page directory\n");
|
|
|
|
#endif
|
2019-03-10 22:40:09 +00:00
|
|
|
|
2019-06-07 09:43:58 +00:00
|
|
|
asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));
|
2018-10-17 21:13:55 +00:00
|
|
|
asm volatile(
|
2019-01-31 02:57:06 +00:00
|
|
|
"movl %%cr0, %%eax\n"
|
|
|
|
"orl $0x80000001, %%eax\n"
|
2019-06-07 09:43:58 +00:00
|
|
|
"movl %%eax, %%cr0\n" ::
|
|
|
|
: "%eax", "memory");
|
2019-03-23 21:03:17 +00:00
|
|
|
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Paging initialized.\n");
|
|
|
|
#endif
|
2018-10-17 21:13:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 09:23:00 +00:00
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_page_table(PageDirectory& page_directory, unsigned index)
|
2018-10-27 12:56:52 +00:00
|
|
|
{
|
2018-12-31 14:10:12 +00:00
|
|
|
ASSERT(!page_directory.m_physical_pages.contains(index));
|
2019-01-01 01:09:43 +00:00
|
|
|
auto physical_page = allocate_supervisor_physical_page();
|
2018-11-10 21:14:41 +00:00
|
|
|
if (!physical_page)
|
|
|
|
return nullptr;
|
2019-01-31 16:31:23 +00:00
|
|
|
page_directory.m_physical_pages.set(index, physical_page.copy_ref());
|
2018-12-31 14:10:12 +00:00
|
|
|
return physical_page;
|
2018-11-02 23:31:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::remove_identity_mapping(PageDirectory& page_directory, VirtualAddress vaddr, size_t size)
|
2018-11-02 23:31:42 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:56:50 +00:00
|
|
|
// FIXME: ASSERT(vaddr is 4KB aligned);
|
2018-11-02 23:31:42 +00:00
|
|
|
for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
|
2019-06-07 10:56:50 +00:00
|
|
|
auto pte_address = vaddr.offset(offset);
|
2019-01-01 01:09:43 +00:00
|
|
|
auto pte = ensure_pte(page_directory, pte_address);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
flush_tlb(pte_address);
|
2018-11-02 23:31:42 +00:00
|
|
|
}
|
2018-10-27 12:56:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
auto MemoryManager::ensure_pte(PageDirectory& page_directory, VirtualAddress vaddr) -> PageTableEntry
|
2018-10-17 21:13:55 +00:00
|
|
|
{
|
2018-10-25 08:15:28 +00:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-06-07 10:56:50 +00:00
|
|
|
dword page_directory_index = (vaddr.get() >> 22) & 0x3ff;
|
|
|
|
dword page_table_index = (vaddr.get() >> 12) & 0x3ff;
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
PageDirectoryEntry pde = PageDirectoryEntry(&page_directory.entries()[page_directory_index]);
|
2018-12-03 00:38:22 +00:00
|
|
|
if (!pde.is_present()) {
|
2018-10-31 19:10:39 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: PDE %u not present (requested for L%x), allocating\n", page_directory_index, vaddr.get());
|
2018-10-31 19:10:39 +00:00
|
|
|
#endif
|
2018-11-01 22:04:34 +00:00
|
|
|
if (page_directory_index == 0) {
|
2019-04-14 00:36:06 +00:00
|
|
|
ASSERT(&page_directory == m_kernel_page_directory);
|
2019-01-31 16:31:23 +00:00
|
|
|
pde.set_page_table_base((dword)m_page_table_zero);
|
2018-12-03 00:38:22 +00:00
|
|
|
pde.set_user_allowed(false);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
2019-06-09 09:48:58 +00:00
|
|
|
} else if (page_directory_index == 1) {
|
|
|
|
ASSERT(&page_directory == m_kernel_page_directory);
|
|
|
|
pde.set_page_table_base((dword)m_page_table_one);
|
|
|
|
pde.set_user_allowed(false);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
2018-10-17 21:13:55 +00:00
|
|
|
} else {
|
2019-03-10 14:25:33 +00:00
|
|
|
//ASSERT(&page_directory != m_kernel_page_directory.ptr());
|
2018-12-31 13:58:03 +00:00
|
|
|
auto page_table = allocate_page_table(page_directory, page_directory_index);
|
2018-11-01 10:30:48 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-01 01:09:43 +00:00
|
|
|
dbgprintf("MM: PD K%x (%s) at P%x allocated page table #%u (for L%x) at P%x\n",
|
2018-12-31 13:58:03 +00:00
|
|
|
&page_directory,
|
2019-04-14 00:36:06 +00:00
|
|
|
&page_directory == m_kernel_page_directory ? "Kernel" : "User",
|
2019-01-01 01:09:43 +00:00
|
|
|
page_directory.cr3(),
|
2018-11-02 23:31:42 +00:00
|
|
|
page_directory_index,
|
2019-06-07 10:56:50 +00:00
|
|
|
vaddr.get(),
|
2018-11-05 09:23:00 +00:00
|
|
|
page_table->paddr().get());
|
2018-11-01 10:30:48 +00:00
|
|
|
#endif
|
2018-11-05 09:23:00 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
pde.set_page_table_base(page_table->paddr().get());
|
2018-12-03 00:38:22 +00:00
|
|
|
pde.set_user_allowed(true);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
2018-12-31 14:10:12 +00:00
|
|
|
page_directory.m_physical_pages.set(page_directory_index, move(page_table));
|
2018-10-17 21:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 16:31:23 +00:00
|
|
|
return PageTableEntry(&pde.page_table_base()[page_table_index]);
|
2018-10-17 21:13:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::map_protected(VirtualAddress vaddr, size_t length)
|
2018-10-21 19:57:59 +00:00
|
|
|
{
|
2018-10-25 08:15:28 +00:00
|
|
|
InterruptDisabler disabler;
|
2019-06-13 19:42:12 +00:00
|
|
|
ASSERT(vaddr.is_page_aligned());
|
2018-11-02 23:31:42 +00:00
|
|
|
for (dword offset = 0; offset < length; offset += PAGE_SIZE) {
|
2019-06-07 10:56:50 +00:00
|
|
|
auto pte_address = vaddr.offset(offset);
|
2019-01-31 16:31:23 +00:00
|
|
|
auto pte = ensure_pte(kernel_page_directory(), pte_address);
|
|
|
|
pte.set_physical_page_base(pte_address.get());
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
2019-01-31 16:31:23 +00:00
|
|
|
flush_tlb(pte_address);
|
2018-10-21 19:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::create_identity_mapping(PageDirectory& page_directory, VirtualAddress vaddr, size_t size)
|
2018-10-17 21:13:55 +00:00
|
|
|
{
|
2018-10-25 08:15:28 +00:00
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:56:50 +00:00
|
|
|
ASSERT((vaddr.get() & ~PAGE_MASK) == 0);
|
2018-11-02 23:31:42 +00:00
|
|
|
for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
|
2019-06-07 10:56:50 +00:00
|
|
|
auto pte_address = vaddr.offset(offset);
|
2019-01-31 16:31:23 +00:00
|
|
|
auto pte = ensure_pte(page_directory, pte_address);
|
|
|
|
pte.set_physical_page_base(pte_address.get());
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
2019-01-31 16:31:23 +00:00
|
|
|
page_directory.flush(pte_address);
|
2018-10-17 21:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::initialize()
|
|
|
|
{
|
|
|
|
s_the = new MemoryManager;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
Region* MemoryManager::region_from_vaddr(Process& process, VirtualAddress vaddr)
|
2018-11-05 12:48:07 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
if (vaddr.get() >= 0xc0000000) {
|
2019-05-14 09:51:00 +00:00
|
|
|
for (auto& region : MM.m_kernel_regions) {
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region->contains(vaddr))
|
2019-05-14 09:51:00 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:48:07 +00:00
|
|
|
// FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure!
|
|
|
|
for (auto& region : process.m_regions) {
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region->contains(vaddr))
|
2018-11-05 12:48:07 +00:00
|
|
|
return region.ptr();
|
|
|
|
}
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), vaddr.get(), process.page_directory().cr3());
|
2019-01-25 00:39:15 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
const Region* MemoryManager::region_from_vaddr(const Process& process, VirtualAddress vaddr)
|
2019-01-25 00:39:15 +00:00
|
|
|
{
|
2019-06-07 10:56:50 +00:00
|
|
|
if (vaddr.get() >= 0xc0000000) {
|
2019-05-14 09:51:00 +00:00
|
|
|
for (auto& region : MM.m_kernel_regions) {
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region->contains(vaddr))
|
2019-05-14 09:51:00 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:39:15 +00:00
|
|
|
// FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure!
|
|
|
|
for (auto& region : process.m_regions) {
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region->contains(vaddr))
|
2019-01-25 00:39:15 +00:00
|
|
|
return region.ptr();
|
|
|
|
}
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), vaddr.get(), process.page_directory().cr3());
|
2018-11-08 14:39:26 +00:00
|
|
|
return nullptr;
|
2018-11-05 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
bool MemoryManager::zero_page(Region& region, unsigned page_index_in_region)
|
2018-11-19 01:17:20 +00:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
auto& vmo = region.vmo();
|
2019-02-20 20:33:07 +00:00
|
|
|
auto& vmo_page = vmo.physical_pages()[region.first_page_index() + page_index_in_region];
|
|
|
|
sti();
|
|
|
|
LOCKER(vmo.m_paging_lock);
|
|
|
|
cli();
|
|
|
|
if (!vmo_page.is_null()) {
|
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf("MM: zero_page() but page already present. Fine with me!\n");
|
|
|
|
#endif
|
|
|
|
remap_region_page(region, page_index_in_region, true);
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-11 11:13:02 +00:00
|
|
|
auto physical_page = allocate_user_physical_page(ShouldZeroFill::Yes);
|
2018-11-19 01:17:20 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> ZERO P%x\n", physical_page->paddr().get());
|
|
|
|
#endif
|
2019-05-14 15:31:57 +00:00
|
|
|
region.set_should_cow(page_index_in_region, false);
|
2018-11-19 01:17:20 +00:00
|
|
|
vmo.physical_pages()[page_index_in_region] = move(physical_page);
|
2019-01-22 04:01:00 +00:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-19 01:17:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
bool MemoryManager::copy_on_write(Region& region, unsigned page_index_in_region)
|
2018-11-05 12:48:07 +00:00
|
|
|
{
|
2018-11-05 13:10:18 +00:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-11-08 13:35:30 +00:00
|
|
|
auto& vmo = region.vmo();
|
|
|
|
if (vmo.physical_pages()[page_index_in_region]->retain_count() == 1) {
|
2018-11-05 13:10:18 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> It's a COW page but nobody is sharing it anymore. Remap r/w\n");
|
|
|
|
#endif
|
2019-05-14 15:31:57 +00:00
|
|
|
region.set_should_cow(page_index_in_region, false);
|
2019-01-22 04:01:00 +00:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-05 13:10:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> It's a COW page and it's time to COW!\n");
|
|
|
|
#endif
|
2018-11-08 13:35:30 +00:00
|
|
|
auto physical_page_to_copy = move(vmo.physical_pages()[page_index_in_region]);
|
2019-06-11 11:13:02 +00:00
|
|
|
auto physical_page = allocate_user_physical_page(ShouldZeroFill::No);
|
2018-11-10 21:14:41 +00:00
|
|
|
byte* dest_ptr = quickmap_page(*physical_page);
|
2019-06-07 10:56:50 +00:00
|
|
|
const byte* src_ptr = region.vaddr().offset(page_index_in_region * PAGE_SIZE).as_ptr();
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2018-11-10 21:14:41 +00:00
|
|
|
dbgprintf(" >> COW P%x <- P%x\n", physical_page->paddr().get(), physical_page_to_copy->paddr().get());
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
|
|
|
memcpy(dest_ptr, src_ptr, PAGE_SIZE);
|
2018-11-10 21:14:41 +00:00
|
|
|
vmo.physical_pages()[page_index_in_region] = move(physical_page);
|
2018-11-05 12:48:07 +00:00
|
|
|
unquickmap_page();
|
2019-05-14 15:31:57 +00:00
|
|
|
region.set_should_cow(page_index_in_region, false);
|
2019-01-22 04:01:00 +00:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-05 12:48:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
bool MemoryManager::page_in_from_inode(Region& region, unsigned page_index_in_region)
|
2018-11-08 11:59:16 +00:00
|
|
|
{
|
2019-01-24 17:09:46 +00:00
|
|
|
ASSERT(region.page_directory());
|
2018-11-08 13:35:30 +00:00
|
|
|
auto& vmo = region.vmo();
|
|
|
|
ASSERT(!vmo.is_anonymous());
|
2019-01-16 11:57:07 +00:00
|
|
|
ASSERT(vmo.inode());
|
2019-02-03 00:36:25 +00:00
|
|
|
|
2018-11-08 20:20:09 +00:00
|
|
|
auto& vmo_page = vmo.physical_pages()[region.first_page_index() + page_index_in_region];
|
2019-02-03 00:36:25 +00:00
|
|
|
|
2019-02-05 10:13:34 +00:00
|
|
|
InterruptFlagSaver saver;
|
2019-02-03 00:36:25 +00:00
|
|
|
|
2019-02-05 10:13:34 +00:00
|
|
|
sti();
|
2019-02-03 00:36:25 +00:00
|
|
|
LOCKER(vmo.m_paging_lock);
|
2019-02-05 10:13:34 +00:00
|
|
|
cli();
|
2019-02-03 00:36:25 +00:00
|
|
|
|
|
|
|
if (!vmo_page.is_null()) {
|
2019-02-06 10:32:23 +00:00
|
|
|
dbgprintf("MM: page_in_from_inode() but page already present. Fine with me!\n");
|
2019-02-03 00:36:25 +00:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
|
|
|
return true;
|
2018-11-08 11:59:16 +00:00
|
|
|
}
|
2019-02-03 00:36:25 +00:00
|
|
|
|
2018-11-11 14:36:40 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-02-05 05:43:33 +00:00
|
|
|
dbgprintf("MM: page_in_from_inode ready to read from inode\n");
|
2018-11-11 14:36:40 +00:00
|
|
|
#endif
|
2019-02-05 10:13:34 +00:00
|
|
|
sti();
|
2019-02-03 00:36:25 +00:00
|
|
|
byte page_buffer[PAGE_SIZE];
|
|
|
|
auto& inode = *vmo.inode();
|
|
|
|
auto nread = inode.read_bytes(vmo.inode_offset() + ((region.first_page_index() + page_index_in_region) * PAGE_SIZE), PAGE_SIZE, page_buffer, nullptr);
|
2018-11-08 11:59:16 +00:00
|
|
|
if (nread < 0) {
|
2019-01-16 11:57:07 +00:00
|
|
|
kprintf("MM: page_in_from_inode had error (%d) while reading!\n", nread);
|
2018-11-08 11:59:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nread < PAGE_SIZE) {
|
|
|
|
// If we read less than a page, zero out the rest to avoid leaking uninitialized data.
|
2019-02-03 00:36:25 +00:00
|
|
|
memset(page_buffer + nread, 0, PAGE_SIZE - nread);
|
2018-11-08 11:59:16 +00:00
|
|
|
}
|
2019-02-05 10:13:34 +00:00
|
|
|
cli();
|
2019-06-11 11:13:02 +00:00
|
|
|
vmo_page = allocate_user_physical_page(ShouldZeroFill::No);
|
2019-02-03 00:36:25 +00:00
|
|
|
if (vmo_page.is_null()) {
|
|
|
|
kprintf("MM: page_in_from_inode was unable to allocate a physical page\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2019-06-07 10:56:50 +00:00
|
|
|
byte* dest_ptr = region.vaddr().offset(page_index_in_region * PAGE_SIZE).as_ptr();
|
2019-02-03 00:36:25 +00:00
|
|
|
memcpy(dest_ptr, page_buffer, PAGE_SIZE);
|
2018-11-08 11:59:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 09:29:19 +00:00
|
|
|
PageFaultResponse MemoryManager::handle_page_fault(const PageFault& fault)
|
2018-10-18 11:05:00 +00:00
|
|
|
{
|
2018-10-25 08:15:28 +00:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-03-23 21:03:17 +00:00
|
|
|
ASSERT(current);
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: handle_page_fault(%w) at L%x\n", fault.code(), fault.vaddr().get());
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
2019-06-07 10:56:50 +00:00
|
|
|
ASSERT(fault.vaddr() != m_quickmap_addr);
|
|
|
|
auto* region = region_from_vaddr(current->process(), fault.vaddr());
|
2018-11-08 14:39:26 +00:00
|
|
|
if (!region) {
|
2019-06-07 10:56:50 +00:00
|
|
|
kprintf("NP(error) fault at invalid address L%x\n", fault.vaddr().get());
|
2018-11-08 14:39:26 +00:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2019-06-07 10:56:50 +00:00
|
|
|
auto page_index_in_region = region->page_index_from_address(fault.vaddr());
|
2018-11-05 09:29:19 +00:00
|
|
|
if (fault.is_not_present()) {
|
2019-01-16 11:57:07 +00:00
|
|
|
if (region->vmo().inode()) {
|
2019-02-10 10:37:59 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2019-01-16 11:57:07 +00:00
|
|
|
dbgprintf("NP(inode) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-02-10 10:37:59 +00:00
|
|
|
#endif
|
2019-01-22 04:01:00 +00:00
|
|
|
page_in_from_inode(*region, page_index_in_region);
|
2018-11-08 11:59:16 +00:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
} else {
|
2019-02-10 10:37:59 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2018-11-19 01:17:20 +00:00
|
|
|
dbgprintf("NP(zero) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-02-10 10:37:59 +00:00
|
|
|
#endif
|
2019-01-22 04:01:00 +00:00
|
|
|
zero_page(*region, page_index_in_region);
|
2018-11-19 01:17:20 +00:00
|
|
|
return PageFaultResponse::Continue;
|
2018-11-08 11:59:16 +00:00
|
|
|
}
|
2018-11-05 09:29:19 +00:00
|
|
|
} else if (fault.is_protection_violation()) {
|
2019-05-14 15:31:57 +00:00
|
|
|
if (region->should_cow(page_index_in_region)) {
|
2019-02-10 10:37:59 +00:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2018-11-08 11:59:16 +00:00
|
|
|
dbgprintf("PV(cow) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-02-10 10:37:59 +00:00
|
|
|
#endif
|
2019-01-22 04:01:00 +00:00
|
|
|
bool success = copy_on_write(*region, page_index_in_region);
|
2018-11-05 12:48:07 +00:00
|
|
|
ASSERT(success);
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2019-06-07 10:56:50 +00:00
|
|
|
kprintf("PV(error) fault in Region{%p}[%u] at L%x\n", region, page_index_in_region, fault.vaddr().get());
|
2018-11-05 12:48:07 +00:00
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-18 11:05:00 +00:00
|
|
|
}
|
2018-11-05 12:48:07 +00:00
|
|
|
|
2018-10-18 11:05:00 +00:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:51:00 +00:00
|
|
|
RetainPtr<Region> MemoryManager::allocate_kernel_region(size_t size, String&& name)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2019-05-20 02:46:29 +00:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2019-05-17 02:02:29 +00:00
|
|
|
ASSERT(range.is_valid());
|
2019-05-30 14:14:37 +00:00
|
|
|
auto region = adopt(*new Region(range, move(name), PROT_READ | PROT_WRITE | PROT_EXEC, false));
|
2019-05-17 02:02:29 +00:00
|
|
|
MM.map_region_at_address(*m_kernel_page_directory, *region, range.base(), false);
|
2019-05-14 13:36:24 +00:00
|
|
|
// FIXME: It would be cool if these could zero-fill on demand instead.
|
|
|
|
region->commit();
|
2019-05-14 09:51:00 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:56:21 +00:00
|
|
|
void MemoryManager::deallocate_user_physical_page(PhysicalPage&& page)
|
2019-06-11 11:13:02 +00:00
|
|
|
{
|
|
|
|
for (auto& region : m_user_physical_regions) {
|
|
|
|
if (!region->contains(page)) {
|
|
|
|
kprintf(
|
|
|
|
"MM: deallocate_user_physical_page: %p not in %p -> %p\n",
|
|
|
|
page.paddr(), region->lower().get(), region->upper().get());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:56:21 +00:00
|
|
|
region->return_page(move(page));
|
2019-06-11 11:13:02 +00:00
|
|
|
m_user_physical_pages_used--;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kprintf("MM: deallocate_user_physical_page couldn't figure out region for user page @ %p\n", page.paddr());
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_user_physical_page(ShouldZeroFill should_zero_fill)
|
2018-11-08 11:59:16 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
RetainPtr<PhysicalPage> page = nullptr;
|
|
|
|
|
|
|
|
for (auto& region : m_user_physical_regions) {
|
|
|
|
page = region->take_free_page(false);
|
|
|
|
if (page.is_null())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
if (m_user_physical_regions.is_empty()) {
|
|
|
|
kprintf("MM: no user physical regions available (?)\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
kprintf("MM: no user physical pages available\n");
|
2019-02-20 14:34:55 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-07 09:43:58 +00:00
|
|
|
return {};
|
2019-02-20 14:34:55 +00:00
|
|
|
}
|
2019-06-11 11:13:02 +00:00
|
|
|
|
2018-11-08 11:59:16 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-11 11:13:02 +00:00
|
|
|
dbgprintf("MM: allocate_user_physical_page vending P%p\n", page->paddr().get());
|
2018-11-08 11:59:16 +00:00
|
|
|
#endif
|
2019-06-11 11:13:02 +00:00
|
|
|
|
2019-01-31 02:57:06 +00:00
|
|
|
if (should_zero_fill == ShouldZeroFill::Yes) {
|
2019-06-11 11:13:02 +00:00
|
|
|
auto* ptr = (dword*)quickmap_page(*page);
|
2019-01-31 02:57:06 +00:00
|
|
|
fast_dword_fill(ptr, 0, PAGE_SIZE / sizeof(dword));
|
|
|
|
unquickmap_page();
|
|
|
|
}
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
m_user_physical_pages_used++;
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:56:21 +00:00
|
|
|
void MemoryManager::deallocate_supervisor_physical_page(PhysicalPage&& page)
|
2019-06-11 11:13:02 +00:00
|
|
|
{
|
|
|
|
for (auto& region : m_super_physical_regions) {
|
|
|
|
if (!region->contains(page)) {
|
|
|
|
kprintf(
|
|
|
|
"MM: deallocate_supervisor_physical_page: %p not in %p -> %p\n",
|
|
|
|
page.paddr(), region->lower().get(), region->upper().get());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:56:21 +00:00
|
|
|
region->return_page(move(page));
|
2019-06-11 11:13:02 +00:00
|
|
|
m_super_physical_pages_used--;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kprintf("MM: deallocate_supervisor_physical_page couldn't figure out region for super page @ %p\n", page.paddr());
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-08 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:09:43 +00:00
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_supervisor_physical_page()
|
2018-11-01 10:30:48 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
RetainPtr<PhysicalPage> page = nullptr;
|
|
|
|
|
|
|
|
for (auto& region : m_super_physical_regions) {
|
|
|
|
page = region->take_free_page(true);
|
|
|
|
if (page.is_null())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
if (m_super_physical_regions.is_empty()) {
|
|
|
|
kprintf("MM: no super physical regions available (?)\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
kprintf("MM: no super physical pages available\n");
|
2019-02-20 14:34:55 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-07 09:43:58 +00:00
|
|
|
return {};
|
2019-02-20 14:34:55 +00:00
|
|
|
}
|
2019-06-11 11:13:02 +00:00
|
|
|
|
2019-01-01 01:09:43 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-11 11:13:02 +00:00
|
|
|
dbgprintf("MM: allocate_supervisor_physical_page vending P%p\n", page->paddr().get());
|
2019-01-01 01:09:43 +00:00
|
|
|
#endif
|
2019-06-11 11:13:02 +00:00
|
|
|
|
|
|
|
fast_dword_fill((dword*)page->paddr().as_ptr(), 0, PAGE_SIZE / sizeof(dword));
|
|
|
|
|
|
|
|
m_super_physical_pages_used++;
|
|
|
|
|
|
|
|
return page;
|
2018-11-01 10:30:48 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 12:15:46 +00:00
|
|
|
void MemoryManager::enter_process_paging_scope(Process& process)
|
2018-11-01 10:30:48 +00:00
|
|
|
{
|
2019-03-23 21:03:17 +00:00
|
|
|
ASSERT(current);
|
2018-11-01 10:30:48 +00:00
|
|
|
InterruptDisabler disabler;
|
2019-03-23 21:03:17 +00:00
|
|
|
current->tss().cr3 = process.page_directory().cr3();
|
2019-06-07 09:43:58 +00:00
|
|
|
asm volatile("movl %%eax, %%cr3" ::"a"(process.page_directory().cr3())
|
|
|
|
: "memory");
|
2018-11-01 10:30:48 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
void MemoryManager::flush_entire_tlb()
|
2018-10-23 09:03:56 +00:00
|
|
|
{
|
|
|
|
asm volatile(
|
2019-01-31 02:57:06 +00:00
|
|
|
"mov %%cr3, %%eax\n"
|
2019-06-07 09:43:58 +00:00
|
|
|
"mov %%eax, %%cr3\n" ::
|
|
|
|
: "%eax", "memory");
|
2018-10-23 09:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::flush_tlb(VirtualAddress vaddr)
|
2018-10-23 13:53:11 +00:00
|
|
|
{
|
2019-06-07 09:43:58 +00:00
|
|
|
asm volatile("invlpg %0"
|
|
|
|
:
|
2019-06-07 10:56:50 +00:00
|
|
|
: "m"(*(char*)vaddr.get())
|
2019-06-07 09:43:58 +00:00
|
|
|
: "memory");
|
2018-10-23 13:53:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::map_for_kernel(VirtualAddress vaddr, PhysicalAddress paddr)
|
2019-03-10 14:25:33 +00:00
|
|
|
{
|
2019-06-07 10:56:50 +00:00
|
|
|
auto pte = ensure_pte(kernel_page_directory(), vaddr);
|
2019-03-10 14:25:33 +00:00
|
|
|
pte.set_physical_page_base(paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
2019-06-07 10:56:50 +00:00
|
|
|
flush_tlb(vaddr);
|
2019-03-10 14:25:33 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 12:48:07 +00:00
|
|
|
byte* MemoryManager::quickmap_page(PhysicalPage& physical_page)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-31 02:57:06 +00:00
|
|
|
ASSERT(!m_quickmap_in_use);
|
|
|
|
m_quickmap_in_use = true;
|
2019-06-07 10:56:50 +00:00
|
|
|
auto page_vaddr = m_quickmap_addr;
|
|
|
|
auto pte = ensure_pte(kernel_page_directory(), page_vaddr);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(physical_page.paddr().get());
|
2019-01-01 01:09:43 +00:00
|
|
|
pte.set_present(true);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_writable(true);
|
2019-01-31 02:57:06 +00:00
|
|
|
pte.set_user_allowed(false);
|
2019-06-07 10:56:50 +00:00
|
|
|
flush_tlb(page_vaddr);
|
2019-01-01 01:09:43 +00:00
|
|
|
ASSERT((dword)pte.physical_page_base() == physical_page.paddr().get());
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: >> quickmap_page L%x => P%x @ PTE=%p\n", page_vaddr, physical_page.paddr().get(), pte.ptr());
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
2019-06-07 10:56:50 +00:00
|
|
|
return page_vaddr.as_ptr();
|
2018-11-05 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unquickmap_page()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-31 02:57:06 +00:00
|
|
|
ASSERT(m_quickmap_in_use);
|
2019-06-07 10:56:50 +00:00
|
|
|
auto page_vaddr = m_quickmap_addr;
|
|
|
|
auto pte = ensure_pte(kernel_page_directory(), page_vaddr);
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef MM_DEBUG
|
2018-12-29 02:34:24 +00:00
|
|
|
auto old_physical_address = pte.physical_page_base();
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
2019-06-07 10:56:50 +00:00
|
|
|
flush_tlb(page_vaddr);
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: >> unquickmap_page L%x =/> P%x\n", page_vaddr, old_physical_address);
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
2019-01-31 02:57:06 +00:00
|
|
|
m_quickmap_in_use = false;
|
2018-11-05 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
void MemoryManager::remap_region_page(Region& region, unsigned page_index_in_region, bool user_allowed)
|
2018-11-05 12:48:07 +00:00
|
|
|
{
|
2019-01-24 17:09:46 +00:00
|
|
|
ASSERT(region.page_directory());
|
2018-11-05 12:48:07 +00:00
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:56:50 +00:00
|
|
|
auto page_vaddr = region.vaddr().offset(page_index_in_region * PAGE_SIZE);
|
|
|
|
auto pte = ensure_pte(*region.page_directory(), page_vaddr);
|
2018-11-08 13:35:30 +00:00
|
|
|
auto& physical_page = region.vmo().physical_pages()[page_index_in_region];
|
2018-11-05 12:48:07 +00:00
|
|
|
ASSERT(physical_page);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(physical_page->paddr().get());
|
|
|
|
pte.set_present(true); // FIXME: Maybe we should use the is_readable flag here?
|
2019-05-14 15:31:57 +00:00
|
|
|
if (region.should_cow(page_index_in_region))
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_writable(false);
|
2018-11-05 12:48:07 +00:00
|
|
|
else
|
2019-01-24 17:09:46 +00:00
|
|
|
pte.set_writable(region.is_writable());
|
2019-02-07 08:24:41 +00:00
|
|
|
pte.set_cache_disabled(!region.vmo().m_allow_cpu_caching);
|
|
|
|
pte.set_write_through(!region.vmo().m_allow_cpu_caching);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_user_allowed(user_allowed);
|
2019-06-07 10:56:50 +00:00
|
|
|
region.page_directory()->flush(page_vaddr);
|
2018-11-05 12:48:07 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: >> remap_region_page (PD=%x, PTE=P%x) '%s' L%x => P%x (@%p)\n", region.page_directory()->cr3(), pte.ptr(), region.name().characters(), page_vaddr.get(), physical_page->paddr().get(), physical_page.ptr());
|
2018-11-05 12:48:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-02-05 07:17:46 +00:00
|
|
|
void MemoryManager::remap_region(PageDirectory& page_directory, Region& region)
|
2018-11-05 12:48:07 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-02-05 07:17:46 +00:00
|
|
|
ASSERT(region.page_directory() == &page_directory);
|
2019-06-07 10:56:50 +00:00
|
|
|
map_region_at_address(page_directory, region, region.vaddr(), true);
|
2018-11-05 12:48:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void MemoryManager::map_region_at_address(PageDirectory& page_directory, Region& region, VirtualAddress vaddr, bool user_allowed)
|
2018-11-01 10:30:48 +00:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-01-24 17:09:46 +00:00
|
|
|
region.set_page_directory(page_directory);
|
2018-11-08 13:35:30 +00:00
|
|
|
auto& vmo = region.vmo();
|
2018-11-11 14:36:40 +00:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-08 20:20:09 +00:00
|
|
|
dbgprintf("MM: map_region_at_address will map VMO pages %u - %u (VMO page count: %u)\n", region.first_page_index(), region.last_page_index(), vmo.page_count());
|
2018-11-11 14:36:40 +00:00
|
|
|
#endif
|
2018-12-29 02:34:24 +00:00
|
|
|
for (size_t i = 0; i < region.page_count(); ++i) {
|
2019-06-07 10:56:50 +00:00
|
|
|
auto page_vaddr = vaddr.offset(i * PAGE_SIZE);
|
|
|
|
auto pte = ensure_pte(page_directory, page_vaddr);
|
2018-12-29 02:34:24 +00:00
|
|
|
auto& physical_page = vmo.physical_pages()[region.first_page_index() + i];
|
2018-11-05 09:23:00 +00:00
|
|
|
if (physical_page) {
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(physical_page->paddr().get());
|
|
|
|
pte.set_present(true); // FIXME: Maybe we should use the is_readable flag here?
|
2018-12-29 02:34:24 +00:00
|
|
|
// FIXME: It seems wrong that the *region* cow map is essentially using *VMO* relative indices.
|
2019-05-14 15:31:57 +00:00
|
|
|
if (region.should_cow(region.first_page_index() + i))
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_writable(false);
|
2018-11-05 12:48:07 +00:00
|
|
|
else
|
2019-01-24 17:09:46 +00:00
|
|
|
pte.set_writable(region.is_writable());
|
2019-02-07 08:24:41 +00:00
|
|
|
pte.set_cache_disabled(!region.vmo().m_allow_cpu_caching);
|
|
|
|
pte.set_write_through(!region.vmo().m_allow_cpu_caching);
|
2018-11-05 09:23:00 +00:00
|
|
|
} else {
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
2019-01-24 17:09:46 +00:00
|
|
|
pte.set_writable(region.is_writable());
|
2018-11-05 09:23:00 +00:00
|
|
|
}
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_user_allowed(user_allowed);
|
2019-06-07 10:56:50 +00:00
|
|
|
page_directory.flush(page_vaddr);
|
2018-11-01 10:30:48 +00:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: >> map_region_at_address (PD=%x) '%s' L%x => P%x (@%p)\n", &page_directory, region.name().characters(), page_vaddr, physical_page ? physical_page->paddr().get() : 0, physical_page.ptr());
|
2018-11-01 10:30:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 04:01:00 +00:00
|
|
|
bool MemoryManager::unmap_region(Region& region)
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2019-01-24 17:09:46 +00:00
|
|
|
ASSERT(region.page_directory());
|
2018-10-25 08:15:28 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 20:20:09 +00:00
|
|
|
for (size_t i = 0; i < region.page_count(); ++i) {
|
2019-06-07 10:56:50 +00:00
|
|
|
auto vaddr = region.vaddr().offset(i * PAGE_SIZE);
|
|
|
|
auto pte = ensure_pte(*region.page_directory(), vaddr);
|
2018-12-03 00:38:22 +00:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
|
|
|
pte.set_user_allowed(false);
|
2019-06-07 10:56:50 +00:00
|
|
|
region.page_directory()->flush(vaddr);
|
2018-11-01 10:30:48 +00:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-08 20:20:09 +00:00
|
|
|
auto& physical_page = region.vmo().physical_pages()[region.first_page_index() + i];
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: >> Unmapped L%x => P%x <<\n", vaddr, physical_page ? physical_page->paddr().get() : 0);
|
2018-11-01 10:30:48 +00:00
|
|
|
#endif
|
2018-10-27 12:56:52 +00:00
|
|
|
}
|
2019-01-24 17:09:46 +00:00
|
|
|
region.release_page_directory();
|
2018-10-27 12:56:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
bool MemoryManager::map_region(Process& process, Region& region)
|
2018-10-22 13:42:39 +00:00
|
|
|
{
|
2019-06-07 10:56:50 +00:00
|
|
|
map_region_at_address(process.page_directory(), region, region.vaddr(), true);
|
2018-10-22 13:42:39 +00:00
|
|
|
return true;
|
2018-11-01 11:45:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
bool MemoryManager::validate_user_read(const Process& process, VirtualAddress vaddr) const
|
2018-11-01 11:45:51 +00:00
|
|
|
{
|
2019-06-07 10:56:50 +00:00
|
|
|
auto* region = region_from_vaddr(process, vaddr);
|
2019-01-25 00:39:15 +00:00
|
|
|
return region && region->is_readable();
|
2018-11-01 11:45:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
bool MemoryManager::validate_user_write(const Process& process, VirtualAddress vaddr) const
|
2018-11-01 11:45:51 +00:00
|
|
|
{
|
2019-06-07 10:56:50 +00:00
|
|
|
auto* region = region_from_vaddr(process, vaddr);
|
2019-01-25 00:39:15 +00:00
|
|
|
return region && region->is_writable();
|
2018-10-22 13:42:39 +00:00
|
|
|
}
|
2018-11-02 19:41:58 +00:00
|
|
|
|
2018-11-08 20:20:09 +00:00
|
|
|
void MemoryManager::register_vmo(VMObject& vmo)
|
|
|
|
{
|
2018-11-08 21:24:02 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 20:20:09 +00:00
|
|
|
m_vmos.set(&vmo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregister_vmo(VMObject& vmo)
|
|
|
|
{
|
2018-11-08 21:24:02 +00:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 20:20:09 +00:00
|
|
|
m_vmos.remove(&vmo);
|
|
|
|
}
|
2018-11-08 21:24:02 +00:00
|
|
|
|
|
|
|
void MemoryManager::register_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region.vaddr().get() >= 0xc0000000)
|
2019-05-14 09:51:00 +00:00
|
|
|
m_kernel_regions.set(®ion);
|
|
|
|
else
|
|
|
|
m_user_regions.set(®ion);
|
2018-11-08 21:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregister_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:56:50 +00:00
|
|
|
if (region.vaddr().get() >= 0xc0000000)
|
2019-05-14 09:51:00 +00:00
|
|
|
m_kernel_regions.remove(®ion);
|
|
|
|
else
|
|
|
|
m_user_regions.remove(®ion);
|
2018-11-08 21:24:02 +00:00
|
|
|
}
|
2018-11-09 00:25:31 +00:00
|
|
|
|
2019-03-23 21:03:17 +00:00
|
|
|
ProcessPagingScope::ProcessPagingScope(Process& process)
|
|
|
|
{
|
|
|
|
ASSERT(current);
|
|
|
|
MM.enter_process_paging_scope(process);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessPagingScope::~ProcessPagingScope()
|
|
|
|
{
|
|
|
|
MM.enter_process_paging_scope(current->process());
|
|
|
|
}
|