service.cpp 9.8 KB

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