IVirtualMemoryManager.cs 11 KB

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