AMemory.cs 21 KB

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