SvcThreadSync.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using ChocolArm64.State;
  2. using Ryujinx.Core.Logging;
  3. using Ryujinx.Core.OsHle.Handles;
  4. using System;
  5. using System.Threading;
  6. using static Ryujinx.Core.OsHle.ErrorCode;
  7. namespace Ryujinx.Core.OsHle.Kernel
  8. {
  9. partial class SvcHandler
  10. {
  11. private const int MutexHasListenersMask = 0x40000000;
  12. private void SvcArbitrateLock(AThreadState ThreadState)
  13. {
  14. int OwnerThreadHandle = (int)ThreadState.X0;
  15. long MutexAddress = (long)ThreadState.X1;
  16. int WaitThreadHandle = (int)ThreadState.X2;
  17. if (IsPointingInsideKernel(MutexAddress))
  18. {
  19. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  20. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  21. return;
  22. }
  23. if (IsWordAddressUnaligned(MutexAddress))
  24. {
  25. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  26. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  27. return;
  28. }
  29. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  30. if (OwnerThread == null)
  31. {
  32. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid owner thread handle 0x{OwnerThreadHandle:x8}!");
  33. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  34. return;
  35. }
  36. KThread WaitThread = Process.HandleTable.GetData<KThread>(WaitThreadHandle);
  37. if (WaitThread == null)
  38. {
  39. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid requesting thread handle 0x{WaitThreadHandle:x8}!");
  40. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  41. return;
  42. }
  43. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  44. MutexLock(CurrThread, WaitThread, OwnerThreadHandle, WaitThreadHandle, MutexAddress);
  45. ThreadState.X0 = 0;
  46. }
  47. private void SvcArbitrateUnlock(AThreadState ThreadState)
  48. {
  49. long MutexAddress = (long)ThreadState.X0;
  50. if (IsPointingInsideKernel(MutexAddress))
  51. {
  52. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  53. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  54. return;
  55. }
  56. if (IsWordAddressUnaligned(MutexAddress))
  57. {
  58. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  59. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  60. return;
  61. }
  62. if (MutexUnlock(Process.GetThread(ThreadState.Tpidr), MutexAddress))
  63. {
  64. Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
  65. }
  66. ThreadState.X0 = 0;
  67. }
  68. private void SvcWaitProcessWideKeyAtomic(AThreadState ThreadState)
  69. {
  70. long MutexAddress = (long)ThreadState.X0;
  71. long CondVarAddress = (long)ThreadState.X1;
  72. int ThreadHandle = (int)ThreadState.X2;
  73. ulong Timeout = ThreadState.X3;
  74. if (IsPointingInsideKernel(MutexAddress))
  75. {
  76. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  77. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  78. return;
  79. }
  80. if (IsWordAddressUnaligned(MutexAddress))
  81. {
  82. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  83. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  84. return;
  85. }
  86. KThread Thread = Process.HandleTable.GetData<KThread>(ThreadHandle);
  87. if (Thread == null)
  88. {
  89. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{ThreadHandle:x8}!");
  90. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  91. return;
  92. }
  93. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  94. MutexUnlock(CurrThread, MutexAddress);
  95. if (!CondVarWait(CurrThread, ThreadHandle, MutexAddress, CondVarAddress, Timeout))
  96. {
  97. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
  98. return;
  99. }
  100. Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
  101. ThreadState.X0 = 0;
  102. }
  103. private void SvcSignalProcessWideKey(AThreadState ThreadState)
  104. {
  105. long CondVarAddress = (long)ThreadState.X0;
  106. int Count = (int)ThreadState.X1;
  107. CondVarSignal(CondVarAddress, Count);
  108. ThreadState.X0 = 0;
  109. }
  110. private void MutexLock(
  111. KThread CurrThread,
  112. KThread WaitThread,
  113. int OwnerThreadHandle,
  114. int WaitThreadHandle,
  115. long MutexAddress)
  116. {
  117. int MutexValue = Process.Memory.ReadInt32(MutexAddress);
  118. if (MutexValue != (OwnerThreadHandle | MutexHasListenersMask))
  119. {
  120. return;
  121. }
  122. CurrThread.WaitHandle = WaitThreadHandle;
  123. CurrThread.MutexAddress = MutexAddress;
  124. InsertWaitingMutexThread(OwnerThreadHandle, WaitThread);
  125. Process.Scheduler.EnterWait(WaitThread);
  126. }
  127. private bool MutexUnlock(KThread CurrThread, long MutexAddress)
  128. {
  129. if (CurrThread == null)
  130. {
  131. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex 0x{MutexAddress:x16}!");
  132. return false;
  133. }
  134. lock (CurrThread)
  135. {
  136. //This is the new thread that will not own the mutex.
  137. //If no threads are waiting for the lock, then it should be null.
  138. KThread OwnerThread = CurrThread.NextMutexThread;
  139. while (OwnerThread != null && OwnerThread.MutexAddress != MutexAddress)
  140. {
  141. OwnerThread = OwnerThread.NextMutexThread;
  142. }
  143. CurrThread.NextMutexThread = null;
  144. if (OwnerThread != null)
  145. {
  146. int HasListeners = OwnerThread.NextMutexThread != null ? MutexHasListenersMask : 0;
  147. Process.Memory.WriteInt32(MutexAddress, HasListeners | OwnerThread.WaitHandle);
  148. OwnerThread.WaitHandle = 0;
  149. OwnerThread.MutexAddress = 0;
  150. OwnerThread.CondVarAddress = 0;
  151. OwnerThread.MutexOwner = null;
  152. OwnerThread.UpdatePriority();
  153. Process.Scheduler.WakeUp(OwnerThread);
  154. return true;
  155. }
  156. else
  157. {
  158. Process.Memory.WriteInt32(MutexAddress, 0);
  159. return false;
  160. }
  161. }
  162. }
  163. private bool CondVarWait(
  164. KThread WaitThread,
  165. int WaitThreadHandle,
  166. long MutexAddress,
  167. long CondVarAddress,
  168. ulong Timeout)
  169. {
  170. WaitThread.WaitHandle = WaitThreadHandle;
  171. WaitThread.MutexAddress = MutexAddress;
  172. WaitThread.CondVarAddress = CondVarAddress;
  173. lock (CondVarLock)
  174. {
  175. KThread CurrThread = Process.ThreadArbiterList;
  176. if (CurrThread != null)
  177. {
  178. bool DoInsert = CurrThread != WaitThread;
  179. while (CurrThread.NextCondVarThread != null)
  180. {
  181. if (CurrThread.NextCondVarThread.ActualPriority < WaitThread.ActualPriority)
  182. {
  183. break;
  184. }
  185. CurrThread = CurrThread.NextCondVarThread;
  186. DoInsert &= CurrThread != WaitThread;
  187. }
  188. //Only insert if the node doesn't already exist in the list.
  189. //This prevents circular references.
  190. if (DoInsert)
  191. {
  192. if (WaitThread.NextCondVarThread != null)
  193. {
  194. throw new InvalidOperationException();
  195. }
  196. WaitThread.NextCondVarThread = CurrThread.NextCondVarThread;
  197. CurrThread.NextCondVarThread = WaitThread;
  198. }
  199. }
  200. else
  201. {
  202. Process.ThreadArbiterList = WaitThread;
  203. }
  204. }
  205. if (Timeout != ulong.MaxValue)
  206. {
  207. return Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
  208. }
  209. else
  210. {
  211. return Process.Scheduler.EnterWait(WaitThread);
  212. }
  213. }
  214. private void CondVarSignal(long CondVarAddress, int Count)
  215. {
  216. lock (CondVarLock)
  217. {
  218. KThread PrevThread = null;
  219. KThread CurrThread = Process.ThreadArbiterList;
  220. while (CurrThread != null && (Count == -1 || Count > 0))
  221. {
  222. if (CurrThread.CondVarAddress == CondVarAddress)
  223. {
  224. if (PrevThread != null)
  225. {
  226. PrevThread.NextCondVarThread = CurrThread.NextCondVarThread;
  227. }
  228. else
  229. {
  230. Process.ThreadArbiterList = CurrThread.NextCondVarThread;
  231. }
  232. CurrThread.NextCondVarThread = null;
  233. AcquireMutexValue(CurrThread.MutexAddress);
  234. int MutexValue = Process.Memory.ReadInt32(CurrThread.MutexAddress);
  235. MutexValue &= ~MutexHasListenersMask;
  236. if (MutexValue == 0)
  237. {
  238. //Give the lock to this thread.
  239. Process.Memory.WriteInt32(CurrThread.MutexAddress, CurrThread.WaitHandle);
  240. CurrThread.WaitHandle = 0;
  241. CurrThread.MutexAddress = 0;
  242. CurrThread.CondVarAddress = 0;
  243. CurrThread.MutexOwner = null;
  244. CurrThread.UpdatePriority();
  245. Process.Scheduler.WakeUp(CurrThread);
  246. }
  247. else
  248. {
  249. //Wait until the lock is released.
  250. InsertWaitingMutexThread(MutexValue, CurrThread);
  251. MutexValue |= MutexHasListenersMask;
  252. Process.Memory.WriteInt32(CurrThread.MutexAddress, MutexValue);
  253. }
  254. ReleaseMutexValue(CurrThread.MutexAddress);
  255. Count--;
  256. }
  257. PrevThread = CurrThread;
  258. CurrThread = CurrThread.NextCondVarThread;
  259. }
  260. }
  261. }
  262. private void InsertWaitingMutexThread(int OwnerThreadHandle, KThread WaitThread)
  263. {
  264. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  265. if (OwnerThread == null)
  266. {
  267. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
  268. return;
  269. }
  270. WaitThread.MutexOwner = OwnerThread;
  271. lock (OwnerThread)
  272. {
  273. KThread CurrThread = OwnerThread;
  274. while (CurrThread.NextMutexThread != null)
  275. {
  276. if (CurrThread == WaitThread)
  277. {
  278. return;
  279. }
  280. if (CurrThread.NextMutexThread.ActualPriority < WaitThread.ActualPriority)
  281. {
  282. break;
  283. }
  284. CurrThread = CurrThread.NextMutexThread;
  285. }
  286. if (CurrThread != WaitThread)
  287. {
  288. if (WaitThread.NextCondVarThread != null)
  289. {
  290. throw new InvalidOperationException();
  291. }
  292. WaitThread.NextMutexThread = CurrThread.NextMutexThread;
  293. CurrThread.NextMutexThread = WaitThread;
  294. }
  295. }
  296. OwnerThread.UpdatePriority();
  297. }
  298. private void AcquireMutexValue(long MutexAddress)
  299. {
  300. while (!Process.Memory.AcquireAddress(MutexAddress))
  301. {
  302. Thread.Yield();
  303. }
  304. }
  305. private void ReleaseMutexValue(long MutexAddress)
  306. {
  307. Process.Memory.ReleaseAddress(MutexAddress);
  308. }
  309. private bool IsPointingInsideKernel(long Address)
  310. {
  311. return ((ulong)Address + 0x1000000000) < 0xffffff000;
  312. }
  313. private bool IsWordAddressUnaligned(long Address)
  314. {
  315. return (Address & 3) != 0;
  316. }
  317. }
  318. }