NativeInterface.cs 12 KB

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