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-11-08 16:35:30 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-01-27 12:17:27 +03:00
|
|
|
#include "CMOS.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::MemoryManager()
|
|
|
|
{
|
2019-01-27 12:17:27 +03:00
|
|
|
// FIXME: This is not the best way to do memory map detection.
|
|
|
|
// Rewrite to use BIOS int 15,e820 once we have VM86 support.
|
|
|
|
word base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
|
|
|
|
word ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
|
|
|
|
|
|
|
|
kprintf("%u kB base memory\n", base_memory);
|
|
|
|
kprintf("%u kB extended memory\n", ext_memory);
|
|
|
|
|
|
|
|
m_ram_size = ext_memory * 1024;
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
m_kernel_page_directory = PageDirectory::create_at_fixed_address(PhysicalAddress(0x4000));
|
2018-12-03 03:38:22 +03:00
|
|
|
m_page_table_zero = (dword*)0x6000;
|
2018-11-01 13:30:48 +03:00
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
initialize_paging();
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
PageDirectory::PageDirectory(PhysicalAddress paddr)
|
2018-11-01 11:01:51 +03:00
|
|
|
{
|
2018-12-31 16:58:03 +03:00
|
|
|
kprintf("Instantiating PageDirectory with specific paddr P%x\n", paddr.get());
|
2019-01-01 04:09:43 +03:00
|
|
|
m_directory_page = adopt(*new PhysicalPage(paddr, true));
|
2018-11-02 01:04:34 +03:00
|
|
|
}
|
2018-11-01 11:01:51 +03:00
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
PageDirectory::PageDirectory()
|
2018-11-02 01:04:34 +03:00
|
|
|
{
|
2018-12-31 16:58:03 +03:00
|
|
|
MM.populate_page_directory(*this);
|
|
|
|
}
|
2018-12-26 23:31:46 +03:00
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
void MemoryManager::populate_page_directory(PageDirectory& page_directory)
|
|
|
|
{
|
2019-01-01 04:09:43 +03:00
|
|
|
page_directory.m_directory_page = allocate_supervisor_physical_page();
|
2018-12-31 16:58:03 +03:00
|
|
|
memset(page_directory.entries(), 0, PAGE_SIZE);
|
|
|
|
page_directory.entries()[0] = kernel_page_directory().entries()[0];
|
2018-11-01 11:01:51 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
void MemoryManager::initialize_paging()
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
|
|
|
static_assert(sizeof(MemoryManager::PageDirectoryEntry) == 4);
|
|
|
|
static_assert(sizeof(MemoryManager::PageTableEntry) == 4);
|
2018-12-03 03:38:22 +03:00
|
|
|
memset(m_page_table_zero, 0, PAGE_SIZE);
|
2018-10-18 00:13:55 +03:00
|
|
|
|
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
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Protect against null dereferences\n");
|
|
|
|
#endif
|
2018-10-21 22:57:59 +03:00
|
|
|
// Make null dereferences crash.
|
2018-12-03 03:38:22 +03:00
|
|
|
map_protected(LinearAddress(0), PAGE_SIZE);
|
2018-10-21 22:57:59 +03:00
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: Identity map bottom 4MB\n");
|
|
|
|
#endif
|
2019-01-01 04:09:43 +03:00
|
|
|
// The bottom 4 MB (except for the null page) are identity mapped & supervisor only.
|
|
|
|
// Every process shares these mappings.
|
|
|
|
create_identity_mapping(kernel_page_directory(), LinearAddress(PAGE_SIZE), (4 * MB) - PAGE_SIZE);
|
2019-01-16 02:44:09 +03:00
|
|
|
|
|
|
|
// Basic memory map:
|
|
|
|
// 0 -> 512 kB Kernel code. Root page directory & PDE 0.
|
|
|
|
// 1 MB -> 2 MB kmalloc_eternal() space.
|
|
|
|
// 2 MB -> 3 MB kmalloc() space.
|
|
|
|
// 3 MB -> 4 MB Supervisor physical pages (available for allocation!)
|
2019-01-27 12:17:27 +03:00
|
|
|
// 4 MB -> (max) MB Userspace physical pages (available for allocation!)
|
2019-01-16 02:44:09 +03:00
|
|
|
for (size_t i = (2 * MB); i < (4 * MB); i += PAGE_SIZE)
|
2019-01-01 04:09:43 +03:00
|
|
|
m_free_supervisor_physical_pages.append(adopt(*new PhysicalPage(PhysicalAddress(i), true)));
|
2018-12-31 16:58:03 +03:00
|
|
|
|
2019-01-27 12:17:27 +03:00
|
|
|
dbgprintf("MM: 4MB-%uMB available for allocation\n", m_ram_size / 1048576);
|
|
|
|
for (size_t i = (4 * MB); i < m_ram_size; i += PAGE_SIZE)
|
2019-01-01 04:09:43 +03:00
|
|
|
m_free_physical_pages.append(adopt(*new PhysicalPage(PhysicalAddress(i), false)));
|
2019-01-20 00:53:05 +03:00
|
|
|
m_quickmap_addr = LinearAddress(m_free_physical_pages.take_last().leakRef()->paddr().get());
|
2018-12-31 16:58:03 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-16 02:44:09 +03:00
|
|
|
dbgprintf("MM: Quickmap will use P%x\n", m_quickmap_addr.get());
|
2018-12-31 16:58:03 +03:00
|
|
|
dbgprintf("MM: Installing page directory\n");
|
|
|
|
#endif
|
|
|
|
asm volatile("movl %%eax, %%cr3"::"a"(kernel_page_directory().cr3()));
|
2018-10-18 00:13:55 +03:00
|
|
|
asm volatile(
|
|
|
|
"movl %cr0, %eax\n"
|
|
|
|
"orl $0x80000001, %eax\n"
|
|
|
|
"movl %eax, %cr0\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:23:00 +03:00
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_page_table(PageDirectory& page_directory, unsigned index)
|
2018-10-27 15:56:52 +03:00
|
|
|
{
|
2018-12-31 17:10:12 +03:00
|
|
|
ASSERT(!page_directory.m_physical_pages.contains(index));
|
2019-01-01 04:09:43 +03:00
|
|
|
auto physical_page = allocate_supervisor_physical_page();
|
2018-11-11 00:14:41 +03:00
|
|
|
if (!physical_page)
|
|
|
|
return nullptr;
|
|
|
|
dword address = physical_page->paddr().get();
|
2018-11-03 02:31:42 +03:00
|
|
|
memset((void*)address, 0, PAGE_SIZE);
|
2018-12-31 17:10:12 +03:00
|
|
|
page_directory.m_physical_pages.set(index, physical_page.copyRef());
|
|
|
|
return physical_page;
|
2018-11-03 02:31:42 +03:00
|
|
|
}
|
|
|
|
|
2019-01-01 04:09:43 +03:00
|
|
|
void MemoryManager::remove_identity_mapping(PageDirectory& page_directory, LinearAddress laddr, size_t size)
|
2018-11-03 02:31:42 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
// FIXME: ASSERT(laddr is 4KB aligned);
|
|
|
|
for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
|
|
|
|
auto pte_address = laddr.offset(offset);
|
2019-01-01 04:09:43 +03:00
|
|
|
auto pte = ensure_pte(page_directory, pte_address);
|
2018-12-03 03:38:22 +03: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-03 02:31:42 +03:00
|
|
|
}
|
2018-10-27 15:56:52 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 16:58:03 +03:00
|
|
|
auto MemoryManager::ensure_pte(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-12-31 16:58:03 +03:00
|
|
|
PageDirectoryEntry pde = PageDirectoryEntry(&page_directory.entries()[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-01-01 04:09:43 +03:00
|
|
|
dbgprintf("MM: PDE %u not present (requested for L%x), allocating\n", page_directory_index, laddr.get());
|
2018-10-31 22:10:39 +03:00
|
|
|
#endif
|
2018-11-02 01:04:34 +03:00
|
|
|
if (page_directory_index == 0) {
|
2018-12-31 16:58:03 +03:00
|
|
|
ASSERT(&page_directory == m_kernel_page_directory.ptr());
|
2018-12-03 03:38:22 +03:00
|
|
|
pde.setPageTableBase((dword)m_page_table_zero);
|
|
|
|
pde.set_user_allowed(false);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
2018-10-18 00:13:55 +03:00
|
|
|
} else {
|
2019-01-01 04:09:43 +03:00
|
|
|
ASSERT(&page_directory != m_kernel_page_directory.ptr());
|
2018-12-31 16:58:03 +03:00
|
|
|
auto page_table = allocate_page_table(page_directory, page_directory_index);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-01 04:09:43 +03:00
|
|
|
dbgprintf("MM: PD K%x (%s) at P%x allocated page table #%u (for L%x) at P%x\n",
|
2018-12-31 16:58:03 +03:00
|
|
|
&page_directory,
|
|
|
|
&page_directory == m_kernel_page_directory.ptr() ? "Kernel" : "User",
|
2019-01-01 04:09:43 +03:00
|
|
|
page_directory.cr3(),
|
2018-11-03 02:31:42 +03:00
|
|
|
page_directory_index,
|
|
|
|
laddr.get(),
|
2018-11-05 12:23:00 +03:00
|
|
|
page_table->paddr().get());
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
2018-11-05 12:23:00 +03:00
|
|
|
|
|
|
|
pde.setPageTableBase(page_table->paddr().get());
|
2018-12-03 03:38:22 +03:00
|
|
|
pde.set_user_allowed(true);
|
|
|
|
pde.set_present(true);
|
|
|
|
pde.set_writable(true);
|
2018-12-31 17:10:12 +03:00
|
|
|
page_directory.m_physical_pages.set(page_directory_index, move(page_table));
|
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
|
|
|
}
|
|
|
|
|
2019-01-24 20:09:46 +03:00
|
|
|
void MemoryManager::map_protected(LinearAddress laddr, size_t length)
|
2018-10-21 22:57:59 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-10-21 22:57:59 +03:00
|
|
|
// FIXME: ASSERT(linearAddress is 4KB aligned);
|
2018-11-03 02:31:42 +03:00
|
|
|
for (dword offset = 0; offset < length; offset += PAGE_SIZE) {
|
2019-01-24 20:09:46 +03:00
|
|
|
auto pteAddress = laddr.offset(offset);
|
2018-12-31 16:58:03 +03:00
|
|
|
auto pte = ensure_pte(kernel_page_directory(), pteAddress);
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(pteAddress.get());
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
|
|
|
flush_tlb(pteAddress);
|
2018-10-21 22:57:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 04:09:43 +03:00
|
|
|
void MemoryManager::create_identity_mapping(PageDirectory& page_directory, LinearAddress laddr, size_t size)
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-01-01 04:09:43 +03:00
|
|
|
ASSERT((laddr.get() & ~PAGE_MASK) == 0);
|
2018-11-03 02:31:42 +03:00
|
|
|
for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
|
|
|
|
auto pteAddress = laddr.offset(offset);
|
2019-01-01 04:09:43 +03:00
|
|
|
auto pte = ensure_pte(page_directory, pteAddress);
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(pteAddress.get());
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
2019-01-01 04:09:43 +03:00
|
|
|
page_directory.flush(pteAddress);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::initialize()
|
|
|
|
{
|
|
|
|
s_the = new MemoryManager;
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:48:07 +03:00
|
|
|
Region* MemoryManager::region_from_laddr(Process& process, LinearAddress laddr)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
|
|
|
|
// FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure!
|
|
|
|
for (auto& region : process.m_regions) {
|
|
|
|
if (region->contains(laddr))
|
|
|
|
return region.ptr();
|
|
|
|
}
|
2019-01-25 03:39:15 +03:00
|
|
|
dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), laddr.get(), process.page_directory().cr3());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Region* MemoryManager::region_from_laddr(const Process& process, LinearAddress laddr)
|
|
|
|
{
|
|
|
|
// FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure!
|
|
|
|
for (auto& region : process.m_regions) {
|
|
|
|
if (region->contains(laddr))
|
|
|
|
return region.ptr();
|
|
|
|
}
|
|
|
|
dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), laddr.get(), process.page_directory().cr3());
|
2018-11-08 17:39:26 +03:00
|
|
|
return nullptr;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
bool MemoryManager::zero_page(Region& region, unsigned page_index_in_region)
|
2018-11-19 04:17:20 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
auto& vmo = region.vmo();
|
|
|
|
auto physical_page = allocate_physical_page();
|
|
|
|
byte* dest_ptr = quickmap_page(*physical_page);
|
|
|
|
memset(dest_ptr, 0, PAGE_SIZE);
|
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> ZERO P%x\n", physical_page->paddr().get());
|
|
|
|
#endif
|
|
|
|
unquickmap_page();
|
2019-01-24 20:09:46 +03:00
|
|
|
region.m_cow_map.set(page_index_in_region, false);
|
2018-11-19 04:17:20 +03:00
|
|
|
vmo.physical_pages()[page_index_in_region] = move(physical_page);
|
2019-01-22 07:01:00 +03:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-19 04:17:20 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
bool MemoryManager::copy_on_write(Region& region, unsigned page_index_in_region)
|
2018-11-05 15:48:07 +03:00
|
|
|
{
|
2018-11-05 16:10:18 +03:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-11-08 16:35:30 +03:00
|
|
|
auto& vmo = region.vmo();
|
|
|
|
if (vmo.physical_pages()[page_index_in_region]->retain_count() == 1) {
|
2018-11-05 16:10:18 +03:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> It's a COW page but nobody is sharing it anymore. Remap r/w\n");
|
|
|
|
#endif
|
2019-01-24 20:09:46 +03:00
|
|
|
region.m_cow_map.set(page_index_in_region, false);
|
2019-01-22 07:01:00 +03:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-05 16:10:18 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf(" >> It's a COW page and it's time to COW!\n");
|
|
|
|
#endif
|
2018-11-08 16:35:30 +03:00
|
|
|
auto physical_page_to_copy = move(vmo.physical_pages()[page_index_in_region]);
|
2018-11-11 00:14:41 +03:00
|
|
|
auto physical_page = allocate_physical_page();
|
|
|
|
byte* dest_ptr = quickmap_page(*physical_page);
|
2019-01-24 20:09:46 +03:00
|
|
|
const byte* src_ptr = region.laddr().offset(page_index_in_region * PAGE_SIZE).as_ptr();
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
2018-11-11 00:14:41 +03:00
|
|
|
dbgprintf(" >> COW P%x <- P%x\n", physical_page->paddr().get(), physical_page_to_copy->paddr().get());
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
|
|
|
memcpy(dest_ptr, src_ptr, PAGE_SIZE);
|
2018-11-11 00:14:41 +03:00
|
|
|
vmo.physical_pages()[page_index_in_region] = move(physical_page);
|
2018-11-05 15:48:07 +03:00
|
|
|
unquickmap_page();
|
2019-01-24 20:09:46 +03:00
|
|
|
region.m_cow_map.set(page_index_in_region, false);
|
2019-01-22 07:01:00 +03:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2018-11-05 15:48:07 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
bool Region::page_in()
|
2018-11-08 23:20:09 +03:00
|
|
|
{
|
2019-01-22 07:01:00 +03:00
|
|
|
ASSERT(m_page_directory);
|
2018-11-08 23:20:09 +03:00
|
|
|
ASSERT(!vmo().is_anonymous());
|
2019-01-16 14:57:07 +03:00
|
|
|
ASSERT(vmo().inode());
|
2018-11-08 23:20:09 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: page_in %u pages\n", page_count());
|
|
|
|
#endif
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
auto& vmo_page = vmo().physical_pages()[first_page_index() + i];
|
|
|
|
if (vmo_page.is_null()) {
|
2019-01-22 07:01:00 +03:00
|
|
|
bool success = MM.page_in_from_inode(*this, i);
|
2018-11-08 23:20:09 +03:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-22 07:01:00 +03:00
|
|
|
MM.remap_region_page(*this, i, true);
|
2018-11-08 23:20:09 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
bool MemoryManager::page_in_from_inode(Region& region, unsigned page_index_in_region)
|
2018-11-08 14:59:16 +03:00
|
|
|
{
|
2019-01-24 20:09:46 +03:00
|
|
|
ASSERT(region.page_directory());
|
2018-11-08 16:35:30 +03:00
|
|
|
auto& vmo = region.vmo();
|
|
|
|
ASSERT(!vmo.is_anonymous());
|
2019-01-16 14:57:07 +03:00
|
|
|
ASSERT(vmo.inode());
|
|
|
|
auto& inode = *vmo.inode();
|
2018-11-08 23:20:09 +03:00
|
|
|
auto& vmo_page = vmo.physical_pages()[region.first_page_index() + page_index_in_region];
|
|
|
|
ASSERT(vmo_page.is_null());
|
|
|
|
vmo_page = allocate_physical_page();
|
|
|
|
if (vmo_page.is_null()) {
|
2019-01-16 14:57:07 +03:00
|
|
|
kprintf("MM: page_in_from_inode was unable to allocate a physical page\n");
|
2018-11-08 14:59:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-22 07:01:00 +03:00
|
|
|
remap_region_page(region, page_index_in_region, true);
|
2019-01-24 20:09:46 +03:00
|
|
|
byte* dest_ptr = region.laddr().offset(page_index_in_region * PAGE_SIZE).as_ptr();
|
2018-11-11 17:36:40 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-16 14:57:07 +03:00
|
|
|
dbgprintf("MM: page_in_from_inode ready to read from inode, will write to L%x!\n", dest_ptr);
|
2018-11-11 17:36:40 +03:00
|
|
|
#endif
|
2018-11-08 14:59:16 +03:00
|
|
|
sti(); // Oh god here we go...
|
2019-01-16 14:57:07 +03:00
|
|
|
auto nread = inode.read_bytes(vmo.inode_offset() + ((region.first_page_index() + page_index_in_region) * PAGE_SIZE), PAGE_SIZE, dest_ptr, nullptr);
|
2018-11-08 14:59:16 +03:00
|
|
|
if (nread < 0) {
|
2019-01-16 14:57:07 +03:00
|
|
|
kprintf("MM: page_in_from_inode had error (%d) while reading!\n", nread);
|
2018-11-08 14:59:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nread < PAGE_SIZE) {
|
|
|
|
// If we read less than a page, zero out the rest to avoid leaking uninitialized data.
|
|
|
|
memset(dest_ptr + nread, 0, PAGE_SIZE - nread);
|
|
|
|
}
|
|
|
|
cli();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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();
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef PAGE_FAULT_DEBUG
|
|
|
|
dbgprintf("MM: handle_page_fault(%w) at L%x\n", fault.code(), fault.laddr().get());
|
|
|
|
#endif
|
2019-01-01 04:09:43 +03:00
|
|
|
ASSERT(fault.laddr() != m_quickmap_addr);
|
2018-11-05 15:48:07 +03:00
|
|
|
auto* region = region_from_laddr(*current, fault.laddr());
|
2018-11-08 17:39:26 +03:00
|
|
|
if (!region) {
|
|
|
|
kprintf("NP(error) fault at invalid address L%x\n", fault.laddr().get());
|
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2018-11-05 15:48:07 +03:00
|
|
|
auto page_index_in_region = region->page_index_from_address(fault.laddr());
|
2018-11-05 12:29:19 +03:00
|
|
|
if (fault.is_not_present()) {
|
2019-01-16 14:57:07 +03:00
|
|
|
if (region->vmo().inode()) {
|
|
|
|
dbgprintf("NP(inode) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-01-22 07:01:00 +03:00
|
|
|
page_in_from_inode(*region, page_index_in_region);
|
2018-11-08 14:59:16 +03:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
} else {
|
2018-11-19 04:17:20 +03:00
|
|
|
dbgprintf("NP(zero) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-01-22 07:01:00 +03:00
|
|
|
zero_page(*region, page_index_in_region);
|
2018-11-19 04:17:20 +03:00
|
|
|
return PageFaultResponse::Continue;
|
2018-11-08 14:59:16 +03:00
|
|
|
}
|
2018-11-05 12:29:19 +03:00
|
|
|
} else if (fault.is_protection_violation()) {
|
2019-01-24 20:09:46 +03:00
|
|
|
if (region->m_cow_map.get(page_index_in_region)) {
|
2018-11-08 14:59:16 +03:00
|
|
|
dbgprintf("PV(cow) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2019-01-22 07:01:00 +03:00
|
|
|
bool success = copy_on_write(*region, page_index_in_region);
|
2018-11-05 15:48:07 +03:00
|
|
|
ASSERT(success);
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2018-11-08 14:59:16 +03:00
|
|
|
kprintf("PV(error) fault in Region{%p}[%u]\n", region, page_index_in_region);
|
2018-11-05 15:48:07 +03:00
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-18 14:05:00 +03:00
|
|
|
}
|
2018-11-05 15:48:07 +03:00
|
|
|
|
2018-10-18 14:05:00 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:59:16 +03:00
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_physical_page()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
if (1 > m_free_physical_pages.size())
|
|
|
|
return { };
|
|
|
|
#ifdef MM_DEBUG
|
2019-01-01 04:09:43 +03:00
|
|
|
dbgprintf("MM: allocate_physical_page vending P%x (%u remaining)\n", m_free_physical_pages.last()->paddr().get(), m_free_physical_pages.size());
|
2018-11-08 14:59:16 +03:00
|
|
|
#endif
|
2019-01-20 00:53:05 +03:00
|
|
|
return m_free_physical_pages.take_last();
|
2018-11-08 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2019-01-01 04:09:43 +03:00
|
|
|
RetainPtr<PhysicalPage> MemoryManager::allocate_supervisor_physical_page()
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-01-01 04:09:43 +03:00
|
|
|
if (1 > m_free_supervisor_physical_pages.size())
|
|
|
|
return { };
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: allocate_supervisor_physical_page vending P%x (%u remaining)\n", m_free_supervisor_physical_pages.last()->paddr().get(), m_free_supervisor_physical_pages.size());
|
|
|
|
#endif
|
2019-01-20 00:53:05 +03:00
|
|
|
return m_free_supervisor_physical_pages.take_last();
|
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
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2018-12-31 16:58:03 +03:00
|
|
|
current->m_tss.cr3 = process.page_directory().cr3();
|
|
|
|
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(
|
|
|
|
"mov %cr3, %eax\n"
|
|
|
|
"mov %eax, %cr3\n"
|
2018-11-03 02:31:42 +03:00
|
|
|
);
|
2018-10-23 12:03:56 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
void MemoryManager::flush_tlb(LinearAddress laddr)
|
2018-10-23 16:53:11 +03:00
|
|
|
{
|
2018-11-02 11:25:23 +03:00
|
|
|
asm volatile("invlpg %0": :"m" (*(char*)laddr.get()) : "memory");
|
2018-10-23 16:53:11 +03:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:48:07 +03:00
|
|
|
byte* MemoryManager::quickmap_page(PhysicalPage& physical_page)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-01 04:09:43 +03:00
|
|
|
auto page_laddr = m_quickmap_addr;
|
|
|
|
auto pte = ensure_pte(current->page_directory(), page_laddr);
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(physical_page.paddr().get());
|
2019-01-01 04:09:43 +03:00
|
|
|
pte.set_present(true);
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_writable(true);
|
|
|
|
flush_tlb(page_laddr);
|
2019-01-01 04:09:43 +03:00
|
|
|
ASSERT((dword)pte.physical_page_base() == physical_page.paddr().get());
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-01 04:09:43 +03:00
|
|
|
dbgprintf("MM: >> quickmap_page L%x => P%x @ PTE=%p\n", page_laddr, physical_page.paddr().get(), pte.ptr());
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
2019-01-24 20:09:46 +03:00
|
|
|
return page_laddr.as_ptr();
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unquickmap_page()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2019-01-01 04:09:43 +03:00
|
|
|
auto page_laddr = m_quickmap_addr;
|
|
|
|
auto pte = ensure_pte(current->page_directory(), page_laddr);
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-12-29 05:34:24 +03:00
|
|
|
auto old_physical_address = pte.physical_page_base();
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
|
|
|
flush_tlb(page_laddr);
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: >> unquickmap_page L%x =/> P%x\n", page_laddr, old_physical_address);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
void MemoryManager::remap_region_page(Region& region, unsigned page_index_in_region, bool user_allowed)
|
2018-11-05 15:48:07 +03:00
|
|
|
{
|
2019-01-24 20:09:46 +03:00
|
|
|
ASSERT(region.page_directory());
|
2018-11-05 15:48:07 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-01-24 20:09:46 +03:00
|
|
|
auto page_laddr = region.laddr().offset(page_index_in_region * PAGE_SIZE);
|
|
|
|
auto pte = ensure_pte(*region.page_directory(), page_laddr);
|
2018-11-08 16:35:30 +03:00
|
|
|
auto& physical_page = region.vmo().physical_pages()[page_index_in_region];
|
2018-11-05 15:48:07 +03:00
|
|
|
ASSERT(physical_page);
|
2018-12-03 03:38:22 +03: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-01-24 20:09:46 +03:00
|
|
|
if (region.m_cow_map.get(page_index_in_region))
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_writable(false);
|
2018-11-05 15:48:07 +03:00
|
|
|
else
|
2019-01-24 20:09:46 +03:00
|
|
|
pte.set_writable(region.is_writable());
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_user_allowed(user_allowed);
|
2019-01-24 20:09:46 +03:00
|
|
|
region.page_directory()->flush(page_laddr);
|
2018-11-05 15:48:07 +03:00
|
|
|
#ifdef MM_DEBUG
|
2019-01-24 20:09:46 +03: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_laddr.get(), physical_page->paddr().get(), physical_page.ptr());
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::remap_region(Process& process, Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
2019-01-24 20:09:46 +03:00
|
|
|
map_region_at_address(process.page_directory(), region, region.laddr(), true);
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-31 16:58:03 +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;
|
2019-01-24 20:09:46 +03:00
|
|
|
region.set_page_directory(page_directory);
|
2018-11-08 16:35:30 +03:00
|
|
|
auto& vmo = region.vmo();
|
2018-11-11 17:36:40 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-08 23:20:09 +03: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 17:36:40 +03:00
|
|
|
#endif
|
2018-12-29 05:34:24 +03:00
|
|
|
for (size_t i = 0; i < region.page_count(); ++i) {
|
2018-11-01 13:30:48 +03:00
|
|
|
auto page_laddr = laddr.offset(i * PAGE_SIZE);
|
2018-12-03 03:38:22 +03:00
|
|
|
auto pte = ensure_pte(page_directory, page_laddr);
|
2018-12-29 05:34:24 +03:00
|
|
|
auto& physical_page = vmo.physical_pages()[region.first_page_index() + i];
|
2018-11-05 12:23:00 +03:00
|
|
|
if (physical_page) {
|
2018-12-03 03:38:22 +03: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 05:34:24 +03:00
|
|
|
// FIXME: It seems wrong that the *region* cow map is essentially using *VMO* relative indices.
|
2019-01-24 20:09:46 +03:00
|
|
|
if (region.m_cow_map.get(region.first_page_index() + i))
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_writable(false);
|
2018-11-05 15:48:07 +03:00
|
|
|
else
|
2019-01-24 20:09:46 +03:00
|
|
|
pte.set_writable(region.is_writable());
|
2018-11-05 12:23:00 +03:00
|
|
|
} else {
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
2019-01-24 20:09:46 +03:00
|
|
|
pte.set_writable(region.is_writable());
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_user_allowed(user_allowed);
|
2018-12-31 22:52:59 +03:00
|
|
|
page_directory.flush(page_laddr);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-12-31 16:58:03 +03:00
|
|
|
dbgprintf("MM: >> map_region_at_address (PD=%x) '%s' L%x => P%x (@%p)\n", &page_directory, region.name.characters(), page_laddr, physical_page ? physical_page->paddr().get() : 0, physical_page.ptr());
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
bool MemoryManager::unmap_region(Region& region)
|
2018-10-23 11:12:50 +03:00
|
|
|
{
|
2019-01-24 20:09:46 +03:00
|
|
|
ASSERT(region.page_directory());
|
2018-10-25 11:15:28 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 23:20:09 +03:00
|
|
|
for (size_t i = 0; i < region.page_count(); ++i) {
|
2019-01-24 20:09:46 +03:00
|
|
|
auto laddr = region.laddr().offset(i * PAGE_SIZE);
|
|
|
|
auto pte = ensure_pte(*region.page_directory(), laddr);
|
2018-12-03 03:38:22 +03:00
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
|
|
|
pte.set_user_allowed(false);
|
2019-01-24 20:09:46 +03:00
|
|
|
region.page_directory()->flush(laddr);
|
2018-11-01 13:30:48 +03:00
|
|
|
#ifdef MM_DEBUG
|
2018-11-08 23:20:09 +03:00
|
|
|
auto& physical_page = region.vmo().physical_pages()[region.first_page_index() + i];
|
2018-11-05 12:23:00 +03:00
|
|
|
dbgprintf("MM: >> Unmapped L%x => P%x <<\n", laddr, physical_page ? physical_page->paddr().get() : 0);
|
2018-11-01 13:30:48 +03:00
|
|
|
#endif
|
2018-10-27 15:56:52 +03:00
|
|
|
}
|
2019-01-24 20:09:46 +03:00
|
|
|
region.release_page_directory();
|
2018-10-27 15:56:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-03 03:38:22 +03:00
|
|
|
bool MemoryManager::map_region(Process& process, Region& region)
|
2018-10-22 16:42:39 +03:00
|
|
|
{
|
2019-01-24 20:09:46 +03:00
|
|
|
map_region_at_address(process.page_directory(), region, region.laddr(), 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
|
|
|
{
|
2019-01-25 03:39:15 +03:00
|
|
|
auto* region = region_from_laddr(process, laddr);
|
|
|
|
return region && region->is_readable();
|
2018-11-01 14:45:51 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-01-25 03:39:15 +03:00
|
|
|
auto* region = region_from_laddr(process, laddr);
|
|
|
|
return region && region->is_writable();
|
2018-10-22 16:42:39 +03:00
|
|
|
}
|
2018-11-02 22:41:58 +03:00
|
|
|
|
|
|
|
RetainPtr<Region> Region::clone()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
2019-01-24 20:09:46 +03:00
|
|
|
if (m_shared || (m_readable && !m_writable)) {
|
2018-11-08 16:35:30 +03:00
|
|
|
// Create a new region backed by the same VMObject.
|
2019-01-24 20:09:46 +03:00
|
|
|
return adopt(*new Region(laddr(), size(), m_vmo.copyRef(), m_offset_in_vmo, String(m_name), m_readable, m_writable));
|
2018-11-04 01:20:49 +03:00
|
|
|
}
|
2018-11-02 22:41:58 +03:00
|
|
|
|
2019-01-21 07:18:28 +03:00
|
|
|
dbgprintf("%s<%u> Region::clone(): cowing %s (L%x)\n",
|
|
|
|
current->name().characters(),
|
|
|
|
current->pid(),
|
2019-01-24 20:09:46 +03:00
|
|
|
m_name.characters(),
|
|
|
|
laddr().get());
|
2018-11-05 15:48:07 +03:00
|
|
|
// Set up a COW region. The parent (this) region becomes COW as well!
|
2018-11-08 23:20:09 +03:00
|
|
|
for (size_t i = 0; i < page_count(); ++i)
|
2019-01-24 20:09:46 +03:00
|
|
|
m_cow_map.set(i, true);
|
2018-11-05 15:48:07 +03:00
|
|
|
MM.remap_region(*current, *this);
|
2019-01-24 20:09:46 +03:00
|
|
|
return adopt(*new Region(laddr(), size(), m_vmo->clone(), m_offset_in_vmo, String(m_name), m_readable, m_writable, true));
|
2018-11-02 22:41:58 +03:00
|
|
|
}
|
2018-11-03 02:31:42 +03:00
|
|
|
|
2018-11-08 16:35:30 +03:00
|
|
|
Region::Region(LinearAddress a, size_t s, String&& n, bool r, bool w, bool cow)
|
2019-01-24 20:09:46 +03:00
|
|
|
: m_laddr(a)
|
|
|
|
, m_size(s)
|
2018-11-08 16:35:30 +03:00
|
|
|
, m_vmo(VMObject::create_anonymous(s))
|
2019-01-24 20:09:46 +03:00
|
|
|
, m_name(move(n))
|
|
|
|
, m_readable(r)
|
|
|
|
, m_writable(w)
|
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
|
2018-11-05 12:23:00 +03:00
|
|
|
{
|
2019-01-24 20:09:46 +03:00
|
|
|
m_vmo->set_name(m_name);
|
2018-11-09 00:24:02 +03:00
|
|
|
MM.register_region(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
Region::Region(LinearAddress a, size_t s, RetainPtr<Inode>&& inode, String&& n, bool r, bool w)
|
2019-01-24 20:09:46 +03:00
|
|
|
: m_laddr(a)
|
|
|
|
, m_size(s)
|
2019-01-16 14:57:07 +03:00
|
|
|
, m_vmo(VMObject::create_file_backed(move(inode), s))
|
2019-01-24 20:09:46 +03:00
|
|
|
, m_name(move(n))
|
|
|
|
, m_readable(r)
|
|
|
|
, m_writable(w)
|
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count()))
|
2018-11-08 16:35:30 +03:00
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
MM.register_region(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
2018-11-08 23:20:09 +03:00
|
|
|
Region::Region(LinearAddress a, size_t s, RetainPtr<VMObject>&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow)
|
2019-01-24 20:09:46 +03:00
|
|
|
: m_laddr(a)
|
|
|
|
, m_size(s)
|
2018-11-08 23:20:09 +03:00
|
|
|
, m_offset_in_vmo(offset_in_vmo)
|
2018-11-08 16:35:30 +03:00
|
|
|
, m_vmo(move(vmo))
|
2019-01-24 20:09:46 +03:00
|
|
|
, m_name(move(n))
|
|
|
|
, m_readable(r)
|
|
|
|
, m_writable(w)
|
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
|
2018-11-08 16:35:30 +03:00
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
MM.register_region(*this);
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Region::~Region()
|
|
|
|
{
|
2019-01-22 07:01:00 +03:00
|
|
|
if (m_page_directory) {
|
|
|
|
MM.unmap_region(*this);
|
|
|
|
ASSERT(!m_page_directory);
|
|
|
|
}
|
2018-11-09 00:24:02 +03:00
|
|
|
MM.unregister_region(*this);
|
2018-11-05 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-01 04:09:43 +03:00
|
|
|
PhysicalPage::PhysicalPage(PhysicalAddress paddr, bool supervisor)
|
|
|
|
: m_supervisor(supervisor)
|
|
|
|
, m_paddr(paddr)
|
2018-12-31 22:25:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-05 12:23:00 +03:00
|
|
|
void PhysicalPage::return_to_freelist()
|
|
|
|
{
|
2019-01-01 04:09:43 +03:00
|
|
|
ASSERT((paddr().get() & ~PAGE_MASK) == 0);
|
2018-11-05 12:23:00 +03:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_retain_count = 1;
|
2019-01-01 04:09:43 +03:00
|
|
|
if (m_supervisor)
|
|
|
|
MM.m_free_supervisor_physical_pages.append(adopt(*this));
|
|
|
|
else
|
|
|
|
MM.m_free_physical_pages.append(adopt(*this));
|
2018-11-05 12:23:00 +03:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: P%x released to freelist\n", m_paddr.get());
|
|
|
|
#endif
|
|
|
|
}
|
2018-11-08 16:35:30 +03:00
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
RetainPtr<VMObject> VMObject::create_file_backed(RetainPtr<Inode>&& inode, size_t size)
|
2018-11-08 16:35:30 +03:00
|
|
|
{
|
2018-11-08 17:39:26 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-01-16 14:57:07 +03:00
|
|
|
if (inode->vmo())
|
|
|
|
return static_cast<VMObject*>(inode->vmo());
|
2018-11-08 23:20:09 +03:00
|
|
|
size = ceilDiv(size, PAGE_SIZE) * PAGE_SIZE;
|
2019-01-16 14:57:07 +03:00
|
|
|
auto vmo = adopt(*new VMObject(move(inode), size));
|
|
|
|
vmo->inode()->set_vmo(vmo.ptr());
|
2018-11-08 17:39:26 +03:00
|
|
|
return vmo;
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RetainPtr<VMObject> VMObject::create_anonymous(size_t size)
|
|
|
|
{
|
2018-11-08 23:20:09 +03:00
|
|
|
size = ceilDiv(size, PAGE_SIZE) * PAGE_SIZE;
|
2018-11-08 16:35:30 +03:00
|
|
|
return adopt(*new VMObject(size));
|
|
|
|
}
|
|
|
|
|
2019-01-09 04:29:11 +03:00
|
|
|
RetainPtr<VMObject> VMObject::create_framebuffer_wrapper(PhysicalAddress paddr, size_t size)
|
|
|
|
{
|
|
|
|
size = ceilDiv(size, PAGE_SIZE) * PAGE_SIZE;
|
|
|
|
return adopt(*new VMObject(paddr, size));
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:35:30 +03:00
|
|
|
RetainPtr<VMObject> VMObject::clone()
|
|
|
|
{
|
|
|
|
return adopt(*new VMObject(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
VMObject::VMObject(VMObject& other)
|
|
|
|
: m_name(other.m_name)
|
|
|
|
, m_anonymous(other.m_anonymous)
|
2019-01-16 14:57:07 +03:00
|
|
|
, m_inode_offset(other.m_inode_offset)
|
2018-11-08 16:35:30 +03:00
|
|
|
, m_size(other.m_size)
|
2019-01-16 14:57:07 +03:00
|
|
|
, m_inode(other.m_inode)
|
2018-11-08 16:35:30 +03:00
|
|
|
, m_physical_pages(other.m_physical_pages)
|
|
|
|
{
|
2018-11-08 23:20:09 +03:00
|
|
|
MM.register_vmo(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VMObject::VMObject(size_t size)
|
|
|
|
: m_anonymous(true)
|
|
|
|
, m_size(size)
|
|
|
|
{
|
2018-11-08 23:20:09 +03:00
|
|
|
MM.register_vmo(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
m_physical_pages.resize(page_count());
|
|
|
|
}
|
|
|
|
|
2019-01-09 04:29:11 +03:00
|
|
|
VMObject::VMObject(PhysicalAddress paddr, size_t size)
|
|
|
|
: m_anonymous(true)
|
|
|
|
, m_size(size)
|
|
|
|
{
|
|
|
|
MM.register_vmo(*this);
|
|
|
|
for (size_t i = 0; i < size; i += PAGE_SIZE) {
|
|
|
|
m_physical_pages.append(adopt(*new PhysicalPage(paddr.offset(i), false)));
|
|
|
|
}
|
|
|
|
ASSERT(m_physical_pages.size() == page_count());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
VMObject::VMObject(RetainPtr<Inode>&& inode, size_t size)
|
2018-11-08 16:35:30 +03:00
|
|
|
: m_size(size)
|
2019-01-16 14:57:07 +03:00
|
|
|
, m_inode(move(inode))
|
2018-11-08 16:35:30 +03:00
|
|
|
{
|
|
|
|
m_physical_pages.resize(page_count());
|
2018-11-08 23:20:09 +03:00
|
|
|
MM.register_vmo(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VMObject::~VMObject()
|
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (m_inode) {
|
|
|
|
ASSERT(m_inode->vmo() == this);
|
|
|
|
m_inode->set_vmo(nullptr);
|
2018-11-08 17:39:26 +03:00
|
|
|
}
|
2018-11-08 23:20:09 +03:00
|
|
|
MM.unregister_vmo(*this);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 07:01:00 +03:00
|
|
|
int Region::commit()
|
2018-11-08 16:35:30 +03:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
#ifdef MM_DEBUG
|
2019-01-24 20:09:46 +03:00
|
|
|
dbgprintf("MM: commit %u pages in Region %p (VMO=%p) at L%x\n", vmo().page_count(), this, &vmo(), laddr.get());
|
2018-11-08 16:35:30 +03:00
|
|
|
#endif
|
2018-11-08 23:20:09 +03:00
|
|
|
for (size_t i = first_page_index(); i <= last_page_index(); ++i) {
|
2018-11-08 16:35:30 +03:00
|
|
|
if (!vmo().physical_pages()[i].is_null())
|
|
|
|
continue;
|
|
|
|
auto physical_page = MM.allocate_physical_page();
|
|
|
|
if (!physical_page) {
|
2018-11-08 23:20:09 +03:00
|
|
|
kprintf("MM: commit was unable to allocate a physical page\n");
|
2018-11-08 16:35:30 +03:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vmo().physical_pages()[i] = move(physical_page);
|
2019-01-22 07:01:00 +03:00
|
|
|
MM.remap_region_page(*this, i, true);
|
2018-11-08 16:35:30 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-08 23:20:09 +03:00
|
|
|
|
|
|
|
void MemoryManager::register_vmo(VMObject& vmo)
|
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 23:20:09 +03:00
|
|
|
m_vmos.set(&vmo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregister_vmo(VMObject& vmo)
|
|
|
|
{
|
2018-11-09 00:24:02 +03:00
|
|
|
InterruptDisabler disabler;
|
2018-11-08 23:20:09 +03:00
|
|
|
m_vmos.remove(&vmo);
|
|
|
|
}
|
2018-11-09 00:24:02 +03:00
|
|
|
|
|
|
|
void MemoryManager::register_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_regions.set(®ion);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unregister_region(Region& region)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
m_regions.remove(®ion);
|
|
|
|
}
|
2018-11-09 03:25:31 +03:00
|
|
|
|
2018-11-19 04:17:20 +03:00
|
|
|
size_t Region::committed() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
if (m_vmo->physical_pages()[first_page_index() + i])
|
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2018-12-31 16:58:03 +03:00
|
|
|
|
|
|
|
PageDirectory::~PageDirectory()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: ~PageDirectory K%x\n", this);
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-31 22:52:59 +03:00
|
|
|
|
|
|
|
void PageDirectory::flush(LinearAddress laddr)
|
|
|
|
{
|
|
|
|
if (¤t->page_directory() == this)
|
|
|
|
MM.flush_tlb(laddr);
|
|
|
|
}
|