service.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <boost/container/flat_map.hpp>
  9. #include "common/bit_field.h"
  10. #include "common/common_types.h"
  11. #include "core/hle/kernel/hle_ipc.h"
  12. #include "core/hle/kernel/kernel.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Namespace Service
  15. namespace Kernel {
  16. class ClientPort;
  17. class ServerPort;
  18. class ServerSession;
  19. class HLERequestContext;
  20. }
  21. namespace Service {
  22. namespace SM {
  23. class ServiceManager;
  24. }
  25. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  26. /// Arbitrary default number of maximum connections to an HLE service.
  27. static const u32 DefaultMaxSessions = 10;
  28. /**
  29. * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it
  30. * is not meant to be used directly.
  31. *
  32. * @see ServiceFramework
  33. */
  34. class ServiceFrameworkBase : public Kernel::SessionRequestHandler {
  35. public:
  36. /// Returns the string identifier used to connect to the service.
  37. std::string GetServiceName() const {
  38. return service_name;
  39. }
  40. /**
  41. * Returns the maximum number of sessions that can be connected to this service at the same
  42. * time.
  43. */
  44. u32 GetMaxSessions() const {
  45. return max_sessions;
  46. }
  47. /// Creates a port pair and registers this service with the given ServiceManager.
  48. void InstallAsService(SM::ServiceManager& service_manager);
  49. /// Creates a port pair and registers it on the kernel's global port registry.
  50. void InstallAsNamedPort();
  51. /// Creates and returns an unregistered port for the service.
  52. Kernel::SharedPtr<Kernel::ClientPort> CreatePort();
  53. void InvokeRequest(Kernel::HLERequestContext& ctx);
  54. ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) override;
  55. protected:
  56. /// Member-function pointer type of SyncRequest handlers.
  57. template <typename Self>
  58. using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
  59. private:
  60. template <typename T>
  61. friend class ServiceFramework;
  62. struct FunctionInfoBase {
  63. u32 expected_header;
  64. HandlerFnP<ServiceFrameworkBase> handler_callback;
  65. const char* name;
  66. };
  67. using InvokerFn = void(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  68. Kernel::HLERequestContext& ctx);
  69. ServiceFrameworkBase(const char* service_name, u32 max_sessions, InvokerFn* handler_invoker);
  70. ~ServiceFrameworkBase();
  71. void RegisterHandlersBase(const FunctionInfoBase* functions, size_t n);
  72. void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info);
  73. /// Identifier string used to connect to the service.
  74. std::string service_name;
  75. /// Maximum number of concurrent sessions that this service can handle.
  76. u32 max_sessions;
  77. /**
  78. * Port where incoming connections will be received. Only created when InstallAsService() or
  79. * InstallAsNamedPort() are called.
  80. */
  81. Kernel::SharedPtr<Kernel::ServerPort> port;
  82. /// Function used to safely up-cast pointers to the derived class before invoking a handler.
  83. InvokerFn* handler_invoker;
  84. boost::container::flat_map<u32, FunctionInfoBase> handlers;
  85. };
  86. /**
  87. * Framework for implementing HLE services. Dispatches on the header id of incoming SyncRequests
  88. * based on a table mapping header ids to handler functions. Service implementations should inherit
  89. * from ServiceFramework using the CRTP (`class Foo : public ServiceFramework<Foo> { ... };`) and
  90. * populate it with handlers by calling #RegisterHandlers.
  91. *
  92. * In order to avoid duplicating code in the binary and exposing too many implementation details in
  93. * the header, this class is split into a non-templated base (ServiceFrameworkBase) and a template
  94. * deriving from it (ServiceFramework). The functions in this class will mostly only erase the type
  95. * of the passed in function pointers and then delegate the actual work to the implementation in the
  96. * base class.
  97. */
  98. template <typename Self>
  99. class ServiceFramework : public ServiceFrameworkBase {
  100. protected:
  101. /// Contains information about a request type which is handled by the service.
  102. struct FunctionInfo : FunctionInfoBase {
  103. // TODO(yuriks): This function could be constexpr, but clang is the only compiler that
  104. // doesn't emit an ICE or a wrong diagnostic because of the static_cast.
  105. /**
  106. * Constructs a FunctionInfo for a function.
  107. *
  108. * @param expected_header request header in the command buffer which will trigger dispatch
  109. * to this handler
  110. * @param handler_callback member function in this service which will be called to handle
  111. * the request
  112. * @param name human-friendly name for the request. Used mostly for logging purposes.
  113. */
  114. FunctionInfo(u32 expected_header, HandlerFnP<Self> handler_callback, const char* name)
  115. : FunctionInfoBase{
  116. expected_header,
  117. // Type-erase member function pointer by casting it down to the base class.
  118. static_cast<HandlerFnP<ServiceFrameworkBase>>(handler_callback), name} {}
  119. };
  120. /**
  121. * Initializes the handler with no functions installed.
  122. * @param max_sessions Maximum number of sessions that can be
  123. * connected to this service at the same time.
  124. */
  125. ServiceFramework(const char* service_name, u32 max_sessions = DefaultMaxSessions)
  126. : ServiceFrameworkBase(service_name, max_sessions, Invoker) {}
  127. /// Registers handlers in the service.
  128. template <size_t N>
  129. void RegisterHandlers(const FunctionInfo (&functions)[N]) {
  130. RegisterHandlers(functions, N);
  131. }
  132. /**
  133. * Registers handlers in the service. Usually prefer using the other RegisterHandlers
  134. * overload in order to avoid needing to specify the array size.
  135. */
  136. void RegisterHandlers(const FunctionInfo* functions, size_t n) {
  137. RegisterHandlersBase(functions, n);
  138. }
  139. private:
  140. /**
  141. * This function is used to allow invocation of pointers to handlers stored in the base class
  142. * without needing to expose the type of this derived class. Pointers-to-member may require a
  143. * fixup when being up or downcast, and thus code that does that needs to know the concrete type
  144. * of the derived class in order to invoke one of it's functions through a pointer.
  145. */
  146. static void Invoker(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  147. Kernel::HLERequestContext& ctx) {
  148. // Cast back up to our original types and call the member function
  149. (static_cast<Self*>(object)->*static_cast<HandlerFnP<Self>>(member))(ctx);
  150. }
  151. };
  152. /// Initialize ServiceManager
  153. void Init();
  154. /// Shutdown ServiceManager
  155. void Shutdown();
  156. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  157. extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
  158. /// Adds a port to the named port table
  159. void AddNamedPort(std::string name, Kernel::SharedPtr<Kernel::ClientPort> port);
  160. } // namespace