hle_ipc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2017 Citra Emulator Project
  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 <vector>
  8. #include <boost/container/small_vector.hpp>
  9. #include "common/common_types.h"
  10. #include "common/swap.h"
  11. #include "core/hle/ipc.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/server_session.h"
  14. namespace Service {
  15. class ServiceFrameworkBase;
  16. }
  17. namespace Kernel {
  18. class Domain;
  19. class HandleTable;
  20. class HLERequestContext;
  21. class Process;
  22. /**
  23. * Interface implemented by HLE Session handlers.
  24. * This can be provided to a ServerSession in order to hook into several relevant events
  25. * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
  26. */
  27. class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
  28. public:
  29. virtual ~SessionRequestHandler() = default;
  30. /**
  31. * Handles a sync request from the emulated application.
  32. * @param server_session The ServerSession that was triggered for this sync request,
  33. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  34. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  35. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  36. * @returns ResultCode the result code of the translate operation.
  37. */
  38. virtual ResultCode HandleSyncRequest(Kernel::HLERequestContext& context) = 0;
  39. /**
  40. * Signals that a client has just connected to this HLE handler and keeps the
  41. * associated ServerSession alive for the duration of the connection.
  42. * @param server_session Owning pointer to the ServerSession associated with the connection.
  43. */
  44. void ClientConnected(SharedPtr<ServerSession> server_session);
  45. /**
  46. * Signals that a client has just disconnected from this HLE handler and releases the
  47. * associated ServerSession.
  48. * @param server_session ServerSession associated with the connection.
  49. */
  50. void ClientDisconnected(SharedPtr<ServerSession> server_session);
  51. protected:
  52. /// List of sessions that are connected to this handler.
  53. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
  54. // for the duration of the connection.
  55. std::vector<SharedPtr<ServerSession>> connected_sessions;
  56. };
  57. /**
  58. * Class containing information about an in-flight IPC request being handled by an HLE service
  59. * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
  60. * when possible use the APIs in this class to service the request.
  61. *
  62. * HLE handle protocol
  63. * ===================
  64. *
  65. * To avoid needing HLE services to keep a separate handle table, or having to directly modify the
  66. * requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel
  67. * will decode the incoming handles into object pointers and insert a id in the buffer where the
  68. * handle would normally be. The service then calls GetIncomingHandle() with that id to get the
  69. * pointer to the object. Similarly, instead of inserting a handle into the command buffer, the
  70. * service calls AddOutgoingHandle() and stores the returned id where the handle would normally go.
  71. *
  72. * The end result is similar to just giving services their own real handle tables, but since these
  73. * ids are local to a specific context, it avoids requiring services to manage handles for objects
  74. * across multiple calls and ensuring that unneeded handles are cleaned up.
  75. */
  76. class HLERequestContext {
  77. public:
  78. HLERequestContext(SharedPtr<Kernel::Domain> domain);
  79. HLERequestContext(SharedPtr<Kernel::ServerSession> session);
  80. ~HLERequestContext();
  81. /// Returns a pointer to the IPC command buffer for this request.
  82. u32* CommandBuffer() {
  83. return cmd_buf.data();
  84. }
  85. /**
  86. * Returns the domain through which this request was made.
  87. */
  88. const SharedPtr<Kernel::Domain>& Domain() const {
  89. return domain;
  90. }
  91. /**
  92. * Returns the session through which this request was made. This can be used as a map key to
  93. * access per-client data on services.
  94. */
  95. const SharedPtr<Kernel::ServerSession>& ServerSession() const {
  96. return server_session;
  97. }
  98. void ParseCommandBuffer(u32_le* src_cmdbuf, bool incoming);
  99. /// Populates this context with data from the requesting process/thread.
  100. ResultCode PopulateFromIncomingCommandBuffer(u32_le* src_cmdbuf, Process& src_process,
  101. HandleTable& src_table);
  102. /// Writes data from this context back to the requesting process/thread.
  103. ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
  104. HandleTable& dst_table);
  105. u32_le GetCommand() const {
  106. return command;
  107. }
  108. IPC::CommandType GetCommandType() const {
  109. return command_header->type;
  110. }
  111. unsigned GetDataPayloadOffset() const {
  112. return data_payload_offset;
  113. }
  114. const std::vector<IPC::BufferDescriptorX>& BufferDescriptorX() const {
  115. return buffer_x_desciptors;
  116. }
  117. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorA() const {
  118. return buffer_a_desciptors;
  119. }
  120. const std::vector<IPC::BufferDescriptorABW>& BufferDescriptorB() const {
  121. return buffer_b_desciptors;
  122. }
  123. const std::unique_ptr<IPC::DomainMessageHeader>& GetDomainMessageHeader() const {
  124. return domain_message_header;
  125. }
  126. bool IsDomain() const {
  127. return domain != nullptr;
  128. }
  129. template<typename T>
  130. SharedPtr<T> GetCopyObject(size_t index) {
  131. ASSERT(index < copy_objects.size());
  132. return DynamicObjectCast<T>(copy_objects[index]);
  133. }
  134. template<typename T>
  135. SharedPtr<T> GetMoveObject(size_t index) {
  136. ASSERT(index < move_objects.size());
  137. return DynamicObjectCast<T>(move_objects[index]);
  138. }
  139. void AddMoveObject(SharedPtr<Object> object) {
  140. move_objects.emplace_back(std::move(object));
  141. }
  142. void AddCopyObject(SharedPtr<Object> object) {
  143. copy_objects.emplace_back(std::move(object));
  144. }
  145. void AddDomainObject(std::shared_ptr<SessionRequestHandler> object) {
  146. domain_objects.emplace_back(std::move(object));
  147. }
  148. private:
  149. std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
  150. SharedPtr<Kernel::Domain> domain;
  151. SharedPtr<Kernel::ServerSession> server_session;
  152. // TODO(yuriks): Check common usage of this and optimize size accordingly
  153. boost::container::small_vector<SharedPtr<Object>, 8> move_objects;
  154. boost::container::small_vector<SharedPtr<Object>, 8> copy_objects;
  155. boost::container::small_vector<std::shared_ptr<SessionRequestHandler>, 8> domain_objects;
  156. std::unique_ptr<IPC::CommandHeader> command_header;
  157. std::unique_ptr<IPC::HandleDescriptorHeader> handle_descriptor_header;
  158. std::unique_ptr<IPC::DataPayloadHeader> data_payload_header;
  159. std::unique_ptr<IPC::DomainMessageHeader> domain_message_header;
  160. std::vector<IPC::BufferDescriptorX> buffer_x_desciptors;
  161. std::vector<IPC::BufferDescriptorABW> buffer_a_desciptors;
  162. std::vector<IPC::BufferDescriptorABW> buffer_b_desciptors;
  163. std::vector<IPC::BufferDescriptorABW> buffer_w_desciptors;
  164. unsigned data_payload_offset{};
  165. u32_le command{};
  166. };
  167. } // namespace Kernel