1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2025-01-24 10:03:56 +00:00

PM: Refactor for R_TRY, remove gotos

This commit is contained in:
Michael Scire 2019-06-17 15:27:29 -07:00
parent c60ee15449
commit ee40dcd76f
15 changed files with 126 additions and 165 deletions

@ -1 +1 @@
Subproject commit b5f1ec02b30a3bbf8a8419f8b0cf3646071ebd2b Subproject commit 777984476ea7b1da56c9a3fd3f55575a8b899896

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
class EmbeddedBoot2 { class EmbeddedBoot2 {

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "pm_boot_mode.hpp" #include "pm_boot_mode.hpp"

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
@ -23,7 +23,7 @@ enum BootModeCmd {
BootMode_Cmd_SetMaintenanceBoot = 1 BootMode_Cmd_SetMaintenanceBoot = 1
}; };
class BootModeService final : public IServiceObject { class BootModeService final : public IServiceObject {
private: private:
/* Actual commands. */ /* Actual commands. */
void GetBootMode(Out<u32> out); void GetBootMode(Out<u32> out);

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "pm_registration.hpp" #include "pm_registration.hpp"
@ -43,7 +43,7 @@ Result DebugMonitorService::LaunchDebugProcess(u64 pid) {
Result DebugMonitorService::GetTitleProcessId(Out<u64> pid, u64 tid) { Result DebugMonitorService::GetTitleProcessId(Out<u64> pid, u64 tid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList()); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> proc = Registration::GetProcessByTitleId(tid); std::shared_ptr<Registration::Process> proc = Registration::GetProcessByTitleId(tid);
if (proc != nullptr) { if (proc != nullptr) {
pid.SetValue(proc->pid); pid.SetValue(proc->pid);
@ -58,7 +58,7 @@ Result DebugMonitorService::EnableDebugForTitleId(Out<CopiedHandle> event, u64 t
Result DebugMonitorService::GetApplicationProcessId(Out<u64> pid) { Result DebugMonitorService::GetApplicationProcessId(Out<u64> pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList()); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> app_proc; std::shared_ptr<Registration::Process> app_proc;
if (Registration::HasApplicationProcess(&app_proc)) { if (Registration::HasApplicationProcess(&app_proc)) {
pid.SetValue(app_proc->pid); pid.SetValue(app_proc->pid);
@ -87,22 +87,14 @@ Result DebugMonitorService::AtmosphereGetProcessInfo(Out<CopiedHandle> proc_hand
} }
Result DebugMonitorService::AtmosphereGetCurrentLimitInfo(Out<u64> cur_val, Out<u64> lim_val, u32 category, u32 resource) { Result DebugMonitorService::AtmosphereGetCurrentLimitInfo(Out<u64> cur_val, Out<u64> lim_val, u32 category, u32 resource) {
Result rc;
if(category > ResourceLimitUtils::ResourceLimitCategory::ResourceLimitCategory_Applet) { if(category > ResourceLimitUtils::ResourceLimitCategory::ResourceLimitCategory_Applet) {
return ResultKernelInvalidEnumValue; return ResultKernelInvalidEnumValue;
} }
Handle limit_h = ResourceLimitUtils::GetResourceLimitHandleByCategory((ResourceLimitUtils::ResourceLimitCategory) category); Handle limit_h = ResourceLimitUtils::GetResourceLimitHandleByCategory((ResourceLimitUtils::ResourceLimitCategory) category);
rc = svcGetResourceLimitCurrentValue(cur_val.GetPointer(), limit_h, (LimitableResource) resource); R_TRY(svcGetResourceLimitCurrentValue(cur_val.GetPointer(), limit_h, (LimitableResource) resource));
if(R_FAILED(rc)) { R_TRY(svcGetResourceLimitLimitValue(lim_val.GetPointer(), limit_h, (LimitableResource) resource));
return rc;
}
rc = svcGetResourceLimitLimitValue(lim_val.GetPointer(), limit_h, (LimitableResource) resource);
if(R_FAILED(rc)) {
return rc;
}
return ResultSuccess; return ResultSuccess;
} }

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
@ -28,14 +28,14 @@ enum DmntCmd {
Dmnt_Cmd_EnableDebugForTitleId = 4, Dmnt_Cmd_EnableDebugForTitleId = 4,
Dmnt_Cmd_GetApplicationProcessId = 5, Dmnt_Cmd_GetApplicationProcessId = 5,
Dmnt_Cmd_EnableDebugForApplication = 6, Dmnt_Cmd_EnableDebugForApplication = 6,
Dmnt_Cmd_5X_GetDebugProcessIds = 0, Dmnt_Cmd_5X_GetDebugProcessIds = 0,
Dmnt_Cmd_5X_LaunchDebugProcess = 1, Dmnt_Cmd_5X_LaunchDebugProcess = 1,
Dmnt_Cmd_5X_GetTitleProcessId = 2, Dmnt_Cmd_5X_GetTitleProcessId = 2,
Dmnt_Cmd_5X_EnableDebugForTitleId = 3, Dmnt_Cmd_5X_EnableDebugForTitleId = 3,
Dmnt_Cmd_5X_GetApplicationProcessId = 4, Dmnt_Cmd_5X_GetApplicationProcessId = 4,
Dmnt_Cmd_5X_EnableDebugForApplication = 5, Dmnt_Cmd_5X_EnableDebugForApplication = 5,
Dmnt_Cmd_6X_DisableDebug = 6, Dmnt_Cmd_6X_DisableDebug = 6,
Dmnt_Cmd_AtmosphereGetProcessInfo = 65000, Dmnt_Cmd_AtmosphereGetProcessInfo = 65000,
@ -67,7 +67,7 @@ class DebugMonitorService final : public IServiceObject {
MakeServiceCommandMeta<Dmnt_Cmd_EnableDebugForTitleId, &DebugMonitorService::EnableDebugForTitleId, FirmwareVersion_Min, FirmwareVersion_400>(), MakeServiceCommandMeta<Dmnt_Cmd_EnableDebugForTitleId, &DebugMonitorService::EnableDebugForTitleId, FirmwareVersion_Min, FirmwareVersion_400>(),
MakeServiceCommandMeta<Dmnt_Cmd_GetApplicationProcessId, &DebugMonitorService::GetApplicationProcessId, FirmwareVersion_Min, FirmwareVersion_400>(), MakeServiceCommandMeta<Dmnt_Cmd_GetApplicationProcessId, &DebugMonitorService::GetApplicationProcessId, FirmwareVersion_Min, FirmwareVersion_400>(),
MakeServiceCommandMeta<Dmnt_Cmd_EnableDebugForApplication, &DebugMonitorService::EnableDebugForApplication, FirmwareVersion_Min, FirmwareVersion_400>(), MakeServiceCommandMeta<Dmnt_Cmd_EnableDebugForApplication, &DebugMonitorService::EnableDebugForApplication, FirmwareVersion_Min, FirmwareVersion_400>(),
/* 5.0.0-* */ /* 5.0.0-* */
MakeServiceCommandMeta<Dmnt_Cmd_5X_GetDebugProcessIds, &DebugMonitorService::GetDebugProcessIds, FirmwareVersion_500>(), MakeServiceCommandMeta<Dmnt_Cmd_5X_GetDebugProcessIds, &DebugMonitorService::GetDebugProcessIds, FirmwareVersion_500>(),
MakeServiceCommandMeta<Dmnt_Cmd_5X_LaunchDebugProcess, &DebugMonitorService::LaunchDebugProcess, FirmwareVersion_500>(), MakeServiceCommandMeta<Dmnt_Cmd_5X_LaunchDebugProcess, &DebugMonitorService::LaunchDebugProcess, FirmwareVersion_500>(),
@ -75,10 +75,10 @@ class DebugMonitorService final : public IServiceObject {
MakeServiceCommandMeta<Dmnt_Cmd_5X_EnableDebugForTitleId, &DebugMonitorService::EnableDebugForTitleId, FirmwareVersion_500>(), MakeServiceCommandMeta<Dmnt_Cmd_5X_EnableDebugForTitleId, &DebugMonitorService::EnableDebugForTitleId, FirmwareVersion_500>(),
MakeServiceCommandMeta<Dmnt_Cmd_5X_GetApplicationProcessId, &DebugMonitorService::GetApplicationProcessId, FirmwareVersion_500>(), MakeServiceCommandMeta<Dmnt_Cmd_5X_GetApplicationProcessId, &DebugMonitorService::GetApplicationProcessId, FirmwareVersion_500>(),
MakeServiceCommandMeta<Dmnt_Cmd_5X_EnableDebugForApplication, &DebugMonitorService::EnableDebugForApplication, FirmwareVersion_500>(), MakeServiceCommandMeta<Dmnt_Cmd_5X_EnableDebugForApplication, &DebugMonitorService::EnableDebugForApplication, FirmwareVersion_500>(),
/* 6.0.0-* */ /* 6.0.0-* */
MakeServiceCommandMeta<Dmnt_Cmd_6X_DisableDebug, &DebugMonitorService::DisableDebug, FirmwareVersion_600>(), MakeServiceCommandMeta<Dmnt_Cmd_6X_DisableDebug, &DebugMonitorService::DisableDebug, FirmwareVersion_600>(),
/* Atmosphere extensions. */ /* Atmosphere extensions. */
MakeServiceCommandMeta<Dmnt_Cmd_AtmosphereGetProcessInfo, &DebugMonitorService::AtmosphereGetProcessInfo>(), MakeServiceCommandMeta<Dmnt_Cmd_AtmosphereGetProcessInfo, &DebugMonitorService::AtmosphereGetProcessInfo>(),
MakeServiceCommandMeta<Dmnt_Cmd_AtmosphereGetCurrentLimitInfo, &DebugMonitorService::AtmosphereGetCurrentLimitInfo>(), MakeServiceCommandMeta<Dmnt_Cmd_AtmosphereGetCurrentLimitInfo, &DebugMonitorService::AtmosphereGetCurrentLimitInfo>(),

View file

@ -13,14 +13,14 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <switch.h> #include <switch.h>
#include "pm_registration.hpp" #include "pm_registration.hpp"
#include "pm_info.hpp" #include "pm_info.hpp"
Result InformationService::GetTitleId(Out<u64> tid, u64 pid) { Result InformationService::GetTitleId(Out<u64> tid, u64 pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList()); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid); std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
if (proc != NULL) { if (proc != NULL) {
tid.SetValue(proc->tid_sid.title_id); tid.SetValue(proc->tid_sid.title_id);

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>

View file

@ -178,7 +178,7 @@ int main(int argc, char **argv)
s_server_manager.AddWaitable(new ServiceServer<DebugMonitorService>("pm:dmnt", 3)); s_server_manager.AddWaitable(new ServiceServer<DebugMonitorService>("pm:dmnt", 3));
s_server_manager.AddWaitable(new ServiceServer<BootModeService>("pm:bm", 6)); s_server_manager.AddWaitable(new ServiceServer<BootModeService>("pm:bm", 6));
s_server_manager.AddWaitable(new ServiceServer<InformationService>("pm:info", 3)); s_server_manager.AddWaitable(new ServiceServer<InformationService>("pm:info", 3));
/* Loop forever, servicing our services. */ /* Loop forever, servicing our services. */
s_server_manager.Process(); s_server_manager.Process();

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <mutex> #include <mutex>
#include <switch.h> #include <switch.h>
@ -23,27 +23,27 @@
class ProcessWaiter final : public IWaitable { class ProcessWaiter final : public IWaitable {
public: public:
std::shared_ptr<Registration::Process> process; std::shared_ptr<Registration::Process> process;
ProcessWaiter(std::shared_ptr<Registration::Process> p) : process(p) { ProcessWaiter(std::shared_ptr<Registration::Process> p) : process(p) {
/* ... */ /* ... */
} }
std::shared_ptr<Registration::Process> GetProcess() { std::shared_ptr<Registration::Process> GetProcess() {
return this->process; return this->process;
} }
/* IWaitable */ /* IWaitable */
Handle GetHandle() override { Handle GetHandle() override {
return this->process->handle; return this->process->handle;
} }
Result HandleSignaled(u64 timeout) override { Result HandleSignaled(u64 timeout) override {
return Registration::HandleSignaledProcess(this->GetProcess()); return Registration::HandleSignaledProcess(this->GetProcess());
} }
}; };
class ProcessList final { class ProcessList final {
private: private:
HosRecursiveMutex m; HosRecursiveMutex m;
HosRecursiveMutex *GetMutex() { HosRecursiveMutex *GetMutex() {
@ -51,15 +51,15 @@ class ProcessList final {
} }
public: public:
std::vector<std::shared_ptr<Registration::Process>> processes; std::vector<std::shared_ptr<Registration::Process>> processes;
void lock() { void lock() {
GetMutex()->lock(); GetMutex()->lock();
} }
void unlock() { void unlock() {
GetMutex()->unlock(); GetMutex()->unlock();
} }
void try_lock() { void try_lock() {
GetMutex()->try_lock(); GetMutex()->try_lock();
} }

View file

@ -24,7 +24,7 @@ static ProcessList g_process_list;
static ProcessList g_dead_process_list; static ProcessList g_dead_process_list;
static IEvent *g_process_launch_start_event = nullptr; static IEvent *g_process_launch_start_event = nullptr;
static HosSemaphore g_sema_finish_launch; static HosSignal g_finish_launch_signal;
static HosMutex g_process_launch_mutex; static HosMutex g_process_launch_mutex;
static Registration::ProcessLaunchState g_process_launch_state; static Registration::ProcessLaunchState g_process_launch_state;
@ -37,8 +37,6 @@ static IEvent *g_debug_title_event = nullptr;
static IEvent *g_debug_application_event = nullptr; static IEvent *g_debug_application_event = nullptr;
static IEvent *g_boot_finished_event = nullptr; static IEvent *g_boot_finished_event = nullptr;
static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
ProcessList &Registration::GetProcessList() { ProcessList &Registration::GetProcessList() {
return g_process_list; return g_process_list;
} }
@ -65,31 +63,29 @@ IWaitable *Registration::GetProcessLaunchStartEvent() {
return g_process_launch_start_event; return g_process_launch_start_event;
} }
void Registration::HandleProcessLaunch() { Result Registration::LaunchProcess(u64 *out_pid, const TidSid tid_sid, const u64 launch_flags) {
LoaderProgramInfo program_info = {0}; LoaderProgramInfo program_info = {0};
Result rc;
u64 launch_flags = g_process_launch_state.launch_flags;
u64 *out_pid = g_process_launch_state.out_pid;
Process new_process = {0}; Process new_process = {0};
new_process.tid_sid = g_process_launch_state.tid_sid; new_process.tid_sid = tid_sid;
std::memset(g_ac_buf, 0xCC, sizeof(g_ac_buf));
u8 *acid_sac = g_ac_buf, *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo); /* Clear, get accessors for access controls. */
static u8 s_ac_buf[4 * sizeof(LoaderProgramInfo)];
std::memset(s_ac_buf, 0xCC, sizeof(s_ac_buf));
u8 *acid_sac = s_ac_buf, *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
/* Check that this is a real program. */ /* Check that this is a real program. */
if (R_FAILED((rc = ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info)))) { R_TRY(ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info));
goto HANDLE_PROCESS_LAUNCH_END;
}
/* Get the resource limit handle, ensure that we can launch the program. */ /* Get the resource limit handle, ensure that we can launch the program. */
if ((program_info.application_type & 3) == 1 && HasApplicationProcess(NULL)) { if ((program_info.application_type & 3) == 1 && HasApplicationProcess()) {
rc = ResultPmApplicationRunning; return ResultPmApplicationRunning;
goto HANDLE_PROCESS_LAUNCH_END;
} }
/* Try to register the title for launch in loader... */ /* Try to register the title for launch in loader... */
if (R_FAILED((rc = ldrPmRegisterTitle(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &new_process.ldr_queue_index)))) { R_TRY(ldrPmRegisterTitle(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &new_process.ldr_queue_index));
goto HANDLE_PROCESS_LAUNCH_END; auto ldr_register_guard = SCOPE_GUARD {
} ldrPmUnregisterTitle(new_process.ldr_queue_index);
};
/* Make sure the previous application is cleaned up. */ /* Make sure the previous application is cleaned up. */
if ((program_info.application_type & 3) == 1) { if ((program_info.application_type & 3) == 1) {
@ -97,26 +93,32 @@ void Registration::HandleProcessLaunch() {
} }
/* Try to create the process... */ /* Try to create the process... */
if (R_FAILED((rc = ldrPmCreateProcess(LAUNCHFLAGS_ARGLOW(launch_flags) | LAUNCHFLAGS_ARGHIGH(launch_flags), new_process.ldr_queue_index, ResourceLimitUtils::GetResourceLimitHandle(program_info.application_type), &new_process.handle)))) { R_TRY(ldrPmCreateProcess(LAUNCHFLAGS_ARGLOW(launch_flags) | LAUNCHFLAGS_ARGHIGH(launch_flags), new_process.ldr_queue_index, ResourceLimitUtils::GetResourceLimitHandle(program_info.application_type), &new_process.handle));
goto PROCESS_CREATION_FAILED; auto proc_create_guard = SCOPE_GUARD {
} svcCloseHandle(new_process.handle);
new_process.handle = 0;
};
/* Get the new process's id. */ /* Get the new process's id. */
svcGetProcessId(&new_process.pid, new_process.handle); if (R_FAILED(svcGetProcessId(&new_process.pid, new_process.handle))) {
std::abort();
}
/* Register with FS. */ /* Register with FS. */
memcpy(fac, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size, program_info.acid_fac_size); memcpy(fac, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size, program_info.acid_fac_size);
memcpy(fah, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size + program_info.acid_fac_size, program_info.aci0_fah_size); memcpy(fah, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size + program_info.acid_fac_size, program_info.aci0_fah_size);
if (R_FAILED((rc = fsprRegisterProgram(new_process.pid, new_process.tid_sid.title_id, new_process.tid_sid.storage_id, fah, program_info.aci0_fah_size, fac, program_info.acid_fac_size)))) { R_TRY(fsprRegisterProgram(new_process.pid, new_process.tid_sid.title_id, new_process.tid_sid.storage_id, fah, program_info.aci0_fah_size, fac, program_info.acid_fac_size));
goto FS_REGISTRATION_FAILED; auto fs_register_guard = SCOPE_GUARD {
} fsprUnregisterProgram(new_process.pid);
};
/* Register with SM. */ /* Register with SM. */
memcpy(acid_sac, program_info.ac_buffer, program_info.acid_sac_size); memcpy(acid_sac, program_info.ac_buffer, program_info.acid_sac_size);
memcpy(aci0_sac, program_info.ac_buffer + program_info.acid_sac_size, program_info.aci0_sac_size); memcpy(aci0_sac, program_info.ac_buffer + program_info.acid_sac_size, program_info.aci0_sac_size);
if (R_FAILED((rc = smManagerRegisterProcess(new_process.pid, acid_sac, program_info.acid_sac_size, aci0_sac, program_info.aci0_sac_size)))) { R_TRY(smManagerRegisterProcess(new_process.pid, acid_sac, program_info.acid_sac_size, aci0_sac, program_info.aci0_sac_size));
goto SM_REGISTRATION_FAILED; auto sm_register_guard = SCOPE_GUARD {
} smManagerUnregisterProcess(new_process.pid);
};
/* Setup process flags. */ /* Setup process flags. */
if (program_info.application_type & 1) { if (program_info.application_type & 1) {
@ -134,61 +136,46 @@ void Registration::HandleProcessLaunch() {
/* Add process to the list. */ /* Add process to the list. */
Registration::AddProcessToList(std::make_shared<Registration::Process>(new_process)); Registration::AddProcessToList(std::make_shared<Registration::Process>(new_process));
auto reg_list_guard = SCOPE_GUARD {
Registration::RemoveProcessFromList(new_process.pid);
};
/* Signal, if relevant. */ /* Signal, if relevant. */
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) { if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
g_debug_title_event->Signal(); g_debug_title_event->Signal();
g_debug_on_launch_tid = 0; g_debug_on_launch_tid = 0;
rc = ResultSuccess;
} else if ((new_process.flags & PROCESSFLAGS_APPLICATION) && g_debug_next_application.load()) { } else if ((new_process.flags & PROCESSFLAGS_APPLICATION) && g_debug_next_application.load()) {
g_debug_application_event->Signal(); g_debug_application_event->Signal();
g_debug_next_application = false; g_debug_next_application = false;
rc = ResultSuccess; } else if (!(LAUNCHFLAGS_STARTSUSPENDED(launch_flags))) {
} else if (LAUNCHFLAGS_STARTSUSPENDED(launch_flags)) { R_TRY(svcStartProcess(new_process.handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size));
rc = ResultSuccess; SetProcessState(new_process.pid, ProcessState_Running);
} else {
rc = svcStartProcess(new_process.handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size);
if (R_SUCCEEDED(rc)) {
SetProcessState(new_process.pid, ProcessState_Running);
}
} }
if (R_FAILED(rc)) { /* Dismiss scope guards. */
Registration::RemoveProcessFromList(new_process.pid); reg_list_guard.Cancel();
smManagerUnregisterProcess(new_process.pid); sm_register_guard.Cancel();
} fs_register_guard.Cancel();
proc_create_guard.Cancel();
ldr_register_guard.Cancel();
SM_REGISTRATION_FAILED: /* Set output pid. */
if (R_FAILED(rc)) { *out_pid = new_process.pid;
fsprUnregisterProgram(new_process.pid);
}
FS_REGISTRATION_FAILED: return ResultSuccess;
if (R_FAILED(rc)) { }
svcCloseHandle(new_process.handle);
new_process.handle = 0;
}
PROCESS_CREATION_FAILED: void Registration::HandleProcessLaunch() {
if (R_FAILED(rc)) { /* Actually launch process. */
ldrPmUnregisterTitle(new_process.ldr_queue_index); g_process_launch_state.result = LaunchProcess(g_process_launch_state.out_pid, g_process_launch_state.tid_sid, g_process_launch_state.launch_flags);
} /* Signal to waiting thread. */
g_finish_launch_signal.Signal();
HANDLE_PROCESS_LAUNCH_END:
g_process_launch_state.result = rc;
if (R_SUCCEEDED(rc)) {
*out_pid = new_process.pid;
}
g_sema_finish_launch.Signal();
} }
Result Registration::LaunchDebugProcess(u64 pid) { Result Registration::LaunchDebugProcess(u64 pid) {
std::scoped_lock<ProcessList &> lk(GetProcessList()); std::scoped_lock<ProcessList &> lk(GetProcessList());
LoaderProgramInfo program_info = {0}; LoaderProgramInfo program_info = {0};
Result rc;
std::shared_ptr<Registration::Process> proc = GetProcess(pid); std::shared_ptr<Registration::Process> proc = GetProcess(pid);
if (proc == NULL) { if (proc == NULL) {
@ -200,15 +187,13 @@ Result Registration::LaunchDebugProcess(u64 pid) {
} }
/* Check that this is a real program. */ /* Check that this is a real program. */
if (R_FAILED((rc = ldrPmGetProgramInfo(proc->tid_sid.title_id, proc->tid_sid.storage_id, &program_info)))) { R_TRY(ldrPmGetProgramInfo(proc->tid_sid.title_id, proc->tid_sid.storage_id, &program_info));
return rc;
}
if (R_SUCCEEDED((rc = svcStartProcess(proc->handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size)))) { /* Actually start the process. */
proc->state = ProcessState_Running; R_TRY(svcStartProcess(proc->handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size));
}
return rc; proc->state = ProcessState_Running;
return ResultSuccess;
} }
Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) { Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) {
@ -220,8 +205,9 @@ Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 lau
g_process_launch_state.out_pid = out_pid; g_process_launch_state.out_pid = out_pid;
/* Start a launch, and wait for it to exit. */ /* Start a launch, and wait for it to exit. */
g_finish_launch_signal.Reset();
g_process_launch_start_event->Signal(); g_process_launch_start_event->Signal();
g_sema_finish_launch.Wait(); g_finish_launch_signal.Wait();
return g_process_launch_state.result; return g_process_launch_state.result;
} }

View file

@ -170,7 +170,10 @@ class Registration {
u64* out_pid; u64* out_pid;
Result result; Result result;
}; };
private:
static Result LaunchProcess(u64 *out_pid, const TidSid tid_sid, const u64 launch_flags);
public:
/* TODO: Better evaluate public vs private API. */
static void InitializeSystemResources(); static void InitializeSystemResources();
static IWaitable *GetProcessLaunchStartEvent(); static IWaitable *GetProcessLaunchStartEvent();
static ProcessList &GetProcessList(); static ProcessList &GetProcessList();
@ -203,7 +206,7 @@ class Registration {
static void SignalBootFinished(); static void SignalBootFinished();
static bool HasApplicationProcess(std::shared_ptr<Process> *out); static bool HasApplicationProcess(std::shared_ptr<Process> *out = nullptr);
}; };
#include "pm_process_wait.hpp" #include "pm_process_wait.hpp"

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
@ -81,27 +81,24 @@ static u64 g_system_boost_size = 0;
/* Tries to set Resource limits for a category. */ /* Tries to set Resource limits for a category. */
static Result SetResourceLimits(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) { static Result SetResourceLimits(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) {
Result rc = ResultSuccess;
u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory]; u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory];
g_resource_limits[category][LimitableResource_Memory] = new_memory_size; g_resource_limits[category][LimitableResource_Memory] = new_memory_size;
for (unsigned int r = 0; r < 5; r++) { for (unsigned int r = 0; r < 5; r++) {
if (R_FAILED((rc = svcSetResourceLimitLimitValue(g_resource_limit_handles[category], (LimitableResource)r, g_resource_limits[category][r])))) { R_TRY_CLEANUP(svcSetResourceLimitLimitValue(g_resource_limit_handles[category], (LimitableResource)r, g_resource_limits[category][r]), {
g_resource_limits[category][LimitableResource_Memory] = old_memory_size; g_resource_limits[category][LimitableResource_Memory] = old_memory_size;
return rc; });
}
} }
return rc; return ResultSuccess;
} }
static Result SetNewMemoryResourceLimit(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) { static Result SetNewMemoryResourceLimit(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) {
Result rc = ResultSuccess;
u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory]; u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory];
g_resource_limits[category][LimitableResource_Memory] = new_memory_size; g_resource_limits[category][LimitableResource_Memory] = new_memory_size;
if (R_FAILED((rc = svcSetResourceLimitLimitValue(g_resource_limit_handles[category], LimitableResource_Memory, g_resource_limits[category][LimitableResource_Memory])))) { R_TRY_CLEANUP(svcSetResourceLimitLimitValue(g_resource_limit_handles[category], LimitableResource_Memory, g_resource_limits[category][LimitableResource_Memory]), {
g_resource_limits[category][LimitableResource_Memory] = old_memory_size; g_resource_limits[category][LimitableResource_Memory] = old_memory_size;
} });
return rc; return ResultSuccess;
} }
void ResourceLimitUtils::InitializeLimits() { void ResourceLimitUtils::InitializeLimits() {
@ -133,39 +130,39 @@ void ResourceLimitUtils::InitializeLimits() {
memcpy(&g_memory_resource_limits, &g_memory_resource_limits_deprecated, sizeof(g_memory_resource_limits_deprecated)); memcpy(&g_memory_resource_limits, &g_memory_resource_limits_deprecated, sizeof(g_memory_resource_limits_deprecated));
memcpy(&g_resource_limits, &g_resource_limits_deprecated, sizeof(g_resource_limits)); memcpy(&g_resource_limits, &g_resource_limits_deprecated, sizeof(g_resource_limits));
} }
/* 7.0.0+: Nintendo restricts the number of system threads here, from 0x260 -> 0x60. */ /* 7.0.0+: Nintendo restricts the number of system threads here, from 0x260 -> 0x60. */
/* We will not do this. */ /* We will not do this. */
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_600) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_600) {
/* NOTE: 5 is a fake type, official code does not do this. */ /* NOTE: 5 is a fake type, official code does not do this. */
/* This is done for ease of backwards compatibility. */ /* This is done for ease of backwards compatibility. */
g_memory_limit_type = 5; g_memory_limit_type = 5;
/* Starting 6.x, 5 MB of memory is always reserved for system. */ /* Starting 6.x, 5 MB of memory is always reserved for system. */
const u64 reserved_system_size_6x = 0x500000; const u64 reserved_system_size_6x = 0x500000;
/* Get total memory available. */ /* Get total memory available. */
u64 total_memory = 0; u64 total_memory = 0;
if (R_FAILED(svcGetResourceLimitLimitValue(&total_memory, g_resource_limit_handles[0], LimitableResource_Memory))) { if (R_FAILED(svcGetResourceLimitLimitValue(&total_memory, g_resource_limit_handles[0], LimitableResource_Memory))) {
std::abort(); std::abort();
} }
/* Get and save application + applet memory. */ /* Get and save application + applet memory. */
if (R_FAILED(svcGetSystemInfo(&g_memory_resource_limits[g_memory_limit_type][1], 0, 0, 0)) || if (R_FAILED(svcGetSystemInfo(&g_memory_resource_limits[g_memory_limit_type][1], 0, 0, 0)) ||
R_FAILED(svcGetSystemInfo(&g_memory_resource_limits[g_memory_limit_type][2], 0, 0, 1))) { R_FAILED(svcGetSystemInfo(&g_memory_resource_limits[g_memory_limit_type][2], 0, 0, 1))) {
std::abort(); std::abort();
} }
const u64 application_size = g_memory_resource_limits[g_memory_limit_type][1]; const u64 application_size = g_memory_resource_limits[g_memory_limit_type][1];
const u64 applet_size = g_memory_resource_limits[g_memory_limit_type][2]; const u64 applet_size = g_memory_resource_limits[g_memory_limit_type][2];
const u64 reserved_nonsys_size = (application_size + applet_size + reserved_system_size_6x); const u64 reserved_nonsys_size = (application_size + applet_size + reserved_system_size_6x);
/* Ensure there's enough memory for system region. */ /* Ensure there's enough memory for system region. */
if (reserved_nonsys_size > total_memory) { if (reserved_nonsys_size > total_memory) {
std::abort(); std::abort();
} }
/* Set System memory. */ /* Set System memory. */
g_memory_resource_limits[g_memory_limit_type][0] = total_memory - (reserved_nonsys_size); g_memory_resource_limits[g_memory_limit_type][0] = total_memory - (reserved_nonsys_size);
} else { } else {
@ -249,49 +246,32 @@ Handle ResourceLimitUtils::GetResourceLimitHandleByCategory(ResourceLimitCategor
} }
Result ResourceLimitUtils::BoostSystemMemoryResourceLimit(u64 boost_size) { Result ResourceLimitUtils::BoostSystemMemoryResourceLimit(u64 boost_size) {
Result rc = ResultSuccess;
if (boost_size > g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application]) { if (boost_size > g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application]) {
return ResultPmInvalidSize; return ResultPmInvalidSize;
} }
u64 app_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application] - boost_size; u64 app_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application] - boost_size;
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) {
if (boost_size < g_system_boost_size) { if (boost_size < g_system_boost_size) {
if (R_FAILED((rc = svcSetUnsafeLimit(boost_size)))) { R_TRY(svcSetUnsafeLimit(boost_size));
return rc; R_TRY(SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size));
}
if (R_FAILED((rc = SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size)))) {
return rc;
}
} else { } else {
if (R_FAILED((rc = SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size)))) { R_TRY(SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size));
return rc; R_TRY(svcSetUnsafeLimit(boost_size));
}
if (R_FAILED((rc = svcSetUnsafeLimit(boost_size)))) {
return rc;
}
} }
} else if (GetRuntimeFirmwareVersion() >= FirmwareVersion_400) { } else if (GetRuntimeFirmwareVersion() >= FirmwareVersion_400) {
u64 sys_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_System] + boost_size; u64 sys_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_System] + boost_size;
if (boost_size < g_system_boost_size) { if (boost_size < g_system_boost_size) {
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_System, sys_size)))) { R_TRY(SetResourceLimits(ResourceLimitCategory_System, sys_size));
return rc; R_TRY(SetResourceLimits(ResourceLimitCategory_Application, app_size));
}
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_Application, app_size)))) {
return rc;
}
} else { } else {
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_Application, app_size)))) { R_TRY(SetResourceLimits(ResourceLimitCategory_Application, app_size));
return rc; R_TRY(SetResourceLimits(ResourceLimitCategory_System, sys_size));
}
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_System, sys_size)))) {
return rc;
}
} }
} else { } else {
rc = ResultKernelConnectionClosed; return ResultKernelConnectionClosed;
} }
if (R_SUCCEEDED(rc)) {
g_system_boost_size = boost_size; g_system_boost_size = boost_size;
} return ResultSuccess;
return rc;
} }

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>