server_session.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/kernel.h"
  10. #include "core/hle/kernel/wait_object.h"
  11. #include "core/hle/result.h"
  12. #include "core/memory.h"
  13. namespace Kernel {
  14. class ClientSession;
  15. class ClientPort;
  16. class ServerSession;
  17. class Session;
  18. class SessionRequestHandler;
  19. class Thread;
  20. /**
  21. * Kernel object representing the server endpoint of an IPC session. Sessions are the basic CTR-OS
  22. * primitive for communication between different processes, and are used to implement service calls
  23. * to the various system services.
  24. *
  25. * To make a service call, the client must write the command header and parameters to the buffer
  26. * located at offset 0x80 of the TLS (Thread-Local Storage) area, then execute a SendSyncRequest
  27. * SVC call with its ClientSession handle. The kernel will read the command header, using it to
  28. * marshall the parameters to the process at the server endpoint of the session.
  29. * After the server replies to the request, the response is marshalled back to the caller's
  30. * TLS buffer and control is transferred back to it.
  31. */
  32. class ServerSession final : public WaitObject {
  33. public:
  34. std::string GetTypeName() const override {
  35. return "ServerSession";
  36. }
  37. static const HandleType HANDLE_TYPE = HandleType::ServerSession;
  38. HandleType GetHandleType() const override {
  39. return HANDLE_TYPE;
  40. }
  41. using SessionPair = std::tuple<SharedPtr<ServerSession>, SharedPtr<ClientSession>>;
  42. /**
  43. * Creates a pair of ServerSession and an associated ClientSession.
  44. * @param name Optional name of the ports.
  45. * @param client_port Optional The ClientPort that spawned this session.
  46. * @return The created session tuple
  47. */
  48. static SessionPair CreateSessionPair(const std::string& name = "Unknown",
  49. SharedPtr<ClientPort> client_port = nullptr);
  50. /**
  51. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  52. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  53. * implemented.)
  54. */
  55. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  56. hle_handler = std::move(hle_handler_);
  57. }
  58. /**
  59. * Handle a sync request from the emulated application.
  60. * @param thread Thread that initiated the request.
  61. * @returns ResultCode from the operation.
  62. */
  63. ResultCode HandleSyncRequest(SharedPtr<Thread> thread);
  64. bool ShouldWait(Thread* thread) const override;
  65. void Acquire(Thread* thread) override;
  66. std::string name; ///< The name of this session (optional)
  67. std::shared_ptr<Session> parent; ///< The parent session, which links to the client endpoint.
  68. std::shared_ptr<SessionRequestHandler>
  69. hle_handler; ///< This session's HLE request handler (applicable when not a domain)
  70. /// This is the list of domain request handlers (after conversion to a domain)
  71. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  72. /// List of threads that are pending a response after a sync request. This list is processed in
  73. /// a LIFO manner, thus, the last request will be dispatched first.
  74. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  75. std::vector<SharedPtr<Thread>> pending_requesting_threads;
  76. /// Thread whose request is currently being handled. A request is considered "handled" when a
  77. /// response is sent via svcReplyAndReceive.
  78. /// TODO(Subv): Find a better name for this.
  79. SharedPtr<Thread> currently_handling;
  80. /// Returns true if the session has been converted to a domain, otherwise False
  81. bool IsDomain() const {
  82. return !domain_request_handlers.empty();
  83. }
  84. /// Converts the session to a domain at the end of the current command
  85. void ConvertToDomain() {
  86. convert_to_domain = true;
  87. }
  88. private:
  89. ServerSession();
  90. ~ServerSession() override;
  91. /**
  92. * Creates a server session. The server session can have an optional HLE handler,
  93. * which will be invoked to handle the IPC requests that this session receives.
  94. * @param name Optional name of the server session.
  95. * @return The created server session
  96. */
  97. static ResultVal<SharedPtr<ServerSession>> Create(std::string name = "Unknown");
  98. /// When set to True, converts the session to a domain at the end of the command
  99. bool convert_to_domain{};
  100. };
  101. /**
  102. * Performs command buffer translation for an HLE IPC request.
  103. * The command buffer from the ServerSession thread's TLS is copied into a
  104. * buffer and all descriptors in the buffer are processed.
  105. * TODO(Subv): Implement this function, currently we do not support multiple processes running at
  106. * once, but once that is implemented we'll need to properly translate all descriptors
  107. * in the command buffer.
  108. */
  109. ResultCode TranslateHLERequest(ServerSession* server_session);
  110. } // namespace Kernel