SvcThreadSync.cs 14 KB

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