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);
}
/* Free the memory used to track what boot2 launches. */
ClearLaunchedTitles();
/* We no longer need the SD card. */
fsdevUnmountAll();
/* Clear titles. */
/* Free the memory used to track what boot2 launches. */
ClearLaunchedTitles();
}

View file

@ -42,7 +42,7 @@ Result DebugMonitorService::LaunchDebugProcess(u64 pid) {
}
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);
if (proc != nullptr) {
@ -57,7 +57,7 @@ Result DebugMonitorService::EnableDebugForTitleId(Out<CopiedHandle> event, u64 t
}
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;
if (Registration::HasApplicationProcess(&app_proc)) {

View file

@ -19,7 +19,7 @@
#include "pm_info.hpp"
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);
if (proc != NULL) {

View file

@ -44,12 +44,24 @@ class ProcessWaiter final : public IWaitable {
class ProcessList final {
private:
HosRecursiveMutex mutex;
HosRecursiveMutex m;
HosRecursiveMutex *GetMutex() {
return &this->m;
}
public:
std::vector<std::shared_ptr<Registration::Process>> processes;
auto GetUniqueLock() {
return std::unique_lock{this->mutex};
void lock() {
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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
#include <atomic>
#include "pm_registration.hpp"
#include "pm_resource_limits.hpp"
#include "pm_process_wait.hpp"
static ProcessList g_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_application_event = nullptr;
std::unique_lock<HosRecursiveMutex> Registration::GetProcessListUniqueLock() {
return g_process_list.GetUniqueLock();
static u8 g_ac_buf[4 * sizeof(LoaderProgramInfo)];
ProcessList &Registration::GetProcessList() {
return g_process_list;
}
void Registration::InitializeSystemResources() {
@ -70,9 +70,8 @@ void Registration::HandleProcessLaunch() {
u64 *out_pid = g_process_launch_state.out_pid;
Process new_process = {0};
new_process.tid_sid = g_process_launch_state.tid_sid;
auto ac_buf = std::vector<u8>(4 * sizeof(LoaderProgramInfo));
std::fill(ac_buf.begin(), ac_buf.end(), 0xCC);
u8 *acid_sac = ac_buf.data(), *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
std::memset(g_ac_buf, 0xCC, sizeof(g_ac_buf));
u8 *acid_sac = g_ac_buf, *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
/* Check that this is a real program. */
if (R_FAILED((rc = ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info)))) {
@ -185,7 +184,7 @@ HANDLE_PROCESS_LAUNCH_END:
Result Registration::LaunchDebugProcess(u64 pid) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
LoaderProgramInfo program_info = {0};
Result rc;
@ -288,48 +287,56 @@ Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process
}
void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) {
auto auto_lock = GetProcessListUniqueLock();
bool signal_debug_process_5x = kernelAbove500() && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED;
/* Unregister with FS. */
if (R_FAILED(fsprUnregisterProgram(process->pid))) {
std::abort();
}
/* Unregister with SM. */
if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
std::abort();
}
/* Unregister with LDR. */
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
std::abort();
}
/* Close the process's handle. */
svcCloseHandle(process->handle);
process->handle = 0;
/* Insert into dead process list, if relevant. */
if (signal_debug_process_5x) {
auto lk = g_dead_process_list.GetUniqueLock();
g_dead_process_list.processes.push_back(process);
}
/* Remove NOTE: This probably frees process. */
RemoveProcessFromList(process->pid);
bool signal_debug_process_5x;
/* Scope to manage process list lock. */
{
std::scoped_lock<ProcessList &> lk(GetProcessList());
signal_debug_process_5x = kernelAbove500() && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED;
/* Unregister with FS. */
if (R_FAILED(fsprUnregisterProgram(process->pid))) {
std::abort();
}
/* Unregister with SM. */
if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
std::abort();
}
/* Unregister with LDR. */
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
std::abort();
}
/* Close the process's handle. */
svcCloseHandle(process->handle);
process->handle = 0;
/* Insert into dead process list, if relevant. */
if (signal_debug_process_5x) {
std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
g_dead_process_list.processes.push_back(process);
}
/* Remove NOTE: This probably frees process. */
RemoveProcessFromList(process->pid);
}
auto_lock.unlock();
if (signal_debug_process_5x) {
g_process_event->Signal();
}
}
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_launch_start_event->GetManager()->AddWaitable(new ProcessWaiter(process));
}
void Registration::RemoveProcessFromList(u64 pid) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Remove process from list. */
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) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
/* Set process state. */
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) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
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) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
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) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &process : g_process_list.processes) {
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) {
auto auto_lock = GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(GetProcessList());
u32 num = 0;
@ -415,42 +422,49 @@ Handle Registration::GetProcessEventHandle() {
}
void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
auto auto_lock = GetProcessListUniqueLock();
for (auto &p : g_process_list.processes) {
if (kernelAbove200() && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
*out_pid = p->pid;
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED;
return;
}
if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) {
u64 old_flags = p->flags;
p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING;
*out_pid = p->pid;
*out_type = kernelAbove500() ?
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
PROCESSEVENTTYPE_500_SUSPENDED :
PROCESSEVENTTYPE_500_RUNNING) :
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
PROCESSEVENTTYPE_SUSPENDED :
PROCESSEVENTTYPE_RUNNING);
return;
}
if (p->flags & PROCESSFLAGS_CRASHED) {
*out_pid = p->pid;
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH;
return;
}
if (!kernelAbove500() && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) {
*out_pid = p->pid;
*out_type = PROCESSEVENTTYPE_EXIT;
return;
/* Scope to manage process list lock. */
{
std::scoped_lock<ProcessList &> lk(GetProcessList());
for (auto &p : g_process_list.processes) {
if (kernelAbove200() && p->state >= ProcessState_Running && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
*out_pid = p->pid;
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED;
return;
}
if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) {
u64 old_flags = p->flags;
p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING;
*out_pid = p->pid;
*out_type = kernelAbove500() ?
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
PROCESSEVENTTYPE_500_SUSPENDED :
PROCESSEVENTTYPE_500_RUNNING) :
((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ?
PROCESSEVENTTYPE_SUSPENDED :
PROCESSEVENTTYPE_RUNNING);
return;
}
if (p->flags & PROCESSFLAGS_CRASHED) {
*out_pid = p->pid;
*out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH;
return;
}
if (!kernelAbove500() && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) {
*out_pid = p->pid;
*out_type = PROCESSEVENTTYPE_EXIT;
return;
}
}
*out_pid = 0;
*out_type = 0;
}
if (kernelAbove500()) {
auto_lock.unlock();
auto dead_process_list_lock = g_dead_process_list.GetUniqueLock();
std::scoped_lock<ProcessList &> dead_lk(g_dead_process_list);
if (g_dead_process_list.processes.size()) {
std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0];
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;
}
}
*out_pid = 0;
*out_type = 0;
}

