2019-04-03 16:13:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Bitmap.h>
|
2019-08-08 11:53:24 +03:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-09-16 11:19:44 +03:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <Kernel/Heap/SlabAllocator.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
2019-05-17 05:32:08 +03:00
|
|
|
#include <Kernel/VM/RangeAllocator.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
|
|
|
|
class Inode;
|
|
|
|
class VMObject;
|
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
enum class PageFaultResponse {
|
|
|
|
ShouldCrash,
|
|
|
|
Continue,
|
|
|
|
};
|
|
|
|
|
2019-09-27 15:19:07 +03:00
|
|
|
class Region final : public InlineLinkedListNode<Region> {
|
2019-04-03 16:13:07 +03:00
|
|
|
friend class MemoryManager;
|
2019-06-07 12:43:58 +03:00
|
|
|
|
2019-09-16 11:19:44 +03:00
|
|
|
MAKE_SLAB_ALLOCATED(Region)
|
2019-04-03 16:13:07 +03:00
|
|
|
public:
|
2019-06-07 18:13:23 +03:00
|
|
|
enum Access {
|
2019-05-30 17:14:37 +03:00
|
|
|
Read = 1,
|
|
|
|
Write = 2,
|
|
|
|
Execute = 4,
|
|
|
|
};
|
|
|
|
|
2019-10-01 20:58:41 +03:00
|
|
|
static NonnullOwnPtr<Region> create_user_accessible(const Range&, const StringView& name, u8 access);
|
|
|
|
static NonnullOwnPtr<Region> create_user_accessible(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const StringView& name, u8 access);
|
|
|
|
static NonnullOwnPtr<Region> create_user_accessible(const Range&, NonnullRefPtr<Inode>, const StringView& name, u8 access);
|
|
|
|
static NonnullOwnPtr<Region> create_kernel_only(const Range&, const StringView& name, u8 access);
|
2019-07-19 17:09:34 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
~Region();
|
|
|
|
|
2019-08-29 21:55:40 +03:00
|
|
|
const Range& range() const { return m_range; }
|
2019-06-07 13:56:50 +03:00
|
|
|
VirtualAddress vaddr() const { return m_range.base(); }
|
2019-05-17 05:32:08 +03:00
|
|
|
size_t size() const { return m_range.size(); }
|
2019-05-30 17:14:37 +03:00
|
|
|
bool is_readable() const { return m_access & Access::Read; }
|
|
|
|
bool is_writable() const { return m_access & Access::Write; }
|
|
|
|
bool is_executable() const { return m_access & Access::Execute; }
|
2019-06-07 21:58:12 +03:00
|
|
|
const String& name() const { return m_name; }
|
2019-08-29 21:55:40 +03:00
|
|
|
unsigned access() const { return m_access; }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-06-07 21:58:12 +03:00
|
|
|
void set_name(const String& name) { m_name = name; }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-09-04 12:27:14 +03:00
|
|
|
const VMObject& vmobject() const { return *m_vmobject; }
|
|
|
|
VMObject& vmobject() { return *m_vmobject; }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
|
|
|
bool is_shared() const { return m_shared; }
|
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
2019-11-17 14:11:43 +03:00
|
|
|
bool is_stack() const { return m_stack; }
|
|
|
|
void set_stack(bool stack) { m_stack = stack; }
|
|
|
|
|
2019-11-24 14:24:16 +03:00
|
|
|
bool is_mmap() const { return m_mmap; }
|
|
|
|
void set_mmap(bool mmap) { m_mmap = mmap; }
|
|
|
|
|
2019-07-19 17:09:34 +03:00
|
|
|
bool is_user_accessible() const { return m_user_accessible; }
|
2019-12-15 22:11:57 +03:00
|
|
|
void set_user_accessible(bool b) { m_user_accessible = b; }
|
2019-07-19 17:09:34 +03:00
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
PageFaultResponse handle_fault(const PageFault&);
|
|
|
|
|
2019-09-27 15:19:07 +03:00
|
|
|
NonnullOwnPtr<Region> clone();
|
2019-05-17 05:32:08 +03:00
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-29 21:55:40 +03:00
|
|
|
bool contains(const Range& range) const
|
|
|
|
{
|
|
|
|
return m_range.contains(range);
|
|
|
|
}
|
|
|
|
|
2019-06-07 13:56:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
size_t first_page_index() const
|
|
|
|
{
|
2019-12-19 21:13:44 +03:00
|
|
|
return m_offset_in_vmobject / PAGE_SIZE;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t last_page_index() const
|
|
|
|
{
|
|
|
|
return (first_page_index() + page_count()) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t page_count() const
|
|
|
|
{
|
2019-05-17 05:32:08 +03:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 12:38:59 +03:00
|
|
|
size_t offset_in_vmobject() const
|
|
|
|
{
|
2019-12-19 21:13:44 +03:00
|
|
|
return m_offset_in_vmobject;
|
2019-10-01 12:38:59 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 00:43:32 +03:00
|
|
|
bool commit();
|
|
|
|
bool commit(size_t page_index);
|
2019-04-03 16:13:07 +03:00
|
|
|
|
|
|
|
size_t amount_resident() const;
|
|
|
|
size_t amount_shared() const;
|
|
|
|
|
2019-10-01 20:58:41 +03:00
|
|
|
bool should_cow(size_t page_index) const;
|
|
|
|
void set_should_cow(size_t page_index, bool);
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-12-15 18:53:00 +03:00
|
|
|
u32 cow_pages() const;
|
|
|
|
|
2019-05-30 17:14:37 +03:00
|
|
|
void set_writable(bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
2019-12-02 20:15:36 +03:00
|
|
|
m_access |= Access::Write;
|
2019-05-30 17:14:37 +03:00
|
|
|
else
|
|
|
|
m_access &= ~Access::Write;
|
|
|
|
}
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-12-02 21:14:16 +03:00
|
|
|
void set_readable(bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
|
|
|
m_access |= Access::Read;
|
|
|
|
else
|
|
|
|
m_access &= ~Access::Read;
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:35 +03:00
|
|
|
void map(PageDirectory&);
|
2019-11-03 22:37:03 +03:00
|
|
|
enum class ShouldDeallocateVirtualMemoryRange {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
void unmap(ShouldDeallocateVirtualMemoryRange = ShouldDeallocateVirtualMemoryRange::Yes);
|
|
|
|
|
2019-11-03 22:59:54 +03:00
|
|
|
void remap();
|
2019-11-03 17:32:11 +03:00
|
|
|
void remap_page(size_t index);
|
|
|
|
|
2019-08-08 11:53:24 +03:00
|
|
|
// For InlineLinkedListNode
|
|
|
|
Region* m_next { nullptr };
|
|
|
|
Region* m_prev { nullptr };
|
|
|
|
|
2019-09-27 15:19:07 +03:00
|
|
|
// NOTE: These are public so we can make<> them.
|
2019-10-01 20:58:41 +03:00
|
|
|
Region(const Range&, const String&, u8 access);
|
2019-12-19 21:13:44 +03:00
|
|
|
Region(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const String&, u8 access);
|
2019-11-04 02:21:08 +03:00
|
|
|
Region(const Range&, NonnullRefPtr<Inode>, const String&, u8 access);
|
2019-07-19 17:09:34 +03:00
|
|
|
|
2019-09-27 15:19:07 +03:00
|
|
|
private:
|
2019-10-01 20:58:41 +03:00
|
|
|
Bitmap& ensure_cow_map() const;
|
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
PageFaultResponse handle_cow_fault(size_t page_index);
|
|
|
|
PageFaultResponse handle_inode_fault(size_t page_index);
|
|
|
|
PageFaultResponse handle_zero_fault(size_t page_index);
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<PageDirectory> m_page_directory;
|
2019-05-17 05:32:08 +03:00
|
|
|
Range m_range;
|
2019-12-19 21:13:44 +03:00
|
|
|
size_t m_offset_in_vmobject { 0 };
|
2019-09-04 12:27:14 +03:00
|
|
|
NonnullRefPtr<VMObject> m_vmobject;
|
2019-04-03 16:13:07 +03:00
|
|
|
String m_name;
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 m_access { 0 };
|
2019-04-03 16:13:07 +03:00
|
|
|
bool m_shared { false };
|
2019-07-19 17:09:34 +03:00
|
|
|
bool m_user_accessible { false };
|
2019-11-17 14:11:43 +03:00
|
|
|
bool m_stack { false };
|
2019-11-24 14:24:16 +03:00
|
|
|
bool m_mmap { false };
|
2019-10-01 20:58:41 +03:00
|
|
|
mutable OwnPtr<Bitmap> m_cow_map;
|
2019-04-03 16:13:07 +03:00
|
|
|
};
|