AddressSpaceManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using Ryujinx.Memory.Range;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Memory
  8. {
  9. /// <summary>
  10. /// Represents a address space manager.
  11. /// Supports virtual memory region mapping, address translation and read/write access to mapped regions.
  12. /// </summary>
  13. public sealed class AddressSpaceManager : IVirtualMemoryManager, IWritableBlock
  14. {
  15. public const int PageBits = PageTable<nuint>.PageBits;
  16. public const int PageSize = PageTable<nuint>.PageSize;
  17. public const int PageMask = PageTable<nuint>.PageMask;
  18. /// <summary>
  19. /// Address space width in bits.
  20. /// </summary>
  21. public int AddressSpaceBits { get; }
  22. private readonly ulong _addressSpaceSize;
  23. private readonly PageTable<nuint> _pageTable;
  24. /// <summary>
  25. /// Creates a new instance of the memory manager.
  26. /// </summary>
  27. /// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
  28. /// <param name="addressSpaceSize">Size of the address space</param>
  29. public AddressSpaceManager(ulong addressSpaceSize)
  30. {
  31. ulong asSize = PageSize;
  32. int asBits = PageBits;
  33. while (asSize < addressSpaceSize)
  34. {
  35. asSize <<= 1;
  36. asBits++;
  37. }
  38. AddressSpaceBits = asBits;
  39. _addressSpaceSize = asSize;
  40. _pageTable = new PageTable<nuint>();
  41. }
  42. /// <summary>
  43. /// Maps a virtual memory range into a physical memory range.
  44. /// </summary>
  45. /// <remarks>
  46. /// Addresses and size must be page aligned.
  47. /// </remarks>
  48. /// <param name="va">Virtual memory address</param>
  49. /// <param name="hostAddress">Physical memory address</param>
  50. /// <param name="size">Size to be mapped</param>
  51. public void Map(ulong va, nuint hostAddress, ulong size)
  52. {
  53. AssertValidAddressAndSize(va, size);
  54. while (size != 0)
  55. {
  56. _pageTable.Map(va, hostAddress);
  57. va += PageSize;
  58. hostAddress += PageSize;
  59. size -= PageSize;
  60. }
  61. }
  62. /// <summary>
  63. /// Unmaps a previously mapped range of virtual memory.
  64. /// </summary>
  65. /// <param name="va">Virtual address of the range to be unmapped</param>
  66. /// <param name="size">Size of the range to be unmapped</param>
  67. public void Unmap(ulong va, ulong size)
  68. {
  69. AssertValidAddressAndSize(va, size);
  70. while (size != 0)
  71. {
  72. _pageTable.Unmap(va);
  73. va += PageSize;
  74. size -= PageSize;
  75. }
  76. }
  77. /// <summary>
  78. /// Reads data from mapped memory.
  79. /// </summary>
  80. /// <typeparam name="T">Type of the data being read</typeparam>
  81. /// <param name="va">Virtual address of the data in memory</param>
  82. /// <returns>The data</returns>
  83. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  84. public T Read<T>(ulong va) where T : unmanaged
  85. {
  86. return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
  87. }
  88. /// <summary>
  89. /// Reads data from mapped memory.
  90. /// </summary>
  91. /// <param name="va">Virtual address of the data in memory</param>
  92. /// <param name="data">Span to store the data being read into</param>
  93. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  94. public void Read(ulong va, Span<byte> data)
  95. {
  96. ReadImpl(va, data);
  97. }
  98. /// <summary>
  99. /// Writes data to mapped memory.
  100. /// </summary>
  101. /// <typeparam name="T">Type of the data being written</typeparam>
  102. /// <param name="va">Virtual address to write the data into</param>
  103. /// <param name="value">Data to be written</param>
  104. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  105. public void Write<T>(ulong va, T value) where T : unmanaged
  106. {
  107. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  108. }
  109. /// <summary>
  110. /// Writes data to mapped memory.
  111. /// </summary>
  112. /// <param name="va">Virtual address to write the data into</param>
  113. /// <param name="data">Data to be written</param>
  114. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  115. public void Write(ulong va, ReadOnlySpan<byte> data)
  116. {
  117. if (data.Length == 0)
  118. {
  119. return;
  120. }
  121. AssertValidAddressAndSize(va, (ulong)data.Length);
  122. if (IsContiguousAndMapped(va, data.Length))
  123. {
  124. data.CopyTo(GetHostSpanContiguous(va, data.Length));
  125. }
  126. else
  127. {
  128. int offset = 0, size;
  129. if ((va & PageMask) != 0)
  130. {
  131. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  132. data.Slice(0, size).CopyTo(GetHostSpanContiguous(va, size));
  133. offset += size;
  134. }
  135. for (; offset < data.Length; offset += size)
  136. {
  137. size = Math.Min(data.Length - offset, PageSize);
  138. data.Slice(offset, size).CopyTo(GetHostSpanContiguous(va + (ulong)offset, size));
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// Gets a read-only span of data from mapped memory.
  144. /// </summary>
  145. /// <remarks>
  146. /// This may perform a allocation if the data is not contiguous in memory.
  147. /// For this reason, the span is read-only, you can't modify the data.
  148. /// </remarks>
  149. /// <param name="va">Virtual address of the data</param>
  150. /// <param name="size">Size of the data</param>
  151. /// <param name="tracked">True if read tracking is triggered on the span</param>
  152. /// <returns>A read-only span of the data</returns>
  153. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  154. public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
  155. {
  156. if (size == 0)
  157. {
  158. return ReadOnlySpan<byte>.Empty;
  159. }
  160. if (IsContiguousAndMapped(va, size))
  161. {
  162. return GetHostSpanContiguous(va, size);
  163. }
  164. else
  165. {
  166. Span<byte> data = new byte[size];
  167. ReadImpl(va, data);
  168. return data;
  169. }
  170. }
  171. /// <summary>
  172. /// Gets a region of memory that can be written to.
  173. /// </summary>
  174. /// <remarks>
  175. /// If the requested region is not contiguous in physical memory,
  176. /// this will perform an allocation, and flush the data (writing it
  177. /// back to the backing memory) on disposal.
  178. /// </remarks>
  179. /// <param name="va">Virtual address of the data</param>
  180. /// <param name="size">Size of the data</param>
  181. /// <returns>A writable region of memory containing the data</returns>
  182. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  183. public unsafe WritableRegion GetWritableRegion(ulong va, int size)
  184. {
  185. if (size == 0)
  186. {
  187. return new WritableRegion(null, va, Memory<byte>.Empty);
  188. }
  189. if (IsContiguousAndMapped(va, size))
  190. {
  191. return new WritableRegion(null, va, new NativeMemoryManager<byte>((byte*)GetHostAddress(va), size).Memory);
  192. }
  193. else
  194. {
  195. Memory<byte> memory = new byte[size];
  196. GetSpan(va, size).CopyTo(memory.Span);
  197. return new WritableRegion(this, va, memory);
  198. }
  199. }
  200. /// <summary>
  201. /// Gets a reference for the given type at the specified virtual memory address.
  202. /// </summary>
  203. /// <remarks>
  204. /// The data must be located at a contiguous memory region.
  205. /// </remarks>
  206. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  207. /// <param name="va">Virtual address of the data</param>
  208. /// <returns>A reference to the data in memory</returns>
  209. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  210. public unsafe ref T GetRef<T>(ulong va) where T : unmanaged
  211. {
  212. if (!IsContiguous(va, Unsafe.SizeOf<T>()))
  213. {
  214. ThrowMemoryNotContiguous();
  215. }
  216. return ref *(T*)GetHostAddress(va);
  217. }
  218. /// <summary>
  219. /// Computes the number of pages in a virtual address range.
  220. /// </summary>
  221. /// <param name="va">Virtual address of the range</param>
  222. /// <param name="size">Size of the range</param>
  223. /// <param name="startVa">The virtual address of the beginning of the first page</param>
  224. /// <remarks>This function does not differentiate between allocated and unallocated pages.</remarks>
  225. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  226. private int GetPagesCount(ulong va, uint size, out ulong startVa)
  227. {
  228. // WARNING: Always check if ulong does not overflow during the operations.
  229. startVa = va & ~(ulong)PageMask;
  230. ulong vaSpan = (va - startVa + size + PageMask) & ~(ulong)PageMask;
  231. return (int)(vaSpan / PageSize);
  232. }
  233. private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
  236. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  237. private bool IsContiguous(ulong va, int size)
  238. {
  239. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, (ulong)size))
  240. {
  241. return false;
  242. }
  243. int pages = GetPagesCount(va, (uint)size, out va);
  244. for (int page = 0; page < pages - 1; page++)
  245. {
  246. if (!ValidateAddress(va + PageSize))
  247. {
  248. return false;
  249. }
  250. if (GetHostAddress(va) + PageSize != GetHostAddress(va + PageSize))
  251. {
  252. return false;
  253. }
  254. va += PageSize;
  255. }
  256. return true;
  257. }
  258. /// <summary>
  259. /// Gets the physical regions that make up the given virtual address region.
  260. /// If any part of the virtual region is unmapped, null is returned.
  261. /// </summary>
  262. /// <param name="va">Virtual address of the range</param>
  263. /// <param name="size">Size of the range</param>
  264. /// <returns>Array of physical regions</returns>
  265. public IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
  266. {
  267. if (size == 0)
  268. {
  269. return Enumerable.Empty<HostMemoryRange>();
  270. }
  271. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
  272. {
  273. return null;
  274. }
  275. int pages = GetPagesCount(va, (uint)size, out va);
  276. var regions = new List<HostMemoryRange>();
  277. nuint regionStart = GetHostAddress(va);
  278. ulong regionSize = PageSize;
  279. for (int page = 0; page < pages - 1; page++)
  280. {
  281. if (!ValidateAddress(va + PageSize))
  282. {
  283. return null;
  284. }
  285. nuint newHostAddress = GetHostAddress(va + PageSize);
  286. if (GetHostAddress(va) + PageSize != newHostAddress)
  287. {
  288. regions.Add(new HostMemoryRange(regionStart, regionSize));
  289. regionStart = newHostAddress;
  290. regionSize = 0;
  291. }
  292. va += PageSize;
  293. regionSize += PageSize;
  294. }
  295. regions.Add(new HostMemoryRange(regionStart, regionSize));
  296. return regions;
  297. }
  298. private void ReadImpl(ulong va, Span<byte> data)
  299. {
  300. if (data.Length == 0)
  301. {
  302. return;
  303. }
  304. AssertValidAddressAndSize(va, (ulong)data.Length);
  305. int offset = 0, size;
  306. if ((va & PageMask) != 0)
  307. {
  308. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  309. GetHostSpanContiguous(va, size).CopyTo(data.Slice(0, size));
  310. offset += size;
  311. }
  312. for (; offset < data.Length; offset += size)
  313. {
  314. size = Math.Min(data.Length - offset, PageSize);
  315. GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
  316. }
  317. }
  318. /// <summary>
  319. /// Checks if the page at a given virtual address is mapped.
  320. /// </summary>
  321. /// <param name="va">Virtual address to check</param>
  322. /// <returns>True if the address is mapped, false otherwise</returns>
  323. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  324. public bool IsMapped(ulong va)
  325. {
  326. if (!ValidateAddress(va))
  327. {
  328. return false;
  329. }
  330. return _pageTable.Read(va) != 0;
  331. }
  332. /// <summary>
  333. /// Checks if a memory range is mapped.
  334. /// </summary>
  335. /// <param name="va">Virtual address of the range</param>
  336. /// <param name="size">Size of the range in bytes</param>
  337. /// <returns>True if the entire range is mapped, false otherwise</returns>
  338. public bool IsRangeMapped(ulong va, ulong size)
  339. {
  340. if (size == 0UL)
  341. {
  342. return true;
  343. }
  344. if (!ValidateAddressAndSize(va, size))
  345. {
  346. return false;
  347. }
  348. int pages = GetPagesCount(va, (uint)size, out va);
  349. for (int page = 0; page < pages; page++)
  350. {
  351. if (!IsMapped(va))
  352. {
  353. return false;
  354. }
  355. va += PageSize;
  356. }
  357. return true;
  358. }
  359. private bool ValidateAddress(ulong va)
  360. {
  361. return va < _addressSpaceSize;
  362. }
  363. /// <summary>
  364. /// Checks if the combination of virtual address and size is part of the addressable space.
  365. /// </summary>
  366. /// <param name="va">Virtual address of the range</param>
  367. /// <param name="size">Size of the range in bytes</param>
  368. /// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
  369. private bool ValidateAddressAndSize(ulong va, ulong size)
  370. {
  371. ulong endVa = va + size;
  372. return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
  373. }
  374. /// <summary>
  375. /// Ensures the combination of virtual address and size is part of the addressable space.
  376. /// </summary>
  377. /// <param name="va">Virtual address of the range</param>
  378. /// <param name="size">Size of the range in bytes</param>
  379. /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
  380. private void AssertValidAddressAndSize(ulong va, ulong size)
  381. {
  382. if (!ValidateAddressAndSize(va, size))
  383. {
  384. throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
  385. }
  386. }
  387. private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
  388. {
  389. return new Span<byte>((void*)GetHostAddress(va), size);
  390. }
  391. private nuint GetHostAddress(ulong va)
  392. {
  393. return _pageTable.Read(va) + (nuint)(va & PageMask);
  394. }
  395. /// <summary>
  396. /// Reprotect a region of virtual memory for tracking. Sets software protection bits.
  397. /// </summary>
  398. /// <param name="va">Virtual address base</param>
  399. /// <param name="size">Size of the region to protect</param>
  400. /// <param name="protection">Memory protection to set</param>
  401. public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
  402. {
  403. throw new NotImplementedException();
  404. }
  405. public void SignalMemoryTracking(ulong va, ulong size, bool write)
  406. {
  407. // Only the ARM Memory Manager has tracking for now.
  408. }
  409. }
  410. }