insn.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. * Copyright (C) 2013 Huawei Ltd.
  3. * Author: Jiang Liu <liuj97@gmail.com>
  4. *
  5. * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/bug.h>
  21. #include <linux/compiler.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/smp.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/stop_machine.h>
  27. #include <linux/types.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/debug-monitors.h>
  31. #include <asm/fixmap.h>
  32. #include <asm/insn.h>
  33. #define AARCH64_INSN_SF_BIT BIT(31)
  34. #define AARCH64_INSN_N_BIT BIT(22)
  35. static int aarch64_insn_encoding_class[] = {
  36. AARCH64_INSN_CLS_UNKNOWN,
  37. AARCH64_INSN_CLS_UNKNOWN,
  38. AARCH64_INSN_CLS_UNKNOWN,
  39. AARCH64_INSN_CLS_UNKNOWN,
  40. AARCH64_INSN_CLS_LDST,
  41. AARCH64_INSN_CLS_DP_REG,
  42. AARCH64_INSN_CLS_LDST,
  43. AARCH64_INSN_CLS_DP_FPSIMD,
  44. AARCH64_INSN_CLS_DP_IMM,
  45. AARCH64_INSN_CLS_DP_IMM,
  46. AARCH64_INSN_CLS_BR_SYS,
  47. AARCH64_INSN_CLS_BR_SYS,
  48. AARCH64_INSN_CLS_LDST,
  49. AARCH64_INSN_CLS_DP_REG,
  50. AARCH64_INSN_CLS_LDST,
  51. AARCH64_INSN_CLS_DP_FPSIMD,
  52. };
  53. enum aarch64_insn_encoding_class __kprobes aarch64_get_insn_class(u32 insn)
  54. {
  55. return aarch64_insn_encoding_class[(insn >> 25) & 0xf];
  56. }
  57. /* NOP is an alias of HINT */
  58. bool __kprobes aarch64_insn_is_nop(u32 insn)
  59. {
  60. if (!aarch64_insn_is_hint(insn))
  61. return false;
  62. switch (insn & 0xFE0) {
  63. case AARCH64_INSN_HINT_YIELD:
  64. case AARCH64_INSN_HINT_WFE:
  65. case AARCH64_INSN_HINT_WFI:
  66. case AARCH64_INSN_HINT_SEV:
  67. case AARCH64_INSN_HINT_SEVL:
  68. return false;
  69. default:
  70. return true;
  71. }
  72. }
  73. bool aarch64_insn_is_branch_imm(u32 insn)
  74. {
  75. return (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn) ||
  76. aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn) ||
  77. aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) ||
  78. aarch64_insn_is_bcond(insn));
  79. }
  80. static DEFINE_RAW_SPINLOCK(patch_lock);
  81. static void __kprobes *patch_map(void *addr, int fixmap)
  82. {
  83. unsigned long uintaddr = (uintptr_t) addr;
  84. bool module = !core_kernel_text(uintaddr);
  85. struct page *page;
  86. if (module && IS_ENABLED(CONFIG_DEBUG_SET_MODULE_RONX))
  87. page = vmalloc_to_page(addr);
  88. else if (!module && IS_ENABLED(CONFIG_DEBUG_RODATA))
  89. page = virt_to_page(addr);
  90. else
  91. return addr;
  92. BUG_ON(!page);
  93. return (void *)set_fixmap_offset(fixmap, page_to_phys(page) +
  94. (uintaddr & ~PAGE_MASK));
  95. }
  96. static void __kprobes patch_unmap(int fixmap)
  97. {
  98. clear_fixmap(fixmap);
  99. }
  100. /*
  101. * In ARMv8-A, A64 instructions have a fixed length of 32 bits and are always
  102. * little-endian.
  103. */
  104. int __kprobes aarch64_insn_read(void *addr, u32 *insnp)
  105. {
  106. int ret;
  107. u32 val;
  108. ret = probe_kernel_read(&val, addr, AARCH64_INSN_SIZE);
  109. if (!ret)
  110. *insnp = le32_to_cpu(val);
  111. return ret;
  112. }
  113. static int __kprobes __aarch64_insn_write(void *addr, u32 insn)
  114. {
  115. void *waddr = addr;
  116. unsigned long flags = 0;
  117. int ret;
  118. raw_spin_lock_irqsave(&patch_lock, flags);
  119. waddr = patch_map(addr, FIX_TEXT_POKE0);
  120. ret = probe_kernel_write(waddr, &insn, AARCH64_INSN_SIZE);
  121. patch_unmap(FIX_TEXT_POKE0);
  122. raw_spin_unlock_irqrestore(&patch_lock, flags);
  123. return ret;
  124. }
  125. int __kprobes aarch64_insn_write(void *addr, u32 insn)
  126. {
  127. insn = cpu_to_le32(insn);
  128. return __aarch64_insn_write(addr, insn);
  129. }
  130. static bool __kprobes __aarch64_insn_hotpatch_safe(u32 insn)
  131. {
  132. if (aarch64_get_insn_class(insn) != AARCH64_INSN_CLS_BR_SYS)
  133. return false;
  134. return aarch64_insn_is_b(insn) ||
  135. aarch64_insn_is_bl(insn) ||
  136. aarch64_insn_is_svc(insn) ||
  137. aarch64_insn_is_hvc(insn) ||
  138. aarch64_insn_is_smc(insn) ||
  139. aarch64_insn_is_brk(insn) ||
  140. aarch64_insn_is_nop(insn);
  141. }
  142. /*
  143. * ARM Architecture Reference Manual for ARMv8 Profile-A, Issue A.a
  144. * Section B2.6.5 "Concurrent modification and execution of instructions":
  145. * Concurrent modification and execution of instructions can lead to the
  146. * resulting instruction performing any behavior that can be achieved by
  147. * executing any sequence of instructions that can be executed from the
  148. * same Exception level, except where the instruction before modification
  149. * and the instruction after modification is a B, BL, NOP, BKPT, SVC, HVC,
  150. * or SMC instruction.
  151. */
  152. bool __kprobes aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn)
  153. {
  154. return __aarch64_insn_hotpatch_safe(old_insn) &&
  155. __aarch64_insn_hotpatch_safe(new_insn);
  156. }
  157. int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn)
  158. {
  159. u32 *tp = addr;
  160. int ret;
  161. /* A64 instructions must be word aligned */
  162. if ((uintptr_t)tp & 0x3)
  163. return -EINVAL;
  164. ret = aarch64_insn_write(tp, insn);
  165. if (ret == 0)
  166. flush_icache_range((uintptr_t)tp,
  167. (uintptr_t)tp + AARCH64_INSN_SIZE);
  168. return ret;
  169. }
  170. struct aarch64_insn_patch {
  171. void **text_addrs;
  172. u32 *new_insns;
  173. int insn_cnt;
  174. atomic_t cpu_count;
  175. };
  176. static int __kprobes aarch64_insn_patch_text_cb(void *arg)
  177. {
  178. int i, ret = 0;
  179. struct aarch64_insn_patch *pp = arg;
  180. /* The first CPU becomes master */
  181. if (atomic_inc_return(&pp->cpu_count) == 1) {
  182. for (i = 0; ret == 0 && i < pp->insn_cnt; i++)
  183. ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i],
  184. pp->new_insns[i]);
  185. /*
  186. * aarch64_insn_patch_text_nosync() calls flush_icache_range(),
  187. * which ends with "dsb; isb" pair guaranteeing global
  188. * visibility.
  189. */
  190. /* Notify other processors with an additional increment. */
  191. atomic_inc(&pp->cpu_count);
  192. } else {
  193. while (atomic_read(&pp->cpu_count) <= num_online_cpus())
  194. cpu_relax();
  195. isb();
  196. }
  197. return ret;
  198. }
  199. int __kprobes aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt)
  200. {
  201. struct aarch64_insn_patch patch = {
  202. .text_addrs = addrs,
  203. .new_insns = insns,
  204. .insn_cnt = cnt,
  205. .cpu_count = ATOMIC_INIT(0),
  206. };
  207. if (cnt <= 0)
  208. return -EINVAL;
  209. return stop_machine(aarch64_insn_patch_text_cb, &patch,
  210. cpu_online_mask);
  211. }
  212. int __kprobes aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt)
  213. {
  214. int ret;
  215. u32 insn;
  216. /* Unsafe to patch multiple instructions without synchronizaiton */
  217. if (cnt == 1) {
  218. ret = aarch64_insn_read(addrs[0], &insn);
  219. if (ret)
  220. return ret;
  221. if (aarch64_insn_hotpatch_safe(insn, insns[0])) {
  222. /*
  223. * ARMv8 architecture doesn't guarantee all CPUs see
  224. * the new instruction after returning from function
  225. * aarch64_insn_patch_text_nosync(). So send IPIs to
  226. * all other CPUs to achieve instruction
  227. * synchronization.
  228. */
  229. ret = aarch64_insn_patch_text_nosync(addrs[0], insns[0]);
  230. kick_all_cpus_sync();
  231. return ret;
  232. }
  233. }
  234. return aarch64_insn_patch_text_sync(addrs, insns, cnt);
  235. }
  236. static int __kprobes aarch64_get_imm_shift_mask(enum aarch64_insn_imm_type type,
  237. u32 *maskp, int *shiftp)
  238. {
  239. u32 mask;
  240. int shift;
  241. switch (type) {
  242. case AARCH64_INSN_IMM_26:
  243. mask = BIT(26) - 1;
  244. shift = 0;
  245. break;
  246. case AARCH64_INSN_IMM_19:
  247. mask = BIT(19) - 1;
  248. shift = 5;
  249. break;
  250. case AARCH64_INSN_IMM_16:
  251. mask = BIT(16) - 1;
  252. shift = 5;
  253. break;
  254. case AARCH64_INSN_IMM_14:
  255. mask = BIT(14) - 1;
  256. shift = 5;
  257. break;
  258. case AARCH64_INSN_IMM_12:
  259. mask = BIT(12) - 1;
  260. shift = 10;
  261. break;
  262. case AARCH64_INSN_IMM_9:
  263. mask = BIT(9) - 1;
  264. shift = 12;
  265. break;
  266. case AARCH64_INSN_IMM_7:
  267. mask = BIT(7) - 1;
  268. shift = 15;
  269. break;
  270. case AARCH64_INSN_IMM_6:
  271. case AARCH64_INSN_IMM_S:
  272. mask = BIT(6) - 1;
  273. shift = 10;
  274. break;
  275. case AARCH64_INSN_IMM_R:
  276. mask = BIT(6) - 1;
  277. shift = 16;
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. *maskp = mask;
  283. *shiftp = shift;
  284. return 0;
  285. }
  286. #define ADR_IMM_HILOSPLIT 2
  287. #define ADR_IMM_SIZE SZ_2M
  288. #define ADR_IMM_LOMASK ((1 << ADR_IMM_HILOSPLIT) - 1)
  289. #define ADR_IMM_HIMASK ((ADR_IMM_SIZE >> ADR_IMM_HILOSPLIT) - 1)
  290. #define ADR_IMM_LOSHIFT 29
  291. #define ADR_IMM_HISHIFT 5
  292. u64 aarch64_insn_decode_immediate(enum aarch64_insn_imm_type type, u32 insn)
  293. {
  294. u32 immlo, immhi, mask;
  295. int shift;
  296. switch (type) {
  297. case AARCH64_INSN_IMM_ADR:
  298. shift = 0;
  299. immlo = (insn >> ADR_IMM_LOSHIFT) & ADR_IMM_LOMASK;
  300. immhi = (insn >> ADR_IMM_HISHIFT) & ADR_IMM_HIMASK;
  301. insn = (immhi << ADR_IMM_HILOSPLIT) | immlo;
  302. mask = ADR_IMM_SIZE - 1;
  303. break;
  304. default:
  305. if (aarch64_get_imm_shift_mask(type, &mask, &shift) < 0) {
  306. pr_err("aarch64_insn_decode_immediate: unknown immediate encoding %d\n",
  307. type);
  308. return 0;
  309. }
  310. }
  311. return (insn >> shift) & mask;
  312. }
  313. u32 __kprobes aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
  314. u32 insn, u64 imm)
  315. {
  316. u32 immlo, immhi, mask;
  317. int shift;
  318. switch (type) {
  319. case AARCH64_INSN_IMM_ADR:
  320. shift = 0;
  321. immlo = (imm & ADR_IMM_LOMASK) << ADR_IMM_LOSHIFT;
  322. imm >>= ADR_IMM_HILOSPLIT;
  323. immhi = (imm & ADR_IMM_HIMASK) << ADR_IMM_HISHIFT;
  324. imm = immlo | immhi;
  325. mask = ((ADR_IMM_LOMASK << ADR_IMM_LOSHIFT) |
  326. (ADR_IMM_HIMASK << ADR_IMM_HISHIFT));
  327. break;
  328. default:
  329. if (aarch64_get_imm_shift_mask(type, &mask, &shift) < 0) {
  330. pr_err("aarch64_insn_encode_immediate: unknown immediate encoding %d\n",
  331. type);
  332. return 0;
  333. }
  334. }
  335. /* Update the immediate field. */
  336. insn &= ~(mask << shift);
  337. insn |= (imm & mask) << shift;
  338. return insn;
  339. }
  340. static u32 aarch64_insn_encode_register(enum aarch64_insn_register_type type,
  341. u32 insn,
  342. enum aarch64_insn_register reg)
  343. {
  344. int shift;
  345. if (reg < AARCH64_INSN_REG_0 || reg > AARCH64_INSN_REG_SP) {
  346. pr_err("%s: unknown register encoding %d\n", __func__, reg);
  347. return 0;
  348. }
  349. switch (type) {
  350. case AARCH64_INSN_REGTYPE_RT:
  351. case AARCH64_INSN_REGTYPE_RD:
  352. shift = 0;
  353. break;
  354. case AARCH64_INSN_REGTYPE_RN:
  355. shift = 5;
  356. break;
  357. case AARCH64_INSN_REGTYPE_RT2:
  358. case AARCH64_INSN_REGTYPE_RA:
  359. shift = 10;
  360. break;
  361. case AARCH64_INSN_REGTYPE_RM:
  362. shift = 16;
  363. break;
  364. default:
  365. pr_err("%s: unknown register type encoding %d\n", __func__,
  366. type);
  367. return 0;
  368. }
  369. insn &= ~(GENMASK(4, 0) << shift);
  370. insn |= reg << shift;
  371. return insn;
  372. }
  373. static u32 aarch64_insn_encode_ldst_size(enum aarch64_insn_size_type type,
  374. u32 insn)
  375. {
  376. u32 size;
  377. switch (type) {
  378. case AARCH64_INSN_SIZE_8:
  379. size = 0;
  380. break;
  381. case AARCH64_INSN_SIZE_16:
  382. size = 1;
  383. break;
  384. case AARCH64_INSN_SIZE_32:
  385. size = 2;
  386. break;
  387. case AARCH64_INSN_SIZE_64:
  388. size = 3;
  389. break;
  390. default:
  391. pr_err("%s: unknown size encoding %d\n", __func__, type);
  392. return 0;
  393. }
  394. insn &= ~GENMASK(31, 30);
  395. insn |= size << 30;
  396. return insn;
  397. }
  398. static inline long branch_imm_common(unsigned long pc, unsigned long addr,
  399. long range)
  400. {
  401. long offset;
  402. /*
  403. * PC: A 64-bit Program Counter holding the address of the current
  404. * instruction. A64 instructions must be word-aligned.
  405. */
  406. BUG_ON((pc & 0x3) || (addr & 0x3));
  407. offset = ((long)addr - (long)pc);
  408. BUG_ON(offset < -range || offset >= range);
  409. return offset;
  410. }
  411. u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
  412. enum aarch64_insn_branch_type type)
  413. {
  414. u32 insn;
  415. long offset;
  416. /*
  417. * B/BL support [-128M, 128M) offset
  418. * ARM64 virtual address arrangement guarantees all kernel and module
  419. * texts are within +/-128M.
  420. */
  421. offset = branch_imm_common(pc, addr, SZ_128M);
  422. switch (type) {
  423. case AARCH64_INSN_BRANCH_LINK:
  424. insn = aarch64_insn_get_bl_value();
  425. break;
  426. case AARCH64_INSN_BRANCH_NOLINK:
  427. insn = aarch64_insn_get_b_value();
  428. break;
  429. default:
  430. BUG_ON(1);
  431. return AARCH64_BREAK_FAULT;
  432. }
  433. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_26, insn,
  434. offset >> 2);
  435. }
  436. u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
  437. enum aarch64_insn_register reg,
  438. enum aarch64_insn_variant variant,
  439. enum aarch64_insn_branch_type type)
  440. {
  441. u32 insn;
  442. long offset;
  443. offset = branch_imm_common(pc, addr, SZ_1M);
  444. switch (type) {
  445. case AARCH64_INSN_BRANCH_COMP_ZERO:
  446. insn = aarch64_insn_get_cbz_value();
  447. break;
  448. case AARCH64_INSN_BRANCH_COMP_NONZERO:
  449. insn = aarch64_insn_get_cbnz_value();
  450. break;
  451. default:
  452. BUG_ON(1);
  453. return AARCH64_BREAK_FAULT;
  454. }
  455. switch (variant) {
  456. case AARCH64_INSN_VARIANT_32BIT:
  457. break;
  458. case AARCH64_INSN_VARIANT_64BIT:
  459. insn |= AARCH64_INSN_SF_BIT;
  460. break;
  461. default:
  462. BUG_ON(1);
  463. return AARCH64_BREAK_FAULT;
  464. }
  465. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
  466. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
  467. offset >> 2);
  468. }
  469. u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
  470. enum aarch64_insn_condition cond)
  471. {
  472. u32 insn;
  473. long offset;
  474. offset = branch_imm_common(pc, addr, SZ_1M);
  475. insn = aarch64_insn_get_bcond_value();
  476. BUG_ON(cond < AARCH64_INSN_COND_EQ || cond > AARCH64_INSN_COND_AL);
  477. insn |= cond;
  478. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
  479. offset >> 2);
  480. }
  481. u32 __kprobes aarch64_insn_gen_hint(enum aarch64_insn_hint_op op)
  482. {
  483. return aarch64_insn_get_hint_value() | op;
  484. }
  485. u32 __kprobes aarch64_insn_gen_nop(void)
  486. {
  487. return aarch64_insn_gen_hint(AARCH64_INSN_HINT_NOP);
  488. }
  489. u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
  490. enum aarch64_insn_branch_type type)
  491. {
  492. u32 insn;
  493. switch (type) {
  494. case AARCH64_INSN_BRANCH_NOLINK:
  495. insn = aarch64_insn_get_br_value();
  496. break;
  497. case AARCH64_INSN_BRANCH_LINK:
  498. insn = aarch64_insn_get_blr_value();
  499. break;
  500. case AARCH64_INSN_BRANCH_RETURN:
  501. insn = aarch64_insn_get_ret_value();
  502. break;
  503. default:
  504. BUG_ON(1);
  505. return AARCH64_BREAK_FAULT;
  506. }
  507. return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, reg);
  508. }
  509. u32 aarch64_insn_gen_load_store_reg(enum aarch64_insn_register reg,
  510. enum aarch64_insn_register base,
  511. enum aarch64_insn_register offset,
  512. enum aarch64_insn_size_type size,
  513. enum aarch64_insn_ldst_type type)
  514. {
  515. u32 insn;
  516. switch (type) {
  517. case AARCH64_INSN_LDST_LOAD_REG_OFFSET:
  518. insn = aarch64_insn_get_ldr_reg_value();
  519. break;
  520. case AARCH64_INSN_LDST_STORE_REG_OFFSET:
  521. insn = aarch64_insn_get_str_reg_value();
  522. break;
  523. default:
  524. BUG_ON(1);
  525. return AARCH64_BREAK_FAULT;
  526. }
  527. insn = aarch64_insn_encode_ldst_size(size, insn);
  528. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
  529. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
  530. base);
  531. return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn,
  532. offset);
  533. }
  534. u32 aarch64_insn_gen_load_store_pair(enum aarch64_insn_register reg1,
  535. enum aarch64_insn_register reg2,
  536. enum aarch64_insn_register base,
  537. int offset,
  538. enum aarch64_insn_variant variant,
  539. enum aarch64_insn_ldst_type type)
  540. {
  541. u32 insn;
  542. int shift;
  543. switch (type) {
  544. case AARCH64_INSN_LDST_LOAD_PAIR_PRE_INDEX:
  545. insn = aarch64_insn_get_ldp_pre_value();
  546. break;
  547. case AARCH64_INSN_LDST_STORE_PAIR_PRE_INDEX:
  548. insn = aarch64_insn_get_stp_pre_value();
  549. break;
  550. case AARCH64_INSN_LDST_LOAD_PAIR_POST_INDEX:
  551. insn = aarch64_insn_get_ldp_post_value();
  552. break;
  553. case AARCH64_INSN_LDST_STORE_PAIR_POST_INDEX:
  554. insn = aarch64_insn_get_stp_post_value();
  555. break;
  556. default:
  557. BUG_ON(1);
  558. return AARCH64_BREAK_FAULT;
  559. }
  560. switch (variant) {
  561. case AARCH64_INSN_VARIANT_32BIT:
  562. /* offset must be multiples of 4 in the range [-256, 252] */
  563. BUG_ON(offset & 0x3);
  564. BUG_ON(offset < -256 || offset > 252);
  565. shift = 2;
  566. break;
  567. case AARCH64_INSN_VARIANT_64BIT:
  568. /* offset must be multiples of 8 in the range [-512, 504] */
  569. BUG_ON(offset & 0x7);
  570. BUG_ON(offset < -512 || offset > 504);
  571. shift = 3;
  572. insn |= AARCH64_INSN_SF_BIT;
  573. break;
  574. default:
  575. BUG_ON(1);
  576. return AARCH64_BREAK_FAULT;
  577. }
  578. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn,
  579. reg1);
  580. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT2, insn,
  581. reg2);
  582. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
  583. base);
  584. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_7, insn,
  585. offset >> shift);
  586. }
  587. u32 aarch64_insn_gen_add_sub_imm(enum aarch64_insn_register dst,
  588. enum aarch64_insn_register src,
  589. int imm, enum aarch64_insn_variant variant,
  590. enum aarch64_insn_adsb_type type)
  591. {
  592. u32 insn;
  593. switch (type) {
  594. case AARCH64_INSN_ADSB_ADD:
  595. insn = aarch64_insn_get_add_imm_value();
  596. break;
  597. case AARCH64_INSN_ADSB_SUB:
  598. insn = aarch64_insn_get_sub_imm_value();
  599. break;
  600. case AARCH64_INSN_ADSB_ADD_SETFLAGS:
  601. insn = aarch64_insn_get_adds_imm_value();
  602. break;
  603. case AARCH64_INSN_ADSB_SUB_SETFLAGS:
  604. insn = aarch64_insn_get_subs_imm_value();
  605. break;
  606. default:
  607. BUG_ON(1);
  608. return AARCH64_BREAK_FAULT;
  609. }
  610. switch (variant) {
  611. case AARCH64_INSN_VARIANT_32BIT:
  612. break;
  613. case AARCH64_INSN_VARIANT_64BIT:
  614. insn |= AARCH64_INSN_SF_BIT;
  615. break;
  616. default:
  617. BUG_ON(1);
  618. return AARCH64_BREAK_FAULT;
  619. }
  620. BUG_ON(imm & ~(SZ_4K - 1));
  621. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  622. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  623. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_12, insn, imm);
  624. }
  625. u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst,
  626. enum aarch64_insn_register src,
  627. int immr, int imms,
  628. enum aarch64_insn_variant variant,
  629. enum aarch64_insn_bitfield_type type)
  630. {
  631. u32 insn;
  632. u32 mask;
  633. switch (type) {
  634. case AARCH64_INSN_BITFIELD_MOVE:
  635. insn = aarch64_insn_get_bfm_value();
  636. break;
  637. case AARCH64_INSN_BITFIELD_MOVE_UNSIGNED:
  638. insn = aarch64_insn_get_ubfm_value();
  639. break;
  640. case AARCH64_INSN_BITFIELD_MOVE_SIGNED:
  641. insn = aarch64_insn_get_sbfm_value();
  642. break;
  643. default:
  644. BUG_ON(1);
  645. return AARCH64_BREAK_FAULT;
  646. }
  647. switch (variant) {
  648. case AARCH64_INSN_VARIANT_32BIT:
  649. mask = GENMASK(4, 0);
  650. break;
  651. case AARCH64_INSN_VARIANT_64BIT:
  652. insn |= AARCH64_INSN_SF_BIT | AARCH64_INSN_N_BIT;
  653. mask = GENMASK(5, 0);
  654. break;
  655. default:
  656. BUG_ON(1);
  657. return AARCH64_BREAK_FAULT;
  658. }
  659. BUG_ON(immr & ~mask);
  660. BUG_ON(imms & ~mask);
  661. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  662. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  663. insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_R, insn, immr);
  664. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_S, insn, imms);
  665. }
  666. u32 aarch64_insn_gen_movewide(enum aarch64_insn_register dst,
  667. int imm, int shift,
  668. enum aarch64_insn_variant variant,
  669. enum aarch64_insn_movewide_type type)
  670. {
  671. u32 insn;
  672. switch (type) {
  673. case AARCH64_INSN_MOVEWIDE_ZERO:
  674. insn = aarch64_insn_get_movz_value();
  675. break;
  676. case AARCH64_INSN_MOVEWIDE_KEEP:
  677. insn = aarch64_insn_get_movk_value();
  678. break;
  679. case AARCH64_INSN_MOVEWIDE_INVERSE:
  680. insn = aarch64_insn_get_movn_value();
  681. break;
  682. default:
  683. BUG_ON(1);
  684. return AARCH64_BREAK_FAULT;
  685. }
  686. BUG_ON(imm & ~(SZ_64K - 1));
  687. switch (variant) {
  688. case AARCH64_INSN_VARIANT_32BIT:
  689. BUG_ON(shift != 0 && shift != 16);
  690. break;
  691. case AARCH64_INSN_VARIANT_64BIT:
  692. insn |= AARCH64_INSN_SF_BIT;
  693. BUG_ON(shift != 0 && shift != 16 && shift != 32 &&
  694. shift != 48);
  695. break;
  696. default:
  697. BUG_ON(1);
  698. return AARCH64_BREAK_FAULT;
  699. }
  700. insn |= (shift >> 4) << 21;
  701. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  702. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
  703. }
  704. u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,
  705. enum aarch64_insn_register src,
  706. enum aarch64_insn_register reg,
  707. int shift,
  708. enum aarch64_insn_variant variant,
  709. enum aarch64_insn_adsb_type type)
  710. {
  711. u32 insn;
  712. switch (type) {
  713. case AARCH64_INSN_ADSB_ADD:
  714. insn = aarch64_insn_get_add_value();
  715. break;
  716. case AARCH64_INSN_ADSB_SUB:
  717. insn = aarch64_insn_get_sub_value();
  718. break;
  719. case AARCH64_INSN_ADSB_ADD_SETFLAGS:
  720. insn = aarch64_insn_get_adds_value();
  721. break;
  722. case AARCH64_INSN_ADSB_SUB_SETFLAGS:
  723. insn = aarch64_insn_get_subs_value();
  724. break;
  725. default:
  726. BUG_ON(1);
  727. return AARCH64_BREAK_FAULT;
  728. }
  729. switch (variant) {
  730. case AARCH64_INSN_VARIANT_32BIT:
  731. BUG_ON(shift & ~(SZ_32 - 1));
  732. break;
  733. case AARCH64_INSN_VARIANT_64BIT:
  734. insn |= AARCH64_INSN_SF_BIT;
  735. BUG_ON(shift & ~(SZ_64 - 1));
  736. break;
  737. default:
  738. BUG_ON(1);
  739. return AARCH64_BREAK_FAULT;
  740. }
  741. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  742. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  743. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
  744. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
  745. }
  746. u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,
  747. enum aarch64_insn_register src,
  748. enum aarch64_insn_variant variant,
  749. enum aarch64_insn_data1_type type)
  750. {
  751. u32 insn;
  752. switch (type) {
  753. case AARCH64_INSN_DATA1_REVERSE_16:
  754. insn = aarch64_insn_get_rev16_value();
  755. break;
  756. case AARCH64_INSN_DATA1_REVERSE_32:
  757. insn = aarch64_insn_get_rev32_value();
  758. break;
  759. case AARCH64_INSN_DATA1_REVERSE_64:
  760. BUG_ON(variant != AARCH64_INSN_VARIANT_64BIT);
  761. insn = aarch64_insn_get_rev64_value();
  762. break;
  763. default:
  764. BUG_ON(1);
  765. return AARCH64_BREAK_FAULT;
  766. }
  767. switch (variant) {
  768. case AARCH64_INSN_VARIANT_32BIT:
  769. break;
  770. case AARCH64_INSN_VARIANT_64BIT:
  771. insn |= AARCH64_INSN_SF_BIT;
  772. break;
  773. default:
  774. BUG_ON(1);
  775. return AARCH64_BREAK_FAULT;
  776. }
  777. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  778. return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  779. }
  780. u32 aarch64_insn_gen_data2(enum aarch64_insn_register dst,
  781. enum aarch64_insn_register src,
  782. enum aarch64_insn_register reg,
  783. enum aarch64_insn_variant variant,
  784. enum aarch64_insn_data2_type type)
  785. {
  786. u32 insn;
  787. switch (type) {
  788. case AARCH64_INSN_DATA2_UDIV:
  789. insn = aarch64_insn_get_udiv_value();
  790. break;
  791. case AARCH64_INSN_DATA2_SDIV:
  792. insn = aarch64_insn_get_sdiv_value();
  793. break;
  794. case AARCH64_INSN_DATA2_LSLV:
  795. insn = aarch64_insn_get_lslv_value();
  796. break;
  797. case AARCH64_INSN_DATA2_LSRV:
  798. insn = aarch64_insn_get_lsrv_value();
  799. break;
  800. case AARCH64_INSN_DATA2_ASRV:
  801. insn = aarch64_insn_get_asrv_value();
  802. break;
  803. case AARCH64_INSN_DATA2_RORV:
  804. insn = aarch64_insn_get_rorv_value();
  805. break;
  806. default:
  807. BUG_ON(1);
  808. return AARCH64_BREAK_FAULT;
  809. }
  810. switch (variant) {
  811. case AARCH64_INSN_VARIANT_32BIT:
  812. break;
  813. case AARCH64_INSN_VARIANT_64BIT:
  814. insn |= AARCH64_INSN_SF_BIT;
  815. break;
  816. default:
  817. BUG_ON(1);
  818. return AARCH64_BREAK_FAULT;
  819. }
  820. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  821. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  822. return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
  823. }
  824. u32 aarch64_insn_gen_data3(enum aarch64_insn_register dst,
  825. enum aarch64_insn_register src,
  826. enum aarch64_insn_register reg1,
  827. enum aarch64_insn_register reg2,
  828. enum aarch64_insn_variant variant,
  829. enum aarch64_insn_data3_type type)
  830. {
  831. u32 insn;
  832. switch (type) {
  833. case AARCH64_INSN_DATA3_MADD:
  834. insn = aarch64_insn_get_madd_value();
  835. break;
  836. case AARCH64_INSN_DATA3_MSUB:
  837. insn = aarch64_insn_get_msub_value();
  838. break;
  839. default:
  840. BUG_ON(1);
  841. return AARCH64_BREAK_FAULT;
  842. }
  843. switch (variant) {
  844. case AARCH64_INSN_VARIANT_32BIT:
  845. break;
  846. case AARCH64_INSN_VARIANT_64BIT:
  847. insn |= AARCH64_INSN_SF_BIT;
  848. break;
  849. default:
  850. BUG_ON(1);
  851. return AARCH64_BREAK_FAULT;
  852. }
  853. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  854. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RA, insn, src);
  855. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
  856. reg1);
  857. return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn,
  858. reg2);
  859. }
  860. u32 aarch64_insn_gen_logical_shifted_reg(enum aarch64_insn_register dst,
  861. enum aarch64_insn_register src,
  862. enum aarch64_insn_register reg,
  863. int shift,
  864. enum aarch64_insn_variant variant,
  865. enum aarch64_insn_logic_type type)
  866. {
  867. u32 insn;
  868. switch (type) {
  869. case AARCH64_INSN_LOGIC_AND:
  870. insn = aarch64_insn_get_and_value();
  871. break;
  872. case AARCH64_INSN_LOGIC_BIC:
  873. insn = aarch64_insn_get_bic_value();
  874. break;
  875. case AARCH64_INSN_LOGIC_ORR:
  876. insn = aarch64_insn_get_orr_value();
  877. break;
  878. case AARCH64_INSN_LOGIC_ORN:
  879. insn = aarch64_insn_get_orn_value();
  880. break;
  881. case AARCH64_INSN_LOGIC_EOR:
  882. insn = aarch64_insn_get_eor_value();
  883. break;
  884. case AARCH64_INSN_LOGIC_EON:
  885. insn = aarch64_insn_get_eon_value();
  886. break;
  887. case AARCH64_INSN_LOGIC_AND_SETFLAGS:
  888. insn = aarch64_insn_get_ands_value();
  889. break;
  890. case AARCH64_INSN_LOGIC_BIC_SETFLAGS:
  891. insn = aarch64_insn_get_bics_value();
  892. break;
  893. default:
  894. BUG_ON(1);
  895. return AARCH64_BREAK_FAULT;
  896. }
  897. switch (variant) {
  898. case AARCH64_INSN_VARIANT_32BIT:
  899. BUG_ON(shift & ~(SZ_32 - 1));
  900. break;
  901. case AARCH64_INSN_VARIANT_64BIT:
  902. insn |= AARCH64_INSN_SF_BIT;
  903. BUG_ON(shift & ~(SZ_64 - 1));
  904. break;
  905. default:
  906. BUG_ON(1);
  907. return AARCH64_BREAK_FAULT;
  908. }
  909. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
  910. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
  911. insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
  912. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
  913. }
  914. /*
  915. * Decode the imm field of a branch, and return the byte offset as a
  916. * signed value (so it can be used when computing a new branch
  917. * target).
  918. */
  919. s32 aarch64_get_branch_offset(u32 insn)
  920. {
  921. s32 imm;
  922. if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn)) {
  923. imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_26, insn);
  924. return (imm << 6) >> 4;
  925. }
  926. if (aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) ||
  927. aarch64_insn_is_bcond(insn)) {
  928. imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_19, insn);
  929. return (imm << 13) >> 11;
  930. }
  931. if (aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn)) {
  932. imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_14, insn);
  933. return (imm << 18) >> 16;
  934. }
  935. /* Unhandled instruction */
  936. BUG();
  937. }
  938. /*
  939. * Encode the displacement of a branch in the imm field and return the
  940. * updated instruction.
  941. */
  942. u32 aarch64_set_branch_offset(u32 insn, s32 offset)
  943. {
  944. if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn))
  945. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_26, insn,
  946. offset >> 2);
  947. if (aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) ||
  948. aarch64_insn_is_bcond(insn))
  949. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
  950. offset >> 2);
  951. if (aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn))
  952. return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_14, insn,
  953. offset >> 2);
  954. /* Unhandled instruction */
  955. BUG();
  956. }
  957. bool aarch32_insn_is_wide(u32 insn)
  958. {
  959. return insn >= 0xe800;
  960. }
  961. /*
  962. * Macros/defines for extracting register numbers from instruction.
  963. */
  964. u32 aarch32_insn_extract_reg_num(u32 insn, int offset)
  965. {
  966. return (insn & (0xf << offset)) >> offset;
  967. }
  968. #define OPC2_MASK 0x7
  969. #define OPC2_OFFSET 5
  970. u32 aarch32_insn_mcr_extract_opc2(u32 insn)
  971. {
  972. return (insn & (OPC2_MASK << OPC2_OFFSET)) >> OPC2_OFFSET;
  973. }
  974. #define CRM_MASK 0xf
  975. u32 aarch32_insn_mcr_extract_crm(u32 insn)
  976. {
  977. return insn & CRM_MASK;
  978. }