InstEmitMemoryHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. ulong protection = (write ? 3UL : 1UL) << 48;
  301. context.BranchIfTrue(lblSlowPath, context.BitwiseAnd(pte, Const(protection)));
  302. }
  303. else
  304. {
  305. // When no label is provided to jump to a slow path if the address is invalid,
  306. // we do the validation ourselves, and throw if needed.
  307. Operand lblNotWatched = Label();
  308. // Is the page currently being tracked for read/write? If so we need to call MarkRegionAsModified.
  309. context.BranchIf(lblNotWatched, pte, Const(0L), Comparison.GreaterOrEqual, BasicBlockFrequency.Cold);
  310. // Mark the region as modified. Size here doesn't matter as address is assumed to be size aligned here.
  311. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SignalMemoryTracking)), address, Const(1UL), Const(write ? 1 : 0));
  312. context.MarkLabel(lblNotWatched);
  313. Operand lblNonNull = Label();
  314. // Skip exception if the PTE address is non-null (not zero).
  315. context.BranchIfTrue(lblNonNull, pte, BasicBlockFrequency.Cold);
  316. // The call is not expected to return (it should throw).
  317. context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.ThrowInvalidMemoryAccess)), address);
  318. context.MarkLabel(lblNonNull);
  319. }
  320. pte = context.BitwiseAnd(pte, Const(0xffffffffffffUL)); // Ignore any software protection bits. (they are still used by c# memory access)
  321. Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, PageMask));
  322. if (pageOffset.Type == OperandType.I32)
  323. {
  324. pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset);
  325. }
  326. return context.Add(pte, pageOffset);
  327. }
  328. private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  329. {
  330. MethodInfo info = null;
  331. switch (size)
  332. {
  333. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadByte)); break;
  334. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt16)); break;
  335. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt32)); break;
  336. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt64)); break;
  337. }
  338. SetInt(context, rt, context.Call(info, address));
  339. }
  340. private static void EmitReadVectorFallback(
  341. ArmEmitterContext context,
  342. Operand address,
  343. Operand vector,
  344. int rt,
  345. int elem,
  346. int size)
  347. {
  348. MethodInfo info = null;
  349. switch (size)
  350. {
  351. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadByte)); break;
  352. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt16)); break;
  353. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt32)); break;
  354. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadUInt64)); break;
  355. case 4: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.ReadVector128)); break;
  356. }
  357. Operand value = context.Call(info, address);
  358. switch (size)
  359. {
  360. case 0: value = context.VectorInsert8 (vector, value, elem); break;
  361. case 1: value = context.VectorInsert16(vector, value, elem); break;
  362. case 2: value = context.VectorInsert (vector, value, elem); break;
  363. case 3: value = context.VectorInsert (vector, value, elem); break;
  364. }
  365. context.Copy(GetVec(rt), value);
  366. }
  367. private static void EmitWriteIntFallback(ArmEmitterContext context, Operand address, int rt, int size)
  368. {
  369. MethodInfo info = null;
  370. switch (size)
  371. {
  372. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteByte)); break;
  373. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt16)); break;
  374. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt32)); break;
  375. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt64)); break;
  376. }
  377. Operand value = GetInt(context, rt);
  378. if (size < 3 && value.Type == OperandType.I64)
  379. {
  380. value = context.ConvertI64ToI32(value);
  381. }
  382. context.Call(info, address, value);
  383. }
  384. private static void EmitWriteVectorFallback(
  385. ArmEmitterContext context,
  386. Operand address,
  387. int rt,
  388. int elem,
  389. int size)
  390. {
  391. MethodInfo info = null;
  392. switch (size)
  393. {
  394. case 0: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteByte)); break;
  395. case 1: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt16)); break;
  396. case 2: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt32)); break;
  397. case 3: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteUInt64)); break;
  398. case 4: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.WriteVector128)); break;
  399. }
  400. Operand value = null;
  401. if (size < 4)
  402. {
  403. switch (size)
  404. {
  405. case 0: value = context.VectorExtract8 (GetVec(rt), elem); break;
  406. case 1: value = context.VectorExtract16(GetVec(rt), elem); break;
  407. case 2: value = context.VectorExtract (OperandType.I32, GetVec(rt), elem); break;
  408. case 3: value = context.VectorExtract (OperandType.I64, GetVec(rt), elem); break;
  409. }
  410. }
  411. else
  412. {
  413. value = GetVec(rt);
  414. }
  415. context.Call(info, address, value);
  416. }
  417. private static Operand GetInt(ArmEmitterContext context, int rt)
  418. {
  419. return context.CurrOp is OpCode32 ? GetIntA32(context, rt) : GetIntOrZR(context, rt);
  420. }
  421. private static void SetInt(ArmEmitterContext context, int rt, Operand value)
  422. {
  423. if (context.CurrOp is OpCode32)
  424. {
  425. SetIntA32(context, rt, value);
  426. }
  427. else
  428. {
  429. SetIntOrZR(context, rt, value);
  430. }
  431. }
  432. // ARM32 helpers.
  433. public static Operand GetMemM(ArmEmitterContext context, bool setCarry = true)
  434. {
  435. switch (context.CurrOp)
  436. {
  437. case OpCode32MemRsImm op: return GetMShiftedByImmediate(context, op, setCarry);
  438. case OpCode32MemReg op: return GetIntA32(context, op.Rm);
  439. case OpCode32Mem op: return Const(op.Immediate);
  440. case OpCode32SimdMemImm op: return Const(op.Immediate);
  441. default: throw InvalidOpCodeType(context.CurrOp);
  442. }
  443. }
  444. private static Exception InvalidOpCodeType(OpCode opCode)
  445. {
  446. return new InvalidOperationException($"Invalid OpCode type \"{opCode?.GetType().Name ?? "null"}\".");
  447. }
  448. public static Operand GetMShiftedByImmediate(ArmEmitterContext context, OpCode32MemRsImm op, bool setCarry)
  449. {
  450. Operand m = GetIntA32(context, op.Rm);
  451. int shift = op.Immediate;
  452. if (shift == 0)
  453. {
  454. switch (op.ShiftType)
  455. {
  456. case ShiftType.Lsr: shift = 32; break;
  457. case ShiftType.Asr: shift = 32; break;
  458. case ShiftType.Ror: shift = 1; break;
  459. }
  460. }
  461. if (shift != 0)
  462. {
  463. setCarry &= false;
  464. switch (op.ShiftType)
  465. {
  466. case ShiftType.Lsl: m = InstEmitAluHelper.GetLslC(context, m, setCarry, shift); break;
  467. case ShiftType.Lsr: m = InstEmitAluHelper.GetLsrC(context, m, setCarry, shift); break;
  468. case ShiftType.Asr: m = InstEmitAluHelper.GetAsrC(context, m, setCarry, shift); break;
  469. case ShiftType.Ror:
  470. if (op.Immediate != 0)
  471. {
  472. m = InstEmitAluHelper.GetRorC(context, m, setCarry, shift);
  473. }
  474. else
  475. {
  476. m = InstEmitAluHelper.GetRrxC(context, m, setCarry);
  477. }
  478. break;
  479. }
  480. }
  481. return m;
  482. }
  483. }
  484. }