stratosphere: only hold sm sessions open when needed

This commit is contained in:
Michael Scire 2019-04-22 12:40:53 -07:00
parent c3875796df
commit b09adb6a34
16 changed files with 432 additions and 403 deletions

View file

@ -73,15 +73,12 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
}
rc = fsInitialize(); rc = fsInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_FS)); std::abort();
} }
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION); CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);
} }
@ -89,7 +86,6 @@ void __appInit(void) {
void __appExit(void) { void __appExit(void) {
/* Cleanup services. */ /* Cleanup services. */
fsExit(); fsExit();
smExit();
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -100,10 +96,10 @@ int main(int argc, char **argv)
LaunchAllMitmModules(); LaunchAllMitmModules();
if (R_FAILED(initializer_thread.Initialize(&Utils::InitializeThreadFunc, NULL, 0x4000, 0x15))) { if (R_FAILED(initializer_thread.Initialize(&Utils::InitializeThreadFunc, NULL, 0x4000, 0x15))) {
/* TODO: Panic. */ std::abort();
} }
if (R_FAILED(initializer_thread.Start())) { if (R_FAILED(initializer_thread.Start())) {
/* TODO: Panic. */ std::abort();
} }
/* Wait for all mitm modules to end. */ /* Wait for all mitm modules to end. */

View file

@ -35,12 +35,12 @@ void NsMitmMain(void *arg) {
Utils::WaitSdInitialized(); Utils::WaitSdInitialized();
/* Ensure we can talk to NS. */ /* Ensure we can talk to NS. */
{ DoWithSmSession([&]() {
if (R_FAILED(nsInitialize())) { if (R_FAILED(nsInitialize())) {
std::abort(); std::abort();
} }
nsExit(); nsExit();
} });
/* Create server manager */ /* Create server manager */
auto server_manager = new WaitableManager(1); auto server_manager = new WaitableManager(1);

View file

@ -78,6 +78,7 @@ static bool IsHexadecimal(const char *str) {
void Utils::InitializeThreadFunc(void *args) { void Utils::InitializeThreadFunc(void *args) {
/* Get required services. */ /* Get required services. */
DoWithSmSession([&]() {
Handle tmp_hnd = 0; Handle tmp_hnd = 0;
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"}; static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) { for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
@ -87,6 +88,7 @@ void Utils::InitializeThreadFunc(void *args) {
svcCloseHandle(tmp_hnd); svcCloseHandle(tmp_hnd);
} }
} }
});
/* Mount SD. */ /* Mount SD. */
while (R_FAILED(fsMountSdcard(&g_sd_filesystem))) { while (R_FAILED(fsMountSdcard(&g_sd_filesystem))) {
@ -197,7 +199,11 @@ void Utils::InitializeThreadFunc(void *args) {
Utils::RefreshConfiguration(); Utils::RefreshConfiguration();
/* Initialize set:sys. */ /* Initialize set:sys. */
setsysInitialize(); DoWithSmSession([&]() {
if (R_FAILED(setsysInitialize())) {
std::abort();
}
});
/* Signal SD is initialized. */ /* Signal SD is initialized. */
g_has_initialized = true; g_has_initialized = true;
@ -209,13 +215,15 @@ void Utils::InitializeThreadFunc(void *args) {
g_sd_signal.Signal(); g_sd_signal.Signal();
/* Initialize HID. */ /* Initialize HID. */
{ while (!g_has_hid_session) {
DoWithSmSession([&]() {
while (R_FAILED(hidInitialize())) { if (R_SUCCEEDED(hidInitialize())) {
g_has_hid_session = true;
}
});
if (!g_has_hid_session) {
svcSleepThread(1000000ULL); svcSleepThread(1000000ULL);
} }
g_has_hid_session = true;
} }
} }

View file

@ -80,11 +80,7 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
/* Initialize services we need (TODO: NCM) */ /* Initialize services we need (TODO: NCM) */
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize(); rc = fsInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
@ -104,6 +100,7 @@ void __appInit(void) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
} }
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION); CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);
} }
@ -114,7 +111,6 @@ void __appExit(void) {
pmshellExit(); pmshellExit();
splExit(); splExit();
fsExit(); fsExit();
smExit();
} }
typedef enum { typedef enum {

View file

@ -240,19 +240,28 @@ bool CrashReport::GetCurrentTime(u64 *out) {
/* Verify that pcv isn't dead. */ /* Verify that pcv isn't dead. */
{ {
bool has_time_service;
DoWithSmSession([&]() {
Handle dummy; Handle dummy;
if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) { if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) {
svcCloseHandle(dummy); svcCloseHandle(dummy);
has_time_service = false;
} else {
has_time_service = true;
}
});
if (!has_time_service) {
return false; return false;
} }
} }
/* Try to get the current time. */ /* Try to get the current time. */
bool success = false; bool success = true;
if (R_SUCCEEDED(timeInitialize())) { DoWithSmSession([&]() {
if (R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out))) { success &= R_SUCCEEDED(timeInitialize());
success = true; });
} if (success) {
success &= R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out));
timeExit(); timeExit();
} }
return success; return success;

