2021-01-20 13:42:27 -08:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
2014-12-16 21:38:14 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 00:49:13 -08:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-09 19:11:18 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-12-05 00:17:18 -08:00
|
|
|
#include <array>
|
2020-12-30 01:14:02 -08:00
|
|
|
#include <span>
|
2014-12-21 22:32:03 -08:00
|
|
|
#include <string>
|
2020-03-10 10:13:39 -07:00
|
|
|
#include <utility>
|
2014-12-21 22:32:03 -08:00
|
|
|
#include <vector>
|
2018-07-31 05:06:09 -07:00
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
|
2014-05-09 19:11:18 -07:00
|
|
|
#include "common/common_types.h"
|
2020-12-30 01:14:02 -08:00
|
|
|
#include "common/intrusive_red_black_tree.h"
|
2020-02-24 18:04:12 -08:00
|
|
|
#include "common/spin_lock.h"
|
2016-12-21 21:08:09 -08:00
|
|
|
#include "core/arm/arm_interface.h"
|
2020-11-16 21:02:45 -08:00
|
|
|
#include "core/hle/kernel/k_affinity_mask.h"
|
2021-01-20 13:42:27 -08:00
|
|
|
#include "core/hle/kernel/k_light_lock.h"
|
2020-12-21 22:36:53 -08:00
|
|
|
#include "core/hle/kernel/k_synchronization_object.h"
|
2018-08-01 19:40:00 -07:00
|
|
|
#include "core/hle/kernel/object.h"
|
2020-12-30 01:14:02 -08:00
|
|
|
#include "core/hle/kernel/svc_common.h"
|
2021-01-20 13:42:27 -08:00
|
|
|
#include "core/hle/kernel/svc_types.h"
|
2014-10-22 20:20:01 -07:00
|
|
|
#include "core/hle/result.h"
|
2014-05-09 19:11:18 -07:00
|
|
|
|
2020-02-24 18:04:12 -08:00
|
|
|
namespace Common {
|
|
|
|
class Fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Core {
|
2020-03-01 08:14:17 -08:00
|
|
|
class ARM_Interface;
|
2020-02-24 18:04:12 -08:00
|
|
|
class System;
|
2020-03-03 09:37:11 -08:00
|
|
|
} // namespace Core
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2018-09-13 12:57:45 -07:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-02 18:08:35 -08:00
|
|
|
class GlobalSchedulerContext;
|
2018-09-13 12:57:45 -07:00
|
|
|
class KernelCore;
|
|
|
|
class Process;
|
2020-12-02 18:08:35 -08:00
|
|
|
class KScheduler;
|
2021-01-20 13:42:27 -08:00
|
|
|
class KThreadQueue;
|
|
|
|
|
|
|
|
using KThreadFunction = VAddr;
|
2018-09-13 12:57:45 -07:00
|
|
|
|
2021-01-01 01:04:30 -08:00
|
|
|
enum class ThreadType : u32 {
|
|
|
|
Main = 0,
|
|
|
|
Kernel = 1,
|
|
|
|
HighPriority = 2,
|
|
|
|
User = 3,
|
2014-05-16 21:56:00 -07:00
|
|
|
};
|
2021-01-01 01:04:30 -08:00
|
|
|
DECLARE_ENUM_FLAG_OPERATORS(ThreadType);
|
2014-05-16 21:56:00 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
enum class SuspendType : u32 {
|
|
|
|
Process = 0,
|
|
|
|
Thread = 1,
|
|
|
|
Debug = 2,
|
|
|
|
Backtrace = 3,
|
|
|
|
Init = 4,
|
|
|
|
|
|
|
|
Count,
|
|
|
|
};
|
|
|
|
|
2020-12-28 13:16:43 -08: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 15:50:36 -07:00
|
|
|
};
|
2020-12-28 13:16:43 -08:00
|
|
|
DECLARE_ENUM_FLAG_OPERATORS(ThreadState);
|
2014-05-22 15:50:36 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
enum class DpcFlag : u32 {
|
|
|
|
Terminating = (1 << 0),
|
|
|
|
Terminated = (1 << 1),
|
2019-03-29 14:01:46 -07:00
|
|
|
};
|
|
|
|
|
2021-01-10 14:29:02 -08: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 13:42:27 -08:00
|
|
|
[[nodiscard]] KThread* GetCurrentThreadPointer(KernelCore& kernel);
|
|
|
|
[[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel);
|
|
|
|
[[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel);
|
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
class KThread final : public KSynchronizationObject, public boost::intrusive::list_base_hook<> {
|
2020-12-30 01:14:02 -08:00
|
|
|
friend class KScheduler;
|
|
|
|
friend class Process;
|
|
|
|
|
2014-12-21 22:32:03 -08:00
|
|
|
public:
|
2021-01-01 02:06:06 -08:00
|
|
|
static constexpr s32 DefaultThreadPriority = 44;
|
2021-01-20 13:42:27 -08:00
|
|
|
static constexpr s32 IdleThreadPriority = Svc::LowestThreadPriority + 1;
|
2021-01-01 02:06:06 -08:00
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
explicit KThread(KernelCore& kernel);
|
|
|
|
~KThread() override;
|
2019-11-24 17:15:51 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
public:
|
2020-03-01 20:46:10 -08:00
|
|
|
using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
|
|
|
|
using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
|
2021-01-20 13:42:27 -08:00
|
|
|
using WaiterList = boost::intrusive::list<KThread>;
|
2018-10-03 15:47:57 -07:00
|
|
|
|
2020-02-25 15:43:28 -08:00
|
|
|
/**
|
|
|
|
* Creates and returns a new thread. The new thread is immediately scheduled
|
|
|
|
* @param system The instance of the whole system
|
|
|
|
* @param name The friendly name desired for the thread
|
|
|
|
* @param entry_point The address at which the thread should start execution
|
|
|
|
* @param priority The thread's priority
|
|
|
|
* @param arg User data to pass to the thread
|
|
|
|
* @param processor_id The ID(s) of the processors on which the thread is desired to be run
|
|
|
|
* @param stack_top The address of the thread's stack top
|
|
|
|
* @param owner_process The parent process for the thread, if null, it's a kernel thread
|
|
|
|
* @return A shared pointer to the newly created thread
|
|
|
|
*/
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] static ResultVal<std::shared_ptr<KThread>> Create(
|
|
|
|
Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point,
|
|
|
|
u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process);
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2015-01-25 22:56:17 -08:00
|
|
|
/**
|
|
|
|
* Creates and returns a new thread. The new thread is immediately scheduled
|
2020-02-24 18:04:12 -08:00
|
|
|
* @param system The instance of the whole system
|
2015-01-25 22:56:17 -08:00
|
|
|
* @param name The friendly name desired for the thread
|
|
|
|
* @param entry_point The address at which the thread should start execution
|
|
|
|
* @param priority The thread's priority
|
|
|
|
* @param arg User data to pass to the thread
|
|
|
|
* @param processor_id The ID(s) of the processors on which the thread is desired to be run
|
|
|
|
* @param stack_top The address of the thread's stack top
|
2020-02-24 18:04:12 -08:00
|
|
|
* @param owner_process The parent process for the thread, if null, it's a kernel thread
|
|
|
|
* @param thread_start_func The function where the host context will start.
|
|
|
|
* @param thread_start_parameter The parameter which will passed to host context on init
|
2015-01-25 22:56:17 -08:00
|
|
|
* @return A shared pointer to the newly created thread
|
|
|
|
*/
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] static ResultVal<std::shared_ptr<KThread>> Create(
|
2020-12-30 23:01:08 -08:00
|
|
|
Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point,
|
|
|
|
u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process,
|
|
|
|
std::function<void(void*)>&& thread_start_func, void* thread_start_parameter);
|
2014-12-22 05:07:22 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] std::string GetName() const override {
|
2016-09-17 17:38:01 -07:00
|
|
|
return name;
|
|
|
|
}
|
2019-04-15 12:54:25 -07:00
|
|
|
|
|
|
|
void SetName(std::string new_name) {
|
|
|
|
name = std::move(new_name);
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] std::string GetTypeName() const override {
|
2016-09-17 17:38:01 -07:00
|
|
|
return "Thread";
|
|
|
|
}
|
2014-12-21 22:32:03 -08:00
|
|
|
|
2019-04-11 13:30:52 -07:00
|
|
|
static constexpr HandleType HANDLE_TYPE = HandleType::Thread;
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] HandleType GetHandleType() const override {
|
2016-09-17 17:38:01 -07:00
|
|
|
return HANDLE_TYPE;
|
|
|
|
}
|
2014-12-21 22:32:03 -08:00
|
|
|
|
2015-01-25 22:56:17 -08:00
|
|
|
/**
|
|
|
|
* Gets the thread's current priority
|
|
|
|
* @return The current thread's priority
|
|
|
|
*/
|
2020-12-30 01:14:02 -08:00
|
|
|
[[nodiscard]] s32 GetPriority() const {
|
2021-01-20 13:42:27 -08:00
|
|
|
return priority;
|
2016-09-17 17:38:01 -07:00
|
|
|
}
|
2015-01-25 22:56:17 -08:00
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
/**
|
|
|
|
* Sets the thread's current priority.
|
|
|
|
* @param priority The new priority.
|
|
|
|
*/
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetPriority(s32 value) {
|
|
|
|
priority = value;
|
2020-12-30 01:14:02 -08:00
|
|
|
}
|
|
|
|
|
2018-10-03 15:47:57 -07:00
|
|
|
/**
|
|
|
|
* Gets the thread's nominal priority.
|
|
|
|
* @return The current thread's nominal priority.
|
|
|
|
*/
|
2020-12-30 01:14:02 -08:00
|
|
|
[[nodiscard]] s32 GetBasePriority() const {
|
|
|
|
return base_priority;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-25 22:56:17 -08:00
|
|
|
/**
|
|
|
|
* Gets the thread's thread ID
|
|
|
|
* @return The thread's ID
|
|
|
|
*/
|
2020-12-30 01:14:02 -08:00
|
|
|
[[nodiscard]] u64 GetThreadID() const {
|
2016-09-17 17:38:01 -07:00
|
|
|
return thread_id;
|
|
|
|
}
|
2015-05-25 11:34:09 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void ContinueIfHasKernelWaiters() {
|
|
|
|
if (GetNumKernelWaiters() > 0) {
|
|
|
|
Continue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 22:36:53 -08:00
|
|
|
void Wakeup();
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetBasePriority(s32 value);
|
2020-02-25 08:40:33 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ResultCode Run();
|
2020-12-21 22:36:53 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void Exit();
|
2019-04-17 04:08:12 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] u32 GetSuspendFlags() const {
|
|
|
|
return suspend_allowed_flags & suspend_request_flags;
|
|
|
|
}
|
2020-02-25 12:38:33 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsSuspended() const {
|
|
|
|
return GetSuspendFlags() != 0;
|
2020-02-25 12:38:33 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08: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 12:38:33 -08:00
|
|
|
}
|
2015-01-16 23:03:44 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsSuspendRequested() const {
|
|
|
|
return suspend_request_flags != 0;
|
2020-12-21 22:36:53 -08:00
|
|
|
}
|
2016-12-03 19:38:14 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void RequestSuspend(SuspendType type);
|
|
|
|
|
|
|
|
void Resume(SuspendType type);
|
|
|
|
|
|
|
|
void TrySuspend();
|
|
|
|
|
|
|
|
void Continue();
|
|
|
|
|
|
|
|
void Suspend();
|
|
|
|
|
|
|
|
void Finalize() override;
|
|
|
|
|
|
|
|
bool IsSignaled() const override;
|
|
|
|
|
|
|
|
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-25 22:56:17 -08:00
|
|
|
|
2015-05-10 16:35:37 -07:00
|
|
|
/*
|
|
|
|
* Returns the Thread Local Storage address of the current thread
|
|
|
|
* @returns VAddr of the thread's TLS
|
|
|
|
*/
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] VAddr GetTLSAddress() const {
|
2016-09-17 17:38:01 -07:00
|
|
|
return tls_address;
|
|
|
|
}
|
2015-05-10 16:35:37 -07:00
|
|
|
|
2018-07-20 17:57:45 -07: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 13:42:27 -08:00
|
|
|
[[nodiscard]] u64 GetTPIDR_EL0() const {
|
|
|
|
return thread_context_64.tpidr;
|
2018-07-20 17:57:45 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 15:47:57 -07:00
|
|
|
/// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
|
|
|
|
void SetTPIDR_EL0(u64 value) {
|
2021-01-20 13:42:27 -08:00
|
|
|
thread_context_64.tpidr = value;
|
|
|
|
thread_context_32.tpidr = static_cast<u32>(value);
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ThreadContext32& GetContext32() {
|
|
|
|
return thread_context_32;
|
2020-03-01 20:46:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const ThreadContext32& GetContext32() const {
|
|
|
|
return thread_context_32;
|
2020-03-01 20:46:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ThreadContext64& GetContext64() {
|
|
|
|
return thread_context_64;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const ThreadContext64& GetContext64() const {
|
|
|
|
return thread_context_64;
|
2020-03-12 13:48:43 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] std::shared_ptr<Common::Fiber>& GetHostContext();
|
2020-03-12 13:48:43 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ThreadState GetState() const {
|
2020-12-28 13:16:43 -08:00
|
|
|
return thread_state & ThreadState::Mask;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ThreadState GetRawState() const {
|
2020-12-28 13:16:43 -08:00
|
|
|
return thread_state;
|
|
|
|
}
|
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
void SetState(ThreadState state);
|
2018-10-03 15:47:57 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] s64 GetLastScheduledTick() const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return last_scheduled_tick;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-05 00:17:18 -08:00
|
|
|
|
|
|
|
void SetLastScheduledTick(s64 tick) {
|
2020-12-30 01:14:02 -08:00
|
|
|
last_scheduled_tick = tick;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08: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 15:42:50 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] s64 GetCpuTime() const {
|
|
|
|
return cpu_time;
|
2018-10-25 15:42:50 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] s32 GetActiveCore() const {
|
|
|
|
return core_id;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetActiveCore(s32 core) {
|
|
|
|
core_id = core;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] s32 GetCurrentCore() const {
|
|
|
|
return current_core_id;
|
2019-03-29 14:01:46 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetCurrentCore(s32 core) {
|
|
|
|
current_core_id = core;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] Process* GetOwnerProcess() {
|
|
|
|
return parent;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const Process* GetOwnerProcess() const {
|
|
|
|
return parent;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsUserThread() const {
|
|
|
|
return parent != nullptr;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] KThread* GetLockOwner() const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return lock_owner;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
2014-12-21 22:32:03 -08:00
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
void SetLockOwner(KThread* owner) {
|
2020-12-30 01:14:02 -08:00
|
|
|
lock_owner = owner;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
2014-12-21 22:32:03 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const KAffinityMask& GetAffinityMask() const {
|
|
|
|
return physical_affinity_mask;
|
2018-10-03 15:47:57 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08: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 15:47:57 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ResultCode SetCoreMask(s32 core_id, u64 v_affinity_mask);
|
2018-12-03 09:25:27 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ResultCode SetActivity(Svc::ThreadActivity activity);
|
2019-03-15 20:28:29 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ResultCode Sleep(s64 timeout);
|
|
|
|
|
|
|
|
[[nodiscard]] s64 GetYieldScheduleCount() const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return schedule_count;
|
2019-09-11 09:14:37 -07:00
|
|
|
}
|
2020-12-05 00:17:18 -08:00
|
|
|
|
|
|
|
void SetYieldScheduleCount(s64 count) {
|
2020-12-30 01:14:02 -08:00
|
|
|
schedule_count = count;
|
2019-09-11 09:14:37 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void WaitCancel();
|
2019-11-16 07:05:39 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsWaitCancelled() const {
|
|
|
|
return wait_cancelled;
|
2019-11-16 07:05:39 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] void ClearWaitCancelled() {
|
|
|
|
wait_cancelled = false;
|
2020-02-14 05:30:53 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsCancellable() const {
|
|
|
|
return cancellable;
|
2020-03-07 06:24:46 -08:00
|
|
|
}
|
|
|
|
|
2020-12-21 22:36:53 -08:00
|
|
|
void SetCancellable() {
|
2021-01-20 13:42:27 -08:00
|
|
|
cancellable = true;
|
2020-03-07 06:24:46 -08:00
|
|
|
}
|
|
|
|
|
2020-12-21 22:36:53 -08:00
|
|
|
void ClearCancellable() {
|
2021-01-20 13:42:27 -08:00
|
|
|
cancellable = false;
|
2020-03-07 08:44:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] bool IsTerminationRequested() const {
|
|
|
|
return termination_requested || GetRawState() == ThreadState::Terminated;
|
2020-03-07 08:44:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08: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 15:41:11 -07:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] StackParameters& GetStackParameters() {
|
|
|
|
return stack_parameters;
|
2020-03-10 15:41:11 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const StackParameters& GetStackParameters() const {
|
|
|
|
return stack_parameters;
|
2020-03-07 10:16:25 -08:00
|
|
|
}
|
|
|
|
|
2020-12-05 00:17:18 -08:00
|
|
|
class QueueEntry {
|
2020-12-02 18:08:35 -08:00
|
|
|
public:
|
2020-12-05 00:17:18 -08:00
|
|
|
constexpr QueueEntry() = default;
|
2020-12-02 18:08:35 -08:00
|
|
|
|
|
|
|
constexpr void Initialize() {
|
2020-12-30 01:14:02 -08:00
|
|
|
prev = nullptr;
|
|
|
|
next = nullptr;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
constexpr KThread* GetPrev() const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return prev;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-30 23:01:08 -08:00
|
|
|
constexpr KThread* GetNext() const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return next;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-30 23:01:08 -08:00
|
|
|
constexpr void SetPrev(KThread* thread) {
|
2020-12-30 01:14:02 -08:00
|
|
|
prev = thread;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-30 23:01:08 -08:00
|
|
|
constexpr void SetNext(KThread* thread) {
|
2020-12-30 01:14:02 -08:00
|
|
|
next = thread;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-05 00:17:18 -08:00
|
|
|
|
|
|
|
private:
|
2020-12-30 23:01:08 -08:00
|
|
|
KThread* prev{};
|
|
|
|
KThread* next{};
|
2020-12-02 18:08:35 -08:00
|
|
|
};
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] QueueEntry& GetPriorityQueueEntry(s32 core) {
|
2020-12-30 01:14:02 -08:00
|
|
|
return per_core_priority_queue_entry[core];
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
2020-12-05 00:17:18 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] const QueueEntry& GetPriorityQueueEntry(s32 core) const {
|
2020-12-30 01:14:02 -08:00
|
|
|
return per_core_priority_queue_entry[core];
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetSleepingQueue(KThreadQueue* q) {
|
|
|
|
sleeping_queue = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] s32 GetDisableDispatchCount() const {
|
|
|
|
return this->GetStackParameters().disable_count;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisableDispatch() {
|
2021-01-20 13:42:27 -08:00
|
|
|
ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() >= 0);
|
|
|
|
this->GetStackParameters().disable_count++;
|
2020-12-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EnableDispatch() {
|
2021-01-20 13:42:27 -08: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-02 18:08:35 -08:00
|
|
|
}
|
|
|
|
|
2021-01-10 14:29:02 -08:00
|
|
|
void SetWaitReasonForDebugging(ThreadWaitReasonForDebugging reason) {
|
|
|
|
wait_reason_for_debugging = reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ThreadWaitReasonForDebugging GetWaitReasonForDebugging() const {
|
|
|
|
return wait_reason_for_debugging;
|
|
|
|
}
|
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
void SetWaitObjectsForDebugging(const std::span<KSynchronizationObject*>& objects) {
|
2020-12-21 22:36:53 -08:00
|
|
|
wait_objects_for_debugging.clear();
|
2020-12-30 01:14:02 -08:00
|
|
|
wait_objects_for_debugging.reserve(objects.size());
|
|
|
|
for (const auto& object : objects) {
|
|
|
|
wait_objects_for_debugging.emplace_back(object);
|
2020-12-21 22:36:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
[[nodiscard]] const std::vector<KSynchronizationObject*>& GetWaitObjectsForDebugging() const {
|
2020-12-21 22:36:53 -08:00
|
|
|
return wait_objects_for_debugging;
|
|
|
|
}
|
|
|
|
|
2020-12-30 01:14:02 -08:00
|
|
|
void SetMutexWaitAddressForDebugging(VAddr address) {
|
|
|
|
mutex_wait_address_for_debugging = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] VAddr GetMutexWaitAddressForDebugging() const {
|
|
|
|
return mutex_wait_address_for_debugging;
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] s32 GetIdealCoreForDebugging() const {
|
|
|
|
return virtual_ideal_core_id;
|
|
|
|
}
|
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
void AddWaiter(KThread* thread);
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
void RemoveWaiter(KThread* thread);
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
[[nodiscard]] ResultCode GetThreadContext3(std::vector<u8>& out);
|
|
|
|
|
2020-12-30 23:01:08 -08:00
|
|
|
[[nodiscard]] KThread* RemoveWaiterByKey(s32* out_num_waiters, VAddr key);
|
2020-12-30 01:14:02 -08: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 13:42:27 -08: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 15:47:57 -07:00
|
|
|
private:
|
2020-12-30 01:14:02 -08: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-30 23:01:08 -08:00
|
|
|
std::same_as<T, KThread> ||
|
2020-12-30 01:14:02 -08:00
|
|
|
std::same_as<T, LightCompareType>) static constexpr int Compare(const T& lhs,
|
2020-12-30 23:01:08 -08:00
|
|
|
const KThread& rhs) {
|
2021-01-20 13:42:27 -08:00
|
|
|
const u64 l_key = lhs.GetConditionVariableKey();
|
|
|
|
const u64 r_key = rhs.GetConditionVariableKey();
|
2020-12-30 01:14:02 -08: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 13:42:27 -08:00
|
|
|
void AddWaiterImpl(KThread* thread);
|
|
|
|
|
|
|
|
void RemoveWaiterImpl(KThread* thread);
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2021-01-20 13:42:27 -08: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,
|
|
|
|
s32 core, Process* owner, ThreadType type);
|
|
|
|
|
|
|
|
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 01:14:02 -08:00
|
|
|
using ConditionVariableThreadTreeTraits =
|
2020-12-30 23:01:08 -08:00
|
|
|
Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
|
|
|
|
&KThread::condvar_arbiter_tree_node>;
|
2020-12-30 01:14:02 -08:00
|
|
|
using ConditionVariableThreadTree =
|
|
|
|
ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;
|
2021-01-20 13:42:27 -08: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{};
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
// For emulation
|
|
|
|
std::shared_ptr<Common::Fiber> host_context{};
|
2020-02-24 18:04:12 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
// For debugging
|
|
|
|
std::vector<KSynchronizationObject*> wait_objects_for_debugging;
|
|
|
|
VAddr mutex_wait_address_for_debugging{};
|
|
|
|
ThreadWaitReasonForDebugging wait_reason_for_debugging{};
|
|
|
|
std::string name;
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
public:
|
|
|
|
using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
|
2020-12-30 01:14:02 -08:00
|
|
|
|
2021-01-20 13:42:27 -08:00
|
|
|
void SetConditionVariable(ConditionVariableThreadTree* tree, VAddr address, u64 cv_key,
|
2020-12-30 01:14:02 -08: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 13:42:27 -08:00
|
|
|
void SetAddressArbiter(ConditionVariableThreadTree* tree, u64 address) {
|
2020-12-30 01:14:02 -08: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-21 22:32:03 -08:00
|
|
|
};
|
|
|
|
|
2017-09-26 15:40:49 -07:00
|
|
|
} // namespace Kernel
|