KernelContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 KMemoryManager MemoryManager { get; }
  25. public KMemoryBlockSlabManager LargeMemoryBlockSlabManager { get; }
  26. public KMemoryBlockSlabManager SmallMemoryBlockSlabManager { 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<ulong, KProcess> Processes { get; }
  35. public ConcurrentDictionary<string, KAutoObject> AutoObjectNames { get; }
  36. public bool ThreadReselectionRequested { get; set; }
  37. private ulong _kipId;
  38. private ulong _processId;
  39. private ulong _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. MemoryManager = new KMemoryManager(memorySize, memoryArrange);
  54. LargeMemoryBlockSlabManager = new KMemoryBlockSlabManager(KernelConstants.MemoryBlockAllocatorSize * 2);
  55. SmallMemoryBlockSlabManager = new KMemoryBlockSlabManager(KernelConstants.MemoryBlockAllocatorSize);
  56. UserSlabHeapPages = new KSlabHeap(
  57. KernelConstants.UserSlabHeapBase,
  58. KernelConstants.UserSlabHeapItemSize,
  59. KernelConstants.UserSlabHeapSize);
  60. memory.Commit(KernelConstants.UserSlabHeapBase - DramMemoryMap.DramBase, KernelConstants.UserSlabHeapSize);
  61. CriticalSection = new KCriticalSection(this);
  62. Schedulers = new KScheduler[KScheduler.CpuCoresCount];
  63. PriorityQueue = new KPriorityQueue();
  64. TimeManager = new KTimeManager(this);
  65. Synchronization = new KSynchronization(this);
  66. ContextIdManager = new KContextIdManager();
  67. for (int core = 0; core < KScheduler.CpuCoresCount; core++)
  68. {
  69. Schedulers[core] = new KScheduler(this, core);
  70. }
  71. StartPreemptionThread();
  72. KernelInitialized = true;
  73. Processes = new ConcurrentDictionary<ulong, KProcess>();
  74. AutoObjectNames = new ConcurrentDictionary<string, KAutoObject>();
  75. _kipId = KernelConstants.InitialKipId;
  76. _processId = KernelConstants.InitialProcessId;
  77. }
  78. private void StartPreemptionThread()
  79. {
  80. void PreemptionThreadStart()
  81. {
  82. KScheduler.PreemptionThreadLoop(this);
  83. }
  84. new Thread(PreemptionThreadStart) { Name = "HLE.PreemptionThread" }.Start();
  85. }
  86. public ulong NewThreadUid()
  87. {
  88. return Interlocked.Increment(ref _threadUid) - 1;
  89. }
  90. public ulong NewKipId()
  91. {
  92. return Interlocked.Increment(ref _kipId) - 1;
  93. }
  94. public ulong NewProcessId()
  95. {
  96. return Interlocked.Increment(ref _processId) - 1;
  97. }
  98. public void Dispose()
  99. {
  100. Running = false;
  101. for (int i = 0; i < KScheduler.CpuCoresCount; i++)
  102. {
  103. Schedulers[i].Dispose();
  104. }
  105. TimeManager.Dispose();
  106. }
  107. }
  108. }