k_server_session.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include <utility>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "common/logging/log.h"
  9. #include "common/scope_exit.h"
  10. #include "core/core_timing.h"
  11. #include "core/hle/ipc_helpers.h"
  12. #include "core/hle/kernel/hle_ipc.h"
  13. #include "core/hle/kernel/k_client_port.h"
  14. #include "core/hle/kernel/k_handle_table.h"
  15. #include "core/hle/kernel/k_port.h"
  16. #include "core/hle/kernel/k_process.h"
  17. #include "core/hle/kernel/k_scheduler.h"
  18. #include "core/hle/kernel/k_server_port.h"
  19. #include "core/hle/kernel/k_server_session.h"
  20. #include "core/hle/kernel/k_session.h"
  21. #include "core/hle/kernel/k_thread.h"
  22. #include "core/hle/kernel/kernel.h"
  23. #include "core/memory.h"
  24. namespace Kernel {
  25. KServerSession::KServerSession(KernelCore& kernel_) : KSynchronizationObject{kernel_} {}
  26. KServerSession::~KServerSession() {
  27. // Ensure that the global list tracking server sessions does not hold on to a reference.
  28. kernel.UnregisterServerSession(this);
  29. }
  30. void KServerSession::Initialize(KSession* parent_session_, std::string&& name_,
  31. std::shared_ptr<SessionRequestManager> manager_) {
  32. // Set member variables.
  33. parent = parent_session_;
  34. name = std::move(name_);
  35. if (manager_) {
  36. manager = manager_;
  37. } else {
  38. manager = std::make_shared<SessionRequestManager>(kernel);
  39. }
  40. }
  41. void KServerSession::Destroy() {
  42. parent->OnServerClosed();
  43. parent->Close();
  44. }
  45. void KServerSession::OnClientClosed() {
  46. if (manager->HasSessionHandler()) {
  47. manager->SessionHandler().ClientDisconnected(this);
  48. }
  49. }
  50. bool KServerSession::IsSignaled() const {
  51. ASSERT(kernel.GlobalSchedulerContext().IsLocked());
  52. // If the client is closed, we're always signaled.
  53. if (parent->IsClientClosed()) {
  54. return true;
  55. }
  56. // Otherwise, we're signaled if we have a request and aren't handling one.
  57. return false;
  58. }
  59. void KServerSession::AppendDomainHandler(SessionRequestHandlerPtr handler) {
  60. manager->AppendDomainHandler(std::move(handler));
  61. }
  62. std::size_t KServerSession::NumDomainRequestHandlers() const {
  63. return manager->DomainHandlerCount();
  64. }
  65. ResultCode KServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& context) {
  66. if (!context.HasDomainMessageHeader()) {
  67. return ResultSuccess;
  68. }
  69. // Set domain handlers in HLE context, used for domain objects (IPC interfaces) as inputs
  70. context.SetSessionRequestManager(manager);
  71. // If there is a DomainMessageHeader, then this is CommandType "Request"
  72. const auto& domain_message_header = context.GetDomainMessageHeader();
  73. const u32 object_id{domain_message_header.object_id};
  74. switch (domain_message_header.command) {
  75. case IPC::DomainMessageHeader::CommandType::SendMessage:
  76. if (object_id > manager->DomainHandlerCount()) {
  77. LOG_CRITICAL(IPC,
  78. "object_id {} is too big! This probably means a recent service call "
  79. "to {} needed to return a new interface!",
  80. object_id, name);
  81. UNREACHABLE();
  82. return ResultSuccess; // Ignore error if asserts are off
  83. }
  84. return manager->DomainHandler(object_id - 1)->HandleSyncRequest(*this, context);
  85. case IPC::DomainMessageHeader::CommandType::CloseVirtualHandle: {
  86. LOG_DEBUG(IPC, "CloseVirtualHandle, object_id=0x{:08X}", object_id);
  87. manager->CloseDomainHandler(object_id - 1);
  88. IPC::ResponseBuilder rb{context, 2};
  89. rb.Push(ResultSuccess);
  90. return ResultSuccess;
  91. }
  92. }
  93. LOG_CRITICAL(IPC, "Unknown domain command={}", domain_message_header.command.Value());
  94. ASSERT(false);
  95. return ResultSuccess;
  96. }
  97. ResultCode KServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) {
  98. u32* cmd_buf{reinterpret_cast<u32*>(memory.GetPointer(thread->GetTLSAddress()))};
  99. auto context = std::make_shared<HLERequestContext>(kernel, memory, this, thread);
  100. context->PopulateFromIncomingCommandBuffer(kernel.CurrentProcess()->GetHandleTable(), cmd_buf);
  101. // In the event that something fails here, stub a result to prevent the game from crashing.
  102. // This is a work-around in the event that somehow we process a service request after the
  103. // session has been closed by the game. This has been observed to happen rarely in Pokemon
  104. // Sword/Shield and is likely a result of us using host threads/scheduling for services.
  105. // TODO(bunnei): Find a better solution here.
  106. auto error_guard = SCOPE_GUARD({ CompleteSyncRequest(*context); });
  107. // Ensure we have a session request handler
  108. if (manager->HasSessionRequestHandler(*context)) {
  109. if (auto strong_ptr = manager->GetServiceThread().lock()) {
  110. strong_ptr->QueueSyncRequest(*parent, std::move(context));
  111. // We succeeded.
  112. error_guard.Cancel();
  113. } else {
  114. ASSERT_MSG(false, "strong_ptr is nullptr!");
  115. }
  116. } else {
  117. ASSERT_MSG(false, "handler is invalid!");
  118. }
  119. return ResultSuccess;
  120. }
  121. ResultCode KServerSession::CompleteSyncRequest(HLERequestContext& context) {
  122. ResultCode result = ResultSuccess;
  123. // If the session has been converted to a domain, handle the domain request
  124. if (manager->HasSessionRequestHandler(context)) {
  125. if (IsDomain() && context.HasDomainMessageHeader()) {
  126. result = HandleDomainSyncRequest(context);
  127. // If there is no domain header, the regular session handler is used
  128. } else if (manager->HasSessionHandler()) {
  129. // If this ServerSession has an associated HLE handler, forward the request to it.
  130. result = manager->SessionHandler().HandleSyncRequest(*this, context);
  131. }
  132. } else {
  133. ASSERT_MSG(false, "Session handler is invalid, stubbing response!");
  134. IPC::ResponseBuilder rb(context, 2);
  135. rb.Push(ResultSuccess);
  136. }
  137. if (convert_to_domain) {
  138. ASSERT_MSG(!IsDomain(), "ServerSession is already a domain instance.");
  139. manager->ConvertToDomain();
  140. convert_to_domain = false;
  141. }
  142. // Some service requests require the thread to block
  143. {
  144. KScopedSchedulerLock lock(kernel);
  145. if (!context.IsThreadWaiting()) {
  146. context.GetThread().Wakeup();
  147. context.GetThread().SetSyncedObject(nullptr, result);
  148. }
  149. }
  150. return result;
  151. }
  152. ResultCode KServerSession::HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory,
  153. Core::Timing::CoreTiming& core_timing) {
  154. return QueueSyncRequest(thread, memory);
  155. }
  156. } // namespace Kernel