SvcThread.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using ChocolArm64.State;
  2. using Ryujinx.Core.Logging;
  3. using Ryujinx.Core.OsHle.Handles;
  4. using System.Threading;
  5. using static Ryujinx.Core.OsHle.ErrorCode;
  6. namespace Ryujinx.Core.OsHle.Kernel
  7. {
  8. partial class SvcHandler
  9. {
  10. private void SvcCreateThread(AThreadState ThreadState)
  11. {
  12. long EntryPoint = (long)ThreadState.X1;
  13. long ArgsPtr = (long)ThreadState.X2;
  14. long StackTop = (long)ThreadState.X3;
  15. int Priority = (int)ThreadState.X4;
  16. int ProcessorId = (int)ThreadState.X5;
  17. if ((uint)Priority > 0x3f)
  18. {
  19. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid priority 0x{Priority:x8}!");
  20. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidPriority);
  21. return;
  22. }
  23. if (ProcessorId == -2)
  24. {
  25. //TODO: Get this value from the NPDM file.
  26. ProcessorId = 0;
  27. }
  28. else if ((uint)ProcessorId > 3)
  29. {
  30. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core id 0x{ProcessorId:x8}!");
  31. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreId);
  32. return;
  33. }
  34. int Handle = Process.MakeThread(
  35. EntryPoint,
  36. StackTop,
  37. ArgsPtr,
  38. Priority,
  39. ProcessorId);
  40. ThreadState.X0 = 0;
  41. ThreadState.X1 = (ulong)Handle;
  42. }
  43. private void SvcStartThread(AThreadState ThreadState)
  44. {
  45. int Handle = (int)ThreadState.X0;
  46. KThread NewThread = Process.HandleTable.GetData<KThread>(Handle);
  47. if (NewThread != null)
  48. {
  49. Process.Scheduler.StartThread(NewThread);
  50. Process.Scheduler.SetReschedule(NewThread.ProcessorId);
  51. ThreadState.X0 = 0;
  52. }
  53. else
  54. {
  55. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  56. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  57. }
  58. }
  59. private void SvcExitThread(AThreadState ThreadState)
  60. {
  61. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  62. CurrThread.Thread.StopExecution();
  63. }
  64. private void SvcSleepThread(AThreadState ThreadState)
  65. {
  66. ulong TimeoutNs = ThreadState.X0;
  67. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  68. if (TimeoutNs == 0)
  69. {
  70. Process.Scheduler.SetReschedule(CurrThread.ActualCore);
  71. }
  72. else
  73. {
  74. Process.Scheduler.Suspend(CurrThread);
  75. Thread.Sleep(NsTimeConverter.GetTimeMs(TimeoutNs));
  76. Process.Scheduler.Resume(CurrThread);
  77. }
  78. }
  79. private void SvcGetThreadPriority(AThreadState ThreadState)
  80. {
  81. int Handle = (int)ThreadState.X1;
  82. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  83. if (Thread != null)
  84. {
  85. ThreadState.X0 = 0;
  86. ThreadState.X1 = (ulong)Thread.ActualPriority;
  87. }
  88. else
  89. {
  90. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  91. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  92. }
  93. }
  94. private void SvcSetThreadPriority(AThreadState ThreadState)
  95. {
  96. int Handle = (int)ThreadState.X0;
  97. int Priority = (int)ThreadState.X1;
  98. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  99. if (Thread != null)
  100. {
  101. Thread.SetPriority(Priority);
  102. ThreadState.X0 = 0;
  103. }
  104. else
  105. {
  106. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  107. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  108. }
  109. }
  110. private void SvcSetThreadCoreMask(AThreadState ThreadState)
  111. {
  112. //FIXME: This is wrong, but the "correct" way to handle
  113. //this svc causes deadlocks when more often.
  114. //There is probably something wrong with it still.
  115. ThreadState.X0 = 0;
  116. return;
  117. int Handle = (int)ThreadState.X0;
  118. int IdealCore = (int)ThreadState.X1;
  119. long CoreMask = (long)ThreadState.X2;
  120. Ns.Log.PrintDebug(LogClass.KernelSvc,
  121. "Handle = " + Handle .ToString("x8") + ", " +
  122. "IdealCore = " + IdealCore.ToString("x8") + ", " +
  123. "CoreMask = " + CoreMask .ToString("x16"));
  124. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  125. if (IdealCore == -2)
  126. {
  127. //TODO: Get this value from the NPDM file.
  128. IdealCore = 0;
  129. CoreMask = 1 << IdealCore;
  130. }
  131. else if (IdealCore != -3)
  132. {
  133. if ((uint)IdealCore > 3)
  134. {
  135. if ((IdealCore | 2) != -1)
  136. {
  137. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core id 0x{IdealCore:x8}!");
  138. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreId);
  139. return;
  140. }
  141. }
  142. else if ((CoreMask & (1 << IdealCore)) == 0)
  143. {
  144. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  145. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  146. return;
  147. }
  148. }
  149. if (Thread == null)
  150. {
  151. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  152. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  153. return;
  154. }
  155. //-1 is used as "don't care", so the IdealCore value is ignored.
  156. //-2 is used as "use NPDM default core id" (handled above).
  157. //-3 is used as "don't update", the old IdealCore value is kept.
  158. if (IdealCore != -3)
  159. {
  160. Thread.IdealCore = IdealCore;
  161. }
  162. else if ((CoreMask & (1 << Thread.IdealCore)) == 0)
  163. {
  164. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  165. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  166. return;
  167. }
  168. Thread.CoreMask = (int)CoreMask;
  169. Process.Scheduler.TryToRun(Thread);
  170. ThreadState.X0 = 0;
  171. }
  172. private void SvcGetCurrentProcessorNumber(AThreadState ThreadState)
  173. {
  174. ThreadState.X0 = (ulong)Process.GetThread(ThreadState.Tpidr).ActualCore;
  175. }
  176. private void SvcGetThreadId(AThreadState ThreadState)
  177. {
  178. int Handle = (int)ThreadState.X1;
  179. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  180. if (Thread != null)
  181. {
  182. ThreadState.X0 = 0;
  183. ThreadState.X1 = (ulong)Thread.ThreadId;
  184. }
  185. else
  186. {
  187. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  188. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  189. }
  190. }
  191. private void SvcSetThreadActivity(AThreadState ThreadState)
  192. {
  193. int Handle = (int)ThreadState.X0;
  194. bool Active = (int)ThreadState.X1 == 0;
  195. KThread Thread = Process.HandleTable.GetData<KThread>(Handle);
  196. if (Thread != null)
  197. {
  198. Process.Scheduler.SetThreadActivity(Thread, Active);
  199. ThreadState.X0 = 0;
  200. }
  201. else
  202. {
  203. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  204. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  205. }
  206. }
  207. }
  208. }