From 57867d6cedcf15b98fce1c9309f0a5fe32817baa Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 13 Jul 2020 12:17:28 -0700 Subject: [PATCH] kern: fix/add InfoType_(Total/Used)(NonSystem/)MemorySize --- .../mesosphere/kern_k_page_table_base.hpp | 2 +- .../include/mesosphere/kern_k_process.hpp | 2 ++ .../libmesosphere/source/kern_k_process.cpp | 24 +++++++++++++++++-- .../source/svc/kern_svc_exception.cpp | 1 + .../source/svc/kern_svc_info.cpp | 12 ++++++++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp index 1dfdfd737..744541e3a 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_page_table_base.hpp @@ -328,7 +328,7 @@ namespace ams::kern { /* Lock the table. */ KScopedLightLock lk(this->general_lock); - return this->GetHeapRegionSize() + this->mapped_physical_memory_size; + return (this->current_heap_end - this->heap_region_start) + this->mapped_physical_memory_size; } public: static ALWAYS_INLINE KVirtualAddress GetLinearMappedVirtualAddress(KPhysicalAddress addr) { diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp index c6feb762e..dec9af3fc 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp @@ -176,6 +176,8 @@ namespace ams::kern { constexpr KHandleTable &GetHandleTable() { return this->handle_table; } constexpr const KHandleTable &GetHandleTable() const { return this->handle_table; } + size_t GetUsedUserPhysicalMemorySize() const; + size_t GetTotalUserPhysicalMemorySize() const; size_t GetUsedNonSystemUserPhysicalMemorySize() const; size_t GetTotalNonSystemUserPhysicalMemorySize() const; diff --git a/libraries/libmesosphere/source/kern_k_process.cpp b/libraries/libmesosphere/source/kern_k_process.cpp index 6a9d246e2..4985fa0f9 100644 --- a/libraries/libmesosphere/source/kern_k_process.cpp +++ b/libraries/libmesosphere/source/kern_k_process.cpp @@ -303,7 +303,7 @@ namespace ams::kern { this->thread_list.erase(this->thread_list.iterator_to(*thread)); } - size_t KProcess::GetUsedNonSystemUserPhysicalMemorySize() const { + size_t KProcess::GetUsedUserPhysicalMemorySize() const { const size_t norm_size = this->page_table.GetNormalMemorySize(); const size_t other_size = this->code_size + this->main_thread_stack_size; const size_t sec_size = KSystemControl::CalculateRequiredSecureMemorySize(this->system_resource_num_pages * PageSize, this->memory_pool); @@ -311,10 +311,30 @@ namespace ams::kern { return norm_size + other_size + sec_size; } - size_t KProcess::GetTotalNonSystemUserPhysicalMemorySize() const { + size_t KProcess::GetTotalUserPhysicalMemorySize() const { /* Get the amount of free and used size. */ const size_t free_size = this->resource_limit->GetFreeValue(ams::svc::LimitableResource_PhysicalMemoryMax); const size_t used_size = this->GetUsedNonSystemUserPhysicalMemorySize(); + const size_t max_size = this->max_process_memory; + + if (used_size + free_size > max_size) { + return max_size; + } else { + return free_size + used_size; + } + } + + size_t KProcess::GetUsedNonSystemUserPhysicalMemorySize() const { + const size_t norm_size = this->page_table.GetNormalMemorySize(); + const size_t other_size = this->code_size + this->main_thread_stack_size; + + return norm_size + other_size; + } + + size_t KProcess::GetTotalNonSystemUserPhysicalMemorySize() const { + /* Get the amount of free and used size. */ + const size_t free_size = this->resource_limit->GetFreeValue(ams::svc::LimitableResource_PhysicalMemoryMax); + const size_t used_size = this->GetUsedUserPhysicalMemorySize(); const size_t sec_size = KSystemControl::CalculateRequiredSecureMemorySize(this->system_resource_num_pages * PageSize, this->memory_pool); const size_t max_size = this->max_process_memory; diff --git a/libraries/libmesosphere/source/svc/kern_svc_exception.cpp b/libraries/libmesosphere/source/svc/kern_svc_exception.cpp index 4df8e6d6b..d6a3222f9 100644 --- a/libraries/libmesosphere/source/svc/kern_svc_exception.cpp +++ b/libraries/libmesosphere/source/svc/kern_svc_exception.cpp @@ -28,6 +28,7 @@ namespace ams::kern::svc { /* ============================= 64 ABI ============================= */ void Break64(ams::svc::BreakReason break_reason, ams::svc::Address arg, ams::svc::Size size) { + MESOSPHERE_LOG("%s: Break\n", GetCurrentProcess().GetName()); MESOSPHERE_PANIC("Stubbed SvcBreak64 was called."); } diff --git a/libraries/libmesosphere/source/svc/kern_svc_info.cpp b/libraries/libmesosphere/source/svc/kern_svc_info.cpp index dc27cc869..eafaa1f64 100644 --- a/libraries/libmesosphere/source/svc/kern_svc_info.cpp +++ b/libraries/libmesosphere/source/svc/kern_svc_info.cpp @@ -32,6 +32,8 @@ namespace ams::kern::svc { case ams::svc::InfoType_AliasRegionSize: case ams::svc::InfoType_HeapRegionAddress: case ams::svc::InfoType_HeapRegionSize: + case ams::svc::InfoType_TotalMemorySize: + case ams::svc::InfoType_UsedMemorySize: case ams::svc::InfoType_AslrRegionAddress: case ams::svc::InfoType_AslrRegionSize: case ams::svc::InfoType_StackRegionAddress: @@ -40,6 +42,7 @@ namespace ams::kern::svc { case ams::svc::InfoType_InitialProcessIdRange: case ams::svc::InfoType_UserExceptionContextAddress: case ams::svc::InfoType_TotalNonSystemMemorySize: + case ams::svc::InfoType_UsedNonSystemMemorySize: { /* These info types don't support non-zero subtypes. */ R_UNLESS(info_subtype == 0, svc::ResultInvalidCombination()); @@ -67,6 +70,12 @@ namespace ams::kern::svc { case ams::svc::InfoType_HeapRegionSize: *out = process->GetPageTable().GetHeapRegionSize(); break; + case ams::svc::InfoType_TotalMemorySize: + *out = process->GetTotalUserPhysicalMemorySize(); + break; + case ams::svc::InfoType_UsedMemorySize: + *out = process->GetUsedUserPhysicalMemorySize(); + break; case ams::svc::InfoType_AslrRegionAddress: *out = GetInteger(process->GetPageTable().GetAliasCodeRegionStart()); break; @@ -91,6 +100,9 @@ namespace ams::kern::svc { case ams::svc::InfoType_TotalNonSystemMemorySize: *out = process->GetTotalNonSystemUserPhysicalMemorySize(); break; + case ams::svc::InfoType_UsedNonSystemMemorySize: + *out = process->GetUsedNonSystemUserPhysicalMemorySize(); + break; MESOSPHERE_UNREACHABLE_DEFAULT_CASE(); } }