SvcSystem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using ARMeilleure.Memory;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.HLE.Exceptions;
  5. using Ryujinx.HLE.HOS.Kernel.Common;
  6. using Ryujinx.HLE.HOS.Kernel.Ipc;
  7. using Ryujinx.HLE.HOS.Kernel.Memory;
  8. using Ryujinx.HLE.HOS.Kernel.Process;
  9. using Ryujinx.HLE.HOS.Kernel.Threading;
  10. namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
  11. {
  12. partial class SvcHandler
  13. {
  14. public void ExitProcess64()
  15. {
  16. ExitProcess();
  17. }
  18. private void ExitProcess()
  19. {
  20. _system.Scheduler.GetCurrentProcess().Terminate();
  21. }
  22. public KernelResult SignalEvent64(int handle)
  23. {
  24. return SignalEvent(handle);
  25. }
  26. private KernelResult SignalEvent(int handle)
  27. {
  28. KWritableEvent writableEvent = _process.HandleTable.GetObject<KWritableEvent>(handle);
  29. KernelResult result;
  30. if (writableEvent != null)
  31. {
  32. writableEvent.Signal();
  33. result = KernelResult.Success;
  34. }
  35. else
  36. {
  37. result = KernelResult.InvalidHandle;
  38. }
  39. return result;
  40. }
  41. public KernelResult ClearEvent64(int handle)
  42. {
  43. return ClearEvent(handle);
  44. }
  45. private KernelResult ClearEvent(int handle)
  46. {
  47. KernelResult result;
  48. KWritableEvent writableEvent = _process.HandleTable.GetObject<KWritableEvent>(handle);
  49. if (writableEvent == null)
  50. {
  51. KReadableEvent readableEvent = _process.HandleTable.GetObject<KReadableEvent>(handle);
  52. result = readableEvent?.Clear() ?? KernelResult.InvalidHandle;
  53. }
  54. else
  55. {
  56. result = writableEvent.Clear();
  57. }
  58. return result;
  59. }
  60. public KernelResult CloseHandle64(int handle)
  61. {
  62. return CloseHandle(handle);
  63. }
  64. private KernelResult CloseHandle(int handle)
  65. {
  66. KAutoObject obj = _process.HandleTable.GetObject<KAutoObject>(handle);
  67. _process.HandleTable.CloseHandle(handle);
  68. if (obj == null)
  69. {
  70. return KernelResult.InvalidHandle;
  71. }
  72. if (obj is KSession session)
  73. {
  74. session.Dispose();
  75. }
  76. else if (obj is KTransferMemory transferMemory)
  77. {
  78. _process.MemoryManager.ResetTransferMemory(
  79. transferMemory.Address,
  80. transferMemory.Size);
  81. }
  82. return KernelResult.Success;
  83. }
  84. public KernelResult ResetSignal64(int handle)
  85. {
  86. return ResetSignal(handle);
  87. }
  88. private KernelResult ResetSignal(int handle)
  89. {
  90. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  91. KReadableEvent readableEvent = currentProcess.HandleTable.GetObject<KReadableEvent>(handle);
  92. KernelResult result;
  93. if (readableEvent != null)
  94. {
  95. result = readableEvent.ClearIfSignaled();
  96. }
  97. else
  98. {
  99. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  100. if (process != null)
  101. {
  102. result = process.ClearIfNotExited();
  103. }
  104. else
  105. {
  106. result = KernelResult.InvalidHandle;
  107. }
  108. }
  109. return result;
  110. }
  111. public ulong GetSystemTick64()
  112. {
  113. return _system.Scheduler.GetCurrentThread().Context.CntpctEl0;
  114. }
  115. public KernelResult GetProcessId64(int handle, out long pid)
  116. {
  117. return GetProcessId(handle, out pid);
  118. }
  119. private KernelResult GetProcessId(int handle, out long pid)
  120. {
  121. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  122. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  123. if (process == null)
  124. {
  125. KThread thread = currentProcess.HandleTable.GetKThread(handle);
  126. if (thread != null)
  127. {
  128. process = thread.Owner;
  129. }
  130. // TODO: KDebugEvent.
  131. }
  132. pid = process?.Pid ?? 0;
  133. return process != null
  134. ? KernelResult.Success
  135. : KernelResult.InvalidHandle;
  136. }
  137. public void Break64(ulong reason, ulong x1, ulong info)
  138. {
  139. Break(reason);
  140. }
  141. private void Break(ulong reason)
  142. {
  143. KThread currentThread = _system.Scheduler.GetCurrentThread();
  144. if ((reason & (1UL << 31)) == 0)
  145. {
  146. currentThread.PrintGuestStackTrace();
  147. throw new GuestBrokeExecutionException();
  148. }
  149. else
  150. {
  151. Logger.PrintInfo(LogClass.KernelSvc, "Debugger triggered.");
  152. currentThread.PrintGuestStackTrace();
  153. }
  154. }
  155. public void OutputDebugString64(ulong strPtr, ulong size)
  156. {
  157. OutputDebugString(strPtr, size);
  158. }
  159. private void OutputDebugString(ulong strPtr, ulong size)
  160. {
  161. string str = MemoryHelper.ReadAsciiString(_process.CpuMemory, (long)strPtr, (long)size);
  162. Logger.PrintWarning(LogClass.KernelSvc, str);
  163. }
  164. public KernelResult GetInfo64(uint id, int handle, long subId, out long value)
  165. {
  166. return GetInfo(id, handle, subId, out value);
  167. }
  168. private KernelResult GetInfo(uint id, int handle, long subId, out long value)
  169. {
  170. value = 0;
  171. switch (id)
  172. {
  173. case 0:
  174. case 1:
  175. case 2:
  176. case 3:
  177. case 4:
  178. case 5:
  179. case 6:
  180. case 7:
  181. case 12:
  182. case 13:
  183. case 14:
  184. case 15:
  185. case 16:
  186. case 17:
  187. case 18:
  188. case 20:
  189. case 21:
  190. case 22:
  191. {
  192. if (subId != 0)
  193. {
  194. return KernelResult.InvalidCombination;
  195. }
  196. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  197. KProcess process = currentProcess.HandleTable.GetKProcess(handle);
  198. if (process == null)
  199. {
  200. return KernelResult.InvalidHandle;
  201. }
  202. switch (id)
  203. {
  204. case 0: value = process.Capabilities.AllowedCpuCoresMask; break;
  205. case 1: value = process.Capabilities.AllowedThreadPriosMask; break;
  206. case 2: value = (long)process.MemoryManager.AliasRegionStart; break;
  207. case 3: value = (long)(process.MemoryManager.AliasRegionEnd -
  208. process.MemoryManager.AliasRegionStart); break;
  209. case 4: value = (long)process.MemoryManager.HeapRegionStart; break;
  210. case 5: value = (long)(process.MemoryManager.HeapRegionEnd -
  211. process.MemoryManager.HeapRegionStart); break;
  212. case 6: value = (long)process.GetMemoryCapacity(); break;
  213. case 7: value = (long)process.GetMemoryUsage(); break;
  214. case 12: value = (long)process.MemoryManager.GetAddrSpaceBaseAddr(); break;
  215. case 13: value = (long)process.MemoryManager.GetAddrSpaceSize(); break;
  216. case 14: value = (long)process.MemoryManager.StackRegionStart; break;
  217. case 15: value = (long)(process.MemoryManager.StackRegionEnd -
  218. process.MemoryManager.StackRegionStart); break;
  219. case 16: value = (long)process.PersonalMmHeapPagesCount * KMemoryManager.PageSize; break;
  220. case 17:
  221. if (process.PersonalMmHeapPagesCount != 0)
  222. {
  223. value = process.MemoryManager.GetMmUsedPages() * KMemoryManager.PageSize;
  224. }
  225. break;
  226. case 18: value = (long)process.TitleId; break;
  227. case 20: value = (long)process.UserExceptionContextAddress; break;
  228. case 21: value = (long)process.GetMemoryCapacityWithoutPersonalMmHeap(); break;
  229. case 22: value = (long)process.GetMemoryUsageWithoutPersonalMmHeap(); break;
  230. }
  231. break;
  232. }
  233. case 8:
  234. {
  235. if (handle != 0)
  236. {
  237. return KernelResult.InvalidHandle;
  238. }
  239. if (subId != 0)
  240. {
  241. return KernelResult.InvalidCombination;
  242. }
  243. value = _system.Scheduler.GetCurrentProcess().Debug ? 1 : 0;
  244. break;
  245. }
  246. case 9:
  247. {
  248. if (handle != 0)
  249. {
  250. return KernelResult.InvalidHandle;
  251. }
  252. if (subId != 0)
  253. {
  254. return KernelResult.InvalidCombination;
  255. }
  256. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  257. if (currentProcess.ResourceLimit != null)
  258. {
  259. KHandleTable handleTable = currentProcess.HandleTable;
  260. KResourceLimit resourceLimit = currentProcess.ResourceLimit;
  261. KernelResult result = handleTable.GenerateHandle(resourceLimit, out int resLimHandle);
  262. if (result != KernelResult.Success)
  263. {
  264. return result;
  265. }
  266. value = (uint)resLimHandle;
  267. }
  268. break;
  269. }
  270. case 10:
  271. {
  272. if (handle != 0)
  273. {
  274. return KernelResult.InvalidHandle;
  275. }
  276. int currentCore = _system.Scheduler.GetCurrentThread().CurrentCore;
  277. if (subId != -1 && subId != currentCore)
  278. {
  279. return KernelResult.InvalidCombination;
  280. }
  281. value = _system.Scheduler.CoreContexts[currentCore].TotalIdleTimeTicks;
  282. break;
  283. }
  284. case 11:
  285. {
  286. if (handle != 0)
  287. {
  288. return KernelResult.InvalidHandle;
  289. }
  290. if ((ulong)subId > 3)
  291. {
  292. return KernelResult.InvalidCombination;
  293. }
  294. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  295. value = currentProcess.RandomEntropy[subId];
  296. break;
  297. }
  298. case 0xf0000002u:
  299. {
  300. if (subId < -1 || subId > 3)
  301. {
  302. return KernelResult.InvalidCombination;
  303. }
  304. KThread thread = _system.Scheduler.GetCurrentProcess().HandleTable.GetKThread(handle);
  305. if (thread == null)
  306. {
  307. return KernelResult.InvalidHandle;
  308. }
  309. KThread currentThread = _system.Scheduler.GetCurrentThread();
  310. int currentCore = currentThread.CurrentCore;
  311. if (subId != -1 && subId != currentCore)
  312. {
  313. return KernelResult.Success;
  314. }
  315. KCoreContext coreContext = _system.Scheduler.CoreContexts[currentCore];
  316. long timeDelta = PerformanceCounter.ElapsedMilliseconds - coreContext.LastContextSwitchTime;
  317. if (subId != -1)
  318. {
  319. value = KTimeManager.ConvertMillisecondsToTicks(timeDelta);
  320. }
  321. else
  322. {
  323. long totalTimeRunning = thread.TotalTimeRunning;
  324. if (thread == currentThread)
  325. {
  326. totalTimeRunning += timeDelta;
  327. }
  328. value = KTimeManager.ConvertMillisecondsToTicks(totalTimeRunning);
  329. }
  330. break;
  331. }
  332. default: return KernelResult.InvalidEnumValue;
  333. }
  334. return KernelResult.Success;
  335. }
  336. public KernelResult CreateEvent64(out int wEventHandle, out int rEventHandle)
  337. {
  338. return CreateEvent(out wEventHandle, out rEventHandle);
  339. }
  340. private KernelResult CreateEvent(out int wEventHandle, out int rEventHandle)
  341. {
  342. KEvent Event = new KEvent(_system);
  343. KernelResult result = _process.HandleTable.GenerateHandle(Event.WritableEvent, out wEventHandle);
  344. if (result == KernelResult.Success)
  345. {
  346. result = _process.HandleTable.GenerateHandle(Event.ReadableEvent, out rEventHandle);
  347. if (result != KernelResult.Success)
  348. {
  349. _process.HandleTable.CloseHandle(wEventHandle);
  350. }
  351. }
  352. else
  353. {
  354. rEventHandle = 0;
  355. }
  356. return result;
  357. }
  358. public KernelResult GetProcessList64(ulong address, int maxCount, out int count)
  359. {
  360. return GetProcessList(address, maxCount, out count);
  361. }
  362. private KernelResult GetProcessList(ulong address, int maxCount, out int count)
  363. {
  364. count = 0;
  365. if ((maxCount >> 28) != 0)
  366. {
  367. return KernelResult.MaximumExceeded;
  368. }
  369. if (maxCount != 0)
  370. {
  371. KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
  372. ulong copySize = (ulong)maxCount * 8;
  373. if (address + copySize <= address)
  374. {
  375. return KernelResult.InvalidMemState;
  376. }
  377. if (currentProcess.MemoryManager.OutsideAddrSpace(address, copySize))
  378. {
  379. return KernelResult.InvalidMemState;
  380. }
  381. }
  382. int copyCount = 0;
  383. lock (_system.Processes)
  384. {
  385. foreach (KProcess process in _system.Processes.Values)
  386. {
  387. if (copyCount < maxCount)
  388. {
  389. if (!KernelTransfer.KernelToUserInt64(_system, address + (ulong)copyCount * 8, process.Pid))
  390. {
  391. return KernelResult.UserCopyFailed;
  392. }
  393. }
  394. copyCount++;
  395. }
  396. }
  397. count = copyCount;
  398. return KernelResult.Success;
  399. }
  400. public KernelResult GetSystemInfo64(uint id, int handle, long subId, out long value)
  401. {
  402. return GetSystemInfo(id, handle, subId, out value);
  403. }
  404. private KernelResult GetSystemInfo(uint id, int handle, long subId, out long value)
  405. {
  406. value = 0;
  407. if (id > 2)
  408. {
  409. return KernelResult.InvalidEnumValue;
  410. }
  411. if (handle != 0)
  412. {
  413. return KernelResult.InvalidHandle;
  414. }
  415. if (id < 2)
  416. {
  417. if ((ulong)subId > 3)
  418. {
  419. return KernelResult.InvalidCombination;
  420. }
  421. KMemoryRegionManager region = _system.MemoryRegions[subId];
  422. switch (id)
  423. {
  424. // Memory region capacity.
  425. case 0: value = (long)region.Size; break;
  426. // Memory region free space.
  427. case 1:
  428. {
  429. ulong freePagesCount = region.GetFreePages();
  430. value = (long)(freePagesCount * KMemoryManager.PageSize);
  431. break;
  432. }
  433. }
  434. }
  435. else /* if (Id == 2) */
  436. {
  437. if ((ulong)subId > 1)
  438. {
  439. return KernelResult.InvalidCombination;
  440. }
  441. switch (subId)
  442. {
  443. case 0: value = _system.PrivilegedProcessLowestId; break;
  444. case 1: value = _system.PrivilegedProcessHighestId; break;
  445. }
  446. }
  447. return KernelResult.Success;
  448. }
  449. }
  450. }