2018-11-28 23:18:09 +01:00
|
|
|
using ChocolArm64;
|
|
|
|
using ChocolArm64.Events;
|
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class KProcess : KSynchronizationObject
|
|
|
|
{
|
|
|
|
public const int KernelVersionMajor = 10;
|
|
|
|
public const int KernelVersionMinor = 4;
|
|
|
|
public const int KernelVersionRevision = 0;
|
|
|
|
|
|
|
|
public const int KernelVersionPacked =
|
|
|
|
(KernelVersionMajor << 19) |
|
|
|
|
(KernelVersionMinor << 15) |
|
|
|
|
(KernelVersionRevision << 0);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KMemoryManager MemoryManager { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private SortedDictionary<ulong, KTlsPageInfo> FullTlsPages;
|
|
|
|
private SortedDictionary<ulong, KTlsPageInfo> FreeTlsPages;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public int DefaultCpuCore { get; private set; }
|
|
|
|
|
|
|
|
public bool Debug { get; private set; }
|
|
|
|
|
|
|
|
public KResourceLimit ResourceLimit { get; private set; }
|
|
|
|
|
|
|
|
public ulong PersonalMmHeapPagesCount { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private ProcessState State;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private object ProcessLock;
|
|
|
|
private object ThreadingLock;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KAddressArbiter AddressArbiter { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public long[] RandomEntropy { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private bool Signaled;
|
|
|
|
private bool UseSystemMemBlocks;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private int ThreadCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public int MmuFlags { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private MemoryRegion MemRegion;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KProcessCapabilities Capabilities { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public long TitleId { get; private set; }
|
|
|
|
public long Pid { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private long CreationTimestamp;
|
|
|
|
private ulong Entrypoint;
|
|
|
|
private ulong ImageSize;
|
|
|
|
private ulong MainThreadStackSize;
|
|
|
|
private ulong MemoryUsageCapacity;
|
|
|
|
private int Category;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public KHandleTable HandleTable { get; private set; }
|
|
|
|
|
|
|
|
public ulong UserExceptionContextAddress { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private LinkedList<KThread> Threads;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public bool IsPaused { get; private set; }
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public Translator Translator { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public MemoryManager CpuMemory { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private SvcHandler SvcHandler;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public HleProcessDebugger Debugger { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KProcess(Horizon System) : base(System)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ProcessLock = new object();
|
|
|
|
ThreadingLock = new object();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
CpuMemory = new MemoryManager(System.Device.Memory.RamPointer);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
CpuMemory.InvalidAccess += InvalidAccessHandler;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
AddressArbiter = new KAddressArbiter(System);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryManager = new KMemoryManager(System, CpuMemory);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
FullTlsPages = new SortedDictionary<ulong, KTlsPageInfo>();
|
|
|
|
FreeTlsPages = new SortedDictionary<ulong, KTlsPageInfo>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
Capabilities = new KProcessCapabilities();
|
|
|
|
|
|
|
|
RandomEntropy = new long[KScheduler.CpuCoresCount];
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Threads = new LinkedList<KThread>();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
Translator = new Translator();
|
|
|
|
|
|
|
|
Translator.CpuTrace += CpuTraceHandler;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
SvcHandler = new SvcHandler(System.Device, this);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
Debugger = new HleProcessDebugger(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult InitializeKip(
|
2018-12-05 01:52:39 +01:00
|
|
|
ProcessCreationInfo CreationInfo,
|
|
|
|
int[] Caps,
|
|
|
|
KPageList PageList,
|
|
|
|
KResourceLimit ResourceLimit,
|
|
|
|
MemoryRegion MemRegion)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
this.ResourceLimit = ResourceLimit;
|
|
|
|
this.MemRegion = MemRegion;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
AddressSpaceType AddrSpaceType = (AddressSpaceType)((CreationInfo.MmuFlags >> 1) & 7);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
bool AslrEnabled = ((CreationInfo.MmuFlags >> 5) & 1) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeAddress = CreationInfo.CodeAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeSize = (ulong)CreationInfo.CodePagesCount * KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KMemoryBlockAllocator MemoryBlockAllocator = (MmuFlags & 0x40) != 0
|
2018-11-28 23:18:09 +01:00
|
|
|
? System.LargeMemoryBlockAllocator
|
|
|
|
: System.SmallMemoryBlockAllocator;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = MemoryManager.InitializeForProcess(
|
|
|
|
AddrSpaceType,
|
|
|
|
AslrEnabled,
|
|
|
|
!AslrEnabled,
|
|
|
|
MemRegion,
|
|
|
|
CodeAddress,
|
|
|
|
CodeSize,
|
|
|
|
MemoryBlockAllocator);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!ValidateCodeAddressAndSize(CodeAddress, CodeSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MemoryManager.MapPages(
|
|
|
|
CodeAddress,
|
|
|
|
PageList,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.CodeStatic,
|
|
|
|
MemoryPermission.None);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = Capabilities.InitializeForKernel(Caps, MemoryManager);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Pid = System.GetKipId();
|
|
|
|
|
|
|
|
if (Pid == 0 || (ulong)Pid >= Horizon.InitialProcessId)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"Invalid KIP Id {Pid}.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = ParseProcessInfo(CreationInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Initialize(
|
2018-12-05 01:52:39 +01:00
|
|
|
ProcessCreationInfo CreationInfo,
|
|
|
|
int[] Caps,
|
|
|
|
KResourceLimit ResourceLimit,
|
|
|
|
MemoryRegion MemRegion)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
this.ResourceLimit = ResourceLimit;
|
|
|
|
this.MemRegion = MemRegion;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong PersonalMmHeapSize = GetPersonalMmHeapSize((ulong)CreationInfo.PersonalMmHeapPagesCount, MemRegion);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodePagesCount = (ulong)CreationInfo.CodePagesCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong NeededSizeForProcess = PersonalMmHeapSize + CodePagesCount * KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (NeededSizeForProcess != 0 && ResourceLimit != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!ResourceLimit.Reserve(LimitableResource.Memory, NeededSizeForProcess))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (NeededSizeForProcess != 0 && ResourceLimit != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ResourceLimit.Release(LimitableResource.Memory, NeededSizeForProcess);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
PersonalMmHeapPagesCount = (ulong)CreationInfo.PersonalMmHeapPagesCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KMemoryBlockAllocator MemoryBlockAllocator;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
if (PersonalMmHeapPagesCount != 0)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryBlockAllocator = new KMemoryBlockAllocator(PersonalMmHeapPagesCount * KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryBlockAllocator = (MmuFlags & 0x40) != 0
|
2018-11-28 23:18:09 +01:00
|
|
|
? System.LargeMemoryBlockAllocator
|
|
|
|
: System.SmallMemoryBlockAllocator;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
AddressSpaceType AddrSpaceType = (AddressSpaceType)((CreationInfo.MmuFlags >> 1) & 7);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
bool AslrEnabled = ((CreationInfo.MmuFlags >> 5) & 1) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeAddress = CreationInfo.CodeAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeSize = CodePagesCount * KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = MemoryManager.InitializeForProcess(
|
|
|
|
AddrSpaceType,
|
|
|
|
AslrEnabled,
|
|
|
|
!AslrEnabled,
|
|
|
|
MemRegion,
|
|
|
|
CodeAddress,
|
|
|
|
CodeSize,
|
|
|
|
MemoryBlockAllocator);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!ValidateCodeAddressAndSize(CodeAddress, CodeSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MemoryManager.MapNewProcessCode(
|
|
|
|
CodeAddress,
|
|
|
|
CodePagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.CodeStatic,
|
|
|
|
MemoryPermission.None);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = Capabilities.InitializeForUser(Caps, MemoryManager);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Pid = System.GetProcessId();
|
|
|
|
|
|
|
|
if (Pid == -1 || (ulong)Pid < Horizon.InitialProcessId)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException($"Invalid Process Id {Pid}.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = ParseProcessInfo(CreationInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private bool ValidateCodeAddressAndSize(ulong Address, ulong Size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeRegionStart;
|
|
|
|
ulong CodeRegionSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
switch (MemoryManager.AddrSpaceWidth)
|
|
|
|
{
|
|
|
|
case 32:
|
2018-12-05 01:52:39 +01:00
|
|
|
CodeRegionStart = 0x200000;
|
|
|
|
CodeRegionSize = 0x3fe00000;
|
2018-11-28 23:18:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 36:
|
2018-12-05 01:52:39 +01:00
|
|
|
CodeRegionStart = 0x8000000;
|
|
|
|
CodeRegionSize = 0x78000000;
|
2018-11-28 23:18:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 39:
|
2018-12-05 01:52:39 +01:00
|
|
|
CodeRegionStart = 0x8000000;
|
|
|
|
CodeRegionSize = 0x7ff8000000;
|
2018-11-28 23:18:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: throw new InvalidOperationException("Invalid address space width on memory manager.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong EndAddr = Address + Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong CodeRegionEnd = CodeRegionStart + CodeRegionSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (EndAddr <= Address ||
|
|
|
|
EndAddr - 1 > CodeRegionEnd - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (MemoryManager.InsideHeapRegion (Address, Size) ||
|
|
|
|
MemoryManager.InsideAliasRegion(Address, Size))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private KernelResult ParseProcessInfo(ProcessCreationInfo CreationInfo)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//Ensure that the current kernel version is equal or above to the minimum required.
|
2018-12-05 01:52:39 +01:00
|
|
|
uint RequiredKernelVersionMajor = (uint)Capabilities.KernelReleaseVersion >> 19;
|
|
|
|
uint RequiredKernelVersionMinor = ((uint)Capabilities.KernelReleaseVersion >> 15) & 0xf;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
if (System.EnableVersionChecks)
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (RequiredKernelVersionMajor > KernelVersionMajor)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (RequiredKernelVersionMajor != KernelVersionMajor && RequiredKernelVersionMajor < 3)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (RequiredKernelVersionMinor > KernelVersionMinor)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = AllocateThreadLocalStorage(out ulong UserExceptionContextAddress);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
this.UserExceptionContextAddress = UserExceptionContextAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryHelper.FillWithZeros(CpuMemory, (long)UserExceptionContextAddress, KTlsPageInfo.TlsEntrySize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Name = CreationInfo.Name;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
State = ProcessState.Created;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
CreationTimestamp = PerformanceCounter.ElapsedMilliseconds;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MmuFlags = CreationInfo.MmuFlags;
|
|
|
|
Category = CreationInfo.Category;
|
|
|
|
TitleId = CreationInfo.TitleId;
|
|
|
|
Entrypoint = CreationInfo.CodeAddress;
|
|
|
|
ImageSize = (ulong)CreationInfo.CodePagesCount * KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
UseSystemMemBlocks = ((MmuFlags >> 6) & 1) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
switch ((AddressSpaceType)((MmuFlags >> 1) & 7))
|
|
|
|
{
|
|
|
|
case AddressSpaceType.Addr32Bits:
|
|
|
|
case AddressSpaceType.Addr36Bits:
|
|
|
|
case AddressSpaceType.Addr39Bits:
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryUsageCapacity = MemoryManager.HeapRegionEnd -
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryManager.HeapRegionStart;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr32BitsNoMap:
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryUsageCapacity = MemoryManager.HeapRegionEnd -
|
|
|
|
MemoryManager.HeapRegionStart +
|
|
|
|
MemoryManager.AliasRegionEnd -
|
|
|
|
MemoryManager.AliasRegionStart;
|
2018-11-28 23:18:09 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: throw new InvalidOperationException($"Invalid MMU flags value 0x{MmuFlags:x2}.");
|
|
|
|
}
|
|
|
|
|
|
|
|
GenerateRandomEntropy();
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KernelResult AllocateThreadLocalStorage(out ulong Address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (FreeTlsPages.Count > 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//If we have free TLS pages available, just use the first one.
|
2018-12-05 01:52:39 +01:00
|
|
|
KTlsPageInfo PageInfo = FreeTlsPages.Values.First();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!PageInfo.TryGetFreePage(out Address))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure getting free TLS page!");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (PageInfo.IsFull())
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
FreeTlsPages.Remove(PageInfo.PageAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
FullTlsPages.Add(PageInfo.PageAddr, PageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Otherwise, we need to create a new one.
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = AllocateTlsPage(out KTlsPageInfo PageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!PageInfo.TryGetFreePage(out Address))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure getting free TLS page!");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
FreeTlsPages.Add(PageInfo.PageAddr, PageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Address = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private KernelResult AllocateTlsPage(out KTlsPageInfo PageInfo)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
PageInfo = default(KTlsPageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!System.UserSlabHeapPages.TryGetItem(out ulong TlsPagePa))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RegionStart = MemoryManager.TlsIoRegionStart;
|
|
|
|
ulong RegionSize = MemoryManager.TlsIoRegionEnd - RegionStart;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RegionPagesCount = RegionSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = MemoryManager.AllocateOrMapPa(
|
2018-11-28 23:18:09 +01:00
|
|
|
1,
|
|
|
|
KMemoryManager.PageSize,
|
2018-12-05 01:52:39 +01:00
|
|
|
TlsPagePa,
|
2018-11-28 23:18:09 +01:00
|
|
|
true,
|
2018-12-05 01:52:39 +01:00
|
|
|
RegionStart,
|
|
|
|
RegionPagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.ThreadLocal,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
2018-12-05 01:52:39 +01:00
|
|
|
out ulong TlsPageVa);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
System.UserSlabHeapPages.Free(TlsPagePa);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
PageInfo = new KTlsPageInfo(TlsPageVa);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryHelper.FillWithZeros(CpuMemory, (long)TlsPageVa, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KernelResult FreeThreadLocalStorage(ulong TlsSlotAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong TlsPageAddr = BitUtils.AlignDown(TlsSlotAddr, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KTlsPageInfo PageInfo = null;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (FullTlsPages.TryGetValue(TlsPageAddr, out PageInfo))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//TLS page was full, free slot and move to free pages tree.
|
2018-12-05 01:52:39 +01:00
|
|
|
FullTlsPages.Remove(TlsPageAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
FreeTlsPages.Add(TlsPageAddr, PageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-05 01:52:39 +01:00
|
|
|
else if (!FreeTlsPages.TryGetValue(TlsPageAddr, out PageInfo))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.InvalidAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (PageInfo != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
PageInfo.FreeTlsSlot(TlsSlotAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (PageInfo.IsEmpty())
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//TLS page is now empty, we should ensure it is removed
|
|
|
|
//from all trees, and free the memory it was using.
|
2018-12-05 01:52:39 +01:00
|
|
|
FreeTlsPages.Remove(TlsPageAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
FreeTlsPage(PageInfo);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private KernelResult FreeTlsPage(KTlsPageInfo PageInfo)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result = MemoryManager.ConvertVaToPa(PageInfo.PageAddr, out ulong TlsPagePa);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure translating virtual address to physical.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MemoryManager.UnmapForKernel(PageInfo.PageAddr, 1, MemoryState.ThreadLocal);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
System.UserSlabHeapPages.Free(TlsPagePa);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void GenerateRandomEntropy()
|
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public KernelResult Start(int MainThreadPriority, ulong StackSize)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ProcessLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (State > ProcessState.CreatedAttached)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ResourceLimit != null && !ResourceLimit.Reserve(LimitableResource.Thread, 1))
|
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KResourceLimit ThreadResourceLimit = ResourceLimit;
|
|
|
|
KResourceLimit MemoryResourceLimit = null;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (MainThreadStackSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Trying to start a process with a invalid state!");
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong StackSizeRounded = BitUtils.AlignUp(StackSize, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong NeededSize = StackSizeRounded + ImageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//Check if the needed size for the code and the stack will fit on the
|
|
|
|
//memory usage capacity of this Process. Also check for possible overflow
|
|
|
|
//on the above addition.
|
2018-12-05 01:52:39 +01:00
|
|
|
if (NeededSize > MemoryUsageCapacity ||
|
|
|
|
NeededSize < StackSizeRounded)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ThreadResourceLimit?.Release(LimitableResource.Thread, 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (StackSizeRounded != 0 && ResourceLimit != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryResourceLimit = ResourceLimit;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (!MemoryResourceLimit.Reserve(LimitableResource.Memory, StackSizeRounded))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ThreadResourceLimit?.Release(LimitableResource.Thread, 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
KThread MainThread = null;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong StackTop = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
MainThread?.Terminate();
|
2018-11-28 23:18:09 +01:00
|
|
|
HandleTable.Destroy();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (MainThreadStackSize != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong StackBottom = StackTop - MainThreadStackSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong StackPagesCount = MainThreadStackSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryManager.UnmapForKernel(StackBottom, StackPagesCount, MemoryState.Stack);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryResourceLimit?.Release(LimitableResource.Memory, StackSizeRounded);
|
|
|
|
ThreadResourceLimit?.Release(LimitableResource.Thread, 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (StackSizeRounded != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong StackPagesCount = StackSizeRounded / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RegionStart = MemoryManager.StackRegionStart;
|
|
|
|
ulong RegionSize = MemoryManager.StackRegionEnd - RegionStart;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong RegionPagesCount = RegionSize / KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MemoryManager.AllocateOrMapPa(
|
|
|
|
StackPagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
KMemoryManager.PageSize,
|
|
|
|
0,
|
|
|
|
false,
|
2018-12-05 01:52:39 +01:00
|
|
|
RegionStart,
|
|
|
|
RegionPagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Stack,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
2018-12-05 01:52:39 +01:00
|
|
|
out ulong StackBottom);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MainThreadStackSize += StackSizeRounded;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
StackTop = StackBottom + StackSizeRounded;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong HeapCapacity = MemoryUsageCapacity - MainThreadStackSize - ImageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MemoryManager.SetHeapCapacity(HeapCapacity);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HandleTable = new KHandleTable(System);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = HandleTable.Initialize(Capabilities.HandleTableSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MainThread = new KThread(System);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = MainThread.Initialize(
|
|
|
|
Entrypoint,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
2018-12-05 01:52:39 +01:00
|
|
|
StackTop,
|
|
|
|
MainThreadPriority,
|
2018-11-28 23:18:09 +01:00
|
|
|
DefaultCpuCore,
|
|
|
|
this);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = HandleTable.GenerateHandle(MainThread, out int MainThreadHandle);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MainThread.SetEntryArguments(0, MainThreadHandle);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ProcessState OldState = State;
|
|
|
|
ProcessState NewState = State != ProcessState.Created
|
2018-11-28 23:18:09 +01:00
|
|
|
? ProcessState.Attached
|
|
|
|
: ProcessState.Started;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
SetState(NewState);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//TODO: We can't call KThread.Start from a non-guest thread.
|
|
|
|
//We will need to make some changes to allow the creation of
|
|
|
|
//dummy threads that will be used to initialize the current
|
|
|
|
//thread on KCoreContext so that GetCurrentThread doesn't fail.
|
|
|
|
/* Result = MainThread.Start();
|
|
|
|
|
|
|
|
if (Result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
SetState(OldState);
|
|
|
|
|
|
|
|
CleanUpForError();
|
|
|
|
} */
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MainThread.Reschedule(ThreadSchedState.Running);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private void SetState(ProcessState NewState)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (State != NewState)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
State = NewState;
|
|
|
|
Signaled = true;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult InitializeThread(
|
2018-12-05 01:52:39 +01:00
|
|
|
KThread Thread,
|
|
|
|
ulong Entrypoint,
|
|
|
|
ulong ArgsPtr,
|
|
|
|
ulong StackTop,
|
|
|
|
int Priority,
|
|
|
|
int CpuCore)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ProcessLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Thread.Initialize(Entrypoint, ArgsPtr, StackTop, Priority, CpuCore, this);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void SubscribeThreadEventHandlers(CpuThread Context)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Context.ThreadState.Interrupt += InterruptHandler;
|
|
|
|
Context.ThreadState.SvcCall += SvcHandler.SvcCall;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InterruptHandler(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
System.Scheduler.ContextSwitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void IncrementThreadCount()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Interlocked.Increment(ref ThreadCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
System.ThreadCounter.AddCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DecrementThreadCountAndTerminateIfZero()
|
|
|
|
{
|
|
|
|
System.ThreadCounter.Signal();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Interlocked.Decrement(ref ThreadCount) == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetMemoryCapacity()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong TotalCapacity = (ulong)ResourceLimit.GetRemainingValue(LimitableResource.Memory);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
TotalCapacity += MemoryManager.GetTotalHeapSize();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
TotalCapacity += GetPersonalMmHeapSize();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
TotalCapacity += ImageSize + MainThreadStackSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (TotalCapacity <= MemoryUsageCapacity)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return TotalCapacity;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return MemoryUsageCapacity;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetMemoryUsage()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return ImageSize + MainThreadStackSize + MemoryManager.GetTotalHeapSize() + GetPersonalMmHeapSize();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetMemoryCapacityWithoutPersonalMmHeap()
|
|
|
|
{
|
|
|
|
return GetMemoryCapacity() - GetPersonalMmHeapSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetMemoryUsageWithoutPersonalMmHeap()
|
|
|
|
{
|
|
|
|
return GetMemoryUsage() - GetPersonalMmHeapSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong GetPersonalMmHeapSize()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return GetPersonalMmHeapSize(PersonalMmHeapPagesCount, MemRegion);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private static ulong GetPersonalMmHeapSize(ulong PersonalMmHeapPagesCount, MemoryRegion MemRegion)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (MemRegion == MemoryRegion.Applet)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return PersonalMmHeapPagesCount * KMemoryManager.PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void AddThread(KThread Thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ThreadingLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Thread.ProcessListNode = Threads.AddLast(Thread);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public void RemoveThread(KThread Thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ThreadingLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Threads.Remove(Thread.ProcessListNode);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool IsCpuCoreAllowed(int Core)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (Capabilities.AllowedCpuCoresMask & (1L << Core)) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public bool IsPriorityAllowed(int Priority)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return (Capabilities.AllowedThreadPriosMask & (1L << Priority)) != 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsSignaled()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
return Signaled;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Terminate()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
bool ShallTerminate = false;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ProcessLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (State >= ProcessState.Started)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (State == ProcessState.Started ||
|
|
|
|
State == ProcessState.Crashed ||
|
|
|
|
State == ProcessState.Attached ||
|
|
|
|
State == ProcessState.DebugSuspended)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
SetState(ProcessState.Exiting);
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
ShallTerminate = true;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.InvalidState;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (ShallTerminate)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//UnpauseAndTerminateAllThreadsExcept(System.Scheduler.GetCurrentThread());
|
|
|
|
|
|
|
|
HandleTable.Destroy();
|
|
|
|
|
|
|
|
SignalExitForDebugEvent();
|
|
|
|
SignalExit();
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private void UnpauseAndTerminateAllThreadsExcept(KThread Thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SignalExitForDebugEvent()
|
|
|
|
{
|
|
|
|
//TODO: Debug events.
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SignalExit()
|
|
|
|
{
|
|
|
|
if (ResourceLimit != null)
|
|
|
|
{
|
|
|
|
ResourceLimit.Release(LimitableResource.Memory, GetMemoryUsage());
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
|
|
|
SetState(ProcessState.Exited);
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult ClearIfNotExited()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
KernelResult Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
System.CriticalSection.Enter();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ProcessLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
if (State != ProcessState.Exited && Signaled)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Signaled = false;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Result = KernelResult.InvalidState;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.CriticalSection.Leave();
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return Result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void StopAllThreads()
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
lock (ThreadingLock)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
foreach (KThread Thread in Threads)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
Thread.Context.StopExecution();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
System.Scheduler.CoreManager.Set(Thread.Context.Work);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InvalidAccessHandler(object sender, InvalidAccessEventArgs e)
|
|
|
|
{
|
|
|
|
PrintCurrentThreadStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PrintCurrentThreadStackTrace()
|
|
|
|
{
|
|
|
|
System.Scheduler.GetCurrentThread().PrintGuestStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CpuTraceHandler(object sender, CpuTraceEventArgs e)
|
|
|
|
{
|
|
|
|
Logger.PrintInfo(LogClass.Cpu, $"Executing at 0x{e.Position:X16}.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|