Process.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(MemoryRegions.HeapRegionAddress);
  63. }
  64. public bool Run(bool UseHbAbi = false)
  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. if (UseHbAbi)
  79. {
  80. Homebrew Homebrew_ABI = new Homebrew(Memory, Executables[0].ImageEnd, (long)Handle);
  81. MainThread.Thread.ThreadState.X0 = (ulong)Executables[0].ImageEnd;
  82. MainThread.Thread.ThreadState.X1 = ulong.MaxValue;
  83. }
  84. Scheduler.StartThread(MainThread);
  85. return true;
  86. }
  87. public void StopAllThreads()
  88. {
  89. if (MainThread != null)
  90. {
  91. while (MainThread.Thread.IsAlive)
  92. {
  93. MainThread.Thread.StopExecution();
  94. }
  95. }
  96. foreach (AThread Thread in TlsSlots.Values)
  97. {
  98. while (Thread.IsAlive)
  99. {
  100. Thread.StopExecution();
  101. }
  102. }
  103. }
  104. public int MakeThread(
  105. long EntryPoint,
  106. long StackTop,
  107. long ArgsPtr,
  108. int Priority,
  109. int ProcessorId)
  110. {
  111. ThreadPriority ThreadPrio;
  112. if (Priority < 12)
  113. {
  114. ThreadPrio = ThreadPriority.Highest;
  115. }
  116. else if (Priority < 24)
  117. {
  118. ThreadPrio = ThreadPriority.AboveNormal;
  119. }
  120. else if (Priority < 36)
  121. {
  122. ThreadPrio = ThreadPriority.Normal;
  123. }
  124. else if (Priority < 48)
  125. {
  126. ThreadPrio = ThreadPriority.BelowNormal;
  127. }
  128. else
  129. {
  130. ThreadPrio = ThreadPriority.Lowest;
  131. }
  132. AThread Thread = new AThread(Memory, ThreadPrio, EntryPoint);
  133. HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
  134. int Handle = Ns.Os.Handles.GenerateId(ThreadHnd);
  135. int TlsSlot = GetFreeTlsSlot(Thread);
  136. if (TlsSlot == -1 || Handle == -1)
  137. {
  138. return -1;
  139. }
  140. Thread.ThreadState.Break += BreakHandler;
  141. Thread.ThreadState.SvcCall += SvcHandler.SvcCall;
  142. Thread.ThreadState.Undefined += UndefinedHandler;
  143. Thread.ThreadState.ProcessId = ProcessId;
  144. Thread.ThreadState.ThreadId = Ns.Os.IdGen.GenerateId();
  145. Thread.ThreadState.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  146. Thread.ThreadState.X0 = (ulong)ArgsPtr;
  147. Thread.ThreadState.X1 = (ulong)Handle;
  148. Thread.ThreadState.X31 = (ulong)StackTop;
  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. }