mirror of
https://git.ryujinx.app/projects/Kenji-NX.git
synced 2026-09-28 05:57:54 +02:00
Android: NCE support
This commit is contained in:
@@ -24,6 +24,8 @@ namespace Ryujinx.HLE.HOS
|
||||
private readonly ICpuContext _cpuContext;
|
||||
private T _memoryManager;
|
||||
|
||||
public ulong ReservedSize { get; }
|
||||
|
||||
public IVirtualMemoryManager AddressSpace => _memoryManager;
|
||||
|
||||
public ulong AddressSpaceSize { get; }
|
||||
@@ -34,7 +36,8 @@ namespace Ryujinx.HLE.HOS
|
||||
GpuContext gpuContext,
|
||||
T memoryManager,
|
||||
ulong addressSpaceSize,
|
||||
bool for64Bit)
|
||||
bool for64Bit,
|
||||
ulong reservedSize = 0UL)
|
||||
{
|
||||
if (memoryManager is IRefCounted rc)
|
||||
{
|
||||
@@ -47,8 +50,8 @@ namespace Ryujinx.HLE.HOS
|
||||
_gpuContext = gpuContext;
|
||||
_cpuContext = cpuEngine.CreateCpuContext(memoryManager, for64Bit);
|
||||
_memoryManager = memoryManager;
|
||||
|
||||
AddressSpaceSize = addressSpaceSize;
|
||||
ReservedSize = reservedSize;
|
||||
}
|
||||
|
||||
public IExecutionContext CreateExecutionContext(ExceptionCallbacks exceptionCallbacks)
|
||||
|
||||
@@ -4,6 +4,7 @@ using Ryujinx.Cpu;
|
||||
using Ryujinx.Cpu.AppleHv;
|
||||
using Ryujinx.Cpu.Jit;
|
||||
using Ryujinx.Cpu.LightningJit;
|
||||
using Ryujinx.Cpu.Nce;
|
||||
using Ryujinx.Graphics.Gpu;
|
||||
using Ryujinx.HLE.HOS.Kernel;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
@@ -46,17 +47,43 @@ namespace Ryujinx.HLE.HOS
|
||||
_codeSize = codeSize;
|
||||
}
|
||||
|
||||
public static NceCpuCodePatch CreateCodePatchForNce(KernelContext context, bool for64Bit, ReadOnlySpan<byte> textSection)
|
||||
{
|
||||
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64 && for64Bit && context.Device.Configuration.UseHypervisor && !OperatingSystem.IsMacOS())
|
||||
{
|
||||
return NcePatcher.CreatePatch(textSection);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public IProcessContext Create(KernelContext context, ulong pid, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler, bool for64Bit)
|
||||
{
|
||||
IArmProcessContext processContext;
|
||||
|
||||
bool isArm64Host = RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
|
||||
|
||||
if (OperatingSystem.IsMacOS() && isArm64Host && for64Bit && context.Device.Configuration.UseHypervisor)
|
||||
if (isArm64Host && for64Bit && context.Device.Configuration.UseHypervisor)
|
||||
{
|
||||
var cpuEngine = new HvEngine(_tickSource);
|
||||
var memoryManager = new HvMemoryManager(context.Memory, addressSpaceSize, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<HvMemoryManager>(pid, cpuEngine, _gpu, memoryManager, addressSpaceSize, for64Bit);
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
var cpuEngine = new HvEngine(_tickSource);
|
||||
var memoryManager = new HvMemoryManager(context.Memory, addressSpaceSize, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<HvMemoryManager>(pid, cpuEngine, _gpu, memoryManager, addressSpaceSize, for64Bit);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!AddressSpace.TryCreateWithoutMirror(addressSpaceSize, out var addressSpace))
|
||||
{
|
||||
throw new Exception("Address space creation failed");
|
||||
}
|
||||
|
||||
Logger.Info?.Print(LogClass.Cpu, $"NCE Base AS Address: 0x{addressSpace.Pointer.ToInt64():X} Size: 0x{addressSpace.Size:X}");
|
||||
|
||||
var cpuEngine = new NceEngine(_tickSource);
|
||||
var memoryManager = new MemoryManagerNative(addressSpace, context.Memory, addressSpaceSize, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManagerNative>(pid, cpuEngine, _gpu, memoryManager, addressSpace.Size, for64Bit, memoryManager.ReservedSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -74,11 +101,13 @@ namespace Ryujinx.HLE.HOS
|
||||
: new JitEngine(_tickSource);
|
||||
|
||||
AddressSpace addressSpace = null;
|
||||
MemoryBlock asNoMirror = null;
|
||||
|
||||
// We want to use host tracked mode if the host page size is > 4KB.
|
||||
if ((mode == MemoryManagerMode.HostMapped || mode == MemoryManagerMode.HostMappedUnsafe) && MemoryBlock.GetPageSize() <= 0x1000)
|
||||
{
|
||||
if (!AddressSpace.TryCreate(context.Memory, addressSpaceSize, out addressSpace))
|
||||
if (!AddressSpace.TryCreate(context.Memory, addressSpaceSize, out addressSpace) &&
|
||||
!AddressSpace.TryCreateWithoutMirror(addressSpaceSize, out asNoMirror))
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Cpu, "Address space creation failed, falling back to software page table");
|
||||
|
||||
@@ -89,35 +118,47 @@ namespace Ryujinx.HLE.HOS
|
||||
switch (mode)
|
||||
{
|
||||
case MemoryManagerMode.SoftwarePageTable:
|
||||
var memoryManager = new MemoryManager(context.Memory, addressSpaceSize, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManager>(pid, cpuEngine, _gpu, memoryManager, addressSpaceSize, for64Bit);
|
||||
{
|
||||
var mm = new MemoryManager(context.Memory, addressSpaceSize, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManager>(pid, cpuEngine, _gpu, mm, addressSpaceSize, for64Bit);
|
||||
}
|
||||
break;
|
||||
|
||||
case MemoryManagerMode.HostMapped:
|
||||
case MemoryManagerMode.HostMappedUnsafe:
|
||||
if (addressSpace == null)
|
||||
if (addressSpace == null && asNoMirror == null)
|
||||
{
|
||||
var memoryManagerHostTracked = new MemoryManagerHostTracked(context.Memory, addressSpaceSize, mode == MemoryManagerMode.HostMappedUnsafe, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManagerHostTracked>(pid, cpuEngine, _gpu, memoryManagerHostTracked, addressSpaceSize, for64Bit);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (addressSpaceSize != addressSpace.AddressSpaceSize)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Emulation, $"Allocated address space (0x{addressSpace.AddressSpaceSize:X}) is smaller than guest application requirements (0x{addressSpaceSize:X})");
|
||||
}
|
||||
bool unsafeMode = mode == MemoryManagerMode.HostMappedUnsafe;
|
||||
|
||||
var memoryManagerHostMapped = new MemoryManagerHostMapped(addressSpace, mode == MemoryManagerMode.HostMappedUnsafe, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManagerHostMapped>(pid, cpuEngine, _gpu, memoryManagerHostMapped, addressSpace.AddressSpaceSize, for64Bit);
|
||||
if (addressSpace != null)
|
||||
{
|
||||
var mm = new MemoryManagerHostMapped(addressSpace, unsafeMode, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManagerHostMapped>(pid, cpuEngine, _gpu, mm, addressSpace.AddressSpaceSize, for64Bit);
|
||||
}
|
||||
else
|
||||
{
|
||||
var mm = new MemoryManagerHostNoMirror(asNoMirror, context.Memory, unsafeMode, invalidAccessHandler);
|
||||
processContext = new ArmProcessContext<MemoryManagerHostNoMirror>(pid, cpuEngine, _gpu, mm, asNoMirror.Size, for64Bit);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException($"{nameof(mode)} contains an invalid value: {mode}");
|
||||
}
|
||||
|
||||
if (addressSpaceSize != processContext.AddressSpaceSize)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Emulation, $"Allocated address space (0x{processContext.AddressSpaceSize:X}) is smaller than guest application requirements (0x{addressSpaceSize:X})");
|
||||
}
|
||||
}
|
||||
|
||||
DiskCacheLoadState = processContext.Initialize(_titleIdText, _displayVersion, _diskCacheEnabled, _codeAddress, _codeSize, _diskCacheSelector ?? "default");
|
||||
DiskCacheLoadState = processContext.Initialize(_titleIdText, _displayVersion, _diskCacheEnabled, _codeAddress, _codeSize, _diskCacheSelector);
|
||||
|
||||
return processContext;
|
||||
}
|
||||
|
||||
@@ -104,6 +104,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryRegion memRegion,
|
||||
ulong address,
|
||||
ulong size,
|
||||
ulong reservedSize,
|
||||
KMemoryBlockSlabManager slabManager)
|
||||
{
|
||||
_contextId = Context.ContextIdManager.GetId();
|
||||
@@ -119,6 +120,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
memRegion,
|
||||
address,
|
||||
size,
|
||||
reservedSize,
|
||||
slabManager);
|
||||
|
||||
if (result != Result.Success)
|
||||
@@ -161,6 +163,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryRegion memRegion,
|
||||
ulong address,
|
||||
ulong size,
|
||||
ulong reservedSize,
|
||||
KMemoryBlockSlabManager slabManager)
|
||||
{
|
||||
ulong endAddr = address + size;
|
||||
@@ -218,11 +221,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
break;
|
||||
|
||||
case ProcessCreationFlags.AddressSpace64Bit:
|
||||
ulong reservedAddressSpaceSize = _reservedAddressSpaceSize;
|
||||
if (_reservedAddressSpaceSize < addrSpaceEnd)
|
||||
{
|
||||
int addressSpaceWidth = (int)ulong.Log2(_reservedAddressSpaceSize);
|
||||
int addressSpaceWidth = (int)ulong.Log2(reservedAddressSpaceSize);
|
||||
|
||||
aliasRegion.Size = 1UL << (addressSpaceWidth - 3);
|
||||
aliasRegion.Size = reservedAddressSpaceSize >= 0x1800000000 ? 0x1000000000 : 1UL << (addressSpaceWidth - 3);
|
||||
heapRegion.Size = 0x180000000;
|
||||
stackRegion.Size = 1UL << (addressSpaceWidth - 8);
|
||||
tlsIoRegion.Size = 1UL << (addressSpaceWidth - 3);
|
||||
@@ -230,8 +234,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
codeRegionSize = BitUtils.AlignUp(endAddr, RegionAlignment) - CodeRegionStart;
|
||||
stackAndTlsIoStart = 0;
|
||||
stackAndTlsIoEnd = 0;
|
||||
AslrRegionStart = 0x8000000;
|
||||
addrSpaceEnd = 1UL << addressSpaceWidth;
|
||||
AslrRegionStart = Math.Max(reservedSize, 0x8000000);
|
||||
addrSpaceEnd = reservedSize + reservedAddressSpaceSize;
|
||||
AslrRegionEnd = addrSpaceEnd;
|
||||
}
|
||||
else
|
||||
@@ -242,7 +246,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
tlsIoRegion.Size = 0x1000000000;
|
||||
CodeRegionStart = BitUtils.AlignDown(address, RegionAlignment);
|
||||
codeRegionSize = BitUtils.AlignUp(endAddr, RegionAlignment) - CodeRegionStart;
|
||||
AslrRegionStart = 0x8000000;
|
||||
AslrRegionStart = Math.Max(reservedSize, 0x8000000);
|
||||
AslrRegionEnd = AslrRegionStart + 0x7ff8000000;
|
||||
stackAndTlsIoStart = 0;
|
||||
stackAndTlsIoEnd = 0;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
interface IProcessContext : IDisposable
|
||||
{
|
||||
IVirtualMemoryManager AddressSpace { get; }
|
||||
ulong ReservedSize { get; }
|
||||
|
||||
ulong AddressSpaceSize { get; }
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
InitializeMemoryManager(creationInfo.Flags);
|
||||
|
||||
ulong codeAddress = creationInfo.CodeAddress;
|
||||
ulong codeAddress = creationInfo.CodeAddress + Context.ReservedSize;
|
||||
|
||||
ulong codeSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
@@ -149,6 +149,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
memRegion,
|
||||
codeAddress,
|
||||
codeSize,
|
||||
Context.ReservedSize,
|
||||
slabManager);
|
||||
|
||||
if (result != Result.Success)
|
||||
@@ -184,7 +185,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
KResourceLimit resourceLimit,
|
||||
MemoryRegion memRegion,
|
||||
IProcessContextFactory contextFactory,
|
||||
ThreadStart customThreadStart = null)
|
||||
ThreadStart customThreadStart = null,
|
||||
ulong entrypointOffset = 0UL)
|
||||
{
|
||||
ResourceLimit = resourceLimit;
|
||||
_memRegion = memRegion;
|
||||
@@ -238,7 +240,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
InitializeMemoryManager(creationInfo.Flags);
|
||||
|
||||
ulong codeAddress = creationInfo.CodeAddress;
|
||||
ulong codeAddress = creationInfo.CodeAddress + Context.ReservedSize;
|
||||
|
||||
ulong codeSize = codePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
@@ -248,6 +250,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
memRegion,
|
||||
codeAddress,
|
||||
codeSize,
|
||||
Context.ReservedSize,
|
||||
slabManager);
|
||||
|
||||
if (result != Result.Success)
|
||||
@@ -293,6 +296,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
CleanUpForError();
|
||||
}
|
||||
|
||||
_entrypoint += entrypointOffset;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -337,10 +342,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
Flags = creationInfo.Flags;
|
||||
TitleId = creationInfo.TitleId;
|
||||
_entrypoint = creationInfo.CodeAddress;
|
||||
_entrypoint = creationInfo.CodeAddress + Context.ReservedSize;
|
||||
_imageSize = (ulong)creationInfo.CodePagesCount * KPageTableBase.PageSize;
|
||||
|
||||
switch (Flags & ProcessCreationFlags.AddressSpaceMask)
|
||||
// 19.0.0+ sets all regions to same size
|
||||
_memoryUsageCapacity = MemoryManager.HeapRegionEnd - MemoryManager.HeapRegionStart;
|
||||
/*switch (Flags & ProcessCreationFlags.AddressSpaceMask)
|
||||
{
|
||||
case ProcessCreationFlags.AddressSpace32Bit:
|
||||
case ProcessCreationFlags.AddressSpace64BitDeprecated:
|
||||
@@ -357,7 +364,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException($"Invalid MMU flags value 0x{Flags:x2}.");
|
||||
}
|
||||
}*/
|
||||
|
||||
GenerateRandomEntropy();
|
||||
|
||||
@@ -535,7 +542,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
{
|
||||
throw new InvalidOperationException("Trying to start a process with an invalid state!");
|
||||
}
|
||||
|
||||
// TODO: after 19.0.0+ alignment is not needed
|
||||
ulong stackSizeRounded = BitUtils.AlignUp<ulong>(stackSize, KPageTableBase.PageSize);
|
||||
|
||||
ulong neededSize = stackSizeRounded + _imageSize;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
class ProcessContext : IProcessContext
|
||||
{
|
||||
public IVirtualMemoryManager AddressSpace { get; }
|
||||
public ulong ReservedSize => 0UL;
|
||||
|
||||
public ulong AddressSpaceSize { get; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user