ipc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "common/common_types.h"
  6. #include "common/swap.h"
  7. #include "core/hle/kernel/errors.h"
  8. #include "core/memory.h"
  9. namespace IPC {
  10. /// Size of the command buffer area, in 32-bit words.
  11. constexpr size_t COMMAND_BUFFER_LENGTH = 0x100 / sizeof(u32);
  12. // These errors are commonly returned by invalid IPC translations, so alias them here for
  13. // convenience.
  14. // TODO(yuriks): These will probably go away once translation is implemented inside the kernel.
  15. constexpr auto ERR_INVALID_HANDLE = Kernel::ERR_INVALID_HANDLE_OS;
  16. enum class ControlCommand : u32 {
  17. ConvertSessionToDomain = 0,
  18. ConvertDomainToSession = 1,
  19. DuplicateSession = 2,
  20. QueryPointerBufferSize = 3,
  21. DuplicateSessionEx = 4,
  22. Unspecified,
  23. };
  24. enum class CommandType : u32 {
  25. Close = 2,
  26. Request = 4,
  27. Control = 5,
  28. Unspecified,
  29. };
  30. struct CommandHeader {
  31. union {
  32. u32_le raw_low;
  33. BitField<0, 16, CommandType> type;
  34. BitField<16, 4, u32_le> num_buf_x_descriptors;
  35. BitField<20, 4, u32_le> num_buf_a_descriptors;
  36. BitField<24, 4, u32_le> num_buf_b_descriptors;
  37. BitField<28, 4, u32_le> num_buf_w_descriptors;
  38. };
  39. enum class BufferDescriptorCFlag : u32 {
  40. Disabled = 0,
  41. InlineDescriptor = 1,
  42. OneDescriptor = 2,
  43. };
  44. union {
  45. u32_le raw_high;
  46. BitField<0, 10, u32_le> data_size;
  47. BitField<10, 4, BufferDescriptorCFlag> buf_c_descriptor_flags;
  48. BitField<31, 1, u32_le> enable_handle_descriptor;
  49. };
  50. };
  51. static_assert(sizeof(CommandHeader) == 8, "CommandHeader size is incorrect");
  52. union HandleDescriptorHeader {
  53. u32_le raw_high;
  54. BitField<0, 1, u32_le> send_current_pid;
  55. BitField<1, 4, u32_le> num_handles_to_copy;
  56. BitField<5, 4, u32_le> num_handles_to_move;
  57. };
  58. static_assert(sizeof(HandleDescriptorHeader) == 4, "HandleDescriptorHeader size is incorrect");
  59. struct BufferDescriptorX {
  60. union {
  61. BitField<0, 6, u32_le> counter_bits_0_5;
  62. BitField<6, 3, u32_le> address_bits_36_38;
  63. BitField<9, 3, u32_le> counter_bits_9_11;
  64. BitField<12, 4, u32_le> address_bits_32_35;
  65. BitField<16, 16, u32_le> size;
  66. };
  67. u32_le address_bits_0_31;
  68. u32_le Counter() const {
  69. u32_le counter{counter_bits_0_5};
  70. counter |= counter_bits_9_11 << 9;
  71. return counter;
  72. }
  73. VAddr Address() const {
  74. VAddr address{address_bits_0_31};
  75. address |= static_cast<VAddr>(address_bits_32_35) << 32;
  76. address |= static_cast<VAddr>(address_bits_36_38) << 36;
  77. return address;
  78. }
  79. u64 Size() const {
  80. return static_cast<u64>(size);
  81. }
  82. };
  83. static_assert(sizeof(BufferDescriptorX) == 8, "BufferDescriptorX size is incorrect");
  84. struct BufferDescriptorABW {
  85. u32_le size_bits_0_31;
  86. u32_le address_bits_0_31;
  87. union {
  88. BitField<0, 2, u32_le> flags;
  89. BitField<2, 3, u32_le> address_bits_36_38;
  90. BitField<24, 4, u32_le> size_bits_32_35;
  91. BitField<28, 4, u32_le> address_bits_32_35;
  92. };
  93. VAddr Address() const {
  94. VAddr address{address_bits_0_31};
  95. address |= static_cast<VAddr>(address_bits_32_35) << 32;
  96. address |= static_cast<VAddr>(address_bits_36_38) << 36;
  97. return address;
  98. }
  99. u64 Size() const {
  100. u64 size{size_bits_0_31};
  101. size |= static_cast<u64>(size_bits_32_35) << 32;
  102. return size;
  103. }
  104. };
  105. static_assert(sizeof(BufferDescriptorABW) == 12, "BufferDescriptorABW size is incorrect");
  106. struct BufferDescriptorC {
  107. u32_le address_bits_0_31;
  108. union {
  109. BitField<0, 16, u32_le> address_bits_32_47;
  110. BitField<16, 16, u32_le> size;
  111. };
  112. VAddr Address() const {
  113. VAddr address{address_bits_0_31};
  114. address |= static_cast<VAddr>(address_bits_32_47) << 32;
  115. return address;
  116. }
  117. u64 Size() const {
  118. return static_cast<u64>(size);
  119. }
  120. };
  121. static_assert(sizeof(BufferDescriptorC) == 8, "BufferDescriptorC size is incorrect");
  122. struct DataPayloadHeader {
  123. u32_le magic;
  124. INSERT_PADDING_WORDS(1);
  125. };
  126. static_assert(sizeof(DataPayloadHeader) == 8, "DataPayloadRequest size is incorrect");
  127. struct DomainMessageHeader {
  128. enum class CommandType : u32_le {
  129. SendMessage = 1,
  130. CloseVirtualHandle = 2,
  131. };
  132. union {
  133. // Used when responding to an IPC request, Server -> Client.
  134. struct {
  135. u32_le num_objects;
  136. INSERT_PADDING_WORDS(3);
  137. };
  138. // Used when performing an IPC request, Client -> Server.
  139. struct {
  140. union {
  141. BitField<0, 8, CommandType> command;
  142. BitField<16, 16, u32_le> size;
  143. };
  144. u32_le object_id;
  145. INSERT_PADDING_WORDS(2);
  146. };
  147. };
  148. };
  149. static_assert(sizeof(DomainMessageHeader) == 16, "DomainMessageHeader size is incorrect");
  150. } // namespace IPC