service.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <boost/container/flat_map.hpp>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/hle_ipc.h"
  10. #include "core/hle/kernel/object.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace Service
  13. namespace Core {
  14. class System;
  15. }
  16. namespace FileSys {
  17. class VfsFilesystem;
  18. }
  19. namespace Kernel {
  20. class ClientPort;
  21. class ServerPort;
  22. class ServerSession;
  23. class HLERequestContext;
  24. } // namespace Kernel
  25. namespace Service {
  26. namespace SM {
  27. class ServiceManager;
  28. }
  29. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  30. /// Arbitrary default number of maximum connections to an HLE service.
  31. static const u32 DefaultMaxSessions = 10;
  32. /**
  33. * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it
  34. * is not meant to be used directly.
  35. *
  36. * @see ServiceFramework
  37. */
  38. class ServiceFrameworkBase : public Kernel::SessionRequestHandler {
  39. public:
  40. /// Returns the string identifier used to connect to the service.
  41. std::string GetServiceName() const {
  42. return service_name;
  43. }
  44. /**
  45. * Returns the maximum number of sessions that can be connected to this service at the same
  46. * time.
  47. */
  48. u32 GetMaxSessions() const {
  49. return max_sessions;
  50. }
  51. /// Creates a port pair and registers this service with the given ServiceManager.
  52. void InstallAsService(SM::ServiceManager& service_manager);
  53. /// Creates a port pair and registers it on the kernel's global port registry.
  54. void InstallAsNamedPort();
  55. /// Creates and returns an unregistered port for the service.
  56. Kernel::SharedPtr<Kernel::ClientPort> CreatePort();
  57. void InvokeRequest(Kernel::HLERequestContext& ctx);
  58. ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) override;
  59. protected:
  60. /// Member-function pointer type of SyncRequest handlers.
  61. template <typename Self>
  62. using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
  63. private:
  64. template <typename T>
  65. friend class ServiceFramework;
  66. struct FunctionInfoBase {
  67. u32 expected_header;
  68. HandlerFnP<ServiceFrameworkBase> handler_callback;
  69. const char* name;
  70. };
  71. using InvokerFn = void(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  72. Kernel::HLERequestContext& ctx);
  73. ServiceFrameworkBase(const char* service_name, u32 max_sessions, InvokerFn* handler_invoker);
  74. ~ServiceFrameworkBase();
  75. void RegisterHandlersBase(const FunctionInfoBase* functions, std::size_t n);
  76. void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info);
  77. /// Identifier string used to connect to the service.
  78. std::string service_name;
  79. /// Maximum number of concurrent sessions that this service can handle.
  80. u32 max_sessions;
  81. /// Flag to store if a port was already create/installed to detect multiple install attempts,
  82. /// which is not supported.
  83. bool port_installed = false;
  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 <std::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, std::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, Core::System& system,
  156. FileSys::VfsFilesystem& vfs);
  157. /// Shutdown ServiceManager
  158. void Shutdown();
  159. } // namespace Service