code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/assert.h"
#include "common/common_types.h"
#include "common/scratch_buffer.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
#include "core/hle/kernel/k_synchronization_object.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_queue.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {

namespace {

class ThreadQueueImplForKSynchronizationObjectWait final : public KThreadQueueWithoutEndWait {
public:
    ThreadQueueImplForKSynchronizationObjectWait(KernelCore& kernel, KSynchronizationObject** o,
                                                 KSynchronizationObject::ThreadListNode* n, s32 c)
        : KThreadQueueWithoutEndWait(kernel), m_objects(o), m_nodes(n), m_count(c) {}

    void NotifyAvailable(KThread* waiting_thread, KSynchronizationObject* signaled_object,
                         Result wait_result) override {
        // Determine the sync index, and unlink all nodes.
        s32 sync_index = -1;
        for (auto i = 0; i < m_count; ++i) {
            // Check if this is the signaled object.
            if (m_objects[i] == signaled_object && sync_index == -1) {
                sync_index = i;
            }

            // Unlink the current node from the current object.
            m_objects[i]->UnlinkNode(std::addressof(m_nodes[i]));
        }

        // Set the waiting thread's sync index.
        waiting_thread->SetSyncedIndex(sync_index);

        // Set the waiting thread as not cancellable.
        waiting_thread->ClearCancellable();

        // Invoke the base end wait handler.
        KThreadQueue::EndWait(waiting_thread, wait_result);
    }

    void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task) override {
        // Remove all nodes from our list.
        for (auto i = 0; i < m_count; ++i) {
            m_objects[i]->UnlinkNode(std::addressof(m_nodes[i]));
        }

        // Set the waiting thread as not cancellable.
        waiting_thread->ClearCancellable();

        // Invoke the base cancel wait handler.
        KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
    }

private:
    KSynchronizationObject** m_objects;
    KSynchronizationObject::ThreadListNode* m_nodes;
    s32 m_count;
};

} // namespace

void KSynchronizationObject::Finalize() {
    this->OnFinalizeSynchronizationObject();
    KAutoObject::Finalize();
}

Result KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index,
                                    KSynchronizationObject** objects, const s32 num_objects,
                                    s64 timeout) {
    // Allocate space on stack for thread nodes.
    std::array<ThreadListNode, Svc::ArgumentHandleCountMax> thread_nodes;

    // Prepare for wait.
    KThread* thread = GetCurrentThreadPointer(kernel);
    KHardwareTimer* timer{};
    ThreadQueueImplForKSynchronizationObjectWait wait_queue(kernel, objects, thread_nodes.data(),
                                                            num_objects);

    {
        // Setup the scheduling lock and sleep.
        KScopedSchedulerLockAndSleep slp(kernel, std::addressof(timer), thread, timeout);

        // Check if the thread should terminate.
        if (thread->IsTerminationRequested()) {
            slp.CancelSleep();
            R_THROW(ResultTerminationRequested);
        }

        // Check if any of the objects are already signaled.
        for (auto i = 0; i < num_objects; ++i) {
            ASSERT(objects[i] != nullptr);

            if (objects[i]->IsSignaled()) {
                *out_index = i;
                slp.CancelSleep();
                R_THROW(ResultSuccess);
            }
        }

        // Check if the timeout is zero.
        if (timeout == 0) {
            slp.CancelSleep();
            R_THROW(ResultTimedOut);
        }

        // Check if waiting was canceled.
        if (thread->IsWaitCancelled()) {
            slp.CancelSleep();
            thread->ClearWaitCancelled();
            R_THROW(ResultCancelled);
        }

        // Add the waiters.
        for (auto i = 0; i < num_objects; ++i) {
            thread_nodes[i].thread = thread;
            thread_nodes[i].next = nullptr;

            objects[i]->LinkNode(std::addressof(thread_nodes[i]));
        }

        // Mark the thread as cancellable.
        thread->SetCancellable();

        // Clear the thread's synced index.
        thread->SetSyncedIndex(-1);

        // Wait for an object to be signaled.
        wait_queue.SetHardwareTimer(timer);
        thread->BeginWait(std::addressof(wait_queue));
        thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Synchronization);
    }

    // Set the output index.
    *out_index = thread->GetSyncedIndex();

    // Get the wait result.
    R_RETURN(thread->GetWaitResult());
}

KSynchronizationObject::KSynchronizationObject(KernelCore& kernel) : KAutoObjectWithList{kernel} {}

KSynchronizationObject::~KSynchronizationObject() = default;

void KSynchronizationObject::NotifyAvailable(Result result) {
    KScopedSchedulerLock sl(m_kernel);

    // If we're not signaled, we've nothing to notify.
    if (!this->IsSignaled()) {
        return;
    }

    // Iterate over each thread.
    for (auto* cur_node = m_thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
        cur_node->thread->NotifyAvailable(this, result);
    }
}

std::vector<KThread*> KSynchronizationObject::GetWaitingThreadsForDebugging() const {
    std::vector<KThread*> threads;

    // If debugging, dump the list of waiters.
    {
        KScopedSchedulerLock lock(m_kernel);
        for (auto* cur_node = m_thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
            threads.emplace_back(cur_node->thread);
        }
    }

    return threads;
}
} // namespace Kernel


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <vector>

#include "core/hle/kernel/k_auto_object.h"
#include "core/hle/result.h"

namespace Kernel {

class KernelCore;
class Synchronization;
class KThread;

/// Class that represents a Kernel object that a thread can be waiting on
class KSynchronizationObject : public KAutoObjectWithList {
    KERNEL_AUTOOBJECT_TRAITS(KSynchronizationObject, KAutoObject);

public:
    struct ThreadListNode {
        ThreadListNode* next{};
        KThread* thread{};
    };

    static Result Wait(KernelCore& kernel, s32* out_index, KSynchronizationObject** objects,
                       const s32 num_objects, s64 timeout);

    void Finalize() override;

    virtual bool IsSignaled() const = 0;

    std::vector<KThread*> GetWaitingThreadsForDebugging() const;

    void LinkNode(ThreadListNode* node_) {
        // Link the node to the list.
        if (m_thread_list_tail == nullptr) {
            m_thread_list_head = node_;
        } else {
            m_thread_list_tail->next = node_;
        }

        m_thread_list_tail = node_;
    }

    void UnlinkNode(ThreadListNode* node_) {
        // Unlink the node from the list.
        ThreadListNode* prev_ptr =
            reinterpret_cast<ThreadListNode*>(std::addressof(m_thread_list_head));
        ThreadListNode* prev_val = nullptr;
        ThreadListNode *prev, *tail_prev;

        do {
            prev = prev_ptr;
            prev_ptr = prev_ptr->next;
            tail_prev = prev_val;
            prev_val = prev_ptr;
        } while (prev_ptr != node_);

        if (m_thread_list_tail == node_) {
            m_thread_list_tail = tail_prev;
        }

        prev->next = node_->next;
    }

protected:
    explicit KSynchronizationObject(KernelCore& kernel);
    ~KSynchronizationObject() override;

    virtual void OnFinalizeSynchronizationObject() {}

    void NotifyAvailable(Result result);
    void NotifyAvailable() {
        return this->NotifyAvailable(ResultSuccess);
    }

private:
    ThreadListNode* m_thread_list_head{};
    ThreadListNode* m_thread_list_tail{};
};

} // namespace Kernel

repo_name: litucks/torzu

license: mit


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/common_types.h"

#define BOARD_NINTENDO_NX

#ifdef BOARD_NINTENDO_NX

#include "core/hle/kernel/board/nintendo/nx/k_system_control.h"

namespace Kernel {

using Kernel::Board::Nintendo::Nx::KSystemControl;

} // namespace Kernel

#else
#error "Unknown board for KSystemControl"
#endif


code:
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/core.h"
#include "core/hle/kernel/k_scoped_resource_reservation.h"
#include "core/hle/kernel/k_system_resource.h"

namespace Kernel {

Result KSecureSystemResource::Initialize(size_t size, KResourceLimit* resource_limit,
                                         KMemoryManager::Pool pool) {
    // Set members.
    m_resource_limit = resource_limit;
    m_resource_size = size;
    m_resource_pool = pool;

    // Determine required size for our secure resource.
    const size_t secure_size = this->CalculateRequiredSecureMemorySize();

    // Reserve memory for our secure resource.
    KScopedResourceReservation memory_reservation(
        m_resource_limit, Svc::LimitableResource::PhysicalMemoryMax, secure_size);
    R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached);

    // Allocate secure memory.
    R_TRY(KSystemControl::AllocateSecureMemory(m_kernel, std::addressof(m_resource_address),
                                               m_resource_size, static_cast<u32>(m_resource_pool)));
    ASSERT(m_resource_address != 0);

    // Ensure we clean up the secure memory, if we fail past this point.
    ON_RESULT_FAILURE {
        KSystemControl::FreeSecureMemory(m_kernel, m_resource_address, m_resource_size,
                                         static_cast<u32>(m_resource_pool));
    };

    // Check that our allocation is bigger than the reference counts needed for it.
    const size_t rc_size =
        Common::AlignUp(KPageTableSlabHeap::CalculateReferenceCountSize(m_resource_size), PageSize);
    R_UNLESS(m_resource_size > rc_size, ResultOutOfMemory);

    // Get resource pointer.
    KPhysicalAddress resource_paddr =
        KPageTable::GetHeapPhysicalAddress(m_kernel, m_resource_address);
    auto* resource =
        m_kernel.System().DeviceMemory().GetPointer<KPageTableManager::RefCount>(resource_paddr);

    // Initialize slab heaps.
    m_dynamic_page_manager.Initialize(m_resource_address + rc_size, m_resource_size - rc_size,
                                      PageSize);
    m_page_table_heap.Initialize(std::addressof(m_dynamic_page_manager), 0, resource);
    m_memory_block_heap.Initialize(std::addressof(m_dynamic_page_manager), 0);
    m_block_info_heap.Initialize(std::addressof(m_dynamic_page_manager), 0);

    // Initialize managers.
    m_page_table_manager.Initialize(std::addressof(m_dynamic_page_manager),
                                    std::addressof(m_page_table_heap));
    m_memory_block_slab_manager.Initialize(std::addressof(m_dynamic_page_manager),
                                           std::addressof(m_memory_block_heap));
    m_block_info_manager.Initialize(std::addressof(m_dynamic_page_manager),
                                    std::addressof(m_block_info_heap));

    // Set our managers.
    this->SetManagers(m_memory_block_slab_manager, m_block_info_manager, m_page_table_manager);

    // Commit the memory reservation.
    memory_reservation.Commit();

    // Open reference to our resource limit.
    m_resource_limit->Open();

    // Set ourselves as initialized.
    m_is_initialized = true;

    R_SUCCEED();
}

void KSecureSystemResource::Finalize() {
    // Check that we have no outstanding allocations.
    ASSERT(m_memory_block_slab_manager.GetUsed() == 0);
    ASSERT(m_block_info_manager.GetUsed() == 0);
    ASSERT(m_page_table_manager.GetUsed() == 0);

    // Free our secure memory.
    KSystemControl::FreeSecureMemory(m_kernel, m_resource_address, m_resource_size,
                                     static_cast<u32>(m_resource_pool));

    // Release the memory reservation.
    m_resource_limit->Release(Svc::LimitableResource::PhysicalMemoryMax,
                              this->CalculateRequiredSecureMemorySize());

    // Close reference to our resource limit.
    m_resource_limit->Close();
}

