2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2022-08-22 15:23:32 +02:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2022-04-02 23:47:07 +01:00
|
|
|
#include <Kernel/Arch/PageDirectory.h>
|
|
|
|
#include <Kernel/Arch/PageFault.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2019-07-19 16:09:34 +02:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2022-10-05 19:27:36 +02:00
|
|
|
#include <Kernel/InterruptDisabler.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/Region.h>
|
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2021-02-14 09:03:54 +01:00
|
|
|
#include <Kernel/Panic.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/Process.h>
|
2022-01-29 13:08:37 +01:00
|
|
|
#include <Kernel/Scheduler.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/Thread.h>
|
|
|
|
|
2021-08-06 13:49:36 +02:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2022-04-03 15:27:47 +02:00
|
|
|
Region::Region()
|
|
|
|
: m_range(VirtualRange({}, 0))
|
2022-04-03 13:28:16 +02:00
|
|
|
{
|
2022-04-03 15:27:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
Region::Region(NonnullLockRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2022-04-03 15:27:47 +02:00
|
|
|
: m_range(VirtualRange({}, 0))
|
|
|
|
, m_offset_in_vmobject(offset_in_vmobject)
|
|
|
|
, m_vmobject(move(vmobject))
|
|
|
|
, m_name(move(name))
|
|
|
|
, m_access(access | ((access & 0x7) << 4))
|
|
|
|
, m_shared(shared)
|
|
|
|
, m_cacheable(cacheable == Cacheable::Yes)
|
|
|
|
{
|
|
|
|
m_vmobject->add_region(*this);
|
2022-04-03 13:28:16 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
Region::Region(VirtualRange const& range, NonnullLockRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2021-07-25 01:46:44 +02:00
|
|
|
: m_range(range)
|
2019-12-19 19:13:44 +01:00
|
|
|
, m_offset_in_vmobject(offset_in_vmobject)
|
|
|
|
, m_vmobject(move(vmobject))
|
2021-02-14 01:25:22 +01:00
|
|
|
, m_name(move(name))
|
2021-01-29 14:38:49 +01:00
|
|
|
, m_access(access | ((access & 0x7) << 4))
|
2021-01-02 16:38:05 +01:00
|
|
|
, m_shared(shared)
|
2021-02-14 01:25:22 +01:00
|
|
|
, m_cacheable(cacheable == Cacheable::Yes)
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_range.base().is_page_aligned());
|
|
|
|
VERIFY(m_range.size());
|
|
|
|
VERIFY((m_range.size() % PAGE_SIZE) == 0);
|
2021-02-13 01:16:57 +01:00
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
m_vmobject->add_region(*this);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Region::~Region()
|
|
|
|
{
|
2021-11-17 19:35:53 +01:00
|
|
|
if (is_writable() && vmobject().is_shared_inode()) {
|
|
|
|
// FIXME: This is very aggressive. Find a way to do less work!
|
|
|
|
(void)static_cast<SharedInodeVMObject&>(vmobject()).sync();
|
|
|
|
}
|
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
m_vmobject->remove_region(*this);
|
2020-09-02 22:57:09 -06:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
if (m_page_directory) {
|
2022-01-15 17:37:17 +01:00
|
|
|
SpinlockLocker pd_locker(m_page_directory->get_lock());
|
|
|
|
if (!is_readable() && !is_writable() && !is_executable()) {
|
2022-04-03 13:28:16 +02:00
|
|
|
// If the region is "PROT_NONE", we didn't map it in the first place.
|
2022-01-15 17:37:17 +01:00
|
|
|
} else {
|
2022-08-23 12:38:17 +02:00
|
|
|
unmap_with_locks_held(ShouldFlushTLB::Yes, pd_locker);
|
2022-01-15 17:37:17 +01:00
|
|
|
VERIFY(!m_page_directory);
|
|
|
|
}
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
2022-04-05 13:46:50 +02:00
|
|
|
|
|
|
|
if (is_kernel())
|
|
|
|
MM.unregister_kernel_region(*this);
|
2023-04-06 01:11:12 +03:00
|
|
|
|
|
|
|
// Extend the lifetime of the region if there are any page faults in progress for this region's pages.
|
|
|
|
// Both the removal of regions from the region trees and the fetching of the regions from the tree
|
|
|
|
// during the start of page fault handling are serialized under the address space spinlock. This means
|
|
|
|
// that once the region is removed no more page faults on this region can start, so this counter will
|
|
|
|
// eventually reach 0. And similarly since we can only reach the region destructor once the region was
|
|
|
|
// removed from the appropriate region tree, it is guaranteed that any page faults that are still being
|
|
|
|
// handled have already increased this counter, and will be allowed to finish before deallocation.
|
|
|
|
while (m_in_progress_page_faults)
|
|
|
|
Processor::wait_check();
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:27:47 +02:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> Region::create_unbacked()
|
|
|
|
{
|
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Region);
|
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> Region::create_unplaced(NonnullLockRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2022-04-03 13:28:16 +02:00
|
|
|
{
|
2022-04-03 17:12:39 +02:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Region(move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared));
|
2022-04-03 13:28:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> Region::try_clone()
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
VERIFY(Process::has_current());
|
2020-04-12 20:22:26 +02:00
|
|
|
|
2020-03-01 10:54:18 +01:00
|
|
|
if (m_shared) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_stack);
|
2020-03-01 11:08:28 +01:00
|
|
|
if (vmobject().is_inode())
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(vmobject().is_shared_inode());
|
2020-03-01 11:08:28 +01:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
// Create a new region backed by the same VMObject.
|
2021-09-06 19:24:54 +02:00
|
|
|
|
|
|
|
OwnPtr<KString> region_name;
|
|
|
|
if (m_name)
|
|
|
|
region_name = TRY(m_name->try_clone());
|
|
|
|
|
2021-09-05 16:06:37 +02:00
|
|
|
auto region = TRY(Region::try_create_user_accessible(
|
2022-04-03 13:28:16 +02:00
|
|
|
m_range, vmobject(), m_offset_in_vmobject, move(region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared));
|
2022-08-23 20:18:39 +02:00
|
|
|
region->set_mmap(m_mmap, m_mmapped_from_readable, m_mmapped_from_writable);
|
2020-02-08 02:39:46 +01:00
|
|
|
region->set_shared(m_shared);
|
2021-02-02 19:56:11 +01:00
|
|
|
region->set_syscall_region(is_syscall_region());
|
2020-01-10 19:24:01 +01:00
|
|
|
return region;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 11:08:28 +01:00
|
|
|
if (vmobject().is_inode())
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(vmobject().is_private_inode());
|
2020-03-01 11:08:28 +01:00
|
|
|
|
2021-09-05 16:06:37 +02:00
|
|
|
auto vmobject_clone = TRY(vmobject().try_clone());
|
2020-09-04 21:12:25 -06:00
|
|
|
|
2020-02-29 12:51:44 +01:00
|
|
|
// Set up a COW region. The parent (this) region becomes COW as well!
|
2022-01-15 17:48:42 +01:00
|
|
|
if (is_writable())
|
|
|
|
remap();
|
2021-09-06 19:24:54 +02:00
|
|
|
|
|
|
|
OwnPtr<KString> clone_region_name;
|
|
|
|
if (m_name)
|
|
|
|
clone_region_name = TRY(m_name->try_clone());
|
|
|
|
|
2021-09-05 16:06:37 +02:00
|
|
|
auto clone_region = TRY(Region::try_create_user_accessible(
|
2022-01-15 17:36:06 +01:00
|
|
|
m_range, move(vmobject_clone), m_offset_in_vmobject, move(clone_region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared));
|
2021-08-15 10:11:05 +00:00
|
|
|
|
2019-11-17 12:11:43 +01:00
|
|
|
if (m_stack) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(vmobject().is_anonymous());
|
2019-11-17 12:11:43 +01:00
|
|
|
clone_region->set_stack(true);
|
|
|
|
}
|
2021-02-02 19:56:11 +01:00
|
|
|
clone_region->set_syscall_region(is_syscall_region());
|
2022-08-23 20:18:39 +02:00
|
|
|
clone_region->set_mmap(m_mmap, m_mmapped_from_readable, m_mmapped_from_writable);
|
2019-10-01 19:58:41 +02:00
|
|
|
return clone_region;
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
void Region::set_vmobject(NonnullLockRefPtr<VMObject>&& obj)
|
2020-09-02 22:57:09 -06:00
|
|
|
{
|
|
|
|
if (m_vmobject.ptr() == obj.ptr())
|
|
|
|
return;
|
2021-07-23 02:40:16 +02:00
|
|
|
m_vmobject->remove_region(*this);
|
2020-09-02 22:57:09 -06:00
|
|
|
m_vmobject = move(obj);
|
2021-07-23 02:40:16 +02:00
|
|
|
m_vmobject->add_region(*this);
|
2020-09-02 22:57:09 -06:00
|
|
|
}
|
|
|
|
|
2020-09-05 15:52:14 -06:00
|
|
|
size_t Region::cow_pages() const
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2020-09-05 15:52:14 -06:00
|
|
|
if (!vmobject().is_anonymous())
|
2019-12-15 16:53:00 +01:00
|
|
|
return 0;
|
2021-07-22 14:25:41 +02:00
|
|
|
return static_cast<AnonymousVMObject const&>(vmobject()).cow_pages();
|
2019-12-15 16:53:00 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:28:32 +01:00
|
|
|
size_t Region::amount_dirty() const
|
|
|
|
{
|
|
|
|
if (!vmobject().is_inode())
|
|
|
|
return amount_resident();
|
2021-07-22 14:25:41 +02:00
|
|
|
return static_cast<InodeVMObject const&>(vmobject()).amount_dirty();
|
2019-12-29 12:28:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
size_t Region::amount_resident() const
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
for (size_t i = 0; i < page_count(); ++i) {
|
2022-08-18 19:20:33 +02:00
|
|
|
auto page = physical_page(i);
|
2021-01-02 00:47:55 +01:00
|
|
|
if (page && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
|
2019-04-03 15:13:07 +02: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) {
|
2022-08-18 19:20:33 +02:00
|
|
|
auto page = physical_page(i);
|
2021-01-02 00:47:55 +01:00
|
|
|
if (page && page->ref_count() > 1 && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
|
2019-04-03 15:13:07 +02:00
|
|
|
bytes += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
2019-07-19 16:09:34 +02:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
ErrorOr<NonnullOwnPtr<Region>> Region::try_create_user_accessible(VirtualRange const& range, NonnullLockRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
|
2019-07-19 16:09:34 +02:00
|
|
|
{
|
2021-08-15 10:11:05 +00:00
|
|
|
return adopt_nonnull_own_or_enomem(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared));
|
2019-07-19 16:09:34 +02:00
|
|
|
}
|
|
|
|
|
2019-10-01 19:58:41 +02:00
|
|
|
bool Region::should_cow(size_t page_index) const
|
|
|
|
{
|
2020-09-05 15:52:14 -06:00
|
|
|
if (!vmobject().is_anonymous())
|
2019-10-01 19:58:41 +02:00
|
|
|
return false;
|
2021-07-22 14:25:41 +02:00
|
|
|
return static_cast<AnonymousVMObject const&>(vmobject()).should_cow(first_page_index() + page_index, m_shared);
|
2019-10-01 19:58:41 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 19:17:27 +02:00
|
|
|
ErrorOr<void> Region::set_should_cow(size_t page_index, bool cow)
|
2019-10-01 19:58:41 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_shared);
|
2020-09-05 15:52:14 -06:00
|
|
|
if (vmobject().is_anonymous())
|
2022-02-10 19:17:27 +02:00
|
|
|
TRY(static_cast<AnonymousVMObject&>(vmobject()).set_should_cow(first_page_index() + page_index, cow));
|
|
|
|
return {};
|
2019-10-01 19:58:41 +02:00
|
|
|
}
|
2019-11-03 15:32:11 +01:00
|
|
|
|
2022-08-24 15:56:26 +02:00
|
|
|
bool Region::map_individual_page_impl(size_t page_index, RefPtr<PhysicalPage> page)
|
2019-11-03 15:32:11 +01:00
|
|
|
{
|
2021-08-29 20:10:24 +02:00
|
|
|
VERIFY(m_page_directory->get_lock().is_locked_by_current_processor());
|
2022-01-10 15:08:44 +01:00
|
|
|
|
2020-07-06 12:47:08 -06:00
|
|
|
auto page_vaddr = vaddr_from_page_index(page_index);
|
2021-02-14 09:03:54 +01:00
|
|
|
|
2021-12-19 19:36:42 +02:00
|
|
|
bool user_allowed = page_vaddr.get() >= USER_RANGE_BASE && is_user_address(page_vaddr);
|
2021-02-14 09:03:54 +01:00
|
|
|
if (is_mmap() && !user_allowed) {
|
|
|
|
PANIC("About to map mmap'ed page at a kernel address");
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:10:54 -06:00
|
|
|
auto* pte = MM.ensure_pte(*m_page_directory, page_vaddr);
|
2021-01-11 22:08:50 +01:00
|
|
|
if (!pte)
|
2020-09-01 16:10:54 -06:00
|
|
|
return false;
|
2022-08-18 18:54:36 +02:00
|
|
|
|
2020-04-28 16:19:50 +02:00
|
|
|
if (!page || (!is_readable() && !is_writable())) {
|
2020-09-01 16:10:54 -06:00
|
|
|
pte->clear();
|
2022-08-18 18:54:36 +02:00
|
|
|
return true;
|
2020-01-09 23:29:31 +02:00
|
|
|
}
|
2022-08-18 18:54:36 +02:00
|
|
|
|
|
|
|
pte->set_cache_disabled(!m_cacheable);
|
|
|
|
pte->set_physical_page_base(page->paddr().get());
|
|
|
|
pte->set_present(true);
|
|
|
|
if (page->is_shared_zero_page() || page->is_lazy_committed_page() || should_cow(page_index))
|
|
|
|
pte->set_writable(false);
|
|
|
|
else
|
|
|
|
pte->set_writable(is_writable());
|
|
|
|
if (Processor::current().has_nx())
|
|
|
|
pte->set_execute_disabled(!is_executable());
|
|
|
|
if (Processor::current().has_pat())
|
|
|
|
pte->set_pat(is_write_combine());
|
|
|
|
pte->set_user_allowed(user_allowed);
|
|
|
|
|
2020-09-01 16:10:54 -06:00
|
|
|
return true;
|
2019-11-03 15:32:11 +01:00
|
|
|
}
|
2019-11-03 20:37:03 +01:00
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
bool Region::map_individual_page_impl(size_t page_index)
|
|
|
|
{
|
2022-08-24 15:56:26 +02:00
|
|
|
RefPtr<PhysicalPage> page;
|
2022-08-18 18:54:36 +02:00
|
|
|
{
|
|
|
|
SpinlockLocker vmobject_locker(vmobject().m_lock);
|
|
|
|
page = physical_page(page_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map_individual_page_impl(page_index, page);
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:56:26 +02:00
|
|
|
bool Region::remap_vmobject_page(size_t page_index, NonnullRefPtr<PhysicalPage> physical_page)
|
2020-01-01 19:30:38 +01:00
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker page_lock(m_page_directory->get_lock());
|
2022-08-18 15:23:56 +02:00
|
|
|
|
|
|
|
// NOTE: `page_index` is a VMObject page index, so first we convert it to a Region page index.
|
|
|
|
if (!translate_vmobject_page(page_index))
|
|
|
|
return false;
|
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
bool success = map_individual_page_impl(page_index, physical_page);
|
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr_from_page_index(page_index));
|
2022-02-02 11:02:54 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2022-04-04 23:36:09 +02:00
|
|
|
void Region::unmap(ShouldFlushTLB should_flush_tlb)
|
2022-01-12 14:32:21 +01:00
|
|
|
{
|
|
|
|
if (!m_page_directory)
|
|
|
|
return;
|
|
|
|
SpinlockLocker pd_locker(m_page_directory->get_lock());
|
2022-08-23 12:38:17 +02:00
|
|
|
unmap_with_locks_held(should_flush_tlb, pd_locker);
|
2022-01-12 14:32:21 +01:00
|
|
|
}
|
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
void Region::unmap_with_locks_held(ShouldFlushTLB should_flush_tlb, SpinlockLocker<RecursiveSpinlock<LockRank::None>>&)
|
2019-11-03 20:37:03 +01:00
|
|
|
{
|
2020-09-05 15:52:14 -06:00
|
|
|
if (!m_page_directory)
|
|
|
|
return;
|
2020-08-27 21:29:17 -06:00
|
|
|
size_t count = page_count();
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2020-07-06 12:47:08 -06:00
|
|
|
auto vaddr = vaddr_from_page_index(i);
|
2021-12-19 01:15:12 +01:00
|
|
|
MM.release_pte(*m_page_directory, vaddr, i == count - 1 ? MemoryManager::IsLastPTERelease::Yes : MemoryManager::IsLastPTERelease::No);
|
2019-11-03 20:37:03 +01:00
|
|
|
}
|
2022-01-12 13:48:43 +01:00
|
|
|
if (should_flush_tlb == ShouldFlushTLB::Yes)
|
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr(), page_count());
|
2019-11-04 00:23:31 +01:00
|
|
|
m_page_directory = nullptr;
|
2019-11-03 20:37:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 23:29:31 +02:00
|
|
|
void Region::set_page_directory(PageDirectory& page_directory)
|
2019-11-03 20:37:03 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_page_directory || m_page_directory == &page_directory);
|
2019-11-04 00:23:31 +01:00
|
|
|
m_page_directory = page_directory;
|
2020-01-09 23:29:31 +02:00
|
|
|
}
|
2020-07-06 12:47:08 -06:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> Region::map(PageDirectory& page_directory, ShouldFlushTLB should_flush_tlb)
|
2020-01-09 23:29:31 +02:00
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker page_lock(page_directory.get_lock());
|
2021-01-26 17:05:36 +01:00
|
|
|
|
|
|
|
// FIXME: Find a better place for this sanity check(?)
|
2021-02-14 01:25:22 +01:00
|
|
|
if (is_user() && !is_shared()) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!vmobject().is_shared_inode());
|
2021-01-26 17:05:36 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 12:47:08 -06:00
|
|
|
set_page_directory(page_directory);
|
2020-09-01 16:10:54 -06: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-03 22:45:18 +01:00
|
|
|
if (should_flush_tlb == ShouldFlushTLB::Yes)
|
2021-10-01 23:45:15 -07:00
|
|
|
MemoryManager::flush_tlb(m_page_directory, vaddr(), page_index);
|
2021-09-06 12:52:23 +02:00
|
|
|
if (page_index == page_count())
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2020-09-01 16:10:54 -06:00
|
|
|
}
|
2021-09-06 12:52:23 +02:00
|
|
|
return ENOMEM;
|
2019-11-03 20:37:03 +01:00
|
|
|
}
|
2019-11-03 20:59:54 +01:00
|
|
|
|
|
|
|
void Region::remap()
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_page_directory);
|
2021-08-24 12:53:47 -07:00
|
|
|
auto result = map(*m_page_directory);
|
2021-09-06 12:52:23 +02:00
|
|
|
if (result.is_error())
|
|
|
|
TODO();
|
2019-11-03 20:59:54 +01:00
|
|
|
}
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2022-01-25 17:35:34 -07:00
|
|
|
ErrorOr<void> Region::set_write_combine(bool enable)
|
|
|
|
{
|
2022-04-02 23:47:07 +01:00
|
|
|
if (enable && !Processor::current().has_pat()) {
|
2022-01-25 17:35:34 -07:00
|
|
|
dbgln("PAT is not supported, implement MTRR fallback if available");
|
|
|
|
return Error::from_errno(ENOTSUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_write_combine = enable;
|
|
|
|
remap();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-29 21:18:59 +02:00
|
|
|
void Region::clear_to_zero()
|
|
|
|
{
|
|
|
|
VERIFY(vmobject().is_anonymous());
|
|
|
|
SpinlockLocker locker(vmobject().m_lock);
|
|
|
|
for (auto i = 0u; i < page_count(); ++i) {
|
2022-01-12 14:49:44 +01:00
|
|
|
auto& page = physical_page_slot(i);
|
2021-11-29 21:18:59 +02:00
|
|
|
VERIFY(page);
|
|
|
|
if (page->is_shared_zero_page())
|
|
|
|
continue;
|
|
|
|
page = MM.shared_zero_page();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 02:40:16 +02:00
|
|
|
PageFaultResponse Region::handle_fault(PageFault const& fault)
|
2019-11-04 00:45:33 +01:00
|
|
|
{
|
|
|
|
auto page_index_in_region = page_index_from_address(fault.vaddr());
|
|
|
|
if (fault.type() == PageFault::Type::PageNotPresent) {
|
2020-01-20 13:06:55 +01:00
|
|
|
if (fault.is_read() && !is_readable()) {
|
2021-01-10 16:21:56 +01:00
|
|
|
dbgln("NP(non-readable) fault in Region({})[{}]", this, page_index_in_region);
|
2019-12-02 19:14:16 +01:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2020-03-06 09:58:59 +01:00
|
|
|
if (fault.is_write() && !is_writable()) {
|
2021-01-10 16:21:56 +01:00
|
|
|
dbgln("NP(non-writable) write fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2020-03-06 09:58:59 +01:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2019-11-04 00:45:33 +01:00
|
|
|
if (vmobject().is_inode()) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "NP(inode) fault in Region({})[{}]", this, page_index_in_region);
|
2022-09-24 14:31:32 +03:00
|
|
|
return handle_inode_fault(page_index_in_region);
|
2019-11-04 00:45:33 +01:00
|
|
|
}
|
2020-09-04 21:12:25 -06:00
|
|
|
|
2022-08-18 19:20:33 +02:00
|
|
|
SpinlockLocker vmobject_locker(vmobject().m_lock);
|
2020-09-04 21:12:25 -06:00
|
|
|
auto& page_slot = physical_page_slot(page_index_in_region);
|
|
|
|
if (page_slot->is_lazy_committed_page()) {
|
2022-02-02 11:02:54 +01:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
2021-07-22 13:51:40 +02:00
|
|
|
VERIFY(m_vmobject->is_anonymous());
|
2021-07-25 01:46:44 +02:00
|
|
|
page_slot = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page({});
|
2022-08-18 18:54:36 +02:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject, *page_slot))
|
2021-08-24 12:53:47 -07:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
2020-09-04 21:12:25 -06:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2021-01-10 16:21:56 +01:00
|
|
|
dbgln("BUG! Unexpected NP fault at {}", fault.vaddr());
|
2022-04-04 16:52:32 +02:00
|
|
|
dbgln(" - Physical page slot pointer: {:p}", page_slot.ptr());
|
|
|
|
if (page_slot) {
|
|
|
|
dbgln(" - Physical page: {}", page_slot->paddr());
|
|
|
|
dbgln(" - Lazy committed: {}", page_slot->is_lazy_committed_page());
|
|
|
|
dbgln(" - Shared zero: {}", page_slot->is_shared_zero_page());
|
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
2019-11-04 00:45:33 +01:00
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(fault.type() == PageFault::Type::ProtectionViolation);
|
2019-12-02 19:20:09 +01:00
|
|
|
if (fault.access() == PageFault::Access::Write && is_writable() && should_cow(page_index_in_region)) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "PV(cow) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2022-08-18 19:20:33 +02:00
|
|
|
auto phys_page = physical_page(page_index_in_region);
|
2020-09-04 21:12:25 -06:00
|
|
|
if (phys_page->is_shared_zero_page() || phys_page->is_lazy_committed_page()) {
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "NP(zero) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2022-08-19 12:51:52 +02:00
|
|
|
return handle_zero_fault(page_index_in_region, *phys_page);
|
2020-02-15 13:12:02 +01:00
|
|
|
}
|
2019-11-04 00:45:33 +01:00
|
|
|
return handle_cow_fault(page_index_in_region);
|
|
|
|
}
|
2021-01-10 16:21:56 +01:00
|
|
|
dbgln("PV(error) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
|
2019-11-04 00:45:33 +01:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
|
|
|
|
2022-08-19 12:51:52 +02:00
|
|
|
PageFaultResponse Region::handle_zero_fault(size_t page_index_in_region, PhysicalPage& page_in_slot_at_time_of_fault)
|
2019-11-04 00:45:33 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(vmobject().is_anonymous());
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2022-02-02 11:02:54 +01:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2020-06-28 15:34:31 -06:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread != nullptr)
|
|
|
|
current_thread->did_zero_fault();
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2022-08-24 15:56:26 +02:00
|
|
|
RefPtr<PhysicalPage> new_physical_page;
|
2022-08-19 12:51:52 +02:00
|
|
|
|
|
|
|
if (page_in_slot_at_time_of_fault.is_lazy_committed_page()) {
|
2021-07-22 13:51:40 +02:00
|
|
|
VERIFY(m_vmobject->is_anonymous());
|
2022-08-19 12:51:52 +02:00
|
|
|
new_physical_page = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page({});
|
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> ALLOCATED COMMITTED {}", new_physical_page->paddr());
|
2020-09-04 21:12:25 -06:00
|
|
|
} else {
|
2022-07-14 15:27:22 +03:00
|
|
|
auto page_or_error = MM.allocate_physical_page(MemoryManager::ShouldZeroFill::Yes);
|
2022-01-28 16:36:53 +02:00
|
|
|
if (page_or_error.is_error()) {
|
2021-03-09 21:30:35 +01:00
|
|
|
dmesgln("MM: handle_zero_fault was unable to allocate a physical page");
|
2020-09-04 21:12:25 -06:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
2022-08-19 12:51:52 +02:00
|
|
|
new_physical_page = page_or_error.release_value();
|
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, " >> ALLOCATED {}", new_physical_page->paddr());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool already_handled = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
SpinlockLocker locker(vmobject().m_lock);
|
|
|
|
auto& page_slot = physical_page_slot(page_index_in_region);
|
|
|
|
already_handled = !page_slot.is_null() && !page_slot->is_shared_zero_page() && !page_slot->is_lazy_committed_page();
|
|
|
|
if (already_handled) {
|
|
|
|
// Someone else already faulted in a new page in this slot. That's fine, we'll just remap with their page.
|
|
|
|
new_physical_page = page_slot;
|
|
|
|
} else {
|
|
|
|
// Install the newly allocated page into the VMObject.
|
|
|
|
page_slot = new_physical_page;
|
|
|
|
}
|
2020-09-05 15:52:14 -06:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:51:52 +02:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject, *new_physical_page)) {
|
|
|
|
dmesgln("MM: handle_zero_fault was unable to allocate a page table to map {}", new_physical_page);
|
2020-09-01 16:10:54 -06:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
2019-11-04 00:45:33 +01:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageFaultResponse Region::handle_cow_fault(size_t page_index_in_region)
|
|
|
|
{
|
2020-06-28 15:34:31 -06:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread)
|
|
|
|
current_thread->did_cow_fault();
|
2019-11-04 00:45:33 +01:00
|
|
|
|
2020-09-05 15:52:14 -06:00
|
|
|
if (!vmobject().is_anonymous())
|
|
|
|
return PageFaultResponse::ShouldCrash;
|
2020-09-11 21:11:07 -06:00
|
|
|
|
2021-01-02 12:03:14 -07: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));
|
2022-08-18 18:54:36 +02:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject, *vmobject().physical_pages()[page_index_in_vmobject]))
|
2020-09-01 16:10:54 -06:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
2020-09-05 15:52:14 -06:00
|
|
|
return response;
|
2019-11-04 00:45:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-24 14:31:32 +03:00
|
|
|
PageFaultResponse Region::handle_inode_fault(size_t page_index_in_region)
|
2019-11-04 00:45:33 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(vmobject().is_inode());
|
2021-08-29 20:10:24 +02:00
|
|
|
VERIFY(!g_scheduler_lock.is_locked_by_current_processor());
|
2021-01-23 09:11:45 -07:00
|
|
|
|
2020-02-28 20:20:35 +01:00
|
|
|
auto& inode_vmobject = static_cast<InodeVMObject&>(vmobject());
|
2021-07-23 02:40:16 +02:00
|
|
|
|
2021-01-02 12:03:14 -07:00
|
|
|
auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
|
2022-08-18 14:43:36 +02:00
|
|
|
auto& vmobject_physical_page_slot = inode_vmobject.physical_pages()[page_index_in_vmobject];
|
2021-08-09 01:27:30 +02:00
|
|
|
|
|
|
|
{
|
2022-08-18 14:43:36 +02:00
|
|
|
// NOTE: The VMObject lock is required when manipulating the VMObject's physical page slot.
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker locker(inode_vmobject.m_lock);
|
2022-08-18 14:43:36 +02:00
|
|
|
if (!vmobject_physical_page_slot.is_null()) {
|
2021-08-09 01:27:30 +02:00
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "handle_inode_fault: Page faulted in by someone else before reading, remapping.");
|
2022-08-18 18:54:36 +02:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject, *vmobject_physical_page_slot))
|
2021-08-09 01:27:30 +02:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 12:54:06 +01:00
|
|
|
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(PAGE_FAULT_DEBUG, "Inode fault in {} page index: {}", name(), page_index_in_region);
|
2019-12-29 13:16:53 +01:00
|
|
|
|
2020-06-28 15:34:31 -06:00
|
|
|
auto current_thread = Thread::current();
|
|
|
|
if (current_thread)
|
|
|
|
current_thread->did_inode_fault();
|
2019-11-04 00:45:33 +01:00
|
|
|
|
|
|
|
u8 page_buffer[PAGE_SIZE];
|
|
|
|
auto& inode = inode_vmobject.inode();
|
2021-01-23 09:11:45 -07:00
|
|
|
|
2021-07-23 02:40:16 +02: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 09:11:45 -07:00
|
|
|
|
2021-05-01 14:29:39 -07:00
|
|
|
if (result.is_error()) {
|
2021-07-23 02:40:16 +02:00
|
|
|
dmesgln("handle_inode_fault: Error ({}) while reading from inode", result.error());
|
2019-11-04 00:45:33 +01:00
|
|
|
return PageFaultResponse::ShouldCrash;
|
|
|
|
}
|
2021-07-23 02:40:16 +02:00
|
|
|
|
2021-05-01 14:29:39 -07:00
|
|
|
auto nread = result.value();
|
2022-09-24 15:10:13 +03:00
|
|
|
// Note: If we received 0, it means we are at the end of file or after it,
|
|
|
|
// which means we should return bus error.
|
|
|
|
if (nread == 0)
|
|
|
|
return PageFaultResponse::BusError;
|
|
|
|
|
2019-11-04 00:45:33 +01: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-11-30 19:04:36 -07:00
|
|
|
|
2022-08-18 14:43:36 +02:00
|
|
|
// Allocate a new physical page, and copy the read inode contents into it.
|
|
|
|
auto new_physical_page_or_error = MM.allocate_physical_page(MemoryManager::ShouldZeroFill::No);
|
|
|
|
if (new_physical_page_or_error.is_error()) {
|
|
|
|
dmesgln("MM: handle_inode_fault was unable to allocate a physical page");
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
}
|
|
|
|
auto new_physical_page = new_physical_page_or_error.release_value();
|
|
|
|
{
|
2022-08-22 15:23:32 +02:00
|
|
|
InterruptDisabler disabler;
|
2022-08-18 14:43:36 +02:00
|
|
|
u8* dest_ptr = MM.quickmap_page(*new_physical_page);
|
|
|
|
memcpy(dest_ptr, page_buffer, PAGE_SIZE);
|
|
|
|
MM.unquickmap_page();
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
{
|
|
|
|
// NOTE: The VMObject lock is required when manipulating the VMObject's physical page slot.
|
|
|
|
SpinlockLocker locker(inode_vmobject.m_lock);
|
2021-07-23 02:40:16 +02:00
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
if (!vmobject_physical_page_slot.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, *vmobject_physical_page_slot))
|
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2021-07-23 02:40:16 +02:00
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
vmobject_physical_page_slot = new_physical_page;
|
|
|
|
}
|
2019-12-21 16:21:13 +01:00
|
|
|
|
2022-08-18 18:54:36 +02:00
|
|
|
if (!remap_vmobject_page(page_index_in_vmobject, *vmobject_physical_page_slot))
|
2021-08-24 12:53:47 -07:00
|
|
|
return PageFaultResponse::OutOfMemory;
|
|
|
|
|
2019-11-04 00:45:33 +01:00
|
|
|
return PageFaultResponse::Continue;
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2022-08-24 15:56:26 +02:00
|
|
|
RefPtr<PhysicalPage> Region::physical_page(size_t index) const
|
2022-08-18 18:52:04 +02:00
|
|
|
{
|
2022-08-18 19:20:33 +02:00
|
|
|
SpinlockLocker vmobject_locker(vmobject().m_lock);
|
2022-08-18 18:52:04 +02:00
|
|
|
VERIFY(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:56:26 +02:00
|
|
|
RefPtr<PhysicalPage>& Region::physical_page_slot(size_t index)
|
2022-08-18 18:52:04 +02:00
|
|
|
{
|
2022-08-18 19:20:33 +02:00
|
|
|
VERIFY(vmobject().m_lock.is_locked_by_current_processor());
|
2022-08-18 18:52:04 +02:00
|
|
|
VERIFY(index < page_count());
|
|
|
|
return vmobject().physical_pages()[first_page_index() + index];
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|