ipc_helpers.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "core/hle/ipc.h"
  6. #include "core/hle/kernel/kernel.h"
  7. namespace IPC {
  8. class RequestHelperBase {
  9. protected:
  10. u32* cmdbuf;
  11. ptrdiff_t index = 1;
  12. Header header;
  13. public:
  14. RequestHelperBase(u32* command_buffer, Header command_header)
  15. : cmdbuf(command_buffer), header(command_header) {}
  16. /// Returns the total size of the request in words
  17. size_t TotalSize() const {
  18. return 1 /* command header */ + header.normal_params_size + header.translate_params_size;
  19. }
  20. void ValidateHeader() {
  21. DEBUG_ASSERT_MSG(index == TotalSize(), "Operations do not match the header (cmd 0x%x)",
  22. header.raw);
  23. }
  24. /**
  25. * @brief Retrieves the address of a static buffer, used when a buffer is needed for output
  26. * @param buffer_id The index of the static buffer
  27. * @param data_size If non-null, will store the size of the buffer
  28. */
  29. VAddr PeekStaticBuffer(u8 buffer_id, size_t* data_size = nullptr) const {
  30. u32* static_buffer = cmdbuf + Kernel::kStaticBuffersOffset / 4 + buffer_id * 2;
  31. if (data_size)
  32. *data_size = StaticBufferDescInfo{static_buffer[0]}.size;
  33. return static_buffer[1];
  34. }
  35. };
  36. class RequestBuilder : public RequestHelperBase {
  37. public:
  38. RequestBuilder(u32* command_buffer, Header command_header)
  39. : RequestHelperBase(command_buffer, command_header) {
  40. cmdbuf[0] = header.raw;
  41. }
  42. explicit RequestBuilder(u32* command_buffer, u32 command_header)
  43. : RequestBuilder(command_buffer, Header{command_header}) {}
  44. RequestBuilder(u32* command_buffer, u16 command_id, unsigned normal_params_size,
  45. unsigned translate_params_size)
  46. : RequestBuilder(command_buffer,
  47. MakeHeader(command_id, normal_params_size, translate_params_size)) {}
  48. // Validate on destruction, as there shouldn't be any case where we don't want it
  49. ~RequestBuilder() {
  50. ValidateHeader();
  51. }
  52. template <typename T>
  53. void Push(T value);
  54. void Push(u32 value) {
  55. cmdbuf[index++] = value;
  56. }
  57. template <typename First, typename... Other>
  58. void Push(const First& first_value, const Other&... other_values) {
  59. Push(first_value);
  60. Push(other_values...);
  61. }
  62. /**
  63. * @brief Copies the content of the given trivially copyable class to the buffer as a normal
  64. * param
  65. * @note: The input class must be correctly packed/padded to fit hardware layout.
  66. */
  67. template <typename T>
  68. void PushRaw(const T& value) {
  69. static_assert(std::is_trivially_copyable<T>(), "Raw types should be trivially copyable");
  70. std::memcpy(cmdbuf + index, &value, sizeof(T));
  71. index += (sizeof(T) + 3) / 4; // round up to word length
  72. }
  73. // TODO : ensure that translate params are added after all regular params
  74. template <typename... H>
  75. void PushCopyHandles(H... handles) {
  76. Push(CopyHandleDesc(sizeof...(H)));
  77. Push(static_cast<Kernel::Handle>(handles)...);
  78. }
  79. template <typename... H>
  80. void PushMoveHandles(H... handles) {
  81. Push(MoveHandleDesc(sizeof...(H)));
  82. Push(static_cast<Kernel::Handle>(handles)...);
  83. }
  84. void PushCurrentPIDHandle() {
  85. Push(CallingPidDesc());
  86. Push(u32(0));
  87. }
  88. void PushStaticBuffer(VAddr buffer_vaddr, u32 size, u8 buffer_id) {
  89. Push(StaticBufferDesc(size, buffer_id));
  90. Push(buffer_vaddr);
  91. }
  92. void PushMappedBuffer(VAddr buffer_vaddr, u32 size, MappedBufferPermissions perms) {
  93. Push(MappedBufferDesc(size, perms));
  94. Push(buffer_vaddr);
  95. }
  96. };
  97. /// Push ///
  98. template <>
  99. inline void RequestBuilder::Push<u32>(u32 value) {
  100. Push(value);
  101. }
  102. template <>
  103. inline void RequestBuilder::Push<u64>(u64 value) {
  104. Push(static_cast<u32>(value));
  105. Push(static_cast<u32>(value >> 32));
  106. }
  107. template <>
  108. inline void RequestBuilder::Push<ResultCode>(ResultCode value) {
  109. Push(value.raw);
  110. }
  111. class RequestParser : public RequestHelperBase {
  112. public:
  113. RequestParser(u32* command_buffer, Header command_header)
  114. : RequestHelperBase(command_buffer, command_header) {}
  115. explicit RequestParser(u32* command_buffer, u32 command_header)
  116. : RequestParser(command_buffer, Header{command_header}) {}
  117. RequestParser(u32* command_buffer, u16 command_id, unsigned normal_params_size,
  118. unsigned translate_params_size)
  119. : RequestParser(command_buffer,
  120. MakeHeader(command_id, normal_params_size, translate_params_size)) {}
  121. RequestBuilder MakeBuilder(u32 normal_params_size, u32 translate_params_size,
  122. bool validateHeader = true) {
  123. if (validateHeader)
  124. ValidateHeader();
  125. Header builderHeader{
  126. MakeHeader(header.command_id, normal_params_size, translate_params_size)};
  127. return {cmdbuf, builderHeader};
  128. }
  129. template <typename T>
  130. T Pop();
  131. template <typename T>
  132. void Pop(T& value);
  133. template <typename First, typename... Other>
  134. void Pop(First& first_value, Other&... other_values);
  135. Kernel::Handle PopHandle();
  136. template <typename... H>
  137. void PopHandles(H&... handles);
  138. /**
  139. * @brief Pops the static buffer vaddr
  140. * @return The virtual address of the buffer
  141. * @param[out] data_size If non-null, the pointed value will be set to the size of the data
  142. * @param[out] useStaticBuffersToGetVaddr Indicates if we should read the vaddr from the static
  143. * buffers (which is the correct thing to do, but no service presently implement it) instead of
  144. * using the same value as the process who sent the request
  145. * given by the source process
  146. *
  147. * Static buffers must be set up before any IPC request using those is sent.
  148. * It is the duty of the process (usually services) to allocate and set up the receiving static
  149. * buffer information
  150. * Please note that the setup uses virtual addresses.
  151. */
  152. VAddr PopStaticBuffer(size_t* data_size = nullptr, bool useStaticBuffersToGetVaddr = false);
  153. /**
  154. * @brief Pops the mapped buffer vaddr
  155. * @return The virtual address of the buffer
  156. * @param[out] data_size If non-null, the pointed value will be set to the size of the data
  157. * given by the source process
  158. * @param[out] buffer_perms If non-null, the pointed value will be set to the permissions of the
  159. * buffer
  160. */
  161. VAddr PopMappedBuffer(size_t* data_size = nullptr,
  162. MappedBufferPermissions* buffer_perms = nullptr);
  163. /**
  164. * @brief Reads the next normal parameters as a struct, by copying it
  165. * @note: The output class must be correctly packed/padded to fit hardware layout.
  166. */
  167. template <typename T>
  168. void PopRaw(T& value);
  169. };
  170. /// Pop ///
  171. template <>
  172. inline u32 RequestParser::Pop<u32>() {
  173. return cmdbuf[index++];
  174. }
  175. template <>
  176. inline u64 RequestParser::Pop<u64>() {
  177. const u64 lsw = Pop<u32>();
  178. const u64 msw = Pop<u32>();
  179. return msw << 32 | lsw;
  180. }
  181. template <>
  182. inline ResultCode RequestParser::Pop<ResultCode>() {
  183. return ResultCode{Pop<u32>()};
  184. }
  185. template <typename T>
  186. void RequestParser::Pop(T& value) {
  187. value = Pop<T>();
  188. }
  189. template <typename First, typename... Other>
  190. void RequestParser::Pop(First& first_value, Other&... other_values) {
  191. first_value = Pop<First>();
  192. Pop(other_values...);
  193. }
  194. inline Kernel::Handle RequestParser::PopHandle() {
  195. const u32 handle_descriptor = Pop<u32>();
  196. DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
  197. "Tried to pop handle(s) but the descriptor is not a handle descriptor");
  198. DEBUG_ASSERT_MSG(HandleNumberFromDesc(handle_descriptor) == 1,
  199. "Descriptor indicates that there isn't exactly one handle");
  200. return Pop<Kernel::Handle>();
  201. }
  202. template <typename... H>
  203. void RequestParser::PopHandles(H&... handles) {
  204. const u32 handle_descriptor = Pop<u32>();
  205. const int handles_number = sizeof...(H);
  206. DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
  207. "Tried to pop handle(s) but the descriptor is not a handle descriptor");
  208. DEBUG_ASSERT_MSG(handles_number == HandleNumberFromDesc(handle_descriptor),
  209. "Number of handles doesn't match the descriptor");
  210. Pop(static_cast<Kernel::Handle&>(handles)...);
  211. }
  212. inline VAddr RequestParser::PopStaticBuffer(size_t* data_size, bool useStaticBuffersToGetVaddr) {
  213. const u32 sbuffer_descriptor = Pop<u32>();
  214. StaticBufferDescInfo bufferInfo{sbuffer_descriptor};
  215. if (data_size != nullptr)
  216. *data_size = bufferInfo.size;
  217. if (!useStaticBuffersToGetVaddr)
  218. return Pop<VAddr>();
  219. else {
  220. ASSERT_MSG(0, "remove the assert if multiprocess/IPC translation are implemented.");
  221. // The buffer has already been copied to the static buffer by the kernel during
  222. // translation
  223. Pop<VAddr>(); // Pop the calling process buffer address
  224. // and get the vaddr from the static buffers
  225. return cmdbuf[(0x100 >> 2) + bufferInfo.buffer_id * 2 + 1];
  226. }
  227. }
  228. inline VAddr RequestParser::PopMappedBuffer(size_t* data_size,
  229. MappedBufferPermissions* buffer_perms) {
  230. const u32 sbuffer_descriptor = Pop<u32>();
  231. MappedBufferDescInfo bufferInfo{sbuffer_descriptor};
  232. if (data_size != nullptr)
  233. *data_size = bufferInfo.size;
  234. if (buffer_perms != nullptr)
  235. *buffer_perms = bufferInfo.perms;
  236. return Pop<VAddr>();
  237. }
  238. template <typename T>
  239. void RequestParser::PopRaw(T& value) {
  240. static_assert(std::is_trivially_copyable<T>(), "Raw types should be trivially copyable");
  241. std::memcpy(&value, cmdbuf + index, sizeof(T));
  242. index += (sizeof(T) + 3) / 4; // round up to word length
  243. }
  244. } // namespace IPC