size_t KSecureSystemResource::CalculateRequiredSecureMemorySize(size_t size,
                                                                KMemoryManager::Pool pool) {
    return KSystemControl::CalculateRequiredSecureMemorySize(size, static_cast<u32>(pool));
}

} // namespace Kernel


code:
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "common/assert.h"
#include "common/common_types.h"
#include "core/hle/kernel/k_auto_object.h"
#include "core/hle/kernel/k_dynamic_resource_manager.h"
#include "core/hle/kernel/k_memory_manager.h"
#include "core/hle/kernel/k_page_table_manager.h"
#include "core/hle/kernel/k_resource_limit.h"
#include "core/hle/kernel/slab_helpers.h"

namespace Kernel {

// NOTE: Nintendo's implementation does not have the "is_secure_resource" field, and instead uses
// virtual IsSecureResource().

class KSystemResource : public KAutoObject {
    KERNEL_AUTOOBJECT_TRAITS(KSystemResource, KAutoObject);

public:
    explicit KSystemResource(KernelCore& kernel) : KAutoObject(kernel) {}

protected:
    void SetSecureResource() {
        m_is_secure_resource = true;
    }

public:
    virtual void Destroy() override {
        UNREACHABLE_MSG("KSystemResource::Destroy() was called");
    }

    bool IsSecureResource() const {
        return m_is_secure_resource;
    }

    void SetManagers(KMemoryBlockSlabManager& mb, KBlockInfoManager& bi, KPageTableManager& pt) {
        ASSERT(m_p_memory_block_slab_manager == nullptr);
        ASSERT(m_p_block_info_manager == nullptr);
        ASSERT(m_p_page_table_manager == nullptr);

        m_p_memory_block_slab_manager = std::addressof(mb);
        m_p_block_info_manager = std::addressof(bi);
        m_p_page_table_manager = std::addressof(pt);
    }

    const KMemoryBlockSlabManager& GetMemoryBlockSlabManager() const {
        return *m_p_memory_block_slab_manager;
    }
    const KBlockInfoManager& GetBlockInfoManager() const {
        return *m_p_block_info_manager;
    }
    const KPageTableManager& GetPageTableManager() const {
        return *m_p_page_table_manager;
    }

    KMemoryBlockSlabManager& GetMemoryBlockSlabManager() {
        return *m_p_memory_block_slab_manager;
    }
    KBlockInfoManager& GetBlockInfoManager() {
        return *m_p_block_info_manager;
    }
    KPageTableManager& GetPageTableManager() {
        return *m_p_page_table_manager;
    }

    KMemoryBlockSlabManager* GetMemoryBlockSlabManagerPointer() {
        return m_p_memory_block_slab_manager;
    }
    KBlockInfoManager* GetBlockInfoManagerPointer() {
        return m_p_block_info_manager;
    }
    KPageTableManager* GetPageTableManagerPointer() {
        return m_p_page_table_manager;
    }

private:
    KMemoryBlockSlabManager* m_p_memory_block_slab_manager{};
    KBlockInfoManager* m_p_block_info_manager{};
    KPageTableManager* m_p_page_table_manager{};
    bool m_is_secure_resource{false};
};

class KSecureSystemResource final
    : public KAutoObjectWithSlabHeap<KSecureSystemResource, KSystemResource> {
public:
    explicit KSecureSystemResource(KernelCore& kernel)
        : KAutoObjectWithSlabHeap<KSecureSystemResource, KSystemResource>(kernel) {
        // Mark ourselves as being a secure resource.
        this->SetSecureResource();
    }

    Result Initialize(size_t size, KResourceLimit* resource_limit, KMemoryManager::Pool pool);
    void Finalize();

    bool IsInitialized() const {
        return m_is_initialized;
    }
    static void PostDestroy(uintptr_t arg) {}

    size_t CalculateRequiredSecureMemorySize() const {
        return CalculateRequiredSecureMemorySize(m_resource_size, m_resource_pool);
    }

    size_t GetSize() const {
        return m_resource_size;
    }
    size_t GetUsedSize() const {
        return m_dynamic_page_manager.GetUsed() * PageSize;
    }

    const KDynamicPageManager& GetDynamicPageManager() const {
        return m_dynamic_page_manager;
    }

public:
    static size_t CalculateRequiredSecureMemorySize(size_t size, KMemoryManager::Pool pool);

private:
    bool m_is_initialized{};
    KMemoryManager::Pool m_resource_pool{};
    KDynamicPageManager m_dynamic_page_manager;
    KMemoryBlockSlabManager m_memory_block_slab_manager;
    KBlockInfoManager m_block_info_manager;
    KPageTableManager m_page_table_manager;
    KMemoryBlockSlabHeap m_memory_block_heap;
    KBlockInfoSlabHeap m_block_info_heap;
    KPageTableSlabHeap m_page_table_heap;
    KResourceLimit* m_resource_limit{};
    KVirtualAddress m_resource_address{};
    size_t m_resource_size{};
};

} // namespace Kernel


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>
#include <atomic>
#include <cinttypes>
#include <condition_variable>
#include <mutex>
#include <optional>
#include <vector>

#include "common/assert.h"
#include "common/bit_util.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/fiber.h"
#include "common/logging/log.h"
#include "common/settings.h"
#include "core/core.h"
#include "core/cpu_manager.h"
#include "core/hardware_properties.h"
#include "core/hle/kernel/k_condition_variable.h"
#include "core/hle/kernel/k_handle_table.h"
#include "core/hle/kernel/k_memory_layout.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_resource_limit.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
#include "core/hle/kernel/k_system_control.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_queue.h"
#include "core/hle/kernel/k_worker_task_manager.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc.h"
#include "core/hle/kernel/svc_results.h"
#include "core/hle/kernel/svc_types.h"
#include "core/hle/result.h"
#include "core/memory.h"

namespace {

constexpr inline s32 TerminatingThreadPriority = Kernel::Svc::SystemThreadPriorityHighest - 1;

static void ResetThreadContext32(Kernel::Svc::ThreadContext& ctx, u64 stack_top, u64 entry_point,
                                 u64 arg) {
    ctx = {};
    ctx.r[0] = arg;
    ctx.r[15] = entry_point;
    ctx.r[13] = stack_top;
    ctx.fpcr = 0;
    ctx.fpsr = 0;
}

static void ResetThreadContext64(Kernel::Svc::ThreadContext& ctx, u64 stack_top, u64 entry_point,
                                 u64 arg) {
    ctx = {};
    ctx.r[0] = arg;
    ctx.r[18] = Kernel::KSystemControl::GenerateRandomU64() | 1;
    ctx.pc = entry_point;
    ctx.sp = stack_top;
    ctx.fpcr = 0;
    ctx.fpsr = 0;
}
} // namespace

namespace Kernel {

namespace {

struct ThreadLocalRegion {
    static constexpr std::size_t MessageBufferSize = 0x100;
    std::array<u32, MessageBufferSize / sizeof(u32)> message_buffer;
    std::atomic_uint16_t disable_count;
    std::atomic_uint16_t interrupt_flag;
};

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)
        : KThreadQueue(kernel), m_wait_list(wl) {}

    void CancelWait(KThread* waiting_thread, Result 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);
    }

private:
    KThread::WaiterList* m_wait_list{};
};

} // namespace

KThread::KThread(KernelCore& kernel)
    : KAutoObjectWithSlabHeapAndContainer{kernel}, m_activity_pause_lock{kernel} {}
KThread::~KThread() = default;

Result KThread::Initialize(KThreadFunction func, uintptr_t arg, KProcessAddress user_stack_top,
                           s32 prio, s32 virt_core, KProcess* owner, ThreadType type) {
    // Assert parameters are valid.
    ASSERT((type == ThreadType::Main) || (type == ThreadType::Dummy) ||
           (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.
    m_tls_address = {};

    // Next, assert things based on the type.
    switch (type) {
    case ThreadType::Main:
        ASSERT(arg == 0);
        [[fallthrough]];
    case ThreadType::User:
        ASSERT(((owner == nullptr) ||
                (owner->GetCoreMask() | (1ULL << virt_core)) == owner->GetCoreMask()));
        ASSERT(((owner == nullptr) || (prio > Svc::LowestThreadPriority) ||
                (owner->GetPriorityMask() | (1ULL << prio)) == owner->GetPriorityMask()));
        break;
    case ThreadType::HighPriority:
    case ThreadType::Dummy:
        break;
    case ThreadType::Kernel:
        UNIMPLEMENTED();
        break;
    default:
        ASSERT_MSG(false, "KThread::Initialize: Unknown ThreadType {}", static_cast<u32>(type));
        break;
    }
    m_thread_type = type;

    // Set the ideal core ID and affinity mask.
    m_virtual_ideal_core_id = virt_core;
    m_physical_ideal_core_id = phys_core;
    m_virtual_affinity_mask = 1ULL << virt_core;
    m_physical_affinity_mask.SetAffinity(phys_core, true);

    // Set the thread state.
    m_thread_state = (type == ThreadType::Main || type == ThreadType::Dummy)
                         ? ThreadState::Runnable
                         : ThreadState::Initialized;

    // Set TLS address.
    m_tls_address = 0;

    // Set parent and condvar tree.
    m_parent = nullptr;
    m_condvar_tree = nullptr;

    // Set sync booleans.
    m_signaled = false;
    m_termination_requested = false;
    m_wait_cancelled = false;
    m_cancellable = false;

    // Set core ID and wait result.
    m_core_id = phys_core;
    m_wait_result = ResultNoSynchronizationObject;

    // Set priorities.
    m_priority = prio;
    m_base_priority = prio;

    // Initialize sleeping queue.
    m_wait_queue = nullptr;

    // Set suspend flags.
    m_suspend_request_flags = 0;
    m_suspend_allowed_flags = static_cast<u32>(ThreadState::SuspendFlagMask);

    // We're neither debug attached, nor are we nesting our priority inheritance.
    m_debug_attached = false;
    m_priority_inheritance_count = 0;

    // We haven't been scheduled, and we have done no light IPC.
    m_schedule_count = -1;
    m_last_scheduled_tick = 0;
    m_light_ipc_data = nullptr;

    // We're not waiting for a lock, and we haven't disabled migration.
    m_waiting_lock_info = nullptr;
    m_num_core_migration_disables = 0;

    // We have no waiters, but we do have an entrypoint.
    m_num_kernel_waiters = 0;

    // Set our current core id.
    m_current_core_id = phys_core;

    // We haven't released our resource limit hint, and we've spent no time on the cpu.
    m_resource_limit_release_hint = false;
    m_cpu_time = 0;

    // Set debug context.
    m_stack_top = user_stack_top;
    m_argument = arg;

    // Clear our stack parameters.
    std::memset(static_cast<void*>(std::addressof(this->GetStackParameters())), 0,
                sizeof(StackParameters));

    // Set parent, if relevant.
    if (owner != nullptr) {
        // Setup the TLS, if needed.
        if (type == ThreadType::User) {
            R_TRY(owner->CreateThreadLocalRegion(std::addressof(m_tls_address)));
            owner->GetMemory().ZeroBlock(m_tls_address, Svc::ThreadLocalRegionSize);
        }

        m_parent = owner;
        m_parent->Open();
    }

    // Initialize thread context.
    if (m_parent != nullptr && !m_parent->Is64Bit()) {
        ResetThreadContext32(m_thread_context, GetInteger(user_stack_top), GetInteger(func), arg);
    } else {
        ResetThreadContext64(m_thread_context, GetInteger(user_stack_top), GetInteger(func), arg);
    }

    // Setup the stack parameters.
    StackParameters& sp = this->GetStackParameters();
    sp.cur_thread = this;
    sp.disable_count = 1;
    this->SetInExceptionHandler();

    // Set thread ID.
    m_thread_id = m_kernel.CreateNewThreadID();

    // We initialized!
    m_initialized = true;

    // Register ourselves with our parent process.
    if (m_parent != nullptr) {
        m_parent->RegisterThread(this);
        if (m_parent->IsSuspended()) {
            RequestSuspend(SuspendType::Process);
        }
    }

    R_SUCCEED();
}

Result KThread::InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg,
                                 KProcessAddress user_stack_top, s32 prio, s32 core,
                                 KProcess* owner, ThreadType type,
                                 std::function<void()>&& init_func) {
    // Initialize the thread.
    R_TRY(thread->Initialize(func, arg, user_stack_top, prio, core, owner, type));

    // Initialize emulation parameters.
    thread->m_host_context = std::make_shared<Common::Fiber>(std::move(init_func));

    R_SUCCEED();
}

Result KThread::InitializeDummyThread(KThread* thread, KProcess* owner) {
    // Initialize the thread.
    R_TRY(thread->Initialize({}, {}, {}, DummyThreadPriority, 3, owner, ThreadType::Dummy));

    // Initialize emulation parameters.
    thread->m_stack_parameters.disable_count = 0;

    R_SUCCEED();
}

Result KThread::InitializeMainThread(Core::System& system, KThread* thread, s32 virt_core) {
    R_RETURN(InitializeThread(thread, {}, {}, {}, IdleThreadPriority, virt_core, {},
                              ThreadType::Main, system.GetCpuManager().GetGuestActivateFunc()));
}

Result KThread::InitializeIdleThread(Core::System& system, KThread* thread, s32 virt_core) {
    R_RETURN(InitializeThread(thread, {}, {}, {}, IdleThreadPriority, virt_core, {},
                              ThreadType::Main, system.GetCpuManager().GetIdleThreadStartFunc()));
}

Result KThread::InitializeHighPriorityThread(Core::System& system, KThread* thread,
                                             KThreadFunction func, uintptr_t arg, s32 virt_core) {
    R_RETURN(InitializeThread(thread, func, arg, {}, {}, virt_core, nullptr,
                              ThreadType::HighPriority,
                              system.GetCpuManager().GetShutdownThreadStartFunc()));
}

Result KThread::InitializeUserThread(Core::System& system, KThread* thread, KThreadFunction func,
                                     uintptr_t arg, KProcessAddress user_stack_top, s32 prio,
                                     s32 virt_core, KProcess* owner) {
    system.Kernel().GlobalSchedulerContext().AddThread(thread);
    R_RETURN(InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner,
                              ThreadType::User, system.GetCpuManager().GetGuestThreadFunc()));
}

