fs.mitm: WIP LayeredFS impl (NOTE: UNUSABLE ATM)

Also greatly refactors libstratosphere, and does a lot of other things.
There is a lot of code in this one.
This commit is contained in:
Michael Scire
2018-06-14 17:50:01 -06:00
parent 82b248aeac
commit c2d9ac8f5c
56 changed files with 1615 additions and 243 deletions

View File

@@ -16,3 +16,4 @@
#include "stratosphere/hossynch.hpp"
#include "stratosphere/waitablemanager.hpp"
#include "stratosphere/multithreadedwaitablemanager.hpp"

View File

@@ -1,37 +1,36 @@
#pragma once
#include <switch.h>
#include <memory>
#include <type_traits>
#include "iserviceobject.hpp"
#define DOMAIN_ID_MAX 0x200
#define DOMAIN_ID_MAX 0x1000
class IServiceObject;
class DomainOwner {
private:
IServiceObject *domain_objects[DOMAIN_ID_MAX];
std::shared_ptr<IServiceObject> domain_objects[DOMAIN_ID_MAX];
public:
DomainOwner() {
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
domain_objects[i] = NULL;
domain_objects[i].reset();
}
}
virtual ~DomainOwner() {
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
this->delete_object(i);
}
/* Shared ptrs should auto delete here. */
}
IServiceObject *get_domain_object(unsigned int i) {
std::shared_ptr<IServiceObject> get_domain_object(unsigned int i) {
if (i < DOMAIN_ID_MAX) {
return domain_objects[i];
}
return NULL;
return nullptr;
}
Result reserve_object(IServiceObject *object, unsigned int *out_i) {
Result reserve_object(std::shared_ptr<IServiceObject> object, unsigned int *out_i) {
for (unsigned int i = 4; i < DOMAIN_ID_MAX; i++) {
if (domain_objects[i] == NULL) {
domain_objects[i] = object;
@@ -43,7 +42,7 @@ class DomainOwner {
return 0x1900B;
}
Result set_object(IServiceObject *object, unsigned int i) {
Result set_object(std::shared_ptr<IServiceObject> object, unsigned int i) {
if (domain_objects[i] == NULL) {
domain_objects[i] = object;
object->set_owner(this);
@@ -52,7 +51,7 @@ class DomainOwner {
return 0x1900B;
}
unsigned int get_object_id(IServiceObject *object) {
unsigned int get_object_id(std::shared_ptr<IServiceObject> object) {
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
if (domain_objects[i] == object) {
return i;
@@ -63,16 +62,14 @@ class DomainOwner {
void delete_object(unsigned int i) {
if (domain_objects[i]) {
delete domain_objects[i];
domain_objects[i] = NULL;
domain_objects[i].reset();
}
}
void delete_object(IServiceObject *object) {
void delete_object(std::shared_ptr<IServiceObject> object) {
for (unsigned int i = 0; i < DOMAIN_ID_MAX; i++) {
if (domain_objects[i] == object) {
delete domain_objects[i];
domain_objects[i] = NULL;
domain_objects[i].reset();
break;
}
}

View File

@@ -4,18 +4,20 @@
#include "iwaitable.hpp"
typedef Result (*EventCallback)(Handle *handles, size_t num_handles, u64 timeout);
typedef Result (*EventCallback)(void *arg, Handle *handles, size_t num_handles, u64 timeout);
class IEvent : public IWaitable {
protected:
std::vector<Handle> handles;
EventCallback callback;
void *arg;
public:
IEvent(Handle wait_h, EventCallback callback) {
IEvent(Handle wait_h, void *a, EventCallback callback) {
if (wait_h) {
this->handles.push_back(wait_h);
}
this->arg = a;
this->callback = callback;
}
@@ -41,7 +43,7 @@ class IEvent : public IWaitable {
}
virtual Result handle_signaled(u64 timeout) {
return this->callback(this->handles.data(), this->handles.size(), timeout);
return this->callback(this->arg, this->handles.data(), this->handles.size(), timeout);
}
static Result PanicCallback(Handle *handles, size_t num_handles, u64 timeout) {

View File

@@ -17,13 +17,13 @@ class IPCSession final : public ISession<T> {
if (R_FAILED((rc = svcCreateSession(&this->server_handle, &this->client_handle, 0, 0)))) {
fatalSimple(rc);
}
this->service_object = new T();
this->service_object = std::make_shared<T>();
this->pointer_buffer_size = pbs;
this->pointer_buffer = new char[this->pointer_buffer_size];
this->is_domain = false;
}
IPCSession<T>(T *so, size_t pbs = 0x400) : ISession<T>(NULL, 0, 0, so, 0) {
IPCSession<T>(std::shared_ptr<T> so, size_t pbs = 0x400) : ISession<T>(NULL, 0, 0, so, 0) {
Result rc;
if (R_FAILED((rc = svcCreateSession(&this->server_handle, &this->client_handle, 0, 0)))) {
fatalSimple(rc);

View File

@@ -18,7 +18,6 @@ class IServiceObject {
DomainOwner *get_owner() { return this->owner; }
void set_owner(DomainOwner *owner) { this->owner = owner; }
virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0;
protected:
virtual Result handle_deferred() = 0;
};

View File

@@ -27,10 +27,10 @@ class IServer;
class IServiceObject;
template <typename T>
class ISession : public IWaitable, public DomainOwner {
class ISession : public IWaitable {
static_assert(std::is_base_of<IServiceObject, T>::value, "Service Objects must derive from IServiceObject");
protected:
T *service_object;
std::shared_ptr<T> service_object;
IServer<T> *server;
Handle server_handle;
Handle client_handle;
@@ -38,35 +38,35 @@ class ISession : public IWaitable, public DomainOwner {
size_t pointer_buffer_size;
bool is_domain;
std::shared_ptr<DomainOwner> domain;
IServiceObject *active_object;
std::shared_ptr<IServiceObject> active_object;
static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!");
public:
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, size_t pbs = 0x400) : server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) {
this->service_object = new T();
this->service_object = std::make_shared<T>();
if (this->pointer_buffer_size) {
this->pointer_buffer = new char[this->pointer_buffer_size];
}
this->is_domain = false;
this->active_object = NULL;
this->domain.reset();
this->active_object.reset();
}
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, T *so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) {
ISession<T>(IServer<T> *s, Handle s_h, Handle c_h, std::shared_ptr<T> so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) {
if (this->pointer_buffer_size) {
this->pointer_buffer = new char[this->pointer_buffer_size];
}
this->is_domain = false;
this->active_object = NULL;
this->domain.reset();
this->active_object.reset();
}
~ISession() override {
delete this->pointer_buffer;
if (this->service_object && !this->is_domain) {
//delete this->service_object;
}
if (server_handle) {
svcCloseHandle(server_handle);
}
@@ -86,12 +86,12 @@ class ISession : public IWaitable, public DomainOwner {
}
}
T *get_service_object() { return this->service_object; }
std::shared_ptr<T> get_service_object() { return this->service_object; }
Handle get_server_handle() { return this->server_handle; }
Handle get_client_handle() { return this->client_handle; }
DomainOwner *get_owner() { return is_domain ? this : NULL; }
DomainOwner *get_owner() { return this->is_domain ? this->domain.get() : NULL; }
/* IWaitable */
Handle get_handle() override {
@@ -125,7 +125,7 @@ class ISession : public IWaitable, public DomainOwner {
if (r.IsDomainMessage && r.MessageType == DomainMessageType_Close) {
this->delete_object(this->active_object);
this->domain->delete_object(this->active_object);
this->active_object = NULL;
struct {
u64 magic;
@@ -188,7 +188,7 @@ class ISession : public IWaitable, public DomainOwner {
ipcAddRecvStatic(&c_for_reply, this->pointer_buffer, this->pointer_buffer_size, 0);
ipcPrepareHeader(&c_for_reply, 0);
if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, timeout))) {
if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, U64_MAX))) {
if (handle_index != 0) {
/* TODO: Panic? */
}
@@ -203,7 +203,7 @@ class ISession : public IWaitable, public DomainOwner {
if (!r.IsDomainMessage || r.ThisObjectId >= DOMAIN_ID_MAX) {
retval = 0xF601;
} else {
this->active_object = this->get_domain_object(r.ThisObjectId);
this->active_object = this->domain->get_domain_object(r.ThisObjectId);
}
} else {
this->active_object = this->service_object;
@@ -218,19 +218,22 @@ class ISession : public IWaitable, public DomainOwner {
if (retval == RESULT_DEFER_SESSION) {
/* Session defer. */
this->active_object = NULL;
this->active_object.reset();
this->set_deferred(true);
rc = retval;
} else if (retval == 0xF601) {
/* Session close. */
this->active_object = NULL;
this->active_object.reset();
rc = retval;
} else {
if (R_SUCCEEDED(retval)) {
this->postprocess(r, cmd_id);
}
this->active_object = NULL;
this->active_object.reset();
rc = svcReplyAndReceive(&handle_index, &this->server_handle, 0, this->server_handle, 0);
if (rc == 0xEA01) {
rc = 0x0;
}
this->cleanup();
}
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <switch.h>
#include <vector>
#include "waitablemanager.hpp"
#include "systemevent.hpp"
class MultiThreadedWaitableManager : public WaitableManager {
protected:
u32 num_threads;
Thread *threads;
HosMutex get_waitable_lock;
SystemEvent *new_waitable_event;
public:
MultiThreadedWaitableManager(u32 n, u64 t, u32 ss = 0x8000) : WaitableManager(t), num_threads(n-1) {
u32 prio;
u32 cpuid = svcGetCurrentProcessorNumber();
Result rc;
threads = new Thread[num_threads];
if (R_FAILED((rc = svcGetThreadPriority(&prio, CUR_THREAD_HANDLE)))) {
fatalSimple(rc);
}
for (unsigned int i = 0; i < num_threads; i++) {
threads[i] = {0};
threadCreate(&threads[i], &MultiThreadedWaitableManager::thread_func, this, ss, prio, cpuid);
}
new_waitable_event = new SystemEvent(this, &MultiThreadedWaitableManager::add_waitable_callback);
this->waitables.push_back(new_waitable_event);
}
~MultiThreadedWaitableManager() override {
/* TODO: Exit the threads? */
}
IWaitable *get_waitable();
void add_waitable(IWaitable *waitable) override;
void process() override;
void process_until_timeout() override;
static Result add_waitable_callback(void *this_ptr, Handle *handles, size_t num_handles, u64 timeout);
static void thread_func(void *this_ptr);
};

View File

@@ -9,7 +9,7 @@
class SystemEvent final : public IEvent {
public:
SystemEvent(EventCallback callback) : IEvent(0, callback) {
SystemEvent(void *a, EventCallback callback) : IEvent(0, a, callback) {
Handle wait_h;
Handle sig_h;
if (R_FAILED(svcCreateEvent(&sig_h, &wait_h))) {

View File

@@ -9,16 +9,17 @@
class IWaitable;
class WaitableManager : public WaitableManagerBase {
std::vector<IWaitable *> to_add_waitables;
std::vector<IWaitable *> waitables;
u64 timeout;
HosMutex lock;
std::atomic_bool has_new_items;
protected:
std::vector<IWaitable *> to_add_waitables;
std::vector<IWaitable *> waitables;
u64 timeout;
HosMutex lock;
std::atomic_bool has_new_items;
private:
void process_internal(bool break_on_timeout);
public:
WaitableManager(u64 t) : waitables(0), timeout(t), has_new_items(false) { }
~WaitableManager() {
~WaitableManager() override {
/* This should call the destructor for every waitable. */
for (auto & waitable : waitables) {
delete waitable;
@@ -26,7 +27,7 @@ class WaitableManager : public WaitableManagerBase {
waitables.clear();
}
void add_waitable(IWaitable *waitable);
void process();
void process_until_timeout();
virtual void add_waitable(IWaitable *waitable);
virtual void process();
virtual void process_until_timeout();
};

View File

@@ -7,6 +7,7 @@ class WaitableManagerBase {
std::atomic<u64> cur_priority;
public:
WaitableManagerBase() : cur_priority(0) { }
virtual ~WaitableManagerBase() { }
u64 get_priority() {
return std::atomic_fetch_add(&cur_priority, (u64)1);