From 7ad20154fc9bf1094f78721fed13fac1436bef17 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Wed, 10 Jan 2018 00:58:25 -0500
Subject: [PATCH] Threads: Added enum values for the Switch's 4 cpu cores and
 implemented svcGetInfo(AllowedCpuIdBitmask)

---
 src/citra_qt/debugger/wait_tree.cpp |  9 +++------
 src/core/hle/kernel/process.h       |  4 ++++
 src/core/hle/kernel/svc.cpp         | 17 +++++++++++------
 src/core/hle/kernel/svc.h           |  1 +
 src/core/hle/kernel/thread.h        | 13 +++++++++----
 5 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/citra_qt/debugger/wait_tree.cpp b/src/citra_qt/debugger/wait_tree.cpp
index cd03a6554..c066a3e17 100644
--- a/src/citra_qt/debugger/wait_tree.cpp
+++ b/src/citra_qt/debugger/wait_tree.cpp
@@ -205,14 +205,11 @@ std::vector<std::unique_ptr<WaitTreeItem>> WaitTreeThread::GetChildren() const {
     case ThreadProcessorId::THREADPROCESSORID_DEFAULT:
         processor = tr("default");
         break;
-    case ThreadProcessorId::THREADPROCESSORID_ALL:
-        processor = tr("all");
-        break;
     case ThreadProcessorId::THREADPROCESSORID_0:
-        processor = tr("AppCore");
-        break;
     case ThreadProcessorId::THREADPROCESSORID_1:
-        processor = tr("SysCore");
+    case ThreadProcessorId::THREADPROCESSORID_2:
+    case ThreadProcessorId::THREADPROCESSORID_3:
+        processor = tr("core %1").arg(thread.processor_id);
         break;
     default:
         processor = tr("Unknown processor %1").arg(thread.processor_id);
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h
index 305275387..20b4e401c 100644
--- a/src/core/hle/kernel/process.h
+++ b/src/core/hle/kernel/process.h
@@ -13,6 +13,7 @@
 #include "common/bit_field.h"
 #include "common/common_types.h"
 #include "core/hle/kernel/kernel.h"
+#include "core/hle/kernel/thread.h"
 #include "core/hle/kernel/vm_manager.h"
 
 namespace Kernel {
@@ -127,6 +128,9 @@ public:
     u16 kernel_version = 0;
     /// The default CPU for this process, threads are scheduled on this cpu by default.
     u8 ideal_processor = 0;
+    /// Bitmask of allowed CPUs that this process' threads can run on. TODO(Subv): Actually parse
+    /// this value from the process header.
+    u32 allowed_processor_mask = THREADPROCESSORID_DEFAULT_MASK;
     /// Current status of the process
     ProcessStatus status;
 
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index bfe1907e3..a1eaab649 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -296,8 +296,14 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id)
     LOG_TRACE(Kernel_SVC, "called info_id=0x%X, info_sub_id=0x%X, handle=0x%08X", info_id,
               info_sub_id, handle);
 
+    ASSERT(handle == 0 || handle == CurrentProcess);
+
     auto& vm_manager = g_current_process->vm_manager;
+
     switch (static_cast<GetInfoType>(info_id)) {
+    case GetInfoType::AllowedCpuIdBitmask:
+        *result = g_current_process->allowed_processor_mask;
+        break;
     case GetInfoType::TotalMemoryUsage:
         *result = vm_manager.GetTotalMemoryUsage();
         break;
@@ -455,16 +461,15 @@ static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, V
     switch (processor_id) {
     case THREADPROCESSORID_0:
         break;
-    case THREADPROCESSORID_ALL:
-        LOG_INFO(Kernel_SVC,
-                 "Newly created thread is allowed to be run in any Core, unimplemented.");
-        break;
     case THREADPROCESSORID_1:
+    case THREADPROCESSORID_2:
+    case THREADPROCESSORID_3:
+        // TODO(bunnei): Implement support for other processor IDs
         LOG_ERROR(Kernel_SVC,
-                  "Newly created thread must run in the SysCore (Core1), unimplemented.");
+                  "Newly created thread must run in another thread (%u), unimplemented.",
+                  processor_id);
         break;
     default:
-        // TODO(bunnei): Implement support for other processor IDs
         ASSERT_MSG(false, "Unsupported thread processor ID: %d", processor_id);
         break;
     }
diff --git a/src/core/hle/kernel/svc.h b/src/core/hle/kernel/svc.h
index a95598994..ff45482a3 100644
--- a/src/core/hle/kernel/svc.h
+++ b/src/core/hle/kernel/svc.h
@@ -23,6 +23,7 @@ struct PageInfo {
 /// Values accepted by svcGetInfo
 enum class GetInfoType : u64 {
     // 1.0.0+
+    AllowedCpuIdBitmask = 0,
     TotalMemoryUsage = 6,
     TotalHeapUsage = 7,
     RandomEntropy = 11,
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index 19ba6e0af..0a1ada27d 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -24,10 +24,15 @@ enum ThreadPriority : u32 {
 
 enum ThreadProcessorId : s32 {
     THREADPROCESSORID_DEFAULT = -2, ///< Run thread on default core specified by exheader
-    THREADPROCESSORID_ALL = -1,     ///< Run thread on either core
-    THREADPROCESSORID_0 = 0,        ///< Run thread on core 0 (AppCore)
-    THREADPROCESSORID_1 = 1,        ///< Run thread on core 1 (SysCore)
-    THREADPROCESSORID_MAX = 2,      ///< Processor ID must be less than this
+    THREADPROCESSORID_0 = 0,        ///< Run thread on core 0
+    THREADPROCESSORID_1 = 1,        ///< Run thread on core 1
+    THREADPROCESSORID_2 = 2,        ///< Run thread on core 2
+    THREADPROCESSORID_3 = 3,        ///< Run thread on core 3
+    THREADPROCESSORID_MAX = 4,      ///< Processor ID must be less than this
+
+    /// Allowed CPU mask
+    THREADPROCESSORID_DEFAULT_MASK = (1 << THREADPROCESSORID_0) | (1 << THREADPROCESSORID_1) |
+                                     (1 << THREADPROCESSORID_2) | (1 << THREADPROCESSORID_3)
 };
 
 enum ThreadStatus {