2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2020-12-01 18:23:43 -05:00
|
|
|
using Ryujinx.Memory;
|
2018-08-15 14:59:51 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-12-01 18:23:43 -05:00
|
|
|
using System.Diagnostics;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
class KMemoryManager
|
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
private static readonly int[] MappingUnitSizes = new int[]
|
|
|
|
{
|
|
|
|
0x1000,
|
|
|
|
0x10000,
|
|
|
|
0x200000,
|
|
|
|
0x400000,
|
|
|
|
0x2000000,
|
|
|
|
0x40000000
|
|
|
|
};
|
|
|
|
|
2018-08-15 14:59:51 -04:00
|
|
|
public const int PageSize = 0x1000;
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
private const int KMemoryBlockSize = 0x40;
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// We need 2 blocks for the case where a big block
|
|
|
|
// needs to be split in 2, plus one block that will be the new one inserted.
|
2018-11-28 17:18:09 -05:00
|
|
|
private const int MaxBlocksNeededForInsertion = 2;
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private readonly LinkedList<KMemoryBlock> _blocks;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private readonly IVirtualMemoryManager _cpuMemory;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private readonly KernelContext _context;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
public ulong AddrSpaceStart { get; private set; }
|
|
|
|
public ulong AddrSpaceEnd { get; private set; }
|
|
|
|
|
|
|
|
public ulong CodeRegionStart { get; private set; }
|
|
|
|
public ulong CodeRegionEnd { get; private set; }
|
|
|
|
|
|
|
|
public ulong HeapRegionStart { get; private set; }
|
|
|
|
public ulong HeapRegionEnd { get; private set; }
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private ulong _currentHeapAddr;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
public ulong AliasRegionStart { get; private set; }
|
|
|
|
public ulong AliasRegionEnd { get; private set; }
|
|
|
|
|
|
|
|
public ulong StackRegionStart { get; private set; }
|
|
|
|
public ulong StackRegionEnd { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public ulong TlsIoRegionStart { get; private set; }
|
|
|
|
public ulong TlsIoRegionEnd { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private ulong _heapCapacity;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public ulong PhysicalMemoryUsage { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private MemoryRegion _memRegion;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private bool _aslrDisabled;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public int AddrSpaceWidth { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private bool _isKernel;
|
2020-04-20 17:59:59 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private bool _aslrEnabled;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KMemoryBlockAllocator _blockAllocator;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private int _contextId;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private MersenneTwister _randomNumberGenerator;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public KMemoryManager(KernelContext context, IVirtualMemoryManager cpuMemory)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
_context = context;
|
2018-12-06 06:16:24 -05:00
|
|
|
_cpuMemory = cpuMemory;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_blocks = new LinkedList<KMemoryBlock>();
|
2020-04-20 17:59:59 -04:00
|
|
|
|
|
|
|
_isKernel = false;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
private static readonly int[] AddrSpaceSizes = new int[] { 32, 36, 32, 39 };
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public KernelResult InitializeForProcess(
|
2018-12-06 06:16:24 -05:00
|
|
|
AddressSpaceType addrSpaceType,
|
|
|
|
bool aslrEnabled,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion memRegion,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KMemoryBlockAllocator blockAllocator)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((uint)addrSpaceType > (uint)AddressSpaceType.Addr39Bits)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException(nameof(addrSpaceType));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
_contextId = _context.ContextIdManager.GetId();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong addrSpaceBase = 0;
|
|
|
|
ulong addrSpaceSize = 1UL << AddrSpaceSizes[(int)addrSpaceType];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = CreateUserAddressSpace(
|
|
|
|
addrSpaceType,
|
|
|
|
aslrEnabled,
|
|
|
|
aslrDisabled,
|
|
|
|
addrSpaceBase,
|
|
|
|
addrSpaceSize,
|
|
|
|
memRegion,
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
blockAllocator);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
_context.ContextIdManager.PutId(_contextId);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Region
|
|
|
|
{
|
|
|
|
public ulong Start;
|
|
|
|
public ulong End;
|
|
|
|
public ulong Size;
|
|
|
|
public ulong AslrOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult CreateUserAddressSpace(
|
2018-12-06 06:16:24 -05:00
|
|
|
AddressSpaceType addrSpaceType,
|
|
|
|
bool aslrEnabled,
|
|
|
|
bool aslrDisabled,
|
|
|
|
ulong addrSpaceStart,
|
|
|
|
ulong addrSpaceEnd,
|
|
|
|
MemoryRegion memRegion,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KMemoryBlockAllocator blockAllocator)
|
|
|
|
{
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
Region aliasRegion = new Region();
|
|
|
|
Region heapRegion = new Region();
|
|
|
|
Region stackRegion = new Region();
|
|
|
|
Region tlsIoRegion = new Region();
|
|
|
|
|
|
|
|
ulong codeRegionSize;
|
|
|
|
ulong stackAndTlsIoStart;
|
|
|
|
ulong stackAndTlsIoEnd;
|
|
|
|
ulong baseAddress;
|
|
|
|
|
|
|
|
switch (addrSpaceType)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
case AddressSpaceType.Addr32Bits:
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.Size = 0x40000000;
|
|
|
|
heapRegion.Size = 0x40000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-06 06:16:24 -05:00
|
|
|
codeRegionSize = 0x3fe00000;
|
|
|
|
stackAndTlsIoStart = 0x200000;
|
|
|
|
stackAndTlsIoEnd = 0x40000000;
|
|
|
|
baseAddress = 0x200000;
|
2018-11-28 17:18:09 -05:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 14:59:51 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr36Bits:
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.Size = 0x180000000;
|
|
|
|
heapRegion.Size = 0x180000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
CodeRegionStart = 0x8000000;
|
2018-12-06 06:16:24 -05:00
|
|
|
codeRegionSize = 0x78000000;
|
|
|
|
stackAndTlsIoStart = 0x8000000;
|
|
|
|
stackAndTlsIoEnd = 0x80000000;
|
|
|
|
baseAddress = 0x8000000;
|
2018-11-28 17:18:09 -05:00
|
|
|
AddrSpaceWidth = 36;
|
2018-08-15 14:59:51 -04:00
|
|
|
break;
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
case AddressSpaceType.Addr32BitsNoMap:
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.Size = 0;
|
|
|
|
heapRegion.Size = 0x80000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-06 06:16:24 -05:00
|
|
|
codeRegionSize = 0x3fe00000;
|
|
|
|
stackAndTlsIoStart = 0x200000;
|
|
|
|
stackAndTlsIoEnd = 0x40000000;
|
|
|
|
baseAddress = 0x200000;
|
2018-11-28 17:18:09 -05:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 14:59:51 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr39Bits:
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.Size = 0x1000000000;
|
|
|
|
heapRegion.Size = 0x180000000;
|
|
|
|
stackRegion.Size = 0x80000000;
|
|
|
|
tlsIoRegion.Size = 0x1000000000;
|
|
|
|
CodeRegionStart = BitUtils.AlignDown(address, 0x200000);
|
|
|
|
codeRegionSize = BitUtils.AlignUp (endAddr, 0x200000) - CodeRegionStart;
|
|
|
|
stackAndTlsIoStart = 0;
|
|
|
|
stackAndTlsIoEnd = 0;
|
|
|
|
baseAddress = 0x8000000;
|
2018-11-28 17:18:09 -05:00
|
|
|
AddrSpaceWidth = 39;
|
2018-08-15 14:59:51 -04:00
|
|
|
break;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
default: throw new ArgumentException(nameof(addrSpaceType));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
CodeRegionEnd = CodeRegionStart + codeRegionSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong mapBaseAddress;
|
|
|
|
ulong mapAvailableSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (CodeRegionStart - baseAddress >= addrSpaceEnd - CodeRegionEnd)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Has more space before the start of the code region.
|
2018-12-06 06:16:24 -05:00
|
|
|
mapBaseAddress = baseAddress;
|
|
|
|
mapAvailableSize = CodeRegionStart - baseAddress;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Has more space after the end of the code region.
|
2018-12-06 06:16:24 -05:00
|
|
|
mapBaseAddress = CodeRegionEnd;
|
|
|
|
mapAvailableSize = addrSpaceEnd - CodeRegionEnd;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong mapTotalSize = aliasRegion.Size + heapRegion.Size + stackRegion.Size + tlsIoRegion.Size;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong aslrMaxOffset = mapAvailableSize - mapTotalSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_aslrEnabled = aslrEnabled;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddrSpaceStart = addrSpaceStart;
|
|
|
|
AddrSpaceEnd = addrSpaceEnd;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_blockAllocator = blockAllocator;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (mapAvailableSize < mapTotalSize)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (aslrEnabled)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.AslrOffset = GetRandomValue(0, aslrMaxOffset >> 21) << 21;
|
|
|
|
heapRegion.AslrOffset = GetRandomValue(0, aslrMaxOffset >> 21) << 21;
|
|
|
|
stackRegion.AslrOffset = GetRandomValue(0, aslrMaxOffset >> 21) << 21;
|
|
|
|
tlsIoRegion.AslrOffset = GetRandomValue(0, aslrMaxOffset >> 21) << 21;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Regions are sorted based on ASLR offset.
|
|
|
|
// When ASLR is disabled, the order is Map, Heap, NewMap and TlsIo.
|
2018-12-06 06:16:24 -05:00
|
|
|
aliasRegion.Start = mapBaseAddress + aliasRegion.AslrOffset;
|
|
|
|
aliasRegion.End = aliasRegion.Start + aliasRegion.Size;
|
|
|
|
heapRegion.Start = mapBaseAddress + heapRegion.AslrOffset;
|
|
|
|
heapRegion.End = heapRegion.Start + heapRegion.Size;
|
|
|
|
stackRegion.Start = mapBaseAddress + stackRegion.AslrOffset;
|
|
|
|
stackRegion.End = stackRegion.Start + stackRegion.Size;
|
|
|
|
tlsIoRegion.Start = mapBaseAddress + tlsIoRegion.AslrOffset;
|
|
|
|
tlsIoRegion.End = tlsIoRegion.Start + tlsIoRegion.Size;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
SortRegion(heapRegion, aliasRegion);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (stackRegion.Size != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
SortRegion(stackRegion, aliasRegion);
|
|
|
|
SortRegion(stackRegion, heapRegion);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
stackRegion.Start = stackAndTlsIoStart;
|
|
|
|
stackRegion.End = stackAndTlsIoEnd;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (tlsIoRegion.Size != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
SortRegion(tlsIoRegion, aliasRegion);
|
|
|
|
SortRegion(tlsIoRegion, heapRegion);
|
|
|
|
SortRegion(tlsIoRegion, stackRegion);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
tlsIoRegion.Start = stackAndTlsIoStart;
|
|
|
|
tlsIoRegion.End = stackAndTlsIoEnd;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AliasRegionStart = aliasRegion.Start;
|
|
|
|
AliasRegionEnd = aliasRegion.End;
|
|
|
|
HeapRegionStart = heapRegion.Start;
|
|
|
|
HeapRegionEnd = heapRegion.End;
|
|
|
|
StackRegionStart = stackRegion.Start;
|
|
|
|
StackRegionEnd = stackRegion.End;
|
|
|
|
TlsIoRegionStart = tlsIoRegion.Start;
|
|
|
|
TlsIoRegionEnd = tlsIoRegion.End;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_currentHeapAddr = HeapRegionStart;
|
|
|
|
_heapCapacity = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
PhysicalMemoryUsage = 0;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_memRegion = memRegion;
|
|
|
|
_aslrDisabled = aslrDisabled;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return InitializeBlocks(addrSpaceStart, addrSpaceEnd);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private ulong GetRandomValue(ulong min, ulong max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return (ulong)GetRandomValue((long)min, (long)max);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private long GetRandomValue(long min, long max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (_randomNumberGenerator == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_randomNumberGenerator = new MersenneTwister(0);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return _randomNumberGenerator.GenRandomNumber(min, max);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private static void SortRegion(Region lhs, Region rhs)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (lhs.AslrOffset < rhs.AslrOffset)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
rhs.Start += lhs.Size;
|
|
|
|
rhs.End += lhs.Size;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lhs.Start += rhs.Size;
|
|
|
|
lhs.End += rhs.Size;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult InitializeBlocks(ulong addrSpaceStart, ulong addrSpaceEnd)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// First insertion will always need only a single block,
|
|
|
|
// because there's nothing else to split.
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(1))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong addrSpacePagesCount = (addrSpaceEnd - addrSpaceStart) / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
_blocks.AddFirst(new KMemoryBlock(
|
|
|
|
addrSpaceStart,
|
|
|
|
addrSpacePagesCount,
|
|
|
|
MemoryState.Unmapped,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.None));
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public KernelResult MapPages(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address,
|
|
|
|
KPageList pageList,
|
|
|
|
MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = pageList.GetPagesCount();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if (!CanContain(address, size, state))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!IsUnmapped(address, pagesCount * PageSize))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MapPages(address, pageList, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, state, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult UnmapPages(ulong address, KPageList pageList, MemoryState stateExpected)
|
2018-10-09 19:01:49 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = pageList.GetPagesCount();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong addrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (AddrSpaceStart > address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (addrSpacePagesCount < pagesCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (endAddr - 1 > AddrSpaceEnd - 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-10-09 19:01:49 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList currentPageList = new KPageList();
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddVaRangeToPageList(currentPageList, address, pagesCount);
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!currentPageList.IsEqual(pageList))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
if (CheckRange(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
size,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Mask,
|
2018-12-06 06:16:24 -05:00
|
|
|
stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState state,
|
2018-11-28 17:18:09 -05:00
|
|
|
out _,
|
|
|
|
out _))
|
2018-10-09 19:01:49 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MmuUnmap(address, pagesCount);
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public KernelResult MapNormalMemory(long address, long size, KMemoryPermission permission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// TODO.
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public KernelResult MapIoMemory(long address, long size, KMemoryPermission permission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// TODO.
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public KernelResult AllocateOrMapPa(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment,
|
|
|
|
ulong srcPa,
|
|
|
|
bool map,
|
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2018-12-06 06:16:24 -05:00
|
|
|
out ulong address)
|
2018-10-09 19:01:49 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
address = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong regionSize = regionPagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong regionEndAddr = regionStart + regionSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if (!CanContain(regionStart, regionSize, state))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (regionPagesCount <= neededPagesCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-10-09 19:01:49 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
address = AllocateVa(regionStart, regionPagesCount, neededPagesCount, alignment);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (address == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryOperation operation = map
|
2018-11-28 17:18:09 -05:00
|
|
|
? MemoryOperation.MapPa
|
|
|
|
: MemoryOperation.Allocate;
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
neededPagesCount,
|
|
|
|
srcPa,
|
|
|
|
map,
|
|
|
|
permission,
|
|
|
|
operation);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, neededPagesCount, state, permission);
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public KernelResult MapNewProcessCode(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if (!CanContain(address, size, state))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!IsUnmapped(address, size))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 17:18:09 -05:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-06 06:16:24 -05:00
|
|
|
permission,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.Allocate);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, state, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult MapProcessCodeMemory(ulong dst, ulong src, ulong size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
bool success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission permission,
|
2018-11-28 17:18:09 -05:00
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
success &= IsUnmapped(dst, size);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList pageList = new KPageList();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddVaRangeToPageList(pageList, src, pagesCount);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KernelResult result = MmuChangePermission(src, pagesCount, KMemoryPermission.None);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
result = MapPages(dst, pageList, KMemoryPermission.None);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
MmuChangePermission(src, pagesCount, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
InsertBlock(src, pagesCount, state, KMemoryPermission.None, MemoryAttribute.Borrowed);
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(dst, pagesCount, MemoryState.ModCodeStatic);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult UnmapProcessCodeMemory(ulong dst, ulong src, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
bool success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
2018-11-28 17:18:09 -05:00
|
|
|
PageSize,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState state,
|
2018-11-28 17:18:09 -05:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
|
|
|
size,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Mask,
|
2018-12-06 06:16:24 -05:00
|
|
|
state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MmuUnmap(dst, pagesCount);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// TODO: Missing some checks here.
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Unmapped);
|
2020-12-01 18:23:43 -05:00
|
|
|
InsertBlock(src, pagesCount, MemoryState.Heap, KMemoryPermission.ReadAndWrite);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult SetHeapSize(ulong size, out ulong address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
address = 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (size > HeapRegionEnd - HeapRegionStart)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-07-17 00:18:31 -04:00
|
|
|
ulong currentHeapSize = GetHeapSize();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
if (currentHeapSize <= size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-07-17 00:18:31 -04:00
|
|
|
// Expand.
|
|
|
|
ulong sizeDelta = size - currentHeapSize;
|
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null && sizeDelta != 0 &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, sizeDelta))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
ulong pagesCount = sizeDelta / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = region.AllocatePages(pagesCount, _aslrDisabled, out KPageList pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
void CleanUpForError()
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (pageList != null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
region.FreePages(pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
if (currentProcess.ResourceLimit != null && sizeDelta != 0)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-07-17 00:18:31 -04:00
|
|
|
currentProcess.ResourceLimit.Release(LimitableResource.Memory, sizeDelta);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
if (!IsUnmapped(_currentHeapAddr, sizeDelta))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = DoMmuOperation(
|
|
|
|
_currentHeapAddr,
|
|
|
|
pagesCount,
|
|
|
|
pageList,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.MapVa);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
InsertBlock(_currentHeapAddr, pagesCount, MemoryState.Heap, KMemoryPermission.ReadAndWrite);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2020-07-17 00:18:31 -04:00
|
|
|
else
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-07-17 00:18:31 -04:00
|
|
|
// Shrink.
|
|
|
|
ulong freeAddr = HeapRegionStart + size;
|
|
|
|
ulong sizeDelta = currentHeapSize - size;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckRange(
|
2018-12-06 06:16:24 -05:00
|
|
|
freeAddr,
|
2020-07-17 00:18:31 -04:00
|
|
|
sizeDelta,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
2018-11-28 17:18:09 -05:00
|
|
|
out _))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
ulong pagesCount = sizeDelta / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MmuUnmap(freeAddr, pagesCount);
|
2018-08-17 09:39:35 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, sizeDelta);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(freeAddr, pagesCount, MemoryState.Unmapped);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:18:31 -04:00
|
|
|
_currentHeapAddr = HeapRegionStart + size;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
address = HeapRegionStart;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public ulong GetTotalHeapSize()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return GetHeapSize() + PhysicalMemoryUsage;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
private ulong GetHeapSize()
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return _currentHeapAddr - HeapRegionStart;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult SetHeapCapacity(ulong capacity)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_heapCapacity = capacity;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult SetMemoryAttribute(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeValue)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.AttributeChangeAllowed,
|
|
|
|
MemoryState.AttributeChangeAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.BorrowedAndIpcMapped,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.DeviceMappedAndUncached,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission permission,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryAttribute attribute))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
attribute &= ~attributeMask;
|
|
|
|
attribute |= attributeMask & attributeValue;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, state, permission, attribute);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KMemoryInfo QueryMemory(ulong address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (address >= AddrSpaceStart &&
|
|
|
|
address < AddrSpaceEnd)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return FindBlock(address).GetInfo();
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new KMemoryInfo(
|
|
|
|
AddrSpaceEnd,
|
2018-11-28 17:18:09 -05:00
|
|
|
~AddrSpaceEnd + 1,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.Reserved,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.None,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult Map(ulong dst, ulong src, ulong size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
bool success;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState srcState,
|
2018-08-15 14:59:51 -04:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
success &= IsUnmapped(dst, size);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList pageList = new KPageList();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddVaRangeToPageList(pageList, src, pagesCount);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KernelResult result = MmuChangePermission(src, pagesCount, KMemoryPermission.None);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
result = MapPages(dst, pageList, KMemoryPermission.ReadAndWrite);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
if (MmuChangePermission(src, pagesCount, KMemoryPermission.ReadAndWrite) != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure reverting memory permission.");
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
InsertBlock(src, pagesCount, srcState, KMemoryPermission.None, MemoryAttribute.Borrowed);
|
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Stack, KMemoryPermission.ReadAndWrite);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult UnmapForKernel(ulong address, ulong pagesCount, MemoryState stateExpected)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
size,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Mask,
|
2018-12-06 06:16:24 -05:00
|
|
|
stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MmuUnmap(address, pagesCount);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult Unmap(ulong dst, ulong src, ulong size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
bool success;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState srcState,
|
2018-08-15 14:59:51 -04:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.Mask,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryState.Stack,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission dstPermission,
|
2018-08-15 14:59:51 -04:00
|
|
|
out _);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList srcPageList = new KPageList();
|
|
|
|
KPageList dstPageList = new KPageList();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddVaRangeToPageList(srcPageList, src, pagesCount);
|
|
|
|
AddVaRangeToPageList(dstPageList, dst, pagesCount);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!dstPageList.IsEqual(srcPageList))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = MmuUnmap(dst, pagesCount);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
result = MmuChangePermission(src, pagesCount, KMemoryPermission.ReadAndWrite);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
MapPages(dst, dstPageList, dstPermission);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
InsertBlock(src, pagesCount, srcState, KMemoryPermission.ReadAndWrite);
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Unmapped);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public KernelResult SetProcessMemoryPermission(ulong address, ulong size, KMemoryPermission permission)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 06:16:24 -05:00
|
|
|
out MemoryState oldState,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission oldPermission,
|
2018-08-15 14:59:51 -04:00
|
|
|
out _))
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryState newState = oldState;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// If writing into the code region is allowed, then we need
|
|
|
|
// to change it to mutable.
|
2020-12-01 18:23:43 -05:00
|
|
|
if ((permission & KMemoryPermission.Write) != 0)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (oldState == MemoryState.CodeStatic)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
newState = MemoryState.CodeMutable;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (oldState == MemoryState.ModCodeStatic)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
newState = MemoryState.ModCodeMutable;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new InvalidOperationException($"Memory state \"{oldState}\" not valid for this operation.");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (newState != oldState || permission != oldPermission)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
MemoryOperation operation = (permission & KMemoryPermission.Execute) != 0
|
2018-11-28 17:18:09 -05:00
|
|
|
? MemoryOperation.ChangePermsAndAttributes
|
|
|
|
: MemoryOperation.ChangePermRw;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = DoMmuOperation(address, pagesCount, 0, false, permission, operation);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, newState, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult MapPhysicalMemory(ulong address, ulong size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong endAddr = address + size;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong mappedSize = 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.State != MemoryState.Unmapped)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
mappedSize += GetSizeInRange(info, address, endAddr);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (mappedSize == size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong remainingSize = size - mappedSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong remainingPages = remainingSize / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, remainingSize))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.ResLimitExceeded;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = region.AllocatePages(remainingPages, _aslrDisabled, out KPageList pageList);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (pageList != null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
region.FreePages(pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, remainingSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-17 09:39:35 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-17 09:39:35 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
MapPhysicalMemory(pageList, address, endAddr);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
PhysicalMemoryUsage += remainingSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
InsertBlock(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryState.Unmapped,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryState.Heap,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-08-15 14:59:51 -04:00
|
|
|
MemoryAttribute.None);
|
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.Success;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KernelResult UnmapPhysicalMemory(ulong address, ulong size)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong endAddr = address + size;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Scan, ensure that the region can be unmapped (all blocks are heap or
|
|
|
|
// already unmapped), fill pages list for freeing memory.
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong heapMappedSize = 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList pageList = new KPageList();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.State == MemoryState.Heap)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.Attribute != MemoryAttribute.None)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
|
|
|
ulong blockAddress = GetAddrInRange(info, address);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
AddVaRangeToPageList(pageList, blockAddress, blockSize / PageSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
heapMappedSize += blockSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (info.State != MemoryState.Unmapped)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (heapMappedSize == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Try to unmap all the heap mapped memory inside range.
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = KernelResult.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.State == MemoryState.Heap)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
|
|
|
ulong blockAddress = GetAddrInRange(info, address);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = MmuUnmap(blockAddress, blockPagesCount);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// If we failed to unmap, we need to remap everything back again.
|
2018-12-06 06:16:24 -05:00
|
|
|
MapPhysicalMemory(pageList, address, blockAddress + blockSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
GetMemoryRegionManager().FreePages(pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
PhysicalMemoryUsage -= heapMappedSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, heapMappedSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void MapPhysicalMemory(KPageList pageList, ulong address, ulong endAddr)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
LinkedListNode<KPageNode> pageListNode = pageList.Nodes.First;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageNode pageNode = pageListNode.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong srcPa = pageNode.Address;
|
|
|
|
ulong srcPaPages = pageNode.PagesCount;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.State == MemoryState.Unmapped)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong dstVaPages = blockSize / PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong dstVa = GetAddrInRange(info, address);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
while (dstVaPages > 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (srcPaPages == 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
pageListNode = pageListNode.Next;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
pageNode = pageListNode.Value;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
srcPa = pageNode.Address;
|
|
|
|
srcPaPages = pageNode.PagesCount;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = srcPaPages;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (pagesCount > dstVaPages)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
pagesCount = dstVaPages;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
DoMmuOperation(
|
2018-12-06 06:16:24 -05:00
|
|
|
dstVa,
|
|
|
|
pagesCount,
|
|
|
|
srcPa,
|
2018-11-28 17:18:09 -05:00
|
|
|
true,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.ReadAndWrite,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.MapPa);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
dstVa += pagesCount * PageSize;
|
|
|
|
srcPa += pagesCount * PageSize;
|
|
|
|
srcPaPages -= pagesCount;
|
|
|
|
dstVaPages -= pagesCount;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public KernelResult CopyDataToCurrentProcess(
|
|
|
|
ulong dst,
|
|
|
|
ulong size,
|
|
|
|
ulong src,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Client -> server.
|
2019-01-18 17:26:39 -05:00
|
|
|
return CopyDataFromOrToCurrentProcess(
|
|
|
|
size,
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
toServer: true);
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public KernelResult CopyDataFromCurrentProcess(
|
|
|
|
ulong dst,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
ulong src)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Server -> client.
|
2019-01-18 17:26:39 -05:00
|
|
|
return CopyDataFromOrToCurrentProcess(
|
|
|
|
size,
|
|
|
|
dst,
|
|
|
|
src,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
toServer: false);
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
private KernelResult CopyDataFromOrToCurrentProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong clientAddress,
|
|
|
|
ulong serverAddress,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
bool toServer)
|
|
|
|
{
|
|
|
|
if (AddrSpaceStart > clientAddress)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong srcEndAddr = clientAddress + size;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (srcEndAddr <= clientAddress || srcEndAddr - 1 > AddrSpaceEnd - 1)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
clientAddress,
|
|
|
|
size,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
permission,
|
|
|
|
attributeMask | MemoryAttribute.Uncached,
|
|
|
|
attributeExpected))
|
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2021-02-07 18:54:49 -05:00
|
|
|
while (size > 0)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2021-02-07 18:54:49 -05:00
|
|
|
ulong copySize = Math.Min(PageSize - (serverAddress & (PageSize - 1)), PageSize - (clientAddress & (PageSize - 1)));
|
|
|
|
|
|
|
|
if (copySize > size)
|
|
|
|
{
|
|
|
|
copySize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong serverDramAddr = currentProcess.MemoryManager.GetDramAddressFromVa(serverAddress);
|
|
|
|
ulong clientDramAddr = GetDramAddressFromVa(clientAddress);
|
|
|
|
|
|
|
|
if (serverDramAddr != clientDramAddr)
|
|
|
|
{
|
|
|
|
if (toServer)
|
|
|
|
{
|
|
|
|
_context.Memory.Copy(serverDramAddr, clientDramAddr, copySize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_context.Memory.Copy(clientDramAddr, serverDramAddr, copySize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAddress += copySize;
|
|
|
|
clientAddress += copySize;
|
|
|
|
size -= copySize;
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
2018-10-09 19:01:49 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public KernelResult MapBufferFromClientProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong src,
|
|
|
|
KMemoryManager sourceMemMgr,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryState state,
|
|
|
|
bool copyData,
|
|
|
|
out ulong dst)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
dst = 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
KernelResult result = sourceMemMgr.GetPagesForMappingIntoAnotherProcess(
|
|
|
|
src,
|
|
|
|
size,
|
|
|
|
permission,
|
|
|
|
state,
|
|
|
|
copyData,
|
|
|
|
_aslrDisabled,
|
|
|
|
_memRegion,
|
|
|
|
out KPageList pageList);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return result;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
result = MapPagesFromAnotherProcess(size, src, permission, state, pageList, out ulong va);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
sourceMemMgr.UnmapIpcRestorePermission(src, size, state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst = va;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
return result;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
private KernelResult GetPagesForMappingIntoAnotherProcess(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryState state,
|
|
|
|
bool copyData,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion region,
|
|
|
|
out KPageList pageList)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
pageList = null;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (AddrSpaceStart > address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (endAddr <= address || endAddr - 1 > AddrSpaceEnd - 1)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryState stateMask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case MemoryState.IpcBuffer0: stateMask = MemoryState.IpcSendAllowedType0; break;
|
|
|
|
case MemoryState.IpcBuffer1: stateMask = MemoryState.IpcSendAllowedType1; break;
|
|
|
|
case MemoryState.IpcBuffer3: stateMask = MemoryState.IpcSendAllowedType3; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
default: return KernelResult.InvalidCombination;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permissionMask = permission == KMemoryPermission.ReadAndWrite
|
|
|
|
? KMemoryPermission.None
|
|
|
|
: KMemoryPermission.Read;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask = MemoryAttribute.Borrowed | MemoryAttribute.Uncached;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (state == MemoryState.IpcBuffer0)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
attributeMask |= MemoryAttribute.DeviceMapped;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong visitedSize = 0;
|
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
if (visitedSize == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddrVisited = address + visitedSize;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressRounded, endAddrVisited))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
if ((info.Permission & KMemoryPermission.ReadAndWrite) != permissionMask && info.IpcRefCount == 0)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrVisited);
|
|
|
|
|
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
|
|
|
|
|
|
|
if (DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
info.Permission,
|
|
|
|
MemoryOperation.ChangePermRw) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure trying to restore permission.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:42:17 -05:00
|
|
|
// Signal a read for any resources tracking reads in the region, as the other process is likely to use their data.
|
|
|
|
_cpuMemory.SignalMemoryTracking(addressTruncated, endAddrRounded - addressTruncated, false);
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
KernelResult result;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (addressRounded < endAddrTruncated)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressRounded, endAddrTruncated))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
// Check if the block state matches what we expect.
|
|
|
|
if ((info.State & stateMask) != stateMask ||
|
|
|
|
(info.Permission & permission) != permission ||
|
|
|
|
(info.Attribute & attributeMask) != MemoryAttribute.None)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrTruncated);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if ((info.Permission & KMemoryPermission.ReadAndWrite) != permissionMask && info.IpcRefCount == 0)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
result = DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
permissionMask,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
visitedSize += blockSize;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result = GetPagesForIpcTransfer(address, size, copyData, aslrDisabled, region, out pageList);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (visitedSize != 0)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
InsertBlock(addressRounded, visitedSize / PageSize, SetIpcMappingPermissions, permissionMask);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult GetPagesForIpcTransfer(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
bool copyData,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion region,
|
|
|
|
out KPageList pageList)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
// When the start address is unaligned, we can't safely map the
|
|
|
|
// first page as it would expose other undesirable information on the
|
|
|
|
// target process. So, instead we allocate new pages, copy the data
|
|
|
|
// inside the range, and then clear the remaining space.
|
|
|
|
// The same also holds for the last page, if the end address
|
|
|
|
// (address + size) is also not aligned.
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
pageList = null;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
KPageList pages = new KPageList();
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong dstFirstPagePa = 0;
|
|
|
|
ulong dstLastPagePa = 0;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
if (dstFirstPagePa != 0)
|
|
|
|
{
|
|
|
|
FreeSinglePage(region, dstFirstPagePa);
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
if (dstLastPagePa != 0)
|
|
|
|
{
|
|
|
|
FreeSinglePage(region, dstLastPagePa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
// Is the first page address aligned?
|
|
|
|
// If not, allocate a new page and copy the unaligned chunck.
|
|
|
|
if (addressTruncated < addressRounded)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
dstFirstPagePa = AllocateSinglePage(region, aslrDisabled);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (dstFirstPagePa == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong firstPageFillAddress = dstFirstPagePa;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (!TryConvertVaToPa(addressTruncated, out ulong srcFirstPagePa))
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong unusedSizeAfter;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (copyData)
|
|
|
|
{
|
|
|
|
ulong unusedSizeBefore = address - addressTruncated;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
_context.Memory.ZeroFill(GetDramAddressFromPa(dstFirstPagePa), unusedSizeBefore);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong copySize = addressRounded <= endAddr ? addressRounded - address : size;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
_context.Memory.Copy(
|
|
|
|
GetDramAddressFromPa(dstFirstPagePa + unusedSizeBefore),
|
|
|
|
GetDramAddressFromPa(srcFirstPagePa + unusedSizeBefore), copySize);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
firstPageFillAddress += unusedSizeBefore + copySize;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
unusedSizeAfter = addressRounded > endAddr ? addressRounded - endAddr : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unusedSizeAfter = PageSize;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (unusedSizeAfter != 0)
|
|
|
|
{
|
2020-12-01 18:23:43 -05:00
|
|
|
_context.Memory.ZeroFill(GetDramAddressFromPa(firstPageFillAddress), unusedSizeAfter);
|
2020-07-17 00:19:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pages.AddRange(dstFirstPagePa, 1) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
if (endAddrTruncated > addressRounded)
|
|
|
|
{
|
|
|
|
ulong alignedPagesCount = (endAddrTruncated - addressRounded) / PageSize;
|
|
|
|
|
|
|
|
AddVaRangeToPageList(pages, addressRounded, alignedPagesCount);
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
// Is the last page end address aligned?
|
|
|
|
// If not, allocate a new page and copy the unaligned chunck.
|
|
|
|
if (endAddrTruncated < endAddrRounded && (addressTruncated == addressRounded || addressTruncated < endAddrTruncated))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
dstLastPagePa = AllocateSinglePage(region, aslrDisabled);
|
|
|
|
|
|
|
|
if (dstLastPagePa == 0)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong lastPageFillAddr = dstLastPagePa;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (!TryConvertVaToPa(endAddrTruncated, out ulong srcLastPagePa))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong unusedSizeAfter;
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (copyData)
|
|
|
|
{
|
|
|
|
ulong copySize = endAddr - endAddrTruncated;
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
_context.Memory.Copy(
|
2019-01-18 17:26:39 -05:00
|
|
|
GetDramAddressFromPa(dstLastPagePa),
|
|
|
|
GetDramAddressFromPa(srcLastPagePa), copySize);
|
|
|
|
|
|
|
|
lastPageFillAddr += copySize;
|
|
|
|
|
|
|
|
unusedSizeAfter = PageSize - copySize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unusedSizeAfter = PageSize;
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
_context.Memory.ZeroFill(GetDramAddressFromPa(lastPageFillAddr), unusedSizeAfter);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
if (pages.AddRange(dstLastPagePa, 1) != KernelResult.Success)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList = pages;
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong AllocateSinglePage(MemoryRegion region, bool aslrDisabled)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KMemoryRegionManager regionMgr = _context.MemoryRegions[(int)region];
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
return regionMgr.AllocatePagesContiguous(1, aslrDisabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FreeSinglePage(MemoryRegion region, ulong address)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
KMemoryRegionManager regionMgr = _context.MemoryRegions[(int)region];
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
regionMgr.FreePage(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult MapPagesFromAnotherProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong address,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryState state,
|
|
|
|
KPageList pageList,
|
2020-07-17 00:19:07 -04:00
|
|
|
out ulong dst)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
dst = 0;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
ulong neededSize = endAddrRounded - addressTruncated;
|
|
|
|
|
|
|
|
ulong neededPagesCount = neededSize / PageSize;
|
|
|
|
|
|
|
|
ulong regionPagesCount = (AliasRegionEnd - AliasRegionStart) / PageSize;
|
|
|
|
|
|
|
|
ulong va = 0;
|
|
|
|
|
|
|
|
for (int unit = MappingUnitSizes.Length - 1; unit >= 0 && va == 0; unit--)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
int alignment = MappingUnitSizes[unit];
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
va = AllocateVa(AliasRegionStart, regionPagesCount, neededPagesCount, alignment);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (va == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfVaSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageList.Nodes.Count != 0)
|
|
|
|
{
|
|
|
|
KernelResult result = MapPages(va, pageList, permission);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertBlock(va, neededPagesCount, state, permission);
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
dst = va + (address - addressTruncated);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapNoAttributeIfStateEquals(ulong address, ulong size, MemoryState state)
|
|
|
|
{
|
|
|
|
if (AddrSpaceStart > address)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
if (endAddr <= address || endAddr - 1 > AddrSpaceEnd - 1)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.Mask,
|
|
|
|
state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Read,
|
|
|
|
KMemoryPermission.Read,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
2020-07-17 00:19:07 -04:00
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
ulong pagesCount = (endAddrRounded - addressTruncated) / PageSize;
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
// Free pages we had to create on-demand, if any of the buffer was not page aligned.
|
|
|
|
// Real kernel has page ref counting, so this is done as part of the unmap operation.
|
|
|
|
if (addressTruncated != addressRounded)
|
|
|
|
{
|
|
|
|
FreeSinglePage(_memRegion, ConvertVaToPa(addressTruncated));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endAddrTruncated < endAddrRounded && (addressTruncated == addressRounded || addressTruncated < endAddrTruncated))
|
|
|
|
{
|
|
|
|
FreeSinglePage(_memRegion, ConvertVaToPa(endAddrTruncated));
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
addressTruncated,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
KMemoryPermission.None,
|
|
|
|
MemoryOperation.Unmap);
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (result == KernelResult.Success)
|
|
|
|
{
|
|
|
|
InsertBlock(addressTruncated, pagesCount, MemoryState.Unmapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapIpcRestorePermission(ulong address, ulong size, MemoryState state)
|
|
|
|
{
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
2020-12-07 19:42:17 -05:00
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
ulong pagesCount = addressRounded < endAddrTruncated ? (endAddrTruncated - addressRounded) / PageSize : 0;
|
|
|
|
|
|
|
|
if (pagesCount == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
MemoryState stateMask;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case MemoryState.IpcBuffer0: stateMask = MemoryState.IpcSendAllowedType0; break;
|
|
|
|
case MemoryState.IpcBuffer1: stateMask = MemoryState.IpcSendAllowedType1; break;
|
|
|
|
case MemoryState.IpcBuffer3: stateMask = MemoryState.IpcSendAllowedType3; break;
|
|
|
|
|
|
|
|
default: return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute attributeMask =
|
|
|
|
MemoryAttribute.Borrowed |
|
|
|
|
MemoryAttribute.IpcMapped |
|
|
|
|
MemoryAttribute.Uncached;
|
|
|
|
|
|
|
|
if (state == MemoryState.IpcBuffer0)
|
|
|
|
{
|
|
|
|
attributeMask |= MemoryAttribute.DeviceMapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:42:17 -05:00
|
|
|
// Anything on the client side should see this memory as modified.
|
|
|
|
_cpuMemory.SignalMemoryTracking(addressTruncated, endAddrRounded - addressTruncated, true);
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressRounded, endAddrTruncated))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 17:26:39 -05:00
|
|
|
if ((info.State & stateMask) != stateMask ||
|
|
|
|
(info.Attribute & attributeMask) != MemoryAttribute.IpcMapped)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.Permission != info.SourcePermission && info.IpcRefCount == 1)
|
|
|
|
{
|
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrTruncated);
|
|
|
|
|
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
|
|
|
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
info.SourcePermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
InsertBlock(addressRounded, pagesCount, RestoreIpcMappingPermissions);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-12-09 17:20:05 -05:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
public KernelResult BorrowIpcBuffer(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return SetAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.ReadAndWrite,
|
2020-07-17 00:19:07 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2020-07-17 00:19:07 -04:00
|
|
|
MemoryAttribute.Borrowed);
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public KernelResult BorrowTransferMemory(KPageList pageList, ulong address, ulong size, KMemoryPermission permission)
|
2020-07-19 14:24:18 -04:00
|
|
|
{
|
|
|
|
return SetAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.TransferMemoryAllowed,
|
|
|
|
MemoryState.TransferMemoryAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.ReadAndWrite,
|
2020-07-19 14:24:18 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
permission,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
pageList);
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
private KernelResult SetAttributesAndChangePermission(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permissionMask,
|
|
|
|
KMemoryPermission permissionExpected,
|
2020-07-17 00:19:07 -04:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission newPermission,
|
2020-07-17 00:19:07 -04:00
|
|
|
MemoryAttribute attributeSetMask,
|
|
|
|
KPageList pageList = null)
|
|
|
|
{
|
|
|
|
if (address + size <= address || !InsideAddrSpace(address, size))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
stateMask | MemoryState.IsPoolAllocated,
|
|
|
|
stateExpected | MemoryState.IsPoolAllocated,
|
|
|
|
permissionMask,
|
|
|
|
permissionExpected,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out MemoryState oldState,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission oldPermission,
|
2020-07-17 00:19:07 -04:00
|
|
|
out MemoryAttribute oldAttribute))
|
|
|
|
{
|
|
|
|
ulong pagesCount = size / PageSize;
|
|
|
|
|
|
|
|
if (pageList != null)
|
|
|
|
{
|
2020-07-19 14:24:18 -04:00
|
|
|
AddVaRangeToPageList(pageList, address, pagesCount);
|
2020-07-17 00:19:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if (newPermission == KMemoryPermission.None)
|
2020-07-17 00:19:07 -04:00
|
|
|
{
|
|
|
|
newPermission = oldPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission != oldPermission)
|
|
|
|
{
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
newPermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute newAttribute = oldAttribute | attributeSetMask;
|
|
|
|
|
|
|
|
InsertBlock(address, pagesCount, oldState, newPermission, newAttribute);
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public KernelResult UnborrowIpcBuffer(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return ClearAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.ReadAndWrite,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Borrowed);
|
|
|
|
}
|
|
|
|
|
2020-07-19 14:24:18 -04:00
|
|
|
public KernelResult UnborrowTransferMemory(ulong address, ulong size, KPageList pageList)
|
|
|
|
{
|
|
|
|
return ClearAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.TransferMemoryAllowed,
|
|
|
|
MemoryState.TransferMemoryAllowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
|
|
|
KMemoryPermission.None,
|
2020-07-19 14:24:18 -04:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.ReadAndWrite,
|
2020-07-19 14:24:18 -04:00
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
pageList);
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
private KernelResult ClearAttributesAndChangePermission(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permissionMask,
|
|
|
|
KMemoryPermission permissionExpected,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission newPermission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeClearMask,
|
|
|
|
KPageList pageList = null)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
if (address + size <= address || !InsideAddrSpace(address, size))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
stateMask | MemoryState.IsPoolAllocated,
|
|
|
|
stateExpected | MemoryState.IsPoolAllocated,
|
|
|
|
permissionMask,
|
|
|
|
permissionExpected,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out MemoryState oldState,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission oldPermission,
|
2019-01-18 17:26:39 -05:00
|
|
|
out MemoryAttribute oldAttribute))
|
|
|
|
{
|
|
|
|
ulong pagesCount = size / PageSize;
|
|
|
|
|
|
|
|
if (pageList != null)
|
|
|
|
{
|
|
|
|
KPageList currPageList = new KPageList();
|
|
|
|
|
|
|
|
AddVaRangeToPageList(currPageList, address, pagesCount);
|
|
|
|
|
|
|
|
if (!currPageList.IsEqual(pageList))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
if (newPermission == KMemoryPermission.None)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
newPermission = oldPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission != oldPermission)
|
|
|
|
{
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
newPermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute newAttribute = oldAttribute & ~attributeClearMask;
|
|
|
|
|
|
|
|
InsertBlock(address, pagesCount, oldState, newPermission, newAttribute);
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddVaRangeToPageList(KPageList pageList, ulong start, ulong pagesCount)
|
|
|
|
{
|
|
|
|
ulong address = start;
|
|
|
|
|
|
|
|
while (address < start + pagesCount * PageSize)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
if (!TryConvertVaToPa(address, out ulong pa))
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure translating virtual address.");
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList.AddRange(pa, 1);
|
|
|
|
|
|
|
|
address += PageSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ulong GetAddrInRange(KMemoryInfo info, ulong start)
|
|
|
|
{
|
|
|
|
if (info.Address < start)
|
|
|
|
{
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ulong GetSizeInRange(KMemoryInfo info, ulong start, ulong end)
|
|
|
|
{
|
|
|
|
ulong endAddr = info.Size + info.Address;
|
|
|
|
ulong size = info.Size;
|
|
|
|
|
|
|
|
if (info.Address < start)
|
|
|
|
{
|
|
|
|
size -= start - info.Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endAddr > end)
|
|
|
|
{
|
|
|
|
size -= endAddr - end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsUnmapped(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Unmapped,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.Mask,
|
|
|
|
KMemoryPermission.None,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permissionMask,
|
|
|
|
KMemoryPermission permissionExpected,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
MemoryAttribute attributeIgnoreMask,
|
|
|
|
out MemoryState outState,
|
2020-12-01 18:23:43 -05:00
|
|
|
out KMemoryPermission outPermission,
|
2019-01-18 17:26:39 -05:00
|
|
|
out MemoryAttribute outAttribute)
|
|
|
|
{
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
|
|
|
|
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
|
|
|
|
|
|
|
MemoryState firstState = info.State;
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission firstPermission = info.Permission;
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute firstAttribute = info.Attribute;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
info = node.Value.GetInfo();
|
|
|
|
|
2019-07-01 22:39:22 -04:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 17:26:39 -05:00
|
|
|
if ( firstState != info.State ||
|
|
|
|
firstPermission != info.Permission ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected ||
|
|
|
|
(firstAttribute | attributeIgnoreMask) != (info.Attribute | attributeIgnoreMask) ||
|
|
|
|
(firstState & stateMask) != stateExpected ||
|
|
|
|
(firstPermission & permissionMask) != permissionExpected)
|
|
|
|
{
|
|
|
|
outState = MemoryState.Unmapped;
|
2020-12-01 18:23:43 -05:00
|
|
|
outPermission = KMemoryPermission.None;
|
2019-01-18 17:26:39 -05:00
|
|
|
outAttribute = MemoryAttribute.None;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (info.Address + info.Size - 1 < endAddr - 1 && (node = node.Next) != null);
|
|
|
|
|
|
|
|
outState = firstState;
|
|
|
|
outPermission = firstPermission;
|
|
|
|
outAttribute = firstAttribute & ~attributeIgnoreMask;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permissionMask,
|
|
|
|
KMemoryPermission permissionExpected,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected)
|
|
|
|
{
|
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, address + size))
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 17:26:39 -05:00
|
|
|
if ((info.State & stateMask) != stateExpected ||
|
|
|
|
(info.Permission & permissionMask) != permissionExpected ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<KMemoryInfo> IterateOverRange(ulong start, ulong end)
|
|
|
|
{
|
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(start);
|
|
|
|
|
|
|
|
KMemoryInfo info;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
info = node.Value.GetInfo();
|
|
|
|
|
|
|
|
yield return info;
|
|
|
|
}
|
|
|
|
while (info.Address + info.Size - 1 < end - 1 && (node = node.Next) != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertBlock(
|
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState oldState,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission oldPermission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute oldAttribute,
|
|
|
|
MemoryState newState,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission newPermission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute newAttribute)
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Insert new block on the list only on areas where the state
|
|
|
|
// of the block matches the state specified on the old* state
|
|
|
|
// arguments, otherwise leave it as is.
|
2019-01-18 17:26:39 -05:00
|
|
|
int oldCount = _blocks.Count;
|
|
|
|
|
|
|
|
oldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
|
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
{
|
2020-05-06 08:48:57 -04:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryAttribute currBlockAttr = currBlock.Attribute | MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (currBlock.State != oldState ||
|
|
|
|
currBlock.Permission != oldPermission ||
|
|
|
|
currBlockAttr != oldAttribute)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-05-06 08:48:57 -04:00
|
|
|
node = node.Next;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (endAddr < currEndAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
newNode.Value.SetState(newPermission, newState, newAttribute);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
node = newNode.Next;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2020-12-01 18:23:43 -05:00
|
|
|
|
|
|
|
ValidateInternalState();
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertBlock(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission = KMemoryPermission.None,
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryAttribute attribute = MemoryAttribute.None)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Inserts new block at the list, replacing and splitting
|
|
|
|
// existing blocks as needed.
|
2018-12-06 06:16:24 -05:00
|
|
|
int oldCount = _blocks.Count;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
while (node != null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2020-05-06 08:48:57 -04:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (endAddr < currEndAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
newNode.Value.SetState(permission, state, attribute);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
node = newNode.Next;
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2020-12-01 18:23:43 -05:00
|
|
|
|
|
|
|
ValidateInternalState();
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private static void SetIpcMappingPermissions(KMemoryBlock block, KMemoryPermission permission)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
block.SetIpcMappingPermission(permission);
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private static void RestoreIpcMappingPermissions(KMemoryBlock block, KMemoryPermission permission)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
block.RestoreIpcMappingPermission();
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private delegate void BlockMutator(KMemoryBlock block, KMemoryPermission newPerm);
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
private void InsertBlock(
|
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
BlockMutator blockMutate,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission = KMemoryPermission.None)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
// Inserts new block at the list, replacing and splitting
|
|
|
|
// existing blocks as needed, then calling the callback
|
|
|
|
// function on the new block.
|
2019-01-18 17:26:39 -05:00
|
|
|
int oldCount = _blocks.Count;
|
|
|
|
|
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
{
|
2020-05-06 08:48:57 -04:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
|
|
|
|
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
|
|
|
{
|
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (endAddr < currEndAddr)
|
|
|
|
{
|
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
KMemoryBlock newBlock = newNode.Value;
|
|
|
|
|
|
|
|
blockMutate(newBlock, permission);
|
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
node = newNode.Next;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2020-12-01 18:23:43 -05:00
|
|
|
|
|
|
|
ValidateInternalState();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
private void ValidateInternalState()
|
|
|
|
{
|
|
|
|
ulong expectedAddress = 0;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
{
|
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
|
|
|
|
|
|
|
KMemoryBlock currBlock = node.Value;
|
|
|
|
|
|
|
|
Debug.Assert(currBlock.BaseAddress == expectedAddress);
|
|
|
|
|
|
|
|
expectedAddress = currBlock.BaseAddress + currBlock.PagesCount * PageSize;
|
|
|
|
|
|
|
|
node = newNode.Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(expectedAddress == AddrSpaceEnd);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2020-05-06 08:48:57 -04:00
|
|
|
private LinkedListNode<KMemoryBlock> MergeEqualStateNeighbors(LinkedListNode<KMemoryBlock> node)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryBlock block = node.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (node.Previous != null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
KMemoryBlock previousBlock = node.Previous.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (BlockStateEquals(block, previousBlock))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
LinkedListNode<KMemoryBlock> previousNode = node.Previous;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
_blocks.Remove(node);
|
|
|
|
|
|
|
|
previousBlock.AddPages(block.PagesCount);
|
|
|
|
|
|
|
|
node = previousNode;
|
|
|
|
block = previousBlock;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (node.Next != null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
KMemoryBlock nextBlock = node.Next.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (BlockStateEquals(block, nextBlock))
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_blocks.Remove(node.Next);
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
block.AddPages(nextBlock.PagesCount);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 08:48:57 -04:00
|
|
|
|
|
|
|
return node;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private static bool BlockStateEquals(KMemoryBlock lhs, KMemoryBlock rhs)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
return lhs.State == rhs.State &&
|
|
|
|
lhs.Permission == rhs.Permission &&
|
|
|
|
lhs.Attribute == rhs.Attribute &&
|
|
|
|
lhs.SourcePermission == rhs.SourcePermission &&
|
|
|
|
lhs.DeviceRefCount == rhs.DeviceRefCount &&
|
|
|
|
lhs.IpcRefCount == rhs.IpcRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong AllocateVa(
|
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment)
|
|
|
|
{
|
|
|
|
ulong address = 0;
|
|
|
|
|
|
|
|
ulong regionEndAddr = regionStart + regionPagesCount * PageSize;
|
|
|
|
|
|
|
|
ulong reservedPagesCount = _isKernel ? 1UL : 4UL;
|
|
|
|
|
|
|
|
if (_aslrEnabled)
|
|
|
|
{
|
|
|
|
ulong totalNeededSize = (reservedPagesCount + neededPagesCount) * PageSize;
|
|
|
|
|
|
|
|
ulong remainingPages = regionPagesCount - neededPagesCount;
|
|
|
|
|
|
|
|
ulong aslrMaxOffset = ((remainingPages + reservedPagesCount) * PageSize) / (ulong)alignment;
|
|
|
|
|
|
|
|
for (int attempt = 0; attempt < 8; attempt++)
|
|
|
|
{
|
|
|
|
address = BitUtils.AlignDown(regionStart + GetRandomValue(0, aslrMaxOffset) * (ulong)alignment, alignment);
|
|
|
|
|
|
|
|
ulong endAddr = address + totalNeededSize;
|
|
|
|
|
|
|
|
KMemoryInfo info = FindBlock(address).GetInfo();
|
|
|
|
|
|
|
|
if (info.State != MemoryState.Unmapped)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong currBaseAddr = info.Address + reservedPagesCount * PageSize;
|
|
|
|
ulong currEndAddr = info.Address + info.Size;
|
|
|
|
|
|
|
|
if (address >= regionStart &&
|
|
|
|
address >= currBaseAddr &&
|
|
|
|
endAddr - 1 <= regionEndAddr - 1 &&
|
|
|
|
endAddr - 1 <= currEndAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address == 0)
|
|
|
|
{
|
|
|
|
ulong aslrPage = GetRandomValue(0, aslrMaxOffset);
|
|
|
|
|
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart + aslrPage * PageSize,
|
|
|
|
regionPagesCount - aslrPage,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
|
|
|
0,
|
|
|
|
reservedPagesCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address == 0)
|
|
|
|
{
|
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart,
|
|
|
|
regionPagesCount,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
|
|
|
0,
|
|
|
|
reservedPagesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
private ulong FindFirstFit(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment,
|
|
|
|
ulong reservedStart,
|
|
|
|
ulong reservedPagesCount)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong reservedSize = reservedPagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong totalNeededSize = reservedSize + neededPagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong regionEndAddr = regionStart + regionPagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(regionStart);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
while (regionEndAddr >= info.Address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (info.State == MemoryState.Unmapped)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong currBaseAddr = info.Address + reservedSize;
|
|
|
|
ulong currEndAddr = info.Address + info.Size - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address = BitUtils.AlignDown(currBaseAddr, alignment) + reservedStart;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (currBaseAddr > address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
address += (ulong)alignment;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong allocationEndAddr = address + totalNeededSize - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (allocationEndAddr <= regionEndAddr &&
|
|
|
|
allocationEndAddr <= currEndAddr &&
|
|
|
|
address < allocationEndAddr)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return address;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
node = node.Next;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (node == null)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
info = node.Value.GetInfo();
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KMemoryBlock FindBlock(ulong address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return FindBlockNode(address)?.Value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private LinkedListNode<KMemoryBlock> FindBlockNode(ulong address)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
while (node != null)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryBlock block = node.Value;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong currEndAddr = block.PagesCount * PageSize + block.BaseAddress;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (block.BaseAddress <= address && currEndAddr - 1 >= address)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return node;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
node = node.Next;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
public bool CanContain(ulong address, ulong size, MemoryState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
ulong regionBaseAddr = GetBaseAddress(state);
|
|
|
|
ulong regionEndAddr = regionBaseAddr + GetSize(state);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
bool InsideRegion()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return regionBaseAddr <= address &&
|
|
|
|
endAddr > address &&
|
|
|
|
endAddr - 1 <= regionEndAddr - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
bool OutsideHeapRegion() => endAddr <= HeapRegionStart || address >= HeapRegionEnd;
|
|
|
|
bool OutsideAliasRegion() => endAddr <= AliasRegionStart || address >= AliasRegionEnd;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case MemoryState.Io:
|
|
|
|
case MemoryState.Normal:
|
|
|
|
case MemoryState.CodeStatic:
|
|
|
|
case MemoryState.CodeMutable:
|
|
|
|
case MemoryState.SharedMemory:
|
|
|
|
case MemoryState.ModCodeStatic:
|
|
|
|
case MemoryState.ModCodeMutable:
|
|
|
|
case MemoryState.Stack:
|
|
|
|
case MemoryState.ThreadLocal:
|
|
|
|
case MemoryState.TransferMemoryIsolated:
|
|
|
|
case MemoryState.TransferMemory:
|
|
|
|
case MemoryState.ProcessMemory:
|
|
|
|
case MemoryState.CodeReadOnly:
|
|
|
|
case MemoryState.CodeWritable:
|
2020-12-01 18:23:43 -05:00
|
|
|
return InsideRegion() && OutsideHeapRegion() && OutsideAliasRegion();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
case MemoryState.Heap:
|
2020-12-01 18:23:43 -05:00
|
|
|
return InsideRegion() && OutsideAliasRegion();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
case MemoryState.IpcBuffer0:
|
|
|
|
case MemoryState.IpcBuffer1:
|
|
|
|
case MemoryState.IpcBuffer3:
|
|
|
|
return InsideRegion() && OutsideHeapRegion();
|
|
|
|
|
|
|
|
case MemoryState.KernelStack:
|
|
|
|
return InsideRegion();
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private ulong GetBaseAddress(MemoryState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case MemoryState.Io:
|
|
|
|
case MemoryState.Normal:
|
|
|
|
case MemoryState.ThreadLocal:
|
|
|
|
return TlsIoRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.CodeStatic:
|
|
|
|
case MemoryState.CodeMutable:
|
|
|
|
case MemoryState.SharedMemory:
|
|
|
|
case MemoryState.ModCodeStatic:
|
|
|
|
case MemoryState.ModCodeMutable:
|
|
|
|
case MemoryState.TransferMemoryIsolated:
|
|
|
|
case MemoryState.TransferMemory:
|
|
|
|
case MemoryState.ProcessMemory:
|
|
|
|
case MemoryState.CodeReadOnly:
|
|
|
|
case MemoryState.CodeWritable:
|
|
|
|
return GetAddrSpaceBaseAddr();
|
|
|
|
|
|
|
|
case MemoryState.Heap:
|
|
|
|
return HeapRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.IpcBuffer0:
|
|
|
|
case MemoryState.IpcBuffer1:
|
|
|
|
case MemoryState.IpcBuffer3:
|
|
|
|
return AliasRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.Stack:
|
|
|
|
return StackRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.KernelStack:
|
|
|
|
return AddrSpaceStart;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private ulong GetSize(MemoryState state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (state)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case MemoryState.Io:
|
|
|
|
case MemoryState.Normal:
|
|
|
|
case MemoryState.ThreadLocal:
|
|
|
|
return TlsIoRegionEnd - TlsIoRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.CodeStatic:
|
|
|
|
case MemoryState.CodeMutable:
|
|
|
|
case MemoryState.SharedMemory:
|
|
|
|
case MemoryState.ModCodeStatic:
|
|
|
|
case MemoryState.ModCodeMutable:
|
|
|
|
case MemoryState.TransferMemoryIsolated:
|
|
|
|
case MemoryState.TransferMemory:
|
|
|
|
case MemoryState.ProcessMemory:
|
|
|
|
case MemoryState.CodeReadOnly:
|
|
|
|
case MemoryState.CodeWritable:
|
|
|
|
return GetAddrSpaceSize();
|
|
|
|
|
|
|
|
case MemoryState.Heap:
|
|
|
|
return HeapRegionEnd - HeapRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.IpcBuffer0:
|
|
|
|
case MemoryState.IpcBuffer1:
|
|
|
|
case MemoryState.IpcBuffer3:
|
|
|
|
return AliasRegionEnd - AliasRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.Stack:
|
|
|
|
return StackRegionEnd - StackRegionStart;
|
|
|
|
|
|
|
|
case MemoryState.KernelStack:
|
|
|
|
return AddrSpaceEnd - AddrSpaceStart;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetAddrSpaceBaseAddr()
|
|
|
|
{
|
|
|
|
if (AddrSpaceWidth == 36 || AddrSpaceWidth == 39)
|
|
|
|
{
|
|
|
|
return 0x8000000;
|
|
|
|
}
|
|
|
|
else if (AddrSpaceWidth == 32)
|
|
|
|
{
|
|
|
|
return 0x200000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Invalid address space width!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ulong GetAddrSpaceSize()
|
|
|
|
{
|
|
|
|
if (AddrSpaceWidth == 36)
|
|
|
|
{
|
|
|
|
return 0xff8000000;
|
|
|
|
}
|
|
|
|
else if (AddrSpaceWidth == 39)
|
|
|
|
{
|
|
|
|
return 0x7ff8000000;
|
|
|
|
}
|
|
|
|
else if (AddrSpaceWidth == 32)
|
|
|
|
{
|
|
|
|
return 0xffe00000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Invalid address space width!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong currAddr = address;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result = KernelResult.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
foreach (KPageNode pageNode in pageList)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = DoMmuOperation(
|
|
|
|
currAddr,
|
|
|
|
pageNode.PagesCount,
|
|
|
|
pageNode.Address,
|
2018-11-28 17:18:09 -05:00
|
|
|
true,
|
2018-12-06 06:16:24 -05:00
|
|
|
permission,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.MapPa);
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryInfo info = FindBlock(currAddr).GetInfo();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCount = (address - currAddr) / PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = MmuUnmap(address, pagesCount);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
currAddr += pageNode.PagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult MmuUnmap(ulong address, ulong pagesCount)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 17:18:09 -05:00
|
|
|
0,
|
|
|
|
false,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission.None,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.Unmap);
|
|
|
|
}
|
|
|
|
|
2020-12-01 18:23:43 -05:00
|
|
|
private KernelResult MmuChangePermission(ulong address, ulong pagesCount, KMemoryPermission permission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-06 06:16:24 -05:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 17:18:09 -05:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-06 06:16:24 -05:00
|
|
|
permission,
|
2018-11-28 17:18:09 -05:00
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong dstVa,
|
|
|
|
ulong pagesCount,
|
|
|
|
ulong srcPa,
|
|
|
|
bool map,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryOperation operation)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (map != (operation == MemoryOperation.MapPa))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException(nameof(map) + " value is invalid for this operation.");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KernelResult result;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
switch (operation)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
case MemoryOperation.MapPa:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
_cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, size);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = KernelResult.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Allocate:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = region.AllocatePages(pagesCount, _aslrDisabled, out KPageList pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
result = MmuMapPages(dstVa, pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Unmap:
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
_cpuMemory.Unmap(dstVa, size);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = KernelResult.Success;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
case MemoryOperation.ChangePermRw: result = KernelResult.Success; break;
|
|
|
|
case MemoryOperation.ChangePermsAndAttributes: result = KernelResult.Success; break;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
default: throw new ArgumentException($"Invalid operation \"{operation}\".");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong address,
|
|
|
|
ulong pagesCount,
|
|
|
|
KPageList pageList,
|
2020-12-01 18:23:43 -05:00
|
|
|
KMemoryPermission permission,
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryOperation operation)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (operation != MemoryOperation.MapVa)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
throw new ArgumentException($"Invalid memory operation \"{operation}\" specified.");
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return MmuMapPages(address, pageList);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private KMemoryRegionManager GetMemoryRegionManager()
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
return _context.MemoryRegions[(int)_memRegion];
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KernelResult MmuMapPages(ulong address, KPageList pageList)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
foreach (KPageNode pageNode in pageList)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = pageNode.PagesCount * PageSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
_cpuMemory.Map(address, pageNode.Address - DramMemoryMap.DramBase, size);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
address += size;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public ulong GetDramAddressFromVa(ulong va)
|
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
return _cpuMemory.GetPhysicalAddress(va);
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
public ulong ConvertVaToPa(ulong va)
|
|
|
|
{
|
|
|
|
if (!TryConvertVaToPa(va, out ulong pa))
|
|
|
|
{
|
|
|
|
throw new ArgumentException($"Invalid virtual address 0x{va:X} specified.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryConvertVaToPa(ulong va, out ulong pa)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-05-03 18:54:50 -04:00
|
|
|
pa = DramMemoryMap.DramBase + _cpuMemory.GetPhysicalAddress(va);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ulong GetDramAddressFromPa(ulong pa)
|
|
|
|
{
|
|
|
|
return pa - DramMemoryMap.DramBase;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public long GetMmUsedPages()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_blocks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return BitUtils.DivRoundUp(GetMmUsedSize(), PageSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private long GetMmUsedSize()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return _blocks.Count * KMemoryBlockSize;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool IsInvalidRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return address + size - 1 > GetAddrSpaceBaseAddr() + GetAddrSpaceSize() - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool InsideAddrSpace(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return AddrSpaceStart <= address && address + size - 1 <= AddrSpaceEnd - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool InsideAliasRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return address + size > AliasRegionStart && AliasRegionEnd > address;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool InsideHeapRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return address + size > HeapRegionStart && HeapRegionEnd > address;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool InsideStackRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return address + size > StackRegionStart && StackRegionEnd > address;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool OutsideAliasRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return AliasRegionStart > address || address + size - 1 > AliasRegionEnd - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool OutsideAddrSpace(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return AddrSpaceStart > address || address + size - 1 > AddrSpaceEnd - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool OutsideStackRegion(ulong address, ulong size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return StackRegionStart > address || address + size - 1 > StackRegionEnd - 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|