ipc_helpers.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/client_port.h"
  11. #include "core/hle/kernel/client_session.h"
  12. #include "core/hle/kernel/domain.h"
  13. #include "core/hle/kernel/handle_table.h"
  14. #include "core/hle/kernel/hle_ipc.h"
  15. #include "core/hle/kernel/kernel.h"
  16. #include "core/hle/kernel/server_port.h"
  17. namespace IPC {
  18. class RequestHelperBase {
  19. protected:
  20. Kernel::HLERequestContext* context = nullptr;
  21. u32* cmdbuf;
  22. ptrdiff_t index = 0;
  23. public:
  24. RequestHelperBase(u32* command_buffer) : cmdbuf(command_buffer) {}
  25. RequestHelperBase(Kernel::HLERequestContext& context)
  26. : context(&context), cmdbuf(context.CommandBuffer()) {}
  27. void ValidateHeader() {
  28. // DEBUG_ASSERT_MSG(index == TotalSize(), "Operations do not match the header (cmd 0x%x)",
  29. // header.raw);
  30. }
  31. void Skip(unsigned size_in_words, bool set_to_null) {
  32. if (set_to_null)
  33. memset(cmdbuf + index, 0, size_in_words * sizeof(u32));
  34. index += size_in_words;
  35. }
  36. /**
  37. * Aligns the current position forward to a 16-byte boundary, padding with zeros.
  38. */
  39. void AlignWithPadding() {
  40. if (index & 3) {
  41. Skip(4 - (index & 3), true);
  42. }
  43. }
  44. unsigned GetCurrentOffset() const {
  45. return static_cast<unsigned>(index);
  46. }
  47. void SetCurrentOffset(unsigned offset) {
  48. index = static_cast<ptrdiff_t>(offset);
  49. }
  50. };
  51. class RequestBuilder : public RequestHelperBase {
  52. public:
  53. RequestBuilder(u32* command_buffer) : RequestHelperBase(command_buffer) {}
  54. RequestBuilder(Kernel::HLERequestContext& context, unsigned normal_params_size,
  55. u32 num_handles_to_copy = 0, u32 num_handles_to_move = 0,
  56. u32 num_domain_objects = 0)
  57. : RequestHelperBase(context) {
  58. memset(cmdbuf, 0, sizeof(u32) * IPC::COMMAND_BUFFER_LENGTH);
  59. context.ClearIncomingObjects();
  60. IPC::CommandHeader header{};
  61. // The entire size of the raw data section in u32 units, including the 16 bytes of mandatory
  62. // padding.
  63. u32 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size;
  64. if (context.IsDomain()) {
  65. raw_data_size += sizeof(DomainMessageHeader) / 4 + num_domain_objects;
  66. } else {
  67. // If we're not in a domain, turn the domain object parameters into move handles.
  68. num_handles_to_move += num_domain_objects;
  69. num_domain_objects = 0;
  70. }
  71. header.data_size.Assign(raw_data_size);
  72. if (num_handles_to_copy || num_handles_to_move) {
  73. header.enable_handle_descriptor.Assign(1);
  74. }
  75. PushRaw(header);
  76. if (header.enable_handle_descriptor) {
  77. IPC::HandleDescriptorHeader handle_descriptor_header{};
  78. handle_descriptor_header.num_handles_to_copy.Assign(num_handles_to_copy);
  79. handle_descriptor_header.num_handles_to_move.Assign(num_handles_to_move);
  80. PushRaw(handle_descriptor_header);
  81. Skip(num_handles_to_copy + num_handles_to_move, true);
  82. }
  83. AlignWithPadding();
  84. if (context.IsDomain()) {
  85. IPC::DomainMessageHeader domain_header{};
  86. domain_header.num_objects = num_domain_objects;
  87. PushRaw(domain_header);
  88. }
  89. IPC::DataPayloadHeader data_payload_header{};
  90. data_payload_header.magic = Common::MakeMagic('S', 'F', 'C', 'O');
  91. PushRaw(data_payload_header);
  92. }
  93. template <class T, class... Args>
  94. void PushIpcInterface(Args&&... args) {
  95. auto iface = std::make_shared<T>(std::forward<Args>(args)...);
  96. if (context->IsDomain()) {
  97. context->AddDomainObject(std::move(iface));
  98. } else {
  99. auto sessions = Kernel::ServerSession::CreateSessionPair(iface->GetServiceName());
  100. auto server = std::get<Kernel::SharedPtr<Kernel::ServerSession>>(sessions);
  101. auto client = std::get<Kernel::SharedPtr<Kernel::ClientSession>>(sessions);
  102. iface->ClientConnected(server);
  103. context->AddMoveObject(std::move(client));
  104. }
  105. }
  106. // Validate on destruction, as there shouldn't be any case where we don't want it
  107. ~RequestBuilder() {
  108. ValidateHeader();
  109. }
  110. template <typename T>
  111. void Push(T value);
  112. template <typename First, typename... Other>
  113. void Push(const First& first_value, const Other&... other_values);
  114. /**
  115. * @brief Copies the content of the given trivially copyable class to the buffer as a normal
  116. * param
  117. * @note: The input class must be correctly packed/padded to fit hardware layout.
  118. */
  119. template <typename T>
  120. void PushRaw(const T& value);
  121. template <typename... O>
  122. void PushMoveObjects(Kernel::SharedPtr<O>... pointers);
  123. template <typename... O>
  124. void PushCopyObjects(Kernel::SharedPtr<O>... pointers);
  125. };
  126. /// Push ///
  127. template <>
  128. inline void RequestBuilder::Push(u32 value) {
  129. cmdbuf[index++] = value;
  130. }
  131. template <typename T>
  132. void RequestBuilder::PushRaw(const T& value) {
  133. std::memcpy(cmdbuf + index, &value, sizeof(T));
  134. index += (sizeof(T) + 3) / 4; // round up to word length
  135. }
  136. template <>
  137. inline void RequestBuilder::Push(ResultCode value) {
  138. // Result codes are actually 64-bit in the IPC buffer, but only the high part is discarded.
  139. Push(value.raw);
  140. Push<u32>(0);
  141. }
  142. template <>
  143. inline void RequestBuilder::Push(u8 value) {
  144. PushRaw(value);
  145. }
  146. template <>
  147. inline void RequestBuilder::Push(u16 value) {
  148. PushRaw(value);
  149. }
  150. template <>
  151. inline void RequestBuilder::Push(u64 value) {
  152. Push(static_cast<u32>(value));
  153. Push(static_cast<u32>(value >> 32));
  154. }
  155. template <>
  156. inline void RequestBuilder::Push(bool value) {
  157. Push(static_cast<u8>(value));
  158. }
  159. template <typename First, typename... Other>
  160. void RequestBuilder::Push(const First& first_value, const Other&... other_values) {
  161. Push(first_value);
  162. Push(other_values...);
  163. }
  164. template <typename... O>
  165. inline void RequestBuilder::PushCopyObjects(Kernel::SharedPtr<O>... pointers) {
  166. auto objects = {pointers...};
  167. for (auto& object : objects) {
  168. context->AddCopyObject(std::move(object));
  169. }
  170. }
  171. template <typename... O>
  172. inline void RequestBuilder::PushMoveObjects(Kernel::SharedPtr<O>... pointers) {
  173. auto objects = {pointers...};
  174. for (auto& object : objects) {
  175. context->AddMoveObject(std::move(object));
  176. }
  177. }
  178. class RequestParser : public RequestHelperBase {
  179. public:
  180. RequestParser(u32* command_buffer) : RequestHelperBase(command_buffer) {}
  181. RequestParser(Kernel::HLERequestContext& context) : RequestHelperBase(context) {
  182. ASSERT_MSG(context.GetDataPayloadOffset(), "context is incomplete");
  183. Skip(context.GetDataPayloadOffset(), false);
  184. // Skip the u64 command id, it's already stored in the context
  185. static constexpr u32 CommandIdSize = 2;
  186. Skip(CommandIdSize, false);
  187. }
  188. RequestBuilder MakeBuilder(u32 normal_params_size, u32 num_handles_to_copy,
  189. u32 num_handles_to_move, u32 num_domain_objects,
  190. bool validate_header = true) {
  191. if (validate_header) {
  192. ValidateHeader();
  193. }
  194. return {*context, normal_params_size, num_handles_to_copy, num_handles_to_move,
  195. num_domain_objects};
  196. }
  197. template <typename T>
  198. T Pop();
  199. template <typename T>
  200. void Pop(T& value);
  201. template <typename First, typename... Other>
  202. void Pop(First& first_value, Other&... other_values);
  203. /**
  204. * @brief Reads the next normal parameters as a struct, by copying it
  205. * @note: The output class must be correctly packed/padded to fit hardware layout.
  206. */
  207. template <typename T>
  208. void PopRaw(T& value);
  209. /**
  210. * @brief Reads the next normal parameters as a struct, by copying it into a new value
  211. * @note: The output class must be correctly packed/padded to fit hardware layout.
  212. */
  213. template <typename T>
  214. T PopRaw();
  215. template <typename T>
  216. Kernel::SharedPtr<T> GetMoveObject(size_t index);
  217. template <typename T>
  218. Kernel::SharedPtr<T> GetCopyObject(size_t index);
  219. };
  220. /// Pop ///
  221. template <>
  222. inline u32 RequestParser::Pop() {
  223. return cmdbuf[index++];
  224. }
  225. template <typename T>
  226. void RequestParser::PopRaw(T& value) {
  227. std::memcpy(&value, cmdbuf + index, sizeof(T));
  228. index += (sizeof(T) + 3) / 4; // round up to word length
  229. }
  230. template <typename T>
  231. T RequestParser::PopRaw() {
  232. T value;
  233. PopRaw(value);
  234. return value;
  235. }
  236. template <>
  237. inline u8 RequestParser::Pop() {
  238. return PopRaw<u8>();
  239. }
  240. template <>
  241. inline u16 RequestParser::Pop() {
  242. return PopRaw<u16>();
  243. }
  244. template <>
  245. inline u64 RequestParser::Pop() {
  246. const u64 lsw = Pop<u32>();
  247. const u64 msw = Pop<u32>();
  248. return msw << 32 | lsw;
  249. }
  250. template <>
  251. inline s64 RequestParser::Pop() {
  252. return static_cast<s64>(Pop<u64>());
  253. }
  254. template <>
  255. inline bool RequestParser::Pop() {
  256. return Pop<u8>() != 0;
  257. }
  258. template <>
  259. inline ResultCode RequestParser::Pop() {
  260. return ResultCode{Pop<u32>()};
  261. }
  262. template <typename T>
  263. void RequestParser::Pop(T& value) {
  264. value = Pop<T>();
  265. }
  266. template <typename First, typename... Other>
  267. void RequestParser::Pop(First& first_value, Other&... other_values) {
  268. first_value = Pop<First>();
  269. Pop(other_values...);
  270. }
  271. template <typename T>
  272. Kernel::SharedPtr<T> RequestParser::GetMoveObject(size_t index) {
  273. return context->GetMoveObject<T>(index);
  274. }
  275. template <typename T>
  276. Kernel::SharedPtr<T> RequestParser::GetCopyObject(size_t index) {
  277. return context->GetCopyObject<T>(index);
  278. }
  279. } // namespace IPC