IVirtualMemoryManager.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. void Fill(ulong va, ulong size, byte value)
  97. {
  98. const int MaxChunkSize = 1 << 24;
  99. for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize)
  100. {
  101. int copySize = (int)Math.Min(MaxChunkSize, size - subOffset);
  102. using var writableRegion = GetWritableRegion(va + subOffset, copySize);
  103. writableRegion.Memory.Span.Fill(value);
  104. }
  105. }
  106. /// <summary>
  107. /// Gets a read-only span of data from CPU mapped memory.
  108. /// </summary>
  109. /// <param name="va">Virtual address of the data</param>
  110. /// <param name="size">Size of the data</param>
  111. /// <param name="tracked">True if read tracking is triggered on the span</param>
  112. /// <returns>A read-only span of the data</returns>
  113. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  114. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  115. /// <summary>
  116. /// Gets a region of memory that can be written to.
  117. /// </summary>
  118. /// <param name="va">Virtual address of the data</param>
  119. /// <param name="size">Size of the data</param>
  120. /// <param name="tracked">True if write tracking is triggered on the span</param>
  121. /// <returns>A writable region of memory containing the data</returns>
  122. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  123. WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false);
  124. /// <summary>
  125. /// Gets a reference for the given type at the specified virtual memory address.
  126. /// </summary>
  127. /// <remarks>
  128. /// The data must be located at a contiguous memory region.
  129. /// </remarks>
  130. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  131. /// <param name="va">Virtual address of the data</param>
  132. /// <returns>A reference to the data in memory</returns>
  133. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  134. ref T GetRef<T>(ulong va) where T : unmanaged;
  135. /// <summary>
  136. /// Gets the host regions that make up the given virtual address region.
  137. /// If any part of the virtual region is unmapped, null is returned.
  138. /// </summary>
  139. /// <param name="va">Virtual address of the range</param>
  140. /// <param name="size">Size of the range</param>
  141. /// <returns>Array of host regions</returns>
  142. IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size);
  143. /// <summary>
  144. /// Gets the physical regions that make up the given virtual address region.
  145. /// If any part of the virtual region is unmapped, null is returned.
  146. /// </summary>
  147. /// <param name="va">Virtual address of the range</param>
  148. /// <param name="size">Size of the range</param>
  149. /// <returns>Array of physical regions</returns>
  150. IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size);
  151. /// <summary>
  152. /// Checks if the page at a given CPU virtual address is mapped.
  153. /// </summary>
  154. /// <param name="va">Virtual address to check</param>
  155. /// <returns>True if the address is mapped, false otherwise</returns>
  156. bool IsMapped(ulong va);
  157. /// <summary>
  158. /// Checks if a memory range is mapped.
  159. /// </summary>
  160. /// <param name="va">Virtual address of the range</param>
  161. /// <param name="size">Size of the range in bytes</param>
  162. /// <returns>True if the entire range is mapped, false otherwise</returns>
  163. bool IsRangeMapped(ulong va, ulong size);
  164. /// <summary>
  165. /// Alerts the memory tracking that a given region has been read from or written to.
  166. /// This should be called before read/write is performed.
  167. /// </summary>
  168. /// <param name="va">Virtual address of the region</param>
  169. /// <param name="size">Size of the region</param>
  170. /// <param name="write">True if the region was written, false if read</param>
  171. /// <param name="precise">True if the access is precise, false otherwise</param>
  172. /// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
  173. void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null);
  174. /// <summary>
  175. /// Reprotect a region of virtual memory for tracking.
  176. /// </summary>
  177. /// <param name="va">Virtual address base</param>
  178. /// <param name="size">Size of the region to protect</param>
  179. /// <param name="protection">Memory protection to set</param>
  180. void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
  181. }
  182. }