From 5002b17c71334c89c28bcb0c3d1b3eb38bac6e69 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 1 Dec 2020 16:32:30 -0800 Subject: [PATCH] kern: add KAlpha/KBeta --- .../libmesosphere/include/mesosphere.hpp | 2 + .../mesosphere/init/kern_init_slab_setup.hpp | 2 + .../include/mesosphere/kern_k_alpha.hpp | 41 ++++++++++++++++ .../include/mesosphere/kern_k_beta.hpp | 48 +++++++++++++++++++ .../include/mesosphere/kern_k_class_token.hpp | 4 ++ .../include/mesosphere/kern_k_process.hpp | 3 ++ .../source/init/kern_init_slab_setup.cpp | 8 +++- .../libmesosphere/source/kern_k_process.cpp | 11 +++++ 8 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 libraries/libmesosphere/include/mesosphere/kern_k_alpha.hpp create mode 100644 libraries/libmesosphere/include/mesosphere/kern_k_beta.hpp diff --git a/libraries/libmesosphere/include/mesosphere.hpp b/libraries/libmesosphere/include/mesosphere.hpp index fd0a2d345..f5ff746ee 100644 --- a/libraries/libmesosphere/include/mesosphere.hpp +++ b/libraries/libmesosphere/include/mesosphere.hpp @@ -77,6 +77,8 @@ #include #include #include +#include +#include /* More Miscellaneous objects. */ #include diff --git a/libraries/libmesosphere/include/mesosphere/init/kern_init_slab_setup.hpp b/libraries/libmesosphere/include/mesosphere/init/kern_init_slab_setup.hpp index 8cfdd9f07..ab3a3ca8f 100644 --- a/libraries/libmesosphere/include/mesosphere/init/kern_init_slab_setup.hpp +++ b/libraries/libmesosphere/include/mesosphere/init/kern_init_slab_setup.hpp @@ -34,6 +34,8 @@ namespace ams::kern::init { size_t num_KObjectName; size_t num_KResourceLimit; size_t num_KDebug; + size_t num_KAlpha; + size_t num_KBeta; }; NOINLINE void InitializeSlabResourceCounts(); diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_alpha.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_alpha.hpp new file mode 100644 index 000000000..78711de56 --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/kern_k_alpha.hpp @@ -0,0 +1,41 @@ +/* + * 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 KAlpha final : public KAutoObjectWithSlabHeapAndContainer { + MESOSPHERE_AUTOOBJECT_TRAITS(KAlpha, KAutoObject); + private: + /* NOTE: Official KAlpha has size 0x50, corresponding to 0x20 bytes of fields. */ + /* TODO: Add these fields, if KAlpha is ever instantiable in the NX kernel. */ + public: + explicit KAlpha() { + /* ... */ + } + + virtual ~KAlpha() { /* ... */ } + + /* virtual void Finalize() override; */ + + virtual bool IsInitialized() const override { return false /* TODO */; } + static void PostDestroy(uintptr_t arg) { MESOSPHERE_UNUSED(arg); /* ... */ } + }; + +} diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_beta.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_beta.hpp new file mode 100644 index 000000000..f5fec3e88 --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/kern_k_beta.hpp @@ -0,0 +1,48 @@ +/* + * 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 KProcess; + + class KBeta final : public KAutoObjectWithSlabHeapAndContainer { + MESOSPHERE_AUTOOBJECT_TRAITS(KBeta, KAutoObject); + private: + friend class KProcess; + private: + /* NOTE: Official KBeta has size 0x88, corresponding to 0x58 bytes of fields. */ + /* TODO: Add these fields, if KBeta is ever instantiable in the NX kernel. */ + util::IntrusiveListNode process_list_node; + public: + explicit KBeta() + : process_list_node() + { + /* ... */ + } + + virtual ~KBeta() { /* ... */ } + + /* virtual void Finalize() override; */ + + virtual bool IsInitialized() const override { return false /* TODO */; } + static void PostDestroy(uintptr_t arg) { MESOSPHERE_UNUSED(arg); /* ... */ } + }; + +} diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_class_token.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_class_token.hpp index c37c88f1a..8e088559d 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_class_token.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_class_token.hpp @@ -112,6 +112,10 @@ namespace ams::kern { KSessionRequest, KCodeMemory, + /* NOTE: True order for these has not been determined yet. */ + KAlpha, + KBeta, + FinalClassesEnd = FinalClassesStart + NumFinalClasses, }; diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp index d48874471..accb4fe7b 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ namespace ams::kern { static constexpr size_t AslrAlignment = KernelAslrAlignment; private: using SharedMemoryInfoList = util::IntrusiveListBaseTraits::ListType; + using BetaList = util::IntrusiveListMemberTraits<&KBeta::process_list_node>::ListType; using TLPTree = util::IntrusiveRedBlackTreeBaseTraits::TreeType; using TLPIterator = TLPTree::iterator; private: @@ -95,6 +97,7 @@ namespace ams::kern { KThread *exception_thread{}; ThreadList thread_list{}; SharedMemoryInfoList shared_memory_list{}; + BetaList beta_list{}; bool is_suspended{}; bool is_jit_debug{}; ams::svc::DebugEvent jit_debug_event_type{}; diff --git a/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp b/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp index e336d3e1c..812623a24 100644 --- a/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp +++ b/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp @@ -39,7 +39,9 @@ namespace ams::kern::init { HANDLER(KObjectName, (SLAB_COUNT(KObjectName)), ## __VA_ARGS__) \ HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ## __VA_ARGS__) \ HANDLER(KEventInfo, (SLAB_COUNT(KThread) + SLAB_COUNT(KDebug)), ## __VA_ARGS__) \ - HANDLER(KDebug, (SLAB_COUNT(KDebug)), ## __VA_ARGS__) + HANDLER(KDebug, (SLAB_COUNT(KDebug)), ## __VA_ARGS__) \ + HANDLER(KAlpha, (SLAB_COUNT(KAlpha)), ## __VA_ARGS__) \ + HANDLER(KBeta, (SLAB_COUNT(KBeta)), ## __VA_ARGS__) namespace { @@ -68,6 +70,8 @@ namespace ams::kern::init { constexpr size_t SlabCountKObjectName = 7; constexpr size_t SlabCountKResourceLimit = 5; constexpr size_t SlabCountKDebug = cpu::NumCores; + constexpr size_t SlabCountKAlpha = 1; + constexpr size_t SlabCountKBeta = 6; constexpr size_t SlabCountExtraKThread = 160; @@ -94,6 +98,8 @@ namespace ams::kern::init { .num_KObjectName = SlabCountKObjectName, .num_KResourceLimit = SlabCountKResourceLimit, .num_KDebug = SlabCountKDebug, + .num_KAlpha = SlabCountKAlpha, + .num_KBeta = SlabCountKBeta, }; template diff --git a/libraries/libmesosphere/source/kern_k_process.cpp b/libraries/libmesosphere/source/kern_k_process.cpp index 7a01aad9b..a562b3ee3 100644 --- a/libraries/libmesosphere/source/kern_k_process.cpp +++ b/libraries/libmesosphere/source/kern_k_process.cpp @@ -129,6 +129,17 @@ namespace ams::kern { } } + /* Close all references to our betas. */ + { + auto it = this->beta_list.begin(); + while (it != this->beta_list.end()) { + KBeta *beta = std::addressof(*it); + it = this->beta_list.erase(it); + + beta->Close(); + } + } + /* Our thread local page list must be empty at this point. */ MESOSPHERE_ABORT_UNLESS(this->partially_used_tlp_tree.empty()); MESOSPHERE_ABORT_UNLESS(this->fully_used_tlp_tree.empty());