2018-10-17 21:13:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Badge.h>
|
2018-11-05 12:48:07 +00:00
|
|
|
#include <AK/Bitmap.h>
|
2018-11-01 12:21:02 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-27 11:34:28 +00:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-06-21 16:58:45 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-05 09:35:49 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2019-11-29 15:15:30 +00:00
|
|
|
#include <AK/String.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Types.h>
|
2018-10-18 11:05:00 +00:00
|
|
|
#include <AK/Vector.h>
|
2019-02-08 15:40:48 +00:00
|
|
|
#include <AK/Weakable.h>
|
2019-06-07 18:02:01 +00:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <Kernel/VM/PhysicalPage.h>
|
2019-06-11 11:13:02 +00:00
|
|
|
#include <Kernel/VM/PhysicalRegion.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <Kernel/VM/Region.h>
|
|
|
|
#include <Kernel/VM/VMObject.h>
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
#define PAGE_ROUND_UP(x) ((((u32)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
|
2019-01-12 23:27:25 +00:00
|
|
|
|
2019-08-05 09:35:49 +00:00
|
|
|
class KBuffer;
|
2019-01-18 14:01:40 +00:00
|
|
|
class SynthFSInode;
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2018-10-27 12:56:52 +00:00
|
|
|
#define MM MemoryManager::the()
|
|
|
|
|
2018-10-17 21:13:55 +00:00
|
|
|
class MemoryManager {
|
2018-10-31 22:19:15 +00:00
|
|
|
AK_MAKE_ETERNAL
|
2018-12-31 13:58:03 +00:00
|
|
|
friend class PageDirectory;
|
2018-11-05 09:23:00 +00:00
|
|
|
friend class PhysicalPage;
|
2019-06-11 11:13:02 +00:00
|
|
|
friend class PhysicalRegion;
|
2018-11-08 13:35:30 +00:00
|
|
|
friend class Region;
|
2018-11-08 20:20:09 +00:00
|
|
|
friend class VMObject;
|
2019-08-05 09:35:49 +00:00
|
|
|
friend Optional<KBuffer> procfs$mm(InodeIdentifier);
|
|
|
|
friend Optional<KBuffer> procfs$memstat(InodeIdentifier);
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2018-10-17 21:13:55 +00:00
|
|
|
public:
|
2019-07-16 11:44:41 +00:00
|
|
|
static MemoryManager& the();
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2019-11-08 14:37:33 +00:00
|
|
|
static void initialize(u32 physical_address_for_kernel_page_tables);
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2018-11-05 09:29:19 +00:00
|
|
|
PageFaultResponse handle_page_fault(const PageFault&);
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2018-11-03 00:49:40 +00:00
|
|
|
void populate_page_directory(PageDirectory&);
|
2018-11-01 10:30:48 +00:00
|
|
|
|
2018-11-01 12:15:46 +00:00
|
|
|
void enter_process_paging_scope(Process&);
|
2018-11-01 08:01:51 +00:00
|
|
|
|
2019-11-17 11:11:43 +00:00
|
|
|
bool validate_user_stack(const Process&, VirtualAddress) const;
|
2019-06-07 10:56:50 +00:00
|
|
|
bool validate_user_read(const Process&, VirtualAddress) const;
|
|
|
|
bool validate_user_write(const Process&, VirtualAddress) const;
|
2018-11-01 11:45:51 +00:00
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class ShouldZeroFill {
|
2019-05-28 09:53:16 +00:00
|
|
|
No,
|
|
|
|
Yes
|
|
|
|
};
|
2019-01-31 02:57:06 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<PhysicalPage> allocate_user_physical_page(ShouldZeroFill);
|
|
|
|
RefPtr<PhysicalPage> allocate_supervisor_physical_page();
|
2019-06-14 11:56:21 +00:00
|
|
|
void deallocate_user_physical_page(PhysicalPage&&);
|
|
|
|
void deallocate_supervisor_physical_page(PhysicalPage&&);
|
2018-11-05 09:23:00 +00:00
|
|
|
|
2019-10-16 16:27:00 +00:00
|
|
|
void map_for_kernel(VirtualAddress, PhysicalAddress, bool cache_disabled = false);
|
2019-03-10 14:25:33 +00:00
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
OwnPtr<Region> allocate_kernel_region(size_t, const StringView& name, bool user_accessible = false, bool should_commit = true);
|
2019-12-15 21:21:28 +00:00
|
|
|
OwnPtr<Region> allocate_kernel_region_with_vmobject(VMObject&, size_t, const StringView& name);
|
2019-09-27 12:19:07 +00:00
|
|
|
OwnPtr<Region> allocate_user_accessible_kernel_region(size_t, const StringView& name);
|
2019-05-14 09:51:00 +00:00
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
unsigned user_physical_pages() const { return m_user_physical_pages; }
|
|
|
|
unsigned user_physical_pages_used() const { return m_user_physical_pages_used; }
|
|
|
|
unsigned super_physical_pages() const { return m_super_physical_pages; }
|
|
|
|
unsigned super_physical_pages_used() const { return m_super_physical_pages_used; }
|
|
|
|
|
2019-08-08 08:43:44 +00:00
|
|
|
template<typename Callback>
|
|
|
|
static void for_each_vmobject(Callback callback)
|
|
|
|
{
|
2019-08-08 11:40:58 +00:00
|
|
|
for (auto& vmobject : MM.m_vmobjects) {
|
|
|
|
if (callback(vmobject) == IterationDecision::Break)
|
2019-08-08 08:43:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 15:15:30 +00:00
|
|
|
static Region* region_from_vaddr(Process&, VirtualAddress);
|
|
|
|
static const Region* region_from_vaddr(const Process&, VirtualAddress);
|
|
|
|
|
2018-10-17 21:13:55 +00:00
|
|
|
private:
|
2019-11-08 14:37:33 +00:00
|
|
|
MemoryManager(u32 physical_address_for_kernel_page_tables);
|
2018-10-17 21:13:55 +00:00
|
|
|
~MemoryManager();
|
|
|
|
|
2018-11-08 20:20:09 +00:00
|
|
|
void register_vmo(VMObject&);
|
|
|
|
void unregister_vmo(VMObject&);
|
2018-11-08 21:24:02 +00:00
|
|
|
void register_region(Region&);
|
|
|
|
void unregister_region(Region&);
|
2018-11-08 20:20:09 +00:00
|
|
|
|
2018-12-03 00:38:22 +00:00
|
|
|
void initialize_paging();
|
|
|
|
void flush_entire_tlb();
|
2019-06-07 10:56:50 +00:00
|
|
|
void flush_tlb(VirtualAddress);
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void map_protected(VirtualAddress, size_t length);
|
2018-11-02 23:31:42 +00:00
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
void create_identity_mapping(PageDirectory&, VirtualAddress, size_t length);
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2019-08-06 08:31:20 +00:00
|
|
|
static Region* user_region_from_vaddr(Process&, VirtualAddress);
|
|
|
|
static Region* kernel_region_from_vaddr(VirtualAddress);
|
|
|
|
|
2019-08-06 09:19:16 +00:00
|
|
|
static Region* region_from_vaddr(VirtualAddress);
|
|
|
|
|
2019-11-08 21:39:29 +00:00
|
|
|
RefPtr<PhysicalPage> find_free_user_physical_page();
|
2019-07-03 19:17:35 +00:00
|
|
|
u8* quickmap_page(PhysicalPage&);
|
2018-11-05 12:48:07 +00:00
|
|
|
void unquickmap_page();
|
|
|
|
|
2018-12-31 13:58:03 +00:00
|
|
|
PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
|
|
|
|
|
2019-06-26 19:45:56 +00:00
|
|
|
PageTableEntry& ensure_pte(PageDirectory&, VirtualAddress);
|
2018-10-17 21:13:55 +00:00
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<PageDirectory> m_kernel_page_directory;
|
2019-06-26 19:45:56 +00:00
|
|
|
PageTableEntry* m_page_table_zero { nullptr };
|
2019-11-23 16:27:09 +00:00
|
|
|
PageTableEntry* m_page_table_one { nullptr };
|
2018-10-18 11:05:00 +00:00
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
VirtualAddress m_quickmap_addr;
|
2018-11-01 10:30:48 +00:00
|
|
|
|
2019-06-11 11:13:02 +00:00
|
|
|
unsigned m_user_physical_pages { 0 };
|
|
|
|
unsigned m_user_physical_pages_used { 0 };
|
|
|
|
unsigned m_super_physical_pages { 0 };
|
|
|
|
unsigned m_super_physical_pages_used { 0 };
|
|
|
|
|
2019-06-27 11:34:28 +00:00
|
|
|
NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
|
|
|
|
NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
|
2018-11-08 20:20:09 +00:00
|
|
|
|
2019-08-08 08:53:24 +00:00
|
|
|
InlineLinkedList<Region> m_user_regions;
|
|
|
|
InlineLinkedList<Region> m_kernel_regions;
|
2019-01-27 09:17:27 +00:00
|
|
|
|
2019-08-08 08:43:44 +00:00
|
|
|
InlineLinkedList<VMObject> m_vmobjects;
|
|
|
|
|
2019-01-31 02:57:06 +00:00
|
|
|
bool m_quickmap_in_use { false };
|
2018-10-17 21:13:55 +00:00
|
|
|
};
|
2018-11-01 10:44:21 +00:00
|
|
|
|
2018-11-01 22:04:34 +00:00
|
|
|
struct ProcessPagingScope {
|
2019-03-23 21:03:17 +00:00
|
|
|
ProcessPagingScope(Process&);
|
|
|
|
~ProcessPagingScope();
|
2018-11-01 10:44:21 +00:00
|
|
|
};
|
2019-12-09 19:06:03 +00:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void VMObject::for_each_region(Callback callback)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|