diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_memory_block.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_memory_block.hpp
index c65c63254..23f00c364 100644
--- a/libraries/libmesosphere/include/mesosphere/kern_k_memory_block.hpp
+++ b/libraries/libmesosphere/include/mesosphere/kern_k_memory_block.hpp
@@ -176,12 +176,54 @@ namespace ams::kern {
KMemoryPermission perm;
KMemoryPermission original_perm;
KMemoryAttribute attribute;
+ public:
+ static constexpr ALWAYS_INLINE int Compare(const KMemoryBlock &lhs, const KMemoryBlock &rhs) {
+ if (lhs.GetAddress() < rhs.GetAddress()) {
+ return -1;
+ } else if (lhs.GetAddress() <= rhs.GetLastAddress()) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ public:
+ constexpr KProcessAddress GetAddress() const {
+ return this->address;
+ }
+
+ constexpr size_t GetNumPages() const {
+ return this->num_pages;
+ }
+
+ constexpr size_t GetSize() const {
+ return this->GetNumPages() * PageSize;
+ }
+
+ constexpr KProcessAddress GetEndAddress() const {
+ return this->GetAddress() + this->GetSize();
+ }
+
+ constexpr KProcessAddress GetLastAddress() const {
+ return this->GetEndAddress() - 1;
+ }
public:
constexpr KMemoryBlock()
: address(), num_pages(), memory_state(KMemoryState_None), ipc_lock_count(), device_use_count(), perm(), original_perm(), attribute()
{
/* ... */
}
+
+ constexpr void Initialize(KProcessAddress addr, size_t np, KMemoryState ms, KMemoryPermission p, KMemoryAttribute attr) {
+ MESOSPHERE_ASSERT_THIS();
+ this->address = addr;
+ this->num_pages = np;
+ this->memory_state = ms;
+ this->ipc_lock_count = 0;
+ this->device_use_count = 0;
+ this->perm = p;
+ this->original_perm = KMemoryPermission_None;
+ this->attribute = attr;
+ }
};
}
diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_memory_block_manager.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_memory_block_manager.hpp
new file mode 100644
index 000000000..ab44b9313
--- /dev/null
+++ b/libraries/libmesosphere/include/mesosphere/kern_k_memory_block_manager.hpp
@@ -0,0 +1,50 @@
+/*
+ * 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 .
+ */
+#pragma once
+#include
+#include
+#include
+
+namespace ams::kern {
+
+ class KMemoryBlockManager {
+ public:
+ using MemoryBlockTree = util::IntrusiveRedBlackTreeBaseTraits::TreeType;
+ private:
+ MemoryBlockTree memory_block_tree;
+ KProcessAddress start;
+ KProcessAddress end;
+ public:
+ constexpr KMemoryBlockManager() : memory_block_tree(), start(), end() { /* ... */ }
+
+ Result Initialize(KProcessAddress st, KProcessAddress nd, KMemoryBlockSlabManager *slab_manager);
+ void Finalize(KMemoryBlockSlabManager *slab_manager);
+
+ /* Debug. */
+ bool CheckState() const;
+ void DumpBlocks() const;
+ };
+
+ class KScopedMemoryBlockManagerVerifier {
+ private:
+ KMemoryBlockManager *manager;
+ public:
+ explicit ALWAYS_INLINE KScopedMemoryBlockManagerVerifier(KMemoryBlockManager *m) : manager(m) { MESOSPHERE_AUDIT(this->manager->CheckState()); }
+ explicit ALWAYS_INLINE KScopedMemoryBlockManagerVerifier(KMemoryBlockManager &m) : KScopedMemoryBlockManagerVerifier(std::addressof(m)) { /* ... */ }
+ ALWAYS_INLINE ~KScopedMemoryBlockManagerVerifier() { MESOSPHERE_AUDIT(this->manager->CheckState()); }
+ };
+
+}
diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp
index c4fa8b689..c5937a388 100644
--- a/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp
+++ b/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp
@@ -20,6 +20,7 @@
#include
#include
#include
+#include
namespace ams::kern {
@@ -54,7 +55,7 @@ namespace ams::kern {
mutable KLightLock general_lock;
mutable KLightLock map_physical_memory_lock;
KPageTableImpl impl;
- /* TODO KMemoryBlockManager memory_block_manager; */
+ KMemoryBlockManager memory_block_manager;
u32 allocate_option;
u32 address_space_size;
bool is_kernel;
@@ -72,7 +73,7 @@ namespace ams::kern {
address_space_start(), address_space_end(), heap_region_start(), heap_region_end(), current_heap_end(),
alias_region_start(), alias_region_end(), stack_region_start(), stack_region_end(), kernel_map_region_start(),
kernel_map_region_end(), alias_code_region_start(), alias_code_region_end(), code_region_start(), code_region_end(),
- max_heap_size(), max_physical_memory_size(), general_lock(), map_physical_memory_lock(), impl(), /* TODO: memory_block_manager(), */
+ max_heap_size(), max_physical_memory_size(), general_lock(), map_physical_memory_lock(), impl(), memory_block_manager(),
allocate_option(), address_space_size(), is_kernel(), enable_aslr(), memory_block_slab_manager(), block_info_manager(),
cached_physical_linear_region(), cached_physical_non_kernel_dram_region(), cached_virtual_managed_pool_dram_region(),
heap_fill_value(), ipc_fill_value(), stack_fill_value()
diff --git a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp
index 3bcdd4f24..2402b7d21 100644
--- a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp
+++ b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp
@@ -52,6 +52,12 @@ namespace ams::kern {
#define MESOSPHERE_ASSERT_THIS()
#endif
+#ifdef MESOSPHERE_BUILD_FOR_AUDITING
+#define MESOSPHERE_AUDIT(expr) MESOSPHERE_ASSERT(expr)
+#else
+#define MESOSPHERE_AUDIT(expr) do { static_cast(expr); } while (0)
+#endif
+
#define MESOSPHERE_TODO(arg) ({ constexpr const char *__mesosphere_todo = arg; MESOSPHERE_PANIC("TODO (%s): %s", __PRETTY_FUNCTION__, __mesosphere_todo); })
#define MESOSPHERE_TODO_IMPLEMENT() MESOSPHERE_TODO("Implement")
diff --git a/libraries/libmesosphere/source/kern_k_memory_block_manager.cpp b/libraries/libmesosphere/source/kern_k_memory_block_manager.cpp
new file mode 100644
index 000000000..9b438722c
--- /dev/null
+++ b/libraries/libmesosphere/source/kern_k_memory_block_manager.cpp
@@ -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 .
+ */
+#include
+
+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");
+ }
+}
diff --git a/libraries/libmesosphere/source/kern_k_page_table_base.cpp b/libraries/libmesosphere/source/kern_k_page_table_base.cpp
index 5d90f26fe..b2fc76ecb 100644
--- a/libraries/libmesosphere/source/kern_k_page_table_base.cpp
+++ b/libraries/libmesosphere/source/kern_k_page_table_base.cpp
@@ -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();");
}
}