VirtualMemoryManagerBase.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using Ryujinx.Common.Memory;
  2. using System;
  3. using System.Buffers;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Memory
  7. {
  8. public abstract class VirtualMemoryManagerBase : IWritableBlock
  9. {
  10. public const int PageBits = 12;
  11. public const int PageSize = 1 << PageBits;
  12. public const int PageMask = PageSize - 1;
  13. protected abstract ulong AddressSpaceSize { get; }
  14. public virtual ReadOnlySequence<byte> GetReadOnlySequence(ulong va, int size, bool tracked = false)
  15. {
  16. if (size == 0)
  17. {
  18. return ReadOnlySequence<byte>.Empty;
  19. }
  20. if (tracked)
  21. {
  22. SignalMemoryTracking(va, (ulong)size, false);
  23. }
  24. if (IsContiguousAndMapped(va, size))
  25. {
  26. nuint pa = TranslateVirtualAddressUnchecked(va);
  27. return new ReadOnlySequence<byte>(GetPhysicalAddressMemory(pa, size));
  28. }
  29. else
  30. {
  31. AssertValidAddressAndSize(va, size);
  32. int offset = 0, segmentSize;
  33. BytesReadOnlySequenceSegment first = null, last = null;
  34. if ((va & PageMask) != 0)
  35. {
  36. nuint pa = TranslateVirtualAddressChecked(va);
  37. segmentSize = Math.Min(size, PageSize - (int)(va & PageMask));
  38. Memory<byte> memory = GetPhysicalAddressMemory(pa, segmentSize);
  39. first = last = new BytesReadOnlySequenceSegment(memory);
  40. offset += segmentSize;
  41. }
  42. for (; offset < size; offset += segmentSize)
  43. {
  44. nuint pa = TranslateVirtualAddressChecked(va + (ulong)offset);
  45. segmentSize = Math.Min(size - offset, PageSize);
  46. Memory<byte> memory = GetPhysicalAddressMemory(pa, segmentSize);
  47. if (first is null)
  48. {
  49. first = last = new BytesReadOnlySequenceSegment(memory);
  50. }
  51. else
  52. {
  53. if (last.IsContiguousWith(memory, out nuint contiguousStart, out int contiguousSize))
  54. {
  55. last.Replace(GetPhysicalAddressMemory(contiguousStart, contiguousSize));
  56. }
  57. else
  58. {
  59. last = last.Append(memory);
  60. }
  61. }
  62. }
  63. return new ReadOnlySequence<byte>(first, 0, last, (int)(size - last.RunningIndex));
  64. }
  65. }
  66. public virtual ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
  67. {
  68. if (size == 0)
  69. {
  70. return ReadOnlySpan<byte>.Empty;
  71. }
  72. if (tracked)
  73. {
  74. SignalMemoryTracking(va, (ulong)size, false);
  75. }
  76. if (IsContiguousAndMapped(va, size))
  77. {
  78. nuint pa = TranslateVirtualAddressUnchecked(va);
  79. return GetPhysicalAddressSpan(pa, size);
  80. }
  81. else
  82. {
  83. Span<byte> data = new byte[size];
  84. Read(va, data);
  85. return data;
  86. }
  87. }
  88. public virtual WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
  89. {
  90. if (size == 0)
  91. {
  92. return new WritableRegion(null, va, Memory<byte>.Empty);
  93. }
  94. if (tracked)
  95. {
  96. SignalMemoryTracking(va, (ulong)size, true);
  97. }
  98. if (IsContiguousAndMapped(va, size))
  99. {
  100. nuint pa = TranslateVirtualAddressUnchecked(va);
  101. return new WritableRegion(null, va, GetPhysicalAddressMemory(pa, size));
  102. }
  103. else
  104. {
  105. IMemoryOwner<byte> memoryOwner = ByteMemoryPool.Rent(size);
  106. Read(va, memoryOwner.Memory.Span);
  107. return new WritableRegion(this, va, memoryOwner);
  108. }
  109. }
  110. public abstract bool IsMapped(ulong va);
  111. public virtual void MapForeign(ulong va, nuint hostPointer, ulong size)
  112. {
  113. throw new NotSupportedException();
  114. }
  115. public virtual T Read<T>(ulong va) where T : unmanaged
  116. {
  117. return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
  118. }
  119. public virtual void Read(ulong va, Span<byte> data)
  120. {
  121. if (data.Length == 0)
  122. {
  123. return;
  124. }
  125. AssertValidAddressAndSize(va, data.Length);
  126. int offset = 0, size;
  127. if ((va & PageMask) != 0)
  128. {
  129. nuint pa = TranslateVirtualAddressChecked(va);
  130. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  131. GetPhysicalAddressSpan(pa, size).CopyTo(data[..size]);
  132. offset += size;
  133. }
  134. for (; offset < data.Length; offset += size)
  135. {
  136. nuint pa = TranslateVirtualAddressChecked(va + (ulong)offset);
  137. size = Math.Min(data.Length - offset, PageSize);
  138. GetPhysicalAddressSpan(pa, size).CopyTo(data.Slice(offset, size));
  139. }
  140. }
  141. public virtual T ReadTracked<T>(ulong va) where T : unmanaged
  142. {
  143. SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);
  144. return Read<T>(va);
  145. }
  146. public virtual void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
  147. {
  148. // No default implementation
  149. }
  150. public virtual void Write(ulong va, ReadOnlySpan<byte> data)
  151. {
  152. if (data.Length == 0)
  153. {
  154. return;
  155. }
  156. SignalMemoryTracking(va, (ulong)data.Length, true);
  157. WriteImpl(va, data);
  158. }
  159. public virtual void Write<T>(ulong va, T value) where T : unmanaged
  160. {
  161. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  162. }
  163. public virtual void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
  164. {
  165. if (data.Length == 0)
  166. {
  167. return;
  168. }
  169. WriteImpl(va, data);
  170. }
  171. public virtual bool WriteWithRedundancyCheck(ulong va, ReadOnlySpan<byte> data)
  172. {
  173. if (data.Length == 0)
  174. {
  175. return false;
  176. }
  177. if (IsContiguousAndMapped(va, data.Length))
  178. {
  179. SignalMemoryTracking(va, (ulong)data.Length, false);
  180. nuint pa = TranslateVirtualAddressChecked(va);
  181. var target = GetPhysicalAddressSpan(pa, data.Length);
  182. bool changed = !data.SequenceEqual(target);
  183. if (changed)
  184. {
  185. data.CopyTo(target);
  186. }
  187. return changed;
  188. }
  189. else
  190. {
  191. Write(va, data);
  192. return true;
  193. }
  194. }
  195. /// <summary>
  196. /// Ensures the combination of virtual address and size is part of the addressable space.
  197. /// </summary>
  198. /// <param name="va">Virtual address of the range</param>
  199. /// <param name="size">Size of the range in bytes</param>
  200. /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
  201. protected void AssertValidAddressAndSize(ulong va, ulong size)
  202. {
  203. if (!ValidateAddressAndSize(va, size))
  204. {
  205. throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
  206. }
  207. }
  208. /// <summary>
  209. /// Ensures the combination of virtual address and size is part of the addressable space.
  210. /// </summary>
  211. /// <param name="va">Virtual address of the range</param>
  212. /// <param name="size">Size of the range in bytes</param>
  213. /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
  214. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  215. protected void AssertValidAddressAndSize(ulong va, int size)
  216. => AssertValidAddressAndSize(va, (ulong)size);
  217. /// <summary>
  218. /// Computes the number of pages in a virtual address range.
  219. /// </summary>
  220. /// <param name="va">Virtual address of the range</param>
  221. /// <param name="size">Size of the range</param>
  222. /// <param name="startVa">The virtual address of the beginning of the first page</param>
  223. /// <remarks>This function does not differentiate between allocated and unallocated pages.</remarks>
  224. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  225. protected static int GetPagesCount(ulong va, ulong size, out ulong startVa)
  226. {
  227. // WARNING: Always check if ulong does not overflow during the operations.
  228. startVa = va & ~(ulong)PageMask;
  229. ulong vaSpan = (va - startVa + size + PageMask) & ~(ulong)PageMask;
  230. return (int)(vaSpan / PageSize);
  231. }
  232. protected abstract Memory<byte> GetPhysicalAddressMemory(nuint pa, int size);
  233. protected abstract Span<byte> GetPhysicalAddressSpan(nuint pa, int size);
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. protected bool IsContiguous(ulong va, int size) => IsContiguous(va, (ulong)size);
  236. protected virtual bool IsContiguous(ulong va, ulong size)
  237. {
  238. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
  239. {
  240. return false;
  241. }
  242. int pages = GetPagesCount(va, size, out va);
  243. for (int page = 0; page < pages - 1; page++)
  244. {
  245. if (!ValidateAddress(va + PageSize))
  246. {
  247. return false;
  248. }
  249. if (TranslateVirtualAddressUnchecked(va) + PageSize != TranslateVirtualAddressUnchecked(va + PageSize))
  250. {
  251. return false;
  252. }
  253. va += PageSize;
  254. }
  255. return true;
  256. }
  257. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  258. protected bool IsContiguousAndMapped(ulong va, int size)
  259. => IsContiguous(va, size) && IsMapped(va);
  260. protected abstract nuint TranslateVirtualAddressChecked(ulong va);
  261. protected abstract nuint TranslateVirtualAddressUnchecked(ulong va);
  262. /// <summary>
  263. /// Checks if the virtual address is part of the addressable space.
  264. /// </summary>
  265. /// <param name="va">Virtual address</param>
  266. /// <returns>True if the virtual address is part of the addressable space</returns>
  267. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  268. protected bool ValidateAddress(ulong va)
  269. {
  270. return va < AddressSpaceSize;
  271. }
  272. /// <summary>
  273. /// Checks if the combination of virtual address and size is part of the addressable space.
  274. /// </summary>
  275. /// <param name="va">Virtual address of the range</param>
  276. /// <param name="size">Size of the range in bytes</param>
  277. /// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
  278. protected bool ValidateAddressAndSize(ulong va, ulong size)
  279. {
  280. ulong endVa = va + size;
  281. return endVa >= va && endVa >= size && endVa <= AddressSpaceSize;
  282. }
  283. protected static void ThrowInvalidMemoryRegionException(string message)
  284. => throw new InvalidMemoryRegionException(message);
  285. protected static void ThrowMemoryNotContiguous()
  286. => throw new MemoryNotContiguousException();
  287. protected virtual void WriteImpl(ulong va, ReadOnlySpan<byte> data)
  288. {
  289. AssertValidAddressAndSize(va, data.Length);
  290. if (IsContiguousAndMapped(va, data.Length))
  291. {
  292. nuint pa = TranslateVirtualAddressUnchecked(va);
  293. data.CopyTo(GetPhysicalAddressSpan(pa, data.Length));
  294. }
  295. else
  296. {
  297. int offset = 0, size;
  298. if ((va & PageMask) != 0)
  299. {
  300. nuint pa = TranslateVirtualAddressChecked(va);
  301. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  302. data[..size].CopyTo(GetPhysicalAddressSpan(pa, size));
  303. offset += size;
  304. }
  305. for (; offset < data.Length; offset += size)
  306. {
  307. nuint pa = TranslateVirtualAddressChecked(va + (ulong)offset);
  308. size = Math.Min(data.Length - offset, PageSize);
  309. data.Slice(offset, size).CopyTo(GetPhysicalAddressSpan(pa, size));
  310. }
  311. }
  312. }
  313. }
  314. }