IVirtualMemoryManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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="hostAddress">Pointer where the region should be mapped to</param>
  16. /// <param name="size">Size to be mapped</param>
  17. void Map(ulong va, nuint hostAddress, 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. void Fill(ulong va, ulong size, byte value)
  55. {
  56. const int MaxChunkSize = 1 << 24;
  57. for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize)
  58. {
  59. int copySize = (int)Math.Min(MaxChunkSize, size - subOffset);
  60. using var writableRegion = GetWritableRegion(va + subOffset, copySize);
  61. writableRegion.Memory.Span.Fill(0);
  62. }
  63. }
  64. /// <summary>
  65. /// Gets a read-only span of data from CPU mapped memory.
  66. /// </summary>
  67. /// <param name="va">Virtual address of the data</param>
  68. /// <param name="size">Size of the data</param>
  69. /// <param name="tracked">True if read tracking is triggered on the span</param>
  70. /// <returns>A read-only span of the data</returns>
  71. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  72. ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false);
  73. /// <summary>
  74. /// Gets a region of memory that can be written to.
  75. /// </summary>
  76. /// <param name="va">Virtual address of the data</param>
  77. /// <param name="size">Size of the data</param>
  78. /// <param name="tracked">True if write tracking is triggered on the span</param>
  79. /// <returns>A writable region of memory containing the data</returns>
  80. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  81. WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false);
  82. /// <summary>
  83. /// Gets a reference for the given type at the specified virtual memory address.
  84. /// </summary>
  85. /// <remarks>
  86. /// The data must be located at a contiguous memory region.
  87. /// </remarks>
  88. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  89. /// <param name="va">Virtual address of the data</param>
  90. /// <returns>A reference to the data in memory</returns>
  91. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  92. ref T GetRef<T>(ulong va) where T : unmanaged;
  93. /// <summary>
  94. /// Gets the physical regions that make up the given virtual address region.
  95. /// If any part of the virtual region is unmapped, null is returned.
  96. /// </summary>
  97. /// <param name="va">Virtual address of the range</param>
  98. /// <param name="size">Size of the range</param>
  99. /// <returns>Array of physical regions</returns>
  100. IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size);
  101. /// <summary>
  102. /// Checks if the page at a given CPU virtual address is mapped.
  103. /// </summary>
  104. /// <param name="va">Virtual address to check</param>
  105. /// <returns>True if the address is mapped, false otherwise</returns>
  106. bool IsMapped(ulong va);
  107. /// <summary>
  108. /// Checks if a memory range is mapped.
  109. /// </summary>
  110. /// <param name="va">Virtual address of the range</param>
  111. /// <param name="size">Size of the range in bytes</param>
  112. /// <returns>True if the entire range is mapped, false otherwise</returns>
  113. bool IsRangeMapped(ulong va, ulong size);
  114. /// <summary>
  115. /// Alerts the memory tracking that a given region has been read from or written to.
  116. /// This should be called before read/write is performed.
  117. /// </summary>
  118. /// <param name="va">Virtual address of the region</param>
  119. /// <param name="size">Size of the region</param>
  120. /// <param name="write">True if the region was written, false if read</param>
  121. /// <param name="precise">True if the access is precise, false otherwise</param>
  122. void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false);
  123. /// <summary>
  124. /// Reprotect a region of virtual memory for tracking.
  125. /// </summary>
  126. /// <param name="va">Virtual address base</param>
  127. /// <param name="size">Size of the region to protect</param>
  128. /// <param name="protection">Memory protection to set</param>
  129. void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
  130. }
  131. }