SvcThreadSync.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using ChocolArm64.State;
  2. using Ryujinx.Core.Logging;
  3. using Ryujinx.Core.OsHle.Handles;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using static Ryujinx.Core.OsHle.ErrorCode;
  9. namespace Ryujinx.Core.OsHle.Kernel
  10. {
  11. partial class SvcHandler
  12. {
  13. private const int MutexHasListenersMask = 0x40000000;
  14. private void SvcArbitrateLock(AThreadState ThreadState)
  15. {
  16. int OwnerThreadHandle = (int)ThreadState.X0;
  17. long MutexAddress = (long)ThreadState.X1;
  18. int WaitThreadHandle = (int)ThreadState.X2;
  19. Ns.Log.PrintDebug(LogClass.KernelSvc,
  20. "OwnerThreadHandle = " + OwnerThreadHandle.ToString("x8") + ", " +
  21. "MutexAddress = " + MutexAddress .ToString("x16") + ", " +
  22. "WaitThreadHandle = " + WaitThreadHandle .ToString("x8"));
  23. if (IsPointingInsideKernel(MutexAddress))
  24. {
  25. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  26. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  27. return;
  28. }
  29. if (IsWordAddressUnaligned(MutexAddress))
  30. {
  31. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  32. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  33. return;
  34. }
  35. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  36. if (OwnerThread == null)
  37. {
  38. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid owner thread handle 0x{OwnerThreadHandle:x8}!");
  39. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  40. return;
  41. }
  42. KThread WaitThread = Process.HandleTable.GetData<KThread>(WaitThreadHandle);
  43. if (WaitThread == null)
  44. {
  45. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid requesting thread handle 0x{WaitThreadHandle:x8}!");
  46. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  47. return;
  48. }
  49. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  50. MutexLock(CurrThread, WaitThread, OwnerThreadHandle, WaitThreadHandle, MutexAddress);
  51. ThreadState.X0 = 0;
  52. }
  53. private void SvcArbitrateUnlock(AThreadState ThreadState)
  54. {
  55. long MutexAddress = (long)ThreadState.X0;
  56. Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexAddress = " + MutexAddress.ToString("x16"));
  57. if (IsPointingInsideKernel(MutexAddress))
  58. {
  59. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  60. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  61. return;
  62. }
  63. if (IsWordAddressUnaligned(MutexAddress))
  64. {
  65. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  66. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  67. return;
  68. }
  69. MutexUnlock(Process.GetThread(ThreadState.Tpidr), MutexAddress);
  70. ThreadState.X0 = 0;
  71. }
  72. private void SvcWaitProcessWideKeyAtomic(AThreadState ThreadState)
  73. {
  74. long MutexAddress = (long)ThreadState.X0;
  75. long CondVarAddress = (long)ThreadState.X1;
  76. int ThreadHandle = (int)ThreadState.X2;
  77. ulong Timeout = ThreadState.X3;
  78. Ns.Log.PrintDebug(LogClass.KernelSvc,
  79. "MutexAddress = " + MutexAddress .ToString("x16") + ", " +
  80. "CondVarAddress = " + CondVarAddress.ToString("x16") + ", " +
  81. "ThreadHandle = " + ThreadHandle .ToString("x8") + ", " +
  82. "Timeout = " + Timeout .ToString("x16"));
  83. if (IsPointingInsideKernel(MutexAddress))
  84. {
  85. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
  86. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
  87. return;
  88. }
  89. if (IsWordAddressUnaligned(MutexAddress))
  90. {
  91. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
  92. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
  93. return;
  94. }
  95. KThread Thread = Process.HandleTable.GetData<KThread>(ThreadHandle);
  96. if (Thread == null)
  97. {
  98. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{ThreadHandle:x8}!");
  99. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
  100. return;
  101. }
  102. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  103. MutexUnlock(CurrThread, MutexAddress);
  104. if (!CondVarWait(CurrThread, ThreadHandle, MutexAddress, CondVarAddress, Timeout))
  105. {
  106. ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
  107. return;
  108. }
  109. ThreadState.X0 = 0;
  110. }
  111. private void SvcSignalProcessWideKey(AThreadState ThreadState)
  112. {
  113. long CondVarAddress = (long)ThreadState.X0;
  114. int Count = (int)ThreadState.X1;
  115. Ns.Log.PrintDebug(LogClass.KernelSvc,
  116. "CondVarAddress = " + CondVarAddress.ToString("x16") + ", " +
  117. "Count = " + Count .ToString("x8"));
  118. KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
  119. CondVarSignal(CurrThread, CondVarAddress, Count);
  120. ThreadState.X0 = 0;
  121. }
  122. private void MutexLock(
  123. KThread CurrThread,
  124. KThread WaitThread,
  125. int OwnerThreadHandle,
  126. int WaitThreadHandle,
  127. long MutexAddress)
  128. {
  129. lock (Process.ThreadSyncLock)
  130. {
  131. int MutexValue = Process.Memory.ReadInt32(MutexAddress);
  132. Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + MutexValue.ToString("x8"));
  133. if (MutexValue != (OwnerThreadHandle | MutexHasListenersMask))
  134. {
  135. return;
  136. }
  137. CurrThread.WaitHandle = WaitThreadHandle;
  138. CurrThread.MutexAddress = MutexAddress;
  139. InsertWaitingMutexThread(OwnerThreadHandle, WaitThread);
  140. }
  141. Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  142. Process.Scheduler.EnterWait(CurrThread);
  143. }
  144. private void MutexUnlock(KThread CurrThread, long MutexAddress)
  145. {
  146. lock (Process.ThreadSyncLock)
  147. {
  148. //This is the new thread that will now own the mutex.
  149. //If no threads are waiting for the lock, then it should be null.
  150. KThread OwnerThread = PopThread(CurrThread.MutexWaiters, x => x.MutexAddress == MutexAddress);
  151. if (OwnerThread != null)
  152. {
  153. //Remove all waiting mutex from the old owner,
  154. //and insert then on the new owner.
  155. UpdateMutexOwner(CurrThread, OwnerThread, MutexAddress);
  156. CurrThread.UpdatePriority();
  157. int HasListeners = OwnerThread.MutexWaiters.Count > 0 ? MutexHasListenersMask : 0;
  158. Process.Memory.WriteInt32(MutexAddress, HasListeners | OwnerThread.WaitHandle);
  159. OwnerThread.WaitHandle = 0;
  160. OwnerThread.MutexAddress = 0;
  161. OwnerThread.CondVarAddress = 0;
  162. OwnerThread.MutexOwner = null;
  163. OwnerThread.UpdatePriority();
  164. Process.Scheduler.WakeUp(OwnerThread);
  165. Ns.Log.PrintDebug(LogClass.KernelSvc, "Gave mutex to thread id " + OwnerThread.ThreadId + "!");
  166. }
  167. else
  168. {
  169. Process.Memory.WriteInt32(MutexAddress, 0);
  170. Ns.Log.PrintDebug(LogClass.KernelSvc, "No threads waiting mutex!");
  171. }
  172. }
  173. }
  174. private bool CondVarWait(
  175. KThread WaitThread,
  176. int WaitThreadHandle,
  177. long MutexAddress,
  178. long CondVarAddress,
  179. ulong Timeout)
  180. {
  181. WaitThread.WaitHandle = WaitThreadHandle;
  182. WaitThread.MutexAddress = MutexAddress;
  183. WaitThread.CondVarAddress = CondVarAddress;
  184. lock (Process.ThreadSyncLock)
  185. {
  186. WaitThread.CondVarSignaled = false;
  187. Process.ThreadArbiterList.Add(WaitThread);
  188. }
  189. Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  190. if (Timeout != ulong.MaxValue)
  191. {
  192. Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
  193. lock (Process.ThreadSyncLock)
  194. {
  195. WaitThread.MutexOwner?.MutexWaiters.Remove(WaitThread);
  196. if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null)
  197. {
  198. WaitThread.MutexOwner = null;
  199. Process.ThreadArbiterList.Remove(WaitThread);
  200. Ns.Log.PrintDebug(LogClass.KernelSvc, "Timed out...");
  201. return false;
  202. }
  203. }
  204. }
  205. else
  206. {
  207. Process.Scheduler.EnterWait(WaitThread);
  208. }
  209. return true;
  210. }
  211. private void CondVarSignal(KThread CurrThread, long CondVarAddress, int Count)
  212. {
  213. lock (Process.ThreadSyncLock)
  214. {
  215. while (Count == -1 || Count-- > 0)
  216. {
  217. KThread WaitThread = PopThread(Process.ThreadArbiterList, x => x.CondVarAddress == CondVarAddress);
  218. if (WaitThread == null)
  219. {
  220. Ns.Log.PrintDebug(LogClass.KernelSvc, "No more threads to wake up!");
  221. break;
  222. }
  223. WaitThread.CondVarSignaled = true;
  224. AcquireMutexValue(WaitThread.MutexAddress);
  225. int MutexValue = Process.Memory.ReadInt32(WaitThread.MutexAddress);
  226. Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + MutexValue.ToString("x8"));
  227. if (MutexValue == 0)
  228. {
  229. //Give the lock to this thread.
  230. Process.Memory.WriteInt32(WaitThread.MutexAddress, WaitThread.WaitHandle);
  231. WaitThread.WaitHandle = 0;
  232. WaitThread.MutexAddress = 0;
  233. WaitThread.CondVarAddress = 0;
  234. WaitThread.MutexOwner?.UpdatePriority();
  235. WaitThread.MutexOwner = null;
  236. Process.Scheduler.WakeUp(WaitThread);
  237. }
  238. else
  239. {
  240. //Wait until the lock is released.
  241. MutexValue &= ~MutexHasListenersMask;
  242. InsertWaitingMutexThread(MutexValue, WaitThread);
  243. MutexValue |= MutexHasListenersMask;
  244. Process.Memory.WriteInt32(WaitThread.MutexAddress, MutexValue);
  245. }
  246. ReleaseMutexValue(WaitThread.MutexAddress);
  247. }
  248. }
  249. }
  250. private void UpdateMutexOwner(KThread CurrThread, KThread NewOwner, long MutexAddress)
  251. {
  252. //Go through all threads waiting for the mutex,
  253. //and update the MutexOwner field to point to the new owner.
  254. lock (Process.ThreadSyncLock)
  255. {
  256. for (int Index = 0; Index < CurrThread.MutexWaiters.Count; Index++)
  257. {
  258. KThread Thread = CurrThread.MutexWaiters[Index];
  259. if (Thread.MutexAddress == MutexAddress)
  260. {
  261. CurrThread.MutexWaiters.RemoveAt(Index--);
  262. Thread.MutexOwner = NewOwner;
  263. InsertWaitingMutexThread(NewOwner, Thread);
  264. }
  265. }
  266. }
  267. }
  268. private void InsertWaitingMutexThread(int OwnerThreadHandle, KThread WaitThread)
  269. {
  270. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  271. if (OwnerThread == null)
  272. {
  273. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
  274. return;
  275. }
  276. InsertWaitingMutexThread(OwnerThread, WaitThread);
  277. }
  278. private void InsertWaitingMutexThread(KThread OwnerThread, KThread WaitThread)
  279. {
  280. lock (Process.ThreadSyncLock)
  281. {
  282. WaitThread.MutexOwner = OwnerThread;
  283. if (!OwnerThread.MutexWaiters.Contains(WaitThread))
  284. {
  285. OwnerThread.MutexWaiters.Add(WaitThread);
  286. OwnerThread.UpdatePriority();
  287. }
  288. }
  289. }
  290. private KThread PopThread(List<KThread> Threads, Func<KThread, bool> Predicate)
  291. {
  292. KThread Thread = Threads.OrderBy(x => x.ActualPriority).FirstOrDefault(Predicate);
  293. if (Thread != null)
  294. {
  295. Threads.Remove(Thread);
  296. }
  297. return Thread;
  298. }
  299. private void AcquireMutexValue(long MutexAddress)
  300. {
  301. while (!Process.Memory.AcquireAddress(MutexAddress))
  302. {
  303. Thread.Yield();
  304. }
  305. }
  306. private void ReleaseMutexValue(long MutexAddress)
  307. {
  308. Process.Memory.ReleaseAddress(MutexAddress);
  309. }
  310. private bool IsPointingInsideKernel(long Address)
  311. {
  312. return ((ulong)Address + 0x1000000000) < 0xffffff000;
  313. }
  314. private bool IsWordAddressUnaligned(long Address)
  315. {
  316. return (Address & 3) != 0;
  317. }
  318. }
  319. }