Result KThread::InitializeServiceThread(Core::System& system, KThread* thread,
                                        std::function<void()>&& func, s32 prio, s32 virt_core,
                                        KProcess* owner) {
    system.Kernel().GlobalSchedulerContext().AddThread(thread);
    std::function<void()> func2{[&system, func_{std::move(func)}] {
        // Similar to UserModeThreadStarter.
        system.Kernel().CurrentScheduler()->OnThreadStart();

        // Run the guest function.
        func_();

        // Exit.
        Svc::ExitThread(system);
    }};

    R_RETURN(InitializeThread(thread, {}, {}, {}, prio, virt_core, owner, ThreadType::HighPriority,
                              std::move(func2)));
}

void KThread::PostDestroy(uintptr_t arg) {
    KProcess* owner = reinterpret_cast<KProcess*>(arg & ~1ULL);
    const bool resource_limit_release_hint = (arg & 1);
    const s64 hint_value = (resource_limit_release_hint ? 0 : 1);
    if (owner != nullptr) {
        owner->GetResourceLimit()->Release(LimitableResource::ThreadCountMax, 1, hint_value);
        owner->Close();
    }
}

void KThread::Finalize() {
    // If the thread has an owner process, unregister it.
    if (m_parent != nullptr) {
        m_parent->UnregisterThread(this);
    }

    // If the thread has a local region, delete it.
    if (m_tls_address != 0) {
        ASSERT(m_parent->DeleteThreadLocalRegion(m_tls_address).IsSuccess());
    }

    // Release any waiters.
    {
        ASSERT(m_waiting_lock_info == nullptr);
        KScopedSchedulerLock sl{m_kernel};

        // Check that we have no kernel waiters.
        ASSERT(m_num_kernel_waiters == 0);

        auto it = m_held_lock_info_list.begin();
        while (it != m_held_lock_info_list.end()) {
            // Get the lock info.
            auto* const lock_info = std::addressof(*it);

            // The lock shouldn't have a kernel waiter.
            ASSERT(!lock_info->GetIsKernelAddressKey());

            // Remove all waiters.
            while (lock_info->GetWaiterCount() != 0) {
                // Get the front waiter.
                KThread* const waiter = lock_info->GetHighestPriorityWaiter();

                // Remove it from the lock.
                if (lock_info->RemoveWaiter(waiter)) {
                    ASSERT(lock_info->GetWaiterCount() == 0);
                }

                // Cancel the thread's wait.
                waiter->CancelWait(ResultInvalidState, true);
            }

            // Remove the held lock from our list.
            it = m_held_lock_info_list.erase(it);

            // Free the lock info.
            LockWithPriorityInheritanceInfo::Free(m_kernel, lock_info);
        }
    }

    // Release host emulation members.
    m_host_context.reset();

    // Perform inherited finalization.
    KSynchronizationObject::Finalize();
}

bool KThread::IsSignaled() const {
    return m_signaled;
}

void KThread::OnTimer() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // If we're waiting, cancel the wait.
    if (this->GetState() == ThreadState::Waiting) {
        m_wait_queue->CancelWait(this, ResultTimedOut, false);
    }
}

void KThread::StartTermination() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Release user exception and unpin, if relevant.
    if (m_parent != nullptr) {
        m_parent->ReleaseUserException(this);
        if (m_parent->GetPinnedThread(GetCurrentCoreId(m_kernel)) == this) {
            m_parent->UnpinCurrentThread();
        }
    }

    // Set state to terminated.
    this->SetState(ThreadState::Terminated);

    // Clear the thread's status as running in parent.
    if (m_parent != nullptr) {
        m_parent->ClearRunningThread(this);
    }

    // Clear previous thread in KScheduler.
    KScheduler::ClearPreviousThread(m_kernel, this);

    // Register terminated dpc flag.
    this->RegisterDpc(DpcFlag::Terminated);
}

void KThread::FinishTermination() {
    // Ensure that the thread is not executing on any core.
    if (m_parent != nullptr) {
        for (std::size_t i = 0; i < static_cast<std::size_t>(Core::Hardware::NUM_CPU_CORES); ++i) {
            KThread* core_thread{};
            do {
                core_thread = m_kernel.Scheduler(i).GetSchedulerCurrentThread();
            } while (core_thread == this);
        }
    }

    // Acquire the scheduler lock.
    KScopedSchedulerLock sl{m_kernel};

    // Signal.
    m_signaled = true;
    KSynchronizationObject::NotifyAvailable();

    // Close the thread.
    this->Close();
}

void KThread::DoWorkerTaskImpl() {
    // Finish the termination that was begun by Exit().
    this->FinishTermination();
}

void KThread::Pin(s32 current_core) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Set ourselves as pinned.
    GetStackParameters().is_pinned = true;

    // Disable core migration.
    ASSERT(m_num_core_migration_disables == 0);
    {
        ++m_num_core_migration_disables;

        // Save our ideal state to restore when we're unpinned.
        m_original_physical_ideal_core_id = m_physical_ideal_core_id;
        m_original_physical_affinity_mask = m_physical_affinity_mask;

        // Bind ourselves to this core.
        const s32 active_core = this->GetActiveCore();

        this->SetActiveCore(current_core);
        m_physical_ideal_core_id = current_core;
        m_physical_affinity_mask.SetAffinityMask(1ULL << current_core);

        if (active_core != current_core ||
            m_physical_affinity_mask.GetAffinityMask() !=
                m_original_physical_affinity_mask.GetAffinityMask()) {
            KScheduler::OnThreadAffinityMaskChanged(m_kernel, this,
                                                    m_original_physical_affinity_mask, active_core);
        }
    }

    // Disallow performing thread suspension.
    {
        // Update our allow flags.
        m_suspend_allowed_flags &= ~(1 << (static_cast<u32>(SuspendType::Thread) +
                                           static_cast<u32>(ThreadState::SuspendShift)));

        // Update our state.
        this->UpdateState();
    }

    // TODO(bunnei): Update our SVC access permissions.
    ASSERT(m_parent != nullptr);
}

void KThread::Unpin() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Set ourselves as unpinned.
    this->GetStackParameters().is_pinned = false;

    // Enable core migration.
    ASSERT(m_num_core_migration_disables == 1);
    {
        m_num_core_migration_disables--;

        // Restore our original state.
        const KAffinityMask old_mask = m_physical_affinity_mask;

        m_physical_ideal_core_id = m_original_physical_ideal_core_id;
        m_physical_affinity_mask = m_original_physical_affinity_mask;

        if (m_physical_affinity_mask.GetAffinityMask() != old_mask.GetAffinityMask()) {
            const s32 active_core = this->GetActiveCore();

            if (!m_physical_affinity_mask.GetAffinity(active_core)) {
                if (m_physical_ideal_core_id >= 0) {
                    this->SetActiveCore(m_physical_ideal_core_id);
                } else {
                    this->SetActiveCore(static_cast<s32>(
                        Common::BitSize<u64>() - 1 -
                        std::countl_zero(m_physical_affinity_mask.GetAffinityMask())));
                }
            }
            KScheduler::OnThreadAffinityMaskChanged(m_kernel, this, old_mask, active_core);
        }
    }

    // Allow performing thread suspension (if termination hasn't been requested).
    if (!this->IsTerminationRequested()) {
        // Update our allow flags.
        m_suspend_allowed_flags |= (1 << (static_cast<u32>(SuspendType::Thread) +
                                          static_cast<u32>(ThreadState::SuspendShift)));

        // Update our state.
        this->UpdateState();
    }

    // TODO(bunnei): Update our SVC access permissions.
    ASSERT(m_parent != nullptr);

    // Resume any threads that began waiting on us while we were pinned.
    for (auto it = m_pinned_waiter_list.begin(); it != m_pinned_waiter_list.end();
         it = m_pinned_waiter_list.erase(it)) {
        it->EndWait(ResultSuccess);
    }
}

u16 KThread::GetUserDisableCount() const {
    if (!this->IsUserThread()) {
        // We only emulate TLS for user threads
        return {};
    }

    auto& memory = this->GetOwnerProcess()->GetMemory();
    return memory.Read16(m_tls_address + offsetof(ThreadLocalRegion, disable_count));
}

void KThread::SetInterruptFlag() {
    if (!this->IsUserThread()) {
        // We only emulate TLS for user threads
        return;
    }

    auto& memory = this->GetOwnerProcess()->GetMemory();
    memory.Write16(m_tls_address + offsetof(ThreadLocalRegion, interrupt_flag), 1);
}

void KThread::ClearInterruptFlag() {
    if (!this->IsUserThread()) {
        // We only emulate TLS for user threads
        return;
    }

    auto& memory = this->GetOwnerProcess()->GetMemory();
    memory.Write16(m_tls_address + offsetof(ThreadLocalRegion, interrupt_flag), 0);
}

Result KThread::GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask) {
    KScopedSchedulerLock sl{m_kernel};

    // Get the virtual mask.
    *out_ideal_core = m_virtual_ideal_core_id;
    *out_affinity_mask = m_virtual_affinity_mask;

    R_SUCCEED();
}

Result KThread::GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask) {
    KScopedSchedulerLock sl{m_kernel};
    ASSERT(m_num_core_migration_disables >= 0);

    // Select between core mask and original core mask.
    if (m_num_core_migration_disables == 0) {
        *out_ideal_core = m_physical_ideal_core_id;
        *out_affinity_mask = m_physical_affinity_mask.GetAffinityMask();
    } else {
        *out_ideal_core = m_original_physical_ideal_core_id;
        *out_affinity_mask = m_original_physical_affinity_mask.GetAffinityMask();
    }

    R_SUCCEED();
}

Result KThread::SetCoreMask(s32 core_id, u64 v_affinity_mask) {
    ASSERT(m_parent != nullptr);
    ASSERT(v_affinity_mask != 0);
    KScopedLightLock lk(m_activity_pause_lock);

    // Set the core mask.
    u64 p_affinity_mask = 0;
    {
        KScopedSchedulerLock sl(m_kernel);
        ASSERT(m_num_core_migration_disables >= 0);

        // If we're updating, set our ideal virtual core.
        if (core_id != Svc::IdealCoreNoUpdate) {
            m_virtual_ideal_core_id = core_id;
        } else {
            // Preserve our ideal core id.
            core_id = m_virtual_ideal_core_id;
            R_UNLESS(((1ULL << core_id) & v_affinity_mask) != 0, ResultInvalidCombination);
        }

        // Set our affinity mask.
        m_virtual_affinity_mask = v_affinity_mask;

        // Translate the virtual core to a physical core.
        if (core_id >= 0) {
            core_id = Core::Hardware::VirtualToPhysicalCoreMap[core_id];
        }

        // 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 (m_num_core_migration_disables == 0) {
            const KAffinityMask old_mask = m_physical_affinity_mask;

            // Set our new ideals.
            m_physical_ideal_core_id = core_id;
            m_physical_affinity_mask.SetAffinityMask(p_affinity_mask);

            if (m_physical_affinity_mask.GetAffinityMask() != old_mask.GetAffinityMask()) {
                const s32 active_core = GetActiveCore();

                if (active_core >= 0 && !m_physical_affinity_mask.GetAffinity(active_core)) {
                    const s32 new_core = static_cast<s32>(
                        m_physical_ideal_core_id >= 0
                            ? m_physical_ideal_core_id
                            : Common::BitSize<u64>() - 1 -
                                  std::countl_zero(m_physical_affinity_mask.GetAffinityMask()));
                    SetActiveCore(new_core);
                }
                KScheduler::OnThreadAffinityMaskChanged(m_kernel, this, old_mask, active_core);
            }
        } else {
            // Otherwise, we edit the original affinity for restoration later.
            m_original_physical_ideal_core_id = core_id;
            m_original_physical_affinity_mask.SetAffinityMask(p_affinity_mask);
        }
    }

    // Update the pinned waiter list.
    ThreadQueueImplForKThreadSetProperty wait_queue(m_kernel, std::addressof(m_pinned_waiter_list));
    {
        bool retry_update{};
        do {
            // Lock the scheduler.
            KScopedSchedulerLock sl(m_kernel);

            // Don't do any further management if our termination has been requested.
            R_SUCCEED_IF(this->IsTerminationRequested());

            // By default, we won't need to retry.
            retry_update = false;

            // Check if the thread is currently running.
            bool thread_is_current{};
            s32 thread_core;
            for (thread_core = 0; thread_core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES);
                 ++thread_core) {
                if (m_kernel.Scheduler(thread_core).GetSchedulerCurrentThread() == 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 (this->GetStackParameters().is_pinned) {
                    // Verify that the current thread isn't terminating.
                    R_UNLESS(!GetCurrentThread(m_kernel).IsTerminationRequested(),
                             ResultTerminationRequested);

                    // Wait until the thread isn't pinned any more.
                    m_pinned_waiter_list.push_back(GetCurrentThread(m_kernel));
                    GetCurrentThread(m_kernel).BeginWait(std::addressof(wait_queue));
                } else {
                    // If the thread isn't pinned, release the scheduler lock and retry until it's
                    // not current.
                    retry_update = true;
                }
            }
        } while (retry_update);
    }

    R_SUCCEED();
}

void KThread::SetBasePriority(s32 value) {
    ASSERT(Svc::HighestThreadPriority <= value && value <= Svc::LowestThreadPriority);

    KScopedSchedulerLock sl{m_kernel};

    // Change our base priority.
    m_base_priority = value;

    // Perform a priority restoration.
    RestorePriority(m_kernel, this);
}

KThread* KThread::GetLockOwner() const {
    return m_waiting_lock_info != nullptr ? m_waiting_lock_info->GetOwner() : nullptr;
}

void KThread::IncreaseBasePriority(s32 priority) {
    ASSERT(Svc::HighestThreadPriority <= priority && priority <= Svc::LowestThreadPriority);
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(!this->GetStackParameters().is_pinned);

    // Set our base priority.
    if (m_base_priority > priority) {
        m_base_priority = priority;

        // Perform a priority restoration.
        RestorePriority(m_kernel, this);
    }
}

void KThread::RequestSuspend(SuspendType type) {
    KScopedSchedulerLock sl{m_kernel};

    // Note the request in our flags.
    m_suspend_request_flags |=
        (1U << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)));

    // Try to perform the suspend.
    this->TrySuspend();
}

void KThread::Resume(SuspendType type) {
    KScopedSchedulerLock sl{m_kernel};

    // Clear the request in our flags.
    m_suspend_request_flags &=
        ~(1U << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)));

    // Update our state.
    this->UpdateState();
}

void KThread::WaitCancel() {
    KScopedSchedulerLock sl{m_kernel};

    // Check if we're waiting and cancellable.
    if (this->GetState() == ThreadState::Waiting && m_cancellable) {
        m_wait_cancelled = false;
        m_wait_queue->CancelWait(this, ResultCancelled, true);
    } else {
        // Otherwise, note that we cancelled a wait.
        m_wait_cancelled = true;
    }
}

void KThread::TrySuspend() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(this->IsSuspendRequested());

    // Ensure that we have no waiters.
    if (this->GetNumKernelWaiters() > 0) {
        return;
    }
    ASSERT(this->GetNumKernelWaiters() == 0);

    // Perform the suspend.
    this->UpdateState();
}

void KThread::UpdateState() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Set our suspend flags in state.
    const ThreadState old_state = m_thread_state.load(std::memory_order_relaxed);
    const auto new_state =
        static_cast<ThreadState>(this->GetSuspendFlags()) | (old_state & ThreadState::Mask);
    m_thread_state.store(new_state, std::memory_order_relaxed);

    // Note the state change in scheduler.
    if (new_state != old_state) {
        KScheduler::OnThreadStateChanged(m_kernel, this, old_state);
    }
}

void KThread::Continue() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Clear our suspend flags in state.
    const ThreadState old_state = m_thread_state.load(std::memory_order_relaxed);
    m_thread_state.store(old_state & ThreadState::Mask, std::memory_order_relaxed);

    // Note the state change in scheduler.
    KScheduler::OnThreadStateChanged(m_kernel, this, old_state);
}

void KThread::CloneFpuStatus() {
    // We shouldn't reach here when starting kernel threads.
    ASSERT(this->GetOwnerProcess() != nullptr);
    ASSERT(this->GetOwnerProcess() == GetCurrentProcessPointer(m_kernel));

    m_kernel.CurrentPhysicalCore().CloneFpuStatus(this);
}

Result KThread::SetActivity(Svc::ThreadActivity activity) {
    // Lock ourselves.
    KScopedLightLock lk(m_activity_pause_lock);

    // Set the activity.
    {
        // Lock the scheduler.
        KScopedSchedulerLock sl(m_kernel);

        // Verify our state.
        const auto cur_state = this->GetState();
        R_UNLESS((cur_state == ThreadState::Waiting || cur_state == ThreadState::Runnable),
                 ResultInvalidState);

        // Either pause or resume.
        if (activity == Svc::ThreadActivity::Paused) {
            // Verify that we're not suspended.
            R_UNLESS(!this->IsSuspendRequested(SuspendType::Thread), ResultInvalidState);

            // Suspend.
            this->RequestSuspend(SuspendType::Thread);
        } else {
            ASSERT(activity == Svc::ThreadActivity::Runnable);

            // Verify that we're suspended.
            R_UNLESS(this->IsSuspendRequested(SuspendType::Thread), ResultInvalidState);

            // Resume.
            this->Resume(SuspendType::Thread);
        }
    }

    // If the thread is now paused, update the pinned waiter list.
    if (activity == Svc::ThreadActivity::Paused) {
        ThreadQueueImplForKThreadSetProperty wait_queue(m_kernel,
                                                        std::addressof(m_pinned_waiter_list));

        bool thread_is_current{};
        do {
            // Lock the scheduler.
            KScopedSchedulerLock sl(m_kernel);

            // Don't do any further management if our termination has been requested.
            R_SUCCEED_IF(this->IsTerminationRequested());

            // By default, treat the thread as not current.
            thread_is_current = false;

            // Check whether the thread is pinned.
            if (this->GetStackParameters().is_pinned) {
                // Verify that the current thread isn't terminating.
                R_UNLESS(!GetCurrentThread(m_kernel).IsTerminationRequested(),
                         ResultTerminationRequested);

                // Wait until the thread isn't pinned any more.
                m_pinned_waiter_list.push_back(GetCurrentThread(m_kernel));
                GetCurrentThread(m_kernel).BeginWait(std::addressof(wait_queue));
            } 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 (m_kernel.Scheduler(i).GetSchedulerCurrentThread() == this) {
                        thread_is_current = true;
                        break;
                    }
                }
            }
        } while (thread_is_current);
    }

    R_SUCCEED();
}

Result KThread::GetThreadContext3(Svc::ThreadContext* out) {
    // Lock ourselves.
    KScopedLightLock lk{m_activity_pause_lock};

    // Get the context.
    {
        // Lock the scheduler.
        KScopedSchedulerLock sl{m_kernel};

        // Verify that we're suspended.
        R_UNLESS(this->IsSuspendRequested(SuspendType::Thread), ResultInvalidState);

        // If we're not terminating, get the thread's user context.
        if (!this->IsTerminationRequested()) {
            *out = m_thread_context;

            // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
            constexpr u32 El0Aarch64PsrMask = 0xF0000000;
            constexpr u32 El0Aarch32PsrMask = 0xFE0FFE20;

            if (m_parent->Is64Bit()) {
                out->pstate &= El0Aarch64PsrMask;
            } else {
                out->pstate &= El0Aarch32PsrMask;
            }
        }
    }

    R_SUCCEED();
}

