2019-06-07 12:43:58 +03:00
|
|
|
#include "CMOS.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "StdLib.h"
|
2018-10-18 00:13:55 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/kstdio.h>
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-06-09 12:48:58 +03:00
|
|
|
#include <Kernel/Multiboot.h>
|
2019-08-07 19:06:17 +03:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/VM/InodeVMObject.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2019-12-26 13:45:36 +03:00
|
|
|
#include <Kernel/VM/PurgeableVMObject.h>
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2018-11-01 13:30:48 +03:00
|
|
|
//#define MM_DEBUG
|
2018-11-05 15:48:07 +03:00
|
|
|
//#define PAGE_FAULT_DEBUG
|
2018-11-01 13:30:48 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
MemoryManager::MemoryManager()
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2020-01-17 21:59:20 +03:00
|
|
|
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
|
2019-12-25 13:22:16 +03:00
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
initialize_paging();
|
2019-03-24 00:03:17 +03:00
|
|
|
|
|
|
|
kprintf("MM initialized.\n");
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
void MemoryManager::initialize_paging()
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2020-01-01 14:56:21 +03:00
|
|
|
if (!g_cpu_supports_pae) {
|
|
|
|
kprintf("x86: Cannot boot on machines without PAE support.\n");
|
|
|
|
hang();
|
|
|
|
}
|
|
|
|
|
2018-10-31 22:10:39 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-12-31 16:58:03 +03:00
|
|
|
dbgprintf("MM: Kernel page directory @ %p\n", kernel_page_directory().cr3());
|
2018-10-31 22:10:39 +03:00
|
|
|
#endif
|
2018-10-18 14:05:00 +03:00
|
|
|
|
2020-01-17 23:03:52 +03:00
|
|
|
parse_memory_map();
|
|
|
|
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Installing page directory\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Turn on CR4.PAE
|
|
|
|
asm volatile(
|
|
|
|
"mov %cr4, %eax\n"
|
|
|
|
"orl $0x20, %eax\n"
|
|
|
|
"mov %eax, %cr4\n");
|
|
|
|
|
|
|
|
if (g_cpu_supports_pge) {
|
|
|
|
// Turn on CR4.PGE so the CPU will respect the G bit in page tables.
|
|
|
|
asm volatile(
|
|
|
|
"mov %cr4, %eax\n"
|
|
|
|
"orl $0x80, %eax\n"
|
|
|
|
"mov %eax, %cr4\n");
|
|
|
|
kprintf("x86: PGE support enabled\n");
|
|
|
|
} else {
|
|
|
|
kprintf("x86: PGE support not detected\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_cpu_supports_smep) {
|
|
|
|
// Turn on CR4.SMEP
|
|
|
|
asm volatile(
|
|
|
|
"mov %cr4, %eax\n"
|
|
|
|
"orl $0x100000, %eax\n"
|
|
|
|
"mov %eax, %cr4\n");
|
|
|
|
kprintf("x86: SMEP support enabled\n");
|
|
|
|
} else {
|
|
|
|
kprintf("x86: SMEP support not detected\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_cpu_supports_smap) {
|
|
|
|
// Turn on CR4.SMAP
|
|
|
|
kprintf("x86: Enabling SMAP\n");
|
|
|
|
asm volatile(
|
|
|
|
"mov %cr4, %eax\n"
|
|
|
|
"orl $0x200000, %eax\n"
|
|
|
|
"mov %eax, %cr4\n");
|
|
|
|
kprintf("x86: SMAP support enabled\n");
|
|
|
|
} else {
|
|
|
|
kprintf("x86: SMAP support not detected\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_cpu_supports_nx) {
|
|
|
|
// Turn on IA32_EFER.NXE
|
|
|
|
asm volatile(
|
|
|
|
"movl $0xc0000080, %ecx\n"
|
|
|
|
"rdmsr\n"
|
|
|
|
"orl $0x800, %eax\n"
|
|
|
|
"wrmsr\n");
|
|
|
|
kprintf("x86: NX support enabled\n");
|
|
|
|
} else {
|
|
|
|
kprintf("x86: NX support not detected\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));
|
|
|
|
asm volatile(
|
|
|
|
"movl %%cr0, %%eax\n"
|
|
|
|
"orl $0x80010001, %%eax\n"
|
|
|
|
"movl %%eax, %%cr0\n" ::
|
|
|
|
: "%eax", "memory");
|
|
|
|
|
2020-01-18 00:02:10 +03:00
|
|
|
setup_low_1mb();
|
|
|
|
|
2020-01-18 00:07:20 +03:00
|
|
|
protect_kernel_image();
|
|
|
|
|
2020-01-17 23:03:52 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Paging initialized.\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:07:20 +03:00
|
|
|
void MemoryManager::protect_kernel_image()
|
|
|
|
{
|
|
|
|
// Disable writing to the kernel text and rodata segments.
|
|
|
|
extern u32 start_of_kernel_text;
|
|
|
|
extern u32 start_of_kernel_data;
|
|
|
|
for (size_t i = (u32)&start_of_kernel_text; i < (u32)&start_of_kernel_data; i += PAGE_SIZE) {
|
|
|
|
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
|
|
|
pte.set_writable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_cpu_supports_nx) {
|
|
|
|
// Disable execution of the kernel data and bss segments.
|
|
|
|
extern u32 end_of_kernel_bss;
|
|
|
|
for (size_t i = (u32)&start_of_kernel_data; i < (u32)&end_of_kernel_bss; i += PAGE_SIZE) {
|
|
|
|
auto& pte = ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
|
|
|
pte.set_execute_disabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:02:10 +03:00
|
|
|
void MemoryManager::setup_low_1mb()
|
|
|
|
{
|
2020-01-18 00:18:56 +03:00
|
|
|
m_low_page_table = allocate_user_physical_page(ShouldZeroFill::Yes);
|
2020-01-18 00:02:10 +03:00
|
|
|
|
|
|
|
auto* pd_zero = quickmap_pd(kernel_page_directory(), 0);
|
|
|
|
pd_zero[1].set_present(false);
|
|
|
|
pd_zero[2].set_present(false);
|
|
|
|
pd_zero[3].set_present(false);
|
|
|
|
|
|
|
|
auto& pde_zero = pd_zero[0];
|
|
|
|
pde_zero.set_page_table_base(m_low_page_table->paddr().get());
|
|
|
|
pde_zero.set_present(true);
|
|
|
|
pde_zero.set_huge(false);
|
|
|
|
pde_zero.set_writable(true);
|
|
|
|
pde_zero.set_user_allowed(false);
|
|
|
|
if (g_cpu_supports_nx)
|
|
|
|
pde_zero.set_execute_disabled(true);
|
|
|
|
|
|
|
|
for (u32 offset = 0; offset < (2 * MB); offset += PAGE_SIZE) {
|
|
|
|
auto& page_table_page = m_low_page_table;
|
|
|
|
auto& pte = quickmap_pt(page_table_page->paddr())[offset / PAGE_SIZE];
|
|
|
|
pte.set_physical_page_base(offset);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(offset != 0);
|
|
|
|
pte.set_writable(offset < (1 * MB));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:03:52 +03:00
|
|
|
void MemoryManager::parse_memory_map()
|
|
|
|
{
|
2019-06-27 14:34:28 +03:00
|
|
|
RefPtr<PhysicalRegion> region;
|
2019-06-11 14:13:02 +03:00
|
|
|
bool region_is_super = false;
|
|
|
|
|
2020-01-17 23:03:52 +03:00
|
|
|
auto* mmap = (multiboot_memory_map_t*)(low_physical_to_virtual(multiboot_info_ptr->mmap_addr));
|
|
|
|
for (; (unsigned long)mmap < (low_physical_to_virtual(multiboot_info_ptr->mmap_addr)) + (multiboot_info_ptr->mmap_length); mmap = (multiboot_memory_map_t*)((unsigned long)mmap + mmap->size + sizeof(mmap->size))) {
|
2019-06-09 12:48:58 +03:00
|
|
|
kprintf("MM: Multiboot mmap: base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n",
|
2019-07-03 22:17:35 +03:00
|
|
|
(u32)(mmap->addr >> 32),
|
|
|
|
(u32)(mmap->addr & 0xffffffff),
|
|
|
|
(u32)(mmap->len >> 32),
|
|
|
|
(u32)(mmap->len & 0xffffffff),
|
|
|
|
(u32)mmap->type);
|
2019-06-09 12:48:58 +03:00
|
|
|
|
|
|
|
if (mmap->type != MULTIBOOT_MEMORY_AVAILABLE)
|
|
|
|
continue;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2019-11-23 19:27:09 +03:00
|
|
|
// FIXME: Maybe make use of stuff below the 1MB mark?
|
|
|
|
if (mmap->addr < (1 * MB))
|
|
|
|
continue;
|
|
|
|
|
2019-09-17 02:58:38 +03:00
|
|
|
if ((mmap->addr + mmap->len) > 0xffffffff)
|
|
|
|
continue;
|
|
|
|
|
2019-09-28 09:22:50 +03:00
|
|
|
auto diff = (u32)mmap->addr % PAGE_SIZE;
|
|
|
|
if (diff != 0) {
|
|
|
|
kprintf("MM: got an unaligned region base from the bootloader; correcting %p by %d bytes\n", mmap->addr, diff);
|
|
|
|
diff = PAGE_SIZE - diff;
|
|
|
|
mmap->addr += diff;
|
|
|
|
mmap->len -= diff;
|
|
|
|
}
|
|
|
|
if ((mmap->len % PAGE_SIZE) != 0) {
|
|
|
|
kprintf("MM: got an unaligned region length from the bootloader; correcting %d by %d bytes\n", mmap->len, mmap->len % PAGE_SIZE);
|
|
|
|
mmap->len -= mmap->len % PAGE_SIZE;
|
|
|
|
}
|
|
|
|
if (mmap->len < PAGE_SIZE) {
|
|
|
|
kprintf("MM: memory region from bootloader is too small; we want >= %d bytes, but got %d bytes\n", PAGE_SIZE, mmap->len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-11 14:13:02 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
kprintf("MM: considering memory at %p - %p\n",
|
2019-07-03 22:17:35 +03:00
|
|
|
(u32)mmap->addr, (u32)(mmap->addr + mmap->len));
|
2019-06-11 14:13:02 +03:00
|
|
|
#endif
|
|
|
|
|
2019-06-09 12:48:58 +03:00
|
|
|
for (size_t page_base = mmap->addr; page_base < (mmap->addr + mmap->len); page_base += PAGE_SIZE) {
|
2019-06-11 14:13:02 +03:00
|
|
|
auto addr = PhysicalAddress(page_base);
|
|
|
|
|
2019-11-23 19:27:09 +03:00
|
|
|
if (page_base < 7 * MB) {
|
|
|
|
// nothing
|
|
|
|
} else if (page_base >= 7 * MB && page_base < 8 * MB) {
|
2019-06-11 14:13:02 +03:00
|
|
|
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);
|
|
|
|
}
|
2019-11-23 19:27:09 +03:00
|
|
|
} else {
|
2019-06-11 14:13:02 +03:00
|
|
|
if (region.is_null() || region_is_super || region->upper().offset(PAGE_SIZE) != addr) {
|
|
|
|
m_user_physical_regions.append(PhysicalRegion::create(addr, addr));
|
2019-11-09 00:39:29 +03:00
|
|
|
region = m_user_physical_regions.last();
|
2019-06-11 14:13:02 +03:00
|
|
|
region_is_super = false;
|
|
|
|
} else {
|
|
|
|
region->expand(region->lower(), addr);
|
|
|
|
}
|
2019-06-09 12:48:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 16:58:03 +03:00
|
|
|
|
2019-06-11 14:13:02 +03:00
|
|
|
for (auto& region : m_super_physical_regions)
|
2019-06-27 14:34:28 +03:00
|
|
|
m_super_physical_pages += region.finalize_capacity();
|
2019-06-11 14:13:02 +03:00
|
|
|
|
|
|
|
for (auto& region : m_user_physical_regions)
|
2019-06-27 14:34:28 +03:00
|
|
|
m_user_physical_pages += region.finalize_capacity();
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 22:45:56 +03:00
|
|
|
PageTableEntry& MemoryManager::ensure_pte(PageDirectory& page_directory, VirtualAddress vaddr)
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-12-25 13:22:16 +03:00
|
|
|
u32 page_directory_table_index = (vaddr.get() >> 30) & 0x3;
|
|
|
|
u32 page_directory_index = (vaddr.get() >> 21) & 0x1ff;
|
|
|
|
u32 page_table_index = (vaddr.get() >> 12) & 0x1ff;
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
auto* pd = quickmap_pd(page_directory, page_directory_table_index);
|
|
|
|
PageDirectoryEntry& pde = pd[page_directory_index];
|
2018-12-03 03:38:22 +03:00
|
|
|
if (!pde.is_present()) {
|
2018-10-31 22:10:39 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-08-23 17:18:27 +03:00
|
|
|
dbgprintf("MM: PDE %u not present (requested for V%p), allocating\n", page_directory_index, vaddr.get());
|
2018-10-31 22:10:39 +03:00
|
|
|
#endif
|
2020-01-18 00:18:56 +03:00
|
|
|
auto page_table = allocate_user_physical_page(ShouldZeroFill::Yes);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
2020-01-17 22:57:32 +03:00
|
|
|
dbgprintf("MM: PD K%p (%s) at P%p allocated page table #%u (for V%p) at P%p\n",
|
|
|
|
&page_directory,
|
|
|
|
&page_directory == m_kernel_page_directory ? "Kernel" : "User",
|
|
|
|
page_directory.cr3(),
|
|
|
|
page_directory_index,
|
|
|
|
vaddr.get(),
|
|
|
|
page_table->paddr().get());
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
2020-01-17 22:57:32 +03:00
|
|
|
pde.set_page_table_base(page_table->paddr().get());
|
|
|
|
pde.set_user_allowed(true);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
|
|
|
pde.set_global(&page_directory == m_kernel_page_directory.ptr());
|
|
|
|
page_directory.m_physical_pages.set(page_directory_index, move(page_table));
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
2020-01-17 21:59:20 +03:00
|
|
|
|
|
|
|
return quickmap_pt(PhysicalAddress((u32)pde.page_table_base()))[page_table_index];
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
void MemoryManager::map_protected(VirtualAddress vaddr, size_t length)
|
2018-10-21 22:57:59 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-06-13 22:42:12 +03:00
|
|
|
ASSERT(vaddr.is_page_aligned());
|
2019-07-03 22:17:35 +03:00
|
|
|
for (u32 offset = 0; offset < length; offset += PAGE_SIZE) {
|
2019-06-07 13:56:50 +03:00
|
|
|
auto pte_address = vaddr.offset(offset);
|
2019-06-26 22:45:56 +03:00
|
|
|
auto& pte = ensure_pte(kernel_page_directory(), pte_address);
|
2019-01-31 19:31:23 +03:00
|
|
|
pte.set_physical_page_base(pte_address.get());
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
2019-01-31 19:31:23 +03:00
|
|
|
flush_tlb(pte_address);
|
2018-10-21 22:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
void MemoryManager::create_identity_mapping(PageDirectory& page_directory, VirtualAddress vaddr, size_t size)
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-06-07 13:56:50 +03:00
|
|
|
ASSERT((vaddr.get() & ~PAGE_MASK) == 0);
|
2019-07-03 22:17:35 +03:00
|
|
|
for (u32 offset = 0; offset < size; offset += PAGE_SIZE) {
|
2019-06-07 13:56:50 +03:00
|
|
|
auto pte_address = vaddr.offset(offset);
|
2019-06-26 22:45:56 +03:00
|
|
|
auto& pte = ensure_pte(page_directory, pte_address);
|
2019-01-31 19:31:23 +03:00
|
|
|
pte.set_physical_page_base(pte_address.get());
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
2020-01-10 00:29:31 +03:00
|
|
|
flush_tlb(pte_address);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
void MemoryManager::initialize()
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2020-01-17 21:59:20 +03:00
|
|
|
s_the = new MemoryManager;
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:31:20 +03:00
|
|
|
Region* MemoryManager::kernel_region_from_vaddr(VirtualAddress vaddr)
|
2018-11-05 15:48:07 +03:00
|
|
|
{
|
2019-08-06 12:19:16 +03:00
|
|
|
if (vaddr.get() < 0xc0000000)
|
|
|
|
return nullptr;
|
2019-08-08 14:40:58 +03:00
|
|
|
for (auto& region : MM.m_kernel_regions) {
|
|
|
|
if (region.contains(vaddr))
|
|
|
|
return ®ion;
|
2019-05-14 12:51:00 +03:00
|
|
|
}
|
2019-08-06 11:31:20 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2019-05-14 12:51:00 +03:00
|
|
|
|
2019-08-06 11:31:20 +03:00
|
|
|
Region* MemoryManager::user_region_from_vaddr(Process& process, VirtualAddress vaddr)
|
|
|
|
{
|
2018-11-05 15:48:07 +03: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-27 14:34:28 +03:00
|
|
|
if (region.contains(vaddr))
|
|
|
|
return ®ion;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
2019-08-06 11:31:20 +03:00
|
|
|
dbg() << process << " Couldn't find user region for " << vaddr;
|
2020-01-17 21:59:20 +03:00
|
|
|
if (auto* kreg = kernel_region_from_vaddr(vaddr)) {
|
|
|
|
dbg() << process << " OTOH, there is a kernel region: " << kreg->range() << ": " << kreg->name();
|
|
|
|
} else {
|
|
|
|
dbg() << process << " AND no kernel region either";
|
|
|
|
}
|
|
|
|
|
|
|
|
process.dump_regions();
|
|
|
|
|
|
|
|
kprintf("Kernel regions:\n");
|
|
|
|
kprintf("BEGIN END SIZE ACCESS NAME\n");
|
|
|
|
for (auto& region : MM.m_kernel_regions) {
|
|
|
|
kprintf("%08x -- %08x %08x %c%c%c%c%c%c %s\n",
|
|
|
|
region.vaddr().get(),
|
|
|
|
region.vaddr().offset(region.size() - 1).get(),
|
|
|
|
region.size(),
|
|
|
|
region.is_readable() ? 'R' : ' ',
|
|
|
|
region.is_writable() ? 'W' : ' ',
|
|
|
|
region.is_executable() ? 'X' : ' ',
|
|
|
|
region.is_shared() ? 'S' : ' ',
|
|
|
|
region.is_stack() ? 'T' : ' ',
|
|
|
|
region.vmobject().is_purgeable() ? 'P' : ' ',
|
|
|
|
region.name().characters());
|
|
|
|
}
|
2019-01-25 03:39:15 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-06 11:31:20 +03:00
|
|
|
Region* MemoryManager::region_from_vaddr(Process& process, VirtualAddress vaddr)
|
|
|
|
{
|
2019-08-06 12:19:16 +03:00
|
|
|
if (auto* region = kernel_region_from_vaddr(vaddr))
|
|
|
|
return region;
|
2019-08-06 11:31:20 +03:00
|
|
|
return user_region_from_vaddr(process, vaddr);
|
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
const Region* MemoryManager::region_from_vaddr(const Process& process, VirtualAddress vaddr)
|
2019-01-25 03:39:15 +03:00
|
|
|
{
|
2019-08-06 12:19:16 +03:00
|
|
|
if (auto* region = kernel_region_from_vaddr(vaddr))
|
|
|
|
return region;
|
2019-08-06 11:31:20 +03:00
|
|
|
return user_region_from_vaddr(const_cast<Process&>(process), vaddr);
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 12:19:16 +03:00
|
|
|
Region* MemoryManager::region_from_vaddr(VirtualAddress vaddr)
|
|
|
|
{
|
|
|
|
if (auto* region = kernel_region_from_vaddr(vaddr))
|
|
|
|
return region;
|
2019-12-25 04:46:17 +03:00
|
|
|
auto page_directory = PageDirectory::find_by_cr3(cpu_cr3());
|
2019-08-06 12:19:16 +03:00
|
|
|
if (!page_directory)
|
|
|
|
return nullptr;
|
|
|
|
ASSERT(page_directory->process());
|
|
|
|
return user_region_from_vaddr(*page_directory->process(), vaddr);
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:29:19 +03:00
|
|
|
PageFaultResponse MemoryManager::handle_page_fault(const PageFault& fault)
|
2018-10-18 14:05:00 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-03-24 00:03:17 +03:00
|
|
|
ASSERT(current);
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2019-08-23 17:18:27 +03:00
|
|
|
dbgprintf("MM: handle_page_fault(%w) at V%p\n", fault.code(), fault.vaddr().get());
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
2019-08-06 12:19:16 +03:00
|
|
|
auto* region = region_from_vaddr(fault.vaddr());
|
2018-11-08 17:39:26 +03:00
|
|
|
if (!region) {
|
2019-08-23 17:18:27 +03:00
|
|
|
kprintf("NP(error) fault at invalid address V%p\n", fault.vaddr().get());
|
2018-11-08 17:39:26 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
|
|
|
|
return region->handle_fault(fault);
|
2018-10-18 14:05:00 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:29:31 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region(size_t size, const StringView& name, u8 access, bool user_accessible, bool should_commit, bool cacheable)
|
2019-05-14 12:51:00 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2019-05-20 05:46:29 +03:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2019-05-17 05:02:29 +03:00
|
|
|
ASSERT(range.is_valid());
|
2019-09-27 15:19:07 +03:00
|
|
|
OwnPtr<Region> region;
|
2019-07-19 18:01:16 +03:00
|
|
|
if (user_accessible)
|
2020-01-10 00:29:31 +03:00
|
|
|
region = Region::create_user_accessible(range, name, access, cacheable);
|
2019-07-19 18:01:16 +03:00
|
|
|
else
|
2020-01-10 00:29:31 +03:00
|
|
|
region = Region::create_kernel_only(range, name, access, cacheable);
|
|
|
|
region->set_page_directory(kernel_page_directory());
|
2019-05-14 16:36:24 +03:00
|
|
|
// FIXME: It would be cool if these could zero-fill on demand instead.
|
2019-08-06 15:12:52 +03:00
|
|
|
if (should_commit)
|
|
|
|
region->commit();
|
2019-05-14 12:51:00 +03:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2020-01-10 00:29:31 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region(PhysicalAddress paddr, size_t size, const StringView& name, u8 access, bool user_accessible, bool cacheable)
|
2019-07-19 18:01:16 +03:00
|
|
|
{
|
2020-01-10 00:29:31 +03:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
|
|
|
ASSERT(range.is_valid());
|
|
|
|
OwnPtr<Region> region;
|
|
|
|
if (user_accessible)
|
|
|
|
region = Region::create_user_accessible(range, AnonymousVMObject::create_for_physical_range(paddr, size), 0, name, access, cacheable);
|
|
|
|
else
|
|
|
|
region = Region::create_kernel_only(range, AnonymousVMObject::create_for_physical_range(paddr, size), 0, name, access, cacheable);
|
|
|
|
region->map(kernel_page_directory());
|
|
|
|
return region;
|
2019-07-19 18:01:16 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:29:31 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_user_accessible_kernel_region(size_t size, const StringView& name, u8 access, bool cacheable)
|
|
|
|
{
|
|
|
|
return allocate_kernel_region(size, name, access, true, true, cacheable);
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, const StringView& name, u8 access, bool user_accessible, bool cacheable)
|
2019-12-16 00:21:28 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
|
|
|
ASSERT(range.is_valid());
|
2020-01-10 00:29:31 +03:00
|
|
|
OwnPtr<Region> region;
|
|
|
|
if (user_accessible)
|
|
|
|
region = Region::create_user_accessible(range, vmobject, 0, name, access, cacheable);
|
|
|
|
else
|
|
|
|
region = Region::create_kernel_only(range, vmobject, 0, name, access, cacheable);
|
2019-12-16 00:21:28 +03:00
|
|
|
region->map(kernel_page_directory());
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-06-14 14:56:21 +03:00
|
|
|
void MemoryManager::deallocate_user_physical_page(PhysicalPage&& page)
|
2019-06-11 14:13:02 +03:00
|
|
|
{
|
|
|
|
for (auto& region : m_user_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
if (!region.contains(page)) {
|
2019-06-11 14:13:02 +03:00
|
|
|
kprintf(
|
|
|
|
"MM: deallocate_user_physical_page: %p not in %p -> %p\n",
|
2019-12-20 21:53:47 +03:00
|
|
|
page.paddr().get(), region.lower().get(), region.upper().get());
|
2019-06-11 14:13:02 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:34:28 +03:00
|
|
|
region.return_page(move(page));
|
|
|
|
--m_user_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:53:47 +03:00
|
|
|
kprintf("MM: deallocate_user_physical_page couldn't figure out region for user page @ %p\n", page.paddr().get());
|
2019-06-11 14:13:02 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:39:29 +03:00
|
|
|
RefPtr<PhysicalPage> MemoryManager::find_free_user_physical_page()
|
2018-11-08 14:59:16 +03:00
|
|
|
{
|
2019-06-27 14:34:28 +03:00
|
|
|
RefPtr<PhysicalPage> page;
|
2019-06-11 14:13:02 +03:00
|
|
|
for (auto& region : m_user_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
page = region.take_free_page(false);
|
2019-11-09 00:39:29 +03:00
|
|
|
if (!page.is_null())
|
|
|
|
break;
|
2019-06-11 14:13:02 +03:00
|
|
|
}
|
2019-11-09 00:39:29 +03:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<PhysicalPage> MemoryManager::allocate_user_physical_page(ShouldZeroFill should_zero_fill)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
RefPtr<PhysicalPage> page = find_free_user_physical_page();
|
2019-06-11 14:13:02 +03:00
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
if (m_user_physical_regions.is_empty()) {
|
|
|
|
kprintf("MM: no user physical regions available (?)\n");
|
|
|
|
}
|
|
|
|
|
2019-12-26 13:45:36 +03:00
|
|
|
for_each_vmobject([&](auto& vmobject) {
|
|
|
|
if (vmobject.is_purgeable()) {
|
|
|
|
auto& purgeable_vmobject = static_cast<PurgeableVMObject&>(vmobject);
|
|
|
|
int purged_page_count = purgeable_vmobject.purge_with_interrupts_disabled({});
|
|
|
|
if (purged_page_count) {
|
|
|
|
kprintf("MM: Purge saved the day! Purged %d pages from PurgeableVMObject{%p}\n", purged_page_count, &purgeable_vmobject);
|
|
|
|
page = find_free_user_physical_page();
|
|
|
|
ASSERT(page);
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
kprintf("MM: no user physical pages available\n");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return {};
|
|
|
|
}
|
2019-02-20 17:34:55 +03:00
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2018-11-08 14:59:16 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-11 14:13:02 +03:00
|
|
|
dbgprintf("MM: allocate_user_physical_page vending P%p\n", page->paddr().get());
|
2018-11-08 14:59:16 +03:00
|
|
|
#endif
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2019-01-31 05:57:06 +03:00
|
|
|
if (should_zero_fill == ShouldZeroFill::Yes) {
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* ptr = (u32*)quickmap_page(*page);
|
2020-01-10 08:59:01 +03:00
|
|
|
memset(ptr, 0, PAGE_SIZE);
|
2019-01-31 05:57:06 +03:00
|
|
|
unquickmap_page();
|
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2019-06-27 14:34:28 +03:00
|
|
|
++m_user_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2019-06-14 14:56:21 +03:00
|
|
|
void MemoryManager::deallocate_supervisor_physical_page(PhysicalPage&& page)
|
2019-06-11 14:13:02 +03:00
|
|
|
{
|
|
|
|
for (auto& region : m_super_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
if (!region.contains(page)) {
|
2019-06-11 14:13:02 +03:00
|
|
|
kprintf(
|
|
|
|
"MM: deallocate_supervisor_physical_page: %p not in %p -> %p\n",
|
2019-12-20 21:53:47 +03:00
|
|
|
page.paddr().get(), region.lower().get(), region.upper().get());
|
2019-06-11 14:13:02 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:34:28 +03:00
|
|
|
region.return_page(move(page));
|
|
|
|
--m_super_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:53:47 +03:00
|
|
|
kprintf("MM: deallocate_supervisor_physical_page couldn't figure out region for super page @ %p\n", page.paddr().get());
|
2019-06-11 14:13:02 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-08 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<PhysicalPage> MemoryManager::allocate_supervisor_physical_page()
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-27 14:34:28 +03:00
|
|
|
RefPtr<PhysicalPage> page;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
|
|
|
for (auto& region : m_super_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
page = region.take_free_page(true);
|
2019-06-11 14:13:02 +03:00
|
|
|
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 17:34:55 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-07 12:43:58 +03:00
|
|
|
return {};
|
2019-02-20 17:34:55 +03:00
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2019-01-01 04:09:43 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-06-11 14:13:02 +03:00
|
|
|
dbgprintf("MM: allocate_supervisor_physical_page vending P%p\n", page->paddr().get());
|
2019-01-01 04:09:43 +03:00
|
|
|
#endif
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
fast_u32_fill((u32*)page->paddr().offset(0xc0000000).as_ptr(), 0, PAGE_SIZE / sizeof(u32));
|
2019-06-27 14:34:28 +03:00
|
|
|
++m_super_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
return page;
|
2018-11-01 13:30:48 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:15:46 +03:00
|
|
|
void MemoryManager::enter_process_paging_scope(Process& process)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
2019-03-24 00:03:17 +03:00
|
|
|
ASSERT(current);
|
2018-11-01 13:30:48 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-11-27 14:40:42 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
current->tss().cr3 = process.page_directory().cr3();
|
2019-06-07 12:43:58 +03:00
|
|
|
asm volatile("movl %%eax, %%cr3" ::"a"(process.page_directory().cr3())
|
|
|
|
: "memory");
|
2018-11-01 13:30:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
void MemoryManager::flush_entire_tlb()
|
2018-10-23 12:03:56 +03:00
|
|
|
{
|
|
|
|
asm volatile(
|
2019-01-31 05:57:06 +03:00
|
|
|
"mov %%cr3, %%eax\n"
|
2019-06-07 12:43:58 +03:00
|
|
|
"mov %%eax, %%cr3\n" ::
|
|
|
|
: "%eax", "memory");
|
2018-10-23 12:03:56 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
void MemoryManager::flush_tlb(VirtualAddress vaddr)
|
2018-10-23 16:53:11 +03:00
|
|
|
{
|
2020-01-10 00:29:31 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Flush page V%p\n", vaddr.get());
|
|
|
|
#endif
|
2019-06-07 12:43:58 +03:00
|
|
|
asm volatile("invlpg %0"
|
|
|
|
:
|
2019-06-07 13:56:50 +03:00
|
|
|
: "m"(*(char*)vaddr.get())
|
2019-06-07 12:43:58 +03:00
|
|
|
: "memory");
|
2018-10-23 16:53:11 +03:00
|
|
|
}
|
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
extern "C" PageTableEntry boot_pd3_pde1023_pt[1024];
|
|
|
|
|
|
|
|
PageDirectoryEntry* MemoryManager::quickmap_pd(PageDirectory& directory, size_t pdpt_index)
|
|
|
|
{
|
|
|
|
auto& pte = boot_pd3_pde1023_pt[4];
|
|
|
|
auto pd_paddr = directory.m_directory_pages[pdpt_index]->paddr();
|
|
|
|
if (pte.physical_page_base() != pd_paddr.as_ptr()) {
|
2020-01-18 00:02:10 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("quickmap_pd: Mapping P%p at 0xffe04000 in pte @ %p\n", directory.m_directory_pages[pdpt_index]->paddr().as_ptr(), &pte);
|
|
|
|
#endif
|
2020-01-17 21:59:20 +03:00
|
|
|
pte.set_physical_page_base(pd_paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
flush_tlb(VirtualAddress(0xffe04000));
|
|
|
|
}
|
|
|
|
return (PageDirectoryEntry*)0xffe04000;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageTableEntry* MemoryManager::quickmap_pt(PhysicalAddress pt_paddr)
|
|
|
|
{
|
|
|
|
auto& pte = boot_pd3_pde1023_pt[8];
|
|
|
|
if (pte.physical_page_base() != pt_paddr.as_ptr()) {
|
2020-01-18 00:02:10 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("quickmap_pt: Mapping P%p at 0xffe08000 in pte @ %p\n", pt_paddr.as_ptr(), &pte);
|
|
|
|
#endif
|
2020-01-17 21:59:20 +03:00
|
|
|
pte.set_physical_page_base(pt_paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
flush_tlb(VirtualAddress(0xffe08000));
|
|
|
|
}
|
|
|
|
return (PageTableEntry*)0xffe08000;
|
|
|
|
}
|
|
|
|
|
2019-10-16 19:27:00 +03:00
|
|
|
void MemoryManager::map_for_kernel(VirtualAddress vaddr, PhysicalAddress paddr, bool cache_disabled)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2019-06-26 22:45:56 +03:00
|
|
|
auto& pte = ensure_pte(kernel_page_directory(), vaddr);
|
2019-03-10 17:25:33 +03:00
|
|
|
pte.set_physical_page_base(paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
2019-10-16 19:27:00 +03:00
|
|
|
pte.set_cache_disabled(cache_disabled);
|
2019-06-07 13:56:50 +03:00
|
|
|
flush_tlb(vaddr);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8* MemoryManager::quickmap_page(PhysicalPage& physical_page)
|
2018-11-05 15:48:07 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-31 05:57:06 +03:00
|
|
|
ASSERT(!m_quickmap_in_use);
|
|
|
|
m_quickmap_in_use = true;
|
2020-01-18 00:18:56 +03:00
|
|
|
|
|
|
|
auto& pte = boot_pd3_pde1023_pt[0];
|
|
|
|
if (pte.physical_page_base() != physical_page.paddr().as_ptr()) {
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef MM_DEBUG
|
2020-01-18 00:18:56 +03:00
|
|
|
dbgprintf("quickmap_page: Mapping P%p at 0xffe00000 in pte @ %p\n", physical_page.paddr().as_ptr(), &pte);
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
2020-01-18 00:18:56 +03:00
|
|
|
pte.set_physical_page_base(physical_page.paddr().get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
flush_tlb(VirtualAddress(0xffe00000));
|
|
|
|
}
|
|
|
|
return (u8*)0xffe00000;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unquickmap_page()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-31 05:57:06 +03:00
|
|
|
ASSERT(m_quickmap_in_use);
|
2020-01-18 00:18:56 +03:00
|
|
|
auto& pte = boot_pd3_pde1023_pt[0];
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
2020-01-18 00:18:56 +03:00
|
|
|
flush_tlb(VirtualAddress(0xffe00000));
|
2019-01-31 05:57:06 +03:00
|
|
|
m_quickmap_in_use = false;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 04:09:25 +03:00
|
|
|
template<MemoryManager::AccessSpace space, MemoryManager::AccessType access_type>
|
|
|
|
bool MemoryManager::validate_range(const Process& process, VirtualAddress base_vaddr, size_t size) const
|
|
|
|
{
|
|
|
|
ASSERT(size);
|
|
|
|
VirtualAddress vaddr = base_vaddr.page_base();
|
|
|
|
VirtualAddress end_vaddr = base_vaddr.offset(size - 1).page_base();
|
|
|
|
if (end_vaddr < vaddr) {
|
|
|
|
dbg() << *current << " Shenanigans! Asked to validate " << base_vaddr << " size=" << size;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const Region* region = nullptr;
|
|
|
|
while (vaddr <= end_vaddr) {
|
|
|
|
if (!region || !region->contains(vaddr)) {
|
|
|
|
if (space == AccessSpace::Kernel)
|
|
|
|
region = kernel_region_from_vaddr(vaddr);
|
|
|
|
if (!region || !region->contains(vaddr))
|
|
|
|
region = user_region_from_vaddr(const_cast<Process&>(process), vaddr);
|
|
|
|
if (!region
|
|
|
|
|| (space == AccessSpace::User && !region->is_user_accessible())
|
|
|
|
|| (access_type == AccessType::Read && !region->is_readable())
|
|
|
|
|| (access_type == AccessType::Write && !region->is_writable())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vaddr = vaddr.offset(PAGE_SIZE);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-17 14:11:43 +03:00
|
|
|
bool MemoryManager::validate_user_stack(const Process& process, VirtualAddress vaddr) const
|
|
|
|
{
|
2019-12-31 05:43:24 +03:00
|
|
|
if (!is_user_address(vaddr))
|
|
|
|
return false;
|
2019-12-31 02:27:04 +03:00
|
|
|
auto* region = user_region_from_vaddr(const_cast<Process&>(process), vaddr);
|
|
|
|
return region && region->is_user_accessible() && region->is_stack();
|
2019-11-17 14:11:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 04:09:25 +03:00
|
|
|
bool MemoryManager::validate_kernel_read(const Process& process, VirtualAddress vaddr, size_t size) const
|
|
|
|
{
|
|
|
|
return validate_range<AccessSpace::Kernel, AccessType::Read>(process, vaddr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::validate_user_read(const Process& process, VirtualAddress vaddr, size_t size) const
|
2018-11-01 14:45:51 +03:00
|
|
|
{
|
2019-12-31 05:43:24 +03:00
|
|
|
if (!is_user_address(vaddr))
|
|
|
|
return false;
|
2020-01-02 04:09:25 +03:00
|
|
|
return validate_range<AccessSpace::User, AccessType::Read>(process, vaddr, size);
|
2018-11-01 14:45:51 +03:00
|
|
|
}
|
|
|
|
|
2020-01-02 04:09:25 +03:00
|
|
|
bool MemoryManager::validate_user_write(const Process& process, VirtualAddress vaddr, size_t size) const
|
2018-11-01 14:45:51 +03:00
|
|
|
{
|
2019-12-31 05:43:24 +03:00
|
|
|
if (!is_user_address(vaddr))
|
|
|
|
return false;
|
2020-01-02 04:09:25 +03:00
|
|
|
return validate_range<AccessSpace::User, AccessType::Write>(process, vaddr, size);
|
2018-10-22 16:42:39 +03:00
|
|
|
}
|
2018-11-02 22:41:58 +03:00
|
|
|
|
2019-12-19 21:13:44 +03:00
|
|
|
void MemoryManager::register_vmobject(VMObject& vmobject)
|
2018-11-08 23:20:09 +03:00
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-12-19 21:13:44 +03:00
|
|
|
m_vmobjects.append(&vmobject);
|
2018-11-08 23:20:09 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:13:44 +03:00
|
|
|
void MemoryManager::unregister_vmobject(VMObject& vmobject)
|
2018-11-08 23:20:09 +03:00
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-12-19 21:13:44 +03:00
|
|
|
m_vmobjects.remove(&vmobject);
|
2018-11-08 23:20:09 +03:00
|
|
|
}
|
2018-11-09 00:24:02 +03:00
|
|
|
|
|
|
|
void MemoryManager::register_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-07 13:56:50 +03:00
|
|
|
if (region.vaddr().get() >= 0xc0000000)
|
2019-08-08 11:53:24 +03:00
|
|
|
m_kernel_regions.append(®ion);
|
2019-05-14 12:51:00 +03:00
|
|
|
else
|
2019-08-08 11:53:24 +03:00
|
|
|
m_user_regions.append(®ion);
|
2018-11-09 00:24:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregister_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-06-07 13:56:50 +03:00
|
|
|
if (region.vaddr().get() >= 0xc0000000)
|
2019-05-14 12:51:00 +03:00
|
|
|
m_kernel_regions.remove(®ion);
|
|
|
|
else
|
|
|
|
m_user_regions.remove(®ion);
|
2018-11-09 00:24:02 +03:00
|
|
|
}
|
2018-11-09 03:25:31 +03:00
|
|
|
|
2019-03-24 00:03:17 +03:00
|
|
|
ProcessPagingScope::ProcessPagingScope(Process& process)
|
|
|
|
{
|
|
|
|
ASSERT(current);
|
|
|
|
MM.enter_process_paging_scope(process);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessPagingScope::~ProcessPagingScope()
|
|
|
|
{
|
|
|
|
MM.enter_process_paging_scope(current->process());
|
|
|
|
}
|