2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-03-08 14:33:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-07-19 17:09:34 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/PageDirectory.h>
|
|
|
|
#include <Kernel/Memory/Region.h>
|
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2021-02-14 11:03:54 +03:00
|
|
|
#include <Kernel/Panic.h>
|
2019-04-03 16:13:07 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-08-06 14:54:48 +03:00
|
|
|
Region::Region(VirtualRange const& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2021-07-25 02:46:44 +03:00
|
|
|
: m_range(range)
|
2019-12-19 21:13:44 +03:00
|
|
|
, m_offset_in_vmobject(offset_in_vmobject)
|
|
|
|
, m_vmobject(move(vmobject))
|
2021-02-14 03:25:22 +03:00
|
|
|
, m_name(move(name))
|
2021-01-29 16:38:49 +03:00
|
|
|
, m_access(access | ((access & 0x7) << 4))
|
2021-01-02 18:38:05 +03:00
|
|
|
, m_shared(shared)
|
2021-02-14 03:25:22 +03:00
|
|
|
, m_cacheable(cacheable == Cacheable::Yes)
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_range.base().is_page_aligned());
|
|
|
|
VERIFY(m_range.size());
|
|
|
|
VERIFY((m_range.size() % PAGE_SIZE) == 0);
|
2021-02-13 03:16:57 +03:00
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
m_vmobject->add_region(*this);
|
2019-04-03 16:13:07 +03:00
|
|
|
MM.register_region(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Region::~Region()
|
|
|
|
{
|
2021-07-23 03:40:16 +03:00
|
|
|
m_vmobject->remove_region(*this);
|
2020-09-03 07:57:09 +03:00
|
|
|
|
2021-08-09 03:21:04 +03:00
|
|
|
MM.unregister_region(*this);
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
if (m_page_directory) {
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker page_lock(m_page_directory->get_lock());
|
|
|
|
SpinlockLocker lock(s_mm_lock);
|
2021-08-06 22:45:05 +03:00
|
|
|
unmap(ShouldDeallocateVirtualRange::Yes);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_page_directory);
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 13:11:05 +03:00
|
|
|
KResultOr<NonnullOwnPtr<Region>> Region::try_clone()
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
VERIFY(Process::has_current());
|
2020-04-12 21:22:26 +03:00
|
|
|
|
2020-03-01 12:54:18 +03:00
|
|
|
if (m_shared) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_stack);
|
2020-03-01 13:08:28 +03:00
|
|
|
if (vmobject().is_inode())
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(vmobject().is_shared_inode());
|
2020-03-01 13:08:28 +03:00
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
// Create a new region backed by the same VMObject.
|
2021-09-06 20:24:54 +03:00
|
|
|
|
|
|
|
OwnPtr<KString> region_name;
|
|
|
|
if (m_name)
|
|
|
|
region_name = TRY(m_name->try_clone());
|
|
|
|
|
2021-09-05 17:06:37 +03:00
|
|
|
auto region = TRY(Region::try_create_user_accessible(
|
2021-09-06 20:24:54 +03:00
|
|
|
m_range, m_vmobject, m_offset_in_vmobject, move(region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared));
|
2020-01-10 21:24:01 +03:00
|
|
|
region->set_mmap(m_mmap);
|
2020-02-08 04:39:46 +03:00
|
|
|
region->set_shared(m_shared);
|
2021-02-02 21:56:11 +03:00
|
|
|
region->set_syscall_region(is_syscall_region());
|
2020-01-10 21:24:01 +03:00
|
|
|
return region;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2020-03-01 13:08:28 +03:00
|
|
|
if (vmobject().is_inode())
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(vmobject().is_private_inode());
|
2020-03-01 13:08:28 +03:00
|
|
|
|
2021-09-05 17:06:37 +03:00
|
|
|
auto vmobject_clone = TRY(vmobject().try_clone());
|
2020-09-05 06:12:25 +03:00
|
|
|
|
2020-02-29 14:51:44 +03:00
|
|
|
// Set up a COW region. The parent (this) region becomes COW as well!
|
2019-11-03 22:59:54 +03:00
|
|
|
remap();
|
2021-09-06 20:24:54 +03:00
|
|
|
|
|
|
|
OwnPtr<KString> clone_region_name;
|
|
|
|
if (m_name)
|
|
|
|
clone_region_name = TRY(m_name->try_clone());
|
|
|
|
|
2021-09-05 17:06:37 +03:00
|
|
|
auto clone_region = TRY(Region::try_create_user_accessible(
|
2021-09-06 20:24:54 +03:00
|
|
|
m_range, vmobject_clone, m_offset_in_vmobject, move(clone_region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared));
|
2021-08-15 13:11:05 +03:00
|
|
|
|
2019-11-17 14:11:43 +03:00
|
|
|
if (m_stack) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(is_readable());
|
|
|
|
VERIFY(is_writable());
|
|
|
|
VERIFY(vmobject().is_anonymous());
|
2019-11-17 14:11:43 +03:00
|
|
|
clone_region->set_stack(true);
|
|
|
|
}
|
2021-02-02 21:56:11 +03:00
|
|
|
clone_region->set_syscall_region(is_syscall_region());
|
2020-01-10 21:24:01 +03:00
|
|
|
clone_region->set_mmap(m_mmap);
|
2019-10-01 20:58:41 +03:00
|
|
|
return clone_region;
|
2019-04-03 16:13:07 +03:00
|
|
|
}
|
|
|
|
|
2020-09-03 07:57:09 +03:00
|
|
|
void Region::set_vmobject(NonnullRefPtr<VMObject>&& obj)
|
|
|
|
{
|
|
|
|
if (m_vmobject.ptr() == obj.ptr())
|
|
|
|
return;
|
2021-07-23 03:40:16 +03:00
|
|
|
m_vmobject->remove_region(*this);
|
2020-09-03 07:57:09 +03:00
|
|
|
m_vmobject = move(obj);
|
2021-07-23 03:40:16 +03:00
|
|
|
m_vmobject->add_region(*this);
|
2020-09-03 07:57:09 +03:00
|
|
|
}
|
|
|
|
|
2020-09-06 00:52:14 +03:00
|
|
|
size_t Region::cow_pages() const
|
2019-04-03 16:13:07 +03:00
|
|
|
{
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!vmobject().is_anonymous())
|
2019-12-15 18:53:00 +03:00
|
|
|
return 0;
|
2021-07-22 15:25:41 +03:00
|
|
|
return static_cast<AnonymousVMObject const&>(vmobject()).cow_pages();
|
2019-12-15 18:53:00 +03:00
|
|
|
}
|
|
|
|
|
2019-12-29 14:28:32 +03:00
|
|
|
size_t Region::amount_dirty() const
|
|
|
|
{
|
|
|
|
if (!vmobject().is_inode())
|
|
|
|
return amount_resident();
|
2021-07-22 15:25:41 +03:00
|
|
|
return static_cast<InodeVMObject const&>(vmobject()).amount_dirty();
|
2019-12-29 14:28:32 +03:00
|
|
|
}
|
|
|
|
|
2019-04-03 16:13:07 +03:00
|
|
|
size_t Region::amount_resident() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
2020-04-28 17:19:50 +03:00
|
|
|
auto* page = physical_page(i);
|
2021-01-02 02:47:55 +03:00
|
|
|
if (page && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
|
2019-04-03 16:13:07 +03: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) {
|
2020-04-28 17:19:50 +03:00
|
|
|
auto* page = physical_page(i);
|
2021-01-02 02:47:55 +03:00
|
|
|
if (page && page->ref_count() > 1 && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
|
2019-04-03 16:13:07 +03:00
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2019-07-19 17:09:34 +03:00
|
|
|
|
2021-08-15 13:11:05 +03:00
|
|
|
KResultOr<NonnullOwnPtr<Region>> Region::try_create_user_accessible(VirtualRange const& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2019-07-19 17:09:34 +03:00
|
|
|
{
|
2021-08-15 13:11:05 +03:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared));
|
2019-07-19 17:09:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-15 13:11:05 +03:00
|
|
|
KResultOr<NonnullOwnPtr<Region>> Region::try_create_kernel_only(VirtualRange const& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable)
|
2019-07-19 17:09:34 +03:00
|
|
|
{
|
2021-08-15 13:11:05 +03:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, false));
|
2020-01-10 08:57:18 +03:00
|
|
|
}
|
|
|
|
|
2019-10-01 20:58:41 +03:00
|
|
|
bool Region::should_cow(size_t page_index) const
|
|
|
|
{
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!vmobject().is_anonymous())
|
2019-10-01 20:58:41 +03:00
|
|
|
return false;
|
2021-07-22 15:25:41 +03:00
|
|
|
return static_cast<AnonymousVMObject const&>(vmobject()).should_cow(first_page_index() + page_index, m_shared);
|
2019-10-01 20:58:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Region::set_should_cow(size_t page_index, bool cow)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_shared);
|
2020-09-06 00:52:14 +03:00
|
|
|
if (vmobject().is_anonymous())
|
|
|
|
static_cast<AnonymousVMObject&>(vmobject()).set_should_cow(first_page_index() + page_index, cow);
|
2019-10-01 20:58:41 +03:00
|
|
|
}
|
2019-11-03 17:32:11 +03:00
|
|
|
|
2020-09-02 01:10:54 +03:00
|
|
|
bool Region::map_individual_page_impl(size_t page_index)
|
2019-11-03 17:32:11 +03:00
|
|
|
{
|
2021-08-29 21:10:24 +03:00
|
|
|
VERIFY(m_page_directory->get_lock().is_locked_by_current_processor());
|
2020-07-06 21:47:08 +03:00
|
|
|
auto page_vaddr = vaddr_from_page_index(page_index);
|
2021-02-14 11:03:54 +03:00
|
|
|
|
|
|
|
bool user_allowed = page_vaddr.get() >= 0x00800000 && is_user_address(page_vaddr);
|
|
|
|
if (is_mmap() && !user_allowed) {
|
|
|
|
PANIC("About to map mmap'ed page at a kernel address");
|
|
|
|
}
|
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
// NOTE: We have to take the MM lock for PTE's to stay valid while we use them.
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker mm_locker(s_mm_lock);
|
2021-07-23 03:40:16 +03:00
|
|
|
|
2020-09-02 01:10:54 +03:00
|
|
|
auto* pte = MM.ensure_pte(*m_page_directory, page_vaddr);
|
2021-01-12 00:08:50 +03:00
|
|
|
if (!pte)
|
2020-09-02 01:10:54 +03:00
|
|
|
return false;
|
2020-04-28 17:19:50 +03:00
|
|
|
auto* page = physical_page(page_index);
|
|
|
|
if (!page || (!is_readable() && !is_writable())) {
|
2020-09-02 01:10:54 +03:00
|
|
|
pte->clear();
|
2020-01-01 21:30:38 +03:00
|
|
|
} else {
|
2020-09-02 01:10:54 +03:00
|
|
|
pte->set_cache_disabled(!m_cacheable);
|
|
|
|
pte->set_physical_page_base(page->paddr().get());
|
|
|
|
pte->set_present(true);
|
2020-09-06 00:52:14 +03:00
|
|
|
if (page->is_shared_zero_page() || page->is_lazy_committed_page() || should_cow(page_index))
|
2020-09-02 01:10:54 +03:00
|
|
|
pte->set_writable(false);
|
2020-01-01 21:30:38 +03:00
|
|
|
else
|
2020-09-02 01:10:54 +03:00
|
|
|
pte->set_writable(is_writable());
|
2020-07-03 19:23:09 +03:00
|
|
|
if (Processor::current().has_feature(CPUFeature::NX))
|
2020-09-02 01:10:54 +03:00
|
|
|
pte->set_execute_disabled(!is_executable());
|
2021-02-14 11:03:54 +03:00
|
|
|
pte->set_user_allowed(user_allowed);
|
2020-01-10 00:29:31 +03:00
|
|
|
}
|
2020-09-02 01:10:54 +03:00
|
|
|
return true;
|
2019-11-03 17:32:11 +03:00
|
|
|
}
|
2019-11-03 22:37:03 +03:00
|
|
|
|
2021-01-02 22:03:14 +03:00
|
|
|
bool Region::do_remap_vmobject_page(size_t page_index, bool with_flush)
|
2020-01-01 21:30:38 +03:00
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(vmobject().m_lock);
|
2021-01-23 19:11:45 +03:00
|
|
|
if (!m_page_directory)
|
|
|
|
return true; // not an error, region may have not yet mapped it
|
2021-01-02 22:03:14 +03:00
|
|
|
if (!translate_vmobject_page(page_index))
|
|
|
|
return true; // not an error, region doesn't map this page
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker page_lock(m_page_directory->get_lock());
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(physical_page(page_index));
|
2020-09-02 01:10:54 +03:00
|
|
|
bool success = map_individual_page_impl(page_index);
|
2020-07-06 21:47:08 +03:00
|
|
|
if (with_flush)
|
2021-10-02 09:45:15 +03:00
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr_from_page_index(page_index));
|
2020-09-02 01:10:54 +03:00
|
|
|
return success;
|
2020-01-01 21:30:38 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 22:03:14 +03:00
|
|
|
bool Region::remap_vmobject_page(size_t page_index, bool with_flush)
|
|
|
|
{
|
|
|
|
auto& vmobject = this->vmobject();
|
2021-07-25 18:11:50 +03:00
|
|
|
bool success = true;
|
|
|
|
vmobject.for_each_region([&](auto& region) {
|
|
|
|
if (!region.do_remap_vmobject_page(page_index, with_flush))
|
2021-01-02 22:03:14 +03:00
|
|
|
success = false;
|
2021-07-25 18:11:50 +03:00
|
|
|
});
|
2021-01-02 22:03:14 +03:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-08-06 22:45:05 +03:00
|
|
|
void Region::unmap(ShouldDeallocateVirtualRange deallocate_range)
|
2019-11-03 22:37:03 +03:00
|
|
|
{
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!m_page_directory)
|
|
|
|
return;
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker page_lock(m_page_directory->get_lock());
|
|
|
|
SpinlockLocker lock(s_mm_lock);
|
2020-08-28 06:29:17 +03:00
|
|
|
size_t count = page_count();
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2020-07-06 21:47:08 +03:00
|
|
|
auto vaddr = vaddr_from_page_index(i);
|
2020-08-28 06:29:17 +03:00
|
|
|
MM.release_pte(*m_page_directory, vaddr, i == count - 1);
|
2019-11-03 22:37:03 +03:00
|
|
|
}
|
2021-10-02 09:45:15 +03:00
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr(), page_count());
|
2021-08-06 22:45:05 +03:00
|
|
|
if (deallocate_range == ShouldDeallocateVirtualRange::Yes) {
|
2021-08-06 22:35:56 +03:00
|
|
|
m_page_directory->range_allocator().deallocate(range());
|
2020-06-02 07:55:09 +03:00
|
|
|
}
|
2019-11-04 02:23:31 +03:00
|
|
|
m_page_directory = nullptr;
|
2019-11-03 22:37:03 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:29:31 +03:00
|
|
|
void Region::set_page_directory(PageDirectory& page_directory)
|
2019-11-03 22:37:03 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_page_directory || m_page_directory == &page_directory);
|
2021-08-29 21:10:24 +03:00
|
|
|
VERIFY(s_mm_lock.is_locked_by_current_processor());
|
2019-11-04 02:23:31 +03:00
|
|
|
m_page_directory = page_directory;
|
2020-01-10 00:29:31 +03:00
|
|
|
}
|
2020-07-06 21:47:08 +03:00
|
|
|
|
2021-09-06 13:52:23 +03:00
|
|
|
KResult Region::map(PageDirectory& page_directory, ShouldFlushTLB should_flush_tlb)
|
2020-01-10 00:29:31 +03:00
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker page_lock(page_directory.get_lock());
|
|
|
|
SpinlockLocker lock(s_mm_lock);
|
2021-01-26 19:05:36 +03:00
|
|
|
|
|
|
|
// FIXME: Find a better place for this sanity check(?)
|
2021-02-14 03:25:22 +03:00
|
|
|
if (is_user() && !is_shared()) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!vmobject().is_shared_inode());
|
2021-01-26 19:05:36 +03:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:47:08 +03:00
|
|
|
set_page_directory(page_directory);
|
2020-09-02 01:10:54 +03:00
|
|
|
size_t page_index = 0;
|
|
|
|
while (page_index < page_count()) {
|
|
|
|
if (!map_individual_page_impl(page_index))
|
|
|
|
break;
|
|
|
|
++page_index;
|
|
|
|
}
|
|
|
|
if (page_index > 0) {
|
2021-03-04 00:45:18 +03:00
|
|
|
if (should_flush_tlb == ShouldFlushTLB::Yes)
|
2021-10-02 09:45:15 +03:00
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr(), page_index);
|
2021-09-06 13:52:23 +03:00
|
|
|
if (page_index == page_count())
|
|
|
|
return KSuccess;
|
2020-09-02 01:10:54 +03:00
|
|
|
}
|
2021-09-06 13:52:23 +03:00
|
|
|
return ENOMEM;
|
2019-11-03 22:37:03 +03:00
|
|
|
}
|
2019-11-03 22:59:54 +03:00
|
|
|
|
|
|
|
void Region::remap()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_page_directory);
|
2021-08-24 22:53:47 +03:00
|
|
|
auto result = map(*m_page_directory);
|
2021-09-06 13:52:23 +03:00
|
|
|
if (result.is_error())
|
|
|
|
TODO();
|
2019-11-03 22:59:54 +03:00
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
PageFaultResponse Region::handle_fault(PageFault const& fault)
|
2019-11-04 02:45:33 +03:00
|
|
|
{
|
|
|
|
auto page_index_in_region = page_index_from_address(fault.vaddr());
|
|
|
|
if (fault.type() == PageFault::Type::PageNotPresent) {
|
2020-01-20 15:06:55 +03:00
|
|
|
if (fault.is_read() && !is_readable()) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("NP(non-readable) fault in Region({})[{}]", this, page_index_in_region);
|
2019-12-02 21:14:16 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2020-03-06 11:58:59 +03:00
|
|
|
if (fault.is_write() && !is_writable()) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("NP(non-writable) write fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2020-03-06 11:58:59 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
if (vmobject().is_inode()) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "NP(inode) fault in Region({})[{}]", this, page_index_in_region);
|
2021-07-23 03:40:16 +03:00
|
|
|
return handle_inode_fault(page_index_in_region);
|
2019-11-04 02:45:33 +03:00
|
|
|
}
|
2020-09-05 06:12:25 +03:00
|
|
|
|
|
|
|
auto& page_slot = physical_page_slot(page_index_in_region);
|
|
|
|
if (page_slot->is_lazy_committed_page()) {
|
2021-01-02 22:03:14 +03:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
2021-07-22 14:51:40 +03:00
|
|
|
VERIFY(m_vmobject->is_anonymous());
|
2021-07-25 02:46:44 +03:00
|
|
|
page_slot = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page({});
|
2021-08-24 22:53:47 +03:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
2020-09-05 06:12:25 +03:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("BUG! Unexpected NP fault at {}", fault.vaddr());
|
2020-03-07 12:27:02 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
2019-11-04 02:45:33 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(fault.type() == PageFault::Type::ProtectionViolation);
|
2019-12-02 21:20:09 +03:00
|
|
|
if (fault.access() == PageFault::Access::Write && is_writable() && should_cow(page_index_in_region)) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "PV(cow) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2020-09-05 06:12:25 +03:00
|
|
|
auto* phys_page = physical_page(page_index_in_region);
|
|
|
|
if (phys_page->is_shared_zero_page() || phys_page->is_lazy_committed_page()) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "NP(zero) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2021-07-23 03:40:16 +03:00
|
|
|
return handle_zero_fault(page_index_in_region);
|
2020-02-15 15:12:02 +03:00
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
return handle_cow_fault(page_index_in_region);
|
|
|
|
}
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("PV(error) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2019-11-04 02:45:33 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
PageFaultResponse Region::handle_zero_fault(size_t page_index_in_region)
|
2019-11-04 02:45:33 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
|
|
|
VERIFY(vmobject().is_anonymous());
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2020-04-28 17:19:50 +03:00
|
|
|
auto& page_slot = physical_page_slot(page_index_in_region);
|
2021-01-02 22:03:14 +03:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker locker(vmobject().m_lock);
|
2021-07-23 03:40:16 +03:00
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
if (!page_slot.is_null() && !page_slot->is_shared_zero_page() && !page_slot->is_lazy_committed_page()) {
|
2021-05-01 22:10:08 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "MM: zero_page() but page already present. Fine with me!");
|
2021-01-02 22:03:14 +03:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
2020-09-02 01:10:54 +03:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
2019-11-04 02:45:33 +03:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread != nullptr)
|
|
|
|
current_thread->did_zero_fault();
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2020-09-05 06:12:25 +03:00
|
|
|
if (page_slot->is_lazy_committed_page()) {
|
2021-07-22 14:51:40 +03:00
|
|
|
VERIFY(m_vmobject->is_anonymous());
|
2021-07-25 02:46:44 +03:00
|
|
|
page_slot = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page({});
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> ALLOCATED COMMITTED {}", page_slot->paddr());
|
2020-09-05 06:12:25 +03:00
|
|
|
} else {
|
|
|
|
page_slot = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
|
|
|
|
if (page_slot.is_null()) {
|
2021-03-09 23:30:35 +03:00
|
|
|
dmesgln("MM: handle_zero_fault was unable to allocate a physical page");
|
2020-09-05 06:12:25 +03:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> ALLOCATED {}", page_slot->paddr());
|
2020-09-06 00:52:14 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 22:03:14 +03:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject)) {
|
2021-03-09 23:30:35 +03:00
|
|
|
dmesgln("MM: handle_zero_fault was unable to allocate a page table to map {}", page_slot);
|
2020-09-02 01:10:54 +03:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
2019-11-04 02:45:33 +03:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageFaultResponse Region::handle_cow_fault(size_t page_index_in_region)
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread)
|
|
|
|
current_thread->did_cow_fault();
|
2019-11-04 02:45:33 +03:00
|
|
|
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!vmobject().is_anonymous())
|
|
|
|
return PageFaultResponse::ShouldCrash;
|
2020-09-12 06:11:07 +03:00
|
|
|
|
2021-01-02 22:03:14 +03:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
|
|
|
auto response = reinterpret_cast<AnonymousVMObject&>(vmobject()).handle_cow_fault(page_index_in_vmobject, vaddr().offset(page_index_in_region * PAGE_SIZE));
|
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
2020-09-02 01:10:54 +03:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
2020-09-06 00:52:14 +03:00
|
|
|
return response;
|
2019-11-04 02:45:33 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
PageFaultResponse Region::handle_inode_fault(size_t page_index_in_region)
|
2019-11-04 02:45:33 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
|
|
|
VERIFY(vmobject().is_inode());
|
2021-08-29 21:10:24 +03:00
|
|
|
VERIFY(!s_mm_lock.is_locked_by_current_processor());
|
|
|
|
VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
|
2021-01-23 19:11:45 +03:00
|
|
|
|
2020-02-28 22:20:35 +03:00
|
|
|
auto& inode_vmobject = static_cast<InodeVMObject&>(vmobject());
|
2021-07-23 03:40:16 +03:00
|
|
|
|
2021-01-02 22:03:14 +03:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
|
|
|
auto& vmobject_physical_page_entry = inode_vmobject.physical_pages()[page_index_in_vmobject];
|
2021-08-09 02:27:30 +03:00
|
|
|
|
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker locker(inode_vmobject.m_lock);
|
2021-08-09 02:27:30 +03:00
|
|
|
if (!vmobject_physical_page_entry.is_null()) {
|
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "handle_inode_fault: Page faulted in by someone else before reading, remapping.");
|
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 14:54:06 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "Inode fault in {} page index: {}", name(), page_index_in_region);
|
2019-12-29 15:16:53 +03:00
|
|
|
|
2020-06-29 00:34:31 +03:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread)
|
|
|
|
current_thread->did_inode_fault();
|
2019-11-04 02:45:33 +03:00
|
|
|
|
|
|
|
u8 page_buffer[PAGE_SIZE];
|
|
|
|
auto& inode = inode_vmobject.inode();
|
2021-01-23 19:11:45 +03:00
|
|
|
|
2021-07-23 03:40:16 +03:00
|
|
|
auto buffer = UserOrKernelBuffer::for_kernel_buffer(page_buffer);
|
|
|
|
auto result = inode.read_bytes(page_index_in_vmobject * PAGE_SIZE, PAGE_SIZE, buffer, nullptr);
|
2021-01-23 19:11:45 +03:00
|
|
|
|
2021-05-02 00:29:39 +03:00
|
|
|
if (result.is_error()) {
|
2021-07-23 03:40:16 +03:00
|
|
|
dmesgln("handle_inode_fault: Error ({}) while reading from inode", result.error());
|
2019-11-04 02:45:33 +03:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2021-07-23 03:40:16 +03:00
|
|
|
|
2021-05-02 00:29:39 +03:00
|
|
|
auto nread = result.value();
|
2019-11-04 02:45:33 +03:00
|
|
|
if (nread < PAGE_SIZE) {
|
|
|
|
// If we read less than a page, zero out the rest to avoid leaking uninitialized data.
|
|
|
|
memset(page_buffer + nread, 0, PAGE_SIZE - nread);
|
|
|
|
}
|
2020-12-01 05:04:36 +03:00
|
|
|
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker locker(inode_vmobject.m_lock);
|
2021-07-23 03:40:16 +03:00
|
|
|
|
|
|
|
if (!vmobject_physical_page_entry.is_null()) {
|
|
|
|
// Someone else faulted in this page while we were reading from the inode.
|
|
|
|
// No harm done (other than some duplicate work), remap the page here and return.
|
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "handle_inode_fault: Page faulted in by someone else, remapping.");
|
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
vmobject_physical_page_entry = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::No);
|
2021-07-23 03:40:16 +03:00
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
if (vmobject_physical_page_entry.is_null()) {
|
2021-03-09 23:30:35 +03:00
|
|
|
dmesgln("MM: handle_inode_fault was unable to allocate a physical page");
|
2020-05-06 22:11:38 +03:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
2019-11-04 02:45:33 +03:00
|
|
|
}
|
2019-12-21 18:21:13 +03:00
|
|
|
|
2021-08-22 13:57:58 +03:00
|
|
|
{
|
|
|
|
SpinlockLocker mm_locker(s_mm_lock);
|
|
|
|
u8* dest_ptr = MM.quickmap_page(*vmobject_physical_page_entry);
|
|
|
|
memcpy(dest_ptr, page_buffer, PAGE_SIZE);
|
|
|
|
MM.unquickmap_page();
|
|
|
|
}
|
2019-12-21 18:21:13 +03:00
|
|
|
|
2021-08-24 22:53:47 +03:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject))
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
|
2019-11-04 02:45:33 +03:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|