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

#include <random>
#include "common/scope_exit.h"
#include "common/settings.h"
#include "core/arm/dynarmic/arm_dynarmic.h"
#include "core/arm/dynarmic/dynarmic_exclusive_monitor.h"
#include "core/core.h"
#include "core/hle/kernel/k_process.h"
#include "core/hle/kernel/k_scoped_resource_reservation.h"
#include "core/hle/kernel/k_shared_memory.h"
#include "core/hle/kernel/k_shared_memory_info.h"
#include "core/hle/kernel/k_thread_local_page.h"
#include "core/hle/kernel/k_thread_queue.h"
#include "core/hle/kernel/k_worker_task_manager.h"

#include "core/arm/dynarmic/arm_dynarmic_32.h"
#include "core/arm/dynarmic/arm_dynarmic_64.h"
#ifdef HAS_NCE
#include "core/arm/nce/arm_nce.h"
#endif

namespace Kernel {

namespace {

Result TerminateChildren(KernelCore& kernel, KProcess* process,
                         const KThread* thread_to_not_terminate) {
    // Request that all children threads terminate.
    {
        KScopedLightLock proc_lk(process->GetListLock());
        KScopedSchedulerLock sl(kernel);

        if (thread_to_not_terminate != nullptr &&
            process->GetPinnedThread(GetCurrentCoreId(kernel)) == thread_to_not_terminate) {
            // NOTE: Here Nintendo unpins the current thread instead of the thread_to_not_terminate.
            // This is valid because the only caller which uses non-nullptr as argument uses
            // GetCurrentThreadPointer(), but it's still notable because it seems incorrect at
            // first glance.
            process->UnpinCurrentThread();
        }

        auto& thread_list = process->GetThreadList();
        for (auto it = thread_list.begin(); it != thread_list.end(); ++it) {
            if (KThread* thread = std::addressof(*it); thread != thread_to_not_terminate) {
                if (thread->GetState() != ThreadState::Terminated) {
                    thread->RequestTerminate();
                }
            }
        }
    }

    // Wait for all children threads to terminate.
    while (true) {
        // Get the next child.
        KThread* cur_child = nullptr;
        {
            KScopedLightLock proc_lk(process->GetListLock());

            auto& thread_list = process->GetThreadList();
            for (auto it = thread_list.begin(); it != thread_list.end(); ++it) {
                if (KThread* thread = std::addressof(*it); thread != thread_to_not_terminate) {
                    if (thread->GetState() != ThreadState::Terminated) {
                        if (thread->Open()) {
                            cur_child = thread;
                            break;
                        }
                    }
                }
            }
        }

        // If we didn't find any non-terminated children, we're done.
        if (cur_child == nullptr) {
            break;
        }

        // Terminate and close the thread.
        SCOPE_EXIT {
            cur_child->Close();
        };

        if (const Result terminate_result = cur_child->Terminate();
            ResultTerminationRequested == terminate_result) {
            R_THROW(terminate_result);
        }
    }

    R_SUCCEED();
}

class ThreadQueueImplForKProcessEnterUserException final : public KThreadQueue {
private:
    KThread** m_exception_thread;

public:
    explicit ThreadQueueImplForKProcessEnterUserException(KernelCore& kernel, KThread** t)
        : KThreadQueue(kernel), m_exception_thread(t) {}

