2018-02-08 19:26:20 -08:00
|
|
|
using ChocolArm64.Exceptions;
|
2018-02-04 15:08:20 -08:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2018-02-17 20:57:33 -08:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-02-04 15:08:20 -08:00
|
|
|
|
|
|
|
namespace ChocolArm64.Memory
|
|
|
|
{
|
|
|
|
public unsafe class AMemory
|
|
|
|
{
|
2018-02-18 11:28:07 -08:00
|
|
|
private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
|
2018-02-06 07:15:08 -08:00
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
public AMemoryMgr Manager { get; private set; }
|
|
|
|
|
|
|
|
private struct ExMonitor
|
|
|
|
{
|
|
|
|
public long Position { get; private set; }
|
|
|
|
|
|
|
|
private bool ExState;
|
|
|
|
|
|
|
|
public ExMonitor(long Position, bool ExState)
|
|
|
|
{
|
|
|
|
this.Position = Position;
|
|
|
|
this.ExState = ExState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool HasExclusiveAccess(long Position)
|
|
|
|
{
|
|
|
|
return this.Position == Position && ExState;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
ExState = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<int, ExMonitor> Monitors;
|
|
|
|
|
|
|
|
private HashSet<long> ExAddrs;
|
|
|
|
|
|
|
|
private byte* RamPtr;
|
|
|
|
|
2018-02-27 15:45:07 -08:00
|
|
|
public AMemory(IntPtr Ram)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-27 15:45:07 -08:00
|
|
|
Manager = new AMemoryMgr();
|
2018-02-04 15:08:20 -08:00
|
|
|
|
|
|
|
Monitors = new Dictionary<int, ExMonitor>();
|
|
|
|
|
|
|
|
ExAddrs = new HashSet<long>();
|
|
|
|
|
|
|
|
RamPtr = (byte*)Ram;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveMonitor(int ThreadId)
|
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-02-06 07:15:08 -08:00
|
|
|
if (Monitors.TryGetValue(ThreadId, out ExMonitor Monitor))
|
|
|
|
{
|
|
|
|
ExAddrs.Remove(Monitor.Position);
|
|
|
|
}
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
Monitors.Remove(ThreadId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:28:07 -08:00
|
|
|
public void SetExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-13 18:43:08 -08:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-02-18 11:28:07 -08:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ExMonitor Monitor))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-06 07:15:08 -08:00
|
|
|
ExAddrs.Remove(Monitor.Position);
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:15:08 -08:00
|
|
|
bool ExState = ExAddrs.Add(Position);
|
|
|
|
|
|
|
|
Monitor = new ExMonitor(Position, ExState);
|
2018-02-04 15:08:20 -08:00
|
|
|
|
2018-02-18 11:28:07 -08:00
|
|
|
if (!Monitors.TryAdd(ThreadState.ThreadId, Monitor))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-18 11:28:07 -08:00
|
|
|
Monitors[ThreadState.ThreadId] = Monitor;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:28:07 -08:00
|
|
|
public bool TestExclusive(AThreadState ThreadState, long Position)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-13 18:43:08 -08:00
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-02-18 11:28:07 -08:00
|
|
|
if (!Monitors.TryGetValue(ThreadState.ThreadId, out ExMonitor Monitor))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Monitor.HasExclusiveAccess(Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 11:28:07 -08:00
|
|
|
public void ClearExclusive(AThreadState ThreadState)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
2018-02-18 11:28:07 -08:00
|
|
|
if (Monitors.TryGetValue(ThreadState.ThreadId, out ExMonitor Monitor))
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
|
|
|
Monitor.Reset();
|
|
|
|
ExAddrs.Remove(Monitor.Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:43:08 -08:00
|
|
|
public bool AcquireAddress(long Position)
|
|
|
|
{
|
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
|
|
|
return ExAddrs.Add(Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ReleaseAddress(long Position)
|
|
|
|
{
|
|
|
|
Position &= ~ErgMask;
|
|
|
|
|
|
|
|
lock (Monitors)
|
|
|
|
{
|
|
|
|
ExAddrs.Remove(Position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
public sbyte ReadSByte(long Position) => (sbyte)ReadByte (Position);
|
|
|
|
public short ReadInt16(long Position) => (short)ReadUInt16(Position);
|
|
|
|
public int ReadInt32(long Position) => (int)ReadUInt32(Position);
|
|
|
|
public long ReadInt64(long Position) => (long)ReadUInt64(Position);
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public byte ReadByte(long Position)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
return *((byte*)(RamPtr + (uint)Position));
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public ushort ReadUInt16(long Position)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
return *((ushort*)(RamPtr + (uint)Position));
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public uint ReadUInt32(long Position)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
return *((uint*)(RamPtr + (uint)Position));
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public ulong ReadUInt64(long Position)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
return *((ulong*)(RamPtr + (uint)Position));
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public AVec ReadVector8(long Position)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return new AVec() { B0 = ReadByte(Position) };
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public AVec ReadVector16(long Position)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return new AVec() { H0 = ReadUInt16(Position) };
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public AVec ReadVector32(long Position)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return new AVec() { W0 = ReadUInt32(Position) };
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public AVec ReadVector64(long Position)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return new AVec() { X0 = ReadUInt64(Position) };
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public AVec ReadVector128(long Position)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
#endif
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
return new AVec()
|
|
|
|
{
|
|
|
|
X0 = ReadUInt64(Position + 0),
|
|
|
|
X1 = ReadUInt64(Position + 8)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public void WriteByte(long Position, byte Value)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
*((byte*)(RamPtr + (uint)Position)) = Value;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public void WriteUInt16(long Position, ushort Value)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
*((ushort*)(RamPtr + (uint)Position)) = Value;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public void WriteUInt32(long Position, uint Value)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
*((uint*)(RamPtr + (uint)Position)) = Value;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public void WriteUInt64(long Position, ulong Value)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
2018-02-07 08:44:48 -08:00
|
|
|
*((ulong*)(RamPtr + (uint)Position)) = Value;
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public void WriteVector8(long Position, AVec Value)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WriteByte(Position, Value.B0);
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public void WriteVector16(long Position, AVec Value)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WriteUInt16(Position, Value.H0);
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public void WriteVector32(long Position, AVec Value)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WriteUInt32(Position, Value.W0);
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-17 13:06:11 -08:00
|
|
|
public void WriteVector64(long Position, AVec Value)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
WriteUInt64(Position, Value.X0);
|
|
|
|
}
|
|
|
|
|
2018-02-17 20:57:33 -08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2018-02-04 15:08:20 -08:00
|
|
|
public void WriteVector128(long Position, AVec Value)
|
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
#if DEBUG
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
#endif
|
|
|
|
|
2018-02-04 15:08:20 -08:00
|
|
|
WriteUInt64(Position + 0, Value.X0);
|
|
|
|
WriteUInt64(Position + 8, Value.X1);
|
|
|
|
}
|
|
|
|
|
2018-02-08 19:26:20 -08:00
|
|
|
private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
|
2018-02-04 15:08:20 -08:00
|
|
|
{
|
2018-02-08 19:26:20 -08:00
|
|
|
if (!Manager.IsMapped(Position))
|
|
|
|
{
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Manager.HasPermission(Position, Perm))
|
|
|
|
{
|
|
|
|
throw new VmmAccessViolationException(Position, Perm);
|
|
|
|
}
|
2018-02-04 15:08:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|