SvcThreadSync.cs 15 KB

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