2019-04-03 16:13:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <AK/Bitmap.h>
|
2019-08-08 11:53:24 +03:00
|
|
|
#include <AK/InlineLinkedList.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-08-08 11:53:24 +03:00
|
|
|
class Region : public RefCounted<Region>
|
|
|
|
, public InlineLinkedListNode<Region> {
|
2019-04-03 16:13:07 +03:00
|
|
|
friend class MemoryManager;
|
2019-06-07 12:43:58 +03:00
|
|
|
|
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-07-19 17:09:34 +03:00
|
|
|
static NonnullRefPtr<Region> create_user_accessible(const Range&, const StringView& name, u8 access, bool cow = false);
|
|
|
|
static NonnullRefPtr<Region> create_user_accessible(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, const StringView& name, u8 access, bool cow = false);
|
|
|
|
static NonnullRefPtr<Region> create_user_accessible(const Range&, NonnullRefPtr<Inode>, const StringView& name, u8 access, bool cow = false);
|
|
|
|
static NonnullRefPtr<Region> create_kernel_only(const Range&, const StringView& name, u8 access, bool cow = false);
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
~Region();
|
|
|
|
|
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-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
|
|
|
|
|
|
|
const VMObject& vmo() const { return *m_vmo; }
|
|
|
|
VMObject& vmo() { return *m_vmo; }
|
|
|
|
|
|
|
|
bool is_shared() const { return m_shared; }
|
|
|
|
void set_shared(bool shared) { m_shared = shared; }
|
|
|
|
|
2019-07-19 17:09:34 +03:00
|
|
|
bool is_user_accessible() const { return m_user_accessible; }
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<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-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
|
|
|
|
{
|
|
|
|
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 05:32:08 +03:00
|
|
|
return size() / PAGE_SIZE;
|
2019-04-03 16:13:07 +03: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 03:36:06 +03:00
|
|
|
ASSERT(!m_page_directory || m_page_directory == &page_directory);
|
2019-04-03 16:13:07 +03:00
|
|
|
m_page_directory = page_directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release_page_directory()
|
|
|
|
{
|
|
|
|
ASSERT(m_page_directory);
|
|
|
|
m_page_directory.clear();
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:31:57 +03:00
|
|
|
bool should_cow(size_t page_index) const { return m_cow_map.get(page_index); }
|
|
|
|
void set_should_cow(size_t page_index, bool cow) { m_cow_map.set(page_index, cow); }
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-05-30 17:14:37 +03:00
|
|
|
void set_writable(bool b)
|
|
|
|
{
|
|
|
|
if (b)
|
|
|
|
m_access |= Access::Read;
|
|
|
|
else
|
|
|
|
m_access &= ~Access::Write;
|
|
|
|
}
|
2019-04-03 16:13:07 +03:00
|
|
|
|
2019-08-08 11:53:24 +03:00
|
|
|
// For InlineLinkedListNode
|
|
|
|
Region* m_next { nullptr };
|
|
|
|
Region* m_prev { nullptr };
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
private:
|
2019-07-19 17:09:34 +03:00
|
|
|
Region(const Range&, const String&, u8 access, bool cow = false);
|
|
|
|
Region(const Range&, NonnullRefPtr<VMObject>, size_t offset_in_vmo, const String&, u8 access, bool cow = false);
|
|
|
|
Region(const Range&, RefPtr<Inode>&&, const String&, u8 access, bool cow = false);
|
|
|
|
|
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-04-03 16:13:07 +03:00
|
|
|
size_t m_offset_in_vmo { 0 };
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<VMObject> m_vmo;
|
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-04-03 16:13:07 +03:00
|
|
|
Bitmap m_cow_map;
|
|
|
|
};
|