MemoryManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using ARMeilleure.Memory;
  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.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Threading;
  12. namespace Ryujinx.Cpu.Jit
  13. {
  14. /// <summary>
  15. /// Represents a CPU memory manager.
  16. /// </summary>
  17. public sealed class MemoryManager : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
  18. {
  19. public const int PageBits = 12;
  20. public const int PageSize = 1 << PageBits;
  21. public const int PageMask = PageSize - 1;
  22. private const int PteSize = 8;
  23. private const int PointerTagBit = 62;
  24. private readonly MemoryBlock _backingMemory;
  25. private readonly InvalidAccessHandler _invalidAccessHandler;
  26. /// <inheritdoc/>
  27. public bool Supports4KBPages => true;
  28. /// <summary>
  29. /// Address space width in bits.
  30. /// </summary>
  31. public int AddressSpaceBits { get; }
  32. private readonly ulong _addressSpaceSize;
  33. private readonly MemoryBlock _pageTable;
  34. /// <summary>
  35. /// Page table base pointer.
  36. /// </summary>
  37. public IntPtr PageTablePointer => _pageTable.Pointer;
  38. public MemoryManagerType Type => MemoryManagerType.SoftwarePageTable;
  39. public MemoryTracking Tracking { get; }
  40. public event Action<ulong, ulong> UnmapEvent;
  41. /// <summary>
  42. /// Creates a new instance of the memory manager.
  43. /// </summary>
  44. /// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
  45. /// <param name="addressSpaceSize">Size of the address space</param>
  46. /// <param name="invalidAccessHandler">Optional function to handle invalid memory accesses</param>
  47. public MemoryManager(MemoryBlock backingMemory, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler = null)
  48. {
  49. _backingMemory = backingMemory;
  50. _invalidAccessHandler = invalidAccessHandler;
  51. ulong asSize = PageSize;
  52. int asBits = PageBits;
  53. while (asSize < addressSpaceSize)
  54. {
  55. asSize <<= 1;
  56. asBits++;
  57. }
  58. AddressSpaceBits = asBits;
  59. _addressSpaceSize = asSize;
  60. _pageTable = new MemoryBlock((asSize / PageSize) * PteSize);
  61. Tracking = new MemoryTracking(this, PageSize);
  62. }
  63. /// <inheritdoc/>
  64. public void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags)
  65. {
  66. AssertValidAddressAndSize(va, size);
  67. ulong remainingSize = size;
  68. ulong oVa = va;
  69. ulong oPa = pa;
  70. while (remainingSize != 0)
  71. {
  72. _pageTable.Write((va / PageSize) * PteSize, PaToPte(pa));
  73. va += PageSize;
  74. pa += PageSize;
  75. remainingSize -= PageSize;
  76. }
  77. Tracking.Map(oVa, size);
  78. }
  79. /// <inheritdoc/>
  80. public void MapForeign(ulong va, nuint hostPointer, ulong size)
  81. {
  82. throw new NotSupportedException();
  83. }
  84. /// <inheritdoc/>
  85. public void Unmap(ulong va, ulong size)
  86. {
  87. // If size is 0, there's nothing to unmap, just exit early.
  88. if (size == 0)
  89. {
  90. return;
  91. }
  92. AssertValidAddressAndSize(va, size);
  93. UnmapEvent?.Invoke(va, size);
  94. Tracking.Unmap(va, size);
  95. ulong remainingSize = size;
  96. while (remainingSize != 0)
  97. {
  98. _pageTable.Write((va / PageSize) * PteSize, 0UL);
  99. va += PageSize;
  100. remainingSize -= PageSize;
  101. }
  102. }
  103. /// <inheritdoc/>
  104. public T Read<T>(ulong va) where T : unmanaged
  105. {
  106. return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
  107. }
  108. /// <inheritdoc/>
  109. public T ReadTracked<T>(ulong va) where T : unmanaged
  110. {
  111. try
  112. {
  113. SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);
  114. return Read<T>(va);
  115. }
  116. catch (InvalidMemoryRegionException)
  117. {
  118. if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
  119. {
  120. throw;
  121. }
  122. return default;
  123. }
  124. }
  125. /// <inheritdoc/>
  126. public void Read(ulong va, Span<byte> data)
  127. {
  128. ReadImpl(va, data);
  129. }
  130. /// <inheritdoc/>
  131. public void Write<T>(ulong va, T value) where T : unmanaged
  132. {
  133. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  134. }
  135. /// <inheritdoc/>
  136. public void Write(ulong va, ReadOnlySpan<byte> data)
  137. {
  138. if (data.Length == 0)
  139. {
  140. return;
  141. }
  142. SignalMemoryTracking(va, (ulong)data.Length, true);
  143. WriteImpl(va, data);
  144. }
  145. /// <inheritdoc/>
  146. public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
  147. {
  148. if (data.Length == 0)
  149. {
  150. return;
  151. }
  152. WriteImpl(va, data);
  153. }
  154. /// <inheritdoc/>
  155. public bool WriteWithRedundancyCheck(ulong va, ReadOnlySpan<byte> data)
  156. {
  157. if (data.Length == 0)
  158. {
  159. return false;
  160. }
  161. SignalMemoryTracking(va, (ulong)data.Length, false);
  162. if (IsContiguousAndMapped(va, data.Length))
  163. {
  164. var target = _backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length);
  165. bool changed = !data.SequenceEqual(target);
  166. if (changed)
  167. {
  168. data.CopyTo(target);
  169. }
  170. return changed;
  171. }
  172. else
  173. {
  174. WriteImpl(va, data);
  175. return true;
  176. }
  177. }
  178. /// <summary>
  179. /// Writes data to CPU mapped memory.
  180. /// </summary>
  181. /// <param name="va">Virtual address to write the data into</param>
  182. /// <param name="data">Data to be written</param>
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. private void WriteImpl(ulong va, ReadOnlySpan<byte> data)
  185. {
  186. try
  187. {
  188. AssertValidAddressAndSize(va, (ulong)data.Length);
  189. if (IsContiguousAndMapped(va, data.Length))
  190. {
  191. data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
  192. }
  193. else
  194. {
  195. int offset = 0, size;
  196. if ((va & PageMask) != 0)
  197. {
  198. ulong pa = GetPhysicalAddressInternal(va);
  199. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  200. data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size));
  201. offset += size;
  202. }
  203. for (; offset < data.Length; offset += size)
  204. {
  205. ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
  206. size = Math.Min(data.Length - offset, PageSize);
  207. data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size));
  208. }
  209. }
  210. }
  211. catch (InvalidMemoryRegionException)
  212. {
  213. if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
  214. {
  215. throw;
  216. }
  217. }
  218. }
  219. /// <inheritdoc/>
  220. public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
  221. {
  222. if (size == 0)
  223. {
  224. return ReadOnlySpan<byte>.Empty;
  225. }
  226. if (tracked)
  227. {
  228. SignalMemoryTracking(va, (ulong)size, false);
  229. }
  230. if (IsContiguousAndMapped(va, size))
  231. {
  232. return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
  233. }
  234. else
  235. {
  236. Span<byte> data = new byte[size];
  237. ReadImpl(va, data);
  238. return data;
  239. }
  240. }
  241. /// <inheritdoc/>
  242. public unsafe WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
  243. {
  244. if (size == 0)
  245. {
  246. return new WritableRegion(null, va, Memory<byte>.Empty);
  247. }
  248. if (IsContiguousAndMapped(va, size))
  249. {
  250. if (tracked)
  251. {
  252. SignalMemoryTracking(va, (ulong)size, true);
  253. }
  254. return new WritableRegion(null, va, _backingMemory.GetMemory(GetPhysicalAddressInternal(va), size));
  255. }
  256. else
  257. {
  258. Memory<byte> memory = new byte[size];
  259. GetSpan(va, size).CopyTo(memory.Span);
  260. return new WritableRegion(this, va, memory, tracked);
  261. }
  262. }
  263. /// <inheritdoc/>
  264. public ref T GetRef<T>(ulong va) where T : unmanaged
  265. {
  266. if (!IsContiguous(va, Unsafe.SizeOf<T>()))
  267. {
  268. ThrowMemoryNotContiguous();
  269. }
  270. SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), true);
  271. return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
  272. }
  273. /// <summary>
  274. /// Computes the number of pages in a virtual address range.
  275. /// </summary>
  276. /// <param name="va">Virtual address of the range</param>
  277. /// <param name="size">Size of the range</param>
  278. /// <param name="startVa">The virtual address of the beginning of the first page</param>
  279. /// <remarks>This function does not differentiate between allocated and unallocated pages.</remarks>
  280. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  281. private int GetPagesCount(ulong va, uint size, out ulong startVa)
  282. {
  283. // WARNING: Always check if ulong does not overflow during the operations.
  284. startVa = va & ~(ulong)PageMask;
  285. ulong vaSpan = (va - startVa + size + PageMask) & ~(ulong)PageMask;
  286. return (int)(vaSpan / PageSize);
  287. }
  288. private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
  289. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  290. private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
  291. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  292. private bool IsContiguous(ulong va, int size)
  293. {
  294. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, (ulong)size))
  295. {
  296. return false;
  297. }
  298. int pages = GetPagesCount(va, (uint)size, out va);
  299. for (int page = 0; page < pages - 1; page++)
  300. {
  301. if (!ValidateAddress(va + PageSize))
  302. {
  303. return false;
  304. }
  305. if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
  306. {
  307. return false;
  308. }
  309. va += PageSize;
  310. }
  311. return true;
  312. }
  313. /// <inheritdoc/>
  314. public IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size)
  315. {
  316. if (size == 0)
  317. {
  318. return Enumerable.Empty<HostMemoryRange>();
  319. }
  320. var guestRegions = GetPhysicalRegionsImpl(va, size);
  321. if (guestRegions == null)
  322. {
  323. return null;
  324. }
  325. var regions = new HostMemoryRange[guestRegions.Count];
  326. for (int i = 0; i < regions.Length; i++)
  327. {
  328. var guestRegion = guestRegions[i];
  329. IntPtr pointer = _backingMemory.GetPointer(guestRegion.Address, guestRegion.Size);
  330. regions[i] = new HostMemoryRange((nuint)(ulong)pointer, guestRegion.Size);
  331. }
  332. return regions;
  333. }
  334. /// <inheritdoc/>
  335. public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
  336. {
  337. if (size == 0)
  338. {
  339. return Enumerable.Empty<MemoryRange>();
  340. }
  341. return GetPhysicalRegionsImpl(va, size);
  342. }
  343. private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
  344. {
  345. if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
  346. {
  347. return null;
  348. }
  349. int pages = GetPagesCount(va, (uint)size, out va);
  350. var regions = new List<MemoryRange>();
  351. ulong regionStart = GetPhysicalAddressInternal(va);
  352. ulong regionSize = PageSize;
  353. for (int page = 0; page < pages - 1; page++)
  354. {
  355. if (!ValidateAddress(va + PageSize))
  356. {
  357. return null;
  358. }
  359. ulong newPa = GetPhysicalAddressInternal(va + PageSize);
  360. if (GetPhysicalAddressInternal(va) + PageSize != newPa)
  361. {
  362. regions.Add(new MemoryRange(regionStart, regionSize));
  363. regionStart = newPa;
  364. regionSize = 0;
  365. }
  366. va += PageSize;
  367. regionSize += PageSize;
  368. }
  369. regions.Add(new MemoryRange(regionStart, regionSize));
  370. return regions;
  371. }
  372. private void ReadImpl(ulong va, Span<byte> data)
  373. {
  374. if (data.Length == 0)
  375. {
  376. return;
  377. }
  378. try
  379. {
  380. AssertValidAddressAndSize(va, (ulong)data.Length);
  381. int offset = 0, size;
  382. if ((va & PageMask) != 0)
  383. {
  384. ulong pa = GetPhysicalAddressInternal(va);
  385. size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
  386. _backingMemory.GetSpan(pa, size).CopyTo(data.Slice(0, size));
  387. offset += size;
  388. }
  389. for (; offset < data.Length; offset += size)
  390. {
  391. ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
  392. size = Math.Min(data.Length - offset, PageSize);
  393. _backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
  394. }
  395. }
  396. catch (InvalidMemoryRegionException)
  397. {
  398. if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
  399. {
  400. throw;
  401. }
  402. }
  403. }
  404. /// <inheritdoc/>
  405. public bool IsRangeMapped(ulong va, ulong size)
  406. {
  407. if (size == 0UL)
  408. {
  409. return true;
  410. }
  411. if (!ValidateAddressAndSize(va, size))
  412. {
  413. return false;
  414. }
  415. int pages = GetPagesCount(va, (uint)size, out va);
  416. for (int page = 0; page < pages; page++)
  417. {
  418. if (!IsMapped(va))
  419. {
  420. return false;
  421. }
  422. va += PageSize;
  423. }
  424. return true;
  425. }
  426. /// <inheritdoc/>
  427. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  428. public bool IsMapped(ulong va)
  429. {
  430. if (!ValidateAddress(va))
  431. {
  432. return false;
  433. }
  434. return _pageTable.Read<ulong>((va / PageSize) * PteSize) != 0;
  435. }
  436. private bool ValidateAddress(ulong va)
  437. {
  438. return va < _addressSpaceSize;
  439. }
  440. /// <summary>
  441. /// Checks if the combination of virtual address and size is part of the addressable space.
  442. /// </summary>
  443. /// <param name="va">Virtual address of the range</param>
  444. /// <param name="size">Size of the range in bytes</param>
  445. /// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
  446. private bool ValidateAddressAndSize(ulong va, ulong size)
  447. {
  448. ulong endVa = va + size;
  449. return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
  450. }
  451. /// <summary>
  452. /// Ensures the combination of virtual address and size is part of the addressable space.
  453. /// </summary>
  454. /// <param name="va">Virtual address of the range</param>
  455. /// <param name="size">Size of the range in bytes</param>
  456. /// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
  457. private void AssertValidAddressAndSize(ulong va, ulong size)
  458. {
  459. if (!ValidateAddressAndSize(va, size))
  460. {
  461. throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
  462. }
  463. }
  464. private ulong GetPhysicalAddress(ulong va)
  465. {
  466. // We return -1L if the virtual address is invalid or unmapped.
  467. if (!ValidateAddress(va) || !IsMapped(va))
  468. {
  469. return ulong.MaxValue;
  470. }
  471. return GetPhysicalAddressInternal(va);
  472. }
  473. private ulong GetPhysicalAddressInternal(ulong va)
  474. {
  475. return PteToPa(_pageTable.Read<ulong>((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask);
  476. }
  477. /// <inheritdoc/>
  478. public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
  479. {
  480. AssertValidAddressAndSize(va, size);
  481. // Protection is inverted on software pages, since the default value is 0.
  482. protection = (~protection) & MemoryPermission.ReadAndWrite;
  483. long tag = protection switch
  484. {
  485. MemoryPermission.None => 0L,
  486. MemoryPermission.Write => 2L << PointerTagBit,
  487. _ => 3L << PointerTagBit
  488. };
  489. int pages = GetPagesCount(va, (uint)size, out va);
  490. ulong pageStart = va >> PageBits;
  491. long invTagMask = ~(0xffffL << 48);
  492. for (int page = 0; page < pages; page++)
  493. {
  494. ref long pageRef = ref _pageTable.GetRef<long>(pageStart * PteSize);
  495. long pte;
  496. do
  497. {
  498. pte = Volatile.Read(ref pageRef);
  499. }
  500. while (pte != 0 && Interlocked.CompareExchange(ref pageRef, (pte & invTagMask) | tag, pte) != pte);
  501. pageStart++;
  502. }
  503. }
  504. /// <inheritdoc/>
  505. public CpuRegionHandle BeginTracking(ulong address, ulong size, int id)
  506. {
  507. return new CpuRegionHandle(Tracking.BeginTracking(address, size, id));
  508. }
  509. /// <inheritdoc/>
  510. public CpuMultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
  511. {
  512. return new CpuMultiRegionHandle(Tracking.BeginGranularTracking(address, size, handles, granularity, id));
  513. }
  514. /// <inheritdoc/>
  515. public CpuSmartMultiRegionHandle BeginSmartGranularTracking(ulong address, ulong size, ulong granularity, int id)
  516. {
  517. return new CpuSmartMultiRegionHandle(Tracking.BeginSmartGranularTracking(address, size, granularity, id));
  518. }
  519. /// <inheritdoc/>
  520. public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
  521. {
  522. AssertValidAddressAndSize(va, size);
  523. if (precise)
  524. {
  525. Tracking.VirtualMemoryEvent(va, size, write, precise: true, exemptId);
  526. return;
  527. }
  528. // We emulate guard pages for software memory access. This makes for an easy transition to
  529. // tracking using host guard pages in future, but also supporting platforms where this is not possible.
  530. // Write tag includes read protection, since we don't have any read actions that aren't performed before write too.
  531. long tag = (write ? 3L : 1L) << PointerTagBit;
  532. int pages = GetPagesCount(va, (uint)size, out _);
  533. ulong pageStart = va >> PageBits;
  534. for (int page = 0; page < pages; page++)
  535. {
  536. ref long pageRef = ref _pageTable.GetRef<long>(pageStart * PteSize);
  537. long pte;
  538. pte = Volatile.Read(ref pageRef);
  539. if ((pte & tag) != 0)
  540. {
  541. Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
  542. break;
  543. }
  544. pageStart++;
  545. }
  546. }
  547. private ulong PaToPte(ulong pa)
  548. {
  549. return (ulong)_backingMemory.GetPointer(pa, PageSize);
  550. }
  551. private ulong PteToPa(ulong pte)
  552. {
  553. return (ulong)((long)pte - _backingMemory.Pointer.ToInt64());
  554. }
  555. /// <summary>
  556. /// Disposes of resources used by the memory manager.
  557. /// </summary>
  558. protected override void Destroy() => _pageTable.Dispose();
  559. private void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
  560. }
  561. }