hle_ipc.cpp 16 KB

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