NativeInterface.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 class ThreadContext
  11. {
  12. public ExecutionContext Context { get; }
  13. public IMemoryManager Memory { get; }
  14. public Translator Translator { get; }
  15. public ThreadContext(ExecutionContext context, IMemoryManager memory, Translator translator)
  16. {
  17. Context = context;
  18. Memory = memory;
  19. Translator = translator;
  20. }
  21. }
  22. [ThreadStatic]
  23. private static ThreadContext _context;
  24. public static void RegisterThread(ExecutionContext context, IMemoryManager memory, Translator translator)
  25. {
  26. _context = new ThreadContext(context, memory, translator);
  27. }
  28. public static void UnregisterThread()
  29. {
  30. _context = null;
  31. }
  32. public static void Break(ulong address, int imm)
  33. {
  34. Statistics.PauseTimer();
  35. GetContext().OnBreak(address, imm);
  36. Statistics.ResumeTimer();
  37. }
  38. public static void SupervisorCall(ulong address, int imm)
  39. {
  40. Statistics.PauseTimer();
  41. GetContext().OnSupervisorCall(address, imm);
  42. Statistics.ResumeTimer();
  43. }
  44. public static void Undefined(ulong address, int opCode)
  45. {
  46. Statistics.PauseTimer();
  47. GetContext().OnUndefined(address, opCode);
  48. Statistics.ResumeTimer();
  49. }
  50. #region "System registers"
  51. public static ulong GetCtrEl0()
  52. {
  53. return (ulong)GetContext().CtrEl0;
  54. }
  55. public static ulong GetDczidEl0()
  56. {
  57. return (ulong)GetContext().DczidEl0;
  58. }
  59. public static ulong GetFpcr()
  60. {
  61. return (ulong)GetContext().Fpcr;
  62. }
  63. public static ulong GetFpsr()
  64. {
  65. return (ulong)GetContext().Fpsr;
  66. }
  67. public static uint GetFpscr()
  68. {
  69. var context = GetContext();
  70. return (uint)(context.Fpsr & FPSR.A32Mask & ~FPSR.Nzcv) |
  71. (uint)(context.Fpcr & FPCR.A32Mask);
  72. }
  73. public static ulong GetTpidrEl0()
  74. {
  75. return (ulong)GetContext().TpidrEl0;
  76. }
  77. public static uint GetTpidrEl032()
  78. {
  79. return (uint)GetContext().TpidrEl0;
  80. }
  81. public static ulong GetTpidr()
  82. {
  83. return (ulong)GetContext().Tpidr;
  84. }
  85. public static uint GetTpidr32()
  86. {
  87. return (uint)GetContext().Tpidr;
  88. }
  89. public static ulong GetCntfrqEl0()
  90. {
  91. return GetContext().CntfrqEl0;
  92. }
  93. public static ulong GetCntpctEl0()
  94. {
  95. return GetContext().CntpctEl0;
  96. }
  97. public static ulong GetCntvctEl0()
  98. {
  99. return GetContext().CntvctEl0;
  100. }
  101. public static void SetFpcr(ulong value)
  102. {
  103. GetContext().Fpcr = (FPCR)value;
  104. }
  105. public static void SetFpsr(ulong value)
  106. {
  107. GetContext().Fpsr = (FPSR)value;
  108. }
  109. public static void SetFpsrQc()
  110. {
  111. GetContext().Fpsr |= FPSR.Qc;
  112. }
  113. public static void SetFpscr(uint fpscr)
  114. {
  115. var context = GetContext();
  116. context.Fpsr = FPSR.A32Mask & (FPSR)fpscr;
  117. context.Fpcr = FPCR.A32Mask & (FPCR)fpscr;
  118. }
  119. public static void SetTpidrEl0(ulong value)
  120. {
  121. GetContext().TpidrEl0 = (long)value;
  122. }
  123. public static void SetTpidrEl032(uint value)
  124. {
  125. GetContext().TpidrEl0 = (long)value;
  126. }
  127. #endregion
  128. #region "Read"
  129. public static byte ReadByte(ulong address)
  130. {
  131. return GetMemoryManager().ReadTracked<byte>(address);
  132. }
  133. public static ushort ReadUInt16(ulong address)
  134. {
  135. return GetMemoryManager().ReadTracked<ushort>(address);
  136. }
  137. public static uint ReadUInt32(ulong address)
  138. {
  139. return GetMemoryManager().ReadTracked<uint>(address);
  140. }
  141. public static ulong ReadUInt64(ulong address)
  142. {
  143. return GetMemoryManager().ReadTracked<ulong>(address);
  144. }
  145. public static V128 ReadVector128(ulong address)
  146. {
  147. return GetMemoryManager().ReadTracked<V128>(address);
  148. }
  149. #endregion
  150. #region "Write"
  151. public static void WriteByte(ulong address, byte value)
  152. {
  153. GetMemoryManager().Write(address, value);
  154. }
  155. public static void WriteUInt16(ulong address, ushort value)
  156. {
  157. GetMemoryManager().Write(address, value);
  158. }
  159. public static void WriteUInt32(ulong address, uint value)
  160. {
  161. GetMemoryManager().Write(address, value);
  162. }
  163. public static void WriteUInt64(ulong address, ulong value)
  164. {
  165. GetMemoryManager().Write(address, value);
  166. }
  167. public static void WriteVector128(ulong address, V128 value)
  168. {
  169. GetMemoryManager().Write(address, value);
  170. }
  171. #endregion
  172. public static void SignalMemoryTracking(ulong address, ulong size, bool write)
  173. {
  174. GetMemoryManager().SignalMemoryTracking(address, size, write);
  175. }
  176. public static void ThrowInvalidMemoryAccess(ulong address)
  177. {
  178. throw new InvalidAccessException(address);
  179. }
  180. public static ulong GetFunctionAddress(ulong address)
  181. {
  182. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  183. return (ulong)function.FuncPtr.ToInt64();
  184. }
  185. public static ulong GetIndirectFunctionAddress(ulong address, ulong entryAddress)
  186. {
  187. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  188. ulong ptr = (ulong)function.FuncPtr.ToInt64();
  189. if (function.HighCq)
  190. {
  191. // Rewrite the host function address in the table to point to the highCq function.
  192. Marshal.WriteInt64((IntPtr)entryAddress, 8, (long)ptr);
  193. }
  194. return ptr;
  195. }
  196. public static bool CheckSynchronization()
  197. {
  198. Statistics.PauseTimer();
  199. var context = GetContext();
  200. context.CheckInterrupt();
  201. Statistics.ResumeTimer();
  202. return context.Running;
  203. }
  204. public static ExecutionContext GetContext()
  205. {
  206. return _context.Context;
  207. }
  208. public static IMemoryManager GetMemoryManager()
  209. {
  210. return _context.Memory;
  211. }
  212. }
  213. }