2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2022-01-16 15:10:05 +03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2022-01-29 20:40:19 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2021-03-07 06:33:25 +03:00
|
|
|
#include <AK/EnumBits.h>
|
2021-05-26 12:47:47 +03:00
|
|
|
#include <AK/IntrusiveList.h>
|
2022-01-16 15:10:05 +03:00
|
|
|
#include <AK/IntrusiveRedBlackTree.h>
|
2021-07-11 12:49:16 +03:00
|
|
|
#include <Kernel/Forward.h>
|
2023-02-24 21:10:59 +03:00
|
|
|
#include <Kernel/Library/KString.h>
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
2022-11-09 13:39:58 +03:00
|
|
|
#include <Kernel/Locking/LockRank.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/PageFaultResponse.h>
|
2022-04-03 14:28:16 +03:00
|
|
|
#include <Kernel/Memory/VirtualRange.h>
|
2021-06-28 19:43:18 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2021-07-23 03:40:16 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-12-01 20:14:19 +03:00
|
|
|
namespace Kernel {
|
|
|
|
class PageFault;
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-03-04 00:45:18 +03:00
|
|
|
enum class ShouldFlushTLB {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2020-02-24 15:24:30 +03:00
|
|
|
class Region final
|
2022-08-19 21:53:40 +03:00
|
|
|
: public LockWeakable<Region> {
|
2022-04-02 02:28:01 +03:00
|
|
|
friend class AddressSpace;
|
2019-04-03 16:13:07 +03:00
|
|
|
friend class MemoryManager;
|
2022-04-02 22:12:05 +03:00
|
|
|
friend class RegionTree;
|
2019-06-07 12:43:58 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
public:
|
2021-01-29 16:38:49 +03:00
|
|
|
enum Access : u8 {
|
2021-03-07 06:33:25 +03:00
|
|
|
None = 0,
|
2019-05-30 17:14:37 +03:00
|
|
|
Read = 1,
|
|
|
|
Write = 2,
|
|
|
|
Execute = 4,
|
2021-01-29 16:38:49 +03:00
|
|
|
HasBeenReadable = 16,
|
|
|
|
HasBeenWritable = 32,
|
|
|
|
HasBeenExecutable = 64,
|
2021-08-06 23:25:00 +03:00
|
|
|
ReadOnly = Read,
|
|
|
|
ReadWrite = Read | Write,
|
|
|
|
ReadWriteExecute = Read | Write | Execute,
|
2019-05-30 17:14:37 +03:00
|
|
|
};
|
|
|
|
|
2021-02-14 03:25:22 +03:00
|
|
|
enum class Cacheable {
|
|
|
|
No = 0,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2022-08-19 21:53:40 +03: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 16:27:47 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<Region>> create_unbacked();
|
2022-08-19 21:53:40 +03: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 17:09:34 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
~Region();
|
|
|
|
|
2021-08-24 22:55:08 +03: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-07 00:06:08 +03: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 22:55:08 +03:00
|
|
|
|
2021-11-07 00:06:08 +03: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 22:55:08 +03: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 16:13:07 +03:00
|
|
|
|
2021-05-28 10:33:14 +03:00
|
|
|
void set_name(OwnPtr<KString> name) { m_name = move(name); }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] VMObject const& vmobject() const { return *m_vmobject; }
|
|
|
|
[[nodiscard]] VMObject& vmobject() { return *m_vmobject; }
|
2022-08-19 21:53:40 +03:00
|
|
|
void set_vmobject(NonnullLockRefPtr<VMObject>&&);
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool is_shared() const { return m_shared; }
|
2019-04-03 16:13:07 +03:00
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool is_stack() const { return m_stack; }
|
2019-11-17 14:11:43 +03:00
|
|
|
void set_stack(bool stack) { m_stack = stack; }
|
|
|
|
|
2022-12-15 22:08:57 +03:00
|
|
|
[[nodiscard]] bool is_immutable() const { return m_immutable; }
|
|
|
|
void set_immutable() { m_immutable = true; }
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool is_mmap() const { return m_mmap; }
|
2022-08-23 21:18:39 +03:00
|
|
|
|
|
|
|
void set_mmap(bool mmap, bool description_was_readable, bool description_was_writable)
|
|
|
|
{
|
|
|
|
m_mmap = mmap;
|
|
|
|
m_mmapped_from_readable = description_was_readable;
|
|
|
|
m_mmapped_from_writable = description_was_writable;
|
|
|
|
}
|
2019-11-24 14:24:16 +03:00
|
|
|
|
2022-01-26 03:35:34 +03:00
|
|
|
[[nodiscard]] bool is_write_combine() const { return m_write_combine; }
|
|
|
|
ErrorOr<void> set_write_combine(bool);
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool is_user() const { return !is_kernel(); }
|
2021-12-19 20:36:42 +03:00
|
|
|
[[nodiscard]] bool is_kernel() const { return vaddr().get() < USER_RANGE_BASE || vaddr().get() >= kernel_mapping_base; }
|
2020-06-02 07:55:09 +03:00
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
PageFaultResponse handle_fault(PageFault const&);
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> try_clone();
|
2019-05-17 05:32:08 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool contains(VirtualAddress vaddr) const
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2019-06-07 13:56:50 +03:00
|
|
|
return m_range.contains(vaddr);
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool contains(VirtualRange const& range) const
|
2019-08-29 21:55:40 +03:00
|
|
|
{
|
|
|
|
return m_range.contains(range);
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] unsigned page_index_from_address(VirtualAddress vaddr) const
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2019-06-07 13:56:50 +03:00
|
|
|
return (vaddr - m_range.base()).get() / PAGE_SIZE;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
2020-09-18 10:49:51 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] VirtualAddress vaddr_from_page_index(size_t page_index) const
|
2020-07-06 21:47:08 +03:00
|
|
|
{
|
|
|
|
return vaddr().offset(page_index * PAGE_SIZE);
|
|
|
|
}
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool translate_vmobject_page(size_t& index) const
|
2021-01-02 22:03:14 +03: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 22:55:08 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t translate_to_vmobject_page(size_t page_index) const
|
2021-01-02 22:03:14 +03:00
|
|
|
{
|
|
|
|
return first_page_index() + page_index;
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t first_page_index() const
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2019-12-19 21:13:44 +03:00
|
|
|
return m_offset_in_vmobject / PAGE_SIZE;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t page_count() const
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2019-05-17 05:32:08 +03:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:56:26 +03:00
|
|
|
RefPtr<PhysicalPage> physical_page(size_t index) const;
|
|
|
|
RefPtr<PhysicalPage>& physical_page_slot(size_t index);
|
2020-04-28 17:19:50 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t offset_in_vmobject() const
|
2019-10-01 12:38:59 +03:00
|
|
|
{
|
2019-12-19 21:13:44 +03:00
|
|
|
return m_offset_in_vmobject;
|
2019-10-01 12:38:59 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t offset_in_vmobject_from_vaddr(VirtualAddress vaddr) const
|
2020-12-22 09:21:58 +03:00
|
|
|
{
|
|
|
|
return m_offset_in_vmobject + vaddr.get() - this->vaddr().get();
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t amount_resident() const;
|
|
|
|
[[nodiscard]] size_t amount_shared() const;
|
|
|
|
[[nodiscard]] size_t amount_dirty() const;
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool should_cow(size_t page_index) const;
|
2022-02-10 20:17:27 +03:00
|
|
|
ErrorOr<void> set_should_cow(size_t page_index, bool);
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] size_t cow_pages() const;
|
2019-12-15 18:53:00 +03:00
|
|
|
|
2019-12-25 04:39:03 +03: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 21:14:16 +03:00
|
|
|
|
2021-10-28 23:33:41 +03:00
|
|
|
void unsafe_clear_access() { m_access = Region::None; }
|
|
|
|
|
2020-01-10 00:29:31 +03:00
|
|
|
void set_page_directory(PageDirectory&);
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes);
|
2022-04-05 00:36:09 +03:00
|
|
|
void unmap(ShouldFlushTLB = ShouldFlushTLB::Yes);
|
2022-11-09 13:39:58 +03:00
|
|
|
void unmap_with_locks_held(ShouldFlushTLB, SpinlockLocker<RecursiveSpinlock<LockRank::None>>& pd_locker);
|
2019-11-03 22:37:03 +03:00
|
|
|
|
2019-11-03 22:59:54 +03:00
|
|
|
void remap();
|
2019-11-03 17:32:11 +03:00
|
|
|
|
2022-02-10 20:59:26 +03:00
|
|
|
[[nodiscard]] bool is_mapped() const { return m_page_directory != nullptr; }
|
|
|
|
|
2021-11-29 22:18:59 +03:00
|
|
|
void clear_to_zero();
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool is_syscall_region() const { return m_syscall_region; }
|
2021-02-02 21:56:11 +03:00
|
|
|
void set_syscall_region(bool b) { m_syscall_region = b; }
|
|
|
|
|
2022-08-23 21:18:39 +03:00
|
|
|
[[nodiscard]] bool mmapped_from_readable() const { return m_mmapped_from_readable; }
|
|
|
|
[[nodiscard]] bool mmapped_from_writable() const { return m_mmapped_from_writable; }
|
|
|
|
|
2023-07-08 05:48:11 +03:00
|
|
|
void start_handling_page_fault(Badge<MemoryManager>) { m_in_progress_page_faults++; }
|
|
|
|
void finish_handling_page_fault(Badge<MemoryManager>) { m_in_progress_page_faults--; }
|
2023-04-06 01:11:12 +03:00
|
|
|
|
2019-09-27 15:19:07 +03:00
|
|
|
private:
|
2022-04-03 16:27:47 +03:00
|
|
|
Region();
|
2022-08-19 21:53:40 +03: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 03:38:47 +03:00
|
|
|
|
2022-08-24 16:56:26 +03:00
|
|
|
[[nodiscard]] bool remap_vmobject_page(size_t page_index, NonnullRefPtr<PhysicalPage>);
|
2021-01-02 22:03:14 +03:00
|
|
|
|
2019-12-25 04:39:03 +03:00
|
|
|
void set_access_bit(Access access, bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
2021-01-29 16:38:49 +03:00
|
|
|
m_access |= access | (access << 4);
|
2019-12-25 04:39:03 +03:00
|
|
|
else
|
|
|
|
m_access &= ~access;
|
|
|
|
}
|
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] PageFaultResponse handle_cow_fault(size_t page_index);
|
2022-09-24 14:31:32 +03:00
|
|
|
[[nodiscard]] PageFaultResponse handle_inode_fault(size_t page_index);
|
2022-08-19 13:51:52 +03:00
|
|
|
[[nodiscard]] PageFaultResponse handle_zero_fault(size_t page_index, PhysicalPage& page_in_slot_at_time_of_fault);
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2021-08-24 22:55:08 +03:00
|
|
|
[[nodiscard]] bool map_individual_page_impl(size_t page_index);
|
2022-08-24 16:56:26 +03:00
|
|
|
[[nodiscard]] bool map_individual_page_impl(size_t page_index, RefPtr<PhysicalPage>);
|
2020-01-01 21:30:38 +03:00
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<PageDirectory> m_page_directory;
|
2021-08-06 14:54:48 +03:00
|
|
|
VirtualRange m_range;
|
2019-12-19 21:13:44 +03:00
|
|
|
size_t m_offset_in_vmobject { 0 };
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<VMObject> m_vmobject;
|
2021-05-28 10:33:14 +03:00
|
|
|
OwnPtr<KString> m_name;
|
2023-04-06 01:11:12 +03:00
|
|
|
Atomic<u32> m_in_progress_page_faults;
|
2021-03-07 06:33:25 +03:00
|
|
|
u8 m_access { Region::None };
|
2020-02-19 14:01:39 +03:00
|
|
|
bool m_shared : 1 { false };
|
|
|
|
bool m_cacheable : 1 { false };
|
|
|
|
bool m_stack : 1 { false };
|
|
|
|
bool m_mmap : 1 { false };
|
2022-12-15 22:08:57 +03:00
|
|
|
bool m_immutable : 1 { false };
|
2021-02-02 21:56:11 +03:00
|
|
|
bool m_syscall_region : 1 { false };
|
2022-01-26 03:35:34 +03:00
|
|
|
bool m_write_combine : 1 { false };
|
2022-08-23 21:18:39 +03:00
|
|
|
bool m_mmapped_from_readable : 1 { false };
|
|
|
|
bool m_mmapped_from_writable : 1 { false };
|
2022-01-16 15:10:05 +03:00
|
|
|
|
|
|
|
IntrusiveRedBlackTreeNode<FlatPtr, Region, RawPtr<Region>> m_tree_node;
|
2021-07-23 03:40:16 +03:00
|
|
|
IntrusiveListNode<Region> m_vmobject_list_node;
|
2021-05-26 12:47:47 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-09 15:00:59 +03:00
|
|
|
using ListInVMObject = IntrusiveList<&Region::m_vmobject_list_node>;
|
2019-04-03 16:13:07 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-03-07 06:33:25 +03:00
|
|
|
AK_ENUM_BITWISE_OPERATORS(Region::Access)
|
|
|
|
|
2022-01-29 20:40:19 +03:00
|
|
|
constexpr Region::Access prot_to_region_access_flags(int prot)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-03-07 06:33:25 +03:00
|
|
|
Region::Access access = Region::Access::None;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((prot & PROT_READ) == PROT_READ)
|
2020-07-31 00:38:15 +03:00
|
|
|
access |= Region::Access::Read;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((prot & PROT_WRITE) == PROT_WRITE)
|
2020-07-31 00:38:15 +03:00
|
|
|
access |= Region::Access::Write;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((prot & PROT_EXEC) == PROT_EXEC)
|
2020-07-31 00:38:15 +03:00
|
|
|
access |= Region::Access::Execute;
|
|
|
|
return access;
|
|
|
|
}
|
|
|
|
|
2022-01-29 20:40:19 +03:00
|
|
|
constexpr int region_access_flags_to_prot(Region::Access access)
|
2020-12-29 04:11:47 +03:00
|
|
|
{
|
|
|
|
int prot = 0;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((access & Region::Access::Read) == Region::Access::Read)
|
2020-12-29 04:11:47 +03:00
|
|
|
prot |= PROT_READ;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((access & Region::Access::Write) == Region::Access::Write)
|
2020-12-29 04:11:47 +03:00
|
|
|
prot |= PROT_WRITE;
|
2021-11-07 00:06:08 +03:00
|
|
|
if ((access & Region::Access::Execute) == Region::Access::Execute)
|
2020-12-29 04:11:47 +03:00
|
|
|
prot |= PROT_EXEC;
|
|
|
|
return prot;
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|