server_session.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include "core/core.h"
  6. #include "core/hle/ipc_helpers.h"
  7. #include "core/hle/kernel/client_port.h"
  8. #include "core/hle/kernel/client_session.h"
  9. #include "core/hle/kernel/handle_table.h"
  10. #include "core/hle/kernel/hle_ipc.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/kernel/server_session.h"
  13. #include "core/hle/kernel/session.h"
  14. #include "core/hle/kernel/thread.h"
  15. namespace Kernel {
  16. ServerSession::ServerSession() = default;
  17. ServerSession::~ServerSession() {
  18. // This destructor will be called automatically when the last ServerSession handle is closed by
  19. // the emulated application.
  20. // Decrease the port's connection count.
  21. if (parent->port)
  22. parent->port->active_sessions--;
  23. // TODO(Subv): Wake up all the ClientSession's waiting threads and set
  24. // the SendSyncRequest result to 0xC920181A.
  25. parent->server = nullptr;
  26. }
  27. ResultVal<SharedPtr<ServerSession>> ServerSession::Create(std::string name) {
  28. SharedPtr<ServerSession> server_session(new ServerSession);
  29. server_session->name = std::move(name);
  30. server_session->parent = nullptr;
  31. return MakeResult(std::move(server_session));
  32. }
  33. bool ServerSession::ShouldWait(Thread* thread) const {
  34. // Closed sessions should never wait, an error will be returned from svcReplyAndReceive.
  35. if (parent->client == nullptr)
  36. return false;
  37. // Wait if we have no pending requests, or if we're currently handling a request.
  38. return pending_requesting_threads.empty() || currently_handling != nullptr;
  39. }
  40. void ServerSession::Acquire(Thread* thread) {
  41. ASSERT_MSG(!ShouldWait(thread), "object unavailable!");
  42. // We are now handling a request, pop it from the stack.
  43. // TODO(Subv): What happens if the client endpoint is closed before any requests are made?
  44. ASSERT(!pending_requesting_threads.empty());
  45. currently_handling = pending_requesting_threads.back();
  46. pending_requesting_threads.pop_back();
  47. }
  48. ResultCode ServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& context) {
  49. auto& domain_message_header = context.GetDomainMessageHeader();
  50. if (domain_message_header) {
  51. // If there is a DomainMessageHeader, then this is CommandType "Request"
  52. const u32 object_id{context.GetDomainMessageHeader()->object_id};
  53. switch (domain_message_header->command) {
  54. case IPC::DomainMessageHeader::CommandType::SendMessage:
  55. return domain_request_handlers[object_id - 1]->HandleSyncRequest(context);
  56. case IPC::DomainMessageHeader::CommandType::CloseVirtualHandle: {
  57. LOG_DEBUG(IPC, "CloseVirtualHandle, object_id=0x%08X", object_id);
  58. domain_request_handlers[object_id - 1] = nullptr;
  59. IPC::ResponseBuilder rb{context, 2};
  60. rb.Push(RESULT_SUCCESS);
  61. return RESULT_SUCCESS;
  62. }
  63. }
  64. LOG_CRITICAL(IPC, "Unknown domain command=%d",
  65. static_cast<int>(domain_message_header->command.Value()));
  66. ASSERT(false);
  67. }
  68. return RESULT_SUCCESS;
  69. }
  70. ResultCode ServerSession::HandleSyncRequest(SharedPtr<Thread> thread) {
  71. // The ServerSession received a sync request, this means that there's new data available
  72. // from its ClientSession, so wake up any threads that may be waiting on a svcReplyAndReceive or
  73. // similar.
  74. Kernel::HLERequestContext context(this);
  75. u32* cmd_buf = (u32*)Memory::GetPointer(thread->GetTLSAddress());
  76. context.PopulateFromIncomingCommandBuffer(cmd_buf, *Core::CurrentProcess(),
  77. Kernel::g_handle_table);
  78. ResultCode result = RESULT_SUCCESS;
  79. // If the session has been converted to a domain, handle the domain request
  80. if (IsDomain() && context.GetDomainMessageHeader()) {
  81. result = HandleDomainSyncRequest(context);
  82. // If there is no domain header, the regular session handler is used
  83. } else if (hle_handler != nullptr) {
  84. // If this ServerSession has an associated HLE handler, forward the request to it.
  85. result = hle_handler->HandleSyncRequest(context);
  86. }
  87. if (thread->status == THREADSTATUS_RUNNING) {
  88. // Put the thread to sleep until the server replies, it will be awoken in
  89. // svcReplyAndReceive for LLE servers.
  90. thread->status = THREADSTATUS_WAIT_IPC;
  91. if (hle_handler != nullptr) {
  92. // For HLE services, we put the request threads to sleep for a short duration to
  93. // simulate IPC overhead, but only if the HLE handler didn't put the thread to sleep for
  94. // other reasons like an async callback. The IPC overhead is needed to prevent
  95. // starvation when a thread only does sync requests to HLE services while a
  96. // lower-priority thread is waiting to run.
  97. // This delay was approximated in a homebrew application by measuring the average time
  98. // it takes for svcSendSyncRequest to return when performing the SetLcdForceBlack IPC
  99. // request to the GSP:GPU service in a n3DS with firmware 11.6. The measured values have
  100. // a high variance and vary between models.
  101. static constexpr u64 IPCDelayNanoseconds = 39000;
  102. thread->WakeAfterDelay(IPCDelayNanoseconds);
  103. } else {
  104. // Add the thread to the list of threads that have issued a sync request with this
  105. // server.
  106. pending_requesting_threads.push_back(std::move(thread));
  107. }
  108. }
  109. // If this ServerSession does not have an HLE implementation, just wake up the threads waiting
  110. // on it.
  111. WakeupAllWaitingThreads();
  112. // Handle scenario when ConvertToDomain command was issued, as we must do the conversion at the
  113. // end of the command such that only commands following this one are handled as domains
  114. if (convert_to_domain) {
  115. ASSERT_MSG(domain_request_handlers.empty(), "already a domain");
  116. domain_request_handlers = {hle_handler};
  117. convert_to_domain = false;
  118. }
  119. return result;
  120. }
  121. ServerSession::SessionPair ServerSession::CreateSessionPair(const std::string& name,
  122. SharedPtr<ClientPort> port) {
  123. auto server_session = ServerSession::Create(name + "_Server").Unwrap();
  124. SharedPtr<ClientSession> client_session(new ClientSession);
  125. client_session->name = name + "_Client";
  126. std::shared_ptr<Session> parent(new Session);
  127. parent->client = client_session.get();
  128. parent->server = server_session.get();
  129. parent->port = port;
  130. client_session->parent = parent;
  131. server_session->parent = parent;
  132. return std::make_tuple(std::move(server_session), std::move(client_session));
  133. }
  134. } // namespace Kernel