server_session.h 5.8 KB

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