SvcThreadSync.cs 13 KB

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