2019-07-19 14:09:34 +00:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Thread.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
#include <Kernel/VM/Region.h>
|
|
|
|
#include <Kernel/VM/VMObject.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
Region::Region(const Range& range, const String& name, u8 access, bool cow)
|
2019-05-17 02:32:08 +00:00
|
|
|
: m_range(range)
|
|
|
|
, m_vmo(VMObject::create_anonymous(size()))
|
2019-06-07 18:58:12 +00:00
|
|
|
, m_name(name)
|
2019-05-30 14:14:37 +00:00
|
|
|
, m_access(access)
|
2019-04-03 13:13:07 +00:00
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
|
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
2019-07-19 14:09:34 +00:00
|
|
|
Region::Region(const Range& range, RefPtr<Inode>&& inode, const String& name, u8 access, bool cow)
|
2019-05-17 02:32:08 +00:00
|
|
|
: m_range(range)
|
2019-04-03 13:13:07 +00:00
|
|
|
, m_vmo(VMObject::create_file_backed(move(inode)))
|
2019-06-07 18:58:12 +00:00
|
|
|
, m_name(name)
|
2019-05-30 14:14:37 +00:00
|
|
|
, m_access(access)
|
2019-07-19 14:09:34 +00:00
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
|
2019-04-03 13:13:07 +00:00
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:38:47 +00:00
|
|
|
Region::Region(const Range& range, NonnullRefPtr<VMObject> vmo, size_t offset_in_vmo, const String& name, u8 access, bool cow)
|
2019-05-17 02:32:08 +00:00
|
|
|
: m_range(range)
|
2019-04-03 13:13:07 +00:00
|
|
|
, m_offset_in_vmo(offset_in_vmo)
|
|
|
|
, m_vmo(move(vmo))
|
2019-06-07 18:58:12 +00:00
|
|
|
, m_name(name)
|
2019-05-30 14:14:37 +00:00
|
|
|
, m_access(access)
|
2019-04-03 13:13:07 +00:00
|
|
|
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
|
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Region::~Region()
|
|
|
|
{
|
|
|
|
if (m_page_directory) {
|
|
|
|
MM.unmap_region(*this);
|
|
|
|
ASSERT(!m_page_directory);
|
|
|
|
}
|
|
|
|
MM.unregister_region(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Region::page_in()
|
|
|
|
{
|
|
|
|
ASSERT(m_page_directory);
|
|
|
|
ASSERT(!vmo().is_anonymous());
|
|
|
|
ASSERT(vmo().inode());
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("MM: page_in %u pages\n", page_count());
|
|
|
|
#endif
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
auto& vmo_page = vmo().physical_pages()[first_page_index() + i];
|
|
|
|
if (vmo_page.is_null()) {
|
|
|
|
bool success = MM.page_in_from_inode(*this, i);
|
|
|
|
if (!success)
|
|
|
|
return false;
|
2019-06-01 13:44:48 +00:00
|
|
|
continue;
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
2019-07-19 14:09:34 +00:00
|
|
|
MM.remap_region_page(*this, i);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr<Region> Region::clone()
|
2019-04-03 13:13:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(current);
|
2019-07-19 14:09:34 +00:00
|
|
|
|
|
|
|
// NOTE: Kernel-only regions should never be cloned.
|
|
|
|
ASSERT(is_user_accessible());
|
|
|
|
|
2019-05-30 14:14:37 +00:00
|
|
|
if (m_shared || (is_readable() && !is_writable())) {
|
2019-04-03 13:13:07 +00:00
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("%s<%u> Region::clone(): sharing %s (L%x)\n",
|
2019-06-07 09:43:58 +00:00
|
|
|
current->process().name().characters(),
|
|
|
|
current->pid(),
|
|
|
|
m_name.characters(),
|
2019-06-07 10:56:50 +00:00
|
|
|
vaddr().get());
|
2019-04-03 13:13:07 +00:00
|
|
|
#endif
|
|
|
|
// Create a new region backed by the same VMObject.
|
2019-07-19 14:09:34 +00:00
|
|
|
return Region::create_user_accessible(m_range, m_vmo, m_offset_in_vmo, m_name, m_access);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbgprintf("%s<%u> Region::clone(): cowing %s (L%x)\n",
|
2019-06-07 09:43:58 +00:00
|
|
|
current->process().name().characters(),
|
|
|
|
current->pid(),
|
|
|
|
m_name.characters(),
|
2019-06-07 10:56:50 +00:00
|
|
|
vaddr().get());
|
2019-04-03 13:13:07 +00:00
|
|
|
#endif
|
|
|
|
// Set up a COW region. The parent (this) region becomes COW as well!
|
2019-05-14 15:31:57 +00:00
|
|
|
m_cow_map.fill(true);
|
2019-04-03 13:13:07 +00:00
|
|
|
MM.remap_region(current->process().page_directory(), *this);
|
2019-07-19 14:09:34 +00:00
|
|
|
return Region::create_user_accessible(m_range, m_vmo->clone(), m_offset_in_vmo, m_name, m_access, true);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Region::commit()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
#ifdef MM_DEBUG
|
2019-06-07 10:56:50 +00:00
|
|
|
dbgprintf("MM: commit %u pages in Region %p (VMO=%p) at L%x\n", vmo().page_count(), this, &vmo(), vaddr().get());
|
2019-04-03 13:13:07 +00:00
|
|
|
#endif
|
|
|
|
for (size_t i = first_page_index(); i <= last_page_index(); ++i) {
|
|
|
|
if (!vmo().physical_pages()[i].is_null())
|
|
|
|
continue;
|
2019-06-11 11:13:02 +00:00
|
|
|
auto physical_page = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
|
2019-04-03 13:13:07 +00:00
|
|
|
if (!physical_page) {
|
|
|
|
kprintf("MM: commit was unable to allocate a physical page\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
vmo().physical_pages()[i] = move(physical_page);
|
2019-07-19 14:09:34 +00:00
|
|
|
MM.remap_region_page(*this, i);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Region::amount_resident() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
if (m_vmo->physical_pages()[first_page_index() + i])
|
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Region::amount_shared() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
auto& physical_page = m_vmo->physical_pages()[first_page_index() + i];
|
2019-06-21 13:29:31 +00:00
|
|
|
if (physical_page && physical_page->ref_count() > 1)
|
2019-04-03 13:13:07 +00:00
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2019-07-19 14:09:34 +00:00
|
|
|
|
|
|
|
NonnullRefPtr<Region> Region::create_user_accessible(const Range& range, const StringView& name, u8 access, bool cow)
|
|
|
|
{
|
|
|
|
auto region = adopt(*new Region(range, name, access, cow));
|
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Region> Region::create_user_accessible(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const StringView& name, u8 access, bool cow)
|
|
|
|
{
|
|
|
|
auto region = adopt(*new Region(range, move(vmobject), offset_in_vmobject, name, access, cow));
|
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Region> Region::create_user_accessible(const Range& range, NonnullRefPtr<Inode> inode, const StringView& name, u8 access, bool cow)
|
|
|
|
{
|
|
|
|
auto region = adopt(*new Region(range, move(inode), name, access, cow));
|
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Region> Region::create_kernel_only(const Range& range, const StringView& name, u8 access, bool cow)
|
|
|
|
{
|
|
|
|
auto region = adopt(*new Region(range, name, access, cow));
|
|
|
|
region->m_user_accessible = false;
|
|
|
|
return region;
|
|
|
|
}
|