NativeInterface.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using ARMeilleure.Memory;
  2. using ARMeilleure.State;
  3. using System;
  4. namespace ARMeilleure.Instructions
  5. {
  6. static class NativeInterface
  7. {
  8. private const int ErgSizeLog2 = 4;
  9. private class ThreadContext
  10. {
  11. public ExecutionContext Context { get; }
  12. public MemoryManager Memory { get; }
  13. public ulong ExclusiveAddress { get; set; }
  14. public ulong ExclusiveValueLow { get; set; }
  15. public ulong ExclusiveValueHigh { get; set; }
  16. public ThreadContext(ExecutionContext context, MemoryManager memory)
  17. {
  18. Context = context;
  19. Memory = memory;
  20. ExclusiveAddress = ulong.MaxValue;
  21. }
  22. }
  23. [ThreadStatic]
  24. private static ThreadContext _context;
  25. public static void RegisterThread(ExecutionContext context, MemoryManager memory)
  26. {
  27. _context = new ThreadContext(context, memory);
  28. }
  29. public static void UnregisterThread()
  30. {
  31. _context = null;
  32. }
  33. public static void Break(ulong address, int imm)
  34. {
  35. Statistics.PauseTimer();
  36. GetContext().OnBreak(address, imm);
  37. Statistics.ResumeTimer();
  38. }
  39. public static void SupervisorCall(ulong address, int imm)
  40. {
  41. Statistics.PauseTimer();
  42. GetContext().OnSupervisorCall(address, imm);
  43. Statistics.ResumeTimer();
  44. }
  45. public static void Undefined(ulong address, int opCode)
  46. {
  47. Statistics.PauseTimer();
  48. GetContext().OnUndefined(address, opCode);
  49. Statistics.ResumeTimer();
  50. }
  51. #region "System registers"
  52. public static ulong GetCtrEl0()
  53. {
  54. return (ulong)GetContext().CtrEl0;
  55. }
  56. public static ulong GetDczidEl0()
  57. {
  58. return (ulong)GetContext().DczidEl0;
  59. }
  60. public static ulong GetFpcr()
  61. {
  62. return (ulong)GetContext().Fpcr;
  63. }
  64. public static ulong GetFpsr()
  65. {
  66. return (ulong)GetContext().Fpsr;
  67. }
  68. public static uint GetFpscr()
  69. {
  70. ExecutionContext context = GetContext();
  71. uint result = (uint)(context.Fpsr & FPSR.A32Mask) | (uint)(context.Fpcr & FPCR.A32Mask);
  72. result |= context.GetFPstateFlag(FPState.NFlag) ? (1u << 31) : 0;
  73. result |= context.GetFPstateFlag(FPState.ZFlag) ? (1u << 30) : 0;
  74. result |= context.GetFPstateFlag(FPState.CFlag) ? (1u << 29) : 0;
  75. result |= context.GetFPstateFlag(FPState.VFlag) ? (1u << 28) : 0;
  76. return result;
  77. }
  78. public static ulong GetTpidrEl0()
  79. {
  80. return (ulong)GetContext().TpidrEl0;
  81. }
  82. public static uint GetTpidrEl032()
  83. {
  84. return (uint)GetContext().TpidrEl0;
  85. }
  86. public static ulong GetTpidr()
  87. {
  88. return (ulong)GetContext().Tpidr;
  89. }
  90. public static uint GetTpidr32()
  91. {
  92. return (uint)GetContext().Tpidr;
  93. }
  94. public static ulong GetCntfrqEl0()
  95. {
  96. return GetContext().CntfrqEl0;
  97. }
  98. public static ulong GetCntpctEl0()
  99. {
  100. return GetContext().CntpctEl0;
  101. }
  102. public static void SetFpcr(ulong value)
  103. {
  104. GetContext().Fpcr = (FPCR)value;
  105. }
  106. public static void SetFpsr(ulong value)
  107. {
  108. GetContext().Fpsr = (FPSR)value;
  109. }
  110. public static void SetFpscr(uint value)
  111. {
  112. ExecutionContext context = GetContext();
  113. context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0);
  114. context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0);
  115. context.SetFPstateFlag(FPState.CFlag, (value & (1u << 29)) != 0);
  116. context.SetFPstateFlag(FPState.VFlag, (value & (1u << 28)) != 0);
  117. context.Fpsr = FPSR.A32Mask & (FPSR)value;
  118. context.Fpcr = FPCR.A32Mask & (FPCR)value;
  119. }
  120. public static void SetTpidrEl0(ulong value)
  121. {
  122. GetContext().TpidrEl0 = (long)value;
  123. }
  124. public static void SetTpidrEl032(uint value)
  125. {
  126. GetContext().TpidrEl0 = (long)value;
  127. }
  128. #endregion
  129. #region "Read"
  130. public static byte ReadByte(ulong address)
  131. {
  132. return GetMemoryManager().ReadByte((long)address);
  133. }
  134. public static ushort ReadUInt16(ulong address)
  135. {
  136. return GetMemoryManager().ReadUInt16((long)address);
  137. }
  138. public static uint ReadUInt32(ulong address)
  139. {
  140. return GetMemoryManager().ReadUInt32((long)address);
  141. }
  142. public static ulong ReadUInt64(ulong address)
  143. {
  144. return GetMemoryManager().ReadUInt64((long)address);
  145. }
  146. public static V128 ReadVector128(ulong address)
  147. {
  148. return GetMemoryManager().ReadVector128((long)address);
  149. }
  150. #endregion
  151. #region "Read exclusive"
  152. public static byte ReadByteExclusive(ulong address)
  153. {
  154. byte value = _context.Memory.ReadByte((long)address);
  155. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  156. _context.ExclusiveValueLow = value;
  157. _context.ExclusiveValueHigh = 0;
  158. return value;
  159. }
  160. public static ushort ReadUInt16Exclusive(ulong address)
  161. {
  162. ushort value = _context.Memory.ReadUInt16((long)address);
  163. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  164. _context.ExclusiveValueLow = value;
  165. _context.ExclusiveValueHigh = 0;
  166. return value;
  167. }
  168. public static uint ReadUInt32Exclusive(ulong address)
  169. {
  170. uint value = _context.Memory.ReadUInt32((long)address);
  171. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  172. _context.ExclusiveValueLow = value;
  173. _context.ExclusiveValueHigh = 0;
  174. return value;
  175. }
  176. public static ulong ReadUInt64Exclusive(ulong address)
  177. {
  178. ulong value = _context.Memory.ReadUInt64((long)address);
  179. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  180. _context.ExclusiveValueLow = value;
  181. _context.ExclusiveValueHigh = 0;
  182. return value;
  183. }
  184. public static V128 ReadVector128Exclusive(ulong address)
  185. {
  186. V128 value = _context.Memory.AtomicLoadInt128((long)address);
  187. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  188. _context.ExclusiveValueLow = value.GetUInt64(0);
  189. _context.ExclusiveValueHigh = value.GetUInt64(1);
  190. return value;
  191. }
  192. #endregion
  193. #region "Write"
  194. public static void WriteByte(ulong address, byte value)
  195. {
  196. GetMemoryManager().WriteByte((long)address, value);
  197. }
  198. public static void WriteUInt16(ulong address, ushort value)
  199. {
  200. GetMemoryManager().WriteUInt16((long)address, value);
  201. }
  202. public static void WriteUInt32(ulong address, uint value)
  203. {
  204. GetMemoryManager().WriteUInt32((long)address, value);
  205. }
  206. public static void WriteUInt64(ulong address, ulong value)
  207. {
  208. GetMemoryManager().WriteUInt64((long)address, value);
  209. }
  210. public static void WriteVector128(ulong address, V128 value)
  211. {
  212. GetMemoryManager().WriteVector128((long)address, value);
  213. }
  214. #endregion
  215. #region "Write exclusive"
  216. public static int WriteByteExclusive(ulong address, byte value)
  217. {
  218. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  219. if (success)
  220. {
  221. success = _context.Memory.AtomicCompareExchangeByte(
  222. (long)address,
  223. (byte)_context.ExclusiveValueLow,
  224. (byte)value);
  225. if (success)
  226. {
  227. ClearExclusive();
  228. }
  229. }
  230. return success ? 0 : 1;
  231. }
  232. public static int WriteUInt16Exclusive(ulong address, ushort value)
  233. {
  234. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  235. if (success)
  236. {
  237. success = _context.Memory.AtomicCompareExchangeInt16(
  238. (long)address,
  239. (short)_context.ExclusiveValueLow,
  240. (short)value);
  241. if (success)
  242. {
  243. ClearExclusive();
  244. }
  245. }
  246. return success ? 0 : 1;
  247. }
  248. public static int WriteUInt32Exclusive(ulong address, uint value)
  249. {
  250. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  251. if (success)
  252. {
  253. success = _context.Memory.AtomicCompareExchangeInt32(
  254. (long)address,
  255. (int)_context.ExclusiveValueLow,
  256. (int)value);
  257. if (success)
  258. {
  259. ClearExclusive();
  260. }
  261. }
  262. return success ? 0 : 1;
  263. }
  264. public static int WriteUInt64Exclusive(ulong address, ulong value)
  265. {
  266. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  267. if (success)
  268. {
  269. success = _context.Memory.AtomicCompareExchangeInt64(
  270. (long)address,
  271. (long)_context.ExclusiveValueLow,
  272. (long)value);
  273. if (success)
  274. {
  275. ClearExclusive();
  276. }
  277. }
  278. return success ? 0 : 1;
  279. }
  280. public static int WriteVector128Exclusive(ulong address, V128 value)
  281. {
  282. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  283. if (success)
  284. {
  285. V128 expected = new V128(_context.ExclusiveValueLow, _context.ExclusiveValueHigh);
  286. success = _context.Memory.AtomicCompareExchangeInt128((long)address, expected, value);
  287. if (success)
  288. {
  289. ClearExclusive();
  290. }
  291. }
  292. return success ? 0 : 1;
  293. }
  294. #endregion
  295. private static ulong GetMaskedExclusiveAddress(ulong address)
  296. {
  297. return address & ~((4UL << ErgSizeLog2) - 1);
  298. }
  299. public static void ClearExclusive()
  300. {
  301. _context.ExclusiveAddress = ulong.MaxValue;
  302. }
  303. public static void CheckSynchronization()
  304. {
  305. Statistics.PauseTimer();
  306. GetContext().CheckInterrupt();
  307. Statistics.ResumeTimer();
  308. }
  309. public static ExecutionContext GetContext()
  310. {
  311. return _context.Context;
  312. }
  313. public static MemoryManager GetMemoryManager()
  314. {
  315. return _context.Memory;
  316. }
  317. }
  318. }