Process.cs 8.2 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. long HbAbiDataPosition = (Executables[0].ImageEnd + 0xfff) & ~0xfff;
  82. Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle);
  83. MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
  84. MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
  85. }
  86. Scheduler.StartThread(MainThread);
  87. return true;
  88. }
  89. public void StopAllThreads()
  90. {
  91. if (MainThread != null)
  92. {
  93. while (MainThread.Thread.IsAlive)
  94. {
  95. MainThread.Thread.StopExecution();
  96. }
  97. }
  98. foreach (AThread Thread in TlsSlots.Values)
  99. {
  100. while (Thread.IsAlive)
  101. {
  102. Thread.StopExecution();
  103. }
  104. }
  105. }
  106. public int MakeThread(
  107. long EntryPoint,
  108. long StackTop,
  109. long ArgsPtr,
  110. int Priority,
  111. int ProcessorId)
  112. {
  113. ThreadPriority ThreadPrio;
  114. if (Priority < 12)
  115. {
  116. ThreadPrio = ThreadPriority.Highest;
  117. }
  118. else if (Priority < 24)
  119. {
  120. ThreadPrio = ThreadPriority.AboveNormal;
  121. }
  122. else if (Priority < 36)
  123. {
  124. ThreadPrio = ThreadPriority.Normal;
  125. }
  126. else if (Priority < 48)
  127. {
  128. ThreadPrio = ThreadPriority.BelowNormal;
  129. }
  130. else
  131. {
  132. ThreadPrio = ThreadPriority.Lowest;
  133. }
  134. AThread Thread = new AThread(GetTranslator(), Memory, ThreadPrio, EntryPoint);
  135. HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
  136. int Handle = Ns.Os.Handles.GenerateId(ThreadHnd);
  137. int TlsSlot = GetFreeTlsSlot(Thread);
  138. if (TlsSlot == -1 || Handle == -1)
  139. {
  140. return -1;
  141. }
  142. Thread.ThreadState.Break += BreakHandler;
  143. Thread.ThreadState.SvcCall += SvcHandler.SvcCall;
  144. Thread.ThreadState.Undefined += UndefinedHandler;
  145. Thread.ThreadState.ProcessId = ProcessId;
  146. Thread.ThreadState.ThreadId = Ns.Os.IdGen.GenerateId();
  147. Thread.ThreadState.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  148. Thread.ThreadState.X0 = (ulong)ArgsPtr;
  149. Thread.ThreadState.X1 = (ulong)Handle;
  150. Thread.ThreadState.X31 = (ulong)StackTop;
  151. Thread.WorkFinished += ThreadFinished;
  152. ThreadsByTpidr.TryAdd(Thread.ThreadState.Tpidr, ThreadHnd);
  153. return Handle;
  154. }
  155. private void BreakHandler(object sender, AInstExceptionEventArgs e)
  156. {
  157. throw new GuestBrokeExecutionException();
  158. }
  159. private void UndefinedHandler(object sender, AInstUndefinedEventArgs e)
  160. {
  161. throw new UndefinedInstructionException(e.Position, e.RawOpCode);
  162. }
  163. private ATranslator GetTranslator()
  164. {
  165. if (Translator == null)
  166. {
  167. Dictionary<long, string> SymbolTable = new Dictionary<long, string>();
  168. foreach (Executable Exe in Executables)
  169. {
  170. foreach (KeyValuePair<long, string> KV in Exe.SymbolTable)
  171. {
  172. SymbolTable.TryAdd(Exe.ImageBase + KV.Key, KV.Value);
  173. }
  174. }
  175. Translator = new ATranslator(SymbolTable);
  176. Translator.CpuTrace += CpuTraceHandler;
  177. }
  178. return Translator;
  179. }
  180. private void CpuTraceHandler(object sender, ACpuTraceEventArgs e)
  181. {
  182. Logging.Trace($"Executing at 0x{e.Position:x16} {e.SubName}");
  183. }
  184. private int GetFreeTlsSlot(AThread Thread)
  185. {
  186. for (int Index = 1; Index < TotalTlsSlots; Index++)
  187. {
  188. if (TlsSlots.TryAdd(Index, Thread))
  189. {
  190. return Index;
  191. }
  192. }
  193. return -1;
  194. }
  195. private void ThreadFinished(object sender, EventArgs e)
  196. {
  197. if (sender is AThread Thread)
  198. {
  199. TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
  200. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  201. }
  202. }
  203. private int GetTlsSlot(long Position)
  204. {
  205. return (int)((Position - TlsPageAddr) / TlsSize);
  206. }
  207. public HThread GetThread(long Tpidr)
  208. {
  209. if (!ThreadsByTpidr.TryGetValue(Tpidr, out HThread Thread))
  210. {
  211. Logging.Error($"Thread with TPIDR 0x{Tpidr:x16} not found!");
  212. }
  213. return Thread;
  214. }
  215. public void Dispose()
  216. {
  217. Dispose(true);
  218. }
  219. protected virtual void Dispose(bool Disposing)
  220. {
  221. if (Disposing)
  222. {
  223. Scheduler.Dispose();
  224. }
  225. }
  226. }
  227. }