SvcThreadSync.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using ChocolArm64.State;
  2. using Ryujinx.HLE.Logging;
  3. using System;
  4. using static Ryujinx.HLE.HOS.ErrorCode;
  5. namespace Ryujinx.HLE.HOS.Kernel
  6. {
  7. partial class SvcHandler
  8. {
  9. private const int MutexHasListenersMask = 0x40000000;
  10. private void SvcArbitrateLock(AThreadState ThreadState)
  11. {
  12. int OwnerThreadHandle = (int)ThreadState.X0;
  13. long MutexAddress = (long)ThreadState.X1;
  14. int WaitThreadHandle = (int)ThreadState.X2;
  15. Device.Log.PrintDebug(LogClass.KernelSvc,
  16. "OwnerThreadHandle = 0x" + OwnerThreadHandle.ToString("x8") + ", " +
  17. "MutexAddress = 0x" + MutexAddress .ToString("x16") + ", " +
  18. "WaitThreadHandle = 0x" + WaitThreadHandle .ToString("x8"));
  19. if (IsPointingInsideKernel(MutexAddress))
  20. {
  21. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  22. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
  23. return;
  24. }
  25. if (IsAddressNotWordAligned(MutexAddress))
  26. {
  27. Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  28. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  29. return;
  30. }
  31. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  32. if (OwnerThread == null)
  33. {
  34. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid owner thread handle 0x{OwnerThreadHandle:x8}!");
  35. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  36. return;
  37. }
  38. KThread WaitThread = Process.HandleTable.GetData<KThread>(WaitThreadHandle);
  39. if (WaitThread == null)
  40. {
  41. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid requesting thread handle 0x{WaitThreadHandle:x8}!");
  42. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  43. return;
  44. }
  45. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  46. MutexLock(CurrThread, WaitThread, OwnerThreadHandle, WaitThreadHandle, MutexAddress);
  47. ThreadState.X0 = 0;
  48. }
  49. private void SvcArbitrateUnlock(AThreadState ThreadState)
  50. {
  51. long MutexAddress = (long)ThreadState.X0;
  52. Device.Log.PrintDebug(LogClass.KernelSvc, "MutexAddress = 0x" + MutexAddress.ToString("x16"));
  53. if (IsPointingInsideKernel(MutexAddress))
  54. {
  55. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  56. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
  57. return;
  58. }
  59. if (IsAddressNotWordAligned(MutexAddress))
  60. {
  61. Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  62. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  63. return;
  64. }
  65. MutexUnlock(Process.GetThread(ThreadState.Tpidr), MutexAddress);
  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. Device.Log.PrintDebug(LogClass.KernelSvc,
  75. "MutexAddress = 0x" + MutexAddress .ToString("x16") + ", " +
  76. "CondVarAddress = 0x" + CondVarAddress.ToString("x16") + ", " +
  77. "ThreadHandle = 0x" + ThreadHandle .ToString("x8") + ", " +
  78. "Timeout = 0x" + Timeout .ToString("x16"));
  79. if (IsPointingInsideKernel(MutexAddress))
  80. {
  81. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  82. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
  83. return;
  84. }
  85. if (IsAddressNotWordAligned(MutexAddress))
  86. {
  87. Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  88. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  89. return;
  90. }
  91. KThread Thread = Process.HandleTable.GetData<KThread>(ThreadHandle);
  92. if (Thread == null)
  93. {
  94. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{ThreadHandle:x8}!");
  95. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  96. return;
  97. }
  98. KThread WaitThread = Process.GetThread(ThreadState.Tpidr);
  99. if (!CondVarWait(WaitThread, ThreadHandle, MutexAddress, CondVarAddress, Timeout))
  100. {
  101. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
  102. return;
  103. }
  104. ThreadState.X0 = 0;
  105. }
  106. private void SvcSignalProcessWideKey(AThreadState ThreadState)
  107. {
  108. long CondVarAddress = (long)ThreadState.X0;
  109. int Count = (int)ThreadState.X1;
  110. Device.Log.PrintDebug(LogClass.KernelSvc,
  111. "CondVarAddress = 0x" + CondVarAddress.ToString("x16") + ", " +
  112. "Count = 0x" + Count .ToString("x8"));
  113. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  114. CondVarSignal(ThreadState, CurrThread, CondVarAddress, Count);
  115. ThreadState.X0 = 0;
  116. }
  117. private void MutexLock(
  118. KThread CurrThread,
  119. KThread WaitThread,
  120. int OwnerThreadHandle,
  121. int WaitThreadHandle,
  122. long MutexAddress)
  123. {
  124. lock (Process.ThreadSyncLock)
  125. {
  126. int MutexValue = Memory.ReadInt32(MutexAddress);
  127. Device.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = 0x" + MutexValue.ToString("x8"));
  128. if (MutexValue != (OwnerThreadHandle | MutexHasListenersMask))
  129. {
  130. return;
  131. }
  132. CurrThread.WaitHandle = WaitThreadHandle;
  133. CurrThread.MutexAddress = MutexAddress;
  134. InsertWaitingMutexThreadUnsafe(OwnerThreadHandle, WaitThread);
  135. }
  136. Device.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  137. Process.Scheduler.EnterWait(CurrThread);
  138. }
  139. private void SvcWaitForAddress(AThreadState ThreadState)
  140. {
  141. long Address = (long)ThreadState.X0;
  142. ArbitrationType Type = (ArbitrationType)ThreadState.X1;
  143. int Value = (int)ThreadState.X2;
  144. ulong Timeout = ThreadState.X3;
  145. Device.Log.PrintDebug(LogClass.KernelSvc,
  146. "Address = 0x" + Address.ToString("x16") + ", " +
  147. "ArbitrationType = 0x" + Type .ToString() + ", " +
  148. "Value = 0x" + Value .ToString("x8") + ", " +
  149. "Timeout = 0x" + Timeout.ToString("x16"));
  150. if (IsPointingInsideKernel(Address))
  151. {
  152. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address 0x{Address:x16}!");
  153. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
  154. return;
  155. }
  156. if (IsAddressNotWordAligned(Address))
  157. {
  158. Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned address 0x{Address:x16}!");
  159. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  160. return;
  161. }
  162. switch (Type)
  163. {
  164. case ArbitrationType.WaitIfLessThan:
  165. ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, false);
  166. break;
  167. case ArbitrationType.DecrementAndWaitIfLessThan:
  168. ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, true);
  169. break;
  170. case ArbitrationType.WaitIfEqual:
  171. ThreadState.X0 = AddressArbiter.WaitForAddressIfEqual(Process, ThreadState, Memory, Address, Value, Timeout);
  172. break;
  173. default:
  174. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
  175. break;
  176. }
  177. }
  178. private void MutexUnlock(KThread CurrThread, long MutexAddress)
  179. {
  180. lock (Process.ThreadSyncLock)
  181. {
  182. //This is the new thread that will now own the mutex.
  183. //If no threads are waiting for the lock, then it should be null.
  184. (KThread OwnerThread, int Count) = PopMutexThreadUnsafe(CurrThread, MutexAddress);
  185. if (OwnerThread == CurrThread)
  186. {
  187. throw new InvalidOperationException();
  188. }
  189. if (OwnerThread != null)
  190. {
  191. //Remove all waiting mutex from the old owner,
  192. //and insert then on the new owner.
  193. UpdateMutexOwnerUnsafe(CurrThread, OwnerThread, MutexAddress);
  194. CurrThread.UpdatePriority();
  195. int HasListeners = Count >= 2 ? MutexHasListenersMask : 0;
  196. Memory.WriteInt32ToSharedAddr(MutexAddress, HasListeners | OwnerThread.WaitHandle);
  197. OwnerThread.WaitHandle = 0;
  198. OwnerThread.MutexAddress = 0;
  199. OwnerThread.CondVarAddress = 0;
  200. OwnerThread.MutexOwner = null;
  201. OwnerThread.UpdatePriority();
  202. Process.Scheduler.WakeUp(OwnerThread);
  203. Device.Log.PrintDebug(LogClass.KernelSvc, "Gave mutex to thread id " + OwnerThread.ThreadId + "!");
  204. }
  205. else
  206. {
  207. Memory.WriteInt32ToSharedAddr(MutexAddress, 0);
  208. Device.Log.PrintDebug(LogClass.KernelSvc, "No threads waiting mutex!");
  209. }
  210. }
  211. }
  212. private bool CondVarWait(
  213. KThread WaitThread,
  214. int WaitThreadHandle,
  215. long MutexAddress,
  216. long CondVarAddress,
  217. ulong Timeout)
  218. {
  219. WaitThread.WaitHandle = WaitThreadHandle;
  220. WaitThread.MutexAddress = MutexAddress;
  221. WaitThread.CondVarAddress = CondVarAddress;
  222. lock (Process.ThreadSyncLock)
  223. {
  224. MutexUnlock(WaitThread, MutexAddress);
  225. WaitThread.CondVarSignaled = false;
  226. Process.ThreadArbiterList.Add(WaitThread);
  227. }
  228. Device.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  229. if (Timeout != ulong.MaxValue)
  230. {
  231. Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
  232. lock (Process.ThreadSyncLock)
  233. {
  234. if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null)
  235. {
  236. if (WaitThread.MutexOwner != null)
  237. {
  238. WaitThread.MutexOwner.MutexWaiters.Remove(WaitThread);
  239. WaitThread.MutexOwner.UpdatePriority();
  240. WaitThread.MutexOwner = null;
  241. }
  242. Process.ThreadArbiterList.Remove(WaitThread);
  243. Device.Log.PrintDebug(LogClass.KernelSvc, "Timed out...");
  244. return false;
  245. }
  246. }
  247. }
  248. else
  249. {
  250. Process.Scheduler.EnterWait(WaitThread);
  251. }
  252. return true;
  253. }
  254. private void CondVarSignal(
  255. AThreadState ThreadState,
  256. KThread CurrThread,
  257. long CondVarAddress,
  258. int Count)
  259. {
  260. lock (Process.ThreadSyncLock)
  261. {
  262. while (Count == -1 || Count-- > 0)
  263. {
  264. KThread WaitThread = PopCondVarThreadUnsafe(CondVarAddress);
  265. if (WaitThread == null)
  266. {
  267. Device.Log.PrintDebug(LogClass.KernelSvc, "No more threads to wake up!");
  268. break;
  269. }
  270. WaitThread.CondVarSignaled = true;
  271. long MutexAddress = WaitThread.MutexAddress;
  272. Memory.SetExclusive(ThreadState, MutexAddress);
  273. int MutexValue = Memory.ReadInt32(MutexAddress);
  274. while (MutexValue != 0)
  275. {
  276. if (Memory.TestExclusive(ThreadState, MutexAddress))
  277. {
  278. //Wait until the lock is released.
  279. InsertWaitingMutexThreadUnsafe(MutexValue & ~MutexHasListenersMask, WaitThread);
  280. Memory.WriteInt32(MutexAddress, MutexValue | MutexHasListenersMask);
  281. Memory.ClearExclusiveForStore(ThreadState);
  282. break;
  283. }
  284. Memory.SetExclusive(ThreadState, MutexAddress);
  285. MutexValue = Memory.ReadInt32(MutexAddress);
  286. }
  287. Device.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = 0x" + MutexValue.ToString("x8"));
  288. if (MutexValue == 0)
  289. {
  290. //Give the lock to this thread.
  291. Memory.WriteInt32ToSharedAddr(MutexAddress, WaitThread.WaitHandle);
  292. WaitThread.WaitHandle = 0;
  293. WaitThread.MutexAddress = 0;
  294. WaitThread.CondVarAddress = 0;
  295. WaitThread.MutexOwner?.UpdatePriority();
  296. WaitThread.MutexOwner = null;
  297. Process.Scheduler.WakeUp(WaitThread);
  298. }
  299. }
  300. }
  301. }
  302. private void UpdateMutexOwnerUnsafe(KThread CurrThread, KThread NewOwner, long MutexAddress)
  303. {
  304. //Go through all threads waiting for the mutex,
  305. //and update the MutexOwner field to point to the new owner.
  306. for (int Index = 0; Index < CurrThread.MutexWaiters.Count; Index++)
  307. {
  308. KThread Thread = CurrThread.MutexWaiters[Index];
  309. if (Thread.MutexAddress == MutexAddress)
  310. {
  311. CurrThread.MutexWaiters.RemoveAt(Index--);
  312. InsertWaitingMutexThreadUnsafe(NewOwner, Thread);
  313. }
  314. }
  315. }
  316. private void InsertWaitingMutexThreadUnsafe(int OwnerThreadHandle, KThread WaitThread)
  317. {
  318. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  319. if (OwnerThread == null)
  320. {
  321. Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
  322. return;
  323. }
  324. InsertWaitingMutexThreadUnsafe(OwnerThread, WaitThread);
  325. }
  326. private void InsertWaitingMutexThreadUnsafe(KThread OwnerThread, KThread WaitThread)
  327. {
  328. WaitThread.MutexOwner = OwnerThread;
  329. if (!OwnerThread.MutexWaiters.Contains(WaitThread))
  330. {
  331. OwnerThread.MutexWaiters.Add(WaitThread);
  332. OwnerThread.UpdatePriority();
  333. }
  334. }
  335. private (KThread, int) PopMutexThreadUnsafe(KThread OwnerThread, long MutexAddress)
  336. {
  337. int Count = 0;
  338. KThread WakeThread = null;
  339. foreach (KThread Thread in OwnerThread.MutexWaiters)
  340. {
  341. if (Thread.MutexAddress != MutexAddress)
  342. {
  343. continue;
  344. }
  345. if (WakeThread == null || Thread.ActualPriority < WakeThread.ActualPriority)
  346. {
  347. WakeThread = Thread;
  348. }
  349. Count++;
  350. }
  351. if (WakeThread != null)
  352. {
  353. OwnerThread.MutexWaiters.Remove(WakeThread);
  354. }
  355. return (WakeThread, Count);
  356. }
  357. private KThread PopCondVarThreadUnsafe(long CondVarAddress)
  358. {
  359. KThread WakeThread = null;
  360. foreach (KThread Thread in Process.ThreadArbiterList)
  361. {
  362. if (Thread.CondVarAddress != CondVarAddress)
  363. {
  364. continue;
  365. }
  366. if (WakeThread == null || Thread.ActualPriority < WakeThread.ActualPriority)
  367. {
  368. WakeThread = Thread;
  369. }
  370. }
  371. if (WakeThread != null)
  372. {
  373. Process.ThreadArbiterList.Remove(WakeThread);
  374. }
  375. return WakeThread;
  376. }
  377. private bool IsPointingInsideKernel(long Address)
  378. {
  379. return ((ulong)Address + 0x1000000000) < 0xffffff000;
  380. }
  381. private bool IsAddressNotWordAligned(long Address)
  382. {
  383. return (Address & 3) != 0;
  384. }
  385. }
  386. }