SvcThread.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (ProcessorId == -2)
  18. {
  19. //TODO: Get this value from the NPDM file.
  20. ProcessorId = 0;
  21. }
  22. int Handle = Process.MakeThread(
  23. EntryPoint,
  24. StackTop,
  25. ArgsPtr,
  26. Priority,
  27. ProcessorId);
  28. ThreadState.X0 = 0;
  29. ThreadState.X1 = (ulong)Handle;
  30. }
  31. private void SvcStartThread(AThreadState ThreadState)
  32. {
  33. int Handle = (int)ThreadState.X0;
  34. KThread CurrThread = Process.HandleTable.GetData<KThread>(Handle);
  35. if (CurrThread != null)
  36. {
  37. Process.Scheduler.StartThread(CurrThread);
  38. Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
  39. ThreadState.X0 = 0;
  40. }
  41. else
  42. {
  43. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  44. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  45. }
  46. }
  47. private void SvcExitThread(AThreadState ThreadState)
  48. {
  49. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  50. CurrThread.Thread.StopExecution();
  51. }
  52. private void SvcSleepThread(AThreadState ThreadState)
  53. {
  54. ulong Ns = ThreadState.X0;
  55. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  56. if (Ns == 0)
  57. {
  58. Process.Scheduler.Yield(CurrThread);
  59. }
  60. else
  61. {
  62. Process.Scheduler.Suspend(CurrThread);
  63. Thread.Sleep(NsTimeConverter.GetTimeMs(Ns));
  64. Process.Scheduler.Resume(CurrThread);
  65. }
  66. }
  67. private void SvcGetThreadPriority(AThreadState ThreadState)
  68. {
  69. int Handle = (int)ThreadState.X1;
  70. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  71. if (Thread != null)
  72. {
  73. ThreadState.X0 = 0;
  74. ThreadState.X1 = (ulong)Thread.ActualPriority;
  75. }
  76. else
  77. {
  78. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  79. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  80. }
  81. }
  82. private void SvcSetThreadPriority(AThreadState ThreadState)
  83. {
  84. int Handle = (int)ThreadState.X0;
  85. int Priority = (int)ThreadState.X1;
  86. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  87. if (Thread != null)
  88. {
  89. Thread.SetPriority(Priority);
  90. ThreadState.X0 = 0;
  91. }
  92. else
  93. {
  94. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  95. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  96. }
  97. }
  98. private void SvcSetThreadCoreMask(AThreadState ThreadState)
  99. {
  100. ThreadState.X0 = 0;
  101. //TODO: Error codes.
  102. }
  103. private void SvcGetCurrentProcessorNumber(AThreadState ThreadState)
  104. {
  105. ThreadState.X0 = (ulong)Process.GetThread(ThreadState.Tpidr).ActualCore;
  106. }
  107. private void SvcGetThreadId(AThreadState ThreadState)
  108. {
  109. int Handle = (int)ThreadState.X1;
  110. KThread Thread = GetThread(ThreadState.Tpidr, Handle);
  111. if (Thread != null)
  112. {
  113. ThreadState.X0 = 0;
  114. ThreadState.X1 = (ulong)Thread.ThreadId;
  115. }
  116. else
  117. {
  118. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  119. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  120. }
  121. }
  122. private void SvcSetThreadActivity(AThreadState ThreadState)
  123. {
  124. int Handle = (int)ThreadState.X0;
  125. bool Active = (int)ThreadState.X1 == 0;
  126. KThread Thread = Process.HandleTable.GetData<KThread>(Handle);
  127. if (Thread != null)
  128. {
  129. Process.Scheduler.SetThreadActivity(Thread, Active);
  130. ThreadState.X0 = 0;
  131. }
  132. else
  133. {
  134. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{Handle:x8}!");
  135. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  136. }
  137. }
  138. }
  139. }