SvcThread.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using ChocolArm64.State;
  2. using Ryujinx.HLE.Logging;
  3. using Ryujinx.HLE.OsHle.Handles;
  4. using System.Threading;
  5. using static Ryujinx.HLE.OsHle.ErrorCode;
  6. namespace Ryujinx.HLE.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. Ns.Log.PrintDebug(LogClass.KernelSvc, "Timeout = " + TimeoutNs.ToString("x16"));
  68. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  69. if (TimeoutNs == 0 || TimeoutNs == ulong.MaxValue)
  70. {
  71. Process.Scheduler.Yield(CurrThread);
  72. }
  73. else
  74. {
  75. Process.Scheduler.Suspend(CurrThread);
  76. Thread.Sleep(NsTimeConverter.GetTimeMs(TimeoutNs));
  77. Process.Scheduler.Resume(CurrThread);
  78. }
  79. }
  80. private void SvcGetThreadPriority(AThreadState ThreadState)
  81. {
  82. int Handle = (int)ThreadState.X1;
  83. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  84. if (Thread != null)
  85. {
  86. ThreadState.X0 = 0;
  87. ThreadState.X1 = (ulong)Thread.ActualPriority;
  88. }
  89. else
  90. {
  91. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  92. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  93. }
  94. }
  95. private void SvcSetThreadPriority(AThreadState ThreadState)
  96. {
  97. int Handle = (int)ThreadState.X0;
  98. int Priority = (int)ThreadState.X1;
  99. Ns.Log.PrintDebug(LogClass.KernelSvc,
  100. "Handle = " + Handle .ToString("x8") + ", " +
  101. "Priority = " + Priority.ToString("x8"));
  102. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  103. if (Thread != null)
  104. {
  105. Thread.SetPriority(Priority);
  106. ThreadState.X0 = 0;
  107. }
  108. else
  109. {
  110. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  111. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  112. }
  113. }
  114. private void SvcGetThreadCoreMask(AThreadState ThreadState)
  115. {
  116. int Handle = (int)ThreadState.X2;
  117. Ns.Log.PrintDebug(LogClass.KernelSvc, "Handle = " + Handle.ToString("x8"));
  118. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  119. if (Thread != null)
  120. {
  121. ThreadState.X0 = 0;
  122. ThreadState.X1 = (ulong)Thread.IdealCore;
  123. ThreadState.X2 = (ulong)Thread.CoreMask;
  124. }
  125. else
  126. {
  127. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  128. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  129. }
  130. }
  131. private void SvcSetThreadCoreMask(AThreadState ThreadState)
  132. {
  133. int Handle = (int)ThreadState.X0;
  134. int IdealCore = (int)ThreadState.X1;
  135. long CoreMask = (long)ThreadState.X2;
  136. Ns.Log.PrintDebug(LogClass.KernelSvc,
  137. "Handle = " + Handle .ToString("x8") + ", " +
  138. "IdealCore = " + IdealCore.ToString("x8") + ", " +
  139. "CoreMask = " + CoreMask .ToString("x16"));
  140. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  141. if (IdealCore == -2)
  142. {
  143. //TODO: Get this value from the NPDM file.
  144. IdealCore = 0;
  145. CoreMask = 1 << IdealCore;
  146. }
  147. else
  148. {
  149. if ((uint)IdealCore > 3)
  150. {
  151. if ((IdealCore | 2) != -1)
  152. {
  153. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core id 0x{IdealCore:x8}!");
  154. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreId);
  155. return;
  156. }
  157. }
  158. else if ((CoreMask & (1 << IdealCore)) == 0)
  159. {
  160. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  161. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  162. return;
  163. }
  164. }
  165. if (Thread == null)
  166. {
  167. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  168. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  169. return;
  170. }
  171. //-1 is used as "don't care", so the IdealCore value is ignored.
  172. //-2 is used as "use NPDM default core id" (handled above).
  173. //-3 is used as "don't update", the old IdealCore value is kept.
  174. if (IdealCore == -3 && (CoreMask & (1 << Thread.IdealCore)) == 0)
  175. {
  176. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid core mask 0x{CoreMask:x8}!");
  177. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidCoreMask);
  178. return;
  179. }
  180. Process.Scheduler.ChangeCore(Thread, IdealCore, (int)CoreMask);
  181. ThreadState.X0 = 0;
  182. }
  183. private void SvcGetCurrentProcessorNumber(AThreadState ThreadState)
  184. {
  185. ThreadState.X0 = (ulong)Process.GetThread(ThreadState.Tpidr).ActualCore;
  186. }
  187. private void SvcGetThreadId(AThreadState ThreadState)
  188. {
  189. int Handle = (int)ThreadState.X1;
  190. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  191. if (Thread != null)
  192. {
  193. ThreadState.X0 = 0;
  194. ThreadState.X1 = (ulong)Thread.ThreadId;
  195. }
  196. else
  197. {
  198. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  199. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  200. }
  201. }
  202. private void SvcSetThreadActivity(AThreadState ThreadState)
  203. {
  204. int Handle = (int)ThreadState.X0;
  205. bool Active = (int)ThreadState.X1 == 0;
  206. KThread Thread = Process.HandleTable.GetData<KThread>(Handle);
  207. if (Thread != null)
  208. {
  209. Process.Scheduler.SetThreadActivity(Thread, Active);
  210. ThreadState.X0 = 0;
  211. }
  212. else
  213. {
  214. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  215. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  216. }
  217. }
  218. private void SvcGetThreadContext3(AThreadState ThreadState)
  219. {
  220. long Position = (long)ThreadState.X0;
  221. int Handle = (int)ThreadState.X1;
  222. KThread Thread = Process.HandleTable.GetData<KThread>(Handle);
  223. if (Thread == null)
  224. {
  225. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  226. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  227. return;
  228. }
  229. if (Process.GetThread(ThreadState.Tpidr) == Thread)
  230. {
  231. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Thread handle 0x{Handle:x8} is current thread!");
  232. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidThread);
  233. return;
  234. }
  235. Memory.WriteUInt64(Position + 0x0, ThreadState.X0);
  236. Memory.WriteUInt64(Position + 0x8, ThreadState.X1);
  237. Memory.WriteUInt64(Position + 0x10, ThreadState.X2);
  238. Memory.WriteUInt64(Position + 0x18, ThreadState.X3);
  239. Memory.WriteUInt64(Position + 0x20, ThreadState.X4);
  240. Memory.WriteUInt64(Position + 0x28, ThreadState.X5);
  241. Memory.WriteUInt64(Position + 0x30, ThreadState.X6);
  242. Memory.WriteUInt64(Position + 0x38, ThreadState.X7);
  243. Memory.WriteUInt64(Position + 0x40, ThreadState.X8);
  244. Memory.WriteUInt64(Position + 0x48, ThreadState.X9);
  245. Memory.WriteUInt64(Position + 0x50, ThreadState.X10);
  246. Memory.WriteUInt64(Position + 0x58, ThreadState.X11);
  247. Memory.WriteUInt64(Position + 0x60, ThreadState.X12);
  248. Memory.WriteUInt64(Position + 0x68, ThreadState.X13);
  249. Memory.WriteUInt64(Position + 0x70, ThreadState.X14);
  250. Memory.WriteUInt64(Position + 0x78, ThreadState.X15);
  251. Memory.WriteUInt64(Position + 0x80, ThreadState.X16);
  252. Memory.WriteUInt64(Position + 0x88, ThreadState.X17);
  253. Memory.WriteUInt64(Position + 0x90, ThreadState.X18);
  254. Memory.WriteUInt64(Position + 0x98, ThreadState.X19);
  255. Memory.WriteUInt64(Position + 0xa0, ThreadState.X20);
  256. Memory.WriteUInt64(Position + 0xa8, ThreadState.X21);
  257. Memory.WriteUInt64(Position + 0xb0, ThreadState.X22);
  258. Memory.WriteUInt64(Position + 0xb8, ThreadState.X23);
  259. Memory.WriteUInt64(Position + 0xc0, ThreadState.X24);
  260. Memory.WriteUInt64(Position + 0xc8, ThreadState.X25);
  261. Memory.WriteUInt64(Position + 0xd0, ThreadState.X26);
  262. Memory.WriteUInt64(Position + 0xd8, ThreadState.X27);
  263. Memory.WriteUInt64(Position + 0xe0, ThreadState.X28);
  264. Memory.WriteUInt64(Position + 0xe8, ThreadState.X29);
  265. Memory.WriteUInt64(Position + 0xf0, ThreadState.X30);
  266. Memory.WriteUInt64(Position + 0xf8, ThreadState.X31);
  267. Memory.WriteInt64(Position + 0x100, Thread.LastPc);
  268. Memory.WriteUInt64(Position + 0x108, (ulong)ThreadState.Psr);
  269. Memory.WriteVector128(Position + 0x110, ThreadState.V0);
  270. Memory.WriteVector128(Position + 0x120, ThreadState.V1);
  271. Memory.WriteVector128(Position + 0x130, ThreadState.V2);
  272. Memory.WriteVector128(Position + 0x140, ThreadState.V3);
  273. Memory.WriteVector128(Position + 0x150, ThreadState.V4);
  274. Memory.WriteVector128(Position + 0x160, ThreadState.V5);
  275. Memory.WriteVector128(Position + 0x170, ThreadState.V6);
  276. Memory.WriteVector128(Position + 0x180, ThreadState.V7);
  277. Memory.WriteVector128(Position + 0x190, ThreadState.V8);
  278. Memory.WriteVector128(Position + 0x1a0, ThreadState.V9);
  279. Memory.WriteVector128(Position + 0x1b0, ThreadState.V10);
  280. Memory.WriteVector128(Position + 0x1c0, ThreadState.V11);
  281. Memory.WriteVector128(Position + 0x1d0, ThreadState.V12);
  282. Memory.WriteVector128(Position + 0x1e0, ThreadState.V13);
  283. Memory.WriteVector128(Position + 0x1f0, ThreadState.V14);
  284. Memory.WriteVector128(Position + 0x200, ThreadState.V15);
  285. Memory.WriteVector128(Position + 0x210, ThreadState.V16);
  286. Memory.WriteVector128(Position + 0x220, ThreadState.V17);
  287. Memory.WriteVector128(Position + 0x230, ThreadState.V18);
  288. Memory.WriteVector128(Position + 0x240, ThreadState.V19);
  289. Memory.WriteVector128(Position + 0x250, ThreadState.V20);
  290. Memory.WriteVector128(Position + 0x260, ThreadState.V21);
  291. Memory.WriteVector128(Position + 0x270, ThreadState.V22);
  292. Memory.WriteVector128(Position + 0x280, ThreadState.V23);
  293. Memory.WriteVector128(Position + 0x290, ThreadState.V24);
  294. Memory.WriteVector128(Position + 0x2a0, ThreadState.V25);
  295. Memory.WriteVector128(Position + 0x2b0, ThreadState.V26);
  296. Memory.WriteVector128(Position + 0x2c0, ThreadState.V27);
  297. Memory.WriteVector128(Position + 0x2d0, ThreadState.V28);
  298. Memory.WriteVector128(Position + 0x2e0, ThreadState.V29);
  299. Memory.WriteVector128(Position + 0x2f0, ThreadState.V30);
  300. Memory.WriteVector128(Position + 0x300, ThreadState.V31);
  301. Memory.WriteInt32(Position + 0x310, ThreadState.Fpcr);
  302. Memory.WriteInt32(Position + 0x314, ThreadState.Fpsr);
  303. Memory.WriteInt64(Position + 0x318, ThreadState.Tpidr);
  304. ThreadState.X0 = 0;
  305. }
  306. }
  307. }