From 1e55498110800623c63e3ef03bfbff6b6de1c522 Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Fri, 1 Jan 2021 02:06:06 -0800
Subject: [PATCH] hle: kernel: KThread: Reorganize thread priority defaults.

---
 src/core/hle/kernel/global_scheduler_context.h |  9 +++++++--
 src/core/hle/kernel/k_scheduler.cpp            |  6 +++---
 src/core/hle/kernel/k_thread.cpp               | 13 ++++++-------
 src/core/hle/kernel/k_thread.h                 | 12 +++---------
 src/core/hle/kernel/svc.cpp                    | 10 ++++------
 src/core/hle/kernel/svc_results.h              |  1 +
 src/core/hle/kernel/svc_types.h                |  3 +++
 src/core/loader/nro.cpp                        |  4 ++--
 src/core/loader/nso.cpp                        |  4 ++--
 9 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/src/core/hle/kernel/global_scheduler_context.h b/src/core/hle/kernel/global_scheduler_context.h
index a365ffdaf..11592843e 100644
--- a/src/core/hle/kernel/global_scheduler_context.h
+++ b/src/core/hle/kernel/global_scheduler_context.h
@@ -13,6 +13,7 @@
 #include "core/hle/kernel/k_priority_queue.h"
 #include "core/hle/kernel/k_scheduler_lock.h"
 #include "core/hle/kernel/k_thread.h"
