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
|
|
|
|
2014-06-06 02:35:36 +00:00
|
|
|
#include <algorithm>
|
2018-02-14 05:33:15 +00:00
|
|
|
#include <cinttypes>
|
2018-10-30 04:03:25 +00:00
|
|
|
#include <optional>
|
2014-08-18 03:03:22 +00:00
|
|
|
#include <vector>
|
2018-07-31 12:06:09 +00:00
|
|
|
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/assert.h"
|
2021-01-20 21:42:27 +00:00
|
|
|
#include "common/bit_util.h"
|
2021-01-01 10:06:06 +00:00
|
|
|
#include "common/common_funcs.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/common_types.h"
|
2020-02-25 02:04:12 +00:00
|
|
|
#include "common/fiber.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/logging/log.h"
|
2021-08-14 09:14:19 +00:00
|
|
|
#include "common/scope_exit.h"
|
|
|
|
#include "common/settings.h"
|
|
|
|
#include "common/thread_queue_list.h"
|
2014-05-14 02:00:11 +00:00
|
|
|
#include "core/core.h"
|
2020-02-25 02:04:12 +00:00
|
|
|
#include "core/cpu_manager.h"
|
2020-02-11 23:56:24 +00:00
|
|
|
#include "core/hardware_properties.h"
|
2020-12-30 09:14:02 +00:00
|
|
|
#include "core/hle/kernel/k_condition_variable.h"
|
2021-04-24 09:40:31 +00:00
|
|
|
#include "core/hle/kernel/k_handle_table.h"
|
2021-02-13 00:02:35 +00:00
|
|
|
#include "core/hle/kernel/k_memory_layout.h"
|
2021-04-24 05:04:28 +00:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2021-01-30 09:40:49 +00:00
|
|
|
#include "core/hle/kernel/k_resource_limit.h"
|
2020-12-03 02:08:35 +00:00
|
|
|
#include "core/hle/kernel/k_scheduler.h"
|
2020-12-04 05:56:02 +00:00
|
|
|
#include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
|
2020-12-31 07:01:08 +00:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2021-01-20 21:42:27 +00:00
|
|
|
#include "core/hle/kernel/k_thread_queue.h"
|
2018-08-28 16:30:33 +00:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
2021-01-01 10:06:06 +00:00
|
|
|
#include "core/hle/kernel/svc_results.h"
|
2020-02-25 02:04:12 +00:00
|
|
|
#include "core/hle/kernel/time_manager.h"
|
2014-10-23 03:20:01 +00:00
|
|
|
#include "core/hle/result.h"
|
2014-05-10 02:11:18 +00:00
|
|
|
|
2020-07-15 17:13:31 +00:00
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
#include "core/arm/dynarmic/arm_dynarmic_32.h"
|
|
|
|
#endif
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
namespace {
|
|
|
|
static void ResetThreadContext32(Core::ARM_Interface::ThreadContext32& context, u32 stack_top,
|
|
|
|
u32 entry_point, u32 arg) {
|
|
|
|
context = {};
|
|
|
|
context.cpu_registers[0] = arg;
|
|
|
|
context.cpu_registers[15] = entry_point;
|
|
|
|
context.cpu_registers[13] = stack_top;
|
|
|
|
}
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
static void ResetThreadContext64(Core::ARM_Interface::ThreadContext64& context, VAddr stack_top,
|
|
|
|
VAddr entry_point, u64 arg) {
|
|
|
|
context = {};
|
|
|
|
context.cpu_registers[0] = arg;
|
|
|
|
context.pc = entry_point;
|
|
|
|
context.sp = stack_top;
|
|
|
|
// TODO(merry): Perform a hardware test to determine the below value.
|
|
|
|
context.fpcr = 0;
|
2020-02-11 21:36:39 +00:00
|
|
|
}
|
2021-01-20 21:42:27 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace Kernel {
|
2020-02-11 21:36:39 +00:00
|
|
|
|
2021-11-11 06:28:30 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ThreadQueueImplForKThreadSleep final : public KThreadQueueWithoutEndWait {
|
|
|
|
public:
|
|
|
|
explicit ThreadQueueImplForKThreadSleep(KernelCore& kernel_)
|
|
|
|
: KThreadQueueWithoutEndWait(kernel_) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadQueueImplForKThreadSetProperty final : public KThreadQueue {
|
|
|
|
public:
|
|
|
|
explicit ThreadQueueImplForKThreadSetProperty(KernelCore& kernel_, KThread::WaiterList* wl)
|
2021-11-21 10:29:53 +00:00
|
|
|
: KThreadQueue(kernel_), m_wait_list(wl) {}
|
2021-11-11 06:28:30 +00:00
|
|
|
|
|
|
|
virtual void CancelWait(KThread* waiting_thread, ResultCode wait_result,
|
|
|
|
bool cancel_timer_task) override {
|
|
|
|
// Remove the thread from the wait list.
|
|
|
|
m_wait_list->erase(m_wait_list->iterator_to(*waiting_thread));
|
|
|
|
|
|
|
|
// Invoke the base cancel wait handler.
|
|
|
|
KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
|
|
|
|
}
|
2021-11-21 10:29:53 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
KThread::WaiterList* m_wait_list;
|
2021-11-11 06:28:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-05-08 16:11:36 +00:00
|
|
|
KThread::KThread(KernelCore& kernel_)
|
|
|
|
: KAutoObjectWithSlabHeapAndContainer{kernel_}, activity_pause_lock{kernel_} {}
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread::~KThread() = default;
|
2015-01-31 21:22:40 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio,
|
2021-04-24 05:04:28 +00:00
|
|
|
s32 virt_core, KProcess* owner, ThreadType type) {
|
2021-01-20 21:42:27 +00:00
|
|
|
// Assert parameters are valid.
|
|
|
|
ASSERT((type == ThreadType::Main) ||
|
|
|
|
(Svc::HighestThreadPriority <= prio && prio <= Svc::LowestThreadPriority));
|
|
|
|
ASSERT((owner != nullptr) || (type != ThreadType::User));
|
|
|
|
ASSERT(0 <= virt_core && virt_core < static_cast<s32>(Common::BitSize<u64>()));
|
|
|
|
|
|
|
|
// Convert the virtual core to a physical core.
|
|
|
|
const s32 phys_core = Core::Hardware::VirtualToPhysicalCoreMap[virt_core];
|
|
|
|
ASSERT(0 <= phys_core && phys_core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
|
|
|
|
|
|
|
|
// First, clear the TLS address.
|
|
|
|
tls_address = {};
|
|
|
|
|
|
|
|
// Next, assert things based on the type.
|
|
|
|
switch (type) {
|
|
|
|
case ThreadType::Main:
|
|
|
|
ASSERT(arg == 0);
|
|
|
|
[[fallthrough]];
|
|
|
|
case ThreadType::HighPriority:
|
|
|
|
[[fallthrough]];
|
2021-11-26 23:10:21 +00:00
|
|
|
case ThreadType::Dummy:
|
|
|
|
[[fallthrough]];
|
2021-01-20 21:42:27 +00:00
|
|
|
case ThreadType::User:
|
|
|
|
ASSERT(((owner == nullptr) ||
|
|
|
|
(owner->GetCoreMask() | (1ULL << virt_core)) == owner->GetCoreMask()));
|
|
|
|
ASSERT(((owner == nullptr) ||
|
|
|
|
(owner->GetPriorityMask() | (1ULL << prio)) == owner->GetPriorityMask()));
|
|
|
|
break;
|
|
|
|
case ThreadType::Kernel:
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE_MSG("KThread::Initialize: Unknown ThreadType {}", static_cast<u32>(type));
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 06:55:08 +00:00
|
|
|
thread_type_for_debugging = type;
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Set the ideal core ID and affinity mask.
|
|
|
|
virtual_ideal_core_id = virt_core;
|
|
|
|
physical_ideal_core_id = phys_core;
|
2021-01-25 06:55:08 +00:00
|
|
|
virtual_affinity_mask = 1ULL << virt_core;
|
2021-01-20 21:42:27 +00:00
|
|
|
physical_affinity_mask.SetAffinity(phys_core, true);
|
|
|
|
|
|
|
|
// Set the thread state.
|
|
|
|
thread_state = (type == ThreadType::Main) ? ThreadState::Runnable : ThreadState::Initialized;
|
|
|
|
|
|
|
|
// Set TLS address.
|
|
|
|
tls_address = 0;
|
|
|
|
|
|
|
|
// Set parent and condvar tree.
|
|
|
|
parent = nullptr;
|
|
|
|
condvar_tree = nullptr;
|
|
|
|
|
|
|
|
// Set sync booleans.
|
|
|
|
signaled = false;
|
|
|
|
termination_requested = false;
|
|
|
|
wait_cancelled = false;
|
|
|
|
cancellable = false;
|
|
|
|
|
|
|
|
// Set core ID and wait result.
|
|
|
|
core_id = phys_core;
|
2021-02-12 23:43:01 +00:00
|
|
|
wait_result = ResultNoSynchronizationObject;
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Set priorities.
|
|
|
|
priority = prio;
|
|
|
|
base_priority = prio;
|
|
|
|
|
|
|
|
// Initialize sleeping queue.
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue = nullptr;
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Set suspend flags.
|
|
|
|
suspend_request_flags = 0;
|
|
|
|
suspend_allowed_flags = static_cast<u32>(ThreadState::SuspendFlagMask);
|
|
|
|
|
|
|
|
// We're neither debug attached, nor are we nesting our priority inheritance.
|
|
|
|
debug_attached = false;
|
|
|
|
priority_inheritance_count = 0;
|
|
|
|
|
|
|
|
// We haven't been scheduled, and we have done no light IPC.
|
|
|
|
schedule_count = -1;
|
|
|
|
last_scheduled_tick = 0;
|
|
|
|
light_ipc_data = nullptr;
|
|
|
|
|
|
|
|
// We're not waiting for a lock, and we haven't disabled migration.
|
|
|
|
lock_owner = nullptr;
|
|
|
|
num_core_migration_disables = 0;
|
|
|
|
|
|
|
|
// We have no waiters, but we do have an entrypoint.
|
|
|
|
num_kernel_waiters = 0;
|
|
|
|
|
|
|
|
// Set our current core id.
|
|
|
|
current_core_id = phys_core;
|
|
|
|
|
|
|
|
// We haven't released our resource limit hint, and we've spent no time on the cpu.
|
|
|
|
resource_limit_release_hint = false;
|
|
|
|
cpu_time = 0;
|
|
|
|
|
|
|
|
// Clear our stack parameters.
|
|
|
|
std::memset(static_cast<void*>(std::addressof(GetStackParameters())), 0,
|
|
|
|
sizeof(StackParameters));
|
|
|
|
|
|
|
|
// Set parent, if relevant.
|
|
|
|
if (owner != nullptr) {
|
2021-05-29 04:31:04 +00:00
|
|
|
// Setup the TLS, if needed.
|
|
|
|
if (type == ThreadType::User) {
|
|
|
|
tls_address = owner->CreateTLSRegion();
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
parent = owner;
|
2021-04-03 06:24:20 +00:00
|
|
|
parent->Open();
|
2021-01-20 21:42:27 +00:00
|
|
|
parent->IncrementThreadCount();
|
|
|
|
}
|
2020-03-03 17:02:50 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Initialize thread context.
|
|
|
|
ResetThreadContext64(thread_context_64, user_stack_top, func, arg);
|
|
|
|
ResetThreadContext32(thread_context_32, static_cast<u32>(user_stack_top),
|
|
|
|
static_cast<u32>(func), static_cast<u32>(arg));
|
2020-03-03 17:02:50 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Setup the stack parameters.
|
|
|
|
StackParameters& sp = GetStackParameters();
|
|
|
|
sp.cur_thread = this;
|
2021-08-07 06:04:32 +00:00
|
|
|
sp.disable_count = 0;
|
2021-01-20 21:42:27 +00:00
|
|
|
SetInExceptionHandler();
|
|
|
|
|
|
|
|
// Set thread ID.
|
|
|
|
thread_id = kernel.CreateNewThreadID();
|
|
|
|
|
|
|
|
// We initialized!
|
|
|
|
initialized = true;
|
|
|
|
|
|
|
|
// Register ourselves with our parent process.
|
|
|
|
if (parent != nullptr) {
|
|
|
|
parent->RegisterThread(this);
|
|
|
|
if (parent->IsSuspended()) {
|
|
|
|
RequestSuspend(SuspendType::Process);
|
2020-03-12 00:44:53 +00:00
|
|
|
}
|
2020-03-03 17:02:50 +00:00
|
|
|
}
|
2014-05-14 02:00:11 +00:00
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
ResultCode KThread::InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg,
|
2021-04-24 05:04:28 +00:00
|
|
|
VAddr user_stack_top, s32 prio, s32 core, KProcess* owner,
|
2021-04-03 06:24:20 +00:00
|
|
|
ThreadType type, std::function<void(void*)>&& init_func,
|
|
|
|
void* init_func_parameter) {
|
2021-01-20 21:42:27 +00:00
|
|
|
// Initialize the thread.
|
|
|
|
R_TRY(thread->Initialize(func, arg, user_stack_top, prio, core, owner, type));
|
|
|
|
|
2021-08-14 09:14:19 +00:00
|
|
|
// Initialize emulation parameters.
|
2021-04-03 06:24:20 +00:00
|
|
|
thread->host_context =
|
|
|
|
std::make_shared<Common::Fiber>(std::move(init_func), init_func_parameter);
|
2021-08-14 09:14:19 +00:00
|
|
|
thread->is_single_core = !Settings::values.use_multi_core.GetValue();
|
2021-04-03 06:24:20 +00:00
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2020-02-25 16:40:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 06:24:20 +00:00
|
|
|
ResultCode KThread::InitializeDummyThread(KThread* thread) {
|
2021-11-26 23:10:21 +00:00
|
|
|
return thread->Initialize({}, {}, {}, DefaultThreadPriority, 3, {}, ThreadType::Dummy);
|
2021-04-03 06:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::InitializeIdleThread(Core::System& system, KThread* thread, s32 virt_core) {
|
|
|
|
return InitializeThread(thread, {}, {}, {}, IdleThreadPriority, virt_core, {}, ThreadType::Main,
|
|
|
|
Core::CpuManager::GetIdleThreadStartFunc(),
|
|
|
|
system.GetCpuManager().GetStartFuncParamater());
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::InitializeHighPriorityThread(Core::System& system, KThread* thread,
|
|
|
|
KThreadFunction func, uintptr_t arg,
|
|
|
|
s32 virt_core) {
|
|
|
|
return InitializeThread(thread, func, arg, {}, {}, virt_core, nullptr, ThreadType::HighPriority,
|
|
|
|
Core::CpuManager::GetSuspendThreadStartFunc(),
|
|
|
|
system.GetCpuManager().GetStartFuncParamater());
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread,
|
|
|
|
KThreadFunction func, uintptr_t arg, VAddr user_stack_top,
|
2021-04-24 05:04:28 +00:00
|
|
|
s32 prio, s32 virt_core, KProcess* owner) {
|
2021-04-03 06:24:20 +00:00
|
|
|
system.Kernel().GlobalSchedulerContext().AddThread(thread);
|
|
|
|
return InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner,
|
|
|
|
ThreadType::User, Core::CpuManager::GetGuestThreadStartFunc(),
|
|
|
|
system.GetCpuManager().GetStartFuncParamater());
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::PostDestroy(uintptr_t arg) {
|
2021-04-24 05:04:28 +00:00
|
|
|
KProcess* owner = reinterpret_cast<KProcess*>(arg & ~1ULL);
|
2021-04-03 06:24:20 +00:00
|
|
|
const bool resource_limit_release_hint = (arg & 1);
|
|
|
|
const s64 hint_value = (resource_limit_release_hint ? 0 : 1);
|
|
|
|
if (owner != nullptr) {
|
2021-04-11 18:42:22 +00:00
|
|
|
owner->GetResourceLimit()->Release(LimitableResource::Threads, 1, hint_value);
|
2021-04-03 06:24:20 +00:00
|
|
|
owner->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::Finalize() {
|
|
|
|
// If the thread has an owner process, unregister it.
|
|
|
|
if (parent != nullptr) {
|
|
|
|
parent->UnregisterThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the thread has a local region, delete it.
|
|
|
|
if (tls_address != 0) {
|
|
|
|
parent->FreeTLSRegion(tls_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release any waiters.
|
|
|
|
{
|
|
|
|
ASSERT(lock_owner == nullptr);
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
auto it = waiter_list.begin();
|
|
|
|
while (it != waiter_list.end()) {
|
2021-11-11 06:28:30 +00:00
|
|
|
// Clear the lock owner
|
2021-01-20 21:42:27 +00:00
|
|
|
it->SetLockOwner(nullptr);
|
2021-11-11 06:28:30 +00:00
|
|
|
|
|
|
|
// Erase the waiter from our list.
|
2021-01-20 21:42:27 +00:00
|
|
|
it = waiter_list.erase(it);
|
2021-11-11 06:28:30 +00:00
|
|
|
|
|
|
|
// Cancel the thread's wait.
|
|
|
|
it->CancelWait(ResultInvalidState, true);
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement the parent process's thread count.
|
|
|
|
if (parent != nullptr) {
|
|
|
|
parent->DecrementThreadCount();
|
2019-11-16 15:05:39 +00:00
|
|
|
}
|
2021-04-04 06:22:07 +00:00
|
|
|
|
|
|
|
// Perform inherited finalization.
|
|
|
|
KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>::Finalize();
|
2019-04-17 11:08:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
bool KThread::IsSignaled() const {
|
|
|
|
return signaled;
|
2020-03-02 04:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 06:28:30 +00:00
|
|
|
void KThread::OnTimer() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
2021-01-20 21:42:27 +00:00
|
|
|
|
2021-11-11 06:28:30 +00:00
|
|
|
// If we're waiting, cancel the wait.
|
2021-01-20 21:42:27 +00:00
|
|
|
if (GetState() == ThreadState::Waiting) {
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue->CancelWait(this, ResultTimedOut, false);
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
2016-09-02 12:53:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::StartTermination() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
|
|
|
|
// Release user exception and unpin, if relevant.
|
|
|
|
if (parent != nullptr) {
|
|
|
|
parent->ReleaseUserException(this);
|
|
|
|
if (parent->GetPinnedThread(GetCurrentCoreId(kernel)) == this) {
|
|
|
|
parent->UnpinCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set state to terminated.
|
|
|
|
SetState(ThreadState::Terminated);
|
|
|
|
|
|
|
|
// Clear the thread's status as running in parent.
|
|
|
|
if (parent != nullptr) {
|
|
|
|
parent->ClearRunningThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal.
|
|
|
|
signaled = true;
|
2021-11-10 03:02:11 +00:00
|
|
|
KSynchronizationObject::NotifyAvailable();
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Clear previous thread in KScheduler.
|
|
|
|
KScheduler::ClearPreviousThread(kernel, this);
|
|
|
|
|
|
|
|
// Register terminated dpc flag.
|
|
|
|
RegisterDpc(DpcFlag::Terminated);
|
2021-04-03 06:24:20 +00:00
|
|
|
|
|
|
|
// Close the thread.
|
|
|
|
this->Close();
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::Pin() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
|
|
|
|
// Set ourselves as pinned.
|
|
|
|
GetStackParameters().is_pinned = true;
|
|
|
|
|
|
|
|
// Disable core migration.
|
|
|
|
ASSERT(num_core_migration_disables == 0);
|
|
|
|
{
|
|
|
|
++num_core_migration_disables;
|
|
|
|
|
|
|
|
// Save our ideal state to restore when we're unpinned.
|
|
|
|
original_physical_ideal_core_id = physical_ideal_core_id;
|
|
|
|
original_physical_affinity_mask = physical_affinity_mask;
|
|
|
|
|
|
|
|
// Bind ourselves to this core.
|
|
|
|
const s32 active_core = GetActiveCore();
|
|
|
|
const s32 current_core = GetCurrentCoreId(kernel);
|
|
|
|
|
|
|
|
SetActiveCore(current_core);
|
|
|
|
physical_ideal_core_id = current_core;
|
|
|
|
physical_affinity_mask.SetAffinityMask(1ULL << current_core);
|
|
|
|
|
|
|
|
if (active_core != current_core || physical_affinity_mask.GetAffinityMask() !=
|
|
|
|
original_physical_affinity_mask.GetAffinityMask()) {
|
|
|
|
KScheduler::OnThreadAffinityMaskChanged(kernel, this, original_physical_affinity_mask,
|
|
|
|
active_core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow performing thread suspension.
|
|
|
|
{
|
|
|
|
// Update our allow flags.
|
|
|
|
suspend_allowed_flags &= ~(1 << (static_cast<u32>(SuspendType::Thread) +
|
|
|
|
static_cast<u32>(ThreadState::SuspendShift)));
|
|
|
|
|
|
|
|
// Update our state.
|
|
|
|
const ThreadState old_state = thread_state;
|
|
|
|
thread_state = static_cast<ThreadState>(GetSuspendFlags() |
|
|
|
|
static_cast<u32>(old_state & ThreadState::Mask));
|
|
|
|
if (thread_state != old_state) {
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bunnei): Update our SVC access permissions.
|
|
|
|
ASSERT(parent != nullptr);
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::Unpin() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
|
|
|
|
// Set ourselves as unpinned.
|
|
|
|
GetStackParameters().is_pinned = false;
|
|
|
|
|
|
|
|
// Enable core migration.
|
|
|
|
ASSERT(num_core_migration_disables == 1);
|
|
|
|
{
|
2021-01-25 06:55:08 +00:00
|
|
|
num_core_migration_disables--;
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Restore our original state.
|
|
|
|
const KAffinityMask old_mask = physical_affinity_mask;
|
2021-01-01 10:06:06 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
physical_ideal_core_id = original_physical_ideal_core_id;
|
|
|
|
physical_affinity_mask = original_physical_affinity_mask;
|
|
|
|
|
|
|
|
if (physical_affinity_mask.GetAffinityMask() != old_mask.GetAffinityMask()) {
|
|
|
|
const s32 active_core = GetActiveCore();
|
|
|
|
|
|
|
|
if (!physical_affinity_mask.GetAffinity(active_core)) {
|
|
|
|
if (physical_ideal_core_id >= 0) {
|
|
|
|
SetActiveCore(physical_ideal_core_id);
|
|
|
|
} else {
|
|
|
|
SetActiveCore(static_cast<s32>(
|
|
|
|
Common::BitSize<u64>() - 1 -
|
|
|
|
std::countl_zero(physical_affinity_mask.GetAffinityMask())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KScheduler::OnThreadAffinityMaskChanged(kernel, this, old_mask, active_core);
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
2014-12-22 13:07:22 +00:00
|
|
|
}
|
2014-05-21 01:02:35 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Allow performing thread suspension (if termination hasn't been requested).
|
|
|
|
{
|
|
|
|
// Update our allow flags.
|
|
|
|
if (!IsTerminationRequested()) {
|
|
|
|
suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) +
|
|
|
|
static_cast<u32>(ThreadState::SuspendShift)));
|
|
|
|
}
|
2014-06-06 02:35:36 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Update our state.
|
|
|
|
const ThreadState old_state = thread_state;
|
|
|
|
thread_state = static_cast<ThreadState>(GetSuspendFlags() |
|
|
|
|
static_cast<u32>(old_state & ThreadState::Mask));
|
|
|
|
if (thread_state != old_state) {
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 08:46:09 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// TODO(bunnei): Update our SVC access permissions.
|
|
|
|
ASSERT(parent != nullptr);
|
|
|
|
|
|
|
|
// Resume any threads that began waiting on us while we were pinned.
|
|
|
|
for (auto it = pinned_waiter_list.begin(); it != pinned_waiter_list.end(); ++it) {
|
|
|
|
if (it->GetState() == ThreadState::Waiting) {
|
|
|
|
it->SetState(ThreadState::Runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask) {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Get the virtual mask.
|
|
|
|
*out_ideal_core = virtual_ideal_core_id;
|
|
|
|
*out_affinity_mask = virtual_affinity_mask;
|
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
2020-12-31 08:46:09 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
ResultCode KThread::GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask) {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
ASSERT(num_core_migration_disables >= 0);
|
|
|
|
|
|
|
|
// Select between core mask and original core mask.
|
|
|
|
if (num_core_migration_disables == 0) {
|
|
|
|
*out_ideal_core = physical_ideal_core_id;
|
|
|
|
*out_affinity_mask = physical_affinity_mask.GetAffinityMask();
|
2020-02-25 02:04:12 +00:00
|
|
|
} else {
|
2021-01-20 21:42:27 +00:00
|
|
|
*out_ideal_core = original_physical_ideal_core_id;
|
|
|
|
*out_affinity_mask = original_physical_affinity_mask.GetAffinityMask();
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
2020-11-04 00:54:53 +00:00
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
2021-11-11 06:46:07 +00:00
|
|
|
|
|
|
|
ResultCode KThread::SetCoreMask(s32 core_id_, u64 v_affinity_mask) {
|
2021-01-20 21:42:27 +00:00
|
|
|
ASSERT(parent != nullptr);
|
|
|
|
ASSERT(v_affinity_mask != 0);
|
2021-11-11 06:46:07 +00:00
|
|
|
KScopedLightLock lk(activity_pause_lock);
|
2015-01-26 06:56:17 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Set the core mask.
|
|
|
|
u64 p_affinity_mask = 0;
|
|
|
|
{
|
2021-11-11 06:46:07 +00:00
|
|
|
KScopedSchedulerLock sl(kernel);
|
2021-01-20 21:42:27 +00:00
|
|
|
ASSERT(num_core_migration_disables >= 0);
|
|
|
|
|
2021-11-11 06:46:07 +00:00
|
|
|
// If we're updating, set our ideal virtual core.
|
|
|
|
if (core_id_ != Svc::IdealCoreNoUpdate) {
|
|
|
|
virtual_ideal_core_id = core_id_;
|
|
|
|
} else {
|
|
|
|
// Preserve our ideal core id.
|
|
|
|
core_id_ = virtual_ideal_core_id;
|
|
|
|
R_UNLESS(((1ULL << core_id_) & v_affinity_mask) != 0, ResultInvalidCombination);
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 06:46:07 +00:00
|
|
|
// Set our affinity mask.
|
2021-01-20 21:42:27 +00:00
|
|
|
virtual_affinity_mask = v_affinity_mask;
|
|
|
|
|
|
|
|
// Translate the virtual core to a physical core.
|
2021-11-11 06:46:07 +00:00
|
|
|
if (core_id_ >= 0) {
|
|
|
|
core_id_ = Core::Hardware::VirtualToPhysicalCoreMap[core_id_];
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate the virtual affinity mask to a physical one.
|
|
|
|
while (v_affinity_mask != 0) {
|
|
|
|
const u64 next = std::countr_zero(v_affinity_mask);
|
|
|
|
v_affinity_mask &= ~(1ULL << next);
|
|
|
|
p_affinity_mask |= (1ULL << Core::Hardware::VirtualToPhysicalCoreMap[next]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't disabled migration, perform an affinity change.
|
|
|
|
if (num_core_migration_disables == 0) {
|
|
|
|
const KAffinityMask old_mask = physical_affinity_mask;
|
|
|
|
|
|
|
|
// Set our new ideals.
|
2021-11-11 06:46:07 +00:00
|
|
|
physical_ideal_core_id = core_id_;
|
2021-01-20 21:42:27 +00:00
|
|
|
physical_affinity_mask.SetAffinityMask(p_affinity_mask);
|
|
|
|
|
|
|
|
if (physical_affinity_mask.GetAffinityMask() != old_mask.GetAffinityMask()) {
|
|
|
|
const s32 active_core = GetActiveCore();
|
|
|
|
|
|
|
|
if (active_core >= 0 && !physical_affinity_mask.GetAffinity(active_core)) {
|
|
|
|
const s32 new_core = static_cast<s32>(
|
|
|
|
physical_ideal_core_id >= 0
|
|
|
|
? physical_ideal_core_id
|
|
|
|
: Common::BitSize<u64>() - 1 -
|
|
|
|
std::countl_zero(physical_affinity_mask.GetAffinityMask()));
|
|
|
|
SetActiveCore(new_core);
|
|
|
|
}
|
|
|
|
KScheduler::OnThreadAffinityMaskChanged(kernel, this, old_mask, active_core);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, we edit the original affinity for restoration later.
|
2021-11-11 06:46:07 +00:00
|
|
|
original_physical_ideal_core_id = core_id_;
|
2021-01-20 21:42:27 +00:00
|
|
|
original_physical_affinity_mask.SetAffinityMask(p_affinity_mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the pinned waiter list.
|
2021-11-28 08:40:25 +00:00
|
|
|
ThreadQueueImplForKThreadSetProperty wait_queue_(kernel, std::addressof(pinned_waiter_list));
|
2021-01-20 21:42:27 +00:00
|
|
|
{
|
2021-01-25 06:55:08 +00:00
|
|
|
bool retry_update{};
|
2021-01-20 21:42:27 +00:00
|
|
|
do {
|
|
|
|
// Lock the scheduler.
|
2021-11-11 06:46:07 +00:00
|
|
|
KScopedSchedulerLock sl(kernel);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Don't do any further management if our termination has been requested.
|
|
|
|
R_SUCCEED_IF(IsTerminationRequested());
|
|
|
|
|
|
|
|
// By default, we won't need to retry.
|
|
|
|
retry_update = false;
|
|
|
|
|
|
|
|
// Check if the thread is currently running.
|
2021-01-25 06:55:08 +00:00
|
|
|
bool thread_is_current{};
|
2021-01-20 21:42:27 +00:00
|
|
|
s32 thread_core;
|
|
|
|
for (thread_core = 0; thread_core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES);
|
|
|
|
++thread_core) {
|
|
|
|
if (kernel.Scheduler(thread_core).GetCurrentThread() == this) {
|
|
|
|
thread_is_current = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the thread is currently running, check whether it's no longer allowed under the
|
|
|
|
// new mask.
|
|
|
|
if (thread_is_current && ((1ULL << thread_core) & p_affinity_mask) == 0) {
|
|
|
|
// If the thread is pinned, we want to wait until it's not pinned.
|
|
|
|
if (GetStackParameters().is_pinned) {
|
|
|
|
// Verify that the current thread isn't terminating.
|
|
|
|
R_UNLESS(!GetCurrentThread(kernel).IsTerminationRequested(),
|
2021-02-12 23:43:01 +00:00
|
|
|
ResultTerminationRequested);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Wait until the thread isn't pinned any more.
|
|
|
|
pinned_waiter_list.push_back(GetCurrentThread(kernel));
|
2021-11-28 08:40:25 +00:00
|
|
|
GetCurrentThread(kernel).BeginWait(std::addressof(wait_queue_));
|
2021-01-20 21:42:27 +00:00
|
|
|
} else {
|
|
|
|
// If the thread isn't pinned, release the scheduler lock and retry until it's
|
|
|
|
// not current.
|
|
|
|
retry_update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (retry_update);
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2014-06-02 02:12:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::SetBasePriority(s32 value) {
|
|
|
|
ASSERT(Svc::HighestThreadPriority <= value && value <= Svc::LowestThreadPriority);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
KScopedSchedulerLock sl{kernel};
|
2020-12-30 09:14:02 +00:00
|
|
|
|
|
|
|
// Change our base priority.
|
2021-01-20 21:42:27 +00:00
|
|
|
base_priority = value;
|
2020-12-30 09:14:02 +00:00
|
|
|
|
|
|
|
// Perform a priority restoration.
|
|
|
|
RestorePriority(kernel, this);
|
2014-06-02 02:12:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::RequestSuspend(SuspendType type) {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
2015-01-17 07:03:44 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Note the request in our flags.
|
|
|
|
suspend_request_flags |=
|
|
|
|
(1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)));
|
|
|
|
|
|
|
|
// Try to perform the suspend.
|
|
|
|
TrySuspend();
|
2017-01-04 15:53:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::Resume(SuspendType type) {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
2020-12-30 09:14:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Clear the request in our flags.
|
|
|
|
suspend_request_flags &=
|
|
|
|
~(1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)));
|
2021-01-10 22:29:02 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Update our state.
|
2020-12-30 09:14:02 +00:00
|
|
|
const ThreadState old_state = thread_state;
|
2021-01-20 21:42:27 +00:00
|
|
|
thread_state = static_cast<ThreadState>(GetSuspendFlags() |
|
|
|
|
static_cast<u32>(old_state & ThreadState::Mask));
|
2020-12-30 09:14:02 +00:00
|
|
|
if (thread_state != old_state) {
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
2020-12-30 09:14:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::WaitCancel() {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Check if we're waiting and cancellable.
|
2021-11-11 06:28:30 +00:00
|
|
|
if (this->GetState() == ThreadState::Waiting && cancellable) {
|
|
|
|
wait_cancelled = false;
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue->CancelWait(this, ResultCancelled, true);
|
2021-01-20 21:42:27 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, note that we cancelled a wait.
|
|
|
|
wait_cancelled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::TrySuspend() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
ASSERT(IsSuspendRequested());
|
|
|
|
|
|
|
|
// Ensure that we have no waiters.
|
|
|
|
if (GetNumKernelWaiters() > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT(GetNumKernelWaiters() == 0);
|
|
|
|
|
|
|
|
// Perform the suspend.
|
|
|
|
Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::Suspend() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
ASSERT(IsSuspendRequested());
|
|
|
|
|
|
|
|
// Set our suspend flags in state.
|
|
|
|
const auto old_state = thread_state;
|
|
|
|
thread_state = static_cast<ThreadState>(GetSuspendFlags()) | (old_state & ThreadState::Mask);
|
|
|
|
|
|
|
|
// Note the state change in scheduler.
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::Continue() {
|
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
|
|
|
|
// Clear our suspend flags in state.
|
|
|
|
const auto old_state = thread_state;
|
|
|
|
thread_state = old_state & ThreadState::Mask;
|
|
|
|
|
|
|
|
// Note the state change in scheduler.
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::SetActivity(Svc::ThreadActivity activity) {
|
|
|
|
// Lock ourselves.
|
|
|
|
KScopedLightLock lk(activity_pause_lock);
|
|
|
|
|
|
|
|
// Set the activity.
|
|
|
|
{
|
|
|
|
// Lock the scheduler.
|
2021-11-11 06:28:30 +00:00
|
|
|
KScopedSchedulerLock sl(kernel);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Verify our state.
|
2021-11-11 06:28:30 +00:00
|
|
|
const auto cur_state = this->GetState();
|
2021-01-20 21:42:27 +00:00
|
|
|
R_UNLESS((cur_state == ThreadState::Waiting || cur_state == ThreadState::Runnable),
|
2021-02-12 23:43:01 +00:00
|
|
|
ResultInvalidState);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Either pause or resume.
|
|
|
|
if (activity == Svc::ThreadActivity::Paused) {
|
|
|
|
// Verify that we're not suspended.
|
2021-11-11 06:28:30 +00:00
|
|
|
R_UNLESS(!this->IsSuspendRequested(SuspendType::Thread), ResultInvalidState);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Suspend.
|
2021-11-11 06:28:30 +00:00
|
|
|
this->RequestSuspend(SuspendType::Thread);
|
2021-01-20 21:42:27 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(activity == Svc::ThreadActivity::Runnable);
|
|
|
|
|
|
|
|
// Verify that we're suspended.
|
2021-11-11 06:28:30 +00:00
|
|
|
R_UNLESS(this->IsSuspendRequested(SuspendType::Thread), ResultInvalidState);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Resume.
|
2021-11-11 06:28:30 +00:00
|
|
|
this->Resume(SuspendType::Thread);
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the thread is now paused, update the pinned waiter list.
|
|
|
|
if (activity == Svc::ThreadActivity::Paused) {
|
2021-11-11 06:28:30 +00:00
|
|
|
ThreadQueueImplForKThreadSetProperty wait_queue_(kernel,
|
|
|
|
std::addressof(pinned_waiter_list));
|
|
|
|
|
|
|
|
bool thread_is_current;
|
2021-01-20 21:42:27 +00:00
|
|
|
do {
|
|
|
|
// Lock the scheduler.
|
2021-11-11 06:28:30 +00:00
|
|
|
KScopedSchedulerLock sl(kernel);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Don't do any further management if our termination has been requested.
|
2021-11-11 06:28:30 +00:00
|
|
|
R_SUCCEED_IF(this->IsTerminationRequested());
|
|
|
|
|
|
|
|
// By default, treat the thread as not current.
|
|
|
|
thread_is_current = false;
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Check whether the thread is pinned.
|
2021-11-11 06:28:30 +00:00
|
|
|
if (this->GetStackParameters().is_pinned) {
|
2021-01-20 21:42:27 +00:00
|
|
|
// Verify that the current thread isn't terminating.
|
|
|
|
R_UNLESS(!GetCurrentThread(kernel).IsTerminationRequested(),
|
2021-02-12 23:43:01 +00:00
|
|
|
ResultTerminationRequested);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Wait until the thread isn't pinned any more.
|
|
|
|
pinned_waiter_list.push_back(GetCurrentThread(kernel));
|
2021-11-11 06:28:30 +00:00
|
|
|
GetCurrentThread(kernel).BeginWait(std::addressof(wait_queue_));
|
2021-01-20 21:42:27 +00:00
|
|
|
} else {
|
|
|
|
// Check if the thread is currently running.
|
|
|
|
// If it is, we'll need to retry.
|
|
|
|
for (auto i = 0; i < static_cast<s32>(Core::Hardware::NUM_CPU_CORES); ++i) {
|
|
|
|
if (kernel.Scheduler(i).GetCurrentThread() == this) {
|
|
|
|
thread_is_current = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (thread_is_current);
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode KThread::GetThreadContext3(std::vector<u8>& out) {
|
|
|
|
// Lock ourselves.
|
|
|
|
KScopedLightLock lk{activity_pause_lock};
|
|
|
|
|
|
|
|
// Get the context.
|
|
|
|
{
|
|
|
|
// Lock the scheduler.
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Verify that we're suspended.
|
2021-02-12 23:43:01 +00:00
|
|
|
R_UNLESS(IsSuspendRequested(SuspendType::Thread), ResultInvalidState);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// If we're not terminating, get the thread's user context.
|
|
|
|
if (!IsTerminationRequested()) {
|
|
|
|
if (parent->Is64BitProcess()) {
|
|
|
|
// Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
|
|
|
|
auto context = GetContext64();
|
|
|
|
context.pstate &= 0xFF0FFE20;
|
|
|
|
|
|
|
|
out.resize(sizeof(context));
|
|
|
|
std::memcpy(out.data(), &context, sizeof(context));
|
|
|
|
} else {
|
|
|
|
// Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
|
|
|
|
auto context = GetContext32();
|
|
|
|
context.cpsr &= 0xFF0FFE20;
|
|
|
|
|
|
|
|
out.resize(sizeof(context));
|
|
|
|
std::memcpy(out.data(), &context, sizeof(context));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void KThread::AddWaiterImpl(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
2018-10-03 22:47:57 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Find the right spot to insert the waiter.
|
|
|
|
auto it = waiter_list.begin();
|
|
|
|
while (it != waiter_list.end()) {
|
|
|
|
if (it->GetPriority() > thread->GetPriority()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it++;
|
2019-03-29 21:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Keep track of how many kernel waiters we have.
|
2021-02-13 00:02:35 +00:00
|
|
|
if (IsKernelAddressKey(thread->GetAddressKey())) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT((num_kernel_waiters++) >= 0);
|
|
|
|
}
|
2020-12-28 21:16:43 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Insert the waiter.
|
|
|
|
waiter_list.insert(it, *thread);
|
|
|
|
thread->SetLockOwner(this);
|
2018-10-03 22:47:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void KThread::RemoveWaiterImpl(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
|
|
|
|
// Keep track of how many kernel waiters we have.
|
2021-02-13 00:02:35 +00:00
|
|
|
if (IsKernelAddressKey(thread->GetAddressKey())) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT((num_kernel_waiters--) > 0);
|
2018-08-12 21:35:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Remove the waiter.
|
|
|
|
waiter_list.erase(waiter_list.iterator_to(*thread));
|
|
|
|
thread->SetLockOwner(nullptr);
|
|
|
|
}
|
2018-08-12 21:35:27 +00:00
|
|
|
|
2021-05-08 16:11:36 +00:00
|
|
|
void KThread::RestorePriority(KernelCore& kernel_ctx, KThread* thread) {
|
|
|
|
ASSERT(kernel_ctx.GlobalSchedulerContext().IsLocked());
|
2019-03-15 05:02:13 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
while (true) {
|
|
|
|
// We want to inherit priority where possible.
|
|
|
|
s32 new_priority = thread->GetBasePriority();
|
|
|
|
if (thread->HasWaiters()) {
|
|
|
|
new_priority = std::min(new_priority, thread->waiter_list.front().GetPriority());
|
|
|
|
}
|
2019-03-15 05:02:13 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// If the priority we would inherit is not different from ours, don't do anything.
|
|
|
|
if (new_priority == thread->GetPriority()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-21 01:15:16 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Ensure we don't violate condition variable red black tree invariants.
|
|
|
|
if (auto* cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
|
2021-05-08 16:11:36 +00:00
|
|
|
BeforeUpdatePriority(kernel_ctx, cv_tree, thread);
|
2020-12-30 09:14:02 +00:00
|
|
|
}
|
2018-08-12 21:35:27 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Change the priority.
|
|
|
|
const s32 old_priority = thread->GetPriority();
|
|
|
|
thread->SetPriority(new_priority);
|
2019-03-15 05:02:13 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Restore the condition variable, if relevant.
|
|
|
|
if (auto* cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
|
2021-05-08 16:11:36 +00:00
|
|
|
AfterUpdatePriority(kernel_ctx, cv_tree, thread);
|
2020-12-30 09:14:02 +00:00
|
|
|
}
|
2018-08-12 21:35:27 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Update the scheduler.
|
2021-05-08 16:11:36 +00:00
|
|
|
KScheduler::OnThreadPriorityChanged(kernel_ctx, thread, old_priority);
|
2018-04-21 01:15:16 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Keep the lock owner up to date.
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread* lock_owner = thread->GetLockOwner();
|
2020-12-30 09:14:02 +00:00
|
|
|
if (lock_owner == nullptr) {
|
|
|
|
return;
|
2019-03-15 01:51:03 +00:00
|
|
|
}
|
2018-04-21 01:15:16 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Update the thread in the lock owner's sorted list, and continue inheriting.
|
|
|
|
lock_owner->RemoveWaiterImpl(thread);
|
|
|
|
lock_owner->AddWaiterImpl(thread);
|
|
|
|
thread = lock_owner;
|
2019-03-15 01:51:03 +00:00
|
|
|
}
|
2020-12-30 09:14:02 +00:00
|
|
|
}
|
2018-04-21 01:15:16 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void KThread::AddWaiter(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
AddWaiterImpl(thread);
|
|
|
|
RestorePriority(kernel, this);
|
|
|
|
}
|
2019-11-15 00:13:18 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
void KThread::RemoveWaiter(KThread* thread) {
|
2020-12-30 09:14:02 +00:00
|
|
|
RemoveWaiterImpl(thread);
|
|
|
|
RestorePriority(kernel, this);
|
|
|
|
}
|
2018-04-21 01:15:16 +00:00
|
|
|
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread* KThread::RemoveWaiterByKey(s32* out_num_waiters, VAddr key) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT(kernel.GlobalSchedulerContext().IsLocked());
|
2019-11-15 00:13:18 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
s32 num_waiters{};
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread* next_lock_owner{};
|
2020-12-30 09:14:02 +00:00
|
|
|
auto it = waiter_list.begin();
|
|
|
|
while (it != waiter_list.end()) {
|
|
|
|
if (it->GetAddressKey() == key) {
|
2020-12-31 07:01:08 +00:00
|
|
|
KThread* thread = std::addressof(*it);
|
2020-12-30 09:14:02 +00:00
|
|
|
|
|
|
|
// Keep track of how many kernel waiters we have.
|
2021-02-13 00:02:35 +00:00
|
|
|
if (IsKernelAddressKey(thread->GetAddressKey())) {
|
2020-12-30 09:14:02 +00:00
|
|
|
ASSERT((num_kernel_waiters--) > 0);
|
|
|
|
}
|
|
|
|
it = waiter_list.erase(it);
|
|
|
|
|
|
|
|
// Update the next lock owner.
|
|
|
|
if (next_lock_owner == nullptr) {
|
|
|
|
next_lock_owner = thread;
|
|
|
|
next_lock_owner->SetLockOwner(nullptr);
|
|
|
|
} else {
|
|
|
|
next_lock_owner->AddWaiterImpl(thread);
|
|
|
|
}
|
|
|
|
num_waiters++;
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
2019-03-15 05:02:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Do priority updates, if we have a next owner.
|
|
|
|
if (next_lock_owner) {
|
|
|
|
RestorePriority(kernel, this);
|
|
|
|
RestorePriority(kernel, next_lock_owner);
|
|
|
|
}
|
2019-03-15 05:02:13 +00:00
|
|
|
|
2020-12-30 09:14:02 +00:00
|
|
|
// Return output.
|
|
|
|
*out_num_waiters = num_waiters;
|
|
|
|
return next_lock_owner;
|
2018-04-21 01:15:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
ResultCode KThread::Run() {
|
|
|
|
while (true) {
|
|
|
|
KScopedSchedulerLock lk{kernel};
|
2020-03-07 16:44:35 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// If either this thread or the current thread are requesting termination, note it.
|
2021-02-12 23:43:01 +00:00
|
|
|
R_UNLESS(!IsTerminationRequested(), ResultTerminationRequested);
|
|
|
|
R_UNLESS(!GetCurrentThread(kernel).IsTerminationRequested(), ResultTerminationRequested);
|
2020-03-07 16:44:35 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// Ensure our thread state is correct.
|
2021-02-12 23:43:01 +00:00
|
|
|
R_UNLESS(GetState() == ThreadState::Initialized, ResultInvalidState);
|
2020-03-07 16:44:35 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
// If the current thread has been asked to suspend, suspend it and retry.
|
|
|
|
if (GetCurrentThread(kernel).IsSuspended()) {
|
|
|
|
GetCurrentThread(kernel).Suspend();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not a kernel thread and we've been asked to suspend, suspend ourselves.
|
|
|
|
if (IsUserThread() && IsSuspended()) {
|
|
|
|
Suspend();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our state and finish.
|
|
|
|
SetState(ThreadState::Runnable);
|
2021-08-07 06:04:32 +00:00
|
|
|
|
|
|
|
DisableDispatch();
|
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2020-03-07 16:44:35 +00:00
|
|
|
}
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
2018-12-03 17:25:27 +00:00
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::Exit() {
|
|
|
|
ASSERT(this == GetCurrentThreadPointer(kernel));
|
|
|
|
|
|
|
|
// Release the thread resource hint from parent.
|
|
|
|
if (parent != nullptr) {
|
2021-04-11 17:51:09 +00:00
|
|
|
parent->GetResourceLimit()->Release(Kernel::LimitableResource::Threads, 0, 1);
|
2021-01-20 21:42:27 +00:00
|
|
|
resource_limit_release_hint = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform termination.
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Disallow all suspension.
|
|
|
|
suspend_allowed_flags = 0;
|
|
|
|
|
|
|
|
// Start termination.
|
|
|
|
StartTermination();
|
2018-12-03 17:25:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
ResultCode KThread::Sleep(s64 timeout) {
|
|
|
|
ASSERT(!kernel.GlobalSchedulerContext().IsLocked());
|
|
|
|
ASSERT(this == GetCurrentThreadPointer(kernel));
|
|
|
|
ASSERT(timeout > 0);
|
|
|
|
|
2021-11-28 08:40:25 +00:00
|
|
|
ThreadQueueImplForKThreadSleep wait_queue_(kernel);
|
2020-02-25 02:04:12 +00:00
|
|
|
{
|
2021-01-20 21:42:27 +00:00
|
|
|
// Setup the scheduling lock and sleep.
|
2021-11-11 06:28:30 +00:00
|
|
|
KScopedSchedulerLockAndSleep slp(kernel, this, timeout);
|
2021-01-20 21:42:27 +00:00
|
|
|
|
|
|
|
// Check if the thread should terminate.
|
2021-11-11 06:28:30 +00:00
|
|
|
if (this->IsTerminationRequested()) {
|
2021-01-20 21:42:27 +00:00
|
|
|
slp.CancelSleep();
|
2021-02-12 23:43:01 +00:00
|
|
|
return ResultTerminationRequested;
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 06:28:30 +00:00
|
|
|
// Wait for the sleep to end.
|
2021-11-28 08:40:25 +00:00
|
|
|
this->BeginWait(std::addressof(wait_queue_));
|
2021-01-10 22:29:02 +00:00
|
|
|
SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Sleep);
|
2020-02-25 02:04:12 +00:00
|
|
|
}
|
2019-03-16 03:28:29 +00:00
|
|
|
|
2021-05-21 05:05:04 +00:00
|
|
|
return ResultSuccess;
|
2019-03-16 03:28:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 03:02:11 +00:00
|
|
|
void KThread::BeginWait(KThreadQueue* queue) {
|
|
|
|
// Set our state as waiting.
|
|
|
|
SetState(ThreadState::Waiting);
|
|
|
|
|
|
|
|
// Set our wait queue.
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue = queue;
|
2021-11-10 03:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::NotifyAvailable(KSynchronizationObject* signaled_object, ResultCode wait_result_) {
|
|
|
|
// Lock the scheduler.
|
|
|
|
KScopedSchedulerLock sl(kernel);
|
|
|
|
|
|
|
|
// If we're waiting, notify our queue that we're available.
|
|
|
|
if (GetState() == ThreadState::Waiting) {
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue->NotifyAvailable(this, signaled_object, wait_result_);
|
2021-11-10 03:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::EndWait(ResultCode wait_result_) {
|
|
|
|
// Lock the scheduler.
|
|
|
|
KScopedSchedulerLock sl(kernel);
|
|
|
|
|
|
|
|
// If we're waiting, notify our queue that we're available.
|
|
|
|
if (GetState() == ThreadState::Waiting) {
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue->EndWait(this, wait_result_);
|
2021-11-10 03:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KThread::CancelWait(ResultCode wait_result_, bool cancel_timer_task) {
|
|
|
|
// Lock the scheduler.
|
|
|
|
KScopedSchedulerLock sl(kernel);
|
|
|
|
|
|
|
|
// If we're waiting, notify our queue that we're available.
|
|
|
|
if (GetState() == ThreadState::Waiting) {
|
2021-11-28 08:40:25 +00:00
|
|
|
wait_queue->CancelWait(this, wait_result_, cancel_timer_task);
|
2021-11-10 03:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
void KThread::SetState(ThreadState state) {
|
|
|
|
KScopedSchedulerLock sl{kernel};
|
|
|
|
|
|
|
|
// Clear debugging state
|
|
|
|
SetMutexWaitAddressForDebugging({});
|
|
|
|
SetWaitReasonForDebugging({});
|
|
|
|
|
|
|
|
const ThreadState old_state = thread_state;
|
|
|
|
thread_state =
|
|
|
|
static_cast<ThreadState>((old_state & ~ThreadState::Mask) | (state & ThreadState::Mask));
|
|
|
|
if (thread_state != old_state) {
|
|
|
|
KScheduler::OnThreadStateChanged(kernel, this, old_state);
|
|
|
|
}
|
2020-03-07 16:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 01:08:17 +00:00
|
|
|
std::shared_ptr<Common::Fiber>& KThread::GetHostContext() {
|
|
|
|
return host_context;
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:42:27 +00:00
|
|
|
KThread* GetCurrentThreadPointer(KernelCore& kernel) {
|
2021-01-21 21:00:16 +00:00
|
|
|
return kernel.GetCurrentEmuThread();
|
2021-01-20 21:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KThread& GetCurrentThread(KernelCore& kernel) {
|
|
|
|
return *GetCurrentThreadPointer(kernel);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 GetCurrentCoreId(KernelCore& kernel) {
|
|
|
|
return GetCurrentThread(kernel).GetCurrentCore();
|
2019-03-29 21:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 06:04:32 +00:00
|
|
|
KScopedDisableDispatch::~KScopedDisableDispatch() {
|
2021-11-26 04:46:17 +00:00
|
|
|
// If we are shutting down the kernel, none of this is relevant anymore.
|
|
|
|
if (kernel.IsShuttingDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-07 06:04:32 +00:00
|
|
|
if (GetCurrentThread(kernel).GetDisableDispatchCount() <= 1) {
|
|
|
|
auto scheduler = kernel.CurrentScheduler();
|
|
|
|
|
|
|
|
if (scheduler) {
|
|
|
|
scheduler->RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GetCurrentThread(kernel).EnableDispatch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 16:35:03 +00:00
|
|
|
} // namespace Kernel
|