diff --git a/libraries/libstratosphere/include/stratosphere/sm.hpp b/libraries/libstratosphere/include/stratosphere/sm.hpp index 9e93cf0d3..df44d6990 100644 --- a/libraries/libstratosphere/include/stratosphere/sm.hpp +++ b/libraries/libstratosphere/include/stratosphere/sm.hpp @@ -19,7 +19,6 @@ #include #include #include -#include #include diff --git a/libraries/libstratosphere/include/stratosphere/sm/sm_api.hpp b/libraries/libstratosphere/include/stratosphere/sm/sm_api.hpp index 76dbf6e36..e14a417c0 100644 --- a/libraries/libstratosphere/include/stratosphere/sm/sm_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/sm/sm_api.hpp @@ -20,6 +20,10 @@ namespace ams::sm { + /* Initialization. */ + Result Initialize(); + Result Finalize(); + /* Ordinary SM API. */ Result GetService(Service *out, ServiceName name); Result RegisterService(Handle *out, ServiceName name, size_t max_sessions, bool is_light); @@ -29,17 +33,4 @@ namespace ams::sm { Result HasService(bool *out, ServiceName name); Result WaitService(ServiceName name); - /* Scoped session access. */ - namespace impl { - - void DoWithSessionImpl(void (*Invoker)(void *), void *Function); - - } - - template - NX_CONSTEXPR void DoWithSession(F f) { - auto invoker = +[](void *func) { (*(F *)func)(); }; - impl::DoWithSessionImpl(invoker, &f); - } - } diff --git a/libraries/libstratosphere/include/stratosphere/sm/sm_scoped_holder.hpp b/libraries/libstratosphere/include/stratosphere/sm/sm_scoped_holder.hpp deleted file mode 100644 index 952f44b27..000000000 --- a/libraries/libstratosphere/include/stratosphere/sm/sm_scoped_holder.hpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2018-2020 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 "sm_api.hpp" - -namespace ams::sm { - - /* Utility, for scoped access to libnx services. */ - template - class ScopedServiceHolder { - NON_COPYABLE(ScopedServiceHolder); - private: - Result result; - bool has_initialized; - public: - ScopedServiceHolder(bool initialize = true) : result(ResultSuccess()), has_initialized(false) { - if (initialize) { - this->Initialize(); - } - } - - ~ScopedServiceHolder() { - if (this->has_initialized) { - this->Finalize(); - } - } - - ScopedServiceHolder(ScopedServiceHolder&& rhs) { - this->result = rhs.result; - this->has_initialized = rhs.has_initialized; - rhs.result = ResultSuccess(); - rhs.has_initialized = false; - } - - ScopedServiceHolder &operator=(ScopedServiceHolder&& rhs) { - rhs.Swap(*this); - return *this; - } - - void Swap(ScopedServiceHolder &rhs) { - std::swap(this->result, rhs.result); - std::swap(this->has_initialized, rhs.has_initialized); - } - - explicit operator bool() const { - return this->has_initialized; - } - - Result Initialize() { - AMS_ABORT_UNLESS(!this->has_initialized); - - sm::DoWithSession([&]() { - if constexpr (std::is_same::value) { - Initializer(); - this->result = ResultSuccess(); - } else { - this->result = Initializer(); - } - }); - - this->has_initialized = R_SUCCEEDED(this->result); - return this->result; - } - - void Finalize() { - AMS_ABORT_UNLESS(this->has_initialized); - Finalizer(); - this->has_initialized = false; - } - - Result GetResult() const { - return this->result; - } - }; - -} diff --git a/libraries/libstratosphere/source/boot2/boot2_api.cpp b/libraries/libstratosphere/source/boot2/boot2_api.cpp index 30b93416d..1605d3e9b 100644 --- a/libraries/libstratosphere/source/boot2/boot2_api.cpp +++ b/libraries/libstratosphere/source/boot2/boot2_api.cpp @@ -387,8 +387,8 @@ namespace ams::boot2 { /* NOTE: Here we work around a race condition in the boot process by ensuring that settings initializes its db. */ { /* Connect to set:sys. */ - sm::ScopedServiceHolder<::setsysInitialize, ::setsysExit> setsys_holder; - AMS_ABORT_UNLESS(setsys_holder); + R_ABORT_UNLESS(::setsysInitialize()); + ON_SCOPE_EXIT { ::setsysExit(); }; /* Retrieve setting from the database. */ u8 force_maintenance = 0; @@ -424,9 +424,7 @@ namespace ams::boot2 { InitializeFsHeapForCleanup(); /* Temporarily initialize fs. */ - sm::DoWithSession([&] { - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(fsInitialize()); ON_SCOPE_EXIT { fsExit(); }; /* Wait for the sd card to be available. */ diff --git a/libraries/libstratosphere/source/crypto/crypto_csrng.os.horizon.cpp b/libraries/libstratosphere/source/crypto/crypto_csrng.os.horizon.cpp index 4ef1c0ce0..93f16d050 100644 --- a/libraries/libstratosphere/source/crypto/crypto_csrng.os.horizon.cpp +++ b/libraries/libstratosphere/source/crypto/crypto_csrng.os.horizon.cpp @@ -19,29 +19,28 @@ namespace ams::crypto { namespace { - bool g_initialized; - os::Mutex g_lock(false); + constinit bool g_initialized = false; + constinit os::SdkMutex g_lock; void InitializeCsrng() { AMS_ASSERT(!g_initialized); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(::csrngInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + R_ABORT_UNLESS(::csrngInitialize()); } } void GenerateCryptographicallyRandomBytes(void *dst, size_t dst_size) { - { + if (AMS_UNLIKELY(!g_initialized)) { std::scoped_lock lk(g_lock); - if (AMS_UNLIKELY(!g_initialized)) { + if (AMS_LIKELY(!g_initialized)) { InitializeCsrng(); g_initialized = true; } } - R_ABORT_UNLESS(csrngGetRandomBytes(dst, dst_size)); + R_ABORT_UNLESS(::csrngGetRandomBytes(dst, dst_size)); } } diff --git a/libraries/libstratosphere/source/gpio/driver/board/nintendo/nx/impl/gpio_initial_config.cpp b/libraries/libstratosphere/source/gpio/driver/board/nintendo/nx/impl/gpio_initial_config.cpp index a5a8a0e5f..e6d8a92b1 100644 --- a/libraries/libstratosphere/source/gpio/driver/board/nintendo/nx/impl/gpio_initial_config.cpp +++ b/libraries/libstratosphere/source/gpio/driver/board/nintendo/nx/impl/gpio_initial_config.cpp @@ -24,8 +24,8 @@ namespace ams::gpio::driver::board::nintendo::nx::impl { spl::HardwareType GetHardwareType() { /* Acquire access to spl: */ - sm::ScopedServiceHolder spl_holder; - AMS_ABORT_UNLESS(static_cast(spl_holder)); + spl::Initialize(); + ON_SCOPE_EXIT { spl::Finalize(); }; /* Get config. */ return ::ams::spl::GetHardwareType(); diff --git a/libraries/libstratosphere/source/hid/hid_api.cpp b/libraries/libstratosphere/source/hid/hid_api.cpp index ba07cde57..7196618b4 100644 --- a/libraries/libstratosphere/source/hid/hid_api.cpp +++ b/libraries/libstratosphere/source/hid/hid_api.cpp @@ -40,12 +40,11 @@ namespace ams::hid { /* Helper. */ void InitializeHid() { - sm::DoWithSession([&]() { - R_ABORT_UNLESS(hidInitialize()); - hidInitializeNpad(); - R_ABORT_UNLESS(hidSetSupportedNpadIdType(NpadIdTypes, NumNpadIdTypes)); - R_ABORT_UNLESS(hidSetSupportedNpadStyleSet(HidNpadStyleSet_NpadStandard | HidNpadStyleTag_NpadSystemExt)); - }); + R_ABORT_UNLESS(sm::Initialize()); + R_ABORT_UNLESS(hidInitialize()); + hidInitializeNpad(); + R_ABORT_UNLESS(hidSetSupportedNpadIdType(NpadIdTypes, NumNpadIdTypes)); + R_ABORT_UNLESS(hidSetSupportedNpadStyleSet(HidNpadStyleSet_NpadStandard | HidNpadStyleTag_NpadSystemExt)); } Result EnsureHidInitialized() { diff --git a/libraries/libstratosphere/source/htcs/client/htcs_session.cpp b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp index c41276bb0..687da86d0 100644 --- a/libraries/libstratosphere/source/htcs/client/htcs_session.cpp +++ b/libraries/libstratosphere/source/htcs/client/htcs_session.cpp @@ -229,10 +229,11 @@ namespace ams::htcs::client { } void InitializeSessionManager(tma::IHtcsManager **out_manager, tma::IHtcsManager **out_monitor, u32 num_sessions) { + /* Ensure we can contact the libnx wrapper. */ + R_ABORT_UNLESS(sm::Initialize()); + /* Initialize the libnx wrapper. */ - sm::DoWithSession([&] { - R_ABORT_UNLESS(::htcsInitialize(num_sessions)); - }); + R_ABORT_UNLESS(::htcsInitialize(num_sessions)); /* Create the output objects. */ *out_manager = ObjectFactory::CreateSharedEmplaced().Detach(); diff --git a/libraries/libstratosphere/source/sm/sm_api.cpp b/libraries/libstratosphere/source/sm/sm_api.cpp index ef7be4e54..104abb80e 100644 --- a/libraries/libstratosphere/source/sm/sm_api.cpp +++ b/libraries/libstratosphere/source/sm/sm_api.cpp @@ -18,47 +18,52 @@ namespace ams::sm { + namespace { + + constinit int g_ref_count = 0; + constinit os::SdkMutex g_mutex; + + } + + /* Initialization. */ + Result Initialize() { + std::scoped_lock lk(g_mutex); + + if (g_ref_count > 0) { + ++g_ref_count; + } else { + R_TRY(::smInitialize()); + g_ref_count = 1; + } + + return ResultSuccess(); + } + + Result Finalize() { + /* NOTE: Nintendo does nothing here. */ + return ResultSuccess(); + } + /* Ordinary SM API. */ Result GetService(Service *out, ServiceName name) { - return impl::DoWithUserSession([&]() { - return smGetServiceWrapper(out, impl::ConvertName(name)); - }); + return smGetServiceWrapper(out, impl::ConvertName(name)); } Result RegisterService(Handle *out, ServiceName name, size_t max_sessions, bool is_light) { - return impl::DoWithUserSession([&]() { - return smRegisterService(out, impl::ConvertName(name), is_light, static_cast(max_sessions)); - }); + return smRegisterService(out, impl::ConvertName(name), is_light, static_cast(max_sessions)); } Result UnregisterService(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smUnregisterService(impl::ConvertName(name)); - }); + return smUnregisterService(impl::ConvertName(name)); } /* Atmosphere extensions. */ Result HasService(bool *out, ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereHasService(out, impl::ConvertName(name)); - }); + return smAtmosphereHasService(out, impl::ConvertName(name)); } Result WaitService(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereWaitService(impl::ConvertName(name)); - }); - } - - namespace impl { - - void DoWithSessionImpl(void (*Invoker)(void *), void *Function) { - impl::DoWithUserSession([&]() { - Invoker(Function); - return ResultSuccess(); - }); - } - + return smAtmosphereWaitService(impl::ConvertName(name)); } } diff --git a/libraries/libstratosphere/source/sm/sm_mitm_api.cpp b/libraries/libstratosphere/source/sm/sm_mitm_api.cpp index cdf07e7ad..ca3edef7a 100644 --- a/libraries/libstratosphere/source/sm/sm_mitm_api.cpp +++ b/libraries/libstratosphere/source/sm/sm_mitm_api.cpp @@ -26,21 +26,15 @@ namespace ams::sm::mitm { } Result UninstallMitm(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereMitmUninstall(impl::ConvertName(name)); - }); + return smAtmosphereMitmUninstall(impl::ConvertName(name)); } Result DeclareFutureMitm(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereMitmDeclareFuture(impl::ConvertName(name)); - }); + return smAtmosphereMitmDeclareFuture(impl::ConvertName(name)); } Result ClearFutureMitm(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereMitmClearFuture(impl::ConvertName(name)); - }); + return smAtmosphereMitmClearFuture(impl::ConvertName(name)); } Result AcknowledgeSession(Service *out_service, MitmProcessInfo *out_info, ServiceName name) { @@ -50,15 +44,11 @@ namespace ams::sm::mitm { } Result HasMitm(bool *out, ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereHasMitm(out, impl::ConvertName(name)); - }); + return smAtmosphereHasMitm(out, impl::ConvertName(name)); } Result WaitMitm(ServiceName name) { - return impl::DoWithUserSession([&]() { - return smAtmosphereWaitMitm(impl::ConvertName(name)); - }); + return smAtmosphereWaitMitm(impl::ConvertName(name)); } } diff --git a/libraries/libstratosphere/source/sm/sm_utils.cpp b/libraries/libstratosphere/source/sm/sm_utils.cpp index 2cfefc23c..f241e79e2 100644 --- a/libraries/libstratosphere/source/sm/sm_utils.cpp +++ b/libraries/libstratosphere/source/sm/sm_utils.cpp @@ -21,17 +21,12 @@ namespace ams::sm::impl { namespace { /* Globals. */ - os::Mutex g_user_session_mutex(true); - os::Mutex g_mitm_ack_session_mutex(true); - os::Mutex g_per_thread_session_mutex(true); + constinit os::Mutex g_mitm_ack_session_mutex(true); + constinit os::Mutex g_per_thread_session_mutex(true); } /* Utilities. */ - os::Mutex &GetUserSessionMutex() { - return g_user_session_mutex; - } - os::Mutex &GetMitmAcknowledgementSessionMutex() { return g_mitm_ack_session_mutex; } diff --git a/libraries/libstratosphere/source/sm/sm_utils.hpp b/libraries/libstratosphere/source/sm/sm_utils.hpp index d869f9cd1..f8926c619 100644 --- a/libraries/libstratosphere/source/sm/sm_utils.hpp +++ b/libraries/libstratosphere/source/sm/sm_utils.hpp @@ -21,24 +21,9 @@ namespace ams::sm::impl { /* Utilities. */ - os::Mutex &GetUserSessionMutex(); os::Mutex &GetMitmAcknowledgementSessionMutex(); os::Mutex &GetPerThreadSessionMutex(); - template - Result DoWithUserSession(F f) { - std::scoped_lock lk(GetUserSessionMutex()); - { - R_ABORT_UNLESS(smInitialize()); - ON_SCOPE_EXIT { - R_ABORT_UNLESS(smDetachClient()); - smExit(); - }; - - return f(); - } - } - template Result DoWithMitmAcknowledgementSession(F f) { std::scoped_lock lk(GetMitmAcknowledgementSessionMutex()); diff --git a/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp b/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp index 9efa5e3ef..7b57a0902 100644 --- a/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp +++ b/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp @@ -197,9 +197,8 @@ namespace ams::socket::impl { const auto service_type = config.IsSystemClient() ? (1 << 1) : (1 << 0); - sm::DoWithSession([&] { - R_ABORT_UNLESS(::bsdInitialize(std::addressof(libnx_config), static_cast(config.GetConcurrencyCountMax()), service_type)); - }); + R_ABORT_UNLESS(sm::Initialize()); + R_ABORT_UNLESS(::bsdInitialize(std::addressof(libnx_config), static_cast(config.GetConcurrencyCountMax()), service_type)); } /* Set the heap generation. */ diff --git a/libraries/libstratosphere/source/updater/updater_api.cpp b/libraries/libstratosphere/source/updater/updater_api.cpp index 00071b8e4..cec4f92e2 100644 --- a/libraries/libstratosphere/source/updater/updater_api.cpp +++ b/libraries/libstratosphere/source/updater/updater_api.cpp @@ -549,8 +549,8 @@ namespace ams::updater { } /* Get a session to ncm. */ - sm::ScopedServiceHolder ncm_holder; - R_ABORT_UNLESS(ncm_holder.GetResult()); + ncm::Initialize(); + ON_SCOPE_EXIT { ncm::Finalize(); }; /* Verify normal, verify safe as needed. */ if (verification_state.needs_verify_normal) { diff --git a/libraries/libvapours/source/sdmmc/impl/sdmmc_device_detector.cpp b/libraries/libvapours/source/sdmmc/impl/sdmmc_device_detector.cpp index 3529b85a0..4deae1590 100644 --- a/libraries/libvapours/source/sdmmc/impl/sdmmc_device_detector.cpp +++ b/libraries/libvapours/source/sdmmc/impl/sdmmc_device_detector.cpp @@ -58,7 +58,9 @@ namespace ams::sdmmc::impl { void DeviceDetector::DetectorThread() { /* Initialize the gpio session. */ - sm::DoWithSession([] { gpio::Initialize(); }); + gpio::Initialize(); + + /* Open and configure the pad session. */ gpio::OpenSession(std::addressof(this->gpio_pad_session), this->gpio_device_code); gpio::SetDirection(std::addressof(this->gpio_pad_session), gpio::Direction_Input); gpio::SetDebounceTime(std::addressof(this->gpio_pad_session), this->gpio_debounce_ms); diff --git a/stratosphere/TioServer/source/tio_main.cpp b/stratosphere/TioServer/source/tio_main.cpp index 9bf97dac4..a56b1b107 100644 --- a/stratosphere/TioServer/source/tio_main.cpp +++ b/stratosphere/TioServer/source/tio_main.cpp @@ -110,9 +110,9 @@ void __appInit(void) { /* Disable FS auto-abort. */ fs::SetEnabledAutoAbort(false); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); ams::CheckApiVersion(); } diff --git a/stratosphere/ams_mitm/source/amsmitm_initialization.cpp b/stratosphere/ams_mitm/source/amsmitm_initialization.cpp index ecd395a61..722c0e630 100644 --- a/stratosphere/ams_mitm/source/amsmitm_initialization.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_initialization.cpp @@ -172,10 +172,8 @@ namespace ams::mitm { } /* Connect to set:sys. */ - sm::DoWithSession([]() { - R_ABORT_UNLESS(setInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - }); + R_ABORT_UNLESS(setInitialize()); + R_ABORT_UNLESS(setsysInitialize()); /* Load settings off the SD card. */ settings::fwdbg::InitializeSdCardKeyValueStore(); diff --git a/stratosphere/ams_mitm/source/amsmitm_main.cpp b/stratosphere/ams_mitm/source/amsmitm_main.cpp index 6f2757132..0a30d0cc0 100644 --- a/stratosphere/ams_mitm/source/amsmitm_main.cpp +++ b/stratosphere/ams_mitm/source/amsmitm_main.cpp @@ -76,13 +76,13 @@ void __libnx_initheap(void) { void __appInit(void) { hos::InitializeForStratosphere(); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - R_ABORT_UNLESS(pmdmntInitialize()); - R_ABORT_UNLESS(pminfoInitialize()); - ncm::Initialize(); - spl::InitializeForFs(); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + R_ABORT_UNLESS(pmdmntInitialize()); + R_ABORT_UNLESS(pminfoInitialize()); + ncm::Initialize(); + spl::InitializeForFs(); /* Disable auto-abort in fs operations. */ fs::SetEnabledAutoAbort(false); diff --git a/stratosphere/ams_mitm/source/sysupdater/sysupdater_module.cpp b/stratosphere/ams_mitm/source/sysupdater/sysupdater_module.cpp index ce25b0312..61eb1da07 100644 --- a/stratosphere/ams_mitm/source/sysupdater/sysupdater_module.cpp +++ b/stratosphere/ams_mitm/source/sysupdater/sysupdater_module.cpp @@ -50,7 +50,7 @@ namespace ams::mitm::sysupdater { mitm::WaitInitialized(); /* Connect to nim. */ - sm::DoWithSession([]() { nim::InitializeForNetworkInstallManager(); }); + nim::InitializeForNetworkInstallManager(); ON_SCOPE_EXIT { nim::FinalizeForNetworkInstallManager(); }; /* Register ams:su. */ diff --git a/stratosphere/boot/source/boot_main.cpp b/stratosphere/boot/source/boot_main.cpp index c7a2cb69e..32e3eca32 100644 --- a/stratosphere/boot/source/boot_main.cpp +++ b/stratosphere/boot/source/boot_main.cpp @@ -126,12 +126,12 @@ void __appInit(void) { fs::SetAllocator(Allocate, Deallocate); - /* Initialize services we need (TODO: NCM) */ - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - spl::Initialize(); - R_ABORT_UNLESS(pmshellInitialize()); - }); + /* Initialize services we need. */ + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + spl::Initialize(); + R_ABORT_UNLESS(pmshellInitialize()); ams::CheckApiVersion(); } diff --git a/stratosphere/boot2/source/boot2_main.cpp b/stratosphere/boot2/source/boot2_main.cpp index 8c2efe241..a1333ea92 100644 --- a/stratosphere/boot2/source/boot2_main.cpp +++ b/stratosphere/boot2/source/boot2_main.cpp @@ -90,14 +90,14 @@ void __appInit(void) { fs::SetAllocator(AllocateForFs, DeallocateForFs); /* Initialize services we need. */ - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - R_ABORT_UNLESS(pmbmInitialize()); - R_ABORT_UNLESS(pminfoInitialize()); - R_ABORT_UNLESS(pmshellInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - gpio::Initialize(); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + R_ABORT_UNLESS(pmbmInitialize()); + R_ABORT_UNLESS(pminfoInitialize()); + R_ABORT_UNLESS(pmshellInitialize()); + R_ABORT_UNLESS(setsysInitialize()); + gpio::Initialize(); /* Mount the SD card. */ R_ABORT_UNLESS(fs::MountSdCard("sdmc")); diff --git a/stratosphere/creport/source/creport_crash_report.cpp b/stratosphere/creport/source/creport_crash_report.cpp index df446498d..669da57dc 100644 --- a/stratosphere/creport/source/creport_crash_report.cpp +++ b/stratosphere/creport/source/creport_crash_report.cpp @@ -41,8 +41,12 @@ namespace ams::creport { /* Try to get the current time. */ { - sm::ScopedServiceHolder time_holder; - return time_holder && R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out)); + if (R_FAILED(::timeInitialize())) { + return false; + } + ON_SCOPE_EXIT { ::timeExit(); }; + + return R_SUCCEEDED(::timeGetCurrentTime(TimeType_LocalSystemClock, out)); } } @@ -337,8 +341,9 @@ namespace ams::creport { /* Since we save reports only locally and do not send them via telemetry, we will skip this. */ AMS_UNUSED(enable_screenshot); if (hos::GetVersion() >= hos::Version_9_0_0 && this->IsApplication()) { - sm::ScopedServiceHolder capssc_holder; - if (capssc_holder) { + if (R_SUCCEEDED(capsrv::InitializeScreenShotControl())) { + ON_SCOPE_EXIT { capsrv::FinalizeScreenShotControl(); }; + u64 jpeg_size; if (R_SUCCEEDED(capsrv::CaptureJpegScreenshot(std::addressof(jpeg_size), this->heap_storage, sizeof(this->heap_storage), vi::LayerStack_ApplicationForDebug, TimeSpan::FromSeconds(10)))) { util::SNPrintf(file_path, sizeof(file_path), "sdmc:/atmosphere/crash_reports/%011lu_%016lx.jpg", timestamp, this->process_info.program_id); diff --git a/stratosphere/creport/source/creport_main.cpp b/stratosphere/creport/source/creport_main.cpp index 847da6cff..d26616b3a 100644 --- a/stratosphere/creport/source/creport_main.cpp +++ b/stratosphere/creport/source/creport_main.cpp @@ -92,9 +92,9 @@ void __appInit(void) { InitializeFsHeap(); fs::SetAllocator(AllocateForFs, DeallocateForFs); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); R_ABORT_UNLESS(fs::MountSdCard("sdmc")); } @@ -179,14 +179,16 @@ int main(int argc, char **argv) { if (hos::GetVersion() < hos::Version_11_0_0 || !enable_jit_debug) { if (hos::GetVersion() >= hos::Version_10_0_0) { /* On 10.0.0+, use pgl to terminate. */ - sm::ScopedServiceHolder pgl_holder; - if (pgl_holder) { + if (R_SUCCEEDED(pgl::Initialize())) { + ON_SCOPE_EXIT { pgl::Finalize(); }; + pgl::TerminateProcess(crashed_pid); } } else { /* On < 10.0.0, use ns:dev to terminate. */ - sm::ScopedServiceHolder ns_holder; - if (ns_holder) { + if (R_SUCCEEDED(::nsdevInitialize())) { + ON_SCOPE_EXIT { ::nsdevExit(); }; + nsdevTerminateProcess(static_cast(crashed_pid)); } } diff --git a/stratosphere/cs/source/cs_main.cpp b/stratosphere/cs/source/cs_main.cpp index 479f82012..85de59736 100644 --- a/stratosphere/cs/source/cs_main.cpp +++ b/stratosphere/cs/source/cs_main.cpp @@ -114,13 +114,13 @@ void __appInit(void) { fs::SetAllocator(cs::Allocate, cs::Deallocate); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - lr::Initialize(); - R_ABORT_UNLESS(ldr::InitializeForShell()); - R_ABORT_UNLESS(pgl::Initialize()); - /* TODO: Other services? */ - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + lr::Initialize(); + R_ABORT_UNLESS(ldr::InitializeForShell()); + R_ABORT_UNLESS(pgl::Initialize()); + /* TODO: Other services? */ ams::CheckApiVersion(); } diff --git a/stratosphere/dmnt/source/dmnt_main.cpp b/stratosphere/dmnt/source/dmnt_main.cpp index bfa05f9bf..881b5cf2b 100644 --- a/stratosphere/dmnt/source/dmnt_main.cpp +++ b/stratosphere/dmnt/source/dmnt_main.cpp @@ -83,18 +83,18 @@ void __appInit(void) { InitializeFsHeap(); fs::SetAllocator(AllocateForFs, DeallocateForFs); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(pmdmntInitialize()); - R_ABORT_UNLESS(pminfoInitialize()); - R_ABORT_UNLESS(ldrDmntInitialize()); - R_ABORT_UNLESS(roDmntInitialize()); - R_ABORT_UNLESS(nsdevInitialize()); - lr::Initialize(); - R_ABORT_UNLESS(setInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(hidInitialize()); - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(pmdmntInitialize()); + R_ABORT_UNLESS(pminfoInitialize()); + R_ABORT_UNLESS(ldrDmntInitialize()); + R_ABORT_UNLESS(roDmntInitialize()); + R_ABORT_UNLESS(nsdevInitialize()); + lr::Initialize(); + R_ABORT_UNLESS(setInitialize()); + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(hidInitialize()); + R_ABORT_UNLESS(fsInitialize()); R_ABORT_UNLESS(fs::MountSdCard("sdmc")); diff --git a/stratosphere/erpt/source/erpt_main.cpp b/stratosphere/erpt/source/erpt_main.cpp index 2a0597bb9..85c917e49 100644 --- a/stratosphere/erpt/source/erpt_main.cpp +++ b/stratosphere/erpt/source/erpt_main.cpp @@ -66,16 +66,16 @@ void __libnx_initheap(void) { void __appInit(void) { hos::InitializeForStratosphere(); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(setInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(pscmInitialize()); - R_ABORT_UNLESS(time::Initialize()); - if (hos::GetVersion() >= hos::Version_11_0_0) { - R_ABORT_UNLESS(ectxrInitialize()); - } - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(setInitialize()); + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(pscmInitialize()); + R_ABORT_UNLESS(time::Initialize()); + if (hos::GetVersion() >= hos::Version_11_0_0) { + R_ABORT_UNLESS(ectxrInitialize()); + } + R_ABORT_UNLESS(fsInitialize()); ams::CheckApiVersion(); } diff --git a/stratosphere/fatal/source/fatal_main.cpp b/stratosphere/fatal/source/fatal_main.cpp index cb2a32a69..5e10fb08f 100644 --- a/stratosphere/fatal/source/fatal_main.cpp +++ b/stratosphere/fatal/source/fatal_main.cpp @@ -99,26 +99,26 @@ void __appInit(void) { fatal::InitializeFsHeap(); fs::SetAllocator(fatal::AllocateForFs, fatal::DeallocateForFs); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(setInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(pminfoInitialize()); - R_ABORT_UNLESS(i2cInitialize()); - R_ABORT_UNLESS(bpcInitialize()); + R_ABORT_UNLESS(sm::Initialize()); - if (hos::GetVersion() >= hos::Version_8_0_0) { - R_ABORT_UNLESS(clkrstInitialize()); - } else { - R_ABORT_UNLESS(pcvInitialize()); - } + R_ABORT_UNLESS(setInitialize()); + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(pminfoInitialize()); + R_ABORT_UNLESS(i2cInitialize()); + R_ABORT_UNLESS(bpcInitialize()); - R_ABORT_UNLESS(lblInitialize()); - R_ABORT_UNLESS(psmInitialize()); - R_ABORT_UNLESS(spsmInitialize()); - R_ABORT_UNLESS(plInitialize(::PlServiceType_User)); - gpio::Initialize(); - R_ABORT_UNLESS(fsInitialize()); - }); + if (hos::GetVersion() >= hos::Version_8_0_0) { + R_ABORT_UNLESS(clkrstInitialize()); + } else { + R_ABORT_UNLESS(pcvInitialize()); + } + + R_ABORT_UNLESS(lblInitialize()); + R_ABORT_UNLESS(psmInitialize()); + R_ABORT_UNLESS(spsmInitialize()); + R_ABORT_UNLESS(plInitialize(::PlServiceType_User)); + gpio::Initialize(); + R_ABORT_UNLESS(fsInitialize()); R_ABORT_UNLESS(fs::MountSdCard("sdmc")); diff --git a/stratosphere/fatal/source/fatal_task_error_report.cpp b/stratosphere/fatal/source/fatal_task_error_report.cpp index 85dc92d54..ef9eed08c 100644 --- a/stratosphere/fatal/source/fatal_task_error_report.cpp +++ b/stratosphere/fatal/source/fatal_task_error_report.cpp @@ -41,8 +41,12 @@ namespace ams::fatal::srv { /* Try to get the current time. */ { - sm::ScopedServiceHolder time_holder; - return time_holder && R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out)); + if (R_FAILED(::timeInitialize())) { + return false; + } + ON_SCOPE_EXIT { ::timeExit(); }; + + return R_SUCCEEDED(::timeGetCurrentTime(TimeType_LocalSystemClock, out)); } } diff --git a/stratosphere/fatal/source/fatal_task_screen.cpp b/stratosphere/fatal/source/fatal_task_screen.cpp index 08f30c77e..415163ae3 100644 --- a/stratosphere/fatal/source/fatal_task_screen.cpp +++ b/stratosphere/fatal/source/fatal_task_screen.cpp @@ -484,9 +484,7 @@ namespace ams::fatal::srv { PreRenderFrameBuffer(); /* Prepare screen for drawing. */ - sm::DoWithSession([&]() { - R_ABORT_UNLESS(PrepareScreenForDrawing()); - }); + R_ABORT_UNLESS(PrepareScreenForDrawing()); /* Display the pre-rendered frame. */ this->DisplayPreRenderedFrame(); diff --git a/stratosphere/htc/source/htc_main.cpp b/stratosphere/htc/source/htc_main.cpp index cfe5db814..14347e342 100644 --- a/stratosphere/htc/source/htc_main.cpp +++ b/stratosphere/htc/source/htc_main.cpp @@ -104,12 +104,12 @@ void __appInit(void) { fs::SetAllocator(htc::Allocate, htc::Deallocate); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(setcalInitialize()); - R_ABORT_UNLESS(pscmInitialize()); - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(setcalInitialize()); + R_ABORT_UNLESS(pscmInitialize()); + R_ABORT_UNLESS(fsInitialize()); R_ABORT_UNLESS(fs::MountSdCard("sdmc")); diff --git a/stratosphere/jpegdec/source/jpegdec_main.cpp b/stratosphere/jpegdec/source/jpegdec_main.cpp index 560e240b9..ad7d8aa4b 100644 --- a/stratosphere/jpegdec/source/jpegdec_main.cpp +++ b/stratosphere/jpegdec/source/jpegdec_main.cpp @@ -62,6 +62,8 @@ void __libnx_initheap(void) { void __appInit(void) { hos::InitializeForStratosphere(); ams::CheckApiVersion(); + + R_ABORT_UNLESS(sm::Initialize()); } void __appExit(void) { diff --git a/stratosphere/loader/source/ldr_main.cpp b/stratosphere/loader/source/ldr_main.cpp index ba7b9b273..09900851f 100644 --- a/stratosphere/loader/source/ldr_main.cpp +++ b/stratosphere/loader/source/ldr_main.cpp @@ -145,12 +145,12 @@ void __appInit(void) { fs::SetAllocator(ldr::Allocate, ldr::Deallocate); /* Initialize services we need. */ - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - lr::Initialize(); - R_ABORT_UNLESS(fsldrInitialize()); - spl::Initialize(); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + lr::Initialize(); + R_ABORT_UNLESS(fsldrInitialize()); + spl::Initialize(); ams::CheckApiVersion(); } diff --git a/stratosphere/ncm/source/ncm_main.cpp b/stratosphere/ncm/source/ncm_main.cpp index 0b31c0bf5..16065c4f2 100644 --- a/stratosphere/ncm/source/ncm_main.cpp +++ b/stratosphere/ncm/source/ncm_main.cpp @@ -93,10 +93,10 @@ void __appInit(void) { fs::SetAllocator(Allocate, Deallocate); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsInitialize()); - spl::Initialize(); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(fsInitialize()); + spl::Initialize(); ams::CheckApiVersion(); } diff --git a/stratosphere/pgl/source/pgl_main.cpp b/stratosphere/pgl/source/pgl_main.cpp index c8ff203e8..2330e29b7 100644 --- a/stratosphere/pgl/source/pgl_main.cpp +++ b/stratosphere/pgl/source/pgl_main.cpp @@ -70,14 +70,14 @@ void __appInit(void) { fs::SetAllocator(pgl::srv::Allocate, pgl::srv::Deallocate); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(setInitialize()); - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(pmshellInitialize()); - R_ABORT_UNLESS(ldrShellInitialize()); - R_ABORT_UNLESS(lrInitialize()); - R_ABORT_UNLESS(fsInitialize()); - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(setInitialize()); + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(pmshellInitialize()); + R_ABORT_UNLESS(ldrShellInitialize()); + R_ABORT_UNLESS(lrInitialize()); + R_ABORT_UNLESS(fsInitialize()); ams::CheckApiVersion(); } diff --git a/stratosphere/pm/source/pm_main.cpp b/stratosphere/pm/source/pm_main.cpp index caabc856b..6cd95e5e2 100644 --- a/stratosphere/pm/source/pm_main.cpp +++ b/stratosphere/pm/source/pm_main.cpp @@ -129,20 +129,20 @@ namespace { void __appInit(void) { hos::InitializeForStratosphere(); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(fsprInitialize()); - R_ABORT_UNLESS(smManagerInitialize()); + R_ABORT_UNLESS(sm::Initialize()); - /* This works around a bug with process permissions on < 4.0.0. */ - /* It also informs SM of privileged process information. */ - RegisterPrivilegedProcesses(); + R_ABORT_UNLESS(fsprInitialize()); + R_ABORT_UNLESS(smManagerInitialize()); - /* Use AMS manager extension to tell SM that FS has been worked around. */ - R_ABORT_UNLESS(sm::manager::EndInitialDefers()); + /* This works around a bug with process permissions on < 4.0.0. */ + /* It also informs SM of privileged process information. */ + RegisterPrivilegedProcesses(); - R_ABORT_UNLESS(ldrPmInitialize()); - spl::Initialize(); - }); + /* Use AMS manager extension to tell SM that FS has been worked around. */ + R_ABORT_UNLESS(sm::manager::EndInitialDefers()); + + R_ABORT_UNLESS(ldrPmInitialize()); + spl::Initialize(); ams::CheckApiVersion(); } diff --git a/stratosphere/ro/source/ro_main.cpp b/stratosphere/ro/source/ro_main.cpp index d49e28409..47249e885 100644 --- a/stratosphere/ro/source/ro_main.cpp +++ b/stratosphere/ro/source/ro_main.cpp @@ -145,14 +145,14 @@ void __appInit(void) { fs::SetAllocator(ro::Allocate, ro::Deallocate); - sm::DoWithSession([&]() { - R_ABORT_UNLESS(setsysInitialize()); - R_ABORT_UNLESS(fsInitialize()); - spl::Initialize(); - if (hos::GetVersion() < hos::Version_3_0_0) { - R_ABORT_UNLESS(pminfoInitialize()); - } - }); + R_ABORT_UNLESS(sm::Initialize()); + + R_ABORT_UNLESS(setsysInitialize()); + R_ABORT_UNLESS(fsInitialize()); + spl::Initialize(); + if (hos::GetVersion() < hos::Version_3_0_0) { + R_ABORT_UNLESS(pminfoInitialize()); + } R_ABORT_UNLESS(fs::MountSdCard("sdmc")); diff --git a/stratosphere/spl/source/spl_main.cpp b/stratosphere/spl/source/spl_main.cpp index 1fe05c15c..dbd48321e 100644 --- a/stratosphere/spl/source/spl_main.cpp +++ b/stratosphere/spl/source/spl_main.cpp @@ -77,6 +77,7 @@ void __appInit(void) { hos::InitializeForStratosphere(); /* SPL doesn't really access any services... */ + R_ABORT_UNLESS(sm::Initialize()); ams::CheckApiVersion(); }