InstEmitMemoryHelper.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Memory;
  4. using ARMeilleure.Translation;
  5. using ARMeilleure.Translation.PTC;
  6. using System;
  7. using System.Reflection;
  8. using static ARMeilleure.Instructions.InstEmitHelper;
  9. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  10. namespace ARMeilleure.Instructions
  11. {
  12. static class InstEmitMemoryHelper
  13. {
  14. private const int PageBits = 12;
  15. private const int PageMask = (1 << PageBits) - 1;
  16. private enum Extension
  17. {
  18. Zx,
  19. Sx32,
  20. Sx64
  21. }
  22. public static void EmitLoadZx(ArmEmitterContext context, Operand address, int rt, int size)
  23. {
  24. EmitLoad(context, address, Extension.Zx, rt, size);
  25. }
  26. public static void EmitLoadSx32(ArmEmitterContext context, Operand address, int rt, int size)
  27. {
  28. EmitLoad(context, address, Extension.Sx32, rt, size);
  29. }
  30. public static void EmitLoadSx64(ArmEmitterContext context, Operand address, int rt, int size)
  31. {
  32. EmitLoad(context, address, Extension.Sx64, rt, size);
  33. }
  34. private static void EmitLoad(ArmEmitterContext context, Operand address, Extension ext, int rt, int size)
  35. {
  36. bool isSimd = IsSimd(context);
  37. if ((uint)size > (isSimd ? 4 : 3))
  38. {
  39. throw new ArgumentOutOfRangeException(nameof(size));
  40. }
  41. if (isSimd)
  42. {
  43. EmitReadVector(context, address, context.VectorZero(), rt, 0, size);
  44. }
  45. else
  46. {
  47. EmitReadInt(context, address, rt, size);
  48. }
  49. if (!isSimd && !(context.CurrOp is OpCode32 && rt == State.RegisterAlias.Aarch32Pc))
  50. {
  51. Operand value = GetInt(context, rt);
  52. if (ext == Extension.Sx32 || ext == Extension.Sx64)
  53. {
  54. OperandType destType = ext == Extension.Sx64 ? OperandType.I64 : OperandType.I32;
  55. switch (size)
  56. {
  57. case 0: value = context.SignExtend8 (destType, value); break;
  58. case 1: value = context.SignExtend16(destType, value); break;
  59. case 2: value = context.SignExtend32(destType, value); break;
  60. }
  61. }
  62. SetInt(context, rt, value);
  63. }
  64. }
  65. public static void EmitLoadSimd(
  66. ArmEmitterContext context,
  67. Operand address,
  68. Operand vector,
  69. int rt,
  70. int elem,
  71. int size)
  72. {
  73. EmitReadVector(context, address, vector, rt, elem, size);
  74. }
  75. public static void EmitStore(ArmEmitterContext context, Operand address, int rt, int size)
  76. {
  77. bool isSimd = IsSimd(context);
  78. if ((uint)size > (isSimd ? 4 : 3))
  79. {
  80. throw new ArgumentOutOfRangeException(nameof(size));
  81. }
  82. if (isSimd)
  83. {
  84. EmitWriteVector(context, address, rt, 0, size);
  85. }
  86. else
  87. {
  88. EmitWriteInt(context, address, rt, size);
  89. }
  90. }
  91. public static void EmitStoreSimd(
  92. ArmEmitterContext context,
  93. Operand address,
  94. int rt,
  95. int elem,
  96. int size)
  97. {
  98. EmitWriteVector(context, address, rt, elem, size);
  99. }
  100. private static bool IsSimd(ArmEmitterContext context)
  101. {
  102. return context.CurrOp is IOpCodeSimd &&
  103. !(context.CurrOp is OpCodeSimdMemMs ||
  104. context.CurrOp is OpCodeSimdMemSs);
  105. }
  106. private static void EmitReadInt(ArmEmitterContext context, Operand address, int rt, int size)
  107. {
  108. Operand lblSlowPath = Label();
  109. Operand lblEnd = Label();
  110. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath, write: false, size);
  111. Operand value = default;
  112. switch (size)
  113. {
  114. case 0: value = context.Load8 (physAddr); break;
  115. case 1: value = context.Load16(physAddr); break;
  116. case 2: value = context.Load (OperandType.I32, physAddr); break;
  117. case 3: value = context.Load (OperandType.I64, physAddr); break;
  118. }
  119. SetInt(context, rt, value);
  120. if (!context.Memory.Type.IsHostMapped())
  121. {
  122. context.Branch(lblEnd);
  123. context.MarkLabel(lblSlowPath, BasicBlockFrequency.Cold);
  124. EmitReadIntFallback(context, address, rt, size);
  125. context.MarkLabel(lblEnd);
  126. }
  127. }
  128. public static Operand EmitReadIntAligned(ArmEmitterContext context, Operand address, int size)
  129. {
  130. if ((uint)size > 4)
  131. {
  132. throw new ArgumentOutOfRangeException(nameof(size));
  133. }
  134. Operand physAddr = EmitPtPointerLoad(context, address, default, write: false, size);
  135. return size switch
  136. {
  137. 0 => context.Load8(physAddr),
  138. 1 => context.Load16(physAddr),
  139. 2 => context.Load(OperandType.I32, physAddr),
  140. 3 => context.Load(OperandType.I64, physAddr),
  141. _ => context.Load(OperandType.V128, physAddr)
  142. };
  143. }
  144. private static void EmitReadVector(
  145. ArmEmitterContext context,
  146. Operand address,
  147. Operand vector,
  148. int rt,
  149. int elem,
  150. int size)
  151. {
  152. Operand lblSlowPath = Label();
  153. Operand lblEnd = Label();
  154. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath, write: false, size);
  155. Operand value = default;
  156. switch (size)
  157. {
  158. case 0: value = context.VectorInsert8 (vector, context.Load8(physAddr), elem); break;
  159. case 1: value = context.VectorInsert16(vector, context.Load16(physAddr), elem); break;
  160. case 2: value = context.VectorInsert (vector, context.Load(OperandType.I32, physAddr), elem); break;
  161. case 3: value = context.VectorInsert (vector, context.Load(OperandType.I64, physAddr), elem); break;
  162. case 4: value = context.Load (OperandType.V128, physAddr); break;
  163. }
  164. context.Copy(GetVec(rt), value);
  165. if (!context.Memory.Type.IsHostMapped())
  166. {
  167. context.Branch(lblEnd);
  168. context.MarkLabel(lblSlowPath, BasicBlockFrequency.Cold);
  169. EmitReadVectorFallback(context, address, vector, rt, elem, size);
  170. context.MarkLabel(lblEnd);
  171. }
  172. }
  173. private static Operand VectorCreate(ArmEmitterContext context, Operand value)
  174. {
  175. return context.VectorInsert(context.VectorZero(), value, 0);
  176. }
  177. private static void EmitWriteInt(ArmEmitterContext context, Operand address, int rt, int size)
  178. {
  179. Operand lblSlowPath = Label();
  180. Operand lblEnd = Label();
  181. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath, write: true, size);
  182. Operand value = GetInt(context, rt);
  183. if (size < 3 && value.Type == OperandType.I64)
  184. {
  185. value = context.ConvertI64ToI32(value);
  186. }
  187. switch (size)
  188. {
  189. case 0: context.Store8 (physAddr, value); break;
  190. case 1: context.Store16(physAddr, value); break;
  191. case 2: context.Store (physAddr, value); break;
  192. case 3: context.Store (physAddr, value); break;
  193. }
  194. if (!context.Memory.Type.IsHostMapped())
  195. {
  196. context.Branch(lblEnd);
  197. context.MarkLabel(lblSlowPath, BasicBlockFrequency.Cold);
  198. EmitWriteIntFallback(context, address, rt, size);
  199. context.MarkLabel(lblEnd);
  200. }
  201. }
  202. public static void EmitWriteIntAligned(ArmEmitterContext context, Operand address, Operand value, int size)
  203. {
  204. if ((uint)size > 4)
  205. {
  206. throw new ArgumentOutOfRangeException(nameof(size));
  207. }
  208. Operand physAddr = EmitPtPointerLoad(context, address, default, write: true, size);
  209. if (size < 3 && value.Type == OperandType.I64)
  210. {
  211. value = context.ConvertI64ToI32(value);
  212. }
  213. if (size == 0)
  214. {
  215. context.Store8(physAddr, value);
  216. }
  217. else if (size == 1)
  218. {
  219. context.Store16(physAddr, value);
  220. }
  221. else
  222. {
  223. context.Store(physAddr, value);
  224. }
  225. }
  226. private static void EmitWriteVector(
  227. ArmEmitterContext context,
  228. Operand address,
  229. int rt,
  230. int elem,
  231. int size)
  232. {
  233. Operand lblSlowPath = Label();
  234. Operand lblEnd = Label();
  235. Operand physAddr = EmitPtPointerLoad(context, address, lblSlowPath, write: true, size);
  236. Operand value = GetVec(rt);
  237. switch (size)
  238. {
  239. case 0: context.Store8 (physAddr, context.VectorExtract8(value, elem)); break;
  240. case 1: context.Store16(physAddr, context.VectorExtract16(value, elem)); break;
  241. case 2: context.Store (physAddr, context.VectorExtract(OperandType.I32, value, elem)); break;
  242. case 3: context.Store (physAddr, context.VectorExtract(OperandType.I64, value, elem)); break;
  243. case 4: context.Store (physAddr, value); break;
  244. }
  245. if (!context.Memory.Type.IsHostMapped())
  246. {
  247. context.Branch(lblEnd);
  248. context.MarkLabel(lblSlowPath, BasicBlockFrequency.Cold);
  249. EmitWriteVectorFallback(context, address, rt, elem, size);
  250. context.MarkLabel(lblEnd);
  251. }
  252. }
  253. public static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblSlowPath, bool write, int size)
  254. {
  255. if (context.Memory.Type.IsHostMapped())
  256. {
  257. return EmitHostMappedPointer(context, address);
  258. }
  259. int ptLevelBits = context.Memory.AddressSpaceBits - PageBits;
  260. int ptLevelSize = 1 << ptLevelBits;
  261. int ptLevelMask = ptLevelSize - 1;
  262. Operand addrRotated = size != 0 ? context.RotateRight(address, Const(size)) : address;
  263. Operand addrShifted = context.ShiftRightUI(addrRotated, Const(PageBits - size));
  264. Operand pte = !context.HasPtc
  265. ? Const(context.Memory.PageTablePointer.ToInt64())
  266. : Const(context.Memory.PageTablePointer.ToInt64(), Ptc.PageTableSymbol);
  267. Operand pteOffset = context.BitwiseAnd(addrShifted, Const(addrShifted.Type, ptLevelMask));
  268. if (pteOffset.Type == OperandType.I32)
  269. {
  270. pteOffset = context.ZeroExtend32(OperandType.I64, pteOffset);
  271. }
  272. pte = context.Load(OperandType.I64, context.Add(pte, context.ShiftLeft(pteOffset, Const(3))));
  273. if (addrShifted.Type == OperandType.I32)
  274. {
  275. addrShifted = context.ZeroExtend32(OperandType.I64, addrShifted);
  276. }
  277. // If the VA is out of range, or not aligned to the access size, force PTE to 0 by masking it.
  278. pte = context.BitwiseAnd(pte, context.ShiftRightSI(context.Add(addrShifted, Const(-(long)ptLevelSize)), Const(63)));
  279. if (lblSlowPath != default)
  280. {
  281. if (write)
  282. {
  283. context.BranchIf(lblSlowPath, pte, Const(0L), Comparison.LessOrEqual);
  284. pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by C# memory access)
  285. }
  286. else
  287. {
  288. pte = context.ShiftLeft(pte, Const(1));
  289. context.BranchIf(lblSlowPath, pte, Const(0L), Comparison.LessOrEqual);
  290. pte = context.ShiftRightUI(pte, Const(1));
  291. }
  292. }
  293. else
  294. {
  295. // When no label is provided to jump to a slow path if the address is invalid,
  296. // we do the validation ourselves, and throw if needed.
  297. Operand lblNotWatched = Label();
  298. // Is the page currently being tracked for read/write? If so we need to call SignalMemoryTracking.
  299. context.BranchIf(lblNotWatched, pte, Const(0L), Comparison.GreaterOrEqual, BasicBlockFrequency.Cold);
  300. // Signal memory tracking. Size here doesn't matter as address is assumed to be size aligned here.
  301. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SignalMemoryTracking)), address, Const(1UL), Const(write ? 1 : 0));
  302. context.MarkLabel(lblNotWatched);
  303. pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by C# memory access)
  304. Operand lblNonNull = Label();
  305. // Skip exception if the PTE address is non-null (not zero).
  306. context.BranchIfTrue(lblNonNull, pte, BasicBlockFrequency.Cold);
  307. // The call is not expected to return (it should throw).
  308. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ThrowInvalidMemoryAccess)), address);
  309. context.MarkLabel(lblNonNull);
  310. }
  311. Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, PageMask));
  312. if (pageOffset.Type == OperandType.I32)
  313. {
  314. pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset);
  315. }
  316. return context.Add(pte, pageOffset);
  317. }
  318. public static Operand EmitHostMappedPointer(ArmEmitterContext context, Operand address)
  319. {
  320. if (address.Type == OperandType.I32)
  321. {
  322. address = context.ZeroExtend32(OperandType.I64, address);
  323. }
  324. if (context.Memory.Type == MemoryManagerType.HostMapped)
  325. {
  326. Operand mask = Const(ulong.MaxValue >> (64 - context.Memory.AddressSpaceBits));
  327. address = context.BitwiseAnd(address, mask);
  328. }
  329. Operand baseAddr = !context.HasPtc
  330. ? Const(context.Memory.PageTablePointer.ToInt64())
  331. : Const(context.Memory.PageTablePointer.ToInt64(), Ptc.PageTableSymbol);
  332. return context.Add(baseAddr, address);
  333. }
  334. private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  335. {
  336. MethodInfo info = null;
  337. switch (size)
  338. {
  339. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadByte)); break;
  340. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt16)); break;
  341. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt32)); break;
  342. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt64)); break;
  343. }
  344. SetInt(context, rt, context.Call(info, address));
  345. }
  346. private static void EmitReadVectorFallback(
  347. ArmEmitterContext context,
  348. Operand address,
  349. Operand vector,
  350. int rt,
  351. int elem,
  352. int size)
  353. {
  354. MethodInfo info = null;
  355. switch (size)
  356. {
  357. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadByte)); break;
  358. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt16)); break;
  359. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt32)); break;
  360. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt64)); break;
  361. case 4: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadVector128)); break;
  362. }
  363. Operand value = context.Call(info, address);
  364. switch (size)
  365. {
  366. case 0: value = context.VectorInsert8 (vector, value, elem); break;
  367. case 1: value = context.VectorInsert16(vector, value, elem); break;
  368. case 2: value = context.VectorInsert (vector, value, elem); break;
  369. case 3: value = context.VectorInsert (vector, value, elem); break;
  370. }
  371. context.Copy(GetVec(rt), value);
  372. }
  373. private static void EmitWriteIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  374. {
  375. MethodInfo info = null;
  376. switch (size)
  377. {
  378. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteByte)); break;
  379. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt16)); break;
  380. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt32)); break;
  381. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt64)); break;
  382. }
  383. Operand value = GetInt(context, rt);
  384. if (size < 3 && value.Type == OperandType.I64)
  385. {
  386. value = context.ConvertI64ToI32(value);
  387. }
  388. context.Call(info, address, value);
  389. }
  390. private static void EmitWriteVectorFallback(
  391. ArmEmitterContext context,
  392. Operand address,
  393. int rt,
  394. int elem,
  395. int size)
  396. {
  397. MethodInfo info = null;
  398. switch (size)
  399. {
  400. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteByte)); break;
  401. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt16)); break;
  402. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt32)); break;
  403. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt64)); break;
  404. case 4: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteVector128)); break;
  405. }
  406. Operand value = default;
  407. if (size < 4)
  408. {
  409. switch (size)
  410. {
  411. case 0: value = context.VectorExtract8 (GetVec(rt), elem); break;
  412. case 1: value = context.VectorExtract16(GetVec(rt), elem); break;
  413. case 2: value = context.VectorExtract (OperandType.I32, GetVec(rt), elem); break;
  414. case 3: value = context.VectorExtract (OperandType.I64, GetVec(rt), elem); break;
  415. }
  416. }
  417. else
  418. {
  419. value = GetVec(rt);
  420. }
  421. context.Call(info, address, value);
  422. }
  423. private static Operand GetInt(ArmEmitterContext context, int rt)
  424. {
  425. return context.CurrOp is OpCode32 ? GetIntA32(context, rt) : GetIntOrZR(context, rt);
  426. }
  427. private static void SetInt(ArmEmitterContext context, int rt, Operand value)
  428. {
  429. if (context.CurrOp is OpCode32)
  430. {
  431. SetIntA32(context, rt, value);
  432. }
  433. else
  434. {
  435. SetIntOrZR(context, rt, value);
  436. }
  437. }
  438. // ARM32 helpers.
  439. public static Operand GetMemM(ArmEmitterContext context, bool setCarry = true)
  440. {
  441. switch (context.CurrOp)
  442. {
  443. case OpCode32MemRsImm op: return GetMShiftedByImmediate(context, op, setCarry);
  444. case IOpCode32MemReg op: return GetIntA32(context, op.Rm);
  445. case IOpCode32Mem op: return Const(op.Immediate);
  446. case OpCode32SimdMemImm op: return Const(op.Immediate);
  447. default: throw InvalidOpCodeType(context.CurrOp);
  448. }
  449. }
  450. private static Exception InvalidOpCodeType(OpCode opCode)
  451. {
  452. return new InvalidOperationException($"Invalid OpCode type \"{opCode?.GetType().Name ?? "null"}\".");
  453. }
  454. public static Operand GetMShiftedByImmediate(ArmEmitterContext context, OpCode32MemRsImm op, bool setCarry)
  455. {
  456. Operand m = GetIntA32(context, op.Rm);
  457. int shift = op.Immediate;
  458. if (shift == 0)
  459. {
  460. switch (op.ShiftType)
  461. {
  462. case ShiftType.Lsr: shift = 32; break;
  463. case ShiftType.Asr: shift = 32; break;
  464. case ShiftType.Ror: shift = 1; break;
  465. }
  466. }
  467. if (shift != 0)
  468. {
  469. setCarry &= false;
  470. switch (op.ShiftType)
  471. {
  472. case ShiftType.Lsl: m = InstEmitAluHelper.GetLslC(context, m, setCarry, shift); break;
  473. case ShiftType.Lsr: m = InstEmitAluHelper.GetLsrC(context, m, setCarry, shift); break;
  474. case ShiftType.Asr: m = InstEmitAluHelper.GetAsrC(context, m, setCarry, shift); break;
  475. case ShiftType.Ror:
  476. if (op.Immediate != 0)
  477. {
  478. m = InstEmitAluHelper.GetRorC(context, m, setCarry, shift);
  479. }
  480. else
  481. {
  482. m = InstEmitAluHelper.GetRrxC(context, m, setCarry);
  483. }
  484. break;
  485. }
  486. }
  487. return m;
  488. }
  489. }
  490. }