AMemory.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. using ChocolArm64.Exceptions;
  2. using ChocolArm64.State;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Intrinsics;
  8. using System.Runtime.Intrinsics.X86;
  9. using System.Threading;
  10. namespace ChocolArm64.Memory
  11. {
  12. public unsafe class AMemory : IAMemory, IDisposable
  13. {
  14. private const long ErgMask = (4 << AThreadState.ErgSizeLog2) - 1;
  15. public AMemoryMgr Manager { get; private set; }
  16. private class ArmMonitor
  17. {
  18. public long Position;
  19. public bool ExState;
  20. public bool HasExclusiveAccess(long Position)
  21. {
  22. return this.Position == Position && ExState;
  23. }
  24. }
  25. private Dictionary<int, ArmMonitor> Monitors;
  26. public IntPtr Ram { get; private set; }
  27. private byte* RamPtr;
  28. private int HostPageSize;
  29. public AMemory()
  30. {
  31. Manager = new AMemoryMgr();
  32. Monitors = new Dictionary<int, ArmMonitor>();
  33. IntPtr Size = (IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize;
  34. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  35. {
  36. Ram = AMemoryWin32.Allocate(Size);
  37. HostPageSize = AMemoryWin32.GetPageSize(Ram, Size);
  38. }
  39. else
  40. {
  41. Ram = Marshal.AllocHGlobal(Size);
  42. }
  43. RamPtr = (byte*)Ram;
  44. }
  45. public void RemoveMonitor(AThreadState State)
  46. {
  47. lock (Monitors)
  48. {
  49. ClearExclusive(State);
  50. Monitors.Remove(State.ThreadId);
  51. }
  52. }
  53. public void SetExclusive(AThreadState ThreadState, long Position)
  54. {
  55. Position &= ~ErgMask;
  56. lock (Monitors)
  57. {
  58. foreach (ArmMonitor Mon in Monitors.Values)
  59. {
  60. if (Mon.Position == Position && Mon.ExState)
  61. {
  62. Mon.ExState = false;
  63. }
  64. }
  65. if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
  66. {
  67. ThreadMon = new ArmMonitor();
  68. Monitors.Add(ThreadState.ThreadId, ThreadMon);
  69. }
  70. ThreadMon.Position = Position;
  71. ThreadMon.ExState = true;
  72. }
  73. }
  74. public bool TestExclusive(AThreadState ThreadState, long Position)
  75. {
  76. //Note: Any call to this method also should be followed by a
  77. //call to ClearExclusiveForStore if this method returns true.
  78. Position &= ~ErgMask;
  79. Monitor.Enter(Monitors);
  80. if (!Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
  81. {
  82. return false;
  83. }
  84. bool ExState = ThreadMon.HasExclusiveAccess(Position);
  85. if (!ExState)
  86. {
  87. Monitor.Exit(Monitors);
  88. }
  89. return ExState;
  90. }
  91. public void ClearExclusiveForStore(AThreadState ThreadState)
  92. {
  93. if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
  94. {
  95. ThreadMon.ExState = false;
  96. }
  97. Monitor.Exit(Monitors);
  98. }
  99. public void ClearExclusive(AThreadState ThreadState)
  100. {
  101. lock (Monitors)
  102. {
  103. if (Monitors.TryGetValue(ThreadState.ThreadId, out ArmMonitor ThreadMon))
  104. {
  105. ThreadMon.ExState = false;
  106. }
  107. }
  108. }
  109. public void WriteInt32ToSharedAddr(long Position, int Value)
  110. {
  111. long MaskedPosition = Position & ~ErgMask;
  112. lock (Monitors)
  113. {
  114. foreach (ArmMonitor Mon in Monitors.Values)
  115. {
  116. if (Mon.Position == MaskedPosition && Mon.ExState)
  117. {
  118. Mon.ExState = false;
  119. }
  120. }
  121. WriteInt32(Position, Value);
  122. }
  123. }
  124. public int GetHostPageSize()
  125. {
  126. return HostPageSize;
  127. }
  128. public (bool[], long) IsRegionModified(long Position, long Size)
  129. {
  130. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  131. {
  132. return (null, 0);
  133. }
  134. long EndPos = Position + Size;
  135. if ((ulong)EndPos < (ulong)Position)
  136. {
  137. return (null, 0);
  138. }
  139. if ((ulong)EndPos > AMemoryMgr.RamSize)
  140. {
  141. return (null, 0);
  142. }
  143. IntPtr MemAddress = new IntPtr(RamPtr + Position);
  144. IntPtr MemSize = new IntPtr(Size);
  145. int HostPageMask = HostPageSize - 1;
  146. Position &= ~HostPageMask;
  147. Size = EndPos - Position;
  148. IntPtr[] Addresses = new IntPtr[(Size + HostPageMask) / HostPageSize];
  149. AMemoryWin32.IsRegionModified(MemAddress, MemSize, Addresses, out int Count);
  150. bool[] Modified = new bool[Addresses.Length];
  151. for (int Index = 0; Index < Count; Index++)
  152. {
  153. long VA = Addresses[Index].ToInt64() - Ram.ToInt64();
  154. Modified[(VA - Position) / HostPageSize] = true;
  155. }
  156. return (Modified, Count);
  157. }
  158. public IntPtr GetHostAddress(long Position, long Size)
  159. {
  160. EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
  161. return (IntPtr)(RamPtr + (ulong)Position);
  162. }
  163. public sbyte ReadSByte(long Position)
  164. {
  165. return (sbyte)ReadByte(Position);
  166. }
  167. public short ReadInt16(long Position)
  168. {
  169. return (short)ReadUInt16(Position);
  170. }
  171. public int ReadInt32(long Position)
  172. {
  173. return (int)ReadUInt32(Position);
  174. }
  175. public long ReadInt64(long Position)
  176. {
  177. return (long)ReadUInt64(Position);
  178. }
  179. public byte ReadByte(long Position)
  180. {
  181. EnsureAccessIsValid(Position, AMemoryPerm.Read);
  182. return ReadByteUnchecked(Position);
  183. }
  184. public ushort ReadUInt16(long Position)
  185. {
  186. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  187. EnsureAccessIsValid(Position + 1, AMemoryPerm.Read);
  188. return ReadUInt16Unchecked(Position);
  189. }
  190. public uint ReadUInt32(long Position)
  191. {
  192. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  193. EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
  194. return ReadUInt32Unchecked(Position);
  195. }
  196. public ulong ReadUInt64(long Position)
  197. {
  198. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  199. EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
  200. return ReadUInt64Unchecked(Position);
  201. }
  202. public Vector128<float> ReadVector8(long Position)
  203. {
  204. if (Sse2.IsSupported)
  205. {
  206. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(Position)));
  207. }
  208. else
  209. {
  210. throw new PlatformNotSupportedException();
  211. }
  212. }
  213. public Vector128<float> ReadVector16(long Position)
  214. {
  215. if (Sse2.IsSupported)
  216. {
  217. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(Position), 0));
  218. }
  219. else
  220. {
  221. throw new PlatformNotSupportedException();
  222. }
  223. }
  224. public Vector128<float> ReadVector32(long Position)
  225. {
  226. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  227. EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
  228. if (Sse.IsSupported)
  229. {
  230. return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
  231. }
  232. else
  233. {
  234. throw new PlatformNotSupportedException();
  235. }
  236. }
  237. public Vector128<float> ReadVector64(long Position)
  238. {
  239. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  240. EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
  241. if (Sse2.IsSupported)
  242. {
  243. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
  244. }
  245. else
  246. {
  247. throw new PlatformNotSupportedException();
  248. }
  249. }
  250. public Vector128<float> ReadVector128(long Position)
  251. {
  252. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  253. EnsureAccessIsValid(Position + 15, AMemoryPerm.Read);
  254. if (Sse.IsSupported)
  255. {
  256. return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
  257. }
  258. else
  259. {
  260. throw new PlatformNotSupportedException();
  261. }
  262. }
  263. public sbyte ReadSByteUnchecked(long Position)
  264. {
  265. return (sbyte)ReadByteUnchecked(Position);
  266. }
  267. public short ReadInt16Unchecked(long Position)
  268. {
  269. return (short)ReadUInt16Unchecked(Position);
  270. }
  271. public int ReadInt32Unchecked(long Position)
  272. {
  273. return (int)ReadUInt32Unchecked(Position);
  274. }
  275. public long ReadInt64Unchecked(long Position)
  276. {
  277. return (long)ReadUInt64Unchecked(Position);
  278. }
  279. public byte ReadByteUnchecked(long Position)
  280. {
  281. return *((byte*)(RamPtr + (uint)Position));
  282. }
  283. public ushort ReadUInt16Unchecked(long Position)
  284. {
  285. return *((ushort*)(RamPtr + (uint)Position));
  286. }
  287. public uint ReadUInt32Unchecked(long Position)
  288. {
  289. return *((uint*)(RamPtr + (uint)Position));
  290. }
  291. public ulong ReadUInt64Unchecked(long Position)
  292. {
  293. return *((ulong*)(RamPtr + (uint)Position));
  294. }
  295. public Vector128<float> ReadVector8Unchecked(long Position)
  296. {
  297. if (Sse2.IsSupported)
  298. {
  299. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(Position)));
  300. }
  301. else
  302. {
  303. throw new PlatformNotSupportedException();
  304. }
  305. }
  306. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  307. public Vector128<float> ReadVector16Unchecked(long Position)
  308. {
  309. if (Sse2.IsSupported)
  310. {
  311. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16Unchecked(Position), 0));
  312. }
  313. else
  314. {
  315. throw new PlatformNotSupportedException();
  316. }
  317. }
  318. [MethodImpl(MethodImplOptions.NoInlining)]
  319. public Vector128<float> ReadVector32Unchecked(long Position)
  320. {
  321. if (Sse.IsSupported)
  322. {
  323. return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
  324. }
  325. else
  326. {
  327. throw new PlatformNotSupportedException();
  328. }
  329. }
  330. [MethodImpl(MethodImplOptions.NoInlining)]
  331. public Vector128<float> ReadVector64Unchecked(long Position)
  332. {
  333. if (Sse2.IsSupported)
  334. {
  335. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
  336. }
  337. else
  338. {
  339. throw new PlatformNotSupportedException();
  340. }
  341. }
  342. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  343. public Vector128<float> ReadVector128Unchecked(long Position)
  344. {
  345. if (Sse.IsSupported)
  346. {
  347. return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
  348. }
  349. else
  350. {
  351. throw new PlatformNotSupportedException();
  352. }
  353. }
  354. public byte[] ReadBytes(long Position, long Size)
  355. {
  356. if ((uint)Size > int.MaxValue)
  357. {
  358. throw new ArgumentOutOfRangeException(nameof(Size));
  359. }
  360. EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
  361. byte[] Data = new byte[Size];
  362. Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
  363. return Data;
  364. }
  365. public void WriteSByte(long Position, sbyte Value)
  366. {
  367. WriteByte(Position, (byte)Value);
  368. }
  369. public void WriteInt16(long Position, short Value)
  370. {
  371. WriteUInt16(Position, (ushort)Value);
  372. }
  373. public void WriteInt32(long Position, int Value)
  374. {
  375. WriteUInt32(Position, (uint)Value);
  376. }
  377. public void WriteInt64(long Position, long Value)
  378. {
  379. WriteUInt64(Position, (ulong)Value);
  380. }
  381. public void WriteByte(long Position, byte Value)
  382. {
  383. EnsureAccessIsValid(Position, AMemoryPerm.Write);
  384. WriteByteUnchecked(Position, Value);
  385. }
  386. public void WriteUInt16(long Position, ushort Value)
  387. {
  388. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  389. EnsureAccessIsValid(Position + 1, AMemoryPerm.Write);
  390. WriteUInt16Unchecked(Position, Value);
  391. }
  392. public void WriteUInt32(long Position, uint Value)
  393. {
  394. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  395. EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
  396. WriteUInt32Unchecked(Position, Value);
  397. }
  398. public void WriteUInt64(long Position, ulong Value)
  399. {
  400. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  401. EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
  402. WriteUInt64Unchecked(Position, Value);
  403. }
  404. public void WriteVector8(long Position, Vector128<float> Value)
  405. {
  406. if (Sse41.IsSupported)
  407. {
  408. WriteByte(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
  409. }
  410. else if (Sse2.IsSupported)
  411. {
  412. WriteByte(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  413. }
  414. else
  415. {
  416. throw new PlatformNotSupportedException();
  417. }
  418. }
  419. public void WriteVector16(long Position, Vector128<float> Value)
  420. {
  421. if (Sse2.IsSupported)
  422. {
  423. WriteUInt16(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  424. }
  425. else
  426. {
  427. throw new PlatformNotSupportedException();
  428. }
  429. }
  430. public void WriteVector32(long Position, Vector128<float> Value)
  431. {
  432. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  433. EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
  434. if (Sse.IsSupported)
  435. {
  436. Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
  437. }
  438. else
  439. {
  440. throw new PlatformNotSupportedException();
  441. }
  442. }
  443. public void WriteVector64(long Position, Vector128<float> Value)
  444. {
  445. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  446. EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
  447. if (Sse2.IsSupported)
  448. {
  449. Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
  450. }
  451. else
  452. {
  453. throw new PlatformNotSupportedException();
  454. }
  455. }
  456. public void WriteVector128(long Position, Vector128<float> Value)
  457. {
  458. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  459. EnsureAccessIsValid(Position + 15, AMemoryPerm.Write);
  460. if (Sse.IsSupported)
  461. {
  462. Sse.Store((float*)(RamPtr + (uint)Position), Value);
  463. }
  464. else
  465. {
  466. throw new PlatformNotSupportedException();
  467. }
  468. }
  469. public void WriteSByteUnchecked(long Position, sbyte Value)
  470. {
  471. WriteByteUnchecked(Position, (byte)Value);
  472. }
  473. public void WriteInt16Unchecked(long Position, short Value)
  474. {
  475. WriteUInt16Unchecked(Position, (ushort)Value);
  476. }
  477. public void WriteInt32Unchecked(long Position, int Value)
  478. {
  479. WriteUInt32Unchecked(Position, (uint)Value);
  480. }
  481. public void WriteInt64Unchecked(long Position, long Value)
  482. {
  483. WriteUInt64Unchecked(Position, (ulong)Value);
  484. }
  485. public void WriteByteUnchecked(long Position, byte Value)
  486. {
  487. *((byte*)(RamPtr + (uint)Position)) = Value;
  488. }
  489. public void WriteUInt16Unchecked(long Position, ushort Value)
  490. {
  491. *((ushort*)(RamPtr + (uint)Position)) = Value;
  492. }
  493. public void WriteUInt32Unchecked(long Position, uint Value)
  494. {
  495. *((uint*)(RamPtr + (uint)Position)) = Value;
  496. }
  497. public void WriteUInt64Unchecked(long Position, ulong Value)
  498. {
  499. *((ulong*)(RamPtr + (uint)Position)) = Value;
  500. }
  501. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  502. public void WriteVector8Unchecked(long Position, Vector128<float> Value)
  503. {
  504. if (Sse41.IsSupported)
  505. {
  506. WriteByteUnchecked(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
  507. }
  508. else if (Sse2.IsSupported)
  509. {
  510. WriteByteUnchecked(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  511. }
  512. else
  513. {
  514. throw new PlatformNotSupportedException();
  515. }
  516. }
  517. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  518. public void WriteVector16Unchecked(long Position, Vector128<float> Value)
  519. {
  520. if (Sse2.IsSupported)
  521. {
  522. WriteUInt16Unchecked(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  523. }
  524. else
  525. {
  526. throw new PlatformNotSupportedException();
  527. }
  528. }
  529. [MethodImpl(MethodImplOptions.NoInlining)]
  530. public void WriteVector32Unchecked(long Position, Vector128<float> Value)
  531. {
  532. if (Sse.IsSupported)
  533. {
  534. Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
  535. }
  536. else
  537. {
  538. throw new PlatformNotSupportedException();
  539. }
  540. }
  541. [MethodImpl(MethodImplOptions.NoInlining)]
  542. public void WriteVector64Unchecked(long Position, Vector128<float> Value)
  543. {
  544. if (Sse2.IsSupported)
  545. {
  546. Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
  547. }
  548. else
  549. {
  550. throw new PlatformNotSupportedException();
  551. }
  552. }
  553. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  554. public void WriteVector128Unchecked(long Position, Vector128<float> Value)
  555. {
  556. if (Sse.IsSupported)
  557. {
  558. Sse.Store((float*)(RamPtr + (uint)Position), Value);
  559. }
  560. else
  561. {
  562. throw new PlatformNotSupportedException();
  563. }
  564. }
  565. public void WriteBytes(long Position, byte[] Data)
  566. {
  567. EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
  568. Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
  569. }
  570. private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
  571. {
  572. long EndPos = Position + Size;
  573. Position &= ~AMemoryMgr.PageMask;
  574. while ((ulong)Position < (ulong)EndPos)
  575. {
  576. EnsureAccessIsValid(Position, Perm);
  577. Position += AMemoryMgr.PageSize;
  578. }
  579. }
  580. private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
  581. {
  582. if (!Manager.IsMapped(Position))
  583. {
  584. throw new VmmPageFaultException(Position);
  585. }
  586. if (!Manager.HasPermission(Position, Perm))
  587. {
  588. throw new VmmAccessViolationException(Position, Perm);
  589. }
  590. }
  591. public void Dispose()
  592. {
  593. Dispose(true);
  594. }
  595. protected virtual void Dispose(bool disposing)
  596. {
  597. if (Ram != IntPtr.Zero)
  598. {
  599. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  600. {
  601. AMemoryWin32.Free(Ram);
  602. }
  603. else
  604. {
  605. Marshal.FreeHGlobal(Ram);
  606. }
  607. Ram = IntPtr.Zero;
  608. }
  609. }
  610. }
  611. }