| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- using ChocolArm64.Exceptions;
- using ChocolArm64.State;
- using System;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Runtime.Intrinsics;
- using System.Runtime.Intrinsics.X86;
- using System.Threading;
- namespace ChocolArm64.Memory
- {
- public unsafe class AMemory : IAMemory, IDisposable
- {
- private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
- public AMemoryMgr Manager { get; private set; }
- private class ArmMonitor
- {
- public long Position;
- public bool ExState;
- public bool HasExclusiveAccess(long Position)
- {
- return this.Position == Position && ExState;
- }
- }
- private Dictionary<int, ArmMonitor> Monitors;
- public IntPtr Ram { get; private set; }
- private byte* RamPtr;
- public AMemory()
- {
- Manager = new AMemoryMgr();
- Monitors = new Dictionary<int, ArmMonitor>();
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- Ram = AMemoryWin32.Allocate((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize);
- }
- else
- {
- Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize);
- }
- RamPtr = (byte*)Ram;
- }
- public void RemoveMonitor(AThreadState State)
- {
- lock (Monitors)
- {
- ClearExclusive(State);
- Monitors.Remove(State.ThreadId);
- }
- }
- public void SetExclusive(AThreadState ThreadState, long Position)
- {
- Position &= ~ErgMask;
- lock (Monitors)
- {
- foreach (ArmMonitor Mon in Monitors.Values)
- {
- if (Mon.Position == Position && Mon.ExState)
- {
- Mon.ExState = false;
- }
- }
- if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
- {
- ThreadMon = new ArmMonitor();
- Monitors.Add(ThreadState.ThreadId, ThreadMon);
- }
- ThreadMon.Position = Position;
- ThreadMon.ExState = true;
- }
- }
- public bool TestExclusive(AThreadState ThreadState, long Position)
- {
- //Note: Any call to this method also should be followed by a
- //call to ClearExclusiveForStore if this method returns true.
- Position &= ~ErgMask;
- Monitor.Enter(Monitors);
- if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
- {
- return false;
- }
- bool ExState = ThreadMon.HasExclusiveAccess(Position);
- if (!ExState)
- {
- Monitor.Exit(Monitors);
- }
- return ExState;
- }
- public void ClearExclusiveForStore(AThreadState ThreadState)
- {
- if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
- {
- ThreadMon.ExState = false;
- }
- Monitor.Exit(Monitors);
- }
- public void ClearExclusive(AThreadState ThreadState)
- {
- lock (Monitors)
- {
- if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
- {
- ThreadMon.ExState = false;
- }
- }
- }
- public void WriteInt32ToSharedAddr(long Position, int Value)
- {
- long MaskedPosition = Position & ~ErgMask;
- lock (Monitors)
- {
- foreach (ArmMonitor Mon in Monitors.Values)
- {
- if (Mon.Position == MaskedPosition && Mon.ExState)
- {
- Mon.ExState = false;
- }
- }
- WriteInt32(Position, Value);
- }
- }
- public long GetHostPageSize()
- {
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- return AMemoryMgr.PageSize;
- }
- IntPtr MemAddress = new IntPtr(RamPtr);
- IntPtr MemSize = new IntPtr(AMemoryMgr.RamSize);
- long PageSize = AMemoryWin32.IsRegionModified(MemAddress, MemSize, Reset: false);
- if (PageSize < 1)
- {
- throw new InvalidOperationException();
- }
- return PageSize;
- }
- public bool IsRegionModified(long Position, long Size)
- {
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- return true;
- }
- long EndPos = Position + Size;
- if ((ulong)EndPos < (ulong)Position)
- {
- return false;
- }
- if ((ulong)EndPos > AMemoryMgr.RamSize)
- {
- return false;
- }
- IntPtr MemAddress = new IntPtr(RamPtr + Position);
- IntPtr MemSize = new IntPtr(Size);
- return AMemoryWin32.IsRegionModified(MemAddress, MemSize, Reset: true) != 0;
- }
- 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);
- }
- public byte ReadByte(long Position)
- {
- EnsureAccessIsValid(Position, AMemoryPerm.Read);
- return ReadByteUnchecked(Position);
- }
- public ushort ReadUInt16(long Position)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
- EnsureAccessIsValid(Position + 1, AMemoryPerm.Read);
- return ReadUInt16Unchecked(Position);
- }
- public uint ReadUInt32(long Position)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
- EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
- return ReadUInt32Unchecked(Position);
- }
- public ulong ReadUInt64(long Position)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
- EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
- return ReadUInt64Unchecked(Position);
- }
- public Vector128<float> ReadVector8(long Position)
- {
- 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();
- }
- }
- public Vector128<float> ReadVector16(long Position)
- {
- if (Sse2.IsSupported)
- {
- return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(Position), 0));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- public Vector128<float> ReadVector32(long Position)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
- EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
- if (Sse.IsSupported)
- {
- return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- public Vector128<float> ReadVector64(long Position)
- {
- 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();
- }
- }
- public Vector128<float> ReadVector128(long Position)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
- EnsureAccessIsValid(Position + 15, AMemoryPerm.Read);
- if (Sse.IsSupported)
- {
- return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- 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));
- }
- public Vector128<float> ReadVector8Unchecked(long Position)
- {
- 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();
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public Vector128<float> ReadVector16Unchecked(long Position)
- {
- if (Sse2.IsSupported)
- {
- return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16Unchecked(Position), 0));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public Vector128<float> ReadVector32Unchecked(long Position)
- {
- if (Sse.IsSupported)
- {
- return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public Vector128<float> ReadVector64Unchecked(long Position)
- {
- if (Sse2.IsSupported)
- {
- return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public Vector128<float> ReadVector128Unchecked(long Position)
- {
- if (Sse.IsSupported)
- {
- return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- 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;
- }
- 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);
- }
- public void WriteByte(long Position, byte Value)
- {
- EnsureAccessIsValid(Position, AMemoryPerm.Write);
- WriteByteUnchecked(Position, Value);
- }
- public void WriteUInt16(long Position, ushort Value)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
- EnsureAccessIsValid(Position + 1, AMemoryPerm.Write);
- WriteUInt16Unchecked(Position, Value);
- }
- public void WriteUInt32(long Position, uint Value)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
- EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
- WriteUInt32Unchecked(Position, Value);
- }
- public void WriteUInt64(long Position, ulong Value)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
- EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
- WriteUInt64Unchecked(Position, Value);
- }
- public void WriteVector8(long Position, Vector128<float> Value)
- {
- 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();
- }
- }
- public void WriteVector16(long Position, Vector128<float> Value)
- {
- if (Sse2.IsSupported)
- {
- WriteUInt16(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- public void WriteVector32(long Position, Vector128<float> Value)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
- EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
- if (Sse.IsSupported)
- {
- Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- public void WriteVector64(long Position, Vector128<float> Value)
- {
- 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();
- }
- }
- public void WriteVector128(long Position, Vector128<float> Value)
- {
- EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
- EnsureAccessIsValid(Position + 15, AMemoryPerm.Write);
- if (Sse.IsSupported)
- {
- Sse.Store((float*)(RamPtr + (uint)Position), Value);
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- 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;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void WriteVector8Unchecked(long Position, Vector128<float> Value)
- {
- 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();
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void WriteVector16Unchecked(long Position, Vector128<float> Value)
- {
- if (Sse2.IsSupported)
- {
- WriteUInt16Unchecked(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public void WriteVector32Unchecked(long Position, Vector128<float> Value)
- {
- if (Sse.IsSupported)
- {
- Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- public void WriteVector64Unchecked(long Position, Vector128<float> Value)
- {
- if (Sse2.IsSupported)
- {
- Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void WriteVector128Unchecked(long Position, Vector128<float> Value)
- {
- if (Sse.IsSupported)
- {
- Sse.Store((float*)(RamPtr + (uint)Position), Value);
- }
- else
- {
- throw new PlatformNotSupportedException();
- }
- }
- public void WriteBytes(long Position, byte[] Data)
- {
- EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
- Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
- }
- private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
- {
- long EndPos = Position + Size;
- Position &= ~AMemoryMgr.PageMask;
- while ((ulong)Position < (ulong)EndPos)
- {
- EnsureAccessIsValid(Position, Perm);
- Position += AMemoryMgr.PageSize;
- }
- }
- 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);
- }
- }
- public void Dispose()
- {
- Dispose(true);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (Ram != IntPtr.Zero)
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- AMemoryWin32.Free(Ram);
- }
- else
- {
- Marshal.FreeHGlobal(Ram);
- }
- Ram = IntPtr.Zero;
- }
- }
- }
- }
|