InstEmitMemoryEx.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using ChocolArm64.Translation;
  5. using System;
  6. using System.Reflection.Emit;
  7. using System.Threading;
  8. using static ChocolArm64.Instructions.InstEmitMemoryHelper;
  9. namespace ChocolArm64.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. [Flags]
  14. private enum AccessType
  15. {
  16. None = 0,
  17. Ordered = 1,
  18. Exclusive = 2,
  19. OrderedEx = Ordered | Exclusive
  20. }
  21. public static void Clrex(ILEmitterCtx context)
  22. {
  23. context.EmitLdarg(TranslatedSub.StateArgIdx);
  24. context.EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.ClearExclusiveAddress));
  25. }
  26. public static void Dmb(ILEmitterCtx context) => EmitBarrier(context);
  27. public static void Dsb(ILEmitterCtx context) => EmitBarrier(context);
  28. public static void Ldar(ILEmitterCtx context) => EmitLdr(context, AccessType.Ordered);
  29. public static void Ldaxr(ILEmitterCtx context) => EmitLdr(context, AccessType.OrderedEx);
  30. public static void Ldxr(ILEmitterCtx context) => EmitLdr(context, AccessType.Exclusive);
  31. public static void Ldxp(ILEmitterCtx context) => EmitLdp(context, AccessType.Exclusive);
  32. public static void Ldaxp(ILEmitterCtx context) => EmitLdp(context, AccessType.OrderedEx);
  33. private static void EmitLdr(ILEmitterCtx context, AccessType accType)
  34. {
  35. EmitLoad(context, accType, pair: false);
  36. }
  37. private static void EmitLdp(ILEmitterCtx context, AccessType accType)
  38. {
  39. EmitLoad(context, accType, pair: true);
  40. }
  41. private static void EmitLoad(ILEmitterCtx context, AccessType accType, bool pair)
  42. {
  43. OpCodeMemEx64 op = (OpCodeMemEx64)context.CurrOp;
  44. bool ordered = (accType & AccessType.Ordered) != 0;
  45. bool exclusive = (accType & AccessType.Exclusive) != 0;
  46. if (ordered)
  47. {
  48. EmitBarrier(context);
  49. }
  50. context.EmitLdint(op.Rn);
  51. context.EmitSttmp();
  52. if (exclusive)
  53. {
  54. context.EmitLdarg(TranslatedSub.StateArgIdx);
  55. context.EmitLdtmp();
  56. context.EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.SetExclusiveAddress));
  57. }
  58. void WriteExclusiveValue(string propName)
  59. {
  60. context.Emit(OpCodes.Dup);
  61. if (op.Size < 3)
  62. {
  63. context.Emit(OpCodes.Conv_U8);
  64. }
  65. context.EmitSttmp2();
  66. context.EmitLdarg(TranslatedSub.StateArgIdx);
  67. context.EmitLdtmp2();
  68. context.EmitCallPrivatePropSet(typeof(CpuThreadState), propName);
  69. }
  70. if (pair)
  71. {
  72. //Exclusive loads should be atomic. For pairwise loads, we need to
  73. //read all the data at once. For a 32-bits pairwise load, we do a
  74. //simple 64-bits load, for a 128-bits load, we need to call a special
  75. //method to read 128-bits atomically.
  76. if (op.Size == 2)
  77. {
  78. context.EmitLdtmp();
  79. EmitReadZxCall(context, 3);
  80. context.Emit(OpCodes.Dup);
  81. //Mask low half.
  82. context.Emit(OpCodes.Conv_U4);
  83. if (exclusive)
  84. {
  85. WriteExclusiveValue(nameof(CpuThreadState.ExclusiveValueLow));
  86. }
  87. context.EmitStintzr(op.Rt);
  88. //Shift high half.
  89. context.EmitLsr(32);
  90. context.Emit(OpCodes.Conv_U4);
  91. if (exclusive)
  92. {
  93. WriteExclusiveValue(nameof(CpuThreadState.ExclusiveValueHigh));
  94. }
  95. context.EmitStintzr(op.Rt2);
  96. }
  97. else if (op.Size == 3)
  98. {
  99. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  100. context.EmitLdtmp();
  101. context.EmitPrivateCall(typeof(MemoryManager), nameof(MemoryManager.AtomicReadInt128));
  102. context.Emit(OpCodes.Dup);
  103. //Load low part of the vector.
  104. context.EmitLdc_I4(0);
  105. context.EmitLdc_I4(3);
  106. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractIntZx));
  107. if (exclusive)
  108. {
  109. WriteExclusiveValue(nameof(CpuThreadState.ExclusiveValueLow));
  110. }
  111. context.EmitStintzr(op.Rt);
  112. //Load high part of the vector.
  113. context.EmitLdc_I4(1);
  114. context.EmitLdc_I4(3);
  115. VectorHelper.EmitCall(context, nameof(VectorHelper.VectorExtractIntZx));
  116. if (exclusive)
  117. {
  118. WriteExclusiveValue(nameof(CpuThreadState.ExclusiveValueHigh));
  119. }
  120. context.EmitStintzr(op.Rt2);
  121. }
  122. else
  123. {
  124. throw new InvalidOperationException($"Invalid load size of {1 << op.Size} bytes.");
  125. }
  126. }
  127. else
  128. {
  129. //8, 16, 32 or 64-bits (non-pairwise) load.
  130. context.EmitLdtmp();
  131. EmitReadZxCall(context, op.Size);
  132. if (exclusive)
  133. {
  134. WriteExclusiveValue(nameof(CpuThreadState.ExclusiveValueLow));
  135. }
  136. context.EmitStintzr(op.Rt);
  137. }
  138. }
  139. public static void Pfrm(ILEmitterCtx context)
  140. {
  141. //Memory Prefetch, execute as no-op.
  142. }
  143. public static void Stlr(ILEmitterCtx context) => EmitStr(context, AccessType.Ordered);
  144. public static void Stlxr(ILEmitterCtx context) => EmitStr(context, AccessType.OrderedEx);
  145. public static void Stxr(ILEmitterCtx context) => EmitStr(context, AccessType.Exclusive);
  146. public static void Stxp(ILEmitterCtx context) => EmitStp(context, AccessType.Exclusive);
  147. public static void Stlxp(ILEmitterCtx context) => EmitStp(context, AccessType.OrderedEx);
  148. private static void EmitStr(ILEmitterCtx context, AccessType accType)
  149. {
  150. EmitStore(context, accType, pair: false);
  151. }
  152. private static void EmitStp(ILEmitterCtx context, AccessType accType)
  153. {
  154. EmitStore(context, accType, pair: true);
  155. }
  156. private static void EmitStore(ILEmitterCtx context, AccessType accType, bool pair)
  157. {
  158. OpCodeMemEx64 op = (OpCodeMemEx64)context.CurrOp;
  159. bool ordered = (accType & AccessType.Ordered) != 0;
  160. bool exclusive = (accType & AccessType.Exclusive) != 0;
  161. if (ordered)
  162. {
  163. EmitBarrier(context);
  164. }
  165. if (exclusive)
  166. {
  167. ILLabel lblEx = new ILLabel();
  168. ILLabel lblEnd = new ILLabel();
  169. context.EmitLdarg(TranslatedSub.StateArgIdx);
  170. context.EmitLdint(op.Rn);
  171. context.EmitPrivateCall(typeof(CpuThreadState), nameof(CpuThreadState.CheckExclusiveAddress));
  172. context.Emit(OpCodes.Brtrue_S, lblEx);
  173. //Address check failed, set error right away and do not store anything.
  174. context.EmitLdc_I4(1);
  175. context.EmitStintzr(op.Rs);
  176. context.Emit(OpCodes.Br, lblEnd);
  177. //Address check passsed.
  178. context.MarkLabel(lblEx);
  179. context.EmitLdarg(TranslatedSub.MemoryArgIdx);
  180. context.EmitLdint(op.Rn);
  181. context.EmitLdarg(TranslatedSub.StateArgIdx);
  182. context.EmitCallPrivatePropGet(typeof(CpuThreadState), nameof(CpuThreadState.ExclusiveValueLow));
  183. void EmitCast()
  184. {
  185. //The input should be always int64.
  186. switch (op.Size)
  187. {
  188. case 0: context.Emit(OpCodes.Conv_U1); break;
  189. case 1: context.Emit(OpCodes.Conv_U2); break;
  190. case 2: context.Emit(OpCodes.Conv_U4); break;
  191. }
  192. }
  193. EmitCast();
  194. if (pair)
  195. {
  196. context.EmitLdarg(TranslatedSub.StateArgIdx);
  197. context.EmitCallPrivatePropGet(typeof(CpuThreadState), nameof(CpuThreadState.ExclusiveValueHigh));
  198. EmitCast();
  199. context.EmitLdintzr(op.Rt);
  200. EmitCast();
  201. context.EmitLdintzr(op.Rt2);
  202. EmitCast();
  203. switch (op.Size)
  204. {
  205. case 2: context.EmitPrivateCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchange2xInt32)); break;
  206. case 3: context.EmitPrivateCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt128)); break;
  207. default: throw new InvalidOperationException($"Invalid store size of {1 << op.Size} bytes.");
  208. }
  209. }
  210. else
  211. {
  212. context.EmitLdintzr(op.Rt);
  213. EmitCast();
  214. switch (op.Size)
  215. {
  216. case 0: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeByte)); break;
  217. case 1: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt16)); break;
  218. case 2: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt32)); break;
  219. case 3: context.EmitCall(typeof(MemoryManager), nameof(MemoryManager.AtomicCompareExchangeInt64)); break;
  220. default: throw new InvalidOperationException($"Invalid store size of {1 << op.Size} bytes.");
  221. }
  222. }
  223. //The value returned is a bool, true if the values compared
  224. //were equal and the new value was written, false otherwise.
  225. //We need to invert this result, as on ARM 1 indicates failure,
  226. //and 0 success on those instructions.
  227. context.EmitLdc_I4(1);
  228. context.Emit(OpCodes.Xor);
  229. context.Emit(OpCodes.Dup);
  230. context.Emit(OpCodes.Conv_U8);
  231. context.EmitStintzr(op.Rs);
  232. //Only clear the exclusive monitor if the store was successful (Rs = false).
  233. context.Emit(OpCodes.Brtrue_S, lblEnd);
  234. Clrex(context);
  235. context.MarkLabel(lblEnd);
  236. }
  237. else
  238. {
  239. void EmitWriteCall(int rt, long offset)
  240. {
  241. context.EmitLdint(op.Rn);
  242. if (offset != 0)
  243. {
  244. context.EmitLdc_I8(offset);
  245. context.Emit(OpCodes.Add);
  246. }
  247. context.EmitLdintzr(rt);
  248. InstEmitMemoryHelper.EmitWriteCall(context, op.Size);
  249. }
  250. EmitWriteCall(op.Rt, 0);
  251. if (pair)
  252. {
  253. EmitWriteCall(op.Rt2, 1 << op.Size);
  254. }
  255. }
  256. }
  257. private static void EmitBarrier(ILEmitterCtx context)
  258. {
  259. //Note: This barrier is most likely not necessary, and probably
  260. //doesn't make any difference since we need to do a ton of stuff
  261. //(software MMU emulation) to read or write anything anyway.
  262. context.EmitCall(typeof(Thread), nameof(Thread.MemoryBarrier));
  263. }
  264. }
  265. }