message_buffer.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "common/alignment.h"
  5. #include "common/bit_field.h"
  6. #include "core/hle/kernel/k_thread.h"
  7. namespace Kernel {
  8. constexpr inline size_t MessageBufferSize = 0x100;
  9. class MessageBuffer {
  10. public:
  11. class MessageHeader {
  12. private:
  13. static constexpr inline u64 NullTag = 0;
  14. public:
  15. enum class ReceiveListCountType : u32 {
  16. None = 0,
  17. ToMessageBuffer = 1,
  18. ToSingleBuffer = 2,
  19. CountOffset = 2,
  20. CountMax = 13,
  21. };
  22. private:
  23. union {
  24. std::array<u32, 2> raw;
  25. struct {
  26. // Define fields for the first header word.
  27. union {
  28. BitField<0, 16, u16> tag;
  29. BitField<16, 4, u32> pointer_count;
  30. BitField<20, 4, u32> send_count;
  31. BitField<24, 4, u32> receive_count;
  32. BitField<28, 4, u32> exchange_count;
  33. };
  34. // Define fields for the second header word.
  35. union {
  36. BitField<0, 10, u32> raw_count;
  37. BitField<10, 4, ReceiveListCountType> receive_list_count;
  38. BitField<14, 6, u32> reserved0;
  39. BitField<20, 11, u32> receive_list_offset;
  40. BitField<31, 1, u32> has_special_header;
  41. };
  42. };
  43. } m_header;
  44. public:
  45. constexpr MessageHeader() : m_header{} {}
  46. constexpr MessageHeader(u16 tag, bool special, s32 ptr, s32 send, s32 recv, s32 exch,
  47. s32 raw, ReceiveListCountType recv_list)
  48. : m_header{} {
  49. m_header.raw[0] = 0;
  50. m_header.raw[1] = 0;
  51. m_header.tag.Assign(tag);
  52. m_header.pointer_count.Assign(ptr);
  53. m_header.send_count.Assign(send);
  54. m_header.receive_count.Assign(recv);
  55. m_header.exchange_count.Assign(exch);
  56. m_header.raw_count.Assign(raw);
  57. m_header.receive_list_count.Assign(recv_list);
  58. m_header.has_special_header.Assign(special);
  59. }
  60. explicit MessageHeader(const MessageBuffer& buf) : m_header{} {
  61. buf.Get(0, m_header.raw.data(), 2);
  62. }
  63. explicit MessageHeader(const u32* msg) : m_header{{msg[0], msg[1]}} {}
  64. constexpr u16 GetTag() const {
  65. return m_header.tag;
  66. }
  67. constexpr s32 GetPointerCount() const {
  68. return m_header.pointer_count;
  69. }
  70. constexpr s32 GetSendCount() const {
  71. return m_header.send_count;
  72. }
  73. constexpr s32 GetReceiveCount() const {
  74. return m_header.receive_count;
  75. }
  76. constexpr s32 GetExchangeCount() const {
  77. return m_header.exchange_count;
  78. }
  79. constexpr s32 GetMapAliasCount() const {
  80. return this->GetSendCount() + this->GetReceiveCount() + this->GetExchangeCount();
  81. }
  82. constexpr s32 GetRawCount() const {
  83. return m_header.raw_count;
  84. }
  85. constexpr ReceiveListCountType GetReceiveListCount() const {
  86. return m_header.receive_list_count;
  87. }
  88. constexpr s32 GetReceiveListOffset() const {
  89. return m_header.receive_list_offset;
  90. }
  91. constexpr bool GetHasSpecialHeader() const {
  92. return m_header.has_special_header.Value() != 0;
  93. }
  94. constexpr void SetReceiveListCount(ReceiveListCountType recv_list) {
  95. m_header.receive_list_count.Assign(recv_list);
  96. }
  97. constexpr const u32* GetData() const {
  98. return m_header.raw.data();
  99. }
  100. static constexpr size_t GetDataSize() {
  101. return sizeof(m_header);
  102. }
  103. };
  104. class SpecialHeader {
  105. private:
  106. union {
  107. std::array<u32, 1> raw;
  108. // Define fields for the header word.
  109. BitField<0, 1, u32> has_process_id;
  110. BitField<1, 4, u32> copy_handle_count;
  111. BitField<5, 4, u32> move_handle_count;
  112. } m_header;
  113. bool m_has_header;
  114. public:
  115. constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move)
  116. : m_header{}, m_has_header(true) {
  117. m_header.has_process_id.Assign(pid);
  118. m_header.copy_handle_count.Assign(copy);
  119. m_header.move_handle_count.Assign(move);
  120. }
  121. constexpr explicit SpecialHeader(bool pid, s32 copy, s32 move, bool _has_header)
  122. : m_header{}, m_has_header(_has_header) {
  123. m_header.has_process_id.Assign(pid);
  124. m_header.copy_handle_count.Assign(copy);
  125. m_header.move_handle_count.Assign(move);
  126. }
  127. explicit SpecialHeader(const MessageBuffer& buf, const MessageHeader& hdr)
  128. : m_header{}, m_has_header(hdr.GetHasSpecialHeader()) {
  129. if (m_has_header) {
  130. buf.Get(static_cast<s32>(MessageHeader::GetDataSize() / sizeof(u32)),
  131. m_header.raw.data(), sizeof(m_header) / sizeof(u32));
  132. }
  133. }
  134. constexpr bool GetHasProcessId() const {
  135. return m_header.has_process_id.Value() != 0;
  136. }
  137. constexpr s32 GetCopyHandleCount() const {
  138. return m_header.copy_handle_count;
  139. }
  140. constexpr s32 GetMoveHandleCount() const {
  141. return m_header.move_handle_count;
  142. }
  143. constexpr const u32* GetHeader() const {
  144. return m_header.raw.data();
  145. }
  146. constexpr size_t GetHeaderSize() const {
  147. if (m_has_header) {
  148. return sizeof(m_header);
  149. } else {
  150. return 0;
  151. }
  152. }
  153. constexpr size_t GetDataSize() const {
  154. if (m_has_header) {
  155. return (this->GetHasProcessId() ? sizeof(u64) : 0) +
  156. (this->GetCopyHandleCount() * sizeof(Handle)) +
  157. (this->GetMoveHandleCount() * sizeof(Handle));
  158. } else {
  159. return 0;
  160. }
  161. }
  162. };
  163. class MapAliasDescriptor {
  164. public:
  165. enum class Attribute : u32 {
  166. Ipc = 0,
  167. NonSecureIpc = 1,
  168. NonDeviceIpc = 3,
  169. };
  170. private:
  171. static constexpr u32 SizeLowCount = 32;
  172. static constexpr u32 SizeHighCount = 4;
  173. static constexpr u32 AddressLowCount = 32;
  174. static constexpr u32 AddressMidCount = 4;
  175. constexpr u32 GetAddressMid(u64 address) {
  176. return static_cast<u32>(address >> AddressLowCount) & ((1U << AddressMidCount) - 1);
  177. }
  178. constexpr u32 GetAddressHigh(u64 address) {
  179. return static_cast<u32>(address >> (AddressLowCount + AddressMidCount));
  180. }
  181. private:
  182. union {
  183. std::array<u32, 3> raw;
  184. struct {
  185. // Define fields for the first two words.
  186. u32 size_low;
  187. u32 address_low;
  188. // Define fields for the packed descriptor word.
  189. union {
  190. BitField<0, 2, Attribute> attributes;
  191. BitField<2, 3, u32> address_high;
  192. BitField<5, 19, u32> reserved;
  193. BitField<24, 4, u32> size_high;
  194. BitField<28, 4, u32> address_mid;
  195. };
  196. };
  197. } m_data;
  198. public:
  199. constexpr MapAliasDescriptor() : m_data{} {}
  200. MapAliasDescriptor(const void* buffer, size_t _size, Attribute attr = Attribute::Ipc)
  201. : m_data{} {
  202. const u64 address = reinterpret_cast<u64>(buffer);
  203. const u64 size = static_cast<u64>(_size);
  204. m_data.size_low = static_cast<u32>(size);
  205. m_data.address_low = static_cast<u32>(address);
  206. m_data.attributes.Assign(attr);
  207. m_data.address_mid.Assign(GetAddressMid(address));
  208. m_data.size_high.Assign(static_cast<u32>(size >> SizeLowCount));
  209. m_data.address_high.Assign(GetAddressHigh(address));
  210. }
  211. MapAliasDescriptor(const MessageBuffer& buf, s32 index) : m_data{} {
  212. buf.Get(index, m_data.raw.data(), 3);
  213. }
  214. constexpr uintptr_t GetAddress() const {
  215. return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid)
  216. << AddressLowCount) |
  217. m_data.address_low;
  218. }
  219. constexpr uintptr_t GetSize() const {
  220. return (static_cast<u64>(m_data.size_high) << SizeLowCount) | m_data.size_low;
  221. }
  222. constexpr Attribute GetAttribute() const {
  223. return m_data.attributes;
  224. }
  225. constexpr const u32* GetData() const {
  226. return m_data.raw.data();
  227. }
  228. static constexpr size_t GetDataSize() {
  229. return sizeof(m_data);
  230. }
  231. };
  232. class PointerDescriptor {
  233. private:
  234. static constexpr u32 AddressLowCount = 32;
  235. static constexpr u32 AddressMidCount = 4;
  236. constexpr u32 GetAddressMid(u64 address) {
  237. return static_cast<u32>(address >> AddressLowCount) & ((1u << AddressMidCount) - 1);
  238. }
  239. constexpr u32 GetAddressHigh(u64 address) {
  240. return static_cast<u32>(address >> (AddressLowCount + AddressMidCount));
  241. }
  242. private:
  243. union {
  244. std::array<u32, 2> raw;
  245. struct {
  246. // Define fields for the packed descriptor word.
  247. union {
  248. BitField<0, 4, u32> index;
  249. BitField<4, 2, u32> reserved0;
  250. BitField<6, 3, u32> address_high;
  251. BitField<9, 3, u32> reserved1;
  252. BitField<12, 4, u32> address_mid;
  253. BitField<16, 16, u32> size;
  254. };
  255. // Define fields for the second word.
  256. u32 address_low;
  257. };
  258. } m_data;
  259. public:
  260. constexpr PointerDescriptor() : m_data{} {}
  261. PointerDescriptor(const void* buffer, size_t size, s32 index) : m_data{} {
  262. const u64 address = reinterpret_cast<u64>(buffer);
  263. m_data.index.Assign(index);
  264. m_data.address_high.Assign(GetAddressHigh(address));
  265. m_data.address_mid.Assign(GetAddressMid(address));
  266. m_data.size.Assign(static_cast<u32>(size));
  267. m_data.address_low = static_cast<u32>(address);
  268. }
  269. PointerDescriptor(const MessageBuffer& buf, s32 index) : m_data{} {
  270. buf.Get(index, m_data.raw.data(), 2);
  271. }
  272. constexpr s32 GetIndex() const {
  273. return m_data.index;
  274. }
  275. constexpr uintptr_t GetAddress() const {
  276. return (static_cast<u64>((m_data.address_high << AddressMidCount) | m_data.address_mid)
  277. << AddressLowCount) |
  278. m_data.address_low;
  279. }
  280. constexpr size_t GetSize() const {
  281. return m_data.size;
  282. }
  283. constexpr const u32* GetData() const {
  284. return m_data.raw.data();
  285. }
  286. static constexpr size_t GetDataSize() {
  287. return sizeof(m_data);
  288. }
  289. };
  290. class ReceiveListEntry {
  291. private:
  292. static constexpr u32 AddressLowCount = 32;
  293. constexpr u32 GetAddressHigh(u64 address) {
  294. return static_cast<u32>(address >> (AddressLowCount));
  295. }
  296. private:
  297. union {
  298. std::array<u32, 2> raw;
  299. struct {
  300. // Define fields for the first word.
  301. u32 address_low;
  302. // Define fields for the packed descriptor word.
  303. union {
  304. BitField<0, 7, u32> address_high;
  305. BitField<7, 9, u32> reserved;
  306. BitField<16, 16, u32> size;
  307. };
  308. };
  309. } m_data;
  310. public:
  311. constexpr ReceiveListEntry() : m_data{} {}
  312. ReceiveListEntry(const void* buffer, size_t size) : m_data{} {
  313. const u64 address = reinterpret_cast<u64>(buffer);
  314. m_data.address_low = static_cast<u32>(address);
  315. m_data.address_high.Assign(GetAddressHigh(address));
  316. m_data.size.Assign(static_cast<u32>(size));
  317. }
  318. ReceiveListEntry(u32 a, u32 b) : m_data{{a, b}} {}
  319. constexpr uintptr_t GetAddress() const {
  320. return (static_cast<u64>(m_data.address_high) << AddressLowCount) | m_data.address_low;
  321. }
  322. constexpr size_t GetSize() const {
  323. return m_data.size;
  324. }
  325. constexpr const u32* GetData() const {
  326. return m_data.raw.data();
  327. }
  328. static constexpr size_t GetDataSize() {
  329. return sizeof(m_data);
  330. }
  331. };
  332. private:
  333. u32* m_buffer;
  334. size_t m_size;
  335. public:
  336. constexpr MessageBuffer(u32* b, size_t sz) : m_buffer(b), m_size(sz) {}
  337. constexpr explicit MessageBuffer(u32* b) : m_buffer(b), m_size(MessageBufferSize) {}
  338. constexpr void* GetBufferForDebug() const {
  339. return m_buffer;
  340. }
  341. constexpr size_t GetBufferSize() const {
  342. return m_size;
  343. }
  344. void Get(s32 index, u32* dst, size_t count) const {
  345. // Ensure that this doesn't get re-ordered.
  346. std::atomic_thread_fence(std::memory_order_seq_cst);
  347. // Get the words.
  348. static_assert(sizeof(*dst) == sizeof(*m_buffer));
  349. memcpy(dst, m_buffer + index, count * sizeof(*dst));
  350. }
  351. s32 Set(s32 index, u32* src, size_t count) const {
  352. // Ensure that this doesn't get re-ordered.
  353. std::atomic_thread_fence(std::memory_order_seq_cst);
  354. // Set the words.
  355. memcpy(m_buffer + index, src, count * sizeof(*src));
  356. // Ensure that this doesn't get re-ordered.
  357. std::atomic_thread_fence(std::memory_order_seq_cst);
  358. return static_cast<s32>(index + count);
  359. }
  360. template <typename T>
  361. const T& GetRaw(s32 index) const {
  362. return *reinterpret_cast<const T*>(m_buffer + index);
  363. }
  364. template <typename T>
  365. s32 SetRaw(s32 index, const T& val) const {
  366. *reinterpret_cast<const T*>(m_buffer + index) = val;
  367. return index + (Common::AlignUp(sizeof(val), sizeof(*m_buffer)) / sizeof(*m_buffer));
  368. }
  369. void GetRawArray(s32 index, void* dst, size_t len) const {
  370. memcpy(dst, m_buffer + index, len);
  371. }
  372. void SetRawArray(s32 index, const void* src, size_t len) const {
  373. memcpy(m_buffer + index, src, len);
  374. }
  375. void SetNull() const {
  376. this->Set(MessageHeader());
  377. }
  378. s32 Set(const MessageHeader& hdr) const {
  379. memcpy(m_buffer, hdr.GetData(), hdr.GetDataSize());
  380. return static_cast<s32>(hdr.GetDataSize() / sizeof(*m_buffer));
  381. }
  382. s32 Set(const SpecialHeader& spc) const {
  383. const s32 index = static_cast<s32>(MessageHeader::GetDataSize() / sizeof(*m_buffer));
  384. memcpy(m_buffer + index, spc.GetHeader(), spc.GetHeaderSize());
  385. return static_cast<s32>(index + (spc.GetHeaderSize() / sizeof(*m_buffer)));
  386. }
  387. s32 SetHandle(s32 index, const Handle& hnd) const {
  388. memcpy(m_buffer + index, std::addressof(hnd), sizeof(hnd));
  389. return static_cast<s32>(index + (sizeof(hnd) / sizeof(*m_buffer)));
  390. }
  391. s32 SetProcessId(s32 index, const u64 pid) const {
  392. memcpy(m_buffer + index, std::addressof(pid), sizeof(pid));
  393. return static_cast<s32>(index + (sizeof(pid) / sizeof(*m_buffer)));
  394. }
  395. s32 Set(s32 index, const MapAliasDescriptor& desc) const {
  396. memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
  397. return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
  398. }
  399. s32 Set(s32 index, const PointerDescriptor& desc) const {
  400. memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
  401. return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
  402. }
  403. s32 Set(s32 index, const ReceiveListEntry& desc) const {
  404. memcpy(m_buffer + index, desc.GetData(), desc.GetDataSize());
  405. return static_cast<s32>(index + (desc.GetDataSize() / sizeof(*m_buffer)));
  406. }
  407. s32 Set(s32 index, const u32 val) const {
  408. memcpy(m_buffer + index, std::addressof(val), sizeof(val));
  409. return static_cast<s32>(index + (sizeof(val) / sizeof(*m_buffer)));
  410. }
  411. Result GetAsyncResult() const {
  412. MessageHeader hdr(m_buffer);
  413. MessageHeader null{};
  414. if (memcmp(hdr.GetData(), null.GetData(), MessageHeader::GetDataSize()) != 0) [[unlikely]] {
  415. R_SUCCEED();
  416. }
  417. return Result(m_buffer[MessageHeader::GetDataSize() / sizeof(*m_buffer)]);
  418. }
  419. void SetAsyncResult(Result res) const {
  420. const s32 index = this->Set(MessageHeader());
  421. const auto value = res.raw;
  422. memcpy(m_buffer + index, std::addressof(value), sizeof(value));
  423. }
  424. u32 Get32(s32 index) const {
  425. return m_buffer[index];
  426. }
  427. u64 Get64(s32 index) const {
  428. u64 value;
  429. memcpy(std::addressof(value), m_buffer + index, sizeof(value));
  430. return value;
  431. }
  432. u64 GetProcessId(s32 index) const {
  433. return this->Get64(index);
  434. }
  435. Handle GetHandle(s32 index) const {
  436. static_assert(sizeof(Handle) == sizeof(*m_buffer));
  437. return Handle(m_buffer[index]);
  438. }
  439. static constexpr s32 GetSpecialDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
  440. return static_cast<s32>((MessageHeader::GetDataSize() / sizeof(u32)) +
  441. (spc.GetHeaderSize() / sizeof(u32)));
  442. }
  443. static constexpr s32 GetPointerDescriptorIndex(const MessageHeader& hdr,
  444. const SpecialHeader& spc) {
  445. return static_cast<s32>(GetSpecialDataIndex(hdr, spc) + (spc.GetDataSize() / sizeof(u32)));
  446. }
  447. static constexpr s32 GetMapAliasDescriptorIndex(const MessageHeader& hdr,
  448. const SpecialHeader& spc) {
  449. return GetPointerDescriptorIndex(hdr, spc) +
  450. static_cast<s32>(hdr.GetPointerCount() * PointerDescriptor::GetDataSize() /
  451. sizeof(u32));
  452. }
  453. static constexpr s32 GetRawDataIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
  454. return GetMapAliasDescriptorIndex(hdr, spc) +
  455. static_cast<s32>(hdr.GetMapAliasCount() * MapAliasDescriptor::GetDataSize() /
  456. sizeof(u32));
  457. }
  458. static constexpr s32 GetReceiveListIndex(const MessageHeader& hdr, const SpecialHeader& spc) {
  459. if (const s32 recv_list_index = hdr.GetReceiveListOffset()) {
  460. return recv_list_index;
  461. } else {
  462. return GetRawDataIndex(hdr, spc) + hdr.GetRawCount();
  463. }
  464. }
  465. static constexpr size_t GetMessageBufferSize(const MessageHeader& hdr,
  466. const SpecialHeader& spc) {
  467. // Get the size of the plain message.
  468. size_t msg_size = GetReceiveListIndex(hdr, spc) * sizeof(u32);
  469. // Add the size of the receive list.
  470. const auto count = hdr.GetReceiveListCount();
  471. switch (count) {
  472. case MessageHeader::ReceiveListCountType::None:
  473. break;
  474. case MessageHeader::ReceiveListCountType::ToMessageBuffer:
  475. break;
  476. case MessageHeader::ReceiveListCountType::ToSingleBuffer:
  477. msg_size += ReceiveListEntry::GetDataSize();
  478. break;
  479. default:
  480. msg_size += (static_cast<s32>(count) -
  481. static_cast<s32>(MessageHeader::ReceiveListCountType::CountOffset)) *
  482. ReceiveListEntry::GetDataSize();
  483. break;
  484. }
  485. return msg_size;
  486. }
  487. };
  488. } // namespace Kernel