service.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/core.h"
  10. #include "core/hle/ipc.h"
  11. #include "core/hle/ipc_helpers.h"
  12. #include "core/hle/kernel/client_port.h"
  13. #include "core/hle/kernel/handle_table.h"
  14. #include "core/hle/kernel/process.h"
  15. #include "core/hle/kernel/server_port.h"
  16. #include "core/hle/kernel/thread.h"
  17. #include "core/hle/service/acc/acc.h"
  18. #include "core/hle/service/am/am.h"
  19. #include "core/hle/service/aoc/aoc_u.h"
  20. #include "core/hle/service/apm/apm.h"
  21. #include "core/hle/service/audio/audio.h"
  22. #include "core/hle/service/bcat/bcat.h"
  23. #include "core/hle/service/btdrv/btdrv.h"
  24. #include "core/hle/service/erpt/erpt.h"
  25. #include "core/hle/service/es/es.h"
  26. #include "core/hle/service/eupld/eupld.h"
  27. #include "core/hle/service/fatal/fatal.h"
  28. #include "core/hle/service/filesystem/filesystem.h"
  29. #include "core/hle/service/friend/friend.h"
  30. #include "core/hle/service/grc/grc.h"
  31. #include "core/hle/service/hid/hid.h"
  32. #include "core/hle/service/ldn/ldn.h"
  33. #include "core/hle/service/ldr/ldr.h"
  34. #include "core/hle/service/lm/lm.h"
  35. #include "core/hle/service/mm/mm_u.h"
  36. #include "core/hle/service/nfp/nfp.h"
  37. #include "core/hle/service/nifm/nifm.h"
  38. #include "core/hle/service/nim/nim.h"
  39. #include "core/hle/service/ns/ns.h"
  40. #include "core/hle/service/nvdrv/nvdrv.h"
  41. #include "core/hle/service/pctl/pctl.h"
  42. #include "core/hle/service/pm/pm.h"
  43. #include "core/hle/service/prepo/prepo.h"
  44. #include "core/hle/service/service.h"
  45. #include "core/hle/service/set/settings.h"
  46. #include "core/hle/service/sm/controller.h"
  47. #include "core/hle/service/sm/sm.h"
  48. #include "core/hle/service/sockets/sockets.h"
  49. #include "core/hle/service/spl/module.h"
  50. #include "core/hle/service/ssl/ssl.h"
  51. #include "core/hle/service/time/time.h"
  52. #include "core/hle/service/vi/vi.h"
  53. using Kernel::ClientPort;
  54. using Kernel::ServerPort;
  55. using Kernel::SharedPtr;
  56. namespace Service {
  57. std::unordered_map<std::string, SharedPtr<ClientPort>> g_kernel_named_ports;
  58. /**
  59. * Creates a function string for logging, complete with the name (or header code, depending
  60. * on what's passed in) the port name, and all the cmd_buff arguments.
  61. */
  62. static std::string MakeFunctionString(const char* name, const char* port_name,
  63. const u32* cmd_buff) {
  64. // Number of params == bits 0-5 + bits 6-11
  65. int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F);
  66. std::string function_string = fmt::format("function '{}': port={}", name, port_name);
  67. for (int i = 1; i <= num_params; ++i) {
  68. function_string += fmt::format(", cmd_buff[{}]=0x{:X}", i, cmd_buff[i]);
  69. }
  70. return function_string;
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////
  73. ServiceFrameworkBase::ServiceFrameworkBase(const char* service_name, u32 max_sessions,
  74. InvokerFn* handler_invoker)
  75. : service_name(service_name), max_sessions(max_sessions), handler_invoker(handler_invoker) {}
  76. ServiceFrameworkBase::~ServiceFrameworkBase() = default;
  77. void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) {
  78. ASSERT(port == nullptr);
  79. port = service_manager.RegisterService(service_name, max_sessions).Unwrap();
  80. port->SetHleHandler(shared_from_this());
  81. }
  82. void ServiceFrameworkBase::InstallAsNamedPort() {
  83. ASSERT(port == nullptr);
  84. SharedPtr<ServerPort> server_port;
  85. SharedPtr<ClientPort> client_port;
  86. std::tie(server_port, client_port) = ServerPort::CreatePortPair(max_sessions, service_name);
  87. server_port->SetHleHandler(shared_from_this());
  88. AddNamedPort(service_name, std::move(client_port));
  89. }
  90. Kernel::SharedPtr<Kernel::ClientPort> ServiceFrameworkBase::CreatePort() {
  91. ASSERT(port == nullptr);
  92. Kernel::SharedPtr<Kernel::ServerPort> server_port;
  93. Kernel::SharedPtr<Kernel::ClientPort> client_port;
  94. std::tie(server_port, client_port) =
  95. Kernel::ServerPort::CreatePortPair(max_sessions, service_name);
  96. port = MakeResult<Kernel::SharedPtr<Kernel::ServerPort>>(std::move(server_port)).Unwrap();
  97. port->SetHleHandler(shared_from_this());
  98. return client_port;
  99. }
  100. void ServiceFrameworkBase::RegisterHandlersBase(const FunctionInfoBase* functions, size_t n) {
  101. handlers.reserve(handlers.size() + n);
  102. for (size_t i = 0; i < n; ++i) {
  103. // Usually this array is sorted by id already, so hint to insert at the end
  104. handlers.emplace_hint(handlers.cend(), functions[i].expected_header, functions[i]);
  105. }
  106. }
  107. void ServiceFrameworkBase::ReportUnimplementedFunction(Kernel::HLERequestContext& ctx,
  108. const FunctionInfoBase* info) {
  109. auto cmd_buf = ctx.CommandBuffer();
  110. std::string function_name = info == nullptr ? fmt::format("{}", ctx.GetCommand()) : info->name;
  111. fmt::memory_buffer buf;
  112. fmt::format_to(buf, "function '{}': port='{}' cmd_buf={{[0]=0x{:X}", function_name,
  113. service_name, cmd_buf[0]);
  114. for (int i = 1; i <= 8; ++i) {
  115. fmt::format_to(buf, ", [{}]=0x{:X}", i, cmd_buf[i]);
  116. }
  117. buf.push_back('}');
  118. LOG_ERROR(Service, "unknown / unimplemented {}", fmt::to_string(buf));
  119. UNIMPLEMENTED();
  120. }
  121. void ServiceFrameworkBase::InvokeRequest(Kernel::HLERequestContext& ctx) {
  122. auto itr = handlers.find(ctx.GetCommand());
  123. const FunctionInfoBase* info = itr == handlers.end() ? nullptr : &itr->second;
  124. if (info == nullptr || info->handler_callback == nullptr) {
  125. return ReportUnimplementedFunction(ctx, info);
  126. }
  127. LOG_TRACE(
  128. Service, "{}",
  129. MakeFunctionString(info->name, GetServiceName().c_str(), ctx.CommandBuffer()).c_str());
  130. handler_invoker(this, info->handler_callback, ctx);
  131. }
  132. ResultCode ServiceFrameworkBase::HandleSyncRequest(Kernel::HLERequestContext& context) {
  133. switch (context.GetCommandType()) {
  134. case IPC::CommandType::Close: {
  135. IPC::ResponseBuilder rb{context, 2};
  136. rb.Push(RESULT_SUCCESS);
  137. return ResultCode(ErrorModule::HIPC, ErrorDescription::RemoteProcessDead);
  138. }
  139. case IPC::CommandType::ControlWithContext:
  140. case IPC::CommandType::Control: {
  141. Core::System::GetInstance().ServiceManager().InvokeControlRequest(context);
  142. break;
  143. }
  144. case IPC::CommandType::RequestWithContext:
  145. case IPC::CommandType::Request: {
  146. InvokeRequest(context);
  147. break;
  148. }
  149. default:
  150. UNIMPLEMENTED_MSG("command_type={}", static_cast<int>(context.GetCommandType()));
  151. }
  152. context.WriteToOutgoingCommandBuffer(*Kernel::GetCurrentThread());
  153. return RESULT_SUCCESS;
  154. }
  155. ////////////////////////////////////////////////////////////////////////////////////////////////////
  156. // Module interface
  157. // TODO(yuriks): Move to kernel
  158. void AddNamedPort(std::string name, SharedPtr<ClientPort> port) {
  159. g_kernel_named_ports.emplace(std::move(name), std::move(port));
  160. }
  161. /// Initialize ServiceManager
  162. void Init(std::shared_ptr<SM::ServiceManager>& sm) {
  163. // NVFlinger needs to be accessed by several services like Vi and AppletOE so we instantiate it
  164. // here and pass it into the respective InstallInterfaces functions.
  165. auto nv_flinger = std::make_shared<NVFlinger::NVFlinger>();
  166. SM::ServiceManager::InstallInterfaces(sm);
  167. Account::InstallInterfaces(*sm);
  168. AM::InstallInterfaces(*sm, nv_flinger);
  169. AOC::InstallInterfaces(*sm);
  170. APM::InstallInterfaces(*sm);
  171. Audio::InstallInterfaces(*sm);
  172. BCAT::InstallInterfaces(*sm);
  173. BtDrv::InstallInterfaces(*sm);
  174. ERPT::InstallInterfaces(*sm);
  175. ES::InstallInterfaces(*sm);
  176. EUPLD::InstallInterfaces(*sm);
  177. Fatal::InstallInterfaces(*sm);
  178. FileSystem::InstallInterfaces(*sm);
  179. Friend::InstallInterfaces(*sm);
  180. GRC::InstallInterfaces(*sm);
  181. HID::InstallInterfaces(*sm);
  182. LDN::InstallInterfaces(*sm);
  183. LDR::InstallInterfaces(*sm);
  184. LM::InstallInterfaces(*sm);
  185. MM::InstallInterfaces(*sm);
  186. NFP::InstallInterfaces(*sm);
  187. NIFM::InstallInterfaces(*sm);
  188. NIM::InstallInterfaces(*sm);
  189. NS::InstallInterfaces(*sm);
  190. Nvidia::InstallInterfaces(*sm);
  191. PCTL::InstallInterfaces(*sm);
  192. PlayReport::InstallInterfaces(*sm);
  193. PM::InstallInterfaces(*sm);
  194. Sockets::InstallInterfaces(*sm);
  195. SPL::InstallInterfaces(*sm);
  196. SSL::InstallInterfaces(*sm);
  197. Time::InstallInterfaces(*sm);
  198. VI::InstallInterfaces(*sm, nv_flinger);
  199. Set::InstallInterfaces(*sm);
  200. LOG_DEBUG(Service, "initialized OK");
  201. }
  202. /// Shutdown ServiceManager
  203. void Shutdown() {
  204. g_kernel_named_ports.clear();
  205. LOG_DEBUG(Service, "shutdown OK");
  206. }
  207. } // namespace Service