1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e65ab5a8SVineet Gupta /*
3e65ab5a8SVineet Gupta * several functions that help interpret ARC instructions
4e65ab5a8SVineet Gupta * used for unaligned accesses, kprobes and kgdb
5e65ab5a8SVineet Gupta *
6e65ab5a8SVineet Gupta * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
7e65ab5a8SVineet Gupta */
8e65ab5a8SVineet Gupta
9e65ab5a8SVineet Gupta #include <linux/types.h>
10e65ab5a8SVineet Gupta #include <linux/kprobes.h>
11e65ab5a8SVineet Gupta #include <linux/slab.h>
121ec9db10SSachin Kamat #include <linux/uaccess.h>
13e65ab5a8SVineet Gupta #include <asm/disasm.h>
14e65ab5a8SVineet Gupta
151736a56fSVineet Gupta #if defined(CONFIG_KGDB) || defined(CONFIG_ARC_EMUL_UNALIGNED) || \
16e65ab5a8SVineet Gupta defined(CONFIG_KPROBES)
17e65ab5a8SVineet Gupta
18e65ab5a8SVineet Gupta /* disasm_instr: Analyses instruction at addr, stores
19e65ab5a8SVineet Gupta * findings in *state
20e65ab5a8SVineet Gupta */
disasm_instr(unsigned long addr,struct disasm_state * state,int userspace,struct pt_regs * regs,struct callee_regs * cregs)21e65ab5a8SVineet Gupta void __kprobes disasm_instr(unsigned long addr, struct disasm_state *state,
22e65ab5a8SVineet Gupta int userspace, struct pt_regs *regs, struct callee_regs *cregs)
23e65ab5a8SVineet Gupta {
24e65ab5a8SVineet Gupta int fieldA = 0;
25e65ab5a8SVineet Gupta int fieldC = 0, fieldCisReg = 0;
26e65ab5a8SVineet Gupta uint16_t word1 = 0, word0 = 0;
27e65ab5a8SVineet Gupta int subopcode, is_linked, op_format;
28e65ab5a8SVineet Gupta uint16_t *ins_ptr;
29e65ab5a8SVineet Gupta uint16_t ins_buf[4];
30e65ab5a8SVineet Gupta int bytes_not_copied = 0;
31e65ab5a8SVineet Gupta
32e65ab5a8SVineet Gupta memset(state, 0, sizeof(struct disasm_state));
33e65ab5a8SVineet Gupta
34e65ab5a8SVineet Gupta /* This fetches the upper part of the 32 bit instruction
35e65ab5a8SVineet Gupta * in both the cases of Little Endian or Big Endian configurations. */
36e65ab5a8SVineet Gupta if (userspace) {
37e65ab5a8SVineet Gupta bytes_not_copied = copy_from_user(ins_buf,
38e65ab5a8SVineet Gupta (const void __user *) addr, 8);
39e65ab5a8SVineet Gupta if (bytes_not_copied > 6)
40e65ab5a8SVineet Gupta goto fault;
41e65ab5a8SVineet Gupta ins_ptr = ins_buf;
42e65ab5a8SVineet Gupta } else {
43e65ab5a8SVineet Gupta ins_ptr = (uint16_t *) addr;
44e65ab5a8SVineet Gupta }
45e65ab5a8SVineet Gupta
46e65ab5a8SVineet Gupta word1 = *((uint16_t *)addr);
47e65ab5a8SVineet Gupta
48e65ab5a8SVineet Gupta state->major_opcode = (word1 >> 11) & 0x1F;
49e65ab5a8SVineet Gupta
50e65ab5a8SVineet Gupta /* Check if the instruction is 32 bit or 16 bit instruction */
51e65ab5a8SVineet Gupta if (state->major_opcode < 0x0B) {
52e65ab5a8SVineet Gupta if (bytes_not_copied > 4)
53e65ab5a8SVineet Gupta goto fault;
54e65ab5a8SVineet Gupta state->instr_len = 4;
55e65ab5a8SVineet Gupta word0 = *((uint16_t *)(addr+2));
56e65ab5a8SVineet Gupta state->words[0] = (word1 << 16) | word0;
57e65ab5a8SVineet Gupta } else {
58e65ab5a8SVineet Gupta state->instr_len = 2;
59e65ab5a8SVineet Gupta state->words[0] = word1;
60e65ab5a8SVineet Gupta }
61e65ab5a8SVineet Gupta
62e65ab5a8SVineet Gupta /* Read the second word in case of limm */
63e65ab5a8SVineet Gupta word1 = *((uint16_t *)(addr + state->instr_len));
64e65ab5a8SVineet Gupta word0 = *((uint16_t *)(addr + state->instr_len + 2));
65e65ab5a8SVineet Gupta state->words[1] = (word1 << 16) | word0;
66e65ab5a8SVineet Gupta
67e65ab5a8SVineet Gupta switch (state->major_opcode) {
68e65ab5a8SVineet Gupta case op_Bcc:
69e65ab5a8SVineet Gupta state->is_branch = 1;
70e65ab5a8SVineet Gupta
71e65ab5a8SVineet Gupta /* unconditional branch s25, conditional branch s21 */
72e65ab5a8SVineet Gupta fieldA = (IS_BIT(state->words[0], 16)) ?
73e65ab5a8SVineet Gupta FIELD_s25(state->words[0]) :
74e65ab5a8SVineet Gupta FIELD_s21(state->words[0]);
75e65ab5a8SVineet Gupta
76e65ab5a8SVineet Gupta state->delay_slot = IS_BIT(state->words[0], 5);
77e65ab5a8SVineet Gupta state->target = fieldA + (addr & ~0x3);
78e65ab5a8SVineet Gupta state->flow = direct_jump;
79e65ab5a8SVineet Gupta break;
80e65ab5a8SVineet Gupta
81e65ab5a8SVineet Gupta case op_BLcc:
82e65ab5a8SVineet Gupta if (IS_BIT(state->words[0], 16)) {
83e65ab5a8SVineet Gupta /* Branch and Link*/
84e65ab5a8SVineet Gupta /* unconditional branch s25, conditional branch s21 */
85e65ab5a8SVineet Gupta fieldA = (IS_BIT(state->words[0], 17)) ?
86e65ab5a8SVineet Gupta (FIELD_s25(state->words[0]) & ~0x3) :
87e65ab5a8SVineet Gupta FIELD_s21(state->words[0]);
88e65ab5a8SVineet Gupta
89e65ab5a8SVineet Gupta state->flow = direct_call;
90e65ab5a8SVineet Gupta } else {
91e65ab5a8SVineet Gupta /*Branch On Compare */
92e65ab5a8SVineet Gupta fieldA = FIELD_s9(state->words[0]) & ~0x3;
93e65ab5a8SVineet Gupta state->flow = direct_jump;
94e65ab5a8SVineet Gupta }
95e65ab5a8SVineet Gupta
96e65ab5a8SVineet Gupta state->delay_slot = IS_BIT(state->words[0], 5);
97e65ab5a8SVineet Gupta state->target = fieldA + (addr & ~0x3);
98e65ab5a8SVineet Gupta state->is_branch = 1;
99e65ab5a8SVineet Gupta break;
100e65ab5a8SVineet Gupta
101e65ab5a8SVineet Gupta case op_LD: /* LD<zz> a,[b,s9] */
102e65ab5a8SVineet Gupta state->write = 0;
103e65ab5a8SVineet Gupta state->di = BITS(state->words[0], 11, 11);
104e65ab5a8SVineet Gupta if (state->di)
105e65ab5a8SVineet Gupta break;
106e65ab5a8SVineet Gupta state->x = BITS(state->words[0], 6, 6);
107e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 7, 8);
108e65ab5a8SVineet Gupta state->aa = BITS(state->words[0], 9, 10);
109e65ab5a8SVineet Gupta state->wb_reg = FIELD_B(state->words[0]);
110e65ab5a8SVineet Gupta if (state->wb_reg == REG_LIMM) {
111e65ab5a8SVineet Gupta state->instr_len += 4;
112e65ab5a8SVineet Gupta state->aa = 0;
113e65ab5a8SVineet Gupta state->src1 = state->words[1];
114e65ab5a8SVineet Gupta } else {
115e65ab5a8SVineet Gupta state->src1 = get_reg(state->wb_reg, regs, cregs);
116e65ab5a8SVineet Gupta }
117e65ab5a8SVineet Gupta state->src2 = FIELD_s9(state->words[0]);
118e65ab5a8SVineet Gupta state->dest = FIELD_A(state->words[0]);
119e65ab5a8SVineet Gupta state->pref = (state->dest == REG_LIMM);
120e65ab5a8SVineet Gupta break;
121e65ab5a8SVineet Gupta
122e65ab5a8SVineet Gupta case op_ST:
123e65ab5a8SVineet Gupta state->write = 1;
124e65ab5a8SVineet Gupta state->di = BITS(state->words[0], 5, 5);
125e65ab5a8SVineet Gupta if (state->di)
126e65ab5a8SVineet Gupta break;
127e65ab5a8SVineet Gupta state->aa = BITS(state->words[0], 3, 4);
128e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 1, 2);
129e65ab5a8SVineet Gupta state->src1 = FIELD_C(state->words[0]);
130e65ab5a8SVineet Gupta if (state->src1 == REG_LIMM) {
131e65ab5a8SVineet Gupta state->instr_len += 4;
132e65ab5a8SVineet Gupta state->src1 = state->words[1];
133e65ab5a8SVineet Gupta } else {
134e65ab5a8SVineet Gupta state->src1 = get_reg(state->src1, regs, cregs);
135e65ab5a8SVineet Gupta }
136e65ab5a8SVineet Gupta state->wb_reg = FIELD_B(state->words[0]);
137e65ab5a8SVineet Gupta if (state->wb_reg == REG_LIMM) {
138e65ab5a8SVineet Gupta state->aa = 0;
139e65ab5a8SVineet Gupta state->instr_len += 4;
140e65ab5a8SVineet Gupta state->src2 = state->words[1];
141e65ab5a8SVineet Gupta } else {
142e65ab5a8SVineet Gupta state->src2 = get_reg(state->wb_reg, regs, cregs);
143e65ab5a8SVineet Gupta }
144e65ab5a8SVineet Gupta state->src3 = FIELD_s9(state->words[0]);
145e65ab5a8SVineet Gupta break;
146e65ab5a8SVineet Gupta
147e65ab5a8SVineet Gupta case op_MAJOR_4:
148e65ab5a8SVineet Gupta subopcode = MINOR_OPCODE(state->words[0]);
149e65ab5a8SVineet Gupta switch (subopcode) {
150e65ab5a8SVineet Gupta case 32: /* Jcc */
151e65ab5a8SVineet Gupta case 33: /* Jcc.D */
152e65ab5a8SVineet Gupta case 34: /* JLcc */
153e65ab5a8SVineet Gupta case 35: /* JLcc.D */
154e65ab5a8SVineet Gupta is_linked = 0;
155e65ab5a8SVineet Gupta
156e65ab5a8SVineet Gupta if (subopcode == 33 || subopcode == 35)
157e65ab5a8SVineet Gupta state->delay_slot = 1;
158e65ab5a8SVineet Gupta
159e65ab5a8SVineet Gupta if (subopcode == 34 || subopcode == 35)
160e65ab5a8SVineet Gupta is_linked = 1;
161e65ab5a8SVineet Gupta
162e65ab5a8SVineet Gupta fieldCisReg = 0;
163e65ab5a8SVineet Gupta op_format = BITS(state->words[0], 22, 23);
164e65ab5a8SVineet Gupta if (op_format == 0 || ((op_format == 3) &&
165e65ab5a8SVineet Gupta (!IS_BIT(state->words[0], 5)))) {
166e65ab5a8SVineet Gupta fieldC = FIELD_C(state->words[0]);
167e65ab5a8SVineet Gupta
168e65ab5a8SVineet Gupta if (fieldC == REG_LIMM) {
169e65ab5a8SVineet Gupta fieldC = state->words[1];
170e65ab5a8SVineet Gupta state->instr_len += 4;
171e65ab5a8SVineet Gupta } else {
172e65ab5a8SVineet Gupta fieldCisReg = 1;
173e65ab5a8SVineet Gupta }
174e65ab5a8SVineet Gupta } else if (op_format == 1 || ((op_format == 3)
175e65ab5a8SVineet Gupta && (IS_BIT(state->words[0], 5)))) {
176e65ab5a8SVineet Gupta fieldC = FIELD_C(state->words[0]);
177e65ab5a8SVineet Gupta } else {
178e65ab5a8SVineet Gupta /* op_format == 2 */
179e65ab5a8SVineet Gupta fieldC = FIELD_s12(state->words[0]);
180e65ab5a8SVineet Gupta }
181e65ab5a8SVineet Gupta
182e65ab5a8SVineet Gupta if (!fieldCisReg) {
183e65ab5a8SVineet Gupta state->target = fieldC;
184e65ab5a8SVineet Gupta state->flow = is_linked ?
185e65ab5a8SVineet Gupta direct_call : direct_jump;
186e65ab5a8SVineet Gupta } else {
187e65ab5a8SVineet Gupta state->target = get_reg(fieldC, regs, cregs);
188e65ab5a8SVineet Gupta state->flow = is_linked ?
189e65ab5a8SVineet Gupta indirect_call : indirect_jump;
190e65ab5a8SVineet Gupta }
191e65ab5a8SVineet Gupta state->is_branch = 1;
192e65ab5a8SVineet Gupta break;
193e65ab5a8SVineet Gupta
194e65ab5a8SVineet Gupta case 40: /* LPcc */
195e65ab5a8SVineet Gupta if (BITS(state->words[0], 22, 23) == 3) {
196e65ab5a8SVineet Gupta /* Conditional LPcc u7 */
197e65ab5a8SVineet Gupta fieldC = FIELD_C(state->words[0]);
198e65ab5a8SVineet Gupta
199e65ab5a8SVineet Gupta fieldC = fieldC << 1;
200e65ab5a8SVineet Gupta fieldC += (addr & ~0x03);
201e65ab5a8SVineet Gupta state->is_branch = 1;
202e65ab5a8SVineet Gupta state->flow = direct_jump;
203e65ab5a8SVineet Gupta state->target = fieldC;
204e65ab5a8SVineet Gupta }
205e65ab5a8SVineet Gupta /* For Unconditional lp, next pc is the fall through
206e65ab5a8SVineet Gupta * which is updated */
207e65ab5a8SVineet Gupta break;
208e65ab5a8SVineet Gupta
209e65ab5a8SVineet Gupta case 48 ... 55: /* LD a,[b,c] */
210e65ab5a8SVineet Gupta state->di = BITS(state->words[0], 15, 15);
211e65ab5a8SVineet Gupta if (state->di)
212e65ab5a8SVineet Gupta break;
213e65ab5a8SVineet Gupta state->x = BITS(state->words[0], 16, 16);
214e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 17, 18);
215e65ab5a8SVineet Gupta state->aa = BITS(state->words[0], 22, 23);
216e65ab5a8SVineet Gupta state->wb_reg = FIELD_B(state->words[0]);
217e65ab5a8SVineet Gupta if (state->wb_reg == REG_LIMM) {
218e65ab5a8SVineet Gupta state->instr_len += 4;
219e65ab5a8SVineet Gupta state->src1 = state->words[1];
220e65ab5a8SVineet Gupta } else {
221e65ab5a8SVineet Gupta state->src1 = get_reg(state->wb_reg, regs,
222e65ab5a8SVineet Gupta cregs);
223e65ab5a8SVineet Gupta }
224e65ab5a8SVineet Gupta state->src2 = FIELD_C(state->words[0]);
225e65ab5a8SVineet Gupta if (state->src2 == REG_LIMM) {
226e65ab5a8SVineet Gupta state->instr_len += 4;
227e65ab5a8SVineet Gupta state->src2 = state->words[1];
228e65ab5a8SVineet Gupta } else {
229e65ab5a8SVineet Gupta state->src2 = get_reg(state->src2, regs,
230e65ab5a8SVineet Gupta cregs);
231e65ab5a8SVineet Gupta }
232e65ab5a8SVineet Gupta state->dest = FIELD_A(state->words[0]);
233e65ab5a8SVineet Gupta if (state->dest == REG_LIMM)
234e65ab5a8SVineet Gupta state->pref = 1;
235e65ab5a8SVineet Gupta break;
236e65ab5a8SVineet Gupta
237e65ab5a8SVineet Gupta case 10: /* MOV */
238e65ab5a8SVineet Gupta /* still need to check for limm to extract instr len */
239e65ab5a8SVineet Gupta /* MOV is special case because it only takes 2 args */
240e65ab5a8SVineet Gupta switch (BITS(state->words[0], 22, 23)) {
241e65ab5a8SVineet Gupta case 0: /* OP a,b,c */
242e65ab5a8SVineet Gupta if (FIELD_C(state->words[0]) == REG_LIMM)
243e65ab5a8SVineet Gupta state->instr_len += 4;
244e65ab5a8SVineet Gupta break;
245e65ab5a8SVineet Gupta case 1: /* OP a,b,u6 */
246e65ab5a8SVineet Gupta break;
247e65ab5a8SVineet Gupta case 2: /* OP b,b,s12 */
248e65ab5a8SVineet Gupta break;
249e65ab5a8SVineet Gupta case 3: /* OP.cc b,b,c/u6 */
250e65ab5a8SVineet Gupta if ((!IS_BIT(state->words[0], 5)) &&
251e65ab5a8SVineet Gupta (FIELD_C(state->words[0]) == REG_LIMM))
252e65ab5a8SVineet Gupta state->instr_len += 4;
253e65ab5a8SVineet Gupta break;
254e65ab5a8SVineet Gupta }
255e65ab5a8SVineet Gupta break;
256e65ab5a8SVineet Gupta
257e65ab5a8SVineet Gupta
258e65ab5a8SVineet Gupta default:
259e65ab5a8SVineet Gupta /* Not a Load, Jump or Loop instruction */
260e65ab5a8SVineet Gupta /* still need to check for limm to extract instr len */
261e65ab5a8SVineet Gupta switch (BITS(state->words[0], 22, 23)) {
262e65ab5a8SVineet Gupta case 0: /* OP a,b,c */
263e65ab5a8SVineet Gupta if ((FIELD_B(state->words[0]) == REG_LIMM) ||
264e65ab5a8SVineet Gupta (FIELD_C(state->words[0]) == REG_LIMM))
265e65ab5a8SVineet Gupta state->instr_len += 4;
266e65ab5a8SVineet Gupta break;
267e65ab5a8SVineet Gupta case 1: /* OP a,b,u6 */
268e65ab5a8SVineet Gupta break;
269e65ab5a8SVineet Gupta case 2: /* OP b,b,s12 */
270e65ab5a8SVineet Gupta break;
271e65ab5a8SVineet Gupta case 3: /* OP.cc b,b,c/u6 */
272e65ab5a8SVineet Gupta if ((!IS_BIT(state->words[0], 5)) &&
273e65ab5a8SVineet Gupta ((FIELD_B(state->words[0]) == REG_LIMM) ||
274e65ab5a8SVineet Gupta (FIELD_C(state->words[0]) == REG_LIMM)))
275e65ab5a8SVineet Gupta state->instr_len += 4;
276e65ab5a8SVineet Gupta break;
277e65ab5a8SVineet Gupta }
278e65ab5a8SVineet Gupta break;
279e65ab5a8SVineet Gupta }
280e65ab5a8SVineet Gupta break;
281e65ab5a8SVineet Gupta
282e65ab5a8SVineet Gupta /* 16 Bit Instructions */
283e65ab5a8SVineet Gupta case op_LD_ADD: /* LD_S|LDB_S|LDW_S a,[b,c] */
284e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 3, 4);
285e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
286e65ab5a8SVineet Gupta state->src2 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
287e65ab5a8SVineet Gupta state->dest = FIELD_S_A(state->words[0]);
288e65ab5a8SVineet Gupta break;
289e65ab5a8SVineet Gupta
290e65ab5a8SVineet Gupta case op_ADD_MOV_CMP:
291e65ab5a8SVineet Gupta /* check for limm, ignore mov_s h,b (== mov_s 0,b) */
292e65ab5a8SVineet Gupta if ((BITS(state->words[0], 3, 4) < 3) &&
293e65ab5a8SVineet Gupta (FIELD_S_H(state->words[0]) == REG_LIMM))
294e65ab5a8SVineet Gupta state->instr_len += 4;
295e65ab5a8SVineet Gupta break;
296e65ab5a8SVineet Gupta
297e65ab5a8SVineet Gupta case op_S:
298e65ab5a8SVineet Gupta subopcode = BITS(state->words[0], 5, 7);
299e65ab5a8SVineet Gupta switch (subopcode) {
300e65ab5a8SVineet Gupta case 0: /* j_s */
301e65ab5a8SVineet Gupta case 1: /* j_s.d */
302e65ab5a8SVineet Gupta case 2: /* jl_s */
303e65ab5a8SVineet Gupta case 3: /* jl_s.d */
304e65ab5a8SVineet Gupta state->target = get_reg(FIELD_S_B(state->words[0]),
305e65ab5a8SVineet Gupta regs, cregs);
306e65ab5a8SVineet Gupta state->delay_slot = subopcode & 1;
307e65ab5a8SVineet Gupta state->flow = (subopcode >= 2) ?
308e65ab5a8SVineet Gupta direct_call : indirect_jump;
309e65ab5a8SVineet Gupta break;
310e65ab5a8SVineet Gupta case 7:
311e65ab5a8SVineet Gupta switch (BITS(state->words[0], 8, 10)) {
312e65ab5a8SVineet Gupta case 4: /* jeq_s [blink] */
313e65ab5a8SVineet Gupta case 5: /* jne_s [blink] */
314e65ab5a8SVineet Gupta case 6: /* j_s [blink] */
315e65ab5a8SVineet Gupta case 7: /* j_s.d [blink] */
316e65ab5a8SVineet Gupta state->delay_slot = (subopcode == 7);
317e65ab5a8SVineet Gupta state->flow = indirect_jump;
318e65ab5a8SVineet Gupta state->target = get_reg(31, regs, cregs);
319e65ab5a8SVineet Gupta default:
320e65ab5a8SVineet Gupta break;
321e65ab5a8SVineet Gupta }
322e65ab5a8SVineet Gupta default:
323e65ab5a8SVineet Gupta break;
324e65ab5a8SVineet Gupta }
325e65ab5a8SVineet Gupta break;
326e65ab5a8SVineet Gupta
327e65ab5a8SVineet Gupta case op_LD_S: /* LD_S c, [b, u7] */
328e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
329e65ab5a8SVineet Gupta state->src2 = FIELD_S_u7(state->words[0]);
330e65ab5a8SVineet Gupta state->dest = FIELD_S_C(state->words[0]);
331e65ab5a8SVineet Gupta break;
332e65ab5a8SVineet Gupta
333e65ab5a8SVineet Gupta case op_LDB_S:
334e65ab5a8SVineet Gupta case op_STB_S:
335e65ab5a8SVineet Gupta /* no further handling required as byte accesses should not
336e65ab5a8SVineet Gupta * cause an unaligned access exception */
337e65ab5a8SVineet Gupta state->zz = 1;
338e65ab5a8SVineet Gupta break;
339e65ab5a8SVineet Gupta
340e65ab5a8SVineet Gupta case op_LDWX_S: /* LDWX_S c, [b, u6] */
341e65ab5a8SVineet Gupta state->x = 1;
342df561f66SGustavo A. R. Silva fallthrough;
343e65ab5a8SVineet Gupta
344e65ab5a8SVineet Gupta case op_LDW_S: /* LDW_S c, [b, u6] */
345e65ab5a8SVineet Gupta state->zz = 2;
346e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
347e65ab5a8SVineet Gupta state->src2 = FIELD_S_u6(state->words[0]);
348e65ab5a8SVineet Gupta state->dest = FIELD_S_C(state->words[0]);
349e65ab5a8SVineet Gupta break;
350e65ab5a8SVineet Gupta
351e65ab5a8SVineet Gupta case op_ST_S: /* ST_S c, [b, u7] */
352e65ab5a8SVineet Gupta state->write = 1;
353e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
354e65ab5a8SVineet Gupta state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
355e65ab5a8SVineet Gupta state->src3 = FIELD_S_u7(state->words[0]);
356e65ab5a8SVineet Gupta break;
357e65ab5a8SVineet Gupta
358e65ab5a8SVineet Gupta case op_STW_S: /* STW_S c,[b,u6] */
359e65ab5a8SVineet Gupta state->write = 1;
360e65ab5a8SVineet Gupta state->zz = 2;
361e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_C(state->words[0]), regs, cregs);
362e65ab5a8SVineet Gupta state->src2 = get_reg(FIELD_S_B(state->words[0]), regs, cregs);
363e65ab5a8SVineet Gupta state->src3 = FIELD_S_u6(state->words[0]);
364e65ab5a8SVineet Gupta break;
365e65ab5a8SVineet Gupta
366e65ab5a8SVineet Gupta case op_SP: /* LD_S|LDB_S b,[sp,u7], ST_S|STB_S b,[sp,u7] */
367e65ab5a8SVineet Gupta /* note: we are ignoring possibility of:
368e65ab5a8SVineet Gupta * ADD_S, SUB_S, PUSH_S, POP_S as these should not
369ecaa054fSJulia Lawall * cause unaligned exception anyway */
370e65ab5a8SVineet Gupta state->write = BITS(state->words[0], 6, 6);
371e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 5, 5);
372e65ab5a8SVineet Gupta if (state->zz)
373e65ab5a8SVineet Gupta break; /* byte accesses should not come here */
374e65ab5a8SVineet Gupta if (!state->write) {
375e65ab5a8SVineet Gupta state->src1 = get_reg(28, regs, cregs);
376e65ab5a8SVineet Gupta state->src2 = FIELD_S_u7(state->words[0]);
377e65ab5a8SVineet Gupta state->dest = FIELD_S_B(state->words[0]);
378e65ab5a8SVineet Gupta } else {
379e65ab5a8SVineet Gupta state->src1 = get_reg(FIELD_S_B(state->words[0]), regs,
380e65ab5a8SVineet Gupta cregs);
381e65ab5a8SVineet Gupta state->src2 = get_reg(28, regs, cregs);
382e65ab5a8SVineet Gupta state->src3 = FIELD_S_u7(state->words[0]);
383e65ab5a8SVineet Gupta }
384e65ab5a8SVineet Gupta break;
385e65ab5a8SVineet Gupta
386e65ab5a8SVineet Gupta case op_GP: /* LD_S|LDB_S|LDW_S r0,[gp,s11/s9/s10] */
387e65ab5a8SVineet Gupta /* note: ADD_S r0, gp, s11 is ignored */
388e65ab5a8SVineet Gupta state->zz = BITS(state->words[0], 9, 10);
389e65ab5a8SVineet Gupta state->src1 = get_reg(26, regs, cregs);
390e65ab5a8SVineet Gupta state->src2 = state->zz ? FIELD_S_s10(state->words[0]) :
391e65ab5a8SVineet Gupta FIELD_S_s11(state->words[0]);
392e65ab5a8SVineet Gupta state->dest = 0;
393e65ab5a8SVineet Gupta break;
394e65ab5a8SVineet Gupta
395e65ab5a8SVineet Gupta case op_Pcl: /* LD_S b,[pcl,u10] */
396e65ab5a8SVineet Gupta state->src1 = regs->ret & ~3;
397e65ab5a8SVineet Gupta state->src2 = FIELD_S_u10(state->words[0]);
398e65ab5a8SVineet Gupta state->dest = FIELD_S_B(state->words[0]);
399e65ab5a8SVineet Gupta break;
400e65ab5a8SVineet Gupta
401e65ab5a8SVineet Gupta case op_BR_S:
402e65ab5a8SVineet Gupta state->target = FIELD_S_s8(state->words[0]) + (addr & ~0x03);
403e65ab5a8SVineet Gupta state->flow = direct_jump;
404e65ab5a8SVineet Gupta state->is_branch = 1;
405e65ab5a8SVineet Gupta break;
406e65ab5a8SVineet Gupta
407e65ab5a8SVineet Gupta case op_B_S:
408e65ab5a8SVineet Gupta fieldA = (BITS(state->words[0], 9, 10) == 3) ?
409e65ab5a8SVineet Gupta FIELD_S_s7(state->words[0]) :
410e65ab5a8SVineet Gupta FIELD_S_s10(state->words[0]);
411e65ab5a8SVineet Gupta state->target = fieldA + (addr & ~0x03);
412e65ab5a8SVineet Gupta state->flow = direct_jump;
413e65ab5a8SVineet Gupta state->is_branch = 1;
414e65ab5a8SVineet Gupta break;
415e65ab5a8SVineet Gupta
416e65ab5a8SVineet Gupta case op_BL_S:
417e65ab5a8SVineet Gupta state->target = FIELD_S_s13(state->words[0]) + (addr & ~0x03);
418e65ab5a8SVineet Gupta state->flow = direct_call;
419e65ab5a8SVineet Gupta state->is_branch = 1;
420e65ab5a8SVineet Gupta break;
421e65ab5a8SVineet Gupta
422e65ab5a8SVineet Gupta default:
423e65ab5a8SVineet Gupta break;
424e65ab5a8SVineet Gupta }
425e65ab5a8SVineet Gupta
426e65ab5a8SVineet Gupta if (bytes_not_copied <= (8 - state->instr_len))
427e65ab5a8SVineet Gupta return;
428e65ab5a8SVineet Gupta
429e65ab5a8SVineet Gupta fault: state->fault = 1;
430e65ab5a8SVineet Gupta }
431e65ab5a8SVineet Gupta
get_reg(int reg,struct pt_regs * regs,struct callee_regs * cregs)432e65ab5a8SVineet Gupta long __kprobes get_reg(int reg, struct pt_regs *regs,
433e65ab5a8SVineet Gupta struct callee_regs *cregs)
434e65ab5a8SVineet Gupta {
435e65ab5a8SVineet Gupta long *p;
436e65ab5a8SVineet Gupta
437*9a78a8a8SSergey Matyukevich #if defined(CONFIG_ISA_ARCOMPACT)
438e65ab5a8SVineet Gupta if (reg <= 12) {
439e65ab5a8SVineet Gupta p = ®s->r0;
440e65ab5a8SVineet Gupta return p[-reg];
441e65ab5a8SVineet Gupta }
442*9a78a8a8SSergey Matyukevich #else /* CONFIG_ISA_ARCV2 */
443*9a78a8a8SSergey Matyukevich if (reg <= 11) {
444*9a78a8a8SSergey Matyukevich p = ®s->r0;
445*9a78a8a8SSergey Matyukevich return p[reg];
446*9a78a8a8SSergey Matyukevich }
447e65ab5a8SVineet Gupta
448*9a78a8a8SSergey Matyukevich if (reg == 12)
449*9a78a8a8SSergey Matyukevich return regs->r12;
450*9a78a8a8SSergey Matyukevich if (reg == 30)
451*9a78a8a8SSergey Matyukevich return regs->r30;
452*9a78a8a8SSergey Matyukevich #ifdef CONFIG_ARC_HAS_ACCL_REGS
453*9a78a8a8SSergey Matyukevich if (reg == 58)
454*9a78a8a8SSergey Matyukevich return regs->r58;
455*9a78a8a8SSergey Matyukevich if (reg == 59)
456*9a78a8a8SSergey Matyukevich return regs->r59;
457*9a78a8a8SSergey Matyukevich #endif
458*9a78a8a8SSergey Matyukevich #endif
459e65ab5a8SVineet Gupta if (cregs && (reg <= 25)) {
460e65ab5a8SVineet Gupta p = &cregs->r13;
461e65ab5a8SVineet Gupta return p[13 - reg];
462e65ab5a8SVineet Gupta }
463e65ab5a8SVineet Gupta
464e65ab5a8SVineet Gupta if (reg == 26)
465e65ab5a8SVineet Gupta return regs->r26;
466e65ab5a8SVineet Gupta if (reg == 27)
467e65ab5a8SVineet Gupta return regs->fp;
468e65ab5a8SVineet Gupta if (reg == 28)
469e65ab5a8SVineet Gupta return regs->sp;
470e65ab5a8SVineet Gupta if (reg == 31)
471e65ab5a8SVineet Gupta return regs->blink;
472e65ab5a8SVineet Gupta
473e65ab5a8SVineet Gupta return 0;
474e65ab5a8SVineet Gupta }
475e65ab5a8SVineet Gupta
set_reg(int reg,long val,struct pt_regs * regs,struct callee_regs * cregs)476e65ab5a8SVineet Gupta void __kprobes set_reg(int reg, long val, struct pt_regs *regs,
477e65ab5a8SVineet Gupta struct callee_regs *cregs)
478e65ab5a8SVineet Gupta {
479e65ab5a8SVineet Gupta long *p;
480e65ab5a8SVineet Gupta
481*9a78a8a8SSergey Matyukevich #if defined(CONFIG_ISA_ARCOMPACT)
482e65ab5a8SVineet Gupta switch (reg) {
483e65ab5a8SVineet Gupta case 0 ... 12:
484e65ab5a8SVineet Gupta p = ®s->r0;
485e65ab5a8SVineet Gupta p[-reg] = val;
486e65ab5a8SVineet Gupta break;
487e65ab5a8SVineet Gupta case 13 ... 25:
488e65ab5a8SVineet Gupta if (cregs) {
489e65ab5a8SVineet Gupta p = &cregs->r13;
490e65ab5a8SVineet Gupta p[13 - reg] = val;
491e65ab5a8SVineet Gupta }
492e65ab5a8SVineet Gupta break;
493e65ab5a8SVineet Gupta case 26:
494e65ab5a8SVineet Gupta regs->r26 = val;
495e65ab5a8SVineet Gupta break;
496e65ab5a8SVineet Gupta case 27:
497e65ab5a8SVineet Gupta regs->fp = val;
498e65ab5a8SVineet Gupta break;
499e65ab5a8SVineet Gupta case 28:
500e65ab5a8SVineet Gupta regs->sp = val;
501e65ab5a8SVineet Gupta break;
502e65ab5a8SVineet Gupta case 31:
503e65ab5a8SVineet Gupta regs->blink = val;
504e65ab5a8SVineet Gupta break;
505e65ab5a8SVineet Gupta default:
506e65ab5a8SVineet Gupta break;
507e65ab5a8SVineet Gupta }
508*9a78a8a8SSergey Matyukevich #else /* CONFIG_ISA_ARCV2 */
509*9a78a8a8SSergey Matyukevich switch (reg) {
510*9a78a8a8SSergey Matyukevich case 0 ... 11:
511*9a78a8a8SSergey Matyukevich p = ®s->r0;
512*9a78a8a8SSergey Matyukevich p[reg] = val;
513*9a78a8a8SSergey Matyukevich break;
514*9a78a8a8SSergey Matyukevich case 12:
515*9a78a8a8SSergey Matyukevich regs->r12 = val;
516*9a78a8a8SSergey Matyukevich break;
517*9a78a8a8SSergey Matyukevich case 13 ... 25:
518*9a78a8a8SSergey Matyukevich if (cregs) {
519*9a78a8a8SSergey Matyukevich p = &cregs->r13;
520*9a78a8a8SSergey Matyukevich p[13 - reg] = val;
521*9a78a8a8SSergey Matyukevich }
522*9a78a8a8SSergey Matyukevich break;
523*9a78a8a8SSergey Matyukevich case 26:
524*9a78a8a8SSergey Matyukevich regs->r26 = val;
525*9a78a8a8SSergey Matyukevich break;
526*9a78a8a8SSergey Matyukevich case 27:
527*9a78a8a8SSergey Matyukevich regs->fp = val;
528*9a78a8a8SSergey Matyukevich break;
529*9a78a8a8SSergey Matyukevich case 28:
530*9a78a8a8SSergey Matyukevich regs->sp = val;
531*9a78a8a8SSergey Matyukevich break;
532*9a78a8a8SSergey Matyukevich case 30:
533*9a78a8a8SSergey Matyukevich regs->r30 = val;
534*9a78a8a8SSergey Matyukevich break;
535*9a78a8a8SSergey Matyukevich case 31:
536*9a78a8a8SSergey Matyukevich regs->blink = val;
537*9a78a8a8SSergey Matyukevich break;
538*9a78a8a8SSergey Matyukevich #ifdef CONFIG_ARC_HAS_ACCL_REGS
539*9a78a8a8SSergey Matyukevich case 58:
540*9a78a8a8SSergey Matyukevich regs->r58 = val;
541*9a78a8a8SSergey Matyukevich break;
542*9a78a8a8SSergey Matyukevich case 59:
543*9a78a8a8SSergey Matyukevich regs->r59 = val;
544*9a78a8a8SSergey Matyukevich break;
545*9a78a8a8SSergey Matyukevich #endif
546*9a78a8a8SSergey Matyukevich default:
547*9a78a8a8SSergey Matyukevich break;
548*9a78a8a8SSergey Matyukevich }
549*9a78a8a8SSergey Matyukevich #endif
550e65ab5a8SVineet Gupta }
551e65ab5a8SVineet Gupta
552e65ab5a8SVineet Gupta /*
553e65ab5a8SVineet Gupta * Disassembles the insn at @pc and sets @next_pc to next PC (which could be
554e65ab5a8SVineet Gupta * @pc +2/4/6 (ARCompact ISA allows free intermixing of 16/32 bit insns).
555e65ab5a8SVineet Gupta *
556e65ab5a8SVineet Gupta * If @pc is a branch
557e65ab5a8SVineet Gupta * -@tgt_if_br is set to branch target.
558e65ab5a8SVineet Gupta * -If branch has delay slot, @next_pc updated with actual next PC.
559e65ab5a8SVineet Gupta */
disasm_next_pc(unsigned long pc,struct pt_regs * regs,struct callee_regs * cregs,unsigned long * next_pc,unsigned long * tgt_if_br)560e65ab5a8SVineet Gupta int __kprobes disasm_next_pc(unsigned long pc, struct pt_regs *regs,
561e65ab5a8SVineet Gupta struct callee_regs *cregs,
562e65ab5a8SVineet Gupta unsigned long *next_pc, unsigned long *tgt_if_br)
563e65ab5a8SVineet Gupta {
564e65ab5a8SVineet Gupta struct disasm_state instr;
565e65ab5a8SVineet Gupta
566e65ab5a8SVineet Gupta disasm_instr(pc, &instr, 0, regs, cregs);
567e65ab5a8SVineet Gupta
568e65ab5a8SVineet Gupta *next_pc = pc + instr.instr_len;
569e65ab5a8SVineet Gupta
570e65ab5a8SVineet Gupta /* Instruction with possible two targets branch, jump and loop */
571e65ab5a8SVineet Gupta if (instr.is_branch)
572e65ab5a8SVineet Gupta *tgt_if_br = instr.target;
573e65ab5a8SVineet Gupta
574e65ab5a8SVineet Gupta /* For the instructions with delay slots, the fall through is the
575e65ab5a8SVineet Gupta * instruction following the instruction in delay slot.
576e65ab5a8SVineet Gupta */
577e65ab5a8SVineet Gupta if (instr.delay_slot) {
578e65ab5a8SVineet Gupta struct disasm_state instr_d;
579e65ab5a8SVineet Gupta
580e65ab5a8SVineet Gupta disasm_instr(*next_pc, &instr_d, 0, regs, cregs);
581e65ab5a8SVineet Gupta
582e65ab5a8SVineet Gupta *next_pc += instr_d.instr_len;
583e65ab5a8SVineet Gupta }
584e65ab5a8SVineet Gupta
585e65ab5a8SVineet Gupta /* Zero Overhead Loop - end of the loop */
586e65ab5a8SVineet Gupta if (!(regs->status32 & STATUS32_L) && (*next_pc == regs->lp_end)
587e65ab5a8SVineet Gupta && (regs->lp_count > 1)) {
588e65ab5a8SVineet Gupta *next_pc = regs->lp_start;
589e65ab5a8SVineet Gupta }
590e65ab5a8SVineet Gupta
591e65ab5a8SVineet Gupta return instr.is_branch;
592e65ab5a8SVineet Gupta }
593e65ab5a8SVineet Gupta
5941736a56fSVineet Gupta #endif /* CONFIG_KGDB || CONFIG_ARC_EMUL_UNALIGNED || CONFIG_KPROBES */
595