hle_ipc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <functional>
  7. #include <memory>
  8. #include <optional>
  9. #include <string>
  10. #include <type_traits>
  11. #include <vector>
  12. #include <boost/container/small_vector.hpp>
  13. #include "common/common_types.h"
  14. #include "common/concepts.h"
  15. #include "common/swap.h"
  16. #include "core/hle/ipc.h"
  17. #include "core/hle/kernel/k_auto_object.h"
  18. #include "core/hle/kernel/svc_common.h"
  19. union ResultCode;
  20. namespace Core::Memory {
  21. class Memory;
  22. }
  23. namespace IPC {
  24. class ResponseBuilder;
  25. }
  26. namespace Service {
  27. class ServiceFrameworkBase;
  28. }
  29. namespace Kernel {
  30. class Domain;
  31. class HLERequestContext;
  32. class KernelCore;
  33. class KHandleTable;
  34. class KProcess;
  35. class KServerSession;
  36. class KThread;
  37. class KReadableEvent;
  38. class KSession;
  39. class KWritableEvent;
  40. enum class ThreadWakeupReason;
  41. /**
  42. * Interface implemented by HLE Session handlers.
  43. * This can be provided to a ServerSession in order to hook into several relevant events
  44. * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
  45. */
  46. class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
  47. public:
  48. SessionRequestHandler();
  49. virtual ~SessionRequestHandler();
  50. /**
  51. * Handles a sync request from the emulated application.
  52. * @param server_session The ServerSession that was triggered for this sync request,
  53. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  54. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  55. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  56. * @returns ResultCode the result code of the translate operation.
  57. */
  58. virtual ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) = 0;
  59. /**
  60. * Signals that a client has just connected to this HLE handler and keeps the
  61. * associated ServerSession alive for the duration of the connection.
  62. * @param server_session Owning pointer to the ServerSession associated with the connection.
  63. */
  64. void ClientConnected(KServerSession* session);
  65. /**
  66. * Signals that a client has just disconnected from this HLE handler and releases the
  67. * associated ServerSession.
  68. * @param server_session ServerSession associated with the connection.
  69. */
  70. void ClientDisconnected(KServerSession* session);
  71. };
  72. /**
  73. * Class containing information about an in-flight IPC request being handled by an HLE service
  74. * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
  75. * when possible use the APIs in this class to service the request.
  76. *
  77. * HLE handle protocol
  78. * ===================
  79. *
  80. * To avoid needing HLE services to keep a separate handle table, or having to directly modify the
  81. * requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel
  82. * will decode the incoming handles into object pointers and insert a id in the buffer where the
  83. * handle would normally be. The service then calls GetIncomingHandle() with that id to get the
  84. * pointer to the object. Similarly, instead of inserting a handle into the command buffer, the
  85. * service calls AddOutgoingHandle() and stores the returned id where the handle would normally go.
  86. *
  87. * The end result is similar to just giving services their own real handle tables, but since these
  88. * ids are local to a specific context, it avoids requiring services to manage handles for objects
  89. * across multiple calls and ensuring that unneeded handles are cleaned up.
  90. */
  91. class HLERequestContext {
  92. public:
  93. explicit HLERequestContext(KernelCore& kernel, Core::Memory::Memory& memory,
  94. KServerSession* session, KThread* thread);
  95. ~HLERequestContext();
  96. /// Returns a pointer to the IPC command buffer for this request.
  97. u32* CommandBuffer() {
  98. return cmd_buf.data();
  99. }
  100. /**
  101. * Returns the session through which this request was made. This can be used as a map key to
  102. * access per-client data on services.
  103. */
  104. Kernel::KServerSession* Session() {
  105. return server_session;
  106. }
  107. /// Populates this context with data from the requesting process/thread.
  108. ResultCode PopulateFromIncomingCommandBuffer(const KHandleTable& handle_table,
  109. u32_le* src_cmdbuf);
  110. /// Writes data from this context back to the requesting process/thread.
  111. ResultCode WriteToOutgoingCommandBuffer(KThread& requesting_thread);
  112. u32_le GetCommand() const {
  113. return command;
  114. }
  115. bool IsTipc() const {
  116. return command_header->IsTipc();
  117. }
  118. IPC::CommandType GetCommandType() const {
  119. return command_header->type;
  120. }
  121. unsigned GetDataPayloadOffset() const {
  122. return data_payload_offset;
  123. }
  124. const std::vector<IPC::BufferDescriptorX>& BufferDescriptorX() const {
  125. return buffer_x_desciptors;
  126. }
  127. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorA() const {
  128. return buffer_a_desciptors;
  129. }
  130. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorB() const {
  131. return buffer_b_desciptors;
  132. }
  133. const std::vector<IPC::BufferDescriptorC>& BufferDescriptorC() const {
  134. return buffer_c_desciptors;
  135. }
  136. const IPC::DomainMessageHeader& GetDomainMessageHeader() const {
  137. return domain_message_header.value();
  138. }
  139. bool HasDomainMessageHeader() const {
  140. return domain_message_header.has_value();
  141. }
  142. /// Helper function to read a buffer using the appropriate buffer descriptor
  143. std::vector<u8> ReadBuffer(std::size_t buffer_index = 0) const;
  144. /// Helper function to write a buffer using the appropriate buffer descriptor
  145. std::size_t WriteBuffer(const void* buffer, std::size_t size,
  146. std::size_t buffer_index = 0) const;
  147. /* Helper function to write a buffer using the appropriate buffer descriptor
  148. *
  149. * @tparam T an arbitrary container that satisfies the
  150. * ContiguousContainer concept in the C++ standard library or a trivially copyable type.
  151. *
  152. * @param data The container/data to write into a buffer.
  153. * @param buffer_index The buffer in particular to write to.
  154. */
  155. template <typename T, typename = std::enable_if_t<!std::is_pointer_v<T>>>
  156. std::size_t WriteBuffer(const T& data, std::size_t buffer_index = 0) const {
  157. if constexpr (Common::IsSTLContainer<T>) {
  158. using ContiguousType = typename T::value_type;
  159. static_assert(std::is_trivially_copyable_v<ContiguousType>,
  160. "Container to WriteBuffer must contain trivially copyable objects");
  161. return WriteBuffer(std::data(data), std::size(data) * sizeof(ContiguousType),
  162. buffer_index);
  163. } else {
  164. static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable");
  165. return WriteBuffer(&data, sizeof(T), buffer_index);
  166. }
  167. }
  168. /// Helper function to get the size of the input buffer
  169. std::size_t GetReadBufferSize(std::size_t buffer_index = 0) const;
  170. /// Helper function to get the size of the output buffer
  171. std::size_t GetWriteBufferSize(std::size_t buffer_index = 0) const;
  172. /// Helper function to test whether the input buffer at buffer_index can be read
  173. bool CanReadBuffer(std::size_t buffer_index = 0) const;
  174. /// Helper function to test whether the output buffer at buffer_index can be written
  175. bool CanWriteBuffer(std::size_t buffer_index = 0) const;
  176. Handle GetCopyHandle(std::size_t index) const {
  177. return copy_handles.at(index);
  178. }
  179. Handle GetMoveHandle(std::size_t index) const {
  180. return move_handles.at(index);
  181. }
  182. void AddMoveObject(KAutoObject* object) {
  183. move_objects.emplace_back(object);
  184. }
  185. void AddCopyObject(KAutoObject* object) {
  186. copy_objects.emplace_back(object);
  187. }
  188. void AddDomainObject(std::shared_ptr<SessionRequestHandler> object) {
  189. domain_objects.emplace_back(std::move(object));
  190. }
  191. template <typename T>
  192. std::shared_ptr<T> GetDomainRequestHandler(std::size_t index) const {
  193. return std::static_pointer_cast<T>(domain_request_handlers.at(index));
  194. }
  195. void SetDomainRequestHandlers(
  196. const std::vector<std::shared_ptr<SessionRequestHandler>>& handlers) {
  197. domain_request_handlers = handlers;
  198. }
  199. /// Clears the list of objects so that no lingering objects are written accidentally to the
  200. /// response buffer.
  201. void ClearIncomingObjects() {
  202. move_objects.clear();
  203. copy_objects.clear();
  204. domain_objects.clear();
  205. }
  206. std::size_t NumMoveObjects() const {
  207. return move_objects.size();
  208. }
  209. std::size_t NumCopyObjects() const {
  210. return copy_objects.size();
  211. }
  212. std::size_t NumDomainObjects() const {
  213. return domain_objects.size();
  214. }
  215. std::string Description() const;
  216. KThread& GetThread() {
  217. return *thread;
  218. }
  219. bool IsThreadWaiting() const {
  220. return is_thread_waiting;
  221. }
  222. private:
  223. friend class IPC::ResponseBuilder;
  224. void ParseCommandBuffer(const KHandleTable& handle_table, u32_le* src_cmdbuf, bool incoming);
  225. std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
  226. Kernel::KServerSession* server_session{};
  227. KThread* thread;
  228. // TODO(yuriks): Check common usage of this and optimize size accordingly
  229. boost::container::small_vector<Handle, 8> move_handles;
  230. boost::container::small_vector<Handle, 8> copy_handles;
  231. boost::container::small_vector<KAutoObject*, 8> move_objects;
  232. boost::container::small_vector<KAutoObject*, 8> copy_objects;
  233. boost::container::small_vector<std::shared_ptr<SessionRequestHandler>, 8> domain_objects;
  234. std::optional<IPC::CommandHeader> command_header;
  235. std::optional<IPC::HandleDescriptorHeader> handle_descriptor_header;
  236. std::optional<IPC::DataPayloadHeader> data_payload_header;
  237. std::optional<IPC::DomainMessageHeader> domain_message_header;
  238. std::vector<IPC::BufferDescriptorX> buffer_x_desciptors;
  239. std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors;
  240. std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors;
  241. std::vector<IPC::BufferDescriptorABW> buffer_w_desciptors;
  242. std::vector<IPC::BufferDescriptorC> buffer_c_desciptors;
  243. u32 data_payload_offset{};
  244. u32 buffer_c_offset{};
  245. u32 handles_offset{};
  246. u32 domain_offset{};
  247. u32_le command{};
  248. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  249. bool is_thread_waiting{};
  250. KernelCore& kernel;
  251. Core::Memory::Memory& memory;
  252. };
  253. } // namespace Kernel