View file

@ -69,15 +69,12 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
}
rc = fsInitialize(); rc = fsInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_FS)); fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_FS));
} }
});
rc = fsdevMountSdmc(); rc = fsdevMountSdmc();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
@ -89,7 +86,6 @@ void __appExit(void) {
/* Cleanup services. */ /* Cleanup services. */
fsdevUnmountAll(); fsdevUnmountAll();
fsExit(); fsExit();
smExit();
} }
static u64 creport_parse_u64(char *s) { static u64 creport_parse_u64(char *s) {
@ -127,10 +123,12 @@ int main(int argc, char **argv) {
if (g_Creport.WasSuccessful()) { if (g_Creport.WasSuccessful()) {
g_Creport.SaveReport(); g_Creport.SaveReport();
DoWithSmSession([&]() {
if (R_SUCCEEDED(nsdevInitialize())) { if (R_SUCCEEDED(nsdevInitialize())) {
nsdevTerminateProcess(crashed_pid); nsdevTerminateProcess(crashed_pid);
nsdevExit(); nsdevExit();
} }
});
/* Don't fatal if we have extra info. */ /* Don't fatal if we have extra info. */
if (kernelAbove500()) { if (kernelAbove500()) {

View file

@ -71,11 +71,7 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
}
rc = pmdmntInitialize(); rc = pmdmntInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
fatalSimple(rc); fatalSimple(rc);
@ -120,6 +116,7 @@ void __appInit(void) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
fatalSimple(rc); fatalSimple(rc);
} }
});
rc = fsdevMountSdmc(); rc = fsdevMountSdmc();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
@ -141,7 +138,6 @@ void __appExit(void) {
roDmntExit(); roDmntExit();
ldrDmntExit(); ldrDmntExit();
pmdmntExit(); pmdmntExit();
smExit();
} }
int main(int argc, char **argv) int main(int argc, char **argv)

View file

@ -76,11 +76,7 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
std::abort();
}
rc = setInitialize(); rc = setInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
@ -140,6 +136,7 @@ void __appInit(void) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
} }
});
rc = fsdevMountSdmc(); rc = fsdevMountSdmc();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
@ -164,7 +161,6 @@ void __appExit(void) {
pminfoExit(); pminfoExit();
setsysExit(); setsysExit();
setExit(); setExit();
smExit();
} }
int main(int argc, char **argv) int main(int argc, char **argv)

View file

@ -38,19 +38,28 @@ bool ErrorReportTask::GetCurrentTime(u64 *out) {
/* Verify that pcv isn't dead. */ /* Verify that pcv isn't dead. */
{ {
bool has_time_service;
DoWithSmSession([&]() {
Handle dummy; Handle dummy;
if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) { if (R_SUCCEEDED(smRegisterService(&dummy, "time:s", false, 0x20))) {
svcCloseHandle(dummy); svcCloseHandle(dummy);
has_time_service = false;
} else {
has_time_service = true;
}
});
if (!has_time_service) {
return false; return false;
} }
} }
/* Try to get the current time. */ /* Try to get the current time. */
bool success = false; bool success = true;
if (R_SUCCEEDED(timeInitialize())) { DoWithSmSession([&]() {
if (R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out))) { success &= R_SUCCEEDED(timeInitialize());
success = true; });
} if (success) {
success &= R_SUCCEEDED(timeGetCurrentTime(TimeType_LocalSystemClock, out));
timeExit(); timeExit();
} }
return success; return success;

