From f77694e4f774c9391aad5344e70a7c8721cfedc6 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 3 May 2020 19:54:50 -0300 Subject: Implement a new physical memory manager and replace DeviceMemory (#856) * Implement a new physical memory manager and replace DeviceMemory * Proper generic constraints * Fix debug build * Add memory tests * New CPU memory manager and general code cleanup * Remove host memory management from CPU project, use Ryujinx.Memory instead * Fix tests * Document exceptions on MemoryBlock * Fix leak on unix memory allocation * Proper disposal of some objects on tests * Fix JitCache not being set as initialized * GetRef without checks for 8-bits and 16-bits CAS * Add MemoryBlock destructor * Throw in separate method to improve codegen * Address PR feedback * QueryModified improvements * Fix memory write tracking not marking all pages as modified in some cases * Simplify MarkRegionAsModified * Remove XML doc for ghost param * Add back optimization to avoid useless buffer updates * Add Ryujinx.Cpu project, move MemoryManager there and remove MemoryBlockWrapper * Some nits * Do not perform address translation when size is 0 * Address PR feedback and format NativeInterface class * Remove ghost parameter description * Update Ryujinx.Cpu to .NET Core 3.1 * Address PR feedback * Fix build * Return a well defined value for GetPhysicalAddress with invalid VA, and do not return unmapped ranges as modified * Typo --- Ryujinx.Memory/MemoryBlock.cs | 276 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 Ryujinx.Memory/MemoryBlock.cs (limited to 'Ryujinx.Memory/MemoryBlock.cs') diff --git a/Ryujinx.Memory/MemoryBlock.cs b/Ryujinx.Memory/MemoryBlock.cs new file mode 100644 index 00000000..850fb115 --- /dev/null +++ b/Ryujinx.Memory/MemoryBlock.cs @@ -0,0 +1,276 @@ +using System; +using System.Runtime.CompilerServices; +using System.Threading; + +namespace Ryujinx.Memory +{ + /// + /// Represents a block of contiguous physical guest memory. + /// + public sealed class MemoryBlock : IDisposable + { + private IntPtr _pointer; + + /// + /// Pointer to the memory block data. + /// + public IntPtr Pointer => _pointer; + + /// + /// Size of the memory block. + /// + public ulong Size { get; } + + /// + /// Initializes a new instance of the memory block class. + /// + /// Size of the memory block + /// Flags that control memory block memory allocation + /// Throw when there's no enough memory to allocate the requested size + /// Throw when the current platform is not supported + public MemoryBlock(ulong size, MemoryAllocationFlags flags = MemoryAllocationFlags.None) + { + if (flags.HasFlag(MemoryAllocationFlags.Reserve)) + { + _pointer = MemoryManagement.Reserve(size); + } + else + { + _pointer = MemoryManagement.Allocate(size); + } + + Size = size; + } + + /// + /// Commits a region of memory that has previously been reserved. + /// This can be used to allocate memory on demand. + /// + /// Starting offset of the range to be committed + /// Size of the range to be committed + /// True if the operation was successful, false otherwise + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + public bool Commit(ulong offset, ulong size) + { + return MemoryManagement.Commit(GetPointerInternal(offset, size), size); + } + + /// + /// Reprotects a region of memory. + /// + /// Starting offset of the range to be reprotected + /// Size of the range to be reprotected + /// New memory permissions + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + /// Throw when is invalid + public void Reprotect(ulong offset, ulong size, MemoryPermission permission) + { + MemoryManagement.Reprotect(GetPointerInternal(offset, size), size, permission); + } + + /// + /// Reads bytes from the memory block. + /// + /// Starting offset of the range being read + /// Span where the bytes being read will be copied to + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Read(ulong offset, Span data) + { + GetSpan(offset, data.Length).CopyTo(data); + } + + /// + /// Reads data from the memory block. + /// + /// Type of the data + /// Offset where the data is located + /// Data at the specified address + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Read(ulong offset) where T : unmanaged + { + return GetRef(offset); + } + + /// + /// Writes bytes to the memory block. + /// + /// Starting offset of the range being written + /// Span where the bytes being written will be copied from + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(ulong offset, ReadOnlySpan data) + { + data.CopyTo(GetSpan(offset, data.Length)); + } + + /// + /// Writes data to the memory block. + /// + /// Type of the data being written + /// Offset to write the data into + /// Data to be written + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(ulong offset, T data) where T : unmanaged + { + GetRef(offset) = data; + } + + /// + /// Copies data from one memory location to another. + /// + /// Source offset to read the data from + /// Destination offset to write the data into + /// Size of the copy in bytes + /// Throw when the memory block has already been disposed + /// Throw when , or is out of range + public void Copy(ulong srcOffset, ulong dstOffset, ulong size) + { + const int MaxChunkSize = 1 << 30; + + for (ulong offset = 0; offset < size; offset += MaxChunkSize) + { + int copySize = (int)Math.Min(MaxChunkSize, size - offset); + + Write(dstOffset + offset, GetSpan(srcOffset + offset, copySize)); + } + } + + /// + /// Fills a region of memory with zeros. + /// + /// Offset of the region to fill with zeros + /// Size in bytes of the region to fill + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + public void ZeroFill(ulong offset, ulong size) + { + const int MaxChunkSize = 1 << 30; + + for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize) + { + int copySize = (int)Math.Min(MaxChunkSize, size - subOffset); + + GetSpan(offset + subOffset, copySize).Fill(0); + } + } + + /// + /// Gets a reference of the data at a given memory block region. + /// + /// Data type + /// Offset of the memory region + /// A reference to the given memory region data + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe ref T GetRef(ulong offset) where T : unmanaged + { + IntPtr ptr = _pointer; + + if (ptr == IntPtr.Zero) + { + ThrowObjectDisposed(); + } + + int size = Unsafe.SizeOf(); + + ulong endOffset = offset + (ulong)size; + + if (endOffset > Size || endOffset < offset) + { + ThrowArgumentOutOfRange(); + } + + return ref Unsafe.AsRef((void*)PtrAddr(ptr, offset)); + } + + /// + /// Gets the pointer of a given memory block region. + /// + /// Start offset of the memory region + /// Size in bytes of the region + /// The pointer to the memory region + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public IntPtr GetPointer(ulong offset, int size) => GetPointerInternal(offset, (ulong)size); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private IntPtr GetPointerInternal(ulong offset, ulong size) + { + IntPtr ptr = _pointer; + + if (ptr == IntPtr.Zero) + { + ThrowObjectDisposed(); + } + + ulong endOffset = offset + size; + + if (endOffset > Size || endOffset < offset) + { + ThrowArgumentOutOfRange(); + } + + return PtrAddr(ptr, offset); + } + + /// + /// Gets the span of a given memory block region. + /// + /// Start offset of the memory region + /// Size in bytes of the region + /// Span of the memory region + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe Span GetSpan(ulong offset, int size) + { + return new Span((void*)GetPointer(offset, size), size); + } + + /// + /// Adds a 64-bits offset to a native pointer. + /// + /// Native pointer + /// Offset to add + /// Native pointer with the added offset + private IntPtr PtrAddr(IntPtr pointer, ulong offset) + { + return (IntPtr)(pointer.ToInt64() + (long)offset); + } + + /// + /// Frees the memory allocated for this memory block. + /// + /// + /// It's an error to use the memory block after disposal. + /// + public void Dispose() => FreeMemory(); + + ~MemoryBlock() => FreeMemory(); + + private void FreeMemory() + { + IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero); + + // If pointer is null, the memory was already freed or never allocated. + if (ptr != IntPtr.Zero) + { + MemoryManagement.Free(ptr); + } + } + + private void ThrowObjectDisposed() => throw new ObjectDisposedException(nameof(MemoryBlock)); + private void ThrowArgumentOutOfRange() => throw new ArgumentOutOfRangeException(); + } +} -- cgit v1.2.3