diff options
| author | gdkchan <gab.dark.100@gmail.com> | 2018-03-09 23:12:57 -0300 |
|---|---|---|
| committer | gdkchan <gab.dark.100@gmail.com> | 2018-03-09 23:12:57 -0300 |
| commit | 5912bd2beb42e1853fdcf11e4bb87e063a0ef35b (patch) | |
| tree | 5ad631cea858a30e6c300eba0fa7da7a8ca14799 /ChocolArm64/Memory | |
| parent | be0e4007dc92e24a77bdc36a40d2450c41d9b560 (diff) | |
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
Diffstat (limited to 'ChocolArm64/Memory')
| -rw-r--r-- | ChocolArm64/Memory/AMemory.cs | 111 | ||||
| -rw-r--r-- | ChocolArm64/Memory/AMemoryMgr.cs | 6 |
2 files changed, 46 insertions, 71 deletions
diff --git a/ChocolArm64/Memory/AMemory.cs b/ChocolArm64/Memory/AMemory.cs index 0d202fed..7f2dd7d3 100644 --- a/ChocolArm64/Memory/AMemory.cs +++ b/ChocolArm64/Memory/AMemory.cs @@ -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)) + { + throw new VmmAccessViolationException(Position, Perm); + } } +#endif + } - if (!Manager.HasPermission(Position, Perm)) + 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; } } } diff --git a/ChocolArm64/Memory/AMemoryMgr.cs b/ChocolArm64/Memory/AMemoryMgr.cs index bc364452..8a165b07 100644 --- a/ChocolArm64/Memory/AMemoryMgr.cs +++ b/ChocolArm64/Memory/AMemoryMgr.cs @@ -4,8 +4,8 @@ namespace ChocolArm64.Memory { public class AMemoryMgr { - public const long AddrSize = RamSize; public const long RamSize = 4L * 1024 * 1024 * 1024; + public const long AddrSize = RamSize; private const int PTLvl0Bits = 10; private const int PTLvl1Bits = 10; @@ -19,8 +19,8 @@ namespace ChocolArm64.Memory private const int PTLvl1Mask = PTLvl1Size - 1; public const int PageMask = PageSize - 1; - private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; - private const int PTLvl1Bit = PTPageBits; + private const int PTLvl0Bit = PTPageBits + PTLvl1Bits; + private const int PTLvl1Bit = PTPageBits; private enum PTMap { |
