2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-18 00:13:55 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-03-08 14:33:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2020-05-06 18:40:06 +03:00
|
|
|
#include <Kernel/CMOS.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2020-08-25 01:38:20 +03:00
|
|
|
#include <Kernel/Heap/kmalloc.h>
|
2019-06-09 12:48:58 +03:00
|
|
|
#include <Kernel/Multiboot.h>
|
2020-05-06 18:40:06 +03:00
|
|
|
#include <Kernel/Process.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <Kernel/StdLib.h>
|
2019-08-07 19:06:17 +03:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2020-03-07 20:24:41 +03:00
|
|
|
#include <Kernel/VM/ContiguousVMObject.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2020-02-16 03:33:41 +03:00
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
|
|
|
#include <Kernel/VM/PhysicalRegion.h>
|
2020-03-01 22:45:39 +03:00
|
|
|
#include <Kernel/VM/SharedInodeVMObject.h>
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2020-08-25 01:38:20 +03:00
|
|
|
extern u8* start_of_kernel_image;
|
|
|
|
extern u8* end_of_kernel_image;
|
2020-03-08 12:36:51 +03:00
|
|
|
extern FlatPtr start_of_kernel_text;
|
|
|
|
extern FlatPtr start_of_kernel_data;
|
|
|
|
extern FlatPtr end_of_kernel_bss;
|
2021-02-14 19:35:07 +03:00
|
|
|
extern FlatPtr start_of_ro_after_init;
|
|
|
|
extern FlatPtr end_of_ro_after_init;
|
2021-02-19 20:21:54 +03:00
|
|
|
extern FlatPtr start_of_unmap_after_init;
|
|
|
|
extern FlatPtr end_of_unmap_after_init;
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-01-20 19:49:55 +03:00
|
|
|
extern multiboot_module_entry_t multiboot_copy_boot_modules_array[16];
|
|
|
|
extern size_t multiboot_copy_boot_modules_count;
|
|
|
|
|
|
|
|
// Treat the super pages as logically separate from .bss
|
|
|
|
__attribute__((section(".super_pages"))) static u8 super_pages[1 * MiB];
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
// NOTE: We can NOT use AK::Singleton for this class, because
|
|
|
|
// MemoryManager::initialize is called *before* global constructors are
|
|
|
|
// run. If we do, then AK::Singleton would get re-initialized, causing
|
|
|
|
// the memory manager to be initialized twice!
|
2020-08-22 18:53:34 +03:00
|
|
|
static MemoryManager* s_the;
|
2020-07-06 16:27:22 +03:00
|
|
|
RecursiveSpinLock s_mm_lock;
|
2018-10-18 00:13:55 +03:00
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
const LogStream& operator<<(const LogStream& stream, const UsedMemoryRange& value)
|
|
|
|
{
|
|
|
|
return stream << UserMemoryRangeTypeNames[static_cast<int>(value.type)] << " range @ " << value.start << " - " << value.end;
|
|
|
|
}
|
|
|
|
|
2018-10-27 15:56:52 +03:00
|
|
|
MemoryManager& MM
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
bool MemoryManager::is_initialized()
|
|
|
|
{
|
|
|
|
return s_the != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:59:20 +03:00
|
|
|
MemoryManager::MemoryManager()
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2020-07-06 18:11:52 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-01-17 21:59:20 +03:00
|
|
|
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
|
2020-01-17 23:03:52 +03:00
|
|
|
parse_memory_map();
|
2020-02-10 22:00:32 +03:00
|
|
|
write_cr3(kernel_page_directory().cr3());
|
2020-01-18 00:07:20 +03:00
|
|
|
protect_kernel_image();
|
2020-02-15 15:12:02 +03:00
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
// We're temporarily "committing" to two pages that we need to allocate below
|
|
|
|
if (!commit_user_physical_pages(2))
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
|
|
|
m_shared_zero_page = allocate_committed_user_physical_page();
|
|
|
|
|
|
|
|
// We're wasting a page here, we just need a special tag (physical
|
|
|
|
// address) so that we know when we need to lazily allocate a page
|
|
|
|
// that we should be drawing this page from the committed pool rather
|
|
|
|
// than potentially failing if no pages are available anymore.
|
|
|
|
// By using a tag we don't have to query the VMObject for every page
|
|
|
|
// whether it was committed or not
|
|
|
|
m_lazy_committed_page = allocate_committed_user_physical_page();
|
2020-01-18 01:56:13 +03:00
|
|
|
}
|
2020-01-18 00:07:20 +03:00
|
|
|
|
2020-01-18 01:56:13 +03:00
|
|
|
MemoryManager::~MemoryManager()
|
|
|
|
{
|
2020-01-17 23:03:52 +03:00
|
|
|
}
|
|
|
|
|
2020-01-18 00:07:20 +03:00
|
|
|
void MemoryManager::protect_kernel_image()
|
|
|
|
{
|
2020-11-01 02:19:18 +03:00
|
|
|
ScopedSpinLock page_lock(kernel_page_directory().get_lock());
|
2020-01-18 00:07:20 +03:00
|
|
|
// Disable writing to the kernel text and rodata segments.
|
2021-02-12 18:20:21 +03:00
|
|
|
for (auto i = (FlatPtr)&start_of_kernel_text; i < (FlatPtr)&start_of_kernel_data; i += PAGE_SIZE) {
|
2020-09-02 01:10:54 +03:00
|
|
|
auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
2020-01-18 00:07:20 +03:00
|
|
|
pte.set_writable(false);
|
|
|
|
}
|
2020-07-03 19:23:09 +03:00
|
|
|
if (Processor::current().has_feature(CPUFeature::NX)) {
|
2021-01-20 19:49:55 +03:00
|
|
|
// Disable execution of the kernel data, bss and heap segments.
|
2021-02-12 18:20:21 +03:00
|
|
|
for (auto i = (FlatPtr)&start_of_kernel_data; i < (FlatPtr)&end_of_kernel_image; i += PAGE_SIZE) {
|
2020-09-02 01:10:54 +03:00
|
|
|
auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
2020-08-25 01:38:20 +03:00
|
|
|
pte.set_execute_disabled(true);
|
|
|
|
}
|
2020-01-18 00:07:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 19:35:07 +03:00
|
|
|
void MemoryManager::protect_readonly_after_init_memory()
|
|
|
|
{
|
|
|
|
ScopedSpinLock mm_lock(s_mm_lock);
|
|
|
|
ScopedSpinLock page_lock(kernel_page_directory().get_lock());
|
|
|
|
// Disable writing to the .ro_after_init section
|
|
|
|
for (auto i = (FlatPtr)&start_of_ro_after_init; i < (FlatPtr)&end_of_ro_after_init; i += PAGE_SIZE) {
|
|
|
|
auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
|
|
|
pte.set_writable(false);
|
|
|
|
flush_tlb(&kernel_page_directory(), VirtualAddress(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:21:54 +03:00
|
|
|
void MemoryManager::unmap_memory_after_init()
|
|
|
|
{
|
|
|
|
ScopedSpinLock mm_lock(s_mm_lock);
|
|
|
|
ScopedSpinLock page_lock(kernel_page_directory().get_lock());
|
|
|
|
|
|
|
|
auto start = page_round_down((FlatPtr)&start_of_unmap_after_init);
|
|
|
|
auto end = page_round_up((FlatPtr)&end_of_unmap_after_init);
|
|
|
|
|
|
|
|
// Unmap the entire .unmap_after_init section
|
|
|
|
for (auto i = start; i < end; i += PAGE_SIZE) {
|
|
|
|
auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i));
|
|
|
|
pte.clear();
|
|
|
|
flush_tlb(&kernel_page_directory(), VirtualAddress(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
dmesgln("Unmapped {} KiB of kernel text after init! :^)", (end - start) / KiB);
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:03:25 +03:00
|
|
|
void MemoryManager::register_reserved_ranges()
|
|
|
|
{
|
|
|
|
ASSERT(!m_physical_memory_ranges.is_empty());
|
|
|
|
ContiguousReservedMemoryRange range;
|
|
|
|
for (auto& current_range : m_physical_memory_ranges) {
|
|
|
|
if (current_range.type != PhysicalMemoryRangeType::Reserved) {
|
|
|
|
if (range.start.is_null())
|
|
|
|
continue;
|
|
|
|
m_reserved_memory_ranges.append(ContiguousReservedMemoryRange { range.start, current_range.start.get() - range.start.get() });
|
|
|
|
range.start.set((FlatPtr) nullptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!range.start.is_null()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
range.start = current_range.start;
|
|
|
|
}
|
|
|
|
if (m_physical_memory_ranges.last().type != PhysicalMemoryRangeType::Reserved)
|
|
|
|
return;
|
|
|
|
if (range.start.is_null())
|
|
|
|
return;
|
|
|
|
m_reserved_memory_ranges.append(ContiguousReservedMemoryRange { range.start, m_physical_memory_ranges.last().start.get() + m_physical_memory_ranges.last().length - range.start.get() });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::is_allowed_to_mmap_to_userspace(PhysicalAddress start_address, const Range& range) const
|
|
|
|
{
|
|
|
|
ASSERT(!m_reserved_memory_ranges.is_empty());
|
|
|
|
for (auto& current_range : m_reserved_memory_ranges) {
|
|
|
|
if (!(current_range.start <= start_address))
|
|
|
|
continue;
|
|
|
|
if (!(current_range.start.offset(current_range.length) > start_address))
|
|
|
|
continue;
|
2021-02-12 18:20:21 +03:00
|
|
|
if (current_range.length < range.size())
|
2021-01-29 15:03:25 +03:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:03:52 +03:00
|
|
|
void MemoryManager::parse_memory_map()
|
|
|
|
{
|
2021-02-12 18:20:21 +03:00
|
|
|
RefPtr<PhysicalRegion> physical_region;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
// Register used memory regions that we know of.
|
|
|
|
m_used_memory_ranges.ensure_capacity(4);
|
|
|
|
m_used_memory_ranges.append(UsedMemoryRange { UsedMemoryRangeType::LowMemory, PhysicalAddress(0x00000000), PhysicalAddress(1 * MiB) });
|
2021-02-14 11:57:19 +03:00
|
|
|
m_used_memory_ranges.append(UsedMemoryRange { UsedMemoryRangeType::Kernel, PhysicalAddress(virtual_to_low_physical(FlatPtr(&start_of_kernel_image))), PhysicalAddress(page_round_up(virtual_to_low_physical(FlatPtr(&end_of_kernel_image)))) });
|
2020-08-25 01:38:20 +03:00
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
if (multiboot_info_ptr->flags & 0x4) {
|
|
|
|
auto* bootmods_start = multiboot_copy_boot_modules_array;
|
|
|
|
auto* bootmods_end = bootmods_start + multiboot_copy_boot_modules_count;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
for (auto* bootmod = bootmods_start; bootmod < bootmods_end; bootmod++) {
|
|
|
|
m_used_memory_ranges.append(UsedMemoryRange { UsedMemoryRangeType::BootModule, PhysicalAddress(bootmod->start), PhysicalAddress(bootmod->end) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* mmap_begin = reinterpret_cast<multiboot_memory_map_t*>(low_physical_to_virtual(multiboot_info_ptr->mmap_addr));
|
|
|
|
auto* mmap_end = reinterpret_cast<multiboot_memory_map_t*>(low_physical_to_virtual(multiboot_info_ptr->mmap_addr) + multiboot_info_ptr->mmap_length);
|
|
|
|
|
2021-02-15 14:56:39 +03:00
|
|
|
for (auto& used_range : m_used_memory_ranges) {
|
2021-01-19 21:13:03 +03:00
|
|
|
klog() << "MM: " << used_range;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* mmap = mmap_begin; mmap < mmap_end; mmap++) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: Multiboot mmap: address={:p}, length={}, type={}", mmap->addr, mmap->len, mmap->type);
|
2021-01-19 21:13:03 +03:00
|
|
|
|
2021-01-29 15:03:25 +03:00
|
|
|
auto start_address = PhysicalAddress(mmap->addr);
|
2021-02-12 18:20:21 +03:00
|
|
|
auto length = static_cast<size_t>(mmap->len);
|
2021-01-29 15:03:25 +03:00
|
|
|
switch (mmap->type) {
|
|
|
|
case (MULTIBOOT_MEMORY_AVAILABLE):
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Usable, start_address, length });
|
|
|
|
break;
|
|
|
|
case (MULTIBOOT_MEMORY_RESERVED):
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Reserved, start_address, length });
|
|
|
|
break;
|
|
|
|
case (MULTIBOOT_MEMORY_ACPI_RECLAIMABLE):
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_Reclaimable, start_address, length });
|
|
|
|
break;
|
|
|
|
case (MULTIBOOT_MEMORY_NVS):
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::ACPI_NVS, start_address, length });
|
|
|
|
break;
|
|
|
|
case (MULTIBOOT_MEMORY_BADRAM):
|
|
|
|
klog() << "MM: Warning, detected bad memory range!";
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::BadMemory, start_address, length });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dbgln("MM: Unknown range!");
|
|
|
|
m_physical_memory_ranges.append(PhysicalMemoryRange { PhysicalMemoryRangeType::Unknown, start_address, length });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
if (mmap->type != MULTIBOOT_MEMORY_AVAILABLE)
|
2019-11-23 19:27:09 +03:00
|
|
|
continue;
|
|
|
|
|
2019-09-17 02:58:38 +03:00
|
|
|
if ((mmap->addr + mmap->len) > 0xffffffff)
|
|
|
|
continue;
|
|
|
|
|
2021-01-19 21:13:03 +03:00
|
|
|
// Fix up unaligned memory regions.
|
2020-03-08 12:36:51 +03:00
|
|
|
auto diff = (FlatPtr)mmap->addr % PAGE_SIZE;
|
2019-09-28 09:22:50 +03:00
|
|
|
if (diff != 0) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: Got an unaligned physical_region from the bootloader; correcting {:p} by {} bytes", mmap->addr, diff);
|
2019-09-28 09:22:50 +03:00
|
|
|
diff = PAGE_SIZE - diff;
|
|
|
|
mmap->addr += diff;
|
|
|
|
mmap->len -= diff;
|
|
|
|
}
|
|
|
|
if ((mmap->len % PAGE_SIZE) != 0) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: Got an unaligned physical_region from the bootloader; correcting length {} by {} bytes", mmap->len, mmap->len % PAGE_SIZE);
|
2019-09-28 09:22:50 +03:00
|
|
|
mmap->len -= mmap->len % PAGE_SIZE;
|
|
|
|
}
|
|
|
|
if (mmap->len < PAGE_SIZE) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: Memory physical_region from bootloader is too small; we want >= {} bytes, but got {} bytes", PAGE_SIZE, mmap->len);
|
2019-09-28 09:22:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-10 19:05:22 +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);
|
|
|
|
|
2021-01-20 19:49:55 +03:00
|
|
|
// Skip used memory ranges.
|
|
|
|
bool should_skip = false;
|
2021-02-15 14:56:39 +03:00
|
|
|
for (auto& used_range : m_used_memory_ranges) {
|
2021-01-20 19:49:55 +03:00
|
|
|
if (addr.get() >= used_range.start.get() && addr.get() <= used_range.end.get()) {
|
|
|
|
should_skip = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (should_skip)
|
2020-08-25 01:38:20 +03:00
|
|
|
continue;
|
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
// Assign page to user physical physical_region.
|
|
|
|
if (physical_region.is_null() || physical_region->upper().offset(PAGE_SIZE) != addr) {
|
2021-01-20 19:49:55 +03:00
|
|
|
m_user_physical_regions.append(PhysicalRegion::create(addr, addr));
|
2021-02-12 18:20:21 +03:00
|
|
|
physical_region = m_user_physical_regions.last();
|
2019-11-23 19:27:09 +03:00
|
|
|
} else {
|
2021-02-12 18:20:21 +03:00
|
|
|
physical_region->expand(physical_region->lower(), addr);
|
2019-06-09 12:48:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 16:58:03 +03:00
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
// Append statically-allocated super physical physical_region.
|
2021-01-20 19:49:55 +03:00
|
|
|
m_super_physical_regions.append(PhysicalRegion::create(
|
|
|
|
PhysicalAddress(virtual_to_low_physical(FlatPtr(super_pages))),
|
|
|
|
PhysicalAddress(virtual_to_low_physical(FlatPtr(super_pages + sizeof(super_pages))))));
|
|
|
|
|
2020-08-25 01:38:20 +03:00
|
|
|
for (auto& region : m_super_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
m_super_physical_pages += region.finalize_capacity();
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: Super physical region: {} - {}", region.lower(), region.upper());
|
2020-08-25 01:38:20 +03:00
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2020-08-25 01:38:20 +03:00
|
|
|
for (auto& region : m_user_physical_regions) {
|
2019-06-27 14:34:28 +03:00
|
|
|
m_user_physical_pages += region.finalize_capacity();
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: User physical region: {} - {}", region.lower(), region.upper());
|
2020-08-25 01:38:20 +03:00
|
|
|
}
|
2020-05-08 23:15:02 +03:00
|
|
|
|
|
|
|
ASSERT(m_super_physical_pages > 0);
|
|
|
|
ASSERT(m_user_physical_pages > 0);
|
2020-09-05 06:12:25 +03:00
|
|
|
|
|
|
|
// We start out with no committed pages
|
2021-01-04 02:58:50 +03:00
|
|
|
m_user_physical_pages_uncommitted = m_user_physical_pages.load();
|
2021-01-29 15:03:25 +03:00
|
|
|
register_reserved_ranges();
|
|
|
|
for (auto& range : m_reserved_memory_ranges) {
|
|
|
|
dmesgln("MM: Contiguous reserved range from {}, length is {}", range.start, range.length);
|
|
|
|
}
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2020-11-01 02:19:18 +03:00
|
|
|
PageTableEntry* MemoryManager::pte(PageDirectory& page_directory, VirtualAddress vaddr)
|
2020-02-21 15:05:39 +03:00
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-07-06 18:11:52 +03:00
|
|
|
ASSERT(s_mm_lock.own_lock());
|
2020-11-01 02:19:18 +03:00
|
|
|
ASSERT(page_directory.get_lock().own_lock());
|
2020-02-21 15:05:39 +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;
|
|
|
|
|
|
|
|
auto* pd = quickmap_pd(const_cast<PageDirectory&>(page_directory), page_directory_table_index);
|
|
|
|
const PageDirectoryEntry& pde = pd[page_directory_index];
|
|
|
|
if (!pde.is_present())
|
|
|
|
return nullptr;
|
|
|
|
|
2020-03-08 12:36:51 +03:00
|
|
|
return &quickmap_pt(PhysicalAddress((FlatPtr)pde.page_table_base()))[page_table_index];
|
2020-02-21 15:05:39 +03:00
|
|
|
}
|
|
|
|
|
2020-09-02 01:10:54 +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();
|
2020-07-06 18:11:52 +03:00
|
|
|
ASSERT(s_mm_lock.own_lock());
|
2020-11-01 02:19:18 +03:00
|
|
|
ASSERT(page_directory.get_lock().own_lock());
|
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()) {
|
2020-09-01 22:40:34 +03:00
|
|
|
bool did_purge = false;
|
|
|
|
auto page_table = allocate_user_physical_page(ShouldZeroFill::Yes, &did_purge);
|
2020-09-02 01:10:54 +03:00
|
|
|
if (!page_table) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("MM: Unable to allocate page table to map {}", vaddr);
|
2020-09-02 01:10:54 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-09-01 22:40:34 +03:00
|
|
|
if (did_purge) {
|
|
|
|
// If any memory had to be purged, ensure_pte may have been called as part
|
|
|
|
// of the purging process. So we need to re-map the pd in this case to ensure
|
|
|
|
// we're writing to the correct underlying physical page
|
|
|
|
pd = quickmap_pd(page_directory, page_directory_table_index);
|
|
|
|
ASSERT(&pde == &pd[page_directory_index]); // Sanity check
|
2020-09-18 10:49:51 +03:00
|
|
|
|
2020-09-01 22:40:34 +03:00
|
|
|
ASSERT(!pde.is_present()); // Should have not changed
|
|
|
|
}
|
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());
|
2020-08-28 06:29:17 +03:00
|
|
|
// Use page_directory_table_index and page_directory_index as key
|
|
|
|
// This allows us to release the page table entry when no longer needed
|
|
|
|
auto result = page_directory.m_page_tables.set(vaddr.get() & ~0x1fffff, move(page_table));
|
2020-08-26 02:14:48 +03:00
|
|
|
ASSERT(result == AK::HashSetResult::InsertedNewEntry);
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
2020-01-17 21:59:20 +03:00
|
|
|
|
2020-09-02 01:10:54 +03:00
|
|
|
return &quickmap_pt(PhysicalAddress((FlatPtr)pde.page_table_base()))[page_table_index];
|
2018-10-18 00:13:55 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 06:29:17 +03:00
|
|
|
void MemoryManager::release_pte(PageDirectory& page_directory, VirtualAddress vaddr, bool is_last_release)
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
ASSERT(s_mm_lock.own_lock());
|
2020-11-01 02:19:18 +03:00
|
|
|
ASSERT(page_directory.get_lock().own_lock());
|
2020-08-28 06:29:17 +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;
|
|
|
|
|
|
|
|
auto* pd = quickmap_pd(page_directory, page_directory_table_index);
|
|
|
|
PageDirectoryEntry& pde = pd[page_directory_index];
|
|
|
|
if (pde.is_present()) {
|
|
|
|
auto* page_table = quickmap_pt(PhysicalAddress((FlatPtr)pde.page_table_base()));
|
|
|
|
auto& pte = page_table[page_table_index];
|
|
|
|
pte.clear();
|
|
|
|
|
|
|
|
if (is_last_release || page_table_index == 0x1ff) {
|
|
|
|
// If this is the last PTE in a region or the last PTE in a page table then
|
|
|
|
// check if we can also release the page table
|
|
|
|
bool all_clear = true;
|
|
|
|
for (u32 i = 0; i <= 0x1ff; i++) {
|
|
|
|
if (!page_table[i].is_null()) {
|
|
|
|
all_clear = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all_clear) {
|
|
|
|
pde.clear();
|
|
|
|
|
|
|
|
auto result = page_directory.m_page_tables.remove(vaddr.get() & ~0x1fffff);
|
|
|
|
ASSERT(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 01:04:35 +03:00
|
|
|
void MemoryManager::initialize(u32 cpu)
|
2018-10-18 00:13:55 +03:00
|
|
|
{
|
2020-06-29 01:04:35 +03:00
|
|
|
auto mm_data = new MemoryManagerData;
|
|
|
|
Processor::current().set_mm_data(*mm_data);
|
|
|
|
|
2020-08-30 01:41:30 +03:00
|
|
|
if (cpu == 0) {
|
2020-08-22 18:53:34 +03:00
|
|
|
s_the = new MemoryManager;
|
2020-08-30 01:41:30 +03:00
|
|
|
kmalloc_enable_expand();
|
|
|
|
}
|
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
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
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
|
|
|
|
2021-02-08 17:45:40 +03:00
|
|
|
Region* MemoryManager::user_region_from_vaddr(Space& space, VirtualAddress vaddr)
|
2019-08-06 11:31:20 +03:00
|
|
|
{
|
2018-11-05 15:48:07 +03:00
|
|
|
// FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure!
|
2021-02-08 17:45:40 +03:00
|
|
|
ScopedSpinLock lock(space.get_lock());
|
|
|
|
for (auto& region : space.regions()) {
|
2019-06-27 14:34:28 +03:00
|
|
|
if (region.contains(vaddr))
|
|
|
|
return ®ion;
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
2019-01-25 03:39:15 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:45:40 +03:00
|
|
|
Region* MemoryManager::find_region_from_vaddr(Space& space, VirtualAddress vaddr)
|
2019-08-06 11:31:20 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2021-02-08 17:45:40 +03:00
|
|
|
if (auto* region = user_region_from_vaddr(space, vaddr))
|
2019-08-06 12:19:16 +03:00
|
|
|
return region;
|
2020-05-20 15:02:43 +03:00
|
|
|
return kernel_region_from_vaddr(vaddr);
|
2019-08-06 11:31:20 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 00:52:28 +03:00
|
|
|
Region* MemoryManager::find_region_from_vaddr(VirtualAddress vaddr)
|
2019-08-06 12:19:16 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2019-08-06 12:19:16 +03:00
|
|
|
if (auto* region = kernel_region_from_vaddr(vaddr))
|
|
|
|
return region;
|
2020-02-10 22:00:32 +03:00
|
|
|
auto page_directory = PageDirectory::find_by_cr3(read_cr3());
|
2019-08-06 12:19:16 +03:00
|
|
|
if (!page_directory)
|
|
|
|
return nullptr;
|
2021-02-08 17:45:40 +03:00
|
|
|
ASSERT(page_directory->space());
|
|
|
|
return user_region_from_vaddr(*page_directory->space(), vaddr);
|
2019-08-06 12:19:16 +03:00
|
|
|
}
|
|
|
|
|
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();
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-06-27 22:42:28 +03:00
|
|
|
if (Processor::current().in_irq()) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("CPU[{}] BUG! Page fault while handling IRQ! code={}, vaddr={}, irq level: {}",
|
2021-01-27 06:44:01 +03:00
|
|
|
Processor::id(), fault.code(), fault.vaddr(), Processor::current().in_irq());
|
2020-02-23 12:46:16 +03:00
|
|
|
dump_kernel_regions();
|
2020-06-27 22:42:28 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
2020-02-23 12:46:16 +03:00
|
|
|
}
|
2021-01-24 01:29:11 +03:00
|
|
|
#if PAGE_FAULT_DEBUG
|
2021-01-27 06:44:01 +03:00
|
|
|
dbgln("MM: CPU[{}] handle_page_fault({:#04x}) at {}", Processor::id(), fault.code(), fault.vaddr());
|
2018-11-05 15:48:07 +03:00
|
|
|
#endif
|
2020-07-31 00:52:28 +03:00
|
|
|
auto* region = find_region_from_vaddr(fault.vaddr());
|
2018-11-08 17:39:26 +03:00
|
|
|
if (!region) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("CPU[{}] NP(error) fault at invalid address {}", Processor::id(), fault.vaddr());
|
2018-11-08 17:39:26 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2021-01-23 19:11:45 +03:00
|
|
|
return region->handle_fault(fault, lock);
|
2018-10-18 14:05:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_contiguous_kernel_region(size_t size, String name, u8 access, size_t physical_alignment, Region::Cacheable cacheable)
|
2020-03-07 20:24:41 +03:00
|
|
|
{
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-03-07 20:24:41 +03:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2021-01-27 23:01:45 +03:00
|
|
|
if (!range.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-12-06 08:49:24 +03:00
|
|
|
auto vmobject = ContiguousVMObject::create_with_size(size, physical_alignment);
|
2021-02-14 03:25:22 +03:00
|
|
|
return allocate_kernel_region_with_vmobject(range.value(), vmobject, move(name), access, cacheable);
|
2020-03-07 20:24:41 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region(size_t size, String name, u8 access, AllocationStrategy strategy, Region::Cacheable cacheable)
|
2019-05-14 12:51:00 +03:00
|
|
|
{
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2019-05-20 05:46:29 +03:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2021-01-27 23:01:45 +03:00
|
|
|
if (!range.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-09-06 00:52:14 +03:00
|
|
|
auto vmobject = AnonymousVMObject::create_with_size(size, strategy);
|
|
|
|
if (!vmobject)
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-02-14 03:25:22 +03:00
|
|
|
return allocate_kernel_region_with_vmobject(range.value(), vmobject.release_nonnull(), move(name), access, cacheable);
|
2019-05-14 12:51:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region(PhysicalAddress paddr, size_t size, String name, u8 access, Region::Cacheable cacheable)
|
2019-07-19 18:01:16 +03:00
|
|
|
{
|
2020-01-10 00:29:31 +03:00
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-01-10 00:29:31 +03:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2021-01-27 23:01:45 +03:00
|
|
|
if (!range.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-01-28 22:48:07 +03:00
|
|
|
auto vmobject = AnonymousVMObject::create_for_physical_range(paddr, size);
|
|
|
|
if (!vmobject)
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-02-14 03:25:22 +03:00
|
|
|
return allocate_kernel_region_with_vmobject(range.value(), *vmobject, move(name), access, cacheable);
|
2019-07-19 18:01:16 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region_identity(PhysicalAddress paddr, size_t size, String name, u8 access, Region::Cacheable cacheable)
|
2020-06-02 07:55:09 +03:00
|
|
|
{
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-06-02 07:55:09 +03:00
|
|
|
auto range = kernel_page_directory().identity_range_allocator().allocate_specific(VirtualAddress(paddr.get()), size);
|
2021-01-27 23:01:45 +03:00
|
|
|
if (!range.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2020-06-02 07:55:09 +03:00
|
|
|
auto vmobject = AnonymousVMObject::create_for_physical_range(paddr, size);
|
|
|
|
if (!vmobject)
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-02-14 03:25:22 +03:00
|
|
|
return allocate_kernel_region_with_vmobject(range.value(), *vmobject, move(name), access, cacheable);
|
2020-06-02 07:55:09 +03:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region_with_vmobject(const Range& range, VMObject& vmobject, String name, u8 access, Region::Cacheable cacheable)
|
2019-12-16 00:21:28 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2021-02-14 03:25:22 +03:00
|
|
|
auto region = Region::create_kernel_only(range, vmobject, 0, move(name), access, cacheable);
|
2020-03-01 17:55:27 +03:00
|
|
|
if (region)
|
|
|
|
region->map(kernel_page_directory());
|
2019-12-16 00:21:28 +03:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
OwnPtr<Region> MemoryManager::allocate_kernel_region_with_vmobject(VMObject& vmobject, size_t size, String name, u8 access, Region::Cacheable cacheable)
|
2020-03-01 17:55:27 +03:00
|
|
|
{
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-03-01 17:55:27 +03:00
|
|
|
auto range = kernel_page_directory().range_allocator().allocate_anywhere(size);
|
2021-01-27 23:01:45 +03:00
|
|
|
if (!range.has_value())
|
2021-01-11 02:29:28 +03:00
|
|
|
return {};
|
2021-02-14 03:25:22 +03:00
|
|
|
return allocate_kernel_region_with_vmobject(range.value(), vmobject, move(name), access, cacheable);
|
2020-03-01 17:55:27 +03:00
|
|
|
}
|
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
bool MemoryManager::commit_user_physical_pages(size_t page_count)
|
|
|
|
{
|
|
|
|
ASSERT(page_count > 0);
|
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
|
|
|
if (m_user_physical_pages_uncommitted < page_count)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_user_physical_pages_uncommitted -= page_count;
|
|
|
|
m_user_physical_pages_committed += page_count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::uncommit_user_physical_pages(size_t page_count)
|
|
|
|
{
|
|
|
|
ASSERT(page_count > 0);
|
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
|
|
|
ASSERT(m_user_physical_pages_committed >= page_count);
|
|
|
|
|
|
|
|
m_user_physical_pages_uncommitted += page_count;
|
|
|
|
m_user_physical_pages_committed -= page_count;
|
|
|
|
}
|
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
void MemoryManager::deallocate_user_physical_page(const PhysicalPage& page)
|
2019-06-11 14:13:02 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2019-06-11 14:13:02 +03:00
|
|
|
for (auto& region : m_user_physical_regions) {
|
2021-01-22 19:49:55 +03:00
|
|
|
if (!region.contains(page))
|
2019-06-11 14:13:02 +03:00
|
|
|
continue;
|
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
region.return_page(page);
|
2019-06-27 14:34:28 +03:00
|
|
|
--m_user_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
// Always return pages to the uncommitted pool. Pages that were
|
|
|
|
// committed and allocated are only freed upon request. Once
|
|
|
|
// returned there is no guarantee being able to get them back.
|
|
|
|
++m_user_physical_pages_uncommitted;
|
2019-06-11 14:13:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: deallocate_user_physical_page couldn't figure out region for user page @ {}", page.paddr());
|
2019-06-11 14:13:02 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
RefPtr<PhysicalPage> MemoryManager::find_free_user_physical_page(bool committed)
|
2018-11-08 14:59:16 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ASSERT(s_mm_lock.is_locked());
|
2019-06-27 14:34:28 +03:00
|
|
|
RefPtr<PhysicalPage> page;
|
2020-09-05 06:12:25 +03:00
|
|
|
if (committed) {
|
|
|
|
// Draw from the committed pages pool. We should always have these pages available
|
|
|
|
ASSERT(m_user_physical_pages_committed > 0);
|
|
|
|
m_user_physical_pages_committed--;
|
|
|
|
} else {
|
|
|
|
// We need to make sure we don't touch pages that we have committed to
|
|
|
|
if (m_user_physical_pages_uncommitted == 0)
|
|
|
|
return {};
|
|
|
|
m_user_physical_pages_uncommitted--;
|
|
|
|
}
|
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);
|
2020-09-05 06:12:25 +03:00
|
|
|
if (!page.is_null()) {
|
|
|
|
++m_user_physical_pages_used;
|
2019-11-09 00:39:29 +03:00
|
|
|
break;
|
2020-09-05 06:12:25 +03:00
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
}
|
2020-09-05 06:12:25 +03:00
|
|
|
ASSERT(!committed || !page.is_null());
|
2019-11-09 00:39:29 +03:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
NonnullRefPtr<PhysicalPage> MemoryManager::allocate_committed_user_physical_page(ShouldZeroFill should_zero_fill)
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
|
|
|
auto page = find_free_user_physical_page(true);
|
|
|
|
if (should_zero_fill == ShouldZeroFill::Yes) {
|
|
|
|
auto* ptr = quickmap_page(*page);
|
|
|
|
memset(ptr, 0, PAGE_SIZE);
|
|
|
|
unquickmap_page();
|
|
|
|
}
|
|
|
|
return page.release_nonnull();
|
|
|
|
}
|
|
|
|
|
2020-09-01 22:40:34 +03:00
|
|
|
RefPtr<PhysicalPage> MemoryManager::allocate_user_physical_page(ShouldZeroFill should_zero_fill, bool* did_purge)
|
2019-11-09 00:39:29 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-09-05 06:12:25 +03:00
|
|
|
auto page = find_free_user_physical_page(false);
|
2020-09-01 22:40:34 +03:00
|
|
|
bool purged_pages = false;
|
2019-06-11 14:13:02 +03:00
|
|
|
|
|
|
|
if (!page) {
|
2020-05-08 23:15:02 +03:00
|
|
|
// We didn't have a single free physical page. Let's try to free something up!
|
|
|
|
// First, we look for a purgeable VMObject in the volatile state.
|
2021-01-01 17:32:06 +03:00
|
|
|
for_each_vmobject([&](auto& vmobject) {
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!vmobject.is_anonymous())
|
2021-01-01 17:32:06 +03:00
|
|
|
return IterationDecision::Continue;
|
2020-09-06 00:52:14 +03:00
|
|
|
int purged_page_count = static_cast<AnonymousVMObject&>(vmobject).purge_with_interrupts_disabled({});
|
2020-05-08 23:10:47 +03:00
|
|
|
if (purged_page_count) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dbgln("MM: Purge saved the day! Purged {} pages from AnonymousVMObject", purged_page_count);
|
2020-09-05 06:12:25 +03:00
|
|
|
page = find_free_user_physical_page(false);
|
2020-09-01 22:40:34 +03:00
|
|
|
purged_pages = true;
|
2020-05-08 23:10:47 +03:00
|
|
|
ASSERT(page);
|
|
|
|
return IterationDecision::Break;
|
2019-12-26 13:45:36 +03:00
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
if (!page) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: no user physical pages available");
|
2019-12-26 13:45:36 +03:00
|
|
|
return {};
|
|
|
|
}
|
2019-02-20 17:34:55 +03:00
|
|
|
}
|
2019-06-11 14:13:02 +03:00
|
|
|
|
2019-01-31 05:57:06 +03:00
|
|
|
if (should_zero_fill == ShouldZeroFill::Yes) {
|
2020-01-20 15:06:14 +03:00
|
|
|
auto* ptr = 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
|
|
|
|
2020-09-01 22:40:34 +03:00
|
|
|
if (did_purge)
|
|
|
|
*did_purge = purged_pages;
|
2019-06-11 14:13:02 +03:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
void MemoryManager::deallocate_supervisor_physical_page(const PhysicalPage& page)
|
2019-06-11 14:13:02 +03:00
|
|
|
{
|
2020-08-21 01:41:05 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
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)) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dbgln("MM: deallocate_supervisor_physical_page: {} not in {} - {}", page.paddr(), region.lower(), region.upper());
|
2019-06-11 14:13:02 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-22 06:49:50 +03:00
|
|
|
region.return_page(page);
|
2019-06-27 14:34:28 +03:00
|
|
|
--m_super_physical_pages_used;
|
2019-06-11 14:13:02 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
dbgln("MM: deallocate_supervisor_physical_page couldn't figure out region for super page @ {}", page.paddr());
|
2019-06-11 14:13:02 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-11-08 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2020-12-06 08:49:24 +03:00
|
|
|
NonnullRefPtrVector<PhysicalPage> MemoryManager::allocate_contiguous_supervisor_physical_pages(size_t size, size_t physical_alignment)
|
2020-03-07 20:24:41 +03:00
|
|
|
{
|
|
|
|
ASSERT(!(size % PAGE_SIZE));
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-03-07 20:24:41 +03:00
|
|
|
size_t count = ceil_div(size, PAGE_SIZE);
|
2020-05-08 21:15:01 +03:00
|
|
|
NonnullRefPtrVector<PhysicalPage> physical_pages;
|
2020-03-07 20:24:41 +03:00
|
|
|
|
|
|
|
for (auto& region : m_super_physical_regions) {
|
2020-12-06 08:49:24 +03:00
|
|
|
physical_pages = region.take_contiguous_free_pages(count, true, physical_alignment);
|
2020-12-20 22:12:32 +03:00
|
|
|
if (!physical_pages.is_empty())
|
2020-12-06 08:49:24 +03:00
|
|
|
continue;
|
2020-03-07 20:24:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (physical_pages.is_empty()) {
|
|
|
|
if (m_super_physical_regions.is_empty()) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: no super physical regions available (?)");
|
2020-03-07 20:24:41 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: no super physical pages available");
|
2020-03-07 20:24:41 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:15:01 +03:00
|
|
|
auto cleanup_region = MM.allocate_kernel_region(physical_pages[0].paddr(), PAGE_SIZE * count, "MemoryManager Allocation Sanitization", Region::Access::Read | Region::Access::Write);
|
2020-03-07 20:24:41 +03:00
|
|
|
fast_u32_fill((u32*)cleanup_region->vaddr().as_ptr(), 0, (PAGE_SIZE * count) / sizeof(u32));
|
|
|
|
m_super_physical_pages_used += count;
|
|
|
|
return physical_pages;
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<PhysicalPage> MemoryManager::allocate_supervisor_physical_page()
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
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);
|
2020-12-20 22:12:32 +03:00
|
|
|
if (!page.is_null())
|
|
|
|
break;
|
2019-06-11 14:13:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!page) {
|
|
|
|
if (m_super_physical_regions.is_empty()) {
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: no super physical regions available (?)");
|
2019-06-11 14:13:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 18:20:21 +03:00
|
|
|
dmesgln("MM: no super physical pages available");
|
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
|
|
|
|
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)
|
2021-02-08 17:45:40 +03:00
|
|
|
{
|
|
|
|
enter_space(process.space());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::enter_space(Space& space)
|
2018-11-01 13:30:48 +03:00
|
|
|
{
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
ASSERT(current_thread != nullptr);
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2019-11-27 14:40:42 +03:00
|
|
|
|
2021-02-08 17:45:40 +03:00
|
|
|
current_thread->tss().cr3 = space.page_directory().cr3();
|
|
|
|
write_cr3(space.page_directory().cr3());
|
2018-11-01 13:30:48 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 16:27:22 +03:00
|
|
|
void MemoryManager::flush_tlb_local(VirtualAddress vaddr, size_t page_count)
|
2018-10-23 12:03:56 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
Processor::flush_tlb_local(vaddr, page_count);
|
2018-10-23 12:03:56 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 22:27:38 +03:00
|
|
|
void MemoryManager::flush_tlb(const PageDirectory* page_directory, VirtualAddress vaddr, size_t page_count)
|
2018-10-23 16:53:11 +03:00
|
|
|
{
|
2021-01-02 22:27:38 +03:00
|
|
|
Processor::flush_tlb(page_directory, vaddr, page_count);
|
2018-10-23 16:53:11 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:19:37 +03:00
|
|
|
extern "C" PageTableEntry boot_pd3_pt1023[1024];
|
2020-01-17 21:59:20 +03:00
|
|
|
|
|
|
|
PageDirectoryEntry* MemoryManager::quickmap_pd(PageDirectory& directory, size_t pdpt_index)
|
|
|
|
{
|
2020-07-06 18:11:52 +03:00
|
|
|
ASSERT(s_mm_lock.own_lock());
|
2020-11-01 02:19:50 +03:00
|
|
|
auto& mm_data = get_data();
|
2020-05-26 00:19:37 +03:00
|
|
|
auto& pte = boot_pd3_pt1023[4];
|
2020-01-17 21:59:20 +03:00
|
|
|
auto pd_paddr = directory.m_directory_pages[pdpt_index]->paddr();
|
|
|
|
if (pte.physical_page_base() != pd_paddr.as_ptr()) {
|
|
|
|
pte.set_physical_page_base(pd_paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
2020-07-06 18:11:52 +03:00
|
|
|
// Because we must continue to hold the MM lock while we use this
|
|
|
|
// mapping, it is sufficient to only flush on the current CPU. Other
|
|
|
|
// CPUs trying to use this API must wait on the MM lock anyway
|
|
|
|
flush_tlb_local(VirtualAddress(0xffe04000));
|
2020-11-01 02:19:50 +03:00
|
|
|
} else {
|
|
|
|
// Even though we don't allow this to be called concurrently, it's
|
|
|
|
// possible that this PD was mapped on a different CPU and we don't
|
|
|
|
// broadcast the flush. If so, we still need to flush the TLB.
|
|
|
|
if (mm_data.m_last_quickmap_pd != pd_paddr)
|
|
|
|
flush_tlb_local(VirtualAddress(0xffe04000));
|
2020-01-17 21:59:20 +03:00
|
|
|
}
|
2020-11-01 02:19:50 +03:00
|
|
|
mm_data.m_last_quickmap_pd = pd_paddr;
|
2020-01-17 21:59:20 +03:00
|
|
|
return (PageDirectoryEntry*)0xffe04000;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageTableEntry* MemoryManager::quickmap_pt(PhysicalAddress pt_paddr)
|
|
|
|
{
|
2020-07-06 18:11:52 +03:00
|
|
|
ASSERT(s_mm_lock.own_lock());
|
2020-11-01 02:19:50 +03:00
|
|
|
auto& mm_data = get_data();
|
2020-06-29 01:04:35 +03:00
|
|
|
auto& pte = boot_pd3_pt1023[0];
|
2020-01-17 21:59:20 +03:00
|
|
|
if (pte.physical_page_base() != pt_paddr.as_ptr()) {
|
|
|
|
pte.set_physical_page_base(pt_paddr.get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
2020-07-06 18:11:52 +03:00
|
|
|
// Because we must continue to hold the MM lock while we use this
|
|
|
|
// mapping, it is sufficient to only flush on the current CPU. Other
|
|
|
|
// CPUs trying to use this API must wait on the MM lock anyway
|
|
|
|
flush_tlb_local(VirtualAddress(0xffe00000));
|
2020-11-01 02:19:50 +03:00
|
|
|
} else {
|
|
|
|
// Even though we don't allow this to be called concurrently, it's
|
|
|
|
// possible that this PT was mapped on a different CPU and we don't
|
|
|
|
// broadcast the flush. If so, we still need to flush the TLB.
|
|
|
|
if (mm_data.m_last_quickmap_pt != pt_paddr)
|
|
|
|
flush_tlb_local(VirtualAddress(0xffe00000));
|
2020-01-17 21:59:20 +03:00
|
|
|
}
|
2020-11-01 02:19:50 +03:00
|
|
|
mm_data.m_last_quickmap_pt = pt_paddr;
|
2020-06-29 01:04:35 +03:00
|
|
|
return (PageTableEntry*)0xffe00000;
|
2020-01-17 21:59:20 +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();
|
2020-06-29 01:04:35 +03:00
|
|
|
auto& mm_data = get_data();
|
2020-07-03 14:19:50 +03:00
|
|
|
mm_data.m_quickmap_prev_flags = mm_data.m_quickmap_in_use.lock();
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-01-18 00:18:56 +03:00
|
|
|
|
2021-01-27 06:44:01 +03:00
|
|
|
u32 pte_idx = 8 + Processor::id();
|
2020-06-29 01:04:35 +03:00
|
|
|
VirtualAddress vaddr(0xffe00000 + pte_idx * PAGE_SIZE);
|
|
|
|
|
|
|
|
auto& pte = boot_pd3_pt1023[pte_idx];
|
2020-01-18 00:18:56 +03:00
|
|
|
if (pte.physical_page_base() != physical_page.paddr().as_ptr()) {
|
|
|
|
pte.set_physical_page_base(physical_page.paddr().get());
|
|
|
|
pte.set_present(true);
|
|
|
|
pte.set_writable(true);
|
|
|
|
pte.set_user_allowed(false);
|
2020-07-06 18:11:52 +03:00
|
|
|
flush_tlb_local(vaddr);
|
2020-01-18 00:18:56 +03:00
|
|
|
}
|
2020-06-29 01:04:35 +03:00
|
|
|
return vaddr.as_ptr();
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::unquickmap_page()
|
|
|
|
{
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-06-29 01:04:35 +03:00
|
|
|
auto& mm_data = get_data();
|
|
|
|
ASSERT(mm_data.m_quickmap_in_use.is_locked());
|
2021-01-27 06:44:01 +03:00
|
|
|
u32 pte_idx = 8 + Processor::id();
|
2020-06-29 01:04:35 +03:00
|
|
|
VirtualAddress vaddr(0xffe00000 + pte_idx * PAGE_SIZE);
|
|
|
|
auto& pte = boot_pd3_pt1023[pte_idx];
|
2020-02-08 14:49:00 +03:00
|
|
|
pte.clear();
|
2020-07-06 18:11:52 +03:00
|
|
|
flush_tlb_local(vaddr);
|
2020-07-03 14:19:50 +03:00
|
|
|
mm_data.m_quickmap_in_use.unlock(mm_data.m_quickmap_prev_flags);
|
2018-11-05 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2021-02-08 17:45:40 +03:00
|
|
|
auto* region = user_region_from_vaddr(const_cast<Process&>(process).space(), vaddr);
|
2021-02-14 03:25:22 +03:00
|
|
|
return region && region->is_user() && region->is_stack();
|
2019-11-17 14:11:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:13:44 +03:00
|
|
|
void MemoryManager::register_vmobject(VMObject& vmobject)
|
2018-11-08 23:20:09 +03:00
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
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
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
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)
|
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-06-02 07:55:09 +03:00
|
|
|
if (region.is_kernel())
|
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)
|
|
|
|
{
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2020-06-02 07:55:09 +03:00
|
|
|
if (region.is_kernel())
|
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
|
|
|
|
2020-01-18 10:34:28 +03:00
|
|
|
void MemoryManager::dump_kernel_regions()
|
|
|
|
{
|
2021-02-12 18:20:21 +03:00
|
|
|
dbgln("Kernel regions:");
|
|
|
|
dbgln("BEGIN END SIZE ACCESS NAME");
|
2020-07-06 16:27:22 +03:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2021-02-12 18:20:21 +03:00
|
|
|
for (auto& region : m_kernel_regions) {
|
2021-02-12 18:33:58 +03:00
|
|
|
dbgln("{:08x} -- {:08x} {:08x} {:c}{:c}{:c}{:c}{:c}{:c} {}",
|
|
|
|
region.vaddr().get(),
|
|
|
|
region.vaddr().offset(region.size() - 1).get(),
|
|
|
|
region.size(),
|
2021-02-12 18:20:21 +03:00
|
|
|
region.is_readable() ? 'R' : ' ',
|
|
|
|
region.is_writable() ? 'W' : ' ',
|
|
|
|
region.is_executable() ? 'X' : ' ',
|
|
|
|
region.is_shared() ? 'S' : ' ',
|
|
|
|
region.is_stack() ? 'T' : ' ',
|
|
|
|
region.is_syscall_region() ? 'C' : ' ',
|
|
|
|
region.name());
|
2020-01-18 10:34:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|