strat: no longer materially constrained by sm session limit

This commit is contained in:
Michael Scire 2021-04-13 23:58:10 -07:00
parent 997e4dd665
commit 2e1a93f1d1
37 changed files with 215 additions and 333 deletions

View file

@ -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. */

View file

@ -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));
}
}

View file

@ -24,8 +24,8 @@ namespace ams::gpio::driver::board::nintendo::nx::impl {
spl::HardwareType GetHardwareType() {
/* Acquire access to spl: */
sm::ScopedServiceHolder<spl::Initialize, spl::Finalize> spl_holder;
AMS_ABORT_UNLESS(static_cast<bool>(spl_holder));
spl::Initialize();
ON_SCOPE_EXIT { spl::Finalize(); };
/* Get config. */
return ::ams::spl::GetHardwareType();

View file

@ -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() {

View file

@ -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<tma::IHtcsManager, RemoteManager>().Detach();

View file

@ -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<int>(max_sessions));
});
return smRegisterService(out, impl::ConvertName(name), is_light, static_cast<int>(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));
}
}

View file

@ -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));
}
}

View file

@ -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;
}

View file

@ -21,24 +21,9 @@
namespace ams::sm::impl {
/* Utilities. */
os::Mutex &GetUserSessionMutex();
os::Mutex &GetMitmAcknowledgementSessionMutex();
os::Mutex &GetPerThreadSessionMutex();
template<typename F>
Result DoWithUserSession(F f) {
std::scoped_lock lk(GetUserSessionMutex());
{
R_ABORT_UNLESS(smInitialize());
ON_SCOPE_EXIT {
R_ABORT_UNLESS(smDetachClient());
smExit();
};
return f();
}
}
template<typename F>
Result DoWithMitmAcknowledgementSession(F f) {
std::scoped_lock lk(GetMitmAcknowledgementSessionMutex());

View file

@ -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<u32>(config.GetConcurrencyCountMax()), service_type));
});
R_ABORT_UNLESS(sm::Initialize());
R_ABORT_UNLESS(::bsdInitialize(std::addressof(libnx_config), static_cast<u32>(config.GetConcurrencyCountMax()), service_type));
}
/* Set the heap generation. */

View file

@ -549,8 +549,8 @@ namespace ams::updater {
}
/* Get a session to ncm. */
sm::ScopedServiceHolder<ncm::Initialize, ncm::Finalize> 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) {