From 2a7eff57a8048933a89c1a8f8d6dced7b5d604f2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 22:04:28 -0700 Subject: hle: kernel: Rename Process to KProcess. --- src/core/hle/kernel/handle_table.cpp | 2 +- src/core/hle/kernel/hle_ipc.cpp | 2 +- src/core/hle/kernel/hle_ipc.h | 2 +- src/core/hle/kernel/init/init_slab_setup.cpp | 8 +- src/core/hle/kernel/init/init_slab_setup.h | 2 +- src/core/hle/kernel/k_auto_object.h | 4 +- src/core/hle/kernel/k_auto_object_container.cpp | 2 +- src/core/hle/kernel/k_auto_object_container.h | 4 +- src/core/hle/kernel/k_class_token.h | 2 +- src/core/hle/kernel/k_condition_variable.cpp | 2 +- src/core/hle/kernel/k_event.cpp | 4 +- src/core/hle/kernel/k_event.h | 6 +- src/core/hle/kernel/k_page_table.cpp | 2 +- src/core/hle/kernel/k_process.cpp | 505 +++++++++++++++++++++ src/core/hle/kernel/k_process.h | 480 ++++++++++++++++++++ src/core/hle/kernel/k_scheduler.cpp | 16 +- src/core/hle/kernel/k_scheduler.h | 6 +- .../hle/kernel/k_scoped_resource_reservation.h | 6 +- src/core/hle/kernel/k_server_session.cpp | 2 +- src/core/hle/kernel/k_session.cpp | 2 +- src/core/hle/kernel/k_session.h | 2 +- src/core/hle/kernel/k_shared_memory.cpp | 4 +- src/core/hle/kernel/k_shared_memory.h | 8 +- src/core/hle/kernel/k_thread.cpp | 10 +- src/core/hle/kernel/k_thread.h | 16 +- src/core/hle/kernel/k_transfer_memory.cpp | 4 +- src/core/hle/kernel/k_transfer_memory.h | 6 +- src/core/hle/kernel/kernel.cpp | 26 +- src/core/hle/kernel/kernel.h | 18 +- src/core/hle/kernel/process.cpp | 505 --------------------- src/core/hle/kernel/process.h | 479 ------------------- src/core/hle/kernel/svc.cpp | 30 +- 32 files changed, 1084 insertions(+), 1083 deletions(-) create mode 100644 src/core/hle/kernel/k_process.cpp create mode 100644 src/core/hle/kernel/k_process.h delete mode 100644 src/core/hle/kernel/process.cpp delete mode 100644 src/core/hle/kernel/process.h (limited to 'src/core/hle/kernel') diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index cd752da4e..16c528f5b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -7,10 +7,10 @@ #include "common/logging/log.h" #include "core/core.h" #include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_process.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/process.h" #include "core/hle/kernel/svc_results.h" namespace Kernel { diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index a11528f28..69190286d 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -16,6 +16,7 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" @@ -23,7 +24,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h index 7f7ab74dd..4b92ba655 100644 --- a/src/core/hle/kernel/hle_ipc.h +++ b/src/core/hle/kernel/hle_ipc.h @@ -38,7 +38,7 @@ class Domain; class HandleTable; class HLERequestContext; class KernelCore; -class Process; +class KProcess; class KServerSession; class KThread; class KReadableEvent; diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index f8c255732..04e481a0a 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -13,6 +13,7 @@ #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" @@ -20,7 +21,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/memory_types.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel::Init { @@ -28,7 +28,7 @@ namespace Kernel::Init { #define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS #define FOREACH_SLAB_TYPE(HANDLER, ...) \ - HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ + HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ @@ -48,7 +48,7 @@ enum KSlabType : u32 { #undef DEFINE_SLAB_TYPE_ENUM_MEMBER // Constexpr counts. -constexpr size_t SlabCountProcess = 80; +constexpr size_t SlabCountKProcess = 80; constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKInterruptEvent = 100; @@ -69,7 +69,7 @@ constexpr size_t SlabCountExtraKThread = 160; // Global to hold our resource counts. KSlabResourceCounts g_slab_resource_counts = { - .num_Process = SlabCountProcess, + .num_KProcess = SlabCountKProcess, .num_KThread = SlabCountKThread, .num_KEvent = SlabCountKEvent, .num_KInterruptEvent = SlabCountKInterruptEvent, diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 8876678b3..6418b97ac 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -15,7 +15,7 @@ class KMemoryLayout; namespace Kernel::Init { struct KSlabResourceCounts { - size_t num_Process; + size_t num_KProcess; size_t num_KThread; size_t num_KEvent; size_t num_KInterruptEvent; diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h index fd6405a0e..5a180b7dc 100644 --- a/src/core/hle/kernel/k_auto_object.h +++ b/src/core/hle/kernel/k_auto_object.h @@ -15,7 +15,7 @@ namespace Kernel { class KernelCore; -class Process; +class KProcess; using Handle = u32; @@ -106,7 +106,7 @@ public: // Finalize is responsible for cleaning up resource, but does not destroy the object. virtual void Finalize() {} - virtual Process* GetOwner() const { + virtual KProcess* GetOwner() const { return nullptr; } diff --git a/src/core/hle/kernel/k_auto_object_container.cpp b/src/core/hle/kernel/k_auto_object_container.cpp index 9ba8a54c7..85d03ebe3 100644 --- a/src/core/hle/kernel/k_auto_object_container.cpp +++ b/src/core/hle/kernel/k_auto_object_container.cpp @@ -18,7 +18,7 @@ void KAutoObjectWithListContainer::Unregister(KAutoObjectWithList* obj) { m_object_list.erase(m_object_list.iterator_to(*obj)); } -size_t KAutoObjectWithListContainer::GetOwnedCount(Process* owner) { +size_t KAutoObjectWithListContainer::GetOwnedCount(KProcess* owner) { KScopedLightLock lk(m_lock); size_t count = 0; diff --git a/src/core/hle/kernel/k_auto_object_container.h b/src/core/hle/kernel/k_auto_object_container.h index 4b599b7c3..6d1cd4862 100644 --- a/src/core/hle/kernel/k_auto_object_container.h +++ b/src/core/hle/kernel/k_auto_object_container.h @@ -16,7 +16,7 @@ namespace Kernel { class KernelCore; -class Process; +class KProcess; class KAutoObjectWithListContainer { NON_COPYABLE(KAutoObjectWithListContainer); @@ -66,7 +66,7 @@ public: void Register(KAutoObjectWithList* obj); void Unregister(KAutoObjectWithList* obj); - size_t GetOwnedCount(Process* owner); + size_t GetOwnedCount(KProcess* owner); }; } // namespace Kernel diff --git a/src/core/hle/kernel/k_class_token.h b/src/core/hle/kernel/k_class_token.h index 89b80a341..fb4307cd0 100644 --- a/src/core/hle/kernel/k_class_token.h +++ b/src/core/hle/kernel/k_class_token.h @@ -97,7 +97,7 @@ public: KServerSession, KClientPort, KClientSession, - Process, + KProcess, KResourceLimit, KLightSession, KPort, diff --git a/src/core/hle/kernel/k_condition_variable.cpp b/src/core/hle/kernel/k_condition_variable.cpp index 72565af05..a9738f7ce 100644 --- a/src/core/hle/kernel/k_condition_variable.cpp +++ b/src/core/hle/kernel/k_condition_variable.cpp @@ -8,12 +8,12 @@ #include "core/core.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_linked_list.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_synchronization_object.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_common.h" #include "core/hle/kernel/svc_results.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/k_event.cpp b/src/core/hle/kernel/k_event.cpp index fdec0c36f..986355b78 100644 --- a/src/core/hle/kernel/k_event.cpp +++ b/src/core/hle/kernel/k_event.cpp @@ -3,8 +3,8 @@ // Refer to the license.txt file included. #include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -45,7 +45,7 @@ void KEvent::Finalize() { void KEvent::PostDestroy(uintptr_t arg) { // Release the event count resource the owner process holds. - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); if (owner) { owner->GetResourceLimit()->Release(LimitableResource::Events, 1); owner->Close(); diff --git a/src/core/hle/kernel/k_event.h b/src/core/hle/kernel/k_event.h index f0b89f882..4ca869930 100644 --- a/src/core/hle/kernel/k_event.h +++ b/src/core/hle/kernel/k_event.h @@ -13,7 +13,7 @@ namespace Kernel { class KernelCore; class KReadableEvent; class KWritableEvent; -class Process; +class KProcess; class KEvent final : public KAutoObjectWithSlabHeapAndContainer { KERNEL_AUTOOBJECT_TRAITS(KEvent, KAutoObject); @@ -36,7 +36,7 @@ public: static void PostDestroy(uintptr_t arg); - virtual Process* GetOwner() const override { + virtual KProcess* GetOwner() const override { return owner; } @@ -51,7 +51,7 @@ public: private: KReadableEvent readable_event; KWritableEvent writable_event; - Process* owner{}; + KProcess* owner{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_page_table.cpp b/src/core/hle/kernel/k_page_table.cpp index 5f60b95cd..2f33cb6c1 100644 --- a/src/core/hle/kernel/k_page_table.cpp +++ b/src/core/hle/kernel/k_page_table.cpp @@ -11,11 +11,11 @@ #include "core/hle/kernel/k_memory_block_manager.h" #include "core/hle/kernel/k_page_linked_list.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scoped_resource_reservation.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/memory.h" diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp new file mode 100644 index 000000000..edc3b5175 --- /dev/null +++ b/src/core/hle/kernel/k_process.cpp @@ -0,0 +1,505 @@ +// Copyright 2015 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include +#include +#include +#include "common/alignment.h" +#include "common/assert.h" +#include "common/logging/log.h" +#include "common/settings.h" +#include "core/core.h" +#include "core/device_memory.h" +#include "core/file_sys/program_metadata.h" +#include "core/hle/kernel/code_set.h" +#include "core/hle/kernel/k_memory_block_manager.h" +#include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" +#include "core/hle/kernel/k_resource_limit.h" +#include "core/hle/kernel/k_scheduler.h" +#include "core/hle/kernel/k_scoped_resource_reservation.h" +#include "core/hle/kernel/k_slab_heap.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/kernel.h" +#include "core/hle/kernel/svc_results.h" +#include "core/hle/lock.h" +#include "core/memory.h" + +namespace Kernel { +namespace { +/** + * Sets up the primary application thread + * + * @param system The system instance to create the main thread under. + * @param owner_process The parent process for the main thread + * @param priority The priority to give the main thread + */ +void SetupMainThread(Core::System& system, KProcess& owner_process, u32 priority, VAddr stack_top) { + const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart(); + ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); + + KThread* thread = KThread::Create(system.Kernel()); + ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority, + owner_process.GetIdealCoreId(), &owner_process) + .IsSuccess()); + + // Register 1 must be a handle to the main thread + Handle thread_handle{}; + owner_process.GetHandleTable().Add(&thread_handle, thread); + + thread->SetName("main"); + thread->GetContext32().cpu_registers[0] = 0; + thread->GetContext64().cpu_registers[0] = 0; + thread->GetContext32().cpu_registers[1] = thread_handle; + thread->GetContext64().cpu_registers[1] = thread_handle; + + auto& kernel = system.Kernel(); + // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires + { + KScopedSchedulerLock lock{kernel}; + thread->SetState(ThreadState::Runnable); + } +} +} // Anonymous namespace + +// Represents a page used for thread-local storage. +// +// Each TLS page contains slots that may be used by processes and threads. +// Every process and thread is created with a slot in some arbitrary page +// (whichever page happens to have an available slot). +class TLSPage { +public: + static constexpr std::size_t num_slot_entries = + Core::Memory::PAGE_SIZE / Core::Memory::TLS_ENTRY_SIZE; + + explicit TLSPage(VAddr address) : base_address{address} {} + + bool HasAvailableSlots() const { + return !is_slot_used.all(); + } + + VAddr GetBaseAddress() const { + return base_address; + } + + std::optional ReserveSlot() { + for (std::size_t i = 0; i < is_slot_used.size(); i++) { + if (is_slot_used[i]) { + continue; + } + + is_slot_used[i] = true; + return base_address + (i * Core::Memory::TLS_ENTRY_SIZE); + } + + return std::nullopt; + } + + void ReleaseSlot(VAddr address) { + // Ensure that all given addresses are consistent with how TLS pages + // are intended to be used when releasing slots. + ASSERT(IsWithinPage(address)); + ASSERT((address % Core::Memory::TLS_ENTRY_SIZE) == 0); + + const std::size_t index = (address - base_address) / Core::Memory::TLS_ENTRY_SIZE; + is_slot_used[index] = false; + } + +private: + bool IsWithinPage(VAddr address) const { + return base_address <= address && address < base_address + Core::Memory::PAGE_SIZE; + } + + VAddr base_address; + std::bitset is_slot_used; +}; + +ResultCode KProcess::Initialize(KProcess* process, Core::System& system, std::string name, + ProcessType type) { + auto& kernel = system.Kernel(); + + process->name = std::move(name); + + process->resource_limit = kernel.GetSystemResourceLimit(); + process->status = ProcessStatus::Created; + process->program_id = 0; + process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID() + : kernel.CreateNewUserProcessID(); + process->capabilities.InitializeForMetadatalessProcess(); + process->is_initialized = true; + + std::mt19937 rng(Settings::values.rng_seed.GetValue().value_or(std::time(nullptr))); + std::uniform_int_distribution distribution; + std::generate(process->random_entropy.begin(), process->random_entropy.end(), + [&] { return distribution(rng); }); + + kernel.AppendNewProcess(process); + + // Open a reference to the resource limit. + process->resource_limit->Open(); + + return RESULT_SUCCESS; +} + +KResourceLimit* KProcess::GetResourceLimit() const { + return resource_limit; +} + +void KProcess::IncrementThreadCount() { + ASSERT(num_threads >= 0); + num_created_threads++; + + if (const auto count = ++num_threads; count > peak_num_threads) { + peak_num_threads = count; + } +} + +void KProcess::DecrementThreadCount() { + ASSERT(num_threads > 0); + + if (const auto count = --num_threads; count == 0) { + UNIMPLEMENTED_MSG("Process termination is not implemented!"); + } +} + +u64 KProcess::GetTotalPhysicalMemoryAvailable() const { + const u64 capacity{resource_limit->GetFreeValue(LimitableResource::PhysicalMemory) + + page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + + main_thread_stack_size}; + if (const auto pool_size = kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application); + capacity != pool_size) { + LOG_WARNING(Kernel, "capacity {} != application pool size {}", capacity, pool_size); + } + if (capacity < memory_usage_capacity) { + return capacity; + } + return memory_usage_capacity; +} + +u64 KProcess::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const { + return GetTotalPhysicalMemoryAvailable() - GetSystemResourceSize(); +} + +u64 KProcess::GetTotalPhysicalMemoryUsed() const { + return image_size + main_thread_stack_size + page_table->GetTotalHeapSize() + + GetSystemResourceSize(); +} + +u64 KProcess::GetTotalPhysicalMemoryUsedWithoutSystemResource() const { + return GetTotalPhysicalMemoryUsed() - GetSystemResourceUsage(); +} + +bool KProcess::ReleaseUserException(KThread* thread) { + KScopedSchedulerLock sl{kernel}; + + if (exception_thread == thread) { + exception_thread = nullptr; + + // Remove waiter thread. + s32 num_waiters{}; + KThread* next = thread->RemoveWaiterByKey( + std::addressof(num_waiters), + reinterpret_cast(std::addressof(exception_thread))); + if (next != nullptr) { + if (next->GetState() == ThreadState::Waiting) { + next->SetState(ThreadState::Runnable); + } else { + KScheduler::SetSchedulerUpdateNeeded(kernel); + } + } + + return true; + } else { + return false; + } +} + +void KProcess::PinCurrentThread() { + ASSERT(kernel.GlobalSchedulerContext().IsLocked()); + + // Get the current thread. + const s32 core_id = GetCurrentCoreId(kernel); + KThread* cur_thread = GetCurrentThreadPointer(kernel); + + // Pin it. + PinThread(core_id, cur_thread); + cur_thread->Pin(); + + // An update is needed. + KScheduler::SetSchedulerUpdateNeeded(kernel); +} + +void KProcess::UnpinCurrentThread() { + ASSERT(kernel.GlobalSchedulerContext().IsLocked()); + + // Get the current thread. + const s32 core_id = GetCurrentCoreId(kernel); + KThread* cur_thread = GetCurrentThreadPointer(kernel); + + // Unpin it. + cur_thread->Unpin(); + UnpinThread(core_id, cur_thread); + + // An update is needed. + KScheduler::SetSchedulerUpdateNeeded(kernel); +} + +void KProcess::RegisterThread(const KThread* thread) { + thread_list.push_back(thread); +} + +void KProcess::UnregisterThread(const KThread* thread) { + thread_list.remove(thread); +} + +ResultCode KProcess::Reset() { + // Lock the process and the scheduler. + KScopedLightLock lk(state_lock); + KScopedSchedulerLock sl{kernel}; + + // Validate that we're in a state that we can reset. + R_UNLESS(status != ProcessStatus::Exited, ResultInvalidState); + R_UNLESS(is_signaled, ResultInvalidState); + + // Clear signaled. + is_signaled = false; + return RESULT_SUCCESS; +} + +ResultCode KProcess::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, + std::size_t code_size) { + program_id = metadata.GetTitleID(); + ideal_core = metadata.GetMainThreadCore(); + is_64bit_process = metadata.Is64BitProgram(); + system_resource_size = metadata.GetSystemResourceSize(); + image_size = code_size; + + KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory, + code_size + system_resource_size); + if (!memory_reservation.Succeeded()) { + LOG_ERROR(Kernel, "Could not reserve process memory requirements of size {:X} bytes", + code_size + system_resource_size); + return ResultLimitReached; + } + // Initialize proces address space + if (const ResultCode result{ + page_table->InitializeForProcess(metadata.GetAddressSpaceType(), false, 0x8000000, + code_size, KMemoryManager::Pool::Application)}; + result.IsError()) { + return result; + } + + // Map process code region + if (const ResultCode result{page_table->MapProcessCode(page_table->GetCodeRegionStart(), + code_size / PageSize, KMemoryState::Code, + KMemoryPermission::None)}; + result.IsError()) { + return result; + } + + // Initialize process capabilities + const auto& caps{metadata.GetKernelCapabilities()}; + if (const ResultCode result{ + capabilities.InitializeForUserProcess(caps.data(), caps.size(), *page_table)}; + result.IsError()) { + return result; + } + + // Set memory usage capacity + switch (metadata.GetAddressSpaceType()) { + case FileSys::ProgramAddressSpaceType::Is32Bit: + case FileSys::ProgramAddressSpaceType::Is36Bit: + case FileSys::ProgramAddressSpaceType::Is39Bit: + memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart(); + break; + + case FileSys::ProgramAddressSpaceType::Is32BitNoMap: + memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart() + + page_table->GetAliasRegionEnd() - page_table->GetAliasRegionStart(); + break; + + default: + UNREACHABLE(); + } + + // Create TLS region + tls_region_address = CreateTLSRegion(); + memory_reservation.Commit(); + + return handle_table.SetSize(capabilities.GetHandleTableSize()); +} + +void KProcess::Run(s32 main_thread_priority, u64 stack_size) { + AllocateMainThreadStack(stack_size); + resource_limit->Reserve(LimitableResource::Threads, 1); + resource_limit->Reserve(LimitableResource::PhysicalMemory, main_thread_stack_size); + + const std::size_t heap_capacity{memory_usage_capacity - main_thread_stack_size - image_size}; + ASSERT(!page_table->SetHeapCapacity(heap_capacity).IsError()); + + ChangeStatus(ProcessStatus::Running); + + SetupMainThread(kernel.System(), *this, main_thread_priority, main_thread_stack_top); +} + +void KProcess::PrepareForTermination() { + ChangeStatus(ProcessStatus::Exiting); + + const auto stop_threads = [this](const std::vector& thread_list) { + for (auto& thread : thread_list) { + if (thread->GetOwnerProcess() != this) + continue; + + if (thread == kernel.CurrentScheduler()->GetCurrentThread()) + continue; + + // TODO(Subv): When are the other running/ready threads terminated? + ASSERT_MSG(thread->GetState() == ThreadState::Waiting, + "Exiting processes with non-waiting threads is currently unimplemented"); + + thread->Exit(); + } + }; + + stop_threads(kernel.System().GlobalSchedulerContext().GetThreadList()); + + FreeTLSRegion(tls_region_address); + tls_region_address = 0; + + if (resource_limit) { + resource_limit->Release(LimitableResource::PhysicalMemory, + main_thread_stack_size + image_size); + } + + ChangeStatus(ProcessStatus::Exited); +} + +void KProcess::Finalize() { + // Release memory to the resource limit. + if (resource_limit != nullptr) { + resource_limit->Close(); + } + + // Perform inherited finalization. + KAutoObjectWithSlabHeapAndContainer::Finalize(); +} + +/** + * Attempts to find a TLS page that contains a free slot for + * use by a thread. + * + * @returns If a page with an available slot is found, then an iterator + * pointing to the page is returned. Otherwise the end iterator + * is returned instead. + */ +static auto FindTLSPageWithAvailableSlots(std::vector& tls_pages) { + return std::find_if(tls_pages.begin(), tls_pages.end(), + [](const auto& page) { return page.HasAvailableSlots(); }); +} + +VAddr KProcess::CreateTLSRegion() { + KScopedSchedulerLock lock(kernel); + if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)}; + tls_page_iter != tls_pages.cend()) { + return *tls_page_iter->ReserveSlot(); + } + + Page* const tls_page_ptr{kernel.GetUserSlabHeapPages().Allocate()}; + ASSERT(tls_page_ptr); + + const VAddr start{page_table->GetKernelMapRegionStart()}; + const VAddr size{page_table->GetKernelMapRegionEnd() - start}; + const PAddr tls_map_addr{kernel.System().DeviceMemory().GetPhysicalAddr(tls_page_ptr)}; + const VAddr tls_page_addr{page_table + ->AllocateAndMapMemory(1, PageSize, true, start, size / PageSize, + KMemoryState::ThreadLocal, + KMemoryPermission::ReadAndWrite, + tls_map_addr) + .ValueOr(0)}; + + ASSERT(tls_page_addr); + + std::memset(tls_page_ptr, 0, PageSize); + tls_pages.emplace_back(tls_page_addr); + + const auto reserve_result{tls_pages.back().ReserveSlot()}; + ASSERT(reserve_result.has_value()); + + return *reserve_result; +} + +void KProcess::FreeTLSRegion(VAddr tls_address) { + KScopedSchedulerLock lock(kernel); + const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE); + auto iter = + std::find_if(tls_pages.begin(), tls_pages.end(), [aligned_address](const auto& page) { + return page.GetBaseAddress() == aligned_address; + }); + + // Something has gone very wrong if we're freeing a region + // with no actual page available. + ASSERT(iter != tls_pages.cend()); + + iter->ReleaseSlot(tls_address); +} + +void KProcess::LoadModule(CodeSet code_set, VAddr base_addr) { + std::lock_guard lock{HLE::g_hle_lock}; + const auto ReprotectSegment = [&](const CodeSet::Segment& segment, + KMemoryPermission permission) { + page_table->SetCodeMemoryPermission(segment.addr + base_addr, segment.size, permission); + }; + + kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), + code_set.memory.size()); + + ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); + ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); + ReprotectSegment(code_set.DataSegment(), KMemoryPermission::ReadAndWrite); +} + +bool KProcess::IsSignaled() const { + ASSERT(kernel.GlobalSchedulerContext().IsLocked()); + return is_signaled; +} + +KProcess::KProcess(KernelCore& kernel) + : KAutoObjectWithSlabHeapAndContainer{kernel}, + page_table{std::make_unique(kernel.System())}, handle_table{kernel}, + address_arbiter{kernel.System()}, condition_var{kernel.System()}, state_lock{kernel} {} + +KProcess::~KProcess() = default; + +void KProcess::ChangeStatus(ProcessStatus new_status) { + if (status == new_status) { + return; + } + + status = new_status; + is_signaled = true; + NotifyAvailable(); +} + +ResultCode KProcess::AllocateMainThreadStack(std::size_t stack_size) { + ASSERT(stack_size); + + // The kernel always ensures that the given stack size is page aligned. + main_thread_stack_size = Common::AlignUp(stack_size, PageSize); + + const VAddr start{page_table->GetStackRegionStart()}; + const std::size_t size{page_table->GetStackRegionEnd() - start}; + + CASCADE_RESULT(main_thread_stack_top, + page_table->AllocateAndMapMemory( + main_thread_stack_size / PageSize, PageSize, false, start, size / PageSize, + KMemoryState::Stack, KMemoryPermission::ReadAndWrite)); + + main_thread_stack_top += main_thread_stack_size; + + return RESULT_SUCCESS; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_process.h b/src/core/hle/kernel/k_process.h new file mode 100644 index 000000000..961c0d9ba --- /dev/null +++ b/src/core/hle/kernel/k_process.h @@ -0,0 +1,480 @@ +// Copyright 2015 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include +#include +#include "common/common_types.h" +#include "core/hle/kernel/handle_table.h" +#include "core/hle/kernel/k_address_arbiter.h" +#include "core/hle/kernel/k_auto_object.h" +#include "core/hle/kernel/k_condition_variable.h" +#include "core/hle/kernel/k_synchronization_object.h" +#include "core/hle/kernel/process_capability.h" +#include "core/hle/kernel/slab_helpers.h" +#include "core/hle/result.h" + +namespace Core { +class System; +} + +namespace FileSys { +class ProgramMetadata; +} + +namespace Kernel { + +class KernelCore; +class KPageTable; +class KResourceLimit; +class KThread; +class TLSPage; + +struct CodeSet; + +enum class MemoryRegion : u16 { + APPLICATION = 1, + SYSTEM = 2, + BASE = 3, +}; + +/** + * Indicates the status of a Process instance. + * + * @note These match the values as used by kernel, + * so new entries should only be added if RE + * shows that a new value has been introduced. + */ +enum class ProcessStatus { + Created, + CreatedWithDebuggerAttached, + Running, + WaitingForDebuggerToAttach, + DebuggerAttached, + Exiting, + Exited, + DebugBreak, +}; + +class KProcess final + : public KAutoObjectWithSlabHeapAndContainer { + KERNEL_AUTOOBJECT_TRAITS(KProcess, KSynchronizationObject); + +public: + explicit KProcess(KernelCore& kernel); + ~KProcess() override; + + enum : u64 { + /// Lowest allowed process ID for a kernel initial process. + InitialKIPIDMin = 1, + /// Highest allowed process ID for a kernel initial process. + InitialKIPIDMax = 80, + + /// Lowest allowed process ID for a userland process. + ProcessIDMin = 81, + /// Highest allowed process ID for a userland process. + ProcessIDMax = 0xFFFFFFFFFFFFFFFF, + }; + + // Used to determine how process IDs are assigned. + enum class ProcessType { + KernelInternal, + Userland, + }; + + static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4; + + static ResultCode Initialize(KProcess* process, Core::System& system, std::string name, + ProcessType type); + + /// Gets a reference to the process' page table. + KPageTable& PageTable() { + return *page_table; + } + + /// Gets const a reference to the process' page table. + const KPageTable& PageTable() const { + return *page_table; + } + + /// Gets a reference to the process' handle table. + HandleTable& GetHandleTable() { + return handle_table; + } + + /// Gets a const reference to the process' handle table. + const HandleTable& GetHandleTable() const { + return handle_table; + } + + ResultCode SignalToAddress(VAddr address) { + return condition_var.SignalToAddress(address); + } + + ResultCode WaitForAddress(Handle handle, VAddr address, u32 tag) { + return condition_var.WaitForAddress(handle, address, tag); + } + + void SignalConditionVariable(u64 cv_key, int32_t count) { + return condition_var.Signal(cv_key, count); + } + + ResultCode WaitConditionVariable(VAddr address, u64 cv_key, u32 tag, s64 ns) { + return condition_var.Wait(address, cv_key, tag, ns); + } + + ResultCode SignalAddressArbiter(VAddr address, Svc::SignalType signal_type, s32 value, + s32 count) { + return address_arbiter.SignalToAddress(address, signal_type, value, count); + } + + ResultCode WaitAddressArbiter(VAddr address, Svc::ArbitrationType arb_type, s32 value, + s64 timeout) { + return address_arbiter.WaitForAddress(address, arb_type, value, timeout); + } + + /// Gets the address to the process' dedicated TLS region. + VAddr GetTLSRegionAddress() const { + return tls_region_address; + } + + /// Gets the current status of the process + ProcessStatus GetStatus() const { + return status; + } + + /// Gets the unique ID that identifies this particular process. + u64 GetProcessID() const { + return process_id; + } + + /// Gets the title ID corresponding to this process. + u64 GetTitleID() const { + return program_id; + } + + /// Gets the resource limit descriptor for this process + KResourceLimit* GetResourceLimit() const; + + /// Gets the ideal CPU core ID for this process + u8 GetIdealCoreId() const { + return ideal_core; + } + + /// Checks if the specified thread priority is valid. + bool CheckThreadPriority(s32 prio) const { + return ((1ULL << prio) & GetPriorityMask()) != 0; + } + + /// Gets the bitmask of allowed cores that this process' threads can run on. + u64 GetCoreMask() const { + return capabilities.GetCoreMask(); + } + + /// Gets the bitmask of allowed thread priorities. + u64 GetPriorityMask() const { + return capabilities.GetPriorityMask(); + } + + /// Gets the amount of secure memory to allocate for memory management. + u32 GetSystemResourceSize() const { + return system_resource_size; + } + + /// Gets the amount of secure memory currently in use for memory management. + u32 GetSystemResourceUsage() const { + // On hardware, this returns the amount of system resource memory that has + // been used by the kernel. This is problematic for Yuzu to emulate, because + // system resource memory is used for page tables -- and yuzu doesn't really + // have a way to calculate how much memory is required for page tables for + // the current process at any given time. + // TODO: Is this even worth implementing? Games may retrieve this value via + // an SDK function that gets used + available system resource size for debug + // or diagnostic purposes. However, it seems unlikely that a game would make + // decisions based on how much system memory is dedicated to its page tables. + // Is returning a value other than zero wise? + return 0; + } + + /// Whether this process is an AArch64 or AArch32 process. + bool Is64BitProcess() const { + return is_64bit_process; + } + + [[nodiscard]] bool IsSuspended() const { + return is_suspended; + } + + void SetSuspended(bool suspended) { + is_suspended = suspended; + } + + /// Gets the total running time of the process instance in ticks. + u64 GetCPUTimeTicks() const { + return total_process_running_time_ticks; + } + + /// Updates the total running time, adding the given ticks to it. + void UpdateCPUTimeTicks(u64 ticks) { + total_process_running_time_ticks += ticks; + } + + /// Gets the process schedule count, used for thread yelding + s64 GetScheduledCount() const { + return schedule_count; + } + + /// Increments the process schedule count, used for thread yielding. + void IncrementScheduledCount() { + ++schedule_count; + } + + void IncrementThreadCount(); + void DecrementThreadCount(); + + void SetRunningThread(s32 core, KThread* thread, u64 idle_count) { + running_threads[core] = thread; + running_thread_idle_counts[core] = idle_count; + } + + void ClearRunningThread(KThread* thread) { + for (size_t i = 0; i < running_threads.size(); ++i) { + if (running_threads[i] == thread) { + running_threads[i] = nullptr; + } + } + } + + [[nodiscard]] KThread* GetRunningThread(s32 core) const { + return running_threads[core]; + } + + bool ReleaseUserException(KThread* thread); + + [[nodiscard]] KThread* GetPinnedThread(s32 core_id) const { + ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); + return pinned_threads[core_id]; + } + + /// Gets 8 bytes of random data for svcGetInfo RandomEntropy + u64 GetRandomEntropy(std::size_t index) const { + return random_entropy.at(index); + } + + /// Retrieves the total physical memory available to this process in bytes. + u64 GetTotalPhysicalMemoryAvailable() const; + + /// Retrieves the total physical memory available to this process in bytes, + /// without the size of the personal system resource heap added to it. + u64 GetTotalPhysicalMemoryAvailableWithoutSystemResource() const; + + /// Retrieves the total physical memory used by this process in bytes. + u64 GetTotalPhysicalMemoryUsed() const; + + /// Retrieves the total physical memory used by this process in bytes, + /// without the size of the personal system resource heap added to it. + u64 GetTotalPhysicalMemoryUsedWithoutSystemResource() const; + + /// Gets the list of all threads created with this process as their owner. + const std::list& GetThreadList() const { + return thread_list; + } + + /// Registers a thread as being created under this process, + /// adding it to this process' thread list. + void RegisterThread(const KThread* thread); + + /// Unregisters a thread from this process, removing it + /// from this process' thread list. + void UnregisterThread(const KThread* thread); + + /// Clears the signaled state of the process if and only if it's signaled. + /// + /// @pre The process must not be already terminated. If this is called on a + /// terminated process, then ERR_INVALID_STATE will be returned. + /// + /// @pre The process must be in a signaled state. If this is called on a + /// process instance that is not signaled, ERR_INVALID_STATE will be + /// returned. + ResultCode Reset(); + + /** + * Loads process-specifics configuration info with metadata provided + * by an executable. + * + * @param metadata The provided metadata to load process specific info from. + * + * @returns RESULT_SUCCESS if all relevant metadata was able to be + * loaded and parsed. Otherwise, an error code is returned. + */ + ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size); + + /** + * Starts the main application thread for this process. + * + * @param main_thread_priority The priority for the main thread. + * @param stack_size The stack size for the main thread in bytes. + */ + void Run(s32 main_thread_priority, u64 stack_size); + + /** + * Prepares a process for termination by stopping all of its threads + * and clearing any other resources. + */ + void PrepareForTermination(); + + void LoadModule(CodeSet code_set, VAddr base_addr); + + virtual bool IsInitialized() const override { + return is_initialized; + } + + static void PostDestroy([[maybe_unused]] uintptr_t arg) {} + + virtual void Finalize(); + + virtual u64 GetId() const override final { + return GetProcessID(); + } + + virtual bool IsSignaled() const override; + + void PinCurrentThread(); + void UnpinCurrentThread(); + + KLightLock& GetStateLock() { + return state_lock; + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // Thread-local storage management + + // Marks the next available region as used and returns the address of the slot. + [[nodiscard]] VAddr CreateTLSRegion(); + + // Frees a used TLS slot identified by the given address + void FreeTLSRegion(VAddr tls_address); + +private: + void PinThread(s32 core_id, KThread* thread) { + ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); + ASSERT(thread != nullptr); + ASSERT(pinned_threads[core_id] == nullptr); + pinned_threads[core_id] = thread; + } + + void UnpinThread(s32 core_id, KThread* thread) { + ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); + ASSERT(thread != nullptr); + ASSERT(pinned_threads[core_id] == thread); + pinned_threads[core_id] = nullptr; + } + + /// Changes the process status. If the status is different + /// from the current process status, then this will trigger + /// a process signal. + void ChangeStatus(ProcessStatus new_status); + + /// Allocates the main thread stack for the process, given the stack size in bytes. + ResultCode AllocateMainThreadStack(std::size_t stack_size); + + /// Memory manager for this process + std::unique_ptr page_table; + + /// Current status of the process + ProcessStatus status{}; + + /// The ID of this process + u64 process_id = 0; + + /// Title ID corresponding to the process + u64 program_id = 0; + + /// Specifies additional memory to be reserved for the process's memory management by the + /// system. When this is non-zero, secure memory is allocated and used for page table allocation + /// instead of using the normal global page tables/memory block management. + u32 system_resource_size = 0; + + /// Resource limit descriptor for this process + KResourceLimit* resource_limit{}; + + /// The ideal CPU core for this process, threads are scheduled on this core by default. + u8 ideal_core = 0; + + /// The Thread Local Storage area is allocated as processes create threads, + /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part + /// holds the TLS for a specific thread. This vector contains which parts are in use for each + /// page as a bitmask. + /// This vector will grow as more pages are allocated for new threads. + std::vector tls_pages; + + /// Contains the parsed process capability descriptors. + ProcessCapabilities capabilities; + + /// Whether or not this process is AArch64, or AArch32. + /// By default, we currently assume this is true, unless otherwise + /// specified by metadata provided to the process during loading. + bool is_64bit_process = true; + + /// Total running time for the process in ticks. + u64 total_process_running_time_ticks = 0; + + /// Per-process handle table for storing created object handles in. + HandleTable handle_table; + + /// Per-process address arbiter. + KAddressArbiter address_arbiter; + + /// The per-process mutex lock instance used for handling various + /// forms of services, such as lock arbitration, and condition + /// variable related facilities. + KConditionVariable condition_var; + + /// Address indicating the location of the process' dedicated TLS region. + VAddr tls_region_address = 0; + + /// Random values for svcGetInfo RandomEntropy + std::array random_entropy{}; + + /// List of threads that are running with this process as their owner. + std::list thread_list; + + /// Address of the top of the main thread's stack + VAddr main_thread_stack_top{}; + + /// Size of the main thread's stack + std::size_t main_thread_stack_size{}; + + /// Memory usage capacity for the process + std::size_t memory_usage_capacity{}; + + /// Process total image size + std::size_t image_size{}; + + /// Schedule count of this process + s64 schedule_count{}; + + bool is_signaled{}; + bool is_suspended{}; + bool is_initialized{}; + + std::atomic num_created_threads{}; + std::atomic num_threads{}; + u16 peak_num_threads{}; + + std::array running_threads{}; + std::array running_thread_idle_counts{}; + std::array pinned_threads{}; + + KThread* exception_thread{}; + + KLightLock state_lock; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 38c6b50fa..0115fe6d1 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -15,12 +15,12 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/cpu_manager.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/time_manager.h" namespace Kernel { @@ -71,7 +71,7 @@ u64 KScheduler::UpdateHighestPriorityThread(KThread* highest_thread) { } if (state.should_count_idle) { if (highest_thread != nullptr) { - if (Process* process = highest_thread->GetOwnerProcess(); process != nullptr) { + if (KProcess* process = highest_thread->GetOwnerProcess(); process != nullptr) { process->SetRunningThread(core_id, highest_thread, state.idle_count); } } else { @@ -104,7 +104,7 @@ u64 KScheduler::UpdateHighestPriorityThreadsImpl(KernelCore& kernel) { if (top_thread != nullptr) { // If the thread has no waiters, we need to check if the process has a thread pinned. if (top_thread->GetNumKernelWaiters() == 0) { - if (Process* parent = top_thread->GetOwnerProcess(); parent != nullptr) { + if (KProcess* parent = top_thread->GetOwnerProcess(); parent != nullptr) { if (KThread* pinned = parent->GetPinnedThread(static_cast(core_id)); pinned != nullptr && pinned != top_thread) { // We prefer our parent's pinned thread if possible. However, we also don't @@ -411,7 +411,7 @@ void KScheduler::YieldWithoutCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -450,7 +450,7 @@ void KScheduler::YieldWithCoreMigration(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -538,7 +538,7 @@ void KScheduler::YieldToAnyThread(KernelCore& kernel) { // Get the current thread and process. KThread& cur_thread = Kernel::GetCurrentThread(kernel); - Process& cur_process = *kernel.CurrentProcess(); + KProcess& cur_process = *kernel.CurrentProcess(); // If the thread's yield count matches, there's nothing for us to do. if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) { @@ -724,7 +724,7 @@ void KScheduler::ScheduleImpl() { current_thread.store(next_thread); - Process* const previous_process = system.Kernel().CurrentProcess(); + KProcess* const previous_process = system.Kernel().CurrentProcess(); UpdateLastContextSwitchTime(previous_thread, previous_process); @@ -780,7 +780,7 @@ void KScheduler::SwitchToCurrent() { } } -void KScheduler::UpdateLastContextSwitchTime(KThread* thread, Process* process) { +void KScheduler::UpdateLastContextSwitchTime(KThread* thread, KProcess* process) { const u64 prev_switch_ticks = last_context_switch_time; const u64 most_recent_switch_ticks = system.CoreTiming().GetCPUTicks(); const u64 update_ticks = most_recent_switch_ticks - prev_switch_ticks; diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 01387b892..282c1863b 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -24,7 +24,7 @@ class System; namespace Kernel { class KernelCore; -class Process; +class KProcess; class SchedulerLock; class KThread; @@ -165,7 +165,7 @@ private: * most recent tick count retrieved. No special arithmetic is * applied to it. */ - void UpdateLastContextSwitchTime(KThread* thread, Process* process); + void UpdateLastContextSwitchTime(KThread* thread, KProcess* process); static void OnSwitch(void* this_scheduler); void SwitchToCurrent(); @@ -197,7 +197,7 @@ private: class [[nodiscard]] KScopedSchedulerLock : KScopedLock { public: - explicit KScopedSchedulerLock(KernelCore & kernel); + explicit KScopedSchedulerLock(KernelCore& kernel); ~KScopedSchedulerLock(); }; diff --git a/src/core/hle/kernel/k_scoped_resource_reservation.h b/src/core/hle/kernel/k_scoped_resource_reservation.h index b160587c5..07272075d 100644 --- a/src/core/hle/kernel/k_scoped_resource_reservation.h +++ b/src/core/hle/kernel/k_scoped_resource_reservation.h @@ -8,8 +8,8 @@ #pragma once #include "common/common_types.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -33,10 +33,10 @@ public: } } - explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v, s64 t) + explicit KScopedResourceReservation(const KProcess* p, LimitableResource r, s64 v, s64 t) : KScopedResourceReservation(p->GetResourceLimit(), r, v, t) {} - explicit KScopedResourceReservation(const Process* p, LimitableResource r, s64 v = 1) + explicit KScopedResourceReservation(const KProcess* p, LimitableResource r, s64 v = 1) : KScopedResourceReservation(p->GetResourceLimit(), r, v) {} ~KScopedResourceReservation() noexcept { diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp index 863f9aa5f..3bc259693 100644 --- a/src/core/hle/kernel/k_server_session.cpp +++ b/src/core/hle/kernel/k_server_session.cpp @@ -13,12 +13,12 @@ #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/hle_ipc.h" #include "core/hle/kernel/k_client_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel { diff --git a/src/core/hle/kernel/k_session.cpp b/src/core/hle/kernel/k_session.cpp index 6f5947ce7..5e629d446 100644 --- a/src/core/hle/kernel/k_session.cpp +++ b/src/core/hle/kernel/k_session.cpp @@ -71,7 +71,7 @@ void KSession::OnClientClosed() { void KSession::PostDestroy(uintptr_t arg) { // Release the session count resource the owner process holds. - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1); owner->Close(); } diff --git a/src/core/hle/kernel/k_session.h b/src/core/hle/kernel/k_session.h index f29195fa0..d50e21f3f 100644 --- a/src/core/hle/kernel/k_session.h +++ b/src/core/hle/kernel/k_session.h @@ -89,7 +89,7 @@ private: std::atomic::type> atomic_state{ static_cast::type>(State::Invalid)}; KClientPort* port{}; - Process* process{}; + KProcess* process{}; bool initialized{}; }; diff --git a/src/core/hle/kernel/k_shared_memory.cpp b/src/core/hle/kernel/k_shared_memory.cpp index e91bc94bd..f137a182a 100644 --- a/src/core/hle/kernel/k_shared_memory.cpp +++ b/src/core/hle/kernel/k_shared_memory.cpp @@ -19,7 +19,7 @@ KSharedMemory::~KSharedMemory() { } ResultCode KSharedMemory::Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, - Process* owner_process_, KPageLinkedList&& page_list_, + KProcess* owner_process_, KPageLinkedList&& page_list_, KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_) { @@ -74,7 +74,7 @@ void KSharedMemory::Finalize() { KAutoObjectWithSlabHeapAndContainer::Finalize(); } -ResultCode KSharedMemory::Map(Process& target_process, VAddr address, std::size_t size, +ResultCode KSharedMemory::Map(KProcess& target_process, VAddr address, std::size_t size, KMemoryPermission permissions) { const u64 page_count{(size + PageSize - 1) / PageSize}; diff --git a/src/core/hle/kernel/k_shared_memory.h b/src/core/hle/kernel/k_shared_memory.h index 9547546a5..2d315c916 100644 --- a/src/core/hle/kernel/k_shared_memory.h +++ b/src/core/hle/kernel/k_shared_memory.h @@ -11,7 +11,7 @@ #include "core/device_memory.h" #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_page_linked_list.h" -#include "core/hle/kernel/process.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/slab_helpers.h" #include "core/hle/result.h" @@ -28,7 +28,7 @@ public: ~KSharedMemory() override; ResultCode Initialize(KernelCore& kernel_, Core::DeviceMemory& device_memory_, - Process* owner_process_, KPageLinkedList&& page_list_, + KProcess* owner_process_, KPageLinkedList&& page_list_, KMemoryPermission owner_permission_, KMemoryPermission user_permission_, PAddr physical_address_, std::size_t size_, std::string name_); @@ -39,7 +39,7 @@ public: * @param size Size of the shared memory block to map * @param permissions Memory block map permissions (specified by SVC field) */ - ResultCode Map(Process& target_process, VAddr address, std::size_t size, + ResultCode Map(KProcess& target_process, VAddr address, std::size_t size, KMemoryPermission permissions); /** @@ -69,7 +69,7 @@ public: private: Core::DeviceMemory* device_memory; - Process* owner_process{}; + KProcess* owner_process{}; KPageLinkedList page_list; KMemoryPermission owner_permission{}; KMemoryPermission user_permission{}; diff --git a/src/core/hle/kernel/k_thread.cpp b/src/core/hle/kernel/k_thread.cpp index c59f3113c..3de0157ac 100644 --- a/src/core/hle/kernel/k_thread.cpp +++ b/src/core/hle/kernel/k_thread.cpp @@ -21,13 +21,13 @@ #include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/k_condition_variable.h" #include "core/hle/kernel/k_memory_layout.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_thread_queue.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" #include "core/hle/result.h" @@ -65,7 +65,7 @@ KThread::KThread(KernelCore& kernel) KThread::~KThread() = default; ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top, s32 prio, - s32 virt_core, Process* owner, ThreadType type) { + s32 virt_core, KProcess* owner, ThreadType type) { // Assert parameters are valid. ASSERT((type == ThreadType::Main) || (Svc::HighestThreadPriority <= prio && prio <= Svc::LowestThreadPriority)); @@ -209,7 +209,7 @@ ResultCode KThread::Initialize(KThreadFunction func, uintptr_t arg, VAddr user_s } ResultCode KThread::InitializeThread(KThread* thread, KThreadFunction func, uintptr_t arg, - VAddr user_stack_top, s32 prio, s32 core, Process* owner, + VAddr user_stack_top, s32 prio, s32 core, KProcess* owner, ThreadType type, std::function&& init_func, void* init_func_parameter) { // Initialize the thread. @@ -242,7 +242,7 @@ ResultCode KThread::InitializeHighPriorityThread(Core::System& system, KThread* ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread, KThreadFunction func, uintptr_t arg, VAddr user_stack_top, - s32 prio, s32 virt_core, Process* owner) { + s32 prio, s32 virt_core, KProcess* owner) { system.Kernel().GlobalSchedulerContext().AddThread(thread); return InitializeThread(thread, func, arg, user_stack_top, prio, virt_core, owner, ThreadType::User, Core::CpuManager::GetGuestThreadStartFunc(), @@ -250,7 +250,7 @@ ResultCode KThread::InitializeUserThread(Core::System& system, KThread* thread, } void KThread::PostDestroy(uintptr_t arg) { - Process* owner = reinterpret_cast(arg & ~1ULL); + KProcess* owner = reinterpret_cast(arg & ~1ULL); const bool resource_limit_release_hint = (arg & 1); const s64 hint_value = (resource_limit_release_hint ? 0 : 1); if (owner != nullptr) { diff --git a/src/core/hle/kernel/k_thread.h b/src/core/hle/kernel/k_thread.h index 5b943b18b..4145ef56c 100644 --- a/src/core/hle/kernel/k_thread.h +++ b/src/core/hle/kernel/k_thread.h @@ -37,7 +37,7 @@ namespace Kernel { class GlobalSchedulerContext; class KernelCore; -class Process; +class KProcess; class KScheduler; class KThreadQueue; @@ -105,7 +105,7 @@ class KThread final : public KAutoObjectWithSlabHeapAndContainer&& init_func, void* init_func_parameter); @@ -669,7 +669,7 @@ private: std::atomic cpu_time{}; KSynchronizationObject* synced_object{}; VAddr address_key{}; - Process* parent{}; + KProcess* parent{}; VAddr kernel_stack_top{}; u32* light_ipc_data{}; VAddr tls_address{}; diff --git a/src/core/hle/kernel/k_transfer_memory.cpp b/src/core/hle/kernel/k_transfer_memory.cpp index 09c067f95..201617d32 100644 --- a/src/core/hle/kernel/k_transfer_memory.cpp +++ b/src/core/hle/kernel/k_transfer_memory.cpp @@ -2,10 +2,10 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" namespace Kernel { @@ -37,7 +37,7 @@ void KTransferMemory::Finalize() { } void KTransferMemory::PostDestroy(uintptr_t arg) { - Process* owner = reinterpret_cast(arg); + KProcess* owner = reinterpret_cast(arg); owner->GetResourceLimit()->Release(LimitableResource::TransferMemory, 1); owner->Close(); } diff --git a/src/core/hle/kernel/k_transfer_memory.h b/src/core/hle/kernel/k_transfer_memory.h index 1e4fa9323..f56398b9c 100644 --- a/src/core/hle/kernel/k_transfer_memory.h +++ b/src/core/hle/kernel/k_transfer_memory.h @@ -19,7 +19,7 @@ class Memory; namespace Kernel { class KernelCore; -class Process; +class KProcess; class KTransferMemory final : public KAutoObjectWithSlabHeapAndContainer { @@ -43,7 +43,7 @@ public: static void PostDestroy(uintptr_t arg); - Process* GetOwner() const { + KProcess* GetOwner() const { return owner; } @@ -56,7 +56,7 @@ public: } private: - Process* owner{}; + KProcess* owner{}; VAddr address{}; Svc::MemoryPermission owner_perm{}; size_t size{}; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 409bcfaa0..718525c4c 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -31,6 +31,7 @@ #include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/k_shared_memory.h" @@ -38,7 +39,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/service_thread.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/time_manager.h" @@ -98,8 +98,8 @@ struct KernelCore::Impl { service_threads.clear(); next_object_id = 0; - next_kernel_process_id = Process::InitialKIPIDMin; - next_user_process_id = Process::ProcessIDMin; + next_kernel_process_id = KProcess::InitialKIPIDMin; + next_user_process_id = KProcess::ProcessIDMin; next_thread_id = 1; for (s32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { @@ -220,7 +220,7 @@ struct KernelCore::Impl { } } - void MakeCurrentProcess(Process* process) { + void MakeCurrentProcess(KProcess* process) { current_process = process; if (process == nullptr) { return; @@ -632,13 +632,13 @@ struct KernelCore::Impl { } std::atomic next_object_id{0}; - std::atomic next_kernel_process_id{Process::InitialKIPIDMin}; - std::atomic next_user_process_id{Process::ProcessIDMin}; + std::atomic next_kernel_process_id{KProcess::InitialKIPIDMin}; + std::atomic next_user_process_id{KProcess::ProcessIDMin}; std::atomic next_thread_id{1}; // Lists all processes that exist in the current session. - std::vector process_list; - Process* current_process{}; + std::vector process_list; + KProcess* current_process{}; std::unique_ptr global_scheduler_context; Kernel::TimeManager time_manager; @@ -725,23 +725,23 @@ KScopedAutoObject KernelCore::RetrieveThreadFromGlobalHandleTable(Handl return impl->global_handle_table.GetObject(handle); } -void KernelCore::AppendNewProcess(Process* process) { +void KernelCore::AppendNewProcess(KProcess* process) { impl->process_list.push_back(process); } -void KernelCore::MakeCurrentProcess(Process* process) { +void KernelCore::MakeCurrentProcess(KProcess* process) { impl->MakeCurrentProcess(process); } -Process* KernelCore::CurrentProcess() { +KProcess* KernelCore::CurrentProcess() { return impl->current_process; } -const Process* KernelCore::CurrentProcess() const { +const KProcess* KernelCore::CurrentProcess() const { return impl->current_process; } -const std::vector& KernelCore::GetProcessList() const { +const std::vector& KernelCore::GetProcessList() const { return impl->process_list; } diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index de7f83423..0dd9deaeb 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -37,7 +37,7 @@ class KEvent; class KLinkedListNode; class KMemoryManager; class KPort; -class Process; +class KProcess; class KResourceLimit; class KScheduler; class KSession; @@ -101,19 +101,19 @@ public: KScopedAutoObject RetrieveThreadFromGlobalHandleTable(Handle handle) const; /// Adds the given shared pointer to an internal list of active processes. - void AppendNewProcess(Process* process); + void AppendNewProcess(KProcess* process); /// Makes the given process the new current process. - void MakeCurrentProcess(Process* process); + void MakeCurrentProcess(KProcess* process); /// Retrieves a pointer to the current process. - Process* CurrentProcess(); + KProcess* CurrentProcess(); /// Retrieves a const pointer to the current process. - const Process* CurrentProcess() const; + const KProcess* CurrentProcess() const; /// Retrieves the list of processes. - const std::vector& GetProcessList() const; + const std::vector& GetProcessList() const; /// Gets the sole instance of the global scheduler Kernel::GlobalSchedulerContext& GlobalSchedulerContext(); @@ -274,7 +274,7 @@ public: return slab_heap_container->linked_list_node; } else if constexpr (std::is_same_v) { return slab_heap_container->port; - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v) { return slab_heap_container->process; } else if constexpr (std::is_same_v) { return slab_heap_container->resource_limit; @@ -292,7 +292,7 @@ public: } private: - friend class Process; + friend class KProcess; friend class KThread; /// Creates a new object ID, incrementing the internal object ID counter. @@ -325,7 +325,7 @@ private: KSlabHeap event; KSlabHeap linked_list_node; KSlabHeap port; - KSlabHeap process; + KSlabHeap process; KSlabHeap resource_limit; KSlabHeap session; KSlabHeap shared_memory; diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp deleted file mode 100644 index 315640bea..000000000 --- a/src/core/hle/kernel/process.cpp +++ /dev/null @@ -1,505 +0,0 @@ -// Copyright 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include -#include -#include -#include -#include "common/alignment.h" -#include "common/assert.h" -#include "common/logging/log.h" -#include "common/settings.h" -#include "core/core.h" -#include "core/device_memory.h" -#include "core/file_sys/program_metadata.h" -#include "core/hle/kernel/code_set.h" -#include "core/hle/kernel/k_memory_block_manager.h" -#include "core/hle/kernel/k_page_table.h" -#include "core/hle/kernel/k_resource_limit.h" -#include "core/hle/kernel/k_scheduler.h" -#include "core/hle/kernel/k_scoped_resource_reservation.h" -#include "core/hle/kernel/k_slab_heap.h" -#include "core/hle/kernel/k_thread.h" -#include "core/hle/kernel/kernel.h" -#include "core/hle/kernel/process.h" -#include "core/hle/kernel/svc_results.h" -#include "core/hle/lock.h" -#include "core/memory.h" - -namespace Kernel { -namespace { -/** - * Sets up the primary application thread - * - * @param system The system instance to create the main thread under. - * @param owner_process The parent process for the main thread - * @param priority The priority to give the main thread - */ -void SetupMainThread(Core::System& system, Process& owner_process, u32 priority, VAddr stack_top) { - const VAddr entry_point = owner_process.PageTable().GetCodeRegionStart(); - ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); - - KThread* thread = KThread::Create(system.Kernel()); - ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority, - owner_process.GetIdealCoreId(), &owner_process) - .IsSuccess()); - - // Register 1 must be a handle to the main thread - Handle thread_handle{}; - owner_process.GetHandleTable().Add(&thread_handle, thread); - - thread->SetName("main"); - thread->GetContext32().cpu_registers[0] = 0; - thread->GetContext64().cpu_registers[0] = 0; - thread->GetContext32().cpu_registers[1] = thread_handle; - thread->GetContext64().cpu_registers[1] = thread_handle; - - auto& kernel = system.Kernel(); - // Threads by default are dormant, wake up the main thread so it runs when the scheduler fires - { - KScopedSchedulerLock lock{kernel}; - thread->SetState(ThreadState::Runnable); - } -} -} // Anonymous namespace - -// Represents a page used for thread-local storage. -// -// Each TLS page contains slots that may be used by processes and threads. -// Every process and thread is created with a slot in some arbitrary page -// (whichever page happens to have an available slot). -class TLSPage { -public: - static constexpr std::size_t num_slot_entries = - Core::Memory::PAGE_SIZE / Core::Memory::TLS_ENTRY_SIZE; - - explicit TLSPage(VAddr address) : base_address{address} {} - - bool HasAvailableSlots() const { - return !is_slot_used.all(); - } - - VAddr GetBaseAddress() const { - return base_address; - } - - std::optional ReserveSlot() { - for (std::size_t i = 0; i < is_slot_used.size(); i++) { - if (is_slot_used[i]) { - continue; - } - - is_slot_used[i] = true; - return base_address + (i * Core::Memory::TLS_ENTRY_SIZE); - } - - return std::nullopt; - } - - void ReleaseSlot(VAddr address) { - // Ensure that all given addresses are consistent with how TLS pages - // are intended to be used when releasing slots. - ASSERT(IsWithinPage(address)); - ASSERT((address % Core::Memory::TLS_ENTRY_SIZE) == 0); - - const std::size_t index = (address - base_address) / Core::Memory::TLS_ENTRY_SIZE; - is_slot_used[index] = false; - } - -private: - bool IsWithinPage(VAddr address) const { - return base_address <= address && address < base_address + Core::Memory::PAGE_SIZE; - } - - VAddr base_address; - std::bitset is_slot_used; -}; - -ResultCode Process::Initialize(Process* process, Core::System& system, std::string name, - ProcessType type) { - auto& kernel = system.Kernel(); - - process->name = std::move(name); - - process->resource_limit = kernel.GetSystemResourceLimit(); - process->status = ProcessStatus::Created; - process->program_id = 0; - process->process_id = type == ProcessType::KernelInternal ? kernel.CreateNewKernelProcessID() - : kernel.CreateNewUserProcessID(); - process->capabilities.InitializeForMetadatalessProcess(); - process->is_initialized = true; - - std::mt19937 rng(Settings::values.rng_seed.GetValue().value_or(std::time(nullptr))); - std::uniform_int_distribution distribution; - std::generate(process->random_entropy.begin(), process->random_entropy.end(), - [&] { return distribution(rng); }); - - kernel.AppendNewProcess(process); - - // Open a reference to the resource limit. - process->resource_limit->Open(); - - return RESULT_SUCCESS; -} - -KResourceLimit* Process::GetResourceLimit() const { - return resource_limit; -} - -void Process::IncrementThreadCount() { - ASSERT(num_threads >= 0); - num_created_threads++; - - if (const auto count = ++num_threads; count > peak_num_threads) { - peak_num_threads = count; - } -} - -void Process::DecrementThreadCount() { - ASSERT(num_threads > 0); - - if (const auto count = --num_threads; count == 0) { - UNIMPLEMENTED_MSG("Process termination is not implemented!"); - } -} - -u64 Process::GetTotalPhysicalMemoryAvailable() const { - const u64 capacity{resource_limit->GetFreeValue(LimitableResource::PhysicalMemory) + - page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + - main_thread_stack_size}; - if (const auto pool_size = kernel.MemoryManager().GetSize(KMemoryManager::Pool::Application); - capacity != pool_size) { - LOG_WARNING(Kernel, "capacity {} != application pool size {}", capacity, pool_size); - } - if (capacity < memory_usage_capacity) { - return capacity; - } - return memory_usage_capacity; -} - -u64 Process::GetTotalPhysicalMemoryAvailableWithoutSystemResource() const { - return GetTotalPhysicalMemoryAvailable() - GetSystemResourceSize(); -} - -u64 Process::GetTotalPhysicalMemoryUsed() const { - return image_size + main_thread_stack_size + page_table->GetTotalHeapSize() + - GetSystemResourceSize(); -} - -u64 Process::GetTotalPhysicalMemoryUsedWithoutSystemResource() const { - return GetTotalPhysicalMemoryUsed() - GetSystemResourceUsage(); -} - -bool Process::ReleaseUserException(KThread* thread) { - KScopedSchedulerLock sl{kernel}; - - if (exception_thread == thread) { - exception_thread = nullptr; - - // Remove waiter thread. - s32 num_waiters{}; - KThread* next = thread->RemoveWaiterByKey( - std::addressof(num_waiters), - reinterpret_cast(std::addressof(exception_thread))); - if (next != nullptr) { - if (next->GetState() == ThreadState::Waiting) { - next->SetState(ThreadState::Runnable); - } else { - KScheduler::SetSchedulerUpdateNeeded(kernel); - } - } - - return true; - } else { - return false; - } -} - -void Process::PinCurrentThread() { - ASSERT(kernel.GlobalSchedulerContext().IsLocked()); - - // Get the current thread. - const s32 core_id = GetCurrentCoreId(kernel); - KThread* cur_thread = GetCurrentThreadPointer(kernel); - - // Pin it. - PinThread(core_id, cur_thread); - cur_thread->Pin(); - - // An update is needed. - KScheduler::SetSchedulerUpdateNeeded(kernel); -} - -void Process::UnpinCurrentThread() { - ASSERT(kernel.GlobalSchedulerContext().IsLocked()); - - // Get the current thread. - const s32 core_id = GetCurrentCoreId(kernel); - KThread* cur_thread = GetCurrentThreadPointer(kernel); - - // Unpin it. - cur_thread->Unpin(); - UnpinThread(core_id, cur_thread); - - // An update is needed. - KScheduler::SetSchedulerUpdateNeeded(kernel); -} - -void Process::RegisterThread(const KThread* thread) { - thread_list.push_back(thread); -} - -void Process::UnregisterThread(const KThread* thread) { - thread_list.remove(thread); -} - -ResultCode Process::Reset() { - // Lock the process and the scheduler. - KScopedLightLock lk(state_lock); - KScopedSchedulerLock sl{kernel}; - - // Validate that we're in a state that we can reset. - R_UNLESS(status != ProcessStatus::Exited, ResultInvalidState); - R_UNLESS(is_signaled, ResultInvalidState); - - // Clear signaled. - is_signaled = false; - return RESULT_SUCCESS; -} - -ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, - std::size_t code_size) { - program_id = metadata.GetTitleID(); - ideal_core = metadata.GetMainThreadCore(); - is_64bit_process = metadata.Is64BitProgram(); - system_resource_size = metadata.GetSystemResourceSize(); - image_size = code_size; - - KScopedResourceReservation memory_reservation(resource_limit, LimitableResource::PhysicalMemory, - code_size + system_resource_size); - if (!memory_reservation.Succeeded()) { - LOG_ERROR(Kernel, "Could not reserve process memory requirements of size {:X} bytes", - code_size + system_resource_size); - return ResultLimitReached; - } - // Initialize proces address space - if (const ResultCode result{ - page_table->InitializeForProcess(metadata.GetAddressSpaceType(), false, 0x8000000, - code_size, KMemoryManager::Pool::Application)}; - result.IsError()) { - return result; - } - - // Map process code region - if (const ResultCode result{page_table->MapProcessCode(page_table->GetCodeRegionStart(), - code_size / PageSize, KMemoryState::Code, - KMemoryPermission::None)}; - result.IsError()) { - return result; - } - - // Initialize process capabilities - const auto& caps{metadata.GetKernelCapabilities()}; - if (const ResultCode result{ - capabilities.InitializeForUserProcess(caps.data(), caps.size(), *page_table)}; - result.IsError()) { - return result; - } - - // Set memory usage capacity - switch (metadata.GetAddressSpaceType()) { - case FileSys::ProgramAddressSpaceType::Is32Bit: - case FileSys::ProgramAddressSpaceType::Is36Bit: - case FileSys::ProgramAddressSpaceType::Is39Bit: - memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart(); - break; - - case FileSys::ProgramAddressSpaceType::Is32BitNoMap: - memory_usage_capacity = page_table->GetHeapRegionEnd() - page_table->GetHeapRegionStart() + - page_table->GetAliasRegionEnd() - page_table->GetAliasRegionStart(); - break; - - default: - UNREACHABLE(); - } - - // Create TLS region - tls_region_address = CreateTLSRegion(); - memory_reservation.Commit(); - - return handle_table.SetSize(capabilities.GetHandleTableSize()); -} - -void Process::Run(s32 main_thread_priority, u64 stack_size) { - AllocateMainThreadStack(stack_size); - resource_limit->Reserve(LimitableResource::Threads, 1); - resource_limit->Reserve(LimitableResource::PhysicalMemory, main_thread_stack_size); - - const std::size_t heap_capacity{memory_usage_capacity - main_thread_stack_size - image_size}; - ASSERT(!page_table->SetHeapCapacity(heap_capacity).IsError()); - - ChangeStatus(ProcessStatus::Running); - - SetupMainThread(kernel.System(), *this, main_thread_priority, main_thread_stack_top); -} - -void Process::PrepareForTermination() { - ChangeStatus(ProcessStatus::Exiting); - - const auto stop_threads = [this](const std::vector& thread_list) { - for (auto& thread : thread_list) { - if (thread->GetOwnerProcess() != this) - continue; - - if (thread == kernel.CurrentScheduler()->GetCurrentThread()) - continue; - - // TODO(Subv): When are the other running/ready threads terminated? - ASSERT_MSG(thread->GetState() == ThreadState::Waiting, - "Exiting processes with non-waiting threads is currently unimplemented"); - - thread->Exit(); - } - }; - - stop_threads(kernel.System().GlobalSchedulerContext().GetThreadList()); - - FreeTLSRegion(tls_region_address); - tls_region_address = 0; - - if (resource_limit) { - resource_limit->Release(LimitableResource::PhysicalMemory, - main_thread_stack_size + image_size); - } - - ChangeStatus(ProcessStatus::Exited); -} - -void Process::Finalize() { - // Release memory to the resource limit. - if (resource_limit != nullptr) { - resource_limit->Close(); - } - - // Perform inherited finalization. - KAutoObjectWithSlabHeapAndContainer::Finalize(); -} - -/** - * Attempts to find a TLS page that contains a free slot for - * use by a thread. - * - * @returns If a page with an available slot is found, then an iterator - * pointing to the page is returned. Otherwise the end iterator - * is returned instead. - */ -static auto FindTLSPageWithAvailableSlots(std::vector& tls_pages) { - return std::find_if(tls_pages.begin(), tls_pages.end(), - [](const auto& page) { return page.HasAvailableSlots(); }); -} - -VAddr Process::CreateTLSRegion() { - KScopedSchedulerLock lock(kernel); - if (auto tls_page_iter{FindTLSPageWithAvailableSlots(tls_pages)}; - tls_page_iter != tls_pages.cend()) { - return *tls_page_iter->ReserveSlot(); - } - - Page* const tls_page_ptr{kernel.GetUserSlabHeapPages().Allocate()}; - ASSERT(tls_page_ptr); - - const VAddr start{page_table->GetKernelMapRegionStart()}; - const VAddr size{page_table->GetKernelMapRegionEnd() - start}; - const PAddr tls_map_addr{kernel.System().DeviceMemory().GetPhysicalAddr(tls_page_ptr)}; - const VAddr tls_page_addr{page_table - ->AllocateAndMapMemory(1, PageSize, true, start, size / PageSize, - KMemoryState::ThreadLocal, - KMemoryPermission::ReadAndWrite, - tls_map_addr) - .ValueOr(0)}; - - ASSERT(tls_page_addr); - - std::memset(tls_page_ptr, 0, PageSize); - tls_pages.emplace_back(tls_page_addr); - - const auto reserve_result{tls_pages.back().ReserveSlot()}; - ASSERT(reserve_result.has_value()); - - return *reserve_result; -} - -void Process::FreeTLSRegion(VAddr tls_address) { - KScopedSchedulerLock lock(kernel); - const VAddr aligned_address = Common::AlignDown(tls_address, Core::Memory::PAGE_SIZE); - auto iter = - std::find_if(tls_pages.begin(), tls_pages.end(), [aligned_address](const auto& page) { - return page.GetBaseAddress() == aligned_address; - }); - - // Something has gone very wrong if we're freeing a region - // with no actual page available. - ASSERT(iter != tls_pages.cend()); - - iter->ReleaseSlot(tls_address); -} - -void Process::LoadModule(CodeSet code_set, VAddr base_addr) { - std::lock_guard lock{HLE::g_hle_lock}; - const auto ReprotectSegment = [&](const CodeSet::Segment& segment, - KMemoryPermission permission) { - page_table->SetCodeMemoryPermission(segment.addr + base_addr, segment.size, permission); - }; - - kernel.System().Memory().WriteBlock(*this, base_addr, code_set.memory.data(), - code_set.memory.size()); - - ReprotectSegment(code_set.CodeSegment(), KMemoryPermission::ReadAndExecute); - ReprotectSegment(code_set.RODataSegment(), KMemoryPermission::Read); - ReprotectSegment(code_set.DataSegment(), KMemoryPermission::ReadAndWrite); -} - -bool Process::IsSignaled() const { - ASSERT(kernel.GlobalSchedulerContext().IsLocked()); - return is_signaled; -} - -Process::Process(KernelCore& kernel) - : KAutoObjectWithSlabHeapAndContainer{kernel}, - page_table{std::make_unique(kernel.System())}, handle_table{kernel}, - address_arbiter{kernel.System()}, condition_var{kernel.System()}, state_lock{kernel} {} - -Process::~Process() = default; - -void Process::ChangeStatus(ProcessStatus new_status) { - if (status == new_status) { - return; - } - - status = new_status; - is_signaled = true; - NotifyAvailable(); -} - -ResultCode Process::AllocateMainThreadStack(std::size_t stack_size) { - ASSERT(stack_size); - - // The kernel always ensures that the given stack size is page aligned. - main_thread_stack_size = Common::AlignUp(stack_size, PageSize); - - const VAddr start{page_table->GetStackRegionStart()}; - const std::size_t size{page_table->GetStackRegionEnd() - start}; - - CASCADE_RESULT(main_thread_stack_top, - page_table->AllocateAndMapMemory( - main_thread_stack_size / PageSize, PageSize, false, start, size / PageSize, - KMemoryState::Stack, KMemoryPermission::ReadAndWrite)); - - main_thread_stack_top += main_thread_stack_size; - - return RESULT_SUCCESS; -} - -} // namespace Kernel diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h deleted file mode 100644 index b775e1fd0..000000000 --- a/src/core/hle/kernel/process.h +++ /dev/null @@ -1,479 +0,0 @@ -// Copyright 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include -#include -#include -#include "common/common_types.h" -#include "core/hle/kernel/handle_table.h" -#include "core/hle/kernel/k_address_arbiter.h" -#include "core/hle/kernel/k_auto_object.h" -#include "core/hle/kernel/k_condition_variable.h" -#include "core/hle/kernel/k_synchronization_object.h" -#include "core/hle/kernel/process_capability.h" -#include "core/hle/kernel/slab_helpers.h" -#include "core/hle/result.h" - -namespace Core { -class System; -} - -namespace FileSys { -class ProgramMetadata; -} - -namespace Kernel { - -class KernelCore; -class KPageTable; -class KResourceLimit; -class KThread; -class TLSPage; - -struct CodeSet; - -enum class MemoryRegion : u16 { - APPLICATION = 1, - SYSTEM = 2, - BASE = 3, -}; - -/** - * Indicates the status of a Process instance. - * - * @note These match the values as used by kernel, - * so new entries should only be added if RE - * shows that a new value has been introduced. - */ -enum class ProcessStatus { - Created, - CreatedWithDebuggerAttached, - Running, - WaitingForDebuggerToAttach, - DebuggerAttached, - Exiting, - Exited, - DebugBreak, -}; - -class Process final : public KAutoObjectWithSlabHeapAndContainer { - KERNEL_AUTOOBJECT_TRAITS(Process, KSynchronizationObject); - -public: - explicit Process(KernelCore& kernel); - ~Process() override; - - enum : u64 { - /// Lowest allowed process ID for a kernel initial process. - InitialKIPIDMin = 1, - /// Highest allowed process ID for a kernel initial process. - InitialKIPIDMax = 80, - - /// Lowest allowed process ID for a userland process. - ProcessIDMin = 81, - /// Highest allowed process ID for a userland process. - ProcessIDMax = 0xFFFFFFFFFFFFFFFF, - }; - - // Used to determine how process IDs are assigned. - enum class ProcessType { - KernelInternal, - Userland, - }; - - static constexpr std::size_t RANDOM_ENTROPY_SIZE = 4; - - static ResultCode Initialize(Process* process, Core::System& system, std::string name, - ProcessType type); - - /// Gets a reference to the process' page table. - KPageTable& PageTable() { - return *page_table; - } - - /// Gets const a reference to the process' page table. - const KPageTable& PageTable() const { - return *page_table; - } - - /// Gets a reference to the process' handle table. - HandleTable& GetHandleTable() { - return handle_table; - } - - /// Gets a const reference to the process' handle table. - const HandleTable& GetHandleTable() const { - return handle_table; - } - - ResultCode SignalToAddress(VAddr address) { - return condition_var.SignalToAddress(address); - } - - ResultCode WaitForAddress(Handle handle, VAddr address, u32 tag) { - return condition_var.WaitForAddress(handle, address, tag); - } - - void SignalConditionVariable(u64 cv_key, int32_t count) { - return condition_var.Signal(cv_key, count); - } - - ResultCode WaitConditionVariable(VAddr address, u64 cv_key, u32 tag, s64 ns) { - return condition_var.Wait(address, cv_key, tag, ns); - } - - ResultCode SignalAddressArbiter(VAddr address, Svc::SignalType signal_type, s32 value, - s32 count) { - return address_arbiter.SignalToAddress(address, signal_type, value, count); - } - - ResultCode WaitAddressArbiter(VAddr address, Svc::ArbitrationType arb_type, s32 value, - s64 timeout) { - return address_arbiter.WaitForAddress(address, arb_type, value, timeout); - } - - /// Gets the address to the process' dedicated TLS region. - VAddr GetTLSRegionAddress() const { - return tls_region_address; - } - - /// Gets the current status of the process - ProcessStatus GetStatus() const { - return status; - } - - /// Gets the unique ID that identifies this particular process. - u64 GetProcessID() const { - return process_id; - } - - /// Gets the title ID corresponding to this process. - u64 GetTitleID() const { - return program_id; - } - - /// Gets the resource limit descriptor for this process - KResourceLimit* GetResourceLimit() const; - - /// Gets the ideal CPU core ID for this process - u8 GetIdealCoreId() const { - return ideal_core; - } - - /// Checks if the specified thread priority is valid. - bool CheckThreadPriority(s32 prio) const { - return ((1ULL << prio) & GetPriorityMask()) != 0; - } - - /// Gets the bitmask of allowed cores that this process' threads can run on. - u64 GetCoreMask() const { - return capabilities.GetCoreMask(); - } - - /// Gets the bitmask of allowed thread priorities. - u64 GetPriorityMask() const { - return capabilities.GetPriorityMask(); - } - - /// Gets the amount of secure memory to allocate for memory management. - u32 GetSystemResourceSize() const { - return system_resource_size; - } - - /// Gets the amount of secure memory currently in use for memory management. - u32 GetSystemResourceUsage() const { - // On hardware, this returns the amount of system resource memory that has - // been used by the kernel. This is problematic for Yuzu to emulate, because - // system resource memory is used for page tables -- and yuzu doesn't really - // have a way to calculate how much memory is required for page tables for - // the current process at any given time. - // TODO: Is this even worth implementing? Games may retrieve this value via - // an SDK function that gets used + available system resource size for debug - // or diagnostic purposes. However, it seems unlikely that a game would make - // decisions based on how much system memory is dedicated to its page tables. - // Is returning a value other than zero wise? - return 0; - } - - /// Whether this process is an AArch64 or AArch32 process. - bool Is64BitProcess() const { - return is_64bit_process; - } - - [[nodiscard]] bool IsSuspended() const { - return is_suspended; - } - - void SetSuspended(bool suspended) { - is_suspended = suspended; - } - - /// Gets the total running time of the process instance in ticks. - u64 GetCPUTimeTicks() const { - return total_process_running_time_ticks; - } - - /// Updates the total running time, adding the given ticks to it. - void UpdateCPUTimeTicks(u64 ticks) { - total_process_running_time_ticks += ticks; - } - - /// Gets the process schedule count, used for thread yelding - s64 GetScheduledCount() const { - return schedule_count; - } - - /// Increments the process schedule count, used for thread yielding. - void IncrementScheduledCount() { - ++schedule_count; - } - - void IncrementThreadCount(); - void DecrementThreadCount(); - - void SetRunningThread(s32 core, KThread* thread, u64 idle_count) { - running_threads[core] = thread; - running_thread_idle_counts[core] = idle_count; - } - - void ClearRunningThread(KThread* thread) { - for (size_t i = 0; i < running_threads.size(); ++i) { - if (running_threads[i] == thread) { - running_threads[i] = nullptr; - } - } - } - - [[nodiscard]] KThread* GetRunningThread(s32 core) const { - return running_threads[core]; - } - - bool ReleaseUserException(KThread* thread); - - [[nodiscard]] KThread* GetPinnedThread(s32 core_id) const { - ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); - return pinned_threads[core_id]; - } - - /// Gets 8 bytes of random data for svcGetInfo RandomEntropy - u64 GetRandomEntropy(std::size_t index) const { - return random_entropy.at(index); - } - - /// Retrieves the total physical memory available to this process in bytes. - u64 GetTotalPhysicalMemoryAvailable() const; - - /// Retrieves the total physical memory available to this process in bytes, - /// without the size of the personal system resource heap added to it. - u64 GetTotalPhysicalMemoryAvailableWithoutSystemResource() const; - - /// Retrieves the total physical memory used by this process in bytes. - u64 GetTotalPhysicalMemoryUsed() const; - - /// Retrieves the total physical memory used by this process in bytes, - /// without the size of the personal system resource heap added to it. - u64 GetTotalPhysicalMemoryUsedWithoutSystemResource() const; - - /// Gets the list of all threads created with this process as their owner. - const std::list& GetThreadList() const { - return thread_list; - } - - /// Registers a thread as being created under this process, - /// adding it to this process' thread list. - void RegisterThread(const KThread* thread); - - /// Unregisters a thread from this process, removing it - /// from this process' thread list. - void UnregisterThread(const KThread* thread); - - /// Clears the signaled state of the process if and only if it's signaled. - /// - /// @pre The process must not be already terminated. If this is called on a - /// terminated process, then ERR_INVALID_STATE will be returned. - /// - /// @pre The process must be in a signaled state. If this is called on a - /// process instance that is not signaled, ERR_INVALID_STATE will be - /// returned. - ResultCode Reset(); - - /** - * Loads process-specifics configuration info with metadata provided - * by an executable. - * - * @param metadata The provided metadata to load process specific info from. - * - * @returns RESULT_SUCCESS if all relevant metadata was able to be - * loaded and parsed. Otherwise, an error code is returned. - */ - ResultCode LoadFromMetadata(const FileSys::ProgramMetadata& metadata, std::size_t code_size); - - /** - * Starts the main application thread for this process. - * - * @param main_thread_priority The priority for the main thread. - * @param stack_size The stack size for the main thread in bytes. - */ - void Run(s32 main_thread_priority, u64 stack_size); - - /** - * Prepares a process for termination by stopping all of its threads - * and clearing any other resources. - */ - void PrepareForTermination(); - - void LoadModule(CodeSet code_set, VAddr base_addr); - - virtual bool IsInitialized() const override { - return is_initialized; - } - - static void PostDestroy([[maybe_unused]] uintptr_t arg) {} - - virtual void Finalize(); - - virtual u64 GetId() const override final { - return GetProcessID(); - } - - virtual bool IsSignaled() const override; - - void PinCurrentThread(); - void UnpinCurrentThread(); - - KLightLock& GetStateLock() { - return state_lock; - } - - /////////////////////////////////////////////////////////////////////////////////////////////// - // Thread-local storage management - - // Marks the next available region as used and returns the address of the slot. - [[nodiscard]] VAddr CreateTLSRegion(); - - // Frees a used TLS slot identified by the given address - void FreeTLSRegion(VAddr tls_address); - -private: - void PinThread(s32 core_id, KThread* thread) { - ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); - ASSERT(thread != nullptr); - ASSERT(pinned_threads[core_id] == nullptr); - pinned_threads[core_id] = thread; - } - - void UnpinThread(s32 core_id, KThread* thread) { - ASSERT(0 <= core_id && core_id < static_cast(Core::Hardware::NUM_CPU_CORES)); - ASSERT(thread != nullptr); - ASSERT(pinned_threads[core_id] == thread); - pinned_threads[core_id] = nullptr; - } - - /// Changes the process status. If the status is different - /// from the current process status, then this will trigger - /// a process signal. - void ChangeStatus(ProcessStatus new_status); - - /// Allocates the main thread stack for the process, given the stack size in bytes. - ResultCode AllocateMainThreadStack(std::size_t stack_size); - - /// Memory manager for this process - std::unique_ptr page_table; - - /// Current status of the process - ProcessStatus status{}; - - /// The ID of this process - u64 process_id = 0; - - /// Title ID corresponding to the process - u64 program_id = 0; - - /// Specifies additional memory to be reserved for the process's memory management by the - /// system. When this is non-zero, secure memory is allocated and used for page table allocation - /// instead of using the normal global page tables/memory block management. - u32 system_resource_size = 0; - - /// Resource limit descriptor for this process - KResourceLimit* resource_limit{}; - - /// The ideal CPU core for this process, threads are scheduled on this core by default. - u8 ideal_core = 0; - - /// The Thread Local Storage area is allocated as processes create threads, - /// each TLS area is 0x200 bytes, so one page (0x1000) is split up in 8 parts, and each part - /// holds the TLS for a specific thread. This vector contains which parts are in use for each - /// page as a bitmask. - /// This vector will grow as more pages are allocated for new threads. - std::vector tls_pages; - - /// Contains the parsed process capability descriptors. - ProcessCapabilities capabilities; - - /// Whether or not this process is AArch64, or AArch32. - /// By default, we currently assume this is true, unless otherwise - /// specified by metadata provided to the process during loading. - bool is_64bit_process = true; - - /// Total running time for the process in ticks. - u64 total_process_running_time_ticks = 0; - - /// Per-process handle table for storing created object handles in. - HandleTable handle_table; - - /// Per-process address arbiter. - KAddressArbiter address_arbiter; - - /// The per-process mutex lock instance used for handling various - /// forms of services, such as lock arbitration, and condition - /// variable related facilities. - KConditionVariable condition_var; - - /// Address indicating the location of the process' dedicated TLS region. - VAddr tls_region_address = 0; - - /// Random values for svcGetInfo RandomEntropy - std::array random_entropy{}; - - /// List of threads that are running with this process as their owner. - std::list thread_list; - - /// Address of the top of the main thread's stack - VAddr main_thread_stack_top{}; - - /// Size of the main thread's stack - std::size_t main_thread_stack_size{}; - - /// Memory usage capacity for the process - std::size_t memory_usage_capacity{}; - - /// Process total image size - std::size_t image_size{}; - - /// Schedule count of this process - s64 schedule_count{}; - - bool is_signaled{}; - bool is_suspended{}; - bool is_initialized{}; - - std::atomic num_created_threads{}; - std::atomic num_threads{}; - u16 peak_num_threads{}; - - std::array running_threads{}; - std::array running_thread_idle_counts{}; - std::array pinned_threads{}; - - KThread* exception_thread{}; - - KLightLock state_lock; -}; - -} // namespace Kernel diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index ef8fa98a9..725f16ea4 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -30,6 +30,7 @@ #include "core/hle/kernel/k_memory_block.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_page_table.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_scheduler.h" @@ -42,7 +43,6 @@ #include "core/hle/kernel/k_writable_event.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" -#include "core/hle/kernel/process.h" #include "core/hle/kernel/svc.h" #include "core/hle/kernel/svc_results.h" #include "core/hle/kernel/svc_types.h" @@ -402,8 +402,8 @@ static ResultCode GetProcessId(Core::System& system, u64* out_process_id, Handle R_UNLESS(obj.IsNotNull(), ResultInvalidHandle); // Get the process from the object. - Process* process = nullptr; - if (Process* p = obj->DynamicCast(); p != nullptr) { + KProcess* process = nullptr; + if (KProcess* p = obj->DynamicCast(); p != nullptr) { // The object is a process, so we can use it directly. process = p; } else if (KThread* t = obj->DynamicCast(); t != nullptr) { @@ -733,7 +733,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(handle); + KScopedAutoObject process = handle_table.GetObject(handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process is not valid! info_id={}, info_sub_id={}, handle={:08X}", info_id, info_sub_id, handle); @@ -838,7 +838,7 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle return ResultInvalidCombination; } - Process* const current_process = system.Kernel().CurrentProcess(); + KProcess* const current_process = system.Kernel().CurrentProcess(); HandleTable& handle_table = current_process->GetHandleTable(); const auto resource_limit = current_process->GetResourceLimit(); if (!resource_limit) { @@ -861,9 +861,9 @@ static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, Handle return ResultInvalidHandle; } - if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) { + if (info_sub_id >= KProcess::RANDOM_ENTROPY_SIZE) { LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}", - Process::RANDOM_ENTROPY_SIZE, info_sub_id); + KProcess::RANDOM_ENTROPY_SIZE, info_sub_id); return ResultInvalidCombination; } @@ -955,7 +955,7 @@ static ResultCode MapPhysicalMemory(Core::System& system, VAddr addr, u64 size) return ResultInvalidMemoryRegion; } - Process* const current_process{system.Kernel().CurrentProcess()}; + KProcess* const current_process{system.Kernel().CurrentProcess()}; auto& page_table{current_process->PageTable()}; if (current_process->GetSystemResourceSize() == 0) { @@ -1009,7 +1009,7 @@ static ResultCode UnmapPhysicalMemory(Core::System& system, VAddr addr, u64 size return ResultInvalidMemoryRegion; } - Process* const current_process{system.Kernel().CurrentProcess()}; + KProcess* const current_process{system.Kernel().CurrentProcess()}; auto& page_table{current_process->PageTable()}; if (current_process->GetSystemResourceSize() == 0) { @@ -1153,7 +1153,7 @@ static ResultCode GetThreadPriority32(Core::System& system, u32* out_priority, H /// Sets the priority for the specified thread static ResultCode SetThreadPriority(Core::System& system, Handle thread_handle, u32 priority) { // Get the current process. - Process& process = *system.Kernel().CurrentProcess(); + KProcess& process = *system.Kernel().CurrentProcess(); // Validate the priority. R_UNLESS(HighestThreadPriority <= priority && priority <= LowestThreadPriority, @@ -1264,7 +1264,7 @@ static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_add std::lock_guard lock{HLE::g_hle_lock}; LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address); const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); @@ -1346,7 +1346,7 @@ static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_hand } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); @@ -1414,7 +1414,7 @@ static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_ha } const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).", process_handle); @@ -1830,7 +1830,7 @@ static ResultCode ResetSignal(Core::System& system, Handle handle) { // Try to reset as process. { - KScopedAutoObject process = handle_table.GetObject(handle); + KScopedAutoObject process = handle_table.GetObject(handle); if (process.IsNotNull()) { return process->Reset(); } @@ -2077,7 +2077,7 @@ static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_ }; const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable(); - KScopedAutoObject process = handle_table.GetObject(process_handle); + KScopedAutoObject process = handle_table.GetObject(process_handle); if (process.IsNull()) { LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}", process_handle); -- cgit v1.2.3