IVirtualMemoryManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Ryujinx.Memory.Range;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Memory
  5. {
  6. public interface IVirtualMemoryManager
  7. {
  8. /// <summary>
  9. /// Indicates whenever the memory manager supports aliasing pages at 4KB granularity.
  10. /// </summary>
  11. /// <returns>True if 4KB pages are supported by the memory manager, false otherwise</returns>
  12. bool Supports4KBPages { get; }
  13. /// <summary>
  14. /// Maps a virtual memory range into a physical memory range.
  15. /// </summary>
  16. /// <remarks>
  17. /// Addresses and size must be page aligned.
  18. /// </remarks>
  19. /// <param name="va">Virtual memory address</param>
  20. /// <param name="pa">Physical memory address where the region should be mapped to</param>
  21. /// <param name="size">Size to be mapped</param>
  22. /// <param name="flags">Flags controlling memory mapping</param>
  23. void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags);
  24. /// <summary>
  25. /// Maps a virtual memory range into an arbitrary host memory range.
  26. /// </summary>
  27. /// <remarks>
  28. /// Addresses and size must be page aligned.
  29. /// Not all memory managers supports this feature.
  30. /// </remarks>
  31. /// <param name="va">Virtual memory address</param>
  32. /// <param name="hostPointer">Host pointer where the virtual region should be mapped</param>
  33. /// <param name="size">Size to be mapped</param>
  34. void MapForeign(ulong va, nuint hostPointer, ulong size);
  35. /// <summary>
  36. /// Unmaps a previously mapped range of virtual memory.
  37. /// </summary>
  38. /// <param name="va">Virtual address of the range to be unmapped</param>
  39. /// <param name="size">Size of the range to be unmapped</param>
  40. void Unmap(ulong va, ulong size);
  41. /// <summary>
  42. /// Reads data from CPU mapped memory.
  43. /// </summary>
  44. /// <typeparam name="T">Type of the data being read</typeparam>
  45. /// <param name="va">Virtual address of the data in memory</param>
  46. /// <returns>The data</returns>
  47. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  48. T Read<T>(ulong va) where T : unmanaged;
  49. /// <summary>
  50. /// Reads data from CPU mapped memory.
  51. /// </summary>
  52. /// <param name="va">Virtual address of the data in memory</param>
  53. /// <param name="data">Span to store the data being read into</param>
  54. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  55. void Read(ulong va, Span<byte> data);
  56. /// <summary>
  57. /// Writes data to CPU mapped memory.
  58. /// </summary>
  59. /// <typeparam name="T">Type of the data being written</typeparam>
  60. /// <param name="va">Virtual address to write the data into</param>
  61. /// <param name="value">Data to be written</param>
  62. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  63. void Write<T>(ulong va, T value) where T : unmanaged;
  64. /// <summary>
  65. /// Writes data to CPU mapped memory, with write tracking.
  66. /// </summary>
  67. /// <param name="va">Virtual address to write the data into</param>
  68. /// <param name="data">Data to be written</param>
  69. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  70. void Write(ulong va, ReadOnlySpan<byte> data);
  71. /// <summary>
  72. /// Writes data to the application process, returning false if the data was not changed.
  73. /// This triggers read memory tracking, as a redundancy check would be useless if the data is not up to date.
  74. /// </summary>
  75. /// <remarks>The memory manager can return that memory has changed when it hasn't to avoid expensive data copies.</remarks>
  76. /// <param name="va">Virtual address to write the data into</param>
  77. /// <param name="data">Data to be written</param>
  78. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  79. /// <returns>True if the data was changed, false otherwise</returns>
  80. bool WriteWithRedundancyCheck(ulong va, ReadOnlySpan<byte> data);
  81. void Fill(ulong va, ulong size, byte value)
  82. {
  83. const int MaxChunkSize = 1 << 24;
  84. for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize)
  85. {
  86. int copySize = (int)Math.Min(MaxChunkSize, size - subOffset);
  87. using var writableRegion = GetWritableRegion(va + subOffset, copySize);
  88. writableRegion.Memory.Span.Fill(value);
  89. }
  90. }
  91. /// <summary>
  92. /// Gets a read-only span of data from CPU mapped memory.
  93. /// </summary>
  94. /// <param name="va">Virtual address of the data</param>
  95. /// <param name="size">Size of the data</param>
  96. /// <param name="tracked">True if read tracking is triggered on the span</param>
  97. /// <returns>A read-only span of the data</returns>
  98. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  99. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  100. /// <summary>
  101. /// Gets a region of memory that can be written to.
  102. /// </summary>
  103. /// <param name="va">Virtual address of the data</param>
  104. /// <param name="size">Size of the data</param>
  105. /// <param name="tracked">True if write tracking is triggered on the span</param>
  106. /// <returns>A writable region of memory containing the data</returns>
  107. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  108. WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false);
  109. /// <summary>
  110. /// Gets a reference for the given type at the specified virtual memory address.
  111. /// </summary>
  112. /// <remarks>
  113. /// The data must be located at a contiguous memory region.
  114. /// </remarks>
  115. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  116. /// <param name="va">Virtual address of the data</param>
  117. /// <returns>A reference to the data in memory</returns>
  118. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  119. ref T GetRef<T>(ulong va) where T : unmanaged;
  120. /// <summary>
  121. /// Gets the host regions that make up the given virtual address region.
  122. /// If any part of the virtual region is unmapped, null is returned.
  123. /// </summary>
  124. /// <param name="va">Virtual address of the range</param>
  125. /// <param name="size">Size of the range</param>
  126. /// <returns>Array of host regions</returns>
  127. IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size);
  128. /// <summary>
  129. /// Gets the physical regions that make up the given virtual address region.
  130. /// If any part of the virtual region is unmapped, null is returned.
  131. /// </summary>
  132. /// <param name="va">Virtual address of the range</param>
  133. /// <param name="size">Size of the range</param>
  134. /// <returns>Array of physical regions</returns>
  135. IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size);
  136. /// <summary>
  137. /// Checks if the page at a given CPU virtual address is mapped.
  138. /// </summary>
  139. /// <param name="va">Virtual address to check</param>
  140. /// <returns>True if the address is mapped, false otherwise</returns>
  141. bool IsMapped(ulong va);
  142. /// <summary>
  143. /// Checks if a memory range is mapped.
  144. /// </summary>
  145. /// <param name="va">Virtual address of the range</param>
  146. /// <param name="size">Size of the range in bytes</param>
  147. /// <returns>True if the entire range is mapped, false otherwise</returns>
  148. bool IsRangeMapped(ulong va, ulong size);
  149. /// <summary>
  150. /// Alerts the memory tracking that a given region has been read from or written to.
  151. /// This should be called before read/write is performed.
  152. /// </summary>
  153. /// <param name="va">Virtual address of the region</param>
  154. /// <param name="size">Size of the region</param>
  155. /// <param name="write">True if the region was written, false if read</param>
  156. /// <param name="precise">True if the access is precise, false otherwise</param>
  157. /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
  158. void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
  159. /// <summary>
  160. /// Reprotect a region of virtual memory for tracking.
  161. /// </summary>
  162. /// <param name="va">Virtual address base</param>
  163. /// <param name="size">Size of the region to protect</param>
  164. /// <param name="protection">Memory protection to set</param>
  165. void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
  166. }
  167. }