server_session.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 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. /**
  61. * Sets the HLE handler for the session. This handler will be called to service IPC requests
  62. * instead of the regular IPC machinery. (The regular IPC machinery is currently not
  63. * implemented.)
  64. */
  65. void SetHleHandler(std::shared_ptr<SessionRequestHandler> hle_handler_) {
  66. hle_handler = std::move(hle_handler_);
  67. }
  68. /**
  69. * Handle a sync request from the emulated application.
  70. *
  71. * @param thread Thread that initiated the request.
  72. * @param memory Memory context to handle the sync request under.
  73. *
  74. * @returns ResultCode from the operation.
  75. */
  76. ResultCode HandleSyncRequest(std::shared_ptr<Thread> thread, Memory::Memory& memory);
  77. bool ShouldWait(const Thread* thread) const override;
  78. void Acquire(Thread* thread) override;
  79. /// Called when a client disconnection occurs.
  80. void ClientDisconnected();
  81. /// Adds a new domain request handler to the collection of request handlers within
  82. /// this ServerSession instance.
  83. void AppendDomainRequestHandler(std::shared_ptr<SessionRequestHandler> handler);
  84. /// Retrieves the total number of domain request handlers that have been
  85. /// appended to this ServerSession instance.
  86. std::size_t NumDomainRequestHandlers() const;
  87. /// Returns true if the session has been converted to a domain, otherwise False
  88. bool IsDomain() const {
  89. return !IsSession();
  90. }
  91. /// Returns true if this session has not been converted to a domain, otherwise false.
  92. bool IsSession() const {
  93. return domain_request_handlers.empty();
  94. }
  95. /// Converts the session to a domain at the end of the current command
  96. void ConvertToDomain() {
  97. convert_to_domain = true;
  98. }
  99. private:
  100. /// Queues a sync request from the emulated application.
  101. ResultCode QueueSyncRequest(std::shared_ptr<Thread> thread, Memory::Memory& memory);
  102. /// Completes a sync request from the emulated application.
  103. ResultCode CompleteSyncRequest();
  104. /// Handles a SyncRequest to a domain, forwarding the request to the proper object or closing an
  105. /// object handle.
  106. ResultCode HandleDomainSyncRequest(Kernel::HLERequestContext& context);
  107. /// The parent session, which links to the client endpoint.
  108. std::shared_ptr<Session> parent;
  109. /// This session's HLE request handler (applicable when not a domain)
  110. std::shared_ptr<SessionRequestHandler> hle_handler;
  111. /// This is the list of domain request handlers (after conversion to a domain)
  112. std::vector<std::shared_ptr<SessionRequestHandler>> domain_request_handlers;
  113. /// List of threads that are pending a response after a sync request. This list is processed in
  114. /// a LIFO manner, thus, the last request will be dispatched first.
  115. /// TODO(Subv): Verify if this is indeed processed in LIFO using a hardware test.
  116. std::vector<std::shared_ptr<Thread>> pending_requesting_threads;
  117. /// Thread whose request is currently being handled. A request is considered "handled" when a
  118. /// response is sent via svcReplyAndReceive.
  119. /// TODO(Subv): Find a better name for this.
  120. std::shared_ptr<Thread> currently_handling;
  121. /// When set to True, converts the session to a domain at the end of the command
  122. bool convert_to_domain{};
  123. /// The name of this session (optional)
  124. std::string name;
  125. /// Core timing event used to schedule the service request at some point in the future
  126. std::shared_ptr<Core::Timing::EventType> request_event;
  127. /// Queue of scheduled service requests
  128. Common::MPSCQueue<std::shared_ptr<Kernel::HLERequestContext>> request_queue;
  129. };
  130. } // namespace Kernel