k_server_session.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "core/core_timing.h"
  10. #include "core/hle/ipc_helpers.h"
  11. #include "core/hle/kernel/hle_ipc.h"
  12. #include "core/hle/kernel/k_client_port.h"
  13. #include "core/hle/kernel/k_handle_table.h"
  14. #include "core/hle/kernel/k_process.h"
  15. #include "core/hle/kernel/k_scheduler.h"
  16. #include "core/hle/kernel/k_server_session.h"
  17. #include "core/hle/kernel/k_session.h"
  18. #include "core/hle/kernel/k_thread.h"
  19. #include "core/hle/kernel/kernel.h"
  20. #include "core/memory.h"
  21. namespace Kernel {
  22. KServerSession::KServerSession(KernelCore& kernel_)
  23. : KSynchronizationObject{kernel_}, manager{std::make_shared<SessionRequestManager>()} {}
  24. KServerSession::~KServerSession() {
  25. kernel.ReleaseServiceThread(service_thread);
  26. }
  27. void KServerSession::Initialize(KSession* parent_, std::string&& name_) {
  28. // Set member variables.
  29. parent = parent_;
  30. name = std::move(name_);
  31. service_thread = kernel.CreateServiceThread(name);
  32. }
  33. void KServerSession::Destroy() {
  34. parent->OnServerClosed();
  35. parent->Close();
  36. }
  37. void KServerSession::OnClientClosed() {
  38. if (manager->HasSessionHandler()) {
  39. manager->SessionHandler().ClientDisconnected(this);
  40. }
  41. }
  42. bool KServerSession::IsSignaled() const {
  43. ASSERT(kernel.GlobalSchedulerContext().IsLocked());
  44. // If the client is closed, we're always signaled.
  45. if (parent->IsClientClosed()) {
  46. return true;
  47. }
  48. // Otherwise, we're signaled if we have a request and aren't handling one.
  49. return false;
  50. }
  51. void KServerSession::AppendDomainHandler(SessionRequestHandlerPtr handler) {
  52. manager->AppendDomainHandler(std::move(handler));
  53. }
  54. std::size_t KServerSession::NumDomainRequestHandlers() const {
  55. return manager->DomainHandlerCount();
  56. }
  57. ResultCode KServerSession::HandleDomainSyncRequest(Kernel::HLERequestContext& context) {
  58. if (!context.HasDomainMessageHeader()) {
  59. return ResultSuccess;
  60. }
  61. // Set domain handlers in HLE context, used for domain objects (IPC interfaces) as inputs
  62. context.SetSessionRequestManager(manager);
  63. // If there is a DomainMessageHeader, then this is CommandType "Request"
  64. const auto& domain_message_header = context.GetDomainMessageHeader();
  65. const u32 object_id{domain_message_header.object_id};
  66. switch (domain_message_header.command) {
  67. case IPC::DomainMessageHeader::CommandType::SendMessage:
  68. if (object_id > manager->DomainHandlerCount()) {
  69. LOG_CRITICAL(IPC,
  70. "object_id {} is too big! This probably means a recent service call "
  71. "to {} needed to return a new interface!",
  72. object_id, name);
  73. UNREACHABLE();
  74. return ResultSuccess; // Ignore error if asserts are off
  75. }
  76. return manager->DomainHandler(object_id - 1)->HandleSyncRequest(*this, context);
  77. case IPC::DomainMessageHeader::CommandType::CloseVirtualHandle: {
  78. LOG_DEBUG(IPC, "CloseVirtualHandle, object_id=0x{:08X}", object_id);
  79. manager->CloseDomainHandler(object_id - 1);
  80. IPC::ResponseBuilder rb{context, 2};
  81. rb.Push(ResultSuccess);
  82. return ResultSuccess;
  83. }
  84. }
  85. LOG_CRITICAL(IPC, "Unknown domain command={}", domain_message_header.command.Value());
  86. ASSERT(false);
  87. return ResultSuccess;
  88. }
  89. ResultCode KServerSession::QueueSyncRequest(KThread* thread, Core::Memory::Memory& memory) {
  90. u32* cmd_buf{reinterpret_cast<u32*>(memory.GetPointer(thread->GetTLSAddress()))};
  91. auto context = std::make_shared<HLERequestContext>(kernel, memory, this, thread);
  92. context->PopulateFromIncomingCommandBuffer(kernel.CurrentProcess()->GetHandleTable(), cmd_buf);
  93. if (auto strong_ptr = service_thread.lock()) {
  94. strong_ptr->QueueSyncRequest(*parent, std::move(context));
  95. return ResultSuccess;
  96. }
  97. return ResultSuccess;
  98. }
  99. ResultCode KServerSession::CompleteSyncRequest(HLERequestContext& context) {
  100. ResultCode result = ResultSuccess;
  101. // If the session has been converted to a domain, handle the domain request
  102. if (IsDomain() && context.HasDomainMessageHeader()) {
  103. result = HandleDomainSyncRequest(context);
  104. // If there is no domain header, the regular session handler is used
  105. } else if (manager->HasSessionHandler()) {
  106. // If this ServerSession has an associated HLE handler, forward the request to it.
  107. result = manager->SessionHandler().HandleSyncRequest(*this, context);
  108. }
  109. if (convert_to_domain) {
  110. ASSERT_MSG(!IsDomain(), "ServerSession is already a domain instance.");
  111. manager->ConvertToDomain();
  112. convert_to_domain = false;
  113. }
  114. // Some service requests require the thread to block
  115. {
  116. KScopedSchedulerLock lock(kernel);
  117. if (!context.IsThreadWaiting()) {
  118. context.GetThread().Wakeup();
  119. context.GetThread().SetSyncedObject(nullptr, result);
  120. }
  121. }
  122. return result;
  123. }
  124. ResultCode KServerSession::HandleSyncRequest(KThread* thread, Core::Memory::Memory& memory,
  125. Core::Timing::CoreTiming& core_timing) {
  126. return QueueSyncRequest(thread, memory);
  127. }
  128. } // namespace Kernel