2018-08-15 20:59:51 +02:00
|
|
|
using ChocolArm64.Memory;
|
2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
2018-08-15 20:59:51 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
class KMemoryManager
|
|
|
|
{
|
|
|
|
public const int PageSize = 0x1000;
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private const int KMemoryBlockSize = 0x40;
|
|
|
|
|
|
|
|
//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.
|
|
|
|
private const int MaxBlocksNeededForInsertion = 2;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private LinkedList<KMemoryBlock> _blocks;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private MemoryManager _cpuMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private Horizon _system;
|
2018-11-28 23:18:09 +01: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 12:16:24 +01:00
|
|
|
private ulong _currentHeapAddr;
|
2018-11-28 23:18:09 +01: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 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public ulong TlsIoRegionStart { get; private set; }
|
|
|
|
public ulong TlsIoRegionEnd { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private ulong _heapCapacity;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public ulong PhysicalMemoryUsage { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private MemoryRegion _memRegion;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private bool _aslrDisabled;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public int AddrSpaceWidth { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private bool _isKernel;
|
|
|
|
private bool _aslrEnabled;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KMemoryBlockAllocator _blockAllocator;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private int _contextId;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private MersenneTwister _randomNumberGenerator;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KMemoryManager(Horizon system, MemoryManager cpuMemory)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_system = system;
|
|
|
|
_cpuMemory = cpuMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks = new LinkedList<KMemoryBlock>();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private static readonly int[] AddrSpaceSizes = new int[] { 32, 36, 32, 39 };
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public KernelResult InitializeForProcess(
|
2018-12-06 12:16:24 +01:00
|
|
|
AddressSpaceType addrSpaceType,
|
|
|
|
bool aslrEnabled,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion memRegion,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KMemoryBlockAllocator blockAllocator)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((uint)addrSpaceType > (uint)AddressSpaceType.Addr39Bits)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new ArgumentException(nameof(addrSpaceType));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_contextId = _system.ContextIdManager.GetId();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong addrSpaceBase = 0;
|
|
|
|
ulong addrSpaceSize = 1UL << AddrSpaceSizes[(int)addrSpaceType];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = CreateUserAddressSpace(
|
|
|
|
addrSpaceType,
|
|
|
|
aslrEnabled,
|
|
|
|
aslrDisabled,
|
|
|
|
addrSpaceBase,
|
|
|
|
addrSpaceSize,
|
|
|
|
memRegion,
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
blockAllocator);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_system.ContextIdManager.PutId(_contextId);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Region
|
|
|
|
{
|
|
|
|
public ulong Start;
|
|
|
|
public ulong End;
|
|
|
|
public ulong Size;
|
|
|
|
public ulong AslrOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult CreateUserAddressSpace(
|
2018-12-06 12:16:24 +01: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 20:59:51 +02:00
|
|
|
{
|
|
|
|
case AddressSpaceType.Addr32Bits:
|
2018-12-06 12:16:24 +01:00
|
|
|
aliasRegion.Size = 0x40000000;
|
|
|
|
heapRegion.Size = 0x40000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-06 12:16:24 +01:00
|
|
|
codeRegionSize = 0x3fe00000;
|
|
|
|
stackAndTlsIoStart = 0x200000;
|
|
|
|
stackAndTlsIoEnd = 0x40000000;
|
|
|
|
baseAddress = 0x200000;
|
2018-11-28 23:18:09 +01:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 20:59:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr36Bits:
|
2018-12-06 12:16:24 +01:00
|
|
|
aliasRegion.Size = 0x180000000;
|
|
|
|
heapRegion.Size = 0x180000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
CodeRegionStart = 0x8000000;
|
2018-12-06 12:16:24 +01:00
|
|
|
codeRegionSize = 0x78000000;
|
|
|
|
stackAndTlsIoStart = 0x8000000;
|
|
|
|
stackAndTlsIoEnd = 0x80000000;
|
|
|
|
baseAddress = 0x8000000;
|
2018-11-28 23:18:09 +01:00
|
|
|
AddrSpaceWidth = 36;
|
2018-08-15 20:59:51 +02:00
|
|
|
break;
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
case AddressSpaceType.Addr32BitsNoMap:
|
2018-12-06 12:16:24 +01:00
|
|
|
aliasRegion.Size = 0;
|
|
|
|
heapRegion.Size = 0x80000000;
|
|
|
|
stackRegion.Size = 0;
|
|
|
|
tlsIoRegion.Size = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-06 12:16:24 +01:00
|
|
|
codeRegionSize = 0x3fe00000;
|
|
|
|
stackAndTlsIoStart = 0x200000;
|
|
|
|
stackAndTlsIoEnd = 0x40000000;
|
|
|
|
baseAddress = 0x200000;
|
2018-11-28 23:18:09 +01:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 20:59:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr39Bits:
|
2018-12-06 12:16:24 +01: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 23:18:09 +01:00
|
|
|
AddrSpaceWidth = 39;
|
2018-08-15 20:59:51 +02:00
|
|
|
break;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
default: throw new ArgumentException(nameof(addrSpaceType));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
CodeRegionEnd = CodeRegionStart + codeRegionSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong mapBaseAddress;
|
|
|
|
ulong mapAvailableSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (CodeRegionStart - baseAddress >= addrSpaceEnd - CodeRegionEnd)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//Has more space before the start of the code region.
|
2018-12-06 12:16:24 +01:00
|
|
|
mapBaseAddress = baseAddress;
|
|
|
|
mapAvailableSize = CodeRegionStart - baseAddress;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Has more space after the end of the code region.
|
2018-12-06 12:16:24 +01:00
|
|
|
mapBaseAddress = CodeRegionEnd;
|
|
|
|
mapAvailableSize = addrSpaceEnd - CodeRegionEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong mapTotalSize = aliasRegion.Size + heapRegion.Size + stackRegion.Size + tlsIoRegion.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong aslrMaxOffset = mapAvailableSize - mapTotalSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_aslrEnabled = aslrEnabled;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddrSpaceStart = addrSpaceStart;
|
|
|
|
AddrSpaceEnd = addrSpaceEnd;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blockAllocator = blockAllocator;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (mapAvailableSize < mapTotalSize)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (aslrEnabled)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01: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 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Regions are sorted based on ASLR offset.
|
|
|
|
//When ASLR is disabled, the order is Map, Heap, NewMap and TlsIo.
|
2018-12-06 12:16:24 +01: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 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
SortRegion(heapRegion, aliasRegion);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (stackRegion.Size != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
SortRegion(stackRegion, aliasRegion);
|
|
|
|
SortRegion(stackRegion, heapRegion);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
stackRegion.Start = stackAndTlsIoStart;
|
|
|
|
stackRegion.End = stackAndTlsIoEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (tlsIoRegion.Size != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
SortRegion(tlsIoRegion, aliasRegion);
|
|
|
|
SortRegion(tlsIoRegion, heapRegion);
|
|
|
|
SortRegion(tlsIoRegion, stackRegion);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
tlsIoRegion.Start = stackAndTlsIoStart;
|
|
|
|
tlsIoRegion.End = stackAndTlsIoEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01: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 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_currentHeapAddr = HeapRegionStart;
|
|
|
|
_heapCapacity = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
PhysicalMemoryUsage = 0;
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_memRegion = memRegion;
|
|
|
|
_aslrDisabled = aslrDisabled;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return InitializeBlocks(addrSpaceStart, addrSpaceEnd);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private ulong GetRandomValue(ulong min, ulong max)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return (ulong)GetRandomValue((long)min, (long)max);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private long GetRandomValue(long min, long max)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_randomNumberGenerator == null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_randomNumberGenerator = new MersenneTwister(0);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return _randomNumberGenerator.GenRandomNumber(min, max);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static void SortRegion(Region lhs, Region rhs)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (lhs.AslrOffset < rhs.AslrOffset)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
rhs.Start += lhs.Size;
|
|
|
|
rhs.End += lhs.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lhs.Start += rhs.Size;
|
|
|
|
lhs.End += rhs.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KernelResult InitializeBlocks(ulong addrSpaceStart, ulong addrSpaceEnd)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//First insertion will always need only a single block,
|
|
|
|
//because there's nothing else to split.
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(1))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong addrSpacePagesCount = (addrSpaceEnd - addrSpaceStart) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(addrSpaceStart, addrSpacePagesCount, MemoryState.Unmapped);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public KernelResult MapPages(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
KPageList pageList,
|
|
|
|
MemoryState state,
|
|
|
|
MemoryPermission permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = pageList.GetPagesCount();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!ValidateRegionForState(address, size, state))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!IsUnmapped(address, pagesCount * PageSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MapPages(address, pageList, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, state, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult UnmapPages(ulong address, KPageList pageList, MemoryState stateExpected)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = pageList.GetPagesCount();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong addrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (AddrSpaceStart > address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (addrSpacePagesCount < pagesCount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (endAddr - 1 > AddrSpaceEnd - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList currentPageList = new KPageList();
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddVaRangeToPageList(currentPageList, address, pagesCount);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!currentPageList.IsEqual(pageList))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Mask,
|
2018-12-06 12:16:24 +01:00
|
|
|
stateExpected,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
2018-11-28 23:18:09 +01:00
|
|
|
out _,
|
|
|
|
out _))
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuUnmap(address, pagesCount);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult MapNormalMemory(long address, long size, MemoryPermission permission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult MapIoMemory(long address, long size, MemoryPermission permission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
return KernelResult.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public KernelResult AllocateOrMapPa(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment,
|
|
|
|
ulong srcPa,
|
|
|
|
bool map,
|
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
MemoryState state,
|
|
|
|
MemoryPermission permission,
|
|
|
|
out ulong address)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionSize = regionPagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionEndAddr = regionStart + regionSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!ValidateRegionForState(regionStart, regionSize, state))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (regionPagesCount <= neededPagesCount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong reservedPagesCount = _isKernel ? 1UL : 4UL;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_aslrEnabled)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong totalNeededSize = (reservedPagesCount + neededPagesCount) * PageSize;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong remainingPages = regionPagesCount - neededPagesCount;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong aslrMaxOffset = ((remainingPages + reservedPagesCount) * PageSize) / (ulong)alignment;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int attempt = 0; attempt < 8; attempt++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address = BitUtils.AlignDown(regionStart + GetRandomValue(0, aslrMaxOffset) * (ulong)alignment, alignment);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + totalNeededSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = FindBlock(address).GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State != MemoryState.Unmapped)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currBaseAddr = info.Address + reservedPagesCount * PageSize;
|
|
|
|
ulong currEndAddr = info.Address + info.Size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (address >= regionStart &&
|
|
|
|
address >= currBaseAddr &&
|
|
|
|
endAddr - 1 <= regionEndAddr - 1 &&
|
|
|
|
endAddr - 1 <= currEndAddr - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (address == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong aslrPage = GetRandomValue(0, aslrMaxOffset);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart + aslrPage * PageSize,
|
|
|
|
regionPagesCount - aslrPage,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
2018-12-06 12:16:24 +01:00
|
|
|
reservedPagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (address == 0)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart,
|
|
|
|
regionPagesCount,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
2018-12-06 12:16:24 +01:00
|
|
|
reservedPagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (address == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryOperation operation = map
|
2018-11-28 23:18:09 +01:00
|
|
|
? MemoryOperation.MapPa
|
|
|
|
: MemoryOperation.Allocate;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
neededPagesCount,
|
|
|
|
srcPa,
|
|
|
|
map,
|
|
|
|
permission,
|
|
|
|
operation);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, neededPagesCount, state, permission);
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public KernelResult MapNewProcessCode(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
|
|
|
MemoryPermission permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!ValidateRegionForState(address, size, state))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!IsUnmapped(address, size))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-06 12:16:24 +01:00
|
|
|
permission,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryOperation.Allocate);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, state, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult MapProcessCodeMemory(ulong dst, ulong src, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
bool success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
|
|
|
out MemoryPermission permission,
|
2018-11-28 23:18:09 +01:00
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success &= IsUnmapped(dst, size);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList pageList = new KPageList();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddVaRangeToPageList(pageList, src, pagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuChangePermission(src, pagesCount, MemoryPermission.None);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MapPages(dst, pageList, MemoryPermission.None);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
MmuChangePermission(src, pagesCount, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(src, pagesCount, state, MemoryPermission.None, MemoryAttribute.Borrowed);
|
|
|
|
InsertBlock(dst, pagesCount, MemoryState.ModCodeStatic);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult UnmapProcessCodeMemory(ulong dst, ulong src, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
bool success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
2018-11-28 23:18:09 +01:00
|
|
|
PageSize,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
2018-11-28 23:18:09 +01:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
|
|
|
size,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Mask,
|
2018-12-06 12:16:24 +01:00
|
|
|
state,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuUnmap(dst, pagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
//TODO: Missing some checks here.
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Unmapped);
|
|
|
|
InsertBlock(src, pagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult SetHeapSize(ulong size, out ulong address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (size > HeapRegionEnd - HeapRegionStart)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currentHeapSize = GetHeapSize();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currentHeapSize <= size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
//Expand.
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong diffSize = size - currentHeapSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currentProcess.ResourceLimit != null && diffSize != 0 &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, diffSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = diffSize / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = region.AllocatePages(pagesCount, _aslrDisabled, out KPageList pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
void CleanUpForError()
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (pageList != null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
region.FreePages(pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currentProcess.ResourceLimit != null && diffSize != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currentProcess.ResourceLimit.Release(LimitableResource.Memory, diffSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!IsUnmapped(_currentHeapAddr, diffSize))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = DoMmuOperation(
|
|
|
|
_currentHeapAddr,
|
|
|
|
pagesCount,
|
|
|
|
pageList,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryOperation.MapVa);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(_currentHeapAddr, pagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Shrink.
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong freeAddr = HeapRegionStart + size;
|
|
|
|
ulong diffSize = currentHeapSize - size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
freeAddr,
|
|
|
|
diffSize,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
2018-11-28 23:18:09 +01:00
|
|
|
out _))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = diffSize / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuUnmap(freeAddr, pagesCount);
|
2018-08-17 15:39:35 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, BitUtils.AlignDown(diffSize, PageSize));
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(freeAddr, pagesCount, MemoryState.Unmapped);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_currentHeapAddr = HeapRegionStart + size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
address = HeapRegionStart;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public ulong GetTotalHeapSize()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return GetHeapSize() + PhysicalMemoryUsage;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private ulong GetHeapSize()
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _currentHeapAddr - HeapRegionStart;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult SetHeapCapacity(ulong capacity)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_heapCapacity = capacity;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult SetMemoryAttribute(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeValue)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.AttributeChangeAllowed,
|
|
|
|
MemoryState.AttributeChangeAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.BorrowedAndIpcMapped,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.DeviceMappedAndUncached,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
|
|
|
out MemoryPermission permission,
|
|
|
|
out MemoryAttribute attribute))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
attribute &= ~attributeMask;
|
|
|
|
attribute |= attributeMask & attributeValue;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, state, permission, attribute);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KMemoryInfo QueryMemory(ulong address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (address >= AddrSpaceStart &&
|
|
|
|
address < AddrSpaceEnd)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return FindBlock(address).GetInfo();
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new KMemoryInfo(
|
|
|
|
AddrSpaceEnd,
|
2018-11-28 23:18:09 +01:00
|
|
|
~AddrSpaceEnd + 1,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.Reserved,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult Map(ulong dst, ulong src, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
bool success;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState srcState,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success &= IsUnmapped(dst, size);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList pageList = new KPageList();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddVaRangeToPageList(pageList, src, pagesCount);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuChangePermission(src, pagesCount, MemoryPermission.None);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MapPages(dst, pageList, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (MmuChangePermission(src, pagesCount, MemoryPermission.ReadAndWrite) != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure reverting memory permission.");
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(src, pagesCount, srcState, MemoryPermission.None, MemoryAttribute.Borrowed);
|
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Stack, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult UnmapForKernel(ulong address, ulong pagesCount, MemoryState stateExpected)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Mask,
|
2018-12-06 12:16:24 +01:00
|
|
|
stateExpected,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuUnmap(address, pagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult Unmap(ulong dst, ulong src, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
bool success;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
success = CheckRange(
|
|
|
|
src,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState srcState,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
success &= CheckRange(
|
|
|
|
dst,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.Mask,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryState.Stack,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryPermission dstPermission,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList srcPageList = new KPageList();
|
|
|
|
KPageList dstPageList = new KPageList();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddVaRangeToPageList(srcPageList, src, pagesCount);
|
|
|
|
AddVaRangeToPageList(dstPageList, dst, pagesCount);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!dstPageList.IsEqual(srcPageList))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = MmuUnmap(dst, pagesCount);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MmuChangePermission(src, pagesCount, MemoryPermission.ReadAndWrite);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
MapPages(dst, dstPageList, dstPermission);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(src, pagesCount, srcState, MemoryPermission.ReadAndWrite);
|
|
|
|
InsertBlock(dst, pagesCount, MemoryState.Unmapped);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult ReserveTransferMemory(ulong address, ulong size, MemoryPermission permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryAttribute attribute))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
//TODO: Missing checks.
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
attribute |= MemoryAttribute.Borrowed;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, state, permission, attribute);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult ResetTransferMemory(ulong address, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState state,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, state, MemoryPermission.ReadAndWrite);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult SetProcessMemoryPermission(ulong address, ulong size, MemoryPermission permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-06 12:16:24 +01:00
|
|
|
out MemoryState oldState,
|
|
|
|
out MemoryPermission oldPermission,
|
2018-08-15 20:59:51 +02:00
|
|
|
out _))
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryState newState = oldState;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//If writing into the code region is allowed, then we need
|
|
|
|
//to change it to mutable.
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((permission & MemoryPermission.Write) != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (oldState == MemoryState.CodeStatic)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newState = MemoryState.CodeMutable;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (oldState == MemoryState.ModCodeStatic)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newState = MemoryState.ModCodeMutable;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new InvalidOperationException($"Memory state \"{oldState}\" not valid for this operation.");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newState != oldState || permission != oldPermission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryOperation operation = (permission & MemoryPermission.Execute) != 0
|
2018-11-28 23:18:09 +01:00
|
|
|
? MemoryOperation.ChangePermsAndAttributes
|
|
|
|
: MemoryOperation.ChangePermRw;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = DoMmuOperation(address, pagesCount, 0, false, permission, operation);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, newState, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult MapPhysicalMemory(ulong address, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong mappedSize = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State != MemoryState.Unmapped)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
mappedSize += GetSizeInRange(info, address, endAddr);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (info.Address + info.Size < endAddr && node != null);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (mappedSize == size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong remainingSize = size - mappedSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong remainingPages = remainingSize / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, remainingSize))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.ResLimitExceeded;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = region.AllocatePages(remainingPages, _aslrDisabled, out KPageList pageList);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (pageList != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
region.FreePages(pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, remainingSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-17 15:39:35 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-17 15:39:35 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MapPhysicalMemory(pageList, address, endAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
PhysicalMemoryUsage += remainingSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
InsertBlock(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.Success;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult UnmapPhysicalMemory(ulong address, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01: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 12:16:24 +01:00
|
|
|
ulong heapMappedSize = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList pageList = new KPageList();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> baseNode = FindBlockNode(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = baseNode;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State == MemoryState.Heap)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.Attribute != MemoryAttribute.None)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
|
|
|
ulong blockAddress = GetAddrInRange(info, address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
AddVaRangeToPageList(pageList, blockAddress, blockSize / PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
heapMappedSize += blockSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (info.State != MemoryState.Unmapped)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (info.Address + info.Size < endAddr && node != null);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (heapMappedSize == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try to unmap all the heap mapped memory inside range.
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = baseNode;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State == MemoryState.Heap)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
|
|
|
ulong blockAddress = GetAddrInRange(info, address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MmuUnmap(blockAddress, blockPagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//If we failed to unmap, we need to remap everything back again.
|
2018-12-06 12:16:24 +01:00
|
|
|
MapPhysicalMemory(pageList, address, blockAddress + blockSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (info.Address + info.Size < endAddr && node != null);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
GetMemoryRegionManager().FreePages(pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
PhysicalMemoryUsage -= heapMappedSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KProcess currentProcess = _system.Scheduler.GetCurrentProcess();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, heapMappedSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = size / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
InsertBlock(address, pagesCount, MemoryState.Unmapped);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private void MapPhysicalMemory(KPageList pageList, ulong address, ulong endAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KPageNode> pageListNode = pageList.Nodes.First;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageNode pageNode = pageListNode.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong srcPa = pageNode.Address;
|
|
|
|
ulong srcPaPages = pageNode.PagesCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State == MemoryState.Unmapped)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong blockSize = GetSizeInRange(info, address, endAddr);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong dstVaPages = blockSize / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong dstVa = GetAddrInRange(info, address);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (dstVaPages > 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (srcPaPages == 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
pageListNode = pageListNode.Next;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
pageNode = pageListNode.Value;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
srcPa = pageNode.Address;
|
|
|
|
srcPaPages = pageNode.PagesCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = srcPaPages;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (pagesCount > dstVaPages)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
pagesCount = dstVaPages;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
DoMmuOperation(
|
2018-12-06 12:16:24 +01:00
|
|
|
dstVa,
|
|
|
|
pagesCount,
|
|
|
|
srcPa,
|
2018-11-28 23:18:09 +01:00
|
|
|
true,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryOperation.MapPa);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
dstVa += pagesCount * PageSize;
|
|
|
|
srcPa += pagesCount * PageSize;
|
|
|
|
srcPaPages -= pagesCount;
|
|
|
|
dstVaPages -= pagesCount;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 15:39:35 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (info.Address + info.Size < endAddr && node != null);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static ulong GetSizeInRange(KMemoryInfo info, ulong start, ulong end)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = info.Size + info.Address;
|
|
|
|
ulong size = info.Size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.Address < start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
size -= start - info.Address;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (endAddr > end)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
size -= endAddr - end;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return size;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static ulong GetAddrInRange(KMemoryInfo info, ulong start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.Address < start)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return start;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return info.Address;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private void AddVaRangeToPageList(KPageList pageList, ulong start, ulong pagesCount)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address = start;
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (address < start + pagesCount * PageSize)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = ConvertVaToPa(address, out ulong pa);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure translating virtual address.");
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
pageList.AddRange(pa, 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
address += PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private bool IsUnmapped(ulong address, ulong size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
return CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
size,
|
2018-08-15 20:59:51 +02:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permissionMask,
|
|
|
|
MemoryPermission permissionExpected,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
MemoryAttribute attributeIgnoreMask,
|
|
|
|
out MemoryState outState,
|
|
|
|
out MemoryPermission outPermission,
|
|
|
|
out MemoryAttribute outAttribute)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryState firstState = info.State;
|
|
|
|
MemoryPermission firstPermission = info.Permission;
|
|
|
|
MemoryAttribute firstAttribute = info.Attribute;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
do
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//Check if the block state matches what we expect.
|
2018-12-06 12:16:24 +01:00
|
|
|
if ( firstState != info.State ||
|
|
|
|
firstPermission != info.Permission ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected ||
|
|
|
|
(firstAttribute | attributeIgnoreMask) != (info.Attribute | attributeIgnoreMask) ||
|
|
|
|
(firstState & stateMask) != stateExpected ||
|
|
|
|
(firstPermission & permissionMask) != permissionExpected)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if this is the last block on the range, if so return success.
|
2018-12-06 12:16:24 +01:00
|
|
|
if (endAddr <= info.Address + info.Size - 1)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
outState = firstState;
|
|
|
|
outPermission = firstPermission;
|
|
|
|
outAttribute = firstAttribute & ~attributeIgnoreMask;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
outState = MemoryState.Unmapped;
|
|
|
|
outPermission = MemoryPermission.None;
|
|
|
|
outAttribute = MemoryAttribute.None;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private bool CheckRange(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permissionMask,
|
|
|
|
MemoryPermission permissionExpected,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//Check if the block state matches what we expect.
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((info.State & stateMask) != stateExpected ||
|
|
|
|
(info.Permission & permissionMask) != permissionExpected ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if this is the last block on the range, if so return success.
|
2018-12-06 12:16:24 +01:00
|
|
|
if (endAddr <= info.Address + info.Size - 1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
private void InsertBlock(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState oldState,
|
|
|
|
MemoryPermission oldPermission,
|
|
|
|
MemoryAttribute oldAttribute,
|
|
|
|
MemoryState newState,
|
|
|
|
MemoryPermission newPermission,
|
|
|
|
MemoryAttribute newAttribute)
|
2018-08-15 20:59:51 +02: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.
|
2018-12-06 12:16:24 +01:00
|
|
|
int oldCount = _blocks.Count;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
oldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = pagesCount * PageSize + baseAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
|
|
|
LinkedListNode<KMemoryBlock> nextNode = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
MemoryAttribute currBlockAttr = currBlock.Attribute | MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currBlock.State != oldState ||
|
|
|
|
currBlock.Permission != oldPermission ||
|
|
|
|
currBlockAttr != oldAttribute)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
node = nextNode;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currBaseAddr >= baseAddress && currEndAddr <= endAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.State = newState;
|
|
|
|
currBlock.Permission = newPermission;
|
|
|
|
currBlock.Attribute &= ~MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
currBlock.Attribute |= newAttribute;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (currBaseAddr >= baseAddress)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.BaseAddress = endAddr;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (currEndAddr - endAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong newPagesCount = (endAddr - currBaseAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddBefore(node, new KMemoryBlock(
|
|
|
|
currBaseAddr,
|
|
|
|
newPagesCount,
|
|
|
|
newState,
|
|
|
|
newPermission,
|
|
|
|
newAttribute));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (currEndAddr <= endAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (baseAddress - currBaseAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong newPagesCount = (currEndAddr - baseAddress) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddAfter(node, new KMemoryBlock(
|
|
|
|
baseAddress,
|
|
|
|
newPagesCount,
|
|
|
|
newState,
|
|
|
|
newPermission,
|
|
|
|
newAttribute));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (baseAddress - currBaseAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong nextPagesCount = (currEndAddr - endAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddAfter(node, new KMemoryBlock(
|
|
|
|
baseAddress,
|
|
|
|
pagesCount,
|
|
|
|
newState,
|
|
|
|
newPermission,
|
|
|
|
newAttribute));
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks.AddAfter(newNode, new KMemoryBlock(
|
|
|
|
endAddr,
|
|
|
|
nextPagesCount,
|
|
|
|
currBlock.State,
|
|
|
|
currBlock.Permission,
|
|
|
|
currBlock.Attribute));
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
nextNode = null;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MergeEqualStateNeighbours(newNode);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = nextNode;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertBlock(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
|
|
|
MemoryPermission permission = MemoryPermission.None,
|
|
|
|
MemoryAttribute attribute = MemoryAttribute.None)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
//Inserts new block at the list, replacing and spliting
|
|
|
|
//existing blocks as needed.
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock block = new KMemoryBlock(baseAddress, pagesCount, state, permission, attribute);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int oldCount = _blocks.Count;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = pagesCount * PageSize + baseAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = null;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> nextNode = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (baseAddress >= currBaseAddr && endAddr <= currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
block.Attribute |= currBlock.Attribute & MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (baseAddress > currBaseAddr && endAddr < currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (baseAddress - currBaseAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong nextPagesCount = (currEndAddr - endAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddAfter(node, block);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks.AddAfter(newNode, new KMemoryBlock(
|
|
|
|
endAddr,
|
|
|
|
nextPagesCount,
|
|
|
|
currBlock.State,
|
|
|
|
currBlock.Permission,
|
|
|
|
currBlock.Attribute));
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (baseAddress <= currBaseAddr && endAddr < currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.BaseAddress = endAddr;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (currEndAddr - endAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newNode == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddBefore(node, block);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
else if (baseAddress > currBaseAddr && endAddr >= currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
currBlock.PagesCount = (baseAddress - currBaseAddr) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newNode == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddAfter(node, block);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newNode == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddBefore(node, block);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks.Remove(node);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = nextNode;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (newNode == null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
newNode = _blocks.AddFirst(block);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MergeEqualStateNeighbours(newNode);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private void MergeEqualStateNeighbours(LinkedListNode<KMemoryBlock> node)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock block = node.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = block.PagesCount * PageSize + block.BaseAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (node.Previous != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock previous = node.Previous.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (BlockStateEquals(block, previous))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks.Remove(node.Previous);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
block.BaseAddress = previous.BaseAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (node.Next != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock next = node.Next.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (BlockStateEquals(block, next))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks.Remove(node.Next);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
endAddr = next.BaseAddress + next.PagesCount * PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
block.PagesCount = (endAddr - block.BaseAddress) / PageSize;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private static bool BlockStateEquals(KMemoryBlock lhs, KMemoryBlock rhs)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return lhs.State == rhs.State &&
|
|
|
|
lhs.Permission == rhs.Permission &&
|
|
|
|
lhs.Attribute == rhs.Attribute &&
|
|
|
|
lhs.DeviceRefCount == rhs.DeviceRefCount &&
|
|
|
|
lhs.IpcRefCount == rhs.IpcRefCount;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private ulong FindFirstFit(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment,
|
|
|
|
ulong reservedStart,
|
|
|
|
ulong reservedPagesCount)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong reservedSize = reservedPagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong totalNeededSize = reservedSize + neededPagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionEndAddr = regionStart + regionPagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(regionStart);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (regionEndAddr >= info.Address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (info.State == MemoryState.Unmapped)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currBaseAddr = info.Address + reservedSize;
|
|
|
|
ulong currEndAddr = info.Address + info.Size - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address = BitUtils.AlignDown(currBaseAddr, alignment) + reservedStart;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (currBaseAddr > address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
address += (ulong)alignment;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong allocationEndAddr = address + totalNeededSize - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (allocationEndAddr <= regionEndAddr &&
|
|
|
|
allocationEndAddr <= currEndAddr &&
|
|
|
|
address < allocationEndAddr)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return address;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (node == null)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
info = node.Value.GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KMemoryBlock FindBlock(ulong address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return FindBlockNode(address)?.Value;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private LinkedListNode<KMemoryBlock> FindBlockNode(ulong address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
while (node != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryBlock block = node.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currEndAddr = block.PagesCount * PageSize + block.BaseAddress;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (block.BaseAddress <= address && currEndAddr - 1 >= address)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return node;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
node = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private bool ValidateRegionForState(ulong address, ulong size, MemoryState state)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionBaseAddr = GetBaseAddrForState(state);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong regionEndAddr = regionBaseAddr + GetSizeForState(state);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
bool InsideRegion()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return regionBaseAddr <= address &&
|
|
|
|
endAddr > address &&
|
|
|
|
endAddr - 1 <= regionEndAddr - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OutsideHeapRegion()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return endAddr <= HeapRegionStart ||
|
|
|
|
address >= HeapRegionEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OutsideMapRegion()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return endAddr <= AliasRegionStart ||
|
|
|
|
address >= AliasRegionEnd;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
switch (state)
|
2018-11-28 23:18:09 +01: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:
|
|
|
|
return InsideRegion() && OutsideHeapRegion() && OutsideMapRegion();
|
|
|
|
|
|
|
|
case MemoryState.Heap:
|
|
|
|
return InsideRegion() && OutsideMapRegion();
|
|
|
|
|
|
|
|
case MemoryState.IpcBuffer0:
|
|
|
|
case MemoryState.IpcBuffer1:
|
|
|
|
case MemoryState.IpcBuffer3:
|
|
|
|
return InsideRegion() && OutsideHeapRegion();
|
|
|
|
|
|
|
|
case MemoryState.KernelStack:
|
|
|
|
return InsideRegion();
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private ulong GetBaseAddrForState(MemoryState state)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
switch (state)
|
2018-11-28 23:18:09 +01: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 12:16:24 +01:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private ulong GetSizeForState(MemoryState state)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
switch (state)
|
2018-11-28 23:18:09 +01: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 12:16:24 +01:00
|
|
|
throw new ArgumentException($"Invalid state value \"{state}\".");
|
2018-11-28 23:18:09 +01: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!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KernelResult MapPages(ulong address, KPageList pageList, MemoryPermission permission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong currAddr = address;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
foreach (KPageNode pageNode in pageList)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
result = DoMmuOperation(
|
|
|
|
currAddr,
|
|
|
|
pageNode.PagesCount,
|
|
|
|
pageNode.Address,
|
2018-11-28 23:18:09 +01:00
|
|
|
true,
|
2018-12-06 12:16:24 +01:00
|
|
|
permission,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryOperation.MapPa);
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryInfo info = FindBlock(currAddr).GetInfo();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCount = (address - currAddr) / PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MmuUnmap(address, pagesCount);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
currAddr += pageNode.PagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KernelResult MmuUnmap(ulong address, ulong pagesCount)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryOperation.Unmap);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KernelResult MmuChangePermission(ulong address, ulong pagesCount, MemoryPermission permission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-06 12:16:24 +01:00
|
|
|
address,
|
|
|
|
pagesCount,
|
2018-11-28 23:18:09 +01:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-06 12:16:24 +01:00
|
|
|
permission,
|
2018-11-28 23:18:09 +01:00
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong dstVa,
|
|
|
|
ulong pagesCount,
|
|
|
|
ulong srcPa,
|
|
|
|
bool map,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryOperation operation)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (map != (operation == MemoryOperation.MapPa))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new ArgumentException(nameof(map) + " value is invalid for this operation.");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
KernelResult result;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
switch (operation)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
case MemoryOperation.MapPa:
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_cpuMemory.Map((long)dstVa, (long)(srcPa - DramMemoryMap.DramBase), (long)size);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Allocate:
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryRegionManager region = GetMemoryRegionManager();
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = region.AllocatePages(pagesCount, _aslrDisabled, out KPageList pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (result == KernelResult.Success)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
result = MmuMapPages(dstVa, pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Unmap:
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_cpuMemory.Unmap((long)dstVa, (long)size);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
result = KernelResult.Success;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
case MemoryOperation.ChangePermRw: result = KernelResult.Success; break;
|
|
|
|
case MemoryOperation.ChangePermsAndAttributes: result = KernelResult.Success; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
default: throw new ArgumentException($"Invalid operation \"{operation}\".");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return result;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong pagesCount,
|
|
|
|
KPageList pageList,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryOperation operation)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (operation != MemoryOperation.MapVa)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
throw new ArgumentException($"Invalid memory operation \"{operation}\" specified.");
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return MmuMapPages(address, pageList);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private KMemoryRegionManager GetMemoryRegionManager()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _system.MemoryRegions[(int)_memRegion];
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private KernelResult MmuMapPages(ulong address, KPageList pageList)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
foreach (KPageNode pageNode in pageList)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong size = pageNode.PagesCount * PageSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_cpuMemory.Map((long)address, (long)(pageNode.Address - DramMemoryMap.DramBase), (long)size);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
address += size;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public KernelResult ConvertVaToPa(ulong va, out ulong pa)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
pa = DramMemoryMap.DramBase + (ulong)_cpuMemory.GetPhysicalAddress((long)va);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetMmUsedPages()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
lock (_blocks)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return BitUtils.DivRoundUp(GetMmUsedSize(), PageSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private long GetMmUsedSize()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _blocks.Count * KMemoryBlockSize;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool IsInvalidRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return address + size - 1 > GetAddrSpaceBaseAddr() + GetAddrSpaceSize() - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool InsideAddrSpace(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return AddrSpaceStart <= address && address + size - 1 <= AddrSpaceEnd - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool InsideAliasRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return address + size > AliasRegionStart && AliasRegionEnd > address;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool InsideHeapRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return address + size > HeapRegionStart && HeapRegionEnd > address;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool InsideStackRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return address + size > StackRegionStart && StackRegionEnd > address;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool OutsideAliasRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return AliasRegionStart > address || address + size - 1 > AliasRegionEnd - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool OutsideAddrSpace(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return AddrSpaceStart > address || address + size - 1 > AddrSpaceEnd - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public bool OutsideStackRegion(ulong address, ulong size)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return StackRegionStart > address || address + size - 1 > StackRegionEnd - 1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|