|
@@ -5,19 +5,22 @@ using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
using Ryujinx.HLE.Utilities;
|
|
using Ryujinx.HLE.Utilities;
|
|
|
|
|
+using System;
|
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
-namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
|
|
|
|
+namespace Ryujinx.HLE.HOS.Services.Ro
|
|
|
{
|
|
{
|
|
|
[Service("ldr:ro")]
|
|
[Service("ldr:ro")]
|
|
|
[Service("ro:1")] // 7.0.0+
|
|
[Service("ro:1")] // 7.0.0+
|
|
|
- class IRoInterface : IpcService
|
|
|
|
|
|
|
+ class IRoInterface : IpcService, IDisposable
|
|
|
{
|
|
{
|
|
|
- private const int MaxNrr = 0x40;
|
|
|
|
|
- private const int MaxNro = 0x40;
|
|
|
|
|
|
|
+ private const int MaxNrr = 0x40;
|
|
|
|
|
+ private const int MaxNro = 0x40;
|
|
|
|
|
+ private const int MaxMapRetries = 0x200;
|
|
|
|
|
+ private const int GuardPagesSize = 0x4000;
|
|
|
|
|
|
|
|
private const uint NrrMagic = 0x3052524E;
|
|
private const uint NrrMagic = 0x3052524E;
|
|
|
private const uint NroMagic = 0x304F524E;
|
|
private const uint NroMagic = 0x304F524E;
|
|
@@ -25,12 +28,15 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
private List<NrrInfo> _nrrInfos;
|
|
private List<NrrInfo> _nrrInfos;
|
|
|
private List<NroInfo> _nroInfos;
|
|
private List<NroInfo> _nroInfos;
|
|
|
|
|
|
|
|
- private bool _isInitialized;
|
|
|
|
|
|
|
+ private KProcess _owner;
|
|
|
|
|
+
|
|
|
|
|
+ private static Random _random = new Random();
|
|
|
|
|
|
|
|
public IRoInterface(ServiceCtx context)
|
|
public IRoInterface(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
_nrrInfos = new List<NrrInfo>(MaxNrr);
|
|
_nrrInfos = new List<NrrInfo>(MaxNrr);
|
|
|
_nroInfos = new List<NroInfo>(MaxNro);
|
|
_nroInfos = new List<NroInfo>(MaxNro);
|
|
|
|
|
+ _owner = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private ResultCode ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, long nrrAddress, long nrrSize)
|
|
private ResultCode ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, long nrrAddress, long nrrSize)
|
|
@@ -39,11 +45,11 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
|
|
|
|
|
if (nrrSize == 0 || nrrAddress + nrrSize <= nrrAddress || (nrrSize & 0xFFF) != 0)
|
|
if (nrrSize == 0 || nrrAddress + nrrSize <= nrrAddress || (nrrSize & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.BadSize;
|
|
|
|
|
|
|
+ return ResultCode.InvalidSize;
|
|
|
}
|
|
}
|
|
|
else if ((nrrAddress & 0xFFF) != 0)
|
|
else if ((nrrAddress & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.UnalignedAddress;
|
|
|
|
|
|
|
+ return ResultCode.InvalidAddress;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
StructReader reader = new StructReader(context.Memory, nrrAddress);
|
|
StructReader reader = new StructReader(context.Memory, nrrAddress);
|
|
@@ -55,7 +61,7 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
}
|
|
}
|
|
|
else if (header.NrrSize != nrrSize)
|
|
else if (header.NrrSize != nrrSize)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.BadSize;
|
|
|
|
|
|
|
+ return ResultCode.InvalidSize;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
List<byte[]> hashes = new List<byte[]>();
|
|
List<byte[]> hashes = new List<byte[]>();
|
|
@@ -105,19 +111,19 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
|
|
|
|
|
if (_nroInfos.Count >= MaxNro)
|
|
if (_nroInfos.Count >= MaxNro)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.MaxNro;
|
|
|
|
|
|
|
+ return ResultCode.TooManyNro;
|
|
|
}
|
|
}
|
|
|
else if (nroSize == 0 || nroAddress + nroSize <= nroAddress || (nroSize & 0xFFF) != 0)
|
|
else if (nroSize == 0 || nroAddress + nroSize <= nroAddress || (nroSize & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.BadSize;
|
|
|
|
|
|
|
+ return ResultCode.InvalidSize;
|
|
|
}
|
|
}
|
|
|
else if (bssSize != 0 && bssAddress + bssSize <= bssAddress)
|
|
else if (bssSize != 0 && bssAddress + bssSize <= bssAddress)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.BadSize;
|
|
|
|
|
|
|
+ return ResultCode.InvalidSize;
|
|
|
}
|
|
}
|
|
|
else if ((nroAddress & 0xFFF) != 0)
|
|
else if ((nroAddress & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.UnalignedAddress;
|
|
|
|
|
|
|
+ return ResultCode.InvalidAddress;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10);
|
|
uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10);
|
|
@@ -140,12 +146,12 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
|
|
|
|
|
if (!IsNroHashPresent(nroHash))
|
|
if (!IsNroHashPresent(nroHash))
|
|
|
{
|
|
{
|
|
|
- return ResultCode.NroHashNotPresent;
|
|
|
|
|
|
|
+ return ResultCode.NotRegistered;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (IsNroLoaded(nroHash))
|
|
if (IsNroLoaded(nroHash))
|
|
|
{
|
|
{
|
|
|
- return ResultCode.NroAlreadyLoaded;
|
|
|
|
|
|
|
+ return ResultCode.AlreadyLoaded;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
stream.Position = 0;
|
|
stream.Position = 0;
|
|
@@ -187,77 +193,120 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
return ResultCode.Success;
|
|
return ResultCode.Success;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ResultCode MapNro(ServiceCtx context, NroInfo info, out ulong nroMappedAddress)
|
|
|
|
|
|
|
+ private ResultCode MapNro(KProcess process, NroInfo info, out ulong nroMappedAddress)
|
|
|
{
|
|
{
|
|
|
- nroMappedAddress = 0;
|
|
|
|
|
|
|
+ KMemoryManager memMgr = process.MemoryManager;
|
|
|
|
|
|
|
|
- KMemoryManager memMgr = context.Process.MemoryManager;
|
|
|
|
|
|
|
+ int retryCount = 0;
|
|
|
|
|
|
|
|
- ulong targetAddress = memMgr.GetAddrSpaceBaseAddr();
|
|
|
|
|
|
|
+ nroMappedAddress = 0;
|
|
|
|
|
|
|
|
- while (true)
|
|
|
|
|
|
|
+ while (retryCount++ < MaxMapRetries)
|
|
|
{
|
|
{
|
|
|
- if (targetAddress + info.TotalSize >= memMgr.AddrSpaceEnd)
|
|
|
|
|
|
|
+ ResultCode result = MapCodeMemoryInProcess(process, info.NroAddress, info.NroSize, out nroMappedAddress);
|
|
|
|
|
+
|
|
|
|
|
+ if (result != ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.InvalidMemoryState;
|
|
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- KMemoryInfo memInfo = memMgr.QueryMemory(targetAddress);
|
|
|
|
|
-
|
|
|
|
|
- if (memInfo.State == MemoryState.Unmapped && memInfo.Size >= info.TotalSize)
|
|
|
|
|
|
|
+ if (info.BssSize > 0)
|
|
|
{
|
|
{
|
|
|
- if (!memMgr.InsideHeapRegion (targetAddress, info.TotalSize) &&
|
|
|
|
|
- !memMgr.InsideAliasRegion(targetAddress, info.TotalSize))
|
|
|
|
|
|
|
+ KernelResult bssMappingResult = memMgr.MapProcessCodeMemory(nroMappedAddress + info.NroSize, info.BssAddress, info.BssSize);
|
|
|
|
|
+
|
|
|
|
|
+ if (bssMappingResult == KernelResult.InvalidMemState)
|
|
|
{
|
|
{
|
|
|
- break;
|
|
|
|
|
|
|
+ memMgr.UnmapProcessCodeMemory(nroMappedAddress + info.NroSize, info.BssAddress, info.BssSize);
|
|
|
|
|
+ memMgr.UnmapProcessCodeMemory(nroMappedAddress, info.NroAddress, info.NroSize);
|
|
|
|
|
+
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (bssMappingResult != KernelResult.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ memMgr.UnmapProcessCodeMemory(nroMappedAddress + info.NroSize, info.BssAddress, info.BssSize);
|
|
|
|
|
+ memMgr.UnmapProcessCodeMemory(nroMappedAddress, info.NroAddress, info.NroSize);
|
|
|
|
|
+
|
|
|
|
|
+ return (ResultCode)bssMappingResult;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- targetAddress += memInfo.Size;
|
|
|
|
|
|
|
+ if (CanAddGuardRegionsInProcess(process, nroMappedAddress, info.TotalSize))
|
|
|
|
|
+ {
|
|
|
|
|
+ return ResultCode.Success;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- KernelResult result = memMgr.MapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
|
|
|
|
|
|
+ return ResultCode.InsufficientAddressSpace;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (result != KernelResult.Success)
|
|
|
|
|
- {
|
|
|
|
|
- return ResultCode.InvalidMemoryState;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private bool CanAddGuardRegionsInProcess(KProcess process, ulong baseAddress, ulong size)
|
|
|
|
|
+ {
|
|
|
|
|
+ KMemoryManager memMgr = process.MemoryManager;
|
|
|
|
|
|
|
|
- ulong bssTargetAddress = targetAddress + info.NroSize;
|
|
|
|
|
|
|
+ KMemoryInfo memInfo = memMgr.QueryMemory(baseAddress - 1);
|
|
|
|
|
|
|
|
- if (info.BssSize != 0)
|
|
|
|
|
|
|
+ if (memInfo.State == MemoryState.Unmapped && baseAddress - GuardPagesSize >= memInfo.Address)
|
|
|
{
|
|
{
|
|
|
- result = memMgr.MapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
|
|
|
|
|
|
|
+ memInfo = memMgr.QueryMemory(baseAddress + size);
|
|
|
|
|
|
|
|
- if (result != KernelResult.Success)
|
|
|
|
|
|
|
+ if (memInfo.State == MemoryState.Unmapped)
|
|
|
{
|
|
{
|
|
|
- memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
|
|
|
|
-
|
|
|
|
|
- return ResultCode.InvalidMemoryState;
|
|
|
|
|
|
|
+ return baseAddress + size + GuardPagesSize <= memInfo.Address + memInfo.Size;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- result = LoadNroIntoMemory(context.Process, info.Executable, targetAddress);
|
|
|
|
|
|
|
+ private ResultCode MapCodeMemoryInProcess(KProcess process, ulong baseAddress, ulong size, out ulong targetAddress)
|
|
|
|
|
+ {
|
|
|
|
|
+ KMemoryManager memMgr = process.MemoryManager;
|
|
|
|
|
|
|
|
- if (result != KernelResult.Success)
|
|
|
|
|
|
|
+ targetAddress = 0;
|
|
|
|
|
+
|
|
|
|
|
+ int retryCount;
|
|
|
|
|
+
|
|
|
|
|
+ int addressSpacePageLimit = (int)((memMgr.GetAddrSpaceSize() - size) >> 12);
|
|
|
|
|
+
|
|
|
|
|
+ for (retryCount = 0; retryCount < MaxMapRetries; retryCount++)
|
|
|
{
|
|
{
|
|
|
- memMgr.UnmapProcessCodeMemory(targetAddress, info.NroAddress, info.NroSize);
|
|
|
|
|
|
|
+ while (true)
|
|
|
|
|
+ {
|
|
|
|
|
+ targetAddress = memMgr.GetAddrSpaceBaseAddr() + (ulong)(_random.Next(addressSpacePageLimit) << 12);
|
|
|
|
|
+
|
|
|
|
|
+ if (memMgr.InsideAddrSpace(targetAddress, size) && !memMgr.InsideHeapRegion(targetAddress, size) && !memMgr.InsideAliasRegion(targetAddress, size))
|
|
|
|
|
+ {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ KernelResult result = memMgr.MapProcessCodeMemory(targetAddress, baseAddress, size);
|
|
|
|
|
+
|
|
|
|
|
+ if (result == KernelResult.InvalidMemState)
|
|
|
|
|
+ {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (result != KernelResult.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ return (ResultCode)result;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (info.BssSize != 0)
|
|
|
|
|
|
|
+ if (!CanAddGuardRegionsInProcess(process, targetAddress, size))
|
|
|
{
|
|
{
|
|
|
- memMgr.UnmapProcessCodeMemory(bssTargetAddress, info.BssAddress, info.BssSize);
|
|
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
return ResultCode.Success;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- info.NroMappedAddress = targetAddress;
|
|
|
|
|
- nroMappedAddress = targetAddress;
|
|
|
|
|
|
|
+ if (retryCount == MaxMapRetries)
|
|
|
|
|
+ {
|
|
|
|
|
+ return ResultCode.InsufficientAddressSpace;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
return ResultCode.Success;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private KernelResult LoadNroIntoMemory(KProcess process, IExecutable relocatableObject, ulong baseAddress)
|
|
|
|
|
|
|
+ private KernelResult SetNroMemoryPermissions(KProcess process, IExecutable relocatableObject, ulong baseAddress)
|
|
|
{
|
|
{
|
|
|
ulong textStart = baseAddress + (ulong)relocatableObject.TextOffset;
|
|
ulong textStart = baseAddress + (ulong)relocatableObject.TextOffset;
|
|
|
ulong roStart = baseAddress + (ulong)relocatableObject.RoOffset;
|
|
ulong roStart = baseAddress + (ulong)relocatableObject.RoOffset;
|
|
@@ -304,10 +353,10 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return ResultCode.BadNrrAddress;
|
|
|
|
|
|
|
+ return ResultCode.NotLoaded;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ResultCode RemoveNroInfo(ServiceCtx context, ulong nroMappedAddress)
|
|
|
|
|
|
|
+ private ResultCode RemoveNroInfo(ulong nroMappedAddress)
|
|
|
{
|
|
{
|
|
|
foreach (NroInfo info in _nroInfos)
|
|
foreach (NroInfo info in _nroInfos)
|
|
|
{
|
|
{
|
|
@@ -315,49 +364,64 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
{
|
|
{
|
|
|
_nroInfos.Remove(info);
|
|
_nroInfos.Remove(info);
|
|
|
|
|
|
|
|
- ulong textSize = (ulong)info.Executable.Text.Length;
|
|
|
|
|
- ulong roSize = (ulong)info.Executable.Ro.Length;
|
|
|
|
|
- ulong dataSize = (ulong)info.Executable.Data.Length;
|
|
|
|
|
- ulong bssSize = (ulong)info.Executable.BssSize;
|
|
|
|
|
|
|
+ return UnmapNroFromInfo(info);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- KernelResult result = KernelResult.Success;
|
|
|
|
|
|
|
+ return ResultCode.NotLoaded;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (info.Executable.BssSize != 0)
|
|
|
|
|
- {
|
|
|
|
|
- result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
- info.NroMappedAddress + textSize + roSize + dataSize,
|
|
|
|
|
- info.Executable.BssAddress,
|
|
|
|
|
- bssSize);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ private ResultCode UnmapNroFromInfo(NroInfo info)
|
|
|
|
|
+ {
|
|
|
|
|
+ ulong textSize = (ulong)info.Executable.Text.Length;
|
|
|
|
|
+ ulong roSize = (ulong)info.Executable.Ro.Length;
|
|
|
|
|
+ ulong dataSize = (ulong)info.Executable.Data.Length;
|
|
|
|
|
+ ulong bssSize = (ulong)info.Executable.BssSize;
|
|
|
|
|
|
|
|
- if (result == KernelResult.Success)
|
|
|
|
|
- {
|
|
|
|
|
- result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
- info.NroMappedAddress + textSize + roSize,
|
|
|
|
|
- info.Executable.SourceAddress + textSize + roSize,
|
|
|
|
|
- dataSize);
|
|
|
|
|
|
|
+ KernelResult result = KernelResult.Success;
|
|
|
|
|
|
|
|
- if (result == KernelResult.Success)
|
|
|
|
|
- {
|
|
|
|
|
- result = context.Process.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
- info.NroMappedAddress,
|
|
|
|
|
- info.Executable.SourceAddress,
|
|
|
|
|
- textSize + roSize);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (info.Executable.BssSize != 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ result = _owner.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
+ info.NroMappedAddress + textSize + roSize + dataSize,
|
|
|
|
|
+ info.Executable.BssAddress,
|
|
|
|
|
+ bssSize);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return (ResultCode)result;
|
|
|
|
|
|
|
+ if (result == KernelResult.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ result = _owner.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
+ info.NroMappedAddress + textSize + roSize,
|
|
|
|
|
+ info.Executable.SourceAddress + textSize + roSize,
|
|
|
|
|
+ dataSize);
|
|
|
|
|
+
|
|
|
|
|
+ if (result == KernelResult.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ result = _owner.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
+ info.NroMappedAddress,
|
|
|
|
|
+ info.Executable.SourceAddress,
|
|
|
|
|
+ textSize + roSize);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return ResultCode.BadNroAddress;
|
|
|
|
|
|
|
+ return (ResultCode)result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ResultCode IsInitialized(KProcess process)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (_owner != null && _owner.Pid == process.Pid)
|
|
|
|
|
+ {
|
|
|
|
|
+ return ResultCode.Success;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return ResultCode.InvalidProcess;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[Command(0)]
|
|
[Command(0)]
|
|
|
// LoadNro(u64, u64, u64, u64, u64, pid) -> u64
|
|
// LoadNro(u64, u64, u64, u64, u64, pid) -> u64
|
|
|
public ResultCode LoadNro(ServiceCtx context)
|
|
public ResultCode LoadNro(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
- ResultCode result = ResultCode.BadInitialization;
|
|
|
|
|
|
|
+ ResultCode result = IsInitialized(context.Process);
|
|
|
|
|
|
|
|
// Zero
|
|
// Zero
|
|
|
context.RequestData.ReadUInt64();
|
|
context.RequestData.ReadUInt64();
|
|
@@ -369,19 +433,24 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
|
|
|
|
|
ulong nroMappedAddress = 0;
|
|
ulong nroMappedAddress = 0;
|
|
|
|
|
|
|
|
- if (_isInitialized)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
NroInfo info;
|
|
NroInfo info;
|
|
|
|
|
|
|
|
result = ParseNro(out info, context, nroHeapAddress, nroSize, bssHeapAddress, bssSize);
|
|
result = ParseNro(out info, context, nroHeapAddress, nroSize, bssHeapAddress, bssSize);
|
|
|
|
|
|
|
|
- if (result == 0)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
- result = MapNro(context, info, out nroMappedAddress);
|
|
|
|
|
|
|
+ result = MapNro(context.Process, info, out nroMappedAddress);
|
|
|
|
|
|
|
|
- if (result == 0)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
- _nroInfos.Add(info);
|
|
|
|
|
|
|
+ result = (ResultCode)SetNroMemoryPermissions(context.Process, info.Executable, nroMappedAddress);
|
|
|
|
|
+
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
|
|
+ {
|
|
|
|
|
+ _nroInfos.Add(info);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -395,21 +464,21 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
// UnloadNro(u64, u64, pid)
|
|
// UnloadNro(u64, u64, pid)
|
|
|
public ResultCode UnloadNro(ServiceCtx context)
|
|
public ResultCode UnloadNro(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
- ResultCode result = ResultCode.BadInitialization;
|
|
|
|
|
|
|
+ ResultCode result = IsInitialized(context.Process);
|
|
|
|
|
|
|
|
// Zero
|
|
// Zero
|
|
|
context.RequestData.ReadUInt64();
|
|
context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
ulong nroMappedAddress = context.RequestData.ReadUInt64();
|
|
ulong nroMappedAddress = context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
- if (_isInitialized)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
if ((nroMappedAddress & 0xFFF) != 0)
|
|
if ((nroMappedAddress & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.UnalignedAddress;
|
|
|
|
|
|
|
+ return ResultCode.InvalidAddress;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- result = RemoveNroInfo(context, nroMappedAddress);
|
|
|
|
|
|
|
+ result = RemoveNroInfo(nroMappedAddress);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
return result;
|
|
@@ -419,24 +488,24 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
// LoadNrr(u64, u64, u64, pid)
|
|
// LoadNrr(u64, u64, u64, pid)
|
|
|
public ResultCode LoadNrr(ServiceCtx context)
|
|
public ResultCode LoadNrr(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
- ResultCode result = ResultCode.BadInitialization;
|
|
|
|
|
|
|
+ ResultCode result = IsInitialized(context.Process);
|
|
|
|
|
|
|
|
- // Zero
|
|
|
|
|
|
|
+ // pid placeholder, zero
|
|
|
context.RequestData.ReadUInt64();
|
|
context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
long nrrAddress = context.RequestData.ReadInt64();
|
|
long nrrAddress = context.RequestData.ReadInt64();
|
|
|
long nrrSize = context.RequestData.ReadInt64();
|
|
long nrrSize = context.RequestData.ReadInt64();
|
|
|
|
|
|
|
|
- if (_isInitialized)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
NrrInfo info;
|
|
NrrInfo info;
|
|
|
result = ParseNrr(out info, context, nrrAddress, nrrSize);
|
|
result = ParseNrr(out info, context, nrrAddress, nrrSize);
|
|
|
|
|
|
|
|
- if (result == 0)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
if (_nrrInfos.Count >= MaxNrr)
|
|
if (_nrrInfos.Count >= MaxNrr)
|
|
|
{
|
|
{
|
|
|
- result = ResultCode.MaxNrr;
|
|
|
|
|
|
|
+ result = ResultCode.NotLoaded;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -452,18 +521,18 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
// UnloadNrr(u64, u64, pid)
|
|
// UnloadNrr(u64, u64, pid)
|
|
|
public ResultCode UnloadNrr(ServiceCtx context)
|
|
public ResultCode UnloadNrr(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
- ResultCode result = ResultCode.BadInitialization;
|
|
|
|
|
|
|
+ ResultCode result = IsInitialized(context.Process);
|
|
|
|
|
|
|
|
- // Zero
|
|
|
|
|
|
|
+ // pid placeholder, zero
|
|
|
context.RequestData.ReadUInt64();
|
|
context.RequestData.ReadUInt64();
|
|
|
|
|
|
|
|
long nrrHeapAddress = context.RequestData.ReadInt64();
|
|
long nrrHeapAddress = context.RequestData.ReadInt64();
|
|
|
|
|
|
|
|
- if (_isInitialized)
|
|
|
|
|
|
|
+ if (result == ResultCode.Success)
|
|
|
{
|
|
{
|
|
|
if ((nrrHeapAddress & 0xFFF) != 0)
|
|
if ((nrrHeapAddress & 0xFFF) != 0)
|
|
|
{
|
|
{
|
|
|
- return ResultCode.UnalignedAddress;
|
|
|
|
|
|
|
+ return ResultCode.InvalidAddress;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
result = RemoveNrrInfo(nrrHeapAddress);
|
|
result = RemoveNrrInfo(nrrHeapAddress);
|
|
@@ -476,10 +545,24 @@ namespace Ryujinx.HLE.HOS.Services.Loader
|
|
|
// Initialize(u64, pid, KObject)
|
|
// Initialize(u64, pid, KObject)
|
|
|
public ResultCode Initialize(ServiceCtx context)
|
|
public ResultCode Initialize(ServiceCtx context)
|
|
|
{
|
|
{
|
|
|
- // TODO: we actually ignore the pid and process handle receive, we will need to use them when we will have multi process support.
|
|
|
|
|
- _isInitialized = true;
|
|
|
|
|
|
|
+ if (_owner != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ return ResultCode.InvalidSession;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _owner = context.Process;
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
return ResultCode.Success;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public void Dispose()
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach (NroInfo info in _nroInfos)
|
|
|
|
|
+ {
|
|
|
|
|
+ UnmapNroFromInfo(info);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _nroInfos.Clear();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|