    virtual void EndWait(KThread* waiting_thread, Result wait_result) override {
        // Set the exception thread.
        *m_exception_thread = waiting_thread;

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

    virtual void CancelWait(KThread* waiting_thread, Result wait_result,
                            bool cancel_timer_task) override {
        // Remove the thread as a waiter on its mutex owner.
        waiting_thread->GetLockOwner()->RemoveWaiter(waiting_thread);

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

void GenerateRandom(std::span<u64> out_random) {
    std::mt19937 rng(Settings::values.rng_seed_enabled ? Settings::values.rng_seed.GetValue()
                                                       : static_cast<u32>(std::time(nullptr)));
    std::uniform_int_distribution<u64> distribution;
    std::generate(out_random.begin(), out_random.end(), [&] { return distribution(rng); });
}

} // namespace

void KProcess::Finalize() {
    // Delete the process local region.
    this->DeleteThreadLocalRegion(m_plr_address);

    // Get the used memory size.
    const size_t used_memory_size = this->GetUsedNonSystemUserPhysicalMemorySize();

    // Finalize the page table.
    m_page_table.Finalize();

    // Finish using our system resource.
    if (m_system_resource) {
        if (m_system_resource->IsSecureResource()) {
            // Finalize optimized memory. If memory wasn't optimized, this is a no-op.
            m_kernel.MemoryManager().FinalizeOptimizedMemory(this->GetId(), m_memory_pool);
        }

        m_system_resource->Close();
        m_system_resource = nullptr;
    }

    // Free all shared memory infos.
    {
        auto it = m_shared_memory_list.begin();
        while (it != m_shared_memory_list.end()) {
            KSharedMemoryInfo* info = std::addressof(*it);
            KSharedMemory* shmem = info->GetSharedMemory();

            while (!info->Close()) {
                shmem->Close();
            }
            shmem->Close();

            it = m_shared_memory_list.erase(it);
            KSharedMemoryInfo::Free(m_kernel, info);
        }
    }

    // Our thread local page list must be empty at this point.
    ASSERT(m_partially_used_tlp_tree.empty());
    ASSERT(m_fully_used_tlp_tree.empty());

    // Release memory to the resource limit.
    if (m_resource_limit != nullptr) {
        ASSERT(used_memory_size >= m_memory_release_hint);
        m_resource_limit->Release(Svc::LimitableResource::PhysicalMemoryMax, used_memory_size,
                                  used_memory_size - m_memory_release_hint);
        m_resource_limit->Close();
    }

    // Clear expensive resources, as the destructor is not called for guest objects.
    for (auto& interface : m_arm_interfaces) {
        interface.reset();
    }
    m_exclusive_monitor.reset();

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

Result KProcess::Initialize(const Svc::CreateProcessParameter& params, KResourceLimit* res_limit,
                            bool is_real) {
    // TODO: remove this special case
    if (is_real) {
        // Create and clear the process local region.
        R_TRY(this->CreateThreadLocalRegion(std::addressof(m_plr_address)));
        this->GetMemory().ZeroBlock(m_plr_address, Svc::ThreadLocalRegionSize);
    }

    // Copy in the name from parameters.
    static_assert(sizeof(params.name) < sizeof(m_name));
    std::memcpy(m_name.data(), params.name.data(), sizeof(params.name));
    m_name[sizeof(params.name)] = 0;

    // Set misc fields.
    m_state = State::Created;
    m_main_thread_stack_size = 0;
    m_used_kernel_memory_size = 0;
    m_ideal_core_id = 0;
    m_flags = params.flags;
    m_version = params.version;
    m_program_id = params.program_id;
    m_code_address = params.code_address;
    m_code_size = params.code_num_pages * PageSize;
    m_is_application = True(params.flags & Svc::CreateProcessFlag::IsApplication);

    // Set thread fields.
    for (size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
        m_running_threads[i] = nullptr;
        m_pinned_threads[i] = nullptr;
        m_running_thread_idle_counts[i] = 0;
        m_running_thread_switch_counts[i] = 0;
    }

    // Set max memory based on address space type.
    switch ((params.flags & Svc::CreateProcessFlag::AddressSpaceMask)) {
    case Svc::CreateProcessFlag::AddressSpace32Bit:
    case Svc::CreateProcessFlag::AddressSpace64BitDeprecated:
    case Svc::CreateProcessFlag::AddressSpace64Bit:
        m_max_process_memory = m_page_table.GetHeapRegionSize();
        break;
    case Svc::CreateProcessFlag::AddressSpace32BitWithoutAlias:
        m_max_process_memory = m_page_table.GetHeapRegionSize() + m_page_table.GetAliasRegionSize();
        break;
    default:
        UNREACHABLE();
    }

    // Generate random entropy.
    GenerateRandom(m_entropy);

    // Clear remaining fields.
    m_num_running_threads = 0;
    m_num_process_switches = 0;
    m_num_thread_switches = 0;
    m_num_fpu_switches = 0;
    m_num_supervisor_calls = 0;
    m_num_ipc_messages = 0;

    m_is_signaled = false;
    m_exception_thread = nullptr;
    m_is_suspended = false;
    m_memory_release_hint = 0;
    m_schedule_count = 0;
    m_is_handle_table_initialized = false;

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

    // We're initialized!
    m_is_initialized = true;

    R_SUCCEED();
}

Result KProcess::Initialize(const Svc::CreateProcessParameter& params, const KPageGroup& pg,
                            std::span<const u32> caps, KResourceLimit* res_limit,
                            KMemoryManager::Pool pool, bool immortal) {
    ASSERT(res_limit != nullptr);
    ASSERT((params.code_num_pages * PageSize) / PageSize ==
           static_cast<size_t>(params.code_num_pages));

    // Set members.
    m_memory_pool = pool;
    m_is_default_application_system_resource = false;
    m_is_immortal = immortal;

    // Setup our system resource.
    if (const size_t system_resource_num_pages = params.system_resource_num_pages;
        system_resource_num_pages != 0) {
        // Create a secure system resource.
        KSecureSystemResource* secure_resource = KSecureSystemResource::Create(m_kernel);
        R_UNLESS(secure_resource != nullptr, ResultOutOfResource);

        ON_RESULT_FAILURE {
            secure_resource->Close();
        };

        // Initialize the secure resource.
        R_TRY(secure_resource->Initialize(system_resource_num_pages * PageSize, res_limit,
                                          m_memory_pool));

        // Set our system resource.
        m_system_resource = secure_resource;
    } else {
        // Use the system-wide system resource.
        const bool is_app = True(params.flags & Svc::CreateProcessFlag::IsApplication);
        m_system_resource = std::addressof(is_app ? m_kernel.GetAppSystemResource()
                                                  : m_kernel.GetSystemSystemResource());

        m_is_default_application_system_resource = is_app;

        // Open reference to the system resource.
        m_system_resource->Open();
    }

    // Ensure we clean up our secure resource, if we fail.
    ON_RESULT_FAILURE {
        m_system_resource->Close();
        m_system_resource = nullptr;
    };

    // Setup page table.
    {
        const auto as_type = params.flags & Svc::CreateProcessFlag::AddressSpaceMask;
        const bool enable_aslr = True(params.flags & Svc::CreateProcessFlag::EnableAslr);
        const bool enable_das_merge =
            False(params.flags & Svc::CreateProcessFlag::DisableDeviceAddressSpaceMerge);
        R_TRY(m_page_table.Initialize(as_type, enable_aslr, enable_das_merge, !enable_aslr, pool,
                                      params.code_address, params.code_num_pages * PageSize,
                                      m_system_resource, res_limit, m_memory, 0));
    }
    ON_RESULT_FAILURE_2 {
        m_page_table.Finalize();
    };

    // Ensure our memory is initialized.
    m_memory.SetCurrentPageTable(*this);
    m_memory.SetGPUDirtyManagers(m_kernel.System().GetGPUDirtyMemoryManager());

    // Ensure we can insert the code region.
    R_UNLESS(m_page_table.CanContain(params.code_address, params.code_num_pages * PageSize,
                                     KMemoryState::Code),
             ResultInvalidMemoryRegion);

    // Map the code region.
    R_TRY(m_page_table.MapPageGroup(params.code_address, pg, KMemoryState::Code,
                                    KMemoryPermission::KernelRead));

    // Initialize capabilities.
    R_TRY(m_capabilities.InitializeForKip(caps, std::addressof(m_page_table)));

    // Initialize the process id.
    m_process_id = m_kernel.CreateNewUserProcessID();
    ASSERT(InitialProcessIdMin <= m_process_id);
    ASSERT(m_process_id <= InitialProcessIdMax);

    // Initialize the rest of the process.
    R_TRY(this->Initialize(params, res_limit, true));

    // We succeeded!
    R_SUCCEED();
}

Result KProcess::Initialize(const Svc::CreateProcessParameter& params,
                            std::span<const u32> user_caps, KResourceLimit* res_limit,
                            KMemoryManager::Pool pool, KProcessAddress aslr_space_start) {
    ASSERT(res_limit != nullptr);

    // Set members.
    m_memory_pool = pool;
    m_is_default_application_system_resource = false;
    m_is_immortal = false;

    // Get the memory sizes.
    const size_t code_num_pages = params.code_num_pages;
    const size_t system_resource_num_pages = params.system_resource_num_pages;
    const size_t code_size = code_num_pages * PageSize;
    const size_t system_resource_size = system_resource_num_pages * PageSize;

    // Reserve memory for our code resource.
    KScopedResourceReservation memory_reservation(
        res_limit, Svc::LimitableResource::PhysicalMemoryMax, code_size);
    R_UNLESS(memory_reservation.Succeeded(), ResultLimitReached);

    // Setup our system resource.
    if (system_resource_num_pages != 0) {
        // Create a secure system resource.
        KSecureSystemResource* secure_resource = KSecureSystemResource::Create(m_kernel);
        R_UNLESS(secure_resource != nullptr, ResultOutOfResource);

        ON_RESULT_FAILURE {
            secure_resource->Close();
        };

        // Initialize the secure resource.
        R_TRY(secure_resource->Initialize(system_resource_size, res_limit, m_memory_pool));

        // Set our system resource.
        m_system_resource = secure_resource;

    } else {
        // Use the system-wide system resource.
        const bool is_app = True(params.flags & Svc::CreateProcessFlag::IsApplication);
        m_system_resource = std::addressof(is_app ? m_kernel.GetAppSystemResource()
                                                  : m_kernel.GetSystemSystemResource());

        m_is_default_application_system_resource = is_app;

        // Open reference to the system resource.
        m_system_resource->Open();
    }

    // Ensure we clean up our secure resource, if we fail.
    ON_RESULT_FAILURE {
        m_system_resource->Close();
        m_system_resource = nullptr;
    };

    // Setup page table.
    {
        const auto as_type = params.flags & Svc::CreateProcessFlag::AddressSpaceMask;
        const bool enable_aslr = True(params.flags & Svc::CreateProcessFlag::EnableAslr);
        const bool enable_das_merge =
            False(params.flags & Svc::CreateProcessFlag::DisableDeviceAddressSpaceMerge);
        R_TRY(m_page_table.Initialize(as_type, enable_aslr, enable_das_merge, !enable_aslr, pool,
                                      params.code_address, code_size, m_system_resource, res_limit,
                                      m_memory, aslr_space_start));
    }
    ON_RESULT_FAILURE_2 {
        m_page_table.Finalize();
    };

    // Ensure our memory is initialized.
    m_memory.SetCurrentPageTable(*this);
    m_memory.SetGPUDirtyManagers(m_kernel.System().GetGPUDirtyMemoryManager());

    // Ensure we can insert the code region.
    R_UNLESS(m_page_table.CanContain(params.code_address, code_size, KMemoryState::Code),
             ResultInvalidMemoryRegion);

    // Map the code region.
    R_TRY(m_page_table.MapPages(params.code_address, code_num_pages, KMemoryState::Code,
                                KMemoryPermission::KernelRead | KMemoryPermission::NotMapped));

    // Initialize capabilities.
    R_TRY(m_capabilities.InitializeForUser(user_caps, std::addressof(m_page_table)));

    // Initialize the process id.
    m_process_id = m_kernel.CreateNewUserProcessID();
    ASSERT(ProcessIdMin <= m_process_id);
    ASSERT(m_process_id <= ProcessIdMax);

    // If we should optimize memory allocations, do so.
    if (m_system_resource->IsSecureResource() &&
        True(params.flags & Svc::CreateProcessFlag::OptimizeMemoryAllocation)) {
        R_TRY(m_kernel.MemoryManager().InitializeOptimizedMemory(m_process_id, pool));
    }

    // Initialize the rest of the process.
    R_TRY(this->Initialize(params, res_limit, true));

    // We succeeded, so commit our memory reservation.
    memory_reservation.Commit();
    R_SUCCEED();
}

void KProcess::DoWorkerTaskImpl() {
    // Terminate child threads.
    TerminateChildren(m_kernel, this, nullptr);

    // Finalize the handle table, if we're not immortal.
    if (!m_is_immortal && m_is_handle_table_initialized) {
        this->FinalizeHandleTable();
    }

    // Finish termination.
    this->FinishTermination();
}

Result KProcess::StartTermination() {
    // Finalize the handle table when we're done, if the process isn't immortal.
    SCOPE_EXIT {
        if (!m_is_immortal) {
            this->FinalizeHandleTable();
        }
    };

    // Terminate child threads other than the current one.
    R_RETURN(TerminateChildren(m_kernel, this, GetCurrentThreadPointer(m_kernel)));
}

void KProcess::FinishTermination() {
    // Only allow termination to occur if the process isn't immortal.
    if (!m_is_immortal) {
        // Release resource limit hint.
        if (m_resource_limit != nullptr) {
            m_memory_release_hint = this->GetUsedNonSystemUserPhysicalMemorySize();
            m_resource_limit->Release(Svc::LimitableResource::PhysicalMemoryMax, 0,
                                      m_memory_release_hint);
        }

        // Change state.
        {
            KScopedSchedulerLock sl(m_kernel);
            this->ChangeState(State::Terminated);
        }

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

void KProcess::Exit() {
    // Determine whether we need to start terminating
    bool needs_terminate = false;
    {
        KScopedLightLock lk(m_state_lock);
        KScopedSchedulerLock sl(m_kernel);

        ASSERT(m_state != State::Created);
        ASSERT(m_state != State::CreatedAttached);
        ASSERT(m_state != State::Crashed);
        ASSERT(m_state != State::Terminated);
        if (m_state == State::Running || m_state == State::RunningAttached ||
            m_state == State::DebugBreak) {
            this->ChangeState(State::Terminating);
            needs_terminate = true;
        }
    }

    // If we need to start termination, do so.
    if (needs_terminate) {
        this->StartTermination();

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

    // Exit the current thread.
    GetCurrentThread(m_kernel).Exit();
}

Result KProcess::Terminate() {
    // Determine whether we need to start terminating.
    bool needs_terminate = false;
    {
        KScopedLightLock lk(m_state_lock);

        // Check whether we're allowed to terminate.
        R_UNLESS(m_state != State::Created, ResultInvalidState);
        R_UNLESS(m_state != State::CreatedAttached, ResultInvalidState);

        KScopedSchedulerLock sl(m_kernel);

        if (m_state == State::Running || m_state == State::RunningAttached ||
            m_state == State::Crashed || m_state == State::DebugBreak) {
            this->ChangeState(State::Terminating);
            needs_terminate = true;
        }
    }

    // If we need to terminate, do so.
    if (needs_terminate) {
        // Start termination.
        if (R_SUCCEEDED(this->StartTermination())) {
            // Finish termination.
            this->FinishTermination();
        } else {
            // Register the process as a work task.
            m_kernel.WorkerTaskManager().AddTask(m_kernel, KWorkerTaskManager::WorkerType::Exit,
                                                 this);
        }
    }

    R_SUCCEED();
}

Result KProcess::AddSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size) {
    // Lock ourselves, to prevent concurrent access.
    KScopedLightLock lk(m_state_lock);

    // Try to find an existing info for the memory.
    KSharedMemoryInfo* info = nullptr;
    for (auto it = m_shared_memory_list.begin(); it != m_shared_memory_list.end(); ++it) {
        if (it->GetSharedMemory() == shmem) {
            info = std::addressof(*it);
            break;
        }
    }

    // If we didn't find an info, create one.
    if (info == nullptr) {
        // Allocate a new info.
        info = KSharedMemoryInfo::Allocate(m_kernel);
        R_UNLESS(info != nullptr, ResultOutOfResource);

        // Initialize the info and add it to our list.
        info->Initialize(shmem);
        m_shared_memory_list.push_back(*info);
    }

    // Open a reference to the shared memory and its info.
    shmem->Open();
    info->Open();

    R_SUCCEED();
}

void KProcess::RemoveSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size) {
    // Lock ourselves, to prevent concurrent access.
    KScopedLightLock lk(m_state_lock);

    // Find an existing info for the memory.
    KSharedMemoryInfo* info = nullptr;
    auto it = m_shared_memory_list.begin();
    for (; it != m_shared_memory_list.end(); ++it) {
        if (it->GetSharedMemory() == shmem) {
            info = std::addressof(*it);
            break;
        }
    }
    ASSERT(info != nullptr);

    // Close a reference to the info and its memory.
    if (info->Close()) {
        m_shared_memory_list.erase(it);
        KSharedMemoryInfo::Free(m_kernel, info);
    }

    shmem->Close();
}

Result KProcess::CreateThreadLocalRegion(KProcessAddress* out) {
    KThreadLocalPage* tlp = nullptr;
    KProcessAddress tlr = 0;

    // See if we can get a region from a partially used TLP.
    {
        KScopedSchedulerLock sl(m_kernel);

        if (auto it = m_partially_used_tlp_tree.begin(); it != m_partially_used_tlp_tree.end()) {
            tlr = it->Reserve();
            ASSERT(tlr != 0);

            if (it->IsAllUsed()) {
                tlp = std::addressof(*it);
                m_partially_used_tlp_tree.erase(it);
                m_fully_used_tlp_tree.insert(*tlp);
            }

            *out = tlr;
            R_SUCCEED();
        }
    }

    // Allocate a new page.
    tlp = KThreadLocalPage::Allocate(m_kernel);
    R_UNLESS(tlp != nullptr, ResultOutOfMemory);
    ON_RESULT_FAILURE {
        KThreadLocalPage::Free(m_kernel, tlp);
    };

    // Initialize the new page.
    R_TRY(tlp->Initialize(m_kernel, this));

    // Reserve a TLR.
    tlr = tlp->Reserve();
    ASSERT(tlr != 0);

    // Insert into our tree.
    {
        KScopedSchedulerLock sl(m_kernel);
        if (tlp->IsAllUsed()) {
            m_fully_used_tlp_tree.insert(*tlp);
        } else {
            m_partially_used_tlp_tree.insert(*tlp);
        }
    }

    // We succeeded!
    *out = tlr;
    R_SUCCEED();
}

Result KProcess::DeleteThreadLocalRegion(KProcessAddress addr) {
    KThreadLocalPage* page_to_free = nullptr;

    // Release the region.
    {
        KScopedSchedulerLock sl(m_kernel);

        // Try to find the page in the partially used list.
        auto it = m_partially_used_tlp_tree.find_key(Common::AlignDown(GetInteger(addr), PageSize));
        if (it == m_partially_used_tlp_tree.end()) {
            // If we don't find it, it has to be in the fully used list.
            it = m_fully_used_tlp_tree.find_key(Common::AlignDown(GetInteger(addr), PageSize));
            R_UNLESS(it != m_fully_used_tlp_tree.end(), ResultInvalidAddress);

            // Release the region.
            it->Release(addr);

            // Move the page out of the fully used list.
            KThreadLocalPage* tlp = std::addressof(*it);
            m_fully_used_tlp_tree.erase(it);
            if (tlp->IsAllFree()) {
                page_to_free = tlp;
            } else {
                m_partially_used_tlp_tree.insert(*tlp);
            }
        } else {
            // Release the region.
            it->Release(addr);

            // Handle the all-free case.
            KThreadLocalPage* tlp = std::addressof(*it);
            if (tlp->IsAllFree()) {
                m_partially_used_tlp_tree.erase(it);
                page_to_free = tlp;
            }
        }
    }

    // If we should free the page it was in, do so.
    if (page_to_free != nullptr) {
        page_to_free->Finalize();

        KThreadLocalPage::Free(m_kernel, page_to_free);
    }

    R_SUCCEED();
}

bool KProcess::ReserveResource(Svc::LimitableResource which, s64 value) {
    if (KResourceLimit* rl = this->GetResourceLimit(); rl != nullptr) {
        return rl->Reserve(which, value);
    } else {
        return true;
    }
}

bool KProcess::ReserveResource(Svc::LimitableResource which, s64 value, s64 timeout) {
    if (KResourceLimit* rl = this->GetResourceLimit(); rl != nullptr) {
        return rl->Reserve(which, value, timeout);
    } else {
        return true;
    }
}

void KProcess::ReleaseResource(Svc::LimitableResource which, s64 value) {
    if (KResourceLimit* rl = this->GetResourceLimit(); rl != nullptr) {
        rl->Release(which, value);
    }
}

void KProcess::ReleaseResource(Svc::LimitableResource which, s64 value, s64 hint) {
    if (KResourceLimit* rl = this->GetResourceLimit(); rl != nullptr) {
        rl->Release(which, value, hint);
    }
}

void KProcess::IncrementRunningThreadCount() {
    ASSERT(m_num_running_threads.load() >= 0);

    ++m_num_running_threads;
}

void KProcess::DecrementRunningThreadCount() {
    ASSERT(m_num_running_threads.load() > 0);

    if (const auto prev = m_num_running_threads--; prev == 1) {
        this->Terminate();
    }
}

bool KProcess::EnterUserException() {
    // Get the current thread.
    KThread* cur_thread = GetCurrentThreadPointer(m_kernel);
    ASSERT(this == cur_thread->GetOwnerProcess());

    // Check that we haven't already claimed the exception thread.
    if (m_exception_thread == cur_thread) {
        return false;
    }

    // Create the wait queue we'll be using.
    ThreadQueueImplForKProcessEnterUserException wait_queue(m_kernel,
                                                            std::addressof(m_exception_thread));

    // Claim the exception thread.
    {
        // Lock the scheduler.
        KScopedSchedulerLock sl(m_kernel);

        // Check that we're not terminating.
        if (cur_thread->IsTerminationRequested()) {
            return false;
        }

        // If we don't have an exception thread, we can just claim it directly.
        if (m_exception_thread == nullptr) {
            m_exception_thread = cur_thread;
            KScheduler::SetSchedulerUpdateNeeded(m_kernel);
            return true;
        }

        // Otherwise, we need to wait until we don't have an exception thread.

        // Add the current thread as a waiter on the current exception thread.
        cur_thread->SetKernelAddressKey(
            reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread)) | 1);
        m_exception_thread->AddWaiter(cur_thread);

        // Wait to claim the exception thread.
        cur_thread->BeginWait(std::addressof(wait_queue));
    }

    // If our wait didn't end due to thread termination, we succeeded.
    return ResultTerminationRequested != cur_thread->GetWaitResult();
}

bool KProcess::LeaveUserException() {
    return this->ReleaseUserException(GetCurrentThreadPointer(m_kernel));
}

bool KProcess::ReleaseUserException(KThread* thread) {
    KScopedSchedulerLock sl(m_kernel);

    if (m_exception_thread == thread) {
        m_exception_thread = nullptr;

        // Remove waiter thread.
        bool has_waiters;
        if (KThread* next = thread->RemoveKernelWaiterByKey(
                std::addressof(has_waiters),
                reinterpret_cast<uintptr_t>(std::addressof(m_exception_thread)) | 1);
            next != nullptr) {
            next->EndWait(ResultSuccess);
        }

        KScheduler::SetSchedulerUpdateNeeded(m_kernel);

        return true;
    } else {
        return false;
    }
}

void KProcess::RegisterThread(KThread* thread) {
    KScopedLightLock lk(m_list_lock);

    m_thread_list.push_back(*thread);
}

void KProcess::UnregisterThread(KThread* thread) {
    KScopedLightLock lk(m_list_lock);

    m_thread_list.erase(m_thread_list.iterator_to(*thread));
}

size_t KProcess::GetUsedUserPhysicalMemorySize() const {
    const size_t norm_size = m_page_table.GetNormalMemorySize();
    const size_t other_size = m_code_size + m_main_thread_stack_size;
    const size_t sec_size = this->GetRequiredSecureMemorySizeNonDefault();

    return norm_size + other_size + sec_size;
}

size_t KProcess::GetTotalUserPhysicalMemorySize() const {
    // Get the amount of free and used size.
    const size_t free_size =
        m_resource_limit->GetFreeValue(Svc::LimitableResource::PhysicalMemoryMax);
    const size_t max_size = m_max_process_memory;

    // Determine used size.
    // NOTE: This does *not* check this->IsDefaultApplicationSystemResource(), unlike
    // GetUsedUserPhysicalMemorySize().
    const size_t norm_size = m_page_table.GetNormalMemorySize();
    const size_t other_size = m_code_size + m_main_thread_stack_size;
    const size_t sec_size = this->GetRequiredSecureMemorySize();
    const size_t used_size = norm_size + other_size + sec_size;

    // NOTE: These function calls will recalculate, introducing a race...it is unclear why Nintendo
    // does it this way.
    if (used_size + free_size > max_size) {
        return max_size;
    } else {
        return free_size + this->GetUsedUserPhysicalMemorySize();
    }
}

size_t KProcess::GetUsedNonSystemUserPhysicalMemorySize() const {
    const size_t norm_size = m_page_table.GetNormalMemorySize();
    const size_t other_size = m_code_size + m_main_thread_stack_size;

    return norm_size + other_size;
}

size_t KProcess::GetTotalNonSystemUserPhysicalMemorySize() const {
    // Get the amount of free and used size.
    const size_t free_size =
        m_resource_limit->GetFreeValue(Svc::LimitableResource::PhysicalMemoryMax);
    const size_t max_size = m_max_process_memory;

    // Determine used size.
    // NOTE: This does *not* check this->IsDefaultApplicationSystemResource(), unlike
    // GetUsedUserPhysicalMemorySize().
    const size_t norm_size = m_page_table.GetNormalMemorySize();
    const size_t other_size = m_code_size + m_main_thread_stack_size;
    const size_t sec_size = this->GetRequiredSecureMemorySize();
    const size_t used_size = norm_size + other_size + sec_size;

    // NOTE: These function calls will recalculate, introducing a race...it is unclear why Nintendo
    // does it this way.
    if (used_size + free_size > max_size) {
        return max_size - this->GetRequiredSecureMemorySizeNonDefault();
    } else {
        return free_size + this->GetUsedNonSystemUserPhysicalMemorySize();
    }
}

Result KProcess::Run(s32 priority, size_t stack_size) {
    // Lock ourselves, to prevent concurrent access.
    KScopedLightLock lk(m_state_lock);

    // Validate that we're in a state where we can initialize.
    const auto state = m_state;
    R_UNLESS(state == State::Created || state == State::CreatedAttached, ResultInvalidState);

    // Place a tentative reservation of a thread for this process.
    KScopedResourceReservation thread_reservation(this, Svc::LimitableResource::ThreadCountMax);
    R_UNLESS(thread_reservation.Succeeded(), ResultLimitReached);

    // Ensure that we haven't already allocated stack.
    ASSERT(m_main_thread_stack_size == 0);

    // Ensure that we're allocating a valid stack.
    stack_size = Common::AlignUp(stack_size, PageSize);
    R_UNLESS(stack_size + m_code_size <= m_max_process_memory, ResultOutOfMemory);
    R_UNLESS(stack_size + m_code_size >= m_code_size, ResultOutOfMemory);

    // Place a tentative reservation of memory for our new stack.
    KScopedResourceReservation mem_reservation(this, Svc::LimitableResource::PhysicalMemoryMax,
                                               stack_size);
    R_UNLESS(mem_reservation.Succeeded(), ResultLimitReached);

    // Allocate and map our stack.
    KProcessAddress stack_top = 0;
    if (stack_size) {
        KProcessAddress stack_bottom;
        R_TRY(m_page_table.MapPages(std::addressof(stack_bottom), stack_size / PageSize,
                                    KMemoryState::Stack, KMemoryPermission::UserReadWrite));

        stack_top = stack_bottom + stack_size;
        m_main_thread_stack_size = stack_size;
    }

    // Ensure our stack is safe to clean up on exit.
    ON_RESULT_FAILURE {
        if (m_main_thread_stack_size) {
            ASSERT(R_SUCCEEDED(m_page_table.UnmapPages(stack_top - m_main_thread_stack_size,
                                                       m_main_thread_stack_size / PageSize,
                                                       KMemoryState::Stack)));
            m_main_thread_stack_size = 0;
        }
    };

    // Set our maximum heap size.
    R_TRY(m_page_table.SetMaxHeapSize(m_max_process_memory -
                                      (m_main_thread_stack_size + m_code_size)));

    // Initialize our handle table.
    R_TRY(this->InitializeHandleTable(m_capabilities.GetHandleTableSize()));
    ON_RESULT_FAILURE_2 {
        this->FinalizeHandleTable();
    };

    // Create a new thread for the process.
    KThread* main_thread = KThread::Create(m_kernel);
    R_UNLESS(main_thread != nullptr, ResultOutOfResource);
    SCOPE_EXIT {
        main_thread->Close();
    };

    // Initialize the thread.
    R_TRY(KThread::InitializeUserThread(m_kernel.System(), main_thread, this->GetEntryPoint(), 0,
                                        stack_top, priority, m_ideal_core_id, this));

    // Register the thread, and commit our reservation.
    KThread::Register(m_kernel, main_thread);
    thread_reservation.Commit();

    // Add the thread to our handle table.
    Handle thread_handle;
    R_TRY(m_handle_table.Add(std::addressof(thread_handle), main_thread));

    // Set the thread arguments.
    main_thread->GetContext().r[0] = 0;
    main_thread->GetContext().r[1] = thread_handle;

    // Update our state.
    this->ChangeState((state == State::Created) ? State::Running : State::RunningAttached);
    ON_RESULT_FAILURE_2 {
        this->ChangeState(state);
    };

    // Suspend for debug, if we should.
    if (m_kernel.System().DebuggerEnabled()) {
        main_thread->RequestSuspend(SuspendType::Debug);
    }

    // Run our thread.
    R_TRY(main_thread->Run());

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

    // We succeeded! Commit our memory reservation.
    mem_reservation.Commit();

    R_SUCCEED();
}

Result KProcess::Reset() {
    // Lock the process and the scheduler.
    KScopedLightLock lk(m_state_lock);
    KScopedSchedulerLock sl(m_kernel);

    // Validate that we're in a state that we can reset.
    R_UNLESS(m_state != State::Terminated, ResultInvalidState);
    R_UNLESS(m_is_signaled, ResultInvalidState);

    // Clear signaled.
    m_is_signaled = false;
    R_SUCCEED();
}

Result KProcess::SetActivity(Svc::ProcessActivity activity) {
    // Lock ourselves and the scheduler.
    KScopedLightLock lk(m_state_lock);
    KScopedLightLock list_lk(m_list_lock);
    KScopedSchedulerLock sl(m_kernel);

    // Validate our state.
    R_UNLESS(m_state != State::Terminating, ResultInvalidState);
    R_UNLESS(m_state != State::Terminated, ResultInvalidState);

    // Either pause or resume.
    if (activity == Svc::ProcessActivity::Paused) {
        // Verify that we're not suspended.
        R_UNLESS(!m_is_suspended, ResultInvalidState);

        // Suspend all threads.
        auto end = this->GetThreadList().end();
        for (auto it = this->GetThreadList().begin(); it != end; ++it) {
            it->RequestSuspend(SuspendType::Process);
        }

        // Set ourselves as suspended.
        this->SetSuspended(true);
    } else {
        ASSERT(activity == Svc::ProcessActivity::Runnable);

        // Verify that we're suspended.
        R_UNLESS(m_is_suspended, ResultInvalidState);

        // Resume all threads.
        auto end = this->GetThreadList().end();
        for (auto it = this->GetThreadList().begin(); it != end; ++it) {
            it->Resume(SuspendType::Process);
        }

        // Set ourselves as resumed.
        this->SetSuspended(false);
    }

    R_SUCCEED();
}

void KProcess::PinCurrentThread() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Get the current thread.
    const s32 core_id = GetCurrentCoreId(m_kernel);
    KThread* cur_thread = GetCurrentThreadPointer(m_kernel);

    // If the thread isn't terminated, pin it.
    if (!cur_thread->IsTerminationRequested()) {
        // Pin it.
        this->PinThread(core_id, cur_thread);
        cur_thread->Pin(core_id);

        // An update is needed.
        KScheduler::SetSchedulerUpdateNeeded(m_kernel);
    }
}

void KProcess::UnpinCurrentThread() {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    // Get the current thread.
    const s32 core_id = GetCurrentCoreId(m_kernel);
    KThread* cur_thread = GetCurrentThreadPointer(m_kernel);

    // Unpin it.
    cur_thread->Unpin();
    this->UnpinThread(core_id, cur_thread);

    // An update is needed.
    KScheduler::SetSchedulerUpdateNeeded(m_kernel);
}

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

