MemoryManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. using ChocolArm64.Events;
  2. using ChocolArm64.Exceptions;
  3. using ChocolArm64.Instructions;
  4. using ChocolArm64.State;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Runtime.CompilerServices;
  9. using System.Runtime.InteropServices;
  10. using System.Runtime.Intrinsics;
  11. using System.Runtime.Intrinsics.X86;
  12. using System.Threading;
  13. namespace ChocolArm64.Memory
  14. {
  15. public unsafe class MemoryManager : IMemory, IDisposable
  16. {
  17. private const int PtLvl0Bits = 13;
  18. private const int PtLvl1Bits = 14;
  19. public const int PageBits = 12;
  20. private const int PtLvl0Size = 1 << PtLvl0Bits;
  21. private const int PtLvl1Size = 1 << PtLvl1Bits;
  22. public const int PageSize = 1 << PageBits;
  23. private const int PtLvl0Mask = PtLvl0Size - 1;
  24. private const int PtLvl1Mask = PtLvl1Size - 1;
  25. public const int PageMask = PageSize - 1;
  26. private const int PtLvl0Bit = PageBits + PtLvl1Bits;
  27. private const int PtLvl1Bit = PageBits;
  28. private const long ErgMask = (4 << CpuThreadState.ErgSizeLog2) - 1;
  29. private class ArmMonitor
  30. {
  31. public long Position;
  32. public bool ExState;
  33. public bool HasExclusiveAccess(long position)
  34. {
  35. return Position == position && ExState;
  36. }
  37. }
  38. private Dictionary<int, ArmMonitor> _monitors;
  39. private ConcurrentDictionary<long, IntPtr> _observedPages;
  40. public IntPtr Ram { get; private set; }
  41. private byte* _ramPtr;
  42. private byte*** _pageTable;
  43. public event EventHandler<MemoryAccessEventArgs> InvalidAccess;
  44. public event EventHandler<MemoryAccessEventArgs> ObservedAccess;
  45. public MemoryManager(IntPtr ram)
  46. {
  47. _monitors = new Dictionary<int, ArmMonitor>();
  48. _observedPages = new ConcurrentDictionary<long, IntPtr>();
  49. Ram = ram;
  50. _ramPtr = (byte*)ram;
  51. _pageTable = (byte***)Marshal.AllocHGlobal(PtLvl0Size * IntPtr.Size);
  52. for (int l0 = 0; l0 < PtLvl0Size; l0++)
  53. {
  54. _pageTable[l0] = null;
  55. }
  56. }
  57. public void RemoveMonitor(int core)
  58. {
  59. lock (_monitors)
  60. {
  61. ClearExclusive(core);
  62. _monitors.Remove(core);
  63. }
  64. }
  65. public void SetExclusive(int core, long position)
  66. {
  67. position &= ~ErgMask;
  68. lock (_monitors)
  69. {
  70. foreach (ArmMonitor mon in _monitors.Values)
  71. {
  72. if (mon.Position == position && mon.ExState)
  73. {
  74. mon.ExState = false;
  75. }
  76. }
  77. if (!_monitors.TryGetValue(core, out ArmMonitor threadMon))
  78. {
  79. threadMon = new ArmMonitor();
  80. _monitors.Add(core, threadMon);
  81. }
  82. threadMon.Position = position;
  83. threadMon.ExState = true;
  84. }
  85. }
  86. public bool TestExclusive(int core, long position)
  87. {
  88. //Note: Any call to this method also should be followed by a
  89. //call to ClearExclusiveForStore if this method returns true.
  90. position &= ~ErgMask;
  91. Monitor.Enter(_monitors);
  92. if (!_monitors.TryGetValue(core, out ArmMonitor threadMon))
  93. {
  94. Monitor.Exit(_monitors);
  95. return false;
  96. }
  97. bool exState = threadMon.HasExclusiveAccess(position);
  98. if (!exState)
  99. {
  100. Monitor.Exit(_monitors);
  101. }
  102. return exState;
  103. }
  104. public void ClearExclusiveForStore(int core)
  105. {
  106. if (_monitors.TryGetValue(core, out ArmMonitor threadMon))
  107. {
  108. threadMon.ExState = false;
  109. }
  110. Monitor.Exit(_monitors);
  111. }
  112. public void ClearExclusive(int core)
  113. {
  114. lock (_monitors)
  115. {
  116. if (_monitors.TryGetValue(core, out ArmMonitor threadMon))
  117. {
  118. threadMon.ExState = false;
  119. }
  120. }
  121. }
  122. public void WriteInt32ToSharedAddr(long position, int value)
  123. {
  124. long maskedPosition = position & ~ErgMask;
  125. lock (_monitors)
  126. {
  127. foreach (ArmMonitor mon in _monitors.Values)
  128. {
  129. if (mon.Position == maskedPosition && mon.ExState)
  130. {
  131. mon.ExState = false;
  132. }
  133. }
  134. WriteInt32(position, value);
  135. }
  136. }
  137. public sbyte ReadSByte(long position)
  138. {
  139. return (sbyte)ReadByte(position);
  140. }
  141. public short ReadInt16(long position)
  142. {
  143. return (short)ReadUInt16(position);
  144. }
  145. public int ReadInt32(long position)
  146. {
  147. return (int)ReadUInt32(position);
  148. }
  149. public long ReadInt64(long position)
  150. {
  151. return (long)ReadUInt64(position);
  152. }
  153. public byte ReadByte(long position)
  154. {
  155. return *((byte*)Translate(position));
  156. }
  157. public ushort ReadUInt16(long position)
  158. {
  159. if ((position & 1) == 0)
  160. {
  161. return *((ushort*)Translate(position));
  162. }
  163. else
  164. {
  165. return (ushort)(ReadByte(position + 0) << 0 |
  166. ReadByte(position + 1) << 8);
  167. }
  168. }
  169. public uint ReadUInt32(long position)
  170. {
  171. if ((position & 3) == 0)
  172. {
  173. return *((uint*)Translate(position));
  174. }
  175. else
  176. {
  177. return (uint)(ReadUInt16(position + 0) << 0 |
  178. ReadUInt16(position + 2) << 16);
  179. }
  180. }
  181. public ulong ReadUInt64(long position)
  182. {
  183. if ((position & 7) == 0)
  184. {
  185. return *((ulong*)Translate(position));
  186. }
  187. else
  188. {
  189. return (ulong)ReadUInt32(position + 0) << 0 |
  190. (ulong)ReadUInt32(position + 4) << 32;
  191. }
  192. }
  193. public Vector128<float> ReadVector8(long position)
  194. {
  195. if (Sse2.IsSupported)
  196. {
  197. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(position)));
  198. }
  199. else
  200. {
  201. Vector128<float> value = VectorHelper.VectorSingleZero();
  202. value = VectorHelper.VectorInsertInt(ReadByte(position), value, 0, 0);
  203. return value;
  204. }
  205. }
  206. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  207. public Vector128<float> ReadVector16(long position)
  208. {
  209. if (Sse2.IsSupported && (position & 1) == 0)
  210. {
  211. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(position), 0));
  212. }
  213. else
  214. {
  215. Vector128<float> value = VectorHelper.VectorSingleZero();
  216. value = VectorHelper.VectorInsertInt(ReadUInt16(position), value, 0, 1);
  217. return value;
  218. }
  219. }
  220. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  221. public Vector128<float> ReadVector32(long position)
  222. {
  223. if (Sse.IsSupported && (position & 3) == 0)
  224. {
  225. return Sse.LoadScalarVector128((float*)Translate(position));
  226. }
  227. else
  228. {
  229. Vector128<float> value = VectorHelper.VectorSingleZero();
  230. value = VectorHelper.VectorInsertInt(ReadUInt32(position), value, 0, 2);
  231. return value;
  232. }
  233. }
  234. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  235. public Vector128<float> ReadVector64(long position)
  236. {
  237. if (Sse2.IsSupported && (position & 7) == 0)
  238. {
  239. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)Translate(position)));
  240. }
  241. else
  242. {
  243. Vector128<float> value = VectorHelper.VectorSingleZero();
  244. value = VectorHelper.VectorInsertInt(ReadUInt64(position), value, 0, 3);
  245. return value;
  246. }
  247. }
  248. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  249. public Vector128<float> ReadVector128(long position)
  250. {
  251. if (Sse.IsSupported && (position & 15) == 0)
  252. {
  253. return Sse.LoadVector128((float*)Translate(position));
  254. }
  255. else
  256. {
  257. Vector128<float> value = VectorHelper.VectorSingleZero();
  258. value = VectorHelper.VectorInsertInt(ReadUInt64(position + 0), value, 0, 3);
  259. value = VectorHelper.VectorInsertInt(ReadUInt64(position + 8), value, 1, 3);
  260. return value;
  261. }
  262. }
  263. public byte[] ReadBytes(long position, long size)
  264. {
  265. long endAddr = position + size;
  266. if ((ulong)size > int.MaxValue)
  267. {
  268. throw new ArgumentOutOfRangeException(nameof(size));
  269. }
  270. if ((ulong)endAddr < (ulong)position)
  271. {
  272. throw new ArgumentOutOfRangeException(nameof(position));
  273. }
  274. byte[] data = new byte[size];
  275. int offset = 0;
  276. while ((ulong)position < (ulong)endAddr)
  277. {
  278. long pageLimit = (position + PageSize) & ~(long)PageMask;
  279. if ((ulong)pageLimit > (ulong)endAddr)
  280. {
  281. pageLimit = endAddr;
  282. }
  283. int copySize = (int)(pageLimit - position);
  284. Marshal.Copy((IntPtr)Translate(position), data, offset, copySize);
  285. position += copySize;
  286. offset += copySize;
  287. }
  288. return data;
  289. }
  290. public void ReadBytes(long position, byte[] data, int startIndex, int size)
  291. {
  292. //Note: This will be moved later.
  293. long endAddr = position + size;
  294. if ((ulong)size > int.MaxValue)
  295. {
  296. throw new ArgumentOutOfRangeException(nameof(size));
  297. }
  298. if ((ulong)endAddr < (ulong)position)
  299. {
  300. throw new ArgumentOutOfRangeException(nameof(position));
  301. }
  302. int offset = startIndex;
  303. while ((ulong)position < (ulong)endAddr)
  304. {
  305. long pageLimit = (position + PageSize) & ~(long)PageMask;
  306. if ((ulong)pageLimit > (ulong)endAddr)
  307. {
  308. pageLimit = endAddr;
  309. }
  310. int copySize = (int)(pageLimit - position);
  311. Marshal.Copy((IntPtr)Translate(position), data, offset, copySize);
  312. position += copySize;
  313. offset += copySize;
  314. }
  315. }
  316. public void WriteSByte(long position, sbyte value)
  317. {
  318. WriteByte(position, (byte)value);
  319. }
  320. public void WriteInt16(long position, short value)
  321. {
  322. WriteUInt16(position, (ushort)value);
  323. }
  324. public void WriteInt32(long position, int value)
  325. {
  326. WriteUInt32(position, (uint)value);
  327. }
  328. public void WriteInt64(long position, long value)
  329. {
  330. WriteUInt64(position, (ulong)value);
  331. }
  332. public void WriteByte(long position, byte value)
  333. {
  334. *((byte*)TranslateWrite(position)) = value;
  335. }
  336. public void WriteUInt16(long position, ushort value)
  337. {
  338. if ((position & 1) == 0)
  339. {
  340. *((ushort*)TranslateWrite(position)) = value;
  341. }
  342. else
  343. {
  344. WriteByte(position + 0, (byte)(value >> 0));
  345. WriteByte(position + 1, (byte)(value >> 8));
  346. }
  347. }
  348. public void WriteUInt32(long position, uint value)
  349. {
  350. if ((position & 3) == 0)
  351. {
  352. *((uint*)TranslateWrite(position)) = value;
  353. }
  354. else
  355. {
  356. WriteUInt16(position + 0, (ushort)(value >> 0));
  357. WriteUInt16(position + 2, (ushort)(value >> 16));
  358. }
  359. }
  360. public void WriteUInt64(long position, ulong value)
  361. {
  362. if ((position & 7) == 0)
  363. {
  364. *((ulong*)TranslateWrite(position)) = value;
  365. }
  366. else
  367. {
  368. WriteUInt32(position + 0, (uint)(value >> 0));
  369. WriteUInt32(position + 4, (uint)(value >> 32));
  370. }
  371. }
  372. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  373. public void WriteVector8(long position, Vector128<float> value)
  374. {
  375. if (Sse41.IsSupported)
  376. {
  377. WriteByte(position, Sse41.Extract(Sse.StaticCast<float, byte>(value), 0));
  378. }
  379. else if (Sse2.IsSupported)
  380. {
  381. WriteByte(position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(value), 0));
  382. }
  383. else
  384. {
  385. WriteByte(position, (byte)VectorHelper.VectorExtractIntZx(value, 0, 0));
  386. }
  387. }
  388. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  389. public void WriteVector16(long position, Vector128<float> value)
  390. {
  391. if (Sse2.IsSupported)
  392. {
  393. WriteUInt16(position, Sse2.Extract(Sse.StaticCast<float, ushort>(value), 0));
  394. }
  395. else
  396. {
  397. WriteUInt16(position, (ushort)VectorHelper.VectorExtractIntZx(value, 0, 1));
  398. }
  399. }
  400. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  401. public void WriteVector32(long position, Vector128<float> value)
  402. {
  403. if (Sse.IsSupported && (position & 3) == 0)
  404. {
  405. Sse.StoreScalar((float*)TranslateWrite(position), value);
  406. }
  407. else
  408. {
  409. WriteUInt32(position, (uint)VectorHelper.VectorExtractIntZx(value, 0, 2));
  410. }
  411. }
  412. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  413. public void WriteVector64(long position, Vector128<float> value)
  414. {
  415. if (Sse2.IsSupported && (position & 7) == 0)
  416. {
  417. Sse2.StoreScalar((double*)TranslateWrite(position), Sse.StaticCast<float, double>(value));
  418. }
  419. else
  420. {
  421. WriteUInt64(position, VectorHelper.VectorExtractIntZx(value, 0, 3));
  422. }
  423. }
  424. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  425. public void WriteVector128(long position, Vector128<float> value)
  426. {
  427. if (Sse.IsSupported && (position & 15) == 0)
  428. {
  429. Sse.Store((float*)TranslateWrite(position), value);
  430. }
  431. else
  432. {
  433. WriteUInt64(position + 0, VectorHelper.VectorExtractIntZx(value, 0, 3));
  434. WriteUInt64(position + 8, VectorHelper.VectorExtractIntZx(value, 1, 3));
  435. }
  436. }
  437. public void WriteBytes(long position, byte[] data)
  438. {
  439. long endAddr = position + data.Length;
  440. if ((ulong)endAddr < (ulong)position)
  441. {
  442. throw new ArgumentOutOfRangeException(nameof(position));
  443. }
  444. int offset = 0;
  445. while ((ulong)position < (ulong)endAddr)
  446. {
  447. long pageLimit = (position + PageSize) & ~(long)PageMask;
  448. if ((ulong)pageLimit > (ulong)endAddr)
  449. {
  450. pageLimit = endAddr;
  451. }
  452. int copySize = (int)(pageLimit - position);
  453. Marshal.Copy(data, offset, (IntPtr)TranslateWrite(position), copySize);
  454. position += copySize;
  455. offset += copySize;
  456. }
  457. }
  458. public void WriteBytes(long position, byte[] data, int startIndex, int size)
  459. {
  460. //Note: This will be moved later.
  461. long endAddr = position + size;
  462. if ((ulong)endAddr < (ulong)position)
  463. {
  464. throw new ArgumentOutOfRangeException(nameof(position));
  465. }
  466. int offset = startIndex;
  467. while ((ulong)position < (ulong)endAddr)
  468. {
  469. long pageLimit = (position + PageSize) & ~(long)PageMask;
  470. if ((ulong)pageLimit > (ulong)endAddr)
  471. {
  472. pageLimit = endAddr;
  473. }
  474. int copySize = (int)(pageLimit - position);
  475. Marshal.Copy(data, offset, (IntPtr)TranslateWrite(position), copySize);
  476. position += copySize;
  477. offset += copySize;
  478. }
  479. }
  480. public void CopyBytes(long src, long dst, long size)
  481. {
  482. //Note: This will be moved later.
  483. if (IsContiguous(src, size) &&
  484. IsContiguous(dst, size))
  485. {
  486. byte* srcPtr = Translate(src);
  487. byte* dstPtr = TranslateWrite(dst);
  488. Buffer.MemoryCopy(srcPtr, dstPtr, size, size);
  489. }
  490. else
  491. {
  492. WriteBytes(dst, ReadBytes(src, size));
  493. }
  494. }
  495. public void Map(long va, long pa, long size)
  496. {
  497. SetPtEntries(va, _ramPtr + pa, size);
  498. }
  499. public void Unmap(long position, long size)
  500. {
  501. SetPtEntries(position, null, size);
  502. StopObservingRegion(position, size);
  503. }
  504. public bool IsMapped(long position)
  505. {
  506. if (!(IsValidPosition(position)))
  507. {
  508. return false;
  509. }
  510. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  511. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  512. if (_pageTable[l0] == null)
  513. {
  514. return false;
  515. }
  516. return _pageTable[l0][l1] != null || _observedPages.ContainsKey(position >> PageBits);
  517. }
  518. public long GetPhysicalAddress(long virtualAddress)
  519. {
  520. byte* ptr = Translate(virtualAddress);
  521. return (long)(ptr - _ramPtr);
  522. }
  523. internal byte* Translate(long position)
  524. {
  525. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  526. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  527. long old = position;
  528. byte** lvl1 = _pageTable[l0];
  529. if ((position >> (PtLvl0Bit + PtLvl0Bits)) != 0)
  530. {
  531. goto Unmapped;
  532. }
  533. if (lvl1 == null)
  534. {
  535. goto Unmapped;
  536. }
  537. position &= PageMask;
  538. byte* ptr = lvl1[l1];
  539. if (ptr == null)
  540. {
  541. goto Unmapped;
  542. }
  543. return ptr + position;
  544. Unmapped:
  545. return HandleNullPte(old);
  546. }
  547. private byte* HandleNullPte(long position)
  548. {
  549. long key = position >> PageBits;
  550. if (_observedPages.TryGetValue(key, out IntPtr ptr))
  551. {
  552. return (byte*)ptr + (position & PageMask);
  553. }
  554. InvalidAccess?.Invoke(this, new MemoryAccessEventArgs(position));
  555. throw new VmmPageFaultException(position);
  556. }
  557. internal byte* TranslateWrite(long position)
  558. {
  559. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  560. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  561. long old = position;
  562. byte** lvl1 = _pageTable[l0];
  563. if ((position >> (PtLvl0Bit + PtLvl0Bits)) != 0)
  564. {
  565. goto Unmapped;
  566. }
  567. if (lvl1 == null)
  568. {
  569. goto Unmapped;
  570. }
  571. position &= PageMask;
  572. byte* ptr = lvl1[l1];
  573. if (ptr == null)
  574. {
  575. goto Unmapped;
  576. }
  577. return ptr + position;
  578. Unmapped:
  579. return HandleNullPteWrite(old);
  580. }
  581. private byte* HandleNullPteWrite(long position)
  582. {
  583. long key = position >> PageBits;
  584. MemoryAccessEventArgs e = new MemoryAccessEventArgs(position);
  585. if (_observedPages.TryGetValue(key, out IntPtr ptr))
  586. {
  587. SetPtEntry(position, (byte*)ptr);
  588. ObservedAccess?.Invoke(this, e);
  589. return (byte*)ptr + (position & PageMask);
  590. }
  591. InvalidAccess?.Invoke(this, e);
  592. throw new VmmPageFaultException(position);
  593. }
  594. private void SetPtEntries(long va, byte* ptr, long size)
  595. {
  596. long endPosition = (va + size + PageMask) & ~PageMask;
  597. while ((ulong)va < (ulong)endPosition)
  598. {
  599. SetPtEntry(va, ptr);
  600. va += PageSize;
  601. if (ptr != null)
  602. {
  603. ptr += PageSize;
  604. }
  605. }
  606. }
  607. private void SetPtEntry(long position, byte* ptr)
  608. {
  609. if (!IsValidPosition(position))
  610. {
  611. throw new ArgumentOutOfRangeException(nameof(position));
  612. }
  613. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  614. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  615. if (_pageTable[l0] == null)
  616. {
  617. byte** lvl1 = (byte**)Marshal.AllocHGlobal(PtLvl1Size * IntPtr.Size);
  618. for (int zl1 = 0; zl1 < PtLvl1Size; zl1++)
  619. {
  620. lvl1[zl1] = null;
  621. }
  622. Thread.MemoryBarrier();
  623. _pageTable[l0] = lvl1;
  624. }
  625. _pageTable[l0][l1] = ptr;
  626. }
  627. public void StartObservingRegion(long position, long size)
  628. {
  629. long endPosition = (position + size + PageMask) & ~PageMask;
  630. position &= ~PageMask;
  631. while ((ulong)position < (ulong)endPosition)
  632. {
  633. _observedPages[position >> PageBits] = (IntPtr)Translate(position);
  634. SetPtEntry(position, null);
  635. position += PageSize;
  636. }
  637. }
  638. public void StopObservingRegion(long position, long size)
  639. {
  640. long endPosition = (position + size + PageMask) & ~PageMask;
  641. while (position < endPosition)
  642. {
  643. lock (_observedPages)
  644. {
  645. if (_observedPages.TryRemove(position >> PageBits, out IntPtr ptr))
  646. {
  647. SetPtEntry(position, (byte*)ptr);
  648. }
  649. }
  650. position += PageSize;
  651. }
  652. }
  653. public bool TryGetHostAddress(long position, long size, out IntPtr ptr)
  654. {
  655. if (IsContiguous(position, size))
  656. {
  657. ptr = (IntPtr)Translate(position);
  658. return true;
  659. }
  660. ptr = IntPtr.Zero;
  661. return false;
  662. }
  663. private bool IsContiguous(long position, long size)
  664. {
  665. long endPos = position + size;
  666. position &= ~PageMask;
  667. long expectedPa = GetPhysicalAddress(position);
  668. while ((ulong)position < (ulong)endPos)
  669. {
  670. long pa = GetPhysicalAddress(position);
  671. if (pa != expectedPa)
  672. {
  673. return false;
  674. }
  675. position += PageSize;
  676. expectedPa += PageSize;
  677. }
  678. return true;
  679. }
  680. public bool IsValidPosition(long position)
  681. {
  682. return position >> (PtLvl0Bits + PtLvl1Bits + PageBits) == 0;
  683. }
  684. public void Dispose()
  685. {
  686. Dispose(true);
  687. }
  688. protected virtual void Dispose(bool disposing)
  689. {
  690. if (_pageTable == null)
  691. {
  692. return;
  693. }
  694. for (int l0 = 0; l0 < PtLvl0Size; l0++)
  695. {
  696. if (_pageTable[l0] != null)
  697. {
  698. Marshal.FreeHGlobal((IntPtr)_pageTable[l0]);
  699. }
  700. _pageTable[l0] = null;
  701. }
  702. Marshal.FreeHGlobal((IntPtr)_pageTable);
  703. _pageTable = null;
  704. }
  705. }
  706. }