1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-30 07:42:13 +00:00

pm: actually implement GetBootFinishedEvent

This commit is contained in:
Michael Scire 2019-05-27 21:52:28 -07:00
parent f38965d0bd
commit d6502c174a
5 changed files with 108 additions and 84 deletions

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include "pm_process_track.hpp"
@ -23,9 +23,9 @@ void ProcessTracking::MainLoop(void *arg) {
/* Make a new waitable manager. */
auto process_waiter = new WaitableManager(1);
process_waiter->AddWaitable(Registration::GetProcessLaunchStartEvent());
/* Service processes. */
process_waiter->Process();
delete process_waiter;
}

View file

@ -35,6 +35,7 @@ static std::atomic<u64> g_debug_on_launch_tid(0);
static IEvent *g_process_event = nullptr;
static IEvent *g_debug_title_event = nullptr;
static IEvent *g_debug_application_event = nullptr;
static IEvent *g_boot_finished_event = nullptr;
static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
@ -46,10 +47,11 @@ void Registration::InitializeSystemResources() {
g_process_event = CreateWriteOnlySystemEvent();
g_debug_title_event = CreateWriteOnlySystemEvent();
g_debug_application_event = CreateWriteOnlySystemEvent();
g_boot_finished_event = CreateWriteOnlySystemEvent();
/* Auto-clear non-system event. */
g_process_launch_start_event = CreateHosEvent(&Registration::ProcessLaunchStartCallback);
ResourceLimitUtils::InitializeLimits();
}
@ -72,55 +74,55 @@ void Registration::HandleProcessLaunch() {
new_process.tid_sid = g_process_launch_state.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);
/* 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)))) {
goto HANDLE_PROCESS_LAUNCH_END;
}
/* Get the resource limit handle, ensure that we can launch the program. */
if ((program_info.application_type & 3) == 1 && HasApplicationProcess(NULL)) {
rc = ResultPmApplicationRunning;
goto HANDLE_PROCESS_LAUNCH_END;
}
/* 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)))) {
goto HANDLE_PROCESS_LAUNCH_END;
}
/* Make sure the previous application is cleaned up. */
if ((program_info.application_type & 3) == 1) {
ResourceLimitUtils::EnsureApplicationResourcesAvailable();
}
/* 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)))) {
goto PROCESS_CREATION_FAILED;
}
/* Get the new process's id. */
svcGetProcessId(&new_process.pid, new_process.handle);
/* 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(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)))) {
goto FS_REGISTRATION_FAILED;
}
/* 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);
if (R_FAILED((rc = smManagerRegisterProcess(new_process.pid, acid_sac, program_info.acid_sac_size, aci0_sac, program_info.aci0_sac_size)))) {
goto SM_REGISTRATION_FAILED;
}
/* Setup process flags. */
if (program_info.application_type & 1) {
new_process.flags |= PROCESSFLAGS_APPLICATION;
}
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && LAUNCHFLAGS_NOTIYDEBUGSPECIAL(launch_flags) && (program_info.application_type & 4)) {
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && LAUNCHFLAGS_NOTIYDEBUGSPECIAL(launch_flags) && (program_info.application_type & 4)) {
new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGSPECIAL;
}
if (LAUNCHFLAGS_NOTIFYWHENEXITED(launch_flags)) {
@ -129,10 +131,10 @@ void Registration::HandleProcessLaunch() {
if (LAUNCHFLAGS_NOTIFYDEBUGEVENTS(launch_flags) && (GetRuntimeFirmwareVersion() < FirmwareVersion_200 || (program_info.application_type & 4))) {
new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGEVENTS;
}
/* Add process to the list. */
Registration::AddProcessToList(std::make_shared<Registration::Process>(new_process));
/* Signal, if relevant. */
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
g_debug_title_event->Signal();
@ -146,39 +148,39 @@ void Registration::HandleProcessLaunch() {
rc = ResultSuccess;
} 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)) {
Registration::RemoveProcessFromList(new_process.pid);
smManagerUnregisterProcess(new_process.pid);
}
SM_REGISTRATION_FAILED:
if (R_FAILED(rc)) {
fsprUnregisterProgram(new_process.pid);
}
FS_REGISTRATION_FAILED:
if (R_FAILED(rc)) {
svcCloseHandle(new_process.handle);
new_process.handle = 0;
}
PROCESS_CREATION_FAILED:
if (R_FAILED(rc)) {
ldrPmUnregisterTitle(new_process.ldr_queue_index);
}
HANDLE_PROCESS_LAUNCH_END:
g_process_launch_state.result = rc;
if (R_SUCCEEDED(rc)) {
*out_pid = new_process.pid;
}
g_sema_finish_launch.Signal();
}
@ -187,25 +189,25 @@ Result Registration::LaunchDebugProcess(u64 pid) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
LoaderProgramInfo program_info = {0};
Result rc;
std::shared_ptr<Registration::Process> proc = GetProcess(pid);
if (proc == NULL) {
return ResultPmProcessNotFound;
}
if (proc->state >= ProcessState_Running) {
return ResultPmAlreadyStarted;
}
/* Check that this is a real program. */
if (R_FAILED((rc = 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)))) {
proc->state = ProcessState_Running;
}
return rc;
}
@ -216,11 +218,11 @@ Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 lau
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;
/* Start a launch, and wait for it to exit. */
g_process_launch_start_event->Signal();
g_sema_finish_launch.Wait();
return g_process_launch_state.result;
}
@ -230,15 +232,15 @@ Result Registration::LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64
Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process> process) {
u64 tmp;
/* Reset the signal. */
svcResetSignal(process->handle);
ProcessState old_state;
old_state = process->state;
svcGetProcessInfo(&tmp, process->handle, ProcessInfoType_ProcessState);
process->state = (ProcessState)tmp;
if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) {
process->flags &= ~PROCESSFLAGS_CRASH_DEBUG;
}
@ -307,18 +309,18 @@ void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process>
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
std::abort();
}
/* Close the process's handle. */
svcCloseHandle(process->handle);
process->handle = 0;
/* 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);
}
/* Remove NOTE: This probably frees process. */
RemoveProcessFromList(process->pid);
}
@ -337,7 +339,7 @@ void Registration::AddProcessToList(std::shared_ptr<Registration::Process> proce
void Registration::RemoveProcessFromList(u64 pid) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Remove process from list. */
for (unsigned int i = 0; i < g_process_list.processes.size(); i++) {
std::shared_ptr<Registration::Process> process = g_process_list.processes[i];
@ -352,10 +354,10 @@ void Registration::RemoveProcessFromList(u64 pid) {
void Registration::SetProcessState(u64 pid, ProcessState new_state) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Set process state. */
for (auto &process : g_process_list.processes) {
if (process->pid == pid) {
if (process->pid == pid) {
process->state = new_state;
break;
}
@ -364,7 +366,7 @@ void Registration::SetProcessState(u64 pid, ProcessState new_state) {
bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process> *out) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
if (process->flags & PROCESSFLAGS_APPLICATION) {
if (out != nullptr) {
@ -373,31 +375,31 @@ bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process>
return true;
}
}
return false;
}
std::shared_ptr<Registration::Process> Registration::GetProcess(u64 pid) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
if (process->pid == pid) {
return process;
}
}
return nullptr;
}
std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
if (process->tid_sid.title_id == tid) {
return process;
}
}
return nullptr;
}
@ -405,14 +407,14 @@ std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid
Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out) {
std::scoped_lock<ProcessList &> lk(GetProcessList());
u32 num = 0;
for (auto &process : g_process_list.processes) {
if (process->flags & PROCESSFLAGS_CRASH_DEBUG && num < max_out) {
out_pids[num++] = process->pid;
}
}
*num_out = num;
return ResultSuccess;
}
@ -425,7 +427,7 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
/* Scope to manage process list lock. */
{
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &p : g_process_list.processes) {
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200) && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
@ -461,7 +463,7 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
*out_pid = 0;
*out_type = 0;
}
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_500)) {
std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
@ -501,3 +503,19 @@ Result Registration::DisableDebug(u32 which) {
}
return ResultSuccess;
}
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();
}

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
@ -98,7 +98,7 @@ enum {
- and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set
*/
PROCESSFLAGS_DEBUGSUSPENDED = 0x020,
/*
set in HandleProcessLaunch when
- program_info.application_type & 1
@ -148,7 +148,7 @@ enum {
PROCESSEVENTTYPE_500_RUNNING = 4, // debug detached or running
PROCESSEVENTTYPE_500_SUSPENDED = 5, // debug suspended
};
class Registration {
public:
struct TidSid {
@ -163,7 +163,7 @@ class Registration {
ProcessState state;
u32 flags;
};
struct ProcessLaunchState {
TidSid tid_sid;
u64 launch_flags;
@ -175,14 +175,14 @@ class Registration {
static IWaitable *GetProcessLaunchStartEvent();
static ProcessList &GetProcessList();
static Result ProcessLaunchStartCallback(u64 timeout);
static Result HandleSignaledProcess(std::shared_ptr<Process> process);
static void FinalizeExitedProcess(std::shared_ptr<Process> process);
static void AddProcessToList(std::shared_ptr<Process> process);
static void RemoveProcessFromList(u64 pid);
static void SetProcessState(u64 pid, ProcessState new_state);
static std::shared_ptr<Registration::Process> GetProcess(u64 pid);
static std::shared_ptr<Registration::Process> GetProcessByTitleId(u64 tid);
static Result GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out);
@ -193,13 +193,16 @@ class Registration {
static Result DisableDebug(u32 which);
static Handle GetDebugTitleEventHandle();
static Handle GetDebugApplicationEventHandle();
static Handle GetBootFinishedEventHandle();
static void HandleProcessLaunch();
static Result LaunchDebugProcess(u64 pid);
static void SignalFinishLaunchProcess();
static Result LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid);
static Result LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid);
static void SignalBootFinished();
static bool HasApplicationProcess(std::shared_ptr<Process> *out);
};

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include "pm_registration.hpp"
@ -29,7 +29,7 @@ Result ShellService::LaunchProcess(Out<u64> pid, Registration::TidSid tid_sid, u
Result ShellService::TerminateProcessId(u64 pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc != nullptr) {
return svcTerminateProcess(proc->handle);
@ -40,7 +40,7 @@ Result ShellService::TerminateProcessId(u64 pid) {
Result ShellService::TerminateTitleId(u64 tid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcessByTitleId(tid);
if (proc != NULL) {
return svcTerminateProcess(proc->handle);
@ -59,7 +59,7 @@ void ShellService::GetProcessEventType(Out<u64> type, Out<u64> pid) {
Result ShellService::FinalizeExitedProcess(u64 pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc == NULL) {
return ResultPmProcessNotFound;
@ -73,7 +73,7 @@ Result ShellService::FinalizeExitedProcess(u64 pid) {
Result ShellService::ClearProcessNotificationFlag(u64 pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc != NULL) {
proc->flags &= ~PROCESSFLAGS_CRASHED;
@ -86,13 +86,14 @@ Result ShellService::ClearProcessNotificationFlag(u64 pid) {
void ShellService::NotifyBootFinished() {
if (!g_has_boot_finished) {
g_has_boot_finished = true;
Registration::SignalBootFinished();
EmbeddedBoot2::Main();
}
}
Result ShellService::GetApplicationProcessId(Out<u64> pid) {
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> app_proc;
if (Registration::HasApplicationProcess(&app_proc)) {
pid.SetValue(app_proc->pid);
@ -113,13 +114,15 @@ Result ShellService::BoostSystemThreadsResourceLimit() {
}
Result ShellService::GetUnimplementedEventHandle(Out<CopiedHandle> event) {
/* In 8.0.0, Nintendo added this command which should return an event handle. */
/* In addition, they also added code to create a new event in the global PM constructor. */
/* However, nothing signals this event, and this command currently does std::abort();. */
/* We will oblige. */
std::abort();
/* TODO: Return an event handle, once N makes this command a real thing in the future. */
/* TODO: return ResultSuccess; */
void ShellService::GetBootFinishedEvent(Out<CopiedHandle> event) {
/* In 8.0.0, Nintendo added this command, which signals that the boot sysmodule has finished. */
/* Nintendo only signals it in safe mode FIRM, and this function aborts on normal FIRM. */
/* We will signal it always, but only allow this function to succeed on safe mode. */
{
u64 is_recovery_boot = 0;
if (R_FAILED(SmcGetConfig(SplConfigItem_IsRecoveryBoot, &is_recovery_boot)) || !is_recovery_boot) {
std::abort();
}
}
event.SetValue(Registration::GetBootFinishedEventHandle());
}

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <switch.h>
#include <stratosphere.hpp>
@ -42,12 +42,12 @@ enum ShellCmd_5X {
Shell_Cmd_5X_NotifyBootFinished = 5,
Shell_Cmd_5X_GetApplicationProcessId = 6,
Shell_Cmd_5X_BoostSystemMemoryResourceLimit = 7,
Shell_Cmd_BoostSystemThreadsResourceLimit = 8,
Shell_Cmd_GetUnimplementedEventHandle = 9 /* TODO: Rename when Nintendo implements this. */
Shell_Cmd_GetBootFinishedEvent = 9,
};
class ShellService final : public IServiceObject {
class ShellService final : public IServiceObject {
private:
/* Actual commands. */
Result LaunchProcess(Out<u64> pid, Registration::TidSid tid_sid, u32 launch_flags);
@ -61,7 +61,7 @@ class ShellService final : public IServiceObject {
Result GetApplicationProcessId(Out<u64> pid);
Result BoostSystemMemoryResourceLimit(u64 sysmem_size);
Result BoostSystemThreadsResourceLimit();
Result GetUnimplementedEventHandle(Out<CopiedHandle> event);
void GetBootFinishedEvent(Out<CopiedHandle> event);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
/* 1.0.0-4.0.0 */
@ -74,10 +74,10 @@ class ShellService final : public IServiceObject {
MakeServiceCommandMeta<Shell_Cmd_ClearProcessNotificationFlag, &ShellService::ClearProcessNotificationFlag, FirmwareVersion_Min, FirmwareVersion_400>(),
MakeServiceCommandMeta<Shell_Cmd_NotifyBootFinished, &ShellService::NotifyBootFinished, FirmwareVersion_Min, FirmwareVersion_400>(),
MakeServiceCommandMeta<Shell_Cmd_GetApplicationProcessId, &ShellService::GetApplicationProcessId, FirmwareVersion_Min, FirmwareVersion_400>(),
/* 4.0.0-4.0.0 */
MakeServiceCommandMeta<Shell_Cmd_BoostSystemMemoryResourceLimit, &ShellService::BoostSystemMemoryResourceLimit, FirmwareVersion_400, FirmwareVersion_400>(),
/* 5.0.0-* */
MakeServiceCommandMeta<Shell_Cmd_5X_LaunchProcess, &ShellService::LaunchProcess, FirmwareVersion_500>(),
MakeServiceCommandMeta<Shell_Cmd_5X_TerminateProcessId, &ShellService::TerminateProcessId, FirmwareVersion_500>(),
@ -87,11 +87,11 @@ class ShellService final : public IServiceObject {
MakeServiceCommandMeta<Shell_Cmd_5X_NotifyBootFinished, &ShellService::NotifyBootFinished, FirmwareVersion_500>(),
MakeServiceCommandMeta<Shell_Cmd_5X_GetApplicationProcessId, &ShellService::GetApplicationProcessId, FirmwareVersion_500>(),
MakeServiceCommandMeta<Shell_Cmd_5X_BoostSystemMemoryResourceLimit, &ShellService::BoostSystemMemoryResourceLimit, FirmwareVersion_500>(),
/* 7.0.0-* */
MakeServiceCommandMeta<Shell_Cmd_BoostSystemThreadsResourceLimit, &ShellService::BoostSystemThreadsResourceLimit, FirmwareVersion_700>(),
/* 8.0.0-* */
MakeServiceCommandMeta<Shell_Cmd_GetUnimplementedEventHandle, &ShellService::GetUnimplementedEventHandle, FirmwareVersion_800>(),
MakeServiceCommandMeta<Shell_Cmd_GetBootFinishedEvent, &ShellService::GetBootFinishedEvent, FirmwareVersion_800>(),
};
};