SvcThreadSync.cs 15 KB

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