1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-28 05:06:08 +00:00

pm: use fixed-sized buf + scoped lock (gcc 8.3 compat)

This commit is contained in:
Michael Scire 2019-03-25 17:12:19 -07:00
parent f4950ff26e
commit 3316820f86
7 changed files with 118 additions and 94 deletions

View file

@ -283,13 +283,10 @@ void EmbeddedBoot2::Main() {
} }
closedir(titles_dir); closedir(titles_dir);
} }
/* Free the memory used to track what boot2 launches. */
ClearLaunchedTitles();
/* We no longer need the SD card. */ /* We no longer need the SD card. */
fsdevUnmountAll(); fsdevUnmountAll();
/* Clear titles. */ /* Free the memory used to track what boot2 launches. */
ClearLaunchedTitles(); ClearLaunchedTitles();
} }

View file

@ -42,7 +42,7 @@ Result DebugMonitorService::LaunchDebugProcess(u64 pid) {
} }
Result DebugMonitorService::GetTitleProcessId(Out<u64> pid, u64 tid) { Result DebugMonitorService::GetTitleProcessId(Out<u64> pid, u64 tid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> proc = Registration::GetProcessByTitleId(tid); std::shared_ptr<Registration::Process> proc = Registration::GetProcessByTitleId(tid);
if (proc != nullptr) { if (proc != nullptr) {
@ -57,7 +57,7 @@ Result DebugMonitorService::EnableDebugForTitleId(Out<CopiedHandle> event, u64 t
} }
Result DebugMonitorService::GetApplicationProcessId(Out<u64> pid) { Result DebugMonitorService::GetApplicationProcessId(Out<u64> pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> app_proc; std::shared_ptr<Registration::Process> app_proc;
if (Registration::HasApplicationProcess(&app_proc)) { if (Registration::HasApplicationProcess(&app_proc)) {

View file

@ -19,7 +19,7 @@
#include "pm_info.hpp" #include "pm_info.hpp"
Result InformationService::GetTitleId(Out<u64> tid, u64 pid) { Result InformationService::GetTitleId(Out<u64> tid, u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid); std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
if (proc != NULL) { if (proc != NULL) {

View file

@ -44,12 +44,24 @@ class ProcessWaiter final : public IWaitable {
class ProcessList final { class ProcessList final {
private: private:
HosRecursiveMutex mutex; HosRecursiveMutex m;
HosRecursiveMutex *GetMutex() {
return &this->m;
}
public: public:
std::vector<std::shared_ptr<Registration::Process>> processes; std::vector<std::shared_ptr<Registration::Process>> processes;
auto GetUniqueLock() { void lock() {
return std::unique_lock{this->mutex}; GetMutex()->lock();
}
void unlock() {
GetMutex()->unlock();
}
void try_lock() {
GetMutex()->try_lock();
} }
}; };

View file

@ -13,14 +13,12 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include <atomic> #include <atomic>
#include "pm_registration.hpp" #include "pm_registration.hpp"
#include "pm_resource_limits.hpp" #include "pm_resource_limits.hpp"
#include "pm_process_wait.hpp"
static ProcessList g_process_list; static ProcessList g_process_list;
static ProcessList g_dead_process_list; static ProcessList g_dead_process_list;
@ -38,8 +36,10 @@ static IEvent *g_process_event = nullptr;
static IEvent *g_debug_title_event = nullptr; static IEvent *g_debug_title_event = nullptr;
static IEvent *g_debug_application_event = nullptr; static IEvent *g_debug_application_event = nullptr;
std::unique_lock<HosRecursiveMutex> Registration::GetProcessListUniqueLock() { static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
return g_process_list.GetUniqueLock();
ProcessList &Registration::GetProcessList() {
return g_process_list;
} }
void Registration::InitializeSystemResources() { void Registration::InitializeSystemResources() {
@ -70,9 +70,8 @@ void Registration::HandleProcessLaunch() {
u64 *out_pid = g_process_launch_state.out_pid; u64 *out_pid = g_process_launch_state.out_pid;
Process new_process = {0}; Process new_process = {0};
new_process.tid_sid = g_process_launch_state.tid_sid; new_process.tid_sid = g_process_launch_state.tid_sid;
auto ac_buf = std::vector<u8>(4 * sizeof(LoaderProgramInfo)); std::memset(g_ac_buf, 0xCC, sizeof(g_ac_buf));
std::fill(ac_buf.begin(), ac_buf.end(), 0xCC); u8 *acid_sac = g_ac_buf, *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
u8 *acid_sac = ac_buf.data(), *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
/* Check that this is a real program. */ /* 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)))) { if (R_FAILED((rc = ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info)))) {
@ -185,7 +184,7 @@ HANDLE_PROCESS_LAUNCH_END:
Result Registration::LaunchDebugProcess(u64 pid) { Result Registration::LaunchDebugProcess(u64 pid) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
LoaderProgramInfo program_info = {0}; LoaderProgramInfo program_info = {0};
Result rc; Result rc;
@ -288,48 +287,56 @@ Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process
} }
void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) { void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) {
auto auto_lock = GetProcessListUniqueLock(); bool signal_debug_process_5x;
bool signal_debug_process_5x = kernelAbove500() && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED;
/* Unregister with FS. */ /* Scope to manage process list lock. */
if (R_FAILED(fsprUnregisterProgram(process->pid))) { {
std::abort(); std::scoped_lock<ProcessList &> lk(GetProcessList());
}
/* Unregister with SM. */ signal_debug_process_5x = kernelAbove500() && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED;
if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
std::abort(); /* Unregister with FS. */
} if (R_FAILED(fsprUnregisterProgram(process->pid))) {
/* Unregister with LDR. */ std::abort();
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) { }
std::abort(); /* Unregister with SM. */
} if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
std::abort();
/* Close the process's handle. */ }
svcCloseHandle(process->handle); /* Unregister with LDR. */
process->handle = 0; if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
std::abort();
/* Insert into dead process list, if relevant. */ }
if (signal_debug_process_5x) {
auto lk = g_dead_process_list.GetUniqueLock(); /* Close the process's handle. */
g_dead_process_list.processes.push_back(process); svcCloseHandle(process->handle);
} process->handle = 0;
/* Remove NOTE: This probably frees process. */ /* Insert into dead process list, if relevant. */
RemoveProcessFromList(process->pid); 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);
}
auto_lock.unlock();
if (signal_debug_process_5x) { if (signal_debug_process_5x) {
g_process_event->Signal(); g_process_event->Signal();
} }
} }
void Registration::AddProcessToList(std::shared_ptr<Registration::Process> process) { void Registration::AddProcessToList(std::shared_ptr<Registration::Process> process) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
g_process_list.processes.push_back(process); g_process_list.processes.push_back(process);
g_process_launch_start_event->GetManager()->AddWaitable(new ProcessWaiter(process)); g_process_launch_start_event->GetManager()->AddWaitable(new ProcessWaiter(process));
} }
void Registration::RemoveProcessFromList(u64 pid) { void Registration::RemoveProcessFromList(u64 pid) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Remove process from list. */ /* Remove process from list. */
for (unsigned int i = 0; i < g_process_list.processes.size(); i++) { for (unsigned int i = 0; i < g_process_list.processes.size(); i++) {
@ -344,7 +351,7 @@ void Registration::RemoveProcessFromList(u64 pid) {
} }
void Registration::SetProcessState(u64 pid, ProcessState new_state) { void Registration::SetProcessState(u64 pid, ProcessState new_state) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Set process state. */ /* Set process state. */
for (auto &process : g_process_list.processes) { for (auto &process : g_process_list.processes) {
@ -356,7 +363,7 @@ void Registration::SetProcessState(u64 pid, ProcessState new_state) {
} }
bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process> *out) { bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process> *out) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) { for (auto &process : g_process_list.processes) {
if (process->flags & PROCESSFLAGS_APPLICATION) { if (process->flags & PROCESSFLAGS_APPLICATION) {
@ -371,7 +378,7 @@ bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process>
} }
std::shared_ptr<Registration::Process> Registration::GetProcess(u64 pid) { std::shared_ptr<Registration::Process> Registration::GetProcess(u64 pid) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) { for (auto &process : g_process_list.processes) {
if (process->pid == pid) { if (process->pid == pid) {
@ -383,7 +390,7 @@ std::shared_ptr<Registration::Process> Registration::GetProcess(u64 pid) {
} }
std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid) { std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) { for (auto &process : g_process_list.processes) {
if (process->tid_sid.title_id == tid) { if (process->tid_sid.title_id == tid) {
@ -396,7 +403,7 @@ std::shared_ptr<Registration::Process> Registration::GetProcessByTitleId(u64 tid
Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out) { Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out) {
auto auto_lock = GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(GetProcessList());
u32 num = 0; u32 num = 0;
@ -415,42 +422,49 @@ Handle Registration::GetProcessEventHandle() {
} }
void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) { void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
auto auto_lock = GetProcessListUniqueLock(); /* Scope to manage process list lock. */
{
for (auto &p : g_process_list.processes) { std::scoped_lock<ProcessList &> lk(GetProcessList());
if (kernelAbove200() && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
p->flags &= ~PROCESSFLAGS_DEBUGDETACHED; for (auto &p : g_process_list.processes) {
*out_pid = p->pid; if (kernelAbove200() && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED; p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
return; *out_pid = p->pid;
} *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED;
if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) { return;
u64 old_flags = p->flags; }
p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING; if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) {
*out_pid = p->pid; u64 old_flags = p->flags;
*out_type = kernelAbove500() ? p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING;
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ? *out_pid = p->pid;
PROCESSEVENTTYPE_500_SUSPENDED : *out_type = kernelAbove500() ?
PROCESSEVENTTYPE_500_RUNNING) : ((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ? PROCESSEVENTTYPE_500_SUSPENDED :
PROCESSEVENTTYPE_SUSPENDED : PROCESSEVENTTYPE_500_RUNNING) :
PROCESSEVENTTYPE_RUNNING); ((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
return; PROCESSEVENTTYPE_SUSPENDED :
} PROCESSEVENTTYPE_RUNNING);
if (p->flags & PROCESSFLAGS_CRASHED) { return;
*out_pid = p->pid; }
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH; if (p->flags & PROCESSFLAGS_CRASHED) {
return; *out_pid = p->pid;
} *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH;
if (!kernelAbove500() && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) { return;
*out_pid = p->pid; }
*out_type = PROCESSEVENTTYPE_EXIT; if (!kernelAbove500() && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) {
return; *out_pid = p->pid;
*out_type = PROCESSEVENTTYPE_EXIT;
return;
}
} }
*out_pid = 0;
*out_type = 0;
} }
if (kernelAbove500()) { if (kernelAbove500()) {
auto_lock.unlock(); std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
auto dead_process_list_lock = g_dead_process_list.GetUniqueLock();
if (g_dead_process_list.processes.size()) { if (g_dead_process_list.processes.size()) {
std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0]; std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0];
g_dead_process_list.processes.erase(g_dead_process_list.processes.begin()); g_dead_process_list.processes.erase(g_dead_process_list.processes.begin());
@ -459,8 +473,6 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
return; return;
} }
} }
*out_pid = 0;
*out_type = 0;
} }

View file

@ -20,6 +20,8 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
class ProcessList;
#define LAUNCHFLAGS_NOTIFYWHENEXITED(flags) (flags & 1) #define LAUNCHFLAGS_NOTIFYWHENEXITED(flags) (flags & 1)
#define LAUNCHFLAGS_STARTSUSPENDED(flags) (flags & (kernelAbove500() ? 0x10 : 0x2)) #define LAUNCHFLAGS_STARTSUSPENDED(flags) (flags & (kernelAbove500() ? 0x10 : 0x2))
#define LAUNCHFLAGS_ARGLOW(flags) (kernelAbove500() ? ((flags & 0x14) != 0x10) : (kernelAbove200() ? ((flags & 0x6) != 0x2) : ((flags >> 2) & 1))) #define LAUNCHFLAGS_ARGLOW(flags) (kernelAbove500() ? ((flags & 0x14) != 0x10) : (kernelAbove200() ? ((flags & 0x6) != 0x2) : ((flags >> 2) & 1)))
@ -171,7 +173,7 @@ class Registration {
static void InitializeSystemResources(); static void InitializeSystemResources();
static IWaitable *GetProcessLaunchStartEvent(); static IWaitable *GetProcessLaunchStartEvent();
static std::unique_lock<HosRecursiveMutex> GetProcessListUniqueLock(); static ProcessList &GetProcessList();
static Result ProcessLaunchStartCallback(u64 timeout); static Result ProcessLaunchStartCallback(u64 timeout);
static Result HandleSignaledProcess(std::shared_ptr<Process> process); static Result HandleSignaledProcess(std::shared_ptr<Process> process);
@ -201,3 +203,4 @@ class Registration {
static bool HasApplicationProcess(std::shared_ptr<Process> *out); static bool HasApplicationProcess(std::shared_ptr<Process> *out);
}; };
#include "pm_process_wait.hpp"

View file

@ -28,7 +28,7 @@ Result ShellService::LaunchProcess(Out<u64> pid, Registration::TidSid tid_sid, u
} }
Result ShellService::TerminateProcessId(u64 pid) { Result ShellService::TerminateProcessId(u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid); auto proc = Registration::GetProcess(pid);
if (proc != nullptr) { if (proc != nullptr) {
@ -39,7 +39,7 @@ Result ShellService::TerminateProcessId(u64 pid) {
} }
Result ShellService::TerminateTitleId(u64 tid) { Result ShellService::TerminateTitleId(u64 tid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcessByTitleId(tid); auto proc = Registration::GetProcessByTitleId(tid);
if (proc != NULL) { if (proc != NULL) {
@ -58,7 +58,7 @@ void ShellService::GetProcessEventType(Out<u64> type, Out<u64> pid) {
} }
Result ShellService::FinalizeExitedProcess(u64 pid) { Result ShellService::FinalizeExitedProcess(u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid); auto proc = Registration::GetProcess(pid);
if (proc == NULL) { if (proc == NULL) {
@ -72,7 +72,7 @@ Result ShellService::FinalizeExitedProcess(u64 pid) {
} }
Result ShellService::ClearProcessNotificationFlag(u64 pid) { Result ShellService::ClearProcessNotificationFlag(u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid); auto proc = Registration::GetProcess(pid);
if (proc != NULL) { if (proc != NULL) {
@ -91,7 +91,7 @@ void ShellService::NotifyBootFinished() {
} }
Result ShellService::GetApplicationProcessId(Out<u64> pid) { Result ShellService::GetApplicationProcessId(Out<u64> pid) {
auto auto_lock = Registration::GetProcessListUniqueLock(); std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
std::shared_ptr<Registration::Process> app_proc; std::shared_ptr<Registration::Process> app_proc;
if (Registration::HasApplicationProcess(&app_proc)) { if (Registration::HasApplicationProcess(&app_proc)) {