void KThread::AddHeldLock(LockWithPriorityInheritanceInfo* lock_info) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Set ourselves as the lock's owner.
    lock_info->SetOwner(this);

    // Add the lock to our held list.
    m_held_lock_info_list.push_front(*lock_info);
}

KThread::LockWithPriorityInheritanceInfo* KThread::FindHeldLock(KProcessAddress address_key,
                                                                bool is_kernel_address_key) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Try to find an existing held lock.
    for (auto& held_lock : m_held_lock_info_list) {
        if (held_lock.GetAddressKey() == address_key &&
            held_lock.GetIsKernelAddressKey() == is_kernel_address_key) {
            return std::addressof(held_lock);
        }
    }

    return nullptr;
}

void KThread::AddWaiterImpl(KThread* thread) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(thread->GetConditionVariableTree() == nullptr);

    // Get the thread's address key.
    const auto address_key = thread->GetAddressKey();
    const auto is_kernel_address_key = thread->GetIsKernelAddressKey();

    // Keep track of how many kernel waiters we have.
    if (is_kernel_address_key) {
        ASSERT((m_num_kernel_waiters++) >= 0);
        KScheduler::SetSchedulerUpdateNeeded(m_kernel);
    }

    // Get the relevant lock info.
    auto* lock_info = this->FindHeldLock(address_key, is_kernel_address_key);
    if (lock_info == nullptr) {
        // Create a new lock for the address key.
        lock_info =
            LockWithPriorityInheritanceInfo::Create(m_kernel, address_key, is_kernel_address_key);

        // Add the new lock to our list.
        this->AddHeldLock(lock_info);
    }

    // Add the thread as waiter to the lock info.
    lock_info->AddWaiter(thread);
}

void KThread::RemoveWaiterImpl(KThread* thread) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Keep track of how many kernel waiters we have.
    if (thread->GetIsKernelAddressKey()) {
        ASSERT((m_num_kernel_waiters--) > 0);
        KScheduler::SetSchedulerUpdateNeeded(m_kernel);
    }

    // Get the info for the lock the thread is waiting on.
    auto* lock_info = thread->GetWaitingLockInfo();
    ASSERT(lock_info->GetOwner() == this);

    // Remove the waiter.
    if (lock_info->RemoveWaiter(thread)) {
        m_held_lock_info_list.erase(m_held_lock_info_list.iterator_to(*lock_info));
        LockWithPriorityInheritanceInfo::Free(m_kernel, lock_info);
    }
}

void KThread::RestorePriority(KernelCore& kernel, KThread* thread) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(kernel));

    while (thread != nullptr) {
        // We want to inherit priority where possible.
        s32 new_priority = thread->GetBasePriority();
        for (const auto& held_lock : thread->m_held_lock_info_list) {
            new_priority =
                std::min(new_priority, held_lock.GetHighestPriorityWaiter()->GetPriority());
        }

        // If the priority we would inherit is not different from ours, don't do anything.
        if (new_priority == thread->GetPriority()) {
            return;
        }

        // Get the owner of whatever lock this thread is waiting on.
        KThread* const lock_owner = thread->GetLockOwner();

        // If the thread is waiting on some lock, remove it as a waiter to prevent violating red
        // black tree invariants.
        if (lock_owner != nullptr) {
            lock_owner->RemoveWaiterImpl(thread);
        }

        // Ensure we don't violate condition variable red black tree invariants.
        if (auto* cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
            BeforeUpdatePriority(kernel, cv_tree, thread);
        }

        // Change the priority.
        const s32 old_priority = thread->GetPriority();
        thread->SetPriority(new_priority);

        // Restore the condition variable, if relevant.
        if (auto* cv_tree = thread->GetConditionVariableTree(); cv_tree != nullptr) {
            AfterUpdatePriority(kernel, cv_tree, thread);
        }

        // If we removed the thread from some lock's waiting list, add it back.
        if (lock_owner != nullptr) {
            lock_owner->AddWaiterImpl(thread);
        }

        // Update the scheduler.
        KScheduler::OnThreadPriorityChanged(kernel, thread, old_priority);

        // Continue inheriting priority.
        thread = lock_owner;
    }
}

void KThread::AddWaiter(KThread* thread) {
    this->AddWaiterImpl(thread);

    // If the thread has a higher priority than us, we should inherit.
    if (thread->GetPriority() < this->GetPriority()) {
        RestorePriority(m_kernel, this);
    }
}

void KThread::RemoveWaiter(KThread* thread) {
    this->RemoveWaiterImpl(thread);

    // If our priority is the same as the thread's (and we've inherited), we may need to restore to
    // lower priority.
    if (this->GetPriority() == thread->GetPriority() &&
        this->GetPriority() < this->GetBasePriority()) {
        RestorePriority(m_kernel, this);
    }
}

KThread* KThread::RemoveWaiterByKey(bool* out_has_waiters, KProcessAddress key,
                                    bool is_kernel_address_key_) {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Get the relevant lock info.
    auto* lock_info = this->FindHeldLock(key, is_kernel_address_key_);
    if (lock_info == nullptr) {
        *out_has_waiters = false;
        return nullptr;
    }

    // Remove the lock info from our held list.
    m_held_lock_info_list.erase(m_held_lock_info_list.iterator_to(*lock_info));

    // Keep track of how many kernel waiters we have.
    if (lock_info->GetIsKernelAddressKey()) {
        m_num_kernel_waiters -= lock_info->GetWaiterCount();
        ASSERT(m_num_kernel_waiters >= 0);
        KScheduler::SetSchedulerUpdateNeeded(m_kernel);
    }

    ASSERT(lock_info->GetWaiterCount() > 0);

    // Remove the highest priority waiter from the lock to be the next owner.
    KThread* next_lock_owner = lock_info->GetHighestPriorityWaiter();
    if (lock_info->RemoveWaiter(next_lock_owner)) {
        // The new owner was the only waiter.
        *out_has_waiters = false;

        // Free the lock info, since it has no waiters.
        LockWithPriorityInheritanceInfo::Free(m_kernel, lock_info);
    } else {
        // There are additional waiters on the lock.
        *out_has_waiters = true;

        // Add the lock to the new owner's held list.
        next_lock_owner->AddHeldLock(lock_info);

        // Keep track of any kernel waiters for the new owner.
        if (lock_info->GetIsKernelAddressKey()) {
            next_lock_owner->m_num_kernel_waiters += lock_info->GetWaiterCount();
            ASSERT(next_lock_owner->m_num_kernel_waiters > 0);

            // NOTE: No need to set scheduler update needed, because we will have already done so
            // when removing earlier.
        }
    }

    // If our priority is the same as the next owner's (and we've inherited), we may need to restore
    // to lower priority.
    if (this->GetPriority() == next_lock_owner->GetPriority() &&
        this->GetPriority() < this->GetBasePriority()) {
        RestorePriority(m_kernel, this);
        // NOTE: No need to restore priority on the next lock owner, because it was already the
        // highest priority waiter on the lock.
    }

    // Return the next lock owner.
    return next_lock_owner;
}

Result KThread::Run() {
    while (true) {
        KScopedSchedulerLock lk{m_kernel};

        // If either this thread or the current thread are requesting termination, note it.
        R_UNLESS(!this->IsTerminationRequested(), ResultTerminationRequested);
        R_UNLESS(!GetCurrentThread(m_kernel).IsTerminationRequested(), ResultTerminationRequested);

        // Ensure our thread state is correct.
        R_UNLESS(this->GetState() == ThreadState::Initialized, ResultInvalidState);

        // If the current thread has been asked to suspend, suspend it and retry.
        if (GetCurrentThread(m_kernel).IsSuspended()) {
            GetCurrentThread(m_kernel).UpdateState();
            continue;
        }

        // If we're not a kernel thread and we've been asked to suspend, suspend ourselves.
        if (KProcess* owner = this->GetOwnerProcess(); owner != nullptr) {
            if (this->IsUserThread() && this->IsSuspended()) {
                this->UpdateState();
            }
            owner->IncrementRunningThreadCount();
        }

        // Open a reference, now that we're running.
        this->Open();

        // Set our state and finish.
        this->SetState(ThreadState::Runnable);

        R_SUCCEED();
    }
}

void KThread::Exit() {
    ASSERT(this == GetCurrentThreadPointer(m_kernel));

    // Release the thread resource hint, running thread count from parent.
    if (m_parent != nullptr) {
        m_parent->GetResourceLimit()->Release(Kernel::LimitableResource::ThreadCountMax, 0, 1);
        m_resource_limit_release_hint = true;
        m_parent->DecrementRunningThreadCount();
    }

    // Perform termination.
    {
        KScopedSchedulerLock sl{m_kernel};

        // Disallow all suspension.
        m_suspend_allowed_flags = 0;
        this->UpdateState();

        // Disallow all suspension.
        m_suspend_allowed_flags = 0;

        // Start termination.
        this->StartTermination();

        // Register the thread as a work task.
        KWorkerTaskManager::AddTask(m_kernel, KWorkerTaskManager::WorkerType::Exit, this);
    }

    UNREACHABLE_MSG("KThread::Exit() would return");
}

Result KThread::Terminate() {
    ASSERT(this != GetCurrentThreadPointer(m_kernel));

    // Request the thread terminate if it hasn't already.
    if (const auto new_state = this->RequestTerminate(); new_state != ThreadState::Terminated) {
        // If the thread isn't terminated, wait for it to terminate.
        s32 index;
        KSynchronizationObject* objects[] = {this};
        R_TRY(KSynchronizationObject::Wait(m_kernel, std::addressof(index), objects, 1,
                                           Svc::WaitInfinite));
    }

    R_SUCCEED();
}

ThreadState KThread::RequestTerminate() {
    ASSERT(this != GetCurrentThreadPointer(m_kernel));

    KScopedSchedulerLock sl{m_kernel};

    // Determine if this is the first termination request.
    const bool first_request = [&]() -> bool {
        // Perform an atomic compare-and-swap from false to true.
        bool expected = false;
        return m_termination_requested.compare_exchange_strong(expected, true);
    }();

    // If this is the first request, start termination procedure.
    if (first_request) {
        // If the thread is in initialized state, just change state to terminated.
        if (this->GetState() == ThreadState::Initialized) {
            m_thread_state = ThreadState::Terminated;
            return ThreadState::Terminated;
        }

        // Register the terminating dpc.
        this->RegisterDpc(DpcFlag::Terminating);

        // If the thread is pinned, unpin it.
        if (this->GetStackParameters().is_pinned) {
            this->GetOwnerProcess()->UnpinThread(this);
        }

        // If the thread is suspended, continue it.
        if (this->IsSuspended()) {
            m_suspend_allowed_flags = 0;
            this->UpdateState();
        }

        // Change the thread's priority to be higher than any system thread's.
        this->IncreaseBasePriority(TerminatingThreadPriority);

        // If the thread is runnable, send a termination interrupt to cores it may be running on.
        if (this->GetState() == ThreadState::Runnable) {
            // NOTE: We do not mask the "current core", because this code may not actually be
            //       executing from the thread representing the "current core".
            if (const u64 core_mask = m_physical_affinity_mask.GetAffinityMask(); core_mask != 0) {
                Kernel::KInterruptManager::SendInterProcessorInterrupt(m_kernel, core_mask);
            }
        }

        // Wake up the thread.
        if (this->GetState() == ThreadState::Waiting) {
            m_wait_queue->CancelWait(this, ResultTerminationRequested, true);
        }
    }

    return this->GetState();
}

