ipc_helpers.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <cstring>
  7. #include <memory>
  8. #include <tuple>
  9. #include <type_traits>
  10. #include <utility>
  11. #include "common/assert.h"
  12. #include "common/common_types.h"
  13. #include "core/core.h"
  14. #include "core/hle/ipc.h"
  15. #include "core/hle/kernel/client_port.h"
  16. #include "core/hle/kernel/client_session.h"
  17. #include "core/hle/kernel/hle_ipc.h"
  18. #include "core/hle/kernel/object.h"
  19. #include "core/hle/kernel/server_session.h"
  20. #include "core/hle/kernel/session.h"
  21. #include "core/hle/result.h"
  22. namespace IPC {
  23. constexpr ResultCode ERR_REMOTE_PROCESS_DEAD{ErrorModule::HIPC, 301};
  24. class RequestHelperBase {
  25. protected:
  26. Kernel::HLERequestContext* context = nullptr;
  27. u32* cmdbuf;
  28. ptrdiff_t index = 0;
  29. public:
  30. explicit RequestHelperBase(u32* command_buffer) : cmdbuf(command_buffer) {}
  31. explicit RequestHelperBase(Kernel::HLERequestContext& context)
  32. : context(&context), cmdbuf(context.CommandBuffer()) {}
  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. * Aligns the current position forward to a 16-byte boundary, padding with zeros.
  40. */
  41. void AlignWithPadding() {
  42. if (index & 3) {
  43. Skip(4 - (index & 3), true);
  44. }
  45. }
  46. unsigned GetCurrentOffset() const {
  47. return static_cast<unsigned>(index);
  48. }
  49. void SetCurrentOffset(unsigned offset) {
  50. index = static_cast<ptrdiff_t>(offset);
  51. }
  52. };
  53. class ResponseBuilder : public RequestHelperBase {
  54. public:
  55. /// Flags used for customizing the behavior of ResponseBuilder
  56. enum class Flags : u32 {
  57. None = 0,
  58. /// Uses move handles to move objects in the response, even when in a domain. This is
  59. /// required when PushMoveObjects is used.
  60. AlwaysMoveHandles = 1,
  61. };
  62. explicit ResponseBuilder(u32* command_buffer) : RequestHelperBase(command_buffer) {}
  63. explicit ResponseBuilder(Kernel::HLERequestContext& context, u32 normal_params_size,
  64. u32 num_handles_to_copy = 0, u32 num_objects_to_move = 0,
  65. Flags flags = Flags::None)
  66. : RequestHelperBase(context), normal_params_size(normal_params_size),
  67. num_handles_to_copy(num_handles_to_copy), num_objects_to_move(num_objects_to_move) {
  68. memset(cmdbuf, 0, sizeof(u32) * IPC::COMMAND_BUFFER_LENGTH);
  69. context.ClearIncomingObjects();
  70. IPC::CommandHeader header{};
  71. // The entire size of the raw data section in u32 units, including the 16 bytes of mandatory
  72. // padding.
  73. u32 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size;
  74. u32 num_handles_to_move{};
  75. u32 num_domain_objects{};
  76. const bool always_move_handles{
  77. (static_cast<u32>(flags) & static_cast<u32>(Flags::AlwaysMoveHandles)) != 0};
  78. if (!context.Session()->IsDomain() || always_move_handles) {
  79. num_handles_to_move = num_objects_to_move;
  80. } else {
  81. num_domain_objects = num_objects_to_move;
  82. }
  83. if (context.Session()->IsDomain()) {
  84. raw_data_size += sizeof(DomainMessageHeader) / 4 + num_domain_objects;
  85. }
  86. header.data_size.Assign(raw_data_size);
  87. if (num_handles_to_copy || num_handles_to_move) {
  88. header.enable_handle_descriptor.Assign(1);
  89. }
  90. PushRaw(header);
  91. if (header.enable_handle_descriptor) {
  92. IPC::HandleDescriptorHeader handle_descriptor_header{};
  93. handle_descriptor_header.num_handles_to_copy.Assign(num_handles_to_copy);
  94. handle_descriptor_header.num_handles_to_move.Assign(num_handles_to_move);
  95. PushRaw(handle_descriptor_header);
  96. Skip(num_handles_to_copy + num_handles_to_move, true);
  97. }
  98. AlignWithPadding();
  99. if (context.Session()->IsDomain() && context.HasDomainMessageHeader()) {
  100. IPC::DomainMessageHeader domain_header{};
  101. domain_header.num_objects = num_domain_objects;
  102. PushRaw(domain_header);
  103. }
  104. IPC::DataPayloadHeader data_payload_header{};
  105. data_payload_header.magic = Common::MakeMagic('S', 'F', 'C', 'O');
  106. PushRaw(data_payload_header);
  107. datapayload_index = index;
  108. }
  109. template <class T>
  110. void PushIpcInterface(std::shared_ptr<T> iface) {
  111. if (context->Session()->IsDomain()) {
  112. context->AddDomainObject(std::move(iface));
  113. } else {
  114. auto& kernel = Core::System::GetInstance().Kernel();
  115. auto [client, server] = Kernel::Session::Create(kernel, iface->GetServiceName());
  116. context->AddMoveObject(std::move(client));
  117. iface->ClientConnected(std::move(server));
  118. }
  119. }
  120. template <class T, class... Args>
  121. void PushIpcInterface(Args&&... args) {
  122. PushIpcInterface<T>(std::make_shared<T>(std::forward<Args>(args)...));
  123. }
  124. void ValidateHeader() {
  125. const std::size_t num_domain_objects = context->NumDomainObjects();
  126. const std::size_t num_move_objects = context->NumMoveObjects();
  127. ASSERT_MSG(!num_domain_objects || !num_move_objects,
  128. "cannot move normal handles and domain objects");
  129. ASSERT_MSG((index - datapayload_index) == normal_params_size,
  130. "normal_params_size value is incorrect");
  131. ASSERT_MSG((num_domain_objects + num_move_objects) == num_objects_to_move,
  132. "num_objects_to_move value is incorrect");
  133. ASSERT_MSG(context->NumCopyObjects() == num_handles_to_copy,
  134. "num_handles_to_copy value is incorrect");
  135. }
  136. // Validate on destruction, as there shouldn't be any case where we don't want it
  137. ~ResponseBuilder() {
  138. ValidateHeader();
  139. }
  140. template <typename T>
  141. void Push(T value);
  142. template <typename First, typename... Other>
  143. void Push(const First& first_value, const Other&... other_values);
  144. /**
  145. * Helper function for pushing strongly-typed enumeration values.
  146. *
  147. * @tparam Enum The enumeration type to be pushed
  148. *
  149. * @param value The value to push.
  150. *
  151. * @note The underlying size of the enumeration type is the size of the
  152. * data that gets pushed. e.g. "enum class SomeEnum : u16" will
  153. * push a u16-sized amount of data.
  154. */
  155. template <typename Enum>
  156. void PushEnum(Enum value) {
  157. static_assert(std::is_enum_v<Enum>, "T must be an enum type within a PushEnum call.");
  158. static_assert(!std::is_convertible_v<Enum, int>,
  159. "enum type in PushEnum must be a strongly typed enum.");
  160. Push(static_cast<std::underlying_type_t<Enum>>(value));
  161. }
  162. /**
  163. * @brief Copies the content of the given trivially copyable class to the buffer as a normal
  164. * param
  165. * @note: The input class must be correctly packed/padded to fit hardware layout.
  166. */
  167. template <typename T>
  168. void PushRaw(const T& value);
  169. template <typename... O>
  170. void PushMoveObjects(std::shared_ptr<O>... pointers);
  171. template <typename... O>
  172. void PushCopyObjects(std::shared_ptr<O>... pointers);
  173. private:
  174. u32 normal_params_size{};
  175. u32 num_handles_to_copy{};
  176. u32 num_objects_to_move{}; ///< Domain objects or move handles, context dependent
  177. std::ptrdiff_t datapayload_index{};
  178. };
  179. /// Push ///
  180. template <>
  181. inline void ResponseBuilder::Push(s32 value) {
  182. cmdbuf[index++] = static_cast<u32>(value);
  183. }
  184. template <>
  185. inline void ResponseBuilder::Push(u32 value) {
  186. cmdbuf[index++] = value;
  187. }
  188. template <typename T>
  189. void ResponseBuilder::PushRaw(const T& value) {
  190. std::memcpy(cmdbuf + index, &value, sizeof(T));
  191. index += (sizeof(T) + 3) / 4; // round up to word length
  192. }
  193. template <>
  194. inline void ResponseBuilder::Push(ResultCode value) {
  195. // Result codes are actually 64-bit in the IPC buffer, but only the high part is discarded.
  196. Push(value.raw);
  197. Push<u32>(0);
  198. }
  199. template <>
  200. inline void ResponseBuilder::Push(s8 value) {
  201. PushRaw(value);
  202. }
  203. template <>
  204. inline void ResponseBuilder::Push(s16 value) {
  205. PushRaw(value);
  206. }
  207. template <>
  208. inline void ResponseBuilder::Push(s64 value) {
  209. Push(static_cast<u32>(value));
  210. Push(static_cast<u32>(value >> 32));
  211. }
  212. template <>
  213. inline void ResponseBuilder::Push(u8 value) {
  214. PushRaw(value);
  215. }
  216. template <>
  217. inline void ResponseBuilder::Push(u16 value) {
  218. PushRaw(value);
  219. }
  220. template <>
  221. inline void ResponseBuilder::Push(u64 value) {
  222. Push(static_cast<u32>(value));
  223. Push(static_cast<u32>(value >> 32));
  224. }
  225. template <>
  226. inline void ResponseBuilder::Push(float value) {
  227. u32 integral;
  228. std::memcpy(&integral, &value, sizeof(u32));
  229. Push(integral);
  230. }
  231. template <>
  232. inline void ResponseBuilder::Push(double value) {
  233. u64 integral;
  234. std::memcpy(&integral, &value, sizeof(u64));
  235. Push(integral);
  236. }
  237. template <>
  238. inline void ResponseBuilder::Push(bool value) {
  239. Push(static_cast<u8>(value));
  240. }
  241. template <typename First, typename... Other>
  242. void ResponseBuilder::Push(const First& first_value, const Other&... other_values) {
  243. Push(first_value);
  244. Push(other_values...);
  245. }
  246. template <typename... O>
  247. inline void ResponseBuilder::PushCopyObjects(std::shared_ptr<O>... pointers) {
  248. auto objects = {pointers...};
  249. for (auto& object : objects) {
  250. context->AddCopyObject(std::move(object));
  251. }
  252. }
  253. template <typename... O>
  254. inline void ResponseBuilder::PushMoveObjects(std::shared_ptr<O>... pointers) {
  255. auto objects = {pointers...};
  256. for (auto& object : objects) {
  257. context->AddMoveObject(std::move(object));
  258. }
  259. }
  260. class RequestParser : public RequestHelperBase {
  261. public:
  262. explicit RequestParser(u32* command_buffer) : RequestHelperBase(command_buffer) {}
  263. explicit RequestParser(Kernel::HLERequestContext& context) : RequestHelperBase(context) {
  264. ASSERT_MSG(context.GetDataPayloadOffset(), "context is incomplete");
  265. Skip(context.GetDataPayloadOffset(), false);
  266. // Skip the u64 command id, it's already stored in the context
  267. static constexpr u32 CommandIdSize = 2;
  268. Skip(CommandIdSize, false);
  269. }
  270. template <typename T>
  271. T Pop();
  272. template <typename T>
  273. void Pop(T& value);
  274. template <typename First, typename... Other>
  275. void Pop(First& first_value, Other&... other_values);
  276. template <typename T>
  277. T PopEnum() {
  278. static_assert(std::is_enum_v<T>, "T must be an enum type within a PopEnum call.");
  279. static_assert(!std::is_convertible_v<T, int>,
  280. "enum type in PopEnum must be a strongly typed enum.");
  281. return static_cast<T>(Pop<std::underlying_type_t<T>>());
  282. }
  283. /**
  284. * @brief Reads the next normal parameters as a struct, by copying it
  285. * @note: The output class must be correctly packed/padded to fit hardware layout.
  286. */
  287. template <typename T>
  288. void PopRaw(T& value);
  289. /**
  290. * @brief Reads the next normal parameters as a struct, by copying it into a new value
  291. * @note: The output class must be correctly packed/padded to fit hardware layout.
  292. */
  293. template <typename T>
  294. T PopRaw();
  295. template <typename T>
  296. std::shared_ptr<T> GetMoveObject(std::size_t index);
  297. template <typename T>
  298. std::shared_ptr<T> GetCopyObject(std::size_t index);
  299. template <class T>
  300. std::shared_ptr<T> PopIpcInterface() {
  301. ASSERT(context->Session()->IsDomain());
  302. ASSERT(context->GetDomainMessageHeader().input_object_count > 0);
  303. return context->GetDomainRequestHandler<T>(Pop<u32>() - 1);
  304. }
  305. };
  306. /// Pop ///
  307. template <>
  308. inline u32 RequestParser::Pop() {
  309. return cmdbuf[index++];
  310. }
  311. template <>
  312. inline s32 RequestParser::Pop() {
  313. return static_cast<s32>(Pop<u32>());
  314. }
  315. template <typename T>
  316. void RequestParser::PopRaw(T& value) {
  317. std::memcpy(&value, cmdbuf + index, sizeof(T));
  318. index += (sizeof(T) + 3) / 4; // round up to word length
  319. }
  320. template <typename T>
  321. T RequestParser::PopRaw() {
  322. T value;
  323. PopRaw(value);
  324. return value;
  325. }
  326. template <>
  327. inline u8 RequestParser::Pop() {
  328. return PopRaw<u8>();
  329. }
  330. template <>
  331. inline u16 RequestParser::Pop() {
  332. return PopRaw<u16>();
  333. }
  334. template <>
  335. inline u64 RequestParser::Pop() {
  336. const u64 lsw = Pop<u32>();
  337. const u64 msw = Pop<u32>();
  338. return msw << 32 | lsw;
  339. }
  340. template <>
  341. inline s8 RequestParser::Pop() {
  342. return static_cast<s8>(Pop<u8>());
  343. }
  344. template <>
  345. inline s16 RequestParser::Pop() {
  346. return static_cast<s16>(Pop<u16>());
  347. }
  348. template <>
  349. inline s64 RequestParser::Pop() {
  350. return static_cast<s64>(Pop<u64>());
  351. }
  352. template <>
  353. inline float RequestParser::Pop() {
  354. const u32 value = Pop<u32>();
  355. float real;
  356. std::memcpy(&real, &value, sizeof(real));
  357. return real;
  358. }
  359. template <>
  360. inline double RequestParser::Pop() {
  361. const u64 value = Pop<u64>();
  362. double real;
  363. std::memcpy(&real, &value, sizeof(real));
  364. return real;
  365. }
  366. template <>
  367. inline bool RequestParser::Pop() {
  368. return Pop<u8>() != 0;
  369. }
  370. template <>
  371. inline ResultCode RequestParser::Pop() {
  372. return ResultCode{Pop<u32>()};
  373. }
  374. template <typename T>
  375. void RequestParser::Pop(T& value) {
  376. value = Pop<T>();
  377. }
  378. template <typename First, typename... Other>
  379. void RequestParser::Pop(First& first_value, Other&... other_values) {
  380. first_value = Pop<First>();
  381. Pop(other_values...);
  382. }
  383. template <typename T>
  384. std::shared_ptr<T> RequestParser::GetMoveObject(std::size_t index) {
  385. return context->GetMoveObject<T>(index);
  386. }
  387. template <typename T>
  388. std::shared_ptr<T> RequestParser::GetCopyObject(std::size_t index) {
  389. return context->GetCopyObject<T>(index);
  390. }
  391. } // namespace IPC