2020-05-04 05:41:29 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.SupervisorCall;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
|
|
|
|
using Ryujinx.Memory;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
|
{
|
|
|
|
|
class KernelContext : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public long PrivilegedProcessLowestId { get; set; } = 1;
|
|
|
|
|
public long PrivilegedProcessHighestId { get; set; } = 8;
|
|
|
|
|
|
|
|
|
|
public bool EnableVersionChecks { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool KernelInitialized { get; }
|
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
|
public bool Running { get; private set; }
|
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
|
public Switch Device { get; }
|
|
|
|
|
public MemoryBlock Memory { get; }
|
|
|
|
|
public Syscall Syscall { get; }
|
|
|
|
|
public SyscallHandler SyscallHandler { get; }
|
|
|
|
|
|
|
|
|
|
public KResourceLimit ResourceLimit { get; }
|
|
|
|
|
|
|
|
|
|
public KMemoryRegionManager[] MemoryRegions { get; }
|
|
|
|
|
|
|
|
|
|
public KMemoryBlockAllocator LargeMemoryBlockAllocator { get; }
|
|
|
|
|
public KMemoryBlockAllocator SmallMemoryBlockAllocator { get; }
|
|
|
|
|
|
|
|
|
|
public KSlabHeap UserSlabHeapPages { get; }
|
|
|
|
|
|
|
|
|
|
public KCriticalSection CriticalSection { get; }
|
2020-12-09 23:20:05 +01:00
|
|
|
|
public KScheduler[] Schedulers { get; }
|
|
|
|
|
public KPriorityQueue PriorityQueue { get; }
|
2020-05-04 05:41:29 +02:00
|
|
|
|
public KTimeManager TimeManager { get; }
|
|
|
|
|
public KSynchronization Synchronization { get; }
|
|
|
|
|
public KContextIdManager ContextIdManager { get; }
|
|
|
|
|
|
|
|
|
|
public ConcurrentDictionary<long, KProcess> Processes { get; }
|
|
|
|
|
public ConcurrentDictionary<string, KAutoObject> AutoObjectNames { get; }
|
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
|
public bool ThreadReselectionRequested { get; set; }
|
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
|
private long _kipId;
|
|
|
|
|
private long _processId;
|
|
|
|
|
private long _threadUid;
|
|
|
|
|
|
2021-04-04 14:06:59 +02:00
|
|
|
|
public KernelContext(
|
|
|
|
|
Switch device,
|
|
|
|
|
MemoryBlock memory,
|
|
|
|
|
MemorySize memorySize,
|
|
|
|
|
MemoryArrange memoryArrange)
|
2020-05-04 05:41:29 +02:00
|
|
|
|
{
|
|
|
|
|
Device = device;
|
|
|
|
|
Memory = memory;
|
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
|
Running = true;
|
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
|
Syscall = new Syscall(this);
|
2020-05-04 05:41:29 +02:00
|
|
|
|
|
|
|
|
|
SyscallHandler = new SyscallHandler(this);
|
|
|
|
|
|
|
|
|
|
ResourceLimit = new KResourceLimit(this);
|
|
|
|
|
|
2021-04-04 14:06:59 +02:00
|
|
|
|
KernelInit.InitializeResourceLimit(ResourceLimit, memorySize);
|
2020-05-04 05:41:29 +02:00
|
|
|
|
|
2021-04-04 14:06:59 +02:00
|
|
|
|
MemoryRegions = KernelInit.GetMemoryRegions(memorySize, memoryArrange);
|
2020-05-04 05:41:29 +02:00
|
|
|
|
|
|
|
|
|
LargeMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize * 2);
|
|
|
|
|
SmallMemoryBlockAllocator = new KMemoryBlockAllocator(KernelConstants.MemoryBlockAllocatorSize);
|
|
|
|
|
|
|
|
|
|
UserSlabHeapPages = new KSlabHeap(
|
|
|
|
|
KernelConstants.UserSlabHeapBase,
|
|
|
|
|
KernelConstants.UserSlabHeapItemSize,
|
|
|
|
|
KernelConstants.UserSlabHeapSize);
|
|
|
|
|
|
|
|
|
|
CriticalSection = new KCriticalSection(this);
|
2020-12-09 23:20:05 +01:00
|
|
|
|
Schedulers = new KScheduler[KScheduler.CpuCoresCount];
|
|
|
|
|
PriorityQueue = new KPriorityQueue();
|
|
|
|
|
TimeManager = new KTimeManager(this);
|
2020-05-04 05:41:29 +02:00
|
|
|
|
Synchronization = new KSynchronization(this);
|
|
|
|
|
ContextIdManager = new KContextIdManager();
|
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
|
for (int core = 0; core < KScheduler.CpuCoresCount; core++)
|
|
|
|
|
{
|
|
|
|
|
Schedulers[core] = new KScheduler(this, core);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StartPreemptionThread();
|
2020-05-04 05:41:29 +02:00
|
|
|
|
|
|
|
|
|
KernelInitialized = true;
|
|
|
|
|
|
|
|
|
|
Processes = new ConcurrentDictionary<long, KProcess>();
|
|
|
|
|
AutoObjectNames = new ConcurrentDictionary<string, KAutoObject>();
|
|
|
|
|
|
|
|
|
|
_kipId = KernelConstants.InitialKipId;
|
|
|
|
|
_processId = KernelConstants.InitialProcessId;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
|
private void StartPreemptionThread()
|
|
|
|
|
{
|
|
|
|
|
void PreemptionThreadStart()
|
|
|
|
|
{
|
|
|
|
|
KScheduler.PreemptionThreadLoop(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new Thread(PreemptionThreadStart) { Name = "HLE.PreemptionThread" }.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
|
public long NewThreadUid()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Increment(ref _threadUid) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long NewKipId()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Increment(ref _kipId) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long NewProcessId()
|
|
|
|
|
{
|
|
|
|
|
return Interlocked.Increment(ref _processId) - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2020-12-09 23:20:05 +01:00
|
|
|
|
Running = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < KScheduler.CpuCoresCount; i++)
|
|
|
|
|
{
|
|
|
|
|
Schedulers[i].Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
|
TimeManager.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|