From 28717bfd27f7fe651b523b27a35336760183e0ac Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 9 Dec 2019 17:56:38 -0800 Subject: [PATCH] fusee/pm: on 5.0.0+, fix system memory pool increase. Previously, we were only setting resource limit, which didn't modify actual reserved pool size for the system pool. This adds kernel patches which reduce the applet pool size, granting the extra memory to the system partition. The given value has been chosen specifically to allow normal applet usages. Further reduction may result in crashes during normal applet usage. --- fusee/fusee-secondary/src/kernel_patches.c | 32 +++++++++++++++++++ .../pm/source/impl/pm_resource_manager.cpp | 19 ++++++----- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/fusee/fusee-secondary/src/kernel_patches.c b/fusee/fusee-secondary/src/kernel_patches.c index 3ace98067..a8b3cb54c 100644 --- a/fusee/fusee-secondary/src/kernel_patches.c +++ b/fusee/fusee-secondary/src/kernel_patches.c @@ -488,6 +488,13 @@ static const instruction_t MAKE_KERNEL_PATCH_NAME(700, svc_control_codememory)[] static const instruction_t MAKE_KERNEL_PATCH_NAME(800, svc_control_codememory)[] = {MAKE_NOP}; static const instruction_t MAKE_KERNEL_PATCH_NAME(900, svc_control_codememory)[] = {MAKE_NOP}; + +static const instruction_t MAKE_KERNEL_PATCH_NAME(500, system_memory_increase)[] = {0x52A3C008}; /* MOV W8, #0x1E000000 */ +static const instruction_t MAKE_KERNEL_PATCH_NAME(600, system_memory_increase)[] = {0x52A3B008}; /* MOV W8, #0x1D800000 */ +static const instruction_t MAKE_KERNEL_PATCH_NAME(700, system_memory_increase)[] = {0x52A3B008}; /* MOV W8, #0x1D800000 */ +static const instruction_t MAKE_KERNEL_PATCH_NAME(800, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */ +static const instruction_t MAKE_KERNEL_PATCH_NAME(900, system_memory_increase)[] = {0x52A3B013}; /* MOV W19, #0x1D800000 */ + /* Hook Definitions. */ static const kernel_patch_t g_kernel_patches_100[] = { { /* Send Message Process ID Patch. */ @@ -600,6 +607,11 @@ static const kernel_patch_t g_kernel_patches_500[] = { .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(500, svc_control_codememory))/sizeof(instruction_t), .payload = MAKE_KERNEL_PATCH_NAME(500, svc_control_codememory), .patch_offset = 0x38C2C, + }, + { /* System Memory Increase Patch. */ + .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(500, system_memory_increase))/sizeof(instruction_t), + .payload = MAKE_KERNEL_PATCH_NAME(500, system_memory_increase), + .patch_offset = 0x54E30, } }; static const kernel_patch_t g_kernel_patches_600[] = { @@ -623,6 +635,11 @@ static const kernel_patch_t g_kernel_patches_600[] = { .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(600, svc_control_codememory))/sizeof(instruction_t), .payload = MAKE_KERNEL_PATCH_NAME(600, svc_control_codememory), .patch_offset = 0x3A8CC, + }, + { /* System Memory Increase Patch. */ + .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(600, system_memory_increase))/sizeof(instruction_t), + .payload = MAKE_KERNEL_PATCH_NAME(600, system_memory_increase), + .patch_offset = 0x57330, } }; static const kernel_patch_t g_kernel_patches_700[] = { @@ -646,6 +663,11 @@ static const kernel_patch_t g_kernel_patches_700[] = { .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(700, svc_control_codememory))/sizeof(instruction_t), .payload = MAKE_KERNEL_PATCH_NAME(700, svc_control_codememory), .patch_offset = 0x3C6E0, + }, + { /* System Memory Increase Patch. */ + .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(700, system_memory_increase))/sizeof(instruction_t), + .payload = MAKE_KERNEL_PATCH_NAME(700, system_memory_increase), + .patch_offset = 0x57F98, } }; @@ -670,6 +692,11 @@ static const kernel_patch_t g_kernel_patches_800[] = { .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(800, svc_control_codememory))/sizeof(instruction_t), .payload = MAKE_KERNEL_PATCH_NAME(800, svc_control_codememory), .patch_offset = 0x3FAD0, + }, + { /* System Memory Increase Patch. */ + .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(800, system_memory_increase))/sizeof(instruction_t), + .payload = MAKE_KERNEL_PATCH_NAME(800, system_memory_increase), + .patch_offset = 0x5F9A4, } }; @@ -694,6 +721,11 @@ static const kernel_patch_t g_kernel_patches_900[] = { .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(900, svc_control_codememory))/sizeof(instruction_t), .payload = MAKE_KERNEL_PATCH_NAME(900, svc_control_codememory), .patch_offset = 0x43DFC, + }, + { /* System Memory Increase Patch. */ + .payload_num_instructions = sizeof(MAKE_KERNEL_PATCH_NAME(900, system_memory_increase))/sizeof(instruction_t), + .payload = MAKE_KERNEL_PATCH_NAME(900, system_memory_increase), + .patch_offset = 0x6493C, } }; diff --git a/stratosphere/pm/source/impl/pm_resource_manager.cpp b/stratosphere/pm/source/impl/pm_resource_manager.cpp index 0babd157e..f07d09dec 100644 --- a/stratosphere/pm/source/impl/pm_resource_manager.cpp +++ b/stratosphere/pm/source/impl/pm_resource_manager.cpp @@ -38,8 +38,9 @@ namespace ams::pm::resource { constexpr size_t ExtraSystemSessionCount600 = 100; constexpr size_t ReservedMemorySize600 = 5 * Megabyte; - /* Atmosphere always allocates 24 extra megabytes for system usage. */ - constexpr size_t ExtraSystemMemorySizeAtmosphere = 24 * Megabyte; + /* Atmosphere always allocates extra memory for system usage. */ + constexpr size_t ExtraSystemMemorySizeAtmosphere = 24 * Megabyte; + constexpr size_t ExtraSystemMemorySizeAtmosphere500 = 33 * Megabyte; /* Applet pool is 0x20100000 */ /* Globals. */ os::Mutex g_resource_limit_lock; @@ -252,12 +253,14 @@ namespace ams::pm::resource { /* Adjust memory limits for atmosphere. */ /* We take memory away from applet normally, but away from application on < 3.0.0 to avoid a rare hang on boot. */ - for (size_t i = 0; i < spl::MemoryArrangement_Count; i++) { - g_memory_resource_limits[i][ResourceLimitGroup_System] += ExtraSystemMemorySizeAtmosphere; - if (hos_version >= hos::Version_300) { - g_memory_resource_limits[i][ResourceLimitGroup_Applet] -= ExtraSystemMemorySizeAtmosphere; - } else { - g_memory_resource_limits[i][ResourceLimitGroup_Application] -= ExtraSystemMemorySizeAtmosphere; + /* NOTE: On Version 5.0.0+, we cannot set the pools so simply. We must instead modify the kernel, which we do */ + /* via patches in fusee-secondary. */ + if (hos_version < hos::Version_600) { + const size_t extra_memory_size = hos_version == hos::Version_500 ? ExtraSystemMemorySizeAtmosphere500 : ExtraSystemMemorySizeAtmosphere; + const auto src_group = hos_version >= hos::Version_300 ? ResourceLimitGroup_Applet : ResourceLimitGroup_Application; + for (size_t i = 0; i < spl::MemoryArrangement_Count; i++) { + g_memory_resource_limits[i][ResourceLimitGroup_System] += extra_memory_size; + g_memory_resource_limits[i][src_group] -= extra_memory_size; } }