SvcSystem.cs 18 KB

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