service.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <mutex>
  7. #include <string>
  8. #include <boost/container/flat_map.hpp>
  9. #include "common/common_types.h"
  10. #include "common/spin_lock.h"
  11. #include "core/hle/kernel/hle_ipc.h"
  12. #include "core/hle/kernel/object.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Namespace Service
  15. namespace Core {
  16. class System;
  17. }
  18. namespace Kernel {
  19. class ClientPort;
  20. class ServerPort;
  21. class ServerSession;
  22. class HLERequestContext;
  23. } // namespace Kernel
  24. namespace Service {
  25. namespace FileSystem {
  26. class FileSystemController;
  27. }
  28. namespace NVFlinger {
  29. class NVFlinger;
  30. }
  31. namespace SM {
  32. class ServiceManager;
  33. }
  34. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  35. /// Arbitrary default number of maximum connections to an HLE service.
  36. static const u32 DefaultMaxSessions = 10;
  37. /**
  38. * This is an non-templated base of ServiceFramework to reduce code bloat and compilation times, it
  39. * is not meant to be used directly.
  40. *
  41. * @see ServiceFramework
  42. */
  43. class ServiceFrameworkBase : public Kernel::SessionRequestHandler {
  44. public:
  45. /// Returns the string identifier used to connect to the service.
  46. std::string GetServiceName() const {
  47. return service_name;
  48. }
  49. /**
  50. * Returns the maximum number of sessions that can be connected to this service at the same
  51. * time.
  52. */
  53. u32 GetMaxSessions() const {
  54. return max_sessions;
  55. }
  56. /// Creates a port pair and registers this service with the given ServiceManager.
  57. void InstallAsService(SM::ServiceManager& service_manager);
  58. /// Creates a port pair and registers it on the kernel's global port registry.
  59. void InstallAsNamedPort(Kernel::KernelCore& kernel);
  60. /// Invokes a service request routine.
  61. void InvokeRequest(Kernel::HLERequestContext& ctx);
  62. /// Handles a synchronization request for the service.
  63. ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) override;
  64. protected:
  65. /// Member-function pointer type of SyncRequest handlers.
  66. template <typename Self>
  67. using HandlerFnP = void (Self::*)(Kernel::HLERequestContext&);
  68. /// Used to gain exclusive access to the service members, e.g. from CoreTiming thread.
  69. [[nodiscard]] std::scoped_lock<Common::SpinLock> LockService() {
  70. return std::scoped_lock{lock_service};
  71. }
  72. /// System context that the service operates under.
  73. Core::System& system;
  74. private:
  75. template <typename T>
  76. friend class ServiceFramework;
  77. struct FunctionInfoBase {
  78. u32 expected_header;
  79. HandlerFnP<ServiceFrameworkBase> handler_callback;
  80. const char* name;
  81. };
  82. using InvokerFn = void(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  83. Kernel::HLERequestContext& ctx);
  84. explicit ServiceFrameworkBase(Core::System& system_, const char* service_name_,
  85. u32 max_sessions_, InvokerFn* handler_invoker_);
  86. ~ServiceFrameworkBase() override;
  87. void RegisterHandlersBase(const FunctionInfoBase* functions, std::size_t n);
  88. void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info);
  89. /// Identifier string used to connect to the service.
  90. std::string service_name;
  91. /// Maximum number of concurrent sessions that this service can handle.
  92. u32 max_sessions;
  93. /// Flag to store if a port was already create/installed to detect multiple install attempts,
  94. /// which is not supported.
  95. bool port_installed = false;
  96. /// Function used to safely up-cast pointers to the derived class before invoking a handler.
  97. InvokerFn* handler_invoker;
  98. boost::container::flat_map<u32, FunctionInfoBase> handlers;
  99. /// Used to gain exclusive access to the service members, e.g. from CoreTiming thread.
  100. Common::SpinLock lock_service;
  101. };
  102. /**
  103. * Framework for implementing HLE services. Dispatches on the header id of incoming SyncRequests
  104. * based on a table mapping header ids to handler functions. Service implementations should inherit
  105. * from ServiceFramework using the CRTP (`class Foo : public ServiceFramework<Foo> { ... };`) and
  106. * populate it with handlers by calling #RegisterHandlers.
  107. *
  108. * In order to avoid duplicating code in the binary and exposing too many implementation details in
  109. * the header, this class is split into a non-templated base (ServiceFrameworkBase) and a template
  110. * deriving from it (ServiceFramework). The functions in this class will mostly only erase the type
  111. * of the passed in function pointers and then delegate the actual work to the implementation in the
  112. * base class.
  113. */
  114. template <typename Self>
  115. class ServiceFramework : public ServiceFrameworkBase {
  116. protected:
  117. /// Contains information about a request type which is handled by the service.
  118. struct FunctionInfo : FunctionInfoBase {
  119. // TODO(yuriks): This function could be constexpr, but clang is the only compiler that
  120. // doesn't emit an ICE or a wrong diagnostic because of the static_cast.
  121. /**
  122. * Constructs a FunctionInfo for a function.
  123. *
  124. * @param expected_header request header in the command buffer which will trigger dispatch
  125. * to this handler
  126. * @param handler_callback member function in this service which will be called to handle
  127. * the request
  128. * @param name human-friendly name for the request. Used mostly for logging purposes.
  129. */
  130. FunctionInfo(u32 expected_header, HandlerFnP<Self> handler_callback, const char* name)
  131. : FunctionInfoBase{
  132. expected_header,
  133. // Type-erase member function pointer by casting it down to the base class.
  134. static_cast<HandlerFnP<ServiceFrameworkBase>>(handler_callback), name} {}
  135. };
  136. /**
  137. * Initializes the handler with no functions installed.
  138. *
  139. * @param system_ The system context to construct this service under.
  140. * @param service_name_ Name of the service.
  141. * @param max_sessions_ Maximum number of sessions that can be
  142. * connected to this service at the same time.
  143. */
  144. explicit ServiceFramework(Core::System& system_, const char* service_name_,
  145. u32 max_sessions_ = DefaultMaxSessions)
  146. : ServiceFrameworkBase(system_, service_name_, max_sessions_, Invoker) {}
  147. /// Registers handlers in the service.
  148. template <std::size_t N>
  149. void RegisterHandlers(const FunctionInfo (&functions)[N]) {
  150. RegisterHandlers(functions, N);
  151. }
  152. /**
  153. * Registers handlers in the service. Usually prefer using the other RegisterHandlers
  154. * overload in order to avoid needing to specify the array size.
  155. */
  156. void RegisterHandlers(const FunctionInfo* functions, std::size_t n) {
  157. RegisterHandlersBase(functions, n);
  158. }
  159. private:
  160. /**
  161. * This function is used to allow invocation of pointers to handlers stored in the base class
  162. * without needing to expose the type of this derived class. Pointers-to-member may require a
  163. * fixup when being up or downcast, and thus code that does that needs to know the concrete type
  164. * of the derived class in order to invoke one of it's functions through a pointer.
  165. */
  166. static void Invoker(ServiceFrameworkBase* object, HandlerFnP<ServiceFrameworkBase> member,
  167. Kernel::HLERequestContext& ctx) {
  168. // Cast back up to our original types and call the member function
  169. (static_cast<Self*>(object)->*static_cast<HandlerFnP<Self>>(member))(ctx);
  170. }
  171. };
  172. /**
  173. * The purpose of this class is to own any objects that need to be shared across the other service
  174. * implementations. Will be torn down when the global system instance is shutdown.
  175. */
  176. class Services final {
  177. public:
  178. explicit Services(std::shared_ptr<SM::ServiceManager>& sm, Core::System& system);
  179. ~Services();
  180. private:
  181. std::unique_ptr<NVFlinger::NVFlinger> nv_flinger;
  182. };
  183. } // namespace Service