service.h 7.3 KB

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