View file

@ -20,6 +20,8 @@
#include <memory>
#include <mutex>
class ProcessList;
#define LAUNCHFLAGS_NOTIFYWHENEXITED(flags) (flags & 1)
#define LAUNCHFLAGS_STARTSUSPENDED(flags) (flags & (kernelAbove500() ? 0x10 : 0x2))
#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 IWaitable *GetProcessLaunchStartEvent();
static std::unique_lock<HosRecursiveMutex> GetProcessListUniqueLock();
static ProcessList &GetProcessList();
static Result ProcessLaunchStartCallback(u64 timeout);
static Result HandleSignaledProcess(std::shared_ptr<Process> process);
@ -201,3 +203,4 @@ class Registration {
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) {
auto auto_lock = Registration::GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc != nullptr) {
@ -39,7 +39,7 @@ Result ShellService::TerminateProcessId(u64 pid) {
}
Result ShellService::TerminateTitleId(u64 tid) {
auto auto_lock = Registration::GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcessByTitleId(tid);
if (proc != NULL) {
@ -58,7 +58,7 @@ void ShellService::GetProcessEventType(Out<u64> type, Out<u64> pid) {
}
Result ShellService::FinalizeExitedProcess(u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc == NULL) {
@ -72,7 +72,7 @@ Result ShellService::FinalizeExitedProcess(u64 pid) {
}
Result ShellService::ClearProcessNotificationFlag(u64 pid) {
auto auto_lock = Registration::GetProcessListUniqueLock();
std::scoped_lock<ProcessList &> lk(Registration::GetProcessList());
auto proc = Registration::GetProcess(pid);
if (proc != NULL) {
@ -91,7 +91,7 @@ void ShellService::NotifyBootFinished() {
}
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;
if (Registration::HasApplicationProcess(&app_proc)) {