IRoInterface.cs 15 KB

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