2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2022-01-16 12:10:05 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2022-01-29 17:40:19 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
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>
|
2022-01-16 12:10:05 +00:00
|
|
|
#include <AK/IntrusiveRedBlackTree.h>
|
2021-07-11 09:49:16 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2021-05-28 07:33:14 +00:00
|
|
|
#include <Kernel/KString.h>
|
2022-08-19 18:53:40 +00:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/PageFaultResponse.h>
|
2022-04-03 11:28:16 +00:00
|
|
|
#include <Kernel/Memory/VirtualRange.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-12-01 17:14:19 +00:00
|
|
|
namespace Kernel {
|
|
|
|
class PageFault;
|
|
|
|
}
|
|
|
|
|
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
|
2022-08-19 18:53:40 +00:00
|
|
|
: public LockWeakable<Region> {
|
2022-04-01 23:28:01 +00:00
|
|
|
friend class AddressSpace;
|
2019-04-03 13:13:07 +00:00
|
|
|
friend class MemoryManager;
|
2022-04-02 19:12:05 +00:00
|
|
|
friend class RegionTree;
|
2019-06-07 09:43:58 +00:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<Region>> try_create_user_accessible(VirtualRange const&, NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable, bool shared);
|
2022-04-03 13:27:47 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<Region>> create_unbacked();
|
2022-08-19 18:53:40 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<Region>> create_unplaced(NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable = Cacheable::Yes, bool shared = false);
|
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(); }
|
2021-11-06 21:06:08 +00:00
|
|
|
[[nodiscard]] bool is_readable() const { return (m_access & Access::Read) == Access::Read; }
|
|
|
|
[[nodiscard]] bool is_writable() const { return (m_access & Access::Write) == Access::Write; }
|
|
|
|
[[nodiscard]] bool is_executable() const { return (m_access & Access::Execute) == Access::Execute; }
|
2021-08-24 19:55:08 +00:00
|
|
|
|
2021-11-06 21:06:08 +00:00
|
|
|
[[nodiscard]] bool has_been_readable() const { return (m_access & Access::HasBeenReadable) == Access::HasBeenReadable; }
|
|
|
|
[[nodiscard]] bool has_been_writable() const { return (m_access & Access::HasBeenWritable) == Access::HasBeenWritable; }
|
|
|
|
[[nodiscard]] bool has_been_executable() const { return (m_access & Access::HasBeenExecutable) == Access::HasBeenExecutable; }
|
2021-08-24 19:55:08 +00:00
|
|
|
|
|
|
|
[[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; }
|
2022-08-19 18:53:40 +00:00
|
|
|
void set_vmobject(NonnullLockRefPtr<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; }
|
|
|
|
|
2022-01-26 00:35:34 +00:00
|
|
|
[[nodiscard]] bool is_write_combine() const { return m_write_combine; }
|
|
|
|
ErrorOr<void> set_write_combine(bool);
|
|
|
|
|
2021-08-24 19:55:08 +00:00
|
|
|
[[nodiscard]] bool is_user() const { return !is_kernel(); }
|
2021-12-19 17:36:42 +00:00
|
|
|
[[nodiscard]] bool is_kernel() const { return vaddr().get() < USER_RANGE_BASE || 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-11-07 23:51:39 +00:00
|
|
|
ErrorOr<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
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<PhysicalPage> physical_page(size_t index) const;
|
|
|
|
LockRefPtr<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;
|
2022-02-10 17:17:27 +00:00
|
|
|
ErrorOr<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
|
|
|
|
2021-10-28 20:33:41 +00:00
|
|
|
void unsafe_clear_access() { m_access = Region::None; }
|
|
|
|
|
2020-01-09 21:29:31 +00:00
|
|
|
void set_page_directory(PageDirectory&);
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes);
|
2022-04-04 21:36:09 +00:00
|
|
|
void unmap(ShouldFlushTLB = ShouldFlushTLB::Yes);
|
2022-08-23 10:38:17 +00:00
|
|
|
void unmap_with_locks_held(ShouldFlushTLB, SpinlockLocker<RecursiveSpinlock>& pd_locker);
|
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
|
|
|
|
2022-02-10 17:59:26 +00:00
|
|
|
[[nodiscard]] bool is_mapped() const { return m_page_directory != nullptr; }
|
|
|
|
|
2021-11-29 19:18:59 +00:00
|
|
|
void clear_to_zero();
|
|
|
|
|
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:
|
2022-04-03 13:27:47 +00:00
|
|
|
Region();
|
2022-08-19 18:53:40 +00:00
|
|
|
Region(NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString>, Region::Access access, Cacheable, bool shared);
|
|
|
|
Region(VirtualRange const&, NonnullLockRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString>, Region::Access access, Cacheable, bool shared);
|
2021-02-14 00:38:47 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
[[nodiscard]] bool remap_vmobject_page(size_t page_index, NonnullLockRefPtr<PhysicalPage>);
|
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);
|
2022-08-19 10:51:52 +00:00
|
|
|
[[nodiscard]] PageFaultResponse handle_zero_fault(size_t page_index, PhysicalPage& page_in_slot_at_time_of_fault);
|
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);
|
2022-08-19 18:53:40 +00:00
|
|
|
[[nodiscard]] bool map_individual_page_impl(size_t page_index, LockRefPtr<PhysicalPage>);
|
2020-01-01 18:30:38 +00:00
|
|
|
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<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 };
|
2022-08-19 18:53:40 +00:00
|
|
|
LockRefPtr<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 };
|
2022-01-26 00:35:34 +00:00
|
|
|
bool m_write_combine : 1 { false };
|
2022-01-16 12:10:05 +00:00
|
|
|
|
|
|
|
IntrusiveRedBlackTreeNode<FlatPtr, Region, RawPtr<Region>> m_tree_node;
|
2021-07-23 00:40:16 +00:00
|
|
|
IntrusiveListNode<Region> m_vmobject_list_node;
|
2021-05-26 09:47:47 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-09 12:00:59 +00:00
|
|
|
using ListInVMObject = IntrusiveList<&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)
|
|
|
|
|
2022-01-29 17:40:19 +00:00
|
|
|
constexpr 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;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((prot & PROT_READ) == PROT_READ)
|
2020-07-30 21:38:15 +00:00
|
|
|
access |= Region::Access::Read;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((prot & PROT_WRITE) == PROT_WRITE)
|
2020-07-30 21:38:15 +00:00
|
|
|
access |= Region::Access::Write;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((prot & PROT_EXEC) == PROT_EXEC)
|
2020-07-30 21:38:15 +00:00
|
|
|
access |= Region::Access::Execute;
|
|
|
|
return access;
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:40:19 +00:00
|
|
|
constexpr int region_access_flags_to_prot(Region::Access access)
|
2020-12-29 01:11:47 +00:00
|
|
|
{
|
|
|
|
int prot = 0;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((access & Region::Access::Read) == Region::Access::Read)
|
2020-12-29 01:11:47 +00:00
|
|
|
prot |= PROT_READ;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((access & Region::Access::Write) == Region::Access::Write)
|
2020-12-29 01:11:47 +00:00
|
|
|
prot |= PROT_WRITE;
|
2021-11-06 21:06:08 +00:00
|
|
|
if ((access & Region::Access::Execute) == Region::Access::Execute)
|
2020-12-29 01:11:47 +00:00
|
|
|
prot |= PROT_EXEC;
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|