SvcThreadSync.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  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 (Process.ThreadArbiterListLock)
  188. {
  189. KThread CurrThread = Process.ThreadArbiterListHead;
  190. if (CurrThread == null || CurrThread.ActualPriority > WaitThread.ActualPriority)
  191. {
  192. WaitThread.NextCondVarThread = Process.ThreadArbiterListHead;
  193. Process.ThreadArbiterListHead = WaitThread;
  194. }
  195. else
  196. {
  197. bool DoInsert = CurrThread != WaitThread;
  198. while (CurrThread.NextCondVarThread != null)
  199. {
  200. if (CurrThread.NextCondVarThread.ActualPriority > WaitThread.ActualPriority)
  201. {
  202. break;
  203. }
  204. CurrThread = CurrThread.NextCondVarThread;
  205. DoInsert &= CurrThread != WaitThread;
  206. }
  207. //Only insert if the node doesn't already exist in the list.
  208. //This prevents circular references.
  209. if (DoInsert)
  210. {
  211. if (WaitThread.NextCondVarThread != null)
  212. {
  213. throw new InvalidOperationException();
  214. }
  215. WaitThread.NextCondVarThread = CurrThread.NextCondVarThread;
  216. CurrThread.NextCondVarThread = WaitThread;
  217. }
  218. }
  219. }
  220. Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  221. if (Timeout != ulong.MaxValue)
  222. {
  223. return Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
  224. }
  225. else
  226. {
  227. Process.Scheduler.EnterWait(WaitThread);
  228. return true;
  229. }
  230. }
  231. private void CondVarSignal(long CondVarAddress, int Count)
  232. {
  233. lock (Process.ThreadArbiterListLock)
  234. {
  235. KThread PrevThread = null;
  236. KThread CurrThread = Process.ThreadArbiterListHead;
  237. while (CurrThread != null && (Count == -1 || Count > 0))
  238. {
  239. if (CurrThread.CondVarAddress == CondVarAddress)
  240. {
  241. if (PrevThread != null)
  242. {
  243. PrevThread.NextCondVarThread = CurrThread.NextCondVarThread;
  244. }
  245. else
  246. {
  247. Process.ThreadArbiterListHead = CurrThread.NextCondVarThread;
  248. }
  249. CurrThread.NextCondVarThread = null;
  250. AcquireMutexValue(CurrThread.MutexAddress);
  251. int MutexValue = Process.Memory.ReadInt32(CurrThread.MutexAddress);
  252. if (MutexValue == 0)
  253. {
  254. //Give the lock to this thread.
  255. Process.Memory.WriteInt32(CurrThread.MutexAddress, CurrThread.WaitHandle);
  256. CurrThread.WaitHandle = 0;
  257. CurrThread.MutexAddress = 0;
  258. CurrThread.CondVarAddress = 0;
  259. CurrThread.MutexOwner?.UpdatePriority();
  260. CurrThread.MutexOwner = null;
  261. Process.Scheduler.WakeUp(CurrThread);
  262. }
  263. else
  264. {
  265. //Wait until the lock is released.
  266. MutexValue &= ~MutexHasListenersMask;
  267. InsertWaitingMutexThread(MutexValue, CurrThread);
  268. MutexValue |= MutexHasListenersMask;
  269. Process.Memory.WriteInt32(CurrThread.MutexAddress, MutexValue);
  270. }
  271. ReleaseMutexValue(CurrThread.MutexAddress);
  272. Count--;
  273. }
  274. PrevThread = CurrThread;
  275. CurrThread = CurrThread.NextCondVarThread;
  276. }
  277. }
  278. }
  279. private void InsertWaitingMutexThread(int OwnerThreadHandle, KThread WaitThread)
  280. {
  281. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  282. if (OwnerThread == null)
  283. {
  284. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
  285. return;
  286. }
  287. WaitThread.MutexOwner = OwnerThread;
  288. lock (OwnerThread)
  289. {
  290. KThread CurrThread = OwnerThread;
  291. while (CurrThread.NextMutexThread != null)
  292. {
  293. if (CurrThread == WaitThread)
  294. {
  295. return;
  296. }
  297. if (CurrThread.NextMutexThread.ActualPriority > WaitThread.ActualPriority)
  298. {
  299. break;
  300. }
  301. CurrThread = CurrThread.NextMutexThread;
  302. }
  303. if (CurrThread != WaitThread)
  304. {
  305. if (WaitThread.NextMutexThread != null)
  306. {
  307. throw new InvalidOperationException();
  308. }
  309. WaitThread.NextMutexThread = CurrThread.NextMutexThread;
  310. CurrThread.NextMutexThread = WaitThread;
  311. }
  312. }
  313. OwnerThread.UpdatePriority();
  314. }
  315. private void UpdateMutexOwner(KThread CurrThread, KThread NewOwner, long MutexAddress)
  316. {
  317. //Go through all threads waiting for the mutex,
  318. //and update the MutexOwner field to point to the new owner.
  319. CurrThread = CurrThread.NextMutexThread;
  320. while (CurrThread != null)
  321. {
  322. if (CurrThread.MutexAddress == MutexAddress)
  323. {
  324. CurrThread.MutexOwner = NewOwner;
  325. }
  326. CurrThread = CurrThread.NextMutexThread;
  327. }
  328. }
  329. private void AcquireMutexValue(long MutexAddress)
  330. {
  331. while (!Process.Memory.AcquireAddress(MutexAddress))
  332. {
  333. Thread.Yield();
  334. }
  335. }
  336. private void ReleaseMutexValue(long MutexAddress)
  337. {
  338. Process.Memory.ReleaseAddress(MutexAddress);
  339. }
  340. private bool IsPointingInsideKernel(long Address)
  341. {
  342. return ((ulong)Address + 0x1000000000) < 0xffffff000;
  343. }
  344. private bool IsWordAddressUnaligned(long Address)
  345. {
  346. return (Address & 3) != 0;
  347. }
  348. }
  349. }