AMemory.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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[] IsRegionModified(long Position, long Size)
  129. {
  130. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  131. {
  132. return null;
  133. }
  134. long EndPos = Position + Size;
  135. if ((ulong)EndPos < (ulong)Position)
  136. {
  137. return null;
  138. }
  139. if ((ulong)EndPos > AMemoryMgr.RamSize)
  140. {
  141. return null;
  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;
  157. }
  158. public sbyte ReadSByte(long Position)
  159. {
  160. return (sbyte)ReadByte(Position);
  161. }
  162. public short ReadInt16(long Position)
  163. {
  164. return (short)ReadUInt16(Position);
  165. }
  166. public int ReadInt32(long Position)
  167. {
  168. return (int)ReadUInt32(Position);
  169. }
  170. public long ReadInt64(long Position)
  171. {
  172. return (long)ReadUInt64(Position);
  173. }
  174. public byte ReadByte(long Position)
  175. {
  176. EnsureAccessIsValid(Position, AMemoryPerm.Read);
  177. return ReadByteUnchecked(Position);
  178. }
  179. public ushort ReadUInt16(long Position)
  180. {
  181. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  182. EnsureAccessIsValid(Position + 1, AMemoryPerm.Read);
  183. return ReadUInt16Unchecked(Position);
  184. }
  185. public uint ReadUInt32(long Position)
  186. {
  187. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  188. EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
  189. return ReadUInt32Unchecked(Position);
  190. }
  191. public ulong ReadUInt64(long Position)
  192. {
  193. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  194. EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
  195. return ReadUInt64Unchecked(Position);
  196. }
  197. public Vector128<float> ReadVector8(long Position)
  198. {
  199. if (Sse2.IsSupported)
  200. {
  201. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(Position)));
  202. }
  203. else
  204. {
  205. throw new PlatformNotSupportedException();
  206. }
  207. }
  208. public Vector128<float> ReadVector16(long Position)
  209. {
  210. if (Sse2.IsSupported)
  211. {
  212. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16(Position), 0));
  213. }
  214. else
  215. {
  216. throw new PlatformNotSupportedException();
  217. }
  218. }
  219. public Vector128<float> ReadVector32(long Position)
  220. {
  221. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  222. EnsureAccessIsValid(Position + 3, AMemoryPerm.Read);
  223. if (Sse.IsSupported)
  224. {
  225. return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
  226. }
  227. else
  228. {
  229. throw new PlatformNotSupportedException();
  230. }
  231. }
  232. public Vector128<float> ReadVector64(long Position)
  233. {
  234. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  235. EnsureAccessIsValid(Position + 7, AMemoryPerm.Read);
  236. if (Sse2.IsSupported)
  237. {
  238. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
  239. }
  240. else
  241. {
  242. throw new PlatformNotSupportedException();
  243. }
  244. }
  245. public Vector128<float> ReadVector128(long Position)
  246. {
  247. EnsureAccessIsValid(Position + 0, AMemoryPerm.Read);
  248. EnsureAccessIsValid(Position + 15, AMemoryPerm.Read);
  249. if (Sse.IsSupported)
  250. {
  251. return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
  252. }
  253. else
  254. {
  255. throw new PlatformNotSupportedException();
  256. }
  257. }
  258. public sbyte ReadSByteUnchecked(long Position)
  259. {
  260. return (sbyte)ReadByteUnchecked(Position);
  261. }
  262. public short ReadInt16Unchecked(long Position)
  263. {
  264. return (short)ReadUInt16Unchecked(Position);
  265. }
  266. public int ReadInt32Unchecked(long Position)
  267. {
  268. return (int)ReadUInt32Unchecked(Position);
  269. }
  270. public long ReadInt64Unchecked(long Position)
  271. {
  272. return (long)ReadUInt64Unchecked(Position);
  273. }
  274. public byte ReadByteUnchecked(long Position)
  275. {
  276. return *((byte*)(RamPtr + (uint)Position));
  277. }
  278. public ushort ReadUInt16Unchecked(long Position)
  279. {
  280. return *((ushort*)(RamPtr + (uint)Position));
  281. }
  282. public uint ReadUInt32Unchecked(long Position)
  283. {
  284. return *((uint*)(RamPtr + (uint)Position));
  285. }
  286. public ulong ReadUInt64Unchecked(long Position)
  287. {
  288. return *((ulong*)(RamPtr + (uint)Position));
  289. }
  290. public Vector128<float> ReadVector8Unchecked(long Position)
  291. {
  292. if (Sse2.IsSupported)
  293. {
  294. return Sse.StaticCast<byte, float>(Sse2.SetVector128(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ReadByte(Position)));
  295. }
  296. else
  297. {
  298. throw new PlatformNotSupportedException();
  299. }
  300. }
  301. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  302. public Vector128<float> ReadVector16Unchecked(long Position)
  303. {
  304. if (Sse2.IsSupported)
  305. {
  306. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse2.SetZeroVector128<ushort>(), ReadUInt16Unchecked(Position), 0));
  307. }
  308. else
  309. {
  310. throw new PlatformNotSupportedException();
  311. }
  312. }
  313. [MethodImpl(MethodImplOptions.NoInlining)]
  314. public Vector128<float> ReadVector32Unchecked(long Position)
  315. {
  316. if (Sse.IsSupported)
  317. {
  318. return Sse.LoadScalarVector128((float*)(RamPtr + (uint)Position));
  319. }
  320. else
  321. {
  322. throw new PlatformNotSupportedException();
  323. }
  324. }
  325. [MethodImpl(MethodImplOptions.NoInlining)]
  326. public Vector128<float> ReadVector64Unchecked(long Position)
  327. {
  328. if (Sse2.IsSupported)
  329. {
  330. return Sse.StaticCast<double, float>(Sse2.LoadScalarVector128((double*)(RamPtr + (uint)Position)));
  331. }
  332. else
  333. {
  334. throw new PlatformNotSupportedException();
  335. }
  336. }
  337. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  338. public Vector128<float> ReadVector128Unchecked(long Position)
  339. {
  340. if (Sse.IsSupported)
  341. {
  342. return Sse.LoadVector128((float*)(RamPtr + (uint)Position));
  343. }
  344. else
  345. {
  346. throw new PlatformNotSupportedException();
  347. }
  348. }
  349. public byte[] ReadBytes(long Position, long Size)
  350. {
  351. if ((uint)Size > int.MaxValue)
  352. {
  353. throw new ArgumentOutOfRangeException(nameof(Size));
  354. }
  355. EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
  356. byte[] Data = new byte[Size];
  357. Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
  358. return Data;
  359. }
  360. public void WriteSByte(long Position, sbyte Value)
  361. {
  362. WriteByte(Position, (byte)Value);
  363. }
  364. public void WriteInt16(long Position, short Value)
  365. {
  366. WriteUInt16(Position, (ushort)Value);
  367. }
  368. public void WriteInt32(long Position, int Value)
  369. {
  370. WriteUInt32(Position, (uint)Value);
  371. }
  372. public void WriteInt64(long Position, long Value)
  373. {
  374. WriteUInt64(Position, (ulong)Value);
  375. }
  376. public void WriteByte(long Position, byte Value)
  377. {
  378. EnsureAccessIsValid(Position, AMemoryPerm.Write);
  379. WriteByteUnchecked(Position, Value);
  380. }
  381. public void WriteUInt16(long Position, ushort Value)
  382. {
  383. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  384. EnsureAccessIsValid(Position + 1, AMemoryPerm.Write);
  385. WriteUInt16Unchecked(Position, Value);
  386. }
  387. public void WriteUInt32(long Position, uint Value)
  388. {
  389. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  390. EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
  391. WriteUInt32Unchecked(Position, Value);
  392. }
  393. public void WriteUInt64(long Position, ulong Value)
  394. {
  395. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  396. EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
  397. WriteUInt64Unchecked(Position, Value);
  398. }
  399. public void WriteVector8(long Position, Vector128<float> Value)
  400. {
  401. if (Sse41.IsSupported)
  402. {
  403. WriteByte(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
  404. }
  405. else if (Sse2.IsSupported)
  406. {
  407. WriteByte(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  408. }
  409. else
  410. {
  411. throw new PlatformNotSupportedException();
  412. }
  413. }
  414. public void WriteVector16(long Position, Vector128<float> Value)
  415. {
  416. if (Sse2.IsSupported)
  417. {
  418. WriteUInt16(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  419. }
  420. else
  421. {
  422. throw new PlatformNotSupportedException();
  423. }
  424. }
  425. public void WriteVector32(long Position, Vector128<float> Value)
  426. {
  427. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  428. EnsureAccessIsValid(Position + 3, AMemoryPerm.Write);
  429. if (Sse.IsSupported)
  430. {
  431. Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
  432. }
  433. else
  434. {
  435. throw new PlatformNotSupportedException();
  436. }
  437. }
  438. public void WriteVector64(long Position, Vector128<float> Value)
  439. {
  440. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  441. EnsureAccessIsValid(Position + 7, AMemoryPerm.Write);
  442. if (Sse2.IsSupported)
  443. {
  444. Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
  445. }
  446. else
  447. {
  448. throw new PlatformNotSupportedException();
  449. }
  450. }
  451. public void WriteVector128(long Position, Vector128<float> Value)
  452. {
  453. EnsureAccessIsValid(Position + 0, AMemoryPerm.Write);
  454. EnsureAccessIsValid(Position + 15, AMemoryPerm.Write);
  455. if (Sse.IsSupported)
  456. {
  457. Sse.Store((float*)(RamPtr + (uint)Position), Value);
  458. }
  459. else
  460. {
  461. throw new PlatformNotSupportedException();
  462. }
  463. }
  464. public void WriteSByteUnchecked(long Position, sbyte Value)
  465. {
  466. WriteByteUnchecked(Position, (byte)Value);
  467. }
  468. public void WriteInt16Unchecked(long Position, short Value)
  469. {
  470. WriteUInt16Unchecked(Position, (ushort)Value);
  471. }
  472. public void WriteInt32Unchecked(long Position, int Value)
  473. {
  474. WriteUInt32Unchecked(Position, (uint)Value);
  475. }
  476. public void WriteInt64Unchecked(long Position, long Value)
  477. {
  478. WriteUInt64Unchecked(Position, (ulong)Value);
  479. }
  480. public void WriteByteUnchecked(long Position, byte Value)
  481. {
  482. *((byte*)(RamPtr + (uint)Position)) = Value;
  483. }
  484. public void WriteUInt16Unchecked(long Position, ushort Value)
  485. {
  486. *((ushort*)(RamPtr + (uint)Position)) = Value;
  487. }
  488. public void WriteUInt32Unchecked(long Position, uint Value)
  489. {
  490. *((uint*)(RamPtr + (uint)Position)) = Value;
  491. }
  492. public void WriteUInt64Unchecked(long Position, ulong Value)
  493. {
  494. *((ulong*)(RamPtr + (uint)Position)) = Value;
  495. }
  496. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  497. public void WriteVector8Unchecked(long Position, Vector128<float> Value)
  498. {
  499. if (Sse41.IsSupported)
  500. {
  501. WriteByteUnchecked(Position, Sse41.Extract(Sse.StaticCast<float, byte>(Value), 0));
  502. }
  503. else if (Sse2.IsSupported)
  504. {
  505. WriteByteUnchecked(Position, (byte)Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  506. }
  507. else
  508. {
  509. throw new PlatformNotSupportedException();
  510. }
  511. }
  512. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  513. public void WriteVector16Unchecked(long Position, Vector128<float> Value)
  514. {
  515. if (Sse2.IsSupported)
  516. {
  517. WriteUInt16Unchecked(Position, Sse2.Extract(Sse.StaticCast<float, ushort>(Value), 0));
  518. }
  519. else
  520. {
  521. throw new PlatformNotSupportedException();
  522. }
  523. }
  524. [MethodImpl(MethodImplOptions.NoInlining)]
  525. public void WriteVector32Unchecked(long Position, Vector128<float> Value)
  526. {
  527. if (Sse.IsSupported)
  528. {
  529. Sse.StoreScalar((float*)(RamPtr + (uint)Position), Value);
  530. }
  531. else
  532. {
  533. throw new PlatformNotSupportedException();
  534. }
  535. }
  536. [MethodImpl(MethodImplOptions.NoInlining)]
  537. public void WriteVector64Unchecked(long Position, Vector128<float> Value)
  538. {
  539. if (Sse2.IsSupported)
  540. {
  541. Sse2.StoreScalar((double*)(RamPtr + (uint)Position), Sse.StaticCast<float, double>(Value));
  542. }
  543. else
  544. {
  545. throw new PlatformNotSupportedException();
  546. }
  547. }
  548. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  549. public void WriteVector128Unchecked(long Position, Vector128<float> Value)
  550. {
  551. if (Sse.IsSupported)
  552. {
  553. Sse.Store((float*)(RamPtr + (uint)Position), Value);
  554. }
  555. else
  556. {
  557. throw new PlatformNotSupportedException();
  558. }
  559. }
  560. public void WriteBytes(long Position, byte[] Data)
  561. {
  562. EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
  563. Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
  564. }
  565. private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
  566. {
  567. long EndPos = Position + Size;
  568. Position &= ~AMemoryMgr.PageMask;
  569. while ((ulong)Position < (ulong)EndPos)
  570. {
  571. EnsureAccessIsValid(Position, Perm);
  572. Position += AMemoryMgr.PageSize;
  573. }
  574. }
  575. private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
  576. {
  577. if (!Manager.IsMapped(Position))
  578. {
  579. throw new VmmPageFaultException(Position);
  580. }
  581. if (!Manager.HasPermission(Position, Perm))
  582. {
  583. throw new VmmAccessViolationException(Position, Perm);
  584. }
  585. }
  586. public void Dispose()
  587. {
  588. Dispose(true);
  589. }
  590. protected virtual void Dispose(bool disposing)
  591. {
  592. if (Ram != IntPtr.Zero)
  593. {
  594. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  595. {
  596. AMemoryWin32.Free(Ram);
  597. }
  598. else
  599. {
  600. Marshal.FreeHGlobal(Ram);
  601. }
  602. Ram = IntPtr.Zero;
  603. }
  604. }
  605. }
  606. }