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