PhysicalMemory.cs 8.8 KB

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