Disable memory checks by default, even on debug, move ram memory allocation inside the CPU, since the size if fixed anyway, better heap region size

This commit is contained in:
gdkchan 2018-03-09 23:12:57 -03:00
parent 9376a61229
commit 9aaf563df1
4 changed files with 49 additions and 74 deletions

View file

@ -1,4 +1,4 @@
public static class AOptimizations public static class AOptimizations
{ {
public static bool EnableMemoryChecks = false;
} }

View file

@ -10,7 +10,7 @@ namespace ChocolArm64
public AThreadState ThreadState { get; private set; } public AThreadState ThreadState { get; private set; }
public AMemory Memory { get; private set; } public AMemory Memory { get; private set; }
public long EntryPoint { get; private set; } private long EntryPoint;
private ATranslator Translator; private ATranslator Translator;

View file

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

View file

@ -4,8 +4,8 @@ namespace ChocolArm64.Memory
{ {
public class AMemoryMgr public class AMemoryMgr
{ {
public const long AddrSize = RamSize;
public const long RamSize = 4L * 1024 * 1024 * 1024; public const long RamSize = 4L * 1024 * 1024 * 1024;
public const long AddrSize = RamSize;
private const int PTLvl0Bits = 10; private const int PTLvl0Bits = 10;
private const int PTLvl1Bits = 10; private const int PTLvl1Bits = 10;
@ -19,8 +19,8 @@ namespace ChocolArm64.Memory
private const int PTLvl1Mask = PTLvl1Size - 1; private const int PTLvl1Mask = PTLvl1Size - 1;
public const int PageMask = PageSize - 1; public const int PageMask = PageSize - 1;
private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; private const int PTLvl0Bit = PTPageBits + PTLvl1Bits;
private const int PTLvl1Bit = PTPageBits; private const int PTLvl1Bit = PTPageBits;
private enum PTMap private enum PTMap
{ {