From ccbb4dd036f4f2dd50649228c1a3b00ba8691d9f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 28 Mar 2019 18:24:56 -0400 Subject: [PATCH 1/5] kernel/process: Ensure that given stack size is always page-aligned The kernel always makes sure that the given stack size is aligned to page boundaries. --- src/core/hle/kernel/process.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 0d782e4bae..73b4ff9610 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "common/alignment.h" #include "common/assert.h" #include "common/logging/log.h" #include "core/core.h" @@ -108,6 +109,9 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) { } void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) { + // The kernel always ensures that the given stack size is page aligned. + stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE); + // Allocate and map the main thread stack // TODO(bunnei): This is heap area that should be allocated by the kernel and not mapped as part // of the user address space. From 62afbfe707a4bfe546c7bd888278620cc22dd36d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 28 Mar 2019 18:26:09 -0400 Subject: [PATCH 2/5] kernel/process: Make Run's stack size parameter a u64 This will make operating with the process-related SVC commands much nicer in the future (the parameter representing the stack size in svcStartProcess is a 64-bit value). --- src/core/hle/kernel/process.cpp | 2 +- src/core/hle/kernel/process.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 73b4ff9610..f18789a606 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -108,7 +108,7 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) { return handle_table.SetSize(capabilities.GetHandleTableSize()); } -void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) { +void Process::Run(VAddr entry_point, s32 main_thread_priority, u64 stack_size) { // The kernel always ensures that the given stack size is page aligned. stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE); diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index a0217d3d84..db14dd4b4c 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -210,7 +210,7 @@ public: /** * Applies address space changes and launches the process main thread. */ - void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size); + void Run(VAddr entry_point, s32 main_thread_priority, u64 stack_size); /** * Prepares a process for termination by stopping all of its threads From 9a748d6f289fe104e712b80dc64b092b39302e36 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 28 Mar 2019 18:30:58 -0400 Subject: [PATCH 3/5] kernel/process: Store the main thread stack size to a data member This will be necessary in order to properly report memory usage within svcGetInfo. --- src/core/hle/kernel/process.cpp | 8 ++++---- src/core/hle/kernel/process.h | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index f18789a606..bb26737325 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -110,15 +110,15 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata) { void Process::Run(VAddr entry_point, s32 main_thread_priority, u64 stack_size) { // The kernel always ensures that the given stack size is page aligned. - stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE); + main_thread_stack_size = Common::AlignUp(stack_size, Memory::PAGE_SIZE); // Allocate and map the main thread stack // TODO(bunnei): This is heap area that should be allocated by the kernel and not mapped as part // of the user address space. + const VAddr mapping_address = vm_manager.GetTLSIORegionEndAddress() - main_thread_stack_size; vm_manager - .MapMemoryBlock(vm_manager.GetTLSIORegionEndAddress() - stack_size, - std::make_shared>(stack_size, 0), 0, stack_size, - MemoryState::Stack) + .MapMemoryBlock(mapping_address, std::make_shared>(main_thread_stack_size), + 0, main_thread_stack_size, MemoryState::Stack) .Unwrap(); vm_manager.LogLayout(); diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index db14dd4b4c..ee559fe4ce 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -247,6 +247,9 @@ private: /// Memory manager for this process. Kernel::VMManager vm_manager; + /// Size of the main thread's stack in bytes. + u64 main_thread_stack_size = 0; + /// Current status of the process ProcessStatus status; From 4ee615b15a10c6fdbb93acd792f876c947c72f31 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 28 Mar 2019 18:52:45 -0400 Subject: [PATCH 4/5] kernel/process: Store the total size of the code memory loaded This will be necessary to properly report the used memory size in svcGetInfo. --- src/core/hle/kernel/process.cpp | 2 ++ src/core/hle/kernel/process.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index bb26737325..819d2cb0bc 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -228,6 +228,8 @@ void Process::LoadModule(CodeSet module_, VAddr base_addr) { MapSegment(module_.RODataSegment(), VMAPermission::Read, MemoryState::CodeData); MapSegment(module_.DataSegment(), VMAPermission::ReadWrite, MemoryState::CodeData); + code_memory_size += module_.memory->size(); + // Clear instruction cache in CPU JIT system.InvalidateCpuInstructionCaches(); } diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index ee559fe4ce..16193ca563 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -250,6 +250,9 @@ private: /// Size of the main thread's stack in bytes. u64 main_thread_stack_size = 0; + /// Size of the loaded code memory in bytes. + u64 code_memory_size = 0; + /// Current status of the process ProcessStatus status; From 60167a13ebb1a720c27f3f8c9170cf20a962d33b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 28 Mar 2019 22:59:17 -0400 Subject: [PATCH 5/5] kernel/process: Report total physical memory used to svcGetInfo Reports the (mostly) correct size through svcGetInfo now for queries to total used physical memory. This still doesn't correctly handle memory allocated via svcMapPhysicalMemory, however, we don't currently handle that case anyways. --- src/core/hle/kernel/process.cpp | 4 ++++ src/core/hle/kernel/process.h | 3 +++ src/core/hle/kernel/svc.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 819d2cb0bc..b0b7af76bd 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -76,6 +76,10 @@ SharedPtr Process::GetResourceLimit() const { return resource_limit; } +u64 Process::GetTotalPhysicalMemoryUsed() const { + return vm_manager.GetCurrentHeapSize() + main_thread_stack_size + code_memory_size; +} + ResultCode Process::ClearSignalState() { if (status == ProcessStatus::Exited) { LOG_ERROR(Kernel, "called on a terminated process instance."); diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 16193ca563..732d121706 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h @@ -186,6 +186,9 @@ public: return random_entropy.at(index); } + /// Retrieves the total physical memory used by this process in bytes. + u64 GetTotalPhysicalMemoryUsed() const; + /// 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 diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index 09d1eadb6c..17bfe10ff5 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -709,7 +709,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) HeapRegionBaseAddr = 4, HeapRegionSize = 5, TotalMemoryUsage = 6, - TotalHeapUsage = 7, + TotalPhysicalMemoryUsed = 7, IsCurrentProcessBeingDebugged = 8, RegisterResourceLimit = 9, IdleTickCount = 10, @@ -745,7 +745,7 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) case GetInfoType::NewMapRegionBaseAddr: case GetInfoType::NewMapRegionSize: case GetInfoType::TotalMemoryUsage: - case GetInfoType::TotalHeapUsage: + case GetInfoType::TotalPhysicalMemoryUsed: case GetInfoType::IsVirtualAddressMemoryEnabled: case GetInfoType::PersonalMmHeapUsage: case GetInfoType::TitleId: @@ -805,8 +805,8 @@ static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) *result = process->VMManager().GetTotalMemoryUsage(); return RESULT_SUCCESS; - case GetInfoType::TotalHeapUsage: - *result = process->VMManager().GetCurrentHeapSize(); + case GetInfoType::TotalPhysicalMemoryUsed: + *result = process->GetTotalPhysicalMemoryUsed(); return RESULT_SUCCESS; case GetInfoType::IsVirtualAddressMemoryEnabled: