SvcThread.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 CurrThread = Process.HandleTable.GetData<KThread>(Handle);
  47. if (CurrThread != null)
  48. {
  49. Process.Scheduler.StartThread(CurrThread);
  50. Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
  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 Ns = ThreadState.X0;
  67. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  68. if (Ns == 0)
  69. {
  70. Process.Scheduler.Yield(CurrThread);
  71. }
  72. else
  73. {
  74. Process.Scheduler.Suspend(CurrThread);
  75. Thread.Sleep(NsTimeConverter.GetTimeMs(Ns));
  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. int Handle = (int)ThreadState.X0;
  113. int IdealCore = (int)ThreadState.X1;
  114. long CoreMask = (long)ThreadState.X2;
  115. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  116. if (IdealCore == -2)
  117. {
  118. //TODO: Get this value from the NPDM file.
  119. IdealCore = 0;
  120. CoreMask = 1 << IdealCore;
  121. }
  122. else if (IdealCore != -3)
  123. {
  124. if ((uint)IdealCore > 3)
  125. {
  126. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core id 0x{IdealCore:x8}!");
  127. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreId);
  128. return;
  129. }
  130. if ((CoreMask & (1 << IdealCore)) == 0)
  131. {
  132. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  133. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  134. return;
  135. }
  136. }
  137. if (Thread == null)
  138. {
  139. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  140. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  141. return;
  142. }
  143. if (IdealCore == -3)
  144. {
  145. if ((CoreMask & (1 << Thread.IdealCore)) == 0)
  146. {
  147. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  148. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  149. return;
  150. }
  151. }
  152. else
  153. {
  154. Thread.IdealCore = IdealCore;
  155. }
  156. Thread.CoreMask = (int)CoreMask;
  157. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  158. Process.Scheduler.Yield(CurrThread);
  159. Process.Scheduler.TryRunning(Thread);
  160. ThreadState.X0 = 0;
  161. }
  162. private void SvcGetCurrentProcessorNumber(AThreadState ThreadState)
  163. {
  164. ThreadState.X0 = (ulong)Process.GetThread(ThreadState.Tpidr).ActualCore;
  165. }
  166. private void SvcGetThreadId(AThreadState ThreadState)
  167. {
  168. int Handle = (int)ThreadState.X1;
  169. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  170. if (Thread != null)
  171. {
  172. ThreadState.X0 = 0;
  173. ThreadState.X1 = (ulong)Thread.ThreadId;
  174. }
  175. else
  176. {
  177. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  178. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  179. }
  180. }
  181. private void SvcSetThreadActivity(AThreadState ThreadState)
  182. {
  183. int Handle = (int)ThreadState.X0;
  184. bool Active = (int)ThreadState.X1 == 0;
  185. KThread Thread = Process.HandleTable.GetData<KThread>(Handle);
  186. if (Thread != null)
  187. {
  188. Process.Scheduler.SetThreadActivity(Thread, Active);
  189. ThreadState.X0 = 0;
  190. }
  191. else
  192. {
  193. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  194. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  195. }
  196. }
  197. }
  198. }