2018-08-15 18:59:51 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-11-28 22:18:09 +00:00
|
|
|
using Ryujinx.Common;
|
2018-08-15 18:59:51 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
class KMemoryManager
|
|
|
|
{
|
|
|
|
public const int PageSize = 0x1000;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private const int KMemoryBlockSize = 0x40;
|
|
|
|
|
|
|
|
//We need 2 blocks for the case where a big block
|
|
|
|
//needs to be split in 2, plus one block that will be the new one inserted.
|
|
|
|
private const int MaxBlocksNeededForInsertion = 2;
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private LinkedList<KMemoryBlock> Blocks;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private MemoryManager CpuMemory;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private Horizon System;
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
private ulong CurrentHeapAddr;
|
2018-11-28 22:18:09 +00: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 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public ulong TlsIoRegionStart { get; private set; }
|
|
|
|
public ulong TlsIoRegionEnd { get; private set; }
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private ulong HeapCapacity;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public ulong PhysicalMemoryUsage { get; private set; }
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private MemoryRegion MemRegion;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool AslrDisabled;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public int AddrSpaceWidth { get; private set; }
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool IsKernel;
|
|
|
|
private bool AslrEnabled;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KMemoryBlockAllocator BlockAllocator;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private int ContextId;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private MersenneTwister RandomNumberGenerator;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KMemoryManager(Horizon System, MemoryManager CpuMemory)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
this.System = System;
|
|
|
|
this.CpuMemory = CpuMemory;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks = new LinkedList<KMemoryBlock>();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private static readonly int[] AddrSpaceSizes = new int[] { 32, 36, 32, 39 };
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult InitializeForProcess(
|
2018-12-05 00:52:39 +00:00
|
|
|
AddressSpaceType AddrSpaceType,
|
|
|
|
bool AslrEnabled,
|
|
|
|
bool AslrDisabled,
|
|
|
|
MemoryRegion MemRegion,
|
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
KMemoryBlockAllocator BlockAllocator)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((uint)AddrSpaceType > (uint)AddressSpaceType.Addr39Bits)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
throw new ArgumentException(nameof(AddrSpaceType));
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ContextId = System.ContextIdManager.GetId();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AddrSpaceBase = 0;
|
|
|
|
ulong AddrSpaceSize = 1UL << AddrSpaceSizes[(int)AddrSpaceType];
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = CreateUserAddressSpace(
|
|
|
|
AddrSpaceType,
|
|
|
|
AslrEnabled,
|
|
|
|
AslrDisabled,
|
|
|
|
AddrSpaceBase,
|
|
|
|
AddrSpaceSize,
|
|
|
|
MemRegion,
|
|
|
|
Address,
|
|
|
|
Size,
|
|
|
|
BlockAllocator);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
System.ContextIdManager.PutId(ContextId);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Region
|
|
|
|
{
|
|
|
|
public ulong Start;
|
|
|
|
public ulong End;
|
|
|
|
public ulong Size;
|
|
|
|
public ulong AslrOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult CreateUserAddressSpace(
|
2018-12-05 00:52:39 +00: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 18:59:51 +00:00
|
|
|
{
|
|
|
|
case AddressSpaceType.Addr32Bits:
|
2018-12-05 00:52:39 +00:00
|
|
|
AliasRegion.Size = 0x40000000;
|
|
|
|
HeapRegion.Size = 0x40000000;
|
|
|
|
StackRegion.Size = 0;
|
|
|
|
TlsIoRegion.Size = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-05 00:52:39 +00:00
|
|
|
CodeRegionSize = 0x3fe00000;
|
|
|
|
StackAndTlsIoStart = 0x200000;
|
|
|
|
StackAndTlsIoEnd = 0x40000000;
|
|
|
|
BaseAddress = 0x200000;
|
2018-11-28 22:18:09 +00:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 18:59:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr36Bits:
|
2018-12-05 00:52:39 +00:00
|
|
|
AliasRegion.Size = 0x180000000;
|
|
|
|
HeapRegion.Size = 0x180000000;
|
|
|
|
StackRegion.Size = 0;
|
|
|
|
TlsIoRegion.Size = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
CodeRegionStart = 0x8000000;
|
2018-12-05 00:52:39 +00:00
|
|
|
CodeRegionSize = 0x78000000;
|
|
|
|
StackAndTlsIoStart = 0x8000000;
|
|
|
|
StackAndTlsIoEnd = 0x80000000;
|
|
|
|
BaseAddress = 0x8000000;
|
2018-11-28 22:18:09 +00:00
|
|
|
AddrSpaceWidth = 36;
|
2018-08-15 18:59:51 +00:00
|
|
|
break;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
case AddressSpaceType.Addr32BitsNoMap:
|
2018-12-05 00:52:39 +00:00
|
|
|
AliasRegion.Size = 0;
|
|
|
|
HeapRegion.Size = 0x80000000;
|
|
|
|
StackRegion.Size = 0;
|
|
|
|
TlsIoRegion.Size = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
CodeRegionStart = 0x200000;
|
2018-12-05 00:52:39 +00:00
|
|
|
CodeRegionSize = 0x3fe00000;
|
|
|
|
StackAndTlsIoStart = 0x200000;
|
|
|
|
StackAndTlsIoEnd = 0x40000000;
|
|
|
|
BaseAddress = 0x200000;
|
2018-11-28 22:18:09 +00:00
|
|
|
AddrSpaceWidth = 32;
|
2018-08-15 18:59:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AddressSpaceType.Addr39Bits:
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
AddrSpaceWidth = 39;
|
2018-08-15 18:59:51 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
default: throw new ArgumentException(nameof(AddrSpaceType));
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CodeRegionEnd = CodeRegionStart + CodeRegionSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong MapBaseAddress;
|
|
|
|
ulong MapAvailableSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CodeRegionStart - BaseAddress >= AddrSpaceEnd - CodeRegionEnd)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
//Has more space before the start of the code region.
|
2018-12-05 00:52:39 +00:00
|
|
|
MapBaseAddress = BaseAddress;
|
|
|
|
MapAvailableSize = CodeRegionStart - BaseAddress;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Has more space after the end of the code region.
|
2018-12-05 00:52:39 +00:00
|
|
|
MapBaseAddress = CodeRegionEnd;
|
|
|
|
MapAvailableSize = AddrSpaceEnd - CodeRegionEnd;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong MapTotalSize = AliasRegion.Size + HeapRegion.Size + StackRegion.Size + TlsIoRegion.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AslrMaxOffset = MapAvailableSize - MapTotalSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
this.AslrEnabled = AslrEnabled;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
this.AddrSpaceStart = AddrSpaceStart;
|
|
|
|
this.AddrSpaceEnd = AddrSpaceEnd;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
this.BlockAllocator = BlockAllocator;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (MapAvailableSize < MapTotalSize)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (AslrEnabled)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Regions are sorted based on ASLR offset.
|
|
|
|
//When ASLR is disabled, the order is Map, Heap, NewMap and TlsIo.
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
SortRegion(HeapRegion, AliasRegion);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (StackRegion.Size != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
SortRegion(StackRegion, AliasRegion);
|
|
|
|
SortRegion(StackRegion, HeapRegion);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
StackRegion.Start = StackAndTlsIoStart;
|
|
|
|
StackRegion.End = StackAndTlsIoEnd;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (TlsIoRegion.Size != 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
SortRegion(TlsIoRegion, AliasRegion);
|
|
|
|
SortRegion(TlsIoRegion, HeapRegion);
|
|
|
|
SortRegion(TlsIoRegion, StackRegion);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
TlsIoRegion.Start = StackAndTlsIoStart;
|
|
|
|
TlsIoRegion.End = StackAndTlsIoEnd;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00: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 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentHeapAddr = HeapRegionStart;
|
|
|
|
HeapCapacity = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
PhysicalMemoryUsage = 0;
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
this.MemRegion = MemRegion;
|
|
|
|
this.AslrDisabled = AslrDisabled;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return InitializeBlocks(AddrSpaceStart, AddrSpaceEnd);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private ulong GetRandomValue(ulong Min, ulong Max)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return (ulong)GetRandomValue((long)Min, (long)Max);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private long GetRandomValue(long Min, long Max)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (RandomNumberGenerator == null)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
RandomNumberGenerator = new MersenneTwister(0);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return RandomNumberGenerator.GenRandomNumber(Min, Max);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private static void SortRegion(Region Lhs, Region Rhs)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Lhs.AslrOffset < Rhs.AslrOffset)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Rhs.Start += Lhs.Size;
|
|
|
|
Rhs.End += Lhs.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Lhs.Start += Rhs.Size;
|
|
|
|
Lhs.End += Rhs.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KernelResult InitializeBlocks(ulong AddrSpaceStart, ulong AddrSpaceEnd)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
//First insertion will always need only a single block,
|
|
|
|
//because there's nothing else to split.
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(1))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AddrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(AddrSpaceStart, AddrSpacePagesCount, MemoryState.Unmapped);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult MapPages(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
KPageList PageList,
|
|
|
|
MemoryState State,
|
|
|
|
MemoryPermission Permission)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = PageList.GetPagesCount();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!ValidateRegionForState(Address, Size, State))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!IsUnmapped(Address, PagesCount * PageSize))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MapPages(Address, PageList, Permission);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, State, Permission);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult UnmapPages(ulong Address, KPageList PageList, MemoryState StateExpected)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = PageList.GetPagesCount();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AddrSpacePagesCount = (AddrSpaceEnd - AddrSpaceStart) / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (AddrSpaceStart > Address)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (AddrSpacePagesCount < PagesCount)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (EndAddr - 1 > AddrSpaceEnd - 1)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageList CurrentPageList = new KPageList();
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
AddVaRangeToPageList(CurrentPageList, Address, PagesCount);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!CurrentPageList.IsEqual(PageList))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Mask,
|
2018-12-05 00:52:39 +00:00
|
|
|
StateExpected,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
2018-11-28 22:18:09 +00:00
|
|
|
out _,
|
|
|
|
out _))
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuUnmap(Address, PagesCount);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, MemoryState.Unmapped);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult MapNormalMemory(long Address, long Size, MemoryPermission Permission)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult MapIoMemory(long Address, long Size, MemoryPermission Permission)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
//TODO.
|
|
|
|
return KernelResult.Success;
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult AllocateOrMapPa(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong NeededPagesCount,
|
|
|
|
int Alignment,
|
|
|
|
ulong SrcPa,
|
|
|
|
bool Map,
|
|
|
|
ulong RegionStart,
|
|
|
|
ulong RegionPagesCount,
|
|
|
|
MemoryState State,
|
|
|
|
MemoryPermission Permission,
|
|
|
|
out ulong Address)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = 0;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionSize = RegionPagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionEndAddr = RegionStart + RegionSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!ValidateRegionForState(RegionStart, RegionSize, State))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (RegionPagesCount <= NeededPagesCount)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong ReservedPagesCount = IsKernel ? 1UL : 4UL;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (AslrEnabled)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong TotalNeededSize = (ReservedPagesCount + NeededPagesCount) * PageSize;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RemainingPages = RegionPagesCount - NeededPagesCount;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AslrMaxOffset = ((RemainingPages + ReservedPagesCount) * PageSize) / (ulong)Alignment;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
for (int Attempt = 0; Attempt < 8; Attempt++)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = BitUtils.AlignDown(RegionStart + GetRandomValue(0, AslrMaxOffset) * (ulong)Alignment, Alignment);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + TotalNeededSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = FindBlock(Address).GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State != MemoryState.Unmapped)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrBaseAddr = Info.Address + ReservedPagesCount * PageSize;
|
|
|
|
ulong CurrEndAddr = Info.Address + Info.Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Address >= RegionStart &&
|
|
|
|
Address >= CurrBaseAddr &&
|
|
|
|
EndAddr - 1 <= RegionEndAddr - 1 &&
|
|
|
|
EndAddr - 1 <= CurrEndAddr - 1)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Address == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AslrPage = GetRandomValue(0, AslrMaxOffset);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = FindFirstFit(
|
|
|
|
RegionStart + AslrPage * PageSize,
|
|
|
|
RegionPagesCount - AslrPage,
|
|
|
|
NeededPagesCount,
|
|
|
|
Alignment,
|
2018-11-28 22:18:09 +00:00
|
|
|
0,
|
2018-12-05 00:52:39 +00:00
|
|
|
ReservedPagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Address == 0)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = FindFirstFit(
|
|
|
|
RegionStart,
|
|
|
|
RegionPagesCount,
|
|
|
|
NeededPagesCount,
|
|
|
|
Alignment,
|
2018-11-28 22:18:09 +00:00
|
|
|
0,
|
2018-12-05 00:52:39 +00:00
|
|
|
ReservedPagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Address == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MemoryOperation Operation = Map
|
2018-11-28 22:18:09 +00:00
|
|
|
? MemoryOperation.MapPa
|
|
|
|
: MemoryOperation.Allocate;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = DoMmuOperation(
|
|
|
|
Address,
|
|
|
|
NeededPagesCount,
|
|
|
|
SrcPa,
|
|
|
|
Map,
|
|
|
|
Permission,
|
|
|
|
Operation);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, NeededPagesCount, State, Permission);
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KernelResult MapNewProcessCode(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
ulong PagesCount,
|
|
|
|
MemoryState State,
|
|
|
|
MemoryPermission Permission)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!ValidateRegionForState(Address, Size, State))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!IsUnmapped(Address, Size))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = DoMmuOperation(
|
|
|
|
Address,
|
|
|
|
PagesCount,
|
2018-11-28 22:18:09 +00:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-05 00:52:39 +00:00
|
|
|
Permission,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryOperation.Allocate);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, State, Permission);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult MapProcessCodeMemory(ulong Dst, ulong Src, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
bool Success = CheckRange(
|
|
|
|
Src,
|
|
|
|
Size,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
|
|
|
out MemoryPermission Permission,
|
2018-11-28 22:18:09 +00:00
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Success &= IsUnmapped(Dst, Size);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Success)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageList PageList = new KPageList();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
AddVaRangeToPageList(PageList, Src, PagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuChangePermission(Src, PagesCount, MemoryPermission.None);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MapPages(Dst, PageList, MemoryPermission.None);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
MmuChangePermission(Src, PagesCount, Permission);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Src, PagesCount, State, MemoryPermission.None, MemoryAttribute.Borrowed);
|
|
|
|
InsertBlock(Dst, PagesCount, MemoryState.ModCodeStatic);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult UnmapProcessCodeMemory(ulong Dst, ulong Src, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
bool Success = CheckRange(
|
|
|
|
Src,
|
|
|
|
Size,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Success &= CheckRange(
|
|
|
|
Dst,
|
2018-11-28 22:18:09 +00:00
|
|
|
PageSize,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
|
|
|
MemoryState.UnmapProcessCodeMemoryAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
2018-11-28 22:18:09 +00:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Success &= CheckRange(
|
|
|
|
Dst,
|
|
|
|
Size,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Mask,
|
2018-12-05 00:52:39 +00:00
|
|
|
State,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuUnmap(Dst, PagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
//TODO: Missing some checks here.
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Dst, PagesCount, MemoryState.Unmapped);
|
|
|
|
InsertBlock(Src, PagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult SetHeapSize(ulong Size, out ulong Address)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = 0;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Size > HeapRegionEnd - HeapRegionStart)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.OutOfMemory;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrentHeapSize = GetHeapSize();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrentHeapSize <= Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
//Expand.
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong DiffSize = Size - CurrentHeapSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrentProcess.ResourceLimit != null && DiffSize != 0 &&
|
|
|
|
!CurrentProcess.ResourceLimit.Reserve(LimitableResource.Memory, DiffSize))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = DiffSize / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryRegionManager Region = GetMemoryRegionManager();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = Region.AllocatePages(PagesCount, AslrDisabled, out KPageList PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
void CleanUpForError()
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (PageList != null)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Region.FreePages(PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrentProcess.ResourceLimit != null && DiffSize != 0)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentProcess.ResourceLimit.Release(LimitableResource.Memory, DiffSize);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!IsUnmapped(CurrentHeapAddr, DiffSize))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
|
|
|
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = DoMmuOperation(
|
|
|
|
CurrentHeapAddr,
|
|
|
|
PagesCount,
|
|
|
|
PageList,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryOperation.MapVa);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(CurrentHeapAddr, PagesCount, MemoryState.Heap, MemoryPermission.ReadAndWrite);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Shrink.
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong FreeAddr = HeapRegionStart + Size;
|
|
|
|
ulong DiffSize = CurrentHeapSize - Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
FreeAddr,
|
|
|
|
DiffSize,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
2018-11-28 22:18:09 +00:00
|
|
|
out _))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = DiffSize / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuUnmap(FreeAddr, PagesCount);
|
2018-08-17 13:39:35 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentProcess.ResourceLimit?.Release(LimitableResource.Memory, BitUtils.AlignDown(DiffSize, PageSize));
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(FreeAddr, PagesCount, MemoryState.Unmapped);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentHeapAddr = HeapRegionStart + Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Address = HeapRegionStart;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public ulong GetTotalHeapSize()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return GetHeapSize() + PhysicalMemoryUsage;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private ulong GetHeapSize()
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return CurrentHeapAddr - HeapRegionStart;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult SetHeapCapacity(ulong Capacity)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
HeapCapacity = Capacity;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult SetMemoryAttribute(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
MemoryAttribute AttributeMask,
|
|
|
|
MemoryAttribute AttributeValue)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.AttributeChangeAllowed,
|
|
|
|
MemoryState.AttributeChangeAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.BorrowedAndIpcMapped,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.DeviceMappedAndUncached,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
|
|
|
out MemoryPermission Permission,
|
|
|
|
out MemoryAttribute Attribute))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Attribute &= ~AttributeMask;
|
|
|
|
Attribute |= AttributeMask & AttributeValue;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, State, Permission, Attribute);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KMemoryInfo QueryMemory(ulong Address)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Address >= AddrSpaceStart &&
|
|
|
|
Address < AddrSpaceEnd)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return FindBlock(Address).GetInfo();
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new KMemoryInfo(
|
|
|
|
AddrSpaceEnd,
|
2018-11-28 22:18:09 +00:00
|
|
|
~AddrSpaceEnd + 1,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.Reserved,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult Map(ulong Dst, ulong Src, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
bool Success;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Success = CheckRange(
|
|
|
|
Src,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState SrcState,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Success &= IsUnmapped(Dst, Size);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Success)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageList PageList = new KPageList();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
AddVaRangeToPageList(PageList, Src, PagesCount);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuChangePermission(Src, PagesCount, MemoryPermission.None);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MapPages(Dst, PageList, MemoryPermission.ReadAndWrite);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (MmuChangePermission(Src, PagesCount, MemoryPermission.ReadAndWrite) != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure reverting memory permission.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Src, PagesCount, SrcState, MemoryPermission.None, MemoryAttribute.Borrowed);
|
|
|
|
InsertBlock(Dst, PagesCount, MemoryState.Stack, MemoryPermission.ReadAndWrite);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult UnmapForKernel(ulong Address, ulong PagesCount, MemoryState StateExpected)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Mask,
|
2018-12-05 00:52:39 +00:00
|
|
|
StateExpected,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuUnmap(Address, PagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, MemoryState.Unmapped);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult Unmap(ulong Dst, ulong Src, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
bool Success;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Success = CheckRange(
|
|
|
|
Src,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryState.MapAllowed,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState SrcState,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Success &= CheckRange(
|
|
|
|
Dst,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.Mask,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryState.Stack,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryPermission DstPermission,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Success)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion * 2))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageList SrcPageList = new KPageList();
|
|
|
|
KPageList DstPageList = new KPageList();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
AddVaRangeToPageList(SrcPageList, Src, PagesCount);
|
|
|
|
AddVaRangeToPageList(DstPageList, Dst, PagesCount);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!DstPageList.IsEqual(SrcPageList))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemRange;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = MmuUnmap(Dst, PagesCount);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MmuChangePermission(Src, PagesCount, MemoryPermission.ReadAndWrite);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
MapPages(Dst, DstPageList, DstPermission);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Src, PagesCount, SrcState, MemoryPermission.ReadAndWrite);
|
|
|
|
InsertBlock(Dst, PagesCount, MemoryState.Unmapped);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult ReserveTransferMemory(ulong Address, ulong Size, MemoryPermission Permission)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryAttribute Attribute))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
//TODO: Missing checks.
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Attribute |= MemoryAttribute.Borrowed;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, State, Permission, Attribute);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult ResetTransferMemory(ulong Address, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryState.TransferMemoryAllowed | MemoryState.IsPoolAllocated,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.Borrowed,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState State,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _,
|
|
|
|
out _))
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, State, MemoryPermission.ReadAndWrite);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult SetProcessMemoryPermission(ulong Address, ulong Size, MemoryPermission Permission)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
if (CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
|
|
|
MemoryState.ProcessPermissionChangeAllowed,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
2018-12-05 00:52:39 +00:00
|
|
|
out MemoryState OldState,
|
|
|
|
out MemoryPermission OldPermission,
|
2018-08-15 18:59:51 +00:00
|
|
|
out _))
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
MemoryState NewState = OldState;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
//If writing into the code region is allowed, then we need
|
|
|
|
//to change it to mutable.
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((Permission & MemoryPermission.Write) != 0)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (OldState == MemoryState.CodeStatic)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewState = MemoryState.CodeMutable;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (OldState == MemoryState.ModCodeStatic)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewState = MemoryState.ModCodeMutable;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
throw new InvalidOperationException($"Memory state \"{OldState}\" not valid for this operation.");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (NewState != OldState || Permission != OldPermission)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MemoryOperation Operation = (Permission & MemoryPermission.Execute) != 0
|
2018-11-28 22:18:09 +00:00
|
|
|
? MemoryOperation.ChangePermsAndAttributes
|
|
|
|
: MemoryOperation.ChangePermRw;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = DoMmuOperation(Address, PagesCount, 0, false, Permission, Operation);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, NewState, Permission);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult MapPhysicalMemory(ulong Address, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong MappedSize = 0;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = FindBlockNode(Address);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State != MemoryState.Unmapped)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
MappedSize += GetSizeInRange(Info, Address, EndAddr);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Info.Address + Info.Size < EndAddr && Node != null);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (MappedSize == Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RemainingSize = Size - MappedSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RemainingPages = RemainingSize / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrentProcess.ResourceLimit != null &&
|
|
|
|
!CurrentProcess.ResourceLimit.Reserve(LimitableResource.Memory, RemainingSize))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.ResLimitExceeded;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryRegionManager Region = GetMemoryRegionManager();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = Region.AllocatePages(RemainingPages, AslrDisabled, out KPageList PageList);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
void CleanUpForError()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (PageList != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Region.FreePages(PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentProcess.ResourceLimit?.Release(LimitableResource.Memory, RemainingSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-17 13:39:35 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-17 13:39:35 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
CleanUpForError();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.OutOfResource;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MapPhysicalMemory(PageList, Address, EndAddr);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
PhysicalMemoryUsage += RemainingSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
InsertBlock(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
PagesCount,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryState.Heap,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryAttribute.None);
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult UnmapPhysicalMemory(ulong Address, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
//Scan, ensure that the region can be unmapped (all blocks are heap or
|
|
|
|
//already unmapped), fill pages list for freeing memory.
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong HeapMappedSize = 0;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageList PageList = new KPageList();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> BaseNode = FindBlockNode(Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = BaseNode;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.Heap)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.Attribute != MemoryAttribute.None)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidMemState;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BlockSize = GetSizeInRange(Info, Address, EndAddr);
|
|
|
|
ulong BlockAddress = GetAddrInRange(Info, Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
AddVaRangeToPageList(PageList, BlockAddress, BlockSize / PageSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
HeapMappedSize += BlockSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (Info.State != MemoryState.Unmapped)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.InvalidMemState;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Info.Address + Info.Size < EndAddr && Node != null);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (HeapMappedSize == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (!BlockAllocator.CanAllocate(MaxBlocksNeededForInsertion))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try to unmap all the heap mapped memory inside range.
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = KernelResult.Success;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = BaseNode;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.Heap)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BlockSize = GetSizeInRange(Info, Address, EndAddr);
|
|
|
|
ulong BlockAddress = GetAddrInRange(Info, Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BlockPagesCount = BlockSize / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MmuUnmap(BlockAddress, BlockPagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
//If we failed to unmap, we need to remap everything back again.
|
2018-12-05 00:52:39 +00:00
|
|
|
MapPhysicalMemory(PageList, Address, BlockAddress + BlockSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Info.Address + Info.Size < EndAddr && Node != null);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
GetMemoryRegionManager().FreePages(PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
PhysicalMemoryUsage -= HeapMappedSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KProcess CurrentProcess = System.Scheduler.GetCurrentProcess();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrentProcess.ResourceLimit?.Release(LimitableResource.Memory, HeapMappedSize);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = Size / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
InsertBlock(Address, PagesCount, MemoryState.Unmapped);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private void MapPhysicalMemory(KPageList PageList, ulong Address, ulong EndAddr)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = FindBlockNode(Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KPageNode> PageListNode = PageList.Nodes.First;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KPageNode PageNode = PageListNode.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong SrcPa = PageNode.Address;
|
|
|
|
ulong SrcPaPages = PageNode.PagesCount;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.Unmapped)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BlockSize = GetSizeInRange(Info, Address, EndAddr);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong DstVaPages = BlockSize / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong DstVa = GetAddrInRange(Info, Address);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (DstVaPages > 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (SrcPaPages == 0)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
PageListNode = PageListNode.Next;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
PageNode = PageListNode.Value;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
SrcPa = PageNode.Address;
|
|
|
|
SrcPaPages = PageNode.PagesCount;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = SrcPaPages;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (PagesCount > DstVaPages)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
PagesCount = DstVaPages;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
DoMmuOperation(
|
2018-12-05 00:52:39 +00:00
|
|
|
DstVa,
|
|
|
|
PagesCount,
|
|
|
|
SrcPa,
|
2018-11-28 22:18:09 +00:00
|
|
|
true,
|
|
|
|
MemoryPermission.ReadAndWrite,
|
|
|
|
MemoryOperation.MapPa);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
DstVa += PagesCount * PageSize;
|
|
|
|
SrcPa += PagesCount * PageSize;
|
|
|
|
SrcPaPages -= PagesCount;
|
|
|
|
DstVaPages -= PagesCount;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 13:39:35 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Info.Address + Info.Size < EndAddr && Node != null);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private static ulong GetSizeInRange(KMemoryInfo Info, ulong Start, ulong End)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Info.Size + Info.Address;
|
|
|
|
ulong Size = Info.Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.Address < Start)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Size -= Start - Info.Address;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (EndAddr > End)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Size -= EndAddr - End;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Size;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private static ulong GetAddrInRange(KMemoryInfo Info, ulong Start)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.Address < Start)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Start;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Info.Address;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private void AddVaRangeToPageList(KPageList PageList, ulong Start, ulong PagesCount)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address = Start;
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Address < Start + PagesCount * PageSize)
|
2018-10-09 23:01:49 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = ConvertVaToPa(Address, out ulong Pa);
|
2018-10-09 23:01:49 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure translating virtual address.");
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
PageList.AddRange(Pa, 1);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Address += PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-10-09 23:01:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool IsUnmapped(ulong Address, ulong Size)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
return CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
Size,
|
2018-08-15 18:59:51 +00:00
|
|
|
MemoryState.Mask,
|
|
|
|
MemoryState.Unmapped,
|
|
|
|
MemoryPermission.Mask,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryAttribute.Mask,
|
|
|
|
MemoryAttribute.None,
|
|
|
|
MemoryAttribute.IpcAndDeviceMapped,
|
|
|
|
out _,
|
|
|
|
out _,
|
|
|
|
out _);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
MemoryState StateMask,
|
|
|
|
MemoryState StateExpected,
|
|
|
|
MemoryPermission PermissionMask,
|
|
|
|
MemoryPermission PermissionExpected,
|
|
|
|
MemoryAttribute AttributeMask,
|
|
|
|
MemoryAttribute AttributeExpected,
|
|
|
|
MemoryAttribute AttributeIgnoreMask,
|
|
|
|
out MemoryState OutState,
|
|
|
|
out MemoryPermission OutPermission,
|
|
|
|
out MemoryAttribute OutAttribute)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = FindBlockNode(Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = Node.Value.GetInfo();
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MemoryState FirstState = Info.State;
|
|
|
|
MemoryPermission FirstPermission = Info.Permission;
|
|
|
|
MemoryAttribute FirstAttribute = Info.Attribute;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
do
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
//Check if the block state matches what we expect.
|
2018-12-05 00:52:39 +00:00
|
|
|
if ( FirstState != Info.State ||
|
|
|
|
FirstPermission != Info.Permission ||
|
|
|
|
(Info.Attribute & AttributeMask) != AttributeExpected ||
|
|
|
|
(FirstAttribute | AttributeIgnoreMask) != (Info.Attribute | AttributeIgnoreMask) ||
|
|
|
|
(FirstState & StateMask) != StateExpected ||
|
|
|
|
(FirstPermission & PermissionMask) != PermissionExpected)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if this is the last block on the range, if so return success.
|
2018-12-05 00:52:39 +00:00
|
|
|
if (EndAddr <= Info.Address + Info.Size - 1)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
OutState = FirstState;
|
|
|
|
OutPermission = FirstPermission;
|
|
|
|
OutAttribute = FirstAttribute & ~AttributeIgnoreMask;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Node != null);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
OutState = MemoryState.Unmapped;
|
|
|
|
OutPermission = MemoryPermission.None;
|
|
|
|
OutAttribute = MemoryAttribute.None;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private bool CheckRange(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
MemoryState StateMask,
|
|
|
|
MemoryState StateExpected,
|
|
|
|
MemoryPermission PermissionMask,
|
|
|
|
MemoryPermission PermissionExpected,
|
|
|
|
MemoryAttribute AttributeMask,
|
|
|
|
MemoryAttribute AttributeExpected)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = FindBlockNode(Address);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
//Check if the block state matches what we expect.
|
2018-12-05 00:52:39 +00:00
|
|
|
if ((Info.State & StateMask) != StateExpected ||
|
|
|
|
(Info.Permission & PermissionMask) != PermissionExpected ||
|
|
|
|
(Info.Attribute & AttributeMask) != AttributeExpected)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Check if this is the last block on the range, if so return success.
|
2018-12-05 00:52:39 +00:00
|
|
|
if (EndAddr <= Info.Address + Info.Size - 1)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Node != null);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private void InsertBlock(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BaseAddress,
|
|
|
|
ulong PagesCount,
|
|
|
|
MemoryState OldState,
|
|
|
|
MemoryPermission OldPermission,
|
|
|
|
MemoryAttribute OldAttribute,
|
|
|
|
MemoryState NewState,
|
|
|
|
MemoryPermission NewPermission,
|
|
|
|
MemoryAttribute NewAttribute)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
//Insert new block on the list only on areas where the state
|
|
|
|
//of the block matches the state specified on the Old* state
|
|
|
|
//arguments, otherwise leave it as is.
|
2018-12-05 00:52:39 +00:00
|
|
|
int OldCount = Blocks.Count;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
OldAttribute |= MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = PagesCount * PageSize + BaseAddress;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = Blocks.First;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Node != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> NewNode = Node;
|
|
|
|
LinkedListNode<KMemoryBlock> NextNode = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock CurrBlock = Node.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrBaseAddr = CurrBlock.BaseAddress;
|
|
|
|
ulong CurrEndAddr = CurrBlock.PagesCount * PageSize + CurrBaseAddr;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BaseAddress < CurrEndAddr && CurrBaseAddr < EndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
MemoryAttribute CurrBlockAttr = CurrBlock.Attribute | MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrBlock.State != OldState ||
|
|
|
|
CurrBlock.Permission != OldPermission ||
|
|
|
|
CurrBlockAttr != OldAttribute)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = NextNode;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrBaseAddr >= BaseAddress && CurrEndAddr <= EndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.State = NewState;
|
|
|
|
CurrBlock.Permission = NewPermission;
|
|
|
|
CurrBlock.Attribute &= ~MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
CurrBlock.Attribute |= NewAttribute;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (CurrBaseAddr >= BaseAddress)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.BaseAddress = EndAddr;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (CurrEndAddr - EndAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong NewPagesCount = (EndAddr - CurrBaseAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddBefore(Node, new KMemoryBlock(
|
|
|
|
CurrBaseAddr,
|
|
|
|
NewPagesCount,
|
|
|
|
NewState,
|
|
|
|
NewPermission,
|
|
|
|
NewAttribute));
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (CurrEndAddr <= EndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (BaseAddress - CurrBaseAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong NewPagesCount = (CurrEndAddr - BaseAddress) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddAfter(Node, new KMemoryBlock(
|
|
|
|
BaseAddress,
|
|
|
|
NewPagesCount,
|
|
|
|
NewState,
|
|
|
|
NewPermission,
|
|
|
|
NewAttribute));
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (BaseAddress - CurrBaseAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong NextPagesCount = (CurrEndAddr - EndAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddAfter(Node, new KMemoryBlock(
|
|
|
|
BaseAddress,
|
|
|
|
PagesCount,
|
|
|
|
NewState,
|
|
|
|
NewPermission,
|
|
|
|
NewAttribute));
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks.AddAfter(NewNode, new KMemoryBlock(
|
|
|
|
EndAddr,
|
|
|
|
NextPagesCount,
|
|
|
|
CurrBlock.State,
|
|
|
|
CurrBlock.Permission,
|
|
|
|
CurrBlock.Attribute));
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
NextNode = null;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MergeEqualStateNeighbours(NewNode);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = NextNode;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
BlockAllocator.Count += Blocks.Count - OldCount;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertBlock(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong BaseAddress,
|
|
|
|
ulong PagesCount,
|
|
|
|
MemoryState State,
|
|
|
|
MemoryPermission Permission = MemoryPermission.None,
|
|
|
|
MemoryAttribute Attribute = MemoryAttribute.None)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
|
|
|
//Inserts new block at the list, replacing and spliting
|
|
|
|
//existing blocks as needed.
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock Block = new KMemoryBlock(BaseAddress, PagesCount, State, Permission, Attribute);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
int OldCount = Blocks.Count;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = PagesCount * PageSize + BaseAddress;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> NewNode = null;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = Blocks.First;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Node != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock CurrBlock = Node.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> NextNode = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrBaseAddr = CurrBlock.BaseAddress;
|
|
|
|
ulong CurrEndAddr = CurrBlock.PagesCount * PageSize + CurrBaseAddr;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BaseAddress < CurrEndAddr && CurrBaseAddr < EndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BaseAddress >= CurrBaseAddr && EndAddr <= CurrEndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Block.Attribute |= CurrBlock.Attribute & MemoryAttribute.IpcAndDeviceMapped;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BaseAddress > CurrBaseAddr && EndAddr < CurrEndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (BaseAddress - CurrBaseAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong NextPagesCount = (CurrEndAddr - EndAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddAfter(Node, Block);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks.AddAfter(NewNode, new KMemoryBlock(
|
|
|
|
EndAddr,
|
|
|
|
NextPagesCount,
|
|
|
|
CurrBlock.State,
|
|
|
|
CurrBlock.Permission,
|
|
|
|
CurrBlock.Attribute));
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (BaseAddress <= CurrBaseAddr && EndAddr < CurrEndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.BaseAddress = EndAddr;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (CurrEndAddr - EndAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (NewNode == null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddBefore(Node, Block);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-05 00:52:39 +00:00
|
|
|
else if (BaseAddress > CurrBaseAddr && EndAddr >= CurrEndAddr)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrBlock.PagesCount = (BaseAddress - CurrBaseAddr) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (NewNode == null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddAfter(Node, Block);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (NewNode == null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddBefore(Node, Block);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks.Remove(Node);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = NextNode;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (NewNode == null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
NewNode = Blocks.AddFirst(Block);
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
MergeEqualStateNeighbours(NewNode);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
BlockAllocator.Count += Blocks.Count - OldCount;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private void MergeEqualStateNeighbours(LinkedListNode<KMemoryBlock> Node)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock Block = Node.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Block.PagesCount * PageSize + Block.BaseAddress;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Node.Previous != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock Previous = Node.Previous.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BlockStateEquals(Block, Previous))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks.Remove(Node.Previous);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Block.BaseAddress = Previous.BaseAddress;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Node.Next != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock Next = Node.Next.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (BlockStateEquals(Block, Next))
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Blocks.Remove(Node.Next);
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
EndAddr = Next.BaseAddress + Next.PagesCount * PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Block.PagesCount = (EndAddr - Block.BaseAddress) / PageSize;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private static bool BlockStateEquals(KMemoryBlock Lhs, KMemoryBlock Rhs)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Lhs.State == Rhs.State &&
|
|
|
|
Lhs.Permission == Rhs.Permission &&
|
|
|
|
Lhs.Attribute == Rhs.Attribute &&
|
|
|
|
Lhs.DeviceRefCount == Rhs.DeviceRefCount &&
|
|
|
|
Lhs.IpcRefCount == Rhs.IpcRefCount;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
private ulong FindFirstFit(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionStart,
|
|
|
|
ulong RegionPagesCount,
|
|
|
|
ulong NeededPagesCount,
|
|
|
|
int Alignment,
|
|
|
|
ulong ReservedStart,
|
|
|
|
ulong ReservedPagesCount)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong ReservedSize = ReservedPagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong TotalNeededSize = ReservedSize + NeededPagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionEndAddr = RegionStart + RegionPagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = FindBlockNode(RegionStart);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (RegionEndAddr >= Info.Address)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Info.State == MemoryState.Unmapped)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrBaseAddr = Info.Address + ReservedSize;
|
|
|
|
ulong CurrEndAddr = Info.Address + Info.Size - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address = BitUtils.AlignDown(CurrBaseAddr, Alignment) + ReservedStart;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (CurrBaseAddr > Address)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Address += (ulong)Alignment;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong AllocationEndAddr = Address + TotalNeededSize - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (AllocationEndAddr <= RegionEndAddr &&
|
|
|
|
AllocationEndAddr <= CurrEndAddr &&
|
|
|
|
Address < AllocationEndAddr)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Node == null)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Info = Node.Value.GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KMemoryBlock FindBlock(ulong Address)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return FindBlockNode(Address)?.Value;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private LinkedListNode<KMemoryBlock> FindBlockNode(ulong Address)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
LinkedListNode<KMemoryBlock> Node = Blocks.First;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
while (Node != null)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryBlock Block = Node.Value;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrEndAddr = Block.PagesCount * PageSize + Block.BaseAddress;
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Block.BaseAddress <= Address && CurrEndAddr - 1 >= Address)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Node;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Node = Node.Next;
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private bool ValidateRegionForState(ulong Address, ulong Size, MemoryState State)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong EndAddr = Address + Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionBaseAddr = GetBaseAddrForState(State);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong RegionEndAddr = RegionBaseAddr + GetSizeForState(State);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
bool InsideRegion()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return RegionBaseAddr <= Address &&
|
|
|
|
EndAddr > Address &&
|
|
|
|
EndAddr - 1 <= RegionEndAddr - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OutsideHeapRegion()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return EndAddr <= HeapRegionStart ||
|
|
|
|
Address >= HeapRegionEnd;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool OutsideMapRegion()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return EndAddr <= AliasRegionStart ||
|
|
|
|
Address >= AliasRegionEnd;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
switch (State)
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
throw new ArgumentException($"Invalid state value \"{State}\".");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private ulong GetBaseAddrForState(MemoryState State)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
switch (State)
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
throw new ArgumentException($"Invalid state value \"{State}\".");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private ulong GetSizeForState(MemoryState State)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
switch (State)
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
throw new ArgumentException($"Invalid state value \"{State}\".");
|
2018-11-28 22:18:09 +00: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-05 00:52:39 +00:00
|
|
|
private KernelResult MapPages(ulong Address, KPageList PageList, MemoryPermission Permission)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong CurrAddr = Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result = KernelResult.Success;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
foreach (KPageNode PageNode in PageList)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = DoMmuOperation(
|
|
|
|
CurrAddr,
|
|
|
|
PageNode.PagesCount,
|
|
|
|
PageNode.Address,
|
2018-11-28 22:18:09 +00:00
|
|
|
true,
|
2018-12-05 00:52:39 +00:00
|
|
|
Permission,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryOperation.MapPa);
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result != KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryInfo Info = FindBlock(CurrAddr).GetInfo();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong PagesCount = (Address - CurrAddr) / PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MmuUnmap(Address, PagesCount);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CurrAddr += PageNode.PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KernelResult MmuUnmap(ulong Address, ulong PagesCount)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
PagesCount,
|
2018-11-28 22:18:09 +00:00
|
|
|
0,
|
|
|
|
false,
|
|
|
|
MemoryPermission.None,
|
|
|
|
MemoryOperation.Unmap);
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KernelResult MmuChangePermission(ulong Address, ulong PagesCount, MemoryPermission Permission)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return DoMmuOperation(
|
2018-12-05 00:52:39 +00:00
|
|
|
Address,
|
|
|
|
PagesCount,
|
2018-11-28 22:18:09 +00:00
|
|
|
0,
|
|
|
|
false,
|
2018-12-05 00:52:39 +00:00
|
|
|
Permission,
|
2018-11-28 22:18:09 +00:00
|
|
|
MemoryOperation.ChangePermRw);
|
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong DstVa,
|
|
|
|
ulong PagesCount,
|
|
|
|
ulong SrcPa,
|
|
|
|
bool Map,
|
|
|
|
MemoryPermission Permission,
|
|
|
|
MemoryOperation Operation)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Map != (Operation == MemoryOperation.MapPa))
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
throw new ArgumentException(nameof(Map) + " value is invalid for this operation.");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
KernelResult Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
switch (Operation)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
case MemoryOperation.MapPa:
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CpuMemory.Map((long)DstVa, (long)(SrcPa - DramMemoryMap.DramBase), (long)Size);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = KernelResult.Success;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Allocate:
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
KMemoryRegionManager Region = GetMemoryRegionManager();
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = Region.AllocatePages(PagesCount, AslrDisabled, out KPageList PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Result == KernelResult.Success)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = MmuMapPages(DstVa, PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MemoryOperation.Unmap:
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CpuMemory.Unmap((long)DstVa, (long)Size);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Result = KernelResult.Success;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
case MemoryOperation.ChangePermRw: Result = KernelResult.Success; break;
|
|
|
|
case MemoryOperation.ChangePermsAndAttributes: Result = KernelResult.Success; break;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
default: throw new ArgumentException($"Invalid operation \"{Operation}\".");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return Result;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private KernelResult DoMmuOperation(
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Address,
|
|
|
|
ulong PagesCount,
|
|
|
|
KPageList PageList,
|
|
|
|
MemoryPermission Permission,
|
|
|
|
MemoryOperation Operation)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
if (Operation != MemoryOperation.MapVa)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
throw new ArgumentException($"Invalid memory operation \"{Operation}\" specified.");
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
return MmuMapPages(Address, PageList);
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private KMemoryRegionManager GetMemoryRegionManager()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return System.MemoryRegions[(int)MemRegion];
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
private KernelResult MmuMapPages(ulong Address, KPageList PageList)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
foreach (KPageNode PageNode in PageList)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
ulong Size = PageNode.PagesCount * PageSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
CpuMemory.Map((long)Address, (long)(PageNode.Address - DramMemoryMap.DramBase), (long)Size);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
Address += Size;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public KernelResult ConvertVaToPa(ulong Va, out ulong Pa)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
Pa = DramMemoryMap.DramBase + (ulong)CpuMemory.GetPhysicalAddress((long)Va);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
|
|
return KernelResult.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long GetMmUsedPages()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
lock (Blocks)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
|
|
|
return BitUtils.DivRoundUp(GetMmUsedSize(), PageSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private long GetMmUsedSize()
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Blocks.Count * KMemoryBlockSize;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool IsInvalidRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Address + Size - 1 > GetAddrSpaceBaseAddr() + GetAddrSpaceSize() - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool InsideAddrSpace(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return AddrSpaceStart <= Address && Address + Size - 1 <= AddrSpaceEnd - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool InsideAliasRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Address + Size > AliasRegionStart && AliasRegionEnd > Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool InsideHeapRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Address + Size > HeapRegionStart && HeapRegionEnd > Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool InsideStackRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return Address + Size > StackRegionStart && StackRegionEnd > Address;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool OutsideAliasRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return AliasRegionStart > Address || Address + Size - 1 > AliasRegionEnd - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool OutsideAddrSpace(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return AddrSpaceStart > Address || Address + Size - 1 > AddrSpaceEnd - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public bool OutsideStackRegion(ulong Address, ulong Size)
|
2018-11-28 22:18:09 +00:00
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
return StackRegionStart > Address || Address + Size - 1 > StackRegionEnd - 1;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|