AMemory.cs 6.9 KB

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