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
|
|
|
*/
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-03-06 19:33:25 -08:00
|
|
|
#include <AK/EnumBits.h>
|
2021-05-26 02:47:47 -07:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-02-24 13:24:30 +01:00
|
|
|
#include <AK/Weakable.h>
|
2021-06-21 17:34:09 +02:00
|
|
|
#include <Kernel/Arch/x86/PageFault.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2019-09-16 10:19:44 +02:00
|
|
|
#include <Kernel/Heap/SlabAllocator.h>
|
2021-05-28 09:33:14 +02:00
|
|
|
#include <Kernel/KString.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/PageFaultResponse.h>
|
2021-08-06 13:54:48 +02:00
|
|
|
#include <Kernel/Memory/VirtualRangeAllocator.h>
|
2021-06-28 18:43:18 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2021-07-23 02:40:16 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-06 13:49:36 +02:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2021-03-03 22:45:18 +01:00
|
|
|
enum class ShouldFlushTLB {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-24 13:24:30 +01:00
|
|
|
class Region final
|
2021-07-25 01:46:44 +02:00
|
|
|
: public Weakable<Region> {
|
2019-04-03 15:13:07 +02:00
|
|
|
friend class MemoryManager;
|
2019-06-07 11:43:58 +02:00
|
|
|
|
2019-09-16 10:19:44 +02:00
|
|
|
MAKE_SLAB_ALLOCATED(Region)
|
2019-04-03 15:13:07 +02:00
|
|
|
public:
|
2021-01-29 14:38:49 +01:00
|
|
|
enum Access : u8 {
|
2021-03-06 19:33:25 -08:00
|
|
|
None = 0,
|
2019-05-30 16:14:37 +02:00
|
|
|
Read = 1,
|
|
|
|
|
Write = 2,
|
|
|
|
|
Execute = 4,
|
2021-01-29 14:38:49 +01:00
|
|
|
HasBeenReadable = 16,
|
|
|
|
|
HasBeenWritable = 32,
|
|
|
|
|
HasBeenExecutable = 64,
|
2021-08-06 22:25:00 +02:00
|
|
|
ReadOnly = Read,
|
|
|
|
|
ReadWrite = Read | Write,
|
|
|
|
|
ReadWriteExecute = Read | Write | Execute,
|
2019-05-30 16:14:37 +02:00
|
|
|
};
|
|
|
|
|
|
2021-02-14 01:25:22 +01:00
|
|
|
enum class Cacheable {
|
|
|
|
|
No = 0,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-15 10:11:05 +00:00
|
|
|
static KResultOr<NonnullOwnPtr<Region>> try_create_user_accessible(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable, bool shared);
|
|
|
|
|
static KResultOr<NonnullOwnPtr<Region>> try_create_kernel_only(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable = Cacheable::Yes);
|
2019-07-19 16:09:34 +02:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
~Region();
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] VirtualRange const& range() const { return m_range; }
|
|
|
|
|
[[nodiscard]] VirtualAddress vaddr() const { return m_range.base(); }
|
|
|
|
|
[[nodiscard]] size_t size() const { return m_range.size(); }
|
|
|
|
|
[[nodiscard]] bool is_readable() const { return m_access & Access::Read; }
|
|
|
|
|
[[nodiscard]] bool is_writable() const { return m_access & Access::Write; }
|
|
|
|
|
[[nodiscard]] bool is_executable() const { return m_access & Access::Execute; }
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] bool has_been_readable() const { return m_access & Access::HasBeenReadable; }
|
|
|
|
|
[[nodiscard]] bool has_been_writable() const { return m_access & Access::HasBeenWritable; }
|
|
|
|
|
[[nodiscard]] bool has_been_executable() const { return m_access & Access::HasBeenExecutable; }
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] bool is_cacheable() const { return m_cacheable; }
|
|
|
|
|
[[nodiscard]] StringView name() const { return m_name ? m_name->view() : StringView {}; }
|
|
|
|
|
[[nodiscard]] OwnPtr<KString> take_name() { return move(m_name); }
|
|
|
|
|
[[nodiscard]] Region::Access access() const { return static_cast<Region::Access>(m_access); }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-05-28 09:33:14 +02:00
|
|
|
void set_name(OwnPtr<KString> name) { m_name = move(name); }
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] VMObject const& vmobject() const { return *m_vmobject; }
|
|
|
|
|
[[nodiscard]] VMObject& vmobject() { return *m_vmobject; }
|
2020-09-02 22:57:09 -06:00
|
|
|
void set_vmobject(NonnullRefPtr<VMObject>&&);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool is_shared() const { return m_shared; }
|
2019-04-03 15:13:07 +02:00
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool is_stack() const { return m_stack; }
|
2019-11-17 12:11:43 +01:00
|
|
|
void set_stack(bool stack) { m_stack = stack; }
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool is_mmap() const { return m_mmap; }
|
2019-11-24 12:24:16 +01:00
|
|
|
void set_mmap(bool mmap) { m_mmap = mmap; }
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool is_user() const { return !is_kernel(); }
|
|
|
|
|
[[nodiscard]] bool is_kernel() const { return vaddr().get() < 0x00800000 || vaddr().get() >= kernel_mapping_base; }
|
2020-06-01 22:55:09 -06:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
PageFaultResponse handle_fault(PageFault const&);
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2021-08-15 10:11:05 +00:00
|
|
|
KResultOr<NonnullOwnPtr<Region>> try_clone();
|
2019-05-17 04:32:08 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool contains(VirtualAddress vaddr) const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
return m_range.contains(vaddr);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool contains(VirtualRange const& range) const
|
2019-08-29 20:55:40 +02:00
|
|
|
{
|
|
|
|
|
return m_range.contains(range);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] unsigned page_index_from_address(VirtualAddress vaddr) const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
return (vaddr - m_range.base()).get() / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
2020-09-18 09:49:51 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] VirtualAddress vaddr_from_page_index(size_t page_index) const
|
2020-07-06 12:47:08 -06:00
|
|
|
{
|
|
|
|
|
return vaddr().offset(page_index * PAGE_SIZE);
|
|
|
|
|
}
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool translate_vmobject_page(size_t& index) const
|
2021-01-02 12:03:14 -07:00
|
|
|
{
|
|
|
|
|
auto first_index = first_page_index();
|
|
|
|
|
if (index < first_index) {
|
|
|
|
|
index = first_index;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
index -= first_index;
|
|
|
|
|
auto total_page_count = this->page_count();
|
|
|
|
|
if (index >= total_page_count) {
|
|
|
|
|
index = first_index + total_page_count - 1;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t translate_to_vmobject_page(size_t page_index) const
|
2021-01-02 12:03:14 -07:00
|
|
|
{
|
|
|
|
|
return first_page_index() + page_index;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t first_page_index() const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-12-19 19:13:44 +01:00
|
|
|
return m_offset_in_vmobject / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t page_count() const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-05-17 04:32:08 +02:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
PhysicalPage const* physical_page(size_t index) const;
|
|
|
|
|
RefPtr<PhysicalPage>& physical_page_slot(size_t index);
|
2020-04-28 16:19:50 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t offset_in_vmobject() const
|
2019-10-01 11:38:59 +02:00
|
|
|
{
|
2019-12-19 19:13:44 +01:00
|
|
|
return m_offset_in_vmobject;
|
2019-10-01 11:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t offset_in_vmobject_from_vaddr(VirtualAddress vaddr) const
|
2020-12-21 23:21:58 -07:00
|
|
|
{
|
|
|
|
|
return m_offset_in_vmobject + vaddr.get() - this->vaddr().get();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t amount_resident() const;
|
|
|
|
|
[[nodiscard]] size_t amount_shared() const;
|
|
|
|
|
[[nodiscard]] size_t amount_dirty() const;
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool should_cow(size_t page_index) const;
|
2019-10-01 19:58:41 +02:00
|
|
|
void set_should_cow(size_t page_index, bool);
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] size_t cow_pages() const;
|
2019-12-15 16:53:00 +01:00
|
|
|
|
2019-12-25 02:39:03 +01:00
|
|
|
void set_readable(bool b) { set_access_bit(Access::Read, b); }
|
|
|
|
|
void set_writable(bool b) { set_access_bit(Access::Write, b); }
|
|
|
|
|
void set_executable(bool b) { set_access_bit(Access::Execute, b); }
|
2019-12-02 19:14:16 +01:00
|
|
|
|
2020-01-09 23:29:31 +02:00
|
|
|
void set_page_directory(PageDirectory&);
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes);
|
2021-08-06 21:45:05 +02:00
|
|
|
enum class ShouldDeallocateVirtualRange {
|
2019-11-03 20:37:03 +01:00
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
2021-08-06 21:45:05 +02:00
|
|
|
void unmap(ShouldDeallocateVirtualRange = ShouldDeallocateVirtualRange::Yes);
|
2019-11-03 20:37:03 +01:00
|
|
|
|
2019-11-03 20:59:54 +01:00
|
|
|
void remap();
|
2019-11-03 15:32:11 +01:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool is_syscall_region() const { return m_syscall_region; }
|
2021-02-02 19:56:11 +01:00
|
|
|
void set_syscall_region(bool b) { m_syscall_region = b; }
|
|
|
|
|
|
2019-09-27 14:19:07 +02:00
|
|
|
private:
|
2021-08-06 13:54:48 +02:00
|
|
|
Region(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString>, Region::Access access, Cacheable, bool shared);
|
2021-02-14 01:38:47 +01:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool remap_vmobject_page(size_t page_index, bool with_flush = true);
|
|
|
|
|
[[nodiscard]] bool do_remap_vmobject_page(size_t page_index, bool with_flush = true);
|
2021-01-02 12:03:14 -07:00
|
|
|
|
2019-12-25 02:39:03 +01:00
|
|
|
void set_access_bit(Access access, bool b)
|
|
|
|
|
{
|
|
|
|
|
if (b)
|
2021-01-29 14:38:49 +01:00
|
|
|
m_access |= access | (access << 4);
|
2019-12-25 02:39:03 +01:00
|
|
|
else
|
|
|
|
|
m_access &= ~access;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] PageFaultResponse handle_cow_fault(size_t page_index);
|
|
|
|
|
[[nodiscard]] PageFaultResponse handle_inode_fault(size_t page_index);
|
|
|
|
|
[[nodiscard]] PageFaultResponse handle_zero_fault(size_t page_index);
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2021-08-24 12:55:08 -07:00
|
|
|
[[nodiscard]] bool map_individual_page_impl(size_t page_index);
|
2020-01-01 19:30:38 +01:00
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<PageDirectory> m_page_directory;
|
2021-08-06 13:54:48 +02:00
|
|
|
VirtualRange m_range;
|
2019-12-19 19:13:44 +01:00
|
|
|
size_t m_offset_in_vmobject { 0 };
|
2019-09-04 11:27:14 +02:00
|
|
|
NonnullRefPtr<VMObject> m_vmobject;
|
2021-05-28 09:33:14 +02:00
|
|
|
OwnPtr<KString> m_name;
|
2021-03-06 19:33:25 -08:00
|
|
|
u8 m_access { Region::None };
|
2020-02-19 12:01:39 +01:00
|
|
|
bool m_shared : 1 { false };
|
|
|
|
|
bool m_cacheable : 1 { false };
|
|
|
|
|
bool m_stack : 1 { false };
|
|
|
|
|
bool m_mmap : 1 { false };
|
2021-02-02 19:56:11 +01:00
|
|
|
bool m_syscall_region : 1 { false };
|
2021-07-23 02:40:16 +02:00
|
|
|
IntrusiveListNode<Region> m_memory_manager_list_node;
|
|
|
|
|
IntrusiveListNode<Region> m_vmobject_list_node;
|
2021-05-26 02:47:47 -07:00
|
|
|
|
|
|
|
|
public:
|
2021-07-23 02:40:16 +02:00
|
|
|
using ListInMemoryManager = IntrusiveList<Region, RawPtr<Region>, &Region::m_memory_manager_list_node>;
|
|
|
|
|
using ListInVMObject = IntrusiveList<Region, RawPtr<Region>, &Region::m_vmobject_list_node>;
|
2019-04-03 15:13:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2021-03-06 19:33:25 -08:00
|
|
|
AK_ENUM_BITWISE_OPERATORS(Region::Access)
|
|
|
|
|
|
|
|
|
|
inline Region::Access prot_to_region_access_flags(int prot)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-03-06 19:33:25 -08:00
|
|
|
Region::Access access = Region::Access::None;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (prot & PROT_READ)
|
|
|
|
|
access |= Region::Access::Read;
|
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
|
access |= Region::Access::Write;
|
|
|
|
|
if (prot & PROT_EXEC)
|
|
|
|
|
access |= Region::Access::Execute;
|
|
|
|
|
return access;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 19:33:25 -08:00
|
|
|
inline int region_access_flags_to_prot(Region::Access access)
|
2020-12-29 02:11:47 +01:00
|
|
|
{
|
|
|
|
|
int prot = 0;
|
|
|
|
|
if (access & Region::Access::Read)
|
|
|
|
|
prot |= PROT_READ;
|
|
|
|
|
if (access & Region::Access::Write)
|
|
|
|
|
prot |= PROT_WRITE;
|
|
|
|
|
if (access & Region::Access::Execute)
|
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
|
return prot;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|