2019-04-03 13:13:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Bitmap.h>
|
2019-08-08 08:53:24 +00:00
|
|
|
#include <AK/InlineLinkedList.h>
|
2019-09-16 08:19:44 +00:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <Kernel/Heap/SlabAllocator.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
2019-05-17 02:32:08 +00:00
|
|
|
#include <Kernel/VM/RangeAllocator.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
|
|
|
|
class Inode;
|
|
|
|
class VMObject;
|
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
class Region final : public InlineLinkedListNode<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:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum Access {
|
2019-05-30 14:14:37 +00:00
|
|
|
Read = 1,
|
|
|
|
Write = 2,
|
|
|
|
Execute = 4,
|
|
|
|
};
|
|
|
|
|
2019-10-01 17:58:41 +00: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 14:09:34 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
~Region();
|
|
|
|
|
2019-08-29 18:55:40 +00:00
|
|
|
const Range& range() const { return m_range; }
|
2019-06-07 10:56:50 +00:00
|
|
|
VirtualAddress vaddr() const { return m_range.base(); }
|
2019-05-17 02:32:08 +00:00
|
|
|
size_t size() const { return m_range.size(); }
|
2019-05-30 14:14:37 +00: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 18:58:12 +00:00
|
|
|
const String& name() const { return m_name; }
|
2019-08-29 18:55:40 +00:00
|
|
|
unsigned access() const { return m_access; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-06-07 18:58:12 +00:00
|
|
|
void set_name(const String& name) { m_name = name; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-09-04 09:27:14 +00:00
|
|
|
const VMObject& vmobject() const { return *m_vmobject; }
|
|
|
|
VMObject& vmobject() { return *m_vmobject; }
|
2019-04-03 13:13:07 +00:00
|
|
|
|
|
|
|
bool is_shared() const { return m_shared; }
|
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
2019-07-19 14:09:34 +00:00
|
|
|
bool is_user_accessible() const { return m_user_accessible; }
|
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
NonnullOwnPtr<Region> clone();
|
2019-05-17 02:32:08 +00:00
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-29 18:55:40 +00:00
|
|
|
bool contains(const Range& range) const
|
|
|
|
{
|
|
|
|
return m_range.contains(range);
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:56:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
size_t first_page_index() const
|
|
|
|
{
|
|
|
|
return m_offset_in_vmo / PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t last_page_index() const
|
|
|
|
{
|
|
|
|
return (first_page_index() + page_count()) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t page_count() const
|
|
|
|
{
|
2019-05-17 02:32:08 +00:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 09:38:59 +00:00
|
|
|
size_t offset_in_vmobject() const
|
|
|
|
{
|
|
|
|
return m_offset_in_vmo;
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
int commit();
|
|
|
|
|
|
|
|
size_t amount_resident() const;
|
|
|
|
size_t amount_shared() const;
|
|
|
|
|
|
|
|
PageDirectory* page_directory() { return m_page_directory.ptr(); }
|
|
|
|
|
|
|
|
void set_page_directory(PageDirectory& page_directory)
|
|
|
|
{
|
2019-04-14 00:36:06 +00:00
|
|
|
ASSERT(!m_page_directory || m_page_directory == &page_directory);
|
2019-04-03 13:13:07 +00:00
|
|
|
m_page_directory = page_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release_page_directory()
|
|
|
|
{
|
|
|
|
ASSERT(m_page_directory);
|
|
|
|
m_page_directory.clear();
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
bool should_cow(size_t page_index) const;
|
|
|
|
void set_should_cow(size_t page_index, bool);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-05-30 14:14:37 +00:00
|
|
|
void set_writable(bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
|
|
|
m_access |= Access::Read;
|
|
|
|
else
|
|
|
|
m_access &= ~Access::Write;
|
|
|
|
}
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-11-03 19:48:35 +00:00
|
|
|
void map(PageDirectory&);
|
2019-11-03 19:37:03 +00:00
|
|
|
enum class ShouldDeallocateVirtualMemoryRange {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
void unmap(ShouldDeallocateVirtualMemoryRange = ShouldDeallocateVirtualMemoryRange::Yes);
|
|
|
|
|
2019-11-03 14:32:11 +00:00
|
|
|
void remap_page(size_t index);
|
|
|
|
|
2019-08-08 08:53:24 +00:00
|
|
|
// For InlineLinkedListNode
|
|
|
|
Region* m_next { nullptr };
|
|
|
|
Region* m_prev { nullptr };
|
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
// NOTE: These are public so we can make<> them.
|
2019-10-01 17:58:41 +00:00
|
|
|
Region(const Range&, const String&, u8 access);
|
|
|
|
Region(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmo, const String&, u8 access);
|
|
|
|
Region(const Range&, RefPtr<Inode>&&, const String&, u8 access);
|
2019-07-19 14:09:34 +00:00
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
private:
|
2019-10-01 17:58:41 +00:00
|
|
|
Bitmap& ensure_cow_map() const;
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<PageDirectory> m_page_directory;
|
2019-05-17 02:32:08 +00:00
|
|
|
Range m_range;
|
2019-04-03 13:13:07 +00:00
|
|
|
size_t m_offset_in_vmo { 0 };
|
2019-09-04 09:27:14 +00:00
|
|
|
NonnullRefPtr<VMObject> m_vmobject;
|
2019-04-03 13:13:07 +00:00
|
|
|
String m_name;
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 m_access { 0 };
|
2019-04-03 13:13:07 +00:00
|
|
|
bool m_shared { false };
|
2019-07-19 14:09:34 +00:00
|
|
|
bool m_user_accessible { false };
|
2019-10-01 17:58:41 +00:00
|
|
|
mutable OwnPtr<Bitmap> m_cow_map;
|
2019-04-03 13:13:07 +00:00
|
|
|
};
|