NativeInterface.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using ARMeilleure.Memory;
  2. using ARMeilleure.State;
  3. using ARMeilleure.Translation;
  4. using System;
  5. namespace ARMeilleure.Instructions
  6. {
  7. static class NativeInterface
  8. {
  9. private class ThreadContext
  10. {
  11. public ExecutionContext Context { get; }
  12. public IMemoryManager Memory { get; }
  13. public Translator Translator { get; }
  14. public ThreadContext(ExecutionContext context, IMemoryManager memory, Translator translator)
  15. {
  16. Context = context;
  17. Memory = memory;
  18. Translator = translator;
  19. }
  20. }
  21. [ThreadStatic]
  22. private static ThreadContext Context;
  23. public static void RegisterThread(ExecutionContext context, IMemoryManager memory, Translator translator)
  24. {
  25. Context = new ThreadContext(context, memory, translator);
  26. }
  27. public static void UnregisterThread()
  28. {
  29. Context = null;
  30. }
  31. public static void Break(ulong address, int imm)
  32. {
  33. Statistics.PauseTimer();
  34. GetContext().OnBreak(address, imm);
  35. Statistics.ResumeTimer();
  36. }
  37. public static void SupervisorCall(ulong address, int imm)
  38. {
  39. Statistics.PauseTimer();
  40. GetContext().OnSupervisorCall(address, imm);
  41. Statistics.ResumeTimer();
  42. }
  43. public static void Undefined(ulong address, int opCode)
  44. {
  45. Statistics.PauseTimer();
  46. GetContext().OnUndefined(address, opCode);
  47. Statistics.ResumeTimer();
  48. }
  49. #region "System registers"
  50. public static ulong GetCtrEl0()
  51. {
  52. return (ulong)GetContext().CtrEl0;
  53. }
  54. public static ulong GetDczidEl0()
  55. {
  56. return (ulong)GetContext().DczidEl0;
  57. }
  58. public static ulong GetCntfrqEl0()
  59. {
  60. return GetContext().CntfrqEl0;
  61. }
  62. public static ulong GetCntpctEl0()
  63. {
  64. return GetContext().CntpctEl0;
  65. }
  66. public static ulong GetCntvctEl0()
  67. {
  68. return GetContext().CntvctEl0;
  69. }
  70. #endregion
  71. #region "Read"
  72. public static byte ReadByte(ulong address)
  73. {
  74. return GetMemoryManager().ReadTracked<byte>(address);
  75. }
  76. public static ushort ReadUInt16(ulong address)
  77. {
  78. return GetMemoryManager().ReadTracked<ushort>(address);
  79. }
  80. public static uint ReadUInt32(ulong address)
  81. {
  82. return GetMemoryManager().ReadTracked<uint>(address);
  83. }
  84. public static ulong ReadUInt64(ulong address)
  85. {
  86. return GetMemoryManager().ReadTracked<ulong>(address);
  87. }
  88. public static V128 ReadVector128(ulong address)
  89. {
  90. return GetMemoryManager().ReadTracked<V128>(address);
  91. }
  92. #endregion
  93. #region "Write"
  94. public static void WriteByte(ulong address, byte value)
  95. {
  96. GetMemoryManager().Write(address, value);
  97. }
  98. public static void WriteUInt16(ulong address, ushort value)
  99. {
  100. GetMemoryManager().Write(address, value);
  101. }
  102. public static void WriteUInt32(ulong address, uint value)
  103. {
  104. GetMemoryManager().Write(address, value);
  105. }
  106. public static void WriteUInt64(ulong address, ulong value)
  107. {
  108. GetMemoryManager().Write(address, value);
  109. }
  110. public static void WriteVector128(ulong address, V128 value)
  111. {
  112. GetMemoryManager().Write(address, value);
  113. }
  114. #endregion
  115. public static void EnqueueForRejit(ulong address)
  116. {
  117. Context.Translator.EnqueueForRejit(address, GetContext().ExecutionMode);
  118. }
  119. public static void SignalMemoryTracking(ulong address, ulong size, bool write)
  120. {
  121. GetMemoryManager().SignalMemoryTracking(address, size, write);
  122. }
  123. public static void ThrowInvalidMemoryAccess(ulong address)
  124. {
  125. throw new InvalidAccessException(address);
  126. }
  127. public static ulong GetFunctionAddress(ulong address)
  128. {
  129. TranslatedFunction function = Context.Translator.GetOrTranslate(address, GetContext().ExecutionMode);
  130. return (ulong)function.FuncPointer.ToInt64();
  131. }
  132. public static void InvalidateCacheLine(ulong address)
  133. {
  134. Context.Translator.InvalidateJitCacheRegion(address, InstEmit.DczSizeInBytes);
  135. }
  136. public static bool CheckSynchronization()
  137. {
  138. Statistics.PauseTimer();
  139. ExecutionContext context = GetContext();
  140. context.CheckInterrupt();
  141. Statistics.ResumeTimer();
  142. return context.Running;
  143. }
  144. public static ExecutionContext GetContext()
  145. {
  146. return Context.Context;
  147. }
  148. public static IMemoryManager GetMemoryManager()
  149. {
  150. return Context.Memory;
  151. }
  152. }
  153. }