mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-12-22 20:31:14 +00:00
ProcessManager: Implement core process management logic.
This commit is contained in:
parent
999498c0a0
commit
d6cf7c605f
12 changed files with 688 additions and 99 deletions
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include "stratosphere/ievent.hpp"
|
#include "stratosphere/ievent.hpp"
|
||||||
#include "stratosphere/systemevent.hpp"
|
#include "stratosphere/systemevent.hpp"
|
||||||
|
#include "stratosphere/hossynch.hpp"
|
||||||
|
|
||||||
#include "stratosphere/waitablemanager.hpp"
|
#include "stratosphere/waitablemanager.hpp"
|
||||||
|
|
||||||
|
|
120
stratosphere/libstratosphere/include/stratosphere/hossynch.hpp
Normal file
120
stratosphere/libstratosphere/include/stratosphere/hossynch.hpp
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
#pragma once
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
|
class HosMutex {
|
||||||
|
private:
|
||||||
|
Mutex m;
|
||||||
|
public:
|
||||||
|
HosMutex() {
|
||||||
|
mutexInit(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lock() {
|
||||||
|
mutexLock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unlock() {
|
||||||
|
mutexUnlock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryLock() {
|
||||||
|
return mutexTryLock(&this->m);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class HosRecursiveMutex {
|
||||||
|
private:
|
||||||
|
RMutex m;
|
||||||
|
public:
|
||||||
|
HosRecursiveMutex() {
|
||||||
|
rmutexInit(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lock() {
|
||||||
|
rmutexLock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unlock() {
|
||||||
|
rmutexUnlock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryLock() {
|
||||||
|
return rmutexTryLock(&this->m);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class HosCondVar {
|
||||||
|
private:
|
||||||
|
CondVar cv;
|
||||||
|
Mutex m;
|
||||||
|
public:
|
||||||
|
HosCondVar() {
|
||||||
|
mutexInit(&m);
|
||||||
|
condvarInit(&cv, &m);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result WaitTimeout(u64 timeout) {
|
||||||
|
return condvarWaitTimeout(&cv, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Wait() {
|
||||||
|
return condvarWait(&cv);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Wake(int num) {
|
||||||
|
return condvarWake(&cv, num);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result WakeOne() {
|
||||||
|
return condvarWakeOne(&cv);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result WakeAll() {
|
||||||
|
return condvarWakeAll(&cv);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class HosSemaphore {
|
||||||
|
private:
|
||||||
|
CondVar cv;
|
||||||
|
Mutex m;
|
||||||
|
u64 count;
|
||||||
|
public:
|
||||||
|
HosSemaphore() {
|
||||||
|
count = 0;
|
||||||
|
mutexInit(&m);
|
||||||
|
condvarInit(&cv, &m);
|
||||||
|
}
|
||||||
|
|
||||||
|
HosSemaphore(u64 c) : count(c) {
|
||||||
|
mutexInit(&m);
|
||||||
|
condvarInit(&cv, &m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Signal() {
|
||||||
|
mutexLock(&this->m);
|
||||||
|
count++;
|
||||||
|
condvarWakeOne(&cv);
|
||||||
|
mutexUnlock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Wait() {
|
||||||
|
mutexLock(&this->m);
|
||||||
|
while (!count) {
|
||||||
|
condvarWait(&cv);
|
||||||
|
}
|
||||||
|
count--;
|
||||||
|
mutexUnlock(&this->m);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryWait() {
|
||||||
|
mutexLock(&this->m);
|
||||||
|
bool success = false;
|
||||||
|
if (count) {
|
||||||
|
count--;
|
||||||
|
success = true;
|
||||||
|
}
|
||||||
|
mutexUnlock(&this->m);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
};
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
typedef Result (*EventCallback)(Handle *handles, size_t num_handles, u64 timeout);
|
typedef Result (*EventCallback)(Handle *handles, size_t num_handles, u64 timeout);
|
||||||
|
|
||||||
class IEvent : IWaitable {
|
class IEvent : public IWaitable {
|
||||||
protected:
|
protected:
|
||||||
std::vector<Handle> handles;
|
std::vector<Handle> handles;
|
||||||
EventCallback callback;
|
EventCallback callback;
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#define SYSTEMEVENT_INDEX_WAITHANDLE 0
|
#define SYSTEMEVENT_INDEX_WAITHANDLE 0
|
||||||
#define SYSTEMEVENT_INDEX_SGNLHANDLE 1
|
#define SYSTEMEVENT_INDEX_SGNLHANDLE 1
|
||||||
|
|
||||||
class SystemEvent : IEvent {
|
class SystemEvent : public IEvent {
|
||||||
public:
|
public:
|
||||||
SystemEvent(EventCallback callback) : IEvent(0, callback) {
|
SystemEvent(EventCallback callback) : IEvent(0, callback) {
|
||||||
Handle wait_h;
|
Handle wait_h;
|
||||||
|
|
|
@ -6,9 +6,9 @@
|
||||||
|
|
||||||
class WaitableManager {
|
class WaitableManager {
|
||||||
std::vector<IWaitable *> waitables;
|
std::vector<IWaitable *> waitables;
|
||||||
|
|
||||||
u64 timeout;
|
u64 timeout;
|
||||||
|
private:
|
||||||
|
void process_internal(bool break_on_timeout);
|
||||||
public:
|
public:
|
||||||
WaitableManager(u64 t) : waitables(0), timeout(t) { }
|
WaitableManager(u64 t) : waitables(0), timeout(t) { }
|
||||||
~WaitableManager() {
|
~WaitableManager() {
|
||||||
|
@ -22,4 +22,5 @@ class WaitableManager {
|
||||||
unsigned int get_num_signalable();
|
unsigned int get_num_signalable();
|
||||||
void add_waitable(IWaitable *waitable);
|
void add_waitable(IWaitable *waitable);
|
||||||
void process();
|
void process();
|
||||||
|
void process_until_timeout();
|
||||||
};
|
};
|
|
@ -17,7 +17,7 @@ void WaitableManager::add_waitable(IWaitable *waitable) {
|
||||||
this->waitables.push_back(waitable);
|
this->waitables.push_back(waitable);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaitableManager::process() {
|
void WaitableManager::process_internal(bool break_on_timeout) {
|
||||||
std::vector<IWaitable *> signalables;
|
std::vector<IWaitable *> signalables;
|
||||||
std::vector<Handle> handles;
|
std::vector<Handle> handles;
|
||||||
|
|
||||||
|
@ -56,6 +56,9 @@ void WaitableManager::process() {
|
||||||
for (auto & waitable : signalables) {
|
for (auto & waitable : signalables) {
|
||||||
waitable->update_priority();
|
waitable->update_priority();
|
||||||
}
|
}
|
||||||
|
if (break_on_timeout) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else if (rc != 0xF601) {
|
} else if (rc != 0xF601) {
|
||||||
/* TODO: Panic. When can this happen? */
|
/* TODO: Panic. When can this happen? */
|
||||||
}
|
}
|
||||||
|
@ -91,4 +94,12 @@ void WaitableManager::process() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WaitableManager::process() {
|
||||||
|
WaitableManager::process_internal(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WaitableManager::process_until_timeout() {
|
||||||
|
WaitableManager::process_internal(true);
|
||||||
}
|
}
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "pm_boot_mode.hpp"
|
#include "pm_boot_mode.hpp"
|
||||||
#include "pm_process_track.hpp"
|
#include "pm_process_track.hpp"
|
||||||
|
#include "pm_registration.hpp"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
extern u32 __start__;
|
extern u32 __start__;
|
||||||
|
@ -59,22 +60,29 @@ void __appInit(void) {
|
||||||
fatalSimple(0xCAFE << 4 | 2);
|
fatalSimple(0xCAFE << 4 | 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = splInitialize();
|
|
||||||
if (R_FAILED(rc)) {
|
|
||||||
fatalSimple(0xCAFE << 4 | 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ldrPmInitialize();
|
rc = ldrPmInitialize();
|
||||||
if (R_FAILED(rc)) {
|
if (R_FAILED(rc)) {
|
||||||
fatalSimple(0xCAFE << 4 | 4);
|
fatalSimple(0xCAFE << 4 | 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rc = smManagerInitialize();
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
fatalSimple(0xCAFE << 4 | 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
rc = splInitialize();
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
fatalSimple(0xCAFE << 4 | 5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void __appExit(void) {
|
void __appExit(void) {
|
||||||
/* Cleanup services. */
|
/* Cleanup services. */
|
||||||
fsdevUnmountAll();
|
fsdevUnmountAll();
|
||||||
ldrPmExit();
|
|
||||||
splExit();
|
splExit();
|
||||||
|
smManagerExit();
|
||||||
|
ldrPmExit();
|
||||||
fsprExit();
|
fsprExit();
|
||||||
lrExit();
|
lrExit();
|
||||||
fsExit();
|
fsExit();
|
||||||
|
@ -87,7 +95,7 @@ int main(int argc, char **argv)
|
||||||
consoleDebugInit(debugDevice_SVC);
|
consoleDebugInit(debugDevice_SVC);
|
||||||
|
|
||||||
/* Initialize and spawn the Process Tracking thread. */
|
/* Initialize and spawn the Process Tracking thread. */
|
||||||
ProcessTracking::Initialize();
|
Registration::InitializeSystemResources();
|
||||||
if (R_FAILED(threadCreate(&process_track_thread, &ProcessTracking::MainLoop, NULL, 0x4000, 0x15, 0))) {
|
if (R_FAILED(threadCreate(&process_track_thread, &ProcessTracking::MainLoop, NULL, 0x4000, 0x15, 0))) {
|
||||||
/* TODO: Panic. */
|
/* TODO: Panic. */
|
||||||
}
|
}
|
||||||
|
@ -95,7 +103,6 @@ int main(int argc, char **argv)
|
||||||
/* TODO: Panic. */
|
/* TODO: Panic. */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TODO: What's a good timeout value to use here? */
|
/* TODO: What's a good timeout value to use here? */
|
||||||
WaitableManager *server_manager = new WaitableManager(U64_MAX);
|
WaitableManager *server_manager = new WaitableManager(U64_MAX);
|
||||||
|
|
||||||
|
|
|
@ -1,92 +1,17 @@
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
#include <stratosphere.hpp>
|
#include <stratosphere.hpp>
|
||||||
#include "pm_process_track.hpp"
|
#include "pm_process_track.hpp"
|
||||||
|
#include "pm_registration.hpp"
|
||||||
static SystemEvent *g_process_event = NULL;
|
|
||||||
static SystemEvent *g_debug_title_event = NULL;
|
|
||||||
static SystemEvent *g_debug_application_event = NULL;
|
|
||||||
|
|
||||||
static const u64 g_memory_resource_limits[5][3] = {
|
|
||||||
{0x010D00000ULL, 0x0CD500000ULL, 0x021700000ULL},
|
|
||||||
{0x01E100000ULL, 0x080000000ULL, 0x061800000ULL},
|
|
||||||
{0x014800000ULL, 0x0CD500000ULL, 0x01DC00000ULL},
|
|
||||||
{0x028D00000ULL, 0x133400000ULL, 0x023800000ULL},
|
|
||||||
{0x028D00000ULL, 0x0CD500000ULL, 0x089700000ULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
/* These are the limits for LimitableResources. */
|
|
||||||
/* Memory, Threads, Events, TransferMemories, Sessions. */
|
|
||||||
static u64 g_resource_limits[3][5] = {
|
|
||||||
{0x0, 0x1FC, 0x258, 0x80, 0x31A},
|
|
||||||
{0x0, 0x60, 0x0, 0x20, 0x1},
|
|
||||||
{0x0, 0x60, 0x0, 0x20, 0x5},
|
|
||||||
};
|
|
||||||
|
|
||||||
static Handle g_resource_limit_handles[5] = {0};
|
|
||||||
|
|
||||||
void ProcessTracking::Initialize() {
|
|
||||||
/* TODO: Setup ResourceLimit values, create MainLoop thread. */
|
|
||||||
g_process_event = new SystemEvent(&IEvent::PanicCallback);
|
|
||||||
g_debug_title_event = new SystemEvent(&IEvent::PanicCallback);
|
|
||||||
g_debug_application_event = new SystemEvent(&IEvent::PanicCallback);
|
|
||||||
|
|
||||||
/* Get memory limits. */
|
|
||||||
u64 memory_arrangement;
|
|
||||||
if (R_FAILED(splGetConfig(SplConfigItem_MemoryArrange, &memory_arrangement))) {
|
|
||||||
/* TODO: panic. */
|
|
||||||
}
|
|
||||||
memory_arrangement &= 0x3F;
|
|
||||||
int memory_limit_type;
|
|
||||||
switch (memory_arrangement) {
|
|
||||||
case 2:
|
|
||||||
memory_limit_type = 1;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
memory_limit_type = 2;
|
|
||||||
break;
|
|
||||||
case 17:
|
|
||||||
memory_limit_type = 3;
|
|
||||||
break;
|
|
||||||
case 18:
|
|
||||||
memory_limit_type = 4;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
memory_limit_type = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
for (unsigned int i = 0; i < 3; i++) {
|
|
||||||
g_resource_limits[i][0] = g_memory_resource_limits[memory_limit_type][i];
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create resource limits. */
|
|
||||||
for (unsigned int i = 0; i < 3; i++) {
|
|
||||||
if (i > 0) {
|
|
||||||
if (R_FAILED(svcCreateResourceLimit(&g_resource_limit_handles[i]))) {
|
|
||||||
/* TODO: Panic. */
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
u64 out = 0;
|
|
||||||
if (R_FAILED(svcGetInfo(&out, 9, 0, 0))) {
|
|
||||||
/* TODO: Panic. */
|
|
||||||
}
|
|
||||||
g_resource_limit_handles[i] = (Handle)out;
|
|
||||||
}
|
|
||||||
for (unsigned int r = 0; r < 5; r++) {
|
|
||||||
if (R_FAILED(svcSetResourceLimitLimitValue(g_resource_limit_handles[i], r, g_resource_limits[i][r]))) {
|
|
||||||
/* TODO: Panic. */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProcessTracking::MainLoop(void *arg) {
|
void ProcessTracking::MainLoop(void *arg) {
|
||||||
/* TODO */
|
/* Make a new waitable manager. */
|
||||||
while (true) {
|
WaitableManager *process_waiter = new WaitableManager(U64_MAX);
|
||||||
/* PM, as a sysmodule, is basically just a while loop. */
|
process_waiter->add_waitable(Registration::GetProcessLaunchStartEvent());
|
||||||
|
process_waiter->add_waitable(Registration::GetProcessList());
|
||||||
|
|
||||||
/* TODO: Properly implement this. */
|
/* Service processes. */
|
||||||
svcSleepThread(100000ULL);
|
process_waiter->process();
|
||||||
|
|
||||||
/* This is that while loop. */
|
delete process_waiter;
|
||||||
}
|
svcExitThread();
|
||||||
}
|
}
|
|
@ -3,6 +3,5 @@
|
||||||
|
|
||||||
class ProcessTracking {
|
class ProcessTracking {
|
||||||
public:
|
public:
|
||||||
static void Initialize();
|
|
||||||
static void MainLoop(void *arg);
|
static void MainLoop(void *arg);
|
||||||
};
|
};
|
97
stratosphere/pm/source/pm_process_wait.hpp
Normal file
97
stratosphere/pm/source/pm_process_wait.hpp
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
#pragma once
|
||||||
|
#include <switch.h>
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
class ProcessWaiter : public IWaitable {
|
||||||
|
public:
|
||||||
|
Registration::Process process;
|
||||||
|
|
||||||
|
ProcessWaiter(Registration::Process p) : process(p) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessWaiter(Registration::Process *p) {
|
||||||
|
this->process = *p;
|
||||||
|
}
|
||||||
|
|
||||||
|
Registration::Process *get_process() {
|
||||||
|
return &this->process;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* IWaitable */
|
||||||
|
virtual unsigned int get_num_waitables() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void get_waitables(IWaitable **dst) {
|
||||||
|
dst[0] = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void delete_child(IWaitable *child) {
|
||||||
|
/* TODO: Panic, because we can never have any children. */
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Handle get_handle() {
|
||||||
|
return this->process.handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void handle_deferred() {
|
||||||
|
/* TODO: Panic, because we can never be deferred. */
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Result handle_signaled(u64 timeout) {
|
||||||
|
Registration::HandleSignaledProcess(this->get_process());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ProcessList : public IWaitable {
|
||||||
|
private:
|
||||||
|
HosRecursiveMutex mutex;
|
||||||
|
public:
|
||||||
|
std::vector<ProcessWaiter *> process_waiters;
|
||||||
|
|
||||||
|
void Lock() {
|
||||||
|
this->mutex.Lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unlock() {
|
||||||
|
this->mutex.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryLock() {
|
||||||
|
return this->mutex.TryLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* IWaitable */
|
||||||
|
virtual unsigned int get_num_waitables() {
|
||||||
|
return process_waiters.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void get_waitables(IWaitable **dst) {
|
||||||
|
Lock();
|
||||||
|
for (unsigned int i = 0; i < process_waiters.size(); i++) {
|
||||||
|
dst[i] = process_waiters[i];
|
||||||
|
}
|
||||||
|
Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void delete_child(IWaitable *child) {
|
||||||
|
/* TODO: Panic, because we should never be asked to delete a child. */
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Handle get_handle() {
|
||||||
|
/* TODO: Panic, because we don't have a handle. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void handle_deferred() {
|
||||||
|
/* TODO: Panic, because we can never be deferred. */
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual Result handle_signaled(u64 timeout) {
|
||||||
|
/* TODO: Panic, because we can never be signaled. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
381
stratosphere/pm/source/pm_registration.cpp
Normal file
381
stratosphere/pm/source/pm_registration.cpp
Normal file
|
@ -0,0 +1,381 @@
|
||||||
|
#include <switch.h>
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
#include "pm_registration.hpp"
|
||||||
|
#include "pm_process_wait.hpp"
|
||||||
|
|
||||||
|
static ProcessList g_process_list;
|
||||||
|
|
||||||
|
static HosSemaphore g_sema_finish_launch;
|
||||||
|
|
||||||
|
static HosMutex g_process_launch_mutex;
|
||||||
|
static Registration::ProcessLaunchState g_process_launch_state;
|
||||||
|
|
||||||
|
static std::atomic_bool g_debug_next_application(false);
|
||||||
|
static std::atomic<u64> g_debug_on_launch_tid(0);
|
||||||
|
|
||||||
|
static SystemEvent *g_process_event = NULL;
|
||||||
|
static SystemEvent *g_debug_title_event = NULL;
|
||||||
|
static SystemEvent *g_debug_application_event = NULL;
|
||||||
|
static SystemEvent *g_process_launch_start_event = NULL;
|
||||||
|
|
||||||
|
static const u64 g_memory_resource_limits[5][3] = {
|
||||||
|
{0x010D00000ULL, 0x0CD500000ULL, 0x021700000ULL},
|
||||||
|
{0x01E100000ULL, 0x080000000ULL, 0x061800000ULL},
|
||||||
|
{0x014800000ULL, 0x0CD500000ULL, 0x01DC00000ULL},
|
||||||
|
{0x028D00000ULL, 0x133400000ULL, 0x023800000ULL},
|
||||||
|
{0x028D00000ULL, 0x0CD500000ULL, 0x089700000ULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* These are the limits for LimitableResources. */
|
||||||
|
/* Memory, Threads, Events, TransferMemories, Sessions. */
|
||||||
|
static u64 g_resource_limits[3][5] = {
|
||||||
|
{0x0, 0x1FC, 0x258, 0x80, 0x31A},
|
||||||
|
{0x0, 0x60, 0x0, 0x20, 0x1},
|
||||||
|
{0x0, 0x60, 0x0, 0x20, 0x5},
|
||||||
|
};
|
||||||
|
|
||||||
|
static Handle g_resource_limit_handles[3] = {0};
|
||||||
|
|
||||||
|
void Registration::InitializeSystemResources() {
|
||||||
|
g_process_event = new SystemEvent(&IEvent::PanicCallback);
|
||||||
|
g_debug_title_event = new SystemEvent(&IEvent::PanicCallback);
|
||||||
|
g_debug_application_event = new SystemEvent(&IEvent::PanicCallback);
|
||||||
|
g_process_launch_start_event = new SystemEvent(&Registration::ProcessLaunchStartCallback);
|
||||||
|
|
||||||
|
/* Get memory limits. */
|
||||||
|
u64 memory_arrangement;
|
||||||
|
if (R_FAILED(splGetConfig(SplConfigItem_MemoryArrange, &memory_arrangement))) {
|
||||||
|
/* TODO: panic. */
|
||||||
|
}
|
||||||
|
memory_arrangement &= 0x3F;
|
||||||
|
int memory_limit_type;
|
||||||
|
switch (memory_arrangement) {
|
||||||
|
case 2:
|
||||||
|
memory_limit_type = 1;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
memory_limit_type = 2;
|
||||||
|
break;
|
||||||
|
case 17:
|
||||||
|
memory_limit_type = 3;
|
||||||
|
break;
|
||||||
|
case 18:
|
||||||
|
memory_limit_type = 4;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
memory_limit_type = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < 3; i++) {
|
||||||
|
g_resource_limits[i][0] = g_memory_resource_limits[memory_limit_type][i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create resource limits. */
|
||||||
|
for (unsigned int i = 0; i < 3; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
if (R_FAILED(svcCreateResourceLimit(&g_resource_limit_handles[i]))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
u64 out = 0;
|
||||||
|
if (R_FAILED(svcGetInfo(&out, 9, 0, 0))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
g_resource_limit_handles[i] = (Handle)out;
|
||||||
|
}
|
||||||
|
for (unsigned int r = 0; r < 5; r++) {
|
||||||
|
if (R_FAILED(svcSetResourceLimitLimitValue(g_resource_limit_handles[i], (LimitableResource)r, g_resource_limits[i][r]))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Registration::ProcessLaunchStartCallback(Handle *handles, size_t num_handles, u64 timeout) {
|
||||||
|
Registration::HandleProcessLaunch();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWaitable *Registration::GetProcessLaunchStartEvent() {
|
||||||
|
return g_process_launch_start_event;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWaitable *Registration::GetProcessList() {
|
||||||
|
return &g_process_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::HandleProcessLaunch() {
|
||||||
|
LoaderProgramInfo program_info = {0};
|
||||||
|
Result rc;
|
||||||
|
u64 launch_flags = g_process_launch_state.launch_flags;
|
||||||
|
const u8 *acid_fac, *aci0_fah, *acid_sac, *aci0_sac;
|
||||||
|
u64 *out_pid = g_process_launch_state.out_pid;
|
||||||
|
u32 reslimit_idx;
|
||||||
|
Process new_process = {0};
|
||||||
|
new_process.tid_sid = g_process_launch_state.tid_sid;
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the resource limit handle, ensure that we can launch the program. */
|
||||||
|
if ((program_info.application_type & 3) == 1) {
|
||||||
|
if (HasApplicationProcess()) {
|
||||||
|
rc = 0xA0F;
|
||||||
|
goto HANDLE_PROCESS_LAUNCH_END;
|
||||||
|
}
|
||||||
|
reslimit_idx = 1;
|
||||||
|
} else {
|
||||||
|
reslimit_idx = 2 * ((program_info.application_type & 3) == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Make sure the previous application is cleaned up. */
|
||||||
|
if ((program_info.application_type & 3) == 1) {
|
||||||
|
EnsureApplicationResourcesAvailable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Try to create the process... */
|
||||||
|
if (R_FAILED((rc = ldrPmCreateProcess((launch_flags >> 2) & 3, new_process.ldr_queue_index, g_resource_limit_handles[reslimit_idx], &new_process.handle)))) {
|
||||||
|
goto PROCESS_CREATION_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the new process's id. */
|
||||||
|
svcGetProcessId(&new_process.pid, new_process.handle);
|
||||||
|
|
||||||
|
/* Register with FS. */
|
||||||
|
acid_fac = program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size;
|
||||||
|
aci0_fah = acid_fac + program_info.acid_fac_size;
|
||||||
|
if (R_FAILED((rc = fsprRegisterProgram(new_process.pid, new_process.tid_sid.title_id, new_process.tid_sid.storage_id, aci0_fah, program_info.aci0_fah_size, acid_fac, program_info.acid_fac_size)))) {
|
||||||
|
goto FS_REGISTRATION_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Register with PM. */
|
||||||
|
acid_sac = program_info.ac_buffer;
|
||||||
|
aci0_sac = program_info.ac_buffer + program_info.acid_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Setup process flags. */
|
||||||
|
if (program_info.application_type & 1) {
|
||||||
|
new_process.flags |= 0x40;
|
||||||
|
}
|
||||||
|
if (launch_flags & 1) {
|
||||||
|
new_process.flags |= 1;
|
||||||
|
}
|
||||||
|
if (launch_flags & 0x10) {
|
||||||
|
new_process.flags |= 0x8;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add process to the list. */
|
||||||
|
Registration::AddProcessToList(&new_process);
|
||||||
|
|
||||||
|
/* Signal, if relevant. */
|
||||||
|
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
|
||||||
|
g_debug_title_event->signal_event();
|
||||||
|
g_debug_on_launch_tid = 0;
|
||||||
|
rc = 0;
|
||||||
|
} else if ((new_process.flags & 0x40) && g_debug_next_application.load()) {
|
||||||
|
g_debug_application_event->signal_event();
|
||||||
|
g_debug_next_application = false;
|
||||||
|
rc = 0;
|
||||||
|
} else if (launch_flags & 2) {
|
||||||
|
rc = 0;
|
||||||
|
} 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_DebugDetached);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
Registration::RemoveProcessFromList(new_process.pid);
|
||||||
|
smManagerUnregisterProcess(new_process.pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
SM_REGISTRATION_FAILED:
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
fsprUnregisterProgram(new_process.pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
FS_REGISTRATION_FAILED:
|
||||||
|
if (R_FAILED(rc)) {
|
||||||
|
svcCloseHandle(new_process.handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) {
|
||||||
|
Result rc;
|
||||||
|
/* Only allow one mutex to exist. */
|
||||||
|
g_process_launch_mutex.Lock();
|
||||||
|
g_process_launch_state.tid_sid.title_id = title_id;
|
||||||
|
g_process_launch_state.tid_sid.storage_id = storage_id;
|
||||||
|
g_process_launch_state.launch_flags = launch_flags;
|
||||||
|
g_process_launch_state.out_pid = out_pid;
|
||||||
|
|
||||||
|
/* Start a launch, and wait for it to exit. */
|
||||||
|
g_process_launch_start_event->signal_event();
|
||||||
|
g_sema_finish_launch.Wait();
|
||||||
|
|
||||||
|
rc = g_process_launch_state.result;
|
||||||
|
|
||||||
|
g_process_launch_mutex.Unlock();
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result Registration::LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid) {
|
||||||
|
return LaunchProcess(tid_sid.title_id, tid_sid.storage_id, launch_flags, out_pid);
|
||||||
|
};
|
||||||
|
|
||||||
|
void Registration::HandleSignaledProcess(Process *process) {
|
||||||
|
u64 tmp;
|
||||||
|
|
||||||
|
/* Reset the signal. */
|
||||||
|
svcResetSignal(process->handle);
|
||||||
|
|
||||||
|
ProcessState old_state;
|
||||||
|
old_state = process->state;
|
||||||
|
svcGetProcessInfo(&tmp, process->handle, ProcessInfoType_ProcessState);
|
||||||
|
process->state = (ProcessState)tmp;
|
||||||
|
|
||||||
|
if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) {
|
||||||
|
process->flags &= ~0x4;
|
||||||
|
}
|
||||||
|
switch (process->state) {
|
||||||
|
case ProcessState_Created:
|
||||||
|
case ProcessState_DebugAttached:
|
||||||
|
case ProcessState_Exiting:
|
||||||
|
break;
|
||||||
|
case ProcessState_DebugDetached:
|
||||||
|
case ProcessState_Running:
|
||||||
|
if (process->flags & 8) {
|
||||||
|
process->flags &= ~0x30;
|
||||||
|
process->flags |= 0x10;
|
||||||
|
g_process_event->signal_event();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ProcessState_Crashed:
|
||||||
|
process->flags |= 6;
|
||||||
|
g_process_event->signal_event();
|
||||||
|
break;
|
||||||
|
case ProcessState_Exited:
|
||||||
|
if (process->flags & 1) {
|
||||||
|
g_process_event->signal_event();
|
||||||
|
} else {
|
||||||
|
FinalizeExitedProcess(process);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ProcessState_DebugSuspended:
|
||||||
|
if (process->flags & 8) {
|
||||||
|
process->flags |= 0x30;
|
||||||
|
g_process_event->signal_event();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::FinalizeExitedProcess(Process *process) {
|
||||||
|
g_process_list.Lock();
|
||||||
|
/* Unregister with FS. */
|
||||||
|
if (R_FAILED(fsprUnregisterProgram(process->pid))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
/* Unregister with SM. */
|
||||||
|
if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
/* Unregister with LDR. */
|
||||||
|
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
|
||||||
|
/* TODO: Panic. */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Remove. */
|
||||||
|
RemoveProcessFromList(process->pid);
|
||||||
|
|
||||||
|
g_process_list.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::AddProcessToList(Process *process) {
|
||||||
|
g_process_list.Lock();
|
||||||
|
g_process_list.process_waiters.push_back(new ProcessWaiter(process));
|
||||||
|
g_process_list.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::RemoveProcessFromList(u64 pid) {
|
||||||
|
g_process_list.Lock();
|
||||||
|
/* Remove process from list. */
|
||||||
|
for (unsigned int i = 0; i < g_process_list.process_waiters.size(); i++) {
|
||||||
|
ProcessWaiter *pw = g_process_list.process_waiters[i];
|
||||||
|
Registration::Process *process = pw->get_process();
|
||||||
|
if (process->pid == pid) {
|
||||||
|
g_process_list.process_waiters.erase(g_process_list.process_waiters.begin() + i);
|
||||||
|
svcCloseHandle(process->handle);
|
||||||
|
delete pw;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_process_list.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::SetProcessState(u64 pid, ProcessState new_state) {
|
||||||
|
g_process_list.Lock();
|
||||||
|
/* Set process state. */
|
||||||
|
for (unsigned int i = 0; i < g_process_list.process_waiters.size(); i++) {
|
||||||
|
ProcessWaiter *pw = g_process_list.process_waiters[i];
|
||||||
|
Registration::Process *process = pw->get_process();
|
||||||
|
if (process->pid == pid) {
|
||||||
|
process->state = new_state;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_process_list.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Registration::HasApplicationProcess() {
|
||||||
|
bool has_app = false;
|
||||||
|
g_process_list.Lock();
|
||||||
|
|
||||||
|
for (auto &pw : g_process_list.process_waiters) {
|
||||||
|
if (pw->get_process()->flags & 0x40) {
|
||||||
|
has_app = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g_process_list.Unlock();
|
||||||
|
return has_app;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Registration::EnsureApplicationResourcesAvailable() {
|
||||||
|
Handle application_reslimit_h = g_resource_limit_handles[1];
|
||||||
|
for (unsigned int i = 0; i < 5; i++) {
|
||||||
|
u64 result;
|
||||||
|
do {
|
||||||
|
if (R_FAILED(svcGetResourceLimitCurrentValue(&result, application_reslimit_h, (LimitableResource)i))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
svcSleepThread(1000000ULL);
|
||||||
|
} while (result);
|
||||||
|
}
|
||||||
|
}
|
47
stratosphere/pm/source/pm_registration.hpp
Normal file
47
stratosphere/pm/source/pm_registration.hpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#pragma once
|
||||||
|
#include <switch.h>
|
||||||
|
#include <stratosphere.hpp>
|
||||||
|
|
||||||
|
class Registration {
|
||||||
|
public:
|
||||||
|
struct TidSid {
|
||||||
|
u64 title_id;
|
||||||
|
FsStorageId storage_id;
|
||||||
|
};
|
||||||
|
struct Process {
|
||||||
|
Handle handle;
|
||||||
|
u64 pid;
|
||||||
|
u64 ldr_queue_index;
|
||||||
|
Registration::TidSid tid_sid;
|
||||||
|
ProcessState state;
|
||||||
|
u32 flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ProcessLaunchState {
|
||||||
|
TidSid tid_sid;
|
||||||
|
u64 launch_flags;
|
||||||
|
u64* out_pid;
|
||||||
|
Result result;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void InitializeSystemResources();
|
||||||
|
static IWaitable *GetProcessLaunchStartEvent();
|
||||||
|
static Result ProcessLaunchStartCallback(Handle *handles, size_t num_handles, u64 timeout);
|
||||||
|
|
||||||
|
static IWaitable *GetProcessList();
|
||||||
|
static void HandleSignaledProcess(Process *process);
|
||||||
|
static void FinalizeExitedProcess(Process *process);
|
||||||
|
|
||||||
|
static void AddProcessToList(Process *process);
|
||||||
|
static void RemoveProcessFromList(u64 pid);
|
||||||
|
static void SetProcessState(u64 pid, ProcessState new_state);
|
||||||
|
|
||||||
|
static void HandleProcessLaunch();
|
||||||
|
static void SignalFinishLaunchProcess();
|
||||||
|
static Result LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid);
|
||||||
|
static Result LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid);
|
||||||
|
|
||||||
|
static bool HasApplicationProcess();
|
||||||
|
static void EnsureApplicationResourcesAvailable();
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue