2019-06-29 11:20:36 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-29 11:20:36 +02: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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-29 11:20:36 +02:00
|
|
|
#include "pm_process_manager.hpp"
|
|
|
|
#include "pm_resource_manager.hpp"
|
|
|
|
|
|
|
|
#include "pm_process_info.hpp"
|
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::pm::impl {
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Types. */
|
|
|
|
enum HookType {
|
2019-10-28 05:43:01 +01:00
|
|
|
HookType_ProgramId = (1 << 0),
|
2019-06-29 11:20:36 +02:00
|
|
|
HookType_Application = (1 << 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LaunchProcessArgs {
|
2019-10-15 07:49:06 +02:00
|
|
|
os::ProcessId *out_process_id;
|
2019-10-28 05:43:01 +01:00
|
|
|
ncm::ProgramLocation location;
|
2019-06-29 11:20:36 +02:00
|
|
|
u32 flags;
|
|
|
|
};
|
|
|
|
|
2020-04-14 07:19:44 +02:00
|
|
|
#define GET_FLAG_MASK(flag) (hos_version >= hos::Version_5_0_0 ? static_cast<u32>(LaunchFlags_##flag) : static_cast<u32>(LaunchFlagsDeprecated_##flag))
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
inline bool ShouldSignalOnExit(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2019-06-29 11:20:36 +02:00
|
|
|
return launch_flags & GET_FLAG_MASK(SignalOnExit);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ShouldSignalOnStart(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos_version < hos::Version_2_0_0) {
|
2019-06-29 11:20:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return launch_flags & GET_FLAG_MASK(SignalOnStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ShouldSignalOnException(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2019-06-29 11:20:36 +02:00
|
|
|
return launch_flags & GET_FLAG_MASK(SignalOnException);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ShouldSignalOnDebugEvent(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2019-06-29 11:20:36 +02:00
|
|
|
return launch_flags & GET_FLAG_MASK(SignalOnDebugEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ShouldStartSuspended(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2019-06-29 11:20:36 +02:00
|
|
|
return launch_flags & GET_FLAG_MASK(StartSuspended);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ShouldDisableAslr(u32 launch_flags) {
|
2019-10-15 07:49:06 +02:00
|
|
|
const auto hos_version = hos::GetVersion();
|
2019-06-29 11:20:36 +02:00
|
|
|
return launch_flags & GET_FLAG_MASK(DisableAslr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef GET_FLAG_MASK
|
|
|
|
|
2019-10-15 08:12:07 +02:00
|
|
|
template<size_t MaxProcessInfos>
|
|
|
|
class ProcessInfoAllocator {
|
|
|
|
NON_COPYABLE(ProcessInfoAllocator);
|
|
|
|
NON_MOVEABLE(ProcessInfoAllocator);
|
|
|
|
static_assert(MaxProcessInfos >= 0x40, "MaxProcessInfos is too small.");
|
|
|
|
private:
|
2021-10-10 09:14:06 +02:00
|
|
|
util::TypedStorage<ProcessInfo> m_process_info_storages[MaxProcessInfos]{};
|
|
|
|
bool m_process_info_allocated[MaxProcessInfos]{};
|
|
|
|
os::SdkMutex m_lock{};
|
2019-10-15 08:12:07 +02:00
|
|
|
private:
|
|
|
|
constexpr inline size_t GetProcessInfoIndex(ProcessInfo *process_info) const {
|
2021-10-10 09:14:06 +02:00
|
|
|
return process_info - GetPointer(m_process_info_storages[0]);
|
2019-10-15 08:12:07 +02:00
|
|
|
}
|
|
|
|
public:
|
2021-09-30 07:52:50 +02:00
|
|
|
constexpr ProcessInfoAllocator() = default;
|
2019-10-15 08:12:07 +02:00
|
|
|
|
2021-03-22 04:30:40 +01:00
|
|
|
template<typename... Args>
|
|
|
|
ProcessInfo *AllocateProcessInfo(Args &&... args) {
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_lock);
|
2021-03-22 04:30:40 +01:00
|
|
|
|
2019-10-15 08:12:07 +02:00
|
|
|
for (size_t i = 0; i < MaxProcessInfos; i++) {
|
2021-10-10 09:14:06 +02:00
|
|
|
if (!m_process_info_allocated[i]) {
|
|
|
|
m_process_info_allocated[i] = true;
|
2021-03-22 04:30:40 +01:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
std::memset(m_process_info_storages + i, 0, sizeof(m_process_info_storages[i]));
|
2021-03-22 04:30:40 +01:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
return util::ConstructAt(m_process_info_storages[i], std::forward<Args>(args)...);
|
2019-10-15 08:12:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 04:30:40 +01:00
|
|
|
|
2019-10-15 08:12:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeProcessInfo(ProcessInfo *process_info) {
|
2021-10-10 09:14:06 +02:00
|
|
|
std::scoped_lock lk(m_lock);
|
2019-10-15 08:12:07 +02:00
|
|
|
|
|
|
|
const size_t index = this->GetProcessInfoIndex(process_info);
|
2020-02-23 08:05:14 +01:00
|
|
|
AMS_ABORT_UNLESS(index < MaxProcessInfos);
|
2021-10-10 09:14:06 +02:00
|
|
|
AMS_ABORT_UNLESS(m_process_info_allocated[index]);
|
2019-10-15 08:12:07 +02:00
|
|
|
|
2021-10-10 09:14:06 +02:00
|
|
|
util::DestroyAt(m_process_info_storages[index]);
|
|
|
|
m_process_info_allocated[index] = false;
|
2019-10-15 08:12:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-29 11:20:36 +02:00
|
|
|
/* Process Tracking globals. */
|
2021-10-07 08:22:54 +02:00
|
|
|
void ProcessTrackingMain(void *);
|
2019-10-19 05:31:15 +02:00
|
|
|
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit os::ThreadType g_process_track_thread;
|
|
|
|
alignas(os::ThreadStackAlignment) constinit u8 g_process_track_thread_stack[16_KB];
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Process lists. */
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit ProcessList g_process_list;
|
|
|
|
constinit ProcessList g_dead_process_list;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-15 08:12:07 +02:00
|
|
|
/* 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;
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit ProcessInfoAllocator<MaxProcessCount> g_process_info_allocator;
|
2019-10-15 08:12:07 +02:00
|
|
|
|
2019-06-29 11:20:36 +02:00
|
|
|
/* Global events. */
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit os::SystemEventType g_process_event;
|
|
|
|
constinit os::SystemEventType g_hook_to_create_process_event;
|
|
|
|
constinit os::SystemEventType g_hook_to_create_application_process_event;
|
|
|
|
constinit os::SystemEventType g_boot_finished_event;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Process Launch synchronization globals. */
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit os::SdkMutex g_launch_program_lock;
|
2020-04-08 11:21:35 +02:00
|
|
|
os::Event g_process_launch_start_event(os::EventClearMode_AutoClear);
|
|
|
|
os::Event g_process_launch_finish_event(os::EventClearMode_AutoClear);
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit Result g_process_launch_result = ResultSuccess();
|
|
|
|
constinit LaunchProcessArgs g_process_launch_args = {};
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Hook globals. */
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit std::atomic<ncm::ProgramId> g_program_id_hook;
|
|
|
|
constinit std::atomic<bool> g_application_hook;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
/* Forward declarations. */
|
2021-10-01 04:00:47 +02:00
|
|
|
Result LaunchProcess(os::MultiWaitType &multi_wait, const LaunchProcessArgs &args);
|
2019-10-24 10:40:44 +02:00
|
|
|
void OnProcessSignaled(ProcessListAccessor &list, ProcessInfo *process_info);
|
2019-09-28 03:04:58 +02:00
|
|
|
|
2019-06-29 11:20:36 +02:00
|
|
|
/* Helpers. */
|
2021-10-07 08:22:54 +02:00
|
|
|
void ProcessTrackingMain(void *) {
|
2019-06-29 11:20:36 +02:00
|
|
|
/* This is the main loop of the process tracking thread. */
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
/* Setup multi wait/holders. */
|
|
|
|
os::MultiWaitType process_multi_wait;
|
|
|
|
os::MultiWaitHolderType start_event_holder;
|
|
|
|
os::InitializeMultiWait(std::addressof(process_multi_wait));
|
|
|
|
os::InitializeMultiWaitHolder(std::addressof(start_event_holder), g_process_launch_start_event.GetBase());
|
|
|
|
os::LinkMultiWaitHolder(std::addressof(process_multi_wait), std::addressof(start_event_holder));
|
2019-09-28 03:04:58 +02:00
|
|
|
|
|
|
|
while (true) {
|
2021-10-01 04:00:47 +02:00
|
|
|
auto signaled_holder = os::WaitAny(std::addressof(process_multi_wait));
|
2021-10-09 23:49:53 +02:00
|
|
|
if (signaled_holder == std::addressof(start_event_holder)) {
|
2019-09-28 03:04:58 +02:00
|
|
|
/* Launch start event signaled. */
|
|
|
|
/* TryWait will clear signaled, preventing duplicate notifications. */
|
|
|
|
if (g_process_launch_start_event.TryWait()) {
|
2021-10-01 04:00:47 +02:00
|
|
|
g_process_launch_result = LaunchProcess(process_multi_wait, g_process_launch_args);
|
2019-09-28 03:04:58 +02:00
|
|
|
g_process_launch_finish_event.Signal();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Some process was signaled. */
|
|
|
|
ProcessListAccessor list(g_process_list);
|
2021-10-01 04:00:47 +02:00
|
|
|
OnProcessSignaled(list, reinterpret_cast<ProcessInfo *>(os::GetMultiWaitHolderUserData(signaled_holder)));
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 GetLoaderCreateProcessFlags(u32 launch_flags) {
|
|
|
|
u32 ldr_flags = 0;
|
|
|
|
|
2020-04-14 07:19:44 +02:00
|
|
|
if (ShouldSignalOnException(launch_flags) || (hos::GetVersion() >= hos::Version_2_0_0 && !ShouldStartSuspended(launch_flags))) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ldr_flags |= ldr::CreateProcessFlag_EnableDebug;
|
|
|
|
}
|
|
|
|
if (ShouldDisableAslr(launch_flags)) {
|
|
|
|
ldr_flags |= ldr::CreateProcessFlag_DisableAslr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ldr_flags;
|
|
|
|
}
|
|
|
|
|
2019-07-03 07:21:47 +02:00
|
|
|
bool HasApplicationProcess() {
|
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
for (auto &process : *list) {
|
|
|
|
if (process.IsApplication()) {
|
2019-07-03 07:21:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
Result StartProcess(ProcessInfo *process_info, const ldr::ProgramInfo *program_info) {
|
2021-10-04 23:54:13 +02:00
|
|
|
R_TRY(svc::StartProcess(process_info->GetHandle(), program_info->main_thread_priority, program_info->default_cpu_id, program_info->main_thread_stack_size));
|
2020-03-30 00:24:40 +02:00
|
|
|
process_info->SetState(svc::ProcessState_Running);
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
void CleanupProcessInfo(ProcessListAccessor &list, ProcessInfo *process_info) {
|
|
|
|
/* Remove the process from the list. */
|
|
|
|
list->Remove(process_info);
|
|
|
|
|
|
|
|
/* Delete the process. */
|
2019-10-15 08:12:07 +02:00
|
|
|
g_process_info_allocator.FreeProcessInfo(process_info);
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 04:00:47 +02:00
|
|
|
Result LaunchProcess(os::MultiWaitType &multi_wait, const LaunchProcessArgs &args) {
|
2019-06-29 11:20:36 +02:00
|
|
|
/* Get Program Info. */
|
|
|
|
ldr::ProgramInfo program_info;
|
2019-11-21 13:03:19 +01:00
|
|
|
cfg::OverrideStatus override_status;
|
2021-10-09 23:49:53 +02:00
|
|
|
R_TRY(ldr::pm::AtmosphereGetProgramInfo(std::addressof(program_info), std::addressof(override_status), args.location));
|
2019-06-29 11:20:36 +02:00
|
|
|
const bool is_application = (program_info.flags & ldr::ProgramInfoFlag_ApplicationTypeMask) == ldr::ProgramInfoFlag_Application;
|
2020-04-14 07:19:44 +02:00
|
|
|
const bool allow_debug = (program_info.flags & ldr::ProgramInfoFlag_AllowDebug) || hos::GetVersion() < hos::Version_2_0_0;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Ensure we only try to run one application. */
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(!is_application || !HasApplicationProcess(), pm::ResultApplicationRunning());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
/* Fix the program location to use the right program id. */
|
|
|
|
const ncm::ProgramLocation location = ncm::ProgramLocation::Make(program_info.program_id, static_cast<ncm::StorageId>(args.location.storage_id));
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
/* Pin and create the process. */
|
|
|
|
os::NativeHandle process_handle;
|
2019-06-29 11:20:36 +02:00
|
|
|
ldr::PinId pin_id;
|
2022-02-14 23:45:32 +01:00
|
|
|
{
|
|
|
|
/* Pin the program with loader. */
|
|
|
|
R_TRY(ldr::pm::AtmospherePinProgram(std::addressof(pin_id), location, override_status));
|
2019-10-24 10:40:44 +02:00
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
/* If we fail after now, unpin. */
|
|
|
|
ON_RESULT_FAILURE { ldr::pm::UnpinProgram(pin_id); };
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
/* Ensure resources are available. */
|
|
|
|
resource::WaitResourceAvailable(std::addressof(program_info));
|
|
|
|
|
|
|
|
/* Actually create the process. */
|
2021-10-09 23:49:53 +02:00
|
|
|
R_TRY(ldr::pm::CreateProcess(std::addressof(process_handle), pin_id, GetLoaderCreateProcessFlags(args.flags), resource::GetResourceLimitHandle(std::addressof(program_info))));
|
2019-10-24 10:40:44 +02:00
|
|
|
}
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Get the process id. */
|
2019-10-20 02:42:53 +02:00
|
|
|
os::ProcessId process_id = os::GetProcessId(process_handle);
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Make new process info. */
|
2021-03-22 04:30:40 +01:00
|
|
|
ProcessInfo *process_info = g_process_info_allocator.AllocateProcessInfo(process_handle, process_id, pin_id, location, override_status);
|
|
|
|
AMS_ABORT_UNLESS(process_info != nullptr);
|
2019-09-28 03:04:58 +02:00
|
|
|
|
|
|
|
/* Link new process info. */
|
|
|
|
{
|
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
list->push_back(*process_info);
|
2021-10-01 04:00:47 +02:00
|
|
|
process_info->LinkToMultiWait(multi_wait);
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prevent resource leakage if register fails. */
|
2022-02-14 23:45:32 +01:00
|
|
|
ON_RESULT_FAILURE {
|
2019-09-28 03:04:58 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
process_info->Cleanup();
|
|
|
|
CleanupProcessInfo(list, process_info);
|
|
|
|
};
|
2019-06-29 11:20:36 +02: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-11-21 09:28:32 +01:00
|
|
|
R_TRY(fsprRegisterProgram(static_cast<u64>(process_id), static_cast<u64>(location.program_id), static_cast<NcmStorageId>(location.storage_id), aci_fah, program_info.aci_fah_size, acid_fac, program_info.acid_fac_size));
|
2019-11-21 13:03:19 +01:00
|
|
|
R_TRY(sm::manager::RegisterProcess(process_id, location.program_id, override_status, acid_sac, program_info.acid_sac_size, aci_sac, program_info.aci_sac_size));
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Set flags. */
|
|
|
|
if (is_application) {
|
|
|
|
process_info->SetApplication();
|
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
if (ShouldSignalOnStart(args.flags) && allow_debug) {
|
2019-06-29 11:20:36 +02:00
|
|
|
process_info->SetSignalOnStart();
|
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
if (ShouldSignalOnExit(args.flags)) {
|
2019-06-29 11:20:36 +02:00
|
|
|
process_info->SetSignalOnExit();
|
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
if (ShouldSignalOnDebugEvent(args.flags) && allow_debug) {
|
2019-06-29 11:20:36 +02:00
|
|
|
process_info->SetSignalOnDebugEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process hooks/signaling. */
|
2019-10-28 05:43:01 +01:00
|
|
|
if (location.program_id == g_program_id_hook) {
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_hook_to_create_process_event));
|
2020-03-08 09:06:23 +01:00
|
|
|
g_program_id_hook = ncm::InvalidProgramId;
|
2019-06-29 11:20:36 +02:00
|
|
|
} else if (is_application && g_application_hook) {
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_hook_to_create_application_process_event));
|
2019-06-29 11:20:36 +02:00
|
|
|
g_application_hook = false;
|
2019-09-28 03:04:58 +02:00
|
|
|
} else if (!ShouldStartSuspended(args.flags)) {
|
2021-10-09 23:49:53 +02:00
|
|
|
R_TRY(StartProcess(process_info, std::addressof(program_info)));
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
*args.out_process_id = process_id;
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
void OnProcessSignaled(ProcessListAccessor &list, ProcessInfo *process_info) {
|
|
|
|
/* Reset the process's signal. */
|
2021-10-04 23:54:13 +02:00
|
|
|
svc::ResetSignal(process_info->GetHandle());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
/* Update the process's state. */
|
2020-03-30 00:24:40 +02:00
|
|
|
const svc::ProcessState old_state = process_info->GetState();
|
2019-09-28 03:04:58 +02:00
|
|
|
{
|
2020-03-30 00:24:40 +02:00
|
|
|
s64 tmp = 0;
|
2021-10-09 23:49:53 +02:00
|
|
|
R_ABORT_UNLESS(svc::GetProcessInfo(std::addressof(tmp), process_info->GetHandle(), svc::ProcessInfoType_ProcessState));
|
2020-03-30 00:24:40 +02:00
|
|
|
process_info->SetState(static_cast<svc::ProcessState>(tmp));
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
2020-03-30 00:24:40 +02:00
|
|
|
const svc::ProcessState new_state = process_info->GetState();
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
/* If we're transitioning away from crashed, clear waiting attached. */
|
2020-03-30 00:24:40 +02:00
|
|
|
if (old_state == svc::ProcessState_Crashed && new_state != svc::ProcessState_Crashed) {
|
2019-09-28 03:04:58 +02:00
|
|
|
process_info->ClearExceptionWaitingAttach();
|
|
|
|
}
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
switch (new_state) {
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_Created:
|
|
|
|
case svc::ProcessState_CreatedAttached:
|
|
|
|
case svc::ProcessState_Terminating:
|
2019-09-28 03:04:58 +02:00
|
|
|
break;
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_Running:
|
2019-09-28 03:04:58 +02:00
|
|
|
if (process_info->ShouldSignalOnDebugEvent()) {
|
|
|
|
process_info->ClearSuspended();
|
|
|
|
process_info->SetSuspendedStateChanged();
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2020-04-14 07:19:44 +02:00
|
|
|
} else if (hos::GetVersion() >= hos::Version_2_0_0 && process_info->ShouldSignalOnStart()) {
|
2019-09-28 03:04:58 +02:00
|
|
|
process_info->SetStartedStateChanged();
|
|
|
|
process_info->ClearSignalOnStart();
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
2020-12-15 03:53:16 +01:00
|
|
|
process_info->ClearUnhandledException();
|
2019-09-28 03:04:58 +02:00
|
|
|
break;
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_Crashed:
|
2020-12-15 03:53:16 +01:00
|
|
|
if (!process_info->HasUnhandledException()) {
|
|
|
|
process_info->SetExceptionOccurred();
|
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
|
|
|
}
|
|
|
|
process_info->SetExceptionWaitingAttach();
|
2019-09-28 03:04:58 +02:00
|
|
|
break;
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_RunningAttached:
|
2019-09-28 03:04:58 +02:00
|
|
|
if (process_info->ShouldSignalOnDebugEvent()) {
|
|
|
|
process_info->ClearSuspended();
|
|
|
|
process_info->SetSuspendedStateChanged();
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
2020-12-15 03:53:16 +01:00
|
|
|
process_info->ClearUnhandledException();
|
2019-09-28 03:04:58 +02:00
|
|
|
break;
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_Terminated:
|
2021-10-01 04:00:47 +02:00
|
|
|
/* Free process resources, unlink from multi wait. */
|
2019-12-13 22:35:33 +01:00
|
|
|
process_info->Cleanup();
|
|
|
|
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos::GetVersion() < hos::Version_5_0_0 && process_info->ShouldSignalOnExit()) {
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2019-09-28 03:04:58 +02:00
|
|
|
} else {
|
|
|
|
/* Handle the case where we need to keep the process alive some time longer. */
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos::GetVersion() >= hos::Version_5_0_0 && process_info->ShouldSignalOnExit()) {
|
2019-09-28 03:04:58 +02:00
|
|
|
/* 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. */
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2019-09-28 03:04:58 +02:00
|
|
|
} else {
|
|
|
|
/* Actually delete process. */
|
|
|
|
CleanupProcessInfo(list, process_info);
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 22:35:33 +01:00
|
|
|
break;
|
2020-03-30 00:24:40 +02:00
|
|
|
case svc::ProcessState_DebugBreak:
|
2019-09-28 03:04:58 +02:00
|
|
|
if (process_info->ShouldSignalOnDebugEvent()) {
|
|
|
|
process_info->SetSuspended();
|
|
|
|
process_info->SetSuspendedStateChanged();
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_process_event));
|
2019-09-28 03:04:58 +02:00
|
|
|
}
|
|
|
|
break;
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialization. */
|
|
|
|
Result InitializeProcessManager() {
|
|
|
|
/* Create events. */
|
2020-04-08 11:21:35 +02:00
|
|
|
R_ABORT_UNLESS(os::CreateSystemEvent(std::addressof(g_process_event), os::EventClearMode_AutoClear, true));
|
|
|
|
R_ABORT_UNLESS(os::CreateSystemEvent(std::addressof(g_hook_to_create_process_event), os::EventClearMode_AutoClear, true));
|
|
|
|
R_ABORT_UNLESS(os::CreateSystemEvent(std::addressof(g_hook_to_create_application_process_event), os::EventClearMode_AutoClear, true));
|
|
|
|
R_ABORT_UNLESS(os::CreateSystemEvent(std::addressof(g_boot_finished_event), os::EventClearMode_AutoClear, true));
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Initialize resource limits. */
|
|
|
|
R_TRY(resource::InitializeResourceManager());
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
/* Create thread. */
|
2020-04-17 10:06:07 +02:00
|
|
|
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_process_track_thread), ProcessTrackingMain, nullptr, g_process_track_thread_stack, sizeof(g_process_track_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(pm, ProcessTrack)));
|
|
|
|
os::SetThreadNamePointer(std::addressof(g_process_track_thread), AMS_GET_SYSTEM_THREAD_NAME(pm, ProcessTrack));
|
2020-04-08 11:21:35 +02:00
|
|
|
|
2019-06-29 11:20:36 +02:00
|
|
|
/* Start thread. */
|
2020-04-08 11:21:35 +02:00
|
|
|
os::StartThread(std::addressof(g_process_track_thread));
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process Management. */
|
2019-10-28 05:43:01 +01:00
|
|
|
Result LaunchProgram(os::ProcessId *out_process_id, const ncm::ProgramLocation &loc, u32 flags) {
|
|
|
|
/* Ensure we only try to launch one program at a time. */
|
2020-02-23 08:11:28 +01:00
|
|
|
std::scoped_lock lk(g_launch_program_lock);
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
/* Set global arguments, signal, wait. */
|
|
|
|
g_process_launch_args = {
|
|
|
|
.out_process_id = out_process_id,
|
|
|
|
.location = loc,
|
|
|
|
.flags = flags,
|
|
|
|
};
|
2019-09-28 03:04:58 +02:00
|
|
|
g_process_launch_start_event.Signal();
|
2019-09-24 12:15:36 +02:00
|
|
|
g_process_launch_finish_event.Wait();
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
return g_process_launch_result;
|
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result StartProcess(os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
|
|
|
R_UNLESS(!process_info->HasStarted(), pm::ResultAlreadyStarted());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
ldr::ProgramInfo program_info;
|
2021-10-09 23:49:53 +02:00
|
|
|
R_TRY(ldr::pm::GetProgramInfo(std::addressof(program_info), process_info->GetProgramLocation()));
|
|
|
|
return StartProcess(process_info, std::addressof(program_info));
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result TerminateProcess(os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
return svc::TerminateProcess(process_info->GetHandle());
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result TerminateProgram(ncm::ProgramId program_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
auto process_info = list->Find(program_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
return svc::TerminateProcess(process_info->GetHandle());
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
Result GetProcessEventHandle(os::NativeHandle *out) {
|
2020-04-08 11:21:35 +02:00
|
|
|
*out = os::GetReadableHandleOfSystemEvent(std::addressof(g_process_event));
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetProcessEventInfo(ProcessEventInfo *out) {
|
|
|
|
/* Check for event from current process. */
|
|
|
|
{
|
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
|
|
|
|
for (auto &process : *list) {
|
|
|
|
if (process.HasStarted() && process.HasStartedStateChanged()) {
|
|
|
|
process.ClearStartedStateChanged();
|
2019-06-29 11:20:36 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::Started);
|
2019-09-28 03:04:58 +02:00
|
|
|
out->process_id = process.GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
if (process.HasSuspendedStateChanged()) {
|
|
|
|
process.ClearSuspendedStateChanged();
|
|
|
|
if (process.IsSuspended()) {
|
2020-03-30 00:24:40 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::DebugBreak);
|
2019-06-29 11:20:36 +02:00
|
|
|
} else {
|
|
|
|
out->event = GetProcessEventValue(ProcessEvent::DebugRunning);
|
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
out->process_id = process.GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
2019-09-28 03:04:58 +02:00
|
|
|
if (process.HasExceptionOccurred()) {
|
|
|
|
process.ClearExceptionOccurred();
|
2019-06-29 11:20:36 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::Exception);
|
2019-09-28 03:04:58 +02:00
|
|
|
out->process_id = process.GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos::GetVersion() < hos::Version_5_0_0 && process.ShouldSignalOnExit() && process.HasTerminated()) {
|
2019-06-29 11:20:36 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::Exited);
|
2019-09-28 03:04:58 +02:00
|
|
|
out->process_id = process.GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for event from exited process. */
|
2020-04-14 07:19:44 +02:00
|
|
|
if (hos::GetVersion() >= hos::Version_5_0_0) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor dead_list(g_dead_process_list);
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
if (!dead_list->empty()) {
|
|
|
|
auto &process_info = dead_list->front();
|
2019-06-29 11:20:36 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::Exited);
|
2019-09-28 03:04:58 +02:00
|
|
|
out->process_id = process_info.GetProcessId();
|
|
|
|
|
2021-10-09 23:49:53 +02:00
|
|
|
CleanupProcessInfo(dead_list, std::addressof(process_info));
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
out->process_id = os::ProcessId{};
|
2019-06-29 11:20:36 +02:00
|
|
|
out->event = GetProcessEventValue(ProcessEvent::None);
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result CleanupProcess(os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2020-03-30 00:24:40 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
|
|
|
R_UNLESS(process_info->HasTerminated(), pm::ResultNotTerminated());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
CleanupProcessInfo(list, process_info);
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result ClearExceptionOccurred(os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
process_info->ClearExceptionOccurred();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Information Getters. */
|
|
|
|
Result GetModuleIdList(u32 *out_count, u8 *out_buf, size_t max_out_count, u64 unused) {
|
|
|
|
/* This function was always stubbed... */
|
2021-10-07 08:22:54 +02:00
|
|
|
AMS_UNUSED(out_buf, max_out_count, unused);
|
2019-06-29 11:20:36 +02:00
|
|
|
*out_count = 0;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result GetExceptionProcessIdList(u32 *out_count, os::ProcessId *out_process_ids, size_t max_out_count) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
size_t count = 0;
|
2021-10-07 08:22:54 +02:00
|
|
|
|
|
|
|
if (max_out_count > 0) {
|
|
|
|
for (auto &process : *list) {
|
|
|
|
if (process.HasExceptionWaitingAttach()) {
|
|
|
|
out_process_ids[count++] = process.GetProcessId();
|
|
|
|
|
|
|
|
if (count >= max_out_count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-07 08:22:54 +02:00
|
|
|
|
2019-06-29 11:20:36 +02:00
|
|
|
*out_count = static_cast<u32>(count);
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result GetProcessId(os::ProcessId *out, ncm::ProgramId program_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
auto process_info = list->Find(program_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
*out = process_info->GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
Result GetProgramId(ncm::ProgramId *out, os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-28 05:43:01 +01:00
|
|
|
*out = process_info->GetProgramLocation().program_id;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:49:06 +02:00
|
|
|
Result GetApplicationProcessId(os::ProcessId *out_process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
2019-09-28 03:04:58 +02:00
|
|
|
for (auto &process : *list) {
|
|
|
|
if (process.IsApplication()) {
|
|
|
|
*out_process_id = process.GetProcessId();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return pm::ResultProcessNotFound();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
Result AtmosphereGetProcessInfo(os::NativeHandle *out_process_handle, ncm::ProgramLocation *out_loc, cfg::OverrideStatus *out_status, os::ProcessId process_id) {
|
2019-06-29 11:20:36 +02:00
|
|
|
ProcessListAccessor list(g_process_list);
|
|
|
|
|
|
|
|
auto process_info = list->Find(process_id);
|
2019-10-24 10:40:44 +02:00
|
|
|
R_UNLESS(process_info != nullptr, pm::ResultProcessNotFound());
|
2019-06-29 11:20:36 +02:00
|
|
|
|
|
|
|
*out_process_handle = process_info->GetHandle();
|
2019-10-28 05:43:01 +01:00
|
|
|
*out_loc = process_info->GetProgramLocation();
|
2019-11-21 13:03:19 +01:00
|
|
|
*out_status = process_info->GetOverrideStatus();
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Hook API. */
|
2021-10-04 23:54:13 +02:00
|
|
|
Result HookToCreateProcess(os::NativeHandle *out_hook, ncm::ProgramId program_id) {
|
|
|
|
*out_hook = os::InvalidNativeHandle;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
{
|
2020-03-08 09:06:23 +01:00
|
|
|
ncm::ProgramId old_value = ncm::InvalidProgramId;
|
2019-10-28 05:43:01 +01:00
|
|
|
R_UNLESS(g_program_id_hook.compare_exchange_strong(old_value, program_id), pm::ResultDebugHookInUse());
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
*out_hook = os::GetReadableHandleOfSystemEvent(std::addressof(g_hook_to_create_process_event));
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
Result HookToCreateApplicationProcess(os::NativeHandle *out_hook) {
|
|
|
|
*out_hook = os::InvalidNativeHandle;
|
2019-06-29 11:20:36 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
{
|
|
|
|
bool old_value = false;
|
|
|
|
R_UNLESS(g_application_hook.compare_exchange_strong(old_value, true), pm::ResultDebugHookInUse());
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:21:35 +02:00
|
|
|
*out_hook = os::GetReadableHandleOfSystemEvent(std::addressof(g_hook_to_create_application_process_event));
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ClearHook(u32 which) {
|
2019-10-28 05:43:01 +01:00
|
|
|
if (which & HookType_ProgramId) {
|
2020-03-08 09:06:23 +01:00
|
|
|
g_program_id_hook = ncm::InvalidProgramId;
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
if (which & HookType_Application) {
|
|
|
|
g_application_hook = false;
|
|
|
|
}
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Boot API. */
|
|
|
|
Result NotifyBootFinished() {
|
|
|
|
static bool g_has_boot_finished = false;
|
|
|
|
if (!g_has_boot_finished) {
|
2020-04-14 11:54:55 +02:00
|
|
|
/* Set program verification disabled, if we should. */
|
|
|
|
/* NOTE: Nintendo does not check the result of this. */
|
|
|
|
if (spl::IsDisabledProgramVerification()) {
|
|
|
|
if (hos::GetVersion() >= hos::Version_10_0_0) {
|
|
|
|
ldr::pm::SetEnabledProgramVerification(false);
|
|
|
|
} else {
|
|
|
|
fsprSetEnabledProgramVerification(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-19 06:06:40 +02:00
|
|
|
boot2::LaunchPreSdCardBootProgramsAndBoot2();
|
2019-06-29 11:20:36 +02:00
|
|
|
g_has_boot_finished = true;
|
2020-04-08 11:21:35 +02:00
|
|
|
os::SignalSystemEvent(std::addressof(g_boot_finished_event));
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
Result GetBootFinishedEventHandle(os::NativeHandle *out) {
|
2019-06-29 11:20:36 +02:00
|
|
|
/* 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. */
|
2020-02-23 08:05:14 +01:00
|
|
|
AMS_ABORT_UNLESS(spl::IsRecoveryBoot());
|
2020-04-08 11:21:35 +02:00
|
|
|
*out = os::GetReadableHandleOfSystemEvent(std::addressof(g_boot_finished_event));
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resource Limit API. */
|
|
|
|
Result BoostSystemMemoryResourceLimit(u64 boost_size) {
|
|
|
|
return resource::BoostSystemMemoryResourceLimit(boost_size);
|
|
|
|
}
|
|
|
|
|
2019-07-03 06:27:53 +02:00
|
|
|
Result BoostApplicationThreadResourceLimit() {
|
|
|
|
return resource::BoostApplicationThreadResourceLimit();
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 00:24:40 +02:00
|
|
|
Result AtmosphereGetCurrentLimitInfo(s64 *out_cur_val, s64 *out_lim_val, u32 group, u32 resource) {
|
|
|
|
return resource::GetResourceLimitValues(out_cur_val, out_lim_val, static_cast<ResourceLimitGroup>(group), static_cast<svc::LimitableResource>(resource));
|
2019-06-29 11:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|