Process.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using ChocolArm64;
  2. using ChocolArm64.Events;
  3. using ChocolArm64.Memory;
  4. using Ryujinx.Core.Loaders;
  5. using Ryujinx.Core.Loaders.Executables;
  6. using Ryujinx.Core.OsHle.Exceptions;
  7. using Ryujinx.Core.OsHle.Handles;
  8. using Ryujinx.Core.OsHle.Svc;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Threading;
  13. namespace Ryujinx.Core.OsHle
  14. {
  15. public class Process : IDisposable
  16. {
  17. private const int MaxStackSize = 8 * 1024 * 1024;
  18. private const int TlsSize = 0x200;
  19. private const int TotalTlsSlots = 32;
  20. private const int TlsTotalSize = TotalTlsSlots * TlsSize;
  21. private const long TlsPageAddr = (AMemoryMgr.AddrSize - TlsTotalSize) & ~AMemoryMgr.PageMask;
  22. private Switch Ns;
  23. private ATranslator Translator;
  24. public int ProcessId { get; private set; }
  25. public AMemory Memory { get; private set; }
  26. public KProcessScheduler Scheduler { get; private set; }
  27. private SvcHandler SvcHandler;
  28. private ConcurrentDictionary<int, AThread> TlsSlots;
  29. private ConcurrentDictionary<long, HThread> ThreadsByTpidr;
  30. private List<Executable> Executables;
  31. private HThread MainThread;
  32. private long ImageBase;
  33. public Process(Switch Ns, AMemoryAlloc Allocator, int ProcessId)
  34. {
  35. this.Ns = Ns;
  36. this.ProcessId = ProcessId;
  37. Memory = new AMemory(Ns.Ram, Allocator);
  38. Scheduler = new KProcessScheduler();
  39. SvcHandler = new SvcHandler(Ns, this);
  40. TlsSlots = new ConcurrentDictionary<int, AThread>();
  41. ThreadsByTpidr = new ConcurrentDictionary<long, HThread>();
  42. Executables = new List<Executable>();
  43. ImageBase = 0x8000000;
  44. Memory.Manager.MapPhys(
  45. TlsPageAddr,
  46. TlsTotalSize,
  47. (int)MemoryType.ThreadLocal,
  48. AMemoryPerm.RW);
  49. }
  50. public void LoadProgram(IExecutable Program)
  51. {
  52. Logging.Info($"Image base at 0x{ImageBase:x16}.");
  53. Executable Executable = new Executable(Program, Memory, ImageBase);
  54. Executables.Add(Executable);
  55. ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
  56. }
  57. public void SetEmptyArgs()
  58. {
  59. ImageBase += AMemoryMgr.PageSize;
  60. }
  61. public void InitializeHeap()
  62. {
  63. Memory.Manager.SetHeapAddr(MemoryRegions.HeapRegionAddress);
  64. }
  65. public bool Run(bool UseHbAbi = false)
  66. {
  67. if (Executables.Count == 0)
  68. {
  69. return false;
  70. }
  71. long StackBot = TlsPageAddr - MaxStackSize;
  72. Memory.Manager.MapPhys(StackBot, MaxStackSize, (int)MemoryType.Normal, AMemoryPerm.RW);
  73. int Handle = MakeThread(Executables[0].ImageBase, TlsPageAddr, 0, 0, 0);
  74. if (Handle == -1)
  75. {
  76. return false;
  77. }
  78. MainThread = Ns.Os.Handles.GetData<HThread>(Handle);
  79. if (UseHbAbi)
  80. {
  81. Homebrew Homebrew_ABI = new Homebrew(Memory, Executables[0].ImageEnd, (long)Handle);
  82. MainThread.Thread.ThreadState.X0 = (ulong)Executables[0].ImageEnd;
  83. MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
  84. }
  85. Scheduler.StartThread(MainThread);
  86. return true;
  87. }
  88. public void StopAllThreads()
  89. {
  90. if (MainThread != null)
  91. {
  92. while (MainThread.Thread.IsAlive)
  93. {
  94. MainThread.Thread.StopExecution();
  95. }
  96. }
  97. foreach (AThread Thread in TlsSlots.Values)
  98. {
  99. while (Thread.IsAlive)
  100. {
  101. Thread.StopExecution();
  102. }
  103. }
  104. }
  105. public int MakeThread(
  106. long EntryPoint,
  107. long StackTop,
  108. long ArgsPtr,
  109. int Priority,
  110. int ProcessorId)
  111. {
  112. ThreadPriority ThreadPrio;
  113. if (Priority < 12)
  114. {
  115. ThreadPrio = ThreadPriority.Highest;
  116. }
  117. else if (Priority < 24)
  118. {
  119. ThreadPrio = ThreadPriority.AboveNormal;
  120. }
  121. else if (Priority < 36)
  122. {
  123. ThreadPrio = ThreadPriority.Normal;
  124. }
  125. else if (Priority < 48)
  126. {
  127. ThreadPrio = ThreadPriority.BelowNormal;
  128. }
  129. else
  130. {
  131. ThreadPrio = ThreadPriority.Lowest;
  132. }
  133. AThread Thread = new AThread(GetTranslator(), Memory, ThreadPrio, EntryPoint);
  134. HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
  135. int Handle = Ns.Os.Handles.GenerateId(ThreadHnd);
  136. int TlsSlot = GetFreeTlsSlot(Thread);
  137. if (TlsSlot == -1 || Handle == -1)
  138. {
  139. return -1;
  140. }
  141. Thread.ThreadState.Break += BreakHandler;
  142. Thread.ThreadState.SvcCall += SvcHandler.SvcCall;
  143. Thread.ThreadState.Undefined += UndefinedHandler;
  144. Thread.ThreadState.ProcessId = ProcessId;
  145. Thread.ThreadState.ThreadId = Ns.Os.IdGen.GenerateId();
  146. Thread.ThreadState.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  147. Thread.ThreadState.X0 = (ulong)ArgsPtr;
  148. Thread.ThreadState.X1 = (ulong)Handle;
  149. Thread.ThreadState.X31 = (ulong)StackTop;
  150. Thread.WorkFinished += ThreadFinished;
  151. ThreadsByTpidr.TryAdd(Thread.ThreadState.Tpidr, ThreadHnd);
  152. return Handle;
  153. }
  154. private void BreakHandler(object sender, AInstExceptionEventArgs e)
  155. {
  156. throw new GuestBrokeExecutionException();
  157. }
  158. private void UndefinedHandler(object sender, AInstUndefinedEventArgs e)
  159. {
  160. throw new UndefinedInstructionException(e.Position, e.RawOpCode);
  161. }
  162. private ATranslator GetTranslator()
  163. {
  164. if (Translator == null)
  165. {
  166. Dictionary<long, string> SymbolTable = new Dictionary<long, string>();
  167. foreach (Executable Exe in Executables)
  168. {
  169. foreach (KeyValuePair<long, string> KV in Exe.SymbolTable)
  170. {
  171. SymbolTable.Add(Exe.ImageBase + KV.Key, KV.Value);
  172. }
  173. }
  174. Translator = new ATranslator(SymbolTable);
  175. Translator.CpuTrace += CpuTraceHandler;
  176. }
  177. return Translator;
  178. }
  179. private void CpuTraceHandler(object sender, ACpuTraceEventArgs e)
  180. {
  181. Logging.Info($"Executing at 0x{e.Position:x16} {e.SubName}");
  182. }
  183. private int GetFreeTlsSlot(AThread Thread)
  184. {
  185. for (int Index = 1; Index < TotalTlsSlots; Index++)
  186. {
  187. if (TlsSlots.TryAdd(Index, Thread))
  188. {
  189. return Index;
  190. }
  191. }
  192. return -1;
  193. }
  194. private void ThreadFinished(object sender, EventArgs e)
  195. {
  196. if (sender is AThread Thread)
  197. {
  198. TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
  199. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  200. }
  201. }
  202. private int GetTlsSlot(long Position)
  203. {
  204. return (int)((Position - TlsPageAddr) / TlsSize);
  205. }
  206. public HThread GetThread(long Tpidr)
  207. {
  208. if (!ThreadsByTpidr.TryGetValue(Tpidr, out HThread Thread))
  209. {
  210. Logging.Error($"Thread with TPIDR 0x{Tpidr:x16} not found!");
  211. }
  212. return Thread;
  213. }
  214. public void Dispose()
  215. {
  216. Dispose(true);
  217. }
  218. protected virtual void Dispose(bool Disposing)
  219. {
  220. if (Disposing)
  221. {
  222. Scheduler.Dispose();
  223. }
  224. }
  225. }
  226. }