sm: Update for libstratosphere refactor

This commit is contained in:
Michael Scire 2018-10-29 17:28:37 -07:00 committed by SciresM
parent 058f735031
commit 9a8c70ed68
7 changed files with 87 additions and 144 deletions

View file

@ -1,7 +1,7 @@
{ {
"name" : "sm", "name" : "sm",
"title_id" : "0x0100000000000004", "title_id" : "0x0100000000000004",
"main_thread_stack_size" : "0x1000", "main_thread_stack_size" : "0x2000",
"main_thread_priority" : 27, "main_thread_priority" : 27,
"default_cpu_id" : 3, "default_cpu_id" : 3,
"process_category" : 1, "process_category" : 1,
@ -56,6 +56,7 @@
"svcReplyAndReceiveLight" : "0x42", "svcReplyAndReceiveLight" : "0x42",
"svcReplyAndReceive" : "0x43", "svcReplyAndReceive" : "0x43",
"svcReplyAndReceiveWithUserBuffer" : "0x44", "svcReplyAndReceiveWithUserBuffer" : "0x44",
"svcCreateEvent" : "0x45",
"svcGetMemoryInfo" : "0x6F", "svcGetMemoryInfo" : "0x6F",
"svcCreatePort" : "0x70", "svcCreatePort" : "0x70",
"svcManageNamedPort" : "0x71", "svcManageNamedPort" : "0x71",

View file

@ -61,15 +61,18 @@ void __appExit(void) {
/* Nothing to clean up, because we're sm. */ /* Nothing to clean up, because we're sm. */
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
consoleDebugInit(debugDevice_SVC); consoleDebugInit(debugDevice_SVC);
/* 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); auto server_manager = new WaitableManager(1);
/* Create sm:, (and thus allow things to register to it). */ /* Create sm:, (and thus allow things to register to it). */
server_manager->add_waitable(new ManagedPortServer<UserService>("sm:", 0x40)); server_manager->AddWaitable(new ManagedPortServer<UserService>("sm:", 0x40));
/* Create sm:m manually. */ /* Create sm:m manually. */
Handle smm_h; Handle smm_h;
@ -78,10 +81,10 @@ int main(int argc, char **argv)
while (1) { } while (1) { }
} }
server_manager->add_waitable(new ExistingPortServer<ManagerService>(smm_h, 1)); server_manager->AddWaitable(new ExistingPortServer<ManagerService>(smm_h, 1));
/* Loop forever, servicing our services. */ /* Loop forever, servicing our services. */
server_manager->process(); server_manager->Process();
/* Cleanup. */ /* Cleanup. */
delete server_manager; delete server_manager;

View file

@ -19,40 +19,15 @@
#include "sm_manager_service.hpp" #include "sm_manager_service.hpp"
#include "sm_registration.hpp" #include "sm_registration.hpp"
Result ManagerService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { Result ManagerService::RegisterProcess(u64 pid, InBuffer<u8> acid_sac, InBuffer<u8> aci0_sac) {
Result rc = 0xF601; return Registration::RegisterProcess(pid, acid_sac.buffer, acid_sac.num_elements, aci0_sac.buffer, aci0_sac.num_elements);
switch ((ManagerServiceCmd)cmd_id) {
case Manager_Cmd_RegisterProcess:
rc = WrapIpcCommandImpl<&ManagerService::register_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case Manager_Cmd_UnregisterProcess:
rc = WrapIpcCommandImpl<&ManagerService::unregister_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case Manager_Cmd_AtmosphereEndInitDefers:
rc = WrapIpcCommandImpl<&ManagerService::end_init_defers>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
default:
break;
}
return rc;
} }
Result ManagerService::handle_deferred() { Result ManagerService::UnregisterProcess(u64 pid) {
/* This service is never deferrable. */ return Registration::UnregisterProcess(pid);
return 0;
} }
void ManagerService::AtmosphereEndInitDefers() {
std::tuple<Result> ManagerService::register_process(u64 pid, InBuffer<u8> acid_sac, InBuffer<u8> aci0_sac) {
return {Registration::RegisterProcess(pid, acid_sac.buffer, acid_sac.num_elements, aci0_sac.buffer, aci0_sac.num_elements)};
}
std::tuple<Result> ManagerService::unregister_process(u64 pid) {
return {Registration::UnregisterProcess(pid)};
}
std::tuple<Result> ManagerService::end_init_defers() {
Registration::EndInitDefers(); Registration::EndInitDefers();
return {0};
} }

View file

@ -16,7 +16,7 @@
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere/iserviceobject.hpp> #include <stratosphere.hpp>
enum ManagerServiceCmd { enum ManagerServiceCmd {
Manager_Cmd_RegisterProcess = 0, Manager_Cmd_RegisterProcess = 0,
@ -27,17 +27,16 @@ enum ManagerServiceCmd {
}; };
class ManagerService final : public IServiceObject { class ManagerService final : public IServiceObject {
public:
Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) override;
Result handle_deferred() override;
ManagerService *clone() override {
return new ManagerService();
}
private: private:
/* Actual commands. */ /* Actual commands. */
std::tuple<Result> register_process(u64 pid, InBuffer<u8> acid_sac, InBuffer<u8> aci0_sac); virtual Result RegisterProcess(u64 pid, InBuffer<u8> acid_sac, InBuffer<u8> aci0_sac);
std::tuple<Result> unregister_process(u64 pid); virtual Result UnregisterProcess(u64 pid);
std::tuple<Result> end_init_defers(); virtual void AtmosphereEndInitDefers();
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<Manager_Cmd_RegisterProcess, &ManagerService::RegisterProcess>(),
MakeServiceCommandMeta<Manager_Cmd_UnregisterProcess, &ManagerService::UnregisterProcess>(),
MakeServiceCommandMeta<Manager_Cmd_AtmosphereEndInitDefers, &ManagerService::AtmosphereEndInitDefers>(),
};
}; };

