service.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/common_types.h"
  10. #include "core/hle/kernel/client_port.h"
  11. #include "core/hle/kernel/thread.h"
  12. #include "core/hle/result.h"
  13. #include "core/memory.h"
  14. namespace Kernel {
  15. class ServerSession;
  16. // TODO(Subv): Move these declarations out of here
  17. static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
  18. /**
  19. * Returns a pointer to the command buffer in the current thread's TLS
  20. * TODO(Subv): This is not entirely correct, the command buffer should be copied from
  21. * the thread's TLS to an intermediate buffer in kernel memory, and then copied again to
  22. * the service handler process' memory.
  23. * @param offset Optional offset into command buffer
  24. * @return Pointer to command buffer
  25. */
  26. inline u32* GetCommandBuffer(const int offset = 0) {
  27. return (u32*)Memory::GetPointer(GetCurrentThread()->GetTLSAddress() + kCommandHeaderOffset +
  28. offset);
  29. }
  30. }
  31. // TODO(Subv): Move this namespace out of here
  32. namespace IPC {
  33. enum DescriptorType : u32 {
  34. // Buffer related desciptors types (mask : 0x0F)
  35. StaticBuffer = 0x02,
  36. PXIBuffer = 0x04,
  37. MappedBuffer = 0x08,
  38. // Handle related descriptors types (mask : 0x30, but need to check for buffer related
  39. // descriptors first )
  40. CopyHandle = 0x00,
  41. MoveHandle = 0x10,
  42. CallingPid = 0x20,
  43. };
  44. /**
  45. * @brief Creates a command header to be used for IPC
  46. * @param command_id ID of the command to create a header for.
  47. * @param normal_params Size of the normal parameters in words. Up to 63.
  48. * @param translate_params_size Size of the translate parameters in words. Up to 63.
  49. * @return The created IPC header.
  50. *
  51. * Normal parameters are sent directly to the process while the translate parameters might go
  52. * through modifications and checks by the kernel.
  53. * The translate parameters are described by headers generated with the IPC::*Desc functions.
  54. *
  55. * @note While #normal_params is equivalent to the number of normal parameters,
  56. * #translate_params_size includes the size occupied by the translate parameters headers.
  57. */
  58. constexpr u32 MakeHeader(u16 command_id, unsigned int normal_params,
  59. unsigned int translate_params_size) {
  60. return (u32(command_id) << 16) | ((u32(normal_params) & 0x3F) << 6) |
  61. (u32(translate_params_size) & 0x3F);
  62. }
  63. union Header {
  64. u32 raw;
  65. BitField<0, 6, u32> translate_params_size;
  66. BitField<6, 6, u32> normal_params;
  67. BitField<16, 16, u32> command_id;
  68. };
  69. inline Header ParseHeader(u32 header) {
  70. return {header};
  71. }
  72. constexpr u32 MoveHandleDesc(u32 num_handles = 1) {
  73. return MoveHandle | ((num_handles - 1) << 26);
  74. }
  75. constexpr u32 CopyHandleDesc(u32 num_handles = 1) {
  76. return CopyHandle | ((num_handles - 1) << 26);
  77. }
  78. constexpr u32 CallingPidDesc() {
  79. return CallingPid;
  80. }
  81. constexpr bool isHandleDescriptor(u32 descriptor) {
  82. return (descriptor & 0xF) == 0x0;
  83. }
  84. constexpr u32 HandleNumberFromDesc(u32 handle_descriptor) {
  85. return (handle_descriptor >> 26) + 1;
  86. }
  87. constexpr u32 StaticBufferDesc(u32 size, u8 buffer_id) {
  88. return StaticBuffer | (size << 14) | ((buffer_id & 0xF) << 10);
  89. }
  90. union StaticBufferDescInfo {
  91. u32 raw;
  92. BitField<10, 4, u32> buffer_id;
  93. BitField<14, 18, u32> size;
  94. };
  95. inline StaticBufferDescInfo ParseStaticBufferDesc(const u32 desc) {
  96. return {desc};
  97. }
  98. /**
  99. * @brief Creates a header describing a buffer to be sent over PXI.
  100. * @param size Size of the buffer. Max 0x00FFFFFF.
  101. * @param buffer_id The Id of the buffer. Max 0xF.
  102. * @param is_read_only true if the buffer is read-only. If false, the buffer is considered to have
  103. * read-write access.
  104. * @return The created PXI buffer header.
  105. *
  106. * The next value is a phys-address of a table located in the BASE memregion.
  107. */
  108. inline u32 PXIBufferDesc(u32 size, unsigned buffer_id, bool is_read_only) {
  109. u32 type = PXIBuffer;
  110. if (is_read_only)
  111. type |= 0x2;
  112. return type | (size << 8) | ((buffer_id & 0xF) << 4);
  113. }
  114. enum MappedBufferPermissions {
  115. R = 1,
  116. W = 2,
  117. RW = R | W,
  118. };
  119. constexpr u32 MappedBufferDesc(u32 size, MappedBufferPermissions perms) {
  120. return MappedBuffer | (size << 4) | (u32(perms) << 1);
  121. }
  122. union MappedBufferDescInfo {
  123. u32 raw;
  124. BitField<4, 28, u32> size;
  125. BitField<1, 2, MappedBufferPermissions> perms;
  126. };
  127. inline MappedBufferDescInfo ParseMappedBufferDesc(const u32 desc) {
  128. return{ desc };
  129. }
  130. inline DescriptorType GetDescriptorType(u32 descriptor) {
  131. // Note: Those checks must be done in this order
  132. if (isHandleDescriptor(descriptor))
  133. return (DescriptorType)(descriptor & 0x30);
  134. // handle the fact that the following descriptors can have rights
  135. if (descriptor & MappedBuffer)
  136. return MappedBuffer;
  137. if (descriptor & PXIBuffer)
  138. return PXIBuffer;
  139. return StaticBuffer;
  140. }
  141. } // namespace IPC
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////
  143. // Namespace Service
  144. namespace Service {
  145. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  146. static const u32 DefaultMaxSessions = 10; ///< Arbitrary default number of maximum connections to an HLE service
  147. /**
  148. * Interface implemented by HLE Session handlers.
  149. * This can be provided to a ServerSession in order to hook into several relevant events (such as a new connection or a SyncRequest)
  150. * so they can be implemented in the emulator.
  151. */
  152. class SessionRequestHandler {
  153. public:
  154. /**
  155. * Dispatches and handles a sync request from the emulated application.
  156. * @param server_session The ServerSession that was triggered for this sync request,
  157. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  158. * @returns ResultCode the result code of the translate operation.
  159. */
  160. ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session);
  161. protected:
  162. /**
  163. * Handles a sync request from the emulated application and writes the response to the command buffer.
  164. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  165. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  166. */
  167. virtual void HandleSyncRequestImpl(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
  168. private:
  169. /**
  170. * Performs command buffer translation for this request.
  171. * The command buffer from the ServerSession thread's TLS is copied into a
  172. * buffer and all descriptors in the buffer are processed.
  173. * TODO(Subv): Implement this function, currently we do not support multiple processes running at once,
  174. * but once that is implemented we'll need to properly translate all descriptors in the command buffer.
  175. */
  176. ResultCode TranslateRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session);
  177. };
  178. /**
  179. * Framework for implementing HLE service handlers which dispatch incoming SyncRequests based on a table mapping header ids to handler functions.
  180. */
  181. class Interface : public SessionRequestHandler {
  182. public:
  183. /**
  184. * Creates an HLE interface with the specified max sessions.
  185. * @param max_sessions Maximum number of sessions that can be
  186. * connected to this service at the same time.
  187. */
  188. Interface(u32 max_sessions = DefaultMaxSessions) : max_sessions(max_sessions) { }
  189. virtual ~Interface() = default;
  190. std::string GetName() const {
  191. return GetPortName();
  192. }
  193. virtual void SetVersion(u32 raw_version) {
  194. version.raw = raw_version;
  195. }
  196. /**
  197. * Gets the maximum allowed number of sessions that can be connected to this service at the same time.
  198. * @returns The maximum number of connections allowed.
  199. */
  200. u32 GetMaxSessions() const { return max_sessions; }
  201. typedef void (*Function)(Interface*);
  202. struct FunctionInfo {
  203. u32 id;
  204. Function func;
  205. const char* name;
  206. };
  207. /**
  208. * Gets the string name used by CTROS for a service
  209. * @return Port name of service
  210. */
  211. virtual std::string GetPortName() const {
  212. return "[UNKNOWN SERVICE PORT]";
  213. }
  214. protected:
  215. void HandleSyncRequestImpl(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
  216. /**
  217. * Registers the functions in the service
  218. */
  219. template <size_t N>
  220. inline void Register(const FunctionInfo (&functions)[N]) {
  221. Register(functions, N);
  222. }
  223. void Register(const FunctionInfo* functions, size_t n);
  224. union {
  225. u32 raw;
  226. BitField<0, 8, u32> major;
  227. BitField<8, 8, u32> minor;
  228. BitField<16, 8, u32> build;
  229. BitField<24, 8, u32> revision;
  230. } version = {};
  231. private:
  232. u32 max_sessions; ///< Maximum number of concurrent sessions that this service can handle.
  233. boost::container::flat_map<u32, FunctionInfo> m_functions;
  234. };
  235. /// Initialize ServiceManager
  236. void Init();
  237. /// Shutdown ServiceManager
  238. void Shutdown();
  239. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  240. extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_kernel_named_ports;
  241. /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
  242. extern std::unordered_map<std::string, Kernel::SharedPtr<Kernel::ClientPort>> g_srv_services;
  243. /// Adds a service to the services table
  244. void AddService(Interface* interface_);
  245. } // namespace