service.cpp 9.3 KB

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