KernelContext.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Ryujinx.HLE.HOS.Kernel.Common;
  2. using Ryujinx.HLE.HOS.Kernel.Memory;
  3. using Ryujinx.HLE.HOS.Kernel.Process;
  4. using Ryujinx.HLE.HOS.Kernel.SupervisorCall;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using Ryujinx.Memory;
  7. using System;
  8. using System.Collections.Concurrent;
  9. using System.Threading;
  10. namespace Ryujinx.HLE.HOS.Kernel
  11. {
  12. class KernelContext : IDisposable
  13. {
  14. public long PrivilegedProcessLowestId { get; set; } = 1;
  15. public long PrivilegedProcessHighestId { get; set; } = 8;
  16. public bool EnableVersionChecks { get; set; }
  17. public bool KernelInitialized { get; }
  18. public bool Running { get; private set; }
  19. public Switch Device { get; }
  20. public MemoryBlock Memory { get; }
  21. public Syscall Syscall { get; }
  22. public SyscallHandler SyscallHandler { get; }
  23. public KResourceLimit ResourceLimit { get; }
  24. public KMemoryRegionManager[] MemoryRegions { get; }
  25. public KMemoryBlockAllocator LargeMemoryBlockAllocator { get; }
  26. public KMemoryBlockAllocator SmallMemoryBlockAllocator { get; }
  27. public KSlabHeap UserSlabHeapPages { get; }
  28. public KCriticalSection CriticalSection { get; }
  29. public KScheduler[] Schedulers { get; }
  30. public KPriorityQueue PriorityQueue { get; }
  31. public KTimeManager TimeManager { get; }
  32. public KSynchronization Synchronization { get; }
  33. public KContextIdManager ContextIdManager { get; }
  34. public ConcurrentDictionary<long, KProcess> Processes { get; }
  35. public ConcurrentDictionary<string, KAutoObject> AutoObjectNames { get; }
  36. public bool ThreadReselectionRequested { get; set; }
  37. private long _kipId;
  38. private long _processId;
  39. private long _threadUid;
  40. public KernelContext(Switch device, MemoryBlock memory)
  41. {
  42. Device = device;
  43. Memory = memory;
  44. Running = true;
  45. Syscall = new Syscall(this);
  46. SyscallHandler = new SyscallHandler(this);
  47. ResourceLimit = new KResourceLimit(this);
  48. KernelInit.InitializeResourceLimit(ResourceLimit);
  49. MemoryRegions = KernelInit.GetMemoryRegions();
  50. LargeMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize * 2);
  51. SmallMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize);
  52. UserSlabHeapPages = new KSlabHeap(
  53. KernelConstants.UserSlabHeapBase,
  54. KernelConstants.UserSlabHeapItemSize,
  55. KernelConstants.UserSlabHeapSize);
  56. CriticalSection = new KCriticalSection(this);
  57. Schedulers = new KScheduler[KScheduler.CpuCoresCount];
  58. PriorityQueue = new KPriorityQueue();
  59. TimeManager = new KTimeManager(this);
  60. Synchronization = new KSynchronization(this);
  61. ContextIdManager = new KContextIdManager();
  62. for (int core = 0; core < KScheduler.CpuCoresCount; core++)
  63. {
  64. Schedulers[core] = new KScheduler(this, core);
  65. }
  66. StartPreemptionThread();
  67. KernelInitialized = true;
  68. Processes = new ConcurrentDictionary<long, KProcess>();
  69. AutoObjectNames = new ConcurrentDictionary<string, KAutoObject>();
  70. _kipId = KernelConstants.InitialKipId;
  71. _processId = KernelConstants.InitialProcessId;
  72. }
  73. private void StartPreemptionThread()
  74. {
  75. void PreemptionThreadStart()
  76. {
  77. KScheduler.PreemptionThreadLoop(this);
  78. }
  79. new Thread(PreemptionThreadStart) { Name = "HLE.PreemptionThread" }.Start();
  80. }
  81. public long NewThreadUid()
  82. {
  83. return Interlocked.Increment(ref _threadUid) - 1;
  84. }
  85. public long NewKipId()
  86. {
  87. return Interlocked.Increment(ref _kipId) - 1;
  88. }
  89. public long NewProcessId()
  90. {
  91. return Interlocked.Increment(ref _processId) - 1;
  92. }
  93. public void Dispose()
  94. {
  95. Running = false;
  96. for (int i = 0; i < KScheduler.CpuCoresCount; i++)
  97. {
  98. Schedulers[i].Dispose();
  99. }
  100. TimeManager.Dispose();
  101. }
  102. }
  103. }