1 // SPDX-License-Identifier: GPL-2.0-only
3 * several functions that help interpret ARC instructions
4 * used for unaligned accesses, kprobes and kgdb
6 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
9 #include <linux/types.h>
10 #include <linux/kprobes.h>
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
13 #include <asm/disasm.h>
15 #if defined(CONFIG_KGDB) || defined(CONFIG_ARC_EMUL_UNALIGNED) || \
16 defined(CONFIG_KPROBES)
18 /* disasm_instr: Analyses instruction at addr, stores
21 void __kprobes disasm_instr(unsigned long addr, struct disasm_state *state,
22 int userspace, struct pt_regs *regs, struct callee_regs *cregs)
25 int fieldC = 0, fieldCisReg = 0;
26 uint16_t word1 = 0, word0 = 0;
27 int subopcode, is_linked, op_format;
30 int bytes_not_copied = 0;
32 memset(state, 0, sizeof(struct disasm_state));
34 /* This fetches the upper part of the 32 bit instruction
35 * in both the cases of Little Endian or Big Endian configurations. */
37 bytes_not_copied = copy_from_user(ins_buf,
38 (const void __user *) addr, 8);
39 if (bytes_not_copied > 6)
43 ins_ptr = (uint16_t *) addr;
46 word1 = *((uint16_t *)addr);
48 state->major_opcode = (word1 >> 11) & 0x1F;
50 /* Check if the instruction is 32 bit or 16 bit instruction */
51 if (state->major_opcode < 0x0B) {
52 if (bytes_not_copied > 4)
55 word0 = *((uint16_t *)(addr+2));
56 state->words[0] = (word1 << 16) | word0;
59 state->words[0] = word1;
62 /* Read the second word in case of limm */
63 word1 = *((uint16_t *)(addr + state->instr_len));
64 word0 = *((uint16_t *)(addr + state->instr_len + 2));
65 state->words[1] = (word1 << 16) | word0;
67 switch (state->major_opcode) {
71 /* unconditional branch s25, conditional branch s21 */
72 fieldA = (IS_BIT(state->words[0], 16)) ?
73 FIELD_s25(state->words[0]) :
74 FIELD_s21(state->words[0]);
76 state->delay_slot = IS_BIT(state->words[0], 5);
77 state->target = fieldA + (addr & ~0x3);
78 state->flow = direct_jump;
82 if (IS_BIT(state->words[0], 16)) {
84 /* unconditional branch s25, conditional branch s21 */
85 fieldA = (IS_BIT(state->words[0], 17)) ?
86 (FIELD_s25(state->words[0]) & ~0x3) :
87 FIELD_s21(state->words[0]);
89 state->flow = direct_call;
91 /*Branch On Compare */
92 fieldA = FIELD_s9(state->words[0]) & ~0x3;
93 state->flow = direct_jump;
96 state->delay_slot = IS_BIT(state->words[0], 5);
97 state->target = fieldA + (addr & ~0x3);
101 case op_LD: /* LD<zz> a,[b,s9] */
103 state->di = BITS(state->words[0], 11, 11);
106 state->x = BITS(state->words[0], 6, 6);
107 state->zz = BITS(state->words[0], 7, 8);
108 state->aa = BITS(state->words[0], 9, 10);
109 state->wb_reg = FIELD_B(state->words[0]);
110 if (state->wb_reg == REG_LIMM) {
111 state->instr_len += 4;
113 state->src1 = state->words[1];
115 state->src1 = get_reg(state->wb_reg, regs, cregs);
117 state->src2 = FIELD_s9(state->words[0]);
118 state->dest = FIELD_A(state->words[0]);
119 state->pref = (state->dest == REG_LIMM);
124 state->di = BITS(state->words[0], 5, 5);
127 state->aa = BITS(state->words[0], 3, 4);
128 state->zz = BITS(state->words[0], 1, 2);
129 state->src1 = FIELD_C(state->words[0]);
130 if (state->src1 == REG_LIMM) {
131 state->instr_len += 4;
132 state->src1 = state->words[1];
134 state->src1 = get_reg(state->src1, regs, cregs);
136 state->wb_reg = FIELD_B(state->words[0]);
137 if (state->wb_reg == REG_LIMM) {
139 state->instr_len += 4;
140 state->src2 = state->words[1];
142 state->src2 = get_reg(state->wb_reg, regs, cregs);
144 state->src3 = FIELD_s9(state->words[0]);
148 subopcode = MINOR_OPCODE(state->words[0]);
153 case 35: /* JLcc.D */
156 if (subopcode == 33 || subopcode == 35)
157 state->delay_slot = 1;
159 if (subopcode == 34 || subopcode == 35)
163 op_format = BITS(state->words[0], 22, 23);
164 if (op_format == 0 || ((op_format == 3) &&
165 (!IS_BIT(state->words[0], 5)))) {
166 fieldC = FIELD_C(state->words[0]);
168 if (fieldC == REG_LIMM) {
169 fieldC = state->words[1];
170 state->instr_len += 4;
174 } else if (op_format == 1 || ((op_format == 3)
175 && (IS_BIT(state->words[0], 5)))) {
176 fieldC = FIELD_C(state->words[0]);
179 fieldC = FIELD_s12(state->words[0]);
183 state->target = fieldC;
184 state->flow = is_linked ?
185 direct_call : direct_jump;
187 state->target = get_reg(fieldC, regs, cregs);
188 state->flow = is_linked ?
189 indirect_call : indirect_jump;
191 state->is_branch = 1;
195 if (BITS(state->words[0], 22, 23) == 3) {
196 /* Conditional LPcc u7 */
197 fieldC = FIELD_C(state->words[0]);
199 fieldC = fieldC << 1;
200 fieldC += (addr & ~0x03);
201 state->is_branch = 1;
202 state->flow = direct_jump;
203 state->target = fieldC;
205 /* For Unconditional lp, next pc is the fall through
206 * which is updated */
209 case 48 ... 55: /* LD a,[b,c] */
210 state->di = BITS(state->words[0], 15, 15);
213 state->x = BITS(state->words[0], 16, 16);
214 state->zz = BITS(state->words[0], 17, 18);
215 state->aa = BITS(state->words[0], 22, 23);
216 state->wb_reg = FIELD_B(state->words[0]);
217 if (state->wb_reg == REG_LIMM) {
218 state->instr_len += 4;
219 state->src1 = state->words[1];
221 state->src1 = get_reg(state->wb_reg, regs,
224 state->src2 = FIELD_C(state->words[0]);
225 if (state->src2 == REG_LIMM) {
226 state->instr_len += 4;
227 state->src2 = state->words[1];
229 state->src2 = get_reg(state->src2, regs,
232 state->dest = FIELD_A(state->words[0]);
233 if (state->dest == REG_LIMM)
238 /* still need to check for limm to extract instr len */
239 /* MOV is special case because it only takes 2 args */
240 switch (BITS(state->words[0], 22, 23)) {
241 case 0: /* OP a,b,c */
242 if (FIELD_C(state->words[0]) == REG_LIMM)
243 state->instr_len += 4;
245 case 1: /* OP a,b,u6 */
247 case 2: /* OP b,b,s12 */
249 case 3: /* OP.cc b,b,c/u6 */
250 if ((!IS_BIT(state->words[0], 5)) &&
251 (FIELD_C(state->words[0]) == REG_LIMM))
252 state->instr_len += 4;
259 /* Not a Load, Jump or Loop instruction */
260 /* still need to check for limm to extract instr len */
261 switch (BITS(state->words[0], 22, 23)) {
262 case 0: /* OP a,b,c */
263 if ((FIELD_B(state->words[0]) == REG_LIMM) ||
264 (FIELD_C(state->words[0]) == REG_LIMM))
265 state->instr_len += 4;
267 case 1: /* OP a,b,u6 */
269 case 2: /* OP b,b,s12 */
271 case 3: /* OP.cc b,b,c/u6 */
272 if ((!IS_BIT(state->words[0], 5)) &&
273 ((FIELD_B(state->words[0]) == REG_LIMM) ||
274 (FIELD_C(state->words[0]) == REG_LIMM)))
275 state->instr_len += 4;
282 /* 16 Bit Instructions */
283 case op_LD_ADD: /* LD_S|LDB_S|LDW_S a,[b,c] */
284 state->zz = BITS(state->words[0], 3, 4);
285 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
286 state->src2 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
287 state->dest = FIELD_S_A(state->words[0]);
291 /* check for limm, ignore mov_s h,b (== mov_s 0,b) */
292 if ((BITS(state->words[0], 3, 4) < 3) &&
293 (FIELD_S_H(state->words[0]) == REG_LIMM))
294 state->instr_len += 4;
298 subopcode = BITS(state->words[0], 5, 7);
304 state->target = get_reg(FIELD_S_B(state->words[0]),
306 state->delay_slot = subopcode & 1;
307 state->flow = (subopcode >= 2) ?
308 direct_call : indirect_jump;
311 switch (BITS(state->words[0], 8, 10)) {
312 case 4: /* jeq_s [blink] */
313 case 5: /* jne_s [blink] */
314 case 6: /* j_s [blink] */
315 case 7: /* j_s.d [blink] */
316 state->delay_slot = (subopcode == 7);
317 state->flow = indirect_jump;
318 state->target = get_reg(31, regs, cregs);
327 case op_LD_S: /* LD_S c, [b, u7] */
328 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
329 state->src2 = FIELD_S_u7(state->words[0]);
330 state->dest = FIELD_S_C(state->words[0]);
335 /* no further handling required as byte accesses should not
336 * cause an unaligned access exception */
340 case op_LDWX_S: /* LDWX_S c, [b, u6] */
342 /* intentional fall-through */
344 case op_LDW_S: /* LDW_S c, [b, u6] */
346 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
347 state->src2 = FIELD_S_u6(state->words[0]);
348 state->dest = FIELD_S_C(state->words[0]);
351 case op_ST_S: /* ST_S c, [b, u7] */
353 state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
354 state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
355 state->src3 = FIELD_S_u7(state->words[0]);
358 case op_STW_S: /* STW_S c,[b,u6] */
361 state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
362 state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
363 state->src3 = FIELD_S_u6(state->words[0]);
366 case op_SP: /* LD_S|LDB_S b,[sp,u7], ST_S|STB_S b,[sp,u7] */
367 /* note: we are ignoring possibility of:
368 * ADD_S, SUB_S, PUSH_S, POP_S as these should not
369 * cause unaliged exception anyway */
370 state->write = BITS(state->words[0], 6, 6);
371 state->zz = BITS(state->words[0], 5, 5);
373 break; /* byte accesses should not come here */
375 state->src1 = get_reg(28, regs, cregs);
376 state->src2 = FIELD_S_u7(state->words[0]);
377 state->dest = FIELD_S_B(state->words[0]);
379 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs,
381 state->src2 = get_reg(28, regs, cregs);
382 state->src3 = FIELD_S_u7(state->words[0]);
386 case op_GP: /* LD_S|LDB_S|LDW_S r0,[gp,s11/s9/s10] */
387 /* note: ADD_S r0, gp, s11 is ignored */
388 state->zz = BITS(state->words[0], 9, 10);
389 state->src1 = get_reg(26, regs, cregs);
390 state->src2 = state->zz ? FIELD_S_s10(state->words[0]) :
391 FIELD_S_s11(state->words[0]);
395 case op_Pcl: /* LD_S b,[pcl,u10] */
396 state->src1 = regs->ret & ~3;
397 state->src2 = FIELD_S_u10(state->words[0]);
398 state->dest = FIELD_S_B(state->words[0]);
402 state->target = FIELD_S_s8(state->words[0]) + (addr & ~0x03);
403 state->flow = direct_jump;
404 state->is_branch = 1;
408 fieldA = (BITS(state->words[0], 9, 10) == 3) ?
409 FIELD_S_s7(state->words[0]) :
410 FIELD_S_s10(state->words[0]);
411 state->target = fieldA + (addr & ~0x03);
412 state->flow = direct_jump;
413 state->is_branch = 1;
417 state->target = FIELD_S_s13(state->words[0]) + (addr & ~0x03);
418 state->flow = direct_call;
419 state->is_branch = 1;
426 if (bytes_not_copied <= (8 - state->instr_len))
429 fault: state->fault = 1;
432 long __kprobes get_reg(int reg, struct pt_regs *regs,
433 struct callee_regs *cregs)
442 if (cregs && (reg <= 25)) {
459 void __kprobes set_reg(int reg, long val, struct pt_regs *regs,
460 struct callee_regs *cregs)
493 * Disassembles the insn at @pc and sets @next_pc to next PC (which could be
494 * @pc +2/4/6 (ARCompact ISA allows free intermixing of 16/32 bit insns).
497 * -@tgt_if_br is set to branch target.
498 * -If branch has delay slot, @next_pc updated with actual next PC.
500 int __kprobes disasm_next_pc(unsigned long pc, struct pt_regs *regs,
501 struct callee_regs *cregs,
502 unsigned long *next_pc, unsigned long *tgt_if_br)
504 struct disasm_state instr;
506 memset(&instr, 0, sizeof(struct disasm_state));
507 disasm_instr(pc, &instr, 0, regs, cregs);
509 *next_pc = pc + instr.instr_len;
511 /* Instruction with possible two targets branch, jump and loop */
513 *tgt_if_br = instr.target;
515 /* For the instructions with delay slots, the fall through is the
516 * instruction following the instruction in delay slot.
518 if (instr.delay_slot) {
519 struct disasm_state instr_d;
521 disasm_instr(*next_pc, &instr_d, 0, regs, cregs);
523 *next_pc += instr_d.instr_len;
526 /* Zero Overhead Loop - end of the loop */
527 if (!(regs->status32 & STATUS32_L) && (*next_pc == regs->lp_end)
528 && (regs->lp_count > 1)) {
529 *next_pc = regs->lp_start;
532 return instr.is_branch;
535 #endif /* CONFIG_KGDB || CONFIG_ARC_EMUL_UNALIGNED || CONFIG_KPROBES */