2020-07-09 06:31:15 +02:00
|
|
|
|
using LibHac.FsSystem;
|
|
|
|
|
using Ryujinx.Common;
|
2020-05-04 00:54:50 +02:00
|
|
|
|
using Ryujinx.Cpu;
|
2018-12-18 06:33:36 +01:00
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Memory;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
using Ryujinx.HLE.Loaders.Executables;
|
|
|
|
|
using Ryujinx.HLE.Utilities;
|
2019-11-08 15:49:48 +01:00
|
|
|
|
using System;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.Ro
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-10 17:59:54 +02:00
|
|
|
|
[Service("ldr:ro")]
|
2019-09-19 02:45:11 +02:00
|
|
|
|
[Service("ro:1")] // 7.0.0+
|
2019-11-08 15:49:48 +01:00
|
|
|
|
class IRoInterface : IpcService, IDisposable
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private const int MaxNrr = 0x40;
|
|
|
|
|
private const int MaxNro = 0x40;
|
|
|
|
|
private const int MaxMapRetries = 0x200;
|
|
|
|
|
private const int GuardPagesSize = 0x4000;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
private const uint NrrMagic = 0x3052524E;
|
|
|
|
|
private const uint NroMagic = 0x304F524E;
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
private List<NrrInfo> _nrrInfos;
|
|
|
|
|
private List<NroInfo> _nroInfos;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private KProcess _owner;
|
|
|
|
|
|
|
|
|
|
private static Random _random = new Random();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-10 17:59:54 +02:00
|
|
|
|
public IRoInterface(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_nrrInfos = new List<NrrInfo>(MaxNrr);
|
|
|
|
|
_nroInfos = new List<NroInfo>(MaxNro);
|
2019-11-08 15:49:48 +01:00
|
|
|
|
_owner = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
private ResultCode ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, long nrrAddress, long nrrSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
nrrInfo = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (nrrSize == 0 || nrrAddress + nrrSize <= nrrAddress || (nrrSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
|
else if ((nrrAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidAddress;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
StructReader reader = new StructReader(context.Memory, nrrAddress);
|
|
|
|
|
NrrHeader header = reader.Read<NrrHeader>();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (header.Magic != NrrMagic)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.InvalidNrr;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
|
else if (header.NrrSize != nrrSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
List<byte[]> hashes = new List<byte[]>();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
for (int i = 0; i < header.HashCount; i++)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
|
byte[] temp = new byte[0x20];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read((ulong)(nrrAddress + header.HashOffset + (i * 0x20)), temp);
|
|
|
|
|
|
|
|
|
|
hashes.Add(temp);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
nrrInfo = new NrrInfo(nrrAddress, header, hashes);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
public bool IsNroHashPresent(byte[] nroHash)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
foreach (NrrInfo info in _nrrInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
foreach (byte[] hash in info.Hashes)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (hash.SequenceEqual(nroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
public bool IsNroLoaded(byte[] nroHash)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
foreach (NroInfo info in _nroInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (info.Hash.SequenceEqual(nroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode ParseNro(out NroInfo res, ServiceCtx context, ulong nroAddress, ulong nroSize, ulong bssAddress, ulong bssSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
res = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (_nroInfos.Count >= MaxNro)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.TooManyNro;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
|
else if (nroSize == 0 || nroAddress + nroSize <= nroAddress || (nroSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
|
else if (bssSize != 0 && bssAddress + bssSize <= bssAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
|
else if ((nroAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidAddress;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
|
uint magic = context.Memory.Read<uint>(nroAddress + 0x10);
|
|
|
|
|
uint nroFileSize = context.Memory.Read<uint>(nroAddress + 0x18);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (magic != NroMagic || nroSize != nroFileSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.InvalidNro;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
|
byte[] nroData = new byte[nroSize];
|
|
|
|
|
|
|
|
|
|
context.Memory.Read(nroAddress, nroData);
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
byte[] nroHash = null;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
MemoryStream stream = new MemoryStream(nroData);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
using (SHA256 hasher = SHA256.Create())
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
nroHash = hasher.ComputeHash(stream);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (!IsNroHashPresent(nroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.NotRegistered;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (IsNroLoaded(nroHash))
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.AlreadyLoaded;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
stream.Position = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
|
NroExecutable nro = new NroExecutable(stream.AsStorage(), nroAddress, bssAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
|
// Check if everything is page align.
|
|
|
|
|
if ((nro.Text.Length & 0xFFF) != 0 || (nro.Ro.Length & 0xFFF) != 0 ||
|
|
|
|
|
(nro.Data.Length & 0xFFF) != 0 || (nro.BssSize & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.InvalidNro;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
|
// Check if everything is contiguous.
|
|
|
|
|
if (nro.RoOffset != nro.TextOffset + nro.Text.Length ||
|
|
|
|
|
nro.DataOffset != nro.RoOffset + nro.Ro.Length ||
|
|
|
|
|
nroFileSize != nro.DataOffset + nro.Data.Length)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.InvalidNro;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
|
// Check the bss size match.
|
|
|
|
|
if ((ulong)nro.BssSize != bssSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.InvalidNro;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 06:31:15 +02:00
|
|
|
|
int totalSize = nro.Text.Length + nro.Ro.Length + nro.Data.Length + nro.BssSize;
|
|
|
|
|
|
|
|
|
|
// Apply patches
|
|
|
|
|
context.Device.FileSystem.ModLoader.ApplyNroPatches(nro);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
res = new NroInfo(
|
2020-07-09 06:31:15 +02:00
|
|
|
|
nro,
|
2018-12-06 12:16:24 +01:00
|
|
|
|
nroHash,
|
|
|
|
|
nroAddress,
|
|
|
|
|
nroSize,
|
|
|
|
|
bssAddress,
|
|
|
|
|
bssSize,
|
|
|
|
|
(ulong)totalSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private ResultCode MapNro(KProcess process, NroInfo info, out ulong nroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
KMemoryManager memMgr = process.MemoryManager;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
int retryCount = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
nroMappedAddress = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
while (retryCount++ < MaxMapRetries)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
ResultCode result = MapCodeMemoryInProcess(process, info.NroAddress, info.NroSize, out nroMappedAddress);
|
|
|
|
|
|
|
|
|
|
if (result != ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (info.BssSize > 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
KernelResult bssMappingResult = memMgr.MapProcessCodeMemory(nroMappedAddress + info.NroSize, info.BssAddress, info.BssSize);
|
|
|
|
|
|
|
|
|
|
if (bssMappingResult == KernelResult.InvalidMemState)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
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;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (CanAddGuardRegionsInProcess(process, nroMappedAddress, info.TotalSize))
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InsufficientAddressSpace;
|
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private bool CanAddGuardRegionsInProcess(KProcess process, ulong baseAddress, ulong size)
|
|
|
|
|
{
|
|
|
|
|
KMemoryManager memMgr = process.MemoryManager;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
KMemoryInfo memInfo = memMgr.QueryMemory(baseAddress - 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (memInfo.State == MemoryState.Unmapped && baseAddress - GuardPagesSize >= memInfo.Address)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
memInfo = memMgr.QueryMemory(baseAddress + size);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (memInfo.State == MemoryState.Unmapped)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return baseAddress + size + GuardPagesSize <= memInfo.Address + memInfo.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private ResultCode MapCodeMemoryInProcess(KProcess process, ulong baseAddress, ulong size, out ulong targetAddress)
|
|
|
|
|
{
|
|
|
|
|
KMemoryManager memMgr = process.MemoryManager;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
targetAddress = 0;
|
|
|
|
|
|
|
|
|
|
int retryCount;
|
|
|
|
|
|
2019-11-26 19:41:17 +01:00
|
|
|
|
ulong addressSpacePageLimit = (memMgr.GetAddrSpaceSize() - size) >> 12;
|
2019-11-08 15:49:48 +01:00
|
|
|
|
|
|
|
|
|
for (retryCount = 0; retryCount < MaxMapRetries; retryCount++)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2019-11-26 19:41:17 +01:00
|
|
|
|
ulong randomOffset = (ulong)(uint)_random.Next(0, (int)addressSpacePageLimit) << 12;
|
|
|
|
|
|
|
|
|
|
targetAddress = memMgr.GetAddrSpaceBaseAddr() + randomOffset;
|
2019-11-08 15:49:48 +01:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (!CanAddGuardRegionsInProcess(process, targetAddress, size))
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
continue;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (retryCount == MaxMapRetries)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.InsufficientAddressSpace;
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private KernelResult SetNroMemoryPermissions(KProcess process, IExecutable relocatableObject, ulong baseAddress)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong textStart = baseAddress + (ulong)relocatableObject.TextOffset;
|
|
|
|
|
ulong roStart = baseAddress + (ulong)relocatableObject.RoOffset;
|
|
|
|
|
ulong dataStart = baseAddress + (ulong)relocatableObject.DataOffset;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong bssStart = dataStart + (ulong)relocatableObject.Data.Length;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
|
process.CpuMemory.Write(textStart, relocatableObject.Text);
|
|
|
|
|
process.CpuMemory.Write(roStart, relocatableObject.Ro);
|
|
|
|
|
process.CpuMemory.Write(dataStart, relocatableObject.Data);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart));
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
KernelResult result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
result = process.MemoryManager.SetProcessMemoryPermission(textStart, roStart - textStart, MemoryPermission.ReadAndExecute);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
result = process.MemoryManager.SetProcessMemoryPermission(roStart, dataStart - roStart, MemoryPermission.Read);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return process.MemoryManager.SetProcessMemoryPermission(dataStart, bssEnd - dataStart, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
private ResultCode RemoveNrrInfo(long nrrAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
foreach (NrrInfo info in _nrrInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (info.NrrAddress == nrrAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_nrrInfos.Remove(info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.NotLoaded;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
private ResultCode RemoveNroInfo(ulong nroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
foreach (NroInfo info in _nroInfos)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (info.NroMappedAddress == nroMappedAddress)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_nroInfos.Remove(info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return UnmapNroFromInfo(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.NotLoaded;
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
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;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
KernelResult result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (info.Executable.BssSize != 0)
|
|
|
|
|
{
|
|
|
|
|
result = _owner.MemoryManager.UnmapProcessCodeMemory(
|
|
|
|
|
info.NroMappedAddress + textSize + roSize + dataSize,
|
|
|
|
|
info.Executable.BssAddress,
|
|
|
|
|
bssSize);
|
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
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);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return (ResultCode)result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ResultCode IsInitialized(KProcess process)
|
|
|
|
|
{
|
|
|
|
|
if (_owner != null && _owner.Pid == process.Pid)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultCode.InvalidProcess;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
|
[Command(0)]
|
2018-10-10 01:01:49 +02:00
|
|
|
|
// LoadNro(u64, u64, u64, u64, u64, pid) -> u64
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode LoadNro(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
ResultCode result = IsInitialized(context.Process);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
|
|
|
|
// Zero
|
2018-12-06 12:16:24 +01:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong nroHeapAddress = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong nroSize = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong bssHeapAddress = context.RequestData.ReadUInt64();
|
|
|
|
|
ulong bssSize = context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong nroMappedAddress = 0;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
NroInfo info;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
result = ParseNro(out info, context, nroHeapAddress, nroSize, bssHeapAddress, bssSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
result = MapNro(context.Process, info, out nroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
result = (ResultCode)SetNroMemoryPermissions(context.Process, info.Executable, nroMappedAddress);
|
|
|
|
|
|
|
|
|
|
if (result == ResultCode.Success)
|
|
|
|
|
{
|
2019-11-26 19:41:17 +01:00
|
|
|
|
info.NroMappedAddress = nroMappedAddress;
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
_nroInfos.Add(info);
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
context.ResponseData.Write(nroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
|
[Command(1)]
|
2018-10-10 01:01:49 +02:00
|
|
|
|
// UnloadNro(u64, u64, pid)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode UnloadNro(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
ResultCode result = IsInitialized(context.Process);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
|
// Zero
|
2018-12-06 12:16:24 +01:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
ulong nroMappedAddress = context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if ((nroMappedAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidAddress;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
result = RemoveNroInfo(nroMappedAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
|
[Command(2)]
|
2018-10-10 01:01:49 +02:00
|
|
|
|
// LoadNrr(u64, u64, u64, pid)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode LoadNrr(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
ResultCode result = IsInitialized(context.Process);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
// pid placeholder, zero
|
2018-12-06 12:16:24 +01:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
long nrrAddress = context.RequestData.ReadInt64();
|
|
|
|
|
long nrrSize = context.RequestData.ReadInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
NrrInfo info;
|
|
|
|
|
result = ParseNrr(out info, context, nrrAddress, nrrSize);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if (_nrrInfos.Count >= MaxNrr)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
result = ResultCode.NotLoaded;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
_nrrInfos.Add(info);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
|
[Command(3)]
|
2018-10-10 01:01:49 +02:00
|
|
|
|
// UnloadNrr(u64, u64, pid)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode UnloadNrr(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
ResultCode result = IsInitialized(context.Process);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
// pid placeholder, zero
|
2018-12-06 12:16:24 +01:00
|
|
|
|
context.RequestData.ReadUInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
long nrrHeapAddress = context.RequestData.ReadInt64();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (result == ResultCode.Success)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
|
if ((nrrHeapAddress & 0xFFF) != 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
return ResultCode.InvalidAddress;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
result = RemoveNrrInfo(nrrHeapAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
|
[Command(4)]
|
2018-10-10 01:01:49 +02:00
|
|
|
|
// Initialize(u64, pid, KObject)
|
2019-07-14 21:04:38 +02:00
|
|
|
|
public ResultCode Initialize(ServiceCtx context)
|
2018-10-10 01:01:49 +02:00
|
|
|
|
{
|
2019-11-08 15:49:48 +01:00
|
|
|
|
if (_owner != null)
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.InvalidSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_owner = context.Process;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
|
return ResultCode.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2019-11-08 15:49:48 +01:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (NroInfo info in _nroInfos)
|
|
|
|
|
{
|
|
|
|
|
UnmapNroFromInfo(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_nroInfos.Clear();
|
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
}
|
2019-07-12 03:13:43 +02:00
|
|
|
|
}
|