KProcess.cs 37 KB

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