View file

@ -16,7 +16,7 @@
#include <switch.h> #include <switch.h>
#include <algorithm> #include <algorithm>
#include <stratosphere/servicesession.hpp> #include <stratosphere.hpp>
#include "sm_registration.hpp" #include "sm_registration.hpp"
#include "meta_tools.hpp" #include "meta_tools.hpp"
@ -250,7 +250,7 @@ Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
struct { struct {
u64 magic; u64 magic;
u64 result; u64 result;
u64 should_mitm; bool should_mitm;
} *resp = ((decltype(resp))r.Raw); } *resp = ((decltype(resp))r.Raw);
rc = resp->result; rc = resp->result;
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {

View file

@ -15,57 +15,20 @@
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere/servicesession.hpp> #include <stratosphere.hpp>
#include "sm_user_service.hpp" #include "sm_user_service.hpp"
#include "sm_registration.hpp" #include "sm_registration.hpp"
Result UserService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) { Result UserService::Initialize(PidDescriptor pid) {
Result rc = 0xF601;
switch ((UserServiceCmd)cmd_id) {
case User_Cmd_Initialize:
rc = WrapIpcCommandImpl<&UserService::initialize>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case User_Cmd_GetService:
rc = WrapIpcCommandImpl<&UserService::get_service>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case User_Cmd_RegisterService:
rc = WrapIpcCommandImpl<&UserService::register_service>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case User_Cmd_UnregisterService:
rc = WrapIpcCommandImpl<&UserService::unregister_service>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
#ifdef SM_ENABLE_MITM
case User_Cmd_AtmosphereInstallMitm:
rc = WrapIpcCommandImpl<&UserService::install_mitm>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case User_Cmd_AtmosphereUninstallMitm:
rc = WrapIpcCommandImpl<&UserService::uninstall_mitm>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case User_Cmd_AtmosphereAssociatePidTidForMitm:
rc = WrapIpcCommandImpl<&UserService::associate_pid_tid_for_mitm>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
#endif
default:
break;
}
return rc;
}
Result UserService::handle_deferred() {
/* If we're deferred, GetService failed. */
return WrapDeferredIpcCommandImpl<&UserService::deferred_get_service>(this, this->deferred_service);
}
std::tuple<Result> UserService::initialize(PidDescriptor pid) {
this->pid = pid.pid; this->pid = pid.pid;
this->has_initialized = true; this->has_initialized = true;
return {0}; return 0;
} }
std::tuple<Result, MovedHandle> UserService::get_service(u64 service) { Result UserService::GetService(Out<MovedHandle> out_h, u64 service) {
Handle session_h = 0; Handle session_h = 0;
Result rc = 0x415; Result rc = 0x415;
#ifdef SM_ENABLE_SMHAX #ifdef SM_ENABLE_SMHAX
if (!this->has_initialized) { if (!this->has_initialized) {
rc = Registration::GetServiceForPid(Registration::GetInitialProcessId(), service, &session_h); rc = Registration::GetServiceForPid(Registration::GetInitialProcessId(), service, &session_h);
@ -74,20 +37,14 @@ std::tuple<Result, MovedHandle> UserService::get_service(u64 service) {
if (this->has_initialized) { if (this->has_initialized) {
rc = Registration::GetServiceForPid(this->pid, service, &session_h); rc = Registration::GetServiceForPid(this->pid, service, &session_h);
} }
/* It's possible that this will end up deferring us...take that into account. */
if (rc == RESULT_DEFER_SESSION) { if (R_SUCCEEDED(rc)) {
this->deferred_service = service; out_h.SetValue(session_h);
} }
return {rc, MovedHandle{session_h}}; return rc;
} }
std::tuple<Result, MovedHandle> UserService::deferred_get_service(u64 service) { Result UserService::RegisterService(Out<MovedHandle> out_h, u64 service, u8 is_light, u32 max_sessions) {
Handle session_h = 0;
Result rc = Registration::GetServiceHandle(this->pid, service, &session_h);
return {rc, MovedHandle{session_h}};
}
std::tuple<Result, MovedHandle> UserService::register_service(u64 service, u8 is_light, u32 max_sessions) {
Handle service_h = 0; Handle service_h = 0;
Result rc = 0x415; Result rc = 0x415;
#ifdef SM_ENABLE_SMHAX #ifdef SM_ENABLE_SMHAX
@ -98,10 +55,14 @@ std::tuple<Result, MovedHandle> UserService::register_service(u64 service, u8 is
if (this->has_initialized) { if (this->has_initialized) {
rc = Registration::RegisterServiceForPid(this->pid, service, max_sessions, (is_light & 1) != 0, &service_h); rc = Registration::RegisterServiceForPid(this->pid, service, max_sessions, (is_light & 1) != 0, &service_h);
} }
return {rc, MovedHandle{service_h}};
if (R_SUCCEEDED(rc)) {
out_h.SetValue(service_h);
}
return rc;
} }
std::tuple<Result> UserService::unregister_service(u64 service) { Result UserService::UnregisterService(u64 service) {
Result rc = 0x415; Result rc = 0x415;
#ifdef SM_ENABLE_SMHAX #ifdef SM_ENABLE_SMHAX
if (!this->has_initialized) { if (!this->has_initialized) {
@ -111,20 +72,33 @@ std::tuple<Result> UserService::unregister_service(u64 service) {
if (this->has_initialized) { if (this->has_initialized) {
rc = Registration::UnregisterServiceForPid(this->pid, service); rc = Registration::UnregisterServiceForPid(this->pid, service);
} }
return {rc}; return rc;
} }
std::tuple<Result, MovedHandle, MovedHandle> UserService::install_mitm(u64 service) { Result UserService::AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, u64 service) {
Handle service_h = 0; Handle service_h = 0;
Handle query_h = 0; Handle query_h = 0;
Result rc = 0x415; Result rc = 0x415;
if (this->has_initialized) { if (this->has_initialized) {
rc = Registration::InstallMitmForPid(this->pid, service, &service_h, &query_h); rc = Registration::InstallMitmForPid(this->pid, service, &service_h, &query_h);
} }
return {rc, MovedHandle{service_h}, MovedHandle{query_h}};
if (R_SUCCEEDED(rc)) {
srv_h.SetValue(service_h);
qry_h.SetValue(query_h);
}
return rc;
} }
std::tuple<Result> UserService::associate_pid_tid_for_mitm(u64 pid, u64 tid) { Result UserService::AtmosphereUninstallMitm(u64 service) {
Result rc = 0x415;
if (this->has_initialized) {
rc = Registration::UninstallMitmForPid(this->pid, service);
}
return rc;
}
Result UserService::AtmosphereAssociatePidTidForMitm(u64 pid, u64 tid) {
Result rc = 0x415; Result rc = 0x415;
if (this->has_initialized) { if (this->has_initialized) {
if (Registration::IsInitialProcess(pid)) { if (Registration::IsInitialProcess(pid)) {
@ -133,13 +107,5 @@ std::tuple<Result> UserService::associate_pid_tid_for_mitm(u64 pid, u64 tid) {
rc = Registration::AssociatePidTidForMitm(pid, tid); rc = Registration::AssociatePidTidForMitm(pid, tid);
} }
} }
return {rc}; return rc;
}
std::tuple<Result> UserService::uninstall_mitm(u64 service) {
Result rc = 0x415;
if (this->has_initialized) {
rc = Registration::UninstallMitmForPid(this->pid, service);
}
return {rc};
} }

View file

@ -16,7 +16,7 @@
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include <stratosphere/iserviceobject.hpp> #include <stratosphere.hpp>
enum UserServiceCmd { enum UserServiceCmd {
User_Cmd_Initialize = 0, User_Cmd_Initialize = 0,
@ -30,32 +30,31 @@ enum UserServiceCmd {
}; };
class UserService final : public IServiceObject { class UserService final : public IServiceObject {
u64 pid = U64_MAX;
bool has_initialized = false;
u64 deferred_service = 0;
public:
Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) override;
Result handle_deferred() override;
UserService *clone() override {
auto new_srv = new UserService();
new_srv->pid = pid;
new_srv->has_initialized = has_initialized;
new_srv->deferred_service = deferred_service;
return new_srv;
}
private: private:
u64 pid = U64_MAX;
bool has_initialized = false;
/* Actual commands. */ /* Actual commands. */
std::tuple<Result> initialize(PidDescriptor pid); virtual Result Initialize(PidDescriptor pid);
std::tuple<Result, MovedHandle> get_service(u64 service); virtual Result GetService(Out<MovedHandle> out_h, u64 service);
std::tuple<Result, MovedHandle> deferred_get_service(u64 service); virtual Result RegisterService(Out<MovedHandle> out_h, u64 service, u8 is_light, u32 max_sessions);
std::tuple<Result, MovedHandle> register_service(u64 service, u8 is_light, u32 max_sessions); virtual Result UnregisterService(u64 service);
std::tuple<Result> unregister_service(u64 service);
/* Atmosphere commands. */ /* Atmosphere commands. */
std::tuple<Result, MovedHandle, MovedHandle> install_mitm(u64 service); virtual Result AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, u64 service);
std::tuple<Result> uninstall_mitm(u64 service); virtual Result AtmosphereUninstallMitm(u64 service);
std::tuple<Result> associate_pid_tid_for_mitm(u64 pid, u64 tid); virtual Result AtmosphereAssociatePidTidForMitm(u64 pid, u64 tid);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<User_Cmd_Initialize, &UserService::Initialize>(),
MakeServiceCommandMeta<User_Cmd_GetService, &UserService::GetService>(),
MakeServiceCommandMeta<User_Cmd_RegisterService, &UserService::RegisterService>(),
MakeServiceCommandMeta<User_Cmd_UnregisterService, &UserService::UnregisterService>(),
#ifdef SM_ENABLE_MITM
MakeServiceCommandMeta<User_Cmd_AtmosphereInstallMitm, &UserService::AtmosphereInstallMitm>(),
MakeServiceCommandMeta<User_Cmd_AtmosphereUninstallMitm, &UserService::AtmosphereUninstallMitm>(),
MakeServiceCommandMeta<User_Cmd_AtmosphereAssociatePidTidForMitm, &UserService::AtmosphereAssociatePidTidForMitm>(),
#endif
};
}; };