hle_ipc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <memory>
  7. #include <string>
  8. #include <vector>
  9. #include <boost/container/small_vector.hpp>
  10. #include "common/common_types.h"
  11. #include "common/swap.h"
  12. #include "core/hle/ipc.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/kernel/server_session.h"
  15. #include "core/hle/kernel/thread.h"
  16. namespace Service {
  17. class ServiceFrameworkBase;
  18. }
  19. namespace Kernel {
  20. class Domain;
  21. class HandleTable;
  22. class HLERequestContext;
  23. class Process;
  24. class Event;
  25. /**
  26. * Interface implemented by HLE Session handlers.
  27. * This can be provided to a ServerSession in order to hook into several relevant events
  28. * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
  29. */
  30. class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
  31. public:
  32. virtual ~SessionRequestHandler() = default;
  33. /**
  34. * Handles a sync request from the emulated application.
  35. * @param server_session The ServerSession that was triggered for this sync request,
  36. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  37. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  38. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  39. * @returns ResultCode the result code of the translate operation.
  40. */
  41. virtual ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) = 0;
  42. /**
  43. * Signals that a client has just connected to this HLE handler and keeps the
  44. * associated ServerSession alive for the duration of the connection.
  45. * @param server_session Owning pointer to the ServerSession associated with the connection.
  46. */
  47. void ClientConnected(SharedPtr<ServerSession> server_session);
  48. /**
  49. * Signals that a client has just disconnected from this HLE handler and releases the
  50. * associated ServerSession.
  51. * @param server_session ServerSession associated with the connection.
  52. */
  53. void ClientDisconnected(SharedPtr<ServerSession> server_session);
  54. protected:
  55. /// List of sessions that are connected to this handler.
  56. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
  57. // for the duration of the connection.
  58. std::vector<SharedPtr<ServerSession>> connected_sessions;
  59. };
  60. /**
  61. * Class containing information about an in-flight IPC request being handled by an HLE service
  62. * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
  63. * when possible use the APIs in this class to service the request.
  64. *
  65. * HLE handle protocol
  66. * ===================
  67. *
  68. * To avoid needing HLE services to keep a separate handle table, or having to directly modify the
  69. * requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel
  70. * will decode the incoming handles into object pointers and insert a id in the buffer where the
  71. * handle would normally be. The service then calls GetIncomingHandle() with that id to get the
  72. * pointer to the object. Similarly, instead of inserting a handle into the command buffer, the
  73. * service calls AddOutgoingHandle() and stores the returned id where the handle would normally go.
  74. *
  75. * The end result is similar to just giving services their own real handle tables, but since these
  76. * ids are local to a specific context, it avoids requiring services to manage handles for objects
  77. * across multiple calls and ensuring that unneeded handles are cleaned up.
  78. */
  79. class HLERequestContext {
  80. public:
  81. HLERequestContext(SharedPtr<Kernel::ServerSession> session);
  82. ~HLERequestContext();
  83. /// Returns a pointer to the IPC command buffer for this request.
  84. u32* CommandBuffer() {
  85. return cmd_buf.data();
  86. }
  87. /**
  88. * Returns the session through which this request was made. This can be used as a map key to
  89. * access per-client data on services.
  90. */
  91. const SharedPtr<Kernel::ServerSession>& Session() const {
  92. return server_session;
  93. }
  94. using WakeupCallback = std::function<void(SharedPtr<Thread> thread, HLERequestContext& context,
  95. ThreadWakeupReason reason)>;
  96. /**
  97. * Puts the specified guest thread to sleep until the returned event is signaled or until the
  98. * specified timeout expires.
  99. * @param thread Thread to be put to sleep.
  100. * @param reason Reason for pausing the thread, to be used for debugging purposes.
  101. * @param timeout Timeout in nanoseconds after which the thread will be awoken and the callback
  102. * invoked with a Timeout reason.
  103. * @param callback Callback to be invoked when the thread is resumed. This callback must write
  104. * the entire command response once again, regardless of the state of it before this function
  105. * was called.
  106. * @returns Event that when signaled will resume the thread and call the callback function.
  107. */
  108. SharedPtr<Event> SleepClientThread(SharedPtr<Thread> thread, const std::string& reason,
  109. u64 timeout, WakeupCallback&& callback);
  110. void ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming);
  111. /// Populates this context with data from the requesting process/thread.
  112. ResultCode PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf, Process& src_process,
  113. HandleTable& src_table);
  114. /// Writes data from this context back to the requesting process/thread.
  115. ResultCode WriteToOutgoingCommandBuffer(Thread& thread);
  116. u32_le GetCommand() const {
  117. return command;
  118. }
  119. IPC::CommandType GetCommandType() const {
  120. return command_header->type;
  121. }
  122. unsigned GetDataPayloadOffset() const {
  123. return data_payload_offset;
  124. }
  125. const std::vector<IPC::BufferDescriptorX>& BufferDescriptorX() const {
  126. return buffer_x_desciptors;
  127. }
  128. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorA() const {
  129. return buffer_a_desciptors;
  130. }
  131. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorB() const {
  132. return buffer_b_desciptors;
  133. }
  134. const std::vector<IPC::BufferDescriptorC>& BufferDescriptorC() const {
  135. return buffer_c_desciptors;
  136. }
  137. const std::shared_ptr<IPC::DomainMessageHeader>& GetDomainMessageHeader() const {
  138. return domain_message_header;
  139. }
  140. /// Helper function to read a buffer using the appropriate buffer descriptor
  141. std::vector<u8> ReadBuffer(int buffer_index = 0) const;
  142. /// Helper function to write a buffer using the appropriate buffer descriptor
  143. size_t WriteBuffer(const void* buffer, size_t size, int buffer_index = 0) const;
  144. /// Helper function to write a buffer using the appropriate buffer descriptor
  145. size_t WriteBuffer(const std::vector<u8>& buffer, int buffer_index = 0) const;
  146. /// Helper function to get the size of the input buffer
  147. size_t GetReadBufferSize(int buffer_index = 0) const;
  148. /// Helper function to get the size of the output buffer
  149. size_t GetWriteBufferSize(int buffer_index = 0) const;
  150. template <typename T>
  151. SharedPtr<T> GetCopyObject(size_t index) {
  152. ASSERT(index < copy_objects.size());
  153. return DynamicObjectCast<T>(copy_objects[index]);
  154. }
  155. template <typename T>
  156. SharedPtr<T> GetMoveObject(size_t index) {
  157. ASSERT(index < move_objects.size());
  158. return DynamicObjectCast<T>(move_objects[index]);
  159. }
  160. void AddMoveObject(SharedPtr<Object> object) {
  161. move_objects.emplace_back(std::move(object));
  162. }
  163. void AddCopyObject(SharedPtr<Object> object) {
  164. copy_objects.emplace_back(std::move(object));
  165. }
  166. void AddDomainObject(std::shared_ptr<SessionRequestHandler> object) {
  167. domain_objects.emplace_back(std::move(object));
  168. }
  169. /// Clears the list of objects so that no lingering objects are written accidentally to the
  170. /// response buffer.
  171. void ClearIncomingObjects() {
  172. move_objects.clear();
  173. copy_objects.clear();
  174. domain_objects.clear();
  175. }
  176. size_t NumMoveObjects() const {
  177. return move_objects.size();
  178. }
  179. size_t NumCopyObjects() const {
  180. return copy_objects.size();
  181. }
  182. size_t NumDomainObjects() const {
  183. return domain_objects.size();
  184. }
  185. std::string Description() const;
  186. private:
  187. std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
  188. SharedPtr<Kernel::ServerSession> server_session;
  189. // TODO(yuriks): Check common usage of this and optimize size accordingly
  190. boost::container::small_vector<SharedPtr<Object>, 8> move_objects;
  191. boost::container::small_vector<SharedPtr<Object>, 8> copy_objects;
  192. boost::container::small_vector<std::shared_ptr<SessionRequestHandler>, 8> domain_objects;
  193. std::shared_ptr<IPC::CommandHeader> command_header;
  194. std::shared_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header;
  195. std::shared_ptr<IPC::DataPayloadHeader> data_payload_header;
  196. std::shared_ptr<IPC::DomainMessageHeader> domain_message_header;
  197. std::vector<IPC::BufferDescriptorX> buffer_x_desciptors;
  198. std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors;
  199. std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors;
  200. std::vector<IPC::BufferDescriptorABW> buffer_w_desciptors;
  201. std::vector<IPC::BufferDescriptorC> buffer_c_desciptors;
  202. unsigned data_payload_offset{};
  203. unsigned buffer_c_offset{};
  204. u32_le command{};
  205. };
  206. } // namespace Kernel