View file

@ -95,7 +95,10 @@ Result ShowFatalTask::PrepareScreenForDrawing() {
Result rc = ResultSuccess; Result rc = ResultSuccess;
/* Connect to vi. */ /* Connect to vi. */
if (R_FAILED((rc = viInitialize(ViServiceType_Manager)))) { DoWithSmSession([&]() {
rc = viInitialize(ViServiceType_Manager);
});
if (R_FAILED(rc)) {
return rc; return rc;
} }

@ -1 +1 @@
Subproject commit 880bce9092fbef0bcbf101b8ec2e3d2c5af3fb98 Subproject commit 79bc9bf8d87dddcfc2d080626eb8c817c7339fd0

View file

@ -94,19 +94,20 @@ Result ContentManagement::MountCode(u64 tid, FsStorageId sid) {
} }
/* Always re-initialize fsp-ldr, in case it's closed */ /* Always re-initialize fsp-ldr, in case it's closed */
if (R_FAILED(rc = fsldrInitialize())) { DoWithSmSession([&]() {
rc = fsldrInitialize();
});
if (R_FAILED(rc)) {
return rc; return rc;
} }
ON_SCOPE_EXIT { fsldrExit(); };
if (R_FAILED(rc = fsldrOpenCodeFileSystem(tid, path, &g_CodeFileSystem))) { if (R_FAILED(rc = fsldrOpenCodeFileSystem(tid, path, &g_CodeFileSystem))) {
fsldrExit();
return rc; return rc;
} }
fsdevMountDevice("code", g_CodeFileSystem); fsdevMountDevice("code", g_CodeFileSystem);
TryMountHblNspOnSd(); TryMountHblNspOnSd();
fsldrExit();
return rc; return rc;
} }
@ -372,17 +373,21 @@ void ContentManagement::RefreshConfigurationData() {
void ContentManagement::TryMountSdCard() { void ContentManagement::TryMountSdCard() {
/* Mount SD card, if psc, bus, and pcv have been created. */ /* Mount SD card, if psc, bus, and pcv have been created. */
if (!g_has_initialized_fs_dev && HasCreatedTitle(TitleId_Psc) && HasCreatedTitle(TitleId_Bus) && HasCreatedTitle(TitleId_Pcv)) { if (!g_has_initialized_fs_dev && HasCreatedTitle(TitleId_Psc) && HasCreatedTitle(TitleId_Bus) && HasCreatedTitle(TitleId_Pcv)) {
bool can_mount = true;
DoWithSmSession([&]() {
Handle tmp_hnd = 0; Handle tmp_hnd = 0;
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"}; static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) { for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) { if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
return; can_mount = false;
break;
} else { } else {
svcCloseHandle(tmp_hnd); svcCloseHandle(tmp_hnd);
} }
} }
});
if (R_SUCCEEDED(fsdevMountSdmc())) { if (can_mount && R_SUCCEEDED(fsdevMountSdmc())) {
g_has_initialized_fs_dev = true; g_has_initialized_fs_dev = true;
} }
} }

View file

@ -25,9 +25,15 @@ Result HidManagement::GetKeysHeld(u64 *keys) {
return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID); return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID);
} }
if (!serviceIsActive(hidGetSessionService()) && R_FAILED(hidInitialize())) { if (!serviceIsActive(hidGetSessionService())) {
Result rc;
DoWithSmSession([&]() {
rc = hidInitialize();
});
if (R_FAILED(rc)) {
return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID); return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID);
} }
}
hidScanInput(); hidScanInput();
*keys = hidKeysHeld(CONTROLLER_P1_AUTO); *keys = hidKeysHeld(CONTROLLER_P1_AUTO);

View file

@ -72,11 +72,7 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
/* Initialize services we need (TODO: SPL) */ /* Initialize services we need (TODO: SPL) */
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize(); rc = fsInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
@ -91,6 +87,8 @@ void __appInit(void) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
} }
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION); CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);
} }
@ -101,7 +99,6 @@ void __appExit(void) {
fsldrExit(); fsldrExit();
lrExit(); lrExit();
fsExit(); fsExit();
smExit();
} }
struct LoaderServerOptions { struct LoaderServerOptions {

View file

@ -105,7 +105,11 @@ static bool GetGpioPadLow(GpioPadName pad) {
static bool IsMaintenanceMode() { static bool IsMaintenanceMode() {
/* Contact set:sys, retrieve boot!force_maintenance. */ /* Contact set:sys, retrieve boot!force_maintenance. */
if (R_SUCCEEDED(setsysInitialize())) { Result rc;
DoWithSmSession([&]() {
rc = setsysInitialize();
});
if (R_SUCCEEDED(rc)) {
ON_SCOPE_EXIT { setsysExit(); }; ON_SCOPE_EXIT { setsysExit(); };
u8 force_maintenance = 1; u8 force_maintenance = 1;
@ -116,7 +120,10 @@ static bool IsMaintenanceMode() {
} }
/* Contact GPIO, read plus/minus buttons. */ /* Contact GPIO, read plus/minus buttons. */
if (R_SUCCEEDED(gpioInitialize())) { DoWithSmSession([&]() {
rc = gpioInitialize();
});
if (R_SUCCEEDED(rc)) {
ON_SCOPE_EXIT { gpioExit(); }; ON_SCOPE_EXIT { gpioExit(); };
return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown); return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown);
@ -168,6 +175,7 @@ static const std::tuple<u64, bool> g_additional_launch_programs[] = {
}; };
static void MountSdCard() { static void MountSdCard() {
DoWithSmSession([&]() {
Handle tmp_hnd = 0; Handle tmp_hnd = 0;
static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"}; static const char * const required_active_services[] = {"pcv", "gpio", "pinmux", "psc:c"};
for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) { for (unsigned int i = 0; i < sizeof(required_active_services) / sizeof(required_active_services[0]); i++) {
@ -177,22 +185,27 @@ static void MountSdCard() {
svcCloseHandle(tmp_hnd); svcCloseHandle(tmp_hnd);
} }
} }
});
fsdevMountSdmc(); fsdevMountSdmc();
} }
static void WaitForMitm(const char *service) { static void WaitForMitm(const char *service) {
bool mitm_installed = false; bool mitm_installed = false;
Result rc = smManagerAmsInitialize(); Result rc;
if (R_FAILED(rc)) { DoWithSmSession([&]() {
if (R_FAILED((rc = smManagerAmsInitialize()))) {
std::abort(); std::abort();
} }
});
while (R_FAILED((rc = smManagerAmsHasMitm(&mitm_installed, service))) || !mitm_installed) { while (R_FAILED((rc = smManagerAmsHasMitm(&mitm_installed, service))) || !mitm_installed) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
} }
svcSleepThread(1000000ull); svcSleepThread(1000000ull);
} }
smManagerAmsExit(); smManagerAmsExit();
} }

View file

@ -99,11 +99,7 @@ void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
rc = smInitialize(); DoWithSmSession([&]() {
if (R_FAILED(rc)) {
std::abort();
}
rc = fsprInitialize(); rc = fsprInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
@ -115,6 +111,7 @@ void __appInit(void) {
rc = smManagerAmsInitialize(); rc = smManagerAmsInitialize();
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
smManagerAmsEndInitialDefers(); smManagerAmsEndInitialDefers();
smManagerAmsExit();
} else { } else {
std::abort(); std::abort();
} }
@ -143,6 +140,7 @@ void __appInit(void) {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
std::abort(); std::abort();
} }
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION); CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);
} }
@ -156,7 +154,6 @@ void __appExit(void) {
fsprExit(); fsprExit();
lrExit(); lrExit();
fsExit(); fsExit();
smExit();
} }
int main(int argc, char **argv) int main(int argc, char **argv)