server_session.h 4.8 KB

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