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-08-07 16:06:17 +00:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/VM/InodeVMObject.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
#include <Kernel/VM/Region.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
Region::Region(const Range& range, const String& name, u8 access)
|
2019-05-17 02:32:08 +00:00
|
|
|
: m_range(range)
|
2019-09-04 09:27:14 +00:00
|
|
|
, m_vmobject(AnonymousVMObject::create_with_size(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
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
Region::Region(const Range& range, RefPtr<Inode>&& inode, const String& name, u8 access)
|
2019-05-17 02:32:08 +00:00
|
|
|
: m_range(range)
|
2019-09-04 09:27:14 +00:00
|
|
|
, m_vmobject(InodeVMObject::create_with_inode(*inode))
|
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
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
Region::Region(const Range& range, NonnullRefPtr<VMObject> vmo, size_t offset_in_vmo, const String& name, u8 access)
|
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)
|
2019-09-04 09:27:14 +00:00
|
|
|
, m_vmobject(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
|
|
|
{
|
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Region::~Region()
|
|
|
|
{
|
2019-09-05 09:13:10 +00:00
|
|
|
// Make sure we disable interrupts so we don't get interrupted between unmapping and unregistering.
|
|
|
|
// Unmapping the region will give the VM back to the RangeAllocator, so an interrupt handler would
|
|
|
|
// find the address<->region mappings in an invalid state there.
|
|
|
|
InterruptDisabler disabler;
|
2019-04-03 13:13:07 +00:00
|
|
|
if (m_page_directory) {
|
2019-11-03 19:37:03 +00:00
|
|
|
unmap(ShouldDeallocateVirtualMemoryRange::Yes);
|
2019-04-03 13:13:07 +00:00
|
|
|
ASSERT(!m_page_directory);
|
|
|
|
}
|
|
|
|
MM.unregister_region(*this);
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:19:07 +00:00
|
|
|
NonnullOwnPtr<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
|
2019-08-23 14:18:27 +00:00
|
|
|
dbgprintf("%s<%u> Region::clone(): sharing %s (V%p)\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-09-04 09:27:14 +00:00
|
|
|
return Region::create_user_accessible(m_range, m_vmobject, m_offset_in_vmo, m_name, m_access);
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MM_DEBUG
|
2019-08-23 14:18:27 +00:00
|
|
|
dbgprintf("%s<%u> Region::clone(): cowing %s (V%p)\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-10-01 17:58:41 +00:00
|
|
|
ensure_cow_map().fill(true);
|
2019-04-03 13:13:07 +00:00
|
|
|
MM.remap_region(current->process().page_directory(), *this);
|
2019-10-01 17:58:41 +00:00
|
|
|
auto clone_region = Region::create_user_accessible(m_range, m_vmobject->clone(), m_offset_in_vmo, m_name, m_access);
|
|
|
|
clone_region->ensure_cow_map();
|
|
|
|
return clone_region;
|
2019-04-03 13:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Region::commit()
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
#ifdef MM_DEBUG
|
2019-09-04 09:27:14 +00:00
|
|
|
dbgprintf("MM: commit %u pages in Region %p (VMO=%p) at V%p\n", vmobject().page_count(), this, &vmobject(), vaddr().get());
|
2019-04-03 13:13:07 +00:00
|
|
|
#endif
|
2019-11-03 14:41:49 +00:00
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
auto& vmobject_physical_page_entry = vmobject().physical_pages()[first_page_index() + i];
|
|
|
|
if (!vmobject_physical_page_entry.is_null())
|
2019-04-03 13:13:07 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-11-03 14:41:49 +00:00
|
|
|
vmobject_physical_page_entry = move(physical_page);
|
|
|
|
remap_page(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) {
|
2019-09-04 09:27:14 +00:00
|
|
|
if (m_vmobject->physical_pages()[first_page_index() + i])
|
2019-04-03 13:13:07 +00:00
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Region::amount_shared() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
2019-09-04 09:27:14 +00:00
|
|
|
auto& physical_page = m_vmobject->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
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
NonnullOwnPtr<Region> Region::create_user_accessible(const Range& range, const StringView& name, u8 access)
|
2019-07-19 14:09:34 +00:00
|
|
|
{
|
2019-10-01 17:58:41 +00:00
|
|
|
auto region = make<Region>(range, name, access);
|
2019-07-19 14:09:34 +00:00
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
NonnullOwnPtr<Region> Region::create_user_accessible(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const StringView& name, u8 access)
|
2019-07-19 14:09:34 +00:00
|
|
|
{
|
2019-10-01 17:58:41 +00:00
|
|
|
auto region = make<Region>(range, move(vmobject), offset_in_vmobject, name, access);
|
2019-07-19 14:09:34 +00:00
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
NonnullOwnPtr<Region> Region::create_user_accessible(const Range& range, NonnullRefPtr<Inode> inode, const StringView& name, u8 access)
|
2019-07-19 14:09:34 +00:00
|
|
|
{
|
2019-10-01 17:58:41 +00:00
|
|
|
auto region = make<Region>(range, move(inode), name, access);
|
2019-07-19 14:09:34 +00:00
|
|
|
region->m_user_accessible = true;
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:58:41 +00:00
|
|
|
NonnullOwnPtr<Region> Region::create_kernel_only(const Range& range, const StringView& name, u8 access)
|
2019-07-19 14:09:34 +00:00
|
|
|
{
|
2019-10-01 17:58:41 +00:00
|
|
|
auto region = make<Region>(range, name, access);
|
2019-07-19 14:09:34 +00:00
|
|
|
region->m_user_accessible = false;
|
|
|
|
return region;
|
|
|
|
}
|
2019-10-01 17:58:41 +00:00
|
|
|
|
|
|
|
bool Region::should_cow(size_t page_index) const
|
|
|
|
{
|
|
|
|
if (m_shared)
|
|
|
|
return false;
|
|
|
|
return m_cow_map && m_cow_map->get(page_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Region::set_should_cow(size_t page_index, bool cow)
|
|
|
|
{
|
|
|
|
ASSERT(!m_shared);
|
|
|
|
ensure_cow_map().set(page_index, cow);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap& Region::ensure_cow_map() const
|
|
|
|
{
|
|
|
|
if (!m_cow_map)
|
|
|
|
m_cow_map = make<Bitmap>(page_count(), true);
|
|
|
|
return *m_cow_map;
|
|
|
|
}
|
2019-11-03 14:32:11 +00:00
|
|
|
|
|
|
|
void Region::remap_page(size_t index)
|
|
|
|
{
|
|
|
|
ASSERT(page_directory());
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto page_vaddr = vaddr().offset(index * PAGE_SIZE);
|
|
|
|
auto& pte = MM.ensure_pte(*page_directory(), page_vaddr);
|
|
|
|
auto& physical_page = vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
ASSERT(physical_page);
|
|
|
|
pte.set_physical_page_base(physical_page->paddr().get());
|
|
|
|
pte.set_present(true);
|
|
|
|
if (should_cow(index))
|
|
|
|
pte.set_writable(false);
|
|
|
|
else
|
|
|
|
pte.set_writable(is_writable());
|
|
|
|
pte.set_user_allowed(is_user_accessible());
|
|
|
|
page_directory()->flush(page_vaddr);
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
dbg() << "MM: >> region.remap_page (PD=" << page_directory()->cr3() << ", PTE=" << (void*)pte.raw() << "{" << &pte << "}) " << name() << " " << page_vaddr << " => " << physical_page->paddr() << " (@" << physical_page.ptr() << ")";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2019-11-03 19:37:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Region::unmap(ShouldDeallocateVirtualMemoryRange deallocate_range)
|
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
ASSERT(page_directory());
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
|
|
|
auto vaddr = this->vaddr().offset(i * PAGE_SIZE);
|
|
|
|
auto& pte = MM.ensure_pte(*page_directory(), vaddr);
|
|
|
|
pte.set_physical_page_base(0);
|
|
|
|
pte.set_present(false);
|
|
|
|
pte.set_writable(false);
|
|
|
|
pte.set_user_allowed(false);
|
|
|
|
page_directory()->flush(vaddr);
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
auto& physical_page = region.vmobject().physical_pages()[region.first_page_index() + i];
|
|
|
|
dbgprintf("MM: >> Unmapped V%p => P%p <<\n", vaddr, physical_page ? physical_page->paddr().get() : 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (deallocate_range == ShouldDeallocateVirtualMemoryRange::Yes)
|
|
|
|
page_directory()->range_allocator().deallocate(range());
|
|
|
|
release_page_directory();
|
|
|
|
}
|
|
|
|
|
2019-11-03 19:48:35 +00:00
|
|
|
void Region::map(PageDirectory& page_directory)
|
2019-11-03 19:37:03 +00:00
|
|
|
{
|
2019-11-03 19:48:35 +00:00
|
|
|
MM.map_region_at_address(page_directory, *this, vaddr());
|
2019-11-03 19:37:03 +00:00
|
|
|
}
|