hle_ipc.cpp 15 KB

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