Atmosphere/stratosphere/pm/source/impl/pm_process_manager.cpp

731 lines
29 KiB
C++
Raw Normal View History

2019-06-29 09:20:36 +00:00
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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/>.
*/
#include "pm_process_manager.hpp"
#include "pm_resource_manager.hpp"
#include "pm_process_info.hpp"
namespace ams::pm::impl {
2019-06-29 09:20:36 +00:00
namespace {
/* Types. */
enum HookType {
HookType_TitleId = (1 << 0),
HookType_Application = (1 << 1),
};
struct LaunchProcessArgs {
2019-10-15 05:49:06 +00:00
os::ProcessId *out_process_id;
2019-06-29 09:20:36 +00:00
ncm::TitleLocation location;
u32 flags;
};
enum LaunchFlags {
LaunchFlags_None = 0,
LaunchFlags_SignalOnExit = (1 << 0),
LaunchFlags_SignalOnStart = (1 << 1),
LaunchFlags_SignalOnException = (1 << 2),
LaunchFlags_SignalOnDebugEvent = (1 << 3),
LaunchFlags_StartSuspended = (1 << 4),
LaunchFlags_DisableAslr = (1 << 5),
};
enum LaunchFlagsDeprecated {
LaunchFlagsDeprecated_None = 0,
LaunchFlagsDeprecated_SignalOnExit = (1 << 0),
LaunchFlagsDeprecated_StartSuspended = (1 << 1),
LaunchFlagsDeprecated_SignalOnException = (1 << 2),
LaunchFlagsDeprecated_DisableAslr = (1 << 3),
LaunchFlagsDeprecated_SignalOnDebugEvent = (1 << 4),
LaunchFlagsDeprecated_SignalOnStart = (1 << 5),
};
2019-10-15 05:49:06 +00:00
#define GET_FLAG_MASK(flag) (hos_version >= hos::Version_500 ? static_cast<u32>(LaunchFlags_##flag) : static_cast<u32>(LaunchFlagsDeprecated_##flag))
2019-06-29 09:20:36 +00:00
inline bool ShouldSignalOnExit(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
2019-06-29 09:20:36 +00:00
return launch_flags & GET_FLAG_MASK(SignalOnExit);
}
inline bool ShouldSignalOnStart(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
if (hos_version < hos::Version_200) {
2019-06-29 09:20:36 +00:00
return false;
}
return launch_flags & GET_FLAG_MASK(SignalOnStart);
}
inline bool ShouldSignalOnException(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
2019-06-29 09:20:36 +00:00
return launch_flags & GET_FLAG_MASK(SignalOnException);
}
inline bool ShouldSignalOnDebugEvent(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
2019-06-29 09:20:36 +00:00
return launch_flags & GET_FLAG_MASK(SignalOnDebugEvent);
}
inline bool ShouldStartSuspended(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
2019-06-29 09:20:36 +00:00
return launch_flags & GET_FLAG_MASK(StartSuspended);
}
inline bool ShouldDisableAslr(u32 launch_flags) {
2019-10-15 05:49:06 +00:00
const auto hos_version = hos::GetVersion();
2019-06-29 09:20:36 +00:00
return launch_flags & GET_FLAG_MASK(DisableAslr);
}
#undef GET_FLAG_MASK
enum class ProcessEvent {
None = 0,
Exited = 1,
Started = 2,
Exception = 3,
DebugRunning = 4,
DebugSuspended = 5,
};
enum class ProcessEventDeprecated {
None = 0,
Exception = 1,
Exited = 2,
DebugRunning = 3,
DebugSuspended = 4,
Started = 5,
};
inline u32 GetProcessEventValue(ProcessEvent event) {
2019-10-15 05:49:06 +00:00
if (hos::GetVersion() >= hos::Version_500) {
2019-06-29 09:20:36 +00:00
return static_cast<u32>(event);
}
switch (event) {
case ProcessEvent::None:
return static_cast<u32>(ProcessEventDeprecated::None);
case ProcessEvent::Exited:
return static_cast<u32>(ProcessEventDeprecated::Exited);
case ProcessEvent::Started:
return static_cast<u32>(ProcessEventDeprecated::Started);
case ProcessEvent::Exception:
return static_cast<u32>(ProcessEventDeprecated::Exception);
case ProcessEvent::DebugRunning:
return static_cast<u32>(ProcessEventDeprecated::DebugRunning);
case ProcessEvent::DebugSuspended:
return static_cast<u32>(ProcessEventDeprecated::DebugSuspended);
AMS_UNREACHABLE_DEFAULT_CASE();
2019-06-29 09:20:36 +00:00
}
}
template<size_t MaxProcessInfos>
class ProcessInfoAllocator {
NON_COPYABLE(ProcessInfoAllocator);
NON_MOVEABLE(ProcessInfoAllocator);
static_assert(MaxProcessInfos >= 0x40, "MaxProcessInfos is too small.");
private:
TYPED_STORAGE(ProcessInfo) process_info_storages[MaxProcessInfos];
bool process_info_allocated[MaxProcessInfos];
os::Mutex lock;
private:
constexpr inline size_t GetProcessInfoIndex(ProcessInfo *process_info) const {
return process_info - GetPointer(this->process_info_storages[0]);
}
public:
constexpr ProcessInfoAllocator() {
std::memset(this->process_info_storages, 0, sizeof(this->process_info_storages));
std::memset(this->process_info_allocated, 0, sizeof(this->process_info_allocated));
}
void *AllocateProcessInfoStorage() {
std::scoped_lock lk(this->lock);
for (size_t i = 0; i < MaxProcessInfos; i++) {
if (!this->process_info_allocated[i]) {
this->process_info_allocated[i] = true;
2019-10-18 17:37:03 +00:00
std::memset(&this->process_info_storages[i], 0, sizeof(this->process_info_storages[i]));
return GetPointer(this->process_info_storages[i]);
}
}
return nullptr;
}
void FreeProcessInfo(ProcessInfo *process_info) {
std::scoped_lock lk(this->lock);
const size_t index = this->GetProcessInfoIndex(process_info);
AMS_ASSERT(index < MaxProcessInfos);
AMS_ASSERT(this->process_info_allocated[index]);
process_info->~ProcessInfo();
this->process_info_allocated[index] = false;
}
};
2019-06-29 09:20:36 +00:00
/* Process Tracking globals. */
void ProcessTrackingMain(void *arg);
constexpr size_t ProcessTrackThreadStackSize = 0x4000;
constexpr int ProcessTrackThreadPriority = 0x15;
os::StaticThread<ProcessTrackThreadStackSize> g_process_track_thread(&ProcessTrackingMain, nullptr, ProcessTrackThreadPriority);
2019-06-29 09:20:36 +00:00
/* Process lists. */
ProcessList g_process_list;
ProcessList g_dead_process_list;
/* Process Info Allocation. */
/* Note: The kernel slabheap is size 0x50 -- we allow slightly larger to account for the dead process list. */
constexpr size_t MaxProcessCount = 0x60;
ProcessInfoAllocator<MaxProcessCount> g_process_info_allocator;
2019-06-29 09:20:36 +00:00
/* Global events. */
os::SystemEvent g_process_event;
os::SystemEvent g_hook_to_create_process_event;
os::SystemEvent g_hook_to_create_application_process_event;
os::SystemEvent g_boot_finished_event;
2019-06-29 09:20:36 +00:00
/* Process Launch synchronization globals. */
os::Event g_process_launch_start_event;
2019-09-24 10:15:36 +00:00
os::Event g_process_launch_finish_event;
Result g_process_launch_result = ResultSuccess();
2019-06-29 09:20:36 +00:00
LaunchProcessArgs g_process_launch_args = {};
/* Hook globals. */
std::atomic<ncm::TitleId> g_title_id_hook;
std::atomic<bool> g_application_hook;
/* Forward declarations. */
Result LaunchProcess(os::WaitableManager &waitable_manager, const LaunchProcessArgs &args);
void OnProcessSignaled(ProcessListAccessor &list, ProcessInfo *process_info);
2019-06-29 09:20:36 +00:00
/* Helpers. */
void ProcessTrackingMain(void *arg) {
/* This is the main loop of the process tracking thread. */
/* Setup waitable manager. */
os::WaitableManager process_waitable_manager;
os::WaitableHolder start_event_holder(&g_process_launch_start_event);
process_waitable_manager.LinkWaitableHolder(&start_event_holder);
while (true) {
auto signaled_holder = process_waitable_manager.WaitAny();
if (signaled_holder == &start_event_holder) {
/* Launch start event signaled. */
/* TryWait will clear signaled, preventing duplicate notifications. */
if (g_process_launch_start_event.TryWait()) {
g_process_launch_result = LaunchProcess(process_waitable_manager, g_process_launch_args);
g_process_launch_finish_event.Signal();
}
} else {
/* Some process was signaled. */
ProcessListAccessor list(g_process_list);
OnProcessSignaled(list, reinterpret_cast<ProcessInfo *>(signaled_holder->GetUserData()));
}
}
2019-06-29 09:20:36 +00:00
}
inline u32 GetLoaderCreateProcessFlags(u32 launch_flags) {
u32 ldr_flags = 0;
2019-10-15 05:49:06 +00:00
if (ShouldSignalOnException(launch_flags) || (hos::GetVersion() >= hos::Version_200 && !ShouldStartSuspended(launch_flags))) {
2019-06-29 09:20:36 +00:00
ldr_flags |= ldr::CreateProcessFlag_EnableDebug;
}
if (ShouldDisableAslr(launch_flags)) {
ldr_flags |= ldr::CreateProcessFlag_DisableAslr;
}
return ldr_flags;
}
2019-07-03 05:21:47 +00:00
bool HasApplicationProcess() {
ProcessListAccessor list(g_process_list);
for (auto &process : *list) {
if (process.IsApplication()) {
2019-07-03 05:21:47 +00:00
return true;
}
}
return false;
}
Result StartProcess(ProcessInfo *process_info, const ldr::ProgramInfo *program_info) {
2019-06-29 09:20:36 +00:00
R_TRY(svcStartProcess(process_info->GetHandle(), program_info->main_thread_priority, program_info->default_cpu_id, program_info->main_thread_stack_size));
process_info->SetState(ProcessState_Running);
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
void CleanupProcessInfo(ProcessListAccessor &list, ProcessInfo *process_info) {
/* Remove the process from the list. */
list->Remove(process_info);
/* Delete the process. */
g_process_info_allocator.FreeProcessInfo(process_info);
}
Result LaunchProcess(os::WaitableManager &waitable_manager, const LaunchProcessArgs &args) {
2019-06-29 09:20:36 +00:00
/* Get Program Info. */
ldr::ProgramInfo program_info;
R_TRY(ldr::pm::GetProgramInfo(&program_info, args.location));
2019-06-29 09:20:36 +00:00
const bool is_application = (program_info.flags & ldr::ProgramInfoFlag_ApplicationTypeMask) == ldr::ProgramInfoFlag_Application;
2019-10-15 05:49:06 +00:00
const bool allow_debug = (program_info.flags & ldr::ProgramInfoFlag_AllowDebug) || hos::GetVersion() < hos::Version_200;
2019-06-29 09:20:36 +00:00
/* Ensure we only try to run one application. */
R_UNLESS(!is_application || !HasApplicationProcess(), pm::ResultApplicationRunning());
2019-06-29 09:20:36 +00:00
/* Fix the title location to use the right title id. */
const ncm::TitleLocation location = ncm::TitleLocation::Make(program_info.title_id, static_cast<ncm::StorageId>(args.location.storage_id));
2019-06-29 09:20:36 +00:00
/* Pin the program with loader. */
ldr::PinId pin_id;
R_TRY(ldr::pm::PinTitle(&pin_id, location));
2019-06-29 09:20:36 +00:00
/* Ensure resources are available. */
resource::WaitResourceAvailable(&program_info);
/* Actually create the process. */
Handle process_handle;
{
auto pin_guard = SCOPE_GUARD { ldr::pm::UnpinTitle(pin_id); };
R_TRY(ldr::pm::CreateProcess(&process_handle, pin_id, GetLoaderCreateProcessFlags(args.flags), resource::GetResourceLimitHandle(&program_info)));
pin_guard.Cancel();
}
2019-06-29 09:20:36 +00:00
/* Get the process id. */
os::ProcessId process_id = os::GetProcessId(process_handle);
2019-06-29 09:20:36 +00:00
/* Make new process info. */
void *process_info_storage = g_process_info_allocator.AllocateProcessInfoStorage();
AMS_ASSERT(process_info_storage != nullptr);
ProcessInfo *process_info = new (process_info_storage) ProcessInfo(process_handle, process_id, pin_id, location);
/* Link new process info. */
{
ProcessListAccessor list(g_process_list);
list->push_back(*process_info);
process_info->LinkToWaitableManager(waitable_manager);
}
/* Prevent resource leakage if register fails. */
auto cleanup_guard = SCOPE_GUARD {
ProcessListAccessor list(g_process_list);
process_info->Cleanup();
CleanupProcessInfo(list, process_info);
};
2019-06-29 09:20:36 +00:00
const u8 *acid_sac = program_info.ac_buffer;
const u8 *aci_sac = acid_sac + program_info.acid_sac_size;
const u8 *acid_fac = aci_sac + program_info.aci_sac_size;
const u8 *aci_fah = acid_fac + program_info.acid_fac_size;
/* Register with FS and SM. */
2019-10-15 05:49:06 +00:00
R_TRY(fsprRegisterProgram(static_cast<u64>(process_id), static_cast<u64>(location.title_id), static_cast<FsStorageId>(location.storage_id), aci_fah, program_info.aci_fah_size, acid_fac, program_info.acid_fac_size));
R_TRY(sm::manager::RegisterProcess(process_id, location.title_id, acid_sac, program_info.acid_sac_size, aci_sac, program_info.aci_sac_size));
2019-06-29 09:20:36 +00:00
/* Set flags. */
if (is_application) {
process_info->SetApplication();
}
if (ShouldSignalOnStart(args.flags) && allow_debug) {
2019-06-29 09:20:36 +00:00
process_info->SetSignalOnStart();
}
if (ShouldSignalOnExit(args.flags)) {
2019-06-29 09:20:36 +00:00
process_info->SetSignalOnExit();
}
if (ShouldSignalOnDebugEvent(args.flags) && allow_debug) {
2019-06-29 09:20:36 +00:00
process_info->SetSignalOnDebugEvent();
}
/* Process hooks/signaling. */
if (location.title_id == g_title_id_hook) {
g_hook_to_create_process_event.Signal();
2019-07-03 03:54:16 +00:00
g_title_id_hook = ncm::TitleId::Invalid;
2019-06-29 09:20:36 +00:00
} else if (is_application && g_application_hook) {
g_hook_to_create_application_process_event.Signal();
2019-06-29 09:20:36 +00:00
g_application_hook = false;
} else if (!ShouldStartSuspended(args.flags)) {
2019-06-29 09:20:36 +00:00
R_TRY(StartProcess(process_info, &program_info));
}
/* We succeeded, so we can cancel our cleanup. */
cleanup_guard.Cancel();
2019-06-29 09:20:36 +00:00
*args.out_process_id = process_id;
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
void OnProcessSignaled(ProcessListAccessor &list, ProcessInfo *process_info) {
/* Reset the process's signal. */
svcResetSignal(process_info->GetHandle());
2019-06-29 09:20:36 +00:00
/* Update the process's state. */
const ProcessState old_state = process_info->GetState();
{
u64 tmp = 0;
R_ASSERT(svcGetProcessInfo(&tmp, process_info->GetHandle(), ProcessInfoType_ProcessState));
process_info->SetState(static_cast<ProcessState>(tmp));
}
const ProcessState new_state = process_info->GetState();
2019-06-29 09:20:36 +00:00
/* If we're transitioning away from crashed, clear waiting attached. */
if (old_state == ProcessState_Crashed && new_state != ProcessState_Crashed) {
process_info->ClearExceptionWaitingAttach();
}
2019-06-29 09:20:36 +00:00
switch (new_state) {
case ProcessState_Created:
case ProcessState_CreatedAttached:
case ProcessState_Exiting:
break;
case ProcessState_Running:
if (process_info->ShouldSignalOnDebugEvent()) {
process_info->ClearSuspended();
process_info->SetSuspendedStateChanged();
g_process_event.Signal();
2019-10-15 05:49:06 +00:00
} else if (hos::GetVersion() >= hos::Version_200 && process_info->ShouldSignalOnStart()) {
process_info->SetStartedStateChanged();
process_info->ClearSignalOnStart();
g_process_event.Signal();
}
break;
case ProcessState_Crashed:
process_info->SetExceptionOccurred();
g_process_event.Signal();
break;
case ProcessState_RunningAttached:
if (process_info->ShouldSignalOnDebugEvent()) {
process_info->ClearSuspended();
process_info->SetSuspendedStateChanged();
g_process_event.Signal();
}
break;
case ProcessState_Exited:
2019-10-15 05:49:06 +00:00
if (hos::GetVersion() < hos::Version_500 && process_info->ShouldSignalOnExit()) {
g_process_event.Signal();
} else {
/* Free process resources, unlink from waitable manager. */
process_info->Cleanup();
/* Handle the case where we need to keep the process alive some time longer. */
2019-10-15 05:49:06 +00:00
if (hos::GetVersion() >= hos::Version_500 && process_info->ShouldSignalOnExit()) {
/* Remove from the living list. */
list->Remove(process_info);
/* Add the process to the list of dead processes. */
{
ProcessListAccessor dead_list(g_dead_process_list);
dead_list->push_back(*process_info);
}
/* Signal. */
g_process_event.Signal();
} else {
/* Actually delete process. */
CleanupProcessInfo(list, process_info);
}
}
case ProcessState_DebugSuspended:
if (process_info->ShouldSignalOnDebugEvent()) {
process_info->SetSuspended();
process_info->SetSuspendedStateChanged();
g_process_event.Signal();
}
break;
2019-06-29 09:20:36 +00:00
}
}
}
/* Initialization. */
Result InitializeProcessManager() {
/* Create events. */
R_ASSERT(g_process_event.InitializeAsInterProcessEvent());
R_ASSERT(g_hook_to_create_process_event.InitializeAsInterProcessEvent());
R_ASSERT(g_hook_to_create_application_process_event.InitializeAsInterProcessEvent());
R_ASSERT(g_boot_finished_event.InitializeAsInterProcessEvent());
2019-06-29 09:20:36 +00:00
/* Initialize resource limits. */
R_TRY(resource::InitializeResourceManager());
/* Start thread. */
R_ASSERT(g_process_track_thread.Start());
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
/* Process Management. */
2019-10-15 05:49:06 +00:00
Result LaunchTitle(os::ProcessId *out_process_id, const ncm::TitleLocation &loc, u32 flags) {
2019-06-29 09:20:36 +00:00
/* Ensure we only try to launch one title at a time. */
2019-09-24 10:15:36 +00:00
static os::Mutex s_lock;
2019-07-03 05:21:47 +00:00
std::scoped_lock lk(s_lock);
2019-06-29 09:20:36 +00:00
/* Set global arguments, signal, wait. */
g_process_launch_args = {
.out_process_id = out_process_id,
.location = loc,
.flags = flags,
};
g_process_launch_start_event.Signal();
2019-09-24 10:15:36 +00:00
g_process_launch_finish_event.Wait();
2019-06-29 09:20:36 +00:00
return g_process_launch_result;
}
2019-10-15 05:49:06 +00:00
Result StartProcess(os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
R_UNLESS(!process_info->HasStarted(), pm::ResultAlreadyStarted());
2019-06-29 09:20:36 +00:00
ldr::ProgramInfo program_info;
R_TRY(ldr::pm::GetProgramInfo(&program_info, process_info->GetTitleLocation()));
return StartProcess(process_info, &program_info);
}
2019-10-15 05:49:06 +00:00
Result TerminateProcess(os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
return svcTerminateProcess(process_info->GetHandle());
}
Result TerminateTitle(ncm::TitleId title_id) {
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(title_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
return svcTerminateProcess(process_info->GetHandle());
}
Result GetProcessEventHandle(Handle *out) {
*out = g_process_event.GetReadableHandle();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
Result GetProcessEventInfo(ProcessEventInfo *out) {
/* Check for event from current process. */
{
ProcessListAccessor list(g_process_list);
for (auto &process : *list) {
if (process.HasStarted() && process.HasStartedStateChanged()) {
process.ClearStartedStateChanged();
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::Started);
out->process_id = process.GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
if (process.HasSuspendedStateChanged()) {
process.ClearSuspendedStateChanged();
if (process.IsSuspended()) {
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::DebugSuspended);
} else {
out->event = GetProcessEventValue(ProcessEvent::DebugRunning);
}
out->process_id = process.GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
if (process.HasExceptionOccurred()) {
process.ClearExceptionOccurred();
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::Exception);
out->process_id = process.GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
if (hos::GetVersion() < hos::Version_500 && process.ShouldSignalOnExit() && process.HasExited()) {
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::Exited);
out->process_id = process.GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
}
}
/* Check for event from exited process. */
2019-10-15 05:49:06 +00:00
if (hos::GetVersion() >= hos::Version_500) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor dead_list(g_dead_process_list);
if (!dead_list->empty()) {
auto &process_info = dead_list->front();
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::Exited);
out->process_id = process_info.GetProcessId();
CleanupProcessInfo(dead_list, &process_info);
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
}
2019-10-15 05:49:06 +00:00
out->process_id = os::ProcessId{};
2019-06-29 09:20:36 +00:00
out->event = GetProcessEventValue(ProcessEvent::None);
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result CleanupProcess(os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
R_UNLESS(process_info->HasExited(), pm::ResultNotExited());
2019-06-29 09:20:36 +00:00
CleanupProcessInfo(list, process_info);
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result ClearExceptionOccurred(os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
process_info->ClearExceptionOccurred();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
/* Information Getters. */
Result GetModuleIdList(u32 *out_count, u8 *out_buf, size_t max_out_count, u64 unused) {
/* This function was always stubbed... */
*out_count = 0;
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result GetExceptionProcessIdList(u32 *out_count, os::ProcessId *out_process_ids, size_t max_out_count) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
size_t count = 0;
for (auto &process : *list) {
if (process.HasExceptionWaitingAttach()) {
out_process_ids[count++] = process.GetProcessId();
2019-06-29 09:20:36 +00:00
}
}
*out_count = static_cast<u32>(count);
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result GetProcessId(os::ProcessId *out, ncm::TitleId title_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(title_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
*out = process_info->GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result GetTitleId(ncm::TitleId *out, os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
*out = process_info->GetTitleLocation().title_id;
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result GetApplicationProcessId(os::ProcessId *out_process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
for (auto &process : *list) {
if (process.IsApplication()) {
*out_process_id = process.GetProcessId();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
}
return pm::ResultProcessNotFound();
2019-06-29 09:20:36 +00:00
}
2019-10-15 05:49:06 +00:00
Result AtmosphereGetProcessInfo(Handle *out_process_handle, ncm::TitleLocation *out_loc, os::ProcessId process_id) {
2019-06-29 09:20:36 +00:00
ProcessListAccessor list(g_process_list);
auto process_info = list->Find(process_id);
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
2019-06-29 09:20:36 +00:00
*out_process_handle = process_info->GetHandle();
*out_loc = process_info->GetTitleLocation();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
/* Hook API. */
Result HookToCreateProcess(Handle *out_hook, ncm::TitleId title_id) {
*out_hook = INVALID_HANDLE;
{
ncm::TitleId old_value = ncm::TitleId::Invalid;
R_UNLESS(g_title_id_hook.compare_exchange_strong(old_value, title_id), pm::ResultDebugHookInUse());
2019-06-29 09:20:36 +00:00
}
*out_hook = g_hook_to_create_process_event.GetReadableHandle();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
Result HookToCreateApplicationProcess(Handle *out_hook) {
*out_hook = INVALID_HANDLE;
{
bool old_value = false;
R_UNLESS(g_application_hook.compare_exchange_strong(old_value, true), pm::ResultDebugHookInUse());
2019-06-29 09:20:36 +00:00
}
*out_hook = g_hook_to_create_application_process_event.GetReadableHandle();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
Result ClearHook(u32 which) {
if (which & HookType_TitleId) {
2019-07-03 03:54:16 +00:00
g_title_id_hook = ncm::TitleId::Invalid;
2019-06-29 09:20:36 +00:00
}
if (which & HookType_Application) {
g_application_hook = false;
}
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
/* Boot API. */
Result NotifyBootFinished() {
static bool g_has_boot_finished = false;
if (!g_has_boot_finished) {
2019-10-19 04:06:40 +00:00
boot2::LaunchPreSdCardBootProgramsAndBoot2();
2019-06-29 09:20:36 +00:00
g_has_boot_finished = true;
g_boot_finished_event.Signal();
2019-06-29 09:20:36 +00:00
}
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
Result GetBootFinishedEventHandle(Handle *out) {
/* 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. */
AMS_ASSERT(spl::IsRecoveryBoot());
*out = g_boot_finished_event.GetReadableHandle();
return ResultSuccess();
2019-06-29 09:20:36 +00:00
}
/* Resource Limit API. */
Result BoostSystemMemoryResourceLimit(u64 boost_size) {
return resource::BoostSystemMemoryResourceLimit(boost_size);
}
Result BoostApplicationThreadResourceLimit() {
return resource::BoostApplicationThreadResourceLimit();
2019-06-29 09:20:36 +00:00
}
Result AtmosphereGetCurrentLimitInfo(u64 *out_cur_val, u64 *out_lim_val, u32 group, u32 resource) {
return resource::GetResourceLimitValues(out_cur_val, out_lim_val, static_cast<ResourceLimitGroup>(group), static_cast<LimitableResource>(resource));
}
}