    // Get the thread's core id.
    const auto core_id = thread->GetActiveCore();

    // Unpin it.
    this->UnpinThread(core_id, thread);
    thread->Unpin();

    // An update is needed.
    KScheduler::SetSchedulerUpdateNeeded(m_kernel);
}

Result KProcess::GetThreadList(s32* out_num_threads, KProcessAddress out_thread_ids,
                               s32 max_out_count) {
    auto& memory = this->GetMemory();

    // Lock the list.
    KScopedLightLock lk(m_list_lock);

    // Iterate over the list.
    s32 count = 0;
    auto end = this->GetThreadList().end();
    for (auto it = this->GetThreadList().begin(); it != end; ++it) {
        // If we're within array bounds, write the id.
        if (count < max_out_count) {
            // Get the thread id.
            KThread* thread = std::addressof(*it);
            const u64 id = thread->GetId();

            // Copy the id to userland.
            memory.Write64(out_thread_ids + count * sizeof(u64), id);
        }

        // Increment the count.
        ++count;
    }

    // We successfully iterated the list.
    *out_num_threads = count;
    R_SUCCEED();
}

void KProcess::Switch(KProcess* cur_process, KProcess* next_process) {}

KProcess::KProcess(KernelCore& kernel)
    : KAutoObjectWithSlabHeapAndContainer(kernel), m_page_table{kernel}, m_state_lock{kernel},
      m_list_lock{kernel}, m_cond_var{kernel.System()}, m_address_arbiter{kernel.System()},
      m_handle_table{kernel}, m_exclusive_monitor{}, m_memory{kernel.System()} {}
KProcess::~KProcess() = default;

Result KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
                                  KProcessAddress aslr_space_start, bool is_hbl) {
    // Create a resource limit for the process.
    const auto pool = static_cast<KMemoryManager::Pool>(metadata.GetPoolPartition());
    const auto physical_memory_size = m_kernel.MemoryManager().GetSize(pool);
    auto* res_limit =
        Kernel::CreateResourceLimitForProcess(m_kernel.System(), physical_memory_size);

    // Ensure we maintain a clean state on exit.
    SCOPE_EXIT {
        res_limit->Close();
    };

    // Declare flags and code address.
    Svc::CreateProcessFlag flag{};
    u64 code_address{};

    // Determine if we are an application.
    if (pool == KMemoryManager::Pool::Application) {
        flag |= Svc::CreateProcessFlag::IsApplication;
    }

    // If we are 64-bit, create as such.
    if (metadata.Is64BitProgram()) {
        flag |= Svc::CreateProcessFlag::Is64Bit;
    }

    // Set the address space type and code address.
    switch (metadata.GetAddressSpaceType()) {
    case FileSys::ProgramAddressSpaceType::Is39Bit:
        flag |= Svc::CreateProcessFlag::AddressSpace64Bit;

        // For 39-bit processes, the ASLR region starts at 0x800'0000 and is ~512GiB large.
        // However, some (buggy) programs/libraries like skyline incorrectly depend on the
        // existence of ASLR pages before the entry point, so we will adjust the load address
        // to point to about 2GiB into the ASLR region.
        code_address = 0x8000'0000;
        break;
    case FileSys::ProgramAddressSpaceType::Is36Bit:
        flag |= Svc::CreateProcessFlag::AddressSpace64BitDeprecated;
        code_address = 0x800'0000;
        break;
    case FileSys::ProgramAddressSpaceType::Is32Bit:
        flag |= Svc::CreateProcessFlag::AddressSpace32Bit;
        code_address = 0x20'0000;
        break;
    case FileSys::ProgramAddressSpaceType::Is32BitNoMap:
        flag |= Svc::CreateProcessFlag::AddressSpace32BitWithoutAlias;
        code_address = 0x20'0000;
        break;
    }

    Svc::CreateProcessParameter params{
        .name = {},
        .version = {},
        .program_id = metadata.GetTitleID(),
        .code_address = code_address + GetInteger(aslr_space_start),
        .code_num_pages = static_cast<s32>(code_size / PageSize),
        .flags = flag,
        .reslimit = Svc::InvalidHandle,
        .system_resource_num_pages = static_cast<s32>(metadata.GetSystemResourceSize() / PageSize),
    };

    // Set the process name.
    const auto& name = metadata.GetName();
    static_assert(sizeof(params.name) <= sizeof(name));
    std::memcpy(params.name.data(), name.data(), sizeof(params.name));

    // Initialize for application process.
    R_TRY(this->Initialize(params, metadata.GetKernelCapabilities(), res_limit, pool,
                           aslr_space_start));

    // Assign remaining properties.
    m_is_hbl = is_hbl;
    m_ideal_core_id = metadata.GetMainThreadCore();

    // Set up emulation context.
    this->InitializeInterfaces();

    // We succeeded.
    R_SUCCEED();
}

void KProcess::LoadModule(CodeSet code_set, KProcessAddress base_addr) {
    const auto ReprotectSegment = [&](const CodeSet::Segment& segment,
                                      Svc::MemoryPermission permission) {
        m_page_table.SetProcessMemoryPermission(segment.addr + base_addr, segment.size, permission);
    };

    this->GetMemory().WriteBlock(base_addr, code_set.memory.data(), code_set.memory.size());

    ReprotectSegment(code_set.CodeSegment(), Svc::MemoryPermission::ReadExecute);
    ReprotectSegment(code_set.RODataSegment(), Svc::MemoryPermission::Read);
    ReprotectSegment(code_set.DataSegment(), Svc::MemoryPermission::ReadWrite);

#ifdef HAS_NCE
    const auto& patch = code_set.PatchSegment();
    if (this->IsApplication() && Settings::IsNceEnabled() && patch.size != 0) {
        auto& buffer = m_kernel.System().DeviceMemory().buffer;
        const auto& code = code_set.CodeSegment();
        buffer.Protect(GetInteger(base_addr + code.addr), code.size,
                       Common::MemoryPermission::Read | Common::MemoryPermission::Execute);
        buffer.Protect(GetInteger(base_addr + patch.addr), patch.size,
                       Common::MemoryPermission::Read | Common::MemoryPermission::Execute);
        ReprotectSegment(code_set.PatchSegment(), Svc::MemoryPermission::None);
    }
#endif
}

void KProcess::InitializeInterfaces() {
    m_exclusive_monitor =
        Core::MakeExclusiveMonitor(this->GetMemory(), Core::Hardware::NUM_CPU_CORES);

#ifdef HAS_NCE
    if (this->IsApplication() && Settings::IsNceEnabled()) {
        // Register the scoped JIT handler before creating any NCE instances
        // so that its signal handler will appear first in the signal chain.
        Core::ScopedJitExecution::RegisterHandler();

        for (size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
            m_arm_interfaces[i] = std::make_unique<Core::ArmNce>(m_kernel.System(), true, i);
        }
    } else
#endif
        if (this->Is64Bit()) {
        for (size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
            m_arm_interfaces[i] = std::make_unique<Core::ArmDynarmic64>(
                m_kernel.System(), m_kernel.IsMulticore(), this,
                static_cast<Core::DynarmicExclusiveMonitor&>(*m_exclusive_monitor), i);
        }
    } else {
        for (size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
            m_arm_interfaces[i] = std::make_unique<Core::ArmDynarmic32>(
                m_kernel.System(), m_kernel.IsMulticore(), this,
                static_cast<Core::DynarmicExclusiveMonitor&>(*m_exclusive_monitor), i);
        }
    }
}

bool KProcess::InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type) {
    const auto watch{std::find_if(m_watchpoints.begin(), m_watchpoints.end(), [&](const auto& wp) {
        return wp.type == DebugWatchpointType::None;
    })};

    if (watch == m_watchpoints.end()) {
        return false;
    }

    watch->start_address = addr;
    watch->end_address = addr + size;
    watch->type = type;

    for (KProcessAddress page = Common::AlignDown(GetInteger(addr), PageSize); page < addr + size;
         page += PageSize) {
        m_debug_page_refcounts[page]++;
        this->GetMemory().MarkRegionDebug(page, PageSize, true);
    }

    return true;
}

