ipc_helpers.h 12 KB

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