|
|
|
@@ -3,10 +3,11 @@ using ChocolArm64.State;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace ChocolArm64.Memory
|
|
|
|
|
{
|
|
|
|
|
public unsafe class AMemory
|
|
|
|
|
public unsafe class AMemory : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
|
|
|
|
|
|
|
|
|
@@ -39,9 +40,11 @@ namespace ChocolArm64.Memory
|
|
|
|
|
|
|
|
|
|
private HashSet<long> ExAddrs;
|
|
|
|
|
|
|
|
|
|
public IntPtr Ram { get; private set; }
|
|
|
|
|
|
|
|
|
|
private byte* RamPtr;
|
|
|
|
|
|
|
|
|
|
public AMemory(IntPtr Ram)
|
|
|
|
|
public AMemory()
|
|
|
|
|
{
|
|
|
|
|
Manager = new AMemoryMgr();
|
|
|
|
|
|
|
|
|
@@ -49,6 +52,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
|
|
|
|
|
ExAddrs = new HashSet<long>();
|
|
|
|
|
|
|
|
|
|
Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize);
|
|
|
|
|
|
|
|
|
|
RamPtr = (byte*)Ram;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -142,9 +147,7 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public byte ReadByte(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return *((byte*)(RamPtr + (uint)Position));
|
|
|
|
|
}
|
|
|
|
@@ -152,9 +155,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public ushort ReadUInt16(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
|
EnsureAccessIsValid(Position + 1, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
|
|
return *((ushort*)(RamPtr + (uint)Position));
|
|
|
|
|
}
|
|
|
|
@@ -162,9 +164,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public uint ReadUInt32(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
|
|
return *((uint*)(RamPtr + (uint)Position));
|
|
|
|
|
}
|
|
|
|
@@ -172,9 +173,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public ulong ReadUInt64(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
|
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
|
|
|
|
|
|
|
|
|
|
return *((ulong*)(RamPtr + (uint)Position));
|
|
|
|
|
}
|
|
|
|
@@ -182,50 +182,30 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public AVec ReadVector8(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new AVec() { B0 = ReadByte(Position) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public AVec ReadVector16(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new AVec() { H0 = ReadUInt16(Position) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public AVec ReadVector32(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new AVec() { W0 = ReadUInt32(Position) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public AVec ReadVector64(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new AVec() { X0 = ReadUInt64(Position) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public AVec ReadVector128(long Position)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Read);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new AVec()
|
|
|
|
|
{
|
|
|
|
|
X0 = ReadUInt64(Position + 0),
|
|
|
|
@@ -241,9 +221,7 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteByte(long Position, byte Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
*((byte*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
|
}
|
|
|
|
@@ -251,9 +229,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteUInt16(long Position, ushort Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
|
EnsureAccessIsValid(Position + 1, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
|
|
*((ushort*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
|
}
|
|
|
|
@@ -261,9 +238,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteUInt32(long Position, uint Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
|
EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
|
|
*((uint*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
|
}
|
|
|
|
@@ -271,9 +247,8 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteUInt64(long Position, ulong Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
|
|
|
|
|
EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
|
|
|
|
|
|
|
|
|
|
*((ulong*)(RamPtr + (uint)Position)) = Value;
|
|
|
|
|
}
|
|
|
|
@@ -281,64 +256,64 @@ namespace ChocolArm64.Memory
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteVector8(long Position, AVec Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteByte(Position, Value.B0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteVector16(long Position, AVec Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteUInt16(Position, Value.H0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteVector32(long Position, AVec Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteUInt32(Position, Value.W0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteVector64(long Position, AVec Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteUInt64(Position, Value.X0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void WriteVector128(long Position, AVec Value)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
EnsureAccessIsValid(Position, AMemoryPerm.Write);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
WriteUInt64(Position + 0, Value.X0);
|
|
|
|
|
WriteUInt64(Position + 8, Value.X1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
|
|
|
|
|
{
|
|
|
|
|
if (!Manager.IsMapped(Position))
|
|
|
|
|
#if DEBUG
|
|
|
|
|
if (AOptimizations.EnableMemoryChecks)
|
|
|
|
|
{
|
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
|
}
|
|
|
|
|
if (!Manager.IsMapped(Position))
|
|
|
|
|
{
|
|
|
|
|
throw new VmmPageFaultException(Position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Manager.HasPermission(Position, Perm))
|
|
|
|
|
if (!Manager.HasPermission(Position, Perm))
|
|
|
|
|
{
|
|
|
|
|
throw new VmmAccessViolationException(Position, Perm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (Ram != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
throw new VmmAccessViolationException(Position, Perm);
|
|
|
|
|
Marshal.FreeHGlobal(Ram);
|
|
|
|
|
|
|
|
|
|
Ram = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|