From 54783b86f163fbe5cfe3c95892b187a8a6a4813e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 1 Nov 2021 17:18:13 -0700 Subject: [PATCH] dmnt2: detect thread name, add monitor get mapping(s), increase buffer sizes --- .../source/kern_k_memory_block_manager.cpp | 3 ++- .../os_thread_manager_impl.os.horizon.cpp | 20 +++++++++++++++++++ .../osdbg_thread_local_region.os.horizon.hpp | 6 +++--- libstratosphere/source/osdbg/osdbg_thread.cpp | 4 ++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libmesosphere/source/kern_k_memory_block_manager.cpp b/libmesosphere/source/kern_k_memory_block_manager.cpp index c10643c1..ea0baf8a 100644 --- a/libmesosphere/source/kern_k_memory_block_manager.cpp +++ b/libmesosphere/source/kern_k_memory_block_manager.cpp @@ -19,7 +19,7 @@ namespace ams::kern { namespace { - constexpr std::tuple MemoryStateNames[] = { + constexpr const std::pair MemoryStateNames[] = { {KMemoryState_Free , "----- Free -----"}, {KMemoryState_Io , "Io "}, {KMemoryState_Static , "Static "}, @@ -41,6 +41,7 @@ namespace ams::kern { {KMemoryState_Kernel , "Kernel "}, {KMemoryState_GeneratedCode , "GeneratedCode "}, {KMemoryState_CodeOut , "CodeOut "}, + {KMemoryState_Coverage , "Coverage "}, }; constexpr const char *GetMemoryStateName(KMemoryState state) { diff --git a/libstratosphere/source/os/impl/os_thread_manager_impl.os.horizon.cpp b/libstratosphere/source/os/impl/os_thread_manager_impl.os.horizon.cpp index 986dbaf7..2bce07e5 100644 --- a/libstratosphere/source/os/impl/os_thread_manager_impl.os.horizon.cpp +++ b/libstratosphere/source/os/impl/os_thread_manager_impl.os.horizon.cpp @@ -52,6 +52,26 @@ namespace ams::os::impl { /* Get the thread impl object from libnx. */ ThreadImpl *thread_impl = ::threadGetSelf(); + /* Hack around libnx's main thread, to ensure stratosphere thread type consistency. */ + { + auto *tlr = reinterpret_cast(svc::GetThreadLocalRegion()); + for (size_t i = sizeof(svc::ThreadLocalRegion) / sizeof(uintptr_t); i > 0; --i) { + if (auto *candidate = reinterpret_cast(tlr[i - 1]); candidate == thread_impl) { + ThreadImpl *embedded_thread = std::addressof(main_thread->thread_impl_storage); + + *embedded_thread = *thread_impl; + + if (embedded_thread->next) { + embedded_thread->next->prev_next = std::addressof(embedded_thread->next); + } + + thread_impl = embedded_thread; + tlr[i-1] = reinterpret_cast(thread_impl); + break; + } + } + } + /* Get the thread priority. */ s32 horizon_priority; R_ABORT_UNLESS(svc::GetThreadPriority(std::addressof(horizon_priority), thread_impl->handle)); diff --git a/libstratosphere/source/osdbg/impl/osdbg_thread_local_region.os.horizon.hpp b/libstratosphere/source/osdbg/impl/osdbg_thread_local_region.os.horizon.hpp index c77f3222..b29d39ab 100644 --- a/libstratosphere/source/osdbg/impl/osdbg_thread_local_region.os.horizon.hpp +++ b/libstratosphere/source/osdbg/impl/osdbg_thread_local_region.os.horizon.hpp @@ -54,7 +54,7 @@ namespace ams::osdbg::impl { static_assert(AMS_OFFSETOF(ThreadLocalRegionIlp32, tls) == 0x1C0); struct LibnxThreadVars { - static constexpr u32 Magic = util::FourCC<'!','T','V','$'>::Code; + static constexpr u32 Magic = util::ReverseFourCC<'!','T','V','$'>::Code; u32 magic; ::Handle handle; @@ -69,11 +69,11 @@ namespace ams::osdbg::impl { volatile u16 disable_counter; volatile u16 interrupt_flag; u32 reserved0; - u64 tls[(0x1E0 - 0x108) / sizeof(u64)]; + u64 tls[(0x200 - sizeof(LibnxThreadVars) - 0x108) / sizeof(u64)]; LibnxThreadVars thread_vars; }; static_assert(sizeof(ThreadLocalRegionLibnx) == sizeof(svc::ThreadLocalRegion)); - static_assert(AMS_OFFSETOF(ThreadLocalRegionLibnx, thread_vars) == 0x1E0); + static_assert(AMS_OFFSETOF(ThreadLocalRegionLibnx, thread_vars) == 0x200 - sizeof(LibnxThreadVars)); struct LibnxThreadEntryArgs { u64 t; diff --git a/libstratosphere/source/osdbg/osdbg_thread.cpp b/libstratosphere/source/osdbg/osdbg_thread.cpp index 6eb28015..81f69ad5 100644 --- a/libstratosphere/source/osdbg/osdbg_thread.cpp +++ b/libstratosphere/source/osdbg/osdbg_thread.cpp @@ -50,9 +50,9 @@ namespace ams::osdbg { } else { /* Special-case libnx threads. */ if (thread_info->_thread_type_type == ThreadTypeType_Libnx) { - util::TSNPrintf(dst, os::ThreadNameLengthMax, "libnx Thread_0x%p", reinterpret_cast(thread_info->_thread_type)); + util::TSNPrintf(dst, os::ThreadNameLengthMax, "libnx Thread_%p", reinterpret_cast(thread_info->_thread_type)); } else { - util::TSNPrintf(dst, os::ThreadNameLengthMax, "Thread_0x%p", reinterpret_cast(thread_info->_thread_type)); + util::TSNPrintf(dst, os::ThreadNameLengthMax, "Thread_%p", reinterpret_cast(thread_info->_thread_type)); } return ResultSuccess();