hle_ipc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <boost/range/algorithm_ext/erase.hpp>
  5. #include "common/assert.h"
  6. #include "common/common_funcs.h"
  7. #include "common/common_types.h"
  8. #include "core/hle/ipc_helpers.h"
  9. #include "core/hle/kernel/handle_table.h"
  10. #include "core/hle/kernel/hle_ipc.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/kernel/process.h"
  13. #include "core/hle/kernel/server_session.h"
  14. #include "core/memory.h"
  15. namespace Kernel {
  16. void SessionRequestHandler::ClientConnected(SharedPtr<ServerSession> server_session) {
  17. server_session->SetHleHandler(shared_from_this());
  18. connected_sessions.push_back(server_session);
  19. }
  20. void SessionRequestHandler::ClientDisconnected(SharedPtr<ServerSession> server_session) {
  21. server_session->SetHleHandler(nullptr);
  22. boost::range::remove_erase(connected_sessions, server_session);
  23. }
  24. HLERequestContext::HLERequestContext(SharedPtr<Kernel::ServerSession> server_session)
  25. : server_session(std::move(server_session)) {
  26. cmd_buf[0] = 0;
  27. }
  28. HLERequestContext::~HLERequestContext() = default;
  29. void HLERequestContext::ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming) {
  30. IPC::RequestParser rp(src_cmdbuf);
  31. command_header = std::make_unique<IPC::CommandHeader>(rp.PopRaw<IPC::CommandHeader>());
  32. if (command_header->type == IPC::CommandType::Close) {
  33. // Close does not populate the rest of the IPC header
  34. return;
  35. }
  36. // If handle descriptor is present, add size of it
  37. if (command_header->enable_handle_descriptor) {
  38. handle_descriptor_header =
  39. std::make_unique<IPC::HandleDescriptorHeader>(rp.PopRaw<IPC::HandleDescriptorHeader>());
  40. if (handle_descriptor_header->send_current_pid) {
  41. rp.Skip(2, false);
  42. }
  43. if (incoming) {
  44. // Populate the object lists with the data in the IPC request.
  45. for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_copy; ++handle) {
  46. copy_objects.push_back(Kernel::g_handle_table.GetGeneric(rp.Pop<Handle>()));
  47. }
  48. for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_move; ++handle) {
  49. move_objects.push_back(Kernel::g_handle_table.GetGeneric(rp.Pop<Handle>()));
  50. }
  51. } else {
  52. // For responses we just ignore the handles, they're empty and will be populated when
  53. // translating the response.
  54. rp.Skip(handle_descriptor_header->num_handles_to_copy, false);
  55. rp.Skip(handle_descriptor_header->num_handles_to_move, false);
  56. }
  57. }
  58. for (unsigned i = 0; i < command_header->num_buf_x_descriptors; ++i) {
  59. buffer_x_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorX>());
  60. }
  61. for (unsigned i = 0; i < command_header->num_buf_a_descriptors; ++i) {
  62. buffer_a_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  63. }
  64. for (unsigned i = 0; i < command_header->num_buf_b_descriptors; ++i) {
  65. buffer_b_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  66. }
  67. for (unsigned i = 0; i < command_header->num_buf_w_descriptors; ++i) {
  68. buffer_w_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  69. }
  70. buffer_c_offset = rp.GetCurrentOffset() + command_header->data_size;
  71. // Padding to align to 16 bytes
  72. rp.AlignWithPadding();
  73. if (Session()->IsDomain() && (command_header->type == IPC::CommandType::Request || !incoming)) {
  74. // If this is an incoming message, only CommandType "Request" has a domain header
  75. // All outgoing domain messages have the domain header, if only incoming has it
  76. if (incoming || domain_message_header) {
  77. domain_message_header =
  78. std::make_unique<IPC::DomainMessageHeader>(rp.PopRaw<IPC::DomainMessageHeader>());
  79. } else {
  80. if (Session()->IsDomain())
  81. LOG_WARNING(IPC, "Domain request has no DomainMessageHeader!");
  82. }
  83. }
  84. data_payload_header =
  85. std::make_unique<IPC::DataPayloadHeader>(rp.PopRaw<IPC::DataPayloadHeader>());
  86. data_payload_offset = rp.GetCurrentOffset();
  87. if (domain_message_header && domain_message_header->command ==
  88. IPC::DomainMessageHeader::CommandType::CloseVirtualHandle) {
  89. // CloseVirtualHandle command does not have SFC* or any data
  90. return;
  91. }
  92. if (incoming) {
  93. ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'I'));
  94. } else {
  95. ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'O'));
  96. }
  97. rp.SetCurrentOffset(buffer_c_offset);
  98. // For Inline buffers, the response data is written directly to buffer_c_offset
  99. // and in this case we don't have any BufferDescriptorC on the request.
  100. if (command_header->buf_c_descriptor_flags >
  101. IPC::CommandHeader::BufferDescriptorCFlag::InlineDescriptor) {
  102. if (command_header->buf_c_descriptor_flags ==
  103. IPC::CommandHeader::BufferDescriptorCFlag::OneDescriptor) {
  104. buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
  105. } else {
  106. unsigned num_buf_c_descriptors =
  107. static_cast<unsigned>(command_header->buf_c_descriptor_flags.Value()) - 2;
  108. // This is used to detect possible underflows, in case something is broken
  109. // with the two ifs above and the flags value is == 0 || == 1.
  110. ASSERT(num_buf_c_descriptors < 14);
  111. for (unsigned i = 0; i < num_buf_c_descriptors; ++i) {
  112. buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
  113. }
  114. }
  115. }
  116. rp.SetCurrentOffset(data_payload_offset);
  117. command = rp.Pop<u32_le>();
  118. rp.Skip(1, false); // The command is actually an u64, but we don't use the high part.
  119. }
  120. ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf,
  121. Process& src_process,
  122. HandleTable& src_table) {
  123. ParseCommandBuffer(src_cmdbuf, true);
  124. if (command_header->type == IPC::CommandType::Close) {
  125. // Close does not populate the rest of the IPC header
  126. return RESULT_SUCCESS;
  127. }
  128. // The data_size already includes the payload header, the padding and the domain header.
  129. size_t size = data_payload_offset + command_header->data_size -
  130. sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
  131. if (domain_message_header)
  132. size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
  133. std::copy_n(src_cmdbuf, size, cmd_buf.begin());
  134. return RESULT_SUCCESS;
  135. }
  136. ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
  137. HandleTable& dst_table) {
  138. // The header was already built in the internal command buffer. Attempt to parse it to verify
  139. // the integrity and then copy it over to the target command buffer.
  140. ParseCommandBuffer(cmd_buf.data(), false);
  141. // The data_size already includes the payload header, the padding and the domain header.
  142. size_t size = data_payload_offset + command_header->data_size -
  143. sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
  144. if (domain_message_header)
  145. size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
  146. std::copy_n(cmd_buf.begin(), size, dst_cmdbuf);
  147. if (command_header->enable_handle_descriptor) {
  148. ASSERT_MSG(!move_objects.empty() || !copy_objects.empty(),
  149. "Handle descriptor bit set but no handles to translate");
  150. // We write the translated handles at a specific offset in the command buffer, this space
  151. // was already reserved when writing the header.
  152. size_t current_offset =
  153. (sizeof(IPC::CommandHeader) + sizeof(IPC::HandleDescriptorHeader)) / sizeof(u32);
  154. ASSERT_MSG(!handle_descriptor_header->send_current_pid, "Sending PID is not implemented");
  155. ASSERT_MSG(copy_objects.size() == handle_descriptor_header->num_handles_to_copy);
  156. ASSERT_MSG(move_objects.size() == handle_descriptor_header->num_handles_to_move);
  157. // We don't make a distinction between copy and move handles when translating since HLE
  158. // services don't deal with handles directly. However, the guest applications might check
  159. // for specific values in each of these descriptors.
  160. for (auto& object : copy_objects) {
  161. ASSERT(object != nullptr);
  162. dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
  163. }
  164. for (auto& object : move_objects) {
  165. ASSERT(object != nullptr);
  166. dst_cmdbuf[current_offset++] = Kernel::g_handle_table.Create(object).Unwrap();
  167. }
  168. }
  169. // TODO(Subv): Translate the X/A/B/W buffers.
  170. if (Session()->IsDomain() && domain_message_header) {
  171. ASSERT(domain_message_header->num_objects == domain_objects.size());
  172. // Write the domain objects to the command buffer, these go after the raw untranslated data.
  173. // TODO(Subv): This completely ignores C buffers.
  174. size_t domain_offset = size - domain_message_header->num_objects;
  175. auto& request_handlers = server_session->domain_request_handlers;
  176. for (auto& object : domain_objects) {
  177. request_handlers.emplace_back(object);
  178. dst_cmdbuf[domain_offset++] = static_cast<u32_le>(request_handlers.size());
  179. }
  180. }
  181. return RESULT_SUCCESS;
  182. }
  183. std::vector<u8> HLERequestContext::ReadBuffer() const {
  184. std::vector<u8> buffer;
  185. const bool is_buffer_a{BufferDescriptorA().size() && BufferDescriptorA()[0].Size()};
  186. if (is_buffer_a) {
  187. buffer.resize(BufferDescriptorA()[0].Size());
  188. Memory::ReadBlock(BufferDescriptorA()[0].Address(), buffer.data(), buffer.size());
  189. } else {
  190. buffer.resize(BufferDescriptorX()[0].Size());
  191. Memory::ReadBlock(BufferDescriptorX()[0].Address(), buffer.data(), buffer.size());
  192. }
  193. return buffer;
  194. }
  195. size_t HLERequestContext::WriteBuffer(const void* buffer, size_t size) const {
  196. const bool is_buffer_b{BufferDescriptorB().size() && BufferDescriptorB()[0].Size()};
  197. ASSERT_MSG(size <= GetWriteBufferSize(), "Size %d is too big", size);
  198. if (is_buffer_b) {
  199. Memory::WriteBlock(BufferDescriptorB()[0].Address(), buffer, size);
  200. } else {
  201. Memory::WriteBlock(BufferDescriptorC()[0].Address(), buffer, size);
  202. }
  203. return size;
  204. }
  205. size_t HLERequestContext::WriteBuffer(const std::vector<u8>& buffer) const {
  206. return WriteBuffer(buffer.data(), buffer.size());
  207. }
  208. size_t HLERequestContext::GetReadBufferSize() const {
  209. const bool is_buffer_a{BufferDescriptorA().size() && BufferDescriptorA()[0].Size()};
  210. return is_buffer_a ? BufferDescriptorA()[0].Size() : BufferDescriptorX()[0].Size();
  211. }
  212. size_t HLERequestContext::GetWriteBufferSize() const {
  213. const bool is_buffer_b{BufferDescriptorB().size() && BufferDescriptorB()[0].Size()};
  214. return is_buffer_b ? BufferDescriptorB()[0].Size() : BufferDescriptorC()[0].Size();
  215. }
  216. std::string HLERequestContext::Description() const {
  217. if (!command_header) {
  218. return "No command header available";
  219. }
  220. std::ostringstream s;
  221. s << "IPC::CommandHeader: Type:" << static_cast<u32>(command_header->type.Value());
  222. s << ", X(Pointer):" << command_header->num_buf_x_descriptors;
  223. if (command_header->num_buf_x_descriptors) {
  224. s << '[';
  225. for (u64 i = 0; i < command_header->num_buf_x_descriptors; ++i) {
  226. s << "0x" << std::hex << BufferDescriptorX()[i].Size();
  227. if (i < command_header->num_buf_x_descriptors - 1)
  228. s << ", ";
  229. }
  230. s << ']';
  231. }
  232. s << ", A(Send):" << command_header->num_buf_a_descriptors;
  233. if (command_header->num_buf_a_descriptors) {
  234. s << '[';
  235. for (u64 i = 0; i < command_header->num_buf_a_descriptors; ++i) {
  236. s << "0x" << std::hex << BufferDescriptorA()[i].Size();
  237. if (i < command_header->num_buf_a_descriptors - 1)
  238. s << ", ";
  239. }
  240. s << ']';
  241. }
  242. s << ", B(Receive):" << command_header->num_buf_b_descriptors;
  243. if (command_header->num_buf_b_descriptors) {
  244. s << '[';
  245. for (u64 i = 0; i < command_header->num_buf_b_descriptors; ++i) {
  246. s << "0x" << std::hex << BufferDescriptorB()[i].Size();
  247. if (i < command_header->num_buf_b_descriptors - 1)
  248. s << ", ";
  249. }
  250. s << ']';
  251. }
  252. s << ", C(ReceiveList):" << BufferDescriptorC().size();
  253. if (!BufferDescriptorC().empty()) {
  254. s << '[';
  255. for (u64 i = 0; i < BufferDescriptorC().size(); ++i) {
  256. s << "0x" << std::hex << BufferDescriptorC()[i].Size();
  257. if (i < BufferDescriptorC().size() - 1)
  258. s << ", ";
  259. }
  260. s << ']';
  261. }
  262. s << ", data_size:" << command_header->data_size.Value();
  263. return s.str();
  264. }
  265. } // namespace Kernel