MemoryManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. using ChocolArm64.Instructions;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. using System.Threading;
  8. using static ChocolArm64.Memory.CompareExchange128;
  9. using static ChocolArm64.Memory.MemoryManagement;
  10. namespace ChocolArm64.Memory
  11. {
  12. public unsafe class MemoryManager : ARMeilleure.Memory.IMemoryManager
  13. {
  14. public const int PageBits = 12;
  15. public const int PageSize = 1 << PageBits;
  16. public const int PageMask = PageSize - 1;
  17. private const long PteFlagNotModified = 1;
  18. internal const long PteFlagsMask = 7;
  19. public IntPtr Ram { get; private set; }
  20. private byte* _ramPtr;
  21. private IntPtr _pageTable;
  22. internal IntPtr PageTable => _pageTable;
  23. internal int PtLevelBits { get; }
  24. internal int PtLevelSize { get; }
  25. internal int PtLevelMask { get; }
  26. public bool HasWriteWatchSupport => MemoryManagement.HasWriteWatchSupport;
  27. public int AddressSpaceBits { get; }
  28. public long AddressSpaceSize { get; }
  29. public MemoryManager(
  30. IntPtr ram,
  31. int addressSpaceBits = 48,
  32. bool useFlatPageTable = false)
  33. {
  34. Ram = ram;
  35. _ramPtr = (byte*)ram;
  36. AddressSpaceBits = addressSpaceBits;
  37. AddressSpaceSize = 1L << addressSpaceBits;
  38. // When flat page table is requested, we use a single
  39. // array for the mappings of the entire address space.
  40. // This has better performance, but also high memory usage.
  41. // The multi level page table uses 9 bits per level, so
  42. // the memory usage is lower, but the performance is also
  43. // lower, since each address translation requires multiple reads.
  44. if (useFlatPageTable)
  45. {
  46. PtLevelBits = addressSpaceBits - PageBits;
  47. }
  48. else
  49. {
  50. PtLevelBits = 9;
  51. }
  52. PtLevelSize = 1 << PtLevelBits;
  53. PtLevelMask = PtLevelSize - 1;
  54. _pageTable = Allocate((ulong)(PtLevelSize * IntPtr.Size));
  55. }
  56. public void Map(long va, long pa, long size)
  57. {
  58. SetPtEntries(va, _ramPtr + pa, size);
  59. }
  60. public void Unmap(long position, long size)
  61. {
  62. SetPtEntries(position, null, size);
  63. }
  64. public bool IsMapped(long position)
  65. {
  66. return Translate(position) != IntPtr.Zero;
  67. }
  68. public long GetPhysicalAddress(long virtualAddress)
  69. {
  70. byte* ptr = (byte*)Translate(virtualAddress);
  71. return (long)(ptr - _ramPtr);
  72. }
  73. private IntPtr Translate(long position)
  74. {
  75. if (!IsValidPosition(position))
  76. {
  77. return IntPtr.Zero;
  78. }
  79. byte* ptr = GetPtEntry(position);
  80. ulong ptrUlong = (ulong)ptr;
  81. if ((ptrUlong & PteFlagsMask) != 0)
  82. {
  83. ptrUlong &= ~(ulong)PteFlagsMask;
  84. ptr = (byte*)ptrUlong;
  85. }
  86. return new IntPtr(ptr + (position & PageMask));
  87. }
  88. private IntPtr TranslateWrite(long position)
  89. {
  90. if (!IsValidPosition(position))
  91. {
  92. return IntPtr.Zero;
  93. }
  94. byte* ptr = GetPtEntry(position);
  95. ulong ptrUlong = (ulong)ptr;
  96. if ((ptrUlong & PteFlagsMask) != 0)
  97. {
  98. if ((ptrUlong & PteFlagNotModified) != 0)
  99. {
  100. ClearPtEntryFlag(position, PteFlagNotModified);
  101. }
  102. ptrUlong &= ~(ulong)PteFlagsMask;
  103. ptr = (byte*)ptrUlong;
  104. }
  105. return new IntPtr(ptr + (position & PageMask));
  106. }
  107. private byte* GetPtEntry(long position)
  108. {
  109. return *(byte**)GetPtPtr(position);
  110. }
  111. private void SetPtEntries(long va, byte* ptr, long size)
  112. {
  113. long endPosition = (va + size + PageMask) & ~PageMask;
  114. while ((ulong)va < (ulong)endPosition)
  115. {
  116. SetPtEntry(va, ptr);
  117. va += PageSize;
  118. if (ptr != null)
  119. {
  120. ptr += PageSize;
  121. }
  122. }
  123. }
  124. private void SetPtEntry(long position, byte* ptr)
  125. {
  126. *(byte**)GetPtPtr(position) = ptr;
  127. }
  128. private void SetPtEntryFlag(long position, long flag)
  129. {
  130. ModifyPtEntryFlag(position, flag, setFlag: true);
  131. }
  132. private void ClearPtEntryFlag(long position, long flag)
  133. {
  134. ModifyPtEntryFlag(position, flag, setFlag: false);
  135. }
  136. private void ModifyPtEntryFlag(long position, long flag, bool setFlag)
  137. {
  138. IntPtr* pt = (IntPtr*)_pageTable;
  139. while (true)
  140. {
  141. IntPtr* ptPtr = GetPtPtr(position);
  142. IntPtr old = *ptPtr;
  143. long modified = old.ToInt64();
  144. if (setFlag)
  145. {
  146. modified |= flag;
  147. }
  148. else
  149. {
  150. modified &= ~flag;
  151. }
  152. IntPtr origValue = Interlocked.CompareExchange(ref *ptPtr, new IntPtr(modified), old);
  153. if (origValue == old)
  154. {
  155. break;
  156. }
  157. }
  158. }
  159. private IntPtr* GetPtPtr(long position)
  160. {
  161. if (!IsValidPosition(position))
  162. {
  163. throw new ArgumentOutOfRangeException(nameof(position));
  164. }
  165. IntPtr nextPtr = _pageTable;
  166. IntPtr* ptePtr = null;
  167. int bit = PageBits;
  168. while (true)
  169. {
  170. long index = (position >> bit) & PtLevelMask;
  171. ptePtr = &((IntPtr*)nextPtr)[index];
  172. bit += PtLevelBits;
  173. if (bit >= AddressSpaceBits)
  174. {
  175. break;
  176. }
  177. nextPtr = *ptePtr;
  178. if (nextPtr == IntPtr.Zero)
  179. {
  180. // Entry does not yet exist, allocate a new one.
  181. IntPtr newPtr = Allocate((ulong)(PtLevelSize * IntPtr.Size));
  182. // Try to swap the current pointer (should be zero), with the allocated one.
  183. nextPtr = Interlocked.Exchange(ref *ptePtr, newPtr);
  184. // If the old pointer is not null, then another thread already has set it.
  185. if (nextPtr != IntPtr.Zero)
  186. {
  187. Free(newPtr);
  188. }
  189. else
  190. {
  191. nextPtr = newPtr;
  192. }
  193. }
  194. }
  195. return ptePtr;
  196. }
  197. public bool IsRegionModified(long position, long size)
  198. {
  199. if (!HasWriteWatchSupport)
  200. {
  201. return IsRegionModifiedFallback(position, size);
  202. }
  203. IntPtr address = Translate(position);
  204. IntPtr baseAddr = address;
  205. IntPtr expectedAddr = address;
  206. long pendingPages = 0;
  207. long pages = size / PageSize;
  208. bool modified = false;
  209. bool IsAnyPageModified()
  210. {
  211. IntPtr pendingSize = new IntPtr(pendingPages * PageSize);
  212. IntPtr[] addresses = new IntPtr[pendingPages];
  213. bool result = GetModifiedPages(baseAddr, pendingSize, addresses, out ulong count);
  214. if (result)
  215. {
  216. return count != 0;
  217. }
  218. else
  219. {
  220. return true;
  221. }
  222. }
  223. while (pages-- > 0)
  224. {
  225. if (address != expectedAddr)
  226. {
  227. modified |= IsAnyPageModified();
  228. baseAddr = address;
  229. pendingPages = 0;
  230. }
  231. expectedAddr = address + PageSize;
  232. pendingPages++;
  233. if (pages == 0)
  234. {
  235. break;
  236. }
  237. position += PageSize;
  238. address = Translate(position);
  239. }
  240. if (pendingPages != 0)
  241. {
  242. modified |= IsAnyPageModified();
  243. }
  244. return modified;
  245. }
  246. private unsafe bool IsRegionModifiedFallback(long position, long size)
  247. {
  248. long endAddr = (position + size + PageMask) & ~PageMask;
  249. bool modified = false;
  250. while ((ulong)position < (ulong)endAddr)
  251. {
  252. if (IsValidPosition(position))
  253. {
  254. byte* ptr = ((byte**)_pageTable)[position >> PageBits];
  255. ulong ptrUlong = (ulong)ptr;
  256. if ((ptrUlong & PteFlagNotModified) == 0)
  257. {
  258. modified = true;
  259. SetPtEntryFlag(position, PteFlagNotModified);
  260. }
  261. }
  262. else
  263. {
  264. modified = true;
  265. }
  266. position += PageSize;
  267. }
  268. return modified;
  269. }
  270. public bool TryGetHostAddress(long position, long size, out IntPtr ptr)
  271. {
  272. if (IsContiguous(position, size))
  273. {
  274. ptr = (IntPtr)Translate(position);
  275. return true;
  276. }
  277. ptr = IntPtr.Zero;
  278. return false;
  279. }
  280. private bool IsContiguous(long position, long size)
  281. {
  282. long endPos = position + size;
  283. position &= ~PageMask;
  284. long expectedPa = GetPhysicalAddress(position);
  285. while ((ulong)position < (ulong)endPos)
  286. {
  287. long pa = GetPhysicalAddress(position);
  288. if (pa != expectedPa)
  289. {
  290. return false;
  291. }
  292. position += PageSize;
  293. expectedPa += PageSize;
  294. }
  295. return true;
  296. }
  297. public bool IsValidPosition(long position)
  298. {
  299. return (ulong)position < (ulong)AddressSpaceSize;
  300. }
  301. internal bool AtomicCompareExchange2xInt32(
  302. long position,
  303. int expectedLow,
  304. int expectedHigh,
  305. int desiredLow,
  306. int desiredHigh)
  307. {
  308. long expected = (uint)expectedLow;
  309. long desired = (uint)desiredLow;
  310. expected |= (long)expectedHigh << 32;
  311. desired |= (long)desiredHigh << 32;
  312. return AtomicCompareExchangeInt64(position, expected, desired);
  313. }
  314. internal bool AtomicCompareExchangeInt128(
  315. long position,
  316. ulong expectedLow,
  317. ulong expectedHigh,
  318. ulong desiredLow,
  319. ulong desiredHigh)
  320. {
  321. if ((position & 0xf) != 0)
  322. {
  323. AbortWithAlignmentFault(position);
  324. }
  325. IntPtr ptr = TranslateWrite(position);
  326. return InterlockedCompareExchange128(ptr, expectedLow, expectedHigh, desiredLow, desiredHigh);
  327. }
  328. internal Vector128<float> AtomicReadInt128(long position)
  329. {
  330. if ((position & 0xf) != 0)
  331. {
  332. AbortWithAlignmentFault(position);
  333. }
  334. IntPtr ptr = Translate(position);
  335. InterlockedRead128(ptr, out ulong low, out ulong high);
  336. Vector128<float> vector = default(Vector128<float>);
  337. vector = VectorHelper.VectorInsertInt(low, vector, 0, 3);
  338. vector = VectorHelper.VectorInsertInt(high, vector, 1, 3);
  339. return vector;
  340. }
  341. public bool AtomicCompareExchangeByte(long position, byte expected, byte desired)
  342. {
  343. int* ptr = (int*)Translate(position);
  344. int currentValue = *ptr;
  345. int expected32 = (currentValue & ~byte.MaxValue) | expected;
  346. int desired32 = (currentValue & ~byte.MaxValue) | desired;
  347. return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32;
  348. }
  349. public bool AtomicCompareExchangeInt16(long position, short expected, short desired)
  350. {
  351. if ((position & 1) != 0)
  352. {
  353. AbortWithAlignmentFault(position);
  354. }
  355. int* ptr = (int*)Translate(position);
  356. int currentValue = *ptr;
  357. int expected32 = (currentValue & ~ushort.MaxValue) | (ushort)expected;
  358. int desired32 = (currentValue & ~ushort.MaxValue) | (ushort)desired;
  359. return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32;
  360. }
  361. public bool AtomicCompareExchangeInt32(long position, int expected, int desired)
  362. {
  363. if ((position & 3) != 0)
  364. {
  365. AbortWithAlignmentFault(position);
  366. }
  367. int* ptr = (int*)TranslateWrite(position);
  368. return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected;
  369. }
  370. public bool AtomicCompareExchangeInt64(long position, long expected, long desired)
  371. {
  372. if ((position & 7) != 0)
  373. {
  374. AbortWithAlignmentFault(position);
  375. }
  376. long* ptr = (long*)TranslateWrite(position);
  377. return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected;
  378. }
  379. public int AtomicIncrementInt32(long position)
  380. {
  381. if ((position & 3) != 0)
  382. {
  383. AbortWithAlignmentFault(position);
  384. }
  385. int* ptr = (int*)TranslateWrite(position);
  386. return Interlocked.Increment(ref *ptr);
  387. }
  388. public int AtomicDecrementInt32(long position)
  389. {
  390. if ((position & 3) != 0)
  391. {
  392. AbortWithAlignmentFault(position);
  393. }
  394. int* ptr = (int*)TranslateWrite(position);
  395. return Interlocked.Decrement(ref *ptr);
  396. }
  397. private void AbortWithAlignmentFault(long position)
  398. {
  399. // TODO: Abort mode and exception support on the CPU.
  400. throw new InvalidOperationException($"Tried to compare exchange a misaligned address 0x{position:X16}.");
  401. }
  402. public sbyte ReadSByte(long position)
  403. {
  404. return (sbyte)ReadByte(position);
  405. }
  406. public short ReadInt16(long position)
  407. {
  408. return (short)ReadUInt16(position);
  409. }
  410. public int ReadInt32(long position)
  411. {
  412. return (int)ReadUInt32(position);
  413. }
  414. public long ReadInt64(long position)
  415. {
  416. return (long)ReadUInt64(position);
  417. }
  418. public byte ReadByte(long position)
  419. {
  420. return *((byte*)Translate(position));
  421. }
  422. public ushort ReadUInt16(long position)
  423. {
  424. if ((position & 1) == 0)
  425. {
  426. return *((ushort*)Translate(position));
  427. }
  428. else
  429. {
  430. return (ushort)(ReadByte(position + 0) << 0 |
  431. ReadByte(position + 1) << 8);
  432. }
  433. }
  434. public uint ReadUInt32(long position)
  435. {
  436. if ((position & 3) == 0)
  437. {
  438. return *((uint*)Translate(position));
  439. }
  440. else
  441. {
  442. return (uint)(ReadUInt16(position + 0) << 0 |
  443. ReadUInt16(position + 2) << 16);
  444. }
  445. }
  446. public ulong ReadUInt64(long position)
  447. {
  448. if ((position & 7) == 0)
  449. {
  450. return *((ulong*)Translate(position));
  451. }
  452. else
  453. {
  454. return (ulong)ReadUInt32(position + 0) << 0 |
  455. (ulong)ReadUInt32(position + 4) << 32;
  456. }
  457. }
  458. public Vector128<float> ReadVector8(long position)
  459. {
  460. if (Sse2.IsSupported)
  461. {
  462. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(position)));
  463. }
  464. else
  465. {
  466. Vector128<float> value = VectorHelper.VectorSingleZero();
  467. value = VectorHelper.VectorInsertInt(ReadByte(position), value, 0, 0);
  468. return value;
  469. }
  470. }
  471. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  472. public Vector128<float> ReadVector16(long position)
  473. {
  474. if (Sse2.IsSupported && (position & 1) == 0)
  475. {
  476. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(position), 0));
  477. }
  478. else
  479. {
  480. Vector128<float> value = VectorHelper.VectorSingleZero();
  481. value = VectorHelper.VectorInsertInt(ReadUInt16(position), value, 0, 1);
  482. return value;
  483. }
  484. }
  485. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  486. public Vector128<float> ReadVector32(long position)
  487. {
  488. if (Sse.IsSupported && (position & 3) == 0)
  489. {
  490. return Sse.LoadScalarVector128((float*)Translate(position));
  491. }
  492. else
  493. {
  494. Vector128<float> value = VectorHelper.VectorSingleZero();
  495. value = VectorHelper.VectorInsertInt(ReadUInt32(position), value, 0, 2);
  496. return value;
  497. }
  498. }
  499. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  500. public Vector128<float> ReadVector64(long position)
  501. {
  502. if (Sse2.IsSupported && (position & 7) == 0)
  503. {
  504. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)Translate(position)));
  505. }
  506. else
  507. {
  508. Vector128<float> value = VectorHelper.VectorSingleZero();
  509. value = VectorHelper.VectorInsertInt(ReadUInt64(position), value, 0, 3);
  510. return value;
  511. }
  512. }
  513. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  514. public Vector128<float> ReadVector128(long position)
  515. {
  516. if (Sse.IsSupported && (position & 15) == 0)
  517. {
  518. return Sse.LoadVector128((float*)Translate(position));
  519. }
  520. else
  521. {
  522. Vector128<float> value = VectorHelper.VectorSingleZero();
  523. value = VectorHelper.VectorInsertInt(ReadUInt64(position + 0), value, 0, 3);
  524. value = VectorHelper.VectorInsertInt(ReadUInt64(position + 8), value, 1, 3);
  525. return value;
  526. }
  527. }
  528. public byte[] ReadBytes(long position, long size)
  529. {
  530. long endAddr = position + size;
  531. if ((ulong)size > int.MaxValue)
  532. {
  533. throw new ArgumentOutOfRangeException(nameof(size));
  534. }
  535. if ((ulong)endAddr < (ulong)position)
  536. {
  537. throw new ArgumentOutOfRangeException(nameof(position));
  538. }
  539. byte[] data = new byte[size];
  540. int offset = 0;
  541. while ((ulong)position < (ulong)endAddr)
  542. {
  543. long pageLimit = (position + PageSize) & ~(long)PageMask;
  544. if ((ulong)pageLimit > (ulong)endAddr)
  545. {
  546. pageLimit = endAddr;
  547. }
  548. int copySize = (int)(pageLimit - position);
  549. Marshal.Copy(Translate(position), data, offset, copySize);
  550. position += copySize;
  551. offset += copySize;
  552. }
  553. return data;
  554. }
  555. public void ReadBytes(long position, byte[] data, int startIndex, int size)
  556. {
  557. // Note: This will be moved later.
  558. long endAddr = position + size;
  559. if ((ulong)size > int.MaxValue)
  560. {
  561. throw new ArgumentOutOfRangeException(nameof(size));
  562. }
  563. if ((ulong)endAddr < (ulong)position)
  564. {
  565. throw new ArgumentOutOfRangeException(nameof(position));
  566. }
  567. int offset = startIndex;
  568. while ((ulong)position < (ulong)endAddr)
  569. {
  570. long pageLimit = (position + PageSize) & ~(long)PageMask;
  571. if ((ulong)pageLimit > (ulong)endAddr)
  572. {
  573. pageLimit = endAddr;
  574. }
  575. int copySize = (int)(pageLimit - position);
  576. Marshal.Copy(Translate(position), data, offset, copySize);
  577. position += copySize;
  578. offset += copySize;
  579. }
  580. }
  581. public void WriteSByte(long position, sbyte value)
  582. {
  583. WriteByte(position, (byte)value);
  584. }
  585. public void WriteInt16(long position, short value)
  586. {
  587. WriteUInt16(position, (ushort)value);
  588. }
  589. public void WriteInt32(long position, int value)
  590. {
  591. WriteUInt32(position, (uint)value);
  592. }
  593. public void WriteInt64(long position, long value)
  594. {
  595. WriteUInt64(position, (ulong)value);
  596. }
  597. public void WriteByte(long position, byte value)
  598. {
  599. *((byte*)TranslateWrite(position)) = value;
  600. }
  601. public void WriteUInt16(long position, ushort value)
  602. {
  603. if ((position & 1) == 0)
  604. {
  605. *((ushort*)TranslateWrite(position)) = value;
  606. }
  607. else
  608. {
  609. WriteByte(position + 0, (byte)(value >> 0));
  610. WriteByte(position + 1, (byte)(value >> 8));
  611. }
  612. }
  613. public void WriteUInt32(long position, uint value)
  614. {
  615. if ((position & 3) == 0)
  616. {
  617. *((uint*)TranslateWrite(position)) = value;
  618. }
  619. else
  620. {
  621. WriteUInt16(position + 0, (ushort)(value >> 0));
  622. WriteUInt16(position + 2, (ushort)(value >> 16));
  623. }
  624. }
  625. public void WriteUInt64(long position, ulong value)
  626. {
  627. if ((position & 7) == 0)
  628. {
  629. *((ulong*)TranslateWrite(position)) = value;
  630. }
  631. else
  632. {
  633. WriteUInt32(position + 0, (uint)(value >> 0));
  634. WriteUInt32(position + 4, (uint)(value >> 32));
  635. }
  636. }
  637. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  638. public void WriteVector8(long position, Vector128<float> value)
  639. {
  640. if (Sse41.IsSupported)
  641. {
  642. WriteByte(position, Sse41.Extract(Sse.StaticCast<float, byte>(value), 0));
  643. }
  644. else if (Sse2.IsSupported)
  645. {
  646. WriteByte(position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(value), 0));
  647. }
  648. else
  649. {
  650. WriteByte(position, (byte)VectorHelper.VectorExtractIntZx(value, 0, 0));
  651. }
  652. }
  653. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  654. public void WriteVector16(long position, Vector128<float> value)
  655. {
  656. if (Sse2.IsSupported)
  657. {
  658. WriteUInt16(position, Sse2.Extract(Sse.StaticCast<float, ushort>(value), 0));
  659. }
  660. else
  661. {
  662. WriteUInt16(position, (ushort)VectorHelper.VectorExtractIntZx(value, 0, 1));
  663. }
  664. }
  665. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  666. public void WriteVector32(long position, Vector128<float> value)
  667. {
  668. if (Sse.IsSupported && (position & 3) == 0)
  669. {
  670. Sse.StoreScalar((float*)TranslateWrite(position), value);
  671. }
  672. else
  673. {
  674. WriteUInt32(position, (uint)VectorHelper.VectorExtractIntZx(value, 0, 2));
  675. }
  676. }
  677. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  678. public void WriteVector64(long position, Vector128<float> value)
  679. {
  680. if (Sse2.IsSupported && (position & 7) == 0)
  681. {
  682. Sse2.StoreScalar((double*)TranslateWrite(position), Sse.StaticCast<float, double>(value));
  683. }
  684. else
  685. {
  686. WriteUInt64(position, VectorHelper.VectorExtractIntZx(value, 0, 3));
  687. }
  688. }
  689. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  690. public void WriteVector128Internal(long position, Vector128<float> value)
  691. {
  692. if (Sse.IsSupported && (position & 15) == 0)
  693. {
  694. Sse.Store((float*)TranslateWrite(position), value);
  695. }
  696. else
  697. {
  698. WriteUInt64(position + 0, VectorHelper.VectorExtractIntZx(value, 0, 3));
  699. WriteUInt64(position + 8, VectorHelper.VectorExtractIntZx(value, 1, 3));
  700. }
  701. }
  702. public void WriteVector128(long position, ARMeilleure.State.V128 value)
  703. {
  704. WriteUInt64(position + 0, value.GetUInt64(0));
  705. WriteUInt64(position + 8, value.GetUInt64(1));
  706. }
  707. public void WriteBytes(long position, byte[] data)
  708. {
  709. long endAddr = position + data.Length;
  710. if ((ulong)endAddr < (ulong)position)
  711. {
  712. throw new ArgumentOutOfRangeException(nameof(position));
  713. }
  714. int offset = 0;
  715. while ((ulong)position < (ulong)endAddr)
  716. {
  717. long pageLimit = (position + PageSize) & ~(long)PageMask;
  718. if ((ulong)pageLimit > (ulong)endAddr)
  719. {
  720. pageLimit = endAddr;
  721. }
  722. int copySize = (int)(pageLimit - position);
  723. Marshal.Copy(data, offset, TranslateWrite(position), copySize);
  724. position += copySize;
  725. offset += copySize;
  726. }
  727. }
  728. public void WriteBytes(long position, byte[] data, int startIndex, int size)
  729. {
  730. // Note: This will be moved later.
  731. long endAddr = position + size;
  732. if ((ulong)endAddr < (ulong)position)
  733. {
  734. throw new ArgumentOutOfRangeException(nameof(position));
  735. }
  736. int offset = startIndex;
  737. while ((ulong)position < (ulong)endAddr)
  738. {
  739. long pageLimit = (position + PageSize) & ~(long)PageMask;
  740. if ((ulong)pageLimit > (ulong)endAddr)
  741. {
  742. pageLimit = endAddr;
  743. }
  744. int copySize = (int)(pageLimit - position);
  745. Marshal.Copy(data, offset, Translate(position), copySize);
  746. position += copySize;
  747. offset += copySize;
  748. }
  749. }
  750. public void CopyBytes(long src, long dst, long size)
  751. {
  752. // Note: This will be moved later.
  753. if (IsContiguous(src, size) &&
  754. IsContiguous(dst, size))
  755. {
  756. byte* srcPtr = (byte*)Translate(src);
  757. byte* dstPtr = (byte*)Translate(dst);
  758. Buffer.MemoryCopy(srcPtr, dstPtr, size, size);
  759. }
  760. else
  761. {
  762. WriteBytes(dst, ReadBytes(src, size));
  763. }
  764. }
  765. public void Dispose()
  766. {
  767. Dispose(true);
  768. }
  769. protected virtual void Dispose(bool disposing)
  770. {
  771. IntPtr ptr = Interlocked.Exchange(ref _pageTable, IntPtr.Zero);
  772. if (ptr != IntPtr.Zero)
  773. {
  774. FreePageTableEntry(ptr, PageBits);
  775. }
  776. }
  777. private void FreePageTableEntry(IntPtr ptr, int levelBitEnd)
  778. {
  779. levelBitEnd += PtLevelBits;
  780. if (levelBitEnd >= AddressSpaceBits)
  781. {
  782. Free(ptr);
  783. return;
  784. }
  785. for (int index = 0; index < PtLevelSize; index++)
  786. {
  787. IntPtr ptePtr = ((IntPtr*)ptr)[index];
  788. if (ptePtr != IntPtr.Zero)
  789. {
  790. FreePageTableEntry(ptePtr, levelBitEnd);
  791. }
  792. }
  793. Free(ptr);
  794. }
  795. }
  796. }