NativeInterface.cs 13 KB

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