server_session.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "core/hle/kernel/object.h"
  10. #include "core/hle/kernel/wait_object.h"
  11. #include "core/hle/result.h"
  12. namespace Memory {
  13. class Memory;
  14. }
  15. namespace Kernel {
  16. class ClientPort;
  17. class ClientSession;
  18. class HLERequestContext;
  19. class KernelCore;
  20. class ServerSession;
  21. class Session;
  22. class SessionRequestHandler;
  23. class Thread;
  24. /**
  25. * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS
  26. * primitive for communication between different processes, and are used to implement service calls
  27. * to the various system services.
  28. *
  29. * To make a service call, the client must write the command header and parameters to the buffer
  30. * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
  31. * SVC call with its ClientSession handle. The kernel will read the command header, using it to
  32. * marshall the parameters to the process at the server endpoint of the session.
  33. * After the server replies to the request, the response is marshalled back to the caller's
  34. * TLS buffer and control is transferred back to it.
  35. */
  36. class ServerSession final : public WaitObject {
  37. public:
  38. explicit ServerSession(KernelCore& kernel);
  39. ~ServerSession() override;
  40. std::string GetTypeName() const override {
  41. return "ServerSession";
  42. }
  43. std::string GetName() const override {
  44. return name;
  45. }
  46. static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession;
  47. HandleType GetHandleType() const override {
  48. return HANDLE_TYPE;
  49. }
  50. Session* GetParent() {
  51. return parent.get();
  52. }
  53. const Session* GetParent() const {
  54. return parent.get();
  55. }
  56. using SessionPair = std::pair<std::shared_ptr<ServerSession>, std::shared_ptr<ClientSession>>;
  57. /**
  58. * Creates a pair of ServerSession and an associated ClientSession.
  59. * @param kernel The kernal instance to create the session pair under.
  60. * @param name Optional name of the ports.
  61. * @param client_port Optional The ClientPort that spawned this session.
  62. * @return The created session tuple
  63. */
  64. static SessionPair CreateSessionPair(KernelCore& kernel, const std::string& name = "Unknown",
  65. std::shared_ptr<ClientPort> client_port = nullptr);
  66. /**
  67. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  68. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  69. * implemented.)
  70. */
  71. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  72. hle_handler = std::move(hle_handler_);
  73. }
  74. /**
  75. * Handle a sync request from the emulated application.
  76. *
  77. * @param thread Thread that initiated the request.
  78. * @param memory Memory context to handle the sync request under.
  79. *
  80. * @returns ResultCode from the operation.
  81. */
  82. ResultCode HandleSyncRequest(std::shared_ptr<Thread> thread, Memory::Memory& memory);
  83. bool ShouldWait(const Thread* thread) const override;
  84. void Acquire(Thread* thread) override;
  85. /// Called when a client disconnection occurs.
  86. void ClientDisconnected();
  87. /// Adds a new domain request handler to the collection of request handlers within
  88. /// this ServerSession instance.
  89. void AppendDomainRequestHandler(std::shared_ptr<SessionRequestHandler> handler);
  90. /// Retrieves the total number of domain request handlers that have been
  91. /// appended to this ServerSession instance.
  92. std::size_t NumDomainRequestHandlers() const;
  93. /// Returns true if the session has been converted to a domain, otherwise False
  94. bool IsDomain() const {
  95. return !IsSession();
  96. }
  97. /// Returns true if this session has not been converted to a domain, otherwise false.
  98. bool IsSession() const {
  99. return domain_request_handlers.empty();
  100. }
  101. /// Converts the session to a domain at the end of the current command
  102. void ConvertToDomain() {
  103. convert_to_domain = true;
  104. }
  105. private:
  106. /**
  107. * Creates a server session. The server session can have an optional HLE handler,
  108. * which will be invoked to handle the IPC requests that this session receives.
  109. * @param kernel The kernel instance to create this server session under.
  110. * @param name Optional name of the server session.
  111. * @return The created server session
  112. */
  113. static ResultVal<std::shared_ptr<ServerSession>> Create(KernelCore& kernel,
  114. std::string name = "Unknown");
  115. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  116. /// object handle.
  117. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  118. /// The parent session, which links to the client endpoint.
  119. std::shared_ptr<Session> parent;
  120. /// This session's HLE request handler (applicable when not a domain)
  121. std::shared_ptr<SessionRequestHandler> hle_handler;
  122. /// This is the list of domain request handlers (after conversion to a domain)
  123. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  124. /// List of threads that are pending a response after a sync request. This list is processed in
  125. /// a LIFO manner, thus, the last request will be dispatched first.
  126. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  127. std::vector<std::shared_ptr<Thread>> pending_requesting_threads;
  128. /// Thread whose request is currently being handled. A request is considered "handled" when a
  129. /// response is sent via svcReplyAndReceive.
  130. /// TODO(Subv): Find a better name for this.
  131. std::shared_ptr<Thread> currently_handling;
  132. /// When set to True, converts the session to a domain at the end of the command
  133. bool convert_to_domain{};
  134. /// The name of this session (optional)
  135. std::string name;
  136. };
  137. } // namespace Kernel