AMemory.cs 20 KB

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