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