Process.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using ChocolArm64;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using Ryujinx.Loaders;
  5. using Ryujinx.Loaders.Executables;
  6. using Ryujinx.OsHle.Exceptions;
  7. using Ryujinx.OsHle.Handles;
  8. using Ryujinx.OsHle.Svc;
  9. using System;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.Threading;
  13. namespace Ryujinx.OsHle
  14. {
  15. 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. Executable Executable = new Executable(Program, Memory, ImageBase);
  52. Executables.Add(Executable);
  53. ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
  54. }
  55. public void SetEmptyArgs()
  56. {
  57. ImageBase += AMemoryMgr.PageSize;
  58. }
  59. public void InitializeHeap()
  60. {
  61. Memory.Manager.SetHeapAddr((ImageBase + 0x3fffffff) & ~0x3fffffff);
  62. }
  63. public bool Run()
  64. {
  65. if (Executables.Count == 0)
  66. {
  67. return false;
  68. }
  69. long StackBot = TlsPageAddr - MaxStackSize;
  70. Memory.Manager.MapPhys(StackBot, MaxStackSize, (int)MemoryType.Normal, AMemoryPerm.RW);
  71. int Handle = MakeThread(Executables[0].ImageBase, TlsPageAddr, 0, 0, 0);
  72. if (Handle == -1)
  73. {
  74. return false;
  75. }
  76. MainThread = Ns.Os.Handles.GetData<HThread>(Handle);
  77. Scheduler.StartThread(MainThread);
  78. return true;
  79. }
  80. public void StopAllThreads()
  81. {
  82. if (MainThread != null)
  83. {
  84. if (MainThread.Thread.IsAlive)
  85. {
  86. MainThread.Thread.StopExecution();
  87. }
  88. }
  89. foreach (AThread Thread in TlsSlots.Values)
  90. {
  91. if (Thread.IsAlive)
  92. {
  93. Thread.StopExecution();
  94. }
  95. }
  96. }
  97. public int MakeThread(
  98. long EntryPoint,
  99. long StackTop,
  100. long ArgsPtr,
  101. int Priority,
  102. int ProcessorId)
  103. {
  104. ThreadPriority ThreadPrio;
  105. if (Priority < 12)
  106. {
  107. ThreadPrio = ThreadPriority.Highest;
  108. }
  109. else if (Priority < 24)
  110. {
  111. ThreadPrio = ThreadPriority.AboveNormal;
  112. }
  113. else if (Priority < 36)
  114. {
  115. ThreadPrio = ThreadPriority.Normal;
  116. }
  117. else if (Priority < 48)
  118. {
  119. ThreadPrio = ThreadPriority.BelowNormal;
  120. }
  121. else
  122. {
  123. ThreadPrio = ThreadPriority.Lowest;
  124. }
  125. AThread Thread = new AThread(Memory, ThreadPrio, EntryPoint);
  126. HThread ThreadHnd = new HThread(Thread, ProcessorId, Priority);
  127. int Handle = Ns.Os.Handles.GenerateId(ThreadHnd);
  128. int TlsSlot = GetFreeTlsSlot(Thread);
  129. if (TlsSlot == -1 || Handle == -1)
  130. {
  131. return -1;
  132. }
  133. Thread.Registers.Break += BreakHandler;
  134. Thread.Registers.SvcCall += SvcHandler.SvcCall;
  135. Thread.Registers.Undefined += UndefinedHandler;
  136. Thread.Registers.ProcessId = ProcessId;
  137. Thread.Registers.ThreadId = Ns.Os.IdGen.GenerateId();
  138. Thread.Registers.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  139. Thread.Registers.X0 = (ulong)ArgsPtr;
  140. Thread.Registers.X1 = (ulong)Handle;
  141. Thread.Registers.X31 = (ulong)StackTop;
  142. Thread.WorkFinished += ThreadFinished;
  143. ThreadsByTpidr.TryAdd(Thread.Registers.Tpidr, ThreadHnd);
  144. return Handle;
  145. }
  146. private void BreakHandler(object sender, AInstExceptEventArgs e)
  147. {
  148. throw new GuestBrokeExecutionException();
  149. }
  150. private void UndefinedHandler(object sender, AInstUndEventArgs e)
  151. {
  152. throw new UndefinedInstructionException(e.Position, e.RawOpCode);
  153. }
  154. private int GetFreeTlsSlot(AThread Thread)
  155. {
  156. for (int Index = 1; Index < TotalTlsSlots; Index++)
  157. {
  158. if (TlsSlots.TryAdd(Index, Thread))
  159. {
  160. return Index;
  161. }
  162. }
  163. return -1;
  164. }
  165. private void ThreadFinished(object sender, EventArgs e)
  166. {
  167. if (sender is AThread Thread)
  168. {
  169. TlsSlots.TryRemove(GetTlsSlot(Thread.Registers.Tpidr), out _);
  170. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  171. }
  172. }
  173. private int GetTlsSlot(long Position)
  174. {
  175. return (int)((Position - TlsPageAddr) / TlsSize);
  176. }
  177. public bool TryGetThread(long Tpidr, out HThread Thread)
  178. {
  179. return ThreadsByTpidr.TryGetValue(Tpidr, out Thread);
  180. }
  181. public void Dispose()
  182. {
  183. Dispose(true);
  184. }
  185. protected virtual void Dispose(bool Disposing)
  186. {
  187. if (Disposing)
  188. {
  189. Scheduler.Dispose();
  190. }
  191. }
  192. }
  193. }