2021-01-20 21:42:27 +00:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 08:49:13 +00:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-10 02:11:18 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-12-05 08:17:18 +00:00
|
|
|
#include <array>
|
2020-12-30 09:14:02 +00:00
|
|
|
#include <span>
|
2014-12-22 06:32:03 +00:00
|
|
|
#include <string>
|
2020-03-10 17:13:39 +00:00
|
|
|
#include <utility>
|
2014-12-22 06:32:03 +00:00
|
|
|
#include <vector>
|
2018-07-31 12:06:09 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
|
2014-05-10 02:11:18 +00:00
|
|
|
#include "common/common_types.h"
|
2020-12-30 09:14:02 +00:00
|
|
|
#include "common/intrusive_red_black_tree.h"
|
2016-12-22 05:08:09 +00:00
|
|
|
#include "core/arm/arm_interface.h"
|
2020-11-17 05:02:45 +00:00
|
|
|
#include "core/hle/kernel/k_affinity_mask.h"
|
2021-01-20 21:42:27 +00:00
|
|
|
#include "core/hle/kernel/k_light_lock.h"
|
2021-02-13 09:29:32 +00:00
|
|
|
#include "core/hle/kernel/k_spin_lock.h"
|
2020-12-22 06:36:53 +00:00
|
|
|
#include "core/hle/kernel/k_synchronization_object.h"
|
2021-04-03 06:24:20 +00:00
|
|
|
#include "core/hle/kernel/slab_helpers.h"
|
2020-12-30 09:14:02 +00:00
|
|
|
#include "core/hle/kernel/svc_common.h"
|
2021-01-20 21:42:27 +00:00
|
|
|
#include "core/hle/kernel/svc_types.h"
|
2014-10-23 03:20:01 +00:00
|
|
|
#include "core/hle/result.h"
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2020-02-25 02:04:12 +00:00
|
|
|
namespace Common {
|
|
|
|
class Fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Core {
|
2020-03-01 16:14:17 +00:00
|
|
|
class ARM_Interface;
|
2020-02-25 02:04:12 +00:00
|
|
|
class System;
|
2020-03-03 17:37:11 +00:00
|
|
|
} // namespace Core
|
2020-02-25 02:04:12 +00:00
|
|
|
|
2018-09-13 19:57:45 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-03 02:08:35 +00:00
|
|
|
class GlobalSchedulerContext;
|
2018-09-13 19:57:45 +00:00
|
|
|
class KernelCore;
|
|
|
|
class Process;
|
2020-12-03 02:08:35 +00:00
|
|
|
class KScheduler;
|
2021-01-20 21:42:27 +00:00
|
|
|
class KThreadQueue;
|
|
|
|
|
|
|
|
using KThreadFunction = VAddr;
|
2018-09-13 19:57:45 +00:00
|
|
|
|
2021-01-01 09:04:30 +00:00
|
|
|
enum class ThreadType : u32 {
|
|
|
|
Main = 0,
|
|
|
|
Kernel = 1,
|
|
|
|
HighPriority = 2,
|
|
|
|
User = 3,
|
2014-05-17 04:56:00 +00:00
|
|
|
};
|
2021-01-01 09:04:30 +00:00
|
|
|
DECLARE_ENUM_FLAG_OPERATORS(ThreadType);
|
2014-05-17 04:56:00 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
enum class SuspendType : u32 {
|
|
|
|
Process = 0,
|
|
|
|
Thread = 1,
|
|
|
|
Debug = 2,
|
|
|
|
Backtrace = 3,
|
|
|
|
Init = 4,
|
|
|
|
|
|
|
|
Count,
|
|
|
|
};
|
|
|
|
|
2020-12-28 21:16:43 +00:00
|
|
|
enum class ThreadState : u16 {
|
|
|
|
Initialized = 0,
|
|
|
|
Waiting = 1,
|
|
|
|
Runnable = 2,
|
|
|
|
Terminated = 3,
|
|
|
|
|
|
|
|
SuspendShift = 4,
|
|
|
|
Mask = (1 << SuspendShift) - 1,
|
|
|
|
|
|
|
|
ProcessSuspended = (1 << (0 + SuspendShift)),
|
|
|
|
ThreadSuspended = (1 << (1 + SuspendShift)),
|
|
|
|
DebugSuspended = (1 << (2 + SuspendShift)),
|
|
|
|
BacktraceSuspended = (1 << (3 + SuspendShift)),
|
|
|
|
InitSuspended = (1 << (4 + SuspendShift)),
|
|
|
|
|
|
|
|
SuspendFlagMask = ((1 << 5) - 1) << SuspendShift,
|
2014-05-22 22:50:36 +00:00
|
|
|
};
|
2020-12-28 21:16:43 +00:00
|
|
|
DECLARE_ENUM_FLAG_OPERATORS(ThreadState);
|
2014-05-22 22:50:36 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
enum class DpcFlag : u32 {
|
|
|
|
Terminating = (1 << 0),
|
|
|
|
Terminated = (1 << 1),
|
2019-03-29 21:01:46 +00:00
|
|
|
};
|
|
|
|
|
2021-01-10 22:29:02 +00:00
|
|
|
enum class ThreadWaitReasonForDebugging : u32 {
|
|
|
|
None, ///< Thread is not waiting
|
|
|
|
Sleep, ///< Thread is waiting due to a SleepThread SVC
|
|
|
|
IPC, ///< Thread is waiting for the reply from an IPC request
|
|
|
|
Synchronization, ///< Thread is waiting due to a WaitSynchronization SVC
|
|
|
|
ConditionVar, ///< Thread is waiting due to a WaitProcessWideKey SVC
|
|
|
|
Arbitration, ///< Thread is waiting due to a SignalToAddress/WaitForAddress SVC
|
|
|
|
Suspended, ///< Thread is waiting due to process suspension
|
|
|
|
};
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] KThread* GetCurrentThreadPointer(KernelCore& kernel);
|
|
|
|
[[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel);
|
|
|
|
[[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel);
|
|
|
|
|
2021-04-03 06:24:20 +00:00
|
|
|
class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>,
|
|
|
|
public boost::intrusive::list_base_hook<> {
|
|
|
|
KERNEL_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject);
|
|
|
|
|
|
|
|
private:
|
2020-12-30 09:14:02 +00:00
|
|
|
friend class KScheduler;
|
|
|
|
friend class Process;
|
|
|
|
|
2014-12-22 06:32:03 +00:00
|
|
|
public:
|
2021-01-01 10:06:06 +00:00
|
|
|
static constexpr s32 DefaultThreadPriority = 44;
|
2021-01-20 21:42:27 +00:00
|
|
|
static constexpr s32 IdleThreadPriority = Svc::LowestThreadPriority + 1;
|
2021-01-01 10:06:06 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
explicit KThread(KernelCore& kernel);
|
|
|
|
~KThread() override;
|
2019-11-25 01:15:51 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
public:
|
2020-03-02 04:46:10 +00:00
|
|
|
using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
|
|
|
|
using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
|
2021-01-20 21:42:27 +00:00
|
|
|
using WaiterList = boost::intrusive::list<KThread>;
|
2018-10-03 22:47:57 +00:00
|
|
|
|
2019-04-15 19:54:25 +00:00
|
|
|
void SetName(std::string new_name) {
|
|
|
|
name = std::move(new_name);
|
|
|
|
}
|
|
|
|
|
2015-01-26 06:56:17 +00:00
|
|
|
/**
|
|
|
|
* Gets the thread's current priority
|
|
|
|
* @return The current thread's priority
|
|
|
|
*/
|
2020-12-30 09:14:02 +00:00
|
|
|
[[nodiscard]] s32 GetPriority() const {
|
2021-01-20 21:42:27 +00:00
|
|
|
return priority;
|
2016-09-18 00:38:01 +00:00
|
|
|
}
|
2015-01-26 06:56:17 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
/**
|
|
|
|
* Sets the thread's current priority.
|
|
|
|
* @param priority The new priority.
|
|
|
|
*/
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetPriority(s32 value) {
|
|
|
|
priority = value;
|
2020-12-30 09:14:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:47:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the thread's nominal priority.
|
|
|
|
* @return The current thread's nominal priority.
|
|
|
|
*/
|
2020-12-30 09:14:02 +00:00
|
|
|
[[nodiscard]] s32 GetBasePriority() const {
|
|
|
|
return base_priority;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 06:56:17 +00:00
|
|
|
/**
|
|
|
|
* Gets the thread's thread ID
|
|
|
|
* @return The thread's ID
|
|
|
|
*/
|
2020-12-30 09:14:02 +00:00
|
|
|
[[nodiscard]] u64 GetThreadID() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return thread_id;
|
|
|
|
}
|
2015-05-25 18:34:09 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void ContinueIfHasKernelWaiters() {
|
|
|
|
if (GetNumKernelWaiters() > 0) {
|
|
|
|
Continue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:36:53 +00:00
|
|
|
void Wakeup();
|
2020-02-25 02:04:12 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetBasePriority(s32 value);
|
2020-02-25 16:40:33 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode Run();
|
2020-12-22 06:36:53 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void Exit();
|
2019-04-17 11:08:12 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] u32 GetSuspendFlags() const {
|
|
|
|
return suspend_allowed_flags & suspend_request_flags;
|
|
|
|
}
|
2020-02-25 20:38:33 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsSuspended() const {
|
|
|
|
return GetSuspendFlags() != 0;
|
2020-02-25 20:38:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsSuspendRequested(SuspendType type) const {
|
|
|
|
return (suspend_request_flags &
|
|
|
|
(1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)))) !=
|
|
|
|
0;
|
2020-02-25 20:38:33 +00:00
|
|
|
}
|
2015-01-17 07:03:44 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsSuspendRequested() const {
|
|
|
|
return suspend_request_flags != 0;
|
2020-12-22 06:36:53 +00:00
|
|
|
}
|
2016-12-04 03:38:14 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void RequestSuspend(SuspendType type);
|
|
|
|
|
|
|
|
void Resume(SuspendType type);
|
|
|
|
|
|
|
|
void TrySuspend();
|
|
|
|
|
|
|
|
void Continue();
|
|
|
|
|
|
|
|
void Suspend();
|
|
|
|
|
|
|
|
void SetSyncedObject(KSynchronizationObject* obj, ResultCode wait_res) {
|
|
|
|
synced_object = obj;
|
|
|
|
wait_result = wait_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ResultCode GetWaitResult(KSynchronizationObject** out) const {
|
|
|
|
*out = synced_object;
|
|
|
|
return wait_result;
|
|
|
|
}
|
2015-01-26 06:56:17 +00:00
|
|
|
|
2015-05-10 23:35:37 +00:00
|
|
|
/*
|
|
|
|
* Returns the Thread Local Storage address of the current thread
|
|
|
|
* @returns VAddr of the thread's TLS
|
|
|
|
*/
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] VAddr GetTLSAddress() const {
|
2016-09-18 00:38:01 +00:00
|
|
|
return tls_address;
|
|
|
|
}
|
2015-05-10 23:35:37 +00:00
|
|
|
|
2018-07-21 00:57:45 +00:00
|
|
|
/*
|
|
|
|
* Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
|
|
|
|
* @returns The value of the TPIDR_EL0 register.
|
|
|
|
*/
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] u64 GetTPIDR_EL0() const {
|
|
|
|
return thread_context_64.tpidr;
|
2018-07-21 00:57:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 22:47:57 +00:00
|
|
|
/// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
|
|
|
|
void SetTPIDR_EL0(u64 value) {
|
2021-01-20 21:42:27 +00:00
|
|
|
thread_context_64.tpidr = value;
|
|
|
|
thread_context_32.tpidr = static_cast<u32>(value);
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ThreadContext32& GetContext32() {
|
|
|
|
return thread_context_32;
|
2020-03-02 04:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const ThreadContext32& GetContext32() const {
|
|
|
|
return thread_context_32;
|
2020-03-02 04:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ThreadContext64& GetContext64() {
|
|
|
|
return thread_context_64;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const ThreadContext64& GetContext64() const {
|
|
|
|
return thread_context_64;
|
2020-03-12 20:48:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 01:08:17 +00:00
|
|
|
[[nodiscard]] std::shared_ptr<Common::Fiber>& GetHostContext();
|
2020-03-12 20:48:43 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ThreadState GetState() const {
|
2020-12-28 21:16:43 +00:00
|
|
|
return thread_state & ThreadState::Mask;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ThreadState GetRawState() const {
|
2020-12-28 21:16:43 +00:00
|
|
|
return thread_state;
|
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
void SetState(ThreadState state);
|
2018-10-03 22:47:57 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] s64 GetLastScheduledTick() const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return last_scheduled_tick;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-05 08:17:18 +00:00
|
|
|
|
|
|
|
void SetLastScheduledTick(s64 tick) {
|
2020-12-30 09:14:02 +00:00
|
|
|
last_scheduled_tick = tick;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void AddCpuTime([[maybe_unused]] s32 core_id_, s64 amount) {
|
|
|
|
cpu_time += amount;
|
|
|
|
// TODO(bunnei): Debug kernels track per-core tick counts. Should we?
|
2018-10-25 22:42:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] s64 GetCpuTime() const {
|
|
|
|
return cpu_time;
|
2018-10-25 22:42:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] s32 GetActiveCore() const {
|
|
|
|
return core_id;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetActiveCore(s32 core) {
|
|
|
|
core_id = core;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] s32 GetCurrentCore() const {
|
|
|
|
return current_core_id;
|
2019-03-29 21:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetCurrentCore(s32 core) {
|
|
|
|
current_core_id = core;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] Process* GetOwnerProcess() {
|
|
|
|
return parent;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const Process* GetOwnerProcess() const {
|
|
|
|
return parent;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsUserThread() const {
|
|
|
|
return parent != nullptr;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] KThread* GetLockOwner() const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return lock_owner;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
2014-12-22 06:32:03 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void SetLockOwner(KThread* owner) {
|
2020-12-30 09:14:02 +00:00
|
|
|
lock_owner = owner;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
2014-12-22 06:32:03 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const KAffinityMask& GetAffinityMask() const {
|
|
|
|
return physical_affinity_mask;
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
|
|
|
|
|
|
|
|
[[nodiscard]] ResultCode GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
|
2018-10-03 22:47:57 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode SetCoreMask(s32 core_id, u64 v_affinity_mask);
|
2018-12-03 17:25:27 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode SetActivity(Svc::ThreadActivity activity);
|
2019-03-16 03:28:29 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode Sleep(s64 timeout);
|
|
|
|
|
|
|
|
[[nodiscard]] s64 GetYieldScheduleCount() const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return schedule_count;
|
2019-09-11 16:14:37 +00:00
|
|
|
}
|
2020-12-05 08:17:18 +00:00
|
|
|
|
|
|
|
void SetYieldScheduleCount(s64 count) {
|
2020-12-30 09:14:02 +00:00
|
|
|
schedule_count = count;
|
2019-09-11 16:14:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void WaitCancel();
|
2019-11-16 15:05:39 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsWaitCancelled() const {
|
|
|
|
return wait_cancelled;
|
2019-11-16 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 14:17:35 +00:00
|
|
|
void ClearWaitCancelled() {
|
2021-01-20 21:42:27 +00:00
|
|
|
wait_cancelled = false;
|
2020-02-14 13:30:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsCancellable() const {
|
|
|
|
return cancellable;
|
2020-03-07 14:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:36:53 +00:00
|
|
|
void SetCancellable() {
|
2021-01-20 21:42:27 +00:00
|
|
|
cancellable = true;
|
2020-03-07 14:24:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:36:53 +00:00
|
|
|
void ClearCancellable() {
|
2021-01-20 21:42:27 +00:00
|
|
|
cancellable = false;
|
2020-03-07 16:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool IsTerminationRequested() const {
|
|
|
|
return termination_requested || GetRawState() == ThreadState::Terminated;
|
2020-03-07 16:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 06:24:20 +00:00
|
|
|
[[nodiscard]] virtual u64 GetId() const override final {
|
|
|
|
return this->GetThreadID();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] virtual bool IsInitialized() const override {
|
|
|
|
return initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] virtual uintptr_t GetPostDestroyArgument() const override {
|
|
|
|
return reinterpret_cast<uintptr_t>(parent) | (resource_limit_release_hint ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Finalize() override;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual bool IsSignaled() const override;
|
|
|
|
|
|
|
|
static void PostDestroy(uintptr_t arg);
|
|
|
|
|
|
|
|
[[nodiscard]] static ResultCode InitializeDummyThread(KThread* thread);
|
|
|
|
|
|
|
|
[[nodiscard]] static ResultCode InitializeIdleThread(Core::System& system, KThread* thread,
|
|
|
|
s32 virt_core);
|
|
|
|
|
|
|
|
[[nodiscard]] static ResultCode InitializeHighPriorityThread(Core::System& system,
|
|
|
|
KThread* thread,
|
|
|
|
KThreadFunction func,
|
|
|
|
uintptr_t arg, s32 virt_core);
|
|
|
|
|
|
|
|
[[nodiscard]] static ResultCode InitializeUserThread(Core::System& system, KThread* thread,
|
|
|
|
KThreadFunction func, uintptr_t arg,
|
|
|
|
VAddr user_stack_top, s32 prio,
|
|
|
|
s32 virt_core, Process* owner);
|
|
|
|
|
|
|
|
public:
|
2021-01-20 21:42:27 +00:00
|
|
|
struct StackParameters {
|
|
|
|
u8 svc_permission[0x10];
|
|
|
|
std::atomic<u8> dpc_flags;
|
|
|
|
u8 current_svc_id;
|
|
|
|
bool is_calling_svc;
|
|
|
|
bool is_in_exception_handler;
|
|
|
|
bool is_pinned;
|
|
|
|
s32 disable_count;
|
|
|
|
KThread* cur_thread;
|
|
|
|
};
|
2020-03-10 22:41:11 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] StackParameters& GetStackParameters() {
|
|
|
|
return stack_parameters;
|
2020-03-10 22:41:11 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const StackParameters& GetStackParameters() const {
|
|
|
|
return stack_parameters;
|
2020-03-07 18:16:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 08:17:18 +00:00
|
|
|
class QueueEntry {
|
2020-12-03 02:08:35 +00:00
|
|
|
public:
|
2020-12-05 08:17:18 +00:00
|
|
|
constexpr QueueEntry() = default;
|
2020-12-03 02:08:35 +00:00
|
|
|
|
|
|
|
constexpr void Initialize() {
|
2020-12-30 09:14:02 +00:00
|
|
|
prev = nullptr;
|
|
|
|
next = nullptr;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
constexpr KThread* GetPrev() const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return prev;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-31 07:01:08 +00:00
|
|
|
constexpr KThread* GetNext() const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return next;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-31 07:01:08 +00:00
|
|
|
constexpr void SetPrev(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
prev = thread;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-31 07:01:08 +00:00
|
|
|
constexpr void SetNext(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
next = thread;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-05 08:17:18 +00:00
|
|
|
|
|
|
|
private:
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread* prev{};
|
|
|
|
KThread* next{};
|
2020-12-03 02:08:35 +00:00
|
|
|
};
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] QueueEntry& GetPriorityQueueEntry(s32 core) {
|
2020-12-30 09:14:02 +00:00
|
|
|
return per_core_priority_queue_entry[core];
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
2020-12-05 08:17:18 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] const QueueEntry& GetPriorityQueueEntry(s32 core) const {
|
2020-12-30 09:14:02 +00:00
|
|
|
return per_core_priority_queue_entry[core];
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetSleepingQueue(KThreadQueue* q) {
|
|
|
|
sleeping_queue = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] s32 GetDisableDispatchCount() const {
|
|
|
|
return this->GetStackParameters().disable_count;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisableDispatch() {
|
2021-01-20 21:42:27 +00:00
|
|
|
ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() >= 0);
|
|
|
|
this->GetStackParameters().disable_count++;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnableDispatch() {
|
2021-01-20 21:42:27 +00:00
|
|
|
ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() > 0);
|
|
|
|
this->GetStackParameters().disable_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pin();
|
|
|
|
|
|
|
|
void Unpin();
|
|
|
|
|
|
|
|
void SetInExceptionHandler() {
|
|
|
|
this->GetStackParameters().is_in_exception_handler = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearInExceptionHandler() {
|
|
|
|
this->GetStackParameters().is_in_exception_handler = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool IsInExceptionHandler() const {
|
|
|
|
return this->GetStackParameters().is_in_exception_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIsCallingSvc() {
|
|
|
|
this->GetStackParameters().is_calling_svc = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearIsCallingSvc() {
|
|
|
|
this->GetStackParameters().is_calling_svc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool IsCallingSvc() const {
|
|
|
|
return this->GetStackParameters().is_calling_svc;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] u8 GetSvcId() const {
|
|
|
|
return this->GetStackParameters().current_svc_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterDpc(DpcFlag flag) {
|
|
|
|
this->GetStackParameters().dpc_flags |= static_cast<u8>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearDpc(DpcFlag flag) {
|
|
|
|
this->GetStackParameters().dpc_flags &= ~static_cast<u8>(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] u8 GetDpc() const {
|
|
|
|
return this->GetStackParameters().dpc_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool HasDpc() const {
|
|
|
|
return this->GetDpc() != 0;
|
2020-12-03 02:08:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 22:29:02 +00:00
|
|
|
void SetWaitReasonForDebugging(ThreadWaitReasonForDebugging reason) {
|
|
|
|
wait_reason_for_debugging = reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ThreadWaitReasonForDebugging GetWaitReasonForDebugging() const {
|
|
|
|
return wait_reason_for_debugging;
|
|
|
|
}
|
|
|
|
|
2021-01-25 06:55:08 +00:00
|
|
|
[[nodiscard]] ThreadType GetThreadTypeForDebugging() const {
|
|
|
|
return thread_type_for_debugging;
|
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
void SetWaitObjectsForDebugging(const std::span<KSynchronizationObject*>& objects) {
|
2020-12-22 06:36:53 +00:00
|
|
|
wait_objects_for_debugging.clear();
|
2020-12-30 09:14:02 +00:00
|
|
|
wait_objects_for_debugging.reserve(objects.size());
|
|
|
|
for (const auto& object : objects) {
|
|
|
|
wait_objects_for_debugging.emplace_back(object);
|
2020-12-22 06:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
[[nodiscard]] const std::vector<KSynchronizationObject*>& GetWaitObjectsForDebugging() const {
|
2020-12-22 06:36:53 +00:00
|
|
|
return wait_objects_for_debugging;
|
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
void SetMutexWaitAddressForDebugging(VAddr address) {
|
|
|
|
mutex_wait_address_for_debugging = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] VAddr GetMutexWaitAddressForDebugging() const {
|
|
|
|
return mutex_wait_address_for_debugging;
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] s32 GetIdealCoreForDebugging() const {
|
|
|
|
return virtual_ideal_core_id;
|
|
|
|
}
|
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void AddWaiter(KThread* thread);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void RemoveWaiter(KThread* thread);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] ResultCode GetThreadContext3(std::vector<u8>& out);
|
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
[[nodiscard]] KThread* RemoveWaiterByKey(s32* out_num_waiters, VAddr key);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
|
|
|
[[nodiscard]] VAddr GetAddressKey() const {
|
|
|
|
return address_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] u32 GetAddressKeyValue() const {
|
|
|
|
return address_key_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddressKey(VAddr key) {
|
|
|
|
address_key = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddressKey(VAddr key, u32 val) {
|
|
|
|
address_key = key;
|
|
|
|
address_key_value = val;
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
[[nodiscard]] bool HasWaiters() const {
|
|
|
|
return !waiter_list.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] s32 GetNumKernelWaiters() const {
|
|
|
|
return num_kernel_waiters;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] u64 GetConditionVariableKey() const {
|
|
|
|
return condvar_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] u64 GetAddressArbiterKey() const {
|
|
|
|
return condvar_key;
|
|
|
|
}
|
|
|
|
|
2018-10-03 22:47:57 +00:00
|
|
|
private:
|
2020-12-30 09:14:02 +00:00
|
|
|
static constexpr size_t PriorityInheritanceCountMax = 10;
|
|
|
|
union SyncObjectBuffer {
|
|
|
|
std::array<KSynchronizationObject*, Svc::ArgumentHandleCountMax> sync_objects{};
|
|
|
|
std::array<Handle,
|
|
|
|
Svc::ArgumentHandleCountMax*(sizeof(KSynchronizationObject*) / sizeof(Handle))>
|
|
|
|
handles;
|
|
|
|
constexpr SyncObjectBuffer() {}
|
|
|
|
};
|
|
|
|
static_assert(sizeof(SyncObjectBuffer::sync_objects) == sizeof(SyncObjectBuffer::handles));
|
|
|
|
|
|
|
|
struct ConditionVariableComparator {
|
|
|
|
struct LightCompareType {
|
|
|
|
u64 cv_key{};
|
|
|
|
s32 priority{};
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr u64 GetConditionVariableKey() const {
|
|
|
|
return cv_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr s32 GetPriority() const {
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
requires(
|
2020-12-31 07:01:08 +00:00
|
|
|
std::same_as<T, KThread> ||
|
2020-12-30 09:14:02 +00:00
|
|
|
std::same_as<T, LightCompareType>) static constexpr int Compare(const T& lhs,
|
2020-12-31 07:01:08 +00:00
|
|
|
const KThread& rhs) {
|
2021-01-20 21:42:27 +00:00
|
|
|
const u64 l_key = lhs.GetConditionVariableKey();
|
|
|
|
const u64 r_key = rhs.GetConditionVariableKey();
|
2020-12-30 09:14:02 +00:00
|
|
|
|
|
|
|
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-01-20 21:42:27 +00:00
|
|
|
void AddWaiterImpl(KThread* thread);
|
|
|
|
|
|
|
|
void RemoveWaiterImpl(KThread* thread);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void StartTermination();
|
|
|
|
|
|
|
|
[[nodiscard]] ResultCode Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top,
|
|
|
|
s32 prio, s32 virt_core, Process* owner, ThreadType type);
|
|
|
|
|
|
|
|
[[nodiscard]] static ResultCode InitializeThread(KThread* thread, KThreadFunction func,
|
|
|
|
uintptr_t arg, VAddr user_stack_top, s32 prio,
|
2021-04-03 06:24:20 +00:00
|
|
|
s32 core, Process* owner, ThreadType type,
|
|
|
|
std::function<void(void*)>&& init_func,
|
|
|
|
void* init_func_parameter);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
static void RestorePriority(KernelCore& kernel, KThread* thread);
|
|
|
|
|
|
|
|
// For core KThread implementation
|
|
|
|
ThreadContext32 thread_context_32{};
|
|
|
|
ThreadContext64 thread_context_64{};
|
|
|
|
Common::IntrusiveRedBlackTreeNode condvar_arbiter_tree_node{};
|
|
|
|
s32 priority{};
|
2020-12-30 09:14:02 +00:00
|
|
|
using ConditionVariableThreadTreeTraits =
|
2020-12-31 07:01:08 +00:00
|
|
|
Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
|
|
|
|
&KThread::condvar_arbiter_tree_node>;
|
2020-12-30 09:14:02 +00:00
|
|
|
using ConditionVariableThreadTree =
|
|
|
|
ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;
|
2021-01-20 21:42:27 +00:00
|
|
|
ConditionVariableThreadTree* condvar_tree{};
|
|
|
|
u64 condvar_key{};
|
|
|
|
u64 virtual_affinity_mask{};
|
|
|
|
KAffinityMask physical_affinity_mask{};
|
|
|
|
u64 thread_id{};
|
|
|
|
std::atomic<s64> cpu_time{};
|
|
|
|
KSynchronizationObject* synced_object{};
|
|
|
|
VAddr address_key{};
|
|
|
|
Process* parent{};
|
|
|
|
VAddr kernel_stack_top{};
|
|
|
|
u32* light_ipc_data{};
|
|
|
|
VAddr tls_address{};
|
|
|
|
KLightLock activity_pause_lock;
|
|
|
|
s64 schedule_count{};
|
|
|
|
s64 last_scheduled_tick{};
|
|
|
|
std::array<QueueEntry, Core::Hardware::NUM_CPU_CORES> per_core_priority_queue_entry{};
|
|
|
|
KThreadQueue* sleeping_queue{};
|
|
|
|
WaiterList waiter_list{};
|
|
|
|
WaiterList pinned_waiter_list{};
|
|
|
|
KThread* lock_owner{};
|
|
|
|
u32 address_key_value{};
|
|
|
|
u32 suspend_request_flags{};
|
|
|
|
u32 suspend_allowed_flags{};
|
|
|
|
ResultCode wait_result{RESULT_SUCCESS};
|
|
|
|
s32 base_priority{};
|
|
|
|
s32 physical_ideal_core_id{};
|
|
|
|
s32 virtual_ideal_core_id{};
|
|
|
|
s32 num_kernel_waiters{};
|
|
|
|
s32 current_core_id{};
|
|
|
|
s32 core_id{};
|
|
|
|
KAffinityMask original_physical_affinity_mask{};
|
|
|
|
s32 original_physical_ideal_core_id{};
|
|
|
|
s32 num_core_migration_disables{};
|
|
|
|
ThreadState thread_state{};
|
|
|
|
std::atomic<bool> termination_requested{};
|
|
|
|
bool wait_cancelled{};
|
|
|
|
bool cancellable{};
|
|
|
|
bool signaled{};
|
|
|
|
bool initialized{};
|
|
|
|
bool debug_attached{};
|
|
|
|
s8 priority_inheritance_count{};
|
|
|
|
bool resource_limit_release_hint{};
|
|
|
|
StackParameters stack_parameters{};
|
2021-02-13 09:29:32 +00:00
|
|
|
KSpinLock context_guard{};
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// For emulation
|
2021-03-06 01:08:17 +00:00
|
|
|
std::shared_ptr<Common::Fiber> host_context{};
|
2020-02-25 02:04:12 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// For debugging
|
|
|
|
std::vector<KSynchronizationObject*> wait_objects_for_debugging;
|
|
|
|
VAddr mutex_wait_address_for_debugging{};
|
|
|
|
ThreadWaitReasonForDebugging wait_reason_for_debugging{};
|
2021-01-25 06:55:08 +00:00
|
|
|
ThreadType thread_type_for_debugging{};
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
public:
|
|
|
|
using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetConditionVariable(ConditionVariableThreadTree* tree, VAddr address, u64 cv_key,
|
2020-12-30 09:14:02 +00:00
|
|
|
u32 value) {
|
|
|
|
condvar_tree = tree;
|
|
|
|
condvar_key = cv_key;
|
|
|
|
address_key = address;
|
|
|
|
address_key_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearConditionVariable() {
|
|
|
|
condvar_tree = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool IsWaitingForConditionVariable() const {
|
|
|
|
return condvar_tree != nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void SetAddressArbiter(ConditionVariableThreadTree* tree, u64 address) {
|
2020-12-30 09:14:02 +00:00
|
|
|
condvar_tree = tree;
|
|
|
|
condvar_key = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearAddressArbiter() {
|
|
|
|
condvar_tree = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool IsWaitingForAddressArbiter() const {
|
|
|
|
return condvar_tree != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ConditionVariableThreadTree* GetConditionVariableTree() const {
|
|
|
|
return condvar_tree;
|
|
|
|
}
|
2014-12-22 06:32:03 +00:00
|
|
|
};
|
|
|
|
|
2017-09-26 22:40:49 +00:00
|
|
|
} // namespace Kernel
|