summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/hle/kernel/hle_ipc.cpp2
-rw-r--r--src/core/hle/kernel/hle_ipc.h46
-rw-r--r--src/core/hle/service/service.cpp98
-rw-r--r--src/core/hle/service/service.h150
-rw-r--r--src/core/hle/service/sm/sm.cpp14
-rw-r--r--src/core/hle/service/sm/sm.h10
-rw-r--r--src/core/hle/service/sm/srv.cpp71
-rw-r--r--src/core/hle/service/sm/srv.h26
9 files changed, 355 insertions, 64 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 6e602b0c5..b16a89990 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -385,4 +385,4 @@ set(HEADERS
385create_directory_groups(${SRCS} ${HEADERS}) 385create_directory_groups(${SRCS} ${HEADERS})
386add_library(core STATIC ${SRCS} ${HEADERS}) 386add_library(core STATIC ${SRCS} ${HEADERS})
387target_link_libraries(core PUBLIC common PRIVATE audio_core video_core) 387target_link_libraries(core PUBLIC common PRIVATE audio_core video_core)
388target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp dynarmic) 388target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp dynarmic fmt)
diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp
index 0922b3f47..a60b8ef00 100644
--- a/src/core/hle/kernel/hle_ipc.cpp
+++ b/src/core/hle/kernel/hle_ipc.cpp
@@ -21,4 +21,6 @@ void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_s
21 boost::range::remove_erase(connected_sessions, server_session); 21 boost::range::remove_erase(connected_sessions, server_session);
22} 22}
23 23
24HLERequestContext::~HLERequestContext() = default;
25
24} // namespace Kernel 26} // namespace Kernel
diff --git a/src/core/hle/kernel/hle_ipc.h b/src/core/hle/kernel/hle_ipc.h
index 14f682f44..c30184eab 100644
--- a/src/core/hle/kernel/hle_ipc.h
+++ b/src/core/hle/kernel/hle_ipc.h
@@ -7,10 +7,13 @@
7#include <memory> 7#include <memory>
8#include <vector> 8#include <vector>
9#include "core/hle/kernel/kernel.h" 9#include "core/hle/kernel/kernel.h"
10#include "core/hle/kernel/server_session.h"
10 11
11namespace Kernel { 12namespace Service {
13class ServiceFrameworkBase;
14}
12 15
13class ServerSession; 16namespace Kernel {
14 17
15/** 18/**
16 * Interface implemented by HLE Session handlers. 19 * Interface implemented by HLE Session handlers.
@@ -19,6 +22,8 @@ class ServerSession;
19 */ 22 */
20class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> { 23class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
21public: 24public:
25 virtual ~SessionRequestHandler() = default;
26
22 /** 27 /**
23 * Handles a sync request from the emulated application. 28 * Handles a sync request from the emulated application.
24 * @param server_session The ServerSession that was triggered for this sync request, 29 * @param server_session The ServerSession that was triggered for this sync request,
@@ -27,27 +32,56 @@ public:
27 * this request (ServerSession, Originator thread, Translated command buffer, etc). 32 * this request (ServerSession, Originator thread, Translated command buffer, etc).
28 * @returns ResultCode the result code of the translate operation. 33 * @returns ResultCode the result code of the translate operation.
29 */ 34 */
30 virtual void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0; 35 virtual void HandleSyncRequest(SharedPtr<ServerSession> server_session) = 0;
31 36
32 /** 37 /**
33 * Signals that a client has just connected to this HLE handler and keeps the 38 * Signals that a client has just connected to this HLE handler and keeps the
34 * associated ServerSession alive for the duration of the connection. 39 * associated ServerSession alive for the duration of the connection.
35 * @param server_session Owning pointer to the ServerSession associated with the connection. 40 * @param server_session Owning pointer to the ServerSession associated with the connection.
36 */ 41 */
37 void ClientConnected(Kernel::SharedPtr<Kernel::ServerSession> server_session); 42 void ClientConnected(SharedPtr<ServerSession> server_session);
38 43
39 /** 44 /**
40 * Signals that a client has just disconnected from this HLE handler and releases the 45 * Signals that a client has just disconnected from this HLE handler and releases the
41 * associated ServerSession. 46 * associated ServerSession.
42 * @param server_session ServerSession associated with the connection. 47 * @param server_session ServerSession associated with the connection.
43 */ 48 */
44 void ClientDisconnected(Kernel::SharedPtr<Kernel::ServerSession> server_session); 49 void ClientDisconnected(SharedPtr<ServerSession> server_session);
45 50
46protected: 51protected:
47 /// List of sessions that are connected to this handler. 52 /// List of sessions that are connected to this handler.
48 /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list 53 /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
49 // for the duration of the connection. 54 // for the duration of the connection.
50 std::vector<Kernel::SharedPtr<Kernel::ServerSession>> connected_sessions; 55 std::vector<SharedPtr<ServerSession>> connected_sessions;
56};
57
58/**
59 * Class containing information about an in-flight IPC request being handled by an HLE service
60 * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
61 * when possible use the APIs in this class to service the request.
62 */
63class HLERequestContext {
64public:
65 ~HLERequestContext();
66
67 /// Returns a pointer to the IPC command buffer for this request.
68 u32* CommandBuffer() const {
69 return cmd_buf;
70 }
71
72 /**
73 * Returns the session through which this request was made. This can be used as a map key to
74 * access per-client data on services.
75 */
76 SharedPtr<ServerSession> Session() const {
77 return session;
78 }
79
80private:
81 friend class Service::ServiceFrameworkBase;
82
83 u32* cmd_buf = nullptr;
84 SharedPtr<ServerSession> session;
51}; 85};
52 86
53} // namespace Kernel 87} // namespace Kernel
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 0d443aa44..d34968428 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <fmt/format.h>
5#include "common/logging/log.h" 6#include "common/logging/log.h"
6#include "common/string_util.h" 7#include "common/string_util.h"
7#include "core/hle/kernel/client_port.h" 8#include "core/hle/kernel/client_port.h"
@@ -45,9 +46,14 @@
45#include "core/hle/service/ssl_c.h" 46#include "core/hle/service/ssl_c.h"
46#include "core/hle/service/y2r_u.h" 47#include "core/hle/service/y2r_u.h"
47 48
49using Kernel::ClientPort;
50using Kernel::ServerPort;
51using Kernel::ServerSession;
52using Kernel::SharedPtr;
53
48namespace Service { 54namespace Service {
49 55
50std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports; 56std::unordered_map<std::string, SharedPtr<ClientPort>> g_kernel_named_ports;
51 57
52/** 58/**
53 * Creates a function string for logging, complete with the name (or header code, depending 59 * Creates a function string for logging, complete with the name (or header code, depending
@@ -69,7 +75,7 @@ static std::string MakeFunctionString(const char* name, const char* port_name,
69Interface::Interface(u32 max_sessions) : max_sessions(max_sessions) {} 75Interface::Interface(u32 max_sessions) : max_sessions(max_sessions) {}
70Interface::~Interface() = default; 76Interface::~Interface() = default;
71 77
72void Interface::HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) { 78void Interface::HandleSyncRequest(SharedPtr<ServerSession> server_session) {
73 // TODO(Subv): Make use of the server_session in the HLE service handlers to distinguish which 79 // TODO(Subv): Make use of the server_session in the HLE service handlers to distinguish which
74 // session triggered each command. 80 // session triggered each command.
75 81
@@ -103,16 +109,91 @@ void Interface::Register(const FunctionInfo* functions, size_t n) {
103} 109}
104 110
105//////////////////////////////////////////////////////////////////////////////////////////////////// 111////////////////////////////////////////////////////////////////////////////////////////////////////
112
113ServiceFrameworkBase::ServiceFrameworkBase(const char* service_name, u32 max_sessions,
114 InvokerFn* handler_invoker)
115 : service_name(service_name), max_sessions(max_sessions), handler_invoker(handler_invoker) {}
116
117ServiceFrameworkBase::~ServiceFrameworkBase() = default;
118
119void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) {
120 ASSERT(port == nullptr);
121 port = service_manager.RegisterService(service_name, max_sessions).Unwrap();
122 port->SetHleHandler(shared_from_this());
123}
124
125void ServiceFrameworkBase::InstallAsNamedPort() {
126 ASSERT(port == nullptr);
127 SharedPtr<ServerPort> server_port;
128 SharedPtr<ClientPort> client_port;
129 std::tie(server_port, client_port) = ServerPort::CreatePortPair(max_sessions, service_name);
130 server_port->SetHleHandler(shared_from_this());
131 AddNamedPort(service_name, std::move(client_port));
132}
133
134void ServiceFrameworkBase::RegisterHandlersBase(const FunctionInfoBase* functions, size_t n) {
135 handlers.reserve(handlers.size() + n);
136 for (size_t i = 0; i < n; ++i) {
137 // Usually this array is sorted by id already, so hint to insert at the end
138 handlers.emplace_hint(handlers.cend(), functions[i].expected_header, functions[i]);
139 }
140}
141
142void ServiceFrameworkBase::ReportUnimplementedFunction(u32* cmd_buf, const FunctionInfoBase* info) {
143 IPC::Header header{cmd_buf[0]};
144 int num_params = header.normal_params_size + header.translate_params_size;
145 std::string function_name = info == nullptr ? fmt::format("{:#08x}", cmd_buf[0]) : info->name;
146
147 fmt::MemoryWriter w;
148 w.write("function '{}': port='{}' cmd_buf={{[0]={:#x}", function_name, service_name,
149 cmd_buf[0]);
150 for (int i = 1; i <= num_params; ++i) {
151 w.write(", [{}]={:#x}", i, cmd_buf[i]);
152 }
153 w << '}';
154
155 LOG_ERROR(Service, "unknown / unimplemented %s", w.c_str());
156 // TODO(bunnei): Hack - ignore error
157 cmd_buf[1] = 0;
158}
159
160void ServiceFrameworkBase::HandleSyncRequest(SharedPtr<ServerSession> server_session) {
161 u32* cmd_buf = Kernel::GetCommandBuffer();
162
163 // TODO(yuriks): The kernel should be the one handling this as part of translation after
164 // everything else is migrated
165 Kernel::HLERequestContext context;
166 context.cmd_buf = cmd_buf;
167 context.session = std::move(server_session);
168
169 u32 header_code = cmd_buf[0];
170 auto itr = handlers.find(header_code);
171 const FunctionInfoBase* info = itr == handlers.end() ? nullptr : &itr->second;
172 if (info == nullptr || info->handler_callback == nullptr) {
173 return ReportUnimplementedFunction(cmd_buf, info);
174 }
175
176 LOG_TRACE(Service, "%s",
177 MakeFunctionString(info->name, GetServiceName().c_str(), cmd_buf).c_str());
178 handler_invoker(this, info->handler_callback, context);
179}
180
181////////////////////////////////////////////////////////////////////////////////////////////////////
106// Module interface 182// Module interface
107 183
184// TODO(yuriks): Move to kernel
185void AddNamedPort(std::string name, SharedPtr<ClientPort> port) {
186 g_kernel_named_ports.emplace(std::move(name), std::move(port));
187}
188
108static void AddNamedPort(Interface* interface_) { 189static void AddNamedPort(Interface* interface_) {
109 Kernel::SharedPtr<Kernel::ServerPort> server_port; 190 SharedPtr<ServerPort> server_port;
110 Kernel::SharedPtr<Kernel::ClientPort> client_port; 191 SharedPtr<ClientPort> client_port;
111 std::tie(server_port, client_port) = 192 std::tie(server_port, client_port) =
112 Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName()); 193 ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName());
113 194
114 server_port->SetHleHandler(std::shared_ptr<Interface>(interface_)); 195 server_port->SetHleHandler(std::shared_ptr<Interface>(interface_));
115 g_kernel_named_ports.emplace(interface_->GetPortName(), std::move(client_port)); 196 AddNamedPort(interface_->GetPortName(), std::move(client_port));
116} 197}
117 198
118void AddService(Interface* interface_) { 199void AddService(Interface* interface_) {
@@ -125,8 +206,9 @@ void AddService(Interface* interface_) {
125 206
126/// Initialize ServiceManager 207/// Initialize ServiceManager
127void Init() { 208void Init() {
128 SM::g_service_manager = std::make_unique<SM::ServiceManager>(); 209 SM::g_service_manager = std::make_shared<SM::ServiceManager>();
129 AddNamedPort(new SM::SRV); 210 SM::ServiceManager::InstallInterfaces(SM::g_service_manager);
211
130 AddNamedPort(new ERR::ERR_F); 212 AddNamedPort(new ERR::ERR_F);
131 213
132 FS::ArchiveInit(); 214 FS::ArchiveInit();
diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h
index 8933d57cc..281ff99bb 100644
--- a/src/core/hle/service/service.h
+++ b/src/core/hle/service/service.h
@@ -18,11 +18,16 @@
18 18
19namespace Kernel { 19namespace Kernel {
20class ClientPort; 20class ClientPort;
21class ServerPort;
21class ServerSession; 22class ServerSession;
22} 23}
23 24
24namespace Service { 25namespace Service {
25 26
27namespace SM {
28class ServiceManager;
29}
30
26static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters) 31static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
27/// Arbitrary default number of maximum connections to an HLE service. 32/// Arbitrary default number of maximum connections to an HLE service.
28static const u32 DefaultMaxSessions = 10; 33static const u32 DefaultMaxSessions = 10;
@@ -30,6 +35,9 @@ static const u32 DefaultMaxSessions = 10;
30/** 35/**
31 * Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a 36 * Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a
32 * table mapping header ids to handler functions. 37 * table mapping header ids to handler functions.
38 *
39 * @deprecated Use ServiceFramework for new services instead. It allows services to be stateful and
40 * is more extensible going forward.
33 */ 41 */
34class Interface : public Kernel::SessionRequestHandler { 42class Interface : public Kernel::SessionRequestHandler {
35public: 43public:
@@ -101,6 +109,146 @@ private:
101 boost::container::flat_map<u32, FunctionInfo> m_functions; 109 boost::container::flat_map<u32, FunctionInfo> m_functions;
102}; 110};
103 111
112/**
113 * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it
114 * is not meant to be used directly.
115 *
116 * @see ServiceFramework
117 */
118class ServiceFrameworkBase : public Kernel::SessionRequestHandler {
119public:
120 /// Returns the string identifier used to connect to the service.
121 std::string GetServiceName() const {
122 return service_name;
123 }
124
125 /**
126 * Returns the maximum number of sessions that can be connected to this service at the same
127 * time.
128 */
129 u32 GetMaxSessions() const {
130 return max_sessions;
131 }
132
133 /// Creates a port pair and registers this service with the given ServiceManager.
134 void InstallAsService(SM::ServiceManager& service_manager);
135 /// Creates a port pair and registers it on the kernel's global port registry.
136 void InstallAsNamedPort();
137
138 void HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
139
140protected:
141 /// Member-function pointer type of SyncRequest handlers.
142 template <typename Self>
143 using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
144
145private:
146 template <typename T>
147 friend class ServiceFramework;
148
149 struct FunctionInfoBase {
150 u32 expected_header;
151 HandlerFnP<ServiceFrameworkBase> handler_callback;
152 const char* name;
153 };
154
155 using InvokerFn = void(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
156 Kernel::HLERequestContext& ctx);
157
158 ServiceFrameworkBase(const char* service_name, u32 max_sessions, InvokerFn* handler_invoker);
159 ~ServiceFrameworkBase();
160
161 void RegisterHandlersBase(const FunctionInfoBase* functions, size_t n);
162 void ReportUnimplementedFunction(u32* cmd_buf, const FunctionInfoBase* info);
163
164 /// Identifier string used to connect to the service.
165 std::string service_name;
166 /// Maximum number of concurrent sessions that this service can handle.
167 u32 max_sessions;
168
169 /**
170 * Port where incoming connections will be received. Only created when InstallAsService() or
171 * InstallAsNamedPort() are called.
172 */
173 Kernel::SharedPtr<Kernel::ServerPort> port;
174
175 /// Function used to safely up-cast pointers to the derived class before invoking a handler.
176 InvokerFn* handler_invoker;
177 boost::container::flat_map<u32, FunctionInfoBase> handlers;
178};
179
180/**
181 * Framework for implementing HLE services. Dispatches on the header id of incoming SyncRequests
182 * based on a table mapping header ids to handler functions. Service implementations should inherit
183 * from ServiceFramework using the CRTP (`class Foo : public ServiceFramework<Foo> { ... };`) and
184 * populate it with handlers by calling #RegisterHandlers.
185 *
186 * In order to avoid duplicating code in the binary and exposing too many implementation details in
187 * the header, this class is split into a non-templated base (ServiceFrameworkBase) and a template
188 * deriving from it (ServiceFramework). The functions in this class will mostly only erase the type
189 * of the passed in function pointers and then delegate the actual work to the implementation in the
190 * base class.
191 */
192template <typename Self>
193class ServiceFramework : public ServiceFrameworkBase {
194protected:
195 /// Contains information about a request type which is handled by the service.
196 struct FunctionInfo : FunctionInfoBase {
197 // TODO(yuriks): This function could be constexpr, but clang is the only compiler that
198 // doesn't emit an ICE or a wrong diagnostic because of the static_cast.
199
200 /**
201 * Constructs a FunctionInfo for a function.
202 *
203 * @param expected_header request header in the command buffer which will trigger dispatch
204 * to this handler
205 * @param handler_callback member function in this service which will be called to handle
206 * the request
207 * @param name human-friendly name for the request. Used mostly for logging purposes.
208 */
209 FunctionInfo(u32 expected_header, HandlerFnP<Self> handler_callback, const char* name)
210 : FunctionInfoBase{
211 expected_header,
212 // Type-erase member function pointer by casting it down to the base class.
213 static_cast<HandlerFnP<ServiceFrameworkBase>>(handler_callback), name} {}
214 };
215
216 /**
217 * Initializes the handler with no functions installed.
218 * @param max_sessions Maximum number of sessions that can be
219 * connected to this service at the same time.
220 */
221 ServiceFramework(const char* service_name, u32 max_sessions = DefaultMaxSessions)
222 : ServiceFrameworkBase(service_name, max_sessions, Invoker) {}
223
224 /// Registers handlers in the service.
225 template <size_t N>
226 void RegisterHandlers(const FunctionInfo (&functions)[N]) {
227 RegisterHandlers(functions, N);
228 }
229
230 /**
231 * Registers handlers in the service. Usually prefer using the other RegisterHandlers
232 * overload in order to avoid needing to specify the array size.
233 */
234 void RegisterHandlers(const FunctionInfo* functions, size_t n) {
235 RegisterHandlersBase(functions, n);
236 }
237
238private:
239 /**
240 * This function is used to allow invocation of pointers to handlers stored in the base class
241 * without needing to expose the type of this derived class. Pointers-to-member may require a
242 * fixup when being up or downcast, and thus code that does that needs to know the concrete type
243 * of the derived class in order to invoke one of it's functions through a pointer.
244 */
245 static void Invoker(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
246 Kernel::HLERequestContext& ctx) {
247 // Cast back up to our original types and call the member function
248 (static_cast<Self*>(object)->*static_cast<HandlerFnP<Self>>(member))(ctx);
249 }
250};
251
104/// Initialize ServiceManager 252/// Initialize ServiceManager
105void Init(); 253void Init();
106 254
@@ -110,6 +258,8 @@ void Shutdown();
110/// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC. 258/// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
111extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports; 259extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
112 260
261/// Adds a port to the named port table
262void AddNamedPort(std::string name, Kernel::SharedPtr<Kernel::ClientPort> port);
113/// Adds a service to the services table 263/// Adds a service to the services table
114void AddService(Interface* interface_); 264void AddService(Interface* interface_);
115 265
diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp
index 361f7a0a9..5e7fc68f9 100644
--- a/src/core/hle/service/sm/sm.cpp
+++ b/src/core/hle/service/sm/sm.cpp
@@ -3,11 +3,13 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <tuple> 5#include <tuple>
6#include "common/assert.h"
6#include "core/hle/kernel/client_port.h" 7#include "core/hle/kernel/client_port.h"
7#include "core/hle/kernel/client_session.h" 8#include "core/hle/kernel/client_session.h"
8#include "core/hle/kernel/server_port.h" 9#include "core/hle/kernel/server_port.h"
9#include "core/hle/result.h" 10#include "core/hle/result.h"
10#include "core/hle/service/sm/sm.h" 11#include "core/hle/service/sm/sm.h"
12#include "core/hle/service/sm/srv.h"
11 13
12namespace Service { 14namespace Service {
13namespace SM { 15namespace SM {
@@ -22,6 +24,14 @@ static ResultCode ValidateServiceName(const std::string& name) {
22 return RESULT_SUCCESS; 24 return RESULT_SUCCESS;
23} 25}
24 26
27void ServiceManager::InstallInterfaces(std::shared_ptr<ServiceManager> self) {
28 ASSERT(self->srv_interface.expired());
29
30 auto srv = std::make_shared<SRV>(self);
31 srv->InstallAsNamedPort();
32 self->srv_interface = srv;
33}
34
25ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> ServiceManager::RegisterService( 35ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> ServiceManager::RegisterService(
26 std::string name, unsigned int max_sessions) { 36 std::string name, unsigned int max_sessions) {
27 37
@@ -30,7 +40,7 @@ ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> ServiceManager::RegisterService
30 Kernel::SharedPtr<Kernel::ClientPort> client_port; 40 Kernel::SharedPtr<Kernel::ClientPort> client_port;
31 std::tie(server_port, client_port) = Kernel::ServerPort::CreatePortPair(max_sessions, name); 41 std::tie(server_port, client_port) = Kernel::ServerPort::CreatePortPair(max_sessions, name);
32 42
33 registered_services.emplace(name, std::move(client_port)); 43 registered_services.emplace(std::move(name), std::move(client_port));
34 return MakeResult<Kernel::SharedPtr<Kernel::ServerPort>>(std::move(server_port)); 44 return MakeResult<Kernel::SharedPtr<Kernel::ServerPort>>(std::move(server_port));
35} 45}
36 46
@@ -53,7 +63,7 @@ ResultVal<Kernel::SharedPtr<Kernel::ClientSession>> ServiceManager::ConnectToSer
53 return client_port->Connect(); 63 return client_port->Connect();
54} 64}
55 65
56std::unique_ptr<ServiceManager> g_service_manager; 66std::shared_ptr<ServiceManager> g_service_manager;
57 67
58} // namespace SM 68} // namespace SM
59} // namespace Service 69} // namespace Service
diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h
index 5fac5455c..8f0dbf2db 100644
--- a/src/core/hle/service/sm/sm.h
+++ b/src/core/hle/service/sm/sm.h
@@ -20,6 +20,8 @@ class SessionRequestHandler;
20namespace Service { 20namespace Service {
21namespace SM { 21namespace SM {
22 22
23class SRV;
24
23constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(1, ErrorModule::SRV, ErrorSummary::WouldBlock, 25constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(1, ErrorModule::SRV, ErrorSummary::WouldBlock,
24 ErrorLevel::Temporary); // 0xD0406401 26 ErrorLevel::Temporary); // 0xD0406401
25constexpr ResultCode ERR_MAX_CONNECTIONS_REACHED(2, ErrorModule::SRV, ErrorSummary::WouldBlock, 27constexpr ResultCode ERR_MAX_CONNECTIONS_REACHED(2, ErrorModule::SRV, ErrorSummary::WouldBlock,
@@ -33,17 +35,21 @@ constexpr ResultCode ERR_NAME_CONTAINS_NUL(7, ErrorModule::SRV, ErrorSummary::Wr
33 35
34class ServiceManager { 36class ServiceManager {
35public: 37public:
38 static void InstallInterfaces(std::shared_ptr<ServiceManager> self);
39
36 ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> RegisterService(std::string name, 40 ResultVal<Kernel::SharedPtr<Kernel::ServerPort>> RegisterService(std::string name,
37 unsigned int max_sessions); 41 unsigned int max_sessions);
38 ResultVal<Kernel::SharedPtr<Kernel::ClientPort>> GetServicePort(const std::string& name); 42 ResultVal<Kernel::SharedPtr<Kernel::ClientPort>> GetServicePort(const std::string& name);
39 ResultVal<Kernel::SharedPtr<Kernel::ClientSession>> ConnectToService(const std::string& name); 43 ResultVal<Kernel::SharedPtr<Kernel::ClientSession>> ConnectToService(const std::string& name);
40 44
41private: 45private:
42 /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle. 46 std::weak_ptr<SRV> srv_interface;
47
48 /// Map of registered services, retrieved using GetServicePort or ConnectToService.
43 std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> registered_services; 49 std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> registered_services;
44}; 50};
45 51
46extern std::unique_ptr<ServiceManager> g_service_manager; 52extern std::shared_ptr<ServiceManager> g_service_manager;
47 53
48} // namespace SM 54} // namespace SM
49} // namespace Service 55} // namespace Service
diff --git a/src/core/hle/service/sm/srv.cpp b/src/core/hle/service/sm/srv.cpp
index 063b1b0fc..b8b62b068 100644
--- a/src/core/hle/service/sm/srv.cpp
+++ b/src/core/hle/service/sm/srv.cpp
@@ -20,8 +20,6 @@ namespace SM {
20 20
21constexpr int MAX_PENDING_NOTIFICATIONS = 16; 21constexpr int MAX_PENDING_NOTIFICATIONS = 16;
22 22
23static Kernel::SharedPtr<Kernel::Semaphore> notification_semaphore;
24
25/** 23/**
26 * SRV::RegisterClient service function 24 * SRV::RegisterClient service function
27 * Inputs: 25 * Inputs:
@@ -31,8 +29,8 @@ static Kernel::SharedPtr<Kernel::Semaphore> notification_semaphore;
31 * 0: 0x00010040 29 * 0: 0x00010040
32 * 1: ResultCode 30 * 1: ResultCode
33 */ 31 */
34static void RegisterClient(Interface* self) { 32void SRV::RegisterClient(Kernel::HLERequestContext& ctx) {
35 u32* cmd_buff = Kernel::GetCommandBuffer(); 33 u32* cmd_buff = ctx.CommandBuffer();
36 34
37 if (cmd_buff[1] != IPC::CallingPidDesc()) { 35 if (cmd_buff[1] != IPC::CallingPidDesc()) {
38 cmd_buff[0] = IPC::MakeHeader(0x0, 0x1, 0); // 0x40 36 cmd_buff[0] = IPC::MakeHeader(0x0, 0x1, 0); // 0x40
@@ -54,8 +52,8 @@ static void RegisterClient(Interface* self) {
54 * 2: Translation descriptor: 0x20 52 * 2: Translation descriptor: 0x20
55 * 3: Handle to semaphore signaled on process notification 53 * 3: Handle to semaphore signaled on process notification
56 */ 54 */
57static void EnableNotification(Interface* self) { 55void SRV::EnableNotification(Kernel::HLERequestContext& ctx) {
58 u32* cmd_buff = Kernel::GetCommandBuffer(); 56 u32* cmd_buff = ctx.CommandBuffer();
59 57
60 notification_semaphore = 58 notification_semaphore =
61 Kernel::Semaphore::Create(0, MAX_PENDING_NOTIFICATIONS, "SRV:Notification").Unwrap(); 59 Kernel::Semaphore::Create(0, MAX_PENDING_NOTIFICATIONS, "SRV:Notification").Unwrap();
@@ -78,9 +76,9 @@ static void EnableNotification(Interface* self) {
78 * 1: ResultCode 76 * 1: ResultCode
79 * 3: Service handle 77 * 3: Service handle
80 */ 78 */
81static void GetServiceHandle(Interface* self) { 79void SRV::GetServiceHandle(Kernel::HLERequestContext& ctx) {
82 ResultCode res = RESULT_SUCCESS; 80 ResultCode res = RESULT_SUCCESS;
83 u32* cmd_buff = Kernel::GetCommandBuffer(); 81 u32* cmd_buff = ctx.CommandBuffer();
84 82
85 size_t name_len = cmd_buff[3]; 83 size_t name_len = cmd_buff[3];
86 if (name_len > Service::kMaxPortSize) { 84 if (name_len > Service::kMaxPortSize) {
@@ -94,7 +92,7 @@ static void GetServiceHandle(Interface* self) {
94 92
95 // TODO(yuriks): Permission checks go here 93 // TODO(yuriks): Permission checks go here
96 94
97 auto client_port = g_service_manager->GetServicePort(name); 95 auto client_port = service_manager->GetServicePort(name);
98 if (client_port.Failed()) { 96 if (client_port.Failed()) {
99 cmd_buff[1] = client_port.Code().raw; 97 cmd_buff[1] = client_port.Code().raw;
100 LOG_ERROR(Service_SRV, "called service=%s, failed with code=0x%08X", name.c_str(), 98 LOG_ERROR(Service_SRV, "called service=%s, failed with code=0x%08X", name.c_str(),
@@ -128,8 +126,8 @@ static void GetServiceHandle(Interface* self) {
128 * 0: 0x00090040 126 * 0: 0x00090040
129 * 1: ResultCode 127 * 1: ResultCode
130 */ 128 */
131static void Subscribe(Interface* self) { 129void SRV::Subscribe(Kernel::HLERequestContext& ctx) {
132 u32* cmd_buff = Kernel::GetCommandBuffer(); 130 u32* cmd_buff = ctx.CommandBuffer();
133 131
134 u32 notification_id = cmd_buff[1]; 132 u32 notification_id = cmd_buff[1];
135 133
@@ -147,8 +145,8 @@ static void Subscribe(Interface* self) {
147 * 0: 0x000A0040 145 * 0: 0x000A0040
148 * 1: ResultCode 146 * 1: ResultCode
149 */ 147 */
150static void Unsubscribe(Interface* self) { 148void SRV::Unsubscribe(Kernel::HLERequestContext& ctx) {
151 u32* cmd_buff = Kernel::GetCommandBuffer(); 149 u32* cmd_buff = ctx.CommandBuffer();
152 150
153 u32 notification_id = cmd_buff[1]; 151 u32 notification_id = cmd_buff[1];
154 152
@@ -167,8 +165,8 @@ static void Unsubscribe(Interface* self) {
167 * 0: 0x000C0040 165 * 0: 0x000C0040
168 * 1: ResultCode 166 * 1: ResultCode
169 */ 167 */
170static void PublishToSubscriber(Interface* self) { 168void SRV::PublishToSubscriber(Kernel::HLERequestContext& ctx) {
171 u32* cmd_buff = Kernel::GetCommandBuffer(); 169 u32* cmd_buff = ctx.CommandBuffer();
172 170
173 u32 notification_id = cmd_buff[1]; 171 u32 notification_id = cmd_buff[1];
174 u8 flags = cmd_buff[2] & 0xFF; 172 u8 flags = cmd_buff[2] & 0xFF;
@@ -179,31 +177,28 @@ static void PublishToSubscriber(Interface* self) {
179 flags); 177 flags);
180} 178}
181 179
182const Interface::FunctionInfo FunctionTable[] = { 180SRV::SRV(std::shared_ptr<ServiceManager> service_manager)
183 {0x00010002, RegisterClient, "RegisterClient"}, 181 : ServiceFramework("srv:", 4), service_manager(std::move(service_manager)) {
184 {0x00020000, EnableNotification, "EnableNotification"}, 182 static const FunctionInfo functions[] = {
185 {0x00030100, nullptr, "RegisterService"}, 183 {0x00010002, &SRV::RegisterClient, "RegisterClient"},
186 {0x000400C0, nullptr, "UnregisterService"}, 184 {0x00020000, &SRV::EnableNotification, "EnableNotification"},
187 {0x00050100, GetServiceHandle, "GetServiceHandle"}, 185 {0x00030100, nullptr, "RegisterService"},
188 {0x000600C2, nullptr, "RegisterPort"}, 186 {0x000400C0, nullptr, "UnregisterService"},
189 {0x000700C0, nullptr, "UnregisterPort"}, 187 {0x00050100, &SRV::GetServiceHandle, "GetServiceHandle"},
190 {0x00080100, nullptr, "GetPort"}, 188 {0x000600C2, nullptr, "RegisterPort"},
191 {0x00090040, Subscribe, "Subscribe"}, 189 {0x000700C0, nullptr, "UnregisterPort"},
192 {0x000A0040, Unsubscribe, "Unsubscribe"}, 190 {0x00080100, nullptr, "GetPort"},
193 {0x000B0000, nullptr, "ReceiveNotification"}, 191 {0x00090040, &SRV::Subscribe, "Subscribe"},
194 {0x000C0080, PublishToSubscriber, "PublishToSubscriber"}, 192 {0x000A0040, &SRV::Unsubscribe, "Unsubscribe"},
195 {0x000D0040, nullptr, "PublishAndGetSubscriber"}, 193 {0x000B0000, nullptr, "ReceiveNotification"},
196 {0x000E00C0, nullptr, "IsServiceRegistered"}, 194 {0x000C0080, &SRV::PublishToSubscriber, "PublishToSubscriber"},
197}; 195 {0x000D0040, nullptr, "PublishAndGetSubscriber"},
198 196 {0x000E00C0, nullptr, "IsServiceRegistered"},
199SRV::SRV() { 197 };
200 Register(FunctionTable); 198 RegisterHandlers(functions);
201 notification_semaphore = nullptr;
202} 199}
203 200
204SRV::~SRV() { 201SRV::~SRV() = default;
205 notification_semaphore = nullptr;
206}
207 202
208} // namespace SM 203} // namespace SM
209} // namespace Service 204} // namespace Service
diff --git a/src/core/hle/service/sm/srv.h b/src/core/hle/service/sm/srv.h
index 4196ca1e2..75cca5184 100644
--- a/src/core/hle/service/sm/srv.h
+++ b/src/core/hle/service/sm/srv.h
@@ -4,21 +4,33 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <string> 7#include "core/hle/kernel/kernel.h"
8#include "core/hle/service/service.h" 8#include "core/hle/service/service.h"
9 9
10namespace Kernel {
11class HLERequestContext;
12class Semaphore;
13}
14
10namespace Service { 15namespace Service {
11namespace SM { 16namespace SM {
12 17
13/// Interface to "srv:" service 18/// Interface to "srv:" service
14class SRV final : public Interface { 19class SRV final : public ServiceFramework<SRV> {
15public: 20public:
16 SRV(); 21 explicit SRV(std::shared_ptr<ServiceManager> service_manager);
17 ~SRV() override; 22 ~SRV();
23
24private:
25 void RegisterClient(Kernel::HLERequestContext& ctx);
26 void EnableNotification(Kernel::HLERequestContext& ctx);
27 void GetServiceHandle(Kernel::HLERequestContext& ctx);
28 void Subscribe(Kernel::HLERequestContext& ctx);
29 void Unsubscribe(Kernel::HLERequestContext& ctx);
30 void PublishToSubscriber(Kernel::HLERequestContext& ctx);
18 31
19 std::string GetPortName() const override { 32 std::shared_ptr<ServiceManager> service_manager;
20 return "srv:"; 33 Kernel::SharedPtr<Kernel::Semaphore> notification_semaphore;
21 }
22}; 34};
23 35
24} // namespace SM 36} // namespace SM