mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-10 07:06:34 +00:00
PM: Refactor for R_TRY, remove gotos
This commit is contained in:
parent
c60ee15449
commit
ee40dcd76f
15 changed files with 126 additions and 165 deletions
|
@ -1 +1 @@
|
|||
Subproject commit b5f1ec02b30a3bbf8a8419f8b0cf3646071ebd2b
|
||||
Subproject commit 777984476ea7b1da56c9a3fd3f55575a8b899896
|
|
@ -87,22 +87,14 @@ Result DebugMonitorService::AtmosphereGetProcessInfo(Out<CopiedHandle> proc_hand
|
|||
}
|
||||
|
||||
Result DebugMonitorService::AtmosphereGetCurrentLimitInfo(Out<u64> cur_val, Out<u64> lim_val, u32 category, u32 resource) {
|
||||
Result rc;
|
||||
if(category > ResourceLimitUtils::ResourceLimitCategory::ResourceLimitCategory_Applet) {
|
||||
return ResultKernelInvalidEnumValue;
|
||||
}
|
||||
|
||||
Handle limit_h = ResourceLimitUtils::GetResourceLimitHandleByCategory((ResourceLimitUtils::ResourceLimitCategory) category);
|
||||
|
||||
rc = svcGetResourceLimitCurrentValue(cur_val.GetPointer(), limit_h, (LimitableResource) resource);
|
||||
if(R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = svcGetResourceLimitLimitValue(lim_val.GetPointer(), limit_h, (LimitableResource) resource);
|
||||
if(R_FAILED(rc)) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(svcGetResourceLimitCurrentValue(cur_val.GetPointer(), limit_h, (LimitableResource) resource));
|
||||
R_TRY(svcGetResourceLimitLimitValue(lim_val.GetPointer(), limit_h, (LimitableResource) resource));
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ static ProcessList g_process_list;
|
|||
static ProcessList g_dead_process_list;
|
||||
|
||||
static IEvent *g_process_launch_start_event = nullptr;
|
||||
static HosSemaphore g_sema_finish_launch;
|
||||
static HosSignal g_finish_launch_signal;
|
||||
|
||||
static HosMutex g_process_launch_mutex;
|
||||
static Registration::ProcessLaunchState g_process_launch_state;
|
||||
|
@ -37,8 +37,6 @@ static IEvent *g_debug_title_event = nullptr;
|
|||
static IEvent *g_debug_application_event = nullptr;
|
||||
static IEvent *g_boot_finished_event = nullptr;
|
||||
|
||||
static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
|
||||
|
||||
ProcessList &Registration::GetProcessList() {
|
||||
return g_process_list;
|
||||
}
|
||||
|
@ -65,31 +63,29 @@ IWaitable *Registration::GetProcessLaunchStartEvent() {
|
|||
return g_process_launch_start_event;
|
||||
}
|
||||
|
||||
void Registration::HandleProcessLaunch() {
|
||||
Result Registration::LaunchProcess(u64 *out_pid, const TidSid tid_sid, const u64 launch_flags) {
|
||||
LoaderProgramInfo program_info = {0};
|
||||
Result rc;
|
||||
u64 launch_flags = g_process_launch_state.launch_flags;
|
||||
u64 *out_pid = g_process_launch_state.out_pid;
|
||||
Process new_process = {0};
|
||||
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);
|
||||
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);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
R_TRY(ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info));
|
||||
|
||||
/* 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;
|
||||
if ((program_info.application_type & 3) == 1 && HasApplicationProcess()) {
|
||||
return ResultPmApplicationRunning;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
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);
|
||||
};
|
||||
|
||||
/* Make sure the previous application is cleaned up. */
|
||||
if ((program_info.application_type & 3) == 1) {
|
||||
|
@ -97,26 +93,32 @@ void Registration::HandleProcessLaunch() {
|
|||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
/* Get the new process's id. */
|
||||
svcGetProcessId(&new_process.pid, new_process.handle);
|
||||
if (R_FAILED(svcGetProcessId(&new_process.pid, new_process.handle))) {
|
||||
std::abort();
|
||||
}
|
||||
|
||||
/* Register with FS. */
|
||||
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;
|
||||
}
|
||||
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);
|
||||
};
|
||||
|
||||
/* 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;
|
||||
}
|
||||
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);
|
||||
};
|
||||
|
||||
/* Setup process flags. */
|
||||
if (program_info.application_type & 1) {
|
||||
|
@ -134,61 +136,46 @@ void Registration::HandleProcessLaunch() {
|
|||
|
||||
/* Add process to the list. */
|
||||
Registration::AddProcessToList(std::make_shared<Registration::Process>(new_process));
|
||||
auto reg_list_guard = SCOPE_GUARD {
|
||||
Registration::RemoveProcessFromList(new_process.pid);
|
||||
};
|
||||
|
||||
/* Signal, if relevant. */
|
||||
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
|
||||
g_debug_title_event->Signal();
|
||||
g_debug_on_launch_tid = 0;
|
||||
rc = ResultSuccess;
|
||||
} else if ((new_process.flags & PROCESSFLAGS_APPLICATION) && g_debug_next_application.load()) {
|
||||
g_debug_application_event->Signal();
|
||||
g_debug_next_application = false;
|
||||
rc = ResultSuccess;
|
||||
} else if (LAUNCHFLAGS_STARTSUSPENDED(launch_flags)) {
|
||||
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);
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
||||
if (R_FAILED(rc)) {
|
||||
Registration::RemoveProcessFromList(new_process.pid);
|
||||
smManagerUnregisterProcess(new_process.pid);
|
||||
}
|
||||
/* Dismiss scope guards. */
|
||||
reg_list_guard.Cancel();
|
||||
sm_register_guard.Cancel();
|
||||
fs_register_guard.Cancel();
|
||||
proc_create_guard.Cancel();
|
||||
ldr_register_guard.Cancel();
|
||||
|
||||
SM_REGISTRATION_FAILED:
|
||||
if (R_FAILED(rc)) {
|
||||
fsprUnregisterProgram(new_process.pid);
|
||||
}
|
||||
/* Set output pid. */
|
||||
*out_pid = new_process.pid;
|
||||
|
||||
FS_REGISTRATION_FAILED:
|
||||
if (R_FAILED(rc)) {
|
||||
svcCloseHandle(new_process.handle);
|
||||
new_process.handle = 0;
|
||||
}
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
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();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
|
@ -200,15 +187,13 @@ Result Registration::LaunchDebugProcess(u64 pid) {
|
|||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
R_TRY(ldrPmGetProgramInfo(proc->tid_sid.title_id, proc->tid_sid.storage_id, &program_info));
|
||||
|
||||
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;
|
||||
}
|
||||
/* 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));
|
||||
|
||||
return rc;
|
||||
proc->state = ProcessState_Running;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) {
|
||||
|
@ -220,8 +205,9 @@ Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 lau
|
|||
g_process_launch_state.out_pid = out_pid;
|
||||
|
||||
/* Start a launch, and wait for it to exit. */
|
||||
g_finish_launch_signal.Reset();
|
||||
g_process_launch_start_event->Signal();
|
||||
g_sema_finish_launch.Wait();
|
||||
g_finish_launch_signal.Wait();
|
||||
|
||||
return g_process_launch_state.result;
|
||||
}
|
||||
|
|
|
@ -170,7 +170,10 @@ class Registration {
|
|||
u64* out_pid;
|
||||
Result result;
|
||||
};
|
||||
|
||||
private:
|
||||
static Result LaunchProcess(u64 *out_pid, const TidSid tid_sid, const u64 launch_flags);
|
||||
public:
|
||||
/* TODO: Better evaluate public vs private API. */
|
||||
static void InitializeSystemResources();
|
||||
static IWaitable *GetProcessLaunchStartEvent();
|
||||
static ProcessList &GetProcessList();
|
||||
|
@ -203,7 +206,7 @@ class Registration {
|
|||
|
||||
static void SignalBootFinished();
|
||||
|
||||
static bool HasApplicationProcess(std::shared_ptr<Process> *out);
|
||||
static bool HasApplicationProcess(std::shared_ptr<Process> *out = nullptr);
|
||||
};
|
||||
|
||||
#include "pm_process_wait.hpp"
|
||||
|
|
|
@ -81,27 +81,24 @@ static u64 g_system_boost_size = 0;
|
|||
|
||||
/* Tries to set Resource limits for a category. */
|
||||
static Result SetResourceLimits(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) {
|
||||
Result rc = ResultSuccess;
|
||||
u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory];
|
||||
g_resource_limits[category][LimitableResource_Memory] = new_memory_size;
|
||||
for (unsigned int r = 0; r < 5; r++) {
|
||||
if (R_FAILED((rc = svcSetResourceLimitLimitValue(g_resource_limit_handles[category], (LimitableResource)r, g_resource_limits[category][r])))) {
|
||||
R_TRY_CLEANUP(svcSetResourceLimitLimitValue(g_resource_limit_handles[category], (LimitableResource)r, g_resource_limits[category][r]), {
|
||||
g_resource_limits[category][LimitableResource_Memory] = old_memory_size;
|
||||
return rc;
|
||||
}
|
||||
});
|
||||
}
|
||||
return rc;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
|
||||
static Result SetNewMemoryResourceLimit(ResourceLimitUtils::ResourceLimitCategory category, u64 new_memory_size) {
|
||||
Result rc = ResultSuccess;
|
||||
u64 old_memory_size = g_resource_limits[category][LimitableResource_Memory];
|
||||
g_resource_limits[category][LimitableResource_Memory] = new_memory_size;
|
||||
if (R_FAILED((rc = svcSetResourceLimitLimitValue(g_resource_limit_handles[category], LimitableResource_Memory, g_resource_limits[category][LimitableResource_Memory])))) {
|
||||
R_TRY_CLEANUP(svcSetResourceLimitLimitValue(g_resource_limit_handles[category], LimitableResource_Memory, g_resource_limits[category][LimitableResource_Memory]), {
|
||||
g_resource_limits[category][LimitableResource_Memory] = old_memory_size;
|
||||
}
|
||||
return rc;
|
||||
});
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
void ResourceLimitUtils::InitializeLimits() {
|
||||
|
@ -249,49 +246,32 @@ Handle ResourceLimitUtils::GetResourceLimitHandleByCategory(ResourceLimitCategor
|
|||
}
|
||||
|
||||
Result ResourceLimitUtils::BoostSystemMemoryResourceLimit(u64 boost_size) {
|
||||
Result rc = ResultSuccess;
|
||||
if (boost_size > g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application]) {
|
||||
return ResultPmInvalidSize;
|
||||
}
|
||||
|
||||
u64 app_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_Application] - boost_size;
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) {
|
||||
if (boost_size < g_system_boost_size) {
|
||||
if (R_FAILED((rc = svcSetUnsafeLimit(boost_size)))) {
|
||||
return rc;
|
||||
}
|
||||
if (R_FAILED((rc = SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size)))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(svcSetUnsafeLimit(boost_size));
|
||||
R_TRY(SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size));
|
||||
} else {
|
||||
if (R_FAILED((rc = SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size)))) {
|
||||
return rc;
|
||||
}
|
||||
if (R_FAILED((rc = svcSetUnsafeLimit(boost_size)))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(SetNewMemoryResourceLimit(ResourceLimitCategory_Application, app_size));
|
||||
R_TRY(svcSetUnsafeLimit(boost_size));
|
||||
}
|
||||
} else if (GetRuntimeFirmwareVersion() >= FirmwareVersion_400) {
|
||||
u64 sys_size = g_memory_resource_limits[g_memory_limit_type][ResourceLimitCategory_System] + boost_size;
|
||||
if (boost_size < g_system_boost_size) {
|
||||
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_System, sys_size)))) {
|
||||
return rc;
|
||||
}
|
||||
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_Application, app_size)))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(SetResourceLimits(ResourceLimitCategory_System, sys_size));
|
||||
R_TRY(SetResourceLimits(ResourceLimitCategory_Application, app_size));
|
||||
} else {
|
||||
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_Application, app_size)))) {
|
||||
return rc;
|
||||
}
|
||||
if (R_FAILED((rc = SetResourceLimits(ResourceLimitCategory_System, sys_size)))) {
|
||||
return rc;
|
||||
}
|
||||
R_TRY(SetResourceLimits(ResourceLimitCategory_Application, app_size));
|
||||
R_TRY(SetResourceLimits(ResourceLimitCategory_System, sys_size));
|
||||
}
|
||||
} else {
|
||||
rc = ResultKernelConnectionClosed;
|
||||
return ResultKernelConnectionClosed;
|
||||
}
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
g_system_boost_size = boost_size;
|
||||
}
|
||||
return rc;
|
||||
|
||||
g_system_boost_size = boost_size;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue