ipc_helpers.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 <array>
  6. #include <tuple>
  7. #include <type_traits>
  8. #include <utility>
  9. #include "core/hle/ipc.h"
  10. #include "core/hle/kernel/handle_table.h"
  11. #include "core/hle/kernel/hle_ipc.h"
  12. #include "core/hle/kernel/kernel.h"
  13. namespace IPC {
  14. class RequestHelperBase {
  15. protected:
  16. Kernel::HLERequestContext* context = nullptr;
  17. u32* cmdbuf;
  18. ptrdiff_t index = 1;
  19. Header header;
  20. public:
  21. RequestHelperBase(Kernel::HLERequestContext& context, Header desired_header)
  22. : context(&context), cmdbuf(context.CommandBuffer()), header(desired_header) {}
  23. RequestHelperBase(u32* command_buffer, Header command_header)
  24. : cmdbuf(command_buffer), header(command_header) {}
  25. /// Returns the total size of the request in words
  26. size_t TotalSize() const {
  27. return 1 /* command header */ + header.normal_params_size + header.translate_params_size;
  28. }
  29. void ValidateHeader() {
  30. DEBUG_ASSERT_MSG(index == TotalSize(), "Operations do not match the header (cmd 0x%x)",
  31. header.raw);
  32. }
  33. void Skip(unsigned size_in_words, bool set_to_null) {
  34. if (set_to_null)
  35. memset(cmdbuf + index, 0, size_in_words * sizeof(u32));
  36. index += size_in_words;
  37. }
  38. /**
  39. * @brief Retrieves the address of a static buffer, used when a buffer is needed for output
  40. * @param buffer_id The index of the static buffer
  41. * @param data_size If non-null, will store the size of the buffer
  42. */
  43. VAddr PeekStaticBuffer(u8 buffer_id, size_t* data_size = nullptr) const {
  44. u32* static_buffer = cmdbuf + Kernel::kStaticBuffersOffset / sizeof(u32) + buffer_id * 2;
  45. if (data_size)
  46. *data_size = StaticBufferDescInfo{static_buffer[0]}.size;
  47. return static_buffer[1];
  48. }
  49. };
  50. class RequestBuilder : public RequestHelperBase {
  51. public:
  52. RequestBuilder(Kernel::HLERequestContext& context, Header command_header)
  53. : RequestHelperBase(context, command_header) {
  54. cmdbuf[0] = header.raw;
  55. }
  56. RequestBuilder(Kernel::HLERequestContext& context, u16 command_id, unsigned normal_params_size,
  57. unsigned translate_params_size)
  58. : RequestBuilder(
  59. context, Header{MakeHeader(command_id, normal_params_size, translate_params_size)}) {}
  60. RequestBuilder(u32* command_buffer, Header command_header)
  61. : RequestHelperBase(command_buffer, command_header) {
  62. cmdbuf[0] = header.raw;
  63. }
  64. explicit RequestBuilder(u32* command_buffer, u32 command_header)
  65. : RequestBuilder(command_buffer, Header{command_header}) {}
  66. RequestBuilder(u32* command_buffer, u16 command_id, unsigned normal_params_size,
  67. unsigned translate_params_size)
  68. : RequestBuilder(command_buffer,
  69. MakeHeader(command_id, normal_params_size, translate_params_size)) {}
  70. // Validate on destruction, as there shouldn't be any case where we don't want it
  71. ~RequestBuilder() {
  72. ValidateHeader();
  73. }
  74. template <typename T>
  75. void Push(T value);
  76. template <typename First, typename... Other>
  77. void Push(const First& first_value, const Other&... other_values);
  78. /**
  79. * @brief Copies the content of the given trivially copyable class to the buffer as a normal
  80. * param
  81. * @note: The input class must be correctly packed/padded to fit hardware layout.
  82. */
  83. template <typename T>
  84. void PushRaw(const T& value);
  85. // TODO : ensure that translate params are added after all regular params
  86. template <typename... H>
  87. void PushCopyHandles(H... handles);
  88. template <typename... H>
  89. void PushMoveHandles(H... handles);
  90. template <typename... O>
  91. void PushObjects(Kernel::SharedPtr<O>... pointers);
  92. void PushCurrentPIDHandle();
  93. void PushStaticBuffer(VAddr buffer_vaddr, u32 size, u8 buffer_id);
  94. void PushMappedBuffer(VAddr buffer_vaddr, u32 size, MappedBufferPermissions perms);
  95. };
  96. /// Push ///
  97. template <>
  98. inline void RequestBuilder::Push(u32 value) {
  99. cmdbuf[index++] = value;
  100. }
  101. template <typename T>
  102. void RequestBuilder::PushRaw(const T& value) {
  103. static_assert(std::is_trivially_copyable<T>(), "Raw types should be trivially copyable");
  104. std::memcpy(cmdbuf + index, &value, sizeof(T));
  105. index += (sizeof(T) + 3) / 4; // round up to word length
  106. }
  107. template <>
  108. inline void RequestBuilder::Push(u8 value) {
  109. PushRaw(value);
  110. }
  111. template <>
  112. inline void RequestBuilder::Push(u16 value) {
  113. PushRaw(value);
  114. }
  115. template <>
  116. inline void RequestBuilder::Push(u64 value) {
  117. Push(static_cast<u32>(value));
  118. Push(static_cast<u32>(value >> 32));
  119. }
  120. template <>
  121. inline void RequestBuilder::Push(bool value) {
  122. Push(static_cast<u8>(value));
  123. }
  124. template <>
  125. inline void RequestBuilder::Push(ResultCode value) {
  126. Push(value.raw);
  127. }
  128. template <typename First, typename... Other>
  129. void RequestBuilder::Push(const First& first_value, const Other&... other_values) {
  130. Push(first_value);
  131. Push(other_values...);
  132. }
  133. template <typename... H>
  134. inline void RequestBuilder::PushCopyHandles(H... handles) {
  135. Push(CopyHandleDesc(sizeof...(H)));
  136. Push(static_cast<Kernel::Handle>(handles)...);
  137. }
  138. template <typename... H>
  139. inline void RequestBuilder::PushMoveHandles(H... handles) {
  140. Push(MoveHandleDesc(sizeof...(H)));
  141. Push(static_cast<Kernel::Handle>(handles)...);
  142. }
  143. template <typename... O>
  144. inline void RequestBuilder::PushObjects(Kernel::SharedPtr<O>... pointers) {
  145. PushMoveHandles(context->AddOutgoingHandle(std::move(pointers))...);
  146. }
  147. inline void RequestBuilder::PushCurrentPIDHandle() {
  148. Push(CallingPidDesc());
  149. Push(u32(0));
  150. }
  151. inline void RequestBuilder::PushStaticBuffer(VAddr buffer_vaddr, u32 size, u8 buffer_id) {
  152. Push(StaticBufferDesc(size, buffer_id));
  153. Push(buffer_vaddr);
  154. }
  155. inline void RequestBuilder::PushMappedBuffer(VAddr buffer_vaddr, u32 size,
  156. MappedBufferPermissions perms) {
  157. Push(MappedBufferDesc(size, perms));
  158. Push(buffer_vaddr);
  159. }
  160. class RequestParser : public RequestHelperBase {
  161. public:
  162. RequestParser(Kernel::HLERequestContext& context, Header desired_header)
  163. : RequestHelperBase(context, desired_header) {}
  164. RequestParser(Kernel::HLERequestContext& context, u16 command_id, unsigned normal_params_size,
  165. unsigned translate_params_size)
  166. : RequestParser(context,
  167. Header{MakeHeader(command_id, normal_params_size, translate_params_size)}) {
  168. }
  169. RequestParser(u32* command_buffer, Header command_header)
  170. : RequestHelperBase(command_buffer, command_header) {}
  171. explicit RequestParser(u32* command_buffer, u32 command_header)
  172. : RequestParser(command_buffer, Header{command_header}) {}
  173. RequestParser(u32* command_buffer, u16 command_id, unsigned normal_params_size,
  174. unsigned translate_params_size)
  175. : RequestParser(command_buffer,
  176. MakeHeader(command_id, normal_params_size, translate_params_size)) {}
  177. RequestBuilder MakeBuilder(u32 normal_params_size, u32 translate_params_size,
  178. bool validateHeader = true) {
  179. if (validateHeader)
  180. ValidateHeader();
  181. Header builderHeader{
  182. MakeHeader(header.command_id, normal_params_size, translate_params_size)};
  183. if (context != nullptr)
  184. return {*context, builderHeader};
  185. else
  186. return {cmdbuf, builderHeader};
  187. }
  188. template <typename T>
  189. T Pop();
  190. template <typename T>
  191. void Pop(T& value);
  192. template <typename First, typename... Other>
  193. void Pop(First& first_value, Other&... other_values);
  194. /// Equivalent to calling `PopHandles<1>()[0]`.
  195. Kernel::Handle PopHandle();
  196. /**
  197. * Pops a descriptor containing `N` handles. The handles are returned as an array. The
  198. * descriptor must contain exactly `N` handles, it is not permitted to, for example, call
  199. * PopHandles<1>() twice to read a multi-handle descriptor with 2 handles, or to make a single
  200. * PopHandles<2>() call to read 2 single-handle descriptors.
  201. */
  202. template <unsigned int N>
  203. std::array<Kernel::Handle, N> PopHandles();
  204. /// Convenience wrapper around PopHandles() which assigns the handles to the passed references.
  205. template <typename... H>
  206. void PopHandles(H&... handles) {
  207. std::tie(handles...) = PopHandles<sizeof...(H)>();
  208. }
  209. /// Equivalent to calling `PopGenericObjects<1>()[0]`.
  210. Kernel::SharedPtr<Kernel::Object> PopGenericObject();
  211. /// Equivalent to calling `std::get<0>(PopObjects<T>())`.
  212. template <typename T>
  213. Kernel::SharedPtr<T> PopObject();
  214. /**
  215. * Pop a descriptor containing `N` handles and resolves them to Kernel::Object pointers. If a
  216. * handle is invalid, null is returned for that object instead. The same caveats from
  217. * PopHandles() apply regarding `N` matching the number of handles in the descriptor.
  218. */
  219. template <unsigned int N>
  220. std::array<Kernel::SharedPtr<Kernel::Object>, N> PopGenericObjects();
  221. /**
  222. * Resolves handles to Kernel::Objects as in PopGenericsObjects(), but then also casts them to
  223. * the passed `T` types, while verifying that the cast is valid. If the type of an object does
  224. * not match, null is returned instead.
  225. */
  226. template <typename... T>
  227. std::tuple<Kernel::SharedPtr<T>...> PopObjects();
  228. /// Convenience wrapper around PopObjects() which assigns the handles to the passed references.
  229. template <typename... T>
  230. void PopObjects(Kernel::SharedPtr<T>&... pointers) {
  231. std::tie(pointers...) = PopObjects<T...>();
  232. }
  233. /**
  234. * @brief Pops the static buffer vaddr
  235. * @return The virtual address of the buffer
  236. * @param[out] data_size If non-null, the pointed value will be set to the size of the data
  237. * @param[out] useStaticBuffersToGetVaddr Indicates if we should read the vaddr from the static
  238. * buffers (which is the correct thing to do, but no service presently implement it) instead of
  239. * using the same value as the process who sent the request
  240. * given by the source process
  241. *
  242. * Static buffers must be set up before any IPC request using those is sent.
  243. * It is the duty of the process (usually services) to allocate and set up the receiving static
  244. * buffer information
  245. * Please note that the setup uses virtual addresses.
  246. */
  247. VAddr PopStaticBuffer(size_t* data_size = nullptr, bool useStaticBuffersToGetVaddr = false);
  248. /**
  249. * @brief Pops the mapped buffer vaddr
  250. * @return The virtual address of the buffer
  251. * @param[out] data_size If non-null, the pointed value will be set to the size of the data
  252. * given by the source process
  253. * @param[out] buffer_perms If non-null, the pointed value will be set to the permissions of the
  254. * buffer
  255. */
  256. VAddr PopMappedBuffer(size_t* data_size = nullptr,
  257. MappedBufferPermissions* buffer_perms = nullptr);
  258. /**
  259. * @brief Reads the next normal parameters as a struct, by copying it
  260. * @note: The output class must be correctly packed/padded to fit hardware layout.
  261. */
  262. template <typename T>
  263. void PopRaw(T& value);
  264. /**
  265. * @brief Reads the next normal parameters as a struct, by copying it into a new value
  266. * @note: The output class must be correctly packed/padded to fit hardware layout.
  267. */
  268. template <typename T>
  269. T PopRaw();
  270. };
  271. /// Pop ///
  272. template <>
  273. inline u32 RequestParser::Pop() {
  274. return cmdbuf[index++];
  275. }
  276. template <typename T>
  277. void RequestParser::PopRaw(T& value) {
  278. static_assert(std::is_trivially_copyable<T>(), "Raw types should be trivially copyable");
  279. std::memcpy(&value, cmdbuf + index, sizeof(T));
  280. index += (sizeof(T) + 3) / 4; // round up to word length
  281. }
  282. template <typename T>
  283. T RequestParser::PopRaw() {
  284. T value;
  285. PopRaw(value);
  286. return value;
  287. }
  288. template <>
  289. inline u8 RequestParser::Pop() {
  290. return PopRaw<u8>();
  291. }
  292. template <>
  293. inline u16 RequestParser::Pop() {
  294. return PopRaw<u16>();
  295. }
  296. template <>
  297. inline u64 RequestParser::Pop() {
  298. const u64 lsw = Pop<u32>();
  299. const u64 msw = Pop<u32>();
  300. return msw << 32 | lsw;
  301. }
  302. template <>
  303. inline bool RequestParser::Pop() {
  304. return Pop<u8>() != 0;
  305. }
  306. template <>
  307. inline ResultCode RequestParser::Pop() {
  308. return ResultCode{Pop<u32>()};
  309. }
  310. template <typename T>
  311. void RequestParser::Pop(T& value) {
  312. value = Pop<T>();
  313. }
  314. template <typename First, typename... Other>
  315. void RequestParser::Pop(First& first_value, Other&... other_values) {
  316. first_value = Pop<First>();
  317. Pop(other_values...);
  318. }
  319. inline Kernel::Handle RequestParser::PopHandle() {
  320. const u32 handle_descriptor = Pop<u32>();
  321. DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
  322. "Tried to pop handle(s) but the descriptor is not a handle descriptor");
  323. DEBUG_ASSERT_MSG(HandleNumberFromDesc(handle_descriptor) == 1,
  324. "Descriptor indicates that there isn't exactly one handle");
  325. return Pop<Kernel::Handle>();
  326. }
  327. template <unsigned int N>
  328. std::array<Kernel::Handle, N> RequestParser::PopHandles() {
  329. u32 handle_descriptor = Pop<u32>();
  330. ASSERT_MSG(IsHandleDescriptor(handle_descriptor),
  331. "Tried to pop handle(s) but the descriptor is not a handle descriptor");
  332. ASSERT_MSG(N == HandleNumberFromDesc(handle_descriptor),
  333. "Number of handles doesn't match the descriptor");
  334. std::array<Kernel::Handle, N> handles{};
  335. for (Kernel::Handle& handle : handles) {
  336. handle = Pop<Kernel::Handle>();
  337. }
  338. return handles;
  339. }
  340. inline Kernel::SharedPtr<Kernel::Object> RequestParser::PopGenericObject() {
  341. Kernel::Handle handle = PopHandle();
  342. return context->GetIncomingHandle(handle);
  343. }
  344. template <typename T>
  345. Kernel::SharedPtr<T> RequestParser::PopObject() {
  346. return Kernel::DynamicObjectCast<T>(PopGenericObject());
  347. }
  348. template <unsigned int N>
  349. inline std::array<Kernel::SharedPtr<Kernel::Object>, N> RequestParser::PopGenericObjects() {
  350. std::array<Kernel::Handle, N> handles = PopHandles<N>();
  351. std::array<Kernel::SharedPtr<Kernel::Object>, N> pointers;
  352. for (int i = 0; i < N; ++i) {
  353. pointers[i] = context->GetIncomingHandle(handles[i]);
  354. }
  355. return pointers;
  356. }
  357. namespace detail {
  358. template <typename... T, size_t... I>
  359. std::tuple<Kernel::SharedPtr<T>...> PopObjectsHelper(
  360. std::array<Kernel::SharedPtr<Kernel::Object>, sizeof...(T)>&& pointers,
  361. std::index_sequence<I...>) {
  362. return std::make_tuple(Kernel::DynamicObjectCast<T>(std::move(pointers[I]))...);
  363. }
  364. } // namespace detail
  365. template <typename... T>
  366. inline std::tuple<Kernel::SharedPtr<T>...> RequestParser::PopObjects() {
  367. return detail::PopObjectsHelper<T...>(PopGenericObjects<sizeof...(T)>(),
  368. std::index_sequence_for<T...>{});
  369. }
  370. inline VAddr RequestParser::PopStaticBuffer(size_t* data_size, bool useStaticBuffersToGetVaddr) {
  371. const u32 sbuffer_descriptor = Pop<u32>();
  372. StaticBufferDescInfo bufferInfo{sbuffer_descriptor};
  373. if (data_size != nullptr)
  374. *data_size = bufferInfo.size;
  375. if (!useStaticBuffersToGetVaddr)
  376. return Pop<VAddr>();
  377. else {
  378. ASSERT_MSG(0, "remove the assert if multiprocess/IPC translation are implemented.");
  379. // The buffer has already been copied to the static buffer by the kernel during
  380. // translation
  381. Pop<VAddr>(); // Pop the calling process buffer address
  382. // and get the vaddr from the static buffers
  383. return cmdbuf[(0x100 >> 2) + bufferInfo.buffer_id * 2 + 1];
  384. }
  385. }
  386. inline VAddr RequestParser::PopMappedBuffer(size_t* data_size,
  387. MappedBufferPermissions* buffer_perms) {
  388. const u32 sbuffer_descriptor = Pop<u32>();
  389. MappedBufferDescInfo bufferInfo{sbuffer_descriptor};
  390. if (data_size != nullptr)
  391. *data_size = bufferInfo.size;
  392. if (buffer_perms != nullptr)
  393. *buffer_perms = bufferInfo.perms;
  394. return Pop<VAddr>();
  395. }
  396. } // namespace IPC