AddressSpaceManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /// <param name="tracked">True if write tracking is triggered on the span</param>
  182. /// <returns>A writable region of memory containing the data</returns>
  183. /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
  184. public unsafe WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
  185. {
  186. if (size == 0)
  187. {
  188. return new WritableRegion(null, va, Memory<byte>.Empty);
  189. }
  190. if (IsContiguousAndMapped(va, size))
  191. {
  192. return new WritableRegion(null, va, new NativeMemoryManager<byte>((byte*)GetHostAddress(va), size).Memory);
  193. }
  194. else
  195. {
  196. Memory<byte> memory = new byte[size];
  197. GetSpan(va, size).CopyTo(memory.Span);
  198. return new WritableRegion(this, va, memory);
  199. }
  200. }
  201. /// <summary>
  202. /// Gets a reference for the given type at the specified virtual memory address.
  203. /// </summary>
  204. /// <remarks>
  205. /// The data must be located at a contiguous memory region.
  206. /// </remarks>
  207. /// <typeparam name="T">Type of the data to get the reference</typeparam>
  208. /// <param name="va">Virtual address of the data</param>
  209. /// <returns>A reference to the data in memory</returns>
  210. /// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
  211. public unsafe ref T GetRef<T>(ulong va) where T : unmanaged
  212. {
  213. if (!IsContiguous(va, Unsafe.SizeOf<T>()))
  214. {
  215. ThrowMemoryNotContiguous();
  216. }
  217. return ref *(T*)GetHostAddress(va);
  218. }
  219. /// <summary>
  220. /// Computes the number of pages in a virtual address range.
  221. /// </summary>
  222. /// <param name="va">Virtual address of the range</param>
  223. /// <param name="size">Size of the range</param>
  224. /// <param name="startVa">The virtual address of the beginning of the first page</param>
  225. /// <remarks>This function does not differentiate between allocated and unallocated pages.</remarks>
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. private int GetPagesCount(ulong va, uint size, out ulong startVa)
  228. {
  229. // WARNING: Always check if ulong does not overflow during the operations.
  230. startVa = va & ~(ulong)PageMask;
  231. ulong vaSpan = (va - startVa + size + PageMask) & ~(ulong)PageMask;
  232. return (int)(vaSpan / PageSize);
  233. }
  234. private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
  235. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  236. private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
  237. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  238. private bool IsContiguous(ulong va, int size)
  239. {
  240. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, (ulong)size))
  241. {
  242. return false;
  243. }
  244. int pages = GetPagesCount(va, (uint)size, out va);
  245. for (int page = 0; page < pages - 1; page++)
  246. {
  247. if (!ValidateAddress(va + PageSize))
  248. {
  249. return false;
  250. }
  251. if (GetHostAddress(va) + PageSize != GetHostAddress(va + PageSize))
  252. {
  253. return false;
  254. }
  255. va += PageSize;
  256. }
  257. return true;
  258. }
  259. /// <summary>
  260. /// Gets the physical regions that make up the given virtual address region.
  261. /// If any part of the virtual region is unmapped, null is returned.
  262. /// </summary>
  263. /// <param name="va">Virtual address of the range</param>
  264. /// <param name="size">Size of the range</param>
  265. /// <returns>Array of physical regions</returns>
  266. public IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
  267. {
  268. if (size == 0)
  269. {
  270. return Enumerable.Empty<HostMemoryRange>();
  271. }
  272. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
  273. {
  274. return null;
  275. }
  276. int pages = GetPagesCount(va, (uint)size, out va);
  277. var regions = new List<HostMemoryRange>();
  278. nuint regionStart = GetHostAddress(va);
  279. ulong regionSize = PageSize;
  280. for (int page = 0; page < pages - 1; page++)
  281. {
  282. if (!ValidateAddress(va + PageSize))
  283. {
  284. return null;
  285. }
  286. nuint newHostAddress = GetHostAddress(va + PageSize);
  287. if (GetHostAddress(va) + PageSize != newHostAddress)
  288. {
  289. regions.Add(new HostMemoryRange(regionStart, regionSize));
  290. regionStart = newHostAddress;
  291. regionSize = 0;
  292. }
  293. va += PageSize;
  294. regionSize += PageSize;
  295. }
  296. regions.Add(new HostMemoryRange(regionStart, regionSize));
  297. return regions;
  298. }
  299. private void ReadImpl(ulong va, Span<byte> data)
  300. {
  301. if (data.Length == 0)
  302. {
  303. return;
  304. }
  305. AssertValidAddressAndSize(va, (ulong)data.Length);
  306. int offset = 0, size;
  307. if ((va & PageMask) != 0)
  308. {
  309. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  310. GetHostSpanContiguous(va, size).CopyTo(data.Slice(0, size));
  311. offset += size;
  312. }
  313. for (; offset < data.Length; offset += size)
  314. {
  315. size = Math.Min(data.Length - offset, PageSize);
  316. GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
  317. }
  318. }
  319. /// <summary>
  320. /// Checks if the page at a given virtual address is mapped.
  321. /// </summary>
  322. /// <param name="va">Virtual address to check</param>
  323. /// <returns>True if the address is mapped, false otherwise</returns>
  324. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  325. public bool IsMapped(ulong va)
  326. {
  327. if (!ValidateAddress(va))
  328. {
  329. return false;
  330. }
  331. return _pageTable.Read(va) != 0;
  332. }
  333. /// <summary>
  334. /// Checks if a memory range is mapped.
  335. /// </summary>
  336. /// <param name="va">Virtual address of the range</param>
  337. /// <param name="size">Size of the range in bytes</param>
  338. /// <returns>True if the entire range is mapped, false otherwise</returns>
  339. public bool IsRangeMapped(ulong va, ulong size)
  340. {
  341. if (size == 0UL)
  342. {
  343. return true;
  344. }
  345. if (!ValidateAddressAndSize(va, size))
  346. {
  347. return false;
  348. }
  349. int pages = GetPagesCount(va, (uint)size, out va);
  350. for (int page = 0; page < pages; page++)
  351. {
  352. if (!IsMapped(va))
  353. {
  354. return false;
  355. }
  356. va += PageSize;
  357. }
  358. return true;
  359. }
  360. private bool ValidateAddress(ulong va)
  361. {
  362. return va < _addressSpaceSize;
  363. }
  364. /// <summary>
  365. /// Checks if the combination of virtual address and size is part of the addressable space.
  366. /// </summary>
  367. /// <param name="va">Virtual address of the range</param>
  368. /// <param name="size">Size of the range in bytes</param>
  369. /// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
  370. private bool ValidateAddressAndSize(ulong va, ulong size)
  371. {
  372. ulong endVa = va + size;
  373. return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
  374. }
  375. /// <summary>
  376. /// Ensures the combination of virtual address and size is part of the addressable space.
  377. /// </summary>
  378. /// <param name="va">Virtual address of the range</param>
  379. /// <param name="size">Size of the range in bytes</param>
  380. /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
  381. private void AssertValidAddressAndSize(ulong va, ulong size)
  382. {
  383. if (!ValidateAddressAndSize(va, size))
  384. {
  385. throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
  386. }
  387. }
  388. private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
  389. {
  390. return new Span<byte>((void*)GetHostAddress(va), size);
  391. }
  392. private nuint GetHostAddress(ulong va)
  393. {
  394. return _pageTable.Read(va) + (nuint)(va & PageMask);
  395. }
  396. /// <summary>
  397. /// Reprotect a region of virtual memory for tracking. Sets software protection bits.
  398. /// </summary>
  399. /// <param name="va">Virtual address base</param>
  400. /// <param name="size">Size of the region to protect</param>
  401. /// <param name="protection">Memory protection to set</param>
  402. public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
  403. {
  404. throw new NotImplementedException();
  405. }
  406. public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false)
  407. {
  408. // Only the ARM Memory Manager has tracking for now.
  409. }
  410. }
  411. }