KernelContext.cs 3.5 KB

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