SvcThreadSync.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 not 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. OwnerThread.UpdatePriority();
  158. int HasListeners = OwnerThread.MutexWaiters.Count > 0 ? MutexHasListenersMask : 0;
  159. Process.Memory.WriteInt32(MutexAddress, HasListeners | OwnerThread.WaitHandle);
  160. OwnerThread.WaitHandle = 0;
  161. OwnerThread.MutexAddress = 0;
  162. OwnerThread.CondVarAddress = 0;
  163. OwnerThread.MutexOwner = null;
  164. OwnerThread.UpdatePriority();
  165. Process.Scheduler.WakeUp(OwnerThread);
  166. Ns.Log.PrintDebug(LogClass.KernelSvc, "Gave mutex to thread id " + OwnerThread.ThreadId + "!");
  167. }
  168. else
  169. {
  170. Process.Memory.WriteInt32(MutexAddress, 0);
  171. Ns.Log.PrintDebug(LogClass.KernelSvc, "No threads waiting mutex!");
  172. }
  173. }
  174. }
  175. private bool CondVarWait(
  176. KThread WaitThread,
  177. int WaitThreadHandle,
  178. long MutexAddress,
  179. long CondVarAddress,
  180. ulong Timeout)
  181. {
  182. WaitThread.WaitHandle = WaitThreadHandle;
  183. WaitThread.MutexAddress = MutexAddress;
  184. WaitThread.CondVarAddress = CondVarAddress;
  185. lock (Process.ThreadArbiterList)
  186. {
  187. WaitThread.CondVarSignaled = false;
  188. Process.ThreadArbiterList.Add(WaitThread);
  189. }
  190. Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
  191. if (Timeout != ulong.MaxValue)
  192. {
  193. Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
  194. lock (Process.ThreadArbiterList)
  195. {
  196. if (!WaitThread.CondVarSignaled)
  197. {
  198. Process.ThreadArbiterList.Remove(WaitThread);
  199. return false;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. Process.Scheduler.EnterWait(WaitThread);
  206. }
  207. return true;
  208. }
  209. private void CondVarSignal(KThread CurrThread, long CondVarAddress, int Count)
  210. {
  211. lock (Process.ThreadArbiterList)
  212. {
  213. while (Count == -1 || Count-- > 0)
  214. {
  215. KThread WaitThread = PopThread(Process.ThreadArbiterList, x => x.CondVarAddress == CondVarAddress);
  216. if (WaitThread == null)
  217. {
  218. Ns.Log.PrintDebug(LogClass.KernelSvc, "No more threads to wake up!");
  219. break;
  220. }
  221. WaitThread.CondVarSignaled = true;
  222. AcquireMutexValue(WaitThread.MutexAddress);
  223. int MutexValue = Process.Memory.ReadInt32(WaitThread.MutexAddress);
  224. Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + MutexValue.ToString("x8"));
  225. if (MutexValue == 0)
  226. {
  227. //Give the lock to this thread.
  228. Process.Memory.WriteInt32(WaitThread.MutexAddress, WaitThread.WaitHandle);
  229. WaitThread.WaitHandle = 0;
  230. WaitThread.MutexAddress = 0;
  231. WaitThread.CondVarAddress = 0;
  232. WaitThread.MutexOwner?.UpdatePriority();
  233. WaitThread.MutexOwner = null;
  234. Process.Scheduler.WakeUp(WaitThread);
  235. }
  236. else
  237. {
  238. //Wait until the lock is released.
  239. MutexValue &= ~MutexHasListenersMask;
  240. InsertWaitingMutexThread(MutexValue, WaitThread);
  241. MutexValue |= MutexHasListenersMask;
  242. Process.Memory.WriteInt32(WaitThread.MutexAddress, MutexValue);
  243. }
  244. ReleaseMutexValue(WaitThread.MutexAddress);
  245. }
  246. }
  247. }
  248. private void UpdateMutexOwner(KThread CurrThread, KThread NewOwner, long MutexAddress)
  249. {
  250. //Go through all threads waiting for the mutex,
  251. //and update the MutexOwner field to point to the new owner.
  252. lock (Process.ThreadSyncLock)
  253. {
  254. for (int Index = 0; Index < CurrThread.MutexWaiters.Count; Index++)
  255. {
  256. KThread Thread = CurrThread.MutexWaiters[Index];
  257. if (Thread.MutexAddress == MutexAddress)
  258. {
  259. CurrThread.MutexWaiters.RemoveAt(Index--);
  260. Thread.MutexOwner = NewOwner;
  261. InsertWaitingMutexThread(NewOwner, Thread);
  262. }
  263. }
  264. }
  265. }
  266. private void InsertWaitingMutexThread(int OwnerThreadHandle, KThread WaitThread)
  267. {
  268. KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
  269. if (OwnerThread == null)
  270. {
  271. Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
  272. return;
  273. }
  274. InsertWaitingMutexThread(OwnerThread, WaitThread);
  275. }
  276. private void InsertWaitingMutexThread(KThread OwnerThread, KThread WaitThread)
  277. {
  278. lock (Process.ThreadSyncLock)
  279. {
  280. WaitThread.MutexOwner = OwnerThread;
  281. if (!OwnerThread.MutexWaiters.Contains(WaitThread))
  282. {
  283. OwnerThread.MutexWaiters.Add(WaitThread);
  284. OwnerThread.UpdatePriority();
  285. }
  286. }
  287. }
  288. private KThread PopThread(List<KThread> Threads, Func<KThread, bool> Predicate)
  289. {
  290. KThread Thread = Threads.OrderBy(x => x.ActualPriority).FirstOrDefault(Predicate);
  291. if (Thread != null)
  292. {
  293. Threads.Remove(Thread);
  294. }
  295. return Thread;
  296. }
  297. private void AcquireMutexValue(long MutexAddress)
  298. {
  299. while (!Process.Memory.AcquireAddress(MutexAddress))
  300. {
  301. Thread.Yield();
  302. }
  303. }
  304. private void ReleaseMutexValue(long MutexAddress)
  305. {
  306. Process.Memory.ReleaseAddress(MutexAddress);
  307. }
  308. private bool IsPointingInsideKernel(long Address)
  309. {
  310. return ((ulong)Address + 0x1000000000) < 0xffffff000;
  311. }
  312. private bool IsWordAddressUnaligned(long Address)
  313. {
  314. return (Address & 3) != 0;
  315. }
  316. }
  317. }