2019-12-13 09:21:43 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-13 09:21:43 +00:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-29 22:26:24 +00:00
|
|
|
#pragma once
|
2020-01-30 06:06:25 +00:00
|
|
|
#include <mesosphere/kern_slab_helpers.hpp>
|
2020-01-30 07:46:55 +00:00
|
|
|
#include <mesosphere/kern_k_synchronization_object.hpp>
|
2020-01-31 01:07:08 +00:00
|
|
|
#include <mesosphere/kern_k_affinity_mask.hpp>
|
2020-01-31 06:46:18 +00:00
|
|
|
#include <mesosphere/kern_k_thread_context.hpp>
|
2020-01-31 08:07:06 +00:00
|
|
|
#include <mesosphere/kern_k_current_context.hpp>
|
2020-01-31 09:53:30 +00:00
|
|
|
#include <mesosphere/kern_k_timer_task.hpp>
|
|
|
|
#include <mesosphere/kern_k_worker_task.hpp>
|
2019-12-13 09:21:43 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-02-06 13:34:38 +00:00
|
|
|
class KThreadQueue;
|
2020-02-18 13:04:49 +00:00
|
|
|
class KProcess;
|
|
|
|
class KConditionVariable;
|
|
|
|
class KAddressArbiter;
|
2020-02-06 13:34:38 +00:00
|
|
|
|
2020-01-31 09:53:30 +00:00
|
|
|
using KThreadFunction = void (*)(uintptr_t);
|
|
|
|
|
2020-12-01 23:19:29 +00:00
|
|
|
class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>, public util::IntrusiveListBaseNode<KThread>, public KTimerTask, public KWorkerTask {
|
2020-01-30 07:46:55 +00:00
|
|
|
MESOSPHERE_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject);
|
2020-02-18 13:04:49 +00:00
|
|
|
private:
|
|
|
|
friend class KProcess;
|
|
|
|
friend class KConditionVariable;
|
|
|
|
friend class KAddressArbiter;
|
2020-01-30 06:06:25 +00:00
|
|
|
public:
|
2020-01-31 09:53:30 +00:00
|
|
|
static constexpr s32 MainThreadPriority = 1;
|
|
|
|
static constexpr s32 IdleThreadPriority = 64;
|
|
|
|
|
2020-01-31 08:07:06 +00:00
|
|
|
enum ThreadType : u32 {
|
|
|
|
ThreadType_Main = 0,
|
|
|
|
ThreadType_Kernel = 1,
|
|
|
|
ThreadType_HighPriority = 2,
|
|
|
|
ThreadType_User = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SuspendType : u32 {
|
2020-12-10 06:49:04 +00:00
|
|
|
SuspendType_Process = 0,
|
|
|
|
SuspendType_Thread = 1,
|
|
|
|
SuspendType_Debug = 2,
|
|
|
|
SuspendType_Backtrace = 3,
|
|
|
|
SuspendType_Init = 4,
|
2020-01-31 09:53:30 +00:00
|
|
|
|
|
|
|
SuspendType_Count,
|
2020-01-31 08:07:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ThreadState : u16 {
|
|
|
|
ThreadState_Initialized = 0,
|
|
|
|
ThreadState_Waiting = 1,
|
|
|
|
ThreadState_Runnable = 2,
|
|
|
|
ThreadState_Terminated = 3,
|
|
|
|
|
|
|
|
ThreadState_SuspendShift = 4,
|
|
|
|
ThreadState_Mask = (1 << ThreadState_SuspendShift) - 1,
|
|
|
|
|
2020-12-10 06:49:04 +00:00
|
|
|
ThreadState_ProcessSuspended = (1 << (SuspendType_Process + ThreadState_SuspendShift)),
|
|
|
|
ThreadState_ThreadSuspended = (1 << (SuspendType_Thread + ThreadState_SuspendShift)),
|
|
|
|
ThreadState_DebugSuspended = (1 << (SuspendType_Debug + ThreadState_SuspendShift)),
|
|
|
|
ThreadState_BacktraceSuspended = (1 << (SuspendType_Backtrace + ThreadState_SuspendShift)),
|
|
|
|
ThreadState_InitSuspended = (1 << (SuspendType_Init + ThreadState_SuspendShift)),
|
2020-01-31 09:53:30 +00:00
|
|
|
|
2020-12-10 06:49:04 +00:00
|
|
|
ThreadState_SuspendFlagMask = ((1 << SuspendType_Count) - 1) << ThreadState_SuspendShift,
|
2020-01-31 08:07:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum DpcFlag : u32 {
|
2021-04-07 21:38:51 +01:00
|
|
|
DpcFlag_Terminating = (1 << 0),
|
|
|
|
DpcFlag_Terminated = (1 << 1),
|
|
|
|
DpcFlag_PerformDestruction = (1 << 2),
|
2020-01-31 08:07:06 +00:00
|
|
|
};
|
|
|
|
|
2020-01-30 06:06:25 +00:00
|
|
|
struct StackParameters {
|
2021-04-07 06:26:52 +01:00
|
|
|
alignas(0x10) u8 svc_permission[0x18];
|
|
|
|
KThreadContext *context;
|
|
|
|
KThread *cur_thread;
|
|
|
|
s16 disable_count;
|
2020-01-30 06:06:25 +00:00
|
|
|
std::atomic<u8> dpc_flags;
|
|
|
|
u8 current_svc_id;
|
|
|
|
bool is_calling_svc;
|
|
|
|
bool is_in_exception_handler;
|
2020-07-28 01:32:04 +01:00
|
|
|
bool is_pinned;
|
2021-07-27 13:34:40 +01:00
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
bool is_single_step;
|
|
|
|
#endif
|
2020-01-30 06:06:25 +00:00
|
|
|
};
|
|
|
|
static_assert(alignof(StackParameters) == 0x10);
|
2021-04-07 06:26:52 +01:00
|
|
|
static_assert(sizeof(StackParameters) == THREAD_STACK_PARAMETERS_SIZE);
|
|
|
|
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, svc_permission) == THREAD_STACK_PARAMETERS_SVC_PERMISSION);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, context) == THREAD_STACK_PARAMETERS_CONTEXT);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, cur_thread) == THREAD_STACK_PARAMETERS_CUR_THREAD);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, disable_count) == THREAD_STACK_PARAMETERS_DISABLE_COUNT);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, dpc_flags) == THREAD_STACK_PARAMETERS_DPC_FLAGS);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, current_svc_id) == THREAD_STACK_PARAMETERS_CURRENT_SVC_ID);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, is_calling_svc) == THREAD_STACK_PARAMETERS_IS_CALLING_SVC);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, is_in_exception_handler) == THREAD_STACK_PARAMETERS_IS_IN_EXCEPTION_HANDLER);
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, is_pinned) == THREAD_STACK_PARAMETERS_IS_PINNED);
|
2020-01-31 04:56:24 +00:00
|
|
|
|
2021-07-27 13:34:40 +01:00
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
static_assert(__builtin_offsetof(StackParameters, is_single_step) == THREAD_STACK_PARAMETERS_IS_SINGLE_STEP);
|
|
|
|
#endif
|
|
|
|
|
2020-01-31 04:56:24 +00:00
|
|
|
struct QueueEntry {
|
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
KThread *m_prev;
|
|
|
|
KThread *m_next;
|
2020-01-31 04:56:24 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr QueueEntry() : m_prev(nullptr), m_next(nullptr) { /* ... */ }
|
2020-01-31 04:56:24 +00:00
|
|
|
|
2020-02-08 03:16:09 +00:00
|
|
|
constexpr void Initialize() {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_prev = nullptr;
|
|
|
|
m_next = nullptr;
|
2020-01-31 09:53:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KThread *GetPrev() const { return m_prev; }
|
|
|
|
constexpr KThread *GetNext() const { return m_next; }
|
|
|
|
constexpr void SetPrev(KThread *t) { m_prev = t; }
|
|
|
|
constexpr void SetNext(KThread *t) { m_next = t; }
|
2020-01-31 04:56:24 +00:00
|
|
|
};
|
2020-12-01 23:19:29 +00:00
|
|
|
|
|
|
|
using WaiterList = util::IntrusiveListBaseTraits<KThread>::ListType;
|
2020-01-31 04:56:24 +00:00
|
|
|
private:
|
2020-01-31 08:07:06 +00:00
|
|
|
static constexpr size_t PriorityInheritanceCountMax = 10;
|
|
|
|
union SyncObjectBuffer {
|
2020-12-18 01:18:47 +00:00
|
|
|
KSynchronizationObject *m_sync_objects[ams::svc::ArgumentHandleCountMax];
|
|
|
|
ams::svc::Handle m_handles[ams::svc::ArgumentHandleCountMax * (sizeof(KSynchronizationObject *) / sizeof(ams::svc::Handle))];
|
2020-01-31 04:56:24 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr SyncObjectBuffer() : m_sync_objects() { /* ... */ }
|
2020-01-31 08:07:06 +00:00
|
|
|
};
|
2020-12-18 01:18:47 +00:00
|
|
|
static_assert(sizeof(SyncObjectBuffer::m_sync_objects) == sizeof(SyncObjectBuffer::m_handles));
|
2020-07-15 17:15:49 +01:00
|
|
|
|
|
|
|
struct ConditionVariableComparator {
|
2021-04-21 00:56:33 +01:00
|
|
|
struct RedBlackKeyType {
|
2020-12-18 01:18:47 +00:00
|
|
|
uintptr_t m_cv_key;
|
|
|
|
s32 m_priority;
|
2020-12-02 00:19:39 +00:00
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE uintptr_t GetConditionVariableKey() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_cv_key;
|
2020-12-02 00:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ALWAYS_INLINE s32 GetPriority() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_priority;
|
2020-12-02 00:19:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-21 00:56:33 +01:00
|
|
|
template<typename T> requires (std::same_as<T, KThread> || std::same_as<T, RedBlackKeyType>)
|
2020-12-02 00:19:39 +00:00
|
|
|
static constexpr ALWAYS_INLINE int Compare(const T &lhs, const KThread &rhs) {
|
2020-07-15 17:15:49 +01:00
|
|
|
const uintptr_t l_key = lhs.GetConditionVariableKey();
|
|
|
|
const uintptr_t r_key = rhs.GetConditionVariableKey();
|
|
|
|
|
|
|
|
if (l_key < r_key) {
|
|
|
|
/* Sort first by key */
|
|
|
|
return -1;
|
|
|
|
} else if (l_key == r_key && lhs.GetPriority() < rhs.GetPriority()) {
|
|
|
|
/* And then by priority. */
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-21 00:56:33 +01:00
|
|
|
static_assert(ams::util::HasRedBlackKeyType<ConditionVariableComparator>);
|
|
|
|
static_assert(std::same_as<ams::util::RedBlackKeyType<ConditionVariableComparator, void>, ConditionVariableComparator::RedBlackKeyType>);
|
2020-01-31 09:53:30 +00:00
|
|
|
private:
|
|
|
|
static inline std::atomic<u64> s_next_thread_id = 0;
|
2020-01-31 08:07:06 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
alignas(16) KThreadContext m_thread_context{};
|
|
|
|
util::IntrusiveListNode m_process_list_node{};
|
|
|
|
util::IntrusiveRedBlackTreeNode m_condvar_arbiter_tree_node{};
|
|
|
|
s32 m_priority{};
|
2020-12-01 23:19:29 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
using ConditionVariableThreadTreeTraits = util::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<&KThread::m_condvar_arbiter_tree_node>;
|
2020-12-01 23:19:29 +00:00
|
|
|
using ConditionVariableThreadTree = ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
ConditionVariableThreadTree *m_condvar_tree{};
|
|
|
|
uintptr_t m_condvar_key{};
|
|
|
|
u64 m_virtual_affinity_mask{};
|
|
|
|
KAffinityMask m_physical_affinity_mask{};
|
|
|
|
u64 m_thread_id{};
|
|
|
|
std::atomic<s64> m_cpu_time{};
|
|
|
|
KSynchronizationObject *m_synced_object{};
|
|
|
|
KProcessAddress m_address_key{};
|
|
|
|
KProcess *m_parent{};
|
|
|
|
void *m_kernel_stack_top{};
|
|
|
|
u32 *m_light_ipc_data{};
|
|
|
|
KProcessAddress m_tls_address{};
|
|
|
|
void *m_tls_heap_address{};
|
|
|
|
KLightLock m_activity_pause_lock{};
|
|
|
|
SyncObjectBuffer m_sync_object_buffer{};
|
|
|
|
s64 m_schedule_count{};
|
|
|
|
s64 m_last_scheduled_tick{};
|
|
|
|
QueueEntry m_per_core_priority_queue_entry[cpu::NumCores]{};
|
|
|
|
KLightLock *m_waiting_lock{};
|
|
|
|
|
|
|
|
KThreadQueue *m_sleeping_queue{};
|
|
|
|
|
|
|
|
WaiterList m_waiter_list{};
|
|
|
|
WaiterList m_pinned_waiter_list{};
|
|
|
|
KThread *m_lock_owner{};
|
|
|
|
uintptr_t m_debug_params[3]{};
|
2021-04-07 21:38:51 +01:00
|
|
|
KAutoObject *m_closed_object{};
|
2020-12-18 01:18:47 +00:00
|
|
|
u32 m_address_key_value{};
|
|
|
|
u32 m_suspend_request_flags{};
|
|
|
|
u32 m_suspend_allowed_flags{};
|
|
|
|
Result m_wait_result;
|
|
|
|
Result m_debug_exception_result;
|
|
|
|
s32 m_base_priority{};
|
2021-04-07 09:18:26 +01:00
|
|
|
s32 m_base_priority_on_unpin{};
|
2020-12-18 01:18:47 +00:00
|
|
|
s32 m_physical_ideal_core_id{};
|
|
|
|
s32 m_virtual_ideal_core_id{};
|
|
|
|
s32 m_num_kernel_waiters{};
|
|
|
|
s32 m_current_core_id{};
|
|
|
|
s32 m_core_id{};
|
|
|
|
KAffinityMask m_original_physical_affinity_mask{};
|
|
|
|
s32 m_original_physical_ideal_core_id{};
|
|
|
|
s32 m_num_core_migration_disables{};
|
|
|
|
ThreadState m_thread_state{};
|
2021-01-08 10:13:36 +00:00
|
|
|
std::atomic<u8> m_termination_requested{};
|
2020-12-18 01:18:47 +00:00
|
|
|
bool m_wait_cancelled{};
|
|
|
|
bool m_cancellable{};
|
|
|
|
bool m_signaled{};
|
|
|
|
bool m_initialized{};
|
|
|
|
bool m_debug_attached{};
|
|
|
|
s8 m_priority_inheritance_count{};
|
|
|
|
bool m_resource_limit_release_hint{};
|
2020-01-31 08:07:06 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KThread() : m_wait_result(svc::ResultNoSynchronizationObject()), m_debug_exception_result(ResultSuccess()) { /* ... */ }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-01-31 09:53:30 +00:00
|
|
|
virtual ~KThread() { /* ... */ }
|
|
|
|
|
2020-12-01 23:54:31 +00:00
|
|
|
Result Initialize(KThreadFunction func, uintptr_t arg, void *kern_stack_top, KProcessAddress user_stack_top, s32 prio, s32 virt_core, KProcess *owner, ThreadType type);
|
2020-01-31 09:53:30 +00:00
|
|
|
|
2020-02-08 03:16:09 +00:00
|
|
|
private:
|
2020-12-01 23:54:31 +00:00
|
|
|
static Result InitializeThread(KThread *thread, KThreadFunction func, uintptr_t arg, KProcessAddress user_stack_top, s32 prio, s32 virt_core, KProcess *owner, ThreadType type);
|
2020-02-08 03:16:09 +00:00
|
|
|
public:
|
2020-12-01 23:54:31 +00:00
|
|
|
static Result InitializeKernelThread(KThread *thread, KThreadFunction func, uintptr_t arg, s32 prio, s32 virt_core) {
|
|
|
|
return InitializeThread(thread, func, arg, Null<KProcessAddress>, prio, virt_core, nullptr, ThreadType_Kernel);
|
2020-02-08 03:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Result InitializeHighPriorityThread(KThread *thread, KThreadFunction func, uintptr_t arg) {
|
|
|
|
return InitializeThread(thread, func, arg, Null<KProcessAddress>, 0, GetCurrentCoreId(), nullptr, ThreadType_HighPriority);
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:54:31 +00:00
|
|
|
static Result InitializeUserThread(KThread *thread, KThreadFunction func, uintptr_t arg, KProcessAddress user_stack_top, s32 prio, s32 virt_core, KProcess *owner) {
|
|
|
|
return InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner, ThreadType_User);
|
2020-02-20 03:38:20 +00:00
|
|
|
}
|
2020-02-20 04:42:21 +00:00
|
|
|
|
|
|
|
static void ResumeThreadsSuspendedForInit();
|
2020-01-31 08:07:06 +00:00
|
|
|
private:
|
|
|
|
StackParameters &GetStackParameters() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return *(reinterpret_cast<StackParameters *>(m_kernel_stack_top) - 1);
|
2020-01-31 08:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const StackParameters &GetStackParameters() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return *(reinterpret_cast<const StackParameters *>(m_kernel_stack_top) - 1);
|
2020-01-31 08:07:06 +00:00
|
|
|
}
|
2020-07-31 13:52:59 +01:00
|
|
|
public:
|
|
|
|
StackParameters &GetStackParametersForExceptionSvcPermission() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return *(reinterpret_cast<StackParameters *>(m_kernel_stack_top) - 1);
|
2020-07-31 13:52:59 +01:00
|
|
|
}
|
2020-01-31 08:07:06 +00:00
|
|
|
public:
|
2021-04-07 06:26:52 +01:00
|
|
|
ALWAYS_INLINE s16 GetDisableDispatchCount() const {
|
2020-01-31 08:07:06 +00:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-02-08 10:49:32 +00:00
|
|
|
return this->GetStackParameters().disable_count;
|
2020-01-31 08:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void DisableDispatch() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentThread().GetDisableDispatchCount() >= 0);
|
2020-02-08 10:49:32 +00:00
|
|
|
this->GetStackParameters().disable_count++;
|
2020-01-31 08:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void EnableDispatch() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentThread().GetDisableDispatchCount() > 0);
|
2020-02-08 10:49:32 +00:00
|
|
|
this->GetStackParameters().disable_count--;
|
|
|
|
}
|
|
|
|
|
2020-07-28 01:32:04 +01:00
|
|
|
void Pin();
|
|
|
|
void Unpin();
|
|
|
|
|
2020-07-31 13:52:59 +01:00
|
|
|
ALWAYS_INLINE void SaveDebugParams(uintptr_t param1, uintptr_t param2, uintptr_t param3) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_debug_params[0] = param1;
|
|
|
|
m_debug_params[1] = param2;
|
|
|
|
m_debug_params[2] = param3;
|
2020-07-31 13:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void RestoreDebugParams(uintptr_t *param1, uintptr_t *param2, uintptr_t *param3) {
|
2020-12-18 01:18:47 +00:00
|
|
|
*param1 = m_debug_params[0];
|
|
|
|
*param2 = m_debug_params[1];
|
|
|
|
*param3 = m_debug_params[2];
|
2020-07-31 13:52:59 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 08:00:35 +00:00
|
|
|
NOINLINE void DisableCoreMigration();
|
|
|
|
NOINLINE void EnableCoreMigration();
|
|
|
|
|
2020-02-08 10:49:32 +00:00
|
|
|
ALWAYS_INLINE void SetInExceptionHandler() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
this->GetStackParameters().is_in_exception_handler = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void ClearInExceptionHandler() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
this->GetStackParameters().is_in_exception_handler = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsInExceptionHandler() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
return this->GetStackParameters().is_in_exception_handler;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:56:47 +01:00
|
|
|
ALWAYS_INLINE bool IsCallingSvc() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
return this->GetStackParameters().is_calling_svc;
|
|
|
|
}
|
|
|
|
|
2020-07-31 10:49:43 +01:00
|
|
|
ALWAYS_INLINE u8 GetSvcId() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
return this->GetStackParameters().current_svc_id;
|
|
|
|
}
|
|
|
|
|
2021-07-27 13:34:40 +01:00
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
|
|
|
|
ALWAYS_INLINE void SetSingleStep() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
this->GetStackParameters().is_single_step = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void ClearSingleStep() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
this->GetStackParameters().is_single_step = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool IsSingleStep() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
return this->GetStackParameters().is_single_step;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-02-08 10:49:32 +00:00
|
|
|
ALWAYS_INLINE void RegisterDpc(DpcFlag flag) {
|
2021-04-07 21:38:51 +01:00
|
|
|
this->GetStackParameters().dpc_flags.fetch_or(flag);
|
2020-02-08 10:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void ClearDpc(DpcFlag flag) {
|
2021-04-08 01:11:01 +01:00
|
|
|
this->GetStackParameters().dpc_flags.fetch_and(~flag);
|
2020-02-08 10:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE u8 GetDpc() const {
|
2021-04-07 21:38:51 +01:00
|
|
|
return this->GetStackParameters().dpc_flags.load();
|
2020-02-08 10:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool HasDpc() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-07-28 01:32:04 +01:00
|
|
|
return this->GetDpc() != 0;
|
2020-01-31 08:07:06 +00:00
|
|
|
}
|
2020-02-08 03:16:09 +00:00
|
|
|
private:
|
2021-04-07 16:17:15 +01:00
|
|
|
void UpdateState();
|
2020-02-14 23:27:34 +00:00
|
|
|
ALWAYS_INLINE void AddWaiterImpl(KThread *thread);
|
|
|
|
ALWAYS_INLINE void RemoveWaiterImpl(KThread *thread);
|
|
|
|
ALWAYS_INLINE static void RestorePriority(KThread *thread);
|
2020-07-11 02:39:53 +01:00
|
|
|
|
|
|
|
void StartTermination();
|
|
|
|
void FinishTermination();
|
2021-04-07 09:18:26 +01:00
|
|
|
|
|
|
|
void IncreaseBasePriority(s32 priority);
|
2020-01-31 08:07:06 +00:00
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetThreadId() const { return m_thread_id; }
|
2020-02-14 23:27:34 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KThreadContext &GetContext() { return m_thread_context; }
|
|
|
|
constexpr const KThreadContext &GetContext() const { return m_thread_context; }
|
2020-07-23 09:25:59 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u64 GetVirtualAffinityMask() const { return m_virtual_affinity_mask; }
|
|
|
|
constexpr const KAffinityMask &GetAffinityMask() const { return m_physical_affinity_mask; }
|
2020-12-01 23:54:31 +00:00
|
|
|
|
2020-07-23 09:25:59 +01:00
|
|
|
Result GetCoreMask(int32_t *out_ideal_core, u64 *out_affinity_mask);
|
|
|
|
Result SetCoreMask(int32_t ideal_core, u64 affinity_mask);
|
|
|
|
|
2020-12-01 23:54:31 +00:00
|
|
|
Result GetPhysicalCoreMask(int32_t *out_ideal_core, u64 *out_affinity_mask);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ThreadState GetState() const { return static_cast<ThreadState>(m_thread_state & ThreadState_Mask); }
|
|
|
|
constexpr ThreadState GetRawState() const { return m_thread_state; }
|
2020-02-05 21:02:35 +00:00
|
|
|
NOINLINE void SetState(ThreadState state);
|
|
|
|
|
|
|
|
NOINLINE KThreadContext *GetContextForSchedulerLoop();
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr uintptr_t GetConditionVariableKey() const { return m_condvar_key; }
|
|
|
|
constexpr uintptr_t GetAddressArbiterKey() const { return m_condvar_key; }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-07-19 04:03:27 +01:00
|
|
|
constexpr void SetConditionVariable(ConditionVariableThreadTree *tree, KProcessAddress address, uintptr_t cv_key, u32 value) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_condvar_tree = tree;
|
|
|
|
m_condvar_key = cv_key;
|
|
|
|
m_address_key = address;
|
|
|
|
m_address_key_value = value;
|
2020-07-19 04:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void ClearConditionVariable() {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_condvar_tree = nullptr;
|
2020-07-15 17:15:49 +01:00
|
|
|
}
|
|
|
|
|
2020-07-19 04:03:27 +01:00
|
|
|
constexpr bool IsWaitingForConditionVariable() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_condvar_tree != nullptr;
|
2020-07-19 04:03:27 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 17:15:49 +01:00
|
|
|
constexpr void SetAddressArbiter(ConditionVariableThreadTree *tree, uintptr_t address) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_condvar_tree = tree;
|
|
|
|
m_condvar_key = address;
|
2020-07-15 17:15:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void ClearAddressArbiter() {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_condvar_tree = nullptr;
|
2020-07-15 17:15:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool IsWaitingForAddressArbiter() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_condvar_tree != nullptr;
|
2020-07-10 03:17:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetIdealVirtualCore() const { return m_virtual_ideal_core_id; }
|
|
|
|
constexpr s32 GetIdealPhysicalCore() const { return m_physical_ideal_core_id; }
|
2020-12-01 23:54:31 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetActiveCore() const { return m_core_id; }
|
|
|
|
constexpr void SetActiveCore(s32 core) { m_core_id = core; }
|
2020-07-20 10:11:49 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ALWAYS_INLINE s32 GetCurrentCore() const { return m_current_core_id; }
|
|
|
|
constexpr void SetCurrentCore(s32 core) { m_current_core_id = core; }
|
2020-12-01 21:41:37 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetPriority() const { return m_priority; }
|
|
|
|
constexpr void SetPriority(s32 prio) { m_priority = prio; }
|
2020-07-20 10:11:49 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetBasePriority() const { return m_base_priority; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr QueueEntry &GetPriorityQueueEntry(s32 core) { return m_per_core_priority_queue_entry[core]; }
|
|
|
|
constexpr const QueueEntry &GetPriorityQueueEntry(s32 core) const { return m_per_core_priority_queue_entry[core]; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr void SetSleepingQueue(KThreadQueue *q) { m_sleeping_queue = q; }
|
2020-02-06 13:34:38 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr ConditionVariableThreadTree *GetConditionVariableTree() const { return m_condvar_tree; }
|
2020-02-14 23:27:34 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s32 GetNumKernelWaiters() const { return m_num_kernel_waiters; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-02-14 23:27:34 +00:00
|
|
|
void AddWaiter(KThread *thread);
|
|
|
|
void RemoveWaiter(KThread *thread);
|
|
|
|
KThread *RemoveWaiterByKey(s32 *out_num_waiters, KProcessAddress key);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcessAddress GetAddressKey() const { return m_address_key; }
|
|
|
|
constexpr u32 GetAddressKeyValue() const { return m_address_key_value; }
|
|
|
|
constexpr void SetAddressKey(KProcessAddress key) { m_address_key = key; }
|
|
|
|
constexpr void SetAddressKey(KProcessAddress key, u32 val) { m_address_key = key; m_address_key_value = val; }
|
2020-07-10 04:45:26 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr void SetLockOwner(KThread *owner) { m_lock_owner = owner; }
|
|
|
|
constexpr KThread *GetLockOwner() const { return m_lock_owner; }
|
2020-02-14 23:27:34 +00:00
|
|
|
|
2020-02-20 03:38:20 +00:00
|
|
|
constexpr void SetSyncedObject(KSynchronizationObject *obj, Result wait_res) {
|
2020-07-10 01:21:47 +01:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
m_synced_object = obj;
|
|
|
|
m_wait_result = wait_res;
|
2020-02-20 03:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:21:47 +01:00
|
|
|
constexpr Result GetWaitResult(KSynchronizationObject **out) const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
*out = m_synced_object;
|
|
|
|
return m_wait_result;
|
2020-07-10 01:21:47 +01:00
|
|
|
}
|
|
|
|
|
2020-07-31 04:49:58 +01:00
|
|
|
constexpr void SetDebugExceptionResult(Result result) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-12-18 01:18:47 +00:00
|
|
|
m_debug_exception_result = result;
|
2020-07-31 04:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Result GetDebugExceptionResult() const {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-12-18 01:18:47 +00:00
|
|
|
return m_debug_exception_result;
|
2020-07-31 04:49:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:27:53 +01:00
|
|
|
void WaitCancel();
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
bool IsWaitCancelled() const { return m_wait_cancelled; }
|
|
|
|
void ClearWaitCancelled() { m_wait_cancelled = false; }
|
2020-07-10 01:21:47 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
void ClearCancellable() { m_cancellable = false; }
|
|
|
|
void SetCancellable() { m_cancellable = true; }
|
2020-07-10 01:21:47 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u32 *GetLightSessionData() const { return m_light_ipc_data; }
|
|
|
|
constexpr void SetLightSessionData(u32 *data) { m_light_ipc_data = data; }
|
2020-07-29 11:57:40 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
bool HasWaiters() const { return !m_waiter_list.empty(); }
|
2020-02-14 23:27:34 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s64 GetLastScheduledTick() const { return m_last_scheduled_tick; }
|
|
|
|
constexpr void SetLastScheduledTick(s64 tick) { m_last_scheduled_tick = tick; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr s64 GetYieldScheduleCount() const { return m_schedule_count; }
|
|
|
|
constexpr void SetYieldScheduleCount(s64 count) { m_schedule_count = count; }
|
2020-07-11 08:48:26 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcess *GetOwnerProcess() const { return m_parent; }
|
|
|
|
constexpr bool IsUserThread() const { return m_parent != nullptr; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KProcessAddress GetThreadLocalRegionAddress() const { return m_tls_address; }
|
|
|
|
constexpr void *GetThreadLocalRegionHeapAddress() const { return m_tls_heap_address; }
|
2020-02-05 21:02:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KSynchronizationObject **GetSynchronizationObjectBuffer() { return std::addressof(m_sync_object_buffer.m_sync_objects[0]); }
|
2021-04-27 00:56:00 +01:00
|
|
|
constexpr ams::svc::Handle *GetHandleBuffer() { return std::addressof(m_sync_object_buffer.m_handles[sizeof(m_sync_object_buffer.m_sync_objects) / (sizeof(ams::svc::Handle)) - ams::svc::ArgumentHandleCountMax]); }
|
2020-07-10 01:21:47 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
u16 GetUserDisableCount() const { return static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->disable_count; }
|
|
|
|
void SetInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 1; }
|
|
|
|
void ClearInterruptFlag() const { static_cast<ams::svc::ThreadLocalRegion *>(m_tls_heap_address)->interrupt_flag = 0; }
|
2020-02-08 10:49:32 +00:00
|
|
|
|
2021-04-07 21:38:51 +01:00
|
|
|
ALWAYS_INLINE KAutoObject *GetClosedObject() { return m_closed_object; }
|
|
|
|
|
|
|
|
ALWAYS_INLINE void SetClosedObject(KAutoObject *object) {
|
|
|
|
MESOSPHERE_ASSERT(object != nullptr);
|
|
|
|
|
|
|
|
/* Set the object to destroy. */
|
|
|
|
m_closed_object = object;
|
|
|
|
|
|
|
|
/* Schedule destruction DPC. */
|
|
|
|
if ((this->GetStackParameters().dpc_flags.load(std::memory_order_relaxed) & DpcFlag_PerformDestruction) == 0) {
|
|
|
|
this->RegisterDpc(DpcFlag_PerformDestruction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void DestroyClosedObjects() {
|
|
|
|
/* Destroy all objects that have been closed. */
|
|
|
|
if (KAutoObject *cur = m_closed_object; cur != nullptr) {
|
|
|
|
do {
|
|
|
|
/* Set our closed object as the next to close. */
|
|
|
|
m_closed_object = cur->GetNextClosedObject();
|
|
|
|
|
|
|
|
/* Destroy the current object. */
|
|
|
|
cur->Destroy();
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
cur = m_closed_object;
|
|
|
|
} while (cur != nullptr);
|
|
|
|
|
|
|
|
/* Clear the pending DPC. */
|
|
|
|
this->ClearDpc(DpcFlag_PerformDestruction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr void SetDebugAttached() { m_debug_attached = true; }
|
|
|
|
constexpr bool IsAttachedToDebugger() const { return m_debug_attached; }
|
2020-07-19 04:03:27 +01:00
|
|
|
|
2020-07-24 13:44:52 +01:00
|
|
|
void AddCpuTime(s32 core_id, s64 amount) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_cpu_time += amount;
|
2020-07-24 13:44:52 +01:00
|
|
|
/* TODO: Debug kernels track per-core tick counts. Should we? */
|
2020-08-21 01:29:10 +01:00
|
|
|
MESOSPHERE_UNUSED(core_id);
|
2020-07-24 13:44:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:13:36 +00:00
|
|
|
s64 GetCpuTime() const { return m_cpu_time.load(); }
|
2020-07-24 13:44:52 +01:00
|
|
|
|
|
|
|
s64 GetCpuTime(s32 core_id) const {
|
|
|
|
MESOSPHERE_ABORT_UNLESS(0 <= core_id && core_id < static_cast<s32>(cpu::NumCores));
|
|
|
|
|
|
|
|
/* TODO: Debug kernels track per-core tick counts. Should we? */
|
|
|
|
return 0;
|
2020-02-05 21:02:35 +00:00
|
|
|
}
|
2020-01-31 08:07:06 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr u32 GetSuspendFlags() const { return m_suspend_allowed_flags & m_suspend_request_flags; }
|
2020-02-08 03:16:09 +00:00
|
|
|
constexpr bool IsSuspended() const { return this->GetSuspendFlags() != 0; }
|
2021-04-27 00:56:00 +01:00
|
|
|
constexpr bool IsSuspendRequested(SuspendType type) const { return (m_suspend_request_flags & (1u << (util::ToUnderlying(ThreadState_SuspendShift) + util::ToUnderlying(type)))) != 0; }
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr bool IsSuspendRequested() const { return m_suspend_request_flags != 0; }
|
2020-02-08 03:16:09 +00:00
|
|
|
void RequestSuspend(SuspendType type);
|
2020-02-20 04:42:21 +00:00
|
|
|
void Resume(SuspendType type);
|
2020-02-08 03:16:09 +00:00
|
|
|
void TrySuspend();
|
2020-02-14 23:27:34 +00:00
|
|
|
void Continue();
|
|
|
|
|
2020-07-23 11:04:43 +01:00
|
|
|
Result SetActivity(ams::svc::ThreadActivity activity);
|
2020-07-28 11:56:47 +01:00
|
|
|
Result GetThreadContext3(ams::svc::ThreadContext *out);
|
2020-07-23 11:04:43 +01:00
|
|
|
|
2020-02-14 23:27:34 +00:00
|
|
|
void ContinueIfHasKernelWaiters() {
|
|
|
|
if (this->GetNumKernelWaiters() > 0) {
|
|
|
|
this->Continue();
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 03:16:09 +00:00
|
|
|
|
2020-02-21 21:05:16 +00:00
|
|
|
void Wakeup();
|
|
|
|
|
2020-07-20 10:11:49 +01:00
|
|
|
void SetBasePriority(s32 priority);
|
2020-02-08 03:16:09 +00:00
|
|
|
Result SetPriorityToIdle();
|
|
|
|
|
|
|
|
Result Run();
|
|
|
|
void Exit();
|
|
|
|
|
2021-04-07 16:17:15 +01:00
|
|
|
Result Terminate();
|
2020-07-23 07:52:29 +01:00
|
|
|
ThreadState RequestTerminate();
|
|
|
|
|
2020-07-11 07:30:15 +01:00
|
|
|
Result Sleep(s64 timeout);
|
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
ALWAYS_INLINE void *GetStackTop() const { return reinterpret_cast<StackParameters *>(m_kernel_stack_top) - 1; }
|
|
|
|
ALWAYS_INLINE void *GetKernelStackTop() const { return m_kernel_stack_top; }
|
2020-01-31 09:53:30 +00:00
|
|
|
|
2020-02-05 21:02:35 +00:00
|
|
|
ALWAYS_INLINE bool IsTerminationRequested() const {
|
2021-01-08 10:13:36 +00:00
|
|
|
return m_termination_requested.load() || this->GetRawState() == ThreadState_Terminated;
|
2020-02-05 21:02:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 13:59:54 +00:00
|
|
|
size_t GetKernelStackUsage() const;
|
2020-01-31 09:53:30 +00:00
|
|
|
public:
|
|
|
|
/* Overridden parent functions. */
|
2020-08-03 16:03:26 +01:00
|
|
|
virtual u64 GetId() const override final { return this->GetThreadId(); }
|
2020-02-18 13:04:49 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
virtual bool IsInitialized() const override { return m_initialized; }
|
|
|
|
virtual uintptr_t GetPostDestroyArgument() const override { return reinterpret_cast<uintptr_t>(m_parent) | (m_resource_limit_release_hint ? 1 : 0); }
|
2020-01-31 09:53:30 +00:00
|
|
|
|
|
|
|
static void PostDestroy(uintptr_t arg);
|
2020-01-31 04:56:24 +00:00
|
|
|
|
2020-01-31 09:53:30 +00:00
|
|
|
virtual void Finalize() override;
|
|
|
|
virtual bool IsSignaled() const override;
|
|
|
|
virtual void OnTimer() override;
|
|
|
|
virtual void DoWorkerTask() override;
|
2020-01-31 08:07:06 +00:00
|
|
|
public:
|
2020-07-15 17:15:49 +01:00
|
|
|
static constexpr bool IsConditionVariableThreadTreeValid() {
|
|
|
|
return ConditionVariableThreadTreeTraits::IsValid();
|
|
|
|
}
|
|
|
|
|
2020-07-31 09:59:46 +01:00
|
|
|
static KThread *GetThreadFromId(u64 thread_id);
|
2020-07-31 00:52:11 +01:00
|
|
|
static Result GetThreadList(s32 *out_num_threads, ams::kern::svc::KUserPointer<u64 *> out_thread_ids, s32 max_out_count);
|
|
|
|
|
2020-07-15 17:15:49 +01:00
|
|
|
using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
|
2020-01-29 22:26:24 +00:00
|
|
|
};
|
2020-01-31 08:07:06 +00:00
|
|
|
static_assert(alignof(KThread) == 0x10);
|
2020-07-15 17:15:49 +01:00
|
|
|
static_assert(KThread::IsConditionVariableThreadTreeValid());
|
2019-12-13 09:21:43 +00:00
|
|
|
|
2020-01-30 23:29:51 +00:00
|
|
|
class KScopedDisableDispatch {
|
|
|
|
public:
|
|
|
|
explicit ALWAYS_INLINE KScopedDisableDispatch() {
|
2020-01-31 08:07:06 +00:00
|
|
|
GetCurrentThread().DisableDispatch();
|
2020-01-30 23:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 07:07:51 +01:00
|
|
|
~KScopedDisableDispatch();
|
2020-01-30 23:29:51 +00:00
|
|
|
};
|
|
|
|
|
2020-07-31 00:31:58 +01:00
|
|
|
ALWAYS_INLINE KExceptionContext *GetExceptionContext(KThread *thread) {
|
|
|
|
return reinterpret_cast<KExceptionContext *>(reinterpret_cast<uintptr_t>(thread->GetKernelStackTop()) - sizeof(KThread::StackParameters) - sizeof(KExceptionContext));
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE const KExceptionContext *GetExceptionContext(const KThread *thread) {
|
|
|
|
return reinterpret_cast<const KExceptionContext *>(reinterpret_cast<uintptr_t>(thread->GetKernelStackTop()) - sizeof(KThread::StackParameters) - sizeof(KExceptionContext));
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:41:37 +00:00
|
|
|
ALWAYS_INLINE KProcess *GetCurrentProcessPointer() {
|
|
|
|
return GetCurrentThread().GetOwnerProcess();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE KProcess &GetCurrentProcess() {
|
|
|
|
return *GetCurrentProcessPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE s32 GetCurrentCoreId() {
|
|
|
|
return GetCurrentThread().GetCurrentCore();
|
|
|
|
}
|
|
|
|
|
2021-04-07 21:38:51 +01:00
|
|
|
ALWAYS_INLINE void KAutoObject::ScheduleDestruction() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Set our object to destroy. */
|
|
|
|
m_next_closed_object = GetCurrentThread().GetClosedObject();
|
|
|
|
|
|
|
|
/* Set ourselves as the thread's next object to destroy. */
|
|
|
|
GetCurrentThread().SetClosedObject(this);
|
|
|
|
}
|
|
|
|
|
2019-12-13 09:21:43 +00:00
|
|
|
}
|