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