diff --git a/libraries/libstratosphere/include/stratosphere.hpp b/libraries/libstratosphere/include/stratosphere.hpp index 8aa4b363a..a4c520b5e 100644 --- a/libraries/libstratosphere/include/stratosphere.hpp +++ b/libraries/libstratosphere/include/stratosphere.hpp @@ -19,6 +19,9 @@ /* libvapours (pulls in util, svc, results). */ #include +/* Libstratosphere definitions. */ +#include + /* Libstratosphere-only utility. */ #include diff --git a/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp b/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp new file mode 100644 index 000000000..c627462eb --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp @@ -0,0 +1,114 @@ +/* + * 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 + +namespace ams::impl { + + struct SystemThreadDefinition { + s32 priority; + const char *name; + }; + + #define AMS_DEFINE_SYSTEM_THREAD(__AMS_THREAD_PRIORITY__, __AMS_MODULE__, __AMS_THREAD_NAME__) \ + constexpr inline const ::ams::impl::SystemThreadDefinition SystemThreadDefinition##__AMS_MODULE__##__AMS_THREAD_NAME__ = { __AMS_THREAD_PRIORITY__, "ams." # __AMS_MODULE__ "." #__AMS_THREAD_NAME__ } + + /* sm. */ + AMS_DEFINE_SYSTEM_THREAD(-1, sm, Main); + + /* spl. */ + AMS_DEFINE_SYSTEM_THREAD(-1, spl, Main); + + /* Loader. */ + AMS_DEFINE_SYSTEM_THREAD(21, ldr, Main); + + /* Process Manager. */ + AMS_DEFINE_SYSTEM_THREAD(21, pm, Main); + AMS_DEFINE_SYSTEM_THREAD(21, pm, ProcessTrack); + + /* NCM. */ + AMS_DEFINE_SYSTEM_THREAD(21, ncm, MainWaitThreads); + AMS_DEFINE_SYSTEM_THREAD(21, ncm, ContentManagerServerIpcSession); + AMS_DEFINE_SYSTEM_THREAD(21, ncm, LocationResolverServerIpcSession); + + /* FS. */ + AMS_DEFINE_SYSTEM_THREAD(16, fs, WorkerThreadPool); + AMS_DEFINE_SYSTEM_THREAD(17, fs, Main); + AMS_DEFINE_SYSTEM_THREAD(17, fs, WorkerRealTimeAccess); + AMS_DEFINE_SYSTEM_THREAD(18, fs, WorkerNormalPriorityAccess); + AMS_DEFINE_SYSTEM_THREAD(19, fs, WorkerLowPriorityAccess); + AMS_DEFINE_SYSTEM_THREAD(30, fs, WorkerBackgroundAccess); + AMS_DEFINE_SYSTEM_THREAD(30, fs, PatrolReader); + + /* Boot. */ + AMS_DEFINE_SYSTEM_THREAD(-1, boot, Main); + + /* Mitm. */ + AMS_DEFINE_SYSTEM_THREAD(-7, mitm, InitializeThread); + AMS_DEFINE_SYSTEM_THREAD(-1, mitm_sf, QueryServerProcessThread); + AMS_DEFINE_SYSTEM_THREAD(16, mitm_fs, RomFileSystemInitializeThread); + AMS_DEFINE_SYSTEM_THREAD(21, mitm, DebugThrowThread); + + /* boot2. */ + AMS_DEFINE_SYSTEM_THREAD(20, boot2, Main); + + /* dmnt. */ + AMS_DEFINE_SYSTEM_THREAD(-3, dmnt, MultiCoreEventManager); + AMS_DEFINE_SYSTEM_THREAD(-1, dmnt, CheatDebugEvents); + AMS_DEFINE_SYSTEM_THREAD(-1, dmnt, MultiCoreBP); + AMS_DEFINE_SYSTEM_THREAD(11, dmnt, Main); + AMS_DEFINE_SYSTEM_THREAD(11, dmnt, Ipc); + AMS_DEFINE_SYSTEM_THREAD(11, dmnt, CheatDetect); + AMS_DEFINE_SYSTEM_THREAD(20, dmnt, CheatVirtualMachine); + + /* fatal */ + AMS_DEFINE_SYSTEM_THREAD(-13, fatal, Main); + AMS_DEFINE_SYSTEM_THREAD(-13, fatalsrv, FatalTaskThread); + AMS_DEFINE_SYSTEM_THREAD( 9, fatalsrv, IpcDispatcher); + + /* creport. */ + AMS_DEFINE_SYSTEM_THREAD(16, creport, Main); + + /* ro. */ + AMS_DEFINE_SYSTEM_THREAD(16, ro, Main); + + /* bpc. */ + AMS_DEFINE_SYSTEM_THREAD(4, bpc, IpcServer); + + /* hid. */ + AMS_DEFINE_SYSTEM_THREAD(-10, hid, IpcServer); + + /* ns.*/ + AMS_DEFINE_SYSTEM_THREAD(21, ns, ApplicationManagerIpcSession); + + /* settings. */ + AMS_DEFINE_SYSTEM_THREAD(21, settings, Main); + AMS_DEFINE_SYSTEM_THREAD(21, settings, IpcServer); + + /* erpt. */ + AMS_DEFINE_SYSTEM_THREAD(21, erpt, Main); + AMS_DEFINE_SYSTEM_THREAD(21, erpt, IpcServer); + + /* pgl. */ + AMS_DEFINE_SYSTEM_THREAD(21, pgl, Main); + AMS_DEFINE_SYSTEM_THREAD(21, pgl, ProcessControlTask); + + #undef AMS_DEFINE_SYSTEM_THREAD + +} + +#define AMS_GET_SYSTEM_THREAD_PRIORITY(__AMS_MODULE__, __AMS_THREAD_NAME__) ::ams::impl::SystemThreadDefinition##__AMS_MODULE__##__AMS_THREAD_NAME__.priority +#define AMS_GET_SYSTEM_THREAD_NAME(__AMS_MODULE__, __AMS_THREAD_NAME__) ::ams::impl::SystemThreadDefinition##__AMS_MODULE__##__AMS_THREAD_NAME__.name diff --git a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_thread_priority_changer.hpp b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_thread_priority_changer.hpp index 6b63537ca..6b545b221 100644 --- a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_thread_priority_changer.hpp +++ b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_thread_priority_changer.hpp @@ -26,14 +26,33 @@ namespace ams::fssystem { Relative, }; private: - /* TODO */ + os::ThreadType *thread; + s32 priority; public: - ALWAYS_INLINE explicit ScopedThreadPriorityChanger(s32 priority, Mode mode) { - /* TODO */ + ALWAYS_INLINE explicit ScopedThreadPriorityChanger(s32 prio, Mode mode) : thread(os::GetCurrentThread()), priority(0) { + const auto result_priority = std::min((mode == Mode::Relative) ? os::GetThreadPriority(this->thread) + priority : priority, os::LowestSystemThreadPriority); + this->priority = os::ChangeThreadPriority(thread, result_priority); } ALWAYS_INLINE ~ScopedThreadPriorityChanger() { - /* TODO */ + os::ChangeThreadPriority(this->thread, this->priority); } }; + + class ScopedThreadPriorityChangerByAccessPriority { + public: + enum class AccessMode { + Read, + Write, + }; + private: + static s32 GetThreadPriorityByAccessPriority(AccessMode mode); + private: + ScopedThreadPriorityChanger scoped_changer; + public: + ALWAYS_INLINE explicit ScopedThreadPriorityChangerByAccessPriority(AccessMode mode) : scoped_changer(GetThreadPriorityByAccessPriority(mode), ScopedThreadPriorityChanger::Mode::Absolute) { + /* ... */ + } + }; + } diff --git a/libraries/libstratosphere/source/erpt/srv/erpt_srv_service.cpp b/libraries/libstratosphere/source/erpt/srv/erpt_srv_service.cpp index a1d08a28c..b6fd8725e 100644 --- a/libraries/libstratosphere/source/erpt/srv/erpt_srv_service.cpp +++ b/libraries/libstratosphere/source/erpt/srv/erpt_srv_service.cpp @@ -34,8 +34,6 @@ namespace ams::erpt::srv { constexpr inline size_t ErrorReportContextSessions = 10; constexpr inline size_t ErrorReportMaxSessions = ErrorReportReportSessions + ErrorReportContextSessions; - constexpr inline s32 ErrorReportServerThreadPriority = 21; - constexpr inline sm::ServiceName ErrorReportContextServiceName = sm::ServiceName::Encode("erpt:c"); constexpr inline sm::ServiceName ErrorReportReportServiceName = sm::ServiceName::Encode("erpt:r"); @@ -64,7 +62,8 @@ namespace ams::erpt::srv { this->ResumeProcessing(); - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_server_thread_stack, sizeof(g_server_thread_stack), ErrorReportServerThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_server_thread_stack, sizeof(g_server_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(erpt, IpcServer))); + os::SetThreadNamePointer(std::addressof(this->thread), AMS_GET_SYSTEM_THREAD_NAME(erpt, IpcServer)); os::StartThread(std::addressof(this->thread)); diff --git a/libraries/libstratosphere/source/fssystem/fssystem_thread_priority_changer.cpp b/libraries/libstratosphere/source/fssystem/fssystem_thread_priority_changer.cpp new file mode 100644 index 000000000..b6338db95 --- /dev/null +++ b/libraries/libstratosphere/source/fssystem/fssystem_thread_priority_changer.cpp @@ -0,0 +1,25 @@ +/* + * 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::fssystem { + + s32 ScopedThreadPriorityChangerByAccessPriority::GetThreadPriorityByAccessPriority(AccessMode mode) { + /* TODO: Actually implement this for real. */ + return os::GetThreadPriority(os::GetCurrentThread()); + } + +} diff --git a/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell.cpp b/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell.cpp index 9529f26e0..9ffbb85a1 100644 --- a/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell.cpp +++ b/libraries/libstratosphere/source/pgl/srv/pgl_srv_shell.cpp @@ -43,7 +43,6 @@ namespace ams::pgl::srv { bool g_enable_jit_debug = false; constexpr inline size_t ProcessControlTaskStackSize = 8_KB; - constexpr inline s32 ProcessControlTaskPriority = 21; os::ThreadType g_process_control_task_thread; alignas(os::ThreadStackAlignment) u8 g_process_control_task_stack[ProcessControlTaskStackSize]; @@ -322,7 +321,8 @@ namespace ams::pgl::srv { void InitializeProcessControlTask() { /* Create the task thread. */ - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_process_control_task_thread), ProcessControlTask, nullptr, g_process_control_task_stack, sizeof(g_process_control_task_stack), ProcessControlTaskPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_process_control_task_thread), ProcessControlTask, nullptr, g_process_control_task_stack, sizeof(g_process_control_task_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(pgl, ProcessControlTask))); + os::SetThreadNamePointer(std::addressof(g_process_control_task_thread), AMS_GET_SYSTEM_THREAD_NAME(pgl, ProcessControlTask)); /* Retrieve settings. */ settings::fwdbg::GetSettingsItemValue(std::addressof(g_enable_jit_debug), sizeof(g_enable_jit_debug), "jit_debug", "enable_jit_debug"); diff --git a/libraries/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp b/libraries/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp index 85023c022..29b3e5c56 100644 --- a/libraries/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp +++ b/libraries/libstratosphere/source/sf/hipc/sf_hipc_mitm_query_api.cpp @@ -49,7 +49,6 @@ namespace ams::sf::hipc::impl { } constexpr size_t QueryServerProcessThreadStackSize = 0x4000; - constexpr s32 QueryServerProcessThreadPriority = -1; alignas(os::ThreadStackAlignment) u8 g_server_process_thread_stack[QueryServerProcessThreadStackSize]; os::ThreadType g_query_server_process_thread; @@ -70,7 +69,8 @@ namespace ams::sf::hipc::impl { R_ABORT_UNLESS(GetPointer(g_query_server_storage)->RegisterSession(query_handle, cmif::ServiceObjectHolder(std::make_shared(query_func)))); if (AMS_UNLIKELY(!g_registered_any)) { - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_query_server_process_thread), &QueryServerProcessThreadMain, GetPointer(g_query_server_storage), g_server_process_thread_stack, sizeof(g_server_process_thread_stack), QueryServerProcessThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_query_server_process_thread), &QueryServerProcessThreadMain, GetPointer(g_query_server_storage), g_server_process_thread_stack, sizeof(g_server_process_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(mitm_sf, QueryServerProcessThread))); + os::SetThreadNamePointer(std::addressof(g_query_server_process_thread), AMS_GET_SYSTEM_THREAD_NAME(mitm_sf, QueryServerProcessThread)); os::StartThread(std::addressof(g_query_server_process_thread)); g_registered_any = true; } diff --git a/stratosphere/ams_mitm/source/amsmitm_debug.cpp b/stratosphere/ams_mitm/source/amsmitm_debug.cpp index fd9a311c4..e4725ac80 100644 --- a/stratosphere/ams_mitm/source/amsmitm_debug.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_debug.cpp @@ -28,7 +28,6 @@ namespace ams::mitm { void DebugThrowThreadFunc(void *arg); constexpr size_t DebugThrowThreadStackSize = 0x4000; - constexpr int DebugThrowThreadPriority = 21; os::ThreadType g_debug_throw_thread; alignas(os::ThreadStackAlignment) u8 g_debug_throw_thread_stack[DebugThrowThreadStackSize]; @@ -50,7 +49,8 @@ namespace ams::mitm { g_throw_result = res; g_threw = true; - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_debug_throw_thread), DebugThrowThreadFunc, nullptr, g_debug_throw_thread_stack, sizeof(g_debug_throw_thread_stack), DebugThrowThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_debug_throw_thread), DebugThrowThreadFunc, nullptr, g_debug_throw_thread_stack, sizeof(g_debug_throw_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(mitm, DebugThrowThread))); + os::SetThreadNamePointer(std::addressof(g_debug_throw_thread), AMS_GET_SYSTEM_THREAD_NAME(mitm, DebugThrowThread)); os::StartThread(std::addressof(g_debug_throw_thread)); } diff --git a/stratosphere/ams_mitm/source/amsmitm_initialization.cpp b/stratosphere/ams_mitm/source/amsmitm_initialization.cpp index 012ea2598..ee4abd888 100644 --- a/stratosphere/ams_mitm/source/amsmitm_initialization.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_initialization.cpp @@ -50,7 +50,6 @@ namespace ams::mitm { void InitializeThreadFunc(void *arg); constexpr size_t InitializeThreadStackSize = 0x4000; - constexpr int InitializeThreadPriority = -7; /* Globals. */ os::Event g_init_event(os::EventClearMode_ManualClear); @@ -223,7 +222,8 @@ namespace ams::mitm { } void StartInitialize() { - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_initialize_thread), InitializeThreadFunc, nullptr, g_initialize_thread_stack, sizeof(g_initialize_thread_stack), InitializeThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_initialize_thread), InitializeThreadFunc, nullptr, g_initialize_thread_stack, sizeof(g_initialize_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(mitm, InitializeThread))); + os::SetThreadNamePointer(std::addressof(g_initialize_thread), AMS_GET_SYSTEM_THREAD_NAME(mitm, InitializeThread)); os::StartThread(std::addressof(g_initialize_thread)); } diff --git a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_module.hpp b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_module.hpp index 09e06742b..f3be48ff6 100644 --- a/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_module.hpp +++ b/stratosphere/ams_mitm/source/bpc_mitm/bpcmitm_module.hpp @@ -19,6 +19,6 @@ namespace ams::mitm::bpc { - DEFINE_MITM_MODULE_CLASS(0x8000, 4); + DEFINE_MITM_MODULE_CLASS(0x8000, AMS_GET_SYSTEM_THREAD_PRIORITY(bpc, IpcServer)); } diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_layered_romfs_storage.cpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_layered_romfs_storage.cpp index ef4c874b2..956c6b622 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_layered_romfs_storage.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_layered_romfs_storage.cpp @@ -39,7 +39,6 @@ namespace ams::mitm::fs { } constexpr size_t RomfsInitializerThreadStackSize = 0x8000; - constexpr int RomfsInitializerThreadPriority = 16; os::ThreadType g_romfs_initializer_thread; alignas(os::ThreadStackAlignment) u8 g_romfs_initializer_thread_stack[RomfsInitializerThreadStackSize]; @@ -47,7 +46,8 @@ namespace ams::mitm::fs { std::scoped_lock lk(g_mq_lock); if (AMS_UNLIKELY(!g_started_req_thread)) { - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_romfs_initializer_thread), RomfsInitializerThreadFunction, nullptr, g_romfs_initializer_thread_stack, sizeof(g_romfs_initializer_thread_stack), RomfsInitializerThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_romfs_initializer_thread), RomfsInitializerThreadFunction, nullptr, g_romfs_initializer_thread_stack, sizeof(g_romfs_initializer_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(mitm_fs, RomFileSystemInitializeThread))); + os::SetThreadNamePointer(std::addressof(g_romfs_initializer_thread), AMS_GET_SYSTEM_THREAD_NAME(mitm_fs, RomFileSystemInitializeThread)); os::StartThread(std::addressof(g_romfs_initializer_thread)); g_started_req_thread = true; } diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.hpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.hpp index fe1e03688..9bb43556c 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_module.hpp @@ -19,6 +19,6 @@ namespace ams::mitm::fs { - DEFINE_MITM_MODULE_CLASS(0x8000, 15); + DEFINE_MITM_MODULE_CLASS(0x8000, AMS_GET_SYSTEM_THREAD_PRIORITY(fs, WorkerThreadPool) - 1); } diff --git a/stratosphere/ams_mitm/source/hid_mitm/hidmitm_module.hpp b/stratosphere/ams_mitm/source/hid_mitm/hidmitm_module.hpp index b507755fe..025540e4a 100644 --- a/stratosphere/ams_mitm/source/hid_mitm/hidmitm_module.hpp +++ b/stratosphere/ams_mitm/source/hid_mitm/hidmitm_module.hpp @@ -19,6 +19,6 @@ namespace ams::mitm::hid { - DEFINE_MITM_MODULE_CLASS(0x8000, 19); + DEFINE_MITM_MODULE_CLASS(0x8000, AMS_GET_SYSTEM_THREAD_PRIORITY(hid, IpcServer)); } diff --git a/stratosphere/ams_mitm/source/ns_mitm/nsmitm_module.hpp b/stratosphere/ams_mitm/source/ns_mitm/nsmitm_module.hpp index 095bdd905..22fc2cc7e 100644 --- a/stratosphere/ams_mitm/source/ns_mitm/nsmitm_module.hpp +++ b/stratosphere/ams_mitm/source/ns_mitm/nsmitm_module.hpp @@ -19,6 +19,6 @@ namespace ams::mitm::ns { - DEFINE_MITM_MODULE_CLASS(0x4000, 20); + DEFINE_MITM_MODULE_CLASS(0x4000, AMS_GET_SYSTEM_THREAD_PRIORITY(ns, ApplicationManagerIpcSession) - 1); } diff --git a/stratosphere/ams_mitm/source/set_mitm/setmitm_module.hpp b/stratosphere/ams_mitm/source/set_mitm/setmitm_module.hpp index c76f37f23..5010fffa8 100644 --- a/stratosphere/ams_mitm/source/set_mitm/setmitm_module.hpp +++ b/stratosphere/ams_mitm/source/set_mitm/setmitm_module.hpp @@ -19,6 +19,6 @@ namespace ams::mitm::settings { - DEFINE_MITM_MODULE_CLASS(0x8000, 20); + DEFINE_MITM_MODULE_CLASS(0x8000, AMS_GET_SYSTEM_THREAD_PRIORITY(settings, IpcServer) - 1); } diff --git a/stratosphere/boot/source/boot_main.cpp b/stratosphere/boot/source/boot_main.cpp index 268cb81db..d9229c080 100644 --- a/stratosphere/boot/source/boot_main.cpp +++ b/stratosphere/boot/source/boot_main.cpp @@ -107,6 +107,10 @@ void __appExit(void) { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(boot, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(boot, Main)); + /* Change voltage from 3.3v to 1.8v for select devices. */ boot::ChangeGpioVoltageTo1_8v(); diff --git a/stratosphere/boot2/source/boot2_main.cpp b/stratosphere/boot2/source/boot2_main.cpp index 5c5114b8e..2fa6c3b43 100644 --- a/stratosphere/boot2/source/boot2_main.cpp +++ b/stratosphere/boot2/source/boot2_main.cpp @@ -96,6 +96,10 @@ void __appExit(void) { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(boot2, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(boot2, Main)); + /* Launch all programs off of SYSTEM/the SD. */ boot2::LaunchPostSdCardBootPrograms(); } diff --git a/stratosphere/creport/source/creport_main.cpp b/stratosphere/creport/source/creport_main.cpp index 19b0355ae..ae0c72373 100644 --- a/stratosphere/creport/source/creport_main.cpp +++ b/stratosphere/creport/source/creport_main.cpp @@ -85,6 +85,10 @@ void __appExit(void) { static creport::CrashReport g_crash_report; int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(creport, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(creport, Main)); + /* Validate arguments. */ if (argc < 2) { return EXIT_FAILURE; diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp index c6ca489de..02e336604 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_api.cpp @@ -29,9 +29,6 @@ namespace ams::dmnt::cheat::impl { class CheatProcessManager { private: static constexpr size_t ThreadStackSize = 0x4000; - static constexpr s32 DetectThreadPriority = 11; - static constexpr s32 VirtualMachineThreadPriority = 20; - static constexpr s32 DebugEventsThreadPriority = -1; private: os::Mutex cheat_lock; os::Event debug_events_event; /* Autoclear. */ @@ -200,9 +197,12 @@ namespace ams::dmnt::cheat::impl { } /* Spawn application detection thread, spawn cheat vm thread. */ - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->detect_thread), DetectLaunchThread, this, this->detect_thread_stack, ThreadStackSize, DetectThreadPriority)); - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->vm_thread), VirtualMachineThread, this, this->vm_thread_stack, ThreadStackSize, VirtualMachineThreadPriority)); - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->debug_events_thread), DebugEventsThread, this, this->debug_events_thread_stack, ThreadStackSize, DebugEventsThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->detect_thread), DetectLaunchThread, this, this->detect_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatDetect))); + os::SetThreadNamePointer(std::addressof(this->detect_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatDetect)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->vm_thread), VirtualMachineThread, this, this->vm_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatVirtualMachine))); + os::SetThreadNamePointer(std::addressof(this->vm_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatVirtualMachine)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->debug_events_thread), DebugEventsThread, this, this->debug_events_thread_stack, ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, CheatDebugEvents))); + os::SetThreadNamePointer(std::addressof(this->debug_events_thread), AMS_GET_SYSTEM_THREAD_NAME(dmnt, CheatDebugEvents)); /* Start threads. */ os::StartThread(std::addressof(this->detect_thread)); diff --git a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp index b69498c7a..54ad300c8 100644 --- a/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp +++ b/stratosphere/dmnt/source/cheat/impl/dmnt_cheat_debug_events_manager.cpp @@ -25,7 +25,6 @@ namespace ams::dmnt::cheat::impl { public: static constexpr size_t NumCores = 4; static constexpr size_t ThreadStackSize = os::MemoryPageSize; - static constexpr s32 ThreadPriority = -3; private: std::array message_queue_buffers; std::array message_queues; @@ -103,7 +102,8 @@ namespace ams::dmnt::cheat::impl { { for (size_t i = 0; i < NumCores; i++) { /* Create thread. */ - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->threads[i]), PerCoreThreadFunction, this, this->thread_stacks[i], ThreadStackSize, ThreadPriority, i)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->threads[i]), PerCoreThreadFunction, this, this->thread_stacks[i], ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, MultiCoreEventManager), i)); + os::SetThreadNamePointer(std::addressof(this->threads[i]), AMS_GET_SYSTEM_THREAD_NAME(dmnt, MultiCoreEventManager)); /* Set core mask. */ os::SetThreadCoreMask(std::addressof(this->threads[i]), i, (1u << i)); diff --git a/stratosphere/dmnt/source/dmnt_main.cpp b/stratosphere/dmnt/source/dmnt_main.cpp index 466a9682d..d321fbbde 100644 --- a/stratosphere/dmnt/source/dmnt_main.cpp +++ b/stratosphere/dmnt/source/dmnt_main.cpp @@ -129,6 +129,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(dmnt, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, Main)); + /* Initialize the cheat manager. */ ams::dmnt::cheat::impl::InitializeCheatManager(); @@ -143,9 +147,10 @@ int main(int argc, char **argv) /* Initialize threads. */ if constexpr (NumExtraThreads > 0) { - const s32 priority = os::GetThreadCurrentPriority(os::GetCurrentThread()); + static_assert(AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, Main) == AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, Ipc)); for (size_t i = 0; i < NumExtraThreads; i++) { - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_extra_threads[i]), LoopServerThread, nullptr, g_extra_thread_stacks[i], ThreadStackSize, priority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_extra_threads[i]), LoopServerThread, nullptr, g_extra_thread_stacks[i], ThreadStackSize, AMS_GET_SYSTEM_THREAD_PRIORITY(dmnt, Ipc))); + os::SetThreadNamePointer(std::addressof(g_extra_threads[i]), AMS_GET_SYSTEM_THREAD_NAME(dmnt, Ipc)); } } diff --git a/stratosphere/erpt/source/erpt_main.cpp b/stratosphere/erpt/source/erpt_main.cpp index 1914354e5..f777a23ab 100644 --- a/stratosphere/erpt/source/erpt_main.cpp +++ b/stratosphere/erpt/source/erpt_main.cpp @@ -120,6 +120,10 @@ namespace ams::erpt { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(erpt, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(erpt, Main)); + /* Set the memory heap for erpt::srv namespace. */ R_ABORT_UNLESS(erpt::srv::Initialize(erpt::g_memory_heap, erpt::MemoryHeapSize)); diff --git a/stratosphere/fatal/source/fatal_main.cpp b/stratosphere/fatal/source/fatal_main.cpp index 40aa49ede..af942d211 100644 --- a/stratosphere/fatal/source/fatal_main.cpp +++ b/stratosphere/fatal/source/fatal_main.cpp @@ -90,7 +90,7 @@ void __appInit(void) { R_ABORT_UNLESS(lblInitialize()); R_ABORT_UNLESS(psmInitialize()); R_ABORT_UNLESS(spsmInitialize()); - R_ABORT_UNLESS(plInitialize()); + R_ABORT_UNLESS(plInitialize(::PlServiceType_User)); R_ABORT_UNLESS(gpioInitialize()); R_ABORT_UNLESS(fsInitialize()); }); @@ -141,6 +141,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(fatal, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(fatal, Main)); + /* Load shared font. */ R_ABORT_UNLESS(fatal::srv::font::InitializeSharedFont()); diff --git a/stratosphere/fatal/source/fatal_task.cpp b/stratosphere/fatal/source/fatal_task.cpp index a98d87118..3354118d5 100644 --- a/stratosphere/fatal/source/fatal_task.cpp +++ b/stratosphere/fatal/source/fatal_task.cpp @@ -27,8 +27,6 @@ namespace ams::fatal::srv { class TaskThread { NON_COPYABLE(TaskThread); - private: - static constexpr s32 TaskThreadPriority = -13; private: os::ThreadType thread; private: @@ -42,7 +40,8 @@ namespace ams::fatal::srv { public: TaskThread() { /* ... */ } void StartTask(ITask *task) { - R_ABORT_UNLESS(os::CreateThread(std::addressof(this->thread), RunTaskImpl, task, task->GetStack(), task->GetStackSize(), TaskThreadPriority, 3)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(this->thread), RunTaskImpl, task, task->GetStack(), task->GetStackSize(), AMS_GET_SYSTEM_THREAD_PRIORITY(fatalsrv, FatalTaskThread), 3)); + os::SetThreadNamePointer(std::addressof(this->thread), AMS_GET_SYSTEM_THREAD_NAME(fatalsrv, FatalTaskThread)); os::StartThread(std::addressof(this->thread)); } }; diff --git a/stratosphere/loader/source/ldr_main.cpp b/stratosphere/loader/source/ldr_main.cpp index a4e3a8918..5d1c16ebf 100644 --- a/stratosphere/loader/source/ldr_main.cpp +++ b/stratosphere/loader/source/ldr_main.cpp @@ -115,6 +115,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(ldr, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(ldr, Main)); + /* Configure development. */ /* NOTE: Nintendo really does call the getter function three times instead of caching the value. */ ldr::SetDevelopmentForAcidProductionCheck(spl::IsDevelopmentHardware()); diff --git a/stratosphere/ncm/source/ncm_main.cpp b/stratosphere/ncm/source/ncm_main.cpp index d36b78bf1..7b6465655 100644 --- a/stratosphere/ncm/source/ncm_main.cpp +++ b/stratosphere/ncm/source/ncm_main.cpp @@ -174,7 +174,8 @@ namespace { } ams::Result StartThreads() { - R_TRY(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_content_manager_thread_stack, sizeof(g_content_manager_thread_stack), 21)); + R_TRY(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_content_manager_thread_stack, sizeof(g_content_manager_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(ncm, ContentManagerServerIpcSession))); + os::SetThreadNamePointer(std::addressof(this->thread), AMS_GET_SYSTEM_THREAD_NAME(ncm, ContentManagerServerIpcSession)); os::StartThread(std::addressof(this->thread)); return ResultSuccess(); } @@ -219,7 +220,8 @@ namespace { } ams::Result StartThreads() { - R_TRY(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_location_resolver_thread_stack, sizeof(g_location_resolver_thread_stack), 21)); + R_TRY(os::CreateThread(std::addressof(this->thread), ThreadFunction, this, g_location_resolver_thread_stack, sizeof(g_location_resolver_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(ncm, LocationResolverServerIpcSession))); + os::SetThreadNamePointer(std::addressof(this->thread), AMS_GET_SYSTEM_THREAD_NAME(ncm, LocationResolverServerIpcSession)); os::StartThread(std::addressof(this->thread)); return ResultSuccess(); } @@ -260,6 +262,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(ncm, MainWaitThreads)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(ncm, MainWaitThreads)); + /* Create and initialize the content manager. */ auto content_manager = GetSharedPointerToContentManager(); R_ABORT_UNLESS(content_manager->Initialize(ManagerConfig)); diff --git a/stratosphere/pgl/source/pgl_main.cpp b/stratosphere/pgl/source/pgl_main.cpp index a69920b3a..03a75f246 100644 --- a/stratosphere/pgl/source/pgl_main.cpp +++ b/stratosphere/pgl/source/pgl_main.cpp @@ -145,6 +145,10 @@ void __appExit(void) { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(pgl, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(pgl, Main)); + /* Register the pgl service. */ pgl::RegisterServiceSession(); diff --git a/stratosphere/pm/source/impl/pm_process_manager.cpp b/stratosphere/pm/source/impl/pm_process_manager.cpp index bc8892e49..3d761ca9d 100644 --- a/stratosphere/pm/source/impl/pm_process_manager.cpp +++ b/stratosphere/pm/source/impl/pm_process_manager.cpp @@ -117,7 +117,6 @@ namespace ams::pm::impl { /* Process Tracking globals. */ void ProcessTrackingMain(void *arg); - constexpr int ProcessTrackThreadPriority = 21; os::ThreadType g_process_track_thread; alignas(os::ThreadStackAlignment) u8 g_process_track_thread_stack[16_KB]; @@ -405,7 +404,8 @@ namespace ams::pm::impl { R_TRY(resource::InitializeResourceManager()); /* Create thread. */ - R_ABORT_UNLESS(os::CreateThread(std::addressof(g_process_track_thread), ProcessTrackingMain, nullptr, g_process_track_thread_stack, sizeof(g_process_track_thread_stack), ProcessTrackThreadPriority)); + R_ABORT_UNLESS(os::CreateThread(std::addressof(g_process_track_thread), ProcessTrackingMain, nullptr, g_process_track_thread_stack, sizeof(g_process_track_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(pm, ProcessTrack))); + os::SetThreadNamePointer(std::addressof(g_process_track_thread), AMS_GET_SYSTEM_THREAD_NAME(pm, ProcessTrack)); /* Start thread. */ os::StartThread(std::addressof(g_process_track_thread)); diff --git a/stratosphere/pm/source/pm_main.cpp b/stratosphere/pm/source/pm_main.cpp index 3aa363278..f25586032 100644 --- a/stratosphere/pm/source/pm_main.cpp +++ b/stratosphere/pm/source/pm_main.cpp @@ -184,6 +184,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(pm, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(pm, Main)); + /* Initialize process manager implementation. */ R_ABORT_UNLESS(pm::impl::InitializeProcessManager()); diff --git a/stratosphere/ro/source/ro_main.cpp b/stratosphere/ro/source/ro_main.cpp index 252bf516f..0f10add7f 100644 --- a/stratosphere/ro/source/ro_main.cpp +++ b/stratosphere/ro/source/ro_main.cpp @@ -107,6 +107,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(ro, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(ro, Main)); + /* Initialize Debug config. */ { ON_SCOPE_EXIT { splExit(); }; diff --git a/stratosphere/sm/source/sm_main.cpp b/stratosphere/sm/source/sm_main.cpp index ad1082f8c..c4e9720fd 100644 --- a/stratosphere/sm/source/sm_main.cpp +++ b/stratosphere/sm/source/sm_main.cpp @@ -89,6 +89,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(sm, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(sm, Main)); + /* NOTE: These handles are manually managed, but we don't save references to them to close on exit. */ /* This is fine, because if SM crashes we have much bigger issues. */ diff --git a/stratosphere/spl/source/spl_main.cpp b/stratosphere/spl/source/spl_main.cpp index 04d2d16a0..3b26e7893 100644 --- a/stratosphere/spl/source/spl_main.cpp +++ b/stratosphere/spl/source/spl_main.cpp @@ -129,6 +129,10 @@ namespace { int main(int argc, char **argv) { + /* Set thread name. */ + os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(spl, Main)); + AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(spl, Main)); + /* Initialize global context. */ spl::impl::Initialize();