bool KProcess::RemoveWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type) {
    const auto watch{std::find_if(m_watchpoints.begin(), m_watchpoints.end(), [&](const auto& wp) {
        return wp.start_address == addr && wp.end_address == addr + size && wp.type == type;
    })};

    if (watch == m_watchpoints.end()) {
        return false;
    }

    watch->start_address = 0;
    watch->end_address = 0;
    watch->type = DebugWatchpointType::None;

    for (KProcessAddress page = Common::AlignDown(GetInteger(addr), PageSize); page < addr + size;
         page += PageSize) {
        m_debug_page_refcounts[page]--;
        if (!m_debug_page_refcounts[page]) {
            this->GetMemory().MarkRegionDebug(page, PageSize, false);
        }
    }

    return true;
}

} // namespace Kernel


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

#pragma once

#include <map>

#include "core/arm/arm_interface.h"
#include "core/file_sys/program_metadata.h"
#include "core/hle/kernel/code_set.h"
#include "core/hle/kernel/k_address_arbiter.h"
#include "core/hle/kernel/k_capabilities.h"
#include "core/hle/kernel/k_condition_variable.h"
#include "core/hle/kernel/k_handle_table.h"
#include "core/hle/kernel/k_page_table_manager.h"
#include "core/hle/kernel/k_process_page_table.h"
#include "core/hle/kernel/k_system_resource.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/k_thread_local_page.h"
#include "core/memory.h"

