kern: implement KMemoryBlockManager init

This commit is contained in:
Michael Scire 2020-02-10 01:50:23 -08:00
parent fdd7b1db15
commit 3c78bc4dbf
6 changed files with 176 additions and 4 deletions

View file

@ -0,0 +1,73 @@
/*
* 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 {
Result KMemoryBlockManager::Initialize(KProcessAddress st, KProcessAddress nd, KMemoryBlockSlabManager *slab_manager) {
/* Allocate a block to encapsulate the address space, insert it into the tree. */
KMemoryBlock *start_block = slab_manager->Allocate();
R_UNLESS(start_block != nullptr, svc::ResultOutOfResource());
/* Set our start and end. */
this->start = st;
this->end = nd;
MESOSPHERE_ASSERT(util::IsAligned(GetInteger(this->start), PageSize));
MESOSPHERE_ASSERT(util::IsAligned(GetInteger(this->end), PageSize));
/* Initialize and insert the block. */
start_block->Initialize(this->start, (this->end - this->start) / PageSize, KMemoryState_Free, KMemoryPermission_None, KMemoryAttribute_None);
this->memory_block_tree.insert(*start_block);
return ResultSuccess();
}
void KMemoryBlockManager::Finalize(KMemoryBlockSlabManager *slab_manager) {
/* Erase every block until we have none left. */
auto it = this->memory_block_tree.begin();
while (it != this->memory_block_tree.end()) {
KMemoryBlock *block = std::addressof(*it);
it = this->memory_block_tree.erase(it);
slab_manager->Free(block);
}
MESOSPHERE_ASSERT(this->memory_block_tree.empty());
}
/* Debug. */
bool KMemoryBlockManager::CheckState() const {
/* If we fail, we should dump blocks. */
auto dump_guard = SCOPE_GUARD { this->DumpBlocks(); };
/* Loop over every block, ensuring that we are sorted and coalesced. */
auto it = this->memory_block_tree.cbegin();
auto prev = it++;
while (it != this->memory_block_tree.cend()) {
MESOSPHERE_TODO("Validate KMemoryBlock Tree");
/* Advance the iterator. */
prev = it++;
}
/* We're valid, so no need to print. */
dump_guard.Cancel();
return true;
}
void KMemoryBlockManager::DumpBlocks() const {
MESOSPHERE_TODO("Dump useful debugging information");
}
}

View file

@ -58,13 +58,13 @@ namespace ams::kern {
this->impl.InitializeForKernel(table, start, end);
/* Initialize our memory block manager. */
MESOSPHERE_TODO("R_TRY(this->memory_block_manager.Initialize(this->address_space_start, this->address_space_end, this->GetMemoryBlockSlabManager()));");
return this->memory_block_manager.Initialize(this->address_space_start, this->address_space_end, this->memory_block_slab_manager);
return ResultSuccess();
}
void KPageTableBase::Finalize() {
MESOSPHERE_TODO("this->memory_block_manager.Finalize(this->GetMemoryBlockSlabManager());");
this->memory_block_manager.Finalize(this->memory_block_slab_manager);
MESOSPHERE_TODO("cpu::InvalidateEntireInstructionCache();");
}
}