Result KThread::Sleep(s64 timeout) {
    ASSERT(!KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(this == GetCurrentThreadPointer(m_kernel));
    ASSERT(timeout > 0);

    ThreadQueueImplForKThreadSleep wait_queue(m_kernel);
    KHardwareTimer* timer{};
    {
        // Setup the scheduling lock and sleep.
        KScopedSchedulerLockAndSleep slp(m_kernel, std::addressof(timer), this, timeout);

        // Check if the thread should terminate.
        if (this->IsTerminationRequested()) {
            slp.CancelSleep();
            R_THROW(ResultTerminationRequested);
        }

        // Wait for the sleep to end.
        wait_queue.SetHardwareTimer(timer);
        this->BeginWait(std::addressof(wait_queue));
        this->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Sleep);
    }

    R_SUCCEED();
}

void KThread::RequestDummyThreadWait() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(this->IsDummyThread());

    // We will block when the scheduler lock is released.
    std::scoped_lock lock{m_dummy_thread_mutex};
    m_dummy_thread_runnable = false;
}

void KThread::DummyThreadBeginWait() {
    if (!this->IsDummyThread() || m_kernel.IsPhantomModeForSingleCore()) {
        // Occurs in single core mode.
        return;
    }

    // Block until runnable is no longer false.
    std::unique_lock lock{m_dummy_thread_mutex};
    m_dummy_thread_cv.wait(lock, [this] { return m_dummy_thread_runnable; });
}

void KThread::DummyThreadEndWait() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
    ASSERT(this->IsDummyThread());

    // Wake up the waiting thread.
    {
        std::scoped_lock lock{m_dummy_thread_mutex};
        m_dummy_thread_runnable = true;
    }
    m_dummy_thread_cv.notify_one();
}

void KThread::BeginWait(KThreadQueue* queue) {
    // Set our state as waiting.
    this->SetState(ThreadState::Waiting);

    // Set our wait queue.
    m_wait_queue = queue;
}

void KThread::NotifyAvailable(KSynchronizationObject* signaled_object, Result wait_result) {
    // Lock the scheduler.
    KScopedSchedulerLock sl(m_kernel);

    // If we're waiting, notify our queue that we're available.
    if (this->GetState() == ThreadState::Waiting) {
        m_wait_queue->NotifyAvailable(this, signaled_object, wait_result);
    }
}

void KThread::EndWait(Result wait_result) {
    // Lock the scheduler.
    KScopedSchedulerLock sl(m_kernel);

    // If we're waiting, notify our queue that we're available.
    if (this->GetState() == ThreadState::Waiting) {
        if (m_wait_queue == nullptr) {
            // This should never happen, but avoid a hard crash below to get this logged.
            ASSERT_MSG(false, "wait_queue is nullptr!");
            return;
        }

        m_wait_queue->EndWait(this, wait_result);
    }
}

void KThread::CancelWait(Result wait_result, bool cancel_timer_task) {
    // Lock the scheduler.
    KScopedSchedulerLock sl(m_kernel);

    // If we're waiting, notify our queue that we're available.
    if (this->GetState() == ThreadState::Waiting) {
        m_wait_queue->CancelWait(this, wait_result, cancel_timer_task);
    }
}

void KThread::SetState(ThreadState state) {
    KScopedSchedulerLock sl{m_kernel};

    // Clear debugging state
    this->SetWaitReasonForDebugging({});

    const ThreadState old_state = m_thread_state.load(std::memory_order_relaxed);
    m_thread_state.store(
        static_cast<ThreadState>((old_state & ~ThreadState::Mask) | (state & ThreadState::Mask)),
        std::memory_order_relaxed);
    if (m_thread_state.load(std::memory_order_relaxed) != old_state) {
        KScheduler::OnThreadStateChanged(m_kernel, this, old_state);
    }
}

std::shared_ptr<Common::Fiber>& KThread::GetHostContext() {
    return m_host_context;
}

void SetCurrentThread(KernelCore& kernel, KThread* thread) {
    kernel.SetCurrentEmuThread(thread);
}

KThread* GetCurrentThreadPointer(KernelCore& kernel) {
    return kernel.GetCurrentEmuThread();
}

KThread& GetCurrentThread(KernelCore& kernel) {
    return *GetCurrentThreadPointer(kernel);
}

KProcess* GetCurrentProcessPointer(KernelCore& kernel) {
    return GetCurrentThread(kernel).GetOwnerProcess();
}

KProcess& GetCurrentProcess(KernelCore& kernel) {
    return *GetCurrentProcessPointer(kernel);
}

s32 GetCurrentCoreId(KernelCore& kernel) {
    return GetCurrentThread(kernel).GetCurrentCore();
}

Core::Memory::Memory& GetCurrentMemory(KernelCore& kernel) {
    return GetCurrentProcess(kernel).GetMemory();
}

KScopedDisableDispatch::~KScopedDisableDispatch() {
    // If we are shutting down the kernel, none of this is relevant anymore.
    if (m_kernel.IsShuttingDown()) {
        return;
    }

    if (GetCurrentThread(m_kernel).GetDisableDispatchCount() <= 1) {
        auto* scheduler = m_kernel.CurrentScheduler();

        if (scheduler && !m_kernel.IsPhantomModeForSingleCore()) {
            scheduler->RescheduleCurrentCore();
        } else {
            KScheduler::RescheduleCurrentHLEThread(m_kernel);
        }
    } else {
        GetCurrentThread(m_kernel).EnableDispatch();
    }
}

} // namespace Kernel


code:
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <span>
#include <string>
#include <utility>
#include <vector>

#include "common/intrusive_list.h"

#include "common/intrusive_red_black_tree.h"
#include "common/scratch_buffer.h"
#include "common/spin_lock.h"
#include "core/arm/arm_interface.h"
#include "core/hle/kernel/k_affinity_mask.h"
#include "core/hle/kernel/k_light_lock.h"
#include "core/hle/kernel/k_spin_lock.h"
#include "core/hle/kernel/k_synchronization_object.h"
#include "core/hle/kernel/k_timer_task.h"
#include "core/hle/kernel/k_typed_address.h"
#include "core/hle/kernel/k_worker_task.h"
#include "core/hle/kernel/slab_helpers.h"
#include "core/hle/kernel/svc_common.h"
#include "core/hle/kernel/svc_types.h"
#include "core/hle/result.h"

namespace Common {
class Fiber;
}

namespace Core {
namespace Memory {
class Memory;
}
class System;
} // namespace Core

namespace Kernel {

class GlobalSchedulerContext;
class KernelCore;
class KProcess;
class KScheduler;
class KThreadQueue;

using KThreadFunction = KProcessAddress;

enum class ThreadType : u32 {
    Main = 0,
    Kernel = 1,
    HighPriority = 2,
    User = 3,
    Dummy = 100, // Special thread type for emulation purposes only
};
DECLARE_ENUM_FLAG_OPERATORS(ThreadType);

enum class SuspendType : u32 {
    Process = 0,
    Thread = 1,
    Debug = 2,
    Backtrace = 3,
    Init = 4,
    System = 5,

    Count,
};

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)),
    SystemSuspended = (1 << (5 + SuspendShift)),

    SuspendFlagMask = ((1 << 6) - 1) << SuspendShift,
};
DECLARE_ENUM_FLAG_OPERATORS(ThreadState);

enum class DpcFlag : u32 {
    Terminating = (1 << 0),
    Terminated = (1 << 1),
};

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
};

enum class StepState : u32 {
    NotStepping,   ///< Thread is not currently stepping
    StepPending,   ///< Thread will step when next scheduled
    StepPerformed, ///< Thread has stepped, waiting to be scheduled again
};

void SetCurrentThread(KernelCore& kernel, KThread* thread);
KThread* GetCurrentThreadPointer(KernelCore& kernel);
KThread& GetCurrentThread(KernelCore& kernel);
KProcess* GetCurrentProcessPointer(KernelCore& kernel);
KProcess& GetCurrentProcess(KernelCore& kernel);
s32 GetCurrentCoreId(KernelCore& kernel);
Core::Memory::Memory& GetCurrentMemory(KernelCore& kernel);

class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KWorkerTask>,
                      public Common::IntrusiveListBaseNode<KThread>,
                      public KTimerTask {
    KERNEL_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject);

private:
    friend class KScheduler;
    friend class KProcess;

public:
    static constexpr s32 DefaultThreadPriority = 44;
    static constexpr s32 IdleThreadPriority = Svc::LowestThreadPriority + 1;
    static constexpr s32 DummyThreadPriority = Svc::LowestThreadPriority + 2;

    explicit KThread(KernelCore& kernel);
    ~KThread() override;

