IVirtualMemoryManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 span of data 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 span</param>
  118. /// <returns>A read-only span of the data</returns>
  119. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  120. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  121. /// <summary>
  122. /// Gets a region of memory that can be written to.
  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 write tracking is triggered on the span</param>
  127. /// <returns>A writable region of memory containing the data</returns>
  128. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  129. WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false);
  130. /// <summary>
  131. /// Gets a reference for the given type at the specified virtual memory address.
  132. /// </summary>
  133. /// <remarks>
  134. /// The data must be located at a contiguous memory region.
  135. /// </remarks>
  136. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  137. /// <param name="va">Virtual address of the data</param>
  138. /// <returns>A reference to the data in memory</returns>
  139. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  140. ref T GetRef<T>(ulong va) where T : unmanaged;
  141. /// <summary>
  142. /// Gets the host regions that make up the given virtual address region.
  143. /// If any part of the virtual region is unmapped, null is returned.
  144. /// </summary>
  145. /// <param name="va">Virtual address of the range</param>
  146. /// <param name="size">Size of the range</param>
  147. /// <returns>Array of host regions</returns>
  148. IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size);
  149. /// <summary>
  150. /// Gets the physical regions that make up the given virtual address region.
  151. /// If any part of the virtual region is unmapped, null is returned.
  152. /// </summary>
  153. /// <param name="va">Virtual address of the range</param>
  154. /// <param name="size">Size of the range</param>
  155. /// <returns>Array of physical regions</returns>
  156. IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size);
  157. /// <summary>
  158. /// Checks if the page at a given CPU virtual address is mapped.
  159. /// </summary>
  160. /// <param name="va">Virtual address to check</param>
  161. /// <returns>True if the address is mapped, false otherwise</returns>
  162. bool IsMapped(ulong va);
  163. /// <summary>
  164. /// Checks if a memory range is mapped.
  165. /// </summary>
  166. /// <param name="va">Virtual address of the range</param>
  167. /// <param name="size">Size of the range in bytes</param>
  168. /// <returns>True if the entire range is mapped, false otherwise</returns>
  169. bool IsRangeMapped(ulong va, ulong size);
  170. /// <summary>
  171. /// Alerts the memory tracking that a given region has been read from or written to.
  172. /// This should be called before read/write is performed.
  173. /// </summary>
  174. /// <param name="va">Virtual address of the region</param>
  175. /// <param name="size">Size of the region</param>
  176. /// <param name="write">True if the region was written, false if read</param>
  177. /// <param name="precise">True if the access is precise, false otherwise</param>
  178. /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
  179. void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
  180. /// <summary>
  181. /// Reprotect a region of virtual memory for guest access.
  182. /// </summary>
  183. /// <param name="va">Virtual address base</param>
  184. /// <param name="size">Size of the region to protect</param>
  185. /// <param name="protection">Memory protection to set</param>
  186. void Reprotect(ulong va, ulong size, MemoryPermission protection);
  187. /// <summary>
  188. /// Reprotect a region of virtual memory for tracking.
  189. /// </summary>
  190. /// <param name="va">Virtual address base</param>
  191. /// <param name="size">Size of the region to protect</param>
  192. /// <param name="protection">Memory protection to set</param>
  193. void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
  194. }
  195. }