service.cpp 9.6 KB

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