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.h36
-rw-r--r--src/core/hle/service/service.cpp83
-rw-r--r--src/core/hle/service/service.h150
5 files changed, 269 insertions, 4 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 5de9d59d3..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.
@@ -52,4 +55,33 @@ protected:
52 std::vector<SharedPtr<ServerSession>> connected_sessions; 55 std::vector<SharedPtr<ServerSession>> connected_sessions;
53}; 56};
54 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;
85};
86
55} // namespace Kernel 87} // namespace Kernel
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 0d443aa44..10f2b3ee3 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,6 +46,11 @@
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, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
@@ -103,8 +109,83 @@ 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 Kernel::SharedPtr<Kernel::ServerPort> server_port;
110 Kernel::SharedPtr<Kernel::ClientPort> client_port; 191 Kernel::SharedPtr<Kernel::ClientPort> client_port;
@@ -112,7 +193,7 @@ static void AddNamedPort(Interface* interface_) {
112 Kernel::ServerPort::CreatePortPair(interface_->GetMaxSessions(), interface_->GetPortName()); 193 Kernel::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_) {
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