KernelContext.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(
  41. Switch device,
  42. MemoryBlock memory,
  43. MemorySize memorySize,
  44. MemoryArrange memoryArrange)
  45. {
  46. Device = device;
  47. Memory = memory;
  48. Running = true;
  49. Syscall = new Syscall(this);
  50. SyscallHandler = new SyscallHandler(this);
  51. ResourceLimit = new KResourceLimit(this);
  52. KernelInit.InitializeResourceLimit(ResourceLimit, memorySize);
  53. MemoryRegions = KernelInit.GetMemoryRegions(memorySize, memoryArrange);
  54. LargeMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize * 2);
  55. SmallMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize);
  56. UserSlabHeapPages = new KSlabHeap(
  57. KernelConstants.UserSlabHeapBase,
  58. KernelConstants.UserSlabHeapItemSize,
  59. KernelConstants.UserSlabHeapSize);
  60. CriticalSection = new KCriticalSection(this);
  61. Schedulers = new KScheduler[KScheduler.CpuCoresCount];
  62. PriorityQueue = new KPriorityQueue();
  63. TimeManager = new KTimeManager(this);
  64. Synchronization = new KSynchronization(this);
  65. ContextIdManager = new KContextIdManager();
  66. for (int core = 0; core < KScheduler.CpuCoresCount; core++)
  67. {
  68. Schedulers[core] = new KScheduler(this, core);
  69. }
  70. StartPreemptionThread();
  71. KernelInitialized = true;
  72. Processes = new ConcurrentDictionary<long, KProcess>();
  73. AutoObjectNames = new ConcurrentDictionary<string, KAutoObject>();
  74. _kipId = KernelConstants.InitialKipId;
  75. _processId = KernelConstants.InitialProcessId;
  76. }
  77. private void StartPreemptionThread()
  78. {
  79. void PreemptionThreadStart()
  80. {
  81. KScheduler.PreemptionThreadLoop(this);
  82. }
  83. new Thread(PreemptionThreadStart) { Name = "HLE.PreemptionThread" }.Start();
  84. }
  85. public long NewThreadUid()
  86. {
  87. return Interlocked.Increment(ref _threadUid) - 1;
  88. }
  89. public long NewKipId()
  90. {
  91. return Interlocked.Increment(ref _kipId) - 1;
  92. }
  93. public long NewProcessId()
  94. {
  95. return Interlocked.Increment(ref _processId) - 1;
  96. }
  97. public void Dispose()
  98. {
  99. Running = false;
  100. for (int i = 0; i < KScheduler.CpuCoresCount; i++)
  101. {
  102. Schedulers[i].Dispose();
  103. }
  104. TimeManager.Dispose();
  105. }
  106. }
  107. }