insn.c 33 KB

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