namespace Kernel {

enum class DebugWatchpointType : u8 {
    None = 0,
    Read = 1 << 0,
    Write = 1 << 1,
    ReadOrWrite = Read | Write,
};
DECLARE_ENUM_FLAG_OPERATORS(DebugWatchpointType);

struct DebugWatchpoint {
    KProcessAddress start_address;
    KProcessAddress end_address;
    DebugWatchpointType type;
};

class KProcess final : public KAutoObjectWithSlabHeapAndContainer<KProcess, KWorkerTask> {
    KERNEL_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject);

public:
    enum class State {
        Created = static_cast<u32>(Svc::ProcessState::Created),
        CreatedAttached = static_cast<u32>(Svc::ProcessState::CreatedAttached),
        Running = static_cast<u32>(Svc::ProcessState::Running),
        Crashed = static_cast<u32>(Svc::ProcessState::Crashed),
        RunningAttached = static_cast<u32>(Svc::ProcessState::RunningAttached),
        Terminating = static_cast<u32>(Svc::ProcessState::Terminating),
        Terminated = static_cast<u32>(Svc::ProcessState::Terminated),
        DebugBreak = static_cast<u32>(Svc::ProcessState::DebugBreak),
    };

    using ThreadList = Common::IntrusiveListMemberTraits<&KThread::m_process_list_node>::ListType;

    static constexpr size_t AslrAlignment = 2_MiB;

public:
    static constexpr u64 InitialProcessIdMin = 1;
    static constexpr u64 InitialProcessIdMax = 0x50;

    static constexpr u64 ProcessIdMin = InitialProcessIdMax + 1;
    static constexpr u64 ProcessIdMax = std::numeric_limits<u64>::max();

private:
    using SharedMemoryInfoList = Common::IntrusiveListBaseTraits<KSharedMemoryInfo>::ListType;
    using TLPTree =
        Common::IntrusiveRedBlackTreeBaseTraits<KThreadLocalPage>::TreeType<KThreadLocalPage>;
    using TLPIterator = TLPTree::iterator;

private:
    KProcessPageTable m_page_table;
    std::atomic<size_t> m_used_kernel_memory_size{};
    TLPTree m_fully_used_tlp_tree{};
    TLPTree m_partially_used_tlp_tree{};
    s32 m_ideal_core_id{};
    KResourceLimit* m_resource_limit{};
    KSystemResource* m_system_resource{};
    size_t m_memory_release_hint{};
    State m_state{};
    KLightLock m_state_lock;
    KLightLock m_list_lock;
    KConditionVariable m_cond_var;
    KAddressArbiter m_address_arbiter;
    std::array<u64, 4> m_entropy{};
    bool m_is_signaled{};
    bool m_is_initialized{};
    bool m_is_application{};
    bool m_is_default_application_system_resource{};
    bool m_is_hbl{};
    std::array<char, 13> m_name{};
    std::atomic<u16> m_num_running_threads{};
    Svc::CreateProcessFlag m_flags{};
    KMemoryManager::Pool m_memory_pool{};
    s64 m_schedule_count{};
    KCapabilities m_capabilities{};
    u64 m_program_id{};
    u64 m_process_id{};
    KProcessAddress m_code_address{};
    size_t m_code_size{};
    size_t m_main_thread_stack_size{};
    size_t m_max_process_memory{};
    u32 m_version{};
    KHandleTable m_handle_table;
    KProcessAddress m_plr_address{};
    KThread* m_exception_thread{};
    ThreadList m_thread_list{};
    SharedMemoryInfoList m_shared_memory_list{};
    bool m_is_suspended{};
    bool m_is_immortal{};
    bool m_is_handle_table_initialized{};
    std::array<std::unique_ptr<Core::ArmInterface>, Core::Hardware::NUM_CPU_CORES>
        m_arm_interfaces{};
    std::array<KThread*, Core::Hardware::NUM_CPU_CORES> m_running_threads{};
    std::array<u64, Core::Hardware::NUM_CPU_CORES> m_running_thread_idle_counts{};
    std::array<u64, Core::Hardware::NUM_CPU_CORES> m_running_thread_switch_counts{};
    std::array<KThread*, Core::Hardware::NUM_CPU_CORES> m_pinned_threads{};
    std::array<DebugWatchpoint, Core::Hardware::NUM_WATCHPOINTS> m_watchpoints{};
    std::map<KProcessAddress, u64> m_debug_page_refcounts{};
    std::atomic<s64> m_cpu_time{};
    std::atomic<s64> m_num_process_switches{};
    std::atomic<s64> m_num_thread_switches{};
    std::atomic<s64> m_num_fpu_switches{};
    std::atomic<s64> m_num_supervisor_calls{};
    std::atomic<s64> m_num_ipc_messages{};
    std::atomic<s64> m_num_ipc_replies{};
    std::atomic<s64> m_num_ipc_receives{};
#ifdef HAS_NCE
    std::unordered_map<u64, u64> m_post_handlers{};
#endif
    std::unique_ptr<Core::ExclusiveMonitor> m_exclusive_monitor;
    Core::Memory::Memory m_memory;

private:
    Result StartTermination();
    void FinishTermination();

    void PinThread(s32 core_id, KThread* thread) {
        ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
        ASSERT(thread != nullptr);
        ASSERT(m_pinned_threads[core_id] == nullptr);
        m_pinned_threads[core_id] = thread;
    }

    void UnpinThread(s32 core_id, KThread* thread) {
        ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
        ASSERT(thread != nullptr);
        ASSERT(m_pinned_threads[core_id] == thread);
        m_pinned_threads[core_id] = nullptr;
    }

public:
    explicit KProcess(KernelCore& kernel);
    ~KProcess() override;

    Result Initialize(const Svc::CreateProcessParameter& params, KResourceLimit* res_limit,
                      bool is_real);

    Result Initialize(const Svc::CreateProcessParameter& params, const KPageGroup& pg,
                      std::span<const u32> caps, KResourceLimit* res_limit,
                      KMemoryManager::Pool pool, bool immortal);
    Result Initialize(const Svc::CreateProcessParameter& params, std::span<const u32> user_caps,
                      KResourceLimit* res_limit, KMemoryManager::Pool pool,
                      KProcessAddress aslr_space_start);
    void Exit();

    const char* GetName() const {
        return m_name.data();
    }

    u64 GetProgramId() const {
        return m_program_id;
    }

    u64 GetProcessId() const {
        return m_process_id;
    }

    State GetState() const {
        return m_state;
    }

    u64 GetCoreMask() const {
        return m_capabilities.GetCoreMask();
    }
    u64 GetPhysicalCoreMask() const {
        return m_capabilities.GetPhysicalCoreMask();
    }
    u64 GetPriorityMask() const {
        return m_capabilities.GetPriorityMask();
    }

    s32 GetIdealCoreId() const {
        return m_ideal_core_id;
    }
    void SetIdealCoreId(s32 core_id) {
        m_ideal_core_id = core_id;
    }

    bool CheckThreadPriority(s32 prio) const {
        return ((1ULL << prio) & this->GetPriorityMask()) != 0;
    }

    u32 GetCreateProcessFlags() const {
        return static_cast<u32>(m_flags);
    }

    bool Is64Bit() const {
        return True(m_flags & Svc::CreateProcessFlag::Is64Bit);
    }

    KProcessAddress GetEntryPoint() const {
        return m_code_address;
    }

    size_t GetMainStackSize() const {
        return m_main_thread_stack_size;
    }

    KMemoryManager::Pool GetMemoryPool() const {
        return m_memory_pool;
    }

    u64 GetRandomEntropy(size_t i) const {
        return m_entropy[i];
    }

    bool IsApplication() const {
        return m_is_application;
    }

    bool IsDefaultApplicationSystemResource() const {
        return m_is_default_application_system_resource;
    }

    bool IsSuspended() const {
        return m_is_suspended;
    }
    void SetSuspended(bool suspended) {
        m_is_suspended = suspended;
    }

    Result Terminate();

    bool IsTerminated() const {
        return m_state == State::Terminated;
    }

    bool IsPermittedSvc(u32 svc_id) const {
        return m_capabilities.IsPermittedSvc(svc_id);
    }

    bool IsPermittedInterrupt(s32 interrupt_id) const {
        return m_capabilities.IsPermittedInterrupt(interrupt_id);
    }

    bool IsPermittedDebug() const {
        return m_capabilities.IsPermittedDebug();
    }

    bool CanForceDebug() const {
        return m_capabilities.CanForceDebug();
    }

    bool IsHbl() const {
        return m_is_hbl;
    }

    u32 GetAllocateOption() const {
        return m_page_table.GetAllocateOption();
    }

    ThreadList& GetThreadList() {
        return m_thread_list;
    }
    const ThreadList& GetThreadList() const {
        return m_thread_list;
    }

    bool EnterUserException();
    bool LeaveUserException();
    bool ReleaseUserException(KThread* thread);

    KThread* GetPinnedThread(s32 core_id) const {
        ASSERT(0 <= core_id && core_id < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
        return m_pinned_threads[core_id];
    }

    const Svc::SvcAccessFlagSet& GetSvcPermissions() const {
        return m_capabilities.GetSvcPermissions();
    }

    KResourceLimit* GetResourceLimit() const {
        return m_resource_limit;
    }

    bool ReserveResource(Svc::LimitableResource which, s64 value);
    bool ReserveResource(Svc::LimitableResource which, s64 value, s64 timeout);
    void ReleaseResource(Svc::LimitableResource which, s64 value);
    void ReleaseResource(Svc::LimitableResource which, s64 value, s64 hint);

    KLightLock& GetStateLock() {
        return m_state_lock;
    }
    KLightLock& GetListLock() {
        return m_list_lock;
    }

    KProcessPageTable& GetPageTable() {
        return m_page_table;
    }
    const KProcessPageTable& GetPageTable() const {
        return m_page_table;
    }

    KHandleTable& GetHandleTable() {
        return m_handle_table;
    }
    const KHandleTable& GetHandleTable() const {
        return m_handle_table;
    }

    size_t GetUsedUserPhysicalMemorySize() const;
    size_t GetTotalUserPhysicalMemorySize() const;
    size_t GetUsedNonSystemUserPhysicalMemorySize() const;
    size_t GetTotalNonSystemUserPhysicalMemorySize() const;

    Result AddSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size);
    void RemoveSharedMemory(KSharedMemory* shmem, KProcessAddress address, size_t size);

    Result CreateThreadLocalRegion(KProcessAddress* out);
    Result DeleteThreadLocalRegion(KProcessAddress addr);

    KProcessAddress GetProcessLocalRegionAddress() const {
        return m_plr_address;
    }

    KThread* GetExceptionThread() const {
        return m_exception_thread;
    }

    void AddCpuTime(s64 diff) {
        m_cpu_time += diff;
    }
    s64 GetCpuTime() {
        return m_cpu_time.load();
    }

