PhysicalMemory.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using Ryujinx.Cpu;
  2. using Ryujinx.Cpu.Tracking;
  3. using Ryujinx.Memory;
  4. using Ryujinx.Memory.Range;
  5. using System;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.Graphics.Gpu.Memory
  9. {
  10. /// <summary>
  11. /// Represents physical memory, accessible from the GPU.
  12. /// This is actually working CPU virtual addresses, of memory mapped on the application process.
  13. /// </summary>
  14. class PhysicalMemory : IDisposable
  15. {
  16. public const int PageSize = 0x1000;
  17. private IVirtualMemoryManagerTracked _cpuMemory;
  18. /// <summary>
  19. /// Creates a new instance of the physical memory.
  20. /// </summary>
  21. /// <param name="cpuMemory">CPU memory manager of the application process</param>
  22. public PhysicalMemory(IVirtualMemoryManagerTracked cpuMemory)
  23. {
  24. _cpuMemory = cpuMemory;
  25. if (_cpuMemory is IRefCounted rc)
  26. {
  27. rc.IncrementReferenceCount();
  28. }
  29. }
  30. /// <summary>
  31. /// Gets a span of data from the application process.
  32. /// </summary>
  33. /// <param name="address">Start address of the range</param>
  34. /// <param name="size">Size in bytes to be range</param>
  35. /// <param name="tracked">True if read tracking is triggered on the span</param>
  36. /// <returns>A read only span of the data at the specified memory location</returns>
  37. public ReadOnlySpan<byte> GetSpan(ulong address, int size, bool tracked = false)
  38. {
  39. return _cpuMemory.GetSpan(address, size, tracked);
  40. }
  41. /// <summary>
  42. /// Gets a span of data from the application process.
  43. /// </summary>
  44. /// <param name="range">Ranges of physical memory where the data is located</param>
  45. /// <param name="tracked">True if read tracking is triggered on the span</param>
  46. /// <returns>A read only span of the data at the specified memory location</returns>
  47. public ReadOnlySpan<byte> GetSpan(MultiRange range, bool tracked = false)
  48. {
  49. if (range.Count == 1)
  50. {
  51. var singleRange = range.GetSubRange(0);
  52. return _cpuMemory.GetSpan(singleRange.Address, (int)singleRange.Size, tracked);
  53. }
  54. else
  55. {
  56. Span<byte> data = new byte[range.GetSize()];
  57. int offset = 0;
  58. for (int i = 0; i < range.Count; i++)
  59. {
  60. var currentRange = range.GetSubRange(i);
  61. int size = (int)currentRange.Size;
  62. _cpuMemory.GetSpan(currentRange.Address, size, tracked).CopyTo(data.Slice(offset, size));
  63. offset += size;
  64. }
  65. return data;
  66. }
  67. }
  68. /// <summary>
  69. /// Gets a writable region from the application process.
  70. /// </summary>
  71. /// <param name="address">Start address of the range</param>
  72. /// <param name="size">Size in bytes to be range</param>
  73. /// <returns>A writable region with the data at the specified memory location</returns>
  74. public WritableRegion GetWritableRegion(ulong address, int size)
  75. {
  76. return _cpuMemory.GetWritableRegion(address, size);
  77. }
  78. /// <summary>
  79. /// Reads data from the application process.
  80. /// </summary>
  81. /// <typeparam name="T">Type of the structure</typeparam>
  82. /// <param name="gpuVa">Address to read from</param>
  83. /// <returns>The data at the specified memory location</returns>
  84. public T Read<T>(ulong address) where T : unmanaged
  85. {
  86. return MemoryMarshal.Cast<byte, T>(GetSpan(address, Unsafe.SizeOf<T>()))[0];
  87. }
  88. /// <summary>
  89. /// Writes data to the application process.
  90. /// </summary>
  91. /// <param name="address">Address to write into</param>
  92. /// <param name="data">Data to be written</param>
  93. public void Write(ulong address, ReadOnlySpan<byte> data)
  94. {
  95. _cpuMemory.Write(address, data);
  96. }
  97. /// <summary>
  98. /// Writes data to the application process.
  99. /// </summary>
  100. /// <param name="range">Ranges of physical memory where the data is located</param>
  101. /// <param name="data">Data to be written</param>
  102. public void Write(MultiRange range, ReadOnlySpan<byte> data)
  103. {
  104. WriteImpl(range, data, _cpuMemory.Write);
  105. }
  106. /// <summary>
  107. /// Writes data to the application process, without any tracking.
  108. /// </summary>
  109. /// <param name="address">Address to write into</param>
  110. /// <param name="data">Data to be written</param>
  111. public void WriteUntracked(ulong address, ReadOnlySpan<byte> data)
  112. {
  113. _cpuMemory.WriteUntracked(address, data);
  114. }
  115. /// <summary>
  116. /// Writes data to the application process, without any tracking.
  117. /// </summary>
  118. /// <param name="range">Ranges of physical memory where the data is located</param>
  119. /// <param name="data">Data to be written</param>
  120. public void WriteUntracked(MultiRange range, ReadOnlySpan<byte> data)
  121. {
  122. WriteImpl(range, data, _cpuMemory.WriteUntracked);
  123. }
  124. private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
  125. /// <summary>
  126. /// Writes data to the application process, using the supplied callback method.
  127. /// </summary>
  128. /// <param name="range">Ranges of physical memory where the data is located</param>
  129. /// <param name="data">Data to be written</param>
  130. /// <param name="writeCallback">Callback method that will perform the write</param>
  131. private void WriteImpl(MultiRange range, ReadOnlySpan<byte> data, WriteCallback writeCallback)
  132. {
  133. if (range.Count == 1)
  134. {
  135. var singleRange = range.GetSubRange(0);
  136. writeCallback(singleRange.Address, data);
  137. }
  138. else
  139. {
  140. int offset = 0;
  141. for (int i = 0; i < range.Count; i++)
  142. {
  143. var currentRange = range.GetSubRange(i);
  144. int size = (int)currentRange.Size;
  145. writeCallback(currentRange.Address, data.Slice(offset, size));
  146. offset += size;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
  152. /// </summary>
  153. /// <param name="address">CPU virtual address of the region</param>
  154. /// <param name="size">Size of the region</param>
  155. /// <returns>The memory tracking handle</returns>
  156. public CpuRegionHandle BeginTracking(ulong address, ulong size)
  157. {
  158. return _cpuMemory.BeginTracking(address, size);
  159. }
  160. /// <summary>
  161. /// Obtains a memory tracking handle for the given virtual region. This should be disposed when finished with.
  162. /// </summary>
  163. /// <param name="range">Ranges of physical memory where the data is located</param>
  164. /// <returns>The memory tracking handle</returns>
  165. public GpuRegionHandle BeginTracking(MultiRange range)
  166. {
  167. var cpuRegionHandles = new CpuRegionHandle[range.Count];
  168. for (int i = 0; i < range.Count; i++)
  169. {
  170. var currentRange = range.GetSubRange(i);
  171. cpuRegionHandles[i] = _cpuMemory.BeginTracking(currentRange.Address, currentRange.Size);
  172. }
  173. return new GpuRegionHandle(cpuRegionHandles);
  174. }
  175. /// <summary>
  176. /// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  177. /// </summary>
  178. /// <param name="address">CPU virtual address of the region</param>
  179. /// <param name="size">Size of the region</param>
  180. /// <param name="granularity">Desired granularity of write tracking</param>
  181. /// <returns>The memory tracking handle</returns>
  182. public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, ulong granularity = 4096)
  183. {
  184. return _cpuMemory.BeginGranularTracking(address, size, granularity);
  185. }
  186. /// <summary>
  187. /// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
  188. /// </summary>
  189. /// <param name="address">CPU virtual address of the region</param>
  190. /// <param name="size">Size of the region</param>
  191. /// <param name="granularity">Desired granularity of write tracking</param>
  192. /// <returns>The memory tracking handle</returns>
  193. public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity = 4096)
  194. {
  195. return _cpuMemory.BeginSmartGranularTracking(address, size, granularity);
  196. }
  197. /// <summary>
  198. /// Release our reference to the CPU memory manager.
  199. /// </summary>
  200. public void Dispose()
  201. {
  202. if (_cpuMemory is IRefCounted rc)
  203. {
  204. rc.DecrementReferenceCount();
  205. _cpuMemory = null;
  206. }
  207. }
  208. }
  209. }