Process.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 TlsSize = 0x200;
  18. private const int TotalTlsSlots = 32;
  19. private Switch Ns;
  20. public int ProcessId { get; private set; }
  21. private ATranslator Translator;
  22. public AMemory Memory { get; private set; }
  23. public KProcessScheduler Scheduler { get; private set; }
  24. private SvcHandler SvcHandler;
  25. private ConcurrentDictionary<int, AThread> TlsSlots;
  26. private ConcurrentDictionary<long, HThread> ThreadsByTpidr;
  27. private List<Executable> Executables;
  28. private HThread MainThread;
  29. private long ImageBase;
  30. public Process(Switch Ns, int ProcessId)
  31. {
  32. this.Ns = Ns;
  33. this.ProcessId = ProcessId;
  34. Memory = Ns.Memory;
  35. Scheduler = new KProcessScheduler();
  36. SvcHandler = new SvcHandler(Ns, this);
  37. TlsSlots = new ConcurrentDictionary<int, AThread>();
  38. ThreadsByTpidr = new ConcurrentDictionary<long, HThread>();
  39. Executables = new List<Executable>();
  40. ImageBase = MemoryRegions.AddrSpaceStart;
  41. MapRWMemRegion(
  42. MemoryRegions.TlsPagesAddress,
  43. MemoryRegions.TlsPagesSize,
  44. MemoryType.ThreadLocal);
  45. }
  46. public void LoadProgram(IExecutable Program)
  47. {
  48. Logging.Info($"Image base at 0x{ImageBase:x16}.");
  49. Executable Executable = new Executable(Program, Memory, ImageBase);
  50. Executables.Add(Executable);
  51. ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
  52. }
  53. public void SetEmptyArgs()
  54. {
  55. ImageBase += AMemoryMgr.PageSize;
  56. }
  57. public bool Run(bool UseHbAbi = false)
  58. {
  59. if (Executables.Count == 0)
  60. {
  61. return false;
  62. }
  63. MapRWMemRegion(
  64. MemoryRegions.MainStackAddress,
  65. MemoryRegions.MainStackSize,
  66. MemoryType.Normal);
  67. long StackTop = MemoryRegions.MainStackAddress + MemoryRegions.MainStackSize;
  68. int Handle = MakeThread(Executables[0].ImageBase, StackTop, 0, 0, 0);
  69. if (Handle == -1)
  70. {
  71. return false;
  72. }
  73. MainThread = Ns.Os.Handles.GetData<HThread>(Handle);
  74. if (UseHbAbi)
  75. {
  76. long HbAbiDataPosition = AMemoryHelper.PageRoundUp(Executables[0].ImageEnd);
  77. Homebrew.WriteHbAbiData(Memory, HbAbiDataPosition, Handle);
  78. MainThread.Thread.ThreadState.X0 = (ulong)HbAbiDataPosition;
  79. MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
  80. }
  81. Scheduler.StartThread(MainThread);
  82. return true;
  83. }
  84. private void MapRWMemRegion(long Position, long Size, MemoryType Type)
  85. {
  86. Memory.Manager.Map(Position, Size, (int)Type, AMemoryPerm.RW);
  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. long Tpidr = MemoryRegions.TlsPagesAddress + TlsSlot * TlsSize;
  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 = Tpidr;
  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. string NsoName = string.Empty;
  183. for (int Index = Executables.Count - 1; Index >= 0; Index--)
  184. {
  185. if (e.Position >= Executables[Index].ImageBase)
  186. {
  187. NsoName = $"{(e.Position - Executables[Index].ImageBase):x16}";
  188. break;
  189. }
  190. }
  191. Logging.Trace($"Executing at 0x{e.Position:x16} {e.SubName} {NsoName}");
  192. }
  193. public void EnableCpuTracing()
  194. {
  195. Translator.EnableCpuTrace = true;
  196. }
  197. public void DisableCpuTracing()
  198. {
  199. Translator.EnableCpuTrace = false;
  200. }
  201. private int GetFreeTlsSlot(AThread Thread)
  202. {
  203. for (int Index = 1; Index < TotalTlsSlots; Index++)
  204. {
  205. if (TlsSlots.TryAdd(Index, Thread))
  206. {
  207. return Index;
  208. }
  209. }
  210. return -1;
  211. }
  212. private void ThreadFinished(object sender, EventArgs e)
  213. {
  214. if (sender is AThread Thread)
  215. {
  216. TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
  217. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  218. }
  219. }
  220. private int GetTlsSlot(long Position)
  221. {
  222. return (int)((Position - MemoryRegions.TlsPagesAddress) / TlsSize);
  223. }
  224. public HThread GetThread(long Tpidr)
  225. {
  226. if (!ThreadsByTpidr.TryGetValue(Tpidr, out HThread Thread))
  227. {
  228. Logging.Error($"Thread with TPIDR 0x{Tpidr:x16} not found!");
  229. }
  230. return Thread;
  231. }
  232. public void Dispose()
  233. {
  234. Dispose(true);
  235. }
  236. protected virtual void Dispose(bool Disposing)
  237. {
  238. if (Disposing)
  239. {
  240. Scheduler.Dispose();
  241. }
  242. }
  243. }
  244. }