KProcess.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Cpu;
  4. using Ryujinx.HLE.Exceptions;
  5. using Ryujinx.HLE.HOS.Kernel.Common;
  6. using Ryujinx.HLE.HOS.Kernel.Memory;
  7. using Ryujinx.HLE.HOS.Kernel.Threading;
  8. using Ryujinx.Horizon.Common;
  9. using Ryujinx.Memory;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading;
  14. namespace Ryujinx.HLE.HOS.Kernel.Process
  15. {
  16. class KProcess : KSynchronizationObject
  17. {
  18. public const uint KernelVersionMajor = 10;
  19. public const uint KernelVersionMinor = 4;
  20. public const uint KernelVersionRevision = 0;
  21. public const uint KernelVersionPacked =
  22. (KernelVersionMajor << 19) |
  23. (KernelVersionMinor << 15) |
  24. (KernelVersionRevision << 0);
  25. public KPageTableBase MemoryManager { get; private set; }
  26. private SortedDictionary<ulong, KTlsPageInfo> _fullTlsPages;
  27. private SortedDictionary<ulong, KTlsPageInfo> _freeTlsPages;
  28. public int DefaultCpuCore { get; set; }
  29. public bool Debug { get; private set; }
  30. public KResourceLimit ResourceLimit { get; private set; }
  31. public ulong PersonalMmHeapPagesCount { get; private set; }
  32. public ProcessState State { get; private set; }
  33. private readonly object _processLock = new();
  34. private readonly object _threadingLock = new();
  35. public KAddressArbiter AddressArbiter { get; private set; }
  36. public ulong[] RandomEntropy { get; private set; }
  37. public KThread[] PinnedThreads { get; private set; }
  38. private bool _signaled;
  39. public string Name { get; private set; }
  40. private int _threadCount;
  41. public ProcessCreationFlags Flags { get; private set; }
  42. private MemoryRegion _memRegion;
  43. public KProcessCapabilities Capabilities { get; private set; }
  44. public bool AllowCodeMemoryForJit { get; private set; }
  45. public ulong TitleId { get; private set; }
  46. public bool IsApplication { get; private set; }
  47. public ulong Pid { get; private set; }
  48. private long _creationTimestamp;
  49. private ulong _entrypoint;
  50. private ThreadStart _customThreadStart;
  51. private ulong _imageSize;
  52. private ulong _mainThreadStackSize;
  53. private ulong _memoryUsageCapacity;
  54. private int _version;
  55. public KHandleTable HandleTable { get; private set; }
  56. public ulong UserExceptionContextAddress { get; private set; }
  57. private LinkedList<KThread> _threads;
  58. public bool IsPaused { get; private set; }
  59. private long _totalTimeRunning;
  60. public long TotalTimeRunning => _totalTimeRunning;
  61. private IProcessContextFactory _contextFactory;
  62. public IProcessContext Context { get; private set; }
  63. public IVirtualMemoryManager CpuMemory => Context.AddressSpace;
  64. public HleProcessDebugger Debugger { get; private set; }
  65. public KProcess(KernelContext context, bool allowCodeMemoryForJit = false) : base(context)
  66. {
  67. AddressArbiter = new KAddressArbiter(context);
  68. _fullTlsPages = new SortedDictionary<ulong, KTlsPageInfo>();
  69. _freeTlsPages = new SortedDictionary<ulong, KTlsPageInfo>();
  70. Capabilities = new KProcessCapabilities();
  71. AllowCodeMemoryForJit = allowCodeMemoryForJit;
  72. RandomEntropy = new ulong[KScheduler.CpuCoresCount];
  73. PinnedThreads = new KThread[KScheduler.CpuCoresCount];
  74. // TODO: Remove once we no longer need to initialize it externally.
  75. HandleTable = new KHandleTable(context);
  76. _threads = new LinkedList<KThread>();
  77. Debugger = new HleProcessDebugger(this);
  78. }
  79. public Result InitializeKip(
  80. ProcessCreationInfo creationInfo,
  81. ReadOnlySpan<uint> capabilities,
  82. KPageList pageList,
  83. KResourceLimit resourceLimit,
  84. MemoryRegion memRegion,
  85. IProcessContextFactory contextFactory,
  86. ThreadStart customThreadStart = null)
  87. {
  88. ResourceLimit = resourceLimit;
  89. _memRegion = memRegion;
  90. _contextFactory = contextFactory ?? new ProcessContextFactory();
  91. _customThreadStart = customThreadStart;
  92. AddressSpaceType addrSpaceType = (AddressSpaceType)((int)(creationInfo.Flags & ProcessCreationFlags.AddressSpaceMask) >> (int)ProcessCreationFlags.AddressSpaceShift);
  93. Pid = KernelContext.NewKipId();
  94. if (Pid == 0 || Pid >= KernelConstants.InitialProcessId)
  95. {
  96. throw new InvalidOperationException($"Invalid KIP Id {Pid}.");
  97. }
  98. InitializeMemoryManager(creationInfo.Flags);
  99. bool aslrEnabled = creationInfo.Flags.HasFlag(ProcessCreationFlags.EnableAslr);
  100. ulong codeAddress = creationInfo.CodeAddress;
  101. ulong codeSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
  102. KMemoryBlockSlabManager slabManager = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
  103. ? KernelContext.LargeMemoryBlockSlabManager
  104. : KernelContext.SmallMemoryBlockSlabManager;
  105. Result result = MemoryManager.InitializeForProcess(
  106. addrSpaceType,
  107. aslrEnabled,
  108. !aslrEnabled,
  109. memRegion,
  110. codeAddress,
  111. codeSize,
  112. slabManager);
  113. if (result != Result.Success)
  114. {
  115. return result;
  116. }
  117. if (!MemoryManager.CanContain(codeAddress, codeSize, MemoryState.CodeStatic))
  118. {
  119. return KernelResult.InvalidMemRange;
  120. }
  121. result = MemoryManager.MapPages(codeAddress, pageList, MemoryState.CodeStatic, KMemoryPermission.None);
  122. if (result != Result.Success)
  123. {
  124. return result;
  125. }
  126. result = Capabilities.InitializeForKernel(capabilities, MemoryManager);
  127. if (result != Result.Success)
  128. {
  129. return result;
  130. }
  131. return ParseProcessInfo(creationInfo);
  132. }
  133. public Result Initialize(
  134. ProcessCreationInfo creationInfo,
  135. ReadOnlySpan<uint> capabilities,
  136. KResourceLimit resourceLimit,
  137. MemoryRegion memRegion,
  138. IProcessContextFactory contextFactory,
  139. ThreadStart customThreadStart = null)
  140. {
  141. ResourceLimit = resourceLimit;
  142. _memRegion = memRegion;
  143. _contextFactory = contextFactory ?? new ProcessContextFactory();
  144. _customThreadStart = customThreadStart;
  145. IsApplication = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication);
  146. ulong personalMmHeapSize = GetPersonalMmHeapSize((ulong)creationInfo.SystemResourcePagesCount, memRegion);
  147. ulong codePagesCount = (ulong)creationInfo.CodePagesCount;
  148. ulong neededSizeForProcess = personalMmHeapSize + codePagesCount * KPageTableBase.PageSize;
  149. if (neededSizeForProcess != 0 && resourceLimit != null)
  150. {
  151. if (!resourceLimit.Reserve(LimitableResource.Memory, neededSizeForProcess))
  152. {
  153. return KernelResult.ResLimitExceeded;
  154. }
  155. }
  156. void CleanUpForError()
  157. {
  158. if (neededSizeForProcess != 0 && resourceLimit != null)
  159. {
  160. resourceLimit.Release(LimitableResource.Memory, neededSizeForProcess);
  161. }
  162. }
  163. PersonalMmHeapPagesCount = (ulong)creationInfo.SystemResourcePagesCount;
  164. KMemoryBlockSlabManager slabManager;
  165. if (PersonalMmHeapPagesCount != 0)
  166. {
  167. slabManager = new KMemoryBlockSlabManager(PersonalMmHeapPagesCount * KPageTableBase.PageSize);
  168. }
  169. else
  170. {
  171. slabManager = creationInfo.Flags.HasFlag(ProcessCreationFlags.IsApplication)
  172. ? KernelContext.LargeMemoryBlockSlabManager
  173. : KernelContext.SmallMemoryBlockSlabManager;
  174. }
  175. AddressSpaceType addrSpaceType = (AddressSpaceType)((int)(creationInfo.Flags & ProcessCreationFlags.AddressSpaceMask) >> (int)ProcessCreationFlags.AddressSpaceShift);
  176. Pid = KernelContext.NewProcessId();
  177. if (Pid == ulong.MaxValue || Pid < KernelConstants.InitialProcessId)
  178. {
  179. throw new InvalidOperationException($"Invalid Process Id {Pid}.");
  180. }
  181. InitializeMemoryManager(creationInfo.Flags);
  182. bool aslrEnabled = creationInfo.Flags.HasFlag(ProcessCreationFlags.EnableAslr);
  183. ulong codeAddress = creationInfo.CodeAddress;
  184. ulong codeSize = codePagesCount * KPageTableBase.PageSize;
  185. Result result = MemoryManager.InitializeForProcess(
  186. addrSpaceType,
  187. aslrEnabled,
  188. !aslrEnabled,
  189. memRegion,
  190. codeAddress,
  191. codeSize,
  192. slabManager);
  193. if (result != Result.Success)
  194. {
  195. CleanUpForError();
  196. return result;
  197. }
  198. if (!MemoryManager.CanContain(codeAddress, codeSize, MemoryState.CodeStatic))
  199. {
  200. CleanUpForError();
  201. return KernelResult.InvalidMemRange;
  202. }
  203. result = MemoryManager.MapPages(
  204. codeAddress,
  205. codePagesCount,
  206. MemoryState.CodeStatic,
  207. KMemoryPermission.None);
  208. if (result != Result.Success)
  209. {
  210. CleanUpForError();
  211. return result;
  212. }
  213. result = Capabilities.InitializeForUser(capabilities, MemoryManager);
  214. if (result != Result.Success)
  215. {
  216. CleanUpForError();
  217. return result;
  218. }
  219. result = ParseProcessInfo(creationInfo);
  220. if (result != Result.Success)
  221. {
  222. CleanUpForError();
  223. }
  224. return result;
  225. }
  226. private Result ParseProcessInfo(ProcessCreationInfo creationInfo)
  227. {
  228. // Ensure that the current kernel version is equal or above to the minimum required.
  229. uint requiredKernelVersionMajor = (uint)Capabilities.KernelReleaseVersion >> 19;
  230. uint requiredKernelVersionMinor = ((uint)Capabilities.KernelReleaseVersion >> 15) & 0xf;
  231. if (KernelContext.EnableVersionChecks)
  232. {
  233. if (requiredKernelVersionMajor > KernelVersionMajor)
  234. {
  235. return KernelResult.InvalidCombination;
  236. }
  237. if (requiredKernelVersionMajor != KernelVersionMajor && requiredKernelVersionMajor < 3)
  238. {
  239. return KernelResult.InvalidCombination;
  240. }
  241. if (requiredKernelVersionMinor > KernelVersionMinor)
  242. {
  243. return KernelResult.InvalidCombination;
  244. }
  245. }
  246. Result result = AllocateThreadLocalStorage(out ulong userExceptionContextAddress);
  247. if (result != Result.Success)
  248. {
  249. return result;
  250. }
  251. UserExceptionContextAddress = userExceptionContextAddress;
  252. MemoryHelper.FillWithZeros(CpuMemory, userExceptionContextAddress, KTlsPageInfo.TlsEntrySize);
  253. Name = creationInfo.Name;
  254. State = ProcessState.Created;
  255. _creationTimestamp = PerformanceCounter.ElapsedMilliseconds;
  256. Flags = creationInfo.Flags;
  257. _version = creationInfo.Version;
  258. TitleId = creationInfo.TitleId;
  259. _entrypoint = creationInfo.CodeAddress;
  260. _imageSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
  261. switch (Flags & ProcessCreationFlags.AddressSpaceMask)
  262. {
  263. case ProcessCreationFlags.AddressSpace32Bit:
  264. case ProcessCreationFlags.AddressSpace64BitDeprecated:
  265. case ProcessCreationFlags.AddressSpace64Bit:
  266. _memoryUsageCapacity = MemoryManager.HeapRegionEnd -
  267. MemoryManager.HeapRegionStart;
  268. break;
  269. case ProcessCreationFlags.AddressSpace32BitWithoutAlias:
  270. _memoryUsageCapacity = MemoryManager.HeapRegionEnd -
  271. MemoryManager.HeapRegionStart +
  272. MemoryManager.AliasRegionEnd -
  273. MemoryManager.AliasRegionStart;
  274. break;
  275. default: throw new InvalidOperationException($"Invalid MMU flags value 0x{Flags:x2}.");
  276. }
  277. GenerateRandomEntropy();
  278. return Result.Success;
  279. }
  280. public Result AllocateThreadLocalStorage(out ulong address)
  281. {
  282. KernelContext.CriticalSection.Enter();
  283. Result result;
  284. if (_freeTlsPages.Count > 0)
  285. {
  286. // If we have free TLS pages available, just use the first one.
  287. KTlsPageInfo pageInfo = _freeTlsPages.Values.First();
  288. if (!pageInfo.TryGetFreePage(out address))
  289. {
  290. throw new InvalidOperationException("Unexpected failure getting free TLS page!");
  291. }
  292. if (pageInfo.IsFull())
  293. {
  294. _freeTlsPages.Remove(pageInfo.PageVirtualAddress);
  295. _fullTlsPages.Add(pageInfo.PageVirtualAddress, pageInfo);
  296. }
  297. result = Result.Success;
  298. }
  299. else
  300. {
  301. // Otherwise, we need to create a new one.
  302. result = AllocateTlsPage(out KTlsPageInfo pageInfo);
  303. if (result == Result.Success)
  304. {
  305. if (!pageInfo.TryGetFreePage(out address))
  306. {
  307. throw new InvalidOperationException("Unexpected failure getting free TLS page!");
  308. }
  309. _freeTlsPages.Add(pageInfo.PageVirtualAddress, pageInfo);
  310. }
  311. else
  312. {
  313. address = 0;
  314. }
  315. }
  316. KernelContext.CriticalSection.Leave();
  317. return result;
  318. }
  319. private Result AllocateTlsPage(out KTlsPageInfo pageInfo)
  320. {
  321. pageInfo = default;
  322. if (!KernelContext.UserSlabHeapPages.TryGetItem(out ulong tlsPagePa))
  323. {
  324. return KernelResult.OutOfMemory;
  325. }
  326. ulong regionStart = MemoryManager.TlsIoRegionStart;
  327. ulong regionSize = MemoryManager.TlsIoRegionEnd - regionStart;
  328. ulong regionPagesCount = regionSize / KPageTableBase.PageSize;
  329. Result result = MemoryManager.MapPages(
  330. 1,
  331. KPageTableBase.PageSize,
  332. tlsPagePa,
  333. true,
  334. regionStart,
  335. regionPagesCount,
  336. MemoryState.ThreadLocal,
  337. KMemoryPermission.ReadAndWrite,
  338. out ulong tlsPageVa);
  339. if (result != Result.Success)
  340. {
  341. KernelContext.UserSlabHeapPages.Free(tlsPagePa);
  342. }
  343. else
  344. {
  345. pageInfo = new KTlsPageInfo(tlsPageVa, tlsPagePa);
  346. MemoryHelper.FillWithZeros(CpuMemory, tlsPageVa, KPageTableBase.PageSize);
  347. }
  348. return result;
  349. }
  350. public Result FreeThreadLocalStorage(ulong tlsSlotAddr)
  351. {
  352. ulong tlsPageAddr = BitUtils.AlignDown<ulong>(tlsSlotAddr, KPageTableBase.PageSize);
  353. KernelContext.CriticalSection.Enter();
  354. Result result = Result.Success;
  355. KTlsPageInfo pageInfo;
  356. if (_fullTlsPages.TryGetValue(tlsPageAddr, out pageInfo))
  357. {
  358. // TLS page was full, free slot and move to free pages tree.
  359. _fullTlsPages.Remove(tlsPageAddr);
  360. _freeTlsPages.Add(tlsPageAddr, pageInfo);
  361. }
  362. else if (!_freeTlsPages.TryGetValue(tlsPageAddr, out pageInfo))
  363. {
  364. result = KernelResult.InvalidAddress;
  365. }
  366. if (pageInfo != null)
  367. {
  368. pageInfo.FreeTlsSlot(tlsSlotAddr);
  369. if (pageInfo.IsEmpty())
  370. {
  371. // TLS page is now empty, we should ensure it is removed
  372. // from all trees, and free the memory it was using.
  373. _freeTlsPages.Remove(tlsPageAddr);
  374. KernelContext.CriticalSection.Leave();
  375. FreeTlsPage(pageInfo);
  376. return Result.Success;
  377. }
  378. }
  379. KernelContext.CriticalSection.Leave();
  380. return result;
  381. }
  382. private Result FreeTlsPage(KTlsPageInfo pageInfo)
  383. {
  384. Result result = MemoryManager.UnmapForKernel(pageInfo.PageVirtualAddress, 1, MemoryState.ThreadLocal);
  385. if (result == Result.Success)
  386. {
  387. KernelContext.UserSlabHeapPages.Free(pageInfo.PagePhysicalAddress);
  388. }
  389. return result;
  390. }
  391. private void GenerateRandomEntropy()
  392. {
  393. // TODO.
  394. }
  395. public Result Start(int mainThreadPriority, ulong stackSize)
  396. {
  397. lock (_processLock)
  398. {
  399. if (State > ProcessState.CreatedAttached)
  400. {
  401. return KernelResult.InvalidState;
  402. }
  403. if (ResourceLimit != null && !ResourceLimit.Reserve(LimitableResource.Thread, 1))
  404. {
  405. return KernelResult.ResLimitExceeded;
  406. }
  407. KResourceLimit threadResourceLimit = ResourceLimit;
  408. KResourceLimit memoryResourceLimit = null;
  409. if (_mainThreadStackSize != 0)
  410. {
  411. throw new InvalidOperationException("Trying to start a process with a invalid state!");
  412. }
  413. ulong stackSizeRounded = BitUtils.AlignUp<ulong>(stackSize, KPageTableBase.PageSize);
  414. ulong neededSize = stackSizeRounded + _imageSize;
  415. // Check if the needed size for the code and the stack will fit on the
  416. // memory usage capacity of this Process. Also check for possible overflow
  417. // on the above addition.
  418. if (neededSize > _memoryUsageCapacity || neededSize < stackSizeRounded)
  419. {
  420. threadResourceLimit?.Release(LimitableResource.Thread, 1);
  421. return KernelResult.OutOfMemory;
  422. }
  423. if (stackSizeRounded != 0 && ResourceLimit != null)
  424. {
  425. memoryResourceLimit = ResourceLimit;
  426. if (!memoryResourceLimit.Reserve(LimitableResource.Memory, stackSizeRounded))
  427. {
  428. threadResourceLimit?.Release(LimitableResource.Thread, 1);
  429. return KernelResult.ResLimitExceeded;
  430. }
  431. }
  432. Result result;
  433. KThread mainThread = null;
  434. ulong stackTop = 0;
  435. void CleanUpForError()
  436. {
  437. HandleTable.Destroy();
  438. mainThread?.DecrementReferenceCount();
  439. if (_mainThreadStackSize != 0)
  440. {
  441. ulong stackBottom = stackTop - _mainThreadStackSize;
  442. ulong stackPagesCount = _mainThreadStackSize / KPageTableBase.PageSize;
  443. MemoryManager.UnmapForKernel(stackBottom, stackPagesCount, MemoryState.Stack);
  444. _mainThreadStackSize = 0;
  445. }
  446. memoryResourceLimit?.Release(LimitableResource.Memory, stackSizeRounded);
  447. threadResourceLimit?.Release(LimitableResource.Thread, 1);
  448. }
  449. if (stackSizeRounded != 0)
  450. {
  451. ulong stackPagesCount = stackSizeRounded / KPageTableBase.PageSize;
  452. ulong regionStart = MemoryManager.StackRegionStart;
  453. ulong regionSize = MemoryManager.StackRegionEnd - regionStart;
  454. ulong regionPagesCount = regionSize / KPageTableBase.PageSize;
  455. result = MemoryManager.MapPages(
  456. stackPagesCount,
  457. KPageTableBase.PageSize,
  458. 0,
  459. false,
  460. regionStart,
  461. regionPagesCount,
  462. MemoryState.Stack,
  463. KMemoryPermission.ReadAndWrite,
  464. out ulong stackBottom);
  465. if (result != Result.Success)
  466. {
  467. CleanUpForError();
  468. return result;
  469. }
  470. _mainThreadStackSize += stackSizeRounded;
  471. stackTop = stackBottom + stackSizeRounded;
  472. }
  473. ulong heapCapacity = _memoryUsageCapacity - _mainThreadStackSize - _imageSize;
  474. result = MemoryManager.SetHeapCapacity(heapCapacity);
  475. if (result != Result.Success)
  476. {
  477. CleanUpForError();
  478. return result;
  479. }
  480. HandleTable = new KHandleTable(KernelContext);
  481. result = HandleTable.Initialize(Capabilities.HandleTableSize);
  482. if (result != Result.Success)
  483. {
  484. CleanUpForError();
  485. return result;
  486. }
  487. mainThread = new KThread(KernelContext);
  488. result = mainThread.Initialize(
  489. _entrypoint,
  490. 0,
  491. stackTop,
  492. mainThreadPriority,
  493. DefaultCpuCore,
  494. this,
  495. ThreadType.User,
  496. _customThreadStart);
  497. if (result != Result.Success)
  498. {
  499. CleanUpForError();
  500. return result;
  501. }
  502. result = HandleTable.GenerateHandle(mainThread, out int mainThreadHandle);
  503. if (result != Result.Success)
  504. {
  505. CleanUpForError();
  506. return result;
  507. }
  508. mainThread.SetEntryArguments(0, mainThreadHandle);
  509. ProcessState oldState = State;
  510. ProcessState newState = State != ProcessState.Created
  511. ? ProcessState.Attached
  512. : ProcessState.Started;
  513. SetState(newState);
  514. result = mainThread.Start();
  515. if (result != Result.Success)
  516. {
  517. SetState(oldState);
  518. CleanUpForError();
  519. }
  520. if (result == Result.Success)
  521. {
  522. mainThread.IncrementReferenceCount();
  523. }
  524. mainThread.DecrementReferenceCount();
  525. return result;
  526. }
  527. }
  528. private void SetState(ProcessState newState)
  529. {
  530. if (State != newState)
  531. {
  532. State = newState;
  533. _signaled = true;
  534. Signal();
  535. }
  536. }
  537. public Result InitializeThread(
  538. KThread thread,
  539. ulong entrypoint,
  540. ulong argsPtr,
  541. ulong stackTop,
  542. int priority,
  543. int cpuCore,
  544. ThreadStart customThreadStart = null)
  545. {
  546. lock (_processLock)
  547. {
  548. return thread.Initialize(entrypoint, argsPtr, stackTop, priority, cpuCore, this, ThreadType.User, customThreadStart);
  549. }
  550. }
  551. public IExecutionContext CreateExecutionContext()
  552. {
  553. return Context?.CreateExecutionContext(new ExceptionCallbacks(
  554. InterruptHandler,
  555. null,
  556. KernelContext.SyscallHandler.SvcCall,
  557. UndefinedInstructionHandler));
  558. }
  559. private void InterruptHandler(IExecutionContext context)
  560. {
  561. KThread currentThread = KernelStatic.GetCurrentThread();
  562. if (currentThread.Context.Running &&
  563. currentThread.Owner != null &&
  564. currentThread.GetUserDisableCount() != 0 &&
  565. currentThread.Owner.PinnedThreads[currentThread.CurrentCore] == null)
  566. {
  567. KernelContext.CriticalSection.Enter();
  568. currentThread.Owner.PinThread(currentThread);
  569. currentThread.SetUserInterruptFlag();
  570. KernelContext.CriticalSection.Leave();
  571. }
  572. if (currentThread.IsSchedulable)
  573. {
  574. KernelContext.Schedulers[currentThread.CurrentCore].Schedule();
  575. }
  576. currentThread.HandlePostSyscall();
  577. }
  578. public void IncrementThreadCount()
  579. {
  580. Interlocked.Increment(ref _threadCount);
  581. }
  582. public void DecrementThreadCountAndTerminateIfZero()
  583. {
  584. if (Interlocked.Decrement(ref _threadCount) == 0)
  585. {
  586. Terminate();
  587. }
  588. }
  589. public void DecrementToZeroWhileTerminatingCurrent()
  590. {
  591. while (Interlocked.Decrement(ref _threadCount) != 0)
  592. {
  593. Destroy();
  594. TerminateCurrentProcess();
  595. }
  596. // Nintendo panic here because if it reaches this point, the current thread should be already dead.
  597. // As we handle the death of the thread in the post SVC handler and inside the CPU emulator, we don't panic here.
  598. }
  599. public ulong GetMemoryCapacity()
  600. {
  601. ulong totalCapacity = (ulong)ResourceLimit.GetRemainingValue(LimitableResource.Memory);
  602. totalCapacity += MemoryManager.GetTotalHeapSize();
  603. totalCapacity += GetPersonalMmHeapSize();
  604. totalCapacity += _imageSize + _mainThreadStackSize;
  605. if (totalCapacity <= _memoryUsageCapacity)
  606. {
  607. return totalCapacity;
  608. }
  609. return _memoryUsageCapacity;
  610. }
  611. public ulong GetMemoryUsage()
  612. {
  613. return _imageSize + _mainThreadStackSize + MemoryManager.GetTotalHeapSize() + GetPersonalMmHeapSize();
  614. }
  615. public ulong GetMemoryCapacityWithoutPersonalMmHeap()
  616. {
  617. return GetMemoryCapacity() - GetPersonalMmHeapSize();
  618. }
  619. public ulong GetMemoryUsageWithoutPersonalMmHeap()
  620. {
  621. return GetMemoryUsage() - GetPersonalMmHeapSize();
  622. }
  623. private ulong GetPersonalMmHeapSize()
  624. {
  625. return GetPersonalMmHeapSize(PersonalMmHeapPagesCount, _memRegion);
  626. }
  627. private static ulong GetPersonalMmHeapSize(ulong personalMmHeapPagesCount, MemoryRegion memRegion)
  628. {
  629. if (memRegion == MemoryRegion.Applet)
  630. {
  631. return 0;
  632. }
  633. return personalMmHeapPagesCount * KPageTableBase.PageSize;
  634. }
  635. public void AddCpuTime(long ticks)
  636. {
  637. Interlocked.Add(ref _totalTimeRunning, ticks);
  638. }
  639. public void AddThread(KThread thread)
  640. {
  641. lock (_threadingLock)
  642. {
  643. thread.ProcessListNode = _threads.AddLast(thread);
  644. }
  645. }
  646. public void RemoveThread(KThread thread)
  647. {
  648. lock (_threadingLock)
  649. {
  650. _threads.Remove(thread.ProcessListNode);
  651. }
  652. }
  653. public bool IsCpuCoreAllowed(int core)
  654. {
  655. return (Capabilities.AllowedCpuCoresMask & (1UL << core)) != 0;
  656. }
  657. public bool IsPriorityAllowed(int priority)
  658. {
  659. return (Capabilities.AllowedThreadPriosMask & (1UL << priority)) != 0;
  660. }
  661. public override bool IsSignaled()
  662. {
  663. return _signaled;
  664. }
  665. public Result Terminate()
  666. {
  667. Result result;
  668. bool shallTerminate = false;
  669. KernelContext.CriticalSection.Enter();
  670. lock (_processLock)
  671. {
  672. if (State >= ProcessState.Started)
  673. {
  674. if (State == ProcessState.Started ||
  675. State == ProcessState.Crashed ||
  676. State == ProcessState.Attached ||
  677. State == ProcessState.DebugSuspended)
  678. {
  679. SetState(ProcessState.Exiting);
  680. shallTerminate = true;
  681. }
  682. result = Result.Success;
  683. }
  684. else
  685. {
  686. result = KernelResult.InvalidState;
  687. }
  688. }
  689. KernelContext.CriticalSection.Leave();
  690. if (shallTerminate)
  691. {
  692. UnpauseAndTerminateAllThreadsExcept(KernelStatic.GetCurrentThread());
  693. HandleTable.Destroy();
  694. SignalExitToDebugTerminated();
  695. SignalExit();
  696. }
  697. return result;
  698. }
  699. public void TerminateCurrentProcess()
  700. {
  701. bool shallTerminate = false;
  702. KernelContext.CriticalSection.Enter();
  703. lock (_processLock)
  704. {
  705. if (State >= ProcessState.Started)
  706. {
  707. if (State == ProcessState.Started ||
  708. State == ProcessState.Attached ||
  709. State == ProcessState.DebugSuspended)
  710. {
  711. SetState(ProcessState.Exiting);
  712. shallTerminate = true;
  713. }
  714. }
  715. }
  716. KernelContext.CriticalSection.Leave();
  717. if (shallTerminate)
  718. {
  719. UnpauseAndTerminateAllThreadsExcept(KernelStatic.GetCurrentThread());
  720. HandleTable.Destroy();
  721. // NOTE: this is supposed to be called in receiving of the mailbox.
  722. SignalExitToDebugExited();
  723. SignalExit();
  724. }
  725. KernelStatic.GetCurrentThread().Exit();
  726. }
  727. private void UnpauseAndTerminateAllThreadsExcept(KThread currentThread)
  728. {
  729. lock (_threadingLock)
  730. {
  731. KernelContext.CriticalSection.Enter();
  732. if (currentThread != null && PinnedThreads[currentThread.CurrentCore] == currentThread)
  733. {
  734. UnpinThread(currentThread);
  735. }
  736. foreach (KThread thread in _threads)
  737. {
  738. if (thread != currentThread && (thread.SchedFlags & ThreadSchedState.LowMask) != ThreadSchedState.TerminationPending)
  739. {
  740. thread.PrepareForTermination();
  741. }
  742. }
  743. KernelContext.CriticalSection.Leave();
  744. }
  745. while (true)
  746. {
  747. KThread blockedThread = null;
  748. lock (_threadingLock)
  749. {
  750. foreach (KThread thread in _threads)
  751. {
  752. if (thread != currentThread && (thread.SchedFlags & ThreadSchedState.LowMask) != ThreadSchedState.TerminationPending)
  753. {
  754. thread.IncrementReferenceCount();
  755. blockedThread = thread;
  756. break;
  757. }
  758. }
  759. }
  760. if (blockedThread == null)
  761. {
  762. break;
  763. }
  764. blockedThread.Terminate();
  765. blockedThread.DecrementReferenceCount();
  766. }
  767. }
  768. private void SignalExitToDebugTerminated()
  769. {
  770. // TODO: Debug events.
  771. }
  772. private void SignalExitToDebugExited()
  773. {
  774. // TODO: Debug events.
  775. }
  776. private void SignalExit()
  777. {
  778. if (ResourceLimit != null)
  779. {
  780. ResourceLimit.Release(LimitableResource.Memory, GetMemoryUsage());
  781. }
  782. KernelContext.CriticalSection.Enter();
  783. SetState(ProcessState.Exited);
  784. KernelContext.CriticalSection.Leave();
  785. }
  786. public Result ClearIfNotExited()
  787. {
  788. Result result;
  789. KernelContext.CriticalSection.Enter();
  790. lock (_processLock)
  791. {
  792. if (State != ProcessState.Exited && _signaled)
  793. {
  794. _signaled = false;
  795. result = Result.Success;
  796. }
  797. else
  798. {
  799. result = KernelResult.InvalidState;
  800. }
  801. }
  802. KernelContext.CriticalSection.Leave();
  803. return result;
  804. }
  805. private void InitializeMemoryManager(ProcessCreationFlags flags)
  806. {
  807. int addrSpaceBits = (flags & ProcessCreationFlags.AddressSpaceMask) switch
  808. {
  809. ProcessCreationFlags.AddressSpace32Bit => 32,
  810. ProcessCreationFlags.AddressSpace64BitDeprecated => 36,
  811. ProcessCreationFlags.AddressSpace32BitWithoutAlias => 32,
  812. ProcessCreationFlags.AddressSpace64Bit => 39,
  813. _ => 39
  814. };
  815. bool for64Bit = flags.HasFlag(ProcessCreationFlags.Is64Bit);
  816. Context = _contextFactory.Create(KernelContext, Pid, 1UL << addrSpaceBits, InvalidAccessHandler, for64Bit);
  817. MemoryManager = new KPageTable(KernelContext, CpuMemory, Context.AddressSpaceSize);
  818. }
  819. private bool InvalidAccessHandler(ulong va)
  820. {
  821. KernelStatic.GetCurrentThread()?.PrintGuestStackTrace();
  822. KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout();
  823. Logger.Error?.Print(LogClass.Cpu, $"Invalid memory access at virtual address 0x{va:X16}.");
  824. return false;
  825. }
  826. private void UndefinedInstructionHandler(IExecutionContext context, ulong address, int opCode)
  827. {
  828. KernelStatic.GetCurrentThread().PrintGuestStackTrace();
  829. KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout();
  830. throw new UndefinedInstructionException(address, opCode);
  831. }
  832. protected override void Destroy() => Context.Dispose();
  833. public Result SetActivity(bool pause)
  834. {
  835. KernelContext.CriticalSection.Enter();
  836. if (State != ProcessState.Exiting && State != ProcessState.Exited)
  837. {
  838. if (pause)
  839. {
  840. if (IsPaused)
  841. {
  842. KernelContext.CriticalSection.Leave();
  843. return KernelResult.InvalidState;
  844. }
  845. lock (_threadingLock)
  846. {
  847. foreach (KThread thread in _threads)
  848. {
  849. thread.Suspend(ThreadSchedState.ProcessPauseFlag);
  850. }
  851. }
  852. IsPaused = true;
  853. }
  854. else
  855. {
  856. if (!IsPaused)
  857. {
  858. KernelContext.CriticalSection.Leave();
  859. return KernelResult.InvalidState;
  860. }
  861. lock (_threadingLock)
  862. {
  863. foreach (KThread thread in _threads)
  864. {
  865. thread.Resume(ThreadSchedState.ProcessPauseFlag);
  866. }
  867. }
  868. IsPaused = false;
  869. }
  870. KernelContext.CriticalSection.Leave();
  871. return Result.Success;
  872. }
  873. KernelContext.CriticalSection.Leave();
  874. return KernelResult.InvalidState;
  875. }
  876. public void PinThread(KThread thread)
  877. {
  878. if (!thread.TerminationRequested)
  879. {
  880. PinnedThreads[thread.CurrentCore] = thread;
  881. thread.Pin();
  882. KernelContext.ThreadReselectionRequested = true;
  883. }
  884. }
  885. public void UnpinThread(KThread thread)
  886. {
  887. if (!thread.TerminationRequested)
  888. {
  889. thread.Unpin();
  890. PinnedThreads[thread.CurrentCore] = null;
  891. KernelContext.ThreadReselectionRequested = true;
  892. }
  893. }
  894. public bool IsExceptionUserThread(KThread thread)
  895. {
  896. // TODO
  897. return false;
  898. }
  899. }
  900. }