server_session.h 6.0 KB

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