Process.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using ChocolArm64;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  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. public int ProcessId { get; private set; }
  24. public AMemory Memory { get; private set; }
  25. public KProcessScheduler Scheduler { get; private set; }
  26. private SvcHandler SvcHandler;
  27. private ConcurrentDictionary<int, AThread> TlsSlots;
  28. private ConcurrentDictionary<long, HThread> ThreadsByTpidr;
  29. private List<Executable> Executables;
  30. private HThread MainThread;
  31. private long ImageBase;
  32. public Process(Switch Ns, AMemoryAlloc Allocator, int ProcessId)
  33. {
  34. this.Ns = Ns;
  35. this.ProcessId = ProcessId;
  36. Memory = new AMemory(Ns.Ram, Allocator);
  37. Scheduler = new KProcessScheduler();
  38. SvcHandler = new SvcHandler(Ns, this);
  39. TlsSlots = new ConcurrentDictionary<int, AThread>();
  40. ThreadsByTpidr = new ConcurrentDictionary<long, HThread>();
  41. Executables = new List<Executable>();
  42. ImageBase = 0x8000000;
  43. Memory.Manager.MapPhys(
  44. TlsPageAddr,
  45. TlsTotalSize,
  46. (int)MemoryType.ThreadLocal,
  47. AMemoryPerm.RW);
  48. }
  49. public void LoadProgram(IExecutable Program)
  50. {
  51. Logging.Info($"Image base at 0x{ImageBase:x16}.");
  52. Executable Executable = new Executable(Program, Memory, ImageBase);
  53. Executables.Add(Executable);
  54. ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
  55. }
  56. public void SetEmptyArgs()
  57. {
  58. ImageBase += AMemoryMgr.PageSize;
  59. }
  60. public void InitializeHeap()
  61. {
  62. Memory.Manager.SetHeapAddr((ImageBase + 0x3fffffff) & ~0x3fffffff);
  63. }
  64. public bool Run()
  65. {
  66. if (Executables.Count == 0)
  67. {
  68. return false;
  69. }
  70. long StackBot = TlsPageAddr - MaxStackSize;
  71. Memory.Manager.MapPhys(StackBot, MaxStackSize, (int)MemoryType.Normal, AMemoryPerm.RW);
  72. int Handle = MakeThread(Executables[0].ImageBase, TlsPageAddr, 0, 0, 0);
  73. if (Handle == -1)
  74. {
  75. return false;
  76. }
  77. MainThread = Ns.Os.Handles.GetData<HThread>(Handle);
  78. Scheduler.StartThread(MainThread);
  79. return true;
  80. }
  81. public void StopAllThreads()
  82. {
  83. if (MainThread != null)
  84. {
  85. while (MainThread.Thread.IsAlive)
  86. {
  87. MainThread.Thread.StopExecution();
  88. }
  89. }
  90. foreach (AThread Thread in TlsSlots.Values)
  91. {
  92. while (Thread.IsAlive)
  93. {
  94. Thread.StopExecution();
  95. }
  96. }
  97. }
  98. public int MakeThread(
  99. long EntryPoint,
  100. long StackTop,
  101. long ArgsPtr,
  102. int Priority,
  103. int ProcessorId)
  104. {
  105. ThreadPriority ThreadPrio;
  106. if (Priority < 12)
  107. {
  108. ThreadPrio = ThreadPriority.Highest;
  109. }
  110. else if (Priority < 24)
  111. {
  112. ThreadPrio = ThreadPriority.AboveNormal;
  113. }
  114. else if (Priority < 36)
  115. {
  116. ThreadPrio = ThreadPriority.Normal;
  117. }
  118. else if (Priority < 48)
  119. {
  120. ThreadPrio = ThreadPriority.BelowNormal;
  121. }
  122. else
  123. {
  124. ThreadPrio = ThreadPriority.Lowest;
  125. }
  126. AThread Thread = new AThread(Memory, ThreadPrio, EntryPoint);
  127. HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
  128. int Handle = Ns.Os.Handles.GenerateId(ThreadHnd);
  129. int TlsSlot = GetFreeTlsSlot(Thread);
  130. if (TlsSlot == -1 || Handle == -1)
  131. {
  132. return -1;
  133. }
  134. Thread.ThreadState.Break += BreakHandler;
  135. Thread.ThreadState.SvcCall += SvcHandler.SvcCall;
  136. Thread.ThreadState.Undefined += UndefinedHandler;
  137. Thread.ThreadState.ProcessId = ProcessId;
  138. Thread.ThreadState.ThreadId = Ns.Os.IdGen.GenerateId();
  139. Thread.ThreadState.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  140. Thread.ThreadState.X0 = (ulong)ArgsPtr;
  141. Thread.ThreadState.X1 = (ulong)Handle;
  142. Thread.ThreadState.X31 = (ulong)StackTop;
  143. if (Executables[0].Extension == Extensions.NRO)
  144. {
  145. Homebrew Homebrew_ABI = new Homebrew(Memory, Executables[0].ImageEnd, (long)Handle);
  146. Thread.ThreadState.X0 = (ulong)Executables[0].ImageEnd;
  147. Thread.ThreadState.X1 = 0xFFFFFFFFFFFFFFFF;
  148. }
  149. Thread.WorkFinished += ThreadFinished;
  150. ThreadsByTpidr.TryAdd(Thread.ThreadState.Tpidr, ThreadHnd);
  151. return Handle;
  152. }
  153. private void BreakHandler(object sender, AInstExceptEventArgs e)
  154. {
  155. throw new GuestBrokeExecutionException();
  156. }
  157. private void UndefinedHandler(object sender, AInstUndEventArgs e)
  158. {
  159. throw new UndefinedInstructionException(e.Position, e.RawOpCode);
  160. }
  161. private int GetFreeTlsSlot(AThread Thread)
  162. {
  163. for (int Index = 1; Index < TotalTlsSlots; Index++)
  164. {
  165. if (TlsSlots.TryAdd(Index, Thread))
  166. {
  167. return Index;
  168. }
  169. }
  170. return -1;
  171. }
  172. private void ThreadFinished(object sender, EventArgs e)
  173. {
  174. if (sender is AThread Thread)
  175. {
  176. TlsSlots.TryRemove(GetTlsSlot(Thread.ThreadState.Tpidr), out _);
  177. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  178. }
  179. }
  180. private int GetTlsSlot(long Position)
  181. {
  182. return (int)((Position - TlsPageAddr) / TlsSize);
  183. }
  184. public HThread GetThread(long Tpidr)
  185. {
  186. if (!ThreadsByTpidr.TryGetValue(Tpidr, out HThread Thread))
  187. {
  188. Logging.Error($"Thread with TPIDR 0x{Tpidr:x16} not found!");
  189. }
  190. return Thread;
  191. }
  192. public void Dispose()
  193. {
  194. Dispose(true);
  195. }
  196. protected virtual void Dispose(bool Disposing)
  197. {
  198. if (Disposing)
  199. {
  200. Scheduler.Dispose();
  201. }
  202. }
  203. }
  204. }