2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
#include <AK/Concepts.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-27 13:34:28 +02:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-11-29 16:15:30 +01:00
|
|
|
#include <AK/String.h>
|
2021-06-21 17:34:09 +02:00
|
|
|
#include <Kernel/Arch/x86/PageFault.h>
|
2020-02-16 01:50:16 +01:00
|
|
|
#include <Kernel/Forward.h>
|
2020-06-27 13:42:28 -06:00
|
|
|
#include <Kernel/SpinLock.h>
|
2020-09-05 15:52:14 -06:00
|
|
|
#include <Kernel/VM/AllocationStrategy.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
|
|
|
|
#include <Kernel/VM/Region.h>
|
|
|
|
|
#include <Kernel/VM/VMObject.h>
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-02-14 09:57:19 +01:00
|
|
|
constexpr bool page_round_up_would_wrap(FlatPtr x)
|
|
|
|
|
{
|
2021-06-28 18:58:23 +02:00
|
|
|
return x > (explode_byte(0xFF) & ~0xFFF);
|
2021-02-14 09:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr FlatPtr page_round_up(FlatPtr x)
|
|
|
|
|
{
|
|
|
|
|
FlatPtr rounded = (((FlatPtr)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1));
|
2021-06-28 18:58:23 +02:00
|
|
|
// Rounding up >0xfffff000 wraps back to 0. That's never what we want.
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(x == 0 || rounded != 0);
|
2021-02-14 09:57:19 +01:00
|
|
|
return rounded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr FlatPtr page_round_down(FlatPtr x)
|
|
|
|
|
{
|
|
|
|
|
return ((FlatPtr)(x)) & ~(PAGE_SIZE - 1);
|
|
|
|
|
}
|
2019-01-13 00:27:25 +01:00
|
|
|
|
2021-03-04 17:50:05 +01:00
|
|
|
inline FlatPtr low_physical_to_virtual(FlatPtr physical)
|
2020-01-17 19:59:20 +01:00
|
|
|
{
|
2021-06-28 18:43:18 +02:00
|
|
|
return physical + KERNEL_BASE;
|
2020-01-17 19:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 13:05:37 +02:00
|
|
|
inline FlatPtr virtual_to_low_physical(FlatPtr virtual_)
|
2020-01-17 19:59:20 +01:00
|
|
|
{
|
2021-06-28 18:43:18 +02:00
|
|
|
return virtual_ - KERNEL_BASE;
|
2020-01-17 19:59:20 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-19 19:13:03 +01:00
|
|
|
enum class UsedMemoryRangeType {
|
|
|
|
|
LowMemory = 0,
|
|
|
|
|
Kernel,
|
|
|
|
|
BootModule,
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-19 08:35:09 -06:00
|
|
|
static constexpr StringView UserMemoryRangeTypeNames[] {
|
2021-01-19 19:13:03 +01:00
|
|
|
"Low memory",
|
|
|
|
|
"Kernel",
|
|
|
|
|
"Boot module",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct UsedMemoryRange {
|
2021-02-11 22:02:39 +01:00
|
|
|
UsedMemoryRangeType type {};
|
2021-01-19 19:13:03 +01:00
|
|
|
PhysicalAddress start;
|
|
|
|
|
PhysicalAddress end;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-29 14:03:25 +02:00
|
|
|
struct ContiguousReservedMemoryRange {
|
|
|
|
|
PhysicalAddress start;
|
2021-07-06 21:35:15 -06:00
|
|
|
PhysicalSize length {};
|
2021-01-29 14:03:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum class PhysicalMemoryRangeType {
|
|
|
|
|
Usable = 0,
|
|
|
|
|
Reserved,
|
|
|
|
|
ACPI_Reclaimable,
|
|
|
|
|
ACPI_NVS,
|
|
|
|
|
BadMemory,
|
2021-02-11 22:02:39 +01:00
|
|
|
Unknown,
|
2021-01-29 14:03:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct PhysicalMemoryRange {
|
2021-02-11 22:02:39 +01:00
|
|
|
PhysicalMemoryRangeType type { PhysicalMemoryRangeType::Unknown };
|
2021-01-29 14:03:25 +02:00
|
|
|
PhysicalAddress start;
|
2021-07-06 21:35:15 -06:00
|
|
|
PhysicalSize length {};
|
2021-01-29 14:03:25 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
#define MM Kernel::MemoryManager::the()
|
2018-10-27 14:56:52 +02:00
|
|
|
|
2020-06-28 16:04:35 -06:00
|
|
|
struct MemoryManagerData {
|
|
|
|
|
SpinLock<u8> m_quickmap_in_use;
|
2020-07-03 05:19:50 -06:00
|
|
|
u32 m_quickmap_prev_flags;
|
2020-10-31 17:19:50 -06:00
|
|
|
|
|
|
|
|
PhysicalAddress m_last_quickmap_pd;
|
|
|
|
|
PhysicalAddress m_last_quickmap_pt;
|
2020-06-28 16:04:35 -06:00
|
|
|
};
|
|
|
|
|
|
2020-07-06 07:27:22 -06:00
|
|
|
extern RecursiveSpinLock s_mm_lock;
|
|
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
class MemoryManager {
|
2018-10-31 23:19:15 +01:00
|
|
|
AK_MAKE_ETERNAL
|
2018-12-31 14:58:03 +01:00
|
|
|
friend class PageDirectory;
|
2018-11-05 10:23:00 +01:00
|
|
|
friend class PhysicalPage;
|
2019-06-11 21:13:02 +10:00
|
|
|
friend class PhysicalRegion;
|
2020-09-05 15:52:14 -06:00
|
|
|
friend class AnonymousVMObject;
|
2018-11-08 14:35:30 +01:00
|
|
|
friend class Region;
|
2018-11-08 21:20:09 +01:00
|
|
|
friend class VMObject;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
public:
|
2019-07-16 13:44:41 +02:00
|
|
|
static MemoryManager& the();
|
2020-08-29 16:41:30 -06:00
|
|
|
static bool is_initialized();
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2020-06-28 16:04:35 -06:00
|
|
|
static void initialize(u32 cpu);
|
2020-09-18 09:49:51 +02:00
|
|
|
|
2020-06-28 16:04:35 -06:00
|
|
|
static inline MemoryManagerData& get_data()
|
|
|
|
|
{
|
|
|
|
|
return Processor::current().get_mm_data();
|
|
|
|
|
}
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2018-11-05 10:29:19 +01:00
|
|
|
PageFaultResponse handle_page_fault(const PageFault&);
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2021-03-11 13:03:40 +01:00
|
|
|
void set_page_writable_direct(VirtualAddress, bool);
|
|
|
|
|
|
2021-02-14 17:35:07 +01:00
|
|
|
void protect_readonly_after_init_memory();
|
2021-02-19 18:21:54 +01:00
|
|
|
void unmap_memory_after_init();
|
2021-02-14 17:35:07 +01:00
|
|
|
|
2021-02-08 15:45:40 +01:00
|
|
|
static void enter_process_paging_scope(Process&);
|
|
|
|
|
static void enter_space(Space&);
|
2018-11-01 09:01:51 +01:00
|
|
|
|
2019-11-17 12:11:43 +01:00
|
|
|
bool validate_user_stack(const Process&, VirtualAddress) const;
|
2020-02-21 13:05:39 +01:00
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class ShouldZeroFill {
|
2019-05-28 11:53:16 +02:00
|
|
|
No,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
2019-01-31 03:57:06 +01:00
|
|
|
|
2020-09-04 21:12:25 -06:00
|
|
|
bool commit_user_physical_pages(size_t);
|
|
|
|
|
void uncommit_user_physical_pages(size_t);
|
|
|
|
|
NonnullRefPtr<PhysicalPage> allocate_committed_user_physical_page(ShouldZeroFill = ShouldZeroFill::Yes);
|
2020-09-01 13:40:34 -06:00
|
|
|
RefPtr<PhysicalPage> allocate_user_physical_page(ShouldZeroFill = ShouldZeroFill::Yes, bool* did_purge = nullptr);
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<PhysicalPage> allocate_supervisor_physical_page();
|
2020-12-05 22:49:24 -07:00
|
|
|
NonnullRefPtrVector<PhysicalPage> allocate_contiguous_supervisor_physical_pages(size_t size, size_t physical_alignment = PAGE_SIZE);
|
2020-08-21 21:49:50 -06:00
|
|
|
void deallocate_user_physical_page(const PhysicalPage&);
|
|
|
|
|
void deallocate_supervisor_physical_page(const PhysicalPage&);
|
2018-11-05 10:23:00 +01:00
|
|
|
|
2021-05-28 09:33:14 +02:00
|
|
|
OwnPtr<Region> allocate_contiguous_kernel_region(size_t, StringView name, Region::Access access, size_t physical_alignment = PAGE_SIZE, Region::Cacheable = Region::Cacheable::Yes);
|
|
|
|
|
OwnPtr<Region> allocate_kernel_region(size_t, StringView name, Region::Access access, AllocationStrategy strategy = AllocationStrategy::Reserve, Region::Cacheable = Region::Cacheable::Yes);
|
|
|
|
|
OwnPtr<Region> allocate_kernel_region(PhysicalAddress, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes);
|
|
|
|
|
OwnPtr<Region> allocate_kernel_region_identity(PhysicalAddress, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes);
|
|
|
|
|
OwnPtr<Region> allocate_kernel_region_with_vmobject(VMObject&, size_t, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes);
|
|
|
|
|
OwnPtr<Region> allocate_kernel_region_with_vmobject(const Range&, VMObject&, StringView name, Region::Access access, Region::Cacheable = Region::Cacheable::Yes);
|
2019-05-14 11:51:00 +02:00
|
|
|
|
2021-07-06 21:35:15 -06:00
|
|
|
struct SystemMemoryInfo {
|
|
|
|
|
PhysicalSize user_physical_pages { 0 };
|
|
|
|
|
PhysicalSize user_physical_pages_used { 0 };
|
|
|
|
|
PhysicalSize user_physical_pages_committed { 0 };
|
|
|
|
|
PhysicalSize user_physical_pages_uncommitted { 0 };
|
|
|
|
|
PhysicalSize super_physical_pages { 0 };
|
|
|
|
|
PhysicalSize super_physical_pages_used { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SystemMemoryInfo get_system_memory_info()
|
|
|
|
|
{
|
|
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
|
|
|
|
return m_system_memory_info;
|
|
|
|
|
}
|
2019-06-11 21:13:02 +10:00
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
template<IteratorFunction<VMObject&> Callback>
|
2019-08-08 10:43:44 +02:00
|
|
|
static void for_each_vmobject(Callback callback)
|
|
|
|
|
{
|
2019-08-08 13:40:58 +02:00
|
|
|
for (auto& vmobject : MM.m_vmobjects) {
|
|
|
|
|
if (callback(vmobject) == IterationDecision::Break)
|
2019-08-08 10:43:44 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-16 02:36:52 -07:00
|
|
|
template<VoidFunction<VMObject&> Callback>
|
|
|
|
|
static void for_each_vmobject(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
for (auto& vmobject : MM.m_vmobjects)
|
|
|
|
|
callback(vmobject);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-08 15:45:40 +01:00
|
|
|
static Region* find_region_from_vaddr(Space&, VirtualAddress);
|
2021-04-20 21:01:13 +02:00
|
|
|
static Region* find_user_region_from_vaddr(Space&, VirtualAddress);
|
2019-11-29 16:15:30 +01:00
|
|
|
|
2020-01-18 08:34:28 +01:00
|
|
|
void dump_kernel_regions();
|
|
|
|
|
|
2020-02-15 13:12:02 +01:00
|
|
|
PhysicalPage& shared_zero_page() { return *m_shared_zero_page; }
|
2020-09-04 21:12:25 -06:00
|
|
|
PhysicalPage& lazy_committed_page() { return *m_lazy_committed_page; }
|
2020-02-15 13:12:02 +01:00
|
|
|
|
2020-06-01 22:55:09 -06:00
|
|
|
PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
|
|
|
|
|
|
2021-01-19 19:13:03 +01:00
|
|
|
const Vector<UsedMemoryRange>& used_memory_ranges() { return m_used_memory_ranges; }
|
2021-01-29 14:03:25 +02:00
|
|
|
bool is_allowed_to_mmap_to_userspace(PhysicalAddress, const Range&) const;
|
2021-01-19 19:13:03 +01:00
|
|
|
|
2018-10-17 23:13:55 +02:00
|
|
|
private:
|
2020-08-22 17:53:34 +02:00
|
|
|
MemoryManager();
|
2018-10-17 23:13:55 +02:00
|
|
|
~MemoryManager();
|
|
|
|
|
|
2021-01-29 14:03:25 +02:00
|
|
|
void register_reserved_ranges();
|
|
|
|
|
|
2019-12-19 19:13:44 +01:00
|
|
|
void register_vmobject(VMObject&);
|
|
|
|
|
void unregister_vmobject(VMObject&);
|
2018-11-08 22:24:02 +01:00
|
|
|
void register_region(Region&);
|
|
|
|
|
void unregister_region(Region&);
|
2018-11-08 21:20:09 +01:00
|
|
|
|
2020-01-17 22:07:20 +01:00
|
|
|
void protect_kernel_image();
|
2020-01-17 21:03:52 +01:00
|
|
|
void parse_memory_map();
|
2020-07-06 07:27:22 -06:00
|
|
|
static void flush_tlb_local(VirtualAddress, size_t page_count = 1);
|
2021-01-02 12:27:38 -07:00
|
|
|
static void flush_tlb(const PageDirectory*, VirtualAddress, size_t page_count = 1);
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2019-08-06 10:31:20 +02:00
|
|
|
static Region* kernel_region_from_vaddr(VirtualAddress);
|
|
|
|
|
|
2020-07-30 23:52:28 +02:00
|
|
|
static Region* find_region_from_vaddr(VirtualAddress);
|
2019-08-06 11:19:16 +02:00
|
|
|
|
2020-09-04 21:12:25 -06:00
|
|
|
RefPtr<PhysicalPage> find_free_user_physical_page(bool);
|
2019-07-03 21:17:35 +02:00
|
|
|
u8* quickmap_page(PhysicalPage&);
|
2018-11-05 13:48:07 +01:00
|
|
|
void unquickmap_page();
|
|
|
|
|
|
2020-01-17 19:59:20 +01:00
|
|
|
PageDirectoryEntry* quickmap_pd(PageDirectory&, size_t pdpt_index);
|
|
|
|
|
PageTableEntry* quickmap_pt(PhysicalAddress);
|
|
|
|
|
|
2020-10-31 17:19:18 -06:00
|
|
|
PageTableEntry* pte(PageDirectory&, VirtualAddress);
|
2020-09-01 16:10:54 -06:00
|
|
|
PageTableEntry* ensure_pte(PageDirectory&, VirtualAddress);
|
2020-08-27 21:29:17 -06:00
|
|
|
void release_pte(PageDirectory&, VirtualAddress, bool);
|
2018-10-17 23:13:55 +02:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<PageDirectory> m_kernel_page_directory;
|
2018-10-18 13:05:00 +02:00
|
|
|
|
2020-02-15 13:12:02 +01:00
|
|
|
RefPtr<PhysicalPage> m_shared_zero_page;
|
2020-09-04 21:12:25 -06:00
|
|
|
RefPtr<PhysicalPage> m_lazy_committed_page;
|
2020-02-15 13:12:02 +01:00
|
|
|
|
2021-07-06 21:35:15 -06:00
|
|
|
SystemMemoryInfo m_system_memory_info;
|
2019-06-11 21:13:02 +10:00
|
|
|
|
2019-06-27 13:34:28 +02:00
|
|
|
NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
|
|
|
|
|
NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
|
2018-11-08 21:20:09 +01:00
|
|
|
|
2021-05-26 02:47:47 -07:00
|
|
|
Region::List m_user_regions;
|
|
|
|
|
Region::List m_kernel_regions;
|
2021-01-19 19:13:03 +01:00
|
|
|
Vector<UsedMemoryRange> m_used_memory_ranges;
|
2021-01-29 14:03:25 +02:00
|
|
|
Vector<PhysicalMemoryRange> m_physical_memory_ranges;
|
|
|
|
|
Vector<ContiguousReservedMemoryRange> m_reserved_memory_ranges;
|
2019-01-27 10:17:27 +01:00
|
|
|
|
2021-05-26 02:57:46 -07:00
|
|
|
VMObject::List m_vmobjects;
|
2018-10-17 23:13:55 +02:00
|
|
|
};
|
2018-11-01 11:44:21 +01:00
|
|
|
|
2019-12-09 20:06:03 +01:00
|
|
|
template<typename Callback>
|
|
|
|
|
void VMObject::for_each_region(Callback callback)
|
|
|
|
|
{
|
2020-08-25 17:12:18 -06:00
|
|
|
ScopedSpinLock lock(s_mm_lock);
|
2019-12-09 20:06:03 +01:00
|
|
|
// FIXME: Figure out a better data structure so we don't have to walk every single region every time an inode changes.
|
|
|
|
|
// Perhaps VMObject could have a Vector<Region*> with all of his mappers?
|
|
|
|
|
for (auto& region : MM.m_user_regions) {
|
|
|
|
|
if (®ion.vmobject() == this)
|
|
|
|
|
callback(region);
|
|
|
|
|
}
|
|
|
|
|
for (auto& region : MM.m_kernel_regions) {
|
|
|
|
|
if (®ion.vmobject() == this)
|
|
|
|
|
callback(region);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-02 20:49:21 +01:00
|
|
|
|
|
|
|
|
inline bool is_user_address(VirtualAddress vaddr)
|
|
|
|
|
{
|
2021-06-28 18:43:18 +02:00
|
|
|
return vaddr.get() < KERNEL_BASE;
|
2020-01-02 20:49:21 +01:00
|
|
|
}
|
2020-01-19 09:14:14 +01:00
|
|
|
|
|
|
|
|
inline bool is_user_range(VirtualAddress vaddr, size_t size)
|
|
|
|
|
{
|
|
|
|
|
if (vaddr.offset(size) < vaddr)
|
|
|
|
|
return false;
|
|
|
|
|
return is_user_address(vaddr) && is_user_address(vaddr.offset(size));
|
|
|
|
|
}
|
2020-02-15 13:12:02 +01:00
|
|
|
|
2021-02-13 00:47:47 +01:00
|
|
|
inline bool is_user_range(const Range& range)
|
|
|
|
|
{
|
|
|
|
|
return is_user_range(range.base(), range.size());
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 13:12:02 +01:00
|
|
|
inline bool PhysicalPage::is_shared_zero_page() const
|
|
|
|
|
{
|
|
|
|
|
return this == &MM.shared_zero_page();
|
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-09-04 21:12:25 -06:00
|
|
|
inline bool PhysicalPage::is_lazy_committed_page() const
|
|
|
|
|
{
|
|
|
|
|
return this == &MM.lazy_committed_page();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|