service.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <fmt/format.h>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "common/settings.h"
  8. #include "core/core.h"
  9. #include "core/hle/ipc.h"
  10. #include "core/hle/ipc_helpers.h"
  11. #include "core/hle/kernel/k_process.h"
  12. #include "core/hle/kernel/k_server_port.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/service/acc/acc.h"
  15. #include "core/hle/service/am/am.h"
  16. #include "core/hle/service/aoc/aoc_u.h"
  17. #include "core/hle/service/apm/apm.h"
  18. #include "core/hle/service/audio/audio.h"
  19. #include "core/hle/service/bcat/bcat_module.h"
  20. #include "core/hle/service/bpc/bpc.h"
  21. #include "core/hle/service/btdrv/btdrv.h"
  22. #include "core/hle/service/btm/btm.h"
  23. #include "core/hle/service/caps/caps.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/fgm/fgm.h"
  29. #include "core/hle/service/filesystem/filesystem.h"
  30. #include "core/hle/service/friend/friend.h"
  31. #include "core/hle/service/glue/glue.h"
  32. #include "core/hle/service/grc/grc.h"
  33. #include "core/hle/service/hid/hid.h"
  34. #include "core/hle/service/lbl/lbl.h"
  35. #include "core/hle/service/ldn/ldn.h"
  36. #include "core/hle/service/ldr/ldr.h"
  37. #include "core/hle/service/lm/lm.h"
  38. #include "core/hle/service/mig/mig.h"
  39. #include "core/hle/service/mii/mii.h"
  40. #include "core/hle/service/mm/mm_u.h"
  41. #include "core/hle/service/mnpp/mnpp_app.h"
  42. #include "core/hle/service/ncm/ncm.h"
  43. #include "core/hle/service/nfc/nfc.h"
  44. #include "core/hle/service/nfp/nfp.h"
  45. #include "core/hle/service/ngct/ngct.h"
  46. #include "core/hle/service/nifm/nifm.h"
  47. #include "core/hle/service/nim/nim.h"
  48. #include "core/hle/service/npns/npns.h"
  49. #include "core/hle/service/ns/ns.h"
  50. #include "core/hle/service/nvdrv/nvdrv.h"
  51. #include "core/hle/service/nvflinger/nvflinger.h"
  52. #include "core/hle/service/olsc/olsc.h"
  53. #include "core/hle/service/pcie/pcie.h"
  54. #include "core/hle/service/pctl/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/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. ServiceFrameworkBase::ServiceFrameworkBase(Core::System& system_, const char* service_name_,
  88. u32 max_sessions_, InvokerFn* handler_invoker_)
  89. : SessionRequestHandler(system_.Kernel(), service_name_), system{system_},
  90. service_name{service_name_}, max_sessions{max_sessions_}, handler_invoker{handler_invoker_} {}
  91. ServiceFrameworkBase::~ServiceFrameworkBase() {
  92. // Wait for other threads to release access before destroying
  93. const auto guard = LockService();
  94. }
  95. void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) {
  96. const auto guard = LockService();
  97. ASSERT(!service_registered);
  98. service_manager.RegisterService(service_name, max_sessions, shared_from_this());
  99. service_registered = true;
  100. }
  101. Kernel::KClientPort& ServiceFrameworkBase::CreatePort() {
  102. const auto guard = LockService();
  103. ASSERT(!service_registered);
  104. auto* port = Kernel::KPort::Create(kernel);
  105. port->Initialize(max_sessions, false, service_name);
  106. port->GetServerPort().SetSessionHandler(shared_from_this());
  107. service_registered = true;
  108. return port->GetClientPort();
  109. }
  110. void ServiceFrameworkBase::RegisterHandlersBase(const FunctionInfoBase* functions, std::size_t n) {
  111. handlers.reserve(handlers.size() + n);
  112. for (std::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::RegisterHandlersBaseTipc(const FunctionInfoBase* functions,
  118. std::size_t n) {
  119. handlers_tipc.reserve(handlers_tipc.size() + n);
  120. for (std::size_t i = 0; i < n; ++i) {
  121. // Usually this array is sorted by id already, so hint to insert at the end
  122. handlers_tipc.emplace_hint(handlers_tipc.cend(), functions[i].expected_header,
  123. functions[i]);
  124. }
  125. }
  126. void ServiceFrameworkBase::ReportUnimplementedFunction(Kernel::HLERequestContext& ctx,
  127. const FunctionInfoBase* info) {
  128. auto cmd_buf = ctx.CommandBuffer();
  129. std::string function_name = info == nullptr ? fmt::format("{}", ctx.GetCommand()) : info->name;
  130. fmt::memory_buffer buf;
  131. fmt::format_to(std::back_inserter(buf), "function '{}': port='{}' cmd_buf={{[0]=0x{:X}",
  132. function_name, service_name, cmd_buf[0]);
  133. for (int i = 1; i <= 8; ++i) {
  134. fmt::format_to(std::back_inserter(buf), ", [{}]=0x{:X}", i, cmd_buf[i]);
  135. }
  136. buf.push_back('}');
  137. system.GetReporter().SaveUnimplementedFunctionReport(ctx, ctx.GetCommand(), function_name,
  138. service_name);
  139. UNIMPLEMENTED_MSG("Unknown / unimplemented {}", fmt::to_string(buf));
  140. if (Settings::values.use_auto_stub) {
  141. LOG_WARNING(Service, "Using auto stub fallback!");
  142. IPC::ResponseBuilder rb{ctx, 2};
  143. rb.Push(ResultSuccess);
  144. }
  145. }
  146. void ServiceFrameworkBase::InvokeRequest(Kernel::HLERequestContext& ctx) {
  147. auto itr = handlers.find(ctx.GetCommand());
  148. const FunctionInfoBase* info = itr == handlers.end() ? nullptr : &itr->second;
  149. if (info == nullptr || info->handler_callback == nullptr) {
  150. return ReportUnimplementedFunction(ctx, info);
  151. }
  152. LOG_TRACE(Service, "{}", MakeFunctionString(info->name, GetServiceName(), ctx.CommandBuffer()));
  153. handler_invoker(this, info->handler_callback, ctx);
  154. }
  155. void ServiceFrameworkBase::InvokeRequestTipc(Kernel::HLERequestContext& ctx) {
  156. boost::container::flat_map<u32, FunctionInfoBase>::iterator itr;
  157. itr = handlers_tipc.find(ctx.GetCommand());
  158. const FunctionInfoBase* info = itr == handlers_tipc.end() ? nullptr : &itr->second;
  159. if (info == nullptr || info->handler_callback == nullptr) {
  160. return ReportUnimplementedFunction(ctx, info);
  161. }
  162. LOG_TRACE(Service, "{}", MakeFunctionString(info->name, GetServiceName(), ctx.CommandBuffer()));
  163. handler_invoker(this, info->handler_callback, ctx);
  164. }
  165. ResultCode ServiceFrameworkBase::HandleSyncRequest(Kernel::KServerSession& session,
  166. Kernel::HLERequestContext& ctx) {
  167. const auto guard = LockService();
  168. switch (ctx.GetCommandType()) {
  169. case IPC::CommandType::Close:
  170. case IPC::CommandType::TIPC_Close: {
  171. session.Close();
  172. IPC::ResponseBuilder rb{ctx, 2};
  173. rb.Push(ResultSuccess);
  174. return IPC::ERR_REMOTE_PROCESS_DEAD;
  175. }
  176. case IPC::CommandType::ControlWithContext:
  177. case IPC::CommandType::Control: {
  178. system.ServiceManager().InvokeControlRequest(ctx);
  179. break;
  180. }
  181. case IPC::CommandType::RequestWithContext:
  182. case IPC::CommandType::Request: {
  183. InvokeRequest(ctx);
  184. break;
  185. }
  186. default:
  187. if (ctx.IsTipc()) {
  188. InvokeRequestTipc(ctx);
  189. break;
  190. }
  191. UNIMPLEMENTED_MSG("command_type={}", ctx.GetCommandType());
  192. }
  193. // If emulation was shutdown, we are closing service threads, do not write the response back to
  194. // memory that may be shutting down as well.
  195. if (system.IsPoweredOn()) {
  196. ctx.WriteToOutgoingCommandBuffer(ctx.GetThread());
  197. }
  198. return ResultSuccess;
  199. }
  200. /// Initialize Services
  201. Services::Services(std::shared_ptr<SM::ServiceManager>& sm, Core::System& system)
  202. : nv_flinger{std::make_unique<NVFlinger::NVFlinger>(system)} {
  203. // NVFlinger needs to be accessed by several services like Vi and AppletOE so we instantiate it
  204. // here and pass it into the respective InstallInterfaces functions.
  205. system.GetFileSystemController().CreateFactories(*system.GetFilesystem(), false);
  206. system.Kernel().RegisterNamedService("sm:", SM::ServiceManager::InterfaceFactory);
  207. Account::InstallInterfaces(system);
  208. AM::InstallInterfaces(*sm, *nv_flinger, system);
  209. AOC::InstallInterfaces(*sm, system);
  210. APM::InstallInterfaces(system);
  211. Audio::InstallInterfaces(*sm, system);
  212. BCAT::InstallInterfaces(system);
  213. BPC::InstallInterfaces(*sm, system);
  214. BtDrv::InstallInterfaces(*sm, system);
  215. BTM::InstallInterfaces(*sm, system);
  216. Capture::InstallInterfaces(*sm, system);
  217. ERPT::InstallInterfaces(*sm, system);
  218. ES::InstallInterfaces(*sm, system);
  219. EUPLD::InstallInterfaces(*sm, system);
  220. Fatal::InstallInterfaces(*sm, system);
  221. FGM::InstallInterfaces(*sm, system);
  222. FileSystem::InstallInterfaces(system);
  223. Friend::InstallInterfaces(*sm, system);
  224. Glue::InstallInterfaces(system);
  225. GRC::InstallInterfaces(*sm, system);
  226. HID::InstallInterfaces(*sm, system);
  227. LBL::InstallInterfaces(*sm, system);
  228. LDN::InstallInterfaces(*sm, system);
  229. LDR::InstallInterfaces(*sm, system);
  230. LM::InstallInterfaces(system);
  231. Migration::InstallInterfaces(*sm, system);
  232. Mii::InstallInterfaces(*sm, system);
  233. MM::InstallInterfaces(*sm, system);
  234. MNPP::InstallInterfaces(*sm, system);
  235. NCM::InstallInterfaces(*sm, system);
  236. NFC::InstallInterfaces(*sm, system);
  237. NFP::InstallInterfaces(*sm, system);
  238. NGCT::InstallInterfaces(*sm, system);
  239. NIFM::InstallInterfaces(*sm, system);
  240. NIM::InstallInterfaces(*sm, system);
  241. NPNS::InstallInterfaces(*sm, system);
  242. NS::InstallInterfaces(*sm, system);
  243. Nvidia::InstallInterfaces(*sm, *nv_flinger, system);
  244. OLSC::InstallInterfaces(*sm, system);
  245. PCIe::InstallInterfaces(*sm, system);
  246. PCTL::InstallInterfaces(*sm, system);
  247. PCV::InstallInterfaces(*sm, system);
  248. PlayReport::InstallInterfaces(*sm, system);
  249. PM::InstallInterfaces(system);
  250. PSC::InstallInterfaces(*sm, system);
  251. PSM::InstallInterfaces(*sm, system);
  252. Set::InstallInterfaces(*sm, system);
  253. Sockets::InstallInterfaces(*sm, system);
  254. SPL::InstallInterfaces(*sm, system);
  255. SSL::InstallInterfaces(*sm, system);
  256. Time::InstallInterfaces(system);
  257. USB::InstallInterfaces(*sm, system);
  258. VI::InstallInterfaces(*sm, system, *nv_flinger);
  259. WLAN::InstallInterfaces(*sm, system);
  260. }
  261. Services::~Services() = default;
  262. } // namespace Service