MemoryManager.cs 25 KB

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