Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Huawei Ltd. |
| 3 | * Author: Jiang Liu <liuj97@gmail.com> |
| 4 | * |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 5 | * Copyright (C) 2014 Zi Shen Lim <zlim.lnx@gmail.com> |
| 6 | * |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 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 | |
Jiang Liu | ae16480 | 2014-01-07 22:17:09 +0800 | [diff] [blame] | 23 | /* A64 instructions are always 32 bits. */ |
| 24 | #define AARCH64_INSN_SIZE 4 |
| 25 | |
AKASHI Takahiro | 26e9b83 | 2014-04-30 10:54:30 +0100 | [diff] [blame] | 26 | #ifndef __ASSEMBLY__ |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 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 | |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 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 | |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 72 | enum aarch64_insn_register_type { |
| 73 | AARCH64_INSN_REGTYPE_RT, |
Zi Shen Lim | c0cafba | 2014-08-27 05:15:18 +0100 | [diff] [blame^] | 74 | AARCH64_INSN_REGTYPE_RN, |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 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 | |
Jiang Liu | 5c5bf25 | 2014-01-07 22:17:11 +0800 | [diff] [blame] | 120 | enum aarch64_insn_branch_type { |
| 121 | AARCH64_INSN_BRANCH_NOLINK, |
| 122 | AARCH64_INSN_BRANCH_LINK, |
Zi Shen Lim | c0cafba | 2014-08-27 05:15:18 +0100 | [diff] [blame^] | 123 | AARCH64_INSN_BRANCH_RETURN, |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 124 | AARCH64_INSN_BRANCH_COMP_ZERO, |
| 125 | AARCH64_INSN_BRANCH_COMP_NONZERO, |
Jiang Liu | 5c5bf25 | 2014-01-07 22:17:11 +0800 | [diff] [blame] | 126 | }; |
| 127 | |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 128 | #define __AARCH64_INSN_FUNCS(abbr, mask, val) \ |
| 129 | static __always_inline bool aarch64_insn_is_##abbr(u32 code) \ |
| 130 | { return (code & (mask)) == (val); } \ |
| 131 | static __always_inline u32 aarch64_insn_get_##abbr##_value(void) \ |
| 132 | { return (val); } |
| 133 | |
| 134 | __AARCH64_INSN_FUNCS(b, 0xFC000000, 0x14000000) |
| 135 | __AARCH64_INSN_FUNCS(bl, 0xFC000000, 0x94000000) |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 136 | __AARCH64_INSN_FUNCS(cbz, 0xFE000000, 0x34000000) |
| 137 | __AARCH64_INSN_FUNCS(cbnz, 0xFE000000, 0x35000000) |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 138 | __AARCH64_INSN_FUNCS(svc, 0xFFE0001F, 0xD4000001) |
| 139 | __AARCH64_INSN_FUNCS(hvc, 0xFFE0001F, 0xD4000002) |
| 140 | __AARCH64_INSN_FUNCS(smc, 0xFFE0001F, 0xD4000003) |
| 141 | __AARCH64_INSN_FUNCS(brk, 0xFFE0001F, 0xD4200000) |
| 142 | __AARCH64_INSN_FUNCS(hint, 0xFFFFF01F, 0xD503201F) |
Zi Shen Lim | c0cafba | 2014-08-27 05:15:18 +0100 | [diff] [blame^] | 143 | __AARCH64_INSN_FUNCS(br, 0xFFFFFC1F, 0xD61F0000) |
| 144 | __AARCH64_INSN_FUNCS(blr, 0xFFFFFC1F, 0xD63F0000) |
| 145 | __AARCH64_INSN_FUNCS(ret, 0xFFFFFC1F, 0xD65F0000) |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 146 | |
| 147 | #undef __AARCH64_INSN_FUNCS |
| 148 | |
| 149 | bool aarch64_insn_is_nop(u32 insn); |
| 150 | |
Jiang Liu | ae16480 | 2014-01-07 22:17:09 +0800 | [diff] [blame] | 151 | int aarch64_insn_read(void *addr, u32 *insnp); |
| 152 | int aarch64_insn_write(void *addr, u32 insn); |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 153 | enum aarch64_insn_encoding_class aarch64_get_insn_class(u32 insn); |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 154 | u32 aarch64_insn_encode_immediate(enum aarch64_insn_imm_type type, |
| 155 | u32 insn, u64 imm); |
Jiang Liu | 5c5bf25 | 2014-01-07 22:17:11 +0800 | [diff] [blame] | 156 | u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigned long addr, |
| 157 | enum aarch64_insn_branch_type type); |
Zi Shen Lim | 617d2fb | 2014-08-27 05:15:17 +0100 | [diff] [blame] | 158 | u32 aarch64_insn_gen_comp_branch_imm(unsigned long pc, unsigned long addr, |
| 159 | enum aarch64_insn_register reg, |
| 160 | enum aarch64_insn_variant variant, |
| 161 | enum aarch64_insn_branch_type type); |
Jiang Liu | 5c5bf25 | 2014-01-07 22:17:11 +0800 | [diff] [blame] | 162 | u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op); |
| 163 | u32 aarch64_insn_gen_nop(void); |
Zi Shen Lim | c0cafba | 2014-08-27 05:15:18 +0100 | [diff] [blame^] | 164 | u32 aarch64_insn_gen_branch_reg(enum aarch64_insn_register reg, |
| 165 | enum aarch64_insn_branch_type type); |
Jiang Liu | 5c5bf25 | 2014-01-07 22:17:11 +0800 | [diff] [blame] | 166 | |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 167 | bool aarch64_insn_hotpatch_safe(u32 old_insn, u32 new_insn); |
| 168 | |
Jiang Liu | ae16480 | 2014-01-07 22:17:09 +0800 | [diff] [blame] | 169 | int aarch64_insn_patch_text_nosync(void *addr, u32 insn); |
| 170 | int aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt); |
| 171 | int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt); |
AKASHI Takahiro | 26e9b83 | 2014-04-30 10:54:30 +0100 | [diff] [blame] | 172 | #endif /* __ASSEMBLY__ */ |
Jiang Liu | ae16480 | 2014-01-07 22:17:09 +0800 | [diff] [blame] | 173 | |
Jiang Liu | b11a64a | 2014-01-07 22:17:08 +0800 | [diff] [blame] | 174 | #endif /* __ASM_INSN_H */ |