2 * several functions that help interpret ARC instructions
3 * used for unaligned accesses, kprobes and kgdb
5 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
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.
12 #include <linux/types.h>
13 #include <linux/kprobes.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <asm/disasm.h>
18 #if defined(CONFIG_KGDB) || defined(CONFIG_ARC_MISALIGN_ACCESS) || \
19 defined(CONFIG_KPROBES)
21 /* disasm_instr: Analyses instruction at addr, stores
24 void __kprobes disasm_instr(unsigned long addr, struct disasm_state *state,
25 int userspace, struct pt_regs *regs, struct callee_regs *cregs)
28 int fieldC = 0, fieldCisReg = 0;
29 uint16_t word1 = 0, word0 = 0;
30 int subopcode, is_linked, op_format;
33 int bytes_not_copied = 0;
35 memset(state, 0, sizeof(struct disasm_state));
37 /* This fetches the upper part of the 32 bit instruction
38 * in both the cases of Little Endian or Big Endian configurations. */
40 bytes_not_copied = copy_from_user(ins_buf,
41 (const void __user *) addr, 8);
42 if (bytes_not_copied > 6)
46 ins_ptr = (uint16_t *) addr;
49 word1 = *((uint16_t *)addr);
51 state->major_opcode = (word1 >> 11) & 0x1F;
53 /* Check if the instruction is 32 bit or 16 bit instruction */
54 if (state->major_opcode < 0x0B) {
55 if (bytes_not_copied > 4)
58 word0 = *((uint16_t *)(addr+2));
59 state->words[0] = (word1 << 16) | word0;
62 state->words[0] = word1;
65 /* Read the second word in case of limm */
66 word1 = *((uint16_t *)(addr + state->instr_len));
67 word0 = *((uint16_t *)(addr + state->instr_len + 2));
68 state->words[1] = (word1 << 16) | word0;
70 switch (state->major_opcode) {
74 /* unconditional branch s25, conditional branch s21 */
75 fieldA = (IS_BIT(state->words[0], 16)) ?
76 FIELD_s25(state->words[0]) :
77 FIELD_s21(state->words[0]);
79 state->delay_slot = IS_BIT(state->words[0], 5);
80 state->target = fieldA + (addr & ~0x3);
81 state->flow = direct_jump;
85 if (IS_BIT(state->words[0], 16)) {
87 /* unconditional branch s25, conditional branch s21 */
88 fieldA = (IS_BIT(state->words[0], 17)) ?
89 (FIELD_s25(state->words[0]) & ~0x3) :
90 FIELD_s21(state->words[0]);
92 state->flow = direct_call;
94 /*Branch On Compare */
95 fieldA = FIELD_s9(state->words[0]) & ~0x3;
96 state->flow = direct_jump;
99 state->delay_slot = IS_BIT(state->words[0], 5);
100 state->target = fieldA + (addr & ~0x3);
101 state->is_branch = 1;
104 case op_LD: /* LD<zz> a,[b,s9] */
106 state->di = BITS(state->words[0], 11, 11);
109 state->x = BITS(state->words[0], 6, 6);
110 state->zz = BITS(state->words[0], 7, 8);
111 state->aa = BITS(state->words[0], 9, 10);
112 state->wb_reg = FIELD_B(state->words[0]);
113 if (state->wb_reg == REG_LIMM) {
114 state->instr_len += 4;
116 state->src1 = state->words[1];
118 state->src1 = get_reg(state->wb_reg, regs, cregs);
120 state->src2 = FIELD_s9(state->words[0]);
121 state->dest = FIELD_A(state->words[0]);
122 state->pref = (state->dest == REG_LIMM);
127 state->di = BITS(state->words[0], 5, 5);
130 state->aa = BITS(state->words[0], 3, 4);
131 state->zz = BITS(state->words[0], 1, 2);
132 state->src1 = FIELD_C(state->words[0]);
133 if (state->src1 == REG_LIMM) {
134 state->instr_len += 4;
135 state->src1 = state->words[1];
137 state->src1 = get_reg(state->src1, regs, cregs);
139 state->wb_reg = FIELD_B(state->words[0]);
140 if (state->wb_reg == REG_LIMM) {
142 state->instr_len += 4;
143 state->src2 = state->words[1];
145 state->src2 = get_reg(state->wb_reg, regs, cregs);
147 state->src3 = FIELD_s9(state->words[0]);
151 subopcode = MINOR_OPCODE(state->words[0]);
156 case 35: /* JLcc.D */
159 if (subopcode == 33 || subopcode == 35)
160 state->delay_slot = 1;
162 if (subopcode == 34 || subopcode == 35)
166 op_format = BITS(state->words[0], 22, 23);
167 if (op_format == 0 || ((op_format == 3) &&
168 (!IS_BIT(state->words[0], 5)))) {
169 fieldC = FIELD_C(state->words[0]);
171 if (fieldC == REG_LIMM) {
172 fieldC = state->words[1];
173 state->instr_len += 4;
177 } else if (op_format == 1 || ((op_format == 3)
178 && (IS_BIT(state->words[0], 5)))) {
179 fieldC = FIELD_C(state->words[0]);
182 fieldC = FIELD_s12(state->words[0]);
186 state->target = fieldC;
187 state->flow = is_linked ?
188 direct_call : direct_jump;
190 state->target = get_reg(fieldC, regs, cregs);
191 state->flow = is_linked ?
192 indirect_call : indirect_jump;
194 state->is_branch = 1;
198 if (BITS(state->words[0], 22, 23) == 3) {
199 /* Conditional LPcc u7 */
200 fieldC = FIELD_C(state->words[0]);
202 fieldC = fieldC << 1;
203 fieldC += (addr & ~0x03);
204 state->is_branch = 1;
205 state->flow = direct_jump;
206 state->target = fieldC;
208 /* For Unconditional lp, next pc is the fall through
209 * which is updated */
212 case 48 ... 55: /* LD a,[b,c] */
213 state->di = BITS(state->words[0], 15, 15);
216 state->x = BITS(state->words[0], 16, 16);
217 state->zz = BITS(state->words[0], 17, 18);
218 state->aa = BITS(state->words[0], 22, 23);
219 state->wb_reg = FIELD_B(state->words[0]);
220 if (state->wb_reg == REG_LIMM) {
221 state->instr_len += 4;
222 state->src1 = state->words[1];
224 state->src1 = get_reg(state->wb_reg, regs,
227 state->src2 = FIELD_C(state->words[0]);
228 if (state->src2 == REG_LIMM) {
229 state->instr_len += 4;
230 state->src2 = state->words[1];
232 state->src2 = get_reg(state->src2, regs,
235 state->dest = FIELD_A(state->words[0]);
236 if (state->dest == REG_LIMM)
241 /* still need to check for limm to extract instr len */
242 /* MOV is special case because it only takes 2 args */
243 switch (BITS(state->words[0], 22, 23)) {
244 case 0: /* OP a,b,c */
245 if (FIELD_C(state->words[0]) == REG_LIMM)
246 state->instr_len += 4;
248 case 1: /* OP a,b,u6 */
250 case 2: /* OP b,b,s12 */
252 case 3: /* OP.cc b,b,c/u6 */
253 if ((!IS_BIT(state->words[0], 5)) &&
254 (FIELD_C(state->words[0]) == REG_LIMM))
255 state->instr_len += 4;
262 /* Not a Load, Jump or Loop instruction */
263 /* still need to check for limm to extract instr len */
264 switch (BITS(state->words[0], 22, 23)) {
265 case 0: /* OP a,b,c */
266 if ((FIELD_B(state->words[0]) == REG_LIMM) ||
267 (FIELD_C(state->words[0]) == REG_LIMM))
268 state->instr_len += 4;
270 case 1: /* OP a,b,u6 */
272 case 2: /* OP b,b,s12 */
274 case 3: /* OP.cc b,b,c/u6 */
275 if ((!IS_BIT(state->words[0], 5)) &&
276 ((FIELD_B(state->words[0]) == REG_LIMM) ||
277 (FIELD_C(state->words[0]) == REG_LIMM)))
278 state->instr_len += 4;
285 /* 16 Bit Instructions */
286 case op_LD_ADD: /* LD_S|LDB_S|LDW_S a,[b,c] */
287 state->zz = BITS(state->words[0], 3, 4);
288 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
289 state->src2 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
290 state->dest = FIELD_S_A(state->words[0]);
294 /* check for limm, ignore mov_s h,b (== mov_s 0,b) */
295 if ((BITS(state->words[0], 3, 4) < 3) &&
296 (FIELD_S_H(state->words[0]) == REG_LIMM))
297 state->instr_len += 4;
301 subopcode = BITS(state->words[0], 5, 7);
307 state->target = get_reg(FIELD_S_B(state->words[0]),
309 state->delay_slot = subopcode & 1;
310 state->flow = (subopcode >= 2) ?
311 direct_call : indirect_jump;
314 switch (BITS(state->words[0], 8, 10)) {
315 case 4: /* jeq_s [blink] */
316 case 5: /* jne_s [blink] */
317 case 6: /* j_s [blink] */
318 case 7: /* j_s.d [blink] */
319 state->delay_slot = (subopcode == 7);
320 state->flow = indirect_jump;
321 state->target = get_reg(31, regs, cregs);
330 case op_LD_S: /* LD_S c, [b, u7] */
331 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
332 state->src2 = FIELD_S_u7(state->words[0]);
333 state->dest = FIELD_S_C(state->words[0]);
338 /* no further handling required as byte accesses should not
339 * cause an unaligned access exception */
343 case op_LDWX_S: /* LDWX_S c, [b, u6] */
345 /* intentional fall-through */
347 case op_LDW_S: /* LDW_S c, [b, u6] */
349 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
350 state->src2 = FIELD_S_u6(state->words[0]);
351 state->dest = FIELD_S_C(state->words[0]);
354 case op_ST_S: /* ST_S c, [b, u7] */
356 state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
357 state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
358 state->src3 = FIELD_S_u7(state->words[0]);
361 case op_STW_S: /* STW_S c,[b,u6] */
364 state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
365 state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
366 state->src3 = FIELD_S_u6(state->words[0]);
369 case op_SP: /* LD_S|LDB_S b,[sp,u7], ST_S|STB_S b,[sp,u7] */
370 /* note: we are ignoring possibility of:
371 * ADD_S, SUB_S, PUSH_S, POP_S as these should not
372 * cause unaliged exception anyway */
373 state->write = BITS(state->words[0], 6, 6);
374 state->zz = BITS(state->words[0], 5, 5);
376 break; /* byte accesses should not come here */
378 state->src1 = get_reg(28, regs, cregs);
379 state->src2 = FIELD_S_u7(state->words[0]);
380 state->dest = FIELD_S_B(state->words[0]);
382 state->src1 = get_reg(FIELD_S_B(state->words[0]), regs,
384 state->src2 = get_reg(28, regs, cregs);
385 state->src3 = FIELD_S_u7(state->words[0]);
389 case op_GP: /* LD_S|LDB_S|LDW_S r0,[gp,s11/s9/s10] */
390 /* note: ADD_S r0, gp, s11 is ignored */
391 state->zz = BITS(state->words[0], 9, 10);
392 state->src1 = get_reg(26, regs, cregs);
393 state->src2 = state->zz ? FIELD_S_s10(state->words[0]) :
394 FIELD_S_s11(state->words[0]);
398 case op_Pcl: /* LD_S b,[pcl,u10] */
399 state->src1 = regs->ret & ~3;
400 state->src2 = FIELD_S_u10(state->words[0]);
401 state->dest = FIELD_S_B(state->words[0]);
405 state->target = FIELD_S_s8(state->words[0]) + (addr & ~0x03);
406 state->flow = direct_jump;
407 state->is_branch = 1;
411 fieldA = (BITS(state->words[0], 9, 10) == 3) ?
412 FIELD_S_s7(state->words[0]) :
413 FIELD_S_s10(state->words[0]);
414 state->target = fieldA + (addr & ~0x03);
415 state->flow = direct_jump;
416 state->is_branch = 1;
420 state->target = FIELD_S_s13(state->words[0]) + (addr & ~0x03);
421 state->flow = direct_call;
422 state->is_branch = 1;
429 if (bytes_not_copied <= (8 - state->instr_len))
432 fault: state->fault = 1;
435 long __kprobes get_reg(int reg, struct pt_regs *regs,
436 struct callee_regs *cregs)
445 if (cregs && (reg <= 25)) {
462 void __kprobes set_reg(int reg, long val, struct pt_regs *regs,
463 struct callee_regs *cregs)
496 * Disassembles the insn at @pc and sets @next_pc to next PC (which could be
497 * @pc +2/4/6 (ARCompact ISA allows free intermixing of 16/32 bit insns).
500 * -@tgt_if_br is set to branch target.
501 * -If branch has delay slot, @next_pc updated with actual next PC.
503 int __kprobes disasm_next_pc(unsigned long pc, struct pt_regs *regs,
504 struct callee_regs *cregs,
505 unsigned long *next_pc, unsigned long *tgt_if_br)
507 struct disasm_state instr;
509 memset(&instr, 0, sizeof(struct disasm_state));
510 disasm_instr(pc, &instr, 0, regs, cregs);
512 *next_pc = pc + instr.instr_len;
514 /* Instruction with possible two targets branch, jump and loop */
516 *tgt_if_br = instr.target;
518 /* For the instructions with delay slots, the fall through is the
519 * instruction following the instruction in delay slot.
521 if (instr.delay_slot) {
522 struct disasm_state instr_d;
524 disasm_instr(*next_pc, &instr_d, 0, regs, cregs);
526 *next_pc += instr_d.instr_len;
529 /* Zero Overhead Loop - end of the loop */
530 if (!(regs->status32 & STATUS32_L) && (*next_pc == regs->lp_end)
531 && (regs->lp_count > 1)) {
532 *next_pc = regs->lp_start;
535 return instr.is_branch;
538 #endif /* CONFIG_KGDB || CONFIG_ARC_MISALIGN_ACCESS || CONFIG_KPROBES */