+#include "core/hle/kernel/svc_types.h"
 
 namespace Kernel {
 
@@ -20,8 +21,12 @@ class KernelCore;
 class SchedulerLock;
 
 using KSchedulerPriorityQueue =
-    KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, THREADPRIO_LOWEST, THREADPRIO_HIGHEST>;
-constexpr s32 HighestCoreMigrationAllowedPriority = 2;
+    KPriorityQueue<KThread, Core::Hardware::NUM_CPU_CORES, Svc::LowestThreadPriority,
+                   Svc::HighestThreadPriority>;
+
+static constexpr s32 HighestCoreMigrationAllowedPriority = 2;
+static_assert(Svc::LowestThreadPriority >= HighestCoreMigrationAllowedPriority);
+static_assert(Svc::HighestThreadPriority <= HighestCoreMigrationAllowedPriority);
 
 class GlobalSchedulerContext final {
     friend class KScheduler;
diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp
index 0f34a8a69..0e6300760 100644
--- a/src/core/hle/kernel/k_scheduler.cpp
+++ b/src/core/hle/kernel/k_scheduler.cpp
@@ -763,9 +763,9 @@ void KScheduler::Initialize() {
     std::string name = "Idle Thread Id:" + std::to_string(core_id);
     std::function<void(void*)> init_func = Core::CpuManager::GetIdleThreadStartFunc();
     void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater();
-    auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0, THREADPRIO_LOWEST, 0,
-                                      static_cast<u32>(core_id), 0, nullptr, std::move(init_func),
-                                      init_func_parameter);
+    auto thread_res = KThread::Create(system, ThreadType::Kernel, name, 0,
+                                      Svc::LowestThreadPriority, 0, static_cast<u32>(core_id), 0,
+                                      nullptr, std::move(init_func), init_func_parameter);
     idle_thread = thread_res.Unwrap().get();
 
     {
diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp
index 0f349dad2..518c5d5df 100644
--- a/src/core/hle/kernel/k_thread.cpp
+++ b/src/core/hle/kernel/k_thread.cpp
@@ -8,6 +8,7 @@
 #include <vector>
 
 #include "common/assert.h"
+#include "common/common_funcs.h"
 #include "common/common_types.h"
 #include "common/fiber.h"
 #include "common/logging/log.h"
@@ -25,6 +26,7 @@
 #include "core/hle/kernel/memory/memory_layout.h"
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/process.h"
+#include "core/hle/kernel/svc_results.h"
 #include "core/hle/kernel/time_manager.h"
 #include "core/hle/result.h"
 #include "core/memory.h"
@@ -124,11 +126,9 @@ ResultVal<std::shared_ptr<KThread>> KThread::Create(Core::System& system, Thread
                                                     std::function<void(void*)>&& thread_start_func,
                                                     void* thread_start_parameter) {
     auto& kernel = system.Kernel();
-    // Check if priority is in ranged. Lowest priority -> highest priority id.
-    if (priority > THREADPRIO_LOWEST) {
-        LOG_ERROR(Kernel_SVC, "Invalid thread priority: {}", priority);
-        return ERR_INVALID_THREAD_PRIORITY;
-    }
+
+    R_UNLESS(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority,
+             Svc::ResultInvalidPriority);
 
     if (processor_id > THREADPROCESSORID_MAX) {
         LOG_ERROR(Kernel_SVC, "Invalid processor id: {}", processor_id);
@@ -186,8 +186,7 @@ ResultVal<std::shared_ptr<KThread>> KThread::Create(Core::System& system, Thread
 }
 
 void KThread::SetBasePriority(u32 priority) {
-    ASSERT_MSG(priority <= THREADPRIO_LOWEST && priority >= THREADPRIO_HIGHEST,
-               "Invalid priority value.");
+    ASSERT(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority);
 
     KScopedSchedulerLock lock(kernel);
 
diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h
index bef480dd7..83a6d36ae 100644
--- a/src/core/hle/kernel/k_thread.h
+++ b/src/core/hle/kernel/k_thread.h
@@ -39,15 +39,6 @@ class KernelCore;
 class Process;
 class KScheduler;
 
-enum ThreadPriority : u32 {
-    THREADPRIO_HIGHEST = 0,            ///< Highest thread priority
-    THREADPRIO_MAX_CORE_MIGRATION = 2, ///< Highest priority for a core migration
-    THREADPRIO_USERLAND_MAX = 24,      ///< Highest thread priority for userland apps
-    THREADPRIO_DEFAULT = 44,           ///< Default thread priority for userland apps
-    THREADPRIO_LOWEST = 63,            ///< Lowest thread priority
-    THREADPRIO_COUNT = 64,             ///< Total number of possible thread priorities.
-};
-
 enum class ThreadType : u32 {
     Main = 0,
     Kernel = 1,
@@ -129,6 +120,9 @@ class KThread final : public KSynchronizationObject, public boost::intrusive::li
     friend class Process;
 
 public:
+    static constexpr s32 DefaultThreadPriority = 44;
+    static constexpr s32 IdleThreadPriority = 64;
+
     explicit KThread(KernelCore& kernel);
     ~KThread() override;
 
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 711b9d520..70a8ef34b 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -1130,11 +1130,9 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* priority, Handl
 static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) {
     LOG_TRACE(Kernel_SVC, "called");
 
-    if (priority > THREADPRIO_LOWEST) {
-        LOG_ERROR(
-            Kernel_SVC,
-            "An invalid priority was specified, expected {} but got {} for thread_handle={:08X}",
-            THREADPRIO_LOWEST, priority, handle);
+    if (priority > Svc::LowestThreadPriority) {
+        LOG_ERROR(Kernel_SVC, "An invalid priority was specified {} for thread_handle={:08X}",
+                  priority, handle);
         return ERR_INVALID_THREAD_PRIORITY;
     }
 
@@ -1472,7 +1470,7 @@ static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr e
         return ERR_INVALID_PROCESSOR_ID;
     }
 
-    if (priority > THREADPRIO_LOWEST) {
+    if (priority > Svc::LowestThreadPriority) {
         LOG_ERROR(Kernel_SVC,
                   "Invalid thread priority specified ({}). Must be within the range 0-64",
                   priority);
diff --git a/src/core/hle/kernel/svc_results.h b/src/core/hle/kernel/svc_results.h
index 78282f021..74adabc11 100644
--- a/src/core/hle/kernel/svc_results.h
+++ b/src/core/hle/kernel/svc_results.h
@@ -11,6 +11,7 @@ namespace Kernel::Svc {
 constexpr ResultCode ResultTerminationRequested{ErrorModule::Kernel, 59};
 constexpr ResultCode ResultInvalidAddress{ErrorModule::Kernel, 102};
 constexpr ResultCode ResultInvalidCurrentMemory{ErrorModule::Kernel, 106};
+constexpr ResultCode ResultInvalidPriority{ErrorModule::Kernel, 112};
 constexpr ResultCode ResultInvalidHandle{ErrorModule::Kernel, 114};
 constexpr ResultCode ResultTimedOut{ErrorModule::Kernel, 117};
 constexpr ResultCode ResultCancelled{ErrorModule::Kernel, 118};
diff --git a/src/core/hle/kernel/svc_types.h b/src/core/hle/kernel/svc_types.h
index d623f7a50..09b858f2a 100644
--- a/src/core/hle/kernel/svc_types.h
+++ b/src/core/hle/kernel/svc_types.h
@@ -77,4 +77,7 @@ enum class ArbitrationType : u32 {
     WaitIfEqual = 2,
 };
 
+constexpr inline s32 LowestThreadPriority = 63;
+constexpr inline s32 HighestThreadPriority = 0;
+
 } // namespace Kernel::Svc
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index 4f55314c7..f976d0a9c 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -219,8 +219,8 @@ AppLoader_NRO::LoadResult AppLoader_NRO::Load(Kernel::Process& process, Core::Sy
     }
 
     is_loaded = true;
-    return {ResultStatus::Success,
-            LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}};
+    return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
+                                                  Core::Memory::DEFAULT_STACK_SIZE}};
 }
 
 ResultStatus AppLoader_NRO::ReadIcon(std::vector<u8>& buffer) {
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 50e6cd080..ea347ea83 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -179,8 +179,8 @@ AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process, Core::Sy
     LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
 
     is_loaded = true;
-    return {ResultStatus::Success,
-            LoadParameters{Kernel::THREADPRIO_DEFAULT, Core::Memory::DEFAULT_STACK_SIZE}};
+    return {ResultStatus::Success, LoadParameters{Kernel::KThread::DefaultThreadPriority,
+                                                  Core::Memory::DEFAULT_STACK_SIZE}};
 }
 
 ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) {