NativeInterface.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. uint result = (uint)(context.Fpsr & FPSR.A32Mask) | (uint)(context.Fpcr & FPCR.A32Mask);
  71. result |= context.GetFPstateFlag(FPState.NFlag) ? (1u << 31) : 0;
  72. result |= context.GetFPstateFlag(FPState.ZFlag) ? (1u << 30) : 0;
  73. result |= context.GetFPstateFlag(FPState.CFlag) ? (1u << 29) : 0;
  74. result |= context.GetFPstateFlag(FPState.VFlag) ? (1u << 28) : 0;
  75. return result;
  76. }
  77. public static ulong GetTpidrEl0()
  78. {
  79. return (ulong)GetContext().TpidrEl0;
  80. }
  81. public static uint GetTpidrEl032()
  82. {
  83. return (uint)GetContext().TpidrEl0;
  84. }
  85. public static ulong GetTpidr()
  86. {
  87. return (ulong)GetContext().Tpidr;
  88. }
  89. public static uint GetTpidr32()
  90. {
  91. return (uint)GetContext().Tpidr;
  92. }
  93. public static ulong GetCntfrqEl0()
  94. {
  95. return GetContext().CntfrqEl0;
  96. }
  97. public static ulong GetCntpctEl0()
  98. {
  99. return GetContext().CntpctEl0;
  100. }
  101. public static ulong GetCntvctEl0()
  102. {
  103. return GetContext().CntvctEl0;
  104. }
  105. public static void SetFpcr(ulong value)
  106. {
  107. GetContext().Fpcr = (FPCR)value;
  108. }
  109. public static void SetFpsr(ulong value)
  110. {
  111. GetContext().Fpsr = (FPSR)value;
  112. }
  113. public static void SetFpscr(uint value)
  114. {
  115. var context = GetContext();
  116. context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0);
  117. context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0);
  118. context.SetFPstateFlag(FPState.CFlag, (value & (1u << 29)) != 0);
  119. context.SetFPstateFlag(FPState.VFlag, (value & (1u << 28)) != 0);
  120. context.Fpsr = FPSR.A32Mask & (FPSR)value;
  121. context.Fpcr = FPCR.A32Mask & (FPCR)value;
  122. }
  123. public static void SetTpidrEl0(ulong value)
  124. {
  125. GetContext().TpidrEl0 = (long)value;
  126. }
  127. public static void SetTpidrEl032(uint value)
  128. {
  129. GetContext().TpidrEl0 = (long)value;
  130. }
  131. #endregion
  132. #region "Read"
  133. public static byte ReadByte(ulong address)
  134. {
  135. return GetMemoryManager().Read<byte>(address);
  136. }
  137. public static ushort ReadUInt16(ulong address)
  138. {
  139. return GetMemoryManager().Read<ushort>(address);
  140. }
  141. public static uint ReadUInt32(ulong address)
  142. {
  143. return GetMemoryManager().Read<uint>(address);
  144. }
  145. public static ulong ReadUInt64(ulong address)
  146. {
  147. return GetMemoryManager().Read<ulong>(address);
  148. }
  149. public static V128 ReadVector128(ulong address)
  150. {
  151. return GetMemoryManager().Read<V128>(address);
  152. }
  153. #endregion
  154. #region "Write"
  155. public static void WriteByte(ulong address, byte value)
  156. {
  157. GetMemoryManager().Write(address, value);
  158. }
  159. public static void WriteUInt16(ulong address, ushort value)
  160. {
  161. GetMemoryManager().Write(address, value);
  162. }
  163. public static void WriteUInt32(ulong address, uint value)
  164. {
  165. GetMemoryManager().Write(address, value);
  166. }
  167. public static void WriteUInt64(ulong address, ulong value)
  168. {
  169. GetMemoryManager().Write(address, value);
  170. }
  171. public static void WriteVector128(ulong address, V128 value)
  172. {
  173. GetMemoryManager().Write(address, value);
  174. }
  175. #endregion
  176. public static void MarkRegionAsModified(ulong address, ulong size)
  177. {
  178. GetMemoryManager().MarkRegionAsModified(address, size);
  179. }
  180. public static void ThrowInvalidMemoryAccess(ulong address)
  181. {
  182. throw new InvalidAccessException(address);
  183. }
  184. public static ulong GetFunctionAddress(ulong address)
  185. {
  186. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  187. return (ulong)function.FuncPtr.ToInt64();
  188. }
  189. public static ulong GetIndirectFunctionAddress(ulong address, ulong entryAddress)
  190. {
  191. TranslatedFunction function = _context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  192. ulong ptr = (ulong)function.FuncPtr.ToInt64();
  193. if (function.HighCq)
  194. {
  195. // Rewrite the host function address in the table to point to the highCq function.
  196. Marshal.WriteInt64((IntPtr)entryAddress, 8, (long)ptr);
  197. }
  198. return ptr;
  199. }
  200. public static bool CheckSynchronization()
  201. {
  202. Statistics.PauseTimer();
  203. var context = GetContext();
  204. context.CheckInterrupt();
  205. Statistics.ResumeTimer();
  206. return context.Running;
  207. }
  208. public static ExecutionContext GetContext()
  209. {
  210. return _context.Context;
  211. }
  212. public static IMemoryManager GetMemoryManager()
  213. {
  214. return _context.Memory;
  215. }
  216. }
  217. }