2020-01-29 09:49:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-02-07 01:40:57 +00:00
|
|
|
constexpr KMemoryManager::Pool GetPoolFromMemoryRegionType(u32 type) {
|
2020-12-02 01:03:00 +00:00
|
|
|
if ((type | KMemoryRegionType_VirtualDramApplicationPool) == type) {
|
|
|
|
return KMemoryManager::Pool_Application;
|
|
|
|
} else if ((type | KMemoryRegionType_VirtualDramAppletPool) == type) {
|
|
|
|
return KMemoryManager::Pool_Applet;
|
|
|
|
} else if ((type | KMemoryRegionType_VirtualDramSystemPool) == type) {
|
|
|
|
return KMemoryManager::Pool_System;
|
|
|
|
} else if ((type | KMemoryRegionType_VirtualDramSystemNonSecurePool) == type) {
|
|
|
|
return KMemoryManager::Pool_SystemNonSecure;
|
|
|
|
} else {
|
|
|
|
MESOSPHERE_PANIC("InvalidMemoryRegionType for conversion to Pool");
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 09:49:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-26 00:12:14 +01:00
|
|
|
void KMemoryManager::Initialize(KVirtualAddress management_region, size_t management_region_size) {
|
|
|
|
/* Clear the management region to zero. */
|
|
|
|
const KVirtualAddress management_region_end = management_region + management_region_size;
|
|
|
|
std::memset(GetVoidPointer(management_region), 0, management_region_size);
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
/* Traverse the virtual memory layout tree, initializing each manager as appropriate. */
|
2020-12-18 01:18:47 +00:00
|
|
|
while (m_num_managers != MaxManagerCount) {
|
2020-02-07 01:40:57 +00:00
|
|
|
/* Locate the region that should initialize the current manager. */
|
2020-12-02 01:03:00 +00:00
|
|
|
uintptr_t region_address = 0;
|
|
|
|
size_t region_size = 0;
|
|
|
|
Pool region_pool = Pool_Count;
|
2020-02-07 01:40:57 +00:00
|
|
|
for (const auto &it : KMemoryLayout::GetVirtualMemoryRegionTree()) {
|
|
|
|
/* We only care about regions that we need to create managers for. */
|
2020-08-23 21:19:45 +01:00
|
|
|
if (!it.IsDerivedFrom(KMemoryRegionType_VirtualDramUserPool)) {
|
2020-02-07 01:40:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We want to initialize the managers in order. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (it.GetAttributes() != m_num_managers) {
|
2020-02-07 01:40:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-02 01:03:00 +00:00
|
|
|
/* Validate the region. */
|
|
|
|
MESOSPHERE_ABORT_UNLESS(it.GetEndAddress() != 0);
|
|
|
|
MESOSPHERE_ASSERT(it.GetAddress() != Null<decltype(it.GetAddress())>);
|
|
|
|
MESOSPHERE_ASSERT(it.GetSize() > 0);
|
|
|
|
|
|
|
|
/* Update the region's extents. */
|
|
|
|
if (region_address == 0) {
|
|
|
|
region_address = it.GetAddress();
|
|
|
|
region_size = it.GetSize();
|
|
|
|
region_pool = GetPoolFromMemoryRegionType(it.GetType());
|
|
|
|
} else {
|
2020-12-02 01:03:42 +00:00
|
|
|
MESOSPHERE_ASSERT(it.GetAddress() == region_address + region_size);
|
2020-12-02 01:03:00 +00:00
|
|
|
|
|
|
|
/* Update the size. */
|
|
|
|
region_size = it.GetEndAddress() - region_address;
|
|
|
|
MESOSPHERE_ABORT_UNLESS(GetPoolFromMemoryRegionType(it.GetType()) == region_pool);
|
|
|
|
}
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 01:03:00 +00:00
|
|
|
/* If we didn't find a region, we're done. */
|
|
|
|
if (region_size == 0) {
|
2020-02-07 01:40:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a new manager for the region. */
|
2020-12-18 01:18:47 +00:00
|
|
|
Impl *manager = std::addressof(m_managers[m_num_managers++]);
|
|
|
|
MESOSPHERE_ABORT_UNLESS(m_num_managers <= util::size(m_managers));
|
2020-02-07 01:40:57 +00:00
|
|
|
|
2020-12-02 01:03:00 +00:00
|
|
|
const size_t cur_size = manager->Initialize(region_address, region_size, management_region, management_region_end, region_pool);
|
2020-08-26 00:12:14 +01:00
|
|
|
management_region += cur_size;
|
|
|
|
MESOSPHERE_ABORT_UNLESS(management_region <= management_region_end);
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
/* Insert the manager into the pool list. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_pool_managers_tail[region_pool] == nullptr) {
|
|
|
|
m_pool_managers_head[region_pool] = manager;
|
2020-02-07 01:40:57 +00:00
|
|
|
} else {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_pool_managers_tail[region_pool]->SetNext(manager);
|
|
|
|
manager->SetPrev(m_pool_managers_tail[region_pool]);
|
2020-12-02 01:03:00 +00:00
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
m_pool_managers_tail[region_pool] = manager;
|
2020-12-02 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free each region to its corresponding heap. */
|
|
|
|
for (const auto &it : KMemoryLayout::GetVirtualMemoryRegionTree()) {
|
|
|
|
if (it.IsDerivedFrom(KMemoryRegionType_VirtualDramUserPool)) {
|
|
|
|
/* Check the region. */
|
|
|
|
MESOSPHERE_ABORT_UNLESS(it.GetEndAddress() != 0);
|
|
|
|
|
|
|
|
/* Free the memory to the heap. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_managers[it.GetAttributes()].Free(it.GetAddress(), it.GetSize() / PageSize);
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
2020-12-02 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the used size for all managers. */
|
2020-12-18 01:18:47 +00:00
|
|
|
for (size_t i = 0; i < m_num_managers; ++i) {
|
|
|
|
m_managers[i].UpdateUsedHeapSize();
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 06:13:16 +01:00
|
|
|
Result KMemoryManager::InitializeOptimizedMemory(u64 process_id, Pool pool) {
|
|
|
|
/* Lock the pool. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[pool]);
|
2020-07-22 06:13:16 +01:00
|
|
|
|
|
|
|
/* Check that we don't already have an optimized process. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(!m_has_optimized_process[pool], svc::ResultBusy());
|
2020-07-22 06:13:16 +01:00
|
|
|
|
|
|
|
/* Set the optimized process id. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_optimized_process_ids[pool] = process_id;
|
|
|
|
m_has_optimized_process[pool] = true;
|
2020-07-22 06:13:16 +01:00
|
|
|
|
|
|
|
/* Clear the management area for the optimized process. */
|
|
|
|
for (auto *manager = this->GetFirstManager(pool, Direction_FromFront); manager != nullptr; manager = this->GetNextManager(manager, Direction_FromFront)) {
|
|
|
|
manager->InitializeOptimizedMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:44:15 +01:00
|
|
|
void KMemoryManager::FinalizeOptimizedMemory(u64 process_id, Pool pool) {
|
|
|
|
/* Lock the pool. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[pool]);
|
2020-07-25 04:44:15 +01:00
|
|
|
|
|
|
|
/* If the process was optimized, clear it. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_has_optimized_process[pool] && m_optimized_process_ids[pool] == process_id) {
|
|
|
|
m_has_optimized_process[pool] = false;
|
2020-07-25 04:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 06:13:16 +01:00
|
|
|
|
2020-12-01 14:01:44 +00:00
|
|
|
KVirtualAddress KMemoryManager::AllocateAndOpenContinuous(size_t num_pages, size_t align_pages, u32 option) {
|
2020-02-07 04:36:26 +00:00
|
|
|
/* Early return if we're allocating no pages. */
|
|
|
|
if (num_pages == 0) {
|
|
|
|
return Null<KVirtualAddress>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock the pool that we're allocating from. */
|
|
|
|
const auto [pool, dir] = DecodeOption(option);
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[pool]);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
|
|
|
/* Choose a heap based on our page size request. */
|
|
|
|
const s32 heap_index = KPageHeap::GetAlignedBlockIndex(num_pages, align_pages);
|
|
|
|
|
|
|
|
/* Loop, trying to iterate from each block. */
|
|
|
|
Impl *chosen_manager = nullptr;
|
|
|
|
KVirtualAddress allocated_block = Null<KVirtualAddress>;
|
2020-02-14 01:38:56 +00:00
|
|
|
for (chosen_manager = this->GetFirstManager(pool, dir); chosen_manager != nullptr; chosen_manager = this->GetNextManager(chosen_manager, dir)) {
|
2020-04-19 01:10:26 +01:00
|
|
|
allocated_block = chosen_manager->AllocateBlock(heap_index, true);
|
2020-02-14 01:38:56 +00:00
|
|
|
if (allocated_block != Null<KVirtualAddress>) {
|
|
|
|
break;
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we failed to allocate, quit now. */
|
|
|
|
if (allocated_block == Null<KVirtualAddress>) {
|
|
|
|
return Null<KVirtualAddress>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we allocated more than we need, free some. */
|
|
|
|
const size_t allocated_pages = KPageHeap::GetBlockNumPages(heap_index);
|
|
|
|
if (allocated_pages > num_pages) {
|
|
|
|
chosen_manager->Free(allocated_block + num_pages * PageSize, allocated_pages - num_pages);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Maintain the optimized memory bitmap, if we should. */
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_has_optimized_process[pool]) {
|
2020-07-24 16:07:34 +01:00
|
|
|
chosen_manager->TrackUnoptimizedAllocation(allocated_block, num_pages);
|
2020-02-07 04:36:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:01:44 +00:00
|
|
|
/* Open the first reference to the pages. */
|
|
|
|
chosen_manager->OpenFirst(allocated_block, num_pages);
|
|
|
|
|
2020-02-07 04:36:26 +00:00
|
|
|
return allocated_block;
|
|
|
|
}
|
|
|
|
|
2020-07-24 16:07:34 +01:00
|
|
|
Result KMemoryManager::AllocatePageGroupImpl(KPageGroup *out, size_t num_pages, Pool pool, Direction dir, bool unoptimized, bool random) {
|
2020-02-14 01:38:56 +00:00
|
|
|
/* Choose a heap based on our page size request. */
|
|
|
|
const s32 heap_index = KPageHeap::GetBlockIndex(num_pages);
|
|
|
|
R_UNLESS(0 <= heap_index, svc::ResultOutOfMemory());
|
|
|
|
|
|
|
|
/* Ensure that we don't leave anything un-freed. */
|
|
|
|
auto group_guard = SCOPE_GUARD {
|
|
|
|
for (const auto &it : *out) {
|
|
|
|
auto &manager = this->GetManager(it.GetAddress());
|
|
|
|
const size_t num_pages = std::min(it.GetNumPages(), (manager.GetEndAddress() - it.GetAddress()) / PageSize);
|
|
|
|
manager.Free(it.GetAddress(), num_pages);
|
|
|
|
}
|
|
|
|
out->Finalize();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Keep allocating until we've allocated all our pages. */
|
|
|
|
for (s32 index = heap_index; index >= 0 && num_pages > 0; index--) {
|
|
|
|
const size_t pages_per_alloc = KPageHeap::GetBlockNumPages(index);
|
|
|
|
for (Impl *cur_manager = this->GetFirstManager(pool, dir); cur_manager != nullptr; cur_manager = this->GetNextManager(cur_manager, dir)) {
|
|
|
|
while (num_pages >= pages_per_alloc) {
|
|
|
|
/* Allocate a block. */
|
2020-04-19 01:10:26 +01:00
|
|
|
KVirtualAddress allocated_block = cur_manager->AllocateBlock(index, random);
|
2020-02-14 01:38:56 +00:00
|
|
|
if (allocated_block == Null<KVirtualAddress>) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Safely add it to our group. */
|
|
|
|
{
|
|
|
|
auto block_guard = SCOPE_GUARD { cur_manager->Free(allocated_block, pages_per_alloc); };
|
|
|
|
R_TRY(out->AddBlock(allocated_block, pages_per_alloc));
|
|
|
|
block_guard.Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Maintain the optimized memory bitmap, if we should. */
|
2020-07-24 16:07:34 +01:00
|
|
|
if (unoptimized) {
|
|
|
|
cur_manager->TrackUnoptimizedAllocation(allocated_block, pages_per_alloc);
|
2020-02-14 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num_pages -= pages_per_alloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only succeed if we allocated as many pages as we wanted. */
|
|
|
|
R_UNLESS(num_pages == 0, svc::ResultOutOfMemory());
|
|
|
|
|
|
|
|
/* We succeeded! */
|
|
|
|
group_guard.Cancel();
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:01:44 +00:00
|
|
|
Result KMemoryManager::AllocateAndOpen(KPageGroup *out, size_t num_pages, u32 option) {
|
2020-04-19 01:10:26 +01:00
|
|
|
MESOSPHERE_ASSERT(out != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(out->GetNumPages() == 0);
|
|
|
|
|
|
|
|
/* Early return if we're allocating no pages. */
|
|
|
|
R_SUCCEED_IF(num_pages == 0);
|
|
|
|
|
|
|
|
/* Lock the pool that we're allocating from. */
|
|
|
|
const auto [pool, dir] = DecodeOption(option);
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[pool]);
|
2020-04-19 01:10:26 +01:00
|
|
|
|
|
|
|
/* Allocate the page group. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_TRY(this->AllocatePageGroupImpl(out, num_pages, pool, dir, m_has_optimized_process[pool], true));
|
2020-12-01 14:01:44 +00:00
|
|
|
|
|
|
|
/* Open the first reference to the pages. */
|
|
|
|
for (const auto &block : *out) {
|
|
|
|
KVirtualAddress cur_address = block.GetAddress();
|
|
|
|
size_t remaining_pages = block.GetNumPages();
|
|
|
|
while (remaining_pages > 0) {
|
|
|
|
/* Get the manager for the current address. */
|
|
|
|
auto &manager = this->GetManager(cur_address);
|
|
|
|
|
|
|
|
/* Process part or all of the block. */
|
|
|
|
const size_t cur_pages = std::min(remaining_pages, manager.GetPageOffsetToEnd(cur_address));
|
|
|
|
manager.OpenFirst(cur_address, cur_pages);
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
cur_address += cur_pages * PageSize;
|
|
|
|
remaining_pages -= cur_pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
2020-04-19 01:10:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:01:44 +00:00
|
|
|
Result KMemoryManager::AllocateAndOpenForProcess(KPageGroup *out, size_t num_pages, u32 option, u64 process_id, u8 fill_pattern) {
|
2020-07-24 16:07:34 +01:00
|
|
|
MESOSPHERE_ASSERT(out != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(out->GetNumPages() == 0);
|
|
|
|
|
|
|
|
/* Decode the option. */
|
|
|
|
const auto [pool, dir] = DecodeOption(option);
|
|
|
|
|
|
|
|
/* Allocate the memory. */
|
2020-08-03 20:28:14 +01:00
|
|
|
bool optimized;
|
2020-07-24 16:07:34 +01:00
|
|
|
{
|
|
|
|
/* Lock the pool that we're allocating from. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[pool]);
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Check if we have an optimized process. */
|
2020-12-18 01:18:47 +00:00
|
|
|
const bool has_optimized = m_has_optimized_process[pool];
|
|
|
|
const bool is_optimized = m_optimized_process_ids[pool] == process_id;
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Allocate the page group. */
|
|
|
|
R_TRY(this->AllocatePageGroupImpl(out, num_pages, pool, dir, has_optimized && !is_optimized, false));
|
2020-08-03 20:28:14 +01:00
|
|
|
|
|
|
|
/* Set whether we should optimize. */
|
|
|
|
optimized = has_optimized && is_optimized;
|
2020-12-01 14:01:44 +00:00
|
|
|
|
|
|
|
/* Open the first reference to the pages. */
|
|
|
|
for (const auto &block : *out) {
|
|
|
|
KVirtualAddress cur_address = block.GetAddress();
|
|
|
|
size_t remaining_pages = block.GetNumPages();
|
|
|
|
while (remaining_pages > 0) {
|
|
|
|
/* Get the manager for the current address. */
|
|
|
|
auto &manager = this->GetManager(cur_address);
|
|
|
|
|
|
|
|
/* Process part or all of the block. */
|
|
|
|
const size_t cur_pages = std::min(remaining_pages, manager.GetPageOffsetToEnd(cur_address));
|
|
|
|
manager.OpenFirst(cur_address, cur_pages);
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
cur_address += cur_pages * PageSize;
|
|
|
|
remaining_pages -= cur_pages;
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 16:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform optimized memory tracking, if we should. */
|
2020-08-03 20:28:14 +01:00
|
|
|
if (optimized) {
|
2020-07-24 16:07:34 +01:00
|
|
|
/* Iterate over the allocated blocks. */
|
|
|
|
for (const auto &block : *out) {
|
|
|
|
/* Get the block extents. */
|
|
|
|
const KVirtualAddress block_address = block.GetAddress();
|
|
|
|
const size_t block_pages = block.GetNumPages();
|
|
|
|
|
|
|
|
/* If it has no pages, we don't need to do anything. */
|
|
|
|
if (block_pages == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill all the pages that we need to fill. */
|
|
|
|
bool any_new = false;
|
|
|
|
{
|
|
|
|
KVirtualAddress cur_address = block_address;
|
2020-08-03 20:28:14 +01:00
|
|
|
size_t remaining_pages = block_pages;
|
|
|
|
while (remaining_pages > 0) {
|
2020-07-24 16:07:34 +01:00
|
|
|
/* Get the manager for the current address. */
|
|
|
|
auto &manager = this->GetManager(cur_address);
|
|
|
|
|
|
|
|
/* Process part or all of the block. */
|
2020-08-03 20:28:14 +01:00
|
|
|
const size_t cur_pages = std::min(remaining_pages, manager.GetPageOffsetToEnd(cur_address));
|
|
|
|
any_new = manager.ProcessOptimizedAllocation(cur_address, cur_pages, fill_pattern);
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Advance. */
|
2020-08-03 20:28:14 +01:00
|
|
|
cur_address += cur_pages * PageSize;
|
|
|
|
remaining_pages -= cur_pages;
|
2020-07-24 16:07:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
/* If there are new pages, update tracking for the allocation. */
|
|
|
|
if (any_new) {
|
|
|
|
/* Update tracking for the allocation. */
|
|
|
|
KVirtualAddress cur_address = block_address;
|
|
|
|
size_t remaining_pages = block_pages;
|
|
|
|
while (remaining_pages > 0) {
|
|
|
|
/* Get the manager for the current address. */
|
|
|
|
auto &manager = this->GetManager(cur_address);
|
2020-07-24 16:07:34 +01:00
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
/* Lock the pool for the manager. */
|
2020-12-18 01:18:47 +00:00
|
|
|
KScopedLightLock lk(m_pool_locks[manager.GetPool()]);
|
2020-07-24 16:07:34 +01:00
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
/* Track some or all of the current pages. */
|
|
|
|
const size_t cur_pages = std::min(remaining_pages, manager.GetPageOffsetToEnd(cur_address));
|
|
|
|
manager.TrackOptimizedAllocation(cur_address, cur_pages);
|
2020-07-24 16:07:34 +01:00
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
/* Advance. */
|
|
|
|
cur_address += cur_pages * PageSize;
|
|
|
|
remaining_pages -= cur_pages;
|
|
|
|
}
|
2020-07-24 16:07:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Set all the allocated memory. */
|
|
|
|
for (const auto &block : *out) {
|
|
|
|
std::memset(GetVoidPointer(block.GetAddress()), fill_pattern, block.GetSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-12-02 01:03:00 +00:00
|
|
|
size_t KMemoryManager::Impl::Initialize(uintptr_t address, size_t size, KVirtualAddress management, KVirtualAddress management_end, Pool p) {
|
2020-08-26 00:12:14 +01:00
|
|
|
/* Calculate management sizes. */
|
2020-12-02 01:03:00 +00:00
|
|
|
const size_t ref_count_size = (size / PageSize) * sizeof(u16);
|
|
|
|
const size_t optimize_map_size = CalculateOptimizedProcessOverheadSize(size);
|
2020-02-07 01:40:57 +00:00
|
|
|
const size_t manager_size = util::AlignUp(optimize_map_size + ref_count_size, PageSize);
|
2020-12-02 01:03:00 +00:00
|
|
|
const size_t page_heap_size = KPageHeap::CalculateManagementOverheadSize(size);
|
2020-08-26 00:12:14 +01:00
|
|
|
const size_t total_management_size = manager_size + page_heap_size;
|
|
|
|
MESOSPHERE_ABORT_UNLESS(manager_size <= total_management_size);
|
|
|
|
MESOSPHERE_ABORT_UNLESS(management + total_management_size <= management_end);
|
|
|
|
MESOSPHERE_ABORT_UNLESS(util::IsAligned(total_management_size, PageSize));
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
/* Setup region. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_pool = p;
|
|
|
|
m_management_region = management;
|
|
|
|
m_page_reference_counts = GetPointer<RefCount>(management + optimize_map_size);
|
|
|
|
MESOSPHERE_ABORT_UNLESS(util::IsAligned(GetInteger(m_management_region), PageSize));
|
2020-02-07 01:40:57 +00:00
|
|
|
|
|
|
|
/* Initialize the manager's KPageHeap. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_heap.Initialize(address, size, management + manager_size, page_heap_size);
|
2020-02-07 04:36:26 +00:00
|
|
|
|
2020-08-26 00:12:14 +01:00
|
|
|
return total_management_size;
|
2020-02-07 01:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 16:07:34 +01:00
|
|
|
void KMemoryManager::Impl::TrackUnoptimizedAllocation(KVirtualAddress block, size_t num_pages) {
|
2020-08-03 20:28:14 +01:00
|
|
|
/* Get the range we're tracking. */
|
|
|
|
size_t offset = this->GetPageOffset(block);
|
2020-02-07 04:36:26 +00:00
|
|
|
const size_t last = offset + num_pages - 1;
|
2020-08-03 20:28:14 +01:00
|
|
|
|
|
|
|
/* Track. */
|
2020-12-18 01:18:47 +00:00
|
|
|
u64 *optimize_map = GetPointer<u64>(m_management_region);
|
2020-02-07 04:36:26 +00:00
|
|
|
while (offset <= last) {
|
2020-08-03 20:28:14 +01:00
|
|
|
/* Mark the page as not being optimized-allocated. */
|
2020-02-07 04:36:26 +00:00
|
|
|
optimize_map[offset / BITSIZEOF(u64)] &= ~(u64(1) << (offset % BITSIZEOF(u64)));
|
2020-08-03 20:28:14 +01:00
|
|
|
|
2020-02-07 04:36:26 +00:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
void KMemoryManager::Impl::TrackOptimizedAllocation(KVirtualAddress block, size_t num_pages) {
|
2020-07-24 16:07:34 +01:00
|
|
|
/* Get the range we're tracking. */
|
2020-08-03 20:28:14 +01:00
|
|
|
size_t offset = this->GetPageOffset(block);
|
|
|
|
const size_t last = offset + num_pages - 1;
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Track. */
|
2020-12-18 01:18:47 +00:00
|
|
|
u64 *optimize_map = GetPointer<u64>(m_management_region);
|
2020-07-24 16:07:34 +01:00
|
|
|
while (offset <= last) {
|
|
|
|
/* Mark the page as being optimized-allocated. */
|
|
|
|
optimize_map[offset / BITSIZEOF(u64)] |= (u64(1) << (offset % BITSIZEOF(u64)));
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:28:14 +01:00
|
|
|
bool KMemoryManager::Impl::ProcessOptimizedAllocation(KVirtualAddress block, size_t num_pages, u8 fill_pattern) {
|
|
|
|
/* We want to return whether any pages were newly allocated. */
|
|
|
|
bool any_new = false;
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Get the range we're processing. */
|
2020-08-03 20:28:14 +01:00
|
|
|
size_t offset = this->GetPageOffset(block);
|
|
|
|
const size_t last = offset + num_pages - 1;
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Process. */
|
2020-12-18 01:18:47 +00:00
|
|
|
u64 *optimize_map = GetPointer<u64>(m_management_region);
|
2020-07-24 16:07:34 +01:00
|
|
|
while (offset <= last) {
|
|
|
|
/* Check if the page has been optimized-allocated before. */
|
|
|
|
if ((optimize_map[offset / BITSIZEOF(u64)] & (u64(1) << (offset % BITSIZEOF(u64)))) == 0) {
|
|
|
|
/* If not, it's new. */
|
2020-08-03 20:28:14 +01:00
|
|
|
any_new = true;
|
2020-07-24 16:07:34 +01:00
|
|
|
|
|
|
|
/* Fill the page. */
|
2020-12-18 01:18:47 +00:00
|
|
|
std::memset(GetVoidPointer(m_heap.GetAddress() + offset * PageSize), fill_pattern, PageSize);
|
2020-07-24 16:07:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of pages we processed. */
|
2020-08-03 20:28:14 +01:00
|
|
|
return any_new;
|
2020-07-24 16:07:34 +01:00
|
|
|
}
|
|
|
|
|
2020-08-26 00:12:14 +01:00
|
|
|
size_t KMemoryManager::Impl::CalculateManagementOverheadSize(size_t region_size) {
|
2020-02-07 01:40:57 +00:00
|
|
|
const size_t ref_count_size = (region_size / PageSize) * sizeof(u16);
|
|
|
|
const size_t optimize_map_size = (util::AlignUp((region_size / PageSize), BITSIZEOF(u64)) / BITSIZEOF(u64)) * sizeof(u64);
|
|
|
|
const size_t manager_meta_size = util::AlignUp(optimize_map_size + ref_count_size, PageSize);
|
2020-08-26 00:12:14 +01:00
|
|
|
const size_t page_heap_size = KPageHeap::CalculateManagementOverheadSize(region_size);
|
2020-02-07 01:40:57 +00:00
|
|
|
return manager_meta_size + page_heap_size;
|
2020-01-29 09:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|