server_session.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <vector>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/object.h"
  10. #include "core/hle/kernel/wait_object.h"
  11. #include "core/hle/result.h"
  12. namespace Kernel {
  13. class ClientPort;
  14. class ClientSession;
  15. class HLERequestContext;
  16. class KernelCore;
  17. class ServerSession;
  18. class Session;
  19. class SessionRequestHandler;
  20. class Thread;
  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 kernel The kernal instance to create the session pair under.
  46. * @param name Optional name of the ports.
  47. * @param client_port Optional The ClientPort that spawned this session.
  48. * @return The created session tuple
  49. */
  50. static SessionPair CreateSessionPair(KernelCore& kernel, const std::string& name = "Unknown",
  51. SharedPtr<ClientPort> client_port = nullptr);
  52. /**
  53. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  54. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  55. * implemented.)
  56. */
  57. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  58. hle_handler = std::move(hle_handler_);
  59. }
  60. /**
  61. * Handle a sync request from the emulated application.
  62. * @param thread Thread that initiated the request.
  63. * @returns ResultCode from the operation.
  64. */
  65. ResultCode HandleSyncRequest(SharedPtr<Thread> thread);
  66. bool ShouldWait(Thread* thread) const override;
  67. void Acquire(Thread* thread) override;
  68. std::string name; ///< The name of this session (optional)
  69. std::shared_ptr<Session> parent; ///< The parent session, which links to the client endpoint.
  70. std::shared_ptr<SessionRequestHandler>
  71. hle_handler; ///< This session's HLE request handler (applicable when not a domain)
  72. /// This is the list of domain request handlers (after conversion to a domain)
  73. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  74. /// List of threads that are pending a response after a sync request. This list is processed in
  75. /// a LIFO manner, thus, the last request will be dispatched first.
  76. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  77. std::vector<SharedPtr<Thread>> pending_requesting_threads;
  78. /// Thread whose request is currently being handled. A request is considered "handled" when a
  79. /// response is sent via svcReplyAndReceive.
  80. /// TODO(Subv): Find a better name for this.
  81. SharedPtr<Thread> currently_handling;
  82. /// Returns true if the session has been converted to a domain, otherwise False
  83. bool IsDomain() const {
  84. return !IsSession();
  85. }
  86. /// Returns true if this session has not been converted to a domain, otherwise false.
  87. bool IsSession() const {
  88. return domain_request_handlers.empty();
  89. }
  90. /// Converts the session to a domain at the end of the current command
  91. void ConvertToDomain() {
  92. convert_to_domain = true;
  93. }
  94. private:
  95. explicit ServerSession(KernelCore& kernel);
  96. ~ServerSession() override;
  97. /**
  98. * Creates a server session. The server session can have an optional HLE handler,
  99. * which will be invoked to handle the IPC requests that this session receives.
  100. * @param kernel The kernel instance to create this server session under.
  101. * @param name Optional name of the server session.
  102. * @return The created server session
  103. */
  104. static ResultVal<SharedPtr<ServerSession>> Create(KernelCore& kernel,
  105. std::string name = "Unknown");
  106. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  107. /// object handle.
  108. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  109. /// When set to True, converts the session to a domain at the end of the command
  110. bool convert_to_domain{};
  111. };
  112. } // namespace Kernel