KernelContext.cs 4.7 KB

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