ro.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <mbedtls/sha256.h>
  4. #include "common/scope_exit.h"
  5. #include "core/hle/kernel/k_process.h"
  6. #include "core/hle/service/ipc_helpers.h"
  7. #include "core/hle/service/ro/ro.h"
  8. #include "core/hle/service/ro/ro_nro_utils.h"
  9. #include "core/hle/service/ro/ro_results.h"
  10. #include "core/hle/service/ro/ro_types.h"
  11. #include "core/hle/service/server_manager.h"
  12. #include "core/hle/service/service.h"
  13. namespace Service::RO {
  14. namespace {
  15. // Convenience definitions.
  16. constexpr size_t MaxSessions = 0x3;
  17. constexpr size_t MaxNrrInfos = 0x40;
  18. constexpr size_t MaxNroInfos = 0x40;
  19. constexpr u64 InvalidProcessId = 0xffffffffffffffffULL;
  20. constexpr u64 InvalidContextId = 0xffffffffffffffffULL;
  21. // Types.
  22. using Sha256Hash = std::array<u8, 32>;
  23. struct NroInfo {
  24. u64 base_address;
  25. u64 nro_heap_address;
  26. u64 nro_heap_size;
  27. u64 bss_heap_address;
  28. u64 bss_heap_size;
  29. u64 code_size;
  30. u64 rw_size;
  31. ModuleId module_id;
  32. };
  33. struct NrrInfo {
  34. u64 nrr_heap_address;
  35. u64 nrr_heap_size;
  36. // Verification.
  37. std::vector<Sha256Hash> hashes;
  38. };
  39. struct ProcessContext {
  40. constexpr ProcessContext() = default;
  41. void Initialize(Kernel::KProcess* process, u64 process_id) {
  42. ASSERT(!m_in_use);
  43. m_nro_in_use = {};
  44. m_nrr_in_use = {};
  45. m_nro_infos = {};
  46. m_nrr_infos = {};
  47. m_process = process;
  48. m_process_id = process_id;
  49. m_in_use = true;
  50. if (m_process) {
  51. m_process->Open();
  52. }
  53. }
  54. void Finalize() {
  55. ASSERT(m_in_use);
  56. if (m_process) {
  57. m_process->Close();
  58. }
  59. m_nro_in_use = {};
  60. m_nrr_in_use = {};
  61. m_nro_infos = {};
  62. m_nrr_infos = {};
  63. m_process = nullptr;
  64. m_process_id = InvalidProcessId;
  65. m_in_use = false;
  66. }
  67. Kernel::KProcess* GetProcess() const {
  68. return m_process;
  69. }
  70. u64 GetProcessId() const {
  71. return m_process_id;
  72. }
  73. bool IsFree() const {
  74. return !m_in_use;
  75. }
  76. u64 GetProgramId(Kernel::KProcess* other_process) const {
  77. // Automatically select a handle, allowing for override.
  78. if (other_process) {
  79. return other_process->GetProgramId();
  80. } else if (m_process) {
  81. return m_process->GetProgramId();
  82. } else {
  83. return 0;
  84. }
  85. }
  86. Result GetNrrInfoByAddress(NrrInfo** out, u64 nrr_heap_address) {
  87. for (size_t i = 0; i < MaxNrrInfos; i++) {
  88. if (m_nrr_in_use[i] && m_nrr_infos[i].nrr_heap_address == nrr_heap_address) {
  89. if (out != nullptr) {
  90. *out = std::addressof(m_nrr_infos[i]);
  91. }
  92. R_SUCCEED();
  93. }
  94. }
  95. R_THROW(RO::ResultNotRegistered);
  96. }
  97. Result GetFreeNrrInfo(NrrInfo** out) {
  98. for (size_t i = 0; i < MaxNrrInfos; i++) {
  99. if (!m_nrr_in_use[i]) {
  100. if (out != nullptr) {
  101. *out = std::addressof(m_nrr_infos[i]);
  102. }
  103. R_SUCCEED();
  104. }
  105. }
  106. R_THROW(RO::ResultTooManyNrr);
  107. }
  108. Result GetNroInfoByAddress(NroInfo** out, u64 nro_address) {
  109. for (size_t i = 0; i < MaxNroInfos; i++) {
  110. if (m_nro_in_use[i] && m_nro_infos[i].base_address == nro_address) {
  111. if (out != nullptr) {
  112. *out = std::addressof(m_nro_infos[i]);
  113. }
  114. R_SUCCEED();
  115. }
  116. }
  117. R_THROW(RO::ResultNotLoaded);
  118. }
  119. Result GetNroInfoByModuleId(NroInfo** out, const ModuleId* module_id) {
  120. for (size_t i = 0; i < MaxNroInfos; i++) {
  121. if (m_nro_in_use[i] && std::memcmp(std::addressof(m_nro_infos[i].module_id), module_id,
  122. sizeof(*module_id)) == 0) {
  123. if (out != nullptr) {
  124. *out = std::addressof(m_nro_infos[i]);
  125. }
  126. R_SUCCEED();
  127. }
  128. }
  129. R_THROW(RO::ResultNotLoaded);
  130. }
  131. Result GetFreeNroInfo(NroInfo** out) {
  132. for (size_t i = 0; i < MaxNroInfos; i++) {
  133. if (!m_nro_in_use[i]) {
  134. if (out != nullptr) {
  135. *out = std::addressof(m_nro_infos[i]);
  136. }
  137. R_SUCCEED();
  138. }
  139. }
  140. R_THROW(RO::ResultTooManyNro);
  141. }
  142. Result ValidateHasNroHash(u64 base_address, const NroHeader* nro_header) const {
  143. // Calculate hash.
  144. Sha256Hash hash;
  145. {
  146. const u64 size = nro_header->GetSize();
  147. std::vector<u8> nro_data(size);
  148. m_process->GetMemory().ReadBlock(base_address, nro_data.data(), size);
  149. mbedtls_sha256_ret(nro_data.data(), size, hash.data(), 0);
  150. }
  151. for (size_t i = 0; i < MaxNrrInfos; i++) {
  152. // Ensure we only check NRRs that are used.
  153. if (!m_nrr_in_use[i]) {
  154. continue;
  155. }
  156. // Locate the hash within the hash list.
  157. const auto hash_it = std::ranges::find(m_nrr_infos[i].hashes, hash);
  158. if (hash_it == m_nrr_infos[i].hashes.end()) {
  159. continue;
  160. }
  161. // The hash is valid!
  162. R_SUCCEED();
  163. }
  164. R_THROW(RO::ResultNotAuthorized);
  165. }
  166. Result ValidateNro(ModuleId* out_module_id, u64* out_rx_size, u64* out_ro_size,
  167. u64* out_rw_size, u64 base_address, u64 expected_nro_size,
  168. u64 expected_bss_size) {
  169. // Ensure we have a process to work on.
  170. R_UNLESS(m_process != nullptr, RO::ResultInvalidProcess);
  171. // Read the NRO header.
  172. NroHeader header{};
  173. m_process->GetMemory().ReadBlock(base_address, std::addressof(header), sizeof(header));
  174. // Validate header.
  175. R_UNLESS(header.IsMagicValid(), RO::ResultInvalidNro);
  176. // Read sizes from header.
  177. const u64 nro_size = header.GetSize();
  178. const u64 text_ofs = header.GetTextOffset();
  179. const u64 text_size = header.GetTextSize();
  180. const u64 ro_ofs = header.GetRoOffset();
  181. const u64 ro_size = header.GetRoSize();
  182. const u64 rw_ofs = header.GetRwOffset();
  183. const u64 rw_size = header.GetRwSize();
  184. const u64 bss_size = header.GetBssSize();
  185. // Validate sizes meet expected.
  186. R_UNLESS(nro_size == expected_nro_size, RO::ResultInvalidNro);
  187. R_UNLESS(bss_size == expected_bss_size, RO::ResultInvalidNro);
  188. // Validate all sizes are aligned.
  189. R_UNLESS(Common::IsAligned(text_size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidNro);
  190. R_UNLESS(Common::IsAligned(ro_size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidNro);
  191. R_UNLESS(Common::IsAligned(rw_size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidNro);
  192. R_UNLESS(Common::IsAligned(bss_size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidNro);
  193. // Validate sections are in order.
  194. R_UNLESS(text_ofs <= ro_ofs, RO::ResultInvalidNro);
  195. R_UNLESS(ro_ofs <= rw_ofs, RO::ResultInvalidNro);
  196. // Validate sections are sequential and contiguous.
  197. R_UNLESS(text_ofs == 0, RO::ResultInvalidNro);
  198. R_UNLESS(text_ofs + text_size == ro_ofs, RO::ResultInvalidNro);
  199. R_UNLESS(ro_ofs + ro_size == rw_ofs, RO::ResultInvalidNro);
  200. R_UNLESS(rw_ofs + rw_size == nro_size, RO::ResultInvalidNro);
  201. // Verify NRO hash.
  202. R_TRY(this->ValidateHasNroHash(base_address, std::addressof(header)));
  203. // Check if NRO has already been loaded.
  204. const ModuleId* module_id = header.GetModuleId();
  205. R_UNLESS(R_FAILED(this->GetNroInfoByModuleId(nullptr, module_id)), RO::ResultAlreadyLoaded);
  206. // Apply patches to NRO.
  207. // LocateAndApplyIpsPatchesToModule(module_id, static_cast<u8*>(mapped_memory), nro_size);
  208. // Copy to output.
  209. *out_module_id = *module_id;
  210. *out_rx_size = text_size;
  211. *out_ro_size = ro_size;
  212. *out_rw_size = rw_size;
  213. R_SUCCEED();
  214. }
  215. void SetNrrInfoInUse(const NrrInfo* info, bool in_use) {
  216. ASSERT(std::addressof(m_nrr_infos[0]) <= info &&
  217. info <= std::addressof(m_nrr_infos[MaxNrrInfos - 1]));
  218. const size_t index = info - std::addressof(m_nrr_infos[0]);
  219. m_nrr_in_use[index] = in_use;
  220. }
  221. void SetNroInfoInUse(const NroInfo* info, bool in_use) {
  222. ASSERT(std::addressof(m_nro_infos[0]) <= info &&
  223. info <= std::addressof(m_nro_infos[MaxNroInfos - 1]));
  224. const size_t index = info - std::addressof(m_nro_infos[0]);
  225. m_nro_in_use[index] = in_use;
  226. }
  227. private:
  228. std::array<bool, MaxNroInfos> m_nro_in_use{};
  229. std::array<bool, MaxNrrInfos> m_nrr_in_use{};
  230. std::array<NroInfo, MaxNroInfos> m_nro_infos{};
  231. std::array<NrrInfo, MaxNrrInfos> m_nrr_infos{};
  232. Kernel::KProcess* m_process{};
  233. u64 m_process_id{InvalidProcessId};
  234. bool m_in_use{};
  235. };
  236. Result ValidateAddressAndNonZeroSize(u64 address, u64 size) {
  237. R_UNLESS(Common::IsAligned(address, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidAddress);
  238. R_UNLESS(size != 0, RO::ResultInvalidSize);
  239. R_UNLESS(Common::IsAligned(size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidSize);
  240. R_UNLESS(address < address + size, RO::ResultInvalidSize);
  241. R_SUCCEED();
  242. }
  243. Result ValidateAddressAndSize(u64 address, u64 size) {
  244. R_UNLESS(Common::IsAligned(address, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidAddress);
  245. R_UNLESS(Common::IsAligned(size, Core::Memory::YUZU_PAGESIZE), RO::ResultInvalidSize);
  246. R_UNLESS(size == 0 || address < address + size, RO::ResultInvalidSize);
  247. R_SUCCEED();
  248. }
  249. class RoContext {
  250. public:
  251. explicit RoContext() = default;
  252. Result RegisterProcess(size_t* out_context_id, Kernel::KProcess* process, u64 process_id) {
  253. // Validate process id.
  254. R_UNLESS(process->GetProcessId() == process_id, RO::ResultInvalidProcess);
  255. // Check if a process context already exists.
  256. R_UNLESS(this->GetContextByProcessId(process_id) == nullptr, RO::ResultInvalidSession);
  257. // Allocate a context to manage the process handle.
  258. *out_context_id = this->AllocateContext(process, process_id);
  259. R_SUCCEED();
  260. }
  261. Result ValidateProcess(size_t context_id, u64 process_id) {
  262. const ProcessContext* ctx = this->GetContextById(context_id);
  263. R_UNLESS(ctx != nullptr, RO::ResultInvalidProcess);
  264. R_UNLESS(ctx->GetProcessId() == process_id, RO::ResultInvalidProcess);
  265. R_SUCCEED();
  266. }
  267. void UnregisterProcess(size_t context_id) {
  268. this->FreeContext(context_id);
  269. }
  270. Result RegisterModuleInfo(size_t context_id, u64 nrr_address, u64 nrr_size, NrrKind nrr_kind,
  271. bool enforce_nrr_kind) {
  272. // Get context.
  273. ProcessContext* context = this->GetContextById(context_id);
  274. ASSERT(context != nullptr);
  275. // Validate address/size.
  276. R_TRY(ValidateAddressAndNonZeroSize(nrr_address, nrr_size));
  277. // Check we have space for a new NRR.
  278. NrrInfo* nrr_info = nullptr;
  279. R_TRY(context->GetFreeNrrInfo(std::addressof(nrr_info)));
  280. // Ensure we have a valid process to read from.
  281. Kernel::KProcess* process = context->GetProcess();
  282. R_UNLESS(process != nullptr, RO::ResultInvalidProcess);
  283. // Read NRR.
  284. NrrHeader header{};
  285. process->GetMemory().ReadBlock(nrr_address, std::addressof(header), sizeof(header));
  286. // Set NRR info.
  287. context->SetNrrInfoInUse(nrr_info, true);
  288. nrr_info->nrr_heap_address = nrr_address;
  289. nrr_info->nrr_heap_size = nrr_size;
  290. // Read NRR hash list.
  291. nrr_info->hashes.resize(header.GetNumHashes());
  292. process->GetMemory().ReadBlock(nrr_address + header.GetHashesOffset(),
  293. nrr_info->hashes.data(),
  294. sizeof(Sha256Hash) * header.GetNumHashes());
  295. R_SUCCEED();
  296. }
  297. Result UnregisterModuleInfo(size_t context_id, u64 nrr_address) {
  298. // Get context.
  299. ProcessContext* context = this->GetContextById(context_id);
  300. ASSERT(context != nullptr);
  301. // Validate address.
  302. R_UNLESS(Common::IsAligned(nrr_address, Core::Memory::YUZU_PAGESIZE),
  303. RO::ResultInvalidAddress);
  304. // Check the NRR is loaded.
  305. NrrInfo* nrr_info = nullptr;
  306. R_TRY(context->GetNrrInfoByAddress(std::addressof(nrr_info), nrr_address));
  307. // Nintendo does this unconditionally, whether or not the actual unmap succeeds.
  308. context->SetNrrInfoInUse(nrr_info, false);
  309. *nrr_info = {};
  310. R_SUCCEED();
  311. }
  312. Result MapManualLoadModuleMemory(u64* out_address, size_t context_id, u64 nro_address,
  313. u64 nro_size, u64 bss_address, u64 bss_size) {
  314. // Get context.
  315. ProcessContext* context = this->GetContextById(context_id);
  316. ASSERT(context != nullptr);
  317. // Validate address/size.
  318. R_TRY(ValidateAddressAndNonZeroSize(nro_address, nro_size));
  319. R_TRY(ValidateAddressAndSize(bss_address, bss_size));
  320. const u64 total_size = nro_size + bss_size;
  321. R_UNLESS(total_size >= nro_size, RO::ResultInvalidSize);
  322. R_UNLESS(total_size >= bss_size, RO::ResultInvalidSize);
  323. // Check we have space for a new NRO.
  324. NroInfo* nro_info = nullptr;
  325. R_TRY(context->GetFreeNroInfo(std::addressof(nro_info)));
  326. nro_info->nro_heap_address = nro_address;
  327. nro_info->nro_heap_size = nro_size;
  328. nro_info->bss_heap_address = bss_address;
  329. nro_info->bss_heap_size = bss_size;
  330. // Map the NRO.
  331. R_TRY(MapNro(std::addressof(nro_info->base_address), context->GetProcess(), nro_address,
  332. nro_size, bss_address, bss_size, generate_random));
  333. ON_RESULT_FAILURE {
  334. UnmapNro(context->GetProcess(), nro_info->base_address, nro_address, nro_size,
  335. bss_address, bss_size);
  336. };
  337. // Validate the NRO (parsing region extents).
  338. u64 rx_size = 0, ro_size = 0, rw_size = 0;
  339. R_TRY(context->ValidateNro(std::addressof(nro_info->module_id), std::addressof(rx_size),
  340. std::addressof(ro_size), std::addressof(rw_size),
  341. nro_info->base_address, nro_size, bss_size));
  342. // Set NRO perms.
  343. R_TRY(SetNroPerms(context->GetProcess(), nro_info->base_address, rx_size, ro_size,
  344. rw_size + bss_size));
  345. context->SetNroInfoInUse(nro_info, true);
  346. nro_info->code_size = rx_size + ro_size;
  347. nro_info->rw_size = rw_size;
  348. *out_address = nro_info->base_address;
  349. R_SUCCEED();
  350. }
  351. Result UnmapManualLoadModuleMemory(size_t context_id, u64 nro_address) {
  352. // Get context.
  353. ProcessContext* context = this->GetContextById(context_id);
  354. ASSERT(context != nullptr);
  355. // Validate address.
  356. R_UNLESS(Common::IsAligned(nro_address, Core::Memory::YUZU_PAGESIZE),
  357. RO::ResultInvalidAddress);
  358. // Check the NRO is loaded.
  359. NroInfo* nro_info = nullptr;
  360. R_TRY(context->GetNroInfoByAddress(std::addressof(nro_info), nro_address));
  361. // Unmap.
  362. const NroInfo nro_backup = *nro_info;
  363. {
  364. // Nintendo does this unconditionally, whether or not the actual unmap succeeds.
  365. context->SetNroInfoInUse(nro_info, false);
  366. std::memset(nro_info, 0, sizeof(*nro_info));
  367. }
  368. R_RETURN(UnmapNro(context->GetProcess(), nro_backup.base_address,
  369. nro_backup.nro_heap_address, nro_backup.code_size + nro_backup.rw_size,
  370. nro_backup.bss_heap_address, nro_backup.bss_heap_size));
  371. }
  372. private:
  373. std::array<ProcessContext, MaxSessions> process_contexts;
  374. std::mt19937_64 generate_random;
  375. // Context Helpers.
  376. ProcessContext* GetContextById(size_t context_id) {
  377. if (context_id == InvalidContextId) {
  378. return nullptr;
  379. }
  380. ASSERT(context_id < process_contexts.size());
  381. return std::addressof(process_contexts[context_id]);
  382. }
  383. ProcessContext* GetContextByProcessId(u64 process_id) {
  384. for (size_t i = 0; i < MaxSessions; i++) {
  385. if (process_contexts[i].GetProcessId() == process_id) {
  386. return std::addressof(process_contexts[i]);
  387. }
  388. }
  389. return nullptr;
  390. }
  391. size_t AllocateContext(Kernel::KProcess* process, u64 process_id) {
  392. // Find a free process context.
  393. for (size_t i = 0; i < MaxSessions; i++) {
  394. ProcessContext* context = std::addressof(process_contexts[i]);
  395. if (context->IsFree()) {
  396. context->Initialize(process, process_id);
  397. return i;
  398. }
  399. }
  400. // Failure to find a free context is actually an abort condition.
  401. UNREACHABLE();
  402. }
  403. void FreeContext(size_t context_id) {
  404. if (ProcessContext* context = GetContextById(context_id); context != nullptr) {
  405. context->Finalize();
  406. }
  407. }
  408. };
  409. class RoInterface {
  410. public:
  411. explicit RoInterface(std::shared_ptr<RoContext> ro, NrrKind nrr_kind)
  412. : m_ro(ro), m_context_id(InvalidContextId), m_nrr_kind(nrr_kind) {}
  413. ~RoInterface() {
  414. m_ro->UnregisterProcess(m_context_id);
  415. }
  416. Result MapManualLoadModuleMemory(u64* out_load_address, u64 client_pid, u64 nro_address,
  417. u64 nro_size, u64 bss_address, u64 bss_size) {
  418. R_TRY(m_ro->ValidateProcess(m_context_id, client_pid));
  419. R_RETURN(m_ro->MapManualLoadModuleMemory(out_load_address, m_context_id, nro_address,
  420. nro_size, bss_address, bss_size));
  421. }
  422. Result UnmapManualLoadModuleMemory(u64 client_pid, u64 nro_address) {
  423. R_TRY(m_ro->ValidateProcess(m_context_id, client_pid));
  424. R_RETURN(m_ro->UnmapManualLoadModuleMemory(m_context_id, nro_address));
  425. }
  426. Result RegisterModuleInfo(u64 client_pid, u64 nrr_address, u64 nrr_size) {
  427. R_TRY(m_ro->ValidateProcess(m_context_id, client_pid));
  428. R_RETURN(
  429. m_ro->RegisterModuleInfo(m_context_id, nrr_address, nrr_size, NrrKind::User, true));
  430. }
  431. Result UnregisterModuleInfo(u64 client_pid, u64 nrr_address) {
  432. R_TRY(m_ro->ValidateProcess(m_context_id, client_pid));
  433. R_RETURN(m_ro->UnregisterModuleInfo(m_context_id, nrr_address));
  434. }
  435. Result RegisterProcessHandle(u64 client_pid, Kernel::KProcess* process) {
  436. // Register the process.
  437. R_RETURN(m_ro->RegisterProcess(std::addressof(m_context_id), process, client_pid));
  438. }
  439. Result RegisterProcessModuleInfo(u64 client_pid, u64 nrr_address, u64 nrr_size,
  440. Kernel::KProcess* process) {
  441. // Validate the process.
  442. R_TRY(m_ro->ValidateProcess(m_context_id, client_pid));
  443. // Register the module.
  444. R_RETURN(m_ro->RegisterModuleInfo(m_context_id, nrr_address, nrr_size, m_nrr_kind,
  445. m_nrr_kind == NrrKind::JitPlugin));
  446. }
  447. private:
  448. std::shared_ptr<RoContext> m_ro{};
  449. size_t m_context_id{};
  450. NrrKind m_nrr_kind{};
  451. };
  452. class IRoInterface : public ServiceFramework<IRoInterface> {
  453. public:
  454. explicit IRoInterface(Core::System& system_, const char* name_, std::shared_ptr<RoContext> ro,
  455. NrrKind nrr_kind)
  456. : ServiceFramework{system_, name_}, interface {
  457. ro, nrr_kind
  458. } {
  459. // clang-format off
  460. static const FunctionInfo functions[] = {
  461. {0, &IRoInterface::MapManualLoadModuleMemory, "MapManualLoadModuleMemory"},
  462. {1, &IRoInterface::UnmapManualLoadModuleMemory, "UnmapManualLoadModuleMemory"},
  463. {2, &IRoInterface::RegisterModuleInfo, "RegisterModuleInfo"},
  464. {3, &IRoInterface::UnregisterModuleInfo, "UnregisterModuleInfo"},
  465. {4, &IRoInterface::RegisterProcessHandle, "RegisterProcessHandle"},
  466. {10, &IRoInterface::RegisterProcessModuleInfo, "RegisterProcessModuleInfo"},
  467. };
  468. // clang-format on
  469. RegisterHandlers(functions);
  470. }
  471. private:
  472. void MapManualLoadModuleMemory(HLERequestContext& ctx) {
  473. LOG_DEBUG(Service_LDR, "(called)");
  474. struct InputParameters {
  475. u64 client_pid;
  476. u64 nro_address;
  477. u64 nro_size;
  478. u64 bss_address;
  479. u64 bss_size;
  480. };
  481. IPC::RequestParser rp{ctx};
  482. auto params = rp.PopRaw<InputParameters>();
  483. u64 load_address = 0;
  484. auto result = interface.MapManualLoadModuleMemory(&load_address, ctx.GetPID(),
  485. params.nro_address, params.nro_size,
  486. params.bss_address, params.bss_size);
  487. IPC::ResponseBuilder rb{ctx, 4};
  488. rb.Push(result);
  489. rb.Push(load_address);
  490. }
  491. void UnmapManualLoadModuleMemory(HLERequestContext& ctx) {
  492. LOG_DEBUG(Service_LDR, "(called)");
  493. struct InputParameters {
  494. u64 client_pid;
  495. u64 nro_address;
  496. };
  497. IPC::RequestParser rp{ctx};
  498. auto params = rp.PopRaw<InputParameters>();
  499. auto result = interface.UnmapManualLoadModuleMemory(ctx.GetPID(), params.nro_address);
  500. IPC::ResponseBuilder rb{ctx, 2};
  501. rb.Push(result);
  502. }
  503. void RegisterModuleInfo(HLERequestContext& ctx) {
  504. LOG_DEBUG(Service_LDR, "(called)");
  505. struct InputParameters {
  506. u64 client_pid;
  507. u64 nrr_address;
  508. u64 nrr_size;
  509. };
  510. IPC::RequestParser rp{ctx};
  511. auto params = rp.PopRaw<InputParameters>();
  512. auto result =
  513. interface.RegisterModuleInfo(ctx.GetPID(), params.nrr_address, params.nrr_size);
  514. IPC::ResponseBuilder rb{ctx, 2};
  515. rb.Push(result);
  516. }
  517. void UnregisterModuleInfo(HLERequestContext& ctx) {
  518. LOG_DEBUG(Service_LDR, "(called)");
  519. struct InputParameters {
  520. u64 client_pid;
  521. u64 nrr_address;
  522. };
  523. IPC::RequestParser rp{ctx};
  524. auto params = rp.PopRaw<InputParameters>();
  525. auto result = interface.UnregisterModuleInfo(ctx.GetPID(), params.nrr_address);
  526. IPC::ResponseBuilder rb{ctx, 2};
  527. rb.Push(result);
  528. }
  529. void RegisterProcessHandle(HLERequestContext& ctx) {
  530. LOG_DEBUG(Service_LDR, "(called)");
  531. auto process = ctx.GetObjectFromHandle<Kernel::KProcess>(ctx.GetCopyHandle(0));
  532. auto client_pid = ctx.GetPID();
  533. auto result = interface.RegisterProcessHandle(client_pid, process.GetPointerUnsafe());
  534. IPC::ResponseBuilder rb{ctx, 2};
  535. rb.Push(result);
  536. }
  537. void RegisterProcessModuleInfo(HLERequestContext& ctx) {
  538. LOG_DEBUG(Service_LDR, "(called)");
  539. struct InputParameters {
  540. u64 client_pid;
  541. u64 nrr_address;
  542. u64 nrr_size;
  543. };
  544. IPC::RequestParser rp{ctx};
  545. auto params = rp.PopRaw<InputParameters>();
  546. auto process = ctx.GetObjectFromHandle<Kernel::KProcess>(ctx.GetCopyHandle(0));
  547. auto client_pid = ctx.GetPID();
  548. auto result = interface.RegisterProcessModuleInfo(
  549. client_pid, params.nrr_address, params.nrr_size, process.GetPointerUnsafe());
  550. IPC::ResponseBuilder rb{ctx, 2};
  551. rb.Push(result);
  552. }
  553. RoInterface interface;
  554. };
  555. } // namespace
  556. void LoopProcess(Core::System& system) {
  557. auto server_manager = std::make_unique<ServerManager>(system);
  558. auto ro = std::make_shared<RoContext>();
  559. const auto RoInterfaceFactoryForUser = [&, ro] {
  560. return std::make_shared<IRoInterface>(system, "ldr:ro", ro, NrrKind::User);
  561. };
  562. const auto RoInterfaceFactoryForJitPlugin = [&, ro] {
  563. return std::make_shared<IRoInterface>(system, "ro:1", ro, NrrKind::JitPlugin);
  564. };
  565. server_manager->RegisterNamedService("ldr:ro", std::move(RoInterfaceFactoryForUser));
  566. server_manager->RegisterNamedService("ro:1", std::move(RoInterfaceFactoryForJitPlugin));
  567. ServerManager::RunServer(std::move(server_manager));
  568. }
  569. } // namespace Service::RO