hle_ipc.cpp 15 KB

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