NativeInterface.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 ulong GetCntvctEl0()
  108. {
  109. return GetContext().CntvctEl0;
  110. }
  111. public static void SetFpcr(ulong value)
  112. {
  113. GetContext().Fpcr = (FPCR)value;
  114. }
  115. public static void SetFpsr(ulong value)
  116. {
  117. GetContext().Fpsr = (FPSR)value;
  118. }
  119. public static void SetFpscr(uint value)
  120. {
  121. var context = GetContext();
  122. context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0);
  123. context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0);
  124. context.SetFPstateFlag(FPState.CFlag, (value & (1u << 29)) != 0);
  125. context.SetFPstateFlag(FPState.VFlag, (value & (1u << 28)) != 0);
  126. context.Fpsr = FPSR.A32Mask & (FPSR)value;
  127. context.Fpcr = FPCR.A32Mask & (FPCR)value;
  128. }
  129. public static void SetTpidrEl0(ulong value)
  130. {
  131. GetContext().TpidrEl0 = (long)value;
  132. }
  133. public static void SetTpidrEl032(uint value)
  134. {
  135. GetContext().TpidrEl0 = (long)value;
  136. }
  137. #endregion
  138. #region "Read"
  139. public static byte ReadByte(ulong address)
  140. {
  141. return GetMemoryManager().Read<byte>(address);
  142. }
  143. public static ushort ReadUInt16(ulong address)
  144. {
  145. return GetMemoryManager().Read<ushort>(address);
  146. }
  147. public static uint ReadUInt32(ulong address)
  148. {
  149. return GetMemoryManager().Read<uint>(address);
  150. }
  151. public static ulong ReadUInt64(ulong address)
  152. {
  153. return GetMemoryManager().Read<ulong>(address);
  154. }
  155. public static V128 ReadVector128(ulong address)
  156. {
  157. return GetMemoryManager().Read<V128>(address);
  158. }
  159. #endregion
  160. #region "Read exclusive"
  161. public static byte ReadByteExclusive(ulong address)
  162. {
  163. byte value = _context.Memory.Read<byte>(address);
  164. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  165. _context.ExclusiveValueLow = value;
  166. _context.ExclusiveValueHigh = 0;
  167. return value;
  168. }
  169. public static ushort ReadUInt16Exclusive(ulong address)
  170. {
  171. ushort value = _context.Memory.Read<ushort>(address);
  172. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  173. _context.ExclusiveValueLow = value;
  174. _context.ExclusiveValueHigh = 0;
  175. return value;
  176. }
  177. public static uint ReadUInt32Exclusive(ulong address)
  178. {
  179. uint value = _context.Memory.Read<uint>(address);
  180. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  181. _context.ExclusiveValueLow = value;
  182. _context.ExclusiveValueHigh = 0;
  183. return value;
  184. }
  185. public static ulong ReadUInt64Exclusive(ulong address)
  186. {
  187. ulong value = _context.Memory.Read<ulong>(address);
  188. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  189. _context.ExclusiveValueLow = value;
  190. _context.ExclusiveValueHigh = 0;
  191. return value;
  192. }
  193. public static V128 ReadVector128Exclusive(ulong address)
  194. {
  195. V128 value = MemoryManagerPal.AtomicLoad128(ref _context.Memory.GetRef<V128>(address));
  196. _context.ExclusiveAddress = GetMaskedExclusiveAddress(address);
  197. _context.ExclusiveValueLow = value.Extract<ulong>(0);
  198. _context.ExclusiveValueHigh = value.Extract<ulong>(1);
  199. return value;
  200. }
  201. #endregion
  202. #region "Write"
  203. public static void WriteByte(ulong address, byte value)
  204. {
  205. GetMemoryManager().Write(address, value);
  206. }
  207. public static void WriteUInt16(ulong address, ushort value)
  208. {
  209. GetMemoryManager().Write(address, value);
  210. }
  211. public static void WriteUInt32(ulong address, uint value)
  212. {
  213. GetMemoryManager().Write(address, value);
  214. }
  215. public static void WriteUInt64(ulong address, ulong value)
  216. {
  217. GetMemoryManager().Write(address, value);
  218. }
  219. public static void WriteVector128(ulong address, V128 value)
  220. {
  221. GetMemoryManager().Write(address, value);
  222. }
  223. #endregion
  224. #region "Write exclusive"
  225. public static int WriteByteExclusive(ulong address, byte value)
  226. {
  227. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  228. if (success)
  229. {
  230. ref int valueRef = ref _context.Memory.GetRefNoChecks<int>(address);
  231. int currentValue = valueRef;
  232. byte expected = (byte)_context.ExclusiveValueLow;
  233. int expected32 = (currentValue & ~byte.MaxValue) | expected;
  234. int desired32 = (currentValue & ~byte.MaxValue) | value;
  235. success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32;
  236. if (success)
  237. {
  238. ClearExclusive();
  239. }
  240. }
  241. return success ? 0 : 1;
  242. }
  243. public static int WriteUInt16Exclusive(ulong address, ushort value)
  244. {
  245. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  246. if (success)
  247. {
  248. ref int valueRef = ref _context.Memory.GetRefNoChecks<int>(address);
  249. int currentValue = valueRef;
  250. ushort expected = (ushort)_context.ExclusiveValueLow;
  251. int expected32 = (currentValue & ~ushort.MaxValue) | expected;
  252. int desired32 = (currentValue & ~ushort.MaxValue) | value;
  253. success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32;
  254. if (success)
  255. {
  256. ClearExclusive();
  257. }
  258. }
  259. return success ? 0 : 1;
  260. }
  261. public static int WriteUInt32Exclusive(ulong address, uint value)
  262. {
  263. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  264. if (success)
  265. {
  266. ref int valueRef = ref _context.Memory.GetRef<int>(address);
  267. success = Interlocked.CompareExchange(ref valueRef, (int)value, (int)_context.ExclusiveValueLow) == (int)_context.ExclusiveValueLow;
  268. if (success)
  269. {
  270. ClearExclusive();
  271. }
  272. }
  273. return success ? 0 : 1;
  274. }
  275. public static int WriteUInt64Exclusive(ulong address, ulong value)
  276. {
  277. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  278. if (success)
  279. {
  280. ref long valueRef = ref _context.Memory.GetRef<long>(address);
  281. success = Interlocked.CompareExchange(ref valueRef, (long)value, (long)_context.ExclusiveValueLow) == (long)_context.ExclusiveValueLow;
  282. if (success)
  283. {
  284. ClearExclusive();
  285. }
  286. }
  287. return success ? 0 : 1;
  288. }
  289. public static int WriteVector128Exclusive(ulong address, V128 value)
  290. {
  291. bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address);
  292. if (success)
  293. {
  294. V128 expected = new V128(_context.ExclusiveValueLow, _context.ExclusiveValueHigh);
  295. ref V128 location = ref _context.Memory.GetRef<V128>(address);
  296. success = MemoryManagerPal.CompareAndSwap128(ref location, expected, value) == expected;
  297. if (success)
  298. {
  299. ClearExclusive();
  300. }
  301. }
  302. return success ? 0 : 1;
  303. }
  304. #endregion
  305. private static ulong GetMaskedExclusiveAddress(ulong address)
  306. {
  307. return address & ~((4UL << ErgSizeLog2) - 1);
  308. }
  309. public static ulong GetFunctionAddress(ulong address)
  310. {
  311. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  312. return (ulong)function.GetPointer().ToInt64();
  313. }
  314. public static ulong GetIndirectFunctionAddress(ulong address, ulong entryAddress)
  315. {
  316. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  317. ulong ptr = (ulong)function.GetPointer().ToInt64();
  318. if (function.HighCq)
  319. {
  320. // Rewrite the host function address in the table to point to the highCq function.
  321. Marshal.WriteInt64((IntPtr)entryAddress, 8, (long)ptr);
  322. }
  323. return ptr;
  324. }
  325. public static void ClearExclusive()
  326. {
  327. _context.ExclusiveAddress = ulong.MaxValue;
  328. }
  329. public static bool CheckSynchronization()
  330. {
  331. Statistics.PauseTimer();
  332. var context = GetContext();
  333. context.CheckInterrupt();
  334. Statistics.ResumeTimer();
  335. return context.Running;
  336. }
  337. public static State.ExecutionContext GetContext()
  338. {
  339. return _context.Context;
  340. }
  341. public static IMemoryManager GetMemoryManager()
  342. {
  343. return _context.Memory;
  344. }
  345. }
  346. }