Process.cs 4.8 KB

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