From efe7b63576174ff816fe591733eb51f5bb292ebc Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 9 May 2023 22:12:00 -0700 Subject: [PATCH] pm/mitm: okay, that api won't work, try a different one --- .../libstratosphere/include/stratosphere.hpp | 1 + .../include/stratosphere/mitm.hpp | 20 +++++++++ .../mitm/impl/mitm_pm_interface.hpp | 27 +++++++++++ .../include/stratosphere/mitm/mitm_pm_api.hpp | 29 ++++++++++++ .../pm/impl/pm_shell_interface.hpp | 6 +-- .../include/stratosphere/pm/pm_shell_api.hpp | 2 - .../source/mitm/mitm_pm.os.horizon.c | 44 ++++++++++++++++++ .../source/mitm/mitm_pm.os.horizon.h | 37 +++++++++++++++ .../source/mitm/mitm_pm_api.cpp | 37 +++++++++++++++ .../source/pm/pm_ams.os.horizon.c | 6 --- .../source/pm/pm_ams.os.horizon.h | 2 - .../source/pm/pm_shell_api.cpp | 4 -- .../source/amsmitm_module_management.cpp | 3 ++ .../source/fs_mitm/fs_mitm_service.cpp | 2 +- .../ams_mitm/source/fs_mitm/fsmitm_romfs.cpp | 27 +++++++++++ .../ams_mitm/source/fs_mitm/fsmitm_romfs.hpp | 1 + .../source/mitm_pm/mitm_pm_module.cpp | 45 +++++++++++++++++++ .../source/mitm_pm/mitm_pm_module.hpp | 24 ++++++++++ .../source/mitm_pm/mitm_pm_service.cpp | 29 ++++++++++++ .../source/mitm_pm/mitm_pm_service.hpp | 27 +++++++++++ .../source/ns_mitm/ns_am_mitm_service.cpp | 2 +- .../source/ns_mitm/ns_web_mitm_service.cpp | 2 +- .../source/set_mitm/set_mitm_service.cpp | 6 +-- .../source/set_mitm/setmitm_module.cpp | 2 +- .../pm/source/impl/pm_process_manager.cpp | 16 +++++++ stratosphere/pm/source/pm_shell_service.cpp | 4 -- stratosphere/pm/source/pm_shell_service.hpp | 3 -- 27 files changed, 376 insertions(+), 32 deletions(-) create mode 100644 libraries/libstratosphere/include/stratosphere/mitm.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/mitm/impl/mitm_pm_interface.hpp create mode 100644 libraries/libstratosphere/include/stratosphere/mitm/mitm_pm_api.hpp create mode 100644 libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.c create mode 100644 libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.h create mode 100644 libraries/libstratosphere/source/mitm/mitm_pm_api.cpp create mode 100644 stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.cpp create mode 100644 stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.hpp create mode 100644 stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.cpp create mode 100644 stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.hpp diff --git a/libraries/libstratosphere/include/stratosphere.hpp b/libraries/libstratosphere/include/stratosphere.hpp index bc5547f97..92ac1defe 100644 --- a/libraries/libstratosphere/include/stratosphere.hpp +++ b/libraries/libstratosphere/include/stratosphere.hpp @@ -73,6 +73,7 @@ #include #include #include +#include #include #include #include diff --git a/libraries/libstratosphere/include/stratosphere/mitm.hpp b/libraries/libstratosphere/include/stratosphere/mitm.hpp new file mode 100644 index 000000000..c4e7afe64 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/mitm.hpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include diff --git a/libraries/libstratosphere/include/stratosphere/mitm/impl/mitm_pm_interface.hpp b/libraries/libstratosphere/include/stratosphere/mitm/impl/mitm_pm_interface.hpp new file mode 100644 index 000000000..704fdb3ab --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/mitm/impl/mitm_pm_interface.hpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include +#include +#include + +#define AMS_MITM_PM_IMPL_I_PM_INTERFACE_INFO(C, H) \ + AMS_SF_METHOD_INFO(C, H, 65000, Result, PrepareLaunchProgram, (sf::Out out_boost_size, ncm::ProgramId program_id, const cfg::OverrideStatus &override_status, bool is_application), (out_boost_size, program_id, override_status, is_application)) + +AMS_SF_DEFINE_INTERFACE(ams::mitm::pm::impl, IPmInterface, AMS_MITM_PM_IMPL_I_PM_INTERFACE_INFO, 0xEA88789C) + diff --git a/libraries/libstratosphere/include/stratosphere/mitm/mitm_pm_api.hpp b/libraries/libstratosphere/include/stratosphere/mitm/mitm_pm_api.hpp new file mode 100644 index 000000000..feab8db66 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/mitm/mitm_pm_api.hpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include + +namespace ams::mitm::pm { + + /* PM API. */ + void Initialize(); + void Finalize(); + + Result PrepareLaunchProgram(u64 *out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, bool is_application); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pm/impl/pm_shell_interface.hpp b/libraries/libstratosphere/include/stratosphere/pm/impl/pm_shell_interface.hpp index ff19597b6..1cccdab65 100644 --- a/libraries/libstratosphere/include/stratosphere/pm/impl/pm_shell_interface.hpp +++ b/libraries/libstratosphere/include/stratosphere/pm/impl/pm_shell_interface.hpp @@ -30,8 +30,7 @@ AMS_SF_METHOD_INFO(C, H, 7, Result, BoostSystemMemoryResourceLimit, (u64 boost_size), (boost_size)) \ AMS_SF_METHOD_INFO(C, H, 8, Result, BoostApplicationThreadResourceLimit, (), ()) \ AMS_SF_METHOD_INFO(C, H, 9, void, GetBootFinishedEventHandle, (sf::OutCopyHandle out), (out), hos::Version_8_0_0) \ - AMS_SF_METHOD_INFO(C, H, 10, Result, BoostSystemThreadResourceLimit, (), ()) \ - AMS_SF_METHOD_INFO(C, H, 65000, Result, AtmosphereBoostSystemMemoryResourceLimitForMitm, (u64 boost_size), (boost_size)) + AMS_SF_METHOD_INFO(C, H, 10, Result, BoostSystemThreadResourceLimit, (), ()) AMS_SF_DEFINE_INTERFACE(ams::pm::impl, IShellInterface, AMS_PM_I_SHELL_INTERFACE_INTERFACE_INFO, 0x387D60C0) @@ -46,7 +45,6 @@ AMS_SF_DEFINE_INTERFACE(ams::pm::impl, IShellInterface, AMS_PM_I_SHELL_INTERFACE AMS_SF_METHOD_INFO(C, H, 7, void, NotifyBootFinished, (), ()) \ AMS_SF_METHOD_INFO(C, H, 8, Result, GetApplicationProcessIdForShell, (sf::Out out), (out)) \ AMS_SF_METHOD_INFO(C, H, 9, Result, BoostSystemMemoryResourceLimit, (u64 boost_size), (boost_size), hos::Version_4_0_0) \ - AMS_SF_METHOD_INFO(C, H, 10, Result, BoostSystemThreadResourceLimit, (), ()) \ - AMS_SF_METHOD_INFO(C, H, 65000, Result, AtmosphereBoostSystemMemoryResourceLimitForMitm, (u64 boost_size), (boost_size)) + AMS_SF_METHOD_INFO(C, H, 10, Result, BoostSystemThreadResourceLimit, (), ()) AMS_SF_DEFINE_INTERFACE(ams::pm::impl, IDeprecatedShellInterface, AMS_PM_I_DEPRECATED_SHELL_INTERFACE_INTERFACE_INFO, 0x387D60C0) diff --git a/libraries/libstratosphere/include/stratosphere/pm/pm_shell_api.hpp b/libraries/libstratosphere/include/stratosphere/pm/pm_shell_api.hpp index ce3f393ab..b4cd348c4 100644 --- a/libraries/libstratosphere/include/stratosphere/pm/pm_shell_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/pm/pm_shell_api.hpp @@ -32,6 +32,4 @@ namespace ams::pm::shell { Result BoostApplicationThreadResourceLimit(); Result BoostSystemThreadResourceLimit(); - Result AtmosphereBoostSystemMemoryResourceLimitForMitm(u64 size); - } diff --git a/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.c b/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.c new file mode 100644 index 000000000..adfabd4fa --- /dev/null +++ b/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "../service_guard.h" +#include "mitm_pm.os.horizon.h" + +static Service g_amsMitmPmSrv; + +NX_GENERATE_SERVICE_GUARD(amsMitmPm); + +Result _amsMitmPmInitialize(void) { + return smGetService(&g_amsMitmPmSrv, "mitm:pm"); +} + +void _amsMitmPmCleanup(void) { + serviceClose(&g_amsMitmPmSrv); +} + +Service *amsMitmPmGetServiceSession(void) { + return &g_amsMitmPmSrv; +} + +Result amsMitmPmPrepareLaunchProgram(u64 *out, u64 program_id, const CfgOverrideStatus *status, bool is_application) { + const struct { + u8 is_application; + u64 program_id; + CfgOverrideStatus status; + } in = { is_application ? 1 : 0, program_id, *status }; + + return serviceDispatchInOut(&g_amsMitmPmSrv, 65000, in, *out); +} diff --git a/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.h b/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.h new file mode 100644 index 000000000..2d8da12c4 --- /dev/null +++ b/libraries/libstratosphere/source/mitm/mitm_pm.os.horizon.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + u64 keys_down; + u64 flags; +} CfgOverrideStatus; + +Result amsMitmPmInitialize(void); +void amsMitmPmExit(void); +Service *amsMitmPmGetServiceSession(void); + +Result amsMitmPmPrepareLaunchProgram(u64 *out, u64 program_id, const CfgOverrideStatus *status, bool is_application); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/libraries/libstratosphere/source/mitm/mitm_pm_api.cpp b/libraries/libstratosphere/source/mitm/mitm_pm_api.cpp new file mode 100644 index 000000000..a49cf9ce9 --- /dev/null +++ b/libraries/libstratosphere/source/mitm/mitm_pm_api.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include "mitm_pm.os.horizon.h" + +namespace ams::mitm::pm { + + /* PM API. */ + #if defined(ATMOSPHERE_OS_HORIZON) + void Initialize() { + R_ABORT_UNLESS(amsMitmPmInitialize()); + } + + void Finalize() { + amsMitmPmExit(); + } + + Result PrepareLaunchProgram(u64 *out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, bool is_application) { + static_assert(sizeof(status) == sizeof(CfgOverrideStatus), "CfgOverrideStatus definition!"); + R_RETURN(amsMitmPmPrepareLaunchProgram(out, program_id.value, reinterpret_cast(std::addressof(status)), is_application)); + } + #endif + +} diff --git a/libraries/libstratosphere/source/pm/pm_ams.os.horizon.c b/libraries/libstratosphere/source/pm/pm_ams.os.horizon.c index d67f5c704..b7b3d249a 100644 --- a/libraries/libstratosphere/source/pm/pm_ams.os.horizon.c +++ b/libraries/libstratosphere/source/pm/pm_ams.os.horizon.c @@ -90,9 +90,3 @@ Result pmdmntAtmosphereGetCurrentLimitInfo(u64 *out_cur, u64 *out_lim, u32 group return rc; } - -Result pmshellAtmosphereBoostSystemMemoryResourceLimitForMitm(u64 size) { - return serviceDispatchIn(pmshellGetServiceSession(), 65000, size); -} - - diff --git a/libraries/libstratosphere/source/pm/pm_ams.os.horizon.h b/libraries/libstratosphere/source/pm/pm_ams.os.horizon.h index b7c6c2217..099752eee 100644 --- a/libraries/libstratosphere/source/pm/pm_ams.os.horizon.h +++ b/libraries/libstratosphere/source/pm/pm_ams.os.horizon.h @@ -26,8 +26,6 @@ Result pminfoAtmosphereGetProcessInfo(NcmProgramLocation *loc_out, CfgOverrideSt Result pmdmntAtmosphereGetProcessInfo(Handle *out, NcmProgramLocation *loc_out, CfgOverrideStatus *status_out, u64 pid); Result pmdmntAtmosphereGetCurrentLimitInfo(u64 *out_cur, u64 *out_lim, u32 group, u32 resource); -Result pmshellAtmosphereBoostSystemMemoryResourceLimitForMitm(u64 size); - #ifdef __cplusplus } #endif diff --git a/libraries/libstratosphere/source/pm/pm_shell_api.cpp b/libraries/libstratosphere/source/pm/pm_shell_api.cpp index 1b0364be7..247d88cbb 100644 --- a/libraries/libstratosphere/source/pm/pm_shell_api.cpp +++ b/libraries/libstratosphere/source/pm/pm_shell_api.cpp @@ -58,10 +58,6 @@ namespace ams::pm::shell { Result BoostSystemThreadResourceLimit() { R_RETURN(::pmshellBoostSystemThreadResourceLimit()); } - - Result AtmosphereBoostSystemMemoryResourceLimitForMitm(u64 size) { - R_RETURN(pmshellAtmosphereBoostSystemMemoryResourceLimitForMitm(size)); - } #endif } diff --git a/stratosphere/ams_mitm/source/amsmitm_module_management.cpp b/stratosphere/ams_mitm/source/amsmitm_module_management.cpp index 6905f5e6a..c8087a075 100644 --- a/stratosphere/ams_mitm/source/amsmitm_module_management.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_module_management.cpp @@ -24,6 +24,7 @@ #include "ns_mitm/nsmitm_module.hpp" #include "dns_mitm/dnsmitm_module.hpp" #include "sysupdater/sysupdater_module.hpp" +#include "mitm_pm/mitm_pm_module.hpp" namespace ams::mitm { @@ -37,6 +38,7 @@ namespace ams::mitm { ModuleId_NsMitm, ModuleId_DnsMitm, ModuleId_Sysupdater, + ModuleId_PmService, ModuleId_Count, }; @@ -70,6 +72,7 @@ namespace ams::mitm { GetModuleDefinition(), GetModuleDefinition(), GetModuleDefinition(), + GetModuleDefinition(), }; } diff --git a/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp b/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp index 12074c135..f82a6e806 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fs_mitm_service.cpp @@ -71,7 +71,7 @@ namespace ams::mitm::fs { Result OpenHblWebContentFileSystem(sf::Out> &out, ncm::ProgramId program_id) { /* Verify eligibility. */ bool is_hbl; - R_UNLESS(R_SUCCEEDED(pm::info::IsHblProgramId(std::addressof(is_hbl), program_id)), sm::mitm::ResultShouldForwardToSession()); + R_UNLESS(R_SUCCEEDED(ams::pm::info::IsHblProgramId(std::addressof(is_hbl), program_id)), sm::mitm::ResultShouldForwardToSession()); R_UNLESS(is_hbl, sm::mitm::ResultShouldForwardToSession()); /* Hbl html directory must exist. */ diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.cpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.cpp index 87daef48b..45669567b 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.cpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.cpp @@ -26,6 +26,18 @@ namespace ams::mitm::fs { namespace { /* TODO: Fancy Dynamic allocation globals. */ + constinit os::SdkMutex g_romfs_build_lock; + //constinit size_t g_dynamic_heap_size = 0; + + void InitializeDynamicHeapForBuildRomfs(ncm::ProgramId program_id) { + /* TODO */ + AMS_UNUSED(program_id); + } + + void FinalizeDynamicHeapForBuildRomfs(ncm::ProgramId program_id) { + /* TODO */ + AMS_UNUSED(program_id); + } } @@ -315,6 +327,12 @@ namespace ams::mitm::fs { } Builder::Builder(ncm::ProgramId pr_id) : m_program_id(pr_id), m_num_dirs(0), m_num_files(0), m_dir_table_size(0), m_file_table_size(0), m_dir_hash_table_size(0), m_file_hash_table_size(0), m_file_partition_size(0) { + /* Ensure only one romfs is built at any time. */ + g_romfs_build_lock.Lock(); + + /* If we should be using dynamic heap, turn it on. */ + InitializeDynamicHeapForBuildRomfs(m_program_id); + auto res = m_directories.emplace(std::unique_ptr(AllocateTyped(AllocationType_BuildDirContext, BuildDirectoryContext::RootTag{}))); AMS_ABORT_UNLESS(res.second); m_root = res.first->get(); @@ -322,6 +340,15 @@ namespace ams::mitm::fs { m_dir_table_size = 0x18; } + Builder::~Builder() { + /* If we have nothing remaining in dynamic heap, release it. */ + FinalizeDynamicHeapForBuildRomfs(m_program_id); + + /* Release the romfs build lock. */ + g_romfs_build_lock.Unlock(); + } + + void Builder::AddDirectory(BuildDirectoryContext **out, BuildDirectoryContext *parent_ctx, std::unique_ptr child_ctx) { /* Set parent context member. */ child_ctx->parent = parent_ctx; diff --git a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.hpp b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.hpp index 69d61686d..395ef265a 100644 --- a/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.hpp +++ b/stratosphere/ams_mitm/source/fs_mitm/fsmitm_romfs.hpp @@ -366,6 +366,7 @@ namespace ams::mitm::fs::romfs { void AddFile(BuildDirectoryContext *parent_ctx, std::unique_ptr file_ctx); public: Builder(ncm::ProgramId pr_id); + ~Builder(); void AddSdFiles(); void AddStorageFiles(ams::fs::IStorage *storage, DataSourceType source_type); diff --git a/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.cpp b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.cpp new file mode 100644 index 000000000..3de64b402 --- /dev/null +++ b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include "../amsmitm_initialization.hpp" +#include "mitm_pm_module.hpp" +#include "mitm_pm_service.hpp" + +namespace ams::mitm::pm { + + namespace { + + constexpr sm::ServiceName PmServiceName = sm::ServiceName::Encode("mitm:pm"); + constexpr size_t PmMaxSessions = 1; + + constexpr size_t MaxServers = 1; + constexpr size_t MaxSessions = PmMaxSessions; + using ServerOptions = sf::hipc::DefaultServerManagerOptions; + sf::hipc::ServerManager g_server_manager; + + constinit sf::UnmanagedServiceObject g_pm_service_object; + + } + + void MitmModule::ThreadFunction(void *) { + /* Create bpc:ams. */ + R_ABORT_UNLESS(g_server_manager.RegisterObjectForServer(g_pm_service_object.GetShared(), PmServiceName, PmMaxSessions)); + + /* Loop forever, servicing our services. */ + g_server_manager.LoopProcess(); + } + +} diff --git a/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.hpp b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.hpp new file mode 100644 index 000000000..d4e061b8d --- /dev/null +++ b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_module.hpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include +#include "../amsmitm_module.hpp" + +namespace ams::mitm::pm { + + DEFINE_MITM_MODULE_CLASS(0x1000, AMS_GET_SYSTEM_THREAD_PRIORITY(fs, WorkerThreadPool) - 2); + +} diff --git a/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.cpp b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.cpp new file mode 100644 index 000000000..6ac85f9fd --- /dev/null +++ b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include "../amsmitm_initialization.hpp" +#include "mitm_pm_service.hpp" + +namespace ams::mitm::pm { + + Result PmService::PrepareLaunchProgram(sf::Out out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, bool is_application) { + /* TODO */ + *out = 0; + AMS_UNUSED(program_id, status, is_application); + R_SUCCEED(); + } + +} diff --git a/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.hpp b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.hpp new file mode 100644 index 000000000..2d28ed182 --- /dev/null +++ b/stratosphere/ams_mitm/source/mitm_pm/mitm_pm_service.hpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::mitm::pm { + + class PmService { + public: + Result PrepareLaunchProgram(sf::Out out, ncm::ProgramId program_id, const cfg::OverrideStatus &status, bool is_application); + }; + static_assert(impl::IsIPmInterface); + +} diff --git a/stratosphere/ams_mitm/source/ns_mitm/ns_am_mitm_service.cpp b/stratosphere/ams_mitm/source/ns_mitm/ns_am_mitm_service.cpp index 11f89bafe..0754f8a7b 100644 --- a/stratosphere/ams_mitm/source/ns_mitm/ns_am_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/ns_mitm/ns_am_mitm_service.cpp @@ -27,7 +27,7 @@ namespace ams::mitm::ns { Result NsAmMitmService::ResolveApplicationContentPath(ncm::ProgramId application_id, u8 content_type) { /* Always succeed for web applets asking about HBL to enable hbl_html, and applications with manual_html to allow custom manual data. */ bool is_hbl = false; - if ((R_SUCCEEDED(pm::info::IsHblProgramId(std::addressof(is_hbl), application_id)) && is_hbl) || (static_cast(content_type) == ncm::ContentType::HtmlDocument && mitm::fs::HasSdManualHtmlContent(application_id))) { + if ((R_SUCCEEDED(ams::pm::info::IsHblProgramId(std::addressof(is_hbl), application_id)) && is_hbl) || (static_cast(content_type) == ncm::ContentType::HtmlDocument && mitm::fs::HasSdManualHtmlContent(application_id))) { nsamResolveApplicationContentPathFwd(m_forward_service.get(), static_cast(application_id), static_cast(content_type)); R_SUCCEED(); } diff --git a/stratosphere/ams_mitm/source/ns_mitm/ns_web_mitm_service.cpp b/stratosphere/ams_mitm/source/ns_mitm/ns_web_mitm_service.cpp index aa9972f65..9568f9108 100644 --- a/stratosphere/ams_mitm/source/ns_mitm/ns_web_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/ns_mitm/ns_web_mitm_service.cpp @@ -27,7 +27,7 @@ namespace ams::mitm::ns { Result NsDocumentService::ResolveApplicationContentPath(ncm::ProgramId application_id, u8 content_type) { /* Always succeed for web applets asking about HBL to enable hbl_html, and applications with manual_html to allow custom manual data. */ bool is_hbl = false; - if ((R_SUCCEEDED(pm::info::IsHblProgramId(std::addressof(is_hbl), application_id)) && is_hbl) || (static_cast(content_type) == ncm::ContentType::HtmlDocument && mitm::fs::HasSdManualHtmlContent(application_id))) { + if ((R_SUCCEEDED(ams::pm::info::IsHblProgramId(std::addressof(is_hbl), application_id)) && is_hbl) || (static_cast(content_type) == ncm::ContentType::HtmlDocument && mitm::fs::HasSdManualHtmlContent(application_id))) { nswebResolveApplicationContentPath(m_srv.get(), static_cast(application_id), static_cast(content_type)); R_SUCCEED(); } diff --git a/stratosphere/ams_mitm/source/set_mitm/set_mitm_service.cpp b/stratosphere/ams_mitm/source/set_mitm/set_mitm_service.cpp index 07686ff77..0915a10ab 100644 --- a/stratosphere/ams_mitm/source/set_mitm/set_mitm_service.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/set_mitm_service.cpp @@ -32,7 +32,7 @@ namespace ams::mitm::settings { SetMitmService::SetMitmService(std::shared_ptr<::Service> &&s, const sm::MitmProcessInfo &c) : sf::MitmServiceImplBase(std::forward>(s), c) { if (m_client_info.program_id == ncm::SystemProgramId::Ns) { os::ProcessId application_process_id; - if (R_SUCCEEDED(pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id))) && g_application_process_id == application_process_id) { + if (R_SUCCEEDED(ams::pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id))) && g_application_process_id == application_process_id) { m_locale = g_application_locale; m_is_valid_language = g_valid_language; m_is_valid_region = g_valid_region; @@ -61,8 +61,8 @@ namespace ams::mitm::settings { if (is_ns) { /* When NS asks for a locale, refresh to get the current application locale. */ - R_TRY(pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id))); - R_TRY(pm::info::GetProgramId(std::addressof(program_id), application_process_id)); + R_TRY(ams::pm::dmnt::GetApplicationProcessId(std::addressof(application_process_id))); + R_TRY(ams::pm::info::GetProgramId(std::addressof(program_id), application_process_id)); } m_locale = cfg::GetOverrideLocale(program_id); m_is_valid_language = settings::IsValidLanguageCode(m_locale.language_code); diff --git a/stratosphere/ams_mitm/source/set_mitm/setmitm_module.cpp b/stratosphere/ams_mitm/source/set_mitm/setmitm_module.cpp index 68579bb40..c756c028f 100644 --- a/stratosphere/ams_mitm/source/set_mitm/setmitm_module.cpp +++ b/stratosphere/ams_mitm/source/set_mitm/setmitm_module.cpp @@ -29,7 +29,7 @@ namespace ams::mitm::settings { PortIndex_Count, }; - constexpr sm::ServiceName SetMitmServiceName = sm::ServiceName::Encode("set"); + constexpr sm::ServiceName SetMitmServiceName = sm::ServiceName::Encode("set"); constexpr sm::ServiceName SetSysMitmServiceName = sm::ServiceName::Encode("set:sys"); struct ServerOptions { diff --git a/stratosphere/pm/source/impl/pm_process_manager.cpp b/stratosphere/pm/source/impl/pm_process_manager.cpp index 6026f061c..748a4f7fc 100644 --- a/stratosphere/pm/source/impl/pm_process_manager.cpp +++ b/stratosphere/pm/source/impl/pm_process_manager.cpp @@ -244,6 +244,22 @@ namespace ams::pm::impl { /* If we fail after now, unpin. */ ON_RESULT_FAILURE { ldr::pm::UnpinProgram(pin_id); }; + /* Ensure we can talk to mitm services. */ + { + AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(bool, s_initialized_mitm, false); + if (!s_initialized_mitm) { + mitm::pm::Initialize(); + s_initialized_mitm = true; + } + } + + /* Determine boost size for mitm. */ + u64 mitm_boost_size = 0; + R_TRY(mitm::pm::PrepareLaunchProgram(std::addressof(mitm_boost_size), program_info.program_id, override_status, is_application)); + + R_ABORT_UNLESS(BoostSystemMemoryResourceLimitForMitm(mitm_boost_size)); + ON_RESULT_FAILURE_2 { R_ABORT_UNLESS(BoostSystemMemoryResourceLimitForMitm(0)); }; + /* Ensure resources are available. */ resource::WaitResourceAvailable(std::addressof(program_info)); diff --git a/stratosphere/pm/source/pm_shell_service.cpp b/stratosphere/pm/source/pm_shell_service.cpp index 2e9b13bf1..7248bc0e2 100644 --- a/stratosphere/pm/source/pm_shell_service.cpp +++ b/stratosphere/pm/source/pm_shell_service.cpp @@ -87,8 +87,4 @@ namespace ams::pm { R_RETURN(impl::BoostSystemThreadResourceLimit()); } - Result ShellService::AtmosphereBoostSystemMemoryResourceLimitForMitm(u64 boost_size) { - R_RETURN(impl::BoostSystemMemoryResourceLimitForMitm(boost_size)); - } - } diff --git a/stratosphere/pm/source/pm_shell_service.hpp b/stratosphere/pm/source/pm_shell_service.hpp index 0e7a3b190..bb08d80bd 100644 --- a/stratosphere/pm/source/pm_shell_service.hpp +++ b/stratosphere/pm/source/pm_shell_service.hpp @@ -34,9 +34,6 @@ namespace ams::pm { Result BoostApplicationThreadResourceLimit(); void GetBootFinishedEventHandle(sf::OutCopyHandle out); Result BoostSystemThreadResourceLimit(); - - /* Atmosphere extension command implementations. */ - Result AtmosphereBoostSystemMemoryResourceLimitForMitm(u64 boost_size); }; static_assert(pm::impl::IsIShellInterface);