public:
    using WaiterList = Common::IntrusiveListBaseTraits<KThread>::ListType;

    /**
     * Gets the thread's current priority
     * @return The current thread's priority
     */
    s32 GetPriority() const {
        return m_priority;
    }

    /**
     * Sets the thread's current priority.
     * @param priority The new priority.
     */
    void SetPriority(s32 value) {
        m_priority = value;
    }

    /**
     * Gets the thread's nominal priority.
     * @return The current thread's nominal priority.
     */
    s32 GetBasePriority() const {
        return m_base_priority;
    }

    /**
     * Gets the thread's thread ID
     * @return The thread's ID
     */
    u64 GetThreadId() const {
        return m_thread_id;
    }

    void ContinueIfHasKernelWaiters() {
        if (GetNumKernelWaiters() > 0) {
            Continue();
        }
    }

    void SetBasePriority(s32 value);

    Result Run();

    void Exit();

    Result Terminate();

    ThreadState RequestTerminate();

    u32 GetSuspendFlags() const {
        return m_suspend_allowed_flags & m_suspend_request_flags;
    }

    bool IsSuspended() const {
        return GetSuspendFlags() != 0;
    }

    bool IsSuspendRequested(SuspendType type) const {
        return (m_suspend_request_flags &
                (1U << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)))) !=
               0;
    }

    bool IsSuspendRequested() const {
        return m_suspend_request_flags != 0;
    }

    void RequestSuspend(SuspendType type);

    void Resume(SuspendType type);

    void TrySuspend();

    void UpdateState();

    void Continue();

    constexpr void SetSyncedIndex(s32 index) {
        m_synced_index = index;
    }

    constexpr s32 GetSyncedIndex() const {
        return m_synced_index;
    }

    constexpr void SetWaitResult(Result wait_res) {
        m_wait_result = wait_res;
    }

    constexpr Result GetWaitResult() const {
        return m_wait_result;
    }

    /*
     * Returns the Thread Local Storage address of the current thread
     * @returns Address of the thread's TLS
     */
    KProcessAddress GetTlsAddress() const {
        return m_tls_address;
    }

    /*
     * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
     * @returns The value of the TPIDR_EL0 register.
     */
    u64 GetTpidrEl0() const {
        return m_thread_context.tpidr;
    }

    /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
    void SetTpidrEl0(u64 value) {
        m_thread_context.tpidr = value;
    }

    void CloneFpuStatus();

    Svc::ThreadContext& GetContext() {
        return m_thread_context;
    }

    const Svc::ThreadContext& GetContext() const {
        return m_thread_context;
    }

    std::shared_ptr<Common::Fiber>& GetHostContext();

    ThreadState GetState() const {
        return m_thread_state.load(std::memory_order_relaxed) & ThreadState::Mask;
    }

    ThreadState GetRawState() const {
        return m_thread_state.load(std::memory_order_relaxed);
    }

    void SetState(ThreadState state);

    StepState GetStepState() const {
        return m_step_state;
    }

    void SetStepState(StepState state) {
        m_step_state = state;
    }

    s64 GetLastScheduledTick() const {
        return m_last_scheduled_tick;
    }

    void SetLastScheduledTick(s64 tick) {
        m_last_scheduled_tick = tick;
    }

    void AddCpuTime(s32 core_id, s64 amount) {
        m_cpu_time += amount;
        // TODO(bunnei): Debug kernels track per-core tick counts. Should we?
    }

    s64 GetCpuTime() const {
        return m_cpu_time;
    }

    s32 GetActiveCore() const {
        return m_core_id;
    }

    void SetActiveCore(s32 core) {
        m_core_id = core;
    }

    s32 GetCurrentCore() const {
        return m_current_core_id;
    }

    void SetCurrentCore(s32 core) {
        m_current_core_id = core;
    }

    KProcess* GetOwnerProcess() const {
        return m_parent;
    }

    bool IsUserThread() const {
        return m_parent != nullptr;
    }

    std::span<KSynchronizationObject*> GetSynchronizationObjectBuffer() {
        return m_sync_object_buffer.sync_objects;
    }

    std::span<Handle> GetHandleBuffer() {
        return {m_sync_object_buffer.handles.data() + Svc::ArgumentHandleCountMax,
                Svc::ArgumentHandleCountMax};
    }

    u16 GetUserDisableCount() const;
    void SetInterruptFlag();
    void ClearInterruptFlag();

    KThread* GetLockOwner() const;

    const KAffinityMask& GetAffinityMask() const {
        return m_physical_affinity_mask;
    }

    Result GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask);

    Result GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask);

    Result SetCoreMask(s32 cpu_core_id, u64 v_affinity_mask);

    Result SetActivity(Svc::ThreadActivity activity);

    Result Sleep(s64 timeout);

    s64 GetYieldScheduleCount() const {
        return m_schedule_count;
    }

    void SetYieldScheduleCount(s64 count) {
        m_schedule_count = count;
    }

    void WaitCancel();

    bool IsWaitCancelled() const {
        return m_wait_cancelled;
    }

    void ClearWaitCancelled() {
        m_wait_cancelled = false;
    }

    bool IsCancellable() const {
        return m_cancellable;
    }

    void SetCancellable() {
        m_cancellable = true;
    }

    void ClearCancellable() {
        m_cancellable = false;
    }

    u32* GetLightSessionData() const {
        return m_light_ipc_data;
    }
    void SetLightSessionData(u32* data) {
        m_light_ipc_data = data;
    }

    bool IsTerminationRequested() const {
        return m_termination_requested || GetRawState() == ThreadState::Terminated;
    }

    u64 GetId() const override {
        return this->GetThreadId();
    }

    bool IsInitialized() const override {
        return m_initialized;
    }

    uintptr_t GetPostDestroyArgument() const override {
        return reinterpret_cast<uintptr_t>(m_parent) | (m_resource_limit_release_hint ? 1 : 0);
    }

    void Finalize() override;

    bool IsSignaled() const override;

    void OnTimer();

    void DoWorkerTaskImpl();

    static void PostDestroy(uintptr_t arg);

    static Result InitializeDummyThread(KThread* thread, KProcess* owner);

    static Result InitializeMainThread(Core::System& system, KThread* thread, s32 virt_core);

    static Result InitializeIdleThread(Core::System& system, KThread* thread, s32 virt_core);

    static Result InitializeHighPriorityThread(Core::System& system, KThread* thread,
                                               KThreadFunction func, uintptr_t arg, s32 virt_core);

    static Result InitializeUserThread(Core::System& system, KThread* thread, KThreadFunction func,
                                       uintptr_t arg, KProcessAddress user_stack_top, s32 prio,
                                       s32 virt_core, KProcess* owner);

    static Result InitializeServiceThread(Core::System& system, KThread* thread,
                                          std::function<void()>&& thread_func, s32 prio,
                                          s32 virt_core, KProcess* owner);

public:
    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;
    };

    StackParameters& GetStackParameters() {
        return m_stack_parameters;
    }

    const StackParameters& GetStackParameters() const {
        return m_stack_parameters;
    }

    class QueueEntry {
    public:
        constexpr QueueEntry() = default;

        constexpr void Initialize() {
            m_prev = nullptr;
            m_next = nullptr;
        }

        constexpr KThread* GetPrev() const {
            return m_prev;
        }
        constexpr KThread* GetNext() const {
            return m_next;
        }
        constexpr void SetPrev(KThread* thread) {
            m_prev = thread;
        }
        constexpr void SetNext(KThread* thread) {
            m_next = thread;
        }

    private:
        KThread* m_prev{};
        KThread* m_next{};
    };

    QueueEntry& GetPriorityQueueEntry(s32 core) {
        return m_per_core_priority_queue_entry[core];
    }

    const QueueEntry& GetPriorityQueueEntry(s32 core) const {
        return m_per_core_priority_queue_entry[core];
    }

    s32 GetDisableDispatchCount() const {
        return this->GetStackParameters().disable_count;
    }

    void DisableDispatch() {
        ASSERT(GetCurrentThread(m_kernel).GetDisableDispatchCount() >= 0);
        this->GetStackParameters().disable_count++;
    }

    void EnableDispatch() {
        ASSERT(GetCurrentThread(m_kernel).GetDisableDispatchCount() > 0);
        this->GetStackParameters().disable_count--;
    }

    void Pin(s32 current_core);

    void Unpin();

    void SetInExceptionHandler() {
        this->GetStackParameters().is_in_exception_handler = true;
    }

    void ClearInExceptionHandler() {
        this->GetStackParameters().is_in_exception_handler = false;
    }

    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;
    }

    bool IsCallingSvc() const {
        return this->GetStackParameters().is_calling_svc;
    }

    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);
    }

    u8 GetDpc() const {
        return this->GetStackParameters().dpc_flags;
    }

    bool HasDpc() const {
        return this->GetDpc() != 0;
    }

    void SetWaitReasonForDebugging(ThreadWaitReasonForDebugging reason) {
        m_wait_reason_for_debugging = reason;
    }

    ThreadWaitReasonForDebugging GetWaitReasonForDebugging() const {
        return m_wait_reason_for_debugging;
    }

    ThreadType GetThreadType() const {
        return m_thread_type;
    }

    bool IsDummyThread() const {
        return this->GetThreadType() == ThreadType::Dummy;
    }

    void AddWaiter(KThread* thread);

    void RemoveWaiter(KThread* thread);

    Result GetThreadContext3(Svc::ThreadContext* out);

    KThread* RemoveUserWaiterByKey(bool* out_has_waiters, KProcessAddress key) {
        return this->RemoveWaiterByKey(out_has_waiters, key, false);
    }

    KThread* RemoveKernelWaiterByKey(bool* out_has_waiters, KProcessAddress key) {
        return this->RemoveWaiterByKey(out_has_waiters, key, true);
    }

    KProcessAddress GetAddressKey() const {
        return m_address_key;
    }

    u32 GetAddressKeyValue() const {
        return m_address_key_value;
    }

    bool GetIsKernelAddressKey() const {
        return m_is_kernel_address_key;
    }

    //! NB: intentional deviation from official kernel.
    //
    // Separate SetAddressKey into user and kernel versions
    // to cope with arbitrary host pointers making their way
    // into things.

    void SetUserAddressKey(KProcessAddress key, u32 val) {
        ASSERT(m_waiting_lock_info == nullptr);
        m_address_key = key;
        m_address_key_value = val;
        m_is_kernel_address_key = false;
    }

    void SetKernelAddressKey(KProcessAddress key) {
        ASSERT(m_waiting_lock_info == nullptr);
        m_address_key = key;
        m_is_kernel_address_key = true;
    }

    void ClearWaitQueue() {
        m_wait_queue = nullptr;
    }

    void BeginWait(KThreadQueue* queue);
    void NotifyAvailable(KSynchronizationObject* signaled_object, Result wait_result);
    void EndWait(Result wait_result);
    void CancelWait(Result wait_result, bool cancel_timer_task);

    s32 GetNumKernelWaiters() const {
        return m_num_kernel_waiters;
    }

    u64 GetConditionVariableKey() const {
        return m_condvar_key;
    }

    u64 GetAddressArbiterKey() const {
        return m_condvar_key;
    }

    // Dummy threads (used for HLE host threads) cannot wait based on the guest scheduler, and
    // therefore will not block on guest kernel synchronization primitives. These methods handle
    // blocking as needed.

    void RequestDummyThreadWait();
    void DummyThreadBeginWait();
    void DummyThreadEndWait();

    uintptr_t GetArgument() const {
        return m_argument;
    }

    KProcessAddress GetUserStackTop() const {
        return m_stack_top;
    }

public:
    // TODO: This shouldn't be defined in kernel namespace
    struct NativeExecutionParameters {
        u64 tpidr_el0{};
        u64 tpidrro_el0{};
        void* native_context{};
        std::atomic<u32> lock{1};
        bool is_running{};
        u32 magic{Common::MakeMagic('Y', 'U', 'Z', 'U')};
    };

    NativeExecutionParameters& GetNativeExecutionParameters() {
        return m_native_execution_parameters;
    }

private:
    KThread* RemoveWaiterByKey(bool* out_has_waiters, KProcessAddress key,
                               bool is_kernel_address_key);

    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 RedBlackKeyType {
            u64 cv_key{};
            s32 priority{};

            constexpr u64 GetConditionVariableKey() const {
                return cv_key;
            }

            constexpr s32 GetPriority() const {
                return priority;
            }
        };

        template <typename T>
            requires(std::same_as<T, KThread> || std::same_as<T, RedBlackKeyType>)
        static constexpr int Compare(const T& lhs, const KThread& rhs) {
            const u64 l_key = lhs.GetConditionVariableKey();
            const u64 r_key = rhs.GetConditionVariableKey();

            if (l_key < r_key) {
                // Sort first by key
                return -1;
            } else if (l_key == r_key && lhs.GetPriority() < rhs.GetPriority()) {
                // And then by priority.
                return -1;
            } else {
                return 1;
            }
        }
    };

    void AddWaiterImpl(KThread* thread);
    void RemoveWaiterImpl(KThread* thread);
    static void RestorePriority(KernelCore& kernel, KThread* thread);

    void StartTermination();
    void FinishTermination();

    void IncreaseBasePriority(s32 priority);

    Result Initialize(KThreadFunction func, uintptr_t arg, KProcessAddress user_stack_top, s32 prio,
                      s32 virt_core, KProcess* owner, ThreadType type);

    static Result InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg,
                                   KProcessAddress user_stack_top, s32 prio, s32 core,
                                   KProcess* owner, ThreadType type,
                                   std::function<void()>&& init_func);

    // For core KThread implementation
    Svc::ThreadContext m_thread_context{};
    Common::IntrusiveListNode m_process_list_node;
    Common::IntrusiveRedBlackTreeNode m_condvar_arbiter_tree_node{};
    s32 m_priority{};
    using ConditionVariableThreadTreeTraits =
        Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
            &KThread::m_condvar_arbiter_tree_node>;
    using ConditionVariableThreadTree =
        ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;

