2021-02-08 14:45:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-02-24 15:44:00 +00:00
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
2021-02-08 14:45:40 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-08 14:45:40 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 11:57:39 +00:00
|
|
|
#include <Kernel/Memory/AddressSpace.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/InodeVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2021-05-28 09:03:21 +00:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2021-02-08 14:45:40 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<AddressSpace>> AddressSpace::try_create(AddressSpace const* parent)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-09-05 13:13:20 +00:00
|
|
|
auto page_directory = TRY(PageDirectory::try_create_for_userspace(parent ? &parent->page_directory().range_allocator() : nullptr));
|
|
|
|
auto space = TRY(adopt_nonnull_own_or_enomem(new (nothrow) AddressSpace(page_directory)));
|
2021-02-08 14:45:40 +00:00
|
|
|
space->page_directory().set_space({}, *space);
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
2021-08-07 19:32:30 +00:00
|
|
|
AddressSpace::AddressSpace(NonnullRefPtr<PageDirectory> page_directory)
|
|
|
|
: m_page_directory(move(page_directory))
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
AddressSpace::~AddressSpace()
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> AddressSpace::unmap_mmap_range(VirtualAddress addr, size_t size)
|
2021-05-28 09:03:21 +00:00
|
|
|
{
|
|
|
|
if (!size)
|
|
|
|
return EINVAL;
|
|
|
|
|
2021-09-05 12:40:06 +00:00
|
|
|
auto range_to_unmap = TRY(VirtualRange::expand_to_page_boundaries(addr.get(), size));
|
2021-05-28 09:03:21 +00:00
|
|
|
|
|
|
|
if (!is_user_range(range_to_unmap))
|
|
|
|
return EFAULT;
|
|
|
|
|
|
|
|
if (auto* whole_region = find_region_from_range(range_to_unmap)) {
|
|
|
|
if (!whole_region->is_mmap())
|
|
|
|
return EPERM;
|
|
|
|
|
2021-08-19 19:45:07 +00:00
|
|
|
PerformanceManager::add_unmap_perf_event(Process::current(), whole_region->range());
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-07-17 13:07:02 +00:00
|
|
|
deallocate_region(*whole_region);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* old_region = find_region_containing(range_to_unmap)) {
|
|
|
|
if (!old_region->is_mmap())
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
// Remove the old region from our regions tree, since were going to add another region
|
2021-07-27 13:04:04 +00:00
|
|
|
// with the exact same start address, but don't deallocate it yet.
|
2021-05-28 09:03:21 +00:00
|
|
|
auto region = take_region(*old_region);
|
|
|
|
|
|
|
|
// We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
|
2021-08-06 19:45:05 +00:00
|
|
|
region->unmap(Region::ShouldDeallocateVirtualRange::No);
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-09-05 12:40:06 +00:00
|
|
|
auto new_regions = TRY(try_split_region_around_range(*region, range_to_unmap));
|
2021-05-28 09:03:21 +00:00
|
|
|
|
|
|
|
// Instead we give back the unwanted VM manually.
|
|
|
|
page_directory().range_allocator().deallocate(range_to_unmap);
|
|
|
|
|
|
|
|
// And finally we map the new region(s) using our page directory (they were just allocated and don't have one).
|
|
|
|
for (auto* new_region : new_regions) {
|
2021-08-24 19:53:47 +00:00
|
|
|
// TODO: Ideally we should do this in a way that can be rolled back on failure, as failing here
|
|
|
|
// leaves the caller in an undefined state.
|
2021-09-06 10:52:23 +00:00
|
|
|
TRY(new_region->map(page_directory()));
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:45:07 +00:00
|
|
|
PerformanceManager::add_unmap_perf_event(Process::current(), range_to_unmap);
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 13:04:04 +00:00
|
|
|
// Try again while checking multiple regions at a time.
|
|
|
|
auto const& regions = find_regions_intersecting(range_to_unmap);
|
2021-07-30 08:17:59 +00:00
|
|
|
if (regions.is_empty())
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-07-27 13:04:04 +00:00
|
|
|
// Check if any of the regions is not mmap'ed, to not accidentally
|
|
|
|
// error out with just half a region map left.
|
2021-05-28 09:03:21 +00:00
|
|
|
for (auto* region : regions) {
|
|
|
|
if (!region->is_mmap())
|
|
|
|
return EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Region*, 2> new_regions;
|
|
|
|
|
|
|
|
for (auto* old_region : regions) {
|
2021-07-27 13:04:04 +00:00
|
|
|
// If it's a full match we can remove the entire old region.
|
2021-05-28 09:03:21 +00:00
|
|
|
if (old_region->range().intersect(range_to_unmap).size() == old_region->size()) {
|
2021-07-17 13:07:02 +00:00
|
|
|
deallocate_region(*old_region);
|
2021-05-28 09:03:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old region from our regions tree, since were going to add another region
|
2021-07-27 13:04:04 +00:00
|
|
|
// with the exact same start address, but don't deallocate it yet.
|
2021-05-28 09:03:21 +00:00
|
|
|
auto region = take_region(*old_region);
|
|
|
|
|
|
|
|
// We manually unmap the old region here, specifying that we *don't* want the VM deallocated.
|
2021-08-06 19:45:05 +00:00
|
|
|
region->unmap(Region::ShouldDeallocateVirtualRange::No);
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-07-27 13:04:04 +00:00
|
|
|
// Otherwise, split the regions and collect them for future mapping.
|
2021-09-05 12:40:06 +00:00
|
|
|
auto split_regions = TRY(try_split_region_around_range(*region, range_to_unmap));
|
2021-11-10 10:55:37 +00:00
|
|
|
TRY(new_regions.try_extend(split_regions));
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
2021-07-27 13:04:04 +00:00
|
|
|
|
|
|
|
// Give back any unwanted VM to the range allocator.
|
2021-05-28 09:03:21 +00:00
|
|
|
page_directory().range_allocator().deallocate(range_to_unmap);
|
2021-07-27 13:04:04 +00:00
|
|
|
|
|
|
|
// And finally map the new region(s) into our page directory.
|
2021-05-28 09:03:21 +00:00
|
|
|
for (auto* new_region : new_regions) {
|
2021-08-24 19:53:47 +00:00
|
|
|
// TODO: Ideally we should do this in a way that can be rolled back on failure, as failing here
|
|
|
|
// leaves the caller in an undefined state.
|
2021-09-06 10:52:23 +00:00
|
|
|
TRY(new_region->map(page_directory()));
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:45:07 +00:00
|
|
|
PerformanceManager::add_unmap_perf_event(Process::current(), range_to_unmap);
|
2021-05-28 09:03:21 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-05-28 09:03:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<VirtualRange> AddressSpace::try_allocate_range(VirtualAddress vaddr, size_t size, size_t alignment)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
|
|
|
vaddr.mask(PAGE_MASK);
|
2021-02-14 08:57:19 +00:00
|
|
|
size = page_round_up(size);
|
2021-02-08 14:45:40 +00:00
|
|
|
if (vaddr.is_null())
|
2021-09-05 21:12:16 +00:00
|
|
|
return page_directory().range_allocator().try_allocate_anywhere(size, alignment);
|
|
|
|
return page_directory().range_allocator().try_allocate_specific(vaddr, size);
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Region*> AddressSpace::try_allocate_split_region(Region const& source_region, VirtualRange const& range, size_t offset_in_vmobject)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-09-06 17:24:54 +00:00
|
|
|
OwnPtr<KString> region_name;
|
|
|
|
if (!source_region.name().is_null())
|
|
|
|
region_name = TRY(KString::try_create(source_region.name()));
|
|
|
|
|
2021-09-05 23:59:30 +00:00
|
|
|
auto new_region = TRY(Region::try_create_user_accessible(
|
2021-09-06 17:24:54 +00:00
|
|
|
range, source_region.vmobject(), offset_in_vmobject, move(region_name), source_region.access(), source_region.is_cacheable() ? Region::Cacheable::Yes : Region::Cacheable::No, source_region.is_shared()));
|
2021-09-06 00:02:06 +00:00
|
|
|
auto* region = TRY(add_region(move(new_region)));
|
2021-07-14 22:18:00 +00:00
|
|
|
region->set_syscall_region(source_region.is_syscall_region());
|
|
|
|
region->set_mmap(source_region.is_mmap());
|
|
|
|
region->set_stack(source_region.is_stack());
|
2021-02-08 14:45:40 +00:00
|
|
|
size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
|
2021-07-14 22:18:00 +00:00
|
|
|
for (size_t i = 0; i < region->page_count(); ++i) {
|
2021-02-08 14:45:40 +00:00
|
|
|
if (source_region.should_cow(page_offset_in_source_region + i))
|
2021-07-14 22:18:00 +00:00
|
|
|
region->set_should_cow(i, true);
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
2021-07-14 22:18:00 +00:00
|
|
|
return region;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Region*> AddressSpace::allocate_region(VirtualRange const& range, StringView name, int prot, AllocationStrategy strategy)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(range.is_valid());
|
2021-09-06 17:24:54 +00:00
|
|
|
OwnPtr<KString> region_name;
|
|
|
|
if (!name.is_null())
|
|
|
|
region_name = TRY(KString::try_create(name));
|
2021-09-05 23:59:30 +00:00
|
|
|
auto vmobject = TRY(AnonymousVMObject::try_create_with_size(range.size(), strategy));
|
2021-09-06 17:24:54 +00:00
|
|
|
auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), 0, move(region_name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, false));
|
2021-09-06 10:52:23 +00:00
|
|
|
TRY(region->map(page_directory()));
|
2021-09-06 00:02:06 +00:00
|
|
|
return add_region(move(region));
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Region*> AddressSpace::allocate_region_with_vmobject(VirtualRange const& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, StringView name, int prot, bool shared)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(range.is_valid());
|
2021-02-08 14:45:40 +00:00
|
|
|
size_t end_in_vmobject = offset_in_vmobject + range.size();
|
|
|
|
if (end_in_vmobject <= offset_in_vmobject) {
|
|
|
|
dbgln("allocate_region_with_vmobject: Overflow (offset + size)");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (offset_in_vmobject >= vmobject->size()) {
|
|
|
|
dbgln("allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
if (end_in_vmobject > vmobject->size()) {
|
|
|
|
dbgln("allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.");
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
offset_in_vmobject &= PAGE_MASK;
|
2021-09-06 17:24:54 +00:00
|
|
|
OwnPtr<KString> region_name;
|
|
|
|
if (!name.is_null())
|
|
|
|
region_name = TRY(KString::try_create(name));
|
|
|
|
auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), offset_in_vmobject, move(region_name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, shared));
|
2021-09-06 00:02:06 +00:00
|
|
|
auto* added_region = TRY(add_region(move(region)));
|
2021-09-06 10:52:23 +00:00
|
|
|
TRY(added_region->map(page_directory()));
|
2021-07-14 22:18:00 +00:00
|
|
|
return added_region;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
void AddressSpace::deallocate_region(Region& region)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-12-02 12:52:09 +00:00
|
|
|
(void)take_region(region);
|
2021-04-06 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
NonnullOwnPtr<Region> AddressSpace::take_region(Region& region)
|
2021-04-06 23:17:05 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
|
|
|
if (m_region_lookup_cache.region.unsafe_ptr() == ®ion)
|
|
|
|
m_region_lookup_cache.region = nullptr;
|
2021-07-17 13:07:02 +00:00
|
|
|
|
|
|
|
auto found_region = m_regions.unsafe_remove(region.vaddr().get());
|
|
|
|
VERIFY(found_region.ptr() == ®ion);
|
|
|
|
return found_region;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
Region* AddressSpace::find_region_from_range(VirtualRange const& range)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 14:45:40 +00:00
|
|
|
if (m_region_lookup_cache.range.has_value() && m_region_lookup_cache.range.value() == range && m_region_lookup_cache.region)
|
|
|
|
return m_region_lookup_cache.region.unsafe_ptr();
|
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
auto found_region = m_regions.find(range.base().get());
|
|
|
|
if (!found_region)
|
|
|
|
return nullptr;
|
|
|
|
auto& region = *found_region;
|
2021-02-14 08:57:19 +00:00
|
|
|
size_t size = page_round_up(range.size());
|
2021-04-06 23:20:29 +00:00
|
|
|
if (region->size() != size)
|
|
|
|
return nullptr;
|
|
|
|
m_region_lookup_cache.range = range;
|
|
|
|
m_region_lookup_cache.region = *region;
|
|
|
|
return region;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
Region* AddressSpace::find_region_containing(VirtualRange const& range)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-04-06 23:20:29 +00:00
|
|
|
auto candidate = m_regions.find_largest_not_above(range.base().get());
|
|
|
|
if (!candidate)
|
|
|
|
return nullptr;
|
|
|
|
return (*candidate)->range().contains(range) ? candidate->ptr() : nullptr;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
Vector<Region*> AddressSpace::find_regions_intersecting(VirtualRange const& range)
|
2021-02-24 15:44:00 +00:00
|
|
|
{
|
|
|
|
Vector<Region*> regions = {};
|
|
|
|
size_t total_size_collected = 0;
|
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-24 15:44:00 +00:00
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
auto found_region = m_regions.find_largest_not_above(range.base().get());
|
|
|
|
if (!found_region)
|
|
|
|
return regions;
|
|
|
|
for (auto iter = m_regions.begin_from((*found_region)->vaddr().get()); !iter.is_end(); ++iter) {
|
2021-09-16 06:50:46 +00:00
|
|
|
const auto& iter_range = (*iter)->range();
|
|
|
|
if (iter_range.base() < range.end() && iter_range.end() > range.base()) {
|
2021-04-06 23:20:29 +00:00
|
|
|
regions.append(*iter);
|
|
|
|
|
2021-09-16 06:50:46 +00:00
|
|
|
total_size_collected += (*iter)->size() - iter_range.intersect(range).size();
|
2021-02-24 15:44:00 +00:00
|
|
|
if (total_size_collected == range.size())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return regions;
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Region*> AddressSpace::add_region(NonnullOwnPtr<Region> region)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
|
|
|
auto* ptr = region.ptr();
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-11-17 14:44:58 +00:00
|
|
|
TRY(m_regions.try_insert(region->vaddr().get(), move(region)));
|
2021-09-06 00:02:06 +00:00
|
|
|
return ptr;
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Carve out a virtual address range from a region and return the two regions on either side
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<Vector<Region*, 2>> AddressSpace::try_split_region_around_range(const Region& source_region, VirtualRange const& desired_range)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-08-06 11:54:48 +00:00
|
|
|
VirtualRange old_region_range = source_region.range();
|
2021-02-08 14:45:40 +00:00
|
|
|
auto remaining_ranges_after_unmap = old_region_range.carve(desired_range);
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!remaining_ranges_after_unmap.is_empty());
|
2021-11-07 23:51:39 +00:00
|
|
|
auto try_make_replacement_region = [&](VirtualRange const& new_range) -> ErrorOr<Region*> {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(old_region_range.contains(new_range));
|
2021-02-08 14:45:40 +00:00
|
|
|
size_t new_range_offset_in_vmobject = source_region.offset_in_vmobject() + (new_range.base().get() - old_region_range.base().get());
|
2021-07-11 16:51:54 +00:00
|
|
|
return try_allocate_split_region(source_region, new_range, new_range_offset_in_vmobject);
|
2021-02-08 14:45:40 +00:00
|
|
|
};
|
|
|
|
Vector<Region*, 2> new_regions;
|
|
|
|
for (auto& new_range : remaining_ranges_after_unmap) {
|
2021-09-05 12:40:06 +00:00
|
|
|
auto new_region = TRY(try_make_replacement_region(new_range));
|
|
|
|
new_regions.unchecked_append(new_region);
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|
|
|
|
return new_regions;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
void AddressSpace::dump_regions()
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-02-12 15:33:58 +00:00
|
|
|
dbgln("Process regions:");
|
2021-07-21 23:21:39 +00:00
|
|
|
#if ARCH(I386)
|
|
|
|
auto addr_padding = "";
|
|
|
|
#else
|
|
|
|
auto addr_padding = " ";
|
|
|
|
#endif
|
|
|
|
dbgln("BEGIN{} END{} SIZE{} ACCESS NAME",
|
|
|
|
addr_padding, addr_padding, addr_padding);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
for (auto& sorted_region : m_regions) {
|
2021-02-08 14:45:40 +00:00
|
|
|
auto& region = *sorted_region;
|
2021-07-21 17:53:38 +00:00
|
|
|
dbgln("{:p} -- {:p} {:p} {:c}{:c}{:c}{:c}{:c}{:c} {}", region.vaddr().get(), region.vaddr().offset(region.size() - 1).get(), region.size(),
|
2021-02-08 14:45:40 +00:00
|
|
|
region.is_readable() ? 'R' : ' ',
|
|
|
|
region.is_writable() ? 'W' : ' ',
|
|
|
|
region.is_executable() ? 'X' : ' ',
|
|
|
|
region.is_shared() ? 'S' : ' ',
|
|
|
|
region.is_stack() ? 'T' : ' ',
|
|
|
|
region.is_syscall_region() ? 'C' : ' ',
|
|
|
|
region.name());
|
|
|
|
}
|
|
|
|
MM.dump_kernel_regions();
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
void AddressSpace::remove_all_regions(Badge<Process>)
|
2021-02-08 14:45:40 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 14:45:40 +00:00
|
|
|
m_regions.clear();
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_dirty_private() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:11:10 +00:00
|
|
|
// FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
|
|
|
|
// The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
|
|
|
|
// That's probably a situation that needs to be looked at in general.
|
|
|
|
size_t amount = 0;
|
|
|
|
for (auto& region : m_regions) {
|
2021-04-06 23:20:29 +00:00
|
|
|
if (!region->is_shared())
|
|
|
|
amount += region->amount_dirty();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_clean_inode() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:11:10 +00:00
|
|
|
HashTable<const InodeVMObject*> vmobjects;
|
2021-02-08 21:18:26 +00:00
|
|
|
for (auto& region : m_regions) {
|
2021-04-06 23:20:29 +00:00
|
|
|
if (region->vmobject().is_inode())
|
|
|
|
vmobjects.set(&static_cast<const InodeVMObject&>(region->vmobject()));
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
size_t amount = 0;
|
|
|
|
for (auto& vmobject : vmobjects)
|
|
|
|
amount += vmobject->amount_clean();
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_virtual() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:18:26 +00:00
|
|
|
size_t amount = 0;
|
2021-02-08 21:11:10 +00:00
|
|
|
for (auto& region : m_regions) {
|
2021-04-06 23:20:29 +00:00
|
|
|
amount += region->size();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_resident() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:11:10 +00:00
|
|
|
// FIXME: This will double count if multiple regions use the same physical page.
|
|
|
|
size_t amount = 0;
|
|
|
|
for (auto& region : m_regions) {
|
2021-04-06 23:20:29 +00:00
|
|
|
amount += region->amount_resident();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_shared() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:11:10 +00:00
|
|
|
// FIXME: This will double count if multiple regions use the same physical page.
|
|
|
|
// FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
|
|
|
|
// and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
|
|
|
|
// so that every Region contributes +1 ref to each of its PhysicalPages.
|
|
|
|
size_t amount = 0;
|
|
|
|
for (auto& region : m_regions) {
|
2021-04-06 23:20:29 +00:00
|
|
|
amount += region->amount_shared();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_purgeable_volatile() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:18:26 +00:00
|
|
|
size_t amount = 0;
|
2021-02-08 21:11:10 +00:00
|
|
|
for (auto& region : m_regions) {
|
2021-07-24 23:46:44 +00:00
|
|
|
if (!region->vmobject().is_anonymous())
|
|
|
|
continue;
|
|
|
|
auto const& vmobject = static_cast<AnonymousVMObject const&>(region->vmobject());
|
|
|
|
if (vmobject.is_purgeable() && vmobject.is_volatile())
|
2021-04-06 23:20:29 +00:00
|
|
|
amount += region->amount_resident();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
size_t AddressSpace::amount_purgeable_nonvolatile() const
|
2021-02-08 21:11:10 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-08 21:18:26 +00:00
|
|
|
size_t amount = 0;
|
2021-02-08 21:11:10 +00:00
|
|
|
for (auto& region : m_regions) {
|
2021-07-24 23:46:44 +00:00
|
|
|
if (!region->vmobject().is_anonymous())
|
|
|
|
continue;
|
|
|
|
auto const& vmobject = static_cast<AnonymousVMObject const&>(region->vmobject());
|
|
|
|
if (vmobject.is_purgeable() && !vmobject.is_volatile())
|
2021-04-06 23:20:29 +00:00
|
|
|
amount += region->amount_resident();
|
2021-02-08 21:11:10 +00:00
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
}
|