AMemory.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace ChocolArm64.Memory
  5. {
  6. public unsafe class AMemory
  7. {
  8. private const long ErgMask = (4 << ARegisters.ErgSizeLog2) - 1;
  9. public AMemoryMgr Manager { get; private set; }
  10. private struct ExMonitor
  11. {
  12. public long Position { get; private set; }
  13. private bool ExState;
  14. public ExMonitor(long Position, bool ExState)
  15. {
  16. this.Position = Position;
  17. this.ExState = ExState;
  18. }
  19. public bool HasExclusiveAccess(long Position)
  20. {
  21. return this.Position == Position && ExState;
  22. }
  23. public void Reset()
  24. {
  25. ExState = false;
  26. }
  27. }
  28. private Dictionary<int, ExMonitor> Monitors;
  29. private HashSet<long> ExAddrs;
  30. private byte* RamPtr;
  31. public AMemory(IntPtr Ram, AMemoryAlloc Allocator)
  32. {
  33. Manager = new AMemoryMgr(Allocator);
  34. Monitors = new Dictionary<int, ExMonitor>();
  35. ExAddrs = new HashSet<long>();
  36. RamPtr = (byte*)Ram;
  37. }
  38. public void RemoveMonitor(int ThreadId)
  39. {
  40. lock (Monitors)
  41. {
  42. if (Monitors.TryGetValue(ThreadId, out ExMonitor Monitor))
  43. {
  44. ExAddrs.Remove(Monitor.Position);
  45. }
  46. Monitors.Remove(ThreadId);
  47. }
  48. }
  49. public void SetExclusive(ARegisters Registers, long Position)
  50. {
  51. lock (Monitors)
  52. {
  53. Position &= ~ErgMask;
  54. if (Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  55. {
  56. ExAddrs.Remove(Monitor.Position);
  57. }
  58. bool ExState = ExAddrs.Add(Position);
  59. Monitor = new ExMonitor(Position, ExState);
  60. if (!Monitors.TryAdd(Registers.ThreadId, Monitor))
  61. {
  62. Monitors[Registers.ThreadId] = Monitor;
  63. }
  64. }
  65. }
  66. public bool TestExclusive(ARegisters Registers, long Position)
  67. {
  68. lock (Monitors)
  69. {
  70. Position &= ~ErgMask;
  71. if (!Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  72. {
  73. return false;
  74. }
  75. return Monitor.HasExclusiveAccess(Position);
  76. }
  77. }
  78. public void ClearExclusive(ARegisters Registers)
  79. {
  80. lock (Monitors)
  81. {
  82. if (Monitors.TryGetValue(Registers.ThreadId, out ExMonitor Monitor))
  83. {
  84. Monitor.Reset();
  85. ExAddrs.Remove(Monitor.Position);
  86. }
  87. }
  88. }
  89. public sbyte ReadSByte(long Position) => (sbyte)ReadByte (Position);
  90. public short ReadInt16(long Position) => (short)ReadUInt16(Position);
  91. public int ReadInt32(long Position) => (int)ReadUInt32(Position);
  92. public long ReadInt64(long Position) => (long)ReadUInt64(Position);
  93. public byte ReadByte(long Position)
  94. {
  95. return *((byte*)(RamPtr + Manager.GetPhys(Position, AMemoryPerm.Read)));
  96. }
  97. public ushort ReadUInt16(long Position)
  98. {
  99. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Read);
  100. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 2))
  101. {
  102. return *((ushort*)(RamPtr + PhysPos));
  103. }
  104. else
  105. {
  106. return (ushort)(
  107. ReadByte(Position + 0) << 0 |
  108. ReadByte(Position + 1) << 8);
  109. }
  110. }
  111. public uint ReadUInt32(long Position)
  112. {
  113. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Read);
  114. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 4))
  115. {
  116. return *((uint*)(RamPtr + PhysPos));
  117. }
  118. else
  119. {
  120. return (uint)(
  121. ReadUInt16(Position + 0) << 0 |
  122. ReadUInt16(Position + 2) << 16);
  123. }
  124. }
  125. public ulong ReadUInt64(long Position)
  126. {
  127. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Read);
  128. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 8))
  129. {
  130. return *((ulong*)(RamPtr + PhysPos));
  131. }
  132. else
  133. {
  134. return
  135. (ulong)ReadUInt32(Position + 0) << 0 |
  136. (ulong)ReadUInt32(Position + 4) << 32;
  137. }
  138. }
  139. public AVec ReadVector128(long Position)
  140. {
  141. return new AVec()
  142. {
  143. X0 = ReadUInt64(Position + 0),
  144. X1 = ReadUInt64(Position + 8)
  145. };
  146. }
  147. public void WriteSByte(long Position, sbyte Value) => WriteByte (Position, (byte)Value);
  148. public void WriteInt16(long Position, short Value) => WriteUInt16(Position, (ushort)Value);
  149. public void WriteInt32(long Position, int Value) => WriteUInt32(Position, (uint)Value);
  150. public void WriteInt64(long Position, long Value) => WriteUInt64(Position, (ulong)Value);
  151. public void WriteByte(long Position, byte Value)
  152. {
  153. *((byte*)(RamPtr + Manager.GetPhys(Position, AMemoryPerm.Write))) = Value;
  154. }
  155. public void WriteUInt16(long Position, ushort Value)
  156. {
  157. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Write);
  158. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 2))
  159. {
  160. *((ushort*)(RamPtr + PhysPos)) = Value;
  161. }
  162. else
  163. {
  164. WriteByte(Position + 0, (byte)(Value >> 0));
  165. WriteByte(Position + 1, (byte)(Value >> 8));
  166. }
  167. }
  168. public void WriteUInt32(long Position, uint Value)
  169. {
  170. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Write);
  171. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 4))
  172. {
  173. *((uint*)(RamPtr + PhysPos)) = Value;
  174. }
  175. else
  176. {
  177. WriteUInt16(Position + 0, (ushort)(Value >> 0));
  178. WriteUInt16(Position + 2, (ushort)(Value >> 16));
  179. }
  180. }
  181. public void WriteUInt64(long Position, ulong Value)
  182. {
  183. long PhysPos = Manager.GetPhys(Position, AMemoryPerm.Write);
  184. if (BitConverter.IsLittleEndian && !IsPageCrossed(Position, 8))
  185. {
  186. *((ulong*)(RamPtr + PhysPos)) = Value;
  187. }
  188. else
  189. {
  190. WriteUInt32(Position + 0, (uint)(Value >> 0));
  191. WriteUInt32(Position + 4, (uint)(Value >> 32));
  192. }
  193. }
  194. public void WriteVector128(long Position, AVec Value)
  195. {
  196. WriteUInt64(Position + 0, Value.X0);
  197. WriteUInt64(Position + 8, Value.X1);
  198. }
  199. private bool IsPageCrossed(long Position, int Size)
  200. {
  201. return (Position & AMemoryMgr.PageMask) + Size > AMemoryMgr.PageSize;
  202. }
  203. }
  204. }