2018-02-08 22:26:20 -05:00
|
|
|
using ChocolArm64.Exceptions;
|
2018-02-04 18:08:20 -05:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-06-04 15:11:11 -04:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-03-09 21:12:57 -05:00
|
|
|
using System.Runtime.InteropServices;
|
2018-05-11 19:10:27 -04:00
|
|
|
using System.Runtime.Intrinsics;
|
|
|
|
using System.Runtime.Intrinsics.X86;
|
2018-06-21 22:05:42 -04:00
|
|
|
using System.Threading;
|
2018-02-04 18:08:20 -05: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 14:53:23 -04:00
|
|
|
public unsafe class AMemory : IAMemory, IDisposable
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
|
2018-02-06 10:15:08 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
public AMemoryMgr Manager { get; private set; }
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
private class ArmMonitor
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
public long Position;
|
|
|
|
public bool ExState;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
public bool HasExclusiveAccess(long Position)
|
|
|
|
{
|
|
|
|
return this.Position == Position && ExState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
private Dictionary<int, ArmMonitor> Monitors;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-09 21:12:57 -05:00
|
|
|
public IntPtr Ram { get; private set; }
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
private byte* RamPtr;
|
|
|
|
|
2018-07-08 15:55:15 -04:00
|
|
|
private int HostPageSize;
|
|
|
|
|
2018-03-09 21:12:57 -05:00
|
|
|
public AMemory()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-27 18:45:07 -05:00
|
|
|
Manager = new AMemoryMgr();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
Monitors = new Dictionary<int, ArmMonitor>();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-07-08 15:55:15 -04:00
|
|
|
IntPtr Size = (IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize;
|
|
|
|
|
2018-06-08 20:15:56 -04:00
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
Ram = AMemoryWin32.Allocate(Size);
|
|
|
|
|
|
|
|
HostPageSize = AMemoryWin32.GetPageSize(Ram, Size);
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
Ram = Marshal.AllocHGlobal(Size);
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
2018-03-09 21:12:57 -05:00
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
RamPtr = (byte*)Ram;
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
public void RemoveMonitor(AThreadState State)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
ClearExclusive(State);
|
2018-02-06 10:15:08 -05:00
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
Monitors.Remove(State.ThreadId);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
public void SetExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-13 21:43:08 -05:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-02-04 18:08:20 -05:00
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
foreach (ArmMonitor Mon in Monitors.Values)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
if (Mon.Position == Position && Mon.ExState)
|
|
|
|
{
|
|
|
|
Mon.ExState = false;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
ThreadMon = new ArmMonitor();
|
|
|
|
|
|
|
|
Monitors.Add(ThreadState.ThreadId, ThreadMon);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-06-21 22:05:42 -04:00
|
|
|
|
|
|
|
ThreadMon.Position = Position;
|
|
|
|
ThreadMon.ExState = true;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:28:07 -05:00
|
|
|
public bool TestExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
//Note: Any call to this method also should be followed by a
|
|
|
|
//call to ClearExclusiveForStore if this method returns true.
|
2018-02-13 21:43:08 -05:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
Monitor.Enter(Monitors);
|
|
|
|
|
|
|
|
if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExState = ThreadMon.HasExclusiveAccess(Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
if (!ExState)
|
|
|
|
{
|
|
|
|
Monitor.Exit(Monitors);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-06-21 22:05:42 -04:00
|
|
|
|
|
|
|
return ExState;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
public void ClearExclusiveForStore(AThreadState ThreadState)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
ThreadMon.ExState = false;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-06-21 22:05:42 -04:00
|
|
|
|
|
|
|
Monitor.Exit(Monitors);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
public void ClearExclusive(AThreadState ThreadState)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
|
|
|
|
{
|
|
|
|
ThreadMon.ExState = false;
|
|
|
|
}
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:05:42 -04:00
|
|
|
public void WriteInt32ToSharedAddr(long Position, int Value)
|
2018-02-13 21:43:08 -05:00
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
long MaskedPosition = Position & ~ErgMask;
|
2018-02-13 21:43:08 -05:00
|
|
|
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-06-21 22:05:42 -04:00
|
|
|
foreach (ArmMonitor Mon in Monitors.Values)
|
|
|
|
{
|
|
|
|
if (Mon.Position == MaskedPosition && Mon.ExState)
|
|
|
|
{
|
|
|
|
Mon.ExState = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteInt32(Position, Value);
|
2018-02-13 21:43:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 15:55:15 -04:00
|
|
|
public int GetHostPageSize()
|
2018-06-08 20:15:56 -04:00
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
return HostPageSize;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
2018-07-08 15:55:15 -04:00
|
|
|
public bool[] IsRegionModified(long Position, long Size)
|
2018-06-08 20:15:56 -04:00
|
|
|
{
|
|
|
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
return null;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
long EndPos = Position + Size;
|
|
|
|
|
|
|
|
if ((ulong)EndPos < (ulong)Position)
|
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
return null;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ulong)EndPos > AMemoryMgr.RamSize)
|
|
|
|
{
|
2018-07-08 15:55:15 -04:00
|
|
|
return null;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IntPtr MemAddress = new IntPtr(RamPtr + Position);
|
|
|
|
IntPtr MemSize = new IntPtr(Size);
|
|
|
|
|
2018-07-08 15:55:15 -04:00
|
|
|
int HostPageMask = HostPageSize - 1;
|
|
|
|
|
|
|
|
Position &= ~HostPageMask;
|
|
|
|
|
|
|
|
Size = EndPos - Position;
|
|
|
|
|
|
|
|
IntPtr[] Addresses = new IntPtr[(Size + HostPageMask) / HostPageSize];
|
|
|
|
|
|
|
|
AMemoryWin32.IsRegionModified(MemAddress, MemSize, Addresses, out int Count);
|
|
|
|
|
|
|
|
bool[] Modified = new bool[Addresses.Length];
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Count; Index++)
|
|
|
|
{
|
|
|
|
long VA = Addresses[Index].ToInt64() - Ram.ToInt64();
|
|
|
|
|
|
|
|
Modified[(VA - Position) / HostPageSize] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
2018-06-08 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:39:16 -05: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 18:08:20 -05:00
|
|
|
|
|
|
|
public byte ReadByte(long Position)
|
|
|
|
{
|
2018-02-08 22:26:20 -05:00
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
return ReadByteUnchecked(Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public ushort ReadUInt16(long Position)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 1, AMemoryPerm.Read);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
return ReadUInt16Unchecked(Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public uint ReadUInt32(long Position)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
return ReadUInt32Unchecked(Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public ulong ReadUInt64(long Position)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
return ReadUInt64Unchecked(Position);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector8(long Position)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04: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 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector16(long Position)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(Position), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector32(long Position)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector64(long Position)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector128(long Position)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
EnsureAccessIsValid(Position + 15, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
else
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
public sbyte ReadSByteUnchecked(long Position)
|
|
|
|
{
|
|
|
|
return (sbyte)ReadByteUnchecked(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public short ReadInt16Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return (short)ReadUInt16Unchecked(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ReadInt32Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return (int)ReadUInt32Unchecked(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long ReadInt64Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return (long)ReadUInt64Unchecked(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte ReadByteUnchecked(long Position)
|
|
|
|
{
|
|
|
|
return *((byte*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ushort ReadUInt16Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return *((ushort*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
|
|
|
|
public uint ReadUInt32Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return *((uint*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
|
|
|
|
public ulong ReadUInt64Unchecked(long Position)
|
|
|
|
{
|
|
|
|
return *((ulong*)(RamPtr + (uint)Position));
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector8Unchecked(long Position)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04: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-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:11:11 -04:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector16Unchecked(long Position)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16Unchecked(Position), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-08 22:49:53 -04:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector32Unchecked(long Position)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
2018-06-08 22:54:50 -04:00
|
|
|
return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
|
2018-05-11 19:10:27 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-08 22:49:53 -04:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector64Unchecked(long Position)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:11:11 -04:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public Vector128<float> ReadVector128Unchecked(long Position)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse.IsSupported)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-06-08 22:54:50 -04:00
|
|
|
return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
|
2018-05-11 19:10:27 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
public byte[] ReadBytes(long Position, long Size)
|
|
|
|
{
|
|
|
|
if ((uint)Size > int.MaxValue)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(Size));
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
byte[] Data = new byte[Size];
|
|
|
|
|
|
|
|
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
|
|
|
|
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
2018-03-10 18:39:16 -05: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 18:08:20 -05:00
|
|
|
|
|
|
|
public void WriteByte(long Position, byte Value)
|
|
|
|
{
|
2018-02-08 22:26:20 -05:00
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
WriteByteUnchecked(Position, Value);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt16(long Position, ushort Value)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 1, AMemoryPerm.Write);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
WriteUInt16Unchecked(Position, Value);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt32(long Position, uint Value)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
WriteUInt32Unchecked(Position, Value);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt64(long Position, ulong Value)
|
|
|
|
{
|
2018-03-09 21:12:57 -05:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
WriteUInt64Unchecked(Position, Value);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector8(long Position, Vector128<float> Value)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04: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 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector16(long Position, Vector128<float> Value)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
WriteUInt16(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector32(long Position, Vector128<float> Value)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector64(long Position, Vector128<float> Value)
|
2018-02-17 16:06:11 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-17 16:06:11 -05:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector128(long Position, Vector128<float> Value)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
EnsureAccessIsValid(Position + 15, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
Sse.Store((float*)(RamPtr + (uint)Position), Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-10 18:39:16 -05:00
|
|
|
public void WriteSByteUnchecked(long Position, sbyte Value)
|
|
|
|
{
|
|
|
|
WriteByteUnchecked(Position, (byte)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt16Unchecked(long Position, short Value)
|
|
|
|
{
|
|
|
|
WriteUInt16Unchecked(Position, (ushort)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt32Unchecked(long Position, int Value)
|
|
|
|
{
|
|
|
|
WriteUInt32Unchecked(Position, (uint)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteInt64Unchecked(long Position, long Value)
|
|
|
|
{
|
|
|
|
WriteUInt64Unchecked(Position, (ulong)Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteByteUnchecked(long Position, byte Value)
|
|
|
|
{
|
|
|
|
*((byte*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt16Unchecked(long Position, ushort Value)
|
|
|
|
{
|
|
|
|
*((ushort*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt32Unchecked(long Position, uint Value)
|
|
|
|
{
|
|
|
|
*((uint*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void WriteUInt64Unchecked(long Position, ulong Value)
|
|
|
|
{
|
|
|
|
*((ulong*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:11:11 -04:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector8Unchecked(long Position, Vector128<float> Value)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse41.IsSupported)
|
|
|
|
{
|
|
|
|
WriteByteUnchecked(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
|
|
|
|
}
|
|
|
|
else if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
WriteByteUnchecked(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:11:11 -04:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector16Unchecked(long Position, Vector128<float> Value)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
WriteUInt16Unchecked(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-08 22:49:53 -04:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector32Unchecked(long Position, Vector128<float> Value)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-08 22:49:53 -04:00
|
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector64Unchecked(long Position, Vector128<float> Value)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse2.IsSupported)
|
|
|
|
{
|
|
|
|
Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:11:11 -04:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-05-11 19:10:27 -04:00
|
|
|
public void WriteVector128Unchecked(long Position, Vector128<float> Value)
|
2018-03-10 18:39:16 -05:00
|
|
|
{
|
2018-05-11 19:10:27 -04:00
|
|
|
if (Sse.IsSupported)
|
|
|
|
{
|
|
|
|
Sse.Store((float*)(RamPtr + (uint)Position), Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException();
|
|
|
|
}
|
2018-03-10 18:39:16 -05:00
|
|
|
}
|
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
public void WriteBytes(long Position, byte[] Data)
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-09 12:05:41 -04:00
|
|
|
EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
|
2018-03-09 21:12:57 -05:00
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
|
2018-03-09 21:12:57 -05:00
|
|
|
}
|
2018-02-08 22:26:20 -05:00
|
|
|
|
2018-06-08 20:15:02 -04:00
|
|
|
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
|
|
|
|
{
|
2018-06-08 22:54:50 -04:00
|
|
|
long EndPos = Position + Size;
|
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
Position &= ~AMemoryMgr.PageMask;
|
|
|
|
|
2018-06-08 22:54:50 -04:00
|
|
|
while ((ulong)Position < (ulong)EndPos)
|
2018-06-08 20:15:02 -04:00
|
|
|
{
|
|
|
|
EnsureAccessIsValid(Position, Perm);
|
2018-06-08 22:54:50 -04:00
|
|
|
|
2018-06-08 20:15:02 -04:00
|
|
|
Position += AMemoryMgr.PageSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
|
|
|
|
{
|
|
|
|
if (!Manager.IsMapped(Position))
|
|
|
|
{
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Manager.HasPermission(Position, Perm))
|
|
|
|
{
|
|
|
|
throw new VmmAccessViolationException(Position, Perm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:12:57 -05:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (Ram != IntPtr.Zero)
|
2018-02-08 22:26:20 -05:00
|
|
|
{
|
2018-06-08 20:15:56 -04:00
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
|
|
|
AMemoryWin32.Free(Ram);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Marshal.FreeHGlobal(Ram);
|
|
|
|
}
|
2018-03-09 21:12:57 -05:00
|
|
|
|
|
|
|
Ram = IntPtr.Zero;
|
2018-02-08 22:26:20 -05:00
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|