hle_ipc.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 HandleTable;
  19. class Process;
  20. /**
  21. * Interface implemented by HLE Session handlers.
  22. * This can be provided to a ServerSession in order to hook into several relevant events
  23. * (such as a new connection or a SyncRequest) so they can be implemented in the emulator.
  24. */
  25. class SessionRequestHandler : public std::enable_shared_from_this<SessionRequestHandler> {
  26. public:
  27. virtual ~SessionRequestHandler() = default;
  28. /**
  29. * Handles a sync request from the emulated application.
  30. * @param server_session The ServerSession that was triggered for this sync request,
  31. * it should be used to differentiate which client (As in ClientSession) we're answering to.
  32. * TODO(Subv): Use a wrapper structure to hold all the information relevant to
  33. * this request (ServerSession, Originator thread, Translated command buffer, etc).
  34. * @returns ResultCode the result code of the translate operation.
  35. */
  36. virtual void HandleSyncRequest(SharedPtr<ServerSession> server_session) = 0;
  37. /**
  38. * Signals that a client has just connected to this HLE handler and keeps the
  39. * associated ServerSession alive for the duration of the connection.
  40. * @param server_session Owning pointer to the ServerSession associated with the connection.
  41. */
  42. void ClientConnected(SharedPtr<ServerSession> server_session);
  43. /**
  44. * Signals that a client has just disconnected from this HLE handler and releases the
  45. * associated ServerSession.
  46. * @param server_session ServerSession associated with the connection.
  47. */
  48. void ClientDisconnected(SharedPtr<ServerSession> server_session);
  49. protected:
  50. /// List of sessions that are connected to this handler.
  51. /// A ServerSession whose server endpoint is an HLE implementation is kept alive by this list
  52. // for the duration of the connection.
  53. std::vector<SharedPtr<ServerSession>> connected_sessions;
  54. };
  55. /**
  56. * Class containing information about an in-flight IPC request being handled by an HLE service
  57. * implementation. Services should avoid using old global APIs (e.g. Kernel::GetCommandBuffer()) and
  58. * when possible use the APIs in this class to service the request.
  59. *
  60. * HLE handle protocol
  61. * ===================
  62. *
  63. * To avoid needing HLE services to keep a separate handle table, or having to directly modify the
  64. * requester's table, a tweaked protocol is used to receive and send handles in requests. The kernel
  65. * will decode the incoming handles into object pointers and insert a id in the buffer where the
  66. * handle would normally be. The service then calls GetIncomingHandle() with that id to get the
  67. * pointer to the object. Similarly, instead of inserting a handle into the command buffer, the
  68. * service calls AddOutgoingHandle() and stores the returned id where the handle would normally go.
  69. *
  70. * The end result is similar to just giving services their own real handle tables, but since these
  71. * ids are local to a specific context, it avoids requiring services to manage handles for objects
  72. * across multiple calls and ensuring that unneeded handles are cleaned up.
  73. */
  74. class HLERequestContext {
  75. public:
  76. ~HLERequestContext();
  77. /// Returns a pointer to the IPC command buffer for this request.
  78. u32* CommandBuffer() {
  79. return cmd_buf.data();
  80. }
  81. /**
  82. * Returns the session through which this request was made. This can be used as a map key to
  83. * access per-client data on services.
  84. */
  85. SharedPtr<ServerSession> Session() const {
  86. return session;
  87. }
  88. /**
  89. * Resolves a object id from the request command buffer into a pointer to an object. See the
  90. * "HLE handle protocol" section in the class documentation for more details.
  91. */
  92. SharedPtr<Object> GetIncomingHandle(u32 id_from_cmdbuf) const;
  93. /**
  94. * Adds an outgoing object to the response, returning the id which should be used to reference
  95. * it. See the "HLE handle protocol" section in the class documentation for more details.
  96. */
  97. u32 AddOutgoingHandle(SharedPtr<Object> object);
  98. /**
  99. * Discards all Objects from the context, invalidating all ids. This may be called after reading
  100. * out all incoming objects, so that the buffer memory can be re-used for outgoing handles, but
  101. * this is not required.
  102. */
  103. void ClearIncomingObjects();
  104. private:
  105. friend class Service::ServiceFrameworkBase;
  106. ResultCode PopulateFromIncomingCommandBuffer(const u32_le* src_cmdbuf, Process& src_process,
  107. HandleTable& src_table);
  108. ResultCode WriteToOutgoingCommandBuffer(u32_le* dst_cmdbuf, Process& dst_process,
  109. HandleTable& dst_table) const;
  110. std::array<u32, IPC::COMMAND_BUFFER_LENGTH> cmd_buf;
  111. SharedPtr<ServerSession> session;
  112. // TODO(yuriks): Check common usage of this and optimize size accordingly
  113. boost::container::small_vector<SharedPtr<Object>, 8> request_handles;
  114. };
  115. } // namespace Kernel