KernelContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Switch Device { get; }
  19. public MemoryBlock Memory { get; }
  20. public Syscall Syscall { get; }
  21. public SyscallHandler SyscallHandler { get; }
  22. public CountdownEvent ThreadCounter { 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 Scheduler { get; }
  30. public KTimeManager TimeManager { get; }
  31. public KSynchronization Synchronization { get; }
  32. public KContextIdManager ContextIdManager { get; }
  33. public ConcurrentDictionary<long, KProcess> Processes { get; }
  34. public ConcurrentDictionary<string, KAutoObject> AutoObjectNames { get; }
  35. private long _kipId;
  36. private long _processId;
  37. private long _threadUid;
  38. public KernelContext(Switch device, MemoryBlock memory)
  39. {
  40. Device = device;
  41. Memory = memory;
  42. Syscall = new Syscall(device, this);
  43. SyscallHandler = new SyscallHandler(this);
  44. ThreadCounter = new CountdownEvent(1);
  45. ResourceLimit = new KResourceLimit(this);
  46. KernelInit.InitializeResourceLimit(ResourceLimit);
  47. MemoryRegions = KernelInit.GetMemoryRegions();
  48. LargeMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize * 2);
  49. SmallMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize);
  50. UserSlabHeapPages = new KSlabHeap(
  51. KernelConstants.UserSlabHeapBase,
  52. KernelConstants.UserSlabHeapItemSize,
  53. KernelConstants.UserSlabHeapSize);
  54. CriticalSection = new KCriticalSection(this);
  55. Scheduler = new KScheduler(this);
  56. TimeManager = new KTimeManager();
  57. Synchronization = new KSynchronization(this);
  58. ContextIdManager = new KContextIdManager();
  59. Scheduler.StartAutoPreemptionThread();
  60. KernelInitialized = true;
  61. Processes = new ConcurrentDictionary<long, KProcess>();
  62. AutoObjectNames = new ConcurrentDictionary<string, KAutoObject>();
  63. _kipId = KernelConstants.InitialKipId;
  64. _processId = KernelConstants.InitialProcessId;
  65. }
  66. public long NewThreadUid()
  67. {
  68. return Interlocked.Increment(ref _threadUid) - 1;
  69. }
  70. public long NewKipId()
  71. {
  72. return Interlocked.Increment(ref _kipId) - 1;
  73. }
  74. public long NewProcessId()
  75. {
  76. return Interlocked.Increment(ref _processId) - 1;
  77. }
  78. public void Dispose()
  79. {
  80. Scheduler.Dispose();
  81. TimeManager.Dispose();
  82. }
  83. }
  84. }