    s64 GetScheduledCount() const {
        return m_schedule_count;
    }
    void IncrementScheduledCount() {
        ++m_schedule_count;
    }

    void IncrementRunningThreadCount();
    void DecrementRunningThreadCount();

    size_t GetRequiredSecureMemorySizeNonDefault() const {
        if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
            auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
            return secure_system_resource->CalculateRequiredSecureMemorySize();
        }

        return 0;
    }

    size_t GetRequiredSecureMemorySize() const {
        if (m_system_resource->IsSecureResource()) {
            auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
            return secure_system_resource->CalculateRequiredSecureMemorySize();
        }

        return 0;
    }

    size_t GetTotalSystemResourceSize() const {
        if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
            auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
            return secure_system_resource->GetSize();
        }

        return 0;
    }

    size_t GetUsedSystemResourceSize() const {
        if (!this->IsDefaultApplicationSystemResource() && m_system_resource->IsSecureResource()) {
            auto* secure_system_resource = static_cast<KSecureSystemResource*>(m_system_resource);
            return secure_system_resource->GetUsedSize();
        }

        return 0;
    }

    void SetRunningThread(s32 core, KThread* thread, u64 idle_count, u64 switch_count) {
        m_running_threads[core] = thread;
        m_running_thread_idle_counts[core] = idle_count;
        m_running_thread_switch_counts[core] = switch_count;
    }

    void ClearRunningThread(KThread* thread) {
        for (size_t i = 0; i < m_running_threads.size(); ++i) {
            if (m_running_threads[i] == thread) {
                m_running_threads[i] = nullptr;
            }
        }
    }

    const KSystemResource& GetSystemResource() const {
        return *m_system_resource;
    }

    const KMemoryBlockSlabManager& GetMemoryBlockSlabManager() const {
        return m_system_resource->GetMemoryBlockSlabManager();
    }
    const KBlockInfoManager& GetBlockInfoManager() const {
        return m_system_resource->GetBlockInfoManager();
    }
    const KPageTableManager& GetPageTableManager() const {
        return m_system_resource->GetPageTableManager();
    }

    KThread* GetRunningThread(s32 core) const {
        return m_running_threads[core];
    }
    u64 GetRunningThreadIdleCount(s32 core) const {
        return m_running_thread_idle_counts[core];
    }
    u64 GetRunningThreadSwitchCount(s32 core) const {
        return m_running_thread_switch_counts[core];
    }

    void RegisterThread(KThread* thread);
    void UnregisterThread(KThread* thread);

    Result Run(s32 priority, size_t stack_size);

    Result Reset();

    void SetDebugBreak() {
        if (m_state == State::RunningAttached) {
            this->ChangeState(State::DebugBreak);
        }
    }

    void SetAttached() {
        if (m_state == State::DebugBreak) {
            this->ChangeState(State::RunningAttached);
        }
    }

    Result SetActivity(Svc::ProcessActivity activity);

    void PinCurrentThread();
    void UnpinCurrentThread();
    void UnpinThread(KThread* thread);

    void SignalConditionVariable(uintptr_t cv_key, int32_t count) {
        return m_cond_var.Signal(cv_key, count);
    }

    Result WaitConditionVariable(KProcessAddress address, uintptr_t cv_key, u32 tag, s64 ns) {
        R_RETURN(m_cond_var.Wait(address, cv_key, tag, ns));
    }

    Result SignalAddressArbiter(uintptr_t address, Svc::SignalType signal_type, s32 value,
                                s32 count) {
        R_RETURN(m_address_arbiter.SignalToAddress(address, signal_type, value, count));
    }

    Result WaitAddressArbiter(uintptr_t address, Svc::ArbitrationType arb_type, s32 value,
                              s64 timeout) {
        R_RETURN(m_address_arbiter.WaitForAddress(address, arb_type, value, timeout));
    }

    Result GetThreadList(s32* out_num_threads, KProcessAddress out_thread_ids, s32 max_out_count);

    static void Switch(KProcess* cur_process, KProcess* next_process);

#ifdef HAS_NCE
    std::unordered_map<u64, u64>& GetPostHandlers() noexcept {
        return m_post_handlers;
    }
#endif

    Core::ArmInterface* GetArmInterface(size_t core_index) const {
        return m_arm_interfaces[core_index].get();
    }

public:
    // Attempts to insert a watchpoint into a free slot. Returns false if none are available.
    bool InsertWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type);

    // Attempts to remove the watchpoint specified by the given parameters.
    bool RemoveWatchpoint(KProcessAddress addr, u64 size, DebugWatchpointType type);

    const std::array<DebugWatchpoint, Core::Hardware::NUM_WATCHPOINTS>& GetWatchpoints() const {
        return m_watchpoints;
    }

public:
    Result LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size,
                            KProcessAddress aslr_space_start, bool is_hbl);

    void LoadModule(CodeSet code_set, KProcessAddress base_addr);

    void InitializeInterfaces();

    Core::Memory::Memory& GetMemory() {
        return m_memory;
    }

    Core::ExclusiveMonitor& GetExclusiveMonitor() const {
        return *m_exclusive_monitor;
    }

public:
    // Overridden parent functions.
    bool IsInitialized() const override {
        return m_is_initialized;
    }

    static void PostDestroy(uintptr_t arg) {}

    void Finalize() override;

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

    virtual bool IsSignaled() const override {
        ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
        return m_is_signaled;
    }

    void DoWorkerTaskImpl();

private:
    void ChangeState(State new_state) {
        if (m_state != new_state) {
            m_state = new_state;
            m_is_signaled = true;
            this->NotifyAvailable();
        }
    }

    Result InitializeHandleTable(s32 size) {
        // Try to initialize the handle table.
        R_TRY(m_handle_table.Initialize(size));

        // We succeeded, so note that we did.
        m_is_handle_table_initialized = true;
        R_SUCCEED();
    }

    void FinalizeHandleTable() {
        // Finalize the table.
        m_handle_table.Finalize();

        // Note that the table is finalized.
        m_is_handle_table_initialized = false;
    }
};

} // namespace Kernel

repo_name: litucks/torzu

license: mit


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

#pragma once

#include "core/hle/kernel/k_page_table.h"
#include "core/hle/kernel/k_scoped_lock.h"
#include "core/hle/kernel/svc_types.h"

namespace Kernel {

class KProcessPageTable {
private:
    KPageTable m_page_table;

public:
    KProcessPageTable(KernelCore& kernel) : m_page_table(kernel) {}

    Result Initialize(Svc::CreateProcessFlag as_type, bool enable_aslr, bool enable_das_merge,
                      bool from_back, KMemoryManager::Pool pool, KProcessAddress code_address,
                      size_t code_size, KSystemResource* system_resource,
                      KResourceLimit* resource_limit, Core::Memory::Memory& memory,
                      KProcessAddress aslr_space_start) {
        R_RETURN(m_page_table.InitializeForProcess(
            as_type, enable_aslr, enable_das_merge, from_back, pool, code_address, code_size,
            system_resource, resource_limit, memory, aslr_space_start));
    }

    void Finalize() {
        m_page_table.Finalize();
    }

    Core::Memory::Memory& GetMemory() {
        return m_page_table.GetMemory();
    }

    Core::Memory::Memory& GetMemory() const {
        return m_page_table.GetMemory();
    }

    Common::PageTable& GetImpl() {
        return m_page_table.GetImpl();
    }

    Common::PageTable& GetImpl() const {
        return m_page_table.GetImpl();
    }

    size_t GetNumGuardPages() const {
        return m_page_table.GetNumGuardPages();
    }

    KScopedLightLock AcquireDeviceMapLock() {
        return m_page_table.AcquireDeviceMapLock();
    }

    Result SetMemoryPermission(KProcessAddress addr, size_t size, Svc::MemoryPermission perm) {
        R_RETURN(m_page_table.SetMemoryPermission(addr, size, perm));
    }

    Result SetProcessMemoryPermission(KProcessAddress addr, size_t size,
                                      Svc::MemoryPermission perm) {
        R_RETURN(m_page_table.SetProcessMemoryPermission(addr, size, perm));
    }

    Result SetMemoryAttribute(KProcessAddress addr, size_t size, KMemoryAttribute mask,
                              KMemoryAttribute attr) {
        R_RETURN(m_page_table.SetMemoryAttribute(addr, size, mask, attr));
    }

    Result SetHeapSize(KProcessAddress* out, size_t size) {
        R_RETURN(m_page_table.SetHeapSize(out, size));
    }

    Result SetMaxHeapSize(size_t size) {
        R_RETURN(m_page_table.SetMaxHeapSize(size));
    }

    Result QueryInfo(KMemoryInfo* out_info, Svc::PageInfo* out_page_info,
                     KProcessAddress addr) const {
        R_RETURN(m_page_table.QueryInfo(out_info, out_page_info, addr));
    }

    Result QueryPhysicalAddress(Svc::lp64::PhysicalMemoryInfo* out, KProcessAddress address) {
        R_RETURN(m_page_table.QueryPhysicalAddress(out, address));
    }

    Result QueryStaticMapping(KProcessAddress* out, KPhysicalAddress address, size_t size) {
        R_RETURN(m_page_table.QueryStaticMapping(out, address, size));
    }

    Result QueryIoMapping(KProcessAddress* out, KPhysicalAddress address, size_t size) {
        R_RETURN(m_page_table.QueryIoMapping(out, address, size));
    }

