IRoInterface.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using ARMeilleure.Memory;
  2. using Ryujinx.Common;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Memory;
  6. using Ryujinx.HLE.HOS.Kernel.Process;
  7. using Ryujinx.HLE.Loaders.Executables;
  8. using Ryujinx.HLE.Utilities;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Security.Cryptography;
  14. namespace Ryujinx.HLE.HOS.Services.Ldr
  15. {
  16. [StructLayout(LayoutKind.Explicit, Size = 0x350)]
  17. unsafe struct NrrHeader
  18. {
  19. [FieldOffset(0)]
  20. public uint Magic;
  21. [FieldOffset(0x10)]
  22. public ulong TitleIdMask;
  23. [FieldOffset(0x18)]
  24. public ulong TitleIdPattern;
  25. [FieldOffset(0x30)]
  26. public fixed byte Modulus[0x100];
  27. [FieldOffset(0x130)]
  28. public fixed byte FixedKeySignature[0x100];
  29. [FieldOffset(0x230)]
  30. public fixed byte NrrSignature[0x100];
  31. [FieldOffset(0x330)]
  32. public ulong TitleIdMin;
  33. [FieldOffset(0x338)]
  34. public uint NrrSize;
  35. [FieldOffset(0x340)]
  36. public uint HashOffset;
  37. [FieldOffset(0x344)]
  38. public uint HashCount;
  39. }
  40. class NrrInfo
  41. {
  42. public NrrHeader Header { get; private set; }
  43. public List<byte[]> Hashes { get; private set; }
  44. public long NrrAddress { get; private set; }
  45. public NrrInfo(long nrrAddress, NrrHeader header, List<byte[]> hashes)
  46. {
  47. NrrAddress = nrrAddress;
  48. Header = header;
  49. Hashes = hashes;
  50. }
  51. }
  52. class NroInfo
  53. {
  54. public NxRelocatableObject Executable { get; private set; }
  55. public byte[] Hash { get; private set; }
  56. public ulong NroAddress { get; private set; }
  57. public ulong NroSize { get; private set; }
  58. public ulong BssAddress { get; private set; }
  59. public ulong BssSize { get; private set; }
  60. public ulong TotalSize { get; private set; }
  61. public ulong NroMappedAddress { get; set; }
  62. public NroInfo(
  63. NxRelocatableObject executable,
  64. byte[] hash,
  65. ulong nroAddress,
  66. ulong nroSize,
  67. ulong bssAddress,
  68. ulong bssSize,
  69. ulong totalSize)
  70. {
  71. Executable = executable;
  72. Hash = hash;
  73. NroAddress = nroAddress;
  74. NroSize = nroSize;
  75. BssAddress = bssAddress;
  76. BssSize = bssSize;
  77. TotalSize = totalSize;
  78. }
  79. }
  80. [Service("ldr:ro")]
  81. class IRoInterface : IpcService
  82. {
  83. private const int MaxNrr = 0x40;
  84. private const int MaxNro = 0x40;
  85. private const uint NrrMagic = 0x3052524E;
  86. private const uint NroMagic = 0x304F524E;
  87. private List<NrrInfo> _nrrInfos;
  88. private List<NroInfo> _nroInfos;
  89. private bool _isInitialized;
  90. public IRoInterface(ServiceCtx context)
  91. {
  92. _nrrInfos = new List<NrrInfo>(MaxNrr);
  93. _nroInfos = new List<NroInfo>(MaxNro);
  94. }
  95. private ResultCode ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, long nrrAddress, long nrrSize)
  96. {
  97. nrrInfo = null;
  98. if (nrrSize == 0 || nrrAddress + nrrSize <= nrrAddress || (nrrSize & 0xFFF) != 0)
  99. {
  100. return ResultCode.BadSize;
  101. }
  102. else if ((nrrAddress & 0xFFF) != 0)
  103. {
  104. return ResultCode.UnalignedAddress;
  105. }
  106. StructReader reader = new StructReader(context.Memory, nrrAddress);
  107. NrrHeader header = reader.Read<NrrHeader>();
  108. if (header.Magic != NrrMagic)
  109. {
  110. return ResultCode.InvalidNrr;
  111. }
  112. else if (header.NrrSize != nrrSize)
  113. {
  114. return ResultCode.BadSize;
  115. }
  116. List<byte[]> hashes = new List<byte[]>();
  117. for (int i = 0; i < header.HashCount; i++)
  118. {
  119. hashes.Add(context.Memory.ReadBytes(nrrAddress + header.HashOffset + (i * 0x20), 0x20));
  120. }
  121. nrrInfo = new NrrInfo(nrrAddress, header, hashes);
  122. return ResultCode.Success;
  123. }
  124. public bool IsNroHashPresent(byte[] nroHash)
  125. {
  126. foreach (NrrInfo info in _nrrInfos)
  127. {
  128. foreach (byte[] hash in info.Hashes)
  129. {
  130. if (hash.SequenceEqual(nroHash))
  131. {
  132. return true;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. public bool IsNroLoaded(byte[] nroHash)
  139. {
  140. foreach (NroInfo info in _nroInfos)
  141. {
  142. if (info.Hash.SequenceEqual(nroHash))
  143. {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. public ResultCode ParseNro(out NroInfo res, ServiceCtx context, ulong nroAddress, ulong nroSize, ulong bssAddress, ulong bssSize)
  150. {
  151. res = null;
  152. if (_nroInfos.Count >= MaxNro)
  153. {
  154. return ResultCode.MaxNro;
  155. }
  156. else if (nroSize == 0 || nroAddress + nroSize <= nroAddress || (nroSize & 0xFFF) != 0)
  157. {
  158. return ResultCode.BadSize;
  159. }
  160. else if (bssSize != 0 && bssAddress + bssSize <= bssAddress)
  161. {
  162. return ResultCode.BadSize;
  163. }
  164. else if ((nroAddress & 0xFFF) != 0)
  165. {
  166. return ResultCode.UnalignedAddress;
  167. }
  168. uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10);
  169. uint nroFileSize = context.Memory.ReadUInt32((long)nroAddress + 0x18);
  170. if (magic != NroMagic || nroSize != nroFileSize)
  171. {
  172. return ResultCode.InvalidNro;
  173. }
  174. byte[] nroData = context.Memory.ReadBytes((long)nroAddress, (long)nroSize);
  175. byte[] nroHash = null;
  176. MemoryStream stream = new MemoryStream(nroData);
  177. using (SHA256 hasher = SHA256.Create())
  178. {
  179. nroHash = hasher.ComputeHash(stream);
  180. }
  181. if (!IsNroHashPresent(nroHash))
  182. {
  183. return ResultCode.NroHashNotPresent;
  184. }
  185. if (IsNroLoaded(nroHash))
  186. {
  187. return ResultCode.NroAlreadyLoaded;
  188. }
  189. stream.Position = 0;
  190. NxRelocatableObject executable = new NxRelocatableObject(stream, nroAddress, bssAddress);
  191. // check if everything is page align.
  192. if ((executable.Text.Length & 0xFFF) != 0 || (executable.Ro.Length & 0xFFF) != 0 ||
  193. (executable.Data.Length & 0xFFF) != 0 || (executable.BssSize & 0xFFF) != 0)
  194. {
  195. return ResultCode.InvalidNro;
  196. }
  197. // check if everything is contiguous.
  198. if (executable.RoOffset != executable.TextOffset + executable.Text.Length ||
  199. executable.DataOffset != executable.RoOffset + executable.Ro.Length ||
  200. nroFileSize != executable.DataOffset + executable.Data.Length)
  201. {
  202. return ResultCode.InvalidNro;
  203. }
  204. // finally check the bss size match.
  205. if ((ulong)executable.BssSize != bssSize)
  206. {
  207. return ResultCode.InvalidNro;
  208. }
  209. int totalSize = executable.Text.Length + executable.Ro.Length + executable.Data.Length + executable.BssSize;
  210. res = new NroInfo(
  211. executable,
  212. nroHash,
  213. nroAddress,
  214. nroSize,
  215. bssAddress,
  216. bssSize,
  217. (ulong)totalSize);
  218. return ResultCode.Success;
  219. }
  220. private ResultCode MapNro(ServiceCtx context, NroInfo info, out ulong nroMappedAddress)
  221. {
  222. nroMappedAddress = 0;
  223. KMemoryManager memMgr = context.Process.MemoryManager;
  224. ulong targetAddress = memMgr.GetAddrSpaceBaseAddr();
  225. while (true)
  226. {
  227. if (targetAddress + info.TotalSize >= memMgr.AddrSpaceEnd)
  228. {
  229. return ResultCode.InvalidMemoryState;
  230. }
  231. KMemoryInfo memInfo = memMgr.QueryMemory(targetAddress);
  232. if (memInfo.State == MemoryState.Unmapped && memInfo.Size >= info.TotalSize)
  233. {
  234. if (!memMgr.InsideHeapRegion (targetAddress, info.TotalSize) &&
  235. !memMgr.InsideAliasRegion(targetAddress, info.TotalSize))
  236. {
  237. break;
  238. }
  239. }
  240. targetAddress += memInfo.Size;
  241. }
  242. KernelResult result = memMgr.MapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
  243. if (result != KernelResult.Success)
  244. {
  245. return ResultCode.InvalidMemoryState;
  246. }
  247. ulong bssTargetAddress = targetAddress + info.NroSize;
  248. if (info.BssSize != 0)
  249. {
  250. result = memMgr.MapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
  251. if (result != KernelResult.Success)
  252. {
  253. memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
  254. return ResultCode.InvalidMemoryState;
  255. }
  256. }
  257. result = LoadNroIntoMemory(context.Process, info.Executable, targetAddress);
  258. if (result != KernelResult.Success)
  259. {
  260. memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
  261. if (info.BssSize != 0)
  262. {
  263. memMgr.UnmapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
  264. }
  265. return ResultCode.Success;
  266. }
  267. info.NroMappedAddress = targetAddress;
  268. nroMappedAddress = targetAddress;
  269. return ResultCode.Success;
  270. }
  271. private KernelResult LoadNroIntoMemory(KProcess process, IExecutable relocatableObject, ulong baseAddress)
  272. {
  273. ulong textStart = baseAddress + (ulong)relocatableObject.TextOffset;
  274. ulong roStart = baseAddress + (ulong)relocatableObject.RoOffset;
  275. ulong dataStart = baseAddress + (ulong)relocatableObject.DataOffset;
  276. ulong bssStart = dataStart + (ulong)relocatableObject.Data.Length;
  277. ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize);
  278. process.CpuMemory.WriteBytes((long)textStart, relocatableObject.Text);
  279. process.CpuMemory.WriteBytes((long)roStart, relocatableObject.Ro);
  280. process.CpuMemory.WriteBytes((long)dataStart, relocatableObject.Data);
  281. MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart));
  282. KernelResult result;
  283. result = process.MemoryManager.SetProcessMemoryPermission(textStart, roStart - textStart, MemoryPermission.ReadAndExecute);
  284. if (result != KernelResult.Success)
  285. {
  286. return result;
  287. }
  288. result = process.MemoryManager.SetProcessMemoryPermission(roStart, dataStart - roStart, MemoryPermission.Read);
  289. if (result != KernelResult.Success)
  290. {
  291. return result;
  292. }
  293. return process.MemoryManager.SetProcessMemoryPermission(dataStart, bssEnd - dataStart, MemoryPermission.ReadAndWrite);
  294. }
  295. private ResultCode RemoveNrrInfo(long nrrAddress)
  296. {
  297. foreach (NrrInfo info in _nrrInfos)
  298. {
  299. if (info.NrrAddress == nrrAddress)
  300. {
  301. _nrrInfos.Remove(info);
  302. return ResultCode.Success;
  303. }
  304. }
  305. return ResultCode.BadNrrAddress;
  306. }
  307. private ResultCode RemoveNroInfo(ServiceCtx context, ulong nroMappedAddress)
  308. {
  309. foreach (NroInfo info in _nroInfos)
  310. {
  311. if (info.NroMappedAddress == nroMappedAddress)
  312. {
  313. _nroInfos.Remove(info);
  314. ulong textSize = (ulong)info.Executable.Text.Length;
  315. ulong roSize = (ulong)info.Executable.Ro.Length;
  316. ulong dataSize = (ulong)info.Executable.Data.Length;
  317. ulong bssSize = (ulong)info.Executable.BssSize;
  318. KernelResult result = KernelResult.Success;
  319. if (info.Executable.BssSize != 0)
  320. {
  321. result = context.Process.MemoryManager.UnmapProcessCodeMemory(
  322. info.NroMappedAddress + textSize + roSize + dataSize,
  323. info.Executable.BssAddress,
  324. bssSize);
  325. }
  326. if (result == KernelResult.Success)
  327. {
  328. result = context.Process.MemoryManager.UnmapProcessCodeMemory(
  329. info.NroMappedAddress + textSize + roSize,
  330. info.Executable.SourceAddress + textSize + roSize,
  331. dataSize);
  332. if (result == KernelResult.Success)
  333. {
  334. result = context.Process.MemoryManager.UnmapProcessCodeMemory(
  335. info.NroMappedAddress,
  336. info.Executable.SourceAddress,
  337. textSize + roSize);
  338. }
  339. }
  340. return (ResultCode)result;
  341. }
  342. }
  343. return ResultCode.BadNroAddress;
  344. }
  345. [Command(0)]
  346. // LoadNro(u64, u64, u64, u64, u64, pid) -> u64
  347. public ResultCode LoadNro(ServiceCtx context)
  348. {
  349. ResultCode result = ResultCode.BadInitialization;
  350. // Zero
  351. context.RequestData.ReadUInt64();
  352. ulong nroHeapAddress = context.RequestData.ReadUInt64();
  353. ulong nroSize = context.RequestData.ReadUInt64();
  354. ulong bssHeapAddress = context.RequestData.ReadUInt64();
  355. ulong bssSize = context.RequestData.ReadUInt64();
  356. ulong nroMappedAddress = 0;
  357. if (_isInitialized)
  358. {
  359. NroInfo info;
  360. result = ParseNro(out info, context, nroHeapAddress, nroSize, bssHeapAddress, bssSize);
  361. if (result == 0)
  362. {
  363. result = MapNro(context, info, out nroMappedAddress);
  364. if (result == 0)
  365. {
  366. _nroInfos.Add(info);
  367. }
  368. }
  369. }
  370. context.ResponseData.Write(nroMappedAddress);
  371. return result;
  372. }
  373. [Command(1)]
  374. // UnloadNro(u64, u64, pid)
  375. public ResultCode UnloadNro(ServiceCtx context)
  376. {
  377. ResultCode result = ResultCode.BadInitialization;
  378. // Zero
  379. context.RequestData.ReadUInt64();
  380. ulong nroMappedAddress = context.RequestData.ReadUInt64();
  381. if (_isInitialized)
  382. {
  383. if ((nroMappedAddress & 0xFFF) != 0)
  384. {
  385. return ResultCode.UnalignedAddress;
  386. }
  387. result = RemoveNroInfo(context, nroMappedAddress);
  388. }
  389. return result;
  390. }
  391. [Command(2)]
  392. // LoadNrr(u64, u64, u64, pid)
  393. public ResultCode LoadNrr(ServiceCtx context)
  394. {
  395. ResultCode result = ResultCode.BadInitialization;
  396. // Zero
  397. context.RequestData.ReadUInt64();
  398. long nrrAddress = context.RequestData.ReadInt64();
  399. long nrrSize = context.RequestData.ReadInt64();
  400. if (_isInitialized)
  401. {
  402. NrrInfo info;
  403. result = ParseNrr(out info, context, nrrAddress, nrrSize);
  404. if (result == 0)
  405. {
  406. if (_nrrInfos.Count >= MaxNrr)
  407. {
  408. result = ResultCode.MaxNrr;
  409. }
  410. else
  411. {
  412. _nrrInfos.Add(info);
  413. }
  414. }
  415. }
  416. return result;
  417. }
  418. [Command(3)]
  419. // UnloadNrr(u64, u64, pid)
  420. public ResultCode UnloadNrr(ServiceCtx context)
  421. {
  422. ResultCode result = ResultCode.BadInitialization;
  423. // Zero
  424. context.RequestData.ReadUInt64();
  425. long nrrHeapAddress = context.RequestData.ReadInt64();
  426. if (_isInitialized)
  427. {
  428. if ((nrrHeapAddress & 0xFFF) != 0)
  429. {
  430. return ResultCode.UnalignedAddress;
  431. }
  432. result = RemoveNrrInfo(nrrHeapAddress);
  433. }
  434. return result;
  435. }
  436. [Command(4)]
  437. // Initialize(u64, pid, KObject)
  438. public ResultCode Initialize(ServiceCtx context)
  439. {
  440. // TODO: we actually ignore the pid and process handle receive, we will need to use them when we will have multi process support.
  441. _isInitialized = true;
  442. return ResultCode.Success;
  443. }
  444. }
  445. }