1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Huawei Ltd.
8 #include <linux/bitops.h>
10 #include <linux/printk.h>
11 #include <linux/sizes.h>
12 #include <linux/types.h>
14 #include <asm/debug-monitors.h>
15 #include <asm/errno.h>
17 #include <asm/kprobes.h>
19 #define AARCH64_INSN_SF_BIT BIT(31)
20 #define AARCH64_INSN_N_BIT BIT(22)
21 #define AARCH64_INSN_LSL_12 BIT(22)
23 static int __kprobes aarch64_get_imm_shift_mask(enum aarch64_insn_imm_type type,
24 u32 *maskp, int *shiftp)
30 case AARCH64_INSN_IMM_26:
34 case AARCH64_INSN_IMM_19:
38 case AARCH64_INSN_IMM_16:
42 case AARCH64_INSN_IMM_14:
46 case AARCH64_INSN_IMM_12:
50 case AARCH64_INSN_IMM_9:
54 case AARCH64_INSN_IMM_7:
58 case AARCH64_INSN_IMM_6:
59 case AARCH64_INSN_IMM_S:
63 case AARCH64_INSN_IMM_R:
67 case AARCH64_INSN_IMM_N:
81 #define ADR_IMM_HILOSPLIT 2
82 #define ADR_IMM_SIZE SZ_2M
83 #define ADR_IMM_LOMASK ((1 << ADR_IMM_HILOSPLIT) - 1)
84 #define ADR_IMM_HIMASK ((ADR_IMM_SIZE >> ADR_IMM_HILOSPLIT) - 1)
85 #define ADR_IMM_LOSHIFT 29
86 #define ADR_IMM_HISHIFT 5
88 u64 aarch64_insn_decode_immediate(enum aarch64_insn_imm_type type, u32 insn)
90 u32 immlo, immhi, mask;
94 case AARCH64_INSN_IMM_ADR:
96 immlo = (insn >> ADR_IMM_LOSHIFT) & ADR_IMM_LOMASK;
97 immhi = (insn >> ADR_IMM_HISHIFT) & ADR_IMM_HIMASK;
98 insn = (immhi << ADR_IMM_HILOSPLIT) | immlo;
99 mask = ADR_IMM_SIZE - 1;
102 if (aarch64_get_imm_shift_mask(type, &mask, &shift) < 0) {
103 pr_err("%s: unknown immediate encoding %d\n", __func__,
109 return (insn >> shift) & mask;
112 u32 __kprobes aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type,
115 u32 immlo, immhi, mask;
118 if (insn == AARCH64_BREAK_FAULT)
119 return AARCH64_BREAK_FAULT;
122 case AARCH64_INSN_IMM_ADR:
124 immlo = (imm & ADR_IMM_LOMASK) << ADR_IMM_LOSHIFT;
125 imm >>= ADR_IMM_HILOSPLIT;
126 immhi = (imm & ADR_IMM_HIMASK) << ADR_IMM_HISHIFT;
128 mask = ((ADR_IMM_LOMASK << ADR_IMM_LOSHIFT) |
129 (ADR_IMM_HIMASK << ADR_IMM_HISHIFT));
132 if (aarch64_get_imm_shift_mask(type, &mask, &shift) < 0) {
133 pr_err("%s: unknown immediate encoding %d\n", __func__,
135 return AARCH64_BREAK_FAULT;
139 /* Update the immediate field. */
140 insn &= ~(mask << shift);
141 insn |= (imm & mask) << shift;
146 u32 aarch64_insn_decode_register(enum aarch64_insn_register_type type,
152 case AARCH64_INSN_REGTYPE_RT:
153 case AARCH64_INSN_REGTYPE_RD:
156 case AARCH64_INSN_REGTYPE_RN:
159 case AARCH64_INSN_REGTYPE_RT2:
160 case AARCH64_INSN_REGTYPE_RA:
163 case AARCH64_INSN_REGTYPE_RM:
167 pr_err("%s: unknown register type encoding %d\n", __func__,
172 return (insn >> shift) & GENMASK(4, 0);
175 static u32 aarch64_insn_encode_register(enum aarch64_insn_register_type type,
177 enum aarch64_insn_register reg)
181 if (insn == AARCH64_BREAK_FAULT)
182 return AARCH64_BREAK_FAULT;
184 if (reg < AARCH64_INSN_REG_0 || reg > AARCH64_INSN_REG_SP) {
185 pr_err("%s: unknown register encoding %d\n", __func__, reg);
186 return AARCH64_BREAK_FAULT;
190 case AARCH64_INSN_REGTYPE_RT:
191 case AARCH64_INSN_REGTYPE_RD:
194 case AARCH64_INSN_REGTYPE_RN:
197 case AARCH64_INSN_REGTYPE_RT2:
198 case AARCH64_INSN_REGTYPE_RA:
201 case AARCH64_INSN_REGTYPE_RM:
202 case AARCH64_INSN_REGTYPE_RS:
206 pr_err("%s: unknown register type encoding %d\n", __func__,
208 return AARCH64_BREAK_FAULT;
211 insn &= ~(GENMASK(4, 0) << shift);
212 insn |= reg << shift;
217 static const u32 aarch64_insn_ldst_size[] = {
218 [AARCH64_INSN_SIZE_8] = 0,
219 [AARCH64_INSN_SIZE_16] = 1,
220 [AARCH64_INSN_SIZE_32] = 2,
221 [AARCH64_INSN_SIZE_64] = 3,
224 static u32 aarch64_insn_encode_ldst_size(enum aarch64_insn_size_type type,
229 if (type < AARCH64_INSN_SIZE_8 || type > AARCH64_INSN_SIZE_64) {
230 pr_err("%s: unknown size encoding %d\n", __func__, type);
231 return AARCH64_BREAK_FAULT;
234 size = aarch64_insn_ldst_size[type];
235 insn &= ~GENMASK(31, 30);
241 static inline long label_imm_common(unsigned long pc, unsigned long addr,
246 if ((pc & 0x3) || (addr & 0x3)) {
247 pr_err("%s: A64 instructions must be word aligned\n", __func__);
251 offset = ((long)addr - (long)pc);
253 if (offset < -range || offset >= range) {
254 pr_err("%s: offset out of range\n", __func__);
261 u32 __kprobes aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr,
262 enum aarch64_insn_branch_type type)
268 * B/BL support [-128M, 128M) offset
269 * ARM64 virtual address arrangement guarantees all kernel and module
270 * texts are within +/-128M.
272 offset = label_imm_common(pc, addr, SZ_128M);
273 if (offset >= SZ_128M)
274 return AARCH64_BREAK_FAULT;
277 case AARCH64_INSN_BRANCH_LINK:
278 insn = aarch64_insn_get_bl_value();
280 case AARCH64_INSN_BRANCH_NOLINK:
281 insn = aarch64_insn_get_b_value();
284 pr_err("%s: unknown branch encoding %d\n", __func__, type);
285 return AARCH64_BREAK_FAULT;
288 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_26, insn,
292 u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr,
293 enum aarch64_insn_register reg,
294 enum aarch64_insn_variant variant,
295 enum aarch64_insn_branch_type type)
300 offset = label_imm_common(pc, addr, SZ_1M);
302 return AARCH64_BREAK_FAULT;
305 case AARCH64_INSN_BRANCH_COMP_ZERO:
306 insn = aarch64_insn_get_cbz_value();
308 case AARCH64_INSN_BRANCH_COMP_NONZERO:
309 insn = aarch64_insn_get_cbnz_value();
312 pr_err("%s: unknown branch encoding %d\n", __func__, type);
313 return AARCH64_BREAK_FAULT;
317 case AARCH64_INSN_VARIANT_32BIT:
319 case AARCH64_INSN_VARIANT_64BIT:
320 insn |= AARCH64_INSN_SF_BIT;
323 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
324 return AARCH64_BREAK_FAULT;
327 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
329 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
333 u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
334 enum aarch64_insn_condition cond)
339 offset = label_imm_common(pc, addr, SZ_1M);
341 insn = aarch64_insn_get_bcond_value();
343 if (cond < AARCH64_INSN_COND_EQ || cond > AARCH64_INSN_COND_AL) {
344 pr_err("%s: unknown condition encoding %d\n", __func__, cond);
345 return AARCH64_BREAK_FAULT;
349 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
353 u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg,
354 enum aarch64_insn_branch_type type)
359 case AARCH64_INSN_BRANCH_NOLINK:
360 insn = aarch64_insn_get_br_value();
362 case AARCH64_INSN_BRANCH_LINK:
363 insn = aarch64_insn_get_blr_value();
365 case AARCH64_INSN_BRANCH_RETURN:
366 insn = aarch64_insn_get_ret_value();
369 pr_err("%s: unknown branch encoding %d\n", __func__, type);
370 return AARCH64_BREAK_FAULT;
373 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, reg);
376 u32 aarch64_insn_gen_load_store_reg(enum aarch64_insn_register reg,
377 enum aarch64_insn_register base,
378 enum aarch64_insn_register offset,
379 enum aarch64_insn_size_type size,
380 enum aarch64_insn_ldst_type type)
385 case AARCH64_INSN_LDST_LOAD_REG_OFFSET:
386 insn = aarch64_insn_get_ldr_reg_value();
388 case AARCH64_INSN_LDST_SIGNED_LOAD_REG_OFFSET:
389 insn = aarch64_insn_get_signed_ldr_reg_value();
391 case AARCH64_INSN_LDST_STORE_REG_OFFSET:
392 insn = aarch64_insn_get_str_reg_value();
395 pr_err("%s: unknown load/store encoding %d\n", __func__, type);
396 return AARCH64_BREAK_FAULT;
399 insn = aarch64_insn_encode_ldst_size(size, insn);
401 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
403 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
406 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn,
410 u32 aarch64_insn_gen_load_store_imm(enum aarch64_insn_register reg,
411 enum aarch64_insn_register base,
413 enum aarch64_insn_size_type size,
414 enum aarch64_insn_ldst_type type)
419 if (size < AARCH64_INSN_SIZE_8 || size > AARCH64_INSN_SIZE_64) {
420 pr_err("%s: unknown size encoding %d\n", __func__, type);
421 return AARCH64_BREAK_FAULT;
424 shift = aarch64_insn_ldst_size[size];
425 if (imm & ~(BIT(12 + shift) - BIT(shift))) {
426 pr_err("%s: invalid imm: %d\n", __func__, imm);
427 return AARCH64_BREAK_FAULT;
433 case AARCH64_INSN_LDST_LOAD_IMM_OFFSET:
434 insn = aarch64_insn_get_ldr_imm_value();
436 case AARCH64_INSN_LDST_SIGNED_LOAD_IMM_OFFSET:
437 insn = aarch64_insn_get_signed_load_imm_value();
439 case AARCH64_INSN_LDST_STORE_IMM_OFFSET:
440 insn = aarch64_insn_get_str_imm_value();
443 pr_err("%s: unknown load/store encoding %d\n", __func__, type);
444 return AARCH64_BREAK_FAULT;
447 insn = aarch64_insn_encode_ldst_size(size, insn);
449 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
451 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
454 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_12, insn, imm);
457 u32 aarch64_insn_gen_load_literal(unsigned long pc, unsigned long addr,
458 enum aarch64_insn_register reg,
464 offset = label_imm_common(pc, addr, SZ_1M);
466 return AARCH64_BREAK_FAULT;
468 insn = aarch64_insn_get_ldr_lit_value();
473 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn, reg);
475 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
479 u32 aarch64_insn_gen_load_store_pair(enum aarch64_insn_register reg1,
480 enum aarch64_insn_register reg2,
481 enum aarch64_insn_register base,
483 enum aarch64_insn_variant variant,
484 enum aarch64_insn_ldst_type type)
490 case AARCH64_INSN_LDST_LOAD_PAIR_PRE_INDEX:
491 insn = aarch64_insn_get_ldp_pre_value();
493 case AARCH64_INSN_LDST_STORE_PAIR_PRE_INDEX:
494 insn = aarch64_insn_get_stp_pre_value();
496 case AARCH64_INSN_LDST_LOAD_PAIR_POST_INDEX:
497 insn = aarch64_insn_get_ldp_post_value();
499 case AARCH64_INSN_LDST_STORE_PAIR_POST_INDEX:
500 insn = aarch64_insn_get_stp_post_value();
503 pr_err("%s: unknown load/store encoding %d\n", __func__, type);
504 return AARCH64_BREAK_FAULT;
508 case AARCH64_INSN_VARIANT_32BIT:
509 if ((offset & 0x3) || (offset < -256) || (offset > 252)) {
510 pr_err("%s: offset must be multiples of 4 in the range of [-256, 252] %d\n",
512 return AARCH64_BREAK_FAULT;
516 case AARCH64_INSN_VARIANT_64BIT:
517 if ((offset & 0x7) || (offset < -512) || (offset > 504)) {
518 pr_err("%s: offset must be multiples of 8 in the range of [-512, 504] %d\n",
520 return AARCH64_BREAK_FAULT;
523 insn |= AARCH64_INSN_SF_BIT;
526 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
527 return AARCH64_BREAK_FAULT;
530 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn,
533 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT2, insn,
536 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
539 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_7, insn,
543 u32 aarch64_insn_gen_load_store_ex(enum aarch64_insn_register reg,
544 enum aarch64_insn_register base,
545 enum aarch64_insn_register state,
546 enum aarch64_insn_size_type size,
547 enum aarch64_insn_ldst_type type)
552 case AARCH64_INSN_LDST_LOAD_EX:
553 case AARCH64_INSN_LDST_LOAD_ACQ_EX:
554 insn = aarch64_insn_get_load_ex_value();
555 if (type == AARCH64_INSN_LDST_LOAD_ACQ_EX)
558 case AARCH64_INSN_LDST_STORE_EX:
559 case AARCH64_INSN_LDST_STORE_REL_EX:
560 insn = aarch64_insn_get_store_ex_value();
561 if (type == AARCH64_INSN_LDST_STORE_REL_EX)
565 pr_err("%s: unknown load/store exclusive encoding %d\n", __func__, type);
566 return AARCH64_BREAK_FAULT;
569 insn = aarch64_insn_encode_ldst_size(size, insn);
571 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn,
574 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
577 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT2, insn,
578 AARCH64_INSN_REG_ZR);
580 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RS, insn,
584 #ifdef CONFIG_ARM64_LSE_ATOMICS
585 static u32 aarch64_insn_encode_ldst_order(enum aarch64_insn_mem_order_type type,
591 case AARCH64_INSN_MEM_ORDER_NONE:
594 case AARCH64_INSN_MEM_ORDER_ACQ:
597 case AARCH64_INSN_MEM_ORDER_REL:
600 case AARCH64_INSN_MEM_ORDER_ACQREL:
604 pr_err("%s: unknown mem order %d\n", __func__, type);
605 return AARCH64_BREAK_FAULT;
608 insn &= ~GENMASK(23, 22);
614 u32 aarch64_insn_gen_atomic_ld_op(enum aarch64_insn_register result,
615 enum aarch64_insn_register address,
616 enum aarch64_insn_register value,
617 enum aarch64_insn_size_type size,
618 enum aarch64_insn_mem_atomic_op op,
619 enum aarch64_insn_mem_order_type order)
624 case AARCH64_INSN_MEM_ATOMIC_ADD:
625 insn = aarch64_insn_get_ldadd_value();
627 case AARCH64_INSN_MEM_ATOMIC_CLR:
628 insn = aarch64_insn_get_ldclr_value();
630 case AARCH64_INSN_MEM_ATOMIC_EOR:
631 insn = aarch64_insn_get_ldeor_value();
633 case AARCH64_INSN_MEM_ATOMIC_SET:
634 insn = aarch64_insn_get_ldset_value();
636 case AARCH64_INSN_MEM_ATOMIC_SWP:
637 insn = aarch64_insn_get_swp_value();
640 pr_err("%s: unimplemented mem atomic op %d\n", __func__, op);
641 return AARCH64_BREAK_FAULT;
645 case AARCH64_INSN_SIZE_32:
646 case AARCH64_INSN_SIZE_64:
649 pr_err("%s: unimplemented size encoding %d\n", __func__, size);
650 return AARCH64_BREAK_FAULT;
653 insn = aarch64_insn_encode_ldst_size(size, insn);
655 insn = aarch64_insn_encode_ldst_order(order, insn);
657 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn,
660 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
663 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RS, insn,
667 static u32 aarch64_insn_encode_cas_order(enum aarch64_insn_mem_order_type type,
673 case AARCH64_INSN_MEM_ORDER_NONE:
676 case AARCH64_INSN_MEM_ORDER_ACQ:
679 case AARCH64_INSN_MEM_ORDER_REL:
682 case AARCH64_INSN_MEM_ORDER_ACQREL:
683 order = BIT(15) | BIT(22);
686 pr_err("%s: unknown mem order %d\n", __func__, type);
687 return AARCH64_BREAK_FAULT;
690 insn &= ~(BIT(15) | BIT(22));
696 u32 aarch64_insn_gen_cas(enum aarch64_insn_register result,
697 enum aarch64_insn_register address,
698 enum aarch64_insn_register value,
699 enum aarch64_insn_size_type size,
700 enum aarch64_insn_mem_order_type order)
705 case AARCH64_INSN_SIZE_32:
706 case AARCH64_INSN_SIZE_64:
709 pr_err("%s: unimplemented size encoding %d\n", __func__, size);
710 return AARCH64_BREAK_FAULT;
713 insn = aarch64_insn_get_cas_value();
715 insn = aarch64_insn_encode_ldst_size(size, insn);
717 insn = aarch64_insn_encode_cas_order(order, insn);
719 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT, insn,
722 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
725 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RS, insn,
730 u32 aarch64_insn_gen_add_sub_imm(enum aarch64_insn_register dst,
731 enum aarch64_insn_register src,
732 int imm, enum aarch64_insn_variant variant,
733 enum aarch64_insn_adsb_type type)
738 case AARCH64_INSN_ADSB_ADD:
739 insn = aarch64_insn_get_add_imm_value();
741 case AARCH64_INSN_ADSB_SUB:
742 insn = aarch64_insn_get_sub_imm_value();
744 case AARCH64_INSN_ADSB_ADD_SETFLAGS:
745 insn = aarch64_insn_get_adds_imm_value();
747 case AARCH64_INSN_ADSB_SUB_SETFLAGS:
748 insn = aarch64_insn_get_subs_imm_value();
751 pr_err("%s: unknown add/sub encoding %d\n", __func__, type);
752 return AARCH64_BREAK_FAULT;
756 case AARCH64_INSN_VARIANT_32BIT:
758 case AARCH64_INSN_VARIANT_64BIT:
759 insn |= AARCH64_INSN_SF_BIT;
762 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
763 return AARCH64_BREAK_FAULT;
766 /* We can't encode more than a 24bit value (12bit + 12bit shift) */
767 if (imm & ~(BIT(24) - 1))
770 /* If we have something in the top 12 bits... */
771 if (imm & ~(SZ_4K - 1)) {
772 /* ... and in the low 12 bits -> error */
773 if (imm & (SZ_4K - 1))
777 insn |= AARCH64_INSN_LSL_12;
780 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
782 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
784 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_12, insn, imm);
787 pr_err("%s: invalid immediate encoding %d\n", __func__, imm);
788 return AARCH64_BREAK_FAULT;
791 u32 aarch64_insn_gen_bitfield(enum aarch64_insn_register dst,
792 enum aarch64_insn_register src,
794 enum aarch64_insn_variant variant,
795 enum aarch64_insn_bitfield_type type)
801 case AARCH64_INSN_BITFIELD_MOVE:
802 insn = aarch64_insn_get_bfm_value();
804 case AARCH64_INSN_BITFIELD_MOVE_UNSIGNED:
805 insn = aarch64_insn_get_ubfm_value();
807 case AARCH64_INSN_BITFIELD_MOVE_SIGNED:
808 insn = aarch64_insn_get_sbfm_value();
811 pr_err("%s: unknown bitfield encoding %d\n", __func__, type);
812 return AARCH64_BREAK_FAULT;
816 case AARCH64_INSN_VARIANT_32BIT:
817 mask = GENMASK(4, 0);
819 case AARCH64_INSN_VARIANT_64BIT:
820 insn |= AARCH64_INSN_SF_BIT | AARCH64_INSN_N_BIT;
821 mask = GENMASK(5, 0);
824 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
825 return AARCH64_BREAK_FAULT;
829 pr_err("%s: invalid immr encoding %d\n", __func__, immr);
830 return AARCH64_BREAK_FAULT;
833 pr_err("%s: invalid imms encoding %d\n", __func__, imms);
834 return AARCH64_BREAK_FAULT;
837 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
839 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
841 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_R, insn, immr);
843 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_S, insn, imms);
846 u32 aarch64_insn_gen_movewide(enum aarch64_insn_register dst,
848 enum aarch64_insn_variant variant,
849 enum aarch64_insn_movewide_type type)
854 case AARCH64_INSN_MOVEWIDE_ZERO:
855 insn = aarch64_insn_get_movz_value();
857 case AARCH64_INSN_MOVEWIDE_KEEP:
858 insn = aarch64_insn_get_movk_value();
860 case AARCH64_INSN_MOVEWIDE_INVERSE:
861 insn = aarch64_insn_get_movn_value();
864 pr_err("%s: unknown movewide encoding %d\n", __func__, type);
865 return AARCH64_BREAK_FAULT;
868 if (imm & ~(SZ_64K - 1)) {
869 pr_err("%s: invalid immediate encoding %d\n", __func__, imm);
870 return AARCH64_BREAK_FAULT;
874 case AARCH64_INSN_VARIANT_32BIT:
875 if (shift != 0 && shift != 16) {
876 pr_err("%s: invalid shift encoding %d\n", __func__,
878 return AARCH64_BREAK_FAULT;
881 case AARCH64_INSN_VARIANT_64BIT:
882 insn |= AARCH64_INSN_SF_BIT;
883 if (shift != 0 && shift != 16 && shift != 32 && shift != 48) {
884 pr_err("%s: invalid shift encoding %d\n", __func__,
886 return AARCH64_BREAK_FAULT;
890 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
891 return AARCH64_BREAK_FAULT;
894 insn |= (shift >> 4) << 21;
896 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
898 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
901 u32 aarch64_insn_gen_add_sub_shifted_reg(enum aarch64_insn_register dst,
902 enum aarch64_insn_register src,
903 enum aarch64_insn_register reg,
905 enum aarch64_insn_variant variant,
906 enum aarch64_insn_adsb_type type)
911 case AARCH64_INSN_ADSB_ADD:
912 insn = aarch64_insn_get_add_value();
914 case AARCH64_INSN_ADSB_SUB:
915 insn = aarch64_insn_get_sub_value();
917 case AARCH64_INSN_ADSB_ADD_SETFLAGS:
918 insn = aarch64_insn_get_adds_value();
920 case AARCH64_INSN_ADSB_SUB_SETFLAGS:
921 insn = aarch64_insn_get_subs_value();
924 pr_err("%s: unknown add/sub encoding %d\n", __func__, type);
925 return AARCH64_BREAK_FAULT;
929 case AARCH64_INSN_VARIANT_32BIT:
930 if (shift & ~(SZ_32 - 1)) {
931 pr_err("%s: invalid shift encoding %d\n", __func__,
933 return AARCH64_BREAK_FAULT;
936 case AARCH64_INSN_VARIANT_64BIT:
937 insn |= AARCH64_INSN_SF_BIT;
938 if (shift & ~(SZ_64 - 1)) {
939 pr_err("%s: invalid shift encoding %d\n", __func__,
941 return AARCH64_BREAK_FAULT;
945 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
946 return AARCH64_BREAK_FAULT;
950 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
952 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
954 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
956 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
959 u32 aarch64_insn_gen_data1(enum aarch64_insn_register dst,
960 enum aarch64_insn_register src,
961 enum aarch64_insn_variant variant,
962 enum aarch64_insn_data1_type type)
967 case AARCH64_INSN_DATA1_REVERSE_16:
968 insn = aarch64_insn_get_rev16_value();
970 case AARCH64_INSN_DATA1_REVERSE_32:
971 insn = aarch64_insn_get_rev32_value();
973 case AARCH64_INSN_DATA1_REVERSE_64:
974 if (variant != AARCH64_INSN_VARIANT_64BIT) {
975 pr_err("%s: invalid variant for reverse64 %d\n",
977 return AARCH64_BREAK_FAULT;
979 insn = aarch64_insn_get_rev64_value();
982 pr_err("%s: unknown data1 encoding %d\n", __func__, type);
983 return AARCH64_BREAK_FAULT;
987 case AARCH64_INSN_VARIANT_32BIT:
989 case AARCH64_INSN_VARIANT_64BIT:
990 insn |= AARCH64_INSN_SF_BIT;
993 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
994 return AARCH64_BREAK_FAULT;
997 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
999 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
1002 u32 aarch64_insn_gen_data2(enum aarch64_insn_register dst,
1003 enum aarch64_insn_register src,
1004 enum aarch64_insn_register reg,
1005 enum aarch64_insn_variant variant,
1006 enum aarch64_insn_data2_type type)
1011 case AARCH64_INSN_DATA2_UDIV:
1012 insn = aarch64_insn_get_udiv_value();
1014 case AARCH64_INSN_DATA2_SDIV:
1015 insn = aarch64_insn_get_sdiv_value();
1017 case AARCH64_INSN_DATA2_LSLV:
1018 insn = aarch64_insn_get_lslv_value();
1020 case AARCH64_INSN_DATA2_LSRV:
1021 insn = aarch64_insn_get_lsrv_value();
1023 case AARCH64_INSN_DATA2_ASRV:
1024 insn = aarch64_insn_get_asrv_value();
1026 case AARCH64_INSN_DATA2_RORV:
1027 insn = aarch64_insn_get_rorv_value();
1030 pr_err("%s: unknown data2 encoding %d\n", __func__, type);
1031 return AARCH64_BREAK_FAULT;
1035 case AARCH64_INSN_VARIANT_32BIT:
1037 case AARCH64_INSN_VARIANT_64BIT:
1038 insn |= AARCH64_INSN_SF_BIT;
1041 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
1042 return AARCH64_BREAK_FAULT;
1045 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
1047 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
1049 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
1052 u32 aarch64_insn_gen_data3(enum aarch64_insn_register dst,
1053 enum aarch64_insn_register src,
1054 enum aarch64_insn_register reg1,
1055 enum aarch64_insn_register reg2,
1056 enum aarch64_insn_variant variant,
1057 enum aarch64_insn_data3_type type)
1062 case AARCH64_INSN_DATA3_MADD:
1063 insn = aarch64_insn_get_madd_value();
1065 case AARCH64_INSN_DATA3_MSUB:
1066 insn = aarch64_insn_get_msub_value();
1069 pr_err("%s: unknown data3 encoding %d\n", __func__, type);
1070 return AARCH64_BREAK_FAULT;
1074 case AARCH64_INSN_VARIANT_32BIT:
1076 case AARCH64_INSN_VARIANT_64BIT:
1077 insn |= AARCH64_INSN_SF_BIT;
1080 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
1081 return AARCH64_BREAK_FAULT;
1084 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
1086 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RA, insn, src);
1088 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn,
1091 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn,
1095 u32 aarch64_insn_gen_logical_shifted_reg(enum aarch64_insn_register dst,
1096 enum aarch64_insn_register src,
1097 enum aarch64_insn_register reg,
1099 enum aarch64_insn_variant variant,
1100 enum aarch64_insn_logic_type type)
1105 case AARCH64_INSN_LOGIC_AND:
1106 insn = aarch64_insn_get_and_value();
1108 case AARCH64_INSN_LOGIC_BIC:
1109 insn = aarch64_insn_get_bic_value();
1111 case AARCH64_INSN_LOGIC_ORR:
1112 insn = aarch64_insn_get_orr_value();
1114 case AARCH64_INSN_LOGIC_ORN:
1115 insn = aarch64_insn_get_orn_value();
1117 case AARCH64_INSN_LOGIC_EOR:
1118 insn = aarch64_insn_get_eor_value();
1120 case AARCH64_INSN_LOGIC_EON:
1121 insn = aarch64_insn_get_eon_value();
1123 case AARCH64_INSN_LOGIC_AND_SETFLAGS:
1124 insn = aarch64_insn_get_ands_value();
1126 case AARCH64_INSN_LOGIC_BIC_SETFLAGS:
1127 insn = aarch64_insn_get_bics_value();
1130 pr_err("%s: unknown logical encoding %d\n", __func__, type);
1131 return AARCH64_BREAK_FAULT;
1135 case AARCH64_INSN_VARIANT_32BIT:
1136 if (shift & ~(SZ_32 - 1)) {
1137 pr_err("%s: invalid shift encoding %d\n", __func__,
1139 return AARCH64_BREAK_FAULT;
1142 case AARCH64_INSN_VARIANT_64BIT:
1143 insn |= AARCH64_INSN_SF_BIT;
1144 if (shift & ~(SZ_64 - 1)) {
1145 pr_err("%s: invalid shift encoding %d\n", __func__,
1147 return AARCH64_BREAK_FAULT;
1151 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
1152 return AARCH64_BREAK_FAULT;
1156 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, dst);
1158 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, src);
1160 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, reg);
1162 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_6, insn, shift);
1166 * MOV (register) is architecturally an alias of ORR (shifted register) where
1167 * MOV <*d>, <*m> is equivalent to ORR <*d>, <*ZR>, <*m>
1169 u32 aarch64_insn_gen_move_reg(enum aarch64_insn_register dst,
1170 enum aarch64_insn_register src,
1171 enum aarch64_insn_variant variant)
1173 return aarch64_insn_gen_logical_shifted_reg(dst, AARCH64_INSN_REG_ZR,
1175 AARCH64_INSN_LOGIC_ORR);
1178 u32 aarch64_insn_gen_adr(unsigned long pc, unsigned long addr,
1179 enum aarch64_insn_register reg,
1180 enum aarch64_insn_adr_type type)
1186 case AARCH64_INSN_ADR_TYPE_ADR:
1187 insn = aarch64_insn_get_adr_value();
1190 case AARCH64_INSN_ADR_TYPE_ADRP:
1191 insn = aarch64_insn_get_adrp_value();
1192 offset = (addr - ALIGN_DOWN(pc, SZ_4K)) >> 12;
1195 pr_err("%s: unknown adr encoding %d\n", __func__, type);
1196 return AARCH64_BREAK_FAULT;
1199 if (offset < -SZ_1M || offset >= SZ_1M)
1200 return AARCH64_BREAK_FAULT;
1202 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, reg);
1204 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_ADR, insn, offset);
1208 * Decode the imm field of a branch, and return the byte offset as a
1209 * signed value (so it can be used when computing a new branch
1212 s32 aarch64_get_branch_offset(u32 insn)
1216 if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn)) {
1217 imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_26, insn);
1218 return (imm << 6) >> 4;
1221 if (aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) ||
1222 aarch64_insn_is_bcond(insn)) {
1223 imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_19, insn);
1224 return (imm << 13) >> 11;
1227 if (aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn)) {
1228 imm = aarch64_insn_decode_immediate(AARCH64_INSN_IMM_14, insn);
1229 return (imm << 18) >> 16;
1232 /* Unhandled instruction */
1237 * Encode the displacement of a branch in the imm field and return the
1238 * updated instruction.
1240 u32 aarch64_set_branch_offset(u32 insn, s32 offset)
1242 if (aarch64_insn_is_b(insn) || aarch64_insn_is_bl(insn))
1243 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_26, insn,
1246 if (aarch64_insn_is_cbz(insn) || aarch64_insn_is_cbnz(insn) ||
1247 aarch64_insn_is_bcond(insn))
1248 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_19, insn,
1251 if (aarch64_insn_is_tbz(insn) || aarch64_insn_is_tbnz(insn))
1252 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_14, insn,
1255 /* Unhandled instruction */
1259 s32 aarch64_insn_adrp_get_offset(u32 insn)
1261 BUG_ON(!aarch64_insn_is_adrp(insn));
1262 return aarch64_insn_decode_immediate(AARCH64_INSN_IMM_ADR, insn) << 12;
1265 u32 aarch64_insn_adrp_set_offset(u32 insn, s32 offset)
1267 BUG_ON(!aarch64_insn_is_adrp(insn));
1268 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_ADR, insn,
1273 * Extract the Op/CR data from a msr/mrs instruction.
1275 u32 aarch64_insn_extract_system_reg(u32 insn)
1277 return (insn & 0x1FFFE0) >> 5;
1280 bool aarch32_insn_is_wide(u32 insn)
1282 return insn >= 0xe800;
1286 * Macros/defines for extracting register numbers from instruction.
1288 u32 aarch32_insn_extract_reg_num(u32 insn, int offset)
1290 return (insn & (0xf << offset)) >> offset;
1293 #define OPC2_MASK 0x7
1294 #define OPC2_OFFSET 5
1295 u32 aarch32_insn_mcr_extract_opc2(u32 insn)
1297 return (insn & (OPC2_MASK << OPC2_OFFSET)) >> OPC2_OFFSET;
1300 #define CRM_MASK 0xf
1301 u32 aarch32_insn_mcr_extract_crm(u32 insn)
1303 return insn & CRM_MASK;
1306 static bool range_of_ones(u64 val)
1308 /* Doesn't handle full ones or full zeroes */
1309 u64 sval = val >> __ffs64(val);
1311 /* One of Sean Eron Anderson's bithack tricks */
1312 return ((sval + 1) & (sval)) == 0;
1315 static u32 aarch64_encode_immediate(u64 imm,
1316 enum aarch64_insn_variant variant,
1319 unsigned int immr, imms, n, ones, ror, esz, tmp;
1323 case AARCH64_INSN_VARIANT_32BIT:
1326 case AARCH64_INSN_VARIANT_64BIT:
1327 insn |= AARCH64_INSN_SF_BIT;
1331 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
1332 return AARCH64_BREAK_FAULT;
1335 mask = GENMASK(esz - 1, 0);
1337 /* Can't encode full zeroes, full ones, or value wider than the mask */
1338 if (!imm || imm == mask || imm & ~mask)
1339 return AARCH64_BREAK_FAULT;
1342 * Inverse of Replicate(). Try to spot a repeating pattern
1343 * with a pow2 stride.
1345 for (tmp = esz / 2; tmp >= 2; tmp /= 2) {
1346 u64 emask = BIT(tmp) - 1;
1348 if ((imm & emask) != ((imm >> tmp) & emask))
1355 /* N is only set if we're encoding a 64bit value */
1358 /* Trim imm to the element size */
1361 /* That's how many ones we need to encode */
1362 ones = hweight64(imm);
1365 * imms is set to (ones - 1), prefixed with a string of ones
1366 * and a zero if they fit. Cap it to 6 bits.
1369 imms |= 0xf << ffs(esz);
1372 /* Compute the rotation */
1373 if (range_of_ones(imm)) {
1375 * Pattern: 0..01..10..0
1377 * Compute how many rotate we need to align it right
1382 * Pattern: 0..01..10..01..1
1384 * Fill the unused top bits with ones, and check if
1385 * the result is a valid immediate (all ones with a
1386 * contiguous ranges of zeroes).
1389 if (!range_of_ones(~imm))
1390 return AARCH64_BREAK_FAULT;
1393 * Compute the rotation to get a continuous set of
1394 * ones, with the first bit set at position 0
1400 * immr is the number of bits we need to rotate back to the
1401 * original set of ones. Note that this is relative to the
1404 immr = (esz - ror) % esz;
1406 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_N, insn, n);
1407 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_R, insn, immr);
1408 return aarch64_insn_encode_immediate(AARCH64_INSN_IMM_S, insn, imms);
1411 u32 aarch64_insn_gen_logical_immediate(enum aarch64_insn_logic_type type,
1412 enum aarch64_insn_variant variant,
1413 enum aarch64_insn_register Rn,
1414 enum aarch64_insn_register Rd,
1420 case AARCH64_INSN_LOGIC_AND:
1421 insn = aarch64_insn_get_and_imm_value();
1423 case AARCH64_INSN_LOGIC_ORR:
1424 insn = aarch64_insn_get_orr_imm_value();
1426 case AARCH64_INSN_LOGIC_EOR:
1427 insn = aarch64_insn_get_eor_imm_value();
1429 case AARCH64_INSN_LOGIC_AND_SETFLAGS:
1430 insn = aarch64_insn_get_ands_imm_value();
1433 pr_err("%s: unknown logical encoding %d\n", __func__, type);
1434 return AARCH64_BREAK_FAULT;
1437 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, Rd);
1438 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, Rn);
1439 return aarch64_encode_immediate(imm, variant, insn);
1442 u32 aarch64_insn_gen_extr(enum aarch64_insn_variant variant,
1443 enum aarch64_insn_register Rm,
1444 enum aarch64_insn_register Rn,
1445 enum aarch64_insn_register Rd,
1450 insn = aarch64_insn_get_extr_value();
1453 case AARCH64_INSN_VARIANT_32BIT:
1455 return AARCH64_BREAK_FAULT;
1457 case AARCH64_INSN_VARIANT_64BIT:
1459 return AARCH64_BREAK_FAULT;
1460 insn |= AARCH64_INSN_SF_BIT;
1461 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_N, insn, 1);
1464 pr_err("%s: unknown variant encoding %d\n", __func__, variant);
1465 return AARCH64_BREAK_FAULT;
1468 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_S, insn, lsb);
1469 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RD, insn, Rd);
1470 insn = aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RN, insn, Rn);
1471 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RM, insn, Rm);
1474 u32 aarch64_insn_gen_dmb(enum aarch64_insn_mb_type type)
1480 case AARCH64_INSN_MB_SY:
1483 case AARCH64_INSN_MB_ST:
1486 case AARCH64_INSN_MB_LD:
1489 case AARCH64_INSN_MB_ISH:
1492 case AARCH64_INSN_MB_ISHST:
1495 case AARCH64_INSN_MB_ISHLD:
1498 case AARCH64_INSN_MB_NSH:
1501 case AARCH64_INSN_MB_NSHST:
1504 case AARCH64_INSN_MB_NSHLD:
1508 pr_err("%s: unknown dmb type %d\n", __func__, type);
1509 return AARCH64_BREAK_FAULT;
1512 insn = aarch64_insn_get_dmb_value();
1513 insn &= ~GENMASK(11, 8);
1519 u32 aarch64_insn_gen_mrs(enum aarch64_insn_register result,
1520 enum aarch64_insn_system_register sysreg)
1522 u32 insn = aarch64_insn_get_mrs_value();
1524 insn &= ~GENMASK(19, 0);
1525 insn |= sysreg << 5;
1526 return aarch64_insn_encode_register(AARCH64_INSN_REGTYPE_RT,