SvcThreadSync.cs 15 KB

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