Skip to content

Commit

Permalink
kern: fix/add InfoType_(Total/Used)(NonSystem/)MemorySize
Browse files Browse the repository at this point in the history
  • Loading branch information
SciresM committed Jul 13, 2020
1 parent 35c1959 commit 57867d6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/libmesosphere/include/mesosphere/kern_k_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 22 additions & 2 deletions libraries/libmesosphere/source/kern_k_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,38 @@ 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);

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;

Expand Down
1 change: 1 addition & 0 deletions libraries/libmesosphere/source/svc/kern_svc_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
12 changes: 12 additions & 0 deletions libraries/libmesosphere/source/svc/kern_svc_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 57867d6

Please sign in to comment.