    Result MapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.MapMemory(dst_address, src_address, size));
    }

    Result UnmapMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.UnmapMemory(dst_address, src_address, size));
    }

    Result MapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.MapCodeMemory(dst_address, src_address, size));
    }

    Result UnmapCodeMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.UnmapCodeMemory(dst_address, src_address, size));
    }

    Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
        R_RETURN(m_page_table.MapIo(phys_addr, size, perm));
    }

    Result MapIoRegion(KProcessAddress dst_address, KPhysicalAddress phys_addr, size_t size,
                       Svc::MemoryMapping mapping, Svc::MemoryPermission perm) {
        R_RETURN(m_page_table.MapIoRegion(dst_address, phys_addr, size, mapping, perm));
    }

    Result UnmapIoRegion(KProcessAddress dst_address, KPhysicalAddress phys_addr, size_t size,
                         Svc::MemoryMapping mapping) {
        R_RETURN(m_page_table.UnmapIoRegion(dst_address, phys_addr, size, mapping));
    }

    Result MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
        R_RETURN(m_page_table.MapStatic(phys_addr, size, perm));
    }

    Result MapRegion(KMemoryRegionType region_type, KMemoryPermission perm) {
        R_RETURN(m_page_table.MapRegion(region_type, perm));
    }

    Result MapInsecureMemory(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.MapInsecureMemory(address, size));
    }

    Result UnmapInsecureMemory(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnmapInsecureMemory(address, size));
    }

    Result MapPageGroup(KProcessAddress addr, const KPageGroup& pg, KMemoryState state,
                        KMemoryPermission perm) {
        R_RETURN(m_page_table.MapPageGroup(addr, pg, state, perm));
    }

    Result UnmapPageGroup(KProcessAddress address, const KPageGroup& pg, KMemoryState state) {
        R_RETURN(m_page_table.UnmapPageGroup(address, pg, state));
    }

    Result MapPages(KProcessAddress* out_addr, size_t num_pages, size_t alignment,
                    KPhysicalAddress phys_addr, KMemoryState state, KMemoryPermission perm) {
        R_RETURN(m_page_table.MapPages(out_addr, num_pages, alignment, phys_addr, state, perm));
    }

    Result MapPages(KProcessAddress* out_addr, size_t num_pages, KMemoryState state,
                    KMemoryPermission perm) {
        R_RETURN(m_page_table.MapPages(out_addr, num_pages, state, perm));
    }

    Result MapPages(KProcessAddress address, size_t num_pages, KMemoryState state,
                    KMemoryPermission perm) {
        R_RETURN(m_page_table.MapPages(address, num_pages, state, perm));
    }

    Result UnmapPages(KProcessAddress addr, size_t num_pages, KMemoryState state) {
        R_RETURN(m_page_table.UnmapPages(addr, num_pages, state));
    }

    Result MakeAndOpenPageGroup(KPageGroup* out, KProcessAddress address, size_t num_pages,
                                KMemoryState state_mask, KMemoryState state,
                                KMemoryPermission perm_mask, KMemoryPermission perm,
                                KMemoryAttribute attr_mask, KMemoryAttribute attr) {
        R_RETURN(m_page_table.MakeAndOpenPageGroup(out, address, num_pages, state_mask, state,
                                                   perm_mask, perm, attr_mask, attr));
    }

    Result InvalidateProcessDataCache(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.InvalidateProcessDataCache(address, size));
    }

    Result ReadDebugMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.ReadDebugMemory(dst_address, src_address, size));
    }

    Result ReadDebugIoMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size,
                             KMemoryState state) {
        R_RETURN(m_page_table.ReadDebugIoMemory(dst_address, src_address, size, state));
    }

    Result WriteDebugMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size) {
        R_RETURN(m_page_table.WriteDebugMemory(dst_address, src_address, size));
    }

    Result WriteDebugIoMemory(KProcessAddress dst_address, KProcessAddress src_address, size_t size,
                              KMemoryState state) {
        R_RETURN(m_page_table.WriteDebugIoMemory(dst_address, src_address, size, state));
    }

    Result LockForMapDeviceAddressSpace(bool* out_is_io, KProcessAddress address, size_t size,
                                        KMemoryPermission perm, bool is_aligned, bool check_heap) {
        R_RETURN(m_page_table.LockForMapDeviceAddressSpace(out_is_io, address, size, perm,
                                                           is_aligned, check_heap));
    }

    Result LockForUnmapDeviceAddressSpace(KProcessAddress address, size_t size, bool check_heap) {
        R_RETURN(m_page_table.LockForUnmapDeviceAddressSpace(address, size, check_heap));
    }

    Result UnlockForDeviceAddressSpace(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnlockForDeviceAddressSpace(address, size));
    }

    Result UnlockForDeviceAddressSpacePartialMap(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnlockForDeviceAddressSpacePartialMap(address, size));
    }

    Result OpenMemoryRangeForMapDeviceAddressSpace(KPageTableBase::MemoryRange* out,
                                                   KProcessAddress address, size_t size,
                                                   KMemoryPermission perm, bool is_aligned) {
        R_RETURN(m_page_table.OpenMemoryRangeForMapDeviceAddressSpace(out, address, size, perm,
                                                                      is_aligned));
    }

    Result OpenMemoryRangeForUnmapDeviceAddressSpace(KPageTableBase::MemoryRange* out,
                                                     KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.OpenMemoryRangeForUnmapDeviceAddressSpace(out, address, size));
    }

    Result LockForIpcUserBuffer(KPhysicalAddress* out, KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.LockForIpcUserBuffer(out, address, size));
    }

    Result UnlockForIpcUserBuffer(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnlockForIpcUserBuffer(address, size));
    }

    Result LockForTransferMemory(KPageGroup* out, KProcessAddress address, size_t size,
                                 KMemoryPermission perm) {
        R_RETURN(m_page_table.LockForTransferMemory(out, address, size, perm));
    }

    Result UnlockForTransferMemory(KProcessAddress address, size_t size, const KPageGroup& pg) {
        R_RETURN(m_page_table.UnlockForTransferMemory(address, size, pg));
    }

    Result LockForCodeMemory(KPageGroup* out, KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.LockForCodeMemory(out, address, size));
    }

    Result UnlockForCodeMemory(KProcessAddress address, size_t size, const KPageGroup& pg) {
        R_RETURN(m_page_table.UnlockForCodeMemory(address, size, pg));
    }

    Result OpenMemoryRangeForProcessCacheOperation(KPageTableBase::MemoryRange* out,
                                                   KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.OpenMemoryRangeForProcessCacheOperation(out, address, size));
    }

    Result CopyMemoryFromLinearToUser(KProcessAddress dst_addr, size_t size,
                                      KProcessAddress src_addr, KMemoryState src_state_mask,
                                      KMemoryState src_state, KMemoryPermission src_test_perm,
                                      KMemoryAttribute src_attr_mask, KMemoryAttribute src_attr) {
        R_RETURN(m_page_table.CopyMemoryFromLinearToUser(dst_addr, size, src_addr, src_state_mask,
                                                         src_state, src_test_perm, src_attr_mask,
                                                         src_attr));
    }

    Result CopyMemoryFromLinearToKernel(void* dst_addr, size_t size, KProcessAddress src_addr,
                                        KMemoryState src_state_mask, KMemoryState src_state,
                                        KMemoryPermission src_test_perm,
                                        KMemoryAttribute src_attr_mask, KMemoryAttribute src_attr) {
        R_RETURN(m_page_table.CopyMemoryFromLinearToKernel(dst_addr, size, src_addr, src_state_mask,
                                                           src_state, src_test_perm, src_attr_mask,
                                                           src_attr));
    }

    Result CopyMemoryFromUserToLinear(KProcessAddress dst_addr, size_t size,
                                      KMemoryState dst_state_mask, KMemoryState dst_state,
                                      KMemoryPermission dst_test_perm,
                                      KMemoryAttribute dst_attr_mask, KMemoryAttribute dst_attr,
                                      KProcessAddress src_addr) {
        R_RETURN(m_page_table.CopyMemoryFromUserToLinear(dst_addr, size, dst_state_mask, dst_state,
                                                         dst_test_perm, dst_attr_mask, dst_attr,
                                                         src_addr));
    }

    Result CopyMemoryFromKernelToLinear(KProcessAddress dst_addr, size_t size,
                                        KMemoryState dst_state_mask, KMemoryState dst_state,
                                        KMemoryPermission dst_test_perm,
                                        KMemoryAttribute dst_attr_mask, KMemoryAttribute dst_attr,
                                        void* src_addr) {
        R_RETURN(m_page_table.CopyMemoryFromKernelToLinear(dst_addr, size, dst_state_mask,
                                                           dst_state, dst_test_perm, dst_attr_mask,
                                                           dst_attr, src_addr));
    }

    Result CopyMemoryFromHeapToHeap(KProcessPageTable& dst_page_table, KProcessAddress dst_addr,
                                    size_t size, KMemoryState dst_state_mask,
                                    KMemoryState dst_state, KMemoryPermission dst_test_perm,
                                    KMemoryAttribute dst_attr_mask, KMemoryAttribute dst_attr,
                                    KProcessAddress src_addr, KMemoryState src_state_mask,
                                    KMemoryState src_state, KMemoryPermission src_test_perm,
                                    KMemoryAttribute src_attr_mask, KMemoryAttribute src_attr) {
        R_RETURN(m_page_table.CopyMemoryFromHeapToHeap(
            dst_page_table.m_page_table, dst_addr, size, dst_state_mask, dst_state, dst_test_perm,
            dst_attr_mask, dst_attr, src_addr, src_state_mask, src_state, src_test_perm,
            src_attr_mask, src_attr));
    }

    Result CopyMemoryFromHeapToHeapWithoutCheckDestination(
        KProcessPageTable& dst_page_table, KProcessAddress dst_addr, size_t size,
        KMemoryState dst_state_mask, KMemoryState dst_state, KMemoryPermission dst_test_perm,
        KMemoryAttribute dst_attr_mask, KMemoryAttribute dst_attr, KProcessAddress src_addr,
        KMemoryState src_state_mask, KMemoryState src_state, KMemoryPermission src_test_perm,
        KMemoryAttribute src_attr_mask, KMemoryAttribute src_attr) {
        R_RETURN(m_page_table.CopyMemoryFromHeapToHeapWithoutCheckDestination(
            dst_page_table.m_page_table, dst_addr, size, dst_state_mask, dst_state, dst_test_perm,
            dst_attr_mask, dst_attr, src_addr, src_state_mask, src_state, src_test_perm,
            src_attr_mask, src_attr));
    }

    Result SetupForIpc(KProcessAddress* out_dst_addr, size_t size, KProcessAddress src_addr,
                       KProcessPageTable& src_page_table, KMemoryPermission test_perm,
                       KMemoryState dst_state, bool send) {
        R_RETURN(m_page_table.SetupForIpc(out_dst_addr, size, src_addr, src_page_table.m_page_table,
                                          test_perm, dst_state, send));
    }

    Result CleanupForIpcServer(KProcessAddress address, size_t size, KMemoryState dst_state) {
        R_RETURN(m_page_table.CleanupForIpcServer(address, size, dst_state));
    }

    Result CleanupForIpcClient(KProcessAddress address, size_t size, KMemoryState dst_state) {
        R_RETURN(m_page_table.CleanupForIpcClient(address, size, dst_state));
    }

    Result MapPhysicalMemory(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.MapPhysicalMemory(address, size));
    }

    Result UnmapPhysicalMemory(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnmapPhysicalMemory(address, size));
    }

    Result MapPhysicalMemoryUnsafe(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.MapPhysicalMemoryUnsafe(address, size));
    }

    Result UnmapPhysicalMemoryUnsafe(KProcessAddress address, size_t size) {
        R_RETURN(m_page_table.UnmapPhysicalMemoryUnsafe(address, size));
    }

    Result UnmapProcessMemory(KProcessAddress dst_address, size_t size,
                              KProcessPageTable& src_page_table, KProcessAddress src_address) {
        R_RETURN(m_page_table.UnmapProcessMemory(dst_address, size, src_page_table.m_page_table,
                                                 src_address));
    }

    bool GetPhysicalAddress(KPhysicalAddress* out, KProcessAddress address) {
        return m_page_table.GetPhysicalAddress(out, address);
    }

    bool Contains(KProcessAddress addr, size_t size) const {
        return m_page_table.Contains(addr, size);
    }

    bool IsInAliasRegion(KProcessAddress addr, size_t size) const {
        return m_page_table.IsInAliasRegion(addr, size);
    }
    bool IsInHeapRegion(KProcessAddress addr, size_t size) const {
        return m_page_table.IsInHeapRegion(addr, size);
    }
    bool IsInUnsafeAliasRegion(KProcessAddress addr, size_t size) const {
        return m_page_table.IsInUnsafeAliasRegion(addr, size);
    }

    bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const {
        return m_page_table.CanContain(addr, size, state);
    }

    KProcessAddress GetAddressSpaceStart() const {
        return m_page_table.GetAddressSpaceStart();
    }
    KProcessAddress GetHeapRegionStart() const {
        return m_page_table.GetHeapRegionStart();
    }
    KProcessAddress GetAliasRegionStart() const {
        return m_page_table.GetAliasRegionStart();
    }
    KProcessAddress GetStackRegionStart() const {
        return m_page_table.GetStackRegionStart();
    }
    KProcessAddress GetKernelMapRegionStart() const {
        return m_page_table.GetKernelMapRegionStart();
    }
    KProcessAddress GetCodeRegionStart() const {
        return m_page_table.GetCodeRegionStart();
    }
    KProcessAddress GetAliasCodeRegionStart() const {
        return m_page_table.GetAliasCodeRegionStart();
    }

    size_t GetAddressSpaceSize() const {
        return m_page_table.GetAddressSpaceSize();
    }
    size_t GetHeapRegionSize() const {
        return m_page_table.GetHeapRegionSize();
    }
    size_t GetAliasRegionSize() const {
        return m_page_table.GetAliasRegionSize();
    }
    size_t GetStackRegionSize() const {
        return m_page_table.GetStackRegionSize();
    }
    size_t GetKernelMapRegionSize() const {
        return m_page_table.GetKernelMapRegionSize();
    }
    size_t GetCodeRegionSize() const {
        return m_page_table.GetCodeRegionSize();
    }
    size_t GetAliasCodeRegionSize() const {
        return m_page_table.GetAliasCodeRegionSize();
    }

    size_t GetNormalMemorySize() const {
        return m_page_table.GetNormalMemorySize();
    }

    size_t GetCodeSize() const {
        return m_page_table.GetCodeSize();
    }
    size_t GetCodeDataSize() const {
        return m_page_table.GetCodeDataSize();
    }

    size_t GetAliasCodeSize() const {
        return m_page_table.GetAliasCodeSize();
    }
    size_t GetAliasCodeDataSize() const {
        return m_page_table.GetAliasCodeDataSize();
    }

    u32 GetAllocateOption() const {
        return m_page_table.GetAllocateOption();
    }

    u32 GetAddressSpaceWidth() const {
        return m_page_table.GetAddressSpaceWidth();
    }

    KPhysicalAddress GetHeapPhysicalAddress(KVirtualAddress address) {
        return m_page_table.GetHeapPhysicalAddress(address);
    }

    u8* GetHeapVirtualPointer(KPhysicalAddress address) {
        return m_page_table.GetHeapVirtualPointer(address);
    }

    KVirtualAddress GetHeapVirtualAddress(KPhysicalAddress address) {
        return m_page_table.GetHeapVirtualAddress(address);
    }

    KBlockInfoManager* GetBlockInfoManager() {
        return m_page_table.GetBlockInfoManager();
    }

    KPageTable& GetBasePageTable() {
        return m_page_table;
    }

    const KPageTable& GetBasePageTable() const {
        return m_page_table;
    }
};

} // namespace Kernel


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

