1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

Kernel: Track user accessibility per Region.

Region now has is_user_accessible(), which informs the memory manager how
to map these pages. Previously, we were just passing a "bool user_allowed"
to various functions and I'm not at all sure that any of that was correct.

All the Region constructors are now hidden, and you must go through one of
these helpers to construct a region:

- Region::create_user_accessible(...)
- Region::create_kernel_only(...)

That ensures that we don't accidentally create a Region without specifying
user accessibility. :^)
This commit is contained in:
Andreas Kling 2019-07-19 16:09:34 +02:00
parent 4547a301c4
commit 5b2447a27b
6 changed files with 73 additions and 30 deletions

View file

@ -1,3 +1,4 @@
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/Process.h>
#include <Kernel/Thread.h>
#include <Kernel/VM/MemoryManager.h>
@ -15,12 +16,12 @@ Region::Region(const Range& range, const String& name, u8 access, bool cow)
MM.register_region(*this);
}
Region::Region(const Range& range, RefPtr<Inode>&& inode, const String& name, u8 access)
Region::Region(const Range& range, RefPtr<Inode>&& inode, const String& name, u8 access, bool cow)
: m_range(range)
, m_vmo(VMObject::create_file_backed(move(inode)))
, m_name(name)
, m_access(access)
, m_cow_map(Bitmap::create(m_vmo->page_count()))
, m_cow_map(Bitmap::create(m_vmo->page_count(), cow))
{
MM.register_region(*this);
}
@ -61,7 +62,7 @@ bool Region::page_in()
return false;
continue;
}
MM.remap_region_page(*this, i, true);
MM.remap_region_page(*this, i);
}
return true;
}
@ -69,6 +70,10 @@ bool Region::page_in()
NonnullRefPtr<Region> Region::clone()
{
ASSERT(current);
// NOTE: Kernel-only regions should never be cloned.
ASSERT(is_user_accessible());
if (m_shared || (is_readable() && !is_writable())) {
#ifdef MM_DEBUG
dbgprintf("%s<%u> Region::clone(): sharing %s (L%x)\n",
@ -78,7 +83,7 @@ NonnullRefPtr<Region> Region::clone()
vaddr().get());
#endif
// Create a new region backed by the same VMObject.
return adopt(*new Region(m_range, m_vmo, m_offset_in_vmo, String(m_name), m_access));
return Region::create_user_accessible(m_range, m_vmo, m_offset_in_vmo, m_name, m_access);
}
#ifdef MM_DEBUG
@ -91,7 +96,7 @@ NonnullRefPtr<Region> Region::clone()
// Set up a COW region. The parent (this) region becomes COW as well!
m_cow_map.fill(true);
MM.remap_region(current->process().page_directory(), *this);
return adopt(*new Region(m_range, m_vmo->clone(), m_offset_in_vmo, String(m_name), m_access, true));
return Region::create_user_accessible(m_range, m_vmo->clone(), m_offset_in_vmo, m_name, m_access, true);
}
int Region::commit()
@ -109,7 +114,7 @@ int Region::commit()
return -ENOMEM;
}
vmo().physical_pages()[i] = move(physical_page);
MM.remap_region_page(*this, i, true);
MM.remap_region_page(*this, i);
}
return 0;
}
@ -134,3 +139,31 @@ size_t Region::amount_shared() const
}
return bytes;
}
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;
}