MemoryManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Memory;
  4. using Ryujinx.Memory.Range;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. namespace Ryujinx.Graphics.Gpu.Memory
  10. {
  11. /// <summary>
  12. /// GPU memory manager.
  13. /// </summary>
  14. public class MemoryManager : IWritableBlock
  15. {
  16. private const int PtLvl0Bits = 14;
  17. private const int PtLvl1Bits = 14;
  18. public const int PtPageBits = 12;
  19. private const ulong PtLvl0Size = 1UL << PtLvl0Bits;
  20. private const ulong PtLvl1Size = 1UL << PtLvl1Bits;
  21. public const ulong PageSize = 1UL << PtPageBits;
  22. private const ulong PtLvl0Mask = PtLvl0Size - 1;
  23. private const ulong PtLvl1Mask = PtLvl1Size - 1;
  24. public const ulong PageMask = PageSize - 1;
  25. private const int PtLvl0Bit = PtPageBits + PtLvl1Bits;
  26. private const int PtLvl1Bit = PtPageBits;
  27. private const int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
  28. public const ulong PteUnmapped = ulong.MaxValue;
  29. private readonly ulong[][] _pageTable;
  30. public event EventHandler<UnmapEventArgs> MemoryUnmapped;
  31. /// <summary>
  32. /// Physical memory where the virtual memory is mapped into.
  33. /// </summary>
  34. internal PhysicalMemory Physical { get; }
  35. /// <summary>
  36. /// Virtual range cache.
  37. /// </summary>
  38. internal VirtualRangeCache VirtualRangeCache { get; }
  39. /// <summary>
  40. /// Cache of GPU counters.
  41. /// </summary>
  42. internal CounterCache CounterCache { get; }
  43. /// <summary>
  44. /// Creates a new instance of the GPU memory manager.
  45. /// </summary>
  46. /// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
  47. /// <param name="cpuMemorySize">The amount of physical CPU Memory Avaiable on the device.</param>
  48. internal MemoryManager(PhysicalMemory physicalMemory, ulong cpuMemorySize)
  49. {
  50. Physical = physicalMemory;
  51. VirtualRangeCache = new VirtualRangeCache(this);
  52. CounterCache = new CounterCache();
  53. _pageTable = new ulong[PtLvl0Size][];
  54. MemoryUnmapped += Physical.TextureCache.MemoryUnmappedHandler;
  55. MemoryUnmapped += Physical.BufferCache.MemoryUnmappedHandler;
  56. MemoryUnmapped += VirtualRangeCache.MemoryUnmappedHandler;
  57. MemoryUnmapped += CounterCache.MemoryUnmappedHandler;
  58. Physical.TextureCache.Initialize(cpuMemorySize);
  59. }
  60. /// <summary>
  61. /// Reads data from GPU mapped memory.
  62. /// </summary>
  63. /// <typeparam name="T">Type of the data</typeparam>
  64. /// <param name="va">GPU virtual address where the data is located</param>
  65. /// <param name="tracked">True if read tracking is triggered on the memory region</param>
  66. /// <returns>The data at the specified memory location</returns>
  67. public T Read<T>(ulong va, bool tracked = false) where T : unmanaged
  68. {
  69. int size = Unsafe.SizeOf<T>();
  70. if (IsContiguous(va, size))
  71. {
  72. ulong address = Translate(va);
  73. if (tracked)
  74. {
  75. return Physical.ReadTracked<T>(address);
  76. }
  77. else
  78. {
  79. return Physical.Read<T>(address);
  80. }
  81. }
  82. else
  83. {
  84. Span<byte> data = new byte[size];
  85. ReadImpl(va, data, tracked);
  86. return MemoryMarshal.Cast<byte, T>(data)[0];
  87. }
  88. }
  89. /// <summary>
  90. /// Gets a read-only span of data from GPU mapped memory.
  91. /// </summary>
  92. /// <param name="va">GPU virtual address where the data is located</param>
  93. /// <param name="size">Size of the data</param>
  94. /// <param name="tracked">True if read tracking is triggered on the span</param>
  95. /// <returns>The span of the data at the specified memory location</returns>
  96. public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
  97. {
  98. if (IsContiguous(va, size))
  99. {
  100. return Physical.GetSpan(Translate(va), size, tracked);
  101. }
  102. else
  103. {
  104. Span<byte> data = new byte[size];
  105. ReadImpl(va, data, tracked);
  106. return data;
  107. }
  108. }
  109. /// <summary>
  110. /// Gets a read-only span of data from GPU mapped memory, up to the entire range specified,
  111. /// or the last mapped page if the range is not fully mapped.
  112. /// </summary>
  113. /// <param name="va">GPU virtual address where the data is located</param>
  114. /// <param name="size">Size of the data</param>
  115. /// <param name="tracked">True if read tracking is triggered on the span</param>
  116. /// <returns>The span of the data at the specified memory location</returns>
  117. public ReadOnlySpan<byte> GetSpanMapped(ulong va, int size, bool tracked = false)
  118. {
  119. bool isContiguous = true;
  120. int mappedSize;
  121. if (ValidateAddress(va) && GetPte(va) != PteUnmapped && Physical.IsMapped(Translate(va)))
  122. {
  123. ulong endVa = va + (ulong)size;
  124. ulong endVaAligned = (endVa + PageMask) & ~PageMask;
  125. ulong currentVa = va & ~PageMask;
  126. int pages = (int)((endVaAligned - currentVa) / PageSize);
  127. for (int page = 0; page < pages - 1; page++)
  128. {
  129. ulong nextVa = currentVa + PageSize;
  130. ulong nextPa = Translate(nextVa);
  131. if (!ValidateAddress(nextVa) || GetPte(nextVa) == PteUnmapped || !Physical.IsMapped(nextPa))
  132. {
  133. break;
  134. }
  135. if (Translate(currentVa) + PageSize != nextPa)
  136. {
  137. isContiguous = false;
  138. }
  139. currentVa += PageSize;
  140. }
  141. currentVa += PageSize;
  142. if (currentVa > endVa)
  143. {
  144. currentVa = endVa;
  145. }
  146. mappedSize = (int)(currentVa - va);
  147. }
  148. else
  149. {
  150. return ReadOnlySpan<byte>.Empty;
  151. }
  152. if (isContiguous)
  153. {
  154. return Physical.GetSpan(Translate(va), mappedSize, tracked);
  155. }
  156. else
  157. {
  158. Span<byte> data = new byte[mappedSize];
  159. ReadImpl(va, data, tracked);
  160. return data;
  161. }
  162. }
  163. /// <summary>
  164. /// Reads data from a possibly non-contiguous region of GPU mapped memory.
  165. /// </summary>
  166. /// <param name="va">GPU virtual address of the data</param>
  167. /// <param name="data">Span to write the read data into</param>
  168. /// <param name="tracked">True to enable write tracking on read, false otherwise</param>
  169. private void ReadImpl(ulong va, Span<byte> data, bool tracked)
  170. {
  171. if (data.Length == 0)
  172. {
  173. return;
  174. }
  175. int offset = 0, size;
  176. if ((va & PageMask) != 0)
  177. {
  178. ulong pa = Translate(va);
  179. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  180. Physical.GetSpan(pa, size, tracked).CopyTo(data[..size]);
  181. offset += size;
  182. }
  183. for (; offset < data.Length; offset += size)
  184. {
  185. ulong pa = Translate(va + (ulong)offset);
  186. size = Math.Min(data.Length - offset, (int)PageSize);
  187. Physical.GetSpan(pa, size, tracked).CopyTo(data.Slice(offset, size));
  188. }
  189. }
  190. /// <summary>
  191. /// Gets a writable region from GPU mapped memory.
  192. /// </summary>
  193. /// <param name="va">Start address of the range</param>
  194. /// <param name="size">Size in bytes to be range</param>
  195. /// <param name="tracked">True if write tracking is triggered on the span</param>
  196. /// <returns>A writable region with the data at the specified memory location</returns>
  197. public WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
  198. {
  199. if (IsContiguous(va, size))
  200. {
  201. return Physical.GetWritableRegion(Translate(va), size, tracked);
  202. }
  203. else
  204. {
  205. MemoryOwner<byte> memoryOwner = MemoryOwner<byte>.Rent(size);
  206. ReadImpl(va, memoryOwner.Span, tracked);
  207. return new WritableRegion(this, va, memoryOwner, tracked);
  208. }
  209. }
  210. /// <summary>
  211. /// Writes data to GPU mapped memory.
  212. /// </summary>
  213. /// <typeparam name="T">Type of the data</typeparam>
  214. /// <param name="va">GPU virtual address to write the value into</param>
  215. /// <param name="value">The value to be written</param>
  216. public void Write<T>(ulong va, T value) where T : unmanaged
  217. {
  218. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  219. }
  220. /// <summary>
  221. /// Writes data to GPU mapped memory.
  222. /// </summary>
  223. /// <param name="va">GPU virtual address to write the data into</param>
  224. /// <param name="data">The data to be written</param>
  225. public void Write(ulong va, ReadOnlySpan<byte> data)
  226. {
  227. WriteImpl(va, data, Physical.Write);
  228. }
  229. /// <summary>
  230. /// Writes data to GPU mapped memory, destined for a tracked resource.
  231. /// </summary>
  232. /// <param name="va">GPU virtual address to write the data into</param>
  233. /// <param name="data">The data to be written</param>
  234. public void WriteTrackedResource(ulong va, ReadOnlySpan<byte> data)
  235. {
  236. WriteImpl(va, data, Physical.WriteTrackedResource);
  237. }
  238. /// <summary>
  239. /// Writes data to GPU mapped memory without write tracking.
  240. /// </summary>
  241. /// <param name="va">GPU virtual address to write the data into</param>
  242. /// <param name="data">The data to be written</param>
  243. public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
  244. {
  245. WriteImpl(va, data, Physical.WriteUntracked);
  246. }
  247. private delegate void WriteCallback(ulong address, ReadOnlySpan<byte> data);
  248. /// <summary>
  249. /// Writes data to possibly non-contiguous GPU mapped memory.
  250. /// </summary>
  251. /// <param name="va">GPU virtual address of the region to write into</param>
  252. /// <param name="data">Data to be written</param>
  253. /// <param name="writeCallback">Write callback</param>
  254. private void WriteImpl(ulong va, ReadOnlySpan<byte> data, WriteCallback writeCallback)
  255. {
  256. if (IsContiguous(va, data.Length))
  257. {
  258. writeCallback(Translate(va), data);
  259. }
  260. else
  261. {
  262. int offset = 0, size;
  263. if ((va & PageMask) != 0)
  264. {
  265. ulong pa = Translate(va);
  266. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  267. writeCallback(pa, data[..size]);
  268. offset += size;
  269. }
  270. for (; offset < data.Length; offset += size)
  271. {
  272. ulong pa = Translate(va + (ulong)offset);
  273. size = Math.Min(data.Length - offset, (int)PageSize);
  274. writeCallback(pa, data.Slice(offset, size));
  275. }
  276. }
  277. }
  278. /// <summary>
  279. /// Runs remap actions that are added to an unmap event.
  280. /// These must run after the mapping completes.
  281. /// </summary>
  282. /// <param name="e">Event with remap actions</param>
  283. private static void RunRemapActions(UnmapEventArgs e)
  284. {
  285. if (e.RemapActions != null)
  286. {
  287. foreach (Action action in e.RemapActions)
  288. {
  289. action();
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// Maps a given range of pages to the specified CPU virtual address.
  295. /// </summary>
  296. /// <remarks>
  297. /// All addresses and sizes must be page aligned.
  298. /// </remarks>
  299. /// <param name="pa">CPU virtual address to map into</param>
  300. /// <param name="va">GPU virtual address to be mapped</param>
  301. /// <param name="size">Size in bytes of the mapping</param>
  302. /// <param name="kind">Kind of the resource located at the mapping</param>
  303. public void Map(ulong pa, ulong va, ulong size, PteKind kind)
  304. {
  305. lock (_pageTable)
  306. {
  307. UnmapEventArgs e = new(va, size);
  308. MemoryUnmapped?.Invoke(this, e);
  309. for (ulong offset = 0; offset < size; offset += PageSize)
  310. {
  311. SetPte(va + offset, PackPte(pa + offset, kind));
  312. }
  313. RunRemapActions(e);
  314. }
  315. }
  316. /// <summary>
  317. /// Unmaps a given range of pages at the specified GPU virtual memory region.
  318. /// </summary>
  319. /// <param name="va">GPU virtual address to unmap</param>
  320. /// <param name="size">Size in bytes of the region being unmapped</param>
  321. public void Unmap(ulong va, ulong size)
  322. {
  323. lock (_pageTable)
  324. {
  325. // Event handlers are not expected to be thread safe.
  326. UnmapEventArgs e = new(va, size);
  327. MemoryUnmapped?.Invoke(this, e);
  328. for (ulong offset = 0; offset < size; offset += PageSize)
  329. {
  330. SetPte(va + offset, PteUnmapped);
  331. }
  332. RunRemapActions(e);
  333. }
  334. }
  335. /// <summary>
  336. /// Checks if a region of GPU mapped memory is contiguous.
  337. /// </summary>
  338. /// <param name="va">GPU virtual address of the region</param>
  339. /// <param name="size">Size of the region</param>
  340. /// <returns>True if the region is contiguous, false otherwise</returns>
  341. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  342. private bool IsContiguous(ulong va, int size)
  343. {
  344. if (!ValidateAddress(va) || GetPte(va) == PteUnmapped)
  345. {
  346. return false;
  347. }
  348. ulong endVa = (va + (ulong)size + PageMask) & ~PageMask;
  349. va &= ~PageMask;
  350. int pages = (int)((endVa - va) / PageSize);
  351. for (int page = 0; page < pages - 1; page++)
  352. {
  353. if (!ValidateAddress(va + PageSize) || GetPte(va + PageSize) == PteUnmapped)
  354. {
  355. return false;
  356. }
  357. if (Translate(va) + PageSize != Translate(va + PageSize))
  358. {
  359. return false;
  360. }
  361. va += PageSize;
  362. }
  363. return true;
  364. }
  365. /// <summary>
  366. /// Gets the physical regions that make up the given virtual address region.
  367. /// </summary>
  368. /// <param name="va">Virtual address of the range</param>
  369. /// <param name="size">Size of the range</param>
  370. /// <returns>Multi-range with the physical regions</returns>
  371. public MultiRange GetPhysicalRegions(ulong va, ulong size)
  372. {
  373. if (IsContiguous(va, (int)size))
  374. {
  375. return new MultiRange(Translate(va), size);
  376. }
  377. ulong regionStart = Translate(va);
  378. ulong regionSize = Math.Min(size, PageSize - (va & PageMask));
  379. ulong endVa = va + size;
  380. ulong endVaRounded = (endVa + PageMask) & ~PageMask;
  381. va &= ~PageMask;
  382. int pages = (int)((endVaRounded - va) / PageSize);
  383. var regions = new List<MemoryRange>();
  384. for (int page = 0; page < pages - 1; page++)
  385. {
  386. ulong currPa = Translate(va);
  387. ulong newPa = Translate(va + PageSize);
  388. if ((currPa != PteUnmapped || newPa != PteUnmapped) && currPa + PageSize != newPa)
  389. {
  390. regions.Add(new MemoryRange(regionStart, regionSize));
  391. regionStart = newPa;
  392. regionSize = 0;
  393. }
  394. va += PageSize;
  395. regionSize += Math.Min(endVa - va, PageSize);
  396. }
  397. if (regions.Count == 0)
  398. {
  399. return new MultiRange(regionStart, regionSize);
  400. }
  401. regions.Add(new MemoryRange(regionStart, regionSize));
  402. return new MultiRange(regions.ToArray());
  403. }
  404. /// <summary>
  405. /// Checks if a given GPU virtual memory range is mapped to the same physical regions
  406. /// as the specified physical memory multi-range.
  407. /// </summary>
  408. /// <param name="range">Physical memory multi-range</param>
  409. /// <param name="va">GPU virtual memory address</param>
  410. /// <returns>True if the virtual memory region is mapped into the specified physical one, false otherwise</returns>
  411. public bool CompareRange(MultiRange range, ulong va)
  412. {
  413. va &= ~PageMask;
  414. for (int i = 0; i < range.Count; i++)
  415. {
  416. MemoryRange currentRange = range.GetSubRange(i);
  417. if (currentRange.Address != PteUnmapped)
  418. {
  419. ulong address = currentRange.Address & ~PageMask;
  420. ulong endAddress = (currentRange.EndAddress + PageMask) & ~PageMask;
  421. while (address < endAddress)
  422. {
  423. if (Translate(va) != address)
  424. {
  425. return false;
  426. }
  427. va += PageSize;
  428. address += PageSize;
  429. }
  430. }
  431. else
  432. {
  433. ulong endVa = va + (((currentRange.Size) + PageMask) & ~PageMask);
  434. while (va < endVa)
  435. {
  436. if (Translate(va) != PteUnmapped)
  437. {
  438. return false;
  439. }
  440. va += PageSize;
  441. }
  442. }
  443. }
  444. return true;
  445. }
  446. /// <summary>
  447. /// Validates a GPU virtual address.
  448. /// </summary>
  449. /// <param name="va">Address to validate</param>
  450. /// <returns>True if the address is valid, false otherwise</returns>
  451. private static bool ValidateAddress(ulong va)
  452. {
  453. return va < (1UL << AddressSpaceBits);
  454. }
  455. /// <summary>
  456. /// Checks if a given page is mapped.
  457. /// </summary>
  458. /// <param name="va">GPU virtual address of the page to check</param>
  459. /// <returns>True if the page is mapped, false otherwise</returns>
  460. public bool IsMapped(ulong va)
  461. {
  462. return Translate(va) != PteUnmapped;
  463. }
  464. /// <summary>
  465. /// Translates a GPU virtual address to a CPU virtual address.
  466. /// </summary>
  467. /// <param name="va">GPU virtual address to be translated</param>
  468. /// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
  469. public ulong Translate(ulong va)
  470. {
  471. if (!ValidateAddress(va))
  472. {
  473. return PteUnmapped;
  474. }
  475. ulong pte = GetPte(va);
  476. if (pte == PteUnmapped)
  477. {
  478. return PteUnmapped;
  479. }
  480. return UnpackPaFromPte(pte) + (va & PageMask);
  481. }
  482. /// <summary>
  483. /// Translates a GPU virtual address to a CPU virtual address on the first mapped page of memory
  484. /// on the specified region.
  485. /// If no page is mapped on the specified region, <see cref="PteUnmapped"/> is returned.
  486. /// </summary>
  487. /// <param name="va">GPU virtual address to be translated</param>
  488. /// <param name="size">Size of the range to be translated</param>
  489. /// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
  490. public ulong TranslateFirstMapped(ulong va, ulong size)
  491. {
  492. if (!ValidateAddress(va))
  493. {
  494. return PteUnmapped;
  495. }
  496. ulong endVa = va + size;
  497. ulong pte = GetPte(va);
  498. for (; va < endVa && pte == PteUnmapped; va += PageSize - (va & PageMask))
  499. {
  500. pte = GetPte(va);
  501. }
  502. if (pte == PteUnmapped)
  503. {
  504. return PteUnmapped;
  505. }
  506. return UnpackPaFromPte(pte) + (va & PageMask);
  507. }
  508. /// <summary>
  509. /// Translates a GPU virtual address and returns the number of bytes that are mapped after it.
  510. /// </summary>
  511. /// <param name="va">GPU virtual address to be translated</param>
  512. /// <param name="maxSize">Maximum size in bytes to scan</param>
  513. /// <returns>Number of bytes, 0 if unmapped</returns>
  514. public ulong GetMappedSize(ulong va, ulong maxSize)
  515. {
  516. if (!ValidateAddress(va))
  517. {
  518. return 0;
  519. }
  520. ulong startVa = va;
  521. ulong endVa = va + maxSize;
  522. ulong pte = GetPte(va);
  523. while (pte != PteUnmapped && va < endVa)
  524. {
  525. va += PageSize - (va & PageMask);
  526. pte = GetPte(va);
  527. }
  528. return Math.Min(maxSize, va - startVa);
  529. }
  530. /// <summary>
  531. /// Gets the kind of a given memory page.
  532. /// This might indicate the type of resource that can be allocated on the page, and also texture tiling.
  533. /// </summary>
  534. /// <param name="va">GPU virtual address</param>
  535. /// <returns>Kind of the memory page</returns>
  536. public PteKind GetKind(ulong va)
  537. {
  538. if (!ValidateAddress(va))
  539. {
  540. return PteKind.Invalid;
  541. }
  542. ulong pte = GetPte(va);
  543. if (pte == PteUnmapped)
  544. {
  545. return PteKind.Invalid;
  546. }
  547. return UnpackKindFromPte(pte);
  548. }
  549. /// <summary>
  550. /// Gets the Page Table entry for a given GPU virtual address.
  551. /// </summary>
  552. /// <param name="va">GPU virtual address</param>
  553. /// <returns>Page table entry (CPU virtual address)</returns>
  554. private ulong GetPte(ulong va)
  555. {
  556. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  557. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  558. if (_pageTable[l0] == null)
  559. {
  560. return PteUnmapped;
  561. }
  562. return _pageTable[l0][l1];
  563. }
  564. /// <summary>
  565. /// Sets a Page Table entry at a given GPU virtual address.
  566. /// </summary>
  567. /// <param name="va">GPU virtual address</param>
  568. /// <param name="pte">Page table entry (CPU virtual address)</param>
  569. private void SetPte(ulong va, ulong pte)
  570. {
  571. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  572. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  573. if (_pageTable[l0] == null)
  574. {
  575. _pageTable[l0] = new ulong[PtLvl1Size];
  576. for (ulong index = 0; index < PtLvl1Size; index++)
  577. {
  578. _pageTable[l0][index] = PteUnmapped;
  579. }
  580. }
  581. _pageTable[l0][l1] = pte;
  582. }
  583. /// <summary>
  584. /// Creates a page table entry from a physical address and kind.
  585. /// </summary>
  586. /// <param name="pa">Physical address</param>
  587. /// <param name="kind">Kind</param>
  588. /// <returns>Page table entry</returns>
  589. private static ulong PackPte(ulong pa, PteKind kind)
  590. {
  591. return pa | ((ulong)kind << 56);
  592. }
  593. /// <summary>
  594. /// Unpacks kind from a page table entry.
  595. /// </summary>
  596. /// <param name="pte">Page table entry</param>
  597. /// <returns>Kind</returns>
  598. private static PteKind UnpackKindFromPte(ulong pte)
  599. {
  600. return (PteKind)(pte >> 56);
  601. }
  602. /// <summary>
  603. /// Unpacks physical address from a page table entry.
  604. /// </summary>
  605. /// <param name="pte">Page table entry</param>
  606. /// <returns>Physical address</returns>
  607. private static ulong UnpackPaFromPte(ulong pte)
  608. {
  609. return pte & 0xffffffffffffffUL;
  610. }
  611. }
  612. }