sf: implement service framework enough for ro to work.

This completely re-does the whole interface for ipc servers.
This commit is contained in:
Michael Scire
2019-10-10 23:49:28 -07:00
committed by SciresM
parent bd341d5c00
commit f4dcd1db9b
47 changed files with 3545 additions and 166 deletions

View File

@@ -45,7 +45,7 @@ namespace sts::ro::impl {
return ResultSuccess;
}
Result ValidateNrr(const NrrHeader *header, u64 size, u64 title_id, ModuleType expected_type, bool enforce_type) {
Result ValidateNrr(const NrrHeader *header, u64 size, ncm::TitleId title_id, ModuleType expected_type, bool enforce_type) {
/* Check magic. */
if (!header->IsMagicValid()) {
return ResultRoInvalidNrr;
@@ -68,7 +68,7 @@ namespace sts::ro::impl {
}
/* Check type. */
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_700 && enforce_type) {
if (hos::GetVersion() >= hos::Version_700 && enforce_type) {
if (expected_type != header->GetType()) {
return ResultRoInvalidNrrType;
}
@@ -81,7 +81,7 @@ namespace sts::ro::impl {
}
/* Utilities for working with NRRs. */
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, u64 title_id, u64 nrr_heap_address, u64 nrr_heap_size, ModuleType expected_type, bool enforce_type) {
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, ncm::TitleId title_id, u64 nrr_heap_address, u64 nrr_heap_size, ModuleType expected_type, bool enforce_type) {
map::MappedCodeMemory nrr_mcm(ResultRoInternalError);
/* First, map the NRR. */

View File

@@ -22,7 +22,7 @@
namespace sts::ro::impl {
/* Utilities for working with NRRs. */
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, u64 title_id, u64 nrr_heap_address, u64 nrr_heap_size, ModuleType expected_type, bool enforce_type);
Result MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, ncm::TitleId title_id, u64 nrr_heap_address, u64 nrr_heap_size, ModuleType expected_type, bool enforce_type);
Result UnmapNrr(Handle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address);
}

View File

@@ -77,25 +77,25 @@ namespace sts::ro::impl {
NroInfo nro_infos[MaxNroInfos];
NrrInfo nrr_infos[MaxNrrInfos];
Handle process_handle;
u64 process_id;
os::ProcessId process_id;
bool in_use;
u64 GetTitleId(Handle other_process_h) const {
ncm::TitleId GetTitleId(Handle other_process_h) const {
/* Automatically select a handle, allowing for override. */
Handle process_h = this->process_handle;
if (other_process_h != INVALID_HANDLE) {
process_h = other_process_h;
}
u64 title_id = 0;
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_300) {
ncm::TitleId title_id = ncm::TitleId::Invalid;
if (hos::GetVersion() >= hos::Version_300) {
/* 3.0.0+: Use svcGetInfo. */
R_ASSERT(svcGetInfo(&title_id, InfoType_TitleId, process_h, 0));
R_ASSERT(svcGetInfo(&title_id.value, InfoType_TitleId, process_h, 0));
} else {
/* 1.0.0-2.3.0: We're not inside loader, so ask pm. */
u64 process_id = 0;
R_ASSERT(svcGetProcessId(&process_id, process_h));
R_ASSERT(pminfoGetTitleId(&title_id, process_id));
os::ProcessId process_id = os::InvalidProcessId;
R_ASSERT(svcGetProcessId(&process_id.value, process_h));
R_ASSERT(pminfoGetTitleId(&title_id.value, process_id.value));
}
return title_id;
}
@@ -254,7 +254,7 @@ namespace sts::ro::impl {
return &g_process_contexts[context_id];
}
ProcessContext *GetContextByProcessId(u64 process_id) {
ProcessContext *GetContextByProcessId(os::ProcessId process_id) {
for (size_t i = 0; i < MaxSessions; i++) {
if (g_process_contexts[i].process_id == process_id) {
return &g_process_contexts[i];
@@ -263,7 +263,7 @@ namespace sts::ro::impl {
return nullptr;
}
size_t AllocateContext(Handle process_handle, u64 process_id) {
size_t AllocateContext(Handle process_handle, os::ProcessId process_id) {
/* Find a free process context. */
for (size_t i = 0; i < MaxSessions; i++) {
ProcessContext *context = &g_process_contexts[i];
@@ -328,13 +328,13 @@ namespace sts::ro::impl {
}
/* Context utilities. */
Result RegisterProcess(size_t *out_context_id, Handle process_handle, u64 process_id) {
Result RegisterProcess(size_t *out_context_id, Handle process_handle, os::ProcessId process_id) {
/* Validate process handle. */
{
u64 handle_pid = 0;
os::ProcessId handle_pid = os::InvalidProcessId;
/* Validate handle is a valid process handle. */
if (R_FAILED(svcGetProcessId(&handle_pid, process_handle))) {
if (R_FAILED(svcGetProcessId(&handle_pid.value, process_handle))) {
return ResultRoInvalidProcess;
}
@@ -353,7 +353,7 @@ namespace sts::ro::impl {
return ResultSuccess;
}
Result ValidateProcess(size_t context_id, u64 process_id) {
Result ValidateProcess(size_t context_id, os::ProcessId process_id) {
const ProcessContext *ctx = GetContextById(context_id);
if (ctx == nullptr || ctx->process_id != process_id) {
return ResultRoInvalidProcess;
@@ -372,7 +372,7 @@ namespace sts::ro::impl {
STS_ASSERT(context != nullptr);
/* Get title id. */
const u64 title_id = context->GetTitleId(process_h);
const ncm::TitleId title_id = context->GetTitleId(process_h);
/* Validate address/size. */
if (nrr_address & 0xFFF) {
@@ -461,7 +461,7 @@ namespace sts::ro::impl {
R_TRY(MapNro(&nro_info->base_address, context->process_handle, nro_address, nro_size, bss_address, bss_size));
/* Validate the NRO (parsing region extents). */
u64 rx_size, ro_size, rw_size;
u64 rx_size = 0, ro_size = 0, rw_size = 0;
R_TRY_CLEANUP(context->ValidateNro(&nro_info->module_id, &rx_size, &ro_size, &rw_size, nro_info->base_address, nro_size, bss_size), {
UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, nro_size, 0);
});
@@ -503,7 +503,7 @@ namespace sts::ro::impl {
}
/* Debug service implementations. */
Result GetProcessModuleInfo(u32 *out_count, LoaderModuleInfo *out_infos, size_t max_out_count, u64 process_id) {
Result GetProcessModuleInfo(u32 *out_count, LoaderModuleInfo *out_infos, size_t max_out_count, os::ProcessId process_id) {
size_t count = 0;
const ProcessContext *context = GetContextByProcessId(process_id);
if (context != nullptr) {

View File

@@ -32,8 +32,8 @@ namespace sts::ro::impl {
bool ShouldEaseNroRestriction();
/* Context utilities. */
Result RegisterProcess(size_t *out_context_id, Handle process_handle, u64 process_id);
Result ValidateProcess(size_t context_id, u64 process_id);
Result RegisterProcess(size_t *out_context_id, Handle process_handle, os::ProcessId process_id);
Result ValidateProcess(size_t context_id, os::ProcessId process_id);
void UnregisterProcess(size_t context_id);
/* Service implementations. */
@@ -43,6 +43,6 @@ namespace sts::ro::impl {
Result UnloadNro(size_t context_id, u64 nro_address);
/* Debug service implementations. */
Result GetProcessModuleInfo(u32 *out_count, LoaderModuleInfo *out_infos, size_t max_out_count, u64 process_id);
Result GetProcessModuleInfo(u32 *out_count, LoaderModuleInfo *out_infos, size_t max_out_count, os::ProcessId process_id);
}

View File

@@ -23,11 +23,11 @@
namespace sts::ro {
Result DebugMonitorService::GetProcessModuleInfo(Out<u32> count, OutBuffer<LoaderModuleInfo> out_infos, u64 pid) {
if (out_infos.num_elements > INT_MAX) {
Result DebugMonitorService::GetProcessModuleInfo(sf::Out<u32> out_count, const sf::OutArray<LoaderModuleInfo> &out_infos, os::ProcessId process_id) {
if (out_infos.GetSize() > INT_MAX) {
return ResultRoInvalidSize;
}
return impl::GetProcessModuleInfo(count.GetPointer(), out_infos.buffer, out_infos.num_elements, pid);
return impl::GetProcessModuleInfo(out_count.GetPointer(), out_infos.GetPointer(), out_infos.GetSize(), process_id);
}
}

View File

@@ -20,17 +20,17 @@
namespace sts::ro {
class DebugMonitorService final : public IServiceObject {
class DebugMonitorService final : public sf::IServiceObject {
protected:
enum class CommandId {
GetProcessModuleInfo = 0,
};
private:
/* Actual commands. */
Result GetProcessModuleInfo(Out<u32> count, OutBuffer<LoaderModuleInfo> out_infos, u64 pid);
Result GetProcessModuleInfo(sf::Out<u32> out_count, const sf::OutArray<LoaderModuleInfo> &out_infos, os::ProcessId process_id);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(DebugMonitorService, GetProcessModuleInfo),
MAKE_SERVICE_COMMAND_META(GetProcessModuleInfo),
};
};

View File

@@ -33,7 +33,7 @@ extern "C" {
u32 __nx_applet_type = AppletType_None;
#define INNER_HEAP_SIZE 0x30000
#define INNER_HEAP_SIZE 0x4000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
@@ -57,13 +57,16 @@ void __libnx_initheap(void) {
fake_heap_end = (char*)addr + size;
}
using namespace sts;
void __appInit(void) {
SetFirmwareVersionForLibnx();
hos::SetVersionForLibnx();
DoWithSmSession([&]() {
R_ASSERT(setsysInitialize());
R_ASSERT(fsInitialize());
if (GetRuntimeFirmwareVersion() < FirmwareVersion_300) {
R_ASSERT(splInitialize());
if (hos::GetVersion() < hos::Version_300) {
R_ASSERT(pminfoInitialize());
}
});
@@ -76,44 +79,55 @@ void __appInit(void) {
void __appExit(void) {
fsdevUnmountAll();
fsExit();
if (GetRuntimeFirmwareVersion() < FirmwareVersion_300) {
if (hos::GetVersion() < hos::Version_300) {
pminfoExit();
}
setsysExit();
}
using namespace sts;
/* Helpers to create RO objects. */
static const auto MakeRoServiceForSelf = []() { return std::make_shared<ro::Service>(ro::ModuleType::ForSelf); };
static const auto MakeRoServiceForOthers = []() { return std::make_shared<ro::Service>(ro::ModuleType::ForOthers); };
static constexpr auto MakeRoServiceForSelf = []() { return std::make_shared<ro::Service>(ro::ModuleType::ForSelf); };
static constexpr auto MakeRoServiceForOthers = []() { return std::make_shared<ro::Service>(ro::ModuleType::ForOthers); };
namespace {
/* ldr:ro, ro:dmnt, ro:1. */
/* TODO: Consider max sessions enforcement? */
constexpr size_t NumServers = 3;
sf::hipc::ServerManager<NumServers> g_server_manager;
constexpr sm::ServiceName DebugMonitorServiceName = sm::ServiceName::Encode("ro:dmnt");
constexpr size_t DebugMonitorMaxSessions = 2;
/* NOTE: Official code passes 32 for ldr:ro max sessions. We will pass 2, because that's the actual limit. */
constexpr sm::ServiceName ForSelfServiceName = sm::ServiceName::Encode("ldr:ro");
constexpr size_t ForSelfMaxSessions = 2;
constexpr sm::ServiceName ForOthersServiceName = sm::ServiceName::Encode("ro:1");
constexpr size_t ForOthersMaxSessions = 2;
}
int main(int argc, char **argv)
{
/* Initialize Debug config. */
{
DoWithSmSession([]() {
R_ASSERT(splInitialize());
});
ON_SCOPE_EXIT { splExit(); };
ro::SetDevelopmentHardware(spl::IsDevelopmentHardware());
ro::SetDevelopmentFunctionEnabled(spl::IsDevelopmentFunctionEnabled());
}
/* Static server manager. */
static auto s_server_manager = WaitableManager(1);
/* Create services. */
s_server_manager.AddWaitable(new ServiceServer<ro::DebugMonitorService>("ro:dmnt", 2));
/* NOTE: Official code passes 32 for ldr:ro max sessions. We will pass 2, because that's the actual limit. */
s_server_manager.AddWaitable(new ServiceServer<ro::Service, +MakeRoServiceForSelf>("ldr:ro", 2));
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_700) {
s_server_manager.AddWaitable(new ServiceServer<ro::Service, +MakeRoServiceForOthers>("ro:1", 2));
R_ASSERT((g_server_manager.RegisterServer<ro::DebugMonitorService>(DebugMonitorServiceName, DebugMonitorMaxSessions)));
R_ASSERT((g_server_manager.RegisterServer<ro::Service, +MakeRoServiceForSelf>(ForSelfServiceName, ForSelfMaxSessions)));
if (hos::GetVersion() >= hos::Version_700) {
R_ASSERT((g_server_manager.RegisterServer<ro::Service, +MakeRoServiceForOthers>(ForOthersServiceName, ForOthersMaxSessions)));
}
/* Loop forever, servicing our services. */
s_server_manager.Process();
g_server_manager.LoopProcess();
/* Cleanup */
return 0;

View File

@@ -40,33 +40,33 @@ namespace sts::ro {
impl::UnregisterProcess(this->context_id);
}
Result Service::LoadNro(Out<u64> load_address, PidDescriptor pid_desc, u64 nro_address, u64 nro_size, u64 bss_address, u64 bss_size) {
R_TRY(impl::ValidateProcess(this->context_id, pid_desc.pid));
Result Service::LoadNro(sf::Out<u64> load_address, const sf::ClientProcessId &client_pid, u64 nro_address, u64 nro_size, u64 bss_address, u64 bss_size) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::LoadNro(load_address.GetPointer(), this->context_id, nro_address, nro_size, bss_address, bss_size);
}
Result Service::UnloadNro(PidDescriptor pid_desc, u64 nro_address) {
R_TRY(impl::ValidateProcess(this->context_id, pid_desc.pid));
Result Service::UnloadNro(const sf::ClientProcessId &client_pid, u64 nro_address) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::UnloadNro(this->context_id, nro_address);
}
Result Service::LoadNrr(PidDescriptor pid_desc, u64 nrr_address, u64 nrr_size) {
R_TRY(impl::ValidateProcess(this->context_id, pid_desc.pid));
Result Service::LoadNrr(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::LoadNrr(this->context_id, INVALID_HANDLE, nrr_address, nrr_size, ModuleType::ForSelf, true);
}
Result Service::UnloadNrr(PidDescriptor pid_desc, u64 nrr_address) {
R_TRY(impl::ValidateProcess(this->context_id, pid_desc.pid));
Result Service::UnloadNrr(const sf::ClientProcessId &client_pid, u64 nrr_address) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::UnloadNrr(this->context_id, nrr_address);
}
Result Service::Initialize(PidDescriptor pid_desc, CopiedHandle process_h) {
return impl::RegisterProcess(&this->context_id, process_h.handle, pid_desc.pid);
Result Service::Initialize(const sf::ClientProcessId &client_pid, sf::CopyHandle process_h) {
return impl::RegisterProcess(&this->context_id, process_h.GetValue(), client_pid.GetValue());
}
Result Service::LoadNrrEx(PidDescriptor pid_desc, u64 nrr_address, u64 nrr_size, CopiedHandle process_h) {
R_TRY(impl::ValidateProcess(this->context_id, pid_desc.pid));
return impl::LoadNrr(this->context_id, process_h.handle, nrr_address, nrr_size, this->type, this->type == ModuleType::ForOthers);
Result Service::LoadNrrEx(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size, sf::CopyHandle process_h) {
R_TRY(impl::ValidateProcess(this->context_id, client_pid.GetValue()));
return impl::LoadNrr(this->context_id, process_h.GetValue(), nrr_address, nrr_size, this->type, this->type == ModuleType::ForOthers);
}
}

View File

@@ -26,7 +26,7 @@ namespace sts::ro {
void SetDevelopmentHardware(bool is_development_hardware);
void SetDevelopmentFunctionEnabled(bool is_development_function_enabled);
class Service final : public IServiceObject {
class Service final : public sf::IServiceObject {
protected:
enum class CommandId {
LoadNro = 0,
@@ -41,23 +41,23 @@ namespace sts::ro {
ModuleType type;
public:
explicit Service(ModuleType t);
virtual ~Service() override;
virtual ~Service();
private:
/* Actual commands. */
Result LoadNro(Out<u64> load_address, PidDescriptor pid_desc, u64 nro_address, u64 nro_size, u64 bss_address, u64 bss_size);
Result UnloadNro(PidDescriptor pid_desc, u64 nro_address);
Result LoadNrr(PidDescriptor pid_desc, u64 nrr_address, u64 nrr_size);
Result UnloadNrr(PidDescriptor pid_desc, u64 nrr_address);
Result Initialize(PidDescriptor pid_desc, CopiedHandle process_h);
Result LoadNrrEx(PidDescriptor pid_desc, u64 nrr_address, u64 nrr_size, CopiedHandle process_h);
Result LoadNro(sf::Out<u64> out_load_address, const sf::ClientProcessId &client_pid, u64 nro_address, u64 nro_size, u64 bss_address, u64 bss_size);
Result UnloadNro(const sf::ClientProcessId &client_pid, u64 nro_address);
Result LoadNrr(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size);
Result UnloadNrr(const sf::ClientProcessId &client_pid, u64 nrr_address);
Result Initialize(const sf::ClientProcessId &client_pid, sf::CopyHandle process_h);
Result LoadNrrEx(const sf::ClientProcessId &client_pid, u64 nrr_address, u64 nrr_size, sf::CopyHandle process_h);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(Service, LoadNro),
MAKE_SERVICE_COMMAND_META(Service, UnloadNro),
MAKE_SERVICE_COMMAND_META(Service, LoadNrr),
MAKE_SERVICE_COMMAND_META(Service, UnloadNrr),
MAKE_SERVICE_COMMAND_META(Service, Initialize),
MAKE_SERVICE_COMMAND_META(Service, LoadNrrEx, FirmwareVersion_700),
MAKE_SERVICE_COMMAND_META(LoadNro),
MAKE_SERVICE_COMMAND_META(UnloadNro),
MAKE_SERVICE_COMMAND_META(LoadNrr),
MAKE_SERVICE_COMMAND_META(UnloadNrr),
MAKE_SERVICE_COMMAND_META(Initialize),
MAKE_SERVICE_COMMAND_META(LoadNrrEx, hos::Version_700),
};
};