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

View file

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

View file

@ -78,15 +78,17 @@ static bool IsHexadecimal(const char *str) {
void Utils::InitializeThreadFunc(void *args) {
/* Get required services. */
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
/* TODO: Panic */
} else {
svcCloseHandle(tmp_hnd);
DoWithSmSession([&]() {
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
/* TODO: Panic */
} else {
svcCloseHandle(tmp_hnd);
}
}
}
});
/* Mount SD. */
while (R_FAILED(fsMountSdcard(&g_sd_filesystem))) {
@ -197,7 +199,11 @@ void Utils::InitializeThreadFunc(void *args) {
Utils::RefreshConfiguration();
/* Initialize set:sys. */
setsysInitialize();
DoWithSmSession([&]() {
if (R_FAILED(setsysInitialize())) {
std::abort();
}
});
/* Signal SD is initialized. */
g_has_initialized = true;
@ -209,13 +215,15 @@ void Utils::InitializeThreadFunc(void *args) {
g_sd_signal.Signal();
/* Initialize HID. */
{
while (R_FAILED(hidInitialize())) {
while (!g_has_hid_session) {
DoWithSmSession([&]() {
if (R_SUCCEEDED(hidInitialize())) {
g_has_hid_session = true;
}
});
if (!g_has_hid_session) {
svcSleepThread(1000000ULL);
}
g_has_hid_session = true;
}
}

View file

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

View file

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

View file

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

View file

@ -71,55 +71,52 @@ void __appInit(void) {
SetFirmwareVersionForLibnx();
rc = smInitialize();
if (R_FAILED(rc)) {
fatalSimple(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
}
DoWithSmSession([&]() {
rc = pmdmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = pmdmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = ldrDmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = ldrDmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = roDmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = roDmntInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = nsdevInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = nsdevInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = lrInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = lrInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = setInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = setInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = setsysInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = setsysInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = hidInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = hidInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = fsInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
rc = fsInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
});
rc = fsdevMountSdmc();
if (R_FAILED(rc)) {
@ -141,7 +138,6 @@ void __appExit(void) {
roDmntExit();
ldrDmntExit();
pmdmntExit();
smExit();
}
int main(int argc, char **argv)

View file

@ -76,70 +76,67 @@ void __appInit(void) {
SetFirmwareVersionForLibnx();
rc = smInitialize();
if (R_FAILED(rc)) {
std::abort();
}
DoWithSmSession([&]() {
rc = setInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = setInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = setsysInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = setsysInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = pminfoInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = pminfoInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = i2cInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = i2cInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = bpcInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = bpcInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = pcvInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = pcvInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = lblInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = lblInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = psmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = psmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = spsmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = spsmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = plInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = plInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = gpioInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = gpioInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
});
rc = fsdevMountSdmc();
if (R_FAILED(rc)) {
@ -164,7 +161,6 @@ void __appExit(void) {
pminfoExit();
setsysExit();
setExit();
smExit();
}
int main(int argc, char **argv)

View file

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

View file

@ -95,7 +95,10 @@ Result ShowFatalTask::PrepareScreenForDrawing() {
Result rc = ResultSuccess;
/* Connect to vi. */
if (R_FAILED((rc = viInitialize(ViServiceType_Manager)))) {
DoWithSmSession([&]() {
rc = viInitialize(ViServiceType_Manager);
});
if (R_FAILED(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 */
if (R_FAILED(rc = fsldrInitialize())) {
DoWithSmSession([&]() {
rc = fsldrInitialize();
});
if (R_FAILED(rc)) {
return rc;
}
ON_SCOPE_EXIT { fsldrExit(); };
if (R_FAILED(rc = fsldrOpenCodeFileSystem(tid, path, &g_CodeFileSystem))) {
fsldrExit();
return rc;
}
fsdevMountDevice("code", g_CodeFileSystem);
TryMountHblNspOnSd();
fsldrExit();
return rc;
}
@ -372,17 +373,21 @@ void ContentManagement::RefreshConfigurationData() {
void ContentManagement::TryMountSdCard() {
/* 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)) {
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
return;
} else {
svcCloseHandle(tmp_hnd);
bool can_mount = true;
DoWithSmSession([&]() {
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
can_mount = false;
break;
} else {
svcCloseHandle(tmp_hnd);
}
}
}
});
if (R_SUCCEEDED(fsdevMountSdmc())) {
if (can_mount && R_SUCCEEDED(fsdevMountSdmc())) {
g_has_initialized_fs_dev = true;
}
}

View file

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

View file

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

View file

@ -105,7 +105,11 @@ static bool GetGpioPadLow(GpioPadName pad) {
static bool IsMaintenanceMode() {
/* Contact set:sys, retrieve boot!force_maintenance. */
if (R_SUCCEEDED(setsysInitialize())) {
Result rc;
DoWithSmSession([&]() {
rc = setsysInitialize();
});
if (R_SUCCEEDED(rc)) {
ON_SCOPE_EXIT { setsysExit(); };
u8 force_maintenance = 1;
@ -116,7 +120,10 @@ static bool IsMaintenanceMode() {
}
/* Contact GPIO, read plus/minus buttons. */
if (R_SUCCEEDED(gpioInitialize())) {
DoWithSmSession([&]() {
rc = gpioInitialize();
});
if (R_SUCCEEDED(rc)) {
ON_SCOPE_EXIT { gpioExit(); };
return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown);
@ -168,31 +175,37 @@ static const std::tuple<u64, bool> g_additional_launch_programs[] = {
};
static void MountSdCard() {
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
/* TODO: Panic */
} else {
svcCloseHandle(tmp_hnd);
DoWithSmSession([&]() {
Handle tmp_hnd = 0;
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++) {
if (R_FAILED(smGetServiceOriginal(&tmp_hnd, smEncodeName(required_active_services[i])))) {
/* TODO: Panic */
} else {
svcCloseHandle(tmp_hnd);
}
}
}
});
fsdevMountSdmc();
}
static void WaitForMitm(const char *service) {
bool mitm_installed = false;
Result rc = smManagerAmsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
Result rc;
DoWithSmSession([&]() {
if (R_FAILED((rc = smManagerAmsInitialize()))) {
std::abort();
}
});
while (R_FAILED((rc = smManagerAmsHasMitm(&mitm_installed, service))) || !mitm_installed) {
if (R_FAILED(rc)) {
std::abort();
}
svcSleepThread(1000000ull);
}
smManagerAmsExit();
}

View file

@ -99,50 +99,48 @@ void __appInit(void) {
SetFirmwareVersionForLibnx();
rc = smInitialize();
if (R_FAILED(rc)) {
std::abort();
}
DoWithSmSession([&]() {
rc = fsprInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsprInitialize();
if (R_FAILED(rc)) {
std::abort();
}
/* This works around a bug with process permissions on < 4.0.0. */
RegisterPrivilegedProcessesWithFs();
/* This works around a bug with process permissions on < 4.0.0. */
RegisterPrivilegedProcessesWithFs();
rc = smManagerAmsInitialize();
if (R_SUCCEEDED(rc)) {
smManagerAmsEndInitialDefers();
smManagerAmsExit();
} else {
std::abort();
}
rc = smManagerAmsInitialize();
if (R_SUCCEEDED(rc)) {
smManagerAmsEndInitialDefers();
} else {
std::abort();
}
rc = smManagerInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = smManagerInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = lrInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = lrInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = ldrPmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = ldrPmInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = splInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = splInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
rc = fsInitialize();
if (R_FAILED(rc)) {
std::abort();
}
});
CheckAtmosphereVersion(CURRENT_ATMOSPHERE_VERSION);
}
@ -156,7 +154,6 @@ void __appExit(void) {
fsprExit();
lrExit();
fsExit();
smExit();
}
int main(int argc, char **argv)