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 #ifndef __ASM_INSN_H 20 #define __ASM_INSN_H 21 #include <linux/types.h> 22 23 /* A64 instructions are always 32 bits. */ 24 #define AARCH64_INSN_SIZE 4 25 26 #ifndef __ASSEMBLY__ 27 /* 28 * ARM Architecture Reference Manual for ARMv8 Profile-A, Issue A.a 29 * Section C3.1 "A64 instruction index by encoding": 30 * AArch64 main encoding table 31 * Bit position 32 * 28 27 26 25 Encoding Group 33 * 0 0 - - Unallocated 34 * 1 0 0 - Data processing, immediate 35 * 1 0 1 - Branch, exception generation and system instructions 36 * - 1 - 0 Loads and stores 37 * - 1 0 1 Data processing - register 38 * 0 1 1 1 Data processing - SIMD and floating point 39 * 1 1 1 1 Data processing - SIMD and floating point 40 * "-" means "don't care" 41 */ 42 enum aarch64_insn_encoding_class { 43 AARCH64_INSN_CLS_UNKNOWN, /* UNALLOCATED */ 44 AARCH64_INSN_CLS_DP_IMM, /* Data processing - immediate */ 45 AARCH64_INSN_CLS_DP_REG, /* Data processing - register */ 46 AARCH64_INSN_CLS_DP_FPSIMD, /* Data processing - SIMD and FP */ 47 AARCH64_INSN_CLS_LDST, /* Loads and stores */ 48 AARCH64_INSN_CLS_BR_SYS, /* Branch, exception generation and 49 * system instructions */ 50 }; 51 52 enum aarch64_insn_hint_op { 53 AARCH64_INSN_HINT_NOP = 0x0 << 5, 54 AARCH64_INSN_HINT_YIELD = 0x1 << 5, 55 AARCH64_INSN_HINT_WFE = 0x2 << 5, 56 AARCH64_INSN_HINT_WFI = 0x3 << 5, 57 AARCH64_INSN_HINT_SEV = 0x4 << 5, 58 AARCH64_INSN_HINT_SEVL = 0x5 << 5, 59 }; 60 61 enum aarch64_insn_imm_type { 62 AARCH64_INSN_IMM_ADR, 63 AARCH64_INSN_IMM_26, 64 AARCH64_INSN_IMM_19, 65 AARCH64_INSN_IMM_16, 66 AARCH64_INSN_IMM_14, 67 AARCH64_INSN_IMM_12, 68 AARCH64_INSN_IMM_9, 69 AARCH64_INSN_IMM_MAX 70 }; 71 72 enum aarch64_insn_register_type { 73 AARCH64_INSN_REGTYPE_RT, 74 AARCH64_INSN_REGTYPE_RN, 75 }; 76 77 enum aarch64_insn_register { 78 AARCH64_INSN_REG_0 = 0, 79 AARCH64_INSN_REG_1 = 1, 80 AARCH64_INSN_REG_2 = 2, 81 AARCH64_INSN_REG_3 = 3, 82 AARCH64_INSN_REG_4 = 4, 83 AARCH64_INSN_REG_5 = 5, 84 AARCH64_INSN_REG_6 = 6, 85 AARCH64_INSN_REG_7 = 7, 86 AARCH64_INSN_REG_8 = 8, 87 AARCH64_INSN_REG_9 = 9, 88 AARCH64_INSN_REG_10 = 10, 89 AARCH64_INSN_REG_11 = 11, 90 AARCH64_INSN_REG_12 = 12, 91 AARCH64_INSN_REG_13 = 13, 92 AARCH64_INSN_REG_14 = 14, 93 AARCH64_INSN_REG_15 = 15, 94 AARCH64_INSN_REG_16 = 16, 95 AARCH64_INSN_REG_17 = 17, 96 AARCH64_INSN_REG_18 = 18, 97 AARCH64_INSN_REG_19 = 19, 98 AARCH64_INSN_REG_20 = 20, 99 AARCH64_INSN_REG_21 = 21, 100 AARCH64_INSN_REG_22 = 22, 101 AARCH64_INSN_REG_23 = 23, 102 AARCH64_INSN_REG_24 = 24, 103 AARCH64_INSN_REG_25 = 25, 104 AARCH64_INSN_REG_26 = 26, 105 AARCH64_INSN_REG_27 = 27, 106 AARCH64_INSN_REG_28 = 28, 107 AARCH64_INSN_REG_29 = 29, 108 AARCH64_INSN_REG_FP = 29, /* Frame pointer */ 109 AARCH64_INSN_REG_30 = 30, 110 AARCH64_INSN_REG_LR = 30, /* Link register */ 111 AARCH64_INSN_REG_ZR = 31, /* Zero: as source register */ 112 AARCH64_INSN_REG_SP = 31 /* Stack pointer: as load/store base reg */ 113 }; 114 115 enum aarch64_insn_variant { 116 AARCH64_INSN_VARIANT_32BIT, 117 AARCH64_INSN_VARIANT_64BIT 118 }; 119 120 enum aarch64_insn_condition { 121 AARCH64_INSN_COND_EQ = 0x0, /* == */ 122 AARCH64_INSN_COND_NE = 0x1, /* != */ 123 AARCH64_INSN_COND_CS = 0x2, /* unsigned >= */ 124 AARCH64_INSN_COND_CC = 0x3, /* unsigned < */ 125 AARCH64_INSN_COND_MI = 0x4, /* < 0 */ 126 AARCH64_INSN_COND_PL = 0x5, /* >= 0 */ 127 AARCH64_INSN_COND_VS = 0x6, /* overflow */ 128 AARCH64_INSN_COND_VC = 0x7, /* no overflow */ 129 AARCH64_INSN_COND_HI = 0x8, /* unsigned > */ 130 AARCH64_INSN_COND_LS = 0x9, /* unsigned <= */ 131 AARCH64_INSN_COND_GE = 0xa, /* signed >= */ 132 AARCH64_INSN_COND_LT = 0xb, /* signed < */ 133 AARCH64_INSN_COND_GT = 0xc, /* signed > */ 134 AARCH64_INSN_COND_LE = 0xd, /* signed <= */ 135 AARCH64_INSN_COND_AL = 0xe, /* always */ 136 }; 137 138 enum aarch64_insn_branch_type { 139 AARCH64_INSN_BRANCH_NOLINK, 140 AARCH64_INSN_BRANCH_LINK, 141 AARCH64_INSN_BRANCH_RETURN, 142 AARCH64_INSN_BRANCH_COMP_ZERO, 143 AARCH64_INSN_BRANCH_COMP_NONZERO, 144 }; 145 146 #define __AARCH64_INSN_FUNCS(abbr, mask, val) \ 147 static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ 148 { return (code & (mask)) == (val); } \ 149 static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ 150 { return (val); } 151 152 __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) 153 __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) 154 __AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000) 155 __AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000) 156 __AARCH64_INSN_FUNCS(bcond, 0xFF000010, 0x54000000) 157 __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) 158 __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002) 159 __AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003) 160 __AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000) 161 __AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F) 162 __AARCH64_INSN_FUNCS(br, 0xFFFFFC1F, 0xD61F0000) 163 __AARCH64_INSN_FUNCS(blr, 0xFFFFFC1F, 0xD63F0000) 164 __AARCH64_INSN_FUNCS(ret, 0xFFFFFC1F, 0xD65F0000) 165 166 #undef __AARCH64_INSN_FUNCS 167 168 bool aarch64_insn_is_nop(u32 insn); 169 170 int aarch64_insn_read(void *addr, u32 *insnp); 171 int aarch64_insn_write(void *addr, u32 insn); 172 enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn); 173 u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, 174 u32 insn, u64 imm); 175 u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, 176 enum aarch64_insn_branch_type type); 177 u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr, 178 enum aarch64_insn_register reg, 179 enum aarch64_insn_variant variant, 180 enum aarch64_insn_branch_type type); 181 u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr, 182 enum aarch64_insn_condition cond); 183 u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op); 184 u32 aarch64_insn_gen_nop(void); 185 u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg, 186 enum aarch64_insn_branch_type type); 187 188 bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn); 189 190 int aarch64_insn_patch_text_nosync(void *addr, u32 insn); 191 int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt); 192 int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt); 193 #endif /* __ASSEMBLY__ */ 194 195 #endif /* __ASM_INSN_H */ 196