PhysicalMemory.cs 9.5 KB

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