2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-05-04 05:58:25 +00:00
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "pm_registration.hpp"
|
2018-05-07 09:50:33 +00:00
|
|
|
#include "pm_resource_limits.hpp"
|
2018-05-04 05:58:25 +00:00
|
|
|
|
|
|
|
static ProcessList g_process_list;
|
2018-05-04 23:55:39 +00:00
|
|
|
static ProcessList g_dead_process_list;
|
2018-05-04 05:58:25 +00:00
|
|
|
|
2018-10-30 00:29:35 +00:00
|
|
|
static IEvent *g_process_launch_start_event = nullptr;
|
2019-06-17 22:27:29 +00:00
|
|
|
static HosSignal g_finish_launch_signal;
|
2018-05-04 05:58:25 +00:00
|
|
|
|
|
|
|
static HosMutex g_process_launch_mutex;
|
|
|
|
static Registration::ProcessLaunchState g_process_launch_state;
|
|
|
|
|
|
|
|
static std::atomic_bool g_debug_next_application(false);
|
|
|
|
static std::atomic<u64> g_debug_on_launch_tid(0);
|
|
|
|
|
2018-10-30 00:29:35 +00:00
|
|
|
static IEvent *g_process_event = nullptr;
|
|
|
|
static IEvent *g_debug_title_event = nullptr;
|
|
|
|
static IEvent *g_debug_application_event = nullptr;
|
2019-05-28 04:52:28 +00:00
|
|
|
static IEvent *g_boot_finished_event = nullptr;
|
2018-05-04 05:58:25 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
ProcessList &Registration::GetProcessList() {
|
|
|
|
return g_process_list;
|
2018-06-15 06:47:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
void Registration::InitializeSystemResources() {
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event = CreateWriteOnlySystemEvent();
|
|
|
|
g_debug_title_event = CreateWriteOnlySystemEvent();
|
|
|
|
g_debug_application_event = CreateWriteOnlySystemEvent();
|
2019-05-28 04:52:28 +00:00
|
|
|
g_boot_finished_event = CreateWriteOnlySystemEvent();
|
|
|
|
|
2018-10-30 00:29:35 +00:00
|
|
|
/* Auto-clear non-system event. */
|
2018-11-08 07:43:59 +00:00
|
|
|
g_process_launch_start_event = CreateHosEvent(&Registration::ProcessLaunchStartCallback);
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-07 09:50:33 +00:00
|
|
|
ResourceLimitUtils::InitializeLimits();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:29:35 +00:00
|
|
|
Result Registration::ProcessLaunchStartCallback(u64 timeout) {
|
|
|
|
g_process_launch_start_event->Clear();
|
2018-05-07 04:59:54 +00:00
|
|
|
Registration::HandleProcessLaunch();
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-05-07 04:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IWaitable *Registration::GetProcessLaunchStartEvent() {
|
|
|
|
return g_process_launch_start_event;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
Result Registration::LaunchProcess(u64 *out_pid, const TidSid tid_sid, const u64 launch_flags) {
|
2018-05-04 05:58:25 +00:00
|
|
|
LoaderProgramInfo program_info = {0};
|
|
|
|
Process new_process = {0};
|
2019-06-17 22:27:29 +00:00
|
|
|
new_process.tid_sid = tid_sid;
|
|
|
|
|
|
|
|
/* 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);
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Check that this is a real program. */
|
2019-06-17 22:27:29 +00:00
|
|
|
R_TRY(ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info));
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Get the resource limit handle, ensure that we can launch the program. */
|
2019-06-17 22:27:29 +00:00
|
|
|
if ((program_info.application_type & 3) == 1 && HasApplicationProcess()) {
|
|
|
|
return ResultPmApplicationRunning;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Try to register the title for launch in loader... */
|
2019-06-17 22:27:29 +00:00
|
|
|
R_TRY(ldrPmRegisterTitle(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &new_process.ldr_queue_index));
|
|
|
|
auto ldr_register_guard = SCOPE_GUARD {
|
|
|
|
ldrPmUnregisterTitle(new_process.ldr_queue_index);
|
|
|
|
};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Make sure the previous application is cleaned up. */
|
|
|
|
if ((program_info.application_type & 3) == 1) {
|
2018-05-07 09:50:33 +00:00
|
|
|
ResourceLimitUtils::EnsureApplicationResourcesAvailable();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Try to create the process... */
|
2019-06-17 22:27:29 +00:00
|
|
|
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));
|
|
|
|
auto proc_create_guard = SCOPE_GUARD {
|
|
|
|
svcCloseHandle(new_process.handle);
|
|
|
|
new_process.handle = 0;
|
|
|
|
};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Get the new process's id. */
|
2019-06-20 09:00:59 +00:00
|
|
|
R_ASSERT(svcGetProcessId(&new_process.pid, new_process.handle));
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Register with FS. */
|
2018-05-06 08:23:47 +00:00
|
|
|
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);
|
2019-06-17 22:27:29 +00:00
|
|
|
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));
|
|
|
|
auto fs_register_guard = SCOPE_GUARD {
|
|
|
|
fsprUnregisterProgram(new_process.pid);
|
|
|
|
};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-06 08:23:47 +00:00
|
|
|
/* Register with SM. */
|
|
|
|
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);
|
2019-06-17 22:27:29 +00:00
|
|
|
R_TRY(smManagerRegisterProcess(new_process.pid, acid_sac, program_info.acid_sac_size, aci0_sac, program_info.aci0_sac_size));
|
|
|
|
auto sm_register_guard = SCOPE_GUARD {
|
|
|
|
smManagerUnregisterProcess(new_process.pid);
|
|
|
|
};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Setup process flags. */
|
|
|
|
if (program_info.application_type & 1) {
|
2018-07-27 08:56:24 +00:00
|
|
|
new_process.flags |= PROCESSFLAGS_APPLICATION;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && LAUNCHFLAGS_NOTIYDEBUGSPECIAL(launch_flags) && (program_info.application_type & 4)) {
|
2018-07-27 08:56:24 +00:00
|
|
|
new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGSPECIAL;
|
2018-05-04 07:01:53 +00:00
|
|
|
}
|
|
|
|
if (LAUNCHFLAGS_NOTIFYWHENEXITED(launch_flags)) {
|
2018-07-27 08:56:24 +00:00
|
|
|
new_process.flags |= PROCESSFLAGS_NOTIFYWHENEXITED;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-10 10:25:07 +00:00
|
|
|
if (LAUNCHFLAGS_NOTIFYDEBUGEVENTS(launch_flags) && (GetRuntimeFirmwareVersion() < FirmwareVersion_200 || (program_info.application_type & 4))) {
|
2018-07-27 08:56:24 +00:00
|
|
|
new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGEVENTS;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Add process to the list. */
|
2018-06-15 06:47:07 +00:00
|
|
|
Registration::AddProcessToList(std::make_shared<Registration::Process>(new_process));
|
2019-06-17 22:27:29 +00:00
|
|
|
auto reg_list_guard = SCOPE_GUARD {
|
|
|
|
Registration::RemoveProcessFromList(new_process.pid);
|
|
|
|
};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Signal, if relevant. */
|
|
|
|
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
|
2018-10-30 00:29:35 +00:00
|
|
|
g_debug_title_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
g_debug_on_launch_tid = 0;
|
2018-07-27 08:56:24 +00:00
|
|
|
} else if ((new_process.flags & PROCESSFLAGS_APPLICATION) && g_debug_next_application.load()) {
|
2018-10-30 00:29:35 +00:00
|
|
|
g_debug_application_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
g_debug_next_application = false;
|
2019-06-17 22:27:29 +00:00
|
|
|
} 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));
|
|
|
|
SetProcessState(new_process.pid, ProcessState_Running);
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
/* Dismiss scope guards. */
|
|
|
|
reg_list_guard.Cancel();
|
|
|
|
sm_register_guard.Cancel();
|
|
|
|
fs_register_guard.Cancel();
|
|
|
|
proc_create_guard.Cancel();
|
|
|
|
ldr_register_guard.Cancel();
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
/* Set output pid. */
|
|
|
|
*out_pid = new_process.pid;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
return ResultSuccess;
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
void Registration::HandleProcessLaunch() {
|
|
|
|
/* Actually launch process. */
|
|
|
|
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();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
Result Registration::LaunchDebugProcess(u64 pid) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2018-05-07 10:45:44 +00:00
|
|
|
LoaderProgramInfo program_info = {0};
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
std::shared_ptr<Registration::Process> proc = GetProcess(pid);
|
2018-05-07 10:45:44 +00:00
|
|
|
if (proc == NULL) {
|
2019-03-28 22:16:36 +00:00
|
|
|
return ResultPmProcessNotFound;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-10-05 21:46:04 +00:00
|
|
|
if (proc->state >= ProcessState_Running) {
|
2019-03-28 22:16:36 +00:00
|
|
|
return ResultPmAlreadyStarted;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
/* Check that this is a real program. */
|
2019-06-17 22:27:29 +00:00
|
|
|
R_TRY(ldrPmGetProgramInfo(proc->tid_sid.title_id, proc->tid_sid.storage_id, &program_info));
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
/* Actually start the process. */
|
|
|
|
R_TRY(svcStartProcess(proc->handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size));
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-06-17 22:27:29 +00:00
|
|
|
proc->state = ProcessState_Running;
|
|
|
|
return ResultSuccess;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) {
|
|
|
|
/* Only allow one mutex to exist. */
|
2018-07-02 14:10:57 +00:00
|
|
|
std::scoped_lock lk{g_process_launch_mutex};
|
2018-05-04 05:58:25 +00:00
|
|
|
g_process_launch_state.tid_sid.title_id = title_id;
|
|
|
|
g_process_launch_state.tid_sid.storage_id = storage_id;
|
|
|
|
g_process_launch_state.launch_flags = launch_flags;
|
|
|
|
g_process_launch_state.out_pid = out_pid;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Start a launch, and wait for it to exit. */
|
2019-06-17 22:27:29 +00:00
|
|
|
g_finish_launch_signal.Reset();
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_launch_start_event->Signal();
|
2019-06-17 22:27:29 +00:00
|
|
|
g_finish_launch_signal.Wait();
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-07-02 14:10:57 +00:00
|
|
|
return g_process_launch_state.result;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid) {
|
|
|
|
return LaunchProcess(tid_sid.title_id, tid_sid.storage_id, launch_flags, out_pid);
|
|
|
|
};
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process> process) {
|
2018-05-04 05:58:25 +00:00
|
|
|
u64 tmp;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Reset the signal. */
|
|
|
|
svcResetSignal(process->handle);
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
ProcessState old_state;
|
|
|
|
old_state = process->state;
|
|
|
|
svcGetProcessInfo(&tmp, process->handle, ProcessInfoType_ProcessState);
|
|
|
|
process->state = (ProcessState)tmp;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) {
|
2018-07-27 08:56:24 +00:00
|
|
|
process->flags &= ~PROCESSFLAGS_CRASH_DEBUG;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
switch (process->state) {
|
|
|
|
case ProcessState_Created:
|
2018-10-05 21:46:04 +00:00
|
|
|
case ProcessState_CreatedAttached:
|
2018-05-04 05:58:25 +00:00
|
|
|
case ProcessState_Exiting:
|
|
|
|
break;
|
2018-10-05 21:46:04 +00:00
|
|
|
case ProcessState_Running:
|
2018-07-27 08:56:24 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
|
|
|
|
process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
|
|
|
|
process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING;
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && process->flags & PROCESSFLAGS_NOTIFYDEBUGSPECIAL) {
|
2018-07-27 08:56:24 +00:00
|
|
|
process->flags &= ~(PROCESSFLAGS_NOTIFYDEBUGSPECIAL | PROCESSFLAGS_DEBUGDETACHED);
|
|
|
|
process->flags |= PROCESSFLAGS_DEBUGDETACHED;
|
2018-05-04 23:55:39 +00:00
|
|
|
}
|
2018-05-04 05:58:25 +00:00
|
|
|
break;
|
|
|
|
case ProcessState_Crashed:
|
2018-07-27 08:56:24 +00:00
|
|
|
process->flags |= (PROCESSFLAGS_CRASHED | PROCESSFLAGS_CRASH_DEBUG);
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
break;
|
2018-10-05 21:46:04 +00:00
|
|
|
case ProcessState_RunningAttached:
|
2018-07-27 08:56:24 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
|
|
|
|
process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
|
|
|
|
process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING;
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 23:55:39 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-05-04 05:58:25 +00:00
|
|
|
case ProcessState_Exited:
|
2019-05-10 10:25:07 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_NOTIFYWHENEXITED && GetRuntimeFirmwareVersion() < FirmwareVersion_500) {
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
} else {
|
|
|
|
FinalizeExitedProcess(process);
|
|
|
|
}
|
2019-03-28 23:57:18 +00:00
|
|
|
return ResultKernelConnectionClosed;
|
2018-05-04 05:58:25 +00:00
|
|
|
case ProcessState_DebugSuspended:
|
2018-07-27 08:56:24 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
|
|
|
|
process->flags |= (PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) {
|
2019-03-26 00:12:19 +00:00
|
|
|
bool signal_debug_process_5x;
|
|
|
|
|
|
|
|
/* Scope to manage process list lock. */
|
|
|
|
{
|
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
|
|
|
|
2019-05-10 10:25:07 +00:00
|
|
|
signal_debug_process_5x = (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED;
|
2019-03-26 00:12:19 +00:00
|
|
|
|
2019-06-20 09:00:59 +00:00
|
|
|
/* Unregister with FS, SM, and LDR. */
|
|
|
|
R_ASSERT(fsprUnregisterProgram(process->pid));
|
|
|
|
R_ASSERT(smManagerUnregisterProcess(process->pid));
|
|
|
|
R_ASSERT(ldrPmUnregisterTitle(process->ldr_queue_index));
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
/* Close the process's handle. */
|
|
|
|
svcCloseHandle(process->handle);
|
|
|
|
process->handle = 0;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
/* Insert into dead process list, if relevant. */
|
|
|
|
if (signal_debug_process_5x) {
|
|
|
|
std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
|
|
|
|
|
|
|
|
g_dead_process_list.processes.push_back(process);
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
/* Remove NOTE: This probably frees process. */
|
|
|
|
RemoveProcessFromList(process->pid);
|
2018-05-04 23:55:39 +00:00
|
|
|
}
|
2018-05-04 05:58:25 +00:00
|
|
|
|
2018-05-04 23:55:39 +00:00
|
|
|
if (signal_debug_process_5x) {
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_event->Signal();
|
2018-05-04 23:55:39 +00:00
|
|
|
}
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
void Registration::AddProcessToList(std::shared_ptr<Registration::Process> process) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
g_process_list.processes.push_back(process);
|
2018-10-30 00:29:35 +00:00
|
|
|
g_process_launch_start_event->GetManager()->AddWaitable(new ProcessWaiter(process));
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::RemoveProcessFromList(u64 pid) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Remove process from list. */
|
2018-06-15 06:47:07 +00:00
|
|
|
for (unsigned int i = 0; i < g_process_list.processes.size(); i++) {
|
|
|
|
std::shared_ptr<Registration::Process> process = g_process_list.processes[i];
|
|
|
|
if (process->pid == pid) {
|
|
|
|
g_process_list.processes.erase(g_process_list.processes.begin() + i);
|
2018-05-04 05:58:25 +00:00
|
|
|
svcCloseHandle(process->handle);
|
2018-06-15 06:47:07 +00:00
|
|
|
process->handle = 0;
|
2018-05-04 05:58:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::SetProcessState(u64 pid, ProcessState new_state) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Set process state. */
|
2018-06-15 06:47:07 +00:00
|
|
|
for (auto &process : g_process_list.processes) {
|
2019-05-28 04:52:28 +00:00
|
|
|
if (process->pid == pid) {
|
2018-05-04 05:58:25 +00:00
|
|
|
process->state = new_state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process> *out) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
for (auto &process : g_process_list.processes) {
|
2018-07-27 08:56:24 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_APPLICATION) {
|
2018-06-15 06:47:07 +00:00
|
|
|
if (out != nullptr) {
|
2018-05-05 01:56:59 +00:00
|
|
|
*out = process;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
std::shared_ptr<Registration::Process> Registration::GetProcess(u64 pid) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
for (auto &process : g_process_list.processes) {
|
|
|
|
if (process->pid == pid) {
|
|
|
|
return process;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
return nullptr;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
for (auto &process : g_process_list.processes) {
|
|
|
|
if (process->tid_sid.title_id == tid) {
|
|
|
|
return process;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
return nullptr;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
|
|
|
|
Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2018-05-07 10:45:44 +00:00
|
|
|
u32 num = 0;
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
for (auto &process : g_process_list.processes) {
|
2018-07-27 08:56:24 +00:00
|
|
|
if (process->flags & PROCESSFLAGS_CRASH_DEBUG && num < max_out) {
|
2018-06-15 06:47:07 +00:00
|
|
|
out_pids[num++] = process->pid;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
*num_out = num;
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
Handle Registration::GetProcessEventHandle() {
|
2018-10-30 00:29:35 +00:00
|
|
|
return g_process_event->GetHandle();
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
|
2019-03-26 00:12:19 +00:00
|
|
|
/* Scope to manage process list lock. */
|
|
|
|
{
|
|
|
|
std::scoped_lock<ProcessList &> lk(GetProcessList());
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-03-26 00:12:19 +00:00
|
|
|
for (auto &p : g_process_list.processes) {
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
|
2019-03-26 00:12:19 +00:00
|
|
|
p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
|
|
|
|
*out_pid = p->pid;
|
2019-05-10 10:25:07 +00:00
|
|
|
*out_type = (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED;
|
2019-03-26 00:12:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) {
|
|
|
|
u64 old_flags = p->flags;
|
|
|
|
p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING;
|
|
|
|
*out_pid = p->pid;
|
2019-05-10 10:25:07 +00:00
|
|
|
*out_type = (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) ?
|
2019-03-26 00:12:19 +00:00
|
|
|
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
|
|
|
|
PROCESSEVENTTYPE_500_SUSPENDED :
|
|
|
|
PROCESSEVENTTYPE_500_RUNNING) :
|
|
|
|
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
|
|
|
|
PROCESSEVENTTYPE_SUSPENDED :
|
|
|
|
PROCESSEVENTTYPE_RUNNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->flags & PROCESSFLAGS_CRASHED) {
|
|
|
|
*out_pid = p->pid;
|
2019-05-10 10:25:07 +00:00
|
|
|
*out_type = (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH;
|
2019-03-26 00:12:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-10 10:25:07 +00:00
|
|
|
if (GetRuntimeFirmwareVersion() < FirmwareVersion_500 && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) {
|
2019-03-26 00:12:19 +00:00
|
|
|
*out_pid = p->pid;
|
|
|
|
*out_type = PROCESSEVENTTYPE_EXIT;
|
|
|
|
return;
|
|
|
|
}
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
2019-03-26 00:12:19 +00:00
|
|
|
|
|
|
|
*out_pid = 0;
|
|
|
|
*out_type = 0;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_500)) {
|
2019-03-26 00:12:19 +00:00
|
|
|
std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
|
|
|
|
|
2018-06-15 06:47:07 +00:00
|
|
|
if (g_dead_process_list.processes.size()) {
|
|
|
|
std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0];
|
|
|
|
g_dead_process_list.processes.erase(g_dead_process_list.processes.begin());
|
2018-05-05 01:56:59 +00:00
|
|
|
*out_pid = process->pid;
|
2018-07-27 08:56:24 +00:00
|
|
|
*out_type = PROCESSEVENTTYPE_500_EXIT;
|
2018-05-05 01:56:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
|
|
|
|
Result Registration::EnableDebugForTitleId(u64 tid, Handle *out) {
|
|
|
|
u64 old = g_debug_on_launch_tid.exchange(tid);
|
|
|
|
if (old) {
|
|
|
|
g_debug_on_launch_tid = old;
|
2019-03-28 22:16:36 +00:00
|
|
|
return ResultPmDebugHookInUse;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
2018-10-30 00:29:35 +00:00
|
|
|
*out = g_debug_title_event->GetHandle();
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
Result Registration::EnableDebugForApplication(Handle *out) {
|
|
|
|
g_debug_next_application = true;
|
2018-10-30 00:29:35 +00:00
|
|
|
*out = g_debug_application_event->GetHandle();
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-05-07 10:45:44 +00:00
|
|
|
}
|
2018-09-09 06:47:15 +00:00
|
|
|
|
|
|
|
Result Registration::DisableDebug(u32 which) {
|
|
|
|
if (which & 1) {
|
|
|
|
g_debug_on_launch_tid = 0;
|
|
|
|
}
|
|
|
|
if (which & 2) {
|
|
|
|
g_debug_next_application = false;
|
|
|
|
}
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-09-09 06:47:15 +00:00
|
|
|
}
|
2019-05-28 04:52:28 +00:00
|
|
|
|
|
|
|
Handle Registration::GetDebugTitleEventHandle() {
|
|
|
|
return g_debug_title_event->GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle Registration::GetDebugApplicationEventHandle() {
|
|
|
|
return g_debug_application_event->GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle Registration::GetBootFinishedEventHandle() {
|
|
|
|
return g_boot_finished_event->GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::SignalBootFinished() {
|
|
|
|
g_boot_finished_event->Signal();
|
|
|
|
}
|