ProgramLoader.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using ARMeilleure.Translation.PTC;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Cpu;
  5. using Ryujinx.HLE.HOS.Kernel;
  6. using Ryujinx.HLE.HOS.Kernel.Common;
  7. using Ryujinx.HLE.HOS.Kernel.Memory;
  8. using Ryujinx.HLE.HOS.Kernel.Process;
  9. using Ryujinx.HLE.Loaders.Executables;
  10. using Ryujinx.HLE.Loaders.Npdm;
  11. namespace Ryujinx.HLE.HOS
  12. {
  13. static class ProgramLoader
  14. {
  15. private const bool AslrEnabled = true;
  16. private const int ArgsHeaderSize = 8;
  17. private const int ArgsDataSize = 0x9000;
  18. private const int ArgsTotalSize = ArgsHeaderSize + ArgsDataSize;
  19. public static bool LoadKip(KernelContext context, KipExecutable kip)
  20. {
  21. int endOffset = kip.DataOffset + kip.Data.Length;
  22. if (kip.BssSize != 0)
  23. {
  24. endOffset = kip.BssOffset + kip.BssSize;
  25. }
  26. int codeSize = BitUtils.AlignUp(kip.TextOffset + endOffset, KMemoryManager.PageSize);
  27. int codePagesCount = codeSize / KMemoryManager.PageSize;
  28. ulong codeBaseAddress = kip.Is64BitAddressSpace ? 0x8000000UL : 0x200000UL;
  29. ulong codeAddress = codeBaseAddress + (ulong)kip.TextOffset;
  30. int mmuFlags = 0;
  31. if (AslrEnabled)
  32. {
  33. // TODO: Randomization.
  34. mmuFlags |= 0x20;
  35. }
  36. if (kip.Is64BitAddressSpace)
  37. {
  38. mmuFlags |= (int)AddressSpaceType.Addr39Bits << 1;
  39. }
  40. if (kip.Is64Bit)
  41. {
  42. mmuFlags |= 1;
  43. }
  44. ProcessCreationInfo creationInfo = new ProcessCreationInfo(
  45. kip.Name,
  46. kip.Version,
  47. kip.ProgramId,
  48. codeAddress,
  49. codePagesCount,
  50. mmuFlags,
  51. 0,
  52. 0);
  53. MemoryRegion memoryRegion = kip.UsesSecureMemory
  54. ? MemoryRegion.Service
  55. : MemoryRegion.Application;
  56. KMemoryRegionManager region = context.MemoryRegions[(int)memoryRegion];
  57. KernelResult result = region.AllocatePages((ulong)codePagesCount, false, out KPageList pageList);
  58. if (result != KernelResult.Success)
  59. {
  60. Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
  61. return false;
  62. }
  63. KProcess process = new KProcess(context);
  64. result = process.InitializeKip(
  65. creationInfo,
  66. kip.Capabilities,
  67. pageList,
  68. context.ResourceLimit,
  69. memoryRegion);
  70. if (result != KernelResult.Success)
  71. {
  72. Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
  73. return false;
  74. }
  75. result = LoadIntoMemory(process, kip, codeBaseAddress);
  76. if (result != KernelResult.Success)
  77. {
  78. Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
  79. return false;
  80. }
  81. process.DefaultCpuCore = kip.IdealCoreId;
  82. result = process.Start(kip.Priority, (ulong)kip.StackSize);
  83. if (result != KernelResult.Success)
  84. {
  85. Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
  86. return false;
  87. }
  88. context.Processes.TryAdd(process.Pid, process);
  89. return true;
  90. }
  91. public static bool LoadNsos(
  92. KernelContext context,
  93. Npdm metaData,
  94. byte[] arguments = null,
  95. params IExecutable[] executables)
  96. {
  97. ulong argsStart = 0;
  98. int argsSize = 0;
  99. ulong codeStart = metaData.Is64Bit ? 0x8000000UL : 0x200000UL;
  100. int codeSize = 0;
  101. ulong[] nsoBase = new ulong[executables.Length];
  102. for (int index = 0; index < executables.Length; index++)
  103. {
  104. IExecutable staticObject = executables[index];
  105. int textEnd = staticObject.TextOffset + staticObject.Text.Length;
  106. int roEnd = staticObject.RoOffset + staticObject.Ro.Length;
  107. int dataEnd = staticObject.DataOffset + staticObject.Data.Length + staticObject.BssSize;
  108. int nsoSize = textEnd;
  109. if ((uint)nsoSize < (uint)roEnd)
  110. {
  111. nsoSize = roEnd;
  112. }
  113. if ((uint)nsoSize < (uint)dataEnd)
  114. {
  115. nsoSize = dataEnd;
  116. }
  117. nsoSize = BitUtils.AlignUp(nsoSize, KMemoryManager.PageSize);
  118. nsoBase[index] = codeStart + (ulong)codeSize;
  119. codeSize += nsoSize;
  120. if (arguments != null && argsSize == 0)
  121. {
  122. argsStart = (ulong)codeSize;
  123. argsSize = BitUtils.AlignDown(arguments.Length * 2 + ArgsTotalSize - 1, KMemoryManager.PageSize);
  124. codeSize += argsSize;
  125. }
  126. }
  127. PtcProfiler.StaticCodeStart = codeStart;
  128. PtcProfiler.StaticCodeSize = codeSize;
  129. int codePagesCount = codeSize / KMemoryManager.PageSize;
  130. int personalMmHeapPagesCount = metaData.PersonalMmHeapSize / KMemoryManager.PageSize;
  131. ProcessCreationInfo creationInfo = new ProcessCreationInfo(
  132. metaData.TitleName,
  133. metaData.Version,
  134. metaData.Aci0.TitleId,
  135. codeStart,
  136. codePagesCount,
  137. metaData.MmuFlags,
  138. 0,
  139. personalMmHeapPagesCount);
  140. KernelResult result;
  141. KResourceLimit resourceLimit = new KResourceLimit(context);
  142. long applicationRgSize = (long)context.MemoryRegions[(int)MemoryRegion.Application].Size;
  143. result = resourceLimit.SetLimitValue(LimitableResource.Memory, applicationRgSize);
  144. result |= resourceLimit.SetLimitValue(LimitableResource.Thread, 608);
  145. result |= resourceLimit.SetLimitValue(LimitableResource.Event, 700);
  146. result |= resourceLimit.SetLimitValue(LimitableResource.TransferMemory, 128);
  147. result |= resourceLimit.SetLimitValue(LimitableResource.Session, 894);
  148. if (result != KernelResult.Success)
  149. {
  150. Logger.PrintError(LogClass.Loader, $"Process initialization failed setting resource limit values.");
  151. return false;
  152. }
  153. KProcess process = new KProcess(context);
  154. MemoryRegion memoryRegion = (MemoryRegion)((metaData.Acid.Flags >> 2) & 0xf);
  155. if (memoryRegion > MemoryRegion.NvServices)
  156. {
  157. Logger.PrintError(LogClass.Loader, $"Process initialization failed due to invalid ACID flags.");
  158. return false;
  159. }
  160. result = process.Initialize(
  161. creationInfo,
  162. metaData.Aci0.KernelAccessControl.Capabilities,
  163. resourceLimit,
  164. memoryRegion);
  165. if (result != KernelResult.Success)
  166. {
  167. Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
  168. return false;
  169. }
  170. for (int index = 0; index < executables.Length; index++)
  171. {
  172. Logger.PrintInfo(LogClass.Loader, $"Loading image {index} at 0x{nsoBase[index]:x16}...");
  173. result = LoadIntoMemory(process, executables[index], nsoBase[index]);
  174. if (result != KernelResult.Success)
  175. {
  176. Logger.PrintError(LogClass.Loader, $"Process initialization returned error \"{result}\".");
  177. return false;
  178. }
  179. }
  180. process.DefaultCpuCore = metaData.DefaultCpuId;
  181. result = process.Start(metaData.MainThreadPriority, (ulong)metaData.MainThreadStackSize);
  182. if (result != KernelResult.Success)
  183. {
  184. Logger.PrintError(LogClass.Loader, $"Process start returned error \"{result}\".");
  185. return false;
  186. }
  187. context.Processes.TryAdd(process.Pid, process);
  188. return true;
  189. }
  190. private static KernelResult LoadIntoMemory(KProcess process, IExecutable image, ulong baseAddress)
  191. {
  192. ulong textStart = baseAddress + (ulong)image.TextOffset;
  193. ulong roStart = baseAddress + (ulong)image.RoOffset;
  194. ulong dataStart = baseAddress + (ulong)image.DataOffset;
  195. ulong bssStart = baseAddress + (ulong)image.BssOffset;
  196. ulong end = dataStart + (ulong)image.Data.Length;
  197. if (image.BssSize != 0)
  198. {
  199. end = bssStart + (ulong)image.BssSize;
  200. }
  201. process.CpuMemory.Write(textStart, image.Text);
  202. process.CpuMemory.Write(roStart, image.Ro);
  203. process.CpuMemory.Write(dataStart, image.Data);
  204. MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize);
  205. KernelResult SetProcessMemoryPermission(ulong address, ulong size, MemoryPermission permission)
  206. {
  207. if (size == 0)
  208. {
  209. return KernelResult.Success;
  210. }
  211. size = BitUtils.AlignUp(size, KMemoryManager.PageSize);
  212. return process.MemoryManager.SetProcessMemoryPermission(address, size, permission);
  213. }
  214. KernelResult result = SetProcessMemoryPermission(textStart, (ulong)image.Text.Length, MemoryPermission.ReadAndExecute);
  215. if (result != KernelResult.Success)
  216. {
  217. return result;
  218. }
  219. result = SetProcessMemoryPermission(roStart, (ulong)image.Ro.Length, MemoryPermission.Read);
  220. if (result != KernelResult.Success)
  221. {
  222. return result;
  223. }
  224. return SetProcessMemoryPermission(dataStart, end - dataStart, MemoryPermission.ReadAndWrite);
  225. }
  226. }
  227. }