2018-02-09 03:26:20 +00:00
|
|
|
using ChocolArm64.Exceptions;
|
2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
2018-08-15 18:59:51 +00:00
|
|
|
using System.Collections.Concurrent;
|
2018-02-04 23:08:20 +00:00
|
|
|
using System.Collections.Generic;
|
2018-06-04 19:11:11 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-03-10 02:12:57 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2018-05-11 23:10:27 +00:00
|
|
|
using System.Runtime.Intrinsics;
|
|
|
|
using System.Runtime.Intrinsics.X86;
|
2018-06-22 02:05:42 +00:00
|
|
|
using System.Threading;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
namespace ChocolArm64.Memory
|
|
|
|
{
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 18:53:23 +00:00
|
|
|
public unsafe class AMemory : IAMemory, IDisposable
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
private const int PTLvl0Bits = 13;
|
|
|
|
private const int PTLvl1Bits = 14;
|
|
|
|
private const int PTPageBits = 12;
|
|
|
|
|
|
|
|
private const int PTLvl0Size = 1 << PTLvl0Bits;
|
|
|
|
private const int PTLvl1Size = 1 << PTLvl1Bits;
|
|
|
|
public const int PageSize = 1 << PTPageBits;
|
|
|
|
|
|
|
|
private const int PTLvl0Mask = PTLvl0Size - 1;
|
|
|
|
private const int PTLvl1Mask = PTLvl1Size - 1;
|
|
|
|
public const int PageMask = PageSize - 1;
|
2018-02-06 15:15:08 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private const int PTLvl0Bit = PTPageBits + PTLvl1Bits;
|
|
|
|
private const int PTLvl1Bit = PTPageBits;
|
|
|
|
|
|
|
|
private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
private class ArmMonitor
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
public long Position;
|
|
|
|
public bool ExState;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public bool HasExclusiveAccess(long Position)
|
|
|
|
{
|
|
|
|
return this.Position == Position && ExState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
private Dictionary<int, ArmMonitor> Monitors;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private ConcurrentDictionary<long, IntPtr> ObservedPages;
|
|
|
|
|
2018-03-10 02:12:57 +00:00
|
|
|
public IntPtr Ram { get; private set; }
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
private byte* RamPtr;
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private byte*** PageTable;
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public AMemory(IntPtr Ram)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
Monitors = new Dictionary<int, ArmMonitor>();
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
ObservedPages = new ConcurrentDictionary<long, IntPtr>();
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
this.Ram = Ram;
|
2018-07-08 19:55:15 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
RamPtr = (byte*)Ram;
|
|
|
|
|
|
|
|
PageTable = (byte***)Marshal.AllocHGlobal(PTLvl0Size * IntPtr.Size);
|
|
|
|
|
|
|
|
for (int L0 = 0; L0 < PTLvl0Size; L0++)
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
PageTable[L0] = null;
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
public void RemoveMonitor(AThreadState State)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
ClearExclusive(State);
|
2018-02-06 15:15:08 +00:00
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
Monitors.Remove(State.ThreadId);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 19:28:07 +00:00
|
|
|
public void SetExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-02-14 02:43:08 +00:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-02-04 23:08:20 +00:00
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
foreach (ArmMonitor Mon in Monitors.Values)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
if (Mon.Position == Position && Mon.ExState)
|
|
|
|
{
|
|
|
|
Mon.ExState = false;
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
ThreadMon = new ArmMonitor();
|
|
|
|
|
|
|
|
Monitors.Add(ThreadState.ThreadId, ThreadMon);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-06-22 02:05:42 +00:00
|
|
|
|
|
|
|
ThreadMon.Position = Position;
|
|
|
|
ThreadMon.ExState = true;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 19:28:07 +00:00
|
|
|
public bool TestExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
//Note: Any call to this method also should be followed by a
|
|
|
|
//call to ClearExclusiveForStore if this method returns true.
|
2018-02-14 02:43:08 +00:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
Monitor.Enter(Monitors);
|
|
|
|
|
|
|
|
if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExState = ThreadMon.HasExclusiveAccess(Position);
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
if (!ExState)
|
|
|
|
{
|
|
|
|
Monitor.Exit(Monitors);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-06-22 02:05:42 +00:00
|
|
|
|
|
|
|
return ExState;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
public void ClearExclusiveForStore(AThreadState ThreadState)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
ThreadMon.ExState = false;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
2018-06-22 02:05:42 +00:00
|
|
|
|
|
|
|
Monitor.Exit(Monitors);
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
public void ClearExclusive(AThreadState ThreadState)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
|
|
|
{
|
|
|
|
ThreadMon.ExState = false;
|
|
|
|
}
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 02:05:42 +00:00
|
|
|
public void WriteInt32ToSharedAddr(long Position, int Value)
|
2018-02-14 02:43:08 +00:00
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
long MaskedPosition = Position & ~ErgMask;
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-22 02:05:42 +00:00
|
|
|
foreach (ArmMonitor Mon in Monitors.Values)
|
|
|
|
{
|
|
|
|
if (Mon.Position == MaskedPosition && Mon.ExState)
|
|
|
|
{
|
|
|
|
Mon.ExState = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteInt32(Position, Value);
|
2018-02-14 02:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 23:39:16 +00:00
|
|
|
public sbyte ReadSByte(long Position)
|
|
|
|
{
|
|
|
|
return (sbyte)ReadByte(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public short ReadInt16(long Position)
|
|
|
|
{
|
|
|
|
return (short)ReadUInt16(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ReadInt32(long Position)
|
|
|
|
{
|
|
|
|
return (int)ReadUInt32(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long ReadInt64(long Position)
|
|
|
|
{
|
|
|
|
return (long)ReadUInt64(Position);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public byte ReadByte(long Position)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return *((byte*)Translate(Position));
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ushort ReadUInt16(long Position)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return *((ushort*)Translate(Position));
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public uint ReadUInt32(long Position)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return *((uint*)Translate(Position));
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ulong ReadUInt64(long Position)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return *((ulong*)Translate(Position));
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 23:10:27 +00:00
|
|
|
public Vector128<float> ReadVector8(long Position)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(Position)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public Vector128<float> ReadVector16(long Position)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(Position), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 02:49:53 +00:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-08-15 18:59:51 +00:00
|
|
|
public Vector128<float> ReadVector32(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return Sse.LoadScalarVector128((float*)Translate(Position));
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 02:49:53 +00:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-08-15 18:59:51 +00:00
|
|
|
public Vector128<float> ReadVector64(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)Translate(Position)));
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:11:11 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-08-15 18:59:51 +00:00
|
|
|
public Vector128<float> ReadVector128(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse.IsSupported)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return Sse.LoadVector128((float*)Translate(Position));
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:05:41 +00:00
|
|
|
public byte[] ReadBytes(long Position, long Size)
|
|
|
|
{
|
|
|
|
if ((uint)Size > int.MaxValue)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(Size));
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
EnsureRangeIsValid(Position, Size);
|
2018-06-09 16:05:41 +00:00
|
|
|
|
|
|
|
byte[] Data = new byte[Size];
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
Marshal.Copy((IntPtr)Translate(Position), Data, 0, (int)Size);
|
2018-06-09 16:05:41 +00:00
|
|
|
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
2018-03-10 23:39:16 +00:00
|
|
|
public void WriteSByte(long Position, sbyte Value)
|
|
|
|
{
|
|
|
|
WriteByte(Position, (byte)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt16(long Position, short Value)
|
|
|
|
{
|
|
|
|
WriteUInt16(Position, (ushort)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt32(long Position, int Value)
|
|
|
|
{
|
|
|
|
WriteUInt32(Position, (uint)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt64(long Position, long Value)
|
|
|
|
{
|
|
|
|
WriteUInt64(Position, (ulong)Value);
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
|
|
|
|
public void WriteByte(long Position, byte Value)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
*((byte*)TranslateWrite(Position)) = Value;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt16(long Position, ushort Value)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
*((ushort*)TranslateWrite(Position)) = Value;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt32(long Position, uint Value)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
*((uint*)TranslateWrite(Position)) = Value;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt64(long Position, ulong Value)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
*((ulong*)TranslateWrite(Position)) = Value;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public void WriteVector8(long Position, Vector128<float> Value)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse41.IsSupported)
|
|
|
|
{
|
|
|
|
WriteByte(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
|
|
|
|
}
|
|
|
|
else if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
WriteByte(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public void WriteVector16(long Position, Vector128<float> Value)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
WriteUInt16(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public void WriteVector32(long Position, Vector128<float> Value)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
Sse.StoreScalar((float*)TranslateWrite(Position), Value);
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public void WriteVector64(long Position, Vector128<float> Value)
|
2018-02-17 21:06:11 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
Sse2.StoreScalar((double*)TranslateWrite(Position), Sse.StaticCast<float, double>(Value));
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 21:06:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 23:10:27 +00:00
|
|
|
public void WriteVector128(long Position, Vector128<float> Value)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-05-11 23:10:27 +00:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
Sse.Store((float*)TranslateWrite(Position), Value);
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public void WriteBytes(long Position, byte[] Data)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
EnsureRangeIsValid(Position, (uint)Data.Length);
|
2018-03-10 23:39:16 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
Marshal.Copy(Data, 0, (IntPtr)TranslateWrite(Position), Data.Length);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public void Map(long VA, long PA, long Size)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
SetPTEntries(VA, RamPtr + PA, Size);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public void Unmap(long Position, long Size)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
SetPTEntries(Position, null, Size);
|
2018-03-10 23:39:16 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
StopObservingRegion(Position, Size);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public bool IsMapped(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
if (!(IsValidPosition(Position)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
|
|
|
|
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
|
|
|
|
|
|
|
|
if (PageTable[L0] == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PageTable[L0][L1] != null || ObservedPages.ContainsKey(Position >> PTPageBits);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public long GetPhysicalAddress(long VirtualAddress)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
byte* Ptr = Translate(VirtualAddress);
|
|
|
|
|
|
|
|
return (long)(Ptr - RamPtr);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
internal byte* Translate(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
|
|
|
|
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
|
|
|
|
|
|
|
|
long Old = Position;
|
|
|
|
|
|
|
|
byte** Lvl1 = PageTable[L0];
|
|
|
|
|
|
|
|
if ((Position >> (PTLvl0Bit + PTLvl0Bits)) != 0)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
goto Unmapped;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
if (Lvl1 == null)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
goto Unmapped;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
Position &= PageMask;
|
|
|
|
|
|
|
|
byte* Ptr = Lvl1[L1];
|
|
|
|
|
|
|
|
if (Ptr == null)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
goto Unmapped;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
return Ptr + Position;
|
|
|
|
|
|
|
|
Unmapped:
|
|
|
|
return HandleNullPte(Old);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private byte* HandleNullPte(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
long Key = Position >> PTPageBits;
|
|
|
|
|
|
|
|
if (ObservedPages.TryGetValue(Key, out IntPtr Ptr))
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return (byte*)Ptr + (Position & PageMask);
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal byte* TranslateWrite(long Position)
|
|
|
|
{
|
|
|
|
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
|
|
|
|
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
|
|
|
|
|
|
|
|
long Old = Position;
|
|
|
|
|
|
|
|
byte** Lvl1 = PageTable[L0];
|
|
|
|
|
|
|
|
if ((Position >> (PTLvl0Bit + PTLvl0Bits)) != 0)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
goto Unmapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Lvl1 == null)
|
|
|
|
{
|
|
|
|
goto Unmapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
Position &= PageMask;
|
|
|
|
|
|
|
|
byte* Ptr = Lvl1[L1];
|
|
|
|
|
|
|
|
if (Ptr == null)
|
|
|
|
{
|
|
|
|
goto Unmapped;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
return Ptr + Position;
|
|
|
|
|
|
|
|
Unmapped:
|
|
|
|
return HandleNullPteWrite(Old);
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private byte* HandleNullPteWrite(long Position)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
long Key = Position >> PTPageBits;
|
|
|
|
|
|
|
|
if (ObservedPages.TryGetValue(Key, out IntPtr Ptr))
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
SetPTEntry(Position, (byte*)Ptr);
|
|
|
|
|
|
|
|
return (byte*)Ptr + (Position & PageMask);
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetPTEntries(long VA, byte* Ptr, long Size)
|
|
|
|
{
|
|
|
|
long EndPosition = (VA + Size + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
while ((ulong)VA < (ulong)EndPosition)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
SetPTEntry(VA, Ptr);
|
|
|
|
|
|
|
|
VA += PageSize;
|
|
|
|
|
|
|
|
if (Ptr != null)
|
|
|
|
{
|
|
|
|
Ptr += PageSize;
|
|
|
|
}
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
private void SetPTEntry(long Position, byte* Ptr)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
if (!IsValidPosition(Position))
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(Position));
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
|
|
|
|
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
|
|
|
|
|
|
|
|
if (PageTable[L0] == null)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
byte** Lvl1 = (byte**)Marshal.AllocHGlobal(PTLvl1Size * IntPtr.Size);
|
|
|
|
|
|
|
|
for (int ZL1 = 0; ZL1 < PTLvl1Size; ZL1++)
|
|
|
|
{
|
|
|
|
Lvl1[ZL1] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.MemoryBarrier();
|
|
|
|
|
|
|
|
PageTable[L0] = Lvl1;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
PageTable[L0][L1] = Ptr;
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public (bool[], int) IsRegionModified(long Position, long Size)
|
2018-03-10 23:39:16 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
long EndPosition = (Position + Size + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
Position &= ~PageMask;
|
|
|
|
|
|
|
|
Size = EndPosition - Position;
|
|
|
|
|
|
|
|
bool[] Modified = new bool[Size >> PTPageBits];
|
|
|
|
|
|
|
|
int Count = 0;
|
|
|
|
|
|
|
|
lock (ObservedPages)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
for (int Page = 0; Page < Modified.Length; Page++)
|
|
|
|
{
|
|
|
|
byte* Ptr = Translate(Position);
|
|
|
|
|
|
|
|
if (ObservedPages.TryAdd(Position >> PTPageBits, (IntPtr)Ptr))
|
|
|
|
{
|
|
|
|
Modified[Page] = true;
|
|
|
|
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
|
|
|
|
long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
|
|
|
|
|
|
|
|
byte** Lvl1 = PageTable[L0];
|
|
|
|
|
|
|
|
if (Lvl1 != null)
|
|
|
|
{
|
|
|
|
if (Modified[Page] = Lvl1[L1] != null)
|
|
|
|
{
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPTEntry(Position, null);
|
|
|
|
|
|
|
|
Position += PageSize;
|
|
|
|
}
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
return (Modified, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StopObservingRegion(long Position, long Size)
|
|
|
|
{
|
|
|
|
long EndPosition = (Position + Size + PageMask) & ~PageMask;
|
|
|
|
|
|
|
|
while (Position < EndPosition)
|
2018-05-11 23:10:27 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
lock (ObservedPages)
|
|
|
|
{
|
|
|
|
if (ObservedPages.TryRemove(Position >> PTPageBits, out IntPtr Ptr))
|
|
|
|
{
|
|
|
|
SetPTEntry(Position, (byte*)Ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Position += PageSize;
|
2018-05-11 23:10:27 +00:00
|
|
|
}
|
2018-03-10 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public IntPtr GetHostAddress(long Position, long Size)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
EnsureRangeIsValid(Position, Size);
|
2018-03-10 02:12:57 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
return (IntPtr)Translate(Position);
|
2018-03-10 02:12:57 +00:00
|
|
|
}
|
2018-02-09 03:26:20 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
internal void EnsureRangeIsValid(long Position, long Size)
|
2018-06-09 00:15:02 +00:00
|
|
|
{
|
2018-06-09 02:54:50 +00:00
|
|
|
long EndPos = Position + Size;
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
Position &= ~PageMask;
|
|
|
|
|
|
|
|
long ExpectedPA = GetPhysicalAddress(Position);
|
2018-06-09 16:05:41 +00:00
|
|
|
|
2018-06-09 02:54:50 +00:00
|
|
|
while ((ulong)Position < (ulong)EndPos)
|
2018-06-09 00:15:02 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
long PA = GetPhysicalAddress(Position);
|
|
|
|
|
|
|
|
if (PA != ExpectedPA)
|
|
|
|
{
|
|
|
|
throw new VmmAccessException(Position, Size);
|
|
|
|
}
|
2018-06-09 02:54:50 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
Position += PageSize;
|
|
|
|
ExpectedPA += PageSize;
|
2018-06-09 00:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
public bool IsValidPosition(long Position)
|
2018-06-09 16:05:41 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return Position >> (PTLvl0Bits + PTLvl1Bits + PTPageBits) == 0;
|
2018-06-09 16:05:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 02:12:57 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
if (PageTable == null)
|
2018-02-09 03:26:20 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int L0 = 0; L0 < PTLvl0Size; L0++)
|
|
|
|
{
|
|
|
|
if (PageTable[L0] != null)
|
2018-06-09 00:15:56 +00:00
|
|
|
{
|
2018-08-15 18:59:51 +00:00
|
|
|
Marshal.FreeHGlobal((IntPtr)PageTable[L0]);
|
2018-06-09 00:15:56 +00:00
|
|
|
}
|
2018-03-10 02:12:57 +00:00
|
|
|
|
2018-08-15 18:59:51 +00:00
|
|
|
PageTable[L0] = null;
|
2018-02-09 03:26:20 +00:00
|
|
|
}
|
2018-08-15 18:59:51 +00:00
|
|
|
|
|
|
|
Marshal.FreeHGlobal((IntPtr)PageTable);
|
|
|
|
|
|
|
|
PageTable = null;
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|