NativeInterface.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using ARMeilleure.Memory;
  2. using ARMeilleure.State;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. namespace ARMeilleure.Instructions
  8. {
  9. static class NativeInterface
  10. {
  11. private class ThreadContext
  12. {
  13. public ExecutionContext Context { get; }
  14. public IMemoryManager Memory { get; }
  15. public Translator Translator { get; }
  16. public ThreadContext(ExecutionContext context, IMemoryManager memory, Translator translator)
  17. {
  18. Context = context;
  19. Memory = memory;
  20. Translator = translator;
  21. }
  22. }
  23. [ThreadStatic]
  24. private static ThreadContext Context;
  25. public static void RegisterThread(ExecutionContext context, IMemoryManager memory, Translator translator)
  26. {
  27. Context = new ThreadContext(context, memory, translator);
  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 bool GetFpcrFz()
  65. {
  66. return (GetContext().Fpcr & FPCR.Fz) != 0;
  67. }
  68. public static ulong GetFpsr()
  69. {
  70. return (ulong)GetContext().Fpsr;
  71. }
  72. public static uint GetFpscr()
  73. {
  74. ExecutionContext context = GetContext();
  75. return (uint)(context.Fpsr & FPSR.A32Mask & ~FPSR.Nzcv) |
  76. (uint)(context.Fpcr & FPCR.A32Mask);
  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 ulong GetCntvctEl0()
  103. {
  104. return GetContext().CntvctEl0;
  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 SetFpsrQc()
  115. {
  116. GetContext().Fpsr |= FPSR.Qc;
  117. }
  118. public static void SetFpscr(uint fpscr)
  119. {
  120. ExecutionContext context = GetContext();
  121. context.Fpsr = FPSR.A32Mask & (FPSR)fpscr;
  122. context.Fpcr = FPCR.A32Mask & (FPCR)fpscr;
  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().ReadTracked<byte>(address);
  137. }
  138. public static ushort ReadUInt16(ulong address)
  139. {
  140. return GetMemoryManager().ReadTracked<ushort>(address);
  141. }
  142. public static uint ReadUInt32(ulong address)
  143. {
  144. return GetMemoryManager().ReadTracked<uint>(address);
  145. }
  146. public static ulong ReadUInt64(ulong address)
  147. {
  148. return GetMemoryManager().ReadTracked<ulong>(address);
  149. }
  150. public static V128 ReadVector128(ulong address)
  151. {
  152. return GetMemoryManager().ReadTracked<V128>(address);
  153. }
  154. #endregion
  155. #region "Write"
  156. public static void WriteByte(ulong address, byte value)
  157. {
  158. GetMemoryManager().Write(address, value);
  159. }
  160. public static void WriteUInt16(ulong address, ushort value)
  161. {
  162. GetMemoryManager().Write(address, value);
  163. }
  164. public static void WriteUInt32(ulong address, uint value)
  165. {
  166. GetMemoryManager().Write(address, value);
  167. }
  168. public static void WriteUInt64(ulong address, ulong value)
  169. {
  170. GetMemoryManager().Write(address, value);
  171. }
  172. public static void WriteVector128(ulong address, V128 value)
  173. {
  174. GetMemoryManager().Write(address, value);
  175. }
  176. #endregion
  177. public static void EnqueueForRejit(ulong address)
  178. {
  179. Context.Translator.EnqueueForRejit(address, GetContext().ExecutionMode);
  180. }
  181. public static void SignalMemoryTracking(ulong address, ulong size, bool write)
  182. {
  183. GetMemoryManager().SignalMemoryTracking(address, size, write);
  184. }
  185. public static void ThrowInvalidMemoryAccess(ulong address)
  186. {
  187. throw new InvalidAccessException(address);
  188. }
  189. public static ulong GetFunctionAddress(ulong address)
  190. {
  191. TranslatedFunction function = Context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  192. return (ulong)function.FuncPtr.ToInt64();
  193. }
  194. public static void InvalidateCacheLine(ulong address)
  195. {
  196. Context.Translator.InvalidateJitCacheRegion(address, InstEmit.DczSizeInBytes);
  197. }
  198. public static bool CheckSynchronization()
  199. {
  200. Statistics.PauseTimer();
  201. ExecutionContext context = GetContext();
  202. context.CheckInterrupt();
  203. Statistics.ResumeTimer();
  204. return context.Running;
  205. }
  206. public static ExecutionContext GetContext()
  207. {
  208. return Context.Context;
  209. }
  210. public static IMemoryManager GetMemoryManager()
  211. {
  212. return Context.Memory;
  213. }
  214. }
  215. }