InstEmitMemoryHelper.cs 22 KB

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