Process.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. namespace Ryujinx.OsHle
  13. {
  14. class Process
  15. {
  16. private const int MaxStackSize = 8 * 1024 * 1024;
  17. private const int TlsSize = 0x200;
  18. private const int TotalTlsSlots = 32;
  19. private const int TlsTotalSize = TotalTlsSlots * TlsSize;
  20. private const long TlsPageAddr = (AMemoryMgr.AddrSize - TlsTotalSize) & ~AMemoryMgr.PageMask;
  21. private Switch Ns;
  22. public int ProcessId { get; private set; }
  23. public AMemory Memory { get; private set; }
  24. private SvcHandler SvcHandler;
  25. private AThread MainThread;
  26. private ConcurrentDictionary<int, AThread> TlsSlots;
  27. private List<Executable> Executables;
  28. private long ImageBase;
  29. public Process(Switch Ns, AMemoryAlloc Allocator, int ProcessId)
  30. {
  31. this.Ns = Ns;
  32. this.ProcessId = ProcessId;
  33. Memory = new AMemory(Ns.Ram, Allocator);
  34. SvcHandler = new SvcHandler(Ns, Memory);
  35. TlsSlots = new ConcurrentDictionary<int, AThread>();
  36. Executables = new List<Executable>();
  37. ImageBase = 0x8000000;
  38. Memory.Manager.MapPhys(
  39. TlsPageAddr,
  40. TlsTotalSize,
  41. (int)MemoryType.ThreadLocal,
  42. AMemoryPerm.RW);
  43. }
  44. public void LoadProgram(IExecutable Program)
  45. {
  46. Executable Executable = new Executable(Program, Memory, ImageBase);
  47. Executables.Add(Executable);
  48. ImageBase = AMemoryHelper.PageRoundUp(Executable.ImageEnd);
  49. }
  50. public void SetEmptyArgs()
  51. {
  52. ImageBase += AMemoryMgr.PageSize;
  53. }
  54. public void InitializeHeap()
  55. {
  56. Memory.Manager.SetHeapAddr((ImageBase + 0x3fffffff) & ~0x3fffffff);
  57. }
  58. public bool Run()
  59. {
  60. if (Executables.Count == 0)
  61. {
  62. return false;
  63. }
  64. long StackBot = TlsPageAddr - MaxStackSize;
  65. Memory.Manager.MapPhys(StackBot, MaxStackSize, (int)MemoryType.Normal, AMemoryPerm.RW);
  66. int Handle = MakeThread(Executables[0].ImageBase, TlsPageAddr, 0, 48, 0);
  67. if (Handle == -1)
  68. {
  69. return false;
  70. }
  71. MainThread = Ns.Os.Handles.GetData<HThread>(Handle).Thread;
  72. MainThread.Execute();
  73. return true;
  74. }
  75. public void StopAllThreads()
  76. {
  77. if (MainThread != null)
  78. {
  79. while (MainThread.IsAlive)
  80. {
  81. MainThread.StopExecution();
  82. }
  83. }
  84. foreach (AThread Thread in TlsSlots.Values)
  85. {
  86. while (Thread.IsAlive)
  87. {
  88. Thread.StopExecution();
  89. }
  90. }
  91. }
  92. public int MakeThread(
  93. long EntryPoint,
  94. long StackTop,
  95. long ArgsPtr,
  96. int Priority,
  97. int ProcessorId)
  98. {
  99. AThread Thread = new AThread(Memory, EntryPoint, Priority);
  100. int TlsSlot = GetFreeTlsSlot(Thread);
  101. int Handle = Ns.Os.Handles.GenerateId(new HThread(Thread));
  102. if (TlsSlot == -1 || Handle == -1)
  103. {
  104. return -1;
  105. }
  106. Thread.Registers.Break += BreakHandler;
  107. Thread.Registers.SvcCall += SvcHandler.SvcCall;
  108. Thread.Registers.ProcessId = ProcessId;
  109. Thread.Registers.ThreadId = Ns.Os.IdGen.GenerateId();
  110. Thread.Registers.Tpidr = TlsPageAddr + TlsSlot * TlsSize;
  111. Thread.Registers.X0 = (ulong)ArgsPtr;
  112. Thread.Registers.X1 = (ulong)Handle;
  113. Thread.Registers.X31 = (ulong)StackTop;
  114. Thread.WorkFinished += ThreadFinished;
  115. return Handle;
  116. }
  117. private void BreakHandler(object sender, AExceptionEventArgs e)
  118. {
  119. throw new GuestBrokeExecutionException();
  120. }
  121. private int GetFreeTlsSlot(AThread Thread)
  122. {
  123. for (int Index = 1; Index < TotalTlsSlots; Index++)
  124. {
  125. if (TlsSlots.TryAdd(Index, Thread))
  126. {
  127. return Index;
  128. }
  129. }
  130. return -1;
  131. }
  132. private void ThreadFinished(object sender, EventArgs e)
  133. {
  134. if (sender is AThread Thread)
  135. {
  136. TlsSlots.TryRemove(GetTlsSlot(Thread.Registers.Tpidr), out _);
  137. Ns.Os.IdGen.DeleteId(Thread.ThreadId);
  138. }
  139. }
  140. private int GetTlsSlot(long Position)
  141. {
  142. return (int)((Position - TlsPageAddr) / TlsSize);
  143. }
  144. }
  145. }