server_session.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2019 yuzu emulator team
  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 "common/threadsafe_queue.h"
  10. #include "core/hle/kernel/synchronization_object.h"
  11. #include "core/hle/result.h"
  12. namespace Core::Memory {
  13. class Memory;
  14. }
  15. namespace Core::Timing {
  16. struct EventType;
  17. }
  18. namespace Kernel {
  19. class HLERequestContext;
  20. class KernelCore;
  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 SynchronizationObject {
  37. public:
  38. explicit ServerSession(KernelCore& kernel);
  39. ~ServerSession() override;
  40. friend class Session;
  41. static ResultVal<std::shared_ptr<ServerSession>> Create(KernelCore& kernel,
  42. std::shared_ptr<Session> parent,
  43. std::string name = "Unknown");
  44. std::string GetTypeName() const override {
  45. return "ServerSession";
  46. }
  47. std::string GetName() const override {
  48. return name;
  49. }
  50. static constexpr HandleType HANDLE_TYPE = HandleType::ServerSession;
  51. HandleType GetHandleType() const override {
  52. return HANDLE_TYPE;
  53. }
  54. Session* GetParent() {
  55. return parent.get();
  56. }
  57. const Session* GetParent() const {
  58. return parent.get();
  59. }
  60. bool IsSignaled() const override;
  61. /**
  62. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  63. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  64. * implemented.)
  65. */
  66. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  67. hle_handler = std::move(hle_handler_);
  68. }
  69. /**
  70. * Handle a sync request from the emulated application.
  71. *
  72. * @param thread Thread that initiated the request.
  73. * @param memory Memory context to handle the sync request under.
  74. *
  75. * @returns ResultCode from the operation.
  76. */
  77. ResultCode HandleSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory);
  78. bool ShouldWait(const Thread* thread) const override;
  79. void Acquire(Thread* thread) override;
  80. /// Called when a client disconnection occurs.
  81. void ClientDisconnected();
  82. /// Adds a new domain request handler to the collection of request handlers within
  83. /// this ServerSession instance.
  84. void AppendDomainRequestHandler(std::shared_ptr<SessionRequestHandler> handler);
  85. /// Retrieves the total number of domain request handlers that have been
  86. /// appended to this ServerSession instance.
  87. std::size_t NumDomainRequestHandlers() const;
  88. /// Returns true if the session has been converted to a domain, otherwise False
  89. bool IsDomain() const {
  90. return !IsSession();
  91. }
  92. /// Returns true if this session has not been converted to a domain, otherwise false.
  93. bool IsSession() const {
  94. return domain_request_handlers.empty();
  95. }
  96. /// Converts the session to a domain at the end of the current command
  97. void ConvertToDomain() {
  98. convert_to_domain = true;
  99. }
  100. private:
  101. /// Queues a sync request from the emulated application.
  102. ResultCode QueueSyncRequest(std::shared_ptr<Thread> thread, Core::Memory::Memory& memory);
  103. /// Completes a sync request from the emulated application.
  104. ResultCode CompleteSyncRequest();
  105. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  106. /// object handle.
  107. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  108. /// The parent session, which links to the client endpoint.
  109. std::shared_ptr<Session> parent;
  110. /// This session's HLE request handler (applicable when not a domain)
  111. std::shared_ptr<SessionRequestHandler> hle_handler;
  112. /// This is the list of domain request handlers (after conversion to a domain)
  113. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  114. /// List of threads that are pending a response after a sync request. This list is processed in
  115. /// a LIFO manner, thus, the last request will be dispatched first.
  116. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  117. std::vector<std::shared_ptr<Thread>> pending_requesting_threads;
  118. /// Thread whose request is currently being handled. A request is considered "handled" when a
  119. /// response is sent via svcReplyAndReceive.
  120. /// TODO(Subv): Find a better name for this.
  121. std::shared_ptr<Thread> currently_handling;
  122. /// When set to True, converts the session to a domain at the end of the command
  123. bool convert_to_domain{};
  124. /// The name of this session (optional)
  125. std::string name;
  126. /// Core timing event used to schedule the service request at some point in the future
  127. std::shared_ptr<Core::Timing::EventType> request_event;
  128. /// Queue of scheduled service requests
  129. Common::MPSCQueue<std::shared_ptr<Kernel::HLERequestContext>> request_queue;
  130. };
  131. } // namespace Kernel