hle_ipc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <sstream>
  7. #include <utility>
  8. #include <boost/range/algorithm_ext/erase.hpp>
  9. #include "common/assert.h"
  10. #include "common/common_funcs.h"
  11. #include "common/common_types.h"
  12. #include "common/logging/log.h"
  13. #include "core/hle/ipc_helpers.h"
  14. #include "core/hle/kernel/handle_table.h"
  15. #include "core/hle/kernel/hle_ipc.h"
  16. #include "core/hle/kernel/k_readable_event.h"
  17. #include "core/hle/kernel/k_scheduler.h"
  18. #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
  19. #include "core/hle/kernel/k_server_session.h"
  20. #include "core/hle/kernel/k_thread.h"
  21. #include "core/hle/kernel/k_writable_event.h"
  22. #include "core/hle/kernel/kernel.h"
  23. #include "core/hle/kernel/object.h"
  24. #include "core/hle/kernel/process.h"
  25. #include "core/hle/kernel/svc_results.h"
  26. #include "core/hle/kernel/time_manager.h"
  27. #include "core/memory.h"
  28. namespace Kernel {
  29. SessionRequestHandler::SessionRequestHandler() = default;
  30. SessionRequestHandler::~SessionRequestHandler() = default;
  31. void SessionRequestHandler::ClientConnected(KSession* session) {
  32. session->GetServerSession().SetHleHandler(shared_from_this());
  33. sessions.push_back(session);
  34. }
  35. void SessionRequestHandler::ClientDisconnected(KSession* session) {
  36. session->GetServerSession().SetHleHandler(nullptr);
  37. boost::range::remove_erase(sessions, session);
  38. }
  39. HLERequestContext::HLERequestContext(KernelCore& kernel_, Core::Memory::Memory& memory_,
  40. KServerSession* server_session_, KThread* thread_)
  41. : server_session(server_session_), thread(thread_), kernel{kernel_}, memory{memory_} {
  42. cmd_buf[0] = 0;
  43. }
  44. HLERequestContext::~HLERequestContext() = default;
  45. void HLERequestContext::ParseCommandBuffer(const HandleTable& handle_table, u32_le* src_cmdbuf,
  46. bool incoming) {
  47. IPC::RequestParser rp(src_cmdbuf);
  48. command_header = rp.PopRaw<IPC::CommandHeader>();
  49. if (command_header->type == IPC::CommandType::Close) {
  50. // Close does not populate the rest of the IPC header
  51. return;
  52. }
  53. // If handle descriptor is present, add size of it
  54. if (command_header->enable_handle_descriptor) {
  55. handle_descriptor_header = rp.PopRaw<IPC::HandleDescriptorHeader>();
  56. if (handle_descriptor_header->send_current_pid) {
  57. rp.Skip(2, false);
  58. }
  59. if (incoming) {
  60. // Populate the object lists with the data in the IPC request.
  61. for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_copy; ++handle) {
  62. const u32 copy_handle{rp.Pop<Handle>()};
  63. copy_handles.push_back(copy_handle);
  64. copy_objects.push_back(handle_table.GetObject(copy_handle).GetPointerUnsafe());
  65. }
  66. for (u32 handle = 0; handle < handle_descriptor_header->num_handles_to_move; ++handle) {
  67. const u32 move_handle{rp.Pop<Handle>()};
  68. move_handles.push_back(move_handle);
  69. move_objects.push_back(handle_table.GetObject(move_handle).GetPointerUnsafe());
  70. }
  71. } else {
  72. // For responses we just ignore the handles, they're empty and will be populated when
  73. // translating the response.
  74. rp.Skip(handle_descriptor_header->num_handles_to_copy, false);
  75. rp.Skip(handle_descriptor_header->num_handles_to_move, false);
  76. }
  77. }
  78. for (unsigned i = 0; i < command_header->num_buf_x_descriptors; ++i) {
  79. buffer_x_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorX>());
  80. }
  81. for (unsigned i = 0; i < command_header->num_buf_a_descriptors; ++i) {
  82. buffer_a_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  83. }
  84. for (unsigned i = 0; i < command_header->num_buf_b_descriptors; ++i) {
  85. buffer_b_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  86. }
  87. for (unsigned i = 0; i < command_header->num_buf_w_descriptors; ++i) {
  88. buffer_w_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorABW>());
  89. }
  90. buffer_c_offset = rp.GetCurrentOffset() + command_header->data_size;
  91. // Padding to align to 16 bytes
  92. rp.AlignWithPadding();
  93. if (Session()->IsDomain() && ((command_header->type == IPC::CommandType::Request ||
  94. command_header->type == IPC::CommandType::RequestWithContext) ||
  95. !incoming)) {
  96. // If this is an incoming message, only CommandType "Request" has a domain header
  97. // All outgoing domain messages have the domain header, if only incoming has it
  98. if (incoming || domain_message_header) {
  99. domain_message_header = rp.PopRaw<IPC::DomainMessageHeader>();
  100. } else {
  101. if (Session()->IsDomain()) {
  102. LOG_WARNING(IPC, "Domain request has no DomainMessageHeader!");
  103. }
  104. }
  105. }
  106. data_payload_header = rp.PopRaw<IPC::DataPayloadHeader>();
  107. data_payload_offset = rp.GetCurrentOffset();
  108. if (domain_message_header && domain_message_header->command ==
  109. IPC::DomainMessageHeader::CommandType::CloseVirtualHandle) {
  110. // CloseVirtualHandle command does not have SFC* or any data
  111. return;
  112. }
  113. if (incoming) {
  114. ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'I'));
  115. } else {
  116. ASSERT(data_payload_header->magic == Common::MakeMagic('S', 'F', 'C', 'O'));
  117. }
  118. rp.SetCurrentOffset(buffer_c_offset);
  119. // For Inline buffers, the response data is written directly to buffer_c_offset
  120. // and in this case we don't have any BufferDescriptorC on the request.
  121. if (command_header->buf_c_descriptor_flags >
  122. IPC::CommandHeader::BufferDescriptorCFlag::InlineDescriptor) {
  123. if (command_header->buf_c_descriptor_flags ==
  124. IPC::CommandHeader::BufferDescriptorCFlag::OneDescriptor) {
  125. buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
  126. } else {
  127. unsigned num_buf_c_descriptors =
  128. static_cast<unsigned>(command_header->buf_c_descriptor_flags.Value()) - 2;
  129. // This is used to detect possible underflows, in case something is broken
  130. // with the two ifs above and the flags value is == 0 || == 1.
  131. ASSERT(num_buf_c_descriptors < 14);
  132. for (unsigned i = 0; i < num_buf_c_descriptors; ++i) {
  133. buffer_c_desciptors.push_back(rp.PopRaw<IPC::BufferDescriptorC>());
  134. }
  135. }
  136. }
  137. rp.SetCurrentOffset(data_payload_offset);
  138. command = rp.Pop<u32_le>();
  139. rp.Skip(1, false); // The command is actually an u64, but we don't use the high part.
  140. }
  141. ResultCode HLERequestContext::PopulateFromIncomingCommandBuffer(const HandleTable& handle_table,
  142. u32_le* src_cmdbuf) {
  143. ParseCommandBuffer(handle_table, src_cmdbuf, true);
  144. if (command_header->type == IPC::CommandType::Close) {
  145. // Close does not populate the rest of the IPC header
  146. return RESULT_SUCCESS;
  147. }
  148. // The data_size already includes the payload header, the padding and the domain header.
  149. std::size_t size = data_payload_offset + command_header->data_size -
  150. sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
  151. if (domain_message_header)
  152. size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
  153. std::copy_n(src_cmdbuf, size, cmd_buf.begin());
  154. return RESULT_SUCCESS;
  155. }
  156. ResultCode HLERequestContext::WriteToOutgoingCommandBuffer(KThread& thread) {
  157. auto& owner_process = *thread.GetOwnerProcess();
  158. auto& handle_table = owner_process.GetHandleTable();
  159. std::array<u32, IPC::COMMAND_BUFFER_LENGTH> dst_cmdbuf;
  160. memory.ReadBlock(owner_process, thread.GetTLSAddress(), dst_cmdbuf.data(),
  161. dst_cmdbuf.size() * sizeof(u32));
  162. // The header was already built in the internal command buffer. Attempt to parse it to verify
  163. // the integrity and then copy it over to the target command buffer.
  164. ParseCommandBuffer(handle_table, cmd_buf.data(), false);
  165. // The data_size already includes the payload header, the padding and the domain header.
  166. std::size_t size = data_payload_offset + command_header->data_size -
  167. sizeof(IPC::DataPayloadHeader) / sizeof(u32) - 4;
  168. if (domain_message_header)
  169. size -= sizeof(IPC::DomainMessageHeader) / sizeof(u32);
  170. std::copy_n(cmd_buf.begin(), size, dst_cmdbuf.data());
  171. if (command_header->enable_handle_descriptor) {
  172. ASSERT_MSG(!move_objects.empty() || !copy_objects.empty(),
  173. "Handle descriptor bit set but no handles to translate");
  174. // We write the translated handles at a specific offset in the command buffer, this space
  175. // was already reserved when writing the header.
  176. std::size_t current_offset =
  177. (sizeof(IPC::CommandHeader) + sizeof(IPC::HandleDescriptorHeader)) / sizeof(u32);
  178. ASSERT_MSG(!handle_descriptor_header->send_current_pid, "Sending PID is not implemented");
  179. ASSERT(copy_objects.size() == handle_descriptor_header->num_handles_to_copy);
  180. ASSERT(move_objects.size() == handle_descriptor_header->num_handles_to_move);
  181. // We don't make a distinction between copy and move handles when translating since HLE
  182. // services don't deal with handles directly. However, the guest applications might check
  183. // for specific values in each of these descriptors.
  184. for (auto& object : copy_objects) {
  185. ASSERT(object != nullptr);
  186. R_TRY(handle_table.Add(&dst_cmdbuf[current_offset++], object));
  187. }
  188. for (auto& object : move_objects) {
  189. ASSERT(object != nullptr);
  190. R_TRY(handle_table.Add(&dst_cmdbuf[current_offset++], object));
  191. }
  192. }
  193. // TODO(Subv): Translate the X/A/B/W buffers.
  194. if (Session()->IsDomain() && domain_message_header) {
  195. ASSERT(domain_message_header->num_objects == domain_objects.size());
  196. // Write the domain objects to the command buffer, these go after the raw untranslated data.
  197. // TODO(Subv): This completely ignores C buffers.
  198. std::size_t domain_offset = size - domain_message_header->num_objects;
  199. for (const auto& object : domain_objects) {
  200. server_session->AppendDomainRequestHandler(object);
  201. dst_cmdbuf[domain_offset++] =
  202. static_cast<u32_le>(server_session->NumDomainRequestHandlers());
  203. }
  204. }
  205. // Copy the translated command buffer back into the thread's command buffer area.
  206. memory.WriteBlock(owner_process, thread.GetTLSAddress(), dst_cmdbuf.data(),
  207. dst_cmdbuf.size() * sizeof(u32));
  208. return RESULT_SUCCESS;
  209. }
  210. std::vector<u8> HLERequestContext::ReadBuffer(std::size_t buffer_index) const {
  211. std::vector<u8> buffer{};
  212. const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
  213. BufferDescriptorA()[buffer_index].Size()};
  214. if (is_buffer_a) {
  215. ASSERT_OR_EXECUTE_MSG(
  216. BufferDescriptorA().size() > buffer_index, { return buffer; },
  217. "BufferDescriptorA invalid buffer_index {}", buffer_index);
  218. buffer.resize(BufferDescriptorA()[buffer_index].Size());
  219. memory.ReadBlock(BufferDescriptorA()[buffer_index].Address(), buffer.data(), buffer.size());
  220. } else {
  221. ASSERT_OR_EXECUTE_MSG(
  222. BufferDescriptorX().size() > buffer_index, { return buffer; },
  223. "BufferDescriptorX invalid buffer_index {}", buffer_index);
  224. buffer.resize(BufferDescriptorX()[buffer_index].Size());
  225. memory.ReadBlock(BufferDescriptorX()[buffer_index].Address(), buffer.data(), buffer.size());
  226. }
  227. return buffer;
  228. }
  229. std::size_t HLERequestContext::WriteBuffer(const void* buffer, std::size_t size,
  230. std::size_t buffer_index) const {
  231. if (size == 0) {
  232. LOG_WARNING(Core, "skip empty buffer write");
  233. return 0;
  234. }
  235. const bool is_buffer_b{BufferDescriptorB().size() > buffer_index &&
  236. BufferDescriptorB()[buffer_index].Size()};
  237. const std::size_t buffer_size{GetWriteBufferSize(buffer_index)};
  238. if (size > buffer_size) {
  239. LOG_CRITICAL(Core, "size ({:016X}) is greater than buffer_size ({:016X})", size,
  240. buffer_size);
  241. size = buffer_size; // TODO(bunnei): This needs to be HW tested
  242. }
  243. if (is_buffer_b) {
  244. ASSERT_OR_EXECUTE_MSG(
  245. BufferDescriptorB().size() > buffer_index &&
  246. BufferDescriptorB()[buffer_index].Size() >= size,
  247. { return 0; }, "BufferDescriptorB is invalid, index={}, size={}", buffer_index, size);
  248. memory.WriteBlock(BufferDescriptorB()[buffer_index].Address(), buffer, size);
  249. } else {
  250. ASSERT_OR_EXECUTE_MSG(
  251. BufferDescriptorC().size() > buffer_index &&
  252. BufferDescriptorC()[buffer_index].Size() >= size,
  253. { return 0; }, "BufferDescriptorC is invalid, index={}, size={}", buffer_index, size);
  254. memory.WriteBlock(BufferDescriptorC()[buffer_index].Address(), buffer, size);
  255. }
  256. return size;
  257. }
  258. std::size_t HLERequestContext::GetReadBufferSize(std::size_t buffer_index) const {
  259. const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
  260. BufferDescriptorA()[buffer_index].Size()};
  261. if (is_buffer_a) {
  262. ASSERT_OR_EXECUTE_MSG(
  263. BufferDescriptorA().size() > buffer_index, { return 0; },
  264. "BufferDescriptorA invalid buffer_index {}", buffer_index);
  265. return BufferDescriptorA()[buffer_index].Size();
  266. } else {
  267. ASSERT_OR_EXECUTE_MSG(
  268. BufferDescriptorX().size() > buffer_index, { return 0; },
  269. "BufferDescriptorX invalid buffer_index {}", buffer_index);
  270. return BufferDescriptorX()[buffer_index].Size();
  271. }
  272. }
  273. std::size_t HLERequestContext::GetWriteBufferSize(std::size_t buffer_index) const {
  274. const bool is_buffer_b{BufferDescriptorB().size() > buffer_index &&
  275. BufferDescriptorB()[buffer_index].Size()};
  276. if (is_buffer_b) {
  277. ASSERT_OR_EXECUTE_MSG(
  278. BufferDescriptorB().size() > buffer_index, { return 0; },
  279. "BufferDescriptorB invalid buffer_index {}", buffer_index);
  280. return BufferDescriptorB()[buffer_index].Size();
  281. } else {
  282. ASSERT_OR_EXECUTE_MSG(
  283. BufferDescriptorC().size() > buffer_index, { return 0; },
  284. "BufferDescriptorC invalid buffer_index {}", buffer_index);
  285. return BufferDescriptorC()[buffer_index].Size();
  286. }
  287. return 0;
  288. }
  289. bool HLERequestContext::CanReadBuffer(std::size_t buffer_index) const {
  290. const bool is_buffer_a{BufferDescriptorA().size() > buffer_index &&
  291. BufferDescriptorA()[buffer_index].Size()};
  292. if (is_buffer_a) {
  293. return BufferDescriptorA().size() > buffer_index;
  294. } else {
  295. return BufferDescriptorX().size() > buffer_index;
  296. }
  297. }
  298. bool HLERequestContext::CanWriteBuffer(std::size_t buffer_index) const {
  299. const bool is_buffer_b{BufferDescriptorB().size() > buffer_index &&
  300. BufferDescriptorB()[buffer_index].Size()};
  301. if (is_buffer_b) {
  302. return BufferDescriptorB().size() > buffer_index;
  303. } else {
  304. return BufferDescriptorC().size() > buffer_index;
  305. }
  306. }
  307. std::string HLERequestContext::Description() const {
  308. if (!command_header) {
  309. return "No command header available";
  310. }
  311. std::ostringstream s;
  312. s << "IPC::CommandHeader: Type:" << static_cast<u32>(command_header->type.Value());
  313. s << ", X(Pointer):" << command_header->num_buf_x_descriptors;
  314. if (command_header->num_buf_x_descriptors) {
  315. s << '[';
  316. for (u64 i = 0; i < command_header->num_buf_x_descriptors; ++i) {
  317. s << "0x" << std::hex << BufferDescriptorX()[i].Size();
  318. if (i < command_header->num_buf_x_descriptors - 1)
  319. s << ", ";
  320. }
  321. s << ']';
  322. }
  323. s << ", A(Send):" << command_header->num_buf_a_descriptors;
  324. if (command_header->num_buf_a_descriptors) {
  325. s << '[';
  326. for (u64 i = 0; i < command_header->num_buf_a_descriptors; ++i) {
  327. s << "0x" << std::hex << BufferDescriptorA()[i].Size();
  328. if (i < command_header->num_buf_a_descriptors - 1)
  329. s << ", ";
  330. }
  331. s << ']';
  332. }
  333. s << ", B(Receive):" << command_header->num_buf_b_descriptors;
  334. if (command_header->num_buf_b_descriptors) {
  335. s << '[';
  336. for (u64 i = 0; i < command_header->num_buf_b_descriptors; ++i) {
  337. s << "0x" << std::hex << BufferDescriptorB()[i].Size();
  338. if (i < command_header->num_buf_b_descriptors - 1)
  339. s << ", ";
  340. }
  341. s << ']';
  342. }
  343. s << ", C(ReceiveList):" << BufferDescriptorC().size();
  344. if (!BufferDescriptorC().empty()) {
  345. s << '[';
  346. for (u64 i = 0; i < BufferDescriptorC().size(); ++i) {
  347. s << "0x" << std::hex << BufferDescriptorC()[i].Size();
  348. if (i < BufferDescriptorC().size() - 1)
  349. s << ", ";
  350. }
  351. s << ']';
  352. }
  353. s << ", data_size:" << command_header->data_size.Value();
  354. return s.str();
  355. }
  356. } // namespace Kernel