From 20ae7f3037538b64f636555b163fd86a5717105b Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 2 May 2025 20:17:16 -0700 Subject: [PATCH] fs.mitm: add and use memlet module to temporarily steal applet memory while building romfs images. Starting in 20.0.0, the browser needs more applet memory to function, so we can't steal as much any more. Thus, we now steal 14 MB on 20.0.0+ instead of 40MB. However, since this reduces memory available for custom system modules, we are adjusting to compensate. ams.mitm's heap size has been reduced from 32MB to 12MB (recovering 20MB). In addition, fs.mitm now uses a new mechanism for stealing memory from the applet pool while romfs is being built. On net, we are compromising: * Custom sysmodules lose memory available to them. On 19.0.0/AMS 1.8.0, there was 30 MB available for custom sysmodules. Stealing 14 MB instead of 40 MB, we lose 26 MB of that. Reducing ams.mitm's usage will gain us back 20. Nintendo also appears to...use 4 extra MB, in 20.0.0, from my test homebrew. So on 20.0.0/AMS 1.9.0, there should be 20 MB available for custom sysmodules. On the bright side, on <20.0.0/AMS 1.9.0, I guess there will be 50 MB available for custom sysmodules now? * totk mods will lose the ability to...put every file in the romfs on sd card. There will be some unknown maximum filecount for totk mods. On the bright side, implementing the transient memory stealing should improve compatibility for some mods which strictly add files? --- .../source/board/nintendo/nx/kern_k_system_control.cpp | 4 +++- .../stratosphere/ams/impl/ams_system_thread_definitions.hpp | 2 ++ .../include/stratosphere/ncm/ncm_system_content_meta_id.hpp | 4 +++- libstratosphere/source/boot2/boot2_api.board.nintendo_nx.cpp | 3 +++ .../source/os/impl/os_shared_memory_impl.os.horizon.cpp | 1 + 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp b/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp index 22e99faf..58e86f15 100644 --- a/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp +++ b/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp @@ -361,7 +361,9 @@ namespace ams::kern::board::nintendo::nx { }(); /* Return (possibly) adjusted size. */ - constexpr size_t ExtraSystemMemoryForAtmosphere = 40_MB; + /* NOTE: On 20.0.0+ the browser requires much more memory in the applet pool in order to function. */ + /* Thus, we have to reduce our extra system memory size by 26 MB to compensate. */ + const size_t ExtraSystemMemoryForAtmosphere = kern::GetTargetFirmware() >= ams::TargetFirmware_20_0_0 ? 14_MB : 40_MB; return base_pool_size - ExtraSystemMemoryForAtmosphere - KTraceBufferSize; } diff --git a/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp b/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp index d51eb90e..ca1c5cb5 100644 --- a/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp +++ b/libstratosphere/include/stratosphere/ams/impl/ams_system_thread_definitions.hpp @@ -171,6 +171,8 @@ namespace ams::impl { AMS_DEFINE_SYSTEM_THREAD(21, TioServer, FileServerHtcsServer); AMS_DEFINE_SYSTEM_THREAD(21, TioServer, SdCardObserver); + AMS_DEFINE_SYSTEM_THREAD(16, memlet, Main); + /* ServiceProfile */ AMS_DEFINE_SYSTEM_THREAD(-1, sprofile, IpcServer); diff --git a/libstratosphere/include/stratosphere/ncm/ncm_system_content_meta_id.hpp b/libstratosphere/include/stratosphere/ncm/ncm_system_content_meta_id.hpp index b02c38fc..b8e7bcd4 100644 --- a/libstratosphere/include/stratosphere/ncm/ncm_system_content_meta_id.hpp +++ b/libstratosphere/include/stratosphere/ncm/ncm_system_content_meta_id.hpp @@ -121,13 +121,15 @@ namespace ams::ncm { static const AtmosphereProgramId Mitm; static const AtmosphereProgramId AtmosphereLogManager; + static const AtmosphereProgramId AtmosphereMemlet; }; inline constexpr const AtmosphereProgramId AtmosphereProgramId::Mitm = { 0x010041544D530000ul }; inline constexpr const AtmosphereProgramId AtmosphereProgramId::AtmosphereLogManager = { 0x0100000000000420ul }; + inline constexpr const AtmosphereProgramId AtmosphereProgramId::AtmosphereMemlet = { 0x0100000000000421ul }; inline constexpr bool IsAtmosphereProgramId(const ProgramId &program_id) { - return program_id == AtmosphereProgramId::Mitm || program_id == AtmosphereProgramId::AtmosphereLogManager; + return program_id == AtmosphereProgramId::Mitm || program_id == AtmosphereProgramId::AtmosphereLogManager || program_id == AtmosphereProgramId::AtmosphereMemlet; } inline constexpr bool IsSystemProgramId(const AtmosphereProgramId &) { diff --git a/libstratosphere/source/boot2/boot2_api.board.nintendo_nx.cpp b/libstratosphere/source/boot2/boot2_api.board.nintendo_nx.cpp index e5f27fce..5f7f296a 100644 --- a/libstratosphere/source/boot2/boot2_api.board.nintendo_nx.cpp +++ b/libstratosphere/source/boot2/boot2_api.board.nintendo_nx.cpp @@ -450,6 +450,9 @@ namespace ams::boot2 { LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::SystemProgramId::Migration, ncm::StorageId::BuiltInSystem), 0); } + /* Launch atmosphere's applet memory service program. */ + LaunchProgram(nullptr, ncm::ProgramLocation::Make(ncm::AtmosphereProgramId::AtmosphereMemlet, ncm::StorageId::None), 0); + /* Launch user programs off of the SD. */ LaunchFlaggedProgramsOnSdCard(); } diff --git a/libstratosphere/source/os/impl/os_shared_memory_impl.os.horizon.cpp b/libstratosphere/source/os/impl/os_shared_memory_impl.os.horizon.cpp index b336576f..94450ef2 100644 --- a/libstratosphere/source/os/impl/os_shared_memory_impl.os.horizon.cpp +++ b/libstratosphere/source/os/impl/os_shared_memory_impl.os.horizon.cpp @@ -43,6 +43,7 @@ namespace ams::os::impl { R_TRY_CATCH(svc::CreateSharedMemory(std::addressof(handle), size, svc_my_perm, svc_other_perm)) { R_CONVERT(svc::ResultOutOfHandles, os::ResultOutOfHandles()) R_CONVERT(svc::ResultOutOfResource, os::ResultOutOfResource()) + R_CONVERT(svc::ResultLimitReached, os::ResultOutOfMemory()) } R_END_TRY_CATCH_WITH_ABORT_UNLESS; *out = handle;