IVirtualMemoryManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /// Maps a virtual memory range into a physical memory range.
  10. /// </summary>
  11. /// <remarks>
  12. /// Addresses and size must be page aligned.
  13. /// </remarks>
  14. /// <param name="va">Virtual memory address</param>
  15. /// <param name="pa">Physical memory address where the region should be mapped to</param>
  16. /// <param name="size">Size to be mapped</param>
  17. void Map(ulong va, ulong pa, ulong size);
  18. /// <summary>
  19. /// Unmaps a previously mapped range of virtual memory.
  20. /// </summary>
  21. /// <param name="va">Virtual address of the range to be unmapped</param>
  22. /// <param name="size">Size of the range to be unmapped</param>
  23. void Unmap(ulong va, ulong size);
  24. /// <summary>
  25. /// Reads data from CPU mapped memory.
  26. /// </summary>
  27. /// <typeparam name="T">Type of the data being read</typeparam>
  28. /// <param name="va">Virtual address of the data in memory</param>
  29. /// <returns>The data</returns>
  30. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  31. T Read<T>(ulong va) where T : unmanaged;
  32. /// <summary>
  33. /// Reads data from CPU mapped memory.
  34. /// </summary>
  35. /// <param name="va">Virtual address of the data in memory</param>
  36. /// <param name="data">Span to store the data being read into</param>
  37. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  38. void Read(ulong va, Span<byte> data);
  39. /// <summary>
  40. /// Writes data to CPU mapped memory.
  41. /// </summary>
  42. /// <typeparam name="T">Type of the data being written</typeparam>
  43. /// <param name="va">Virtual address to write the data into</param>
  44. /// <param name="value">Data to be written</param>
  45. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  46. void Write<T>(ulong va, T value) where T : unmanaged;
  47. /// <summary>
  48. /// Writes data to CPU mapped memory, with write tracking.
  49. /// </summary>
  50. /// <param name="va">Virtual address to write the data into</param>
  51. /// <param name="data">Data to be written</param>
  52. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  53. void Write(ulong va, ReadOnlySpan<byte> data);
  54. /// <summary>
  55. /// Writes data to the application process, returning false if the data was not changed.
  56. /// This triggers read memory tracking, as a redundancy check would be useless if the data is not up to date.
  57. /// </summary>
  58. /// <remarks>The memory manager can return that memory has changed when it hasn't to avoid expensive data copies.</remarks>
  59. /// <param name="va">Virtual address to write the data into</param>
  60. /// <param name="data">Data to be written</param>
  61. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  62. /// <returns>True if the data was changed, false otherwise</returns>
  63. bool WriteWithRedundancyCheck(ulong va, ReadOnlySpan<byte> data);
  64. void Fill(ulong va, ulong size, byte value)
  65. {
  66. const int MaxChunkSize = 1 << 24;
  67. for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize)
  68. {
  69. int copySize = (int)Math.Min(MaxChunkSize, size - subOffset);
  70. using var writableRegion = GetWritableRegion(va + subOffset, copySize);
  71. writableRegion.Memory.Span.Fill(value);
  72. }
  73. }
  74. /// <summary>
  75. /// Gets a read-only span of data from CPU mapped memory.
  76. /// </summary>
  77. /// <param name="va">Virtual address of the data</param>
  78. /// <param name="size">Size of the data</param>
  79. /// <param name="tracked">True if read tracking is triggered on the span</param>
  80. /// <returns>A read-only span of the data</returns>
  81. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  82. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  83. /// <summary>
  84. /// Gets a region of memory that can be written to.
  85. /// </summary>
  86. /// <param name="va">Virtual address of the data</param>
  87. /// <param name="size">Size of the data</param>
  88. /// <param name="tracked">True if write tracking is triggered on the span</param>
  89. /// <returns>A writable region of memory containing the data</returns>
  90. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  91. WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false);
  92. /// <summary>
  93. /// Gets a reference for the given type at the specified virtual memory address.
  94. /// </summary>
  95. /// <remarks>
  96. /// The data must be located at a contiguous memory region.
  97. /// </remarks>
  98. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  99. /// <param name="va">Virtual address of the data</param>
  100. /// <returns>A reference to the data in memory</returns>
  101. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  102. ref T GetRef<T>(ulong va) where T : unmanaged;
  103. /// <summary>
  104. /// Gets the physical regions that make up the given virtual address region.
  105. /// If any part of the virtual region is unmapped, null is returned.
  106. /// </summary>
  107. /// <param name="va">Virtual address of the range</param>
  108. /// <param name="size">Size of the range</param>
  109. /// <returns>Array of physical regions</returns>
  110. IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size);
  111. /// <summary>
  112. /// Checks if the page at a given CPU virtual address is mapped.
  113. /// </summary>
  114. /// <param name="va">Virtual address to check</param>
  115. /// <returns>True if the address is mapped, false otherwise</returns>
  116. bool IsMapped(ulong va);
  117. /// <summary>
  118. /// Checks if a memory range is mapped.
  119. /// </summary>
  120. /// <param name="va">Virtual address of the range</param>
  121. /// <param name="size">Size of the range in bytes</param>
  122. /// <returns>True if the entire range is mapped, false otherwise</returns>
  123. bool IsRangeMapped(ulong va, ulong size);
  124. /// <summary>
  125. /// Alerts the memory tracking that a given region has been read from or written to.
  126. /// This should be called before read/write is performed.
  127. /// </summary>
  128. /// <param name="va">Virtual address of the region</param>
  129. /// <param name="size">Size of the region</param>
  130. /// <param name="write">True if the region was written, false if read</param>
  131. /// <param name="precise">True if the access is precise, false otherwise</param>
  132. void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false);
  133. /// <summary>
  134. /// Reprotect a region of virtual memory for tracking.
  135. /// </summary>
  136. /// <param name="va">Virtual address base</param>
  137. /// <param name="size">Size of the region to protect</param>
  138. /// <param name="protection">Memory protection to set</param>
  139. void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
  140. }
  141. }