service.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 port
  147. /// TODO(Subv): Write documentation for this class
  148. class SessionRequestHandler {
  149. public:
  150. /**
  151. * Dispatches and handles a sync request from the emulated application.
  152. * @param server_session The ServerSession that was triggered for this sync request,
  153. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  154. * TODO(Subv): Make a HandleSyncRequestParent function that is called from the outside and does { ReturnIfError(Translate()); HandleSyncRequest(); }
  155. * The Translate() function would copy the command buffer from the ServerSession thread's TLS into a temporary buffer, and pass it to HandleSyncRequest.
  156. * TODO(Subv): HandleSyncRequest's return type should be void.
  157. */
  158. virtual ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) = 0;
  159. };
  160. /// Interface to a CTROS service
  161. class Interface : public SessionRequestHandler {
  162. public:
  163. std::string GetName() const {
  164. return GetPortName();
  165. }
  166. virtual void SetVersion(u32 raw_version) {
  167. version.raw = raw_version;
  168. }
  169. virtual ~Interface() {}
  170. /**
  171. * Gets the maximum allowed number of sessions that can be connected to this port at the same time.
  172. * It should be overwritten by each service implementation for more fine-grained control.
  173. * @returns The maximum number of connections allowed.
  174. */
  175. virtual u32 GetMaxSessions() const { return DefaultMaxSessions; }
  176. typedef void (*Function)(Interface*);
  177. struct FunctionInfo {
  178. u32 id;
  179. Function func;
  180. const char* name;
  181. };
  182. /**
  183. * Gets the string name used by CTROS for a service
  184. * @return Port name of service
  185. */
  186. virtual std::string GetPortName() const {
  187. return "[UNKNOWN SERVICE PORT]";
  188. }
  189. ResultCode HandleSyncRequest(Kernel::SharedPtr<Kernel::ServerSession> server_session) override;
  190. protected:
  191. /**
  192. * Registers the functions in the service
  193. */
  194. template <size_t N>
  195. inline void Register(const FunctionInfo (&functions)[N]) {
  196. Register(functions, N);
  197. }
  198. void Register(const FunctionInfo* functions, size_t n);
  199. union {
  200. u32 raw;
  201. BitField<0, 8, u32> major;
  202. BitField<8, 8, u32> minor;
  203. BitField<16, 8, u32> build;
  204. BitField<24, 8, u32> revision;
  205. } version = {};
  206. private:
  207. boost::container::flat_map<u32, FunctionInfo> m_functions;
  208. };
  209. /// Initialize ServiceManager
  210. void Init();
  211. /// Shutdown ServiceManager
  212. void Shutdown();
  213. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  214. extern std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_kernel_named_ports;
  215. /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
  216. extern std::unordered_map<std::string, std::tuple<Kernel::SharedPtr<Kernel::ClientPort>, std::shared_ptr<Interface>>> g_srv_services;
  217. /// Adds a service to the services table
  218. void AddService(Interface* interface_);
  219. } // namespace