2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
2020-05-04 00:54:50 +02:00
|
|
|
using Ryujinx.Cpu;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2018-08-15 20:59:51 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
class KMemoryManager
|
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
private static readonly int[] MappingUnitSizes = new int[]
|
|
|
|
{
|
|
|
|
0x1000,
|
|
|
|
0x10000,
|
|
|
|
0x200000,
|
|
|
|
0x400000,
|
|
|
|
0x2000000,
|
|
|
|
0x40000000
|
|
|
|
};
|
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
public const int PageSize = 0x1000;
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
private const int KMemoryBlockSize = 0x40;
|
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// We need 2 blocks for the case where a big block
|
|
|
|
// needs to be split in 2, plus one block that will be the new one inserted.
|
2018-11-28 23:18:09 +01:00
|
|
|
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
|
|
|
|
2019-10-31 19:09:03 +01:00
|
|
|
private MemoryManager _cpuMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
private KernelContext _context;
|
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;
|
2020-04-20 23:59:59 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
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
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
public KMemoryManager(KernelContext context, MemoryManager cpuMemory)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
_context = context;
|
2018-12-06 12:16:24 +01:00
|
|
|
_cpuMemory = cpuMemory;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blocks = new LinkedList<KMemoryBlock>();
|
2020-04-20 23:59:59 +02:00
|
|
|
|
|
|
|
_isKernel = false;
|
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
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
_contextId = _context.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
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
_context.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
|
|
|
{
|
2019-07-02 04:39:22 +02: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
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-07-02 04:39:22 +02: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
|
|
|
{
|
2019-07-02 04:39:22 +02: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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
_blocks.AddFirst(new KMemoryBlock(
|
|
|
|
addrSpaceStart,
|
|
|
|
addrSpacePagesCount,
|
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.None));
|
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
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// TODO.
|
2018-11-28 23:18:09 +01:00
|
|
|
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
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// TODO.
|
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 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
|
|
|
lock (_blocks)
|
2018-10-10 01:01:49 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
address = AllocateVa(regionStart, regionPagesCount, neededPagesCount, alignment);
|
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
|
|
|
|
2019-07-02 04:39:22 +02: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
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
lock (_blocks)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
ulong currentHeapSize = GetHeapSize();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
if (currentHeapSize <= size)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
// Expand.
|
|
|
|
ulong sizeDelta = size - currentHeapSize;
|
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null && sizeDelta != 0 &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Memory, sizeDelta))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
ulong pagesCount = sizeDelta / 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
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
if (currentProcess.ResourceLimit != null && sizeDelta != 0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
currentProcess.ResourceLimit.Release(LimitableResource.Memory, sizeDelta);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
if (!IsUnmapped(_currentHeapAddr, sizeDelta))
|
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
|
|
|
}
|
2020-07-17 06:18:31 +02:00
|
|
|
else
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-17 06:18:31 +02:00
|
|
|
// Shrink.
|
|
|
|
ulong freeAddr = HeapRegionStart + size;
|
|
|
|
ulong sizeDelta = currentHeapSize - size;
|
|
|
|
|
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,
|
2020-07-17 06:18:31 +02:00
|
|
|
sizeDelta,
|
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
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
ulong pagesCount = sizeDelta / 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
|
|
|
|
2020-07-17 06:18:31 +02:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Memory, sizeDelta);
|
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
|
|
|
}
|
|
|
|
|
2020-07-17 06:18:31 +02: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,
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryPermission.None,
|
2018-08-15 20:59:51 +02:00
|
|
|
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
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// TODO: Missing checks.
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-07-02 04:39:22 +02: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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
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
|
|
|
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
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
KProcess currentProcess = _context.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
|
|
|
{
|
2019-07-02 04:39:22 +02: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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// 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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
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
|
|
|
{
|
2019-07-02 04:39:22 +02: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-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
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
KProcess currentProcess = _context.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
|
|
|
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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, endAddr))
|
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-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public KernelResult CopyDataToCurrentProcess(
|
|
|
|
ulong dst,
|
|
|
|
ulong size,
|
|
|
|
ulong src,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Client -> server.
|
2019-01-18 23:26:39 +01:00
|
|
|
return CopyDataFromOrToCurrentProcess(
|
|
|
|
size,
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
toServer: true);
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public KernelResult CopyDataFromCurrentProcess(
|
|
|
|
ulong dst,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
ulong src)
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Server -> client.
|
2019-01-18 23:26:39 +01:00
|
|
|
return CopyDataFromOrToCurrentProcess(
|
|
|
|
size,
|
|
|
|
dst,
|
|
|
|
src,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
toServer: false);
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private KernelResult CopyDataFromOrToCurrentProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong clientAddress,
|
|
|
|
ulong serverAddress,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
bool toServer)
|
|
|
|
{
|
|
|
|
if (AddrSpaceStart > clientAddress)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong srcEndAddr = clientAddress + size;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (srcEndAddr <= clientAddress || srcEndAddr - 1 > AddrSpaceEnd - 1)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
clientAddress,
|
|
|
|
size,
|
|
|
|
stateMask,
|
|
|
|
stateExpected,
|
|
|
|
permission,
|
|
|
|
permission,
|
|
|
|
attributeMask | MemoryAttribute.Uncached,
|
|
|
|
attributeExpected))
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KProcess currentProcess = _context.Scheduler.GetCurrentProcess();
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
serverAddress = currentProcess.MemoryManager.GetDramAddressFromVa(serverAddress);
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (toServer)
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
_context.Memory.Copy(serverAddress, GetDramAddressFromVa(clientAddress), size);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
_context.Memory.Copy(GetDramAddressFromVa(clientAddress), serverAddress, size);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 01:01:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public KernelResult MapBufferFromClientProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong src,
|
|
|
|
KMemoryManager sourceMemMgr,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryState state,
|
|
|
|
bool copyData,
|
|
|
|
out ulong dst)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
dst = 0;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
KernelResult result = sourceMemMgr.GetPagesForMappingIntoAnotherProcess(
|
|
|
|
src,
|
|
|
|
size,
|
|
|
|
permission,
|
|
|
|
state,
|
|
|
|
copyData,
|
|
|
|
_aslrDisabled,
|
|
|
|
_memRegion,
|
|
|
|
out KPageList pageList);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (result != KernelResult.Success)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return result;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
result = MapPagesFromAnotherProcess(size, src, permission, state, pageList, out ulong va);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
sourceMemMgr.UnmapIpcRestorePermission(src, size, state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst = va;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
return result;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private KernelResult GetPagesForMappingIntoAnotherProcess(
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong address,
|
|
|
|
ulong size,
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryState state,
|
|
|
|
bool copyData,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion region,
|
|
|
|
out KPageList pageList)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
pageList = null;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (AddrSpaceStart > address)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong endAddr = address + size;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (endAddr <= address || endAddr - 1 > AddrSpaceEnd - 1)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryState stateMask;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case MemoryState.IpcBuffer0: stateMask = MemoryState.IpcSendAllowedType0; break;
|
|
|
|
case MemoryState.IpcBuffer1: stateMask = MemoryState.IpcSendAllowedType1; break;
|
|
|
|
case MemoryState.IpcBuffer3: stateMask = MemoryState.IpcSendAllowedType3; break;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
default: return KernelResult.InvalidCombination;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryPermission permissionMask = permission == MemoryPermission.ReadAndWrite
|
|
|
|
? MemoryPermission.None
|
|
|
|
: MemoryPermission.Read;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
MemoryAttribute attributeMask = MemoryAttribute.Borrowed | MemoryAttribute.Uncached;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (state == MemoryState.IpcBuffer0)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
attributeMask |= MemoryAttribute.DeviceMapped;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong visitedSize = 0;
|
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
if (visitedSize == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong endAddrVisited = address + visitedSize;
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressRounded, endAddrVisited))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
if ((info.Permission & MemoryPermission.ReadAndWrite) != permissionMask && info.IpcRefCount == 0)
|
|
|
|
{
|
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrVisited);
|
|
|
|
|
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
|
|
|
|
|
|
|
if (DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
info.Permission,
|
|
|
|
MemoryOperation.ChangePermRw) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure trying to restore permission.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
KernelResult result;
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (addressRounded < endAddrTruncated)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressTruncated, endAddrRounded))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
// Check if the block state matches what we expect.
|
|
|
|
if ((info.State & stateMask) != stateMask ||
|
|
|
|
(info.Permission & permission) != permission ||
|
|
|
|
(info.Attribute & attributeMask) != MemoryAttribute.None)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrTruncated);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if ((info.Permission & MemoryPermission.ReadAndWrite) != permissionMask && info.IpcRefCount == 0)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
result = DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
permissionMask,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
visitedSize += blockSize;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result = GetPagesForIpcTransfer(address, size, copyData, aslrDisabled, region, out pageList);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (visitedSize != 0)
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
InsertBlock(addressRounded, visitedSize / PageSize, SetIpcMappingPermissions, permissionMask);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult GetPagesForIpcTransfer(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
bool copyData,
|
|
|
|
bool aslrDisabled,
|
|
|
|
MemoryRegion region,
|
|
|
|
out KPageList pageList)
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
// When the start address is unaligned, we can't safely map the
|
|
|
|
// first page as it would expose other undesirable information on the
|
|
|
|
// target process. So, instead we allocate new pages, copy the data
|
|
|
|
// inside the range, and then clear the remaining space.
|
|
|
|
// The same also holds for the last page, if the end address
|
|
|
|
// (address + size) is also not aligned.
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
pageList = null;
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
KPageList pages = new KPageList();
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong dstFirstPagePa = 0;
|
|
|
|
ulong dstLastPagePa = 0;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
if (dstFirstPagePa != 0)
|
|
|
|
{
|
|
|
|
FreeSinglePage(region, dstFirstPagePa);
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
if (dstLastPagePa != 0)
|
|
|
|
{
|
|
|
|
FreeSinglePage(region, dstLastPagePa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
// Is the first page address aligned?
|
|
|
|
// If not, allocate a new page and copy the unaligned chunck.
|
|
|
|
if (addressTruncated < addressRounded)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
dstFirstPagePa = AllocateSinglePage(region, aslrDisabled);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (dstFirstPagePa == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong firstPageFillAddress = dstFirstPagePa;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (!TryConvertVaToPa(addressTruncated, out ulong srcFirstPagePa))
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong unusedSizeAfter;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (copyData)
|
|
|
|
{
|
|
|
|
ulong unusedSizeBefore = address - addressTruncated;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
_context.Memory.ZeroFill(dstFirstPagePa, unusedSizeBefore);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong copySize = addressRounded <= endAddr ? addressRounded - address : size;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
_context.Memory.Copy(
|
|
|
|
GetDramAddressFromPa(dstFirstPagePa + unusedSizeBefore),
|
|
|
|
GetDramAddressFromPa(srcFirstPagePa + unusedSizeBefore), copySize);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
firstPageFillAddress += unusedSizeBefore + copySize;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
unusedSizeAfter = addressRounded > endAddr ? addressRounded - endAddr : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unusedSizeAfter = PageSize;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (unusedSizeAfter != 0)
|
|
|
|
{
|
|
|
|
_context.Memory.ZeroFill(firstPageFillAddress, unusedSizeAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pages.AddRange(dstFirstPagePa, 1) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
if (endAddrTruncated > addressRounded)
|
|
|
|
{
|
|
|
|
ulong alignedPagesCount = (endAddrTruncated - addressRounded) / PageSize;
|
|
|
|
|
|
|
|
AddVaRangeToPageList(pages, addressRounded, alignedPagesCount);
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
// Is the last page end address aligned?
|
|
|
|
// If not, allocate a new page and copy the unaligned chunck.
|
|
|
|
if (endAddrTruncated < endAddrRounded && (addressTruncated == addressRounded || addressTruncated < endAddrTruncated))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
dstLastPagePa = AllocateSinglePage(region, aslrDisabled);
|
|
|
|
|
|
|
|
if (dstLastPagePa == 0)
|
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong lastPageFillAddr = dstLastPagePa;
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (!TryConvertVaToPa(endAddrTruncated, out ulong srcLastPagePa))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong unusedSizeAfter;
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (copyData)
|
|
|
|
{
|
|
|
|
ulong copySize = endAddr - endAddrTruncated;
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
_context.Memory.Copy(
|
2019-01-18 23:26:39 +01:00
|
|
|
GetDramAddressFromPa(dstLastPagePa),
|
|
|
|
GetDramAddressFromPa(srcLastPagePa), copySize);
|
|
|
|
|
|
|
|
lastPageFillAddr += copySize;
|
|
|
|
|
|
|
|
unusedSizeAfter = PageSize - copySize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unusedSizeAfter = PageSize;
|
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
_context.Memory.ZeroFill(lastPageFillAddr, unusedSizeAfter);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (pages.AddRange(dstLastPagePa, 1) != KernelResult.Success)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList = pages;
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong AllocateSinglePage(MemoryRegion region, bool aslrDisabled)
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KMemoryRegionManager regionMgr = _context.MemoryRegions[(int)region];
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
return regionMgr.AllocatePagesContiguous(1, aslrDisabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void FreeSinglePage(MemoryRegion region, ulong address)
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KMemoryRegionManager regionMgr = _context.MemoryRegions[(int)region];
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
regionMgr.FreePage(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult MapPagesFromAnotherProcess(
|
|
|
|
ulong size,
|
|
|
|
ulong address,
|
|
|
|
MemoryPermission permission,
|
|
|
|
MemoryState state,
|
|
|
|
KPageList pageList,
|
2020-07-17 06:19:07 +02:00
|
|
|
out ulong dst)
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
dst = 0;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
ulong neededSize = endAddrRounded - addressTruncated;
|
|
|
|
|
|
|
|
ulong neededPagesCount = neededSize / PageSize;
|
|
|
|
|
|
|
|
ulong regionPagesCount = (AliasRegionEnd - AliasRegionStart) / PageSize;
|
|
|
|
|
|
|
|
ulong va = 0;
|
|
|
|
|
|
|
|
for (int unit = MappingUnitSizes.Length - 1; unit >= 0 && va == 0; unit--)
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
int alignment = MappingUnitSizes[unit];
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
va = AllocateVa(AliasRegionStart, regionPagesCount, neededPagesCount, alignment);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (va == 0)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfVaSpace;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageList.Nodes.Count != 0)
|
|
|
|
{
|
|
|
|
KernelResult result = MapPages(va, pageList, permission);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertBlock(va, neededPagesCount, state, permission);
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
dst = va + (address - addressTruncated);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapNoAttributeIfStateEquals(ulong address, ulong size, MemoryState state)
|
|
|
|
{
|
|
|
|
if (AddrSpaceStart > address)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
if (endAddr <= address || endAddr - 1 > AddrSpaceEnd - 1)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.Mask,
|
|
|
|
state,
|
|
|
|
MemoryPermission.Read,
|
|
|
|
MemoryPermission.Read,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong addressTruncated = BitUtils.AlignDown(address, PageSize);
|
2020-07-17 06:19:07 +02:00
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong endAddrRounded = BitUtils.AlignUp (endAddr, PageSize);
|
|
|
|
|
|
|
|
ulong pagesCount = (endAddrRounded - addressTruncated) / PageSize;
|
|
|
|
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
addressTruncated,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryOperation.Unmap);
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
// Free pages we had to create on-demand, if any of the buffer was not page aligned.
|
|
|
|
// Real kernel has page ref counting, so this is done as part of the unmap operation.
|
|
|
|
if (addressTruncated != addressRounded)
|
|
|
|
{
|
|
|
|
FreeSinglePage(_memRegion, ConvertVaToPa(addressTruncated));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endAddrTruncated < endAddrRounded && (addressTruncated == addressRounded || addressTruncated < endAddrTruncated))
|
|
|
|
{
|
|
|
|
FreeSinglePage(_memRegion, ConvertVaToPa(endAddrTruncated));
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (result == KernelResult.Success)
|
|
|
|
{
|
|
|
|
InsertBlock(addressTruncated, pagesCount, MemoryState.Unmapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapIpcRestorePermission(ulong address, ulong size, MemoryState state)
|
|
|
|
{
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
ulong addressRounded = BitUtils.AlignUp (address, PageSize);
|
|
|
|
ulong endAddrTruncated = BitUtils.AlignDown(endAddr, PageSize);
|
|
|
|
|
|
|
|
ulong pagesCount = (endAddrTruncated - addressRounded) / PageSize;
|
|
|
|
|
|
|
|
MemoryState stateMask;
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case MemoryState.IpcBuffer0: stateMask = MemoryState.IpcSendAllowedType0; break;
|
|
|
|
case MemoryState.IpcBuffer1: stateMask = MemoryState.IpcSendAllowedType1; break;
|
|
|
|
case MemoryState.IpcBuffer3: stateMask = MemoryState.IpcSendAllowedType3; break;
|
|
|
|
|
|
|
|
default: return KernelResult.InvalidCombination;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute attributeMask =
|
|
|
|
MemoryAttribute.Borrowed |
|
|
|
|
MemoryAttribute.IpcMapped |
|
|
|
|
MemoryAttribute.Uncached;
|
|
|
|
|
|
|
|
if (state == MemoryState.IpcBuffer0)
|
|
|
|
{
|
|
|
|
attributeMask |= MemoryAttribute.DeviceMapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
foreach (KMemoryInfo info in IterateOverRange(addressRounded, endAddrTruncated))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 23:26:39 +01:00
|
|
|
if ((info.State & stateMask) != stateMask ||
|
|
|
|
(info.Attribute & attributeMask) != MemoryAttribute.IpcMapped)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.Permission != info.SourcePermission && info.IpcRefCount == 1)
|
|
|
|
{
|
|
|
|
ulong blockAddress = GetAddrInRange(info, addressRounded);
|
|
|
|
ulong blockSize = GetSizeInRange(info, addressRounded, endAddrTruncated);
|
|
|
|
|
|
|
|
ulong blockPagesCount = blockSize / PageSize;
|
|
|
|
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
blockAddress,
|
|
|
|
blockPagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
info.SourcePermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
InsertBlock(addressRounded, pagesCount, RestoreIpcMappingPermissions);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
public KernelResult BorrowIpcBuffer(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return SetAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Borrowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult SetAttributesAndChangePermission(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permissionMask,
|
|
|
|
MemoryPermission permissionExpected,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
MemoryPermission newPermission,
|
|
|
|
MemoryAttribute attributeSetMask,
|
|
|
|
KPageList pageList = null)
|
|
|
|
{
|
|
|
|
if (address + size <= address || !InsideAddrSpace(address, size))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
stateMask | MemoryState.IsPoolAllocated,
|
|
|
|
stateExpected | MemoryState.IsPoolAllocated,
|
|
|
|
permissionMask,
|
|
|
|
permissionExpected,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out MemoryState oldState,
|
|
|
|
out MemoryPermission oldPermission,
|
|
|
|
out MemoryAttribute oldAttribute))
|
|
|
|
{
|
|
|
|
ulong pagesCount = size / PageSize;
|
|
|
|
|
|
|
|
if (pageList != null)
|
|
|
|
{
|
|
|
|
KPageList currPageList = new KPageList();
|
|
|
|
|
|
|
|
AddVaRangeToPageList(currPageList, address, pagesCount);
|
|
|
|
|
|
|
|
if (!currPageList.IsEqual(pageList))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission == MemoryPermission.None)
|
|
|
|
{
|
|
|
|
newPermission = oldPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission != oldPermission)
|
|
|
|
{
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
newPermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute newAttribute = oldAttribute | attributeSetMask;
|
|
|
|
|
|
|
|
InsertBlock(address, pagesCount, oldState, newPermission, newAttribute);
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public KernelResult UnborrowIpcBuffer(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return ClearAttributesAndChangePermission(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryState.IpcBufferAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Borrowed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult ClearAttributesAndChangePermission(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permissionMask,
|
|
|
|
MemoryPermission permissionExpected,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected,
|
|
|
|
MemoryPermission newPermission,
|
|
|
|
MemoryAttribute attributeClearMask,
|
|
|
|
KPageList pageList = null)
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
if (address + size <= address || !InsideAddrSpace(address, size))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
lock (_blocks)
|
|
|
|
{
|
|
|
|
if (CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
stateMask | MemoryState.IsPoolAllocated,
|
|
|
|
stateExpected | MemoryState.IsPoolAllocated,
|
|
|
|
permissionMask,
|
|
|
|
permissionExpected,
|
|
|
|
attributeMask,
|
|
|
|
attributeExpected,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out MemoryState oldState,
|
|
|
|
out MemoryPermission oldPermission,
|
|
|
|
out MemoryAttribute oldAttribute))
|
|
|
|
{
|
|
|
|
ulong pagesCount = size / PageSize;
|
|
|
|
|
|
|
|
if (pageList != null)
|
|
|
|
{
|
|
|
|
KPageList currPageList = new KPageList();
|
|
|
|
|
|
|
|
AddVaRangeToPageList(currPageList, address, pagesCount);
|
|
|
|
|
|
|
|
if (!currPageList.IsEqual(pageList))
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_blockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission == MemoryPermission.None)
|
|
|
|
{
|
|
|
|
newPermission = oldPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newPermission != oldPermission)
|
|
|
|
{
|
|
|
|
KernelResult result = DoMmuOperation(
|
|
|
|
address,
|
|
|
|
pagesCount,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
newPermission,
|
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryAttribute newAttribute = oldAttribute & ~attributeClearMask;
|
|
|
|
|
|
|
|
InsertBlock(address, pagesCount, oldState, newPermission, newAttribute);
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddVaRangeToPageList(KPageList pageList, ulong start, ulong pagesCount)
|
|
|
|
{
|
|
|
|
ulong address = start;
|
|
|
|
|
|
|
|
while (address < start + pagesCount * PageSize)
|
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
if (!TryConvertVaToPa(address, out ulong pa))
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure translating virtual address.");
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList.AddRange(pa, 1);
|
|
|
|
|
|
|
|
address += PageSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ulong GetAddrInRange(KMemoryInfo info, ulong start)
|
|
|
|
{
|
|
|
|
if (info.Address < start)
|
|
|
|
{
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info.Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ulong GetSizeInRange(KMemoryInfo info, ulong start, ulong end)
|
|
|
|
{
|
|
|
|
ulong endAddr = info.Size + info.Address;
|
|
|
|
ulong size = info.Size;
|
|
|
|
|
|
|
|
if (info.Address < start)
|
|
|
|
{
|
|
|
|
size -= start - info.Address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endAddr > end)
|
|
|
|
{
|
|
|
|
size -= endAddr - end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsUnmapped(ulong address, ulong size)
|
|
|
|
{
|
|
|
|
return CheckRange(
|
|
|
|
address,
|
|
|
|
size,
|
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ulong endAddr = address + size;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(address);
|
|
|
|
|
|
|
|
KMemoryInfo info = node.Value.GetInfo();
|
|
|
|
|
|
|
|
MemoryState firstState = info.State;
|
|
|
|
MemoryPermission firstPermission = info.Permission;
|
|
|
|
MemoryAttribute firstAttribute = info.Attribute;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
info = node.Value.GetInfo();
|
|
|
|
|
2019-07-02 04:39:22 +02:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 23:26:39 +01:00
|
|
|
if ( firstState != info.State ||
|
|
|
|
firstPermission != info.Permission ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected ||
|
|
|
|
(firstAttribute | attributeIgnoreMask) != (info.Attribute | attributeIgnoreMask) ||
|
|
|
|
(firstState & stateMask) != stateExpected ||
|
|
|
|
(firstPermission & permissionMask) != permissionExpected)
|
|
|
|
{
|
|
|
|
outState = MemoryState.Unmapped;
|
|
|
|
outPermission = MemoryPermission.None;
|
|
|
|
outAttribute = MemoryAttribute.None;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (info.Address + info.Size - 1 < endAddr - 1 && (node = node.Next) != null);
|
|
|
|
|
|
|
|
outState = firstState;
|
|
|
|
outPermission = firstPermission;
|
|
|
|
outAttribute = firstAttribute & ~attributeIgnoreMask;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
MemoryState stateMask,
|
|
|
|
MemoryState stateExpected,
|
|
|
|
MemoryPermission permissionMask,
|
|
|
|
MemoryPermission permissionExpected,
|
|
|
|
MemoryAttribute attributeMask,
|
|
|
|
MemoryAttribute attributeExpected)
|
|
|
|
{
|
|
|
|
foreach (KMemoryInfo info in IterateOverRange(address, address + size))
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Check if the block state matches what we expect.
|
2019-01-18 23:26:39 +01:00
|
|
|
if ((info.State & stateMask) != stateExpected ||
|
|
|
|
(info.Permission & permissionMask) != permissionExpected ||
|
|
|
|
(info.Attribute & attributeMask) != attributeExpected)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<KMemoryInfo> IterateOverRange(ulong start, ulong end)
|
|
|
|
{
|
|
|
|
LinkedListNode<KMemoryBlock> node = FindBlockNode(start);
|
|
|
|
|
|
|
|
KMemoryInfo info;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
info = node.Value.GetInfo();
|
|
|
|
|
|
|
|
yield return info;
|
|
|
|
}
|
|
|
|
while (info.Address + info.Size - 1 < end - 1 && (node = node.Next) != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertBlock(
|
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState oldState,
|
|
|
|
MemoryPermission oldPermission,
|
|
|
|
MemoryAttribute oldAttribute,
|
|
|
|
MemoryState newState,
|
|
|
|
MemoryPermission newPermission,
|
|
|
|
MemoryAttribute newAttribute)
|
|
|
|
{
|
2019-07-02 04:39:22 +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.
|
2019-01-18 23:26:39 +01:00
|
|
|
int oldCount = _blocks.Count;
|
|
|
|
|
|
|
|
oldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
|
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
{
|
2020-05-06 14:48:57 +02:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
2018-12-06 12:16:24 +01:00
|
|
|
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
|
|
|
{
|
2020-05-06 14:48:57 +02:00
|
|
|
node = node.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (endAddr < currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
newNode.Value.SetState(newPermission, newState, newAttribute);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
node = newNode.Next;
|
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
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Inserts new block at the list, replacing and splitting
|
|
|
|
// existing blocks as needed.
|
2018-12-06 12:16:24 +01:00
|
|
|
int oldCount = _blocks.Count;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
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
|
|
|
{
|
2020-05-06 14:48:57 +02:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-06 14:48:57 +02: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
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (endAddr < currEndAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
newNode.Value.SetState(permission, state, attribute);
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
node = newNode.Next;
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private static void SetIpcMappingPermissions(KMemoryBlock block, MemoryPermission permission)
|
|
|
|
{
|
|
|
|
block.SetIpcMappingPermission(permission);
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
private static void RestoreIpcMappingPermissions(KMemoryBlock block, MemoryPermission permission)
|
|
|
|
{
|
|
|
|
block.RestoreIpcMappingPermission();
|
|
|
|
}
|
|
|
|
|
|
|
|
private delegate void BlockMutator(KMemoryBlock block, MemoryPermission newPerm);
|
|
|
|
|
|
|
|
private void InsertBlock(
|
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
BlockMutator blockMutate,
|
|
|
|
MemoryPermission permission = MemoryPermission.None)
|
|
|
|
{
|
2019-07-02 04:39:22 +02:00
|
|
|
// Inserts new block at the list, replacing and splitting
|
|
|
|
// existing blocks as needed, then calling the callback
|
|
|
|
// function on the new block.
|
2019-01-18 23:26:39 +01:00
|
|
|
int oldCount = _blocks.Count;
|
|
|
|
|
|
|
|
ulong endAddr = baseAddress + pagesCount * PageSize;
|
|
|
|
|
|
|
|
LinkedListNode<KMemoryBlock> node = _blocks.First;
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
{
|
2020-05-06 14:48:57 +02:00
|
|
|
LinkedListNode<KMemoryBlock> newNode = node;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
KMemoryBlock currBlock = node.Value;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
ulong currBaseAddr = currBlock.BaseAddress;
|
|
|
|
ulong currEndAddr = currBlock.PagesCount * PageSize + currBaseAddr;
|
|
|
|
|
|
|
|
if (baseAddress < currEndAddr && currBaseAddr < endAddr)
|
|
|
|
{
|
|
|
|
if (baseAddress > currBaseAddr)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
_blocks.AddBefore(node, currBlock.SplitRightAtAddress(baseAddress));
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (endAddr < currEndAddr)
|
|
|
|
{
|
|
|
|
newNode = _blocks.AddBefore(node, currBlock.SplitRightAtAddress(endAddr));
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
KMemoryBlock newBlock = newNode.Value;
|
|
|
|
|
|
|
|
blockMutate(newBlock, permission);
|
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
newNode = MergeEqualStateNeighbors(newNode);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (currEndAddr - 1 >= endAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
node = newNode.Next;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_blockAllocator.Count += _blocks.Count - oldCount;
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 14:48:57 +02:00
|
|
|
private LinkedListNode<KMemoryBlock> MergeEqualStateNeighbors(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
|
|
|
if (node.Previous != null)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
KMemoryBlock previousBlock = node.Previous.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (BlockStateEquals(block, previousBlock))
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
LinkedListNode<KMemoryBlock> previousNode = node.Previous;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
_blocks.Remove(node);
|
|
|
|
|
|
|
|
previousBlock.AddPages(block.PagesCount);
|
|
|
|
|
|
|
|
node = previousNode;
|
|
|
|
block = previousBlock;
|
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
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
KMemoryBlock nextBlock = node.Next.Value;
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
if (BlockStateEquals(block, nextBlock))
|
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
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
block.AddPages(nextBlock.PagesCount);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 14:48:57 +02:00
|
|
|
|
|
|
|
return node;
|
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
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
return lhs.State == rhs.State &&
|
|
|
|
lhs.Permission == rhs.Permission &&
|
|
|
|
lhs.Attribute == rhs.Attribute &&
|
|
|
|
lhs.SourcePermission == rhs.SourcePermission &&
|
|
|
|
lhs.DeviceRefCount == rhs.DeviceRefCount &&
|
|
|
|
lhs.IpcRefCount == rhs.IpcRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ulong AllocateVa(
|
|
|
|
ulong regionStart,
|
|
|
|
ulong regionPagesCount,
|
|
|
|
ulong neededPagesCount,
|
|
|
|
int alignment)
|
|
|
|
{
|
|
|
|
ulong address = 0;
|
|
|
|
|
|
|
|
ulong regionEndAddr = regionStart + regionPagesCount * PageSize;
|
|
|
|
|
|
|
|
ulong reservedPagesCount = _isKernel ? 1UL : 4UL;
|
|
|
|
|
|
|
|
if (_aslrEnabled)
|
|
|
|
{
|
|
|
|
ulong totalNeededSize = (reservedPagesCount + neededPagesCount) * PageSize;
|
|
|
|
|
|
|
|
ulong remainingPages = regionPagesCount - neededPagesCount;
|
|
|
|
|
|
|
|
ulong aslrMaxOffset = ((remainingPages + reservedPagesCount) * PageSize) / (ulong)alignment;
|
|
|
|
|
|
|
|
for (int attempt = 0; attempt < 8; attempt++)
|
|
|
|
{
|
|
|
|
address = BitUtils.AlignDown(regionStart + GetRandomValue(0, aslrMaxOffset) * (ulong)alignment, alignment);
|
|
|
|
|
|
|
|
ulong endAddr = address + totalNeededSize;
|
|
|
|
|
|
|
|
KMemoryInfo info = FindBlock(address).GetInfo();
|
|
|
|
|
|
|
|
if (info.State != MemoryState.Unmapped)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong currBaseAddr = info.Address + reservedPagesCount * PageSize;
|
|
|
|
ulong currEndAddr = info.Address + info.Size;
|
|
|
|
|
|
|
|
if (address >= regionStart &&
|
|
|
|
address >= currBaseAddr &&
|
|
|
|
endAddr - 1 <= regionEndAddr - 1 &&
|
|
|
|
endAddr - 1 <= currEndAddr - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address == 0)
|
|
|
|
{
|
|
|
|
ulong aslrPage = GetRandomValue(0, aslrMaxOffset);
|
|
|
|
|
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart + aslrPage * PageSize,
|
|
|
|
regionPagesCount - aslrPage,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
|
|
|
0,
|
|
|
|
reservedPagesCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address == 0)
|
|
|
|
{
|
|
|
|
address = FindFirstFit(
|
|
|
|
regionStart,
|
|
|
|
regionPagesCount,
|
|
|
|
neededPagesCount,
|
|
|
|
alignment,
|
|
|
|
0,
|
|
|
|
reservedPagesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
2018-08-15 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
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
_cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, 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
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
_cpuMemory.Unmap(dstVa, 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()
|
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
return _context.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
|
|
|
|
2020-05-04 00:54:50 +02:00
|
|
|
_cpuMemory.Map(address, pageNode.Address - DramMemoryMap.DramBase, 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;
|
|
|
|
}
|
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public ulong GetDramAddressFromVa(ulong va)
|
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
return _cpuMemory.GetPhysicalAddress(va);
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
public ulong ConvertVaToPa(ulong va)
|
|
|
|
{
|
|
|
|
if (!TryConvertVaToPa(va, out ulong pa))
|
|
|
|
{
|
|
|
|
throw new ArgumentException($"Invalid virtual address 0x{va:X} specified.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryConvertVaToPa(ulong va, out ulong pa)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
pa = DramMemoryMap.DramBase + _cpuMemory.GetPhysicalAddress(va);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ulong GetDramAddressFromPa(ulong pa)
|
|
|
|
{
|
|
|
|
return pa - DramMemoryMap.DramBase;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|