service.cpp 9.9 KB

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