Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

objtool/LoongArch: Implement instruction decoder

Only copy the minimal definitions of instruction opcodes and formats
in inst.h from arch/loongarch to tools/arch/loongarch, and also copy
the definition of sign_extend64() to tools/include/linux/bitops.h to
decode the following kinds of instructions:

(1) stack pointer related instructions
addi.d, ld.d, st.d, ldptr.d and stptr.d

(2) branch and jump related instructions
beq, bne, blt, bge, bltu, bgeu, beqz, bnez, bceqz, bcnez, b, bl and jirl

(3) other instructions
break, nop and ertn

See more info about instructions in LoongArch Reference Manual:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html

Co-developed-by: Jinyang He <hejinyang@loongson.cn>
Signed-off-by: Jinyang He <hejinyang@loongson.cn>
Co-developed-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>

authored by

Tiezhu Yang and committed by
Huacai Chen
b2d23158 e8aff71c

+443 -2
+161
tools/arch/loongarch/include/asm/inst.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited 4 + */ 5 + #ifndef _ASM_INST_H 6 + #define _ASM_INST_H 7 + 8 + #include <linux/bitops.h> 9 + 10 + #define LOONGARCH_INSN_NOP 0x03400000 11 + 12 + enum reg0i15_op { 13 + break_op = 0x54, 14 + }; 15 + 16 + enum reg0i26_op { 17 + b_op = 0x14, 18 + bl_op = 0x15, 19 + }; 20 + 21 + enum reg1i21_op { 22 + beqz_op = 0x10, 23 + bnez_op = 0x11, 24 + bceqz_op = 0x12, /* bits[9:8] = 0x00 */ 25 + bcnez_op = 0x12, /* bits[9:8] = 0x01 */ 26 + }; 27 + 28 + enum reg2_op { 29 + ertn_op = 0x1920e, 30 + }; 31 + 32 + enum reg2i12_op { 33 + addid_op = 0x0b, 34 + andi_op = 0x0d, 35 + ldd_op = 0xa3, 36 + std_op = 0xa7, 37 + }; 38 + 39 + enum reg2i14_op { 40 + ldptrd_op = 0x26, 41 + stptrd_op = 0x27, 42 + }; 43 + 44 + enum reg2i16_op { 45 + jirl_op = 0x13, 46 + beq_op = 0x16, 47 + bne_op = 0x17, 48 + blt_op = 0x18, 49 + bge_op = 0x19, 50 + bltu_op = 0x1a, 51 + bgeu_op = 0x1b, 52 + }; 53 + 54 + struct reg0i15_format { 55 + unsigned int immediate : 15; 56 + unsigned int opcode : 17; 57 + }; 58 + 59 + struct reg0i26_format { 60 + unsigned int immediate_h : 10; 61 + unsigned int immediate_l : 16; 62 + unsigned int opcode : 6; 63 + }; 64 + 65 + struct reg1i21_format { 66 + unsigned int immediate_h : 5; 67 + unsigned int rj : 5; 68 + unsigned int immediate_l : 16; 69 + unsigned int opcode : 6; 70 + }; 71 + 72 + struct reg2_format { 73 + unsigned int rd : 5; 74 + unsigned int rj : 5; 75 + unsigned int opcode : 22; 76 + }; 77 + 78 + struct reg2i12_format { 79 + unsigned int rd : 5; 80 + unsigned int rj : 5; 81 + unsigned int immediate : 12; 82 + unsigned int opcode : 10; 83 + }; 84 + 85 + struct reg2i14_format { 86 + unsigned int rd : 5; 87 + unsigned int rj : 5; 88 + unsigned int immediate : 14; 89 + unsigned int opcode : 8; 90 + }; 91 + 92 + struct reg2i16_format { 93 + unsigned int rd : 5; 94 + unsigned int rj : 5; 95 + unsigned int immediate : 16; 96 + unsigned int opcode : 6; 97 + }; 98 + 99 + union loongarch_instruction { 100 + unsigned int word; 101 + struct reg0i15_format reg0i15_format; 102 + struct reg0i26_format reg0i26_format; 103 + struct reg1i21_format reg1i21_format; 104 + struct reg2_format reg2_format; 105 + struct reg2i12_format reg2i12_format; 106 + struct reg2i14_format reg2i14_format; 107 + struct reg2i16_format reg2i16_format; 108 + }; 109 + 110 + #define LOONGARCH_INSN_SIZE sizeof(union loongarch_instruction) 111 + 112 + enum loongarch_gpr { 113 + LOONGARCH_GPR_ZERO = 0, 114 + LOONGARCH_GPR_RA = 1, 115 + LOONGARCH_GPR_TP = 2, 116 + LOONGARCH_GPR_SP = 3, 117 + LOONGARCH_GPR_A0 = 4, /* Reused as V0 for return value */ 118 + LOONGARCH_GPR_A1, /* Reused as V1 for return value */ 119 + LOONGARCH_GPR_A2, 120 + LOONGARCH_GPR_A3, 121 + LOONGARCH_GPR_A4, 122 + LOONGARCH_GPR_A5, 123 + LOONGARCH_GPR_A6, 124 + LOONGARCH_GPR_A7, 125 + LOONGARCH_GPR_T0 = 12, 126 + LOONGARCH_GPR_T1, 127 + LOONGARCH_GPR_T2, 128 + LOONGARCH_GPR_T3, 129 + LOONGARCH_GPR_T4, 130 + LOONGARCH_GPR_T5, 131 + LOONGARCH_GPR_T6, 132 + LOONGARCH_GPR_T7, 133 + LOONGARCH_GPR_T8, 134 + LOONGARCH_GPR_FP = 22, 135 + LOONGARCH_GPR_S0 = 23, 136 + LOONGARCH_GPR_S1, 137 + LOONGARCH_GPR_S2, 138 + LOONGARCH_GPR_S3, 139 + LOONGARCH_GPR_S4, 140 + LOONGARCH_GPR_S5, 141 + LOONGARCH_GPR_S6, 142 + LOONGARCH_GPR_S7, 143 + LOONGARCH_GPR_S8, 144 + LOONGARCH_GPR_MAX 145 + }; 146 + 147 + #define DEF_EMIT_REG2I16_FORMAT(NAME, OP) \ 148 + static inline void emit_##NAME(union loongarch_instruction *insn, \ 149 + enum loongarch_gpr rj, \ 150 + enum loongarch_gpr rd, \ 151 + int offset) \ 152 + { \ 153 + insn->reg2i16_format.opcode = OP; \ 154 + insn->reg2i16_format.immediate = offset; \ 155 + insn->reg2i16_format.rj = rj; \ 156 + insn->reg2i16_format.rd = rd; \ 157 + } 158 + 159 + DEF_EMIT_REG2I16_FORMAT(jirl, jirl_op) 160 + 161 + #endif /* _ASM_INST_H */
+11
tools/include/linux/bitops.h
··· 87 87 return (word << shift) | (word >> ((-shift) & 31)); 88 88 } 89 89 90 + /** 91 + * sign_extend64 - sign extend a 64-bit value using specified bit as sign-bit 92 + * @value: value to sign extend 93 + * @index: 0 based bit index (0<=index<64) to sign bit 94 + */ 95 + static __always_inline __s64 sign_extend64(__u64 value, int index) 96 + { 97 + __u8 shift = 63 - index; 98 + return (__s64)(value << shift) >> shift; 99 + } 100 + 90 101 #endif
+271 -2
tools/objtool/arch/loongarch/decode.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 #include <string.h> 3 3 #include <objtool/check.h> 4 + #include <objtool/warn.h> 5 + #include <asm/inst.h> 6 + 7 + #ifndef EM_LOONGARCH 8 + #define EM_LOONGARCH 258 9 + #endif 4 10 5 11 int arch_ftrace_match(char *name) 6 12 { ··· 45 39 return 0; 46 40 } 47 41 42 + static bool is_loongarch(const struct elf *elf) 43 + { 44 + if (elf->ehdr.e_machine == EM_LOONGARCH) 45 + return true; 46 + 47 + WARN("unexpected ELF machine type %d", elf->ehdr.e_machine); 48 + return false; 49 + } 50 + 51 + #define ADD_OP(op) \ 52 + if (!(op = calloc(1, sizeof(*op)))) \ 53 + return -1; \ 54 + else for (*ops_list = op, ops_list = &op->next; op; op = NULL) 55 + 56 + static bool decode_insn_reg0i26_fomat(union loongarch_instruction inst, 57 + struct instruction *insn) 58 + { 59 + switch (inst.reg0i26_format.opcode) { 60 + case b_op: 61 + insn->type = INSN_JUMP_UNCONDITIONAL; 62 + insn->immediate = sign_extend64(inst.reg0i26_format.immediate_h << 16 | 63 + inst.reg0i26_format.immediate_l, 25); 64 + break; 65 + case bl_op: 66 + insn->type = INSN_CALL; 67 + insn->immediate = sign_extend64(inst.reg0i26_format.immediate_h << 16 | 68 + inst.reg0i26_format.immediate_l, 25); 69 + break; 70 + default: 71 + return false; 72 + } 73 + 74 + return true; 75 + } 76 + 77 + static bool decode_insn_reg1i21_fomat(union loongarch_instruction inst, 78 + struct instruction *insn) 79 + { 80 + switch (inst.reg1i21_format.opcode) { 81 + case beqz_op: 82 + case bnez_op: 83 + case bceqz_op: 84 + insn->type = INSN_JUMP_CONDITIONAL; 85 + insn->immediate = sign_extend64(inst.reg1i21_format.immediate_h << 16 | 86 + inst.reg1i21_format.immediate_l, 20); 87 + break; 88 + default: 89 + return false; 90 + } 91 + 92 + return true; 93 + } 94 + 95 + static bool decode_insn_reg2i12_fomat(union loongarch_instruction inst, 96 + struct instruction *insn, 97 + struct stack_op **ops_list, 98 + struct stack_op *op) 99 + { 100 + switch (inst.reg2i12_format.opcode) { 101 + case addid_op: 102 + if ((inst.reg2i12_format.rd == CFI_SP) || (inst.reg2i12_format.rj == CFI_SP)) { 103 + /* addi.d sp,sp,si12 or addi.d fp,sp,si12 */ 104 + insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11); 105 + ADD_OP(op) { 106 + op->src.type = OP_SRC_ADD; 107 + op->src.reg = inst.reg2i12_format.rj; 108 + op->src.offset = insn->immediate; 109 + op->dest.type = OP_DEST_REG; 110 + op->dest.reg = inst.reg2i12_format.rd; 111 + } 112 + } 113 + break; 114 + case ldd_op: 115 + if (inst.reg2i12_format.rj == CFI_SP) { 116 + /* ld.d rd,sp,si12 */ 117 + insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11); 118 + ADD_OP(op) { 119 + op->src.type = OP_SRC_REG_INDIRECT; 120 + op->src.reg = CFI_SP; 121 + op->src.offset = insn->immediate; 122 + op->dest.type = OP_DEST_REG; 123 + op->dest.reg = inst.reg2i12_format.rd; 124 + } 125 + } 126 + break; 127 + case std_op: 128 + if (inst.reg2i12_format.rj == CFI_SP) { 129 + /* st.d rd,sp,si12 */ 130 + insn->immediate = sign_extend64(inst.reg2i12_format.immediate, 11); 131 + ADD_OP(op) { 132 + op->src.type = OP_SRC_REG; 133 + op->src.reg = inst.reg2i12_format.rd; 134 + op->dest.type = OP_DEST_REG_INDIRECT; 135 + op->dest.reg = CFI_SP; 136 + op->dest.offset = insn->immediate; 137 + } 138 + } 139 + break; 140 + case andi_op: 141 + if (inst.reg2i12_format.rd == 0 && 142 + inst.reg2i12_format.rj == 0 && 143 + inst.reg2i12_format.immediate == 0) 144 + /* andi r0,r0,0 */ 145 + insn->type = INSN_NOP; 146 + break; 147 + default: 148 + return false; 149 + } 150 + 151 + return true; 152 + } 153 + 154 + static bool decode_insn_reg2i14_fomat(union loongarch_instruction inst, 155 + struct instruction *insn, 156 + struct stack_op **ops_list, 157 + struct stack_op *op) 158 + { 159 + switch (inst.reg2i14_format.opcode) { 160 + case ldptrd_op: 161 + if (inst.reg2i14_format.rj == CFI_SP) { 162 + /* ldptr.d rd,sp,si14 */ 163 + insn->immediate = sign_extend64(inst.reg2i14_format.immediate, 13); 164 + ADD_OP(op) { 165 + op->src.type = OP_SRC_REG_INDIRECT; 166 + op->src.reg = CFI_SP; 167 + op->src.offset = insn->immediate; 168 + op->dest.type = OP_DEST_REG; 169 + op->dest.reg = inst.reg2i14_format.rd; 170 + } 171 + } 172 + break; 173 + case stptrd_op: 174 + if (inst.reg2i14_format.rj == CFI_SP) { 175 + /* stptr.d ra,sp,0 */ 176 + if (inst.reg2i14_format.rd == LOONGARCH_GPR_RA && 177 + inst.reg2i14_format.immediate == 0) 178 + break; 179 + 180 + /* stptr.d rd,sp,si14 */ 181 + insn->immediate = sign_extend64(inst.reg2i14_format.immediate, 13); 182 + ADD_OP(op) { 183 + op->src.type = OP_SRC_REG; 184 + op->src.reg = inst.reg2i14_format.rd; 185 + op->dest.type = OP_DEST_REG_INDIRECT; 186 + op->dest.reg = CFI_SP; 187 + op->dest.offset = insn->immediate; 188 + } 189 + } 190 + break; 191 + default: 192 + return false; 193 + } 194 + 195 + return true; 196 + } 197 + 198 + static bool decode_insn_reg2i16_fomat(union loongarch_instruction inst, 199 + struct instruction *insn) 200 + { 201 + switch (inst.reg2i16_format.opcode) { 202 + case jirl_op: 203 + if (inst.reg2i16_format.rd == 0 && 204 + inst.reg2i16_format.rj == CFI_RA && 205 + inst.reg2i16_format.immediate == 0) { 206 + /* jirl r0,ra,0 */ 207 + insn->type = INSN_RETURN; 208 + } else if (inst.reg2i16_format.rd == CFI_RA) { 209 + /* jirl ra,rj,offs16 */ 210 + insn->type = INSN_CALL_DYNAMIC; 211 + } else if (inst.reg2i16_format.rd == CFI_A0 && 212 + inst.reg2i16_format.immediate == 0) { 213 + /* 214 + * jirl a0,t0,0 215 + * this is a special case in loongarch_suspend_enter, 216 + * just treat it as a call instruction. 217 + */ 218 + insn->type = INSN_CALL_DYNAMIC; 219 + } else if (inst.reg2i16_format.rd == 0 && 220 + inst.reg2i16_format.immediate == 0) { 221 + /* jirl r0,rj,0 */ 222 + insn->type = INSN_JUMP_DYNAMIC; 223 + } else if (inst.reg2i16_format.rd == 0 && 224 + inst.reg2i16_format.immediate != 0) { 225 + /* 226 + * jirl r0,t0,12 227 + * this is a rare case in JUMP_VIRT_ADDR, 228 + * just ignore it due to it is harmless for tracing. 229 + */ 230 + break; 231 + } else { 232 + /* jirl rd,rj,offs16 */ 233 + insn->type = INSN_JUMP_UNCONDITIONAL; 234 + insn->immediate = sign_extend64(inst.reg2i16_format.immediate, 15); 235 + } 236 + break; 237 + case beq_op: 238 + case bne_op: 239 + case blt_op: 240 + case bge_op: 241 + case bltu_op: 242 + case bgeu_op: 243 + insn->type = INSN_JUMP_CONDITIONAL; 244 + insn->immediate = sign_extend64(inst.reg2i16_format.immediate, 15); 245 + break; 246 + default: 247 + return false; 248 + } 249 + 250 + return true; 251 + } 252 + 48 253 int arch_decode_instruction(struct objtool_file *file, const struct section *sec, 49 254 unsigned long offset, unsigned int maxlen, 50 255 struct instruction *insn) 51 256 { 257 + struct stack_op **ops_list = &insn->stack_ops; 258 + const struct elf *elf = file->elf; 259 + struct stack_op *op = NULL; 260 + union loongarch_instruction inst; 261 + 262 + if (!is_loongarch(elf)) 263 + return -1; 264 + 265 + if (maxlen < LOONGARCH_INSN_SIZE) 266 + return 0; 267 + 268 + insn->len = LOONGARCH_INSN_SIZE; 269 + insn->type = INSN_OTHER; 270 + insn->immediate = 0; 271 + 272 + inst = *(union loongarch_instruction *)(sec->data->d_buf + offset); 273 + 274 + if (decode_insn_reg0i26_fomat(inst, insn)) 275 + return 0; 276 + if (decode_insn_reg1i21_fomat(inst, insn)) 277 + return 0; 278 + if (decode_insn_reg2i12_fomat(inst, insn, ops_list, op)) 279 + return 0; 280 + if (decode_insn_reg2i14_fomat(inst, insn, ops_list, op)) 281 + return 0; 282 + if (decode_insn_reg2i16_fomat(inst, insn)) 283 + return 0; 284 + 285 + if (inst.word == 0) 286 + insn->type = INSN_NOP; 287 + else if (inst.reg0i15_format.opcode == break_op) { 288 + /* break */ 289 + insn->type = INSN_BUG; 290 + } else if (inst.reg2_format.opcode == ertn_op) { 291 + /* ertn */ 292 + insn->type = INSN_RETURN; 293 + } 294 + 52 295 return 0; 53 296 } 54 297 55 298 const char *arch_nop_insn(int len) 56 299 { 57 - return NULL; 300 + static u32 nop; 301 + 302 + if (len != LOONGARCH_INSN_SIZE) 303 + WARN("invalid NOP size: %d\n", len); 304 + 305 + nop = LOONGARCH_INSN_NOP; 306 + 307 + return (const char *)&nop; 58 308 } 59 309 60 310 const char *arch_ret_insn(int len) 61 311 { 62 - return NULL; 312 + static u32 ret; 313 + 314 + if (len != LOONGARCH_INSN_SIZE) 315 + WARN("invalid RET size: %d\n", len); 316 + 317 + emit_jirl((union loongarch_instruction *)&ret, LOONGARCH_GPR_RA, LOONGARCH_GPR_ZERO, 0); 318 + 319 + return (const char *)&ret; 63 320 } 64 321 65 322 void arch_initial_func_cfi_state(struct cfi_init_state *state)