#include "common/assert.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "core/hle/kernel/k_scheduler.h"
#include "core/hle/kernel/k_thread.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {

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

KReadableEvent::~KReadableEvent() = default;

void KReadableEvent::Initialize(KEvent* parent) {
    m_is_signaled = false;
    m_parent = parent;

    if (m_parent != nullptr) {
        m_parent->Open();
    }
}

bool KReadableEvent::IsSignaled() const {
    ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));

    return m_is_signaled;
}

void KReadableEvent::Destroy() {
    if (m_parent) {
        {
            KScopedSchedulerLock sl{m_kernel};
            m_parent->OnReadableEventDestroyed();
        }
        m_parent->Close();
    }
}

Result KReadableEvent::Signal() {
    KScopedSchedulerLock lk{m_kernel};

    if (!m_is_signaled) {
        m_is_signaled = true;
        this->NotifyAvailable();
    }

    R_SUCCEED();
}

Result KReadableEvent::Clear() {
    this->Reset();

    R_SUCCEED();
}

Result KReadableEvent::Reset() {
    KScopedSchedulerLock lk{m_kernel};

    R_UNLESS(m_is_signaled, ResultInvalidState);

    m_is_signaled = false;
    R_SUCCEED();
}

} // namespace Kernel


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

#pragma once

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

namespace Kernel {

class KernelCore;
class KEvent;

class KReadableEvent : public KSynchronizationObject {
    KERNEL_AUTOOBJECT_TRAITS(KReadableEvent, KSynchronizationObject);

public:
    explicit KReadableEvent(KernelCore& kernel);
    ~KReadableEvent() override;

    void Initialize(KEvent* parent);

    KEvent* GetParent() const {
        return m_parent;
    }

    Result Signal();
    Result Clear();

    bool IsSignaled() const override;
    void Destroy() override;

    Result Reset();

private:
    bool m_is_signaled{};
    KEvent* m_parent{};
};

} // namespace Kernel


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

#include "common/assert.h"
#include "common/overflow.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/kernel/k_hardware_timer.h"
#include "core/hle/kernel/k_resource_limit.h"
#include "core/hle/kernel/svc_results.h"

namespace Kernel {
constexpr s64 DefaultTimeout = 10000000000; // 10 seconds

KResourceLimit::KResourceLimit(KernelCore& kernel)
    : KAutoObjectWithSlabHeapAndContainer{kernel}, m_lock{m_kernel}, m_cond_var{m_kernel} {}
KResourceLimit::~KResourceLimit() = default;

void KResourceLimit::Initialize() {}

void KResourceLimit::Finalize() {}

s64 KResourceLimit::GetLimitValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{m_lock};
        value = m_limit_values[index];
        ASSERT(value >= 0);
        ASSERT(m_current_values[index] <= m_limit_values[index]);
        ASSERT(m_current_hints[index] <= m_current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetCurrentValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{m_lock};
        value = m_current_values[index];
        ASSERT(value >= 0);
        ASSERT(m_current_values[index] <= m_limit_values[index]);
        ASSERT(m_current_hints[index] <= m_current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetPeakValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk{m_lock};
        value = m_peak_values[index];
        ASSERT(value >= 0);
        ASSERT(m_current_values[index] <= m_limit_values[index]);
        ASSERT(m_current_hints[index] <= m_current_values[index]);
    }
    return value;
}

s64 KResourceLimit::GetFreeValue(LimitableResource which) const {
    const auto index = static_cast<std::size_t>(which);
    s64 value{};
    {
        KScopedLightLock lk(m_lock);
        ASSERT(m_current_values[index] >= 0);
        ASSERT(m_current_values[index] <= m_limit_values[index]);
        ASSERT(m_current_hints[index] <= m_current_values[index]);
        value = m_limit_values[index] - m_current_values[index];
    }

    return value;
}

Result KResourceLimit::SetLimitValue(LimitableResource which, s64 value) {
    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(m_lock);
    R_UNLESS(m_current_values[index] <= value, ResultInvalidState);

    m_limit_values[index] = value;
    m_peak_values[index] = m_current_values[index];

    R_SUCCEED();
}

bool KResourceLimit::Reserve(LimitableResource which, s64 value) {
    return Reserve(which, value, m_kernel.HardwareTimer().GetTick() + DefaultTimeout);
}

bool KResourceLimit::Reserve(LimitableResource which, s64 value, s64 timeout) {
    ASSERT(value >= 0);
    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(m_lock);

    ASSERT(m_current_hints[index] <= m_current_values[index]);
    if (m_current_hints[index] >= m_limit_values[index]) {
        return false;
    }

    // Loop until we reserve or run out of time.
    while (true) {
        ASSERT(m_current_values[index] <= m_limit_values[index]);
        ASSERT(m_current_hints[index] <= m_current_values[index]);

        // If we would overflow, don't allow to succeed.
        if (Common::WrappingAdd(m_current_values[index], value) <= m_current_values[index]) {
            break;
        }

        if (m_current_values[index] + value <= m_limit_values[index]) {
            m_current_values[index] += value;
            m_current_hints[index] += value;
            m_peak_values[index] = std::max(m_peak_values[index], m_current_values[index]);
            return true;
        }

        if (m_current_hints[index] + value <= m_limit_values[index] &&
            (timeout < 0 || m_kernel.HardwareTimer().GetTick() < timeout)) {
            m_waiter_count++;
            m_cond_var.Wait(std::addressof(m_lock), timeout, false);
            m_waiter_count--;
        } else {
            break;
        }
    }

    return false;
}

void KResourceLimit::Release(LimitableResource which, s64 value) {
    Release(which, value, value);
}

void KResourceLimit::Release(LimitableResource which, s64 value, s64 hint) {
    ASSERT(value >= 0);
    ASSERT(hint >= 0);

    const auto index = static_cast<std::size_t>(which);
    KScopedLightLock lk(m_lock);
    ASSERT(m_current_values[index] <= m_limit_values[index]);
    ASSERT(m_current_hints[index] <= m_current_values[index]);
    ASSERT(value <= m_current_values[index]);
    ASSERT(hint <= m_current_hints[index]);

    m_current_values[index] -= value;
    m_current_hints[index] -= hint;

    if (m_waiter_count != 0) {
        m_cond_var.Broadcast();
    }
}

KResourceLimit* CreateResourceLimitForProcess(Core::System& system, s64 physical_memory_size) {
    auto* resource_limit = KResourceLimit::Create(system.Kernel());
    resource_limit->Initialize();

    // Initialize default resource limit values.
    // TODO(bunnei): These values are the system defaults, the limits for service processes are
    // lower. These should use the correct limit values.

    ASSERT(resource_limit->SetLimitValue(LimitableResource::PhysicalMemoryMax, physical_memory_size)
               .IsSuccess());
    ASSERT(resource_limit->SetLimitValue(LimitableResource::ThreadCountMax, 800).IsSuccess());
    ASSERT(resource_limit->SetLimitValue(LimitableResource::EventCountMax, 900).IsSuccess());
    ASSERT(
        resource_limit->SetLimitValue(LimitableResource::TransferMemoryCountMax, 200).IsSuccess());
    ASSERT(resource_limit->SetLimitValue(LimitableResource::SessionCountMax, 1133).IsSuccess());

    return resource_limit;
}

} // namespace Kernel


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

#pragma once

#include <array>
#include "common/common_types.h"
#include "core/hle/kernel/k_light_condition_variable.h"
#include "core/hle/kernel/k_light_lock.h"

union Result;

namespace Core::Timing {
class CoreTiming;
}

namespace Kernel {
class KernelCore;

using LimitableResource = Svc::LimitableResource;

constexpr bool IsValidResourceType(LimitableResource type) {
    return type < LimitableResource::Count;
}

class KResourceLimit final
    : public KAutoObjectWithSlabHeapAndContainer<KResourceLimit, KAutoObjectWithList> {
    KERNEL_AUTOOBJECT_TRAITS(KResourceLimit, KAutoObject);

public:
    explicit KResourceLimit(KernelCore& kernel);
    ~KResourceLimit() override;

    void Initialize();
    void Finalize() override;

    s64 GetLimitValue(LimitableResource which) const;
    s64 GetCurrentValue(LimitableResource which) const;
    s64 GetPeakValue(LimitableResource which) const;
    s64 GetFreeValue(LimitableResource which) const;

    Result SetLimitValue(LimitableResource which, s64 value);

    bool Reserve(LimitableResource which, s64 value);
    bool Reserve(LimitableResource which, s64 value, s64 timeout);
    void Release(LimitableResource which, s64 value);
    void Release(LimitableResource which, s64 value, s64 hint);

    static void PostDestroy(uintptr_t arg) {}

private:
    using ResourceArray = std::array<s64, static_cast<std::size_t>(LimitableResource::Count)>;
    ResourceArray m_limit_values{};
    ResourceArray m_current_values{};
    ResourceArray m_current_hints{};
    ResourceArray m_peak_values{};
    mutable KLightLock m_lock;
    s32 m_waiter_count{};
    KLightConditionVariable m_cond_var;
};

KResourceLimit* CreateResourceLimitForProcess(Core::System& system, s64 physical_memory_size);

} // namespace Kernel