service.cpp 7.8 KB

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