service.cpp
1 // Copyright 2014 Citra Emulator Project 2 // Licensed under GPLv2 or any later version 3 // Refer to the license.txt file included. 4 5 #include <algorithm> 6 #include <fmt/format.h> 7 #include "common/assert.h" 8 #include "common/logging/log.h" 9 #include "core/core.h" 10 #include "core/hle/ipc.h" 11 #include "core/hle/kernel/client_port.h" 12 #include "core/hle/kernel/handle_table.h" 13 #include "core/hle/kernel/process.h" 14 #include "core/hle/kernel/server_port.h" 15 #include "core/hle/kernel/server_session.h" 16 #include "core/hle/service/ac/ac.h" 17 #include "core/hle/service/act/act.h" 18 #include "core/hle/service/am/am.h" 19 #include "core/hle/service/apt/apt.h" 20 #include "core/hle/service/boss/boss.h" 21 #include "core/hle/service/cam/cam.h" 22 #include "core/hle/service/cam/y2r_u.h" 23 #include "core/hle/service/cecd/cecd.h" 24 #include "core/hle/service/cfg/cfg.h" 25 #include "core/hle/service/csnd/csnd_snd.h" 26 #include "core/hle/service/dlp/dlp.h" 27 #include "core/hle/service/dsp/dsp_dsp.h" 28 #include "core/hle/service/err/err_f.h" 29 #include "core/hle/service/frd/frd.h" 30 #include "core/hle/service/fs/archive.h" 31 #include "core/hle/service/fs/fs_user.h" 32 #include "core/hle/service/gsp/gsp.h" 33 #include "core/hle/service/gsp/gsp_lcd.h" 34 #include "core/hle/service/hid/hid.h" 35 #include "core/hle/service/http/http_c.h" 36 #include "core/hle/service/ir/ir.h" 37 #include "core/hle/service/ldr_ro/ldr_ro.h" 38 #include "core/hle/service/mcu/mcu.h" 39 #include "core/hle/service/mic/mic_u.h" 40 #include "core/hle/service/mvd/mvd.h" 41 #include "core/hle/service/ndm/ndm_u.h" 42 #include "core/hle/service/news/news.h" 43 #include "core/hle/service/nfc/nfc.h" 44 #include "core/hle/service/nim/nim.h" 45 #include "core/hle/service/nwm/nwm.h" 46 #include "core/hle/service/plgldr/plgldr.h" 47 #include "core/hle/service/pm/pm.h" 48 #include "core/hle/service/ps/ps_ps.h" 49 #include "core/hle/service/ptm/ptm.h" 50 #include "core/hle/service/pxi/pxi.h" 51 #include "core/hle/service/qtm/qtm.h" 52 #include "core/hle/service/service.h" 53 #include "core/hle/service/sm/sm.h" 54 #include "core/hle/service/sm/srv.h" 55 #include "core/hle/service/soc/soc_u.h" 56 #include "core/hle/service/ssl/ssl_c.h" 57 #include "core/loader/loader.h" 58 59 namespace Service { 60 61 const std::array<ServiceModuleInfo, 41> service_module_map{ 62 {{"FS", 0x00040130'00001102, FS::InstallInterfaces}, 63 {"PM", 0x00040130'00001202, PM::InstallInterfaces}, 64 {"LDR", 0x00040130'00003702, LDR::InstallInterfaces}, 65 {"PXI", 0x00040130'00001402, PXI::InstallInterfaces}, 66 67 {"ERR", 0x00040030'00008A02, ERR::InstallInterfaces}, 68 {"AC", 0x00040130'00002402, AC::InstallInterfaces}, 69 {"ACT", 0x00040130'00003802, ACT::InstallInterfaces}, 70 {"AM", 0x00040130'00001502, AM::InstallInterfaces}, 71 {"BOSS", 0x00040130'00003402, BOSS::InstallInterfaces}, 72 {"CAM", 0x00040130'00001602, 73 [](Core::System& system) { 74 CAM::InstallInterfaces(system); 75 Y2R::InstallInterfaces(system); 76 }}, 77 {"CECD", 0x00040130'00002602, CECD::InstallInterfaces}, 78 {"CFG", 0x00040130'00001702, CFG::InstallInterfaces}, 79 {"DLP", 0x00040130'00002802, DLP::InstallInterfaces}, 80 {"DSP", 0x00040130'00001A02, DSP::InstallInterfaces}, 81 {"FRD", 0x00040130'00003202, FRD::InstallInterfaces}, 82 {"GSP", 0x00040130'00001C02, GSP::InstallInterfaces}, 83 {"HID", 0x00040130'00001D02, HID::InstallInterfaces}, 84 {"IR", 0x00040130'00003302, IR::InstallInterfaces}, 85 {"MIC", 0x00040130'00002002, MIC::InstallInterfaces}, 86 {"MVD", 0x00040130'20004102, MVD::InstallInterfaces}, 87 {"NDM", 0x00040130'00002B02, NDM::InstallInterfaces}, 88 {"NEWS", 0x00040130'00003502, NEWS::InstallInterfaces}, 89 {"NFC", 0x00040130'00004002, NFC::InstallInterfaces}, 90 {"NIM", 0x00040130'00002C02, NIM::InstallInterfaces}, 91 {"NS", 0x00040130'00008002, APT::InstallInterfaces}, 92 {"NWM", 0x00040130'00002D02, NWM::InstallInterfaces}, 93 {"PTM", 0x00040130'00002202, PTM::InstallInterfaces}, 94 {"QTM", 0x00040130'00004202, QTM::InstallInterfaces}, 95 {"CSND", 0x00040130'00002702, CSND::InstallInterfaces}, 96 {"HTTP", 0x00040130'00002902, HTTP::InstallInterfaces}, 97 {"SOC", 0x00040130'00002E02, SOC::InstallInterfaces}, 98 {"SSL", 0x00040130'00002F02, SSL::InstallInterfaces}, 99 {"PS", 0x00040130'00003102, PS::InstallInterfaces}, 100 {"PLGLDR", 0x00040130'00006902, PLGLDR::InstallInterfaces}, 101 // no HLE implementation 102 {"CDC", 0x00040130'00001802, nullptr}, 103 {"GPIO", 0x00040130'00001B02, nullptr}, 104 {"I2C", 0x00040130'00001E02, nullptr}, 105 {"MCU", 0x00040130'00001F02, MCU::InstallInterfaces}, 106 {"MP", 0x00040130'00002A02, nullptr}, 107 {"PDN", 0x00040130'00002102, nullptr}, 108 {"SPI", 0x00040130'00002302, nullptr}}}; 109 110 /** 111 * Creates a function string for logging, complete with the name (or header code, depending 112 * on what's passed in) the port name, and all the cmd_buff arguments. 113 */ 114 [[maybe_unused]] static std::string MakeFunctionString(std::string_view name, 115 std::string_view port_name, 116 const u32* cmd_buff) { 117 // Number of params == bits 0-5 + bits 6-11 118 int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); 119 120 std::string function_string = fmt::format("function '{}': port={}", name, port_name); 121 for (int i = 1; i <= num_params; ++i) { 122 function_string += fmt::format(", cmd_buff[{}]={:#X}", i, cmd_buff[i]); 123 } 124 return function_string; 125 } 126 127 ServiceFrameworkBase::ServiceFrameworkBase(const char* service_name, u32 max_sessions, 128 InvokerFn* handler_invoker) 129 : service_name(service_name), max_sessions(max_sessions), handler_invoker(handler_invoker) {} 130 131 ServiceFrameworkBase::~ServiceFrameworkBase() = default; 132 133 void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) { 134 std::shared_ptr<Kernel::ServerPort> port; 135 R_ASSERT(service_manager.RegisterService(std::addressof(port), service_name, max_sessions)); 136 port->SetHleHandler(shared_from_this()); 137 } 138 139 void ServiceFrameworkBase::InstallAsNamedPort(Kernel::KernelSystem& kernel) { 140 auto [server_port, client_port] = kernel.CreatePortPair(max_sessions, service_name); 141 server_port->SetHleHandler(shared_from_this()); 142 kernel.AddNamedPort(service_name, std::move(client_port)); 143 } 144 145 void ServiceFrameworkBase::RegisterHandlersBase(const FunctionInfoBase* functions, std::size_t n) { 146 handlers.reserve(handlers.size() + n); 147 for (std::size_t i = 0; i < n; ++i) { 148 // Usually this array is sorted by id already, so hint to insert at the end 149 handlers.emplace_hint(handlers.cend(), functions[i].command_id, functions[i]); 150 } 151 } 152 153 void ServiceFrameworkBase::ReportUnimplementedFunction(u32* cmd_buf, const FunctionInfoBase* info) { 154 IPC::Header header{cmd_buf[0]}; 155 int num_params = header.normal_params_size + header.translate_params_size; 156 std::string function_name = info == nullptr ? fmt::format("{:#08x}", cmd_buf[0]) : info->name; 157 158 std::string result = 159 fmt::format("function '{}': port='{}' cmd_buf={{[0]={:#x} (0x{:04X}, {}, {})", 160 function_name, service_name, header.raw, header.command_id.Value(), 161 header.normal_params_size.Value(), header.translate_params_size.Value()); 162 for (int i = 1; i <= num_params; ++i) { 163 result += fmt::format(", [{}]={:#x}", i, cmd_buf[i]); 164 } 165 166 result.push_back('}'); 167 168 LOG_ERROR(Service, "unknown / unimplemented {}", result); 169 // TODO(bunnei): Hack - ignore error 170 header.normal_params_size.Assign(1); 171 header.translate_params_size.Assign(0); 172 cmd_buf[0] = header.raw; 173 cmd_buf[1] = 0; 174 } 175 176 void ServiceFrameworkBase::HandleSyncRequest(Kernel::HLERequestContext& context) { 177 auto itr = handlers.find(context.CommandHeader().command_id.Value()); 178 const FunctionInfoBase* info = itr == handlers.end() ? nullptr : &itr->second; 179 if (info == nullptr || info->handler_callback == nullptr) { 180 context.ReportUnimplemented(); 181 return ReportUnimplementedFunction(context.CommandBuffer(), info); 182 } 183 184 LOG_TRACE(Service, "{}", 185 MakeFunctionString(info->name, GetServiceName(), context.CommandBuffer())); 186 handler_invoker(this, info->handler_callback, context); 187 } 188 189 std::string ServiceFrameworkBase::GetFunctionName(IPC::Header header) const { 190 auto itr = handlers.find(header.command_id.Value()); 191 if (itr == handlers.end()) { 192 return ""; 193 } 194 195 return itr->second.name; 196 } 197 198 static bool AttemptLLE(const ServiceModuleInfo& service_module) { 199 if (!Settings::values.lle_modules.at(service_module.name)) 200 return false; 201 std::unique_ptr<Loader::AppLoader> loader = 202 Loader::GetLoader(AM::GetTitleContentPath(FS::MediaType::NAND, service_module.title_id)); 203 if (!loader) { 204 LOG_ERROR(Service, 205 "Service module \"{}\" could not be loaded; Defaulting to HLE implementation.", 206 service_module.name); 207 return false; 208 } 209 std::shared_ptr<Kernel::Process> process; 210 loader->Load(process); 211 LOG_DEBUG(Service, "Service module \"{}\" has been successfully loaded.", service_module.name); 212 return true; 213 } 214 215 /// Initialize ServiceManager 216 void Init(Core::System& core) { 217 SM::ServiceManager::InstallInterfaces(core); 218 core.Kernel().SetAppMainThreadExtendedSleep(false); 219 bool lle_module_present = false; 220 221 for (const auto& service_module : service_module_map) { 222 const bool has_lle = AttemptLLE(service_module); 223 if (!has_lle && service_module.init_function != nullptr) { 224 service_module.init_function(core); 225 } 226 lle_module_present |= has_lle; 227 } 228 if (lle_module_present) { 229 // If there is at least one LLE module, tell the kernel to 230 // add a extended sleep to the app main thread (if option enabled). 231 core.Kernel().SetAppMainThreadExtendedSleep(true); 232 } 233 LOG_DEBUG(Service, "initialized OK"); 234 } 235 236 } // namespace Service