private:
    struct LockWithPriorityInheritanceComparator {
        struct RedBlackKeyType {
            s32 m_priority;

            constexpr s32 GetPriority() const {
                return m_priority;
            }
        };

        template <typename T>
            requires(std::same_as<T, KThread> || std::same_as<T, RedBlackKeyType>)
        static constexpr int Compare(const T& lhs, const KThread& rhs) {
            if (lhs.GetPriority() < rhs.GetPriority()) {
                // Sort by priority.
                return -1;
            } else {
                return 1;
            }
        }
    };
    static_assert(std::same_as<Common::RedBlackKeyType<LockWithPriorityInheritanceComparator, void>,
                               LockWithPriorityInheritanceComparator::RedBlackKeyType>);

    using LockWithPriorityInheritanceThreadTreeTraits =
        Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
            &KThread::m_condvar_arbiter_tree_node>;
    using LockWithPriorityInheritanceThreadTree =
        ConditionVariableThreadTreeTraits::TreeType<LockWithPriorityInheritanceComparator>;

public:
    class LockWithPriorityInheritanceInfo
        : public KSlabAllocated<LockWithPriorityInheritanceInfo>,
          public Common::IntrusiveListBaseNode<LockWithPriorityInheritanceInfo> {
    public:
        explicit LockWithPriorityInheritanceInfo(KernelCore&) {}

        static LockWithPriorityInheritanceInfo* Create(KernelCore& kernel,
                                                       KProcessAddress address_key,
                                                       bool is_kernel_address_key) {
            // Create a new lock info.
            auto* new_lock = LockWithPriorityInheritanceInfo::Allocate(kernel);
            ASSERT(new_lock != nullptr);

            // Set the new lock's address key.
            new_lock->m_address_key = address_key;
            new_lock->m_is_kernel_address_key = is_kernel_address_key;

            return new_lock;
        }

        void SetOwner(KThread* new_owner) {
            // Set new owner.
            m_owner = new_owner;
        }

        void AddWaiter(KThread* waiter) {
            // Insert the waiter.
            m_tree.insert(*waiter);
            m_waiter_count++;

            waiter->SetWaitingLockInfo(this);
        }

        bool RemoveWaiter(KThread* waiter) {
            m_tree.erase(m_tree.iterator_to(*waiter));

            waiter->SetWaitingLockInfo(nullptr);

            return (--m_waiter_count) == 0;
        }

        KThread* GetHighestPriorityWaiter() {
            return std::addressof(m_tree.front());
        }
        const KThread* GetHighestPriorityWaiter() const {
            return std::addressof(m_tree.front());
        }

        LockWithPriorityInheritanceThreadTree& GetThreadTree() {
            return m_tree;
        }
        const LockWithPriorityInheritanceThreadTree& GetThreadTree() const {
            return m_tree;
        }

        KProcessAddress GetAddressKey() const {
            return m_address_key;
        }
        bool GetIsKernelAddressKey() const {
            return m_is_kernel_address_key;
        }
        KThread* GetOwner() const {
            return m_owner;
        }
        u32 GetWaiterCount() const {
            return m_waiter_count;
        }

    private:
        LockWithPriorityInheritanceThreadTree m_tree{};
        KProcessAddress m_address_key{};
        KThread* m_owner{};
        u32 m_waiter_count{};
        bool m_is_kernel_address_key{};
    };

    void SetWaitingLockInfo(LockWithPriorityInheritanceInfo* lock) {
        m_waiting_lock_info = lock;
    }

    LockWithPriorityInheritanceInfo* GetWaitingLockInfo() {
        return m_waiting_lock_info;
    }

    void AddHeldLock(LockWithPriorityInheritanceInfo* lock_info);
    LockWithPriorityInheritanceInfo* FindHeldLock(KProcessAddress address_key,
                                                  bool is_kernel_address_key);

private:
    using LockWithPriorityInheritanceInfoList =
        Common::IntrusiveListBaseTraits<LockWithPriorityInheritanceInfo>::ListType;

    ConditionVariableThreadTree* m_condvar_tree{};
    u64 m_condvar_key{};
    u64 m_virtual_affinity_mask{};
    KAffinityMask m_physical_affinity_mask{};
    u64 m_thread_id{};
    std::atomic<s64> m_cpu_time{};
    KProcessAddress m_address_key{};
    KProcess* m_parent{};
    KVirtualAddress m_kernel_stack_top{};
    u32* m_light_ipc_data{};
    KProcessAddress m_tls_address{};
    KLightLock m_activity_pause_lock;
    SyncObjectBuffer m_sync_object_buffer{};
    s64 m_schedule_count{};
    s64 m_last_scheduled_tick{};
    std::array<QueueEntry, Core::Hardware::NUM_CPU_CORES> m_per_core_priority_queue_entry{};
    KThreadQueue* m_wait_queue{};
    LockWithPriorityInheritanceInfoList m_held_lock_info_list{};
    LockWithPriorityInheritanceInfo* m_waiting_lock_info{};
    WaiterList m_pinned_waiter_list{};
    u32 m_address_key_value{};
    u32 m_suspend_request_flags{};
    u32 m_suspend_allowed_flags{};
    s32 m_synced_index{};
    Result m_wait_result{ResultSuccess};
    s32 m_base_priority{};
    s32 m_physical_ideal_core_id{};
    s32 m_virtual_ideal_core_id{};
    s32 m_num_kernel_waiters{};
    s32 m_current_core_id{};
    s32 m_core_id{};
    KAffinityMask m_original_physical_affinity_mask{};
    s32 m_original_physical_ideal_core_id{};
    s32 m_num_core_migration_disables{};
    std::atomic<ThreadState> m_thread_state{};
    std::atomic<bool> m_termination_requested{};
    bool m_wait_cancelled{};
    bool m_cancellable{};
    bool m_signaled{};
    bool m_initialized{};
    bool m_debug_attached{};
    s8 m_priority_inheritance_count{};
    bool m_resource_limit_release_hint{};
    bool m_is_kernel_address_key{};
    StackParameters m_stack_parameters{};
    Common::SpinLock m_context_guard{};

    // For emulation
    std::shared_ptr<Common::Fiber> m_host_context{};
    ThreadType m_thread_type{};
    StepState m_step_state{};
    bool m_dummy_thread_runnable{true};
    std::mutex m_dummy_thread_mutex{};
    std::condition_variable m_dummy_thread_cv{};

    // For debugging
    std::vector<KSynchronizationObject*> m_wait_objects_for_debugging{};
    KProcessAddress m_mutex_wait_address_for_debugging{};
    ThreadWaitReasonForDebugging m_wait_reason_for_debugging{};
    uintptr_t m_argument{};
    KProcessAddress m_stack_top{};
    NativeExecutionParameters m_native_execution_parameters{};

public:
    using ConditionVariableThreadTreeType = ConditionVariableThreadTree;

    void SetConditionVariable(ConditionVariableThreadTree* tree, KProcessAddress address,
                              u64 cv_key, u32 value) {
        ASSERT(m_waiting_lock_info == nullptr);
        m_condvar_tree = tree;
        m_condvar_key = cv_key;
        m_address_key = address;
        m_address_key_value = value;
        m_is_kernel_address_key = false;
    }

    void ClearConditionVariable() {
        m_condvar_tree = nullptr;
    }

    bool IsWaitingForConditionVariable() const {
        return m_condvar_tree != nullptr;
    }

    void SetAddressArbiter(ConditionVariableThreadTree* tree, u64 address) {
        ASSERT(m_waiting_lock_info == nullptr);
        m_condvar_tree = tree;
        m_condvar_key = address;
    }

    void ClearAddressArbiter() {
        m_condvar_tree = nullptr;
    }

    bool IsWaitingForAddressArbiter() const {
        return m_condvar_tree != nullptr;
    }

    ConditionVariableThreadTree* GetConditionVariableTree() const {
        return m_condvar_tree;
    }
};

class KScopedDisableDispatch {
public:
    explicit KScopedDisableDispatch(KernelCore& kernel) : m_kernel{kernel} {
        // If we are shutting down the kernel, none of this is relevant anymore.
        if (m_kernel.IsShuttingDown()) {
            return;
        }
        GetCurrentThread(kernel).DisableDispatch();
    }

    ~KScopedDisableDispatch();

private:
    KernelCore& m_kernel;
};

inline void KTimerTask::OnTimer() {
    static_cast<KThread*>(this)->OnTimer();
}

} // namespace Kernel


code:
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/scope_exit.h"
#include "core/core.h"

#include "core/hle/kernel/k_memory_block.h"
#include "core/hle/kernel/k_page_buffer.h"
#include "core/hle/kernel/k_page_table.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_thread_local_page.h"
#include "core/hle/kernel/kernel.h"

namespace Kernel {

Result KThreadLocalPage::Initialize(KernelCore& kernel, KProcess* process) {
    // Set that this process owns us.
    m_owner = process;
    m_kernel = std::addressof(kernel);

    // Allocate a new page.
    KPageBuffer* page_buf = KPageBuffer::Allocate(kernel);
    R_UNLESS(page_buf != nullptr, ResultOutOfMemory);
    auto page_buf_guard = SCOPE_GUARD {
        KPageBuffer::Free(kernel, page_buf);
    };

    // Map the address in.
    const auto phys_addr = kernel.System().DeviceMemory().GetPhysicalAddr(page_buf);
    R_TRY(m_owner->GetPageTable().MapPages(std::addressof(m_virt_addr), 1, PageSize, phys_addr,
                                           KMemoryState::ThreadLocal,
                                           KMemoryPermission::UserReadWrite));

    // We succeeded.
    page_buf_guard.Cancel();

    return ResultSuccess;
}

Result KThreadLocalPage::Finalize() {
    // Get the physical address of the page.
    KPhysicalAddress phys_addr{};
    ASSERT(m_owner->GetPageTable().GetPhysicalAddress(std::addressof(phys_addr), m_virt_addr));

    // Unmap the page.
    R_TRY(m_owner->GetPageTable().UnmapPages(this->GetAddress(), 1, KMemoryState::ThreadLocal));

    // Free the page.
    KPageBuffer::Free(*m_kernel, KPageBuffer::FromPhysicalAddress(m_kernel->System(), phys_addr));

    return ResultSuccess;
}

KProcessAddress KThreadLocalPage::Reserve() {
    for (size_t i = 0; i < m_is_region_free.size(); i++) {
        if (m_is_region_free[i]) {
            m_is_region_free[i] = false;
            return this->GetRegionAddress(i);
        }
    }

    return 0;
}

void KThreadLocalPage::Release(KProcessAddress addr) {
    m_is_region_free[this->GetRegionIndex(addr)] = true;
}

} // namespace Kernel