service.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <fmt/format.h>
  6. #include "common/assert.h"
  7. #include "common/logging/log.h"
  8. #include "common/string_util.h"
  9. #include "core/hle/ipc.h"
  10. #include "core/hle/ipc_helpers.h"
  11. #include "core/hle/kernel/client_port.h"
  12. #include "core/hle/kernel/handle_table.h"
  13. #include "core/hle/kernel/process.h"
  14. #include "core/hle/kernel/server_port.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/hle/service/acc/acc.h"
  17. #include "core/hle/service/am/am.h"
  18. #include "core/hle/service/aoc/aoc_u.h"
  19. #include "core/hle/service/apm/apm.h"
  20. #include "core/hle/service/audio/audio.h"
  21. #include "core/hle/service/filesystem/filesystem.h"
  22. #include "core/hle/service/hid/hid.h"
  23. #include "core/hle/service/lm/lm.h"
  24. #include "core/hle/service/nifm/nifm.h"
  25. #include "core/hle/service/ns/ns.h"
  26. #include "core/hle/service/nvdrv/nvdrv.h"
  27. #include "core/hle/service/pctl/pctl.h"
  28. #include "core/hle/service/service.h"
  29. #include "core/hle/service/set/set.h"
  30. #include "core/hle/service/sm/controller.h"
  31. #include "core/hle/service/sm/sm.h"
  32. #include "core/hle/service/sockets/sockets.h"
  33. #include "core/hle/service/time/time.h"
  34. #include "core/hle/service/vi/vi.h"
  35. using Kernel::ClientPort;
  36. using Kernel::ServerPort;
  37. using Kernel::SharedPtr;
  38. namespace Service {
  39. std::unordered_map<std::string, SharedPtr<ClientPort>> g_kernel_named_ports;
  40. /**
  41. * Creates a function string for logging, complete with the name (or header code, depending
  42. * on what's passed in) the port name, and all the cmd_buff arguments.
  43. */
  44. static std::string MakeFunctionString(const char* name, const char* port_name,
  45. const u32* cmd_buff) {
  46. // Number of params == bits 0-5 + bits 6-11
  47. int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F);
  48. std::string function_string =
  49. Common::StringFromFormat("function '%s': port=%s", name, port_name);
  50. for (int i = 1; i <= num_params; ++i) {
  51. function_string += Common::StringFromFormat(", cmd_buff[%i]=0x%X", i, cmd_buff[i]);
  52. }
  53. return function_string;
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////
  56. ServiceFrameworkBase::ServiceFrameworkBase(const char* service_name, u32 max_sessions,
  57. InvokerFn* handler_invoker)
  58. : service_name(service_name), max_sessions(max_sessions), handler_invoker(handler_invoker) {}
  59. ServiceFrameworkBase::~ServiceFrameworkBase() = default;
  60. void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) {
  61. ASSERT(port == nullptr);
  62. port = service_manager.RegisterService(service_name, max_sessions).Unwrap();
  63. port->SetHleHandler(shared_from_this());
  64. }
  65. void ServiceFrameworkBase::InstallAsNamedPort() {
  66. ASSERT(port == nullptr);
  67. SharedPtr<ServerPort> server_port;
  68. SharedPtr<ClientPort> client_port;
  69. std::tie(server_port, client_port) = ServerPort::CreatePortPair(max_sessions, service_name);
  70. server_port->SetHleHandler(shared_from_this());
  71. AddNamedPort(service_name, std::move(client_port));
  72. }
  73. Kernel::SharedPtr<Kernel::ClientPort> ServiceFrameworkBase::CreatePort() {
  74. ASSERT(port == nullptr);
  75. Kernel::SharedPtr<Kernel::ServerPort> server_port;
  76. Kernel::SharedPtr<Kernel::ClientPort> client_port;
  77. std::tie(server_port, client_port) =
  78. Kernel::ServerPort::CreatePortPair(max_sessions, service_name);
  79. port = MakeResult<Kernel::SharedPtr<Kernel::ServerPort>>(std::move(server_port)).Unwrap();
  80. port->SetHleHandler(shared_from_this());
  81. return client_port;
  82. }
  83. void ServiceFrameworkBase::RegisterHandlersBase(const FunctionInfoBase* functions, size_t n) {
  84. handlers.reserve(handlers.size() + n);
  85. for (size_t i = 0; i < n; ++i) {
  86. // Usually this array is sorted by id already, so hint to insert at the end
  87. handlers.emplace_hint(handlers.cend(), functions[i].expected_header, functions[i]);
  88. }
  89. }
  90. void ServiceFrameworkBase::ReportUnimplementedFunction(Kernel::HLERequestContext& ctx,
  91. const FunctionInfoBase* info) {
  92. auto cmd_buf = ctx.CommandBuffer();
  93. std::string function_name = info == nullptr ? fmt::format("{}", ctx.GetCommand()) : info->name;
  94. fmt::MemoryWriter w;
  95. w.write("function '{}': port='{}' cmd_buf={{[0]={:#x}", function_name, service_name,
  96. cmd_buf[0]);
  97. for (int i = 1; i <= 8; ++i) {
  98. w.write(", [{}]={:#x}", i, cmd_buf[i]);
  99. }
  100. w << '}';
  101. LOG_ERROR(Service, "unknown / unimplemented %s", w.c_str());
  102. UNIMPLEMENTED();
  103. }
  104. void ServiceFrameworkBase::InvokeRequest(Kernel::HLERequestContext& ctx) {
  105. auto itr = handlers.find(ctx.GetCommand());
  106. const FunctionInfoBase* info = itr == handlers.end() ? nullptr : &itr->second;
  107. if (info == nullptr || info->handler_callback == nullptr) {
  108. return ReportUnimplementedFunction(ctx, info);
  109. }
  110. LOG_TRACE(
  111. Service, "%s",
  112. MakeFunctionString(info->name, GetServiceName().c_str(), ctx.CommandBuffer()).c_str());
  113. handler_invoker(this, info->handler_callback, ctx);
  114. }
  115. ResultCode ServiceFrameworkBase::HandleSyncRequest(Kernel::HLERequestContext& context) {
  116. switch (context.GetCommandType()) {
  117. case IPC::CommandType::Close: {
  118. IPC::ResponseBuilder rb{context, 2};
  119. rb.Push(RESULT_SUCCESS);
  120. return ResultCode(ErrorModule::HIPC, ErrorDescription::RemoteProcessDead);
  121. }
  122. case IPC::CommandType::Control: {
  123. SM::g_service_manager->InvokeControlRequest(context);
  124. break;
  125. }
  126. case IPC::CommandType::Request: {
  127. InvokeRequest(context);
  128. break;
  129. }
  130. default:
  131. UNIMPLEMENTED_MSG("command_type=%d", context.GetCommandType());
  132. }
  133. u32* cmd_buf = (u32*)Memory::GetPointer(Kernel::GetCurrentThread()->GetTLSAddress());
  134. context.WriteToOutgoingCommandBuffer(cmd_buf, *Kernel::g_current_process,
  135. Kernel::g_handle_table);
  136. return RESULT_SUCCESS;
  137. }
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////
  139. // Module interface
  140. // TODO(yuriks): Move to kernel
  141. void AddNamedPort(std::string name, SharedPtr<ClientPort> port) {
  142. g_kernel_named_ports.emplace(std::move(name), std::move(port));
  143. }
  144. /// Initialize ServiceManager
  145. void Init() {
  146. // NVFlinger needs to be accessed by several services like Vi and AppletOE so we instantiate it
  147. // here and pass it into the respective InstallInterfaces functions.
  148. auto nv_flinger = std::make_shared<NVFlinger::NVFlinger>();
  149. SM::g_service_manager = std::make_shared<SM::ServiceManager>();
  150. SM::ServiceManager::InstallInterfaces(SM::g_service_manager);
  151. Account::InstallInterfaces(*SM::g_service_manager);
  152. AM::InstallInterfaces(*SM::g_service_manager, nv_flinger);
  153. AOC::InstallInterfaces(*SM::g_service_manager);
  154. APM::InstallInterfaces(*SM::g_service_manager);
  155. Audio::InstallInterfaces(*SM::g_service_manager);
  156. FileSystem::InstallInterfaces(*SM::g_service_manager);
  157. HID::InstallInterfaces(*SM::g_service_manager);
  158. LM::InstallInterfaces(*SM::g_service_manager);
  159. NIFM::InstallInterfaces(*SM::g_service_manager);
  160. NS::InstallInterfaces(*SM::g_service_manager);
  161. Nvidia::InstallInterfaces(*SM::g_service_manager);
  162. PCTL::InstallInterfaces(*SM::g_service_manager);
  163. Sockets::InstallInterfaces(*SM::g_service_manager);
  164. Time::InstallInterfaces(*SM::g_service_manager);
  165. VI::InstallInterfaces(*SM::g_service_manager, nv_flinger);
  166. Set::InstallInterfaces(*SM::g_service_manager);
  167. LOG_DEBUG(Service, "initialized OK");
  168. }
  169. /// Shutdown ServiceManager
  170. void Shutdown() {
  171. SM::g_service_manager = nullptr;
  172. g_kernel_named_ports.clear();
  173. LOG_DEBUG(Service, "shutdown OK");
  174. }
  175. } // namespace Service