xref: /OK3568_Linux_fs/kernel/arch/arm/probes/decode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * arch/arm/probes/decode.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Some contents moved here from arch/arm/include/asm/kprobes-arm.c which is
8*4882a593Smuzhiyun  * Copyright (C) 2006, 2007 Motorola Inc.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <asm/system_info.h>
14*4882a593Smuzhiyun #include <asm/ptrace.h>
15*4882a593Smuzhiyun #include <linux/bug.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "decode.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #ifndef find_str_pc_offset
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * For STR and STM instructions, an ARM core may choose to use either
24*4882a593Smuzhiyun  * a +8 or a +12 displacement from the current instruction's address.
25*4882a593Smuzhiyun  * Whichever value is chosen for a given core, it must be the same for
26*4882a593Smuzhiyun  * both instructions and may not change.  This function measures it.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun int str_pc_offset;
30*4882a593Smuzhiyun 
find_str_pc_offset(void)31*4882a593Smuzhiyun void __init find_str_pc_offset(void)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	int addr, scratch, ret;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	__asm__ (
36*4882a593Smuzhiyun 		"sub	%[ret], pc, #4		\n\t"
37*4882a593Smuzhiyun 		"str	pc, %[addr]		\n\t"
38*4882a593Smuzhiyun 		"ldr	%[scr], %[addr]		\n\t"
39*4882a593Smuzhiyun 		"sub	%[ret], %[scr], %[ret]	\n\t"
40*4882a593Smuzhiyun 		: [ret] "=r" (ret), [scr] "=r" (scratch), [addr] "+m" (addr));
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	str_pc_offset = ret;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #endif /* !find_str_pc_offset */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #ifndef test_load_write_pc_interworking
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun bool load_write_pc_interworks;
51*4882a593Smuzhiyun 
test_load_write_pc_interworking(void)52*4882a593Smuzhiyun void __init test_load_write_pc_interworking(void)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	int arch = cpu_architecture();
55*4882a593Smuzhiyun 	BUG_ON(arch == CPU_ARCH_UNKNOWN);
56*4882a593Smuzhiyun 	load_write_pc_interworks = arch >= CPU_ARCH_ARMv5T;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #endif /* !test_load_write_pc_interworking */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #ifndef test_alu_write_pc_interworking
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun bool alu_write_pc_interworks;
65*4882a593Smuzhiyun 
test_alu_write_pc_interworking(void)66*4882a593Smuzhiyun void __init test_alu_write_pc_interworking(void)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	int arch = cpu_architecture();
69*4882a593Smuzhiyun 	BUG_ON(arch == CPU_ARCH_UNKNOWN);
70*4882a593Smuzhiyun 	alu_write_pc_interworks = arch >= CPU_ARCH_ARMv7;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #endif /* !test_alu_write_pc_interworking */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 
arm_probes_decode_init(void)76*4882a593Smuzhiyun void __init arm_probes_decode_init(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	find_str_pc_offset();
79*4882a593Smuzhiyun 	test_load_write_pc_interworking();
80*4882a593Smuzhiyun 	test_alu_write_pc_interworking();
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 
__check_eq(unsigned long cpsr)84*4882a593Smuzhiyun static unsigned long __kprobes __check_eq(unsigned long cpsr)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	return cpsr & PSR_Z_BIT;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
__check_ne(unsigned long cpsr)89*4882a593Smuzhiyun static unsigned long __kprobes __check_ne(unsigned long cpsr)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	return (~cpsr) & PSR_Z_BIT;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
__check_cs(unsigned long cpsr)94*4882a593Smuzhiyun static unsigned long __kprobes __check_cs(unsigned long cpsr)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	return cpsr & PSR_C_BIT;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
__check_cc(unsigned long cpsr)99*4882a593Smuzhiyun static unsigned long __kprobes __check_cc(unsigned long cpsr)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	return (~cpsr) & PSR_C_BIT;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
__check_mi(unsigned long cpsr)104*4882a593Smuzhiyun static unsigned long __kprobes __check_mi(unsigned long cpsr)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	return cpsr & PSR_N_BIT;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
__check_pl(unsigned long cpsr)109*4882a593Smuzhiyun static unsigned long __kprobes __check_pl(unsigned long cpsr)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	return (~cpsr) & PSR_N_BIT;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
__check_vs(unsigned long cpsr)114*4882a593Smuzhiyun static unsigned long __kprobes __check_vs(unsigned long cpsr)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	return cpsr & PSR_V_BIT;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
__check_vc(unsigned long cpsr)119*4882a593Smuzhiyun static unsigned long __kprobes __check_vc(unsigned long cpsr)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	return (~cpsr) & PSR_V_BIT;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
__check_hi(unsigned long cpsr)124*4882a593Smuzhiyun static unsigned long __kprobes __check_hi(unsigned long cpsr)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
127*4882a593Smuzhiyun 	return cpsr & PSR_C_BIT;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
__check_ls(unsigned long cpsr)130*4882a593Smuzhiyun static unsigned long __kprobes __check_ls(unsigned long cpsr)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	cpsr &= ~(cpsr >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
133*4882a593Smuzhiyun 	return (~cpsr) & PSR_C_BIT;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
__check_ge(unsigned long cpsr)136*4882a593Smuzhiyun static unsigned long __kprobes __check_ge(unsigned long cpsr)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
139*4882a593Smuzhiyun 	return (~cpsr) & PSR_N_BIT;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
__check_lt(unsigned long cpsr)142*4882a593Smuzhiyun static unsigned long __kprobes __check_lt(unsigned long cpsr)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	cpsr ^= (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
145*4882a593Smuzhiyun 	return cpsr & PSR_N_BIT;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
__check_gt(unsigned long cpsr)148*4882a593Smuzhiyun static unsigned long __kprobes __check_gt(unsigned long cpsr)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
151*4882a593Smuzhiyun 	temp |= (cpsr << 1);			 /* PSR_N_BIT |= PSR_Z_BIT */
152*4882a593Smuzhiyun 	return (~temp) & PSR_N_BIT;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
__check_le(unsigned long cpsr)155*4882a593Smuzhiyun static unsigned long __kprobes __check_le(unsigned long cpsr)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	unsigned long temp = cpsr ^ (cpsr << 3); /* PSR_N_BIT ^= PSR_V_BIT */
158*4882a593Smuzhiyun 	temp |= (cpsr << 1);			 /* PSR_N_BIT |= PSR_Z_BIT */
159*4882a593Smuzhiyun 	return temp & PSR_N_BIT;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
__check_al(unsigned long cpsr)162*4882a593Smuzhiyun static unsigned long __kprobes __check_al(unsigned long cpsr)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	return true;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun probes_check_cc * const probes_condition_checks[16] = {
168*4882a593Smuzhiyun 	&__check_eq, &__check_ne, &__check_cs, &__check_cc,
169*4882a593Smuzhiyun 	&__check_mi, &__check_pl, &__check_vs, &__check_vc,
170*4882a593Smuzhiyun 	&__check_hi, &__check_ls, &__check_ge, &__check_lt,
171*4882a593Smuzhiyun 	&__check_gt, &__check_le, &__check_al, &__check_al
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 
probes_simulate_nop(probes_opcode_t opcode,struct arch_probes_insn * asi,struct pt_regs * regs)175*4882a593Smuzhiyun void __kprobes probes_simulate_nop(probes_opcode_t opcode,
176*4882a593Smuzhiyun 	struct arch_probes_insn *asi,
177*4882a593Smuzhiyun 	struct pt_regs *regs)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
probes_emulate_none(probes_opcode_t opcode,struct arch_probes_insn * asi,struct pt_regs * regs)181*4882a593Smuzhiyun void __kprobes probes_emulate_none(probes_opcode_t opcode,
182*4882a593Smuzhiyun 	struct arch_probes_insn *asi,
183*4882a593Smuzhiyun 	struct pt_regs *regs)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	asi->insn_fn();
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun  * Prepare an instruction slot to receive an instruction for emulating.
190*4882a593Smuzhiyun  * This is done by placing a subroutine return after the location where the
191*4882a593Smuzhiyun  * instruction will be placed. We also modify ARM instructions to be
192*4882a593Smuzhiyun  * unconditional as the condition code will already be checked before any
193*4882a593Smuzhiyun  * emulation handler is called.
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun static probes_opcode_t __kprobes
prepare_emulated_insn(probes_opcode_t insn,struct arch_probes_insn * asi,bool thumb)196*4882a593Smuzhiyun prepare_emulated_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
197*4882a593Smuzhiyun 		      bool thumb)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun #ifdef CONFIG_THUMB2_KERNEL
200*4882a593Smuzhiyun 	if (thumb) {
201*4882a593Smuzhiyun 		u16 *thumb_insn = (u16 *)asi->insn;
202*4882a593Smuzhiyun 		/* Thumb bx lr */
203*4882a593Smuzhiyun 		thumb_insn[1] = __opcode_to_mem_thumb16(0x4770);
204*4882a593Smuzhiyun 		thumb_insn[2] = __opcode_to_mem_thumb16(0x4770);
205*4882a593Smuzhiyun 		return insn;
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 	asi->insn[1] = __opcode_to_mem_arm(0xe12fff1e); /* ARM bx lr */
208*4882a593Smuzhiyun #else
209*4882a593Smuzhiyun 	asi->insn[1] = __opcode_to_mem_arm(0xe1a0f00e); /* mov pc, lr */
210*4882a593Smuzhiyun #endif
211*4882a593Smuzhiyun 	/* Make an ARM instruction unconditional */
212*4882a593Smuzhiyun 	if (insn < 0xe0000000)
213*4882a593Smuzhiyun 		insn = (insn | 0xe0000000) & ~0x10000000;
214*4882a593Smuzhiyun 	return insn;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun  * Write a (probably modified) instruction into the slot previously prepared by
219*4882a593Smuzhiyun  * prepare_emulated_insn
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun static void  __kprobes
set_emulated_insn(probes_opcode_t insn,struct arch_probes_insn * asi,bool thumb)222*4882a593Smuzhiyun set_emulated_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
223*4882a593Smuzhiyun 		  bool thumb)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun #ifdef CONFIG_THUMB2_KERNEL
226*4882a593Smuzhiyun 	if (thumb) {
227*4882a593Smuzhiyun 		u16 *ip = (u16 *)asi->insn;
228*4882a593Smuzhiyun 		if (is_wide_instruction(insn))
229*4882a593Smuzhiyun 			*ip++ = __opcode_to_mem_thumb16(insn >> 16);
230*4882a593Smuzhiyun 		*ip++ = __opcode_to_mem_thumb16(insn);
231*4882a593Smuzhiyun 		return;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun #endif
234*4882a593Smuzhiyun 	asi->insn[0] = __opcode_to_mem_arm(insn);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun  * When we modify the register numbers encoded in an instruction to be emulated,
239*4882a593Smuzhiyun  * the new values come from this define. For ARM and 32-bit Thumb instructions
240*4882a593Smuzhiyun  * this gives...
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  *	bit position	  16  12   8   4   0
243*4882a593Smuzhiyun  *	---------------+---+---+---+---+---+
244*4882a593Smuzhiyun  *	register	 r2  r0  r1  --  r3
245*4882a593Smuzhiyun  */
246*4882a593Smuzhiyun #define INSN_NEW_BITS		0x00020103
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /* Each nibble has same value as that at INSN_NEW_BITS bit 16 */
249*4882a593Smuzhiyun #define INSN_SAMEAS16_BITS	0x22222222
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun  * Validate and modify each of the registers encoded in an instruction.
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * Each nibble in regs contains a value from enum decode_reg_type. For each
255*4882a593Smuzhiyun  * non-zero value, the corresponding nibble in pinsn is validated and modified
256*4882a593Smuzhiyun  * according to the type.
257*4882a593Smuzhiyun  */
decode_regs(probes_opcode_t * pinsn,u32 regs,bool modify)258*4882a593Smuzhiyun static bool __kprobes decode_regs(probes_opcode_t *pinsn, u32 regs, bool modify)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	probes_opcode_t insn = *pinsn;
261*4882a593Smuzhiyun 	probes_opcode_t mask = 0xf; /* Start at least significant nibble */
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	for (; regs != 0; regs >>= 4, mask <<= 4) {
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		probes_opcode_t new_bits = INSN_NEW_BITS;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		switch (regs & 0xf) {
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		case REG_TYPE_NONE:
270*4882a593Smuzhiyun 			/* Nibble not a register, skip to next */
271*4882a593Smuzhiyun 			continue;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		case REG_TYPE_ANY:
274*4882a593Smuzhiyun 			/* Any register is allowed */
275*4882a593Smuzhiyun 			break;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		case REG_TYPE_SAMEAS16:
278*4882a593Smuzhiyun 			/* Replace register with same as at bit position 16 */
279*4882a593Smuzhiyun 			new_bits = INSN_SAMEAS16_BITS;
280*4882a593Smuzhiyun 			break;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 		case REG_TYPE_SP:
283*4882a593Smuzhiyun 			/* Only allow SP (R13) */
284*4882a593Smuzhiyun 			if ((insn ^ 0xdddddddd) & mask)
285*4882a593Smuzhiyun 				goto reject;
286*4882a593Smuzhiyun 			break;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		case REG_TYPE_PC:
289*4882a593Smuzhiyun 			/* Only allow PC (R15) */
290*4882a593Smuzhiyun 			if ((insn ^ 0xffffffff) & mask)
291*4882a593Smuzhiyun 				goto reject;
292*4882a593Smuzhiyun 			break;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		case REG_TYPE_NOSP:
295*4882a593Smuzhiyun 			/* Reject SP (R13) */
296*4882a593Smuzhiyun 			if (((insn ^ 0xdddddddd) & mask) == 0)
297*4882a593Smuzhiyun 				goto reject;
298*4882a593Smuzhiyun 			break;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		case REG_TYPE_NOSPPC:
301*4882a593Smuzhiyun 		case REG_TYPE_NOSPPCX:
302*4882a593Smuzhiyun 			/* Reject SP and PC (R13 and R15) */
303*4882a593Smuzhiyun 			if (((insn ^ 0xdddddddd) & 0xdddddddd & mask) == 0)
304*4882a593Smuzhiyun 				goto reject;
305*4882a593Smuzhiyun 			break;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 		case REG_TYPE_NOPCWB:
308*4882a593Smuzhiyun 			if (!is_writeback(insn))
309*4882a593Smuzhiyun 				break; /* No writeback, so any register is OK */
310*4882a593Smuzhiyun 			fallthrough;
311*4882a593Smuzhiyun 		case REG_TYPE_NOPC:
312*4882a593Smuzhiyun 		case REG_TYPE_NOPCX:
313*4882a593Smuzhiyun 			/* Reject PC (R15) */
314*4882a593Smuzhiyun 			if (((insn ^ 0xffffffff) & mask) == 0)
315*4882a593Smuzhiyun 				goto reject;
316*4882a593Smuzhiyun 			break;
317*4882a593Smuzhiyun 		}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 		/* Replace value of nibble with new register number... */
320*4882a593Smuzhiyun 		insn &= ~mask;
321*4882a593Smuzhiyun 		insn |= new_bits & mask;
322*4882a593Smuzhiyun 	}
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (modify)
325*4882a593Smuzhiyun 		*pinsn = insn;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	return true;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun reject:
330*4882a593Smuzhiyun 	return false;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
334*4882a593Smuzhiyun 	[DECODE_TYPE_TABLE]	= sizeof(struct decode_table),
335*4882a593Smuzhiyun 	[DECODE_TYPE_CUSTOM]	= sizeof(struct decode_custom),
336*4882a593Smuzhiyun 	[DECODE_TYPE_SIMULATE]	= sizeof(struct decode_simulate),
337*4882a593Smuzhiyun 	[DECODE_TYPE_EMULATE]	= sizeof(struct decode_emulate),
338*4882a593Smuzhiyun 	[DECODE_TYPE_OR]	= sizeof(struct decode_or),
339*4882a593Smuzhiyun 	[DECODE_TYPE_REJECT]	= sizeof(struct decode_reject)
340*4882a593Smuzhiyun };
341*4882a593Smuzhiyun 
run_checkers(const struct decode_checker * checkers[],int action,probes_opcode_t insn,struct arch_probes_insn * asi,const struct decode_header * h)342*4882a593Smuzhiyun static int run_checkers(const struct decode_checker *checkers[],
343*4882a593Smuzhiyun 		int action, probes_opcode_t insn,
344*4882a593Smuzhiyun 		struct arch_probes_insn *asi,
345*4882a593Smuzhiyun 		const struct decode_header *h)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	const struct decode_checker **p;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	if (!checkers)
350*4882a593Smuzhiyun 		return INSN_GOOD;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	p = checkers;
353*4882a593Smuzhiyun 	while (*p != NULL) {
354*4882a593Smuzhiyun 		int retval;
355*4882a593Smuzhiyun 		probes_check_t *checker_func = (*p)[action].checker;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		retval = INSN_GOOD;
358*4882a593Smuzhiyun 		if (checker_func)
359*4882a593Smuzhiyun 			retval = checker_func(insn, asi, h);
360*4882a593Smuzhiyun 		if (retval == INSN_REJECTED)
361*4882a593Smuzhiyun 			return retval;
362*4882a593Smuzhiyun 		p++;
363*4882a593Smuzhiyun 	}
364*4882a593Smuzhiyun 	return INSN_GOOD;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun  * probes_decode_insn operates on data tables in order to decode an ARM
369*4882a593Smuzhiyun  * architecture instruction onto which a kprobe has been placed.
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * These instruction decoding tables are a concatenation of entries each
372*4882a593Smuzhiyun  * of which consist of one of the following structs:
373*4882a593Smuzhiyun  *
374*4882a593Smuzhiyun  *	decode_table
375*4882a593Smuzhiyun  *	decode_custom
376*4882a593Smuzhiyun  *	decode_simulate
377*4882a593Smuzhiyun  *	decode_emulate
378*4882a593Smuzhiyun  *	decode_or
379*4882a593Smuzhiyun  *	decode_reject
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * Each of these starts with a struct decode_header which has the following
382*4882a593Smuzhiyun  * fields:
383*4882a593Smuzhiyun  *
384*4882a593Smuzhiyun  *	type_regs
385*4882a593Smuzhiyun  *	mask
386*4882a593Smuzhiyun  *	value
387*4882a593Smuzhiyun  *
388*4882a593Smuzhiyun  * The least significant DECODE_TYPE_BITS of type_regs contains a value
389*4882a593Smuzhiyun  * from enum decode_type, this indicates which of the decode_* structs
390*4882a593Smuzhiyun  * the entry contains. The value DECODE_TYPE_END indicates the end of the
391*4882a593Smuzhiyun  * table.
392*4882a593Smuzhiyun  *
393*4882a593Smuzhiyun  * When the table is parsed, each entry is checked in turn to see if it
394*4882a593Smuzhiyun  * matches the instruction to be decoded using the test:
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  *	(insn & mask) == value
397*4882a593Smuzhiyun  *
398*4882a593Smuzhiyun  * If no match is found before the end of the table is reached then decoding
399*4882a593Smuzhiyun  * fails with INSN_REJECTED.
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * When a match is found, decode_regs() is called to validate and modify each
402*4882a593Smuzhiyun  * of the registers encoded in the instruction; the data it uses to do this
403*4882a593Smuzhiyun  * is (type_regs >> DECODE_TYPE_BITS). A validation failure will cause decoding
404*4882a593Smuzhiyun  * to fail with INSN_REJECTED.
405*4882a593Smuzhiyun  *
406*4882a593Smuzhiyun  * Once the instruction has passed the above tests, further processing
407*4882a593Smuzhiyun  * depends on the type of the table entry's decode struct.
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  */
410*4882a593Smuzhiyun int __kprobes
probes_decode_insn(probes_opcode_t insn,struct arch_probes_insn * asi,const union decode_item * table,bool thumb,bool emulate,const union decode_action * actions,const struct decode_checker * checkers[])411*4882a593Smuzhiyun probes_decode_insn(probes_opcode_t insn, struct arch_probes_insn *asi,
412*4882a593Smuzhiyun 		   const union decode_item *table, bool thumb,
413*4882a593Smuzhiyun 		   bool emulate, const union decode_action *actions,
414*4882a593Smuzhiyun 		   const struct decode_checker *checkers[])
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	const struct decode_header *h = (struct decode_header *)table;
417*4882a593Smuzhiyun 	const struct decode_header *next;
418*4882a593Smuzhiyun 	bool matched = false;
419*4882a593Smuzhiyun 	/*
420*4882a593Smuzhiyun 	 * @insn can be modified by decode_regs. Save its original
421*4882a593Smuzhiyun 	 * value for checkers.
422*4882a593Smuzhiyun 	 */
423*4882a593Smuzhiyun 	probes_opcode_t origin_insn = insn;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	/*
426*4882a593Smuzhiyun 	 * stack_space is initialized to 0 here. Checker functions
427*4882a593Smuzhiyun 	 * should update is value if they find this is a stack store
428*4882a593Smuzhiyun 	 * instruction: positive value means bytes of stack usage,
429*4882a593Smuzhiyun 	 * negitive value means unable to determine stack usage
430*4882a593Smuzhiyun 	 * statically. For instruction doesn't store to stack, checker
431*4882a593Smuzhiyun 	 * do nothing with it.
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	asi->stack_space = 0;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/*
436*4882a593Smuzhiyun 	 * Similarly to stack_space, register_usage_flags is filled by
437*4882a593Smuzhiyun 	 * checkers. Its default value is set to ~0, which is 'all
438*4882a593Smuzhiyun 	 * registers are used', to prevent any potential optimization.
439*4882a593Smuzhiyun 	 */
440*4882a593Smuzhiyun 	asi->register_usage_flags = ~0UL;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	if (emulate)
443*4882a593Smuzhiyun 		insn = prepare_emulated_insn(insn, asi, thumb);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	for (;; h = next) {
446*4882a593Smuzhiyun 		enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
447*4882a593Smuzhiyun 		u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 		if (type == DECODE_TYPE_END)
450*4882a593Smuzhiyun 			return INSN_REJECTED;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 		next = (struct decode_header *)
453*4882a593Smuzhiyun 				((uintptr_t)h + decode_struct_sizes[type]);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 		if (!matched && (insn & h->mask.bits) != h->value.bits)
456*4882a593Smuzhiyun 			continue;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		if (!decode_regs(&insn, regs, emulate))
459*4882a593Smuzhiyun 			return INSN_REJECTED;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		switch (type) {
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 		case DECODE_TYPE_TABLE: {
464*4882a593Smuzhiyun 			struct decode_table *d = (struct decode_table *)h;
465*4882a593Smuzhiyun 			next = (struct decode_header *)d->table.table;
466*4882a593Smuzhiyun 			break;
467*4882a593Smuzhiyun 		}
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		case DECODE_TYPE_CUSTOM: {
470*4882a593Smuzhiyun 			int err;
471*4882a593Smuzhiyun 			struct decode_custom *d = (struct decode_custom *)h;
472*4882a593Smuzhiyun 			int action = d->decoder.action;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 			err = run_checkers(checkers, action, origin_insn, asi, h);
475*4882a593Smuzhiyun 			if (err == INSN_REJECTED)
476*4882a593Smuzhiyun 				return INSN_REJECTED;
477*4882a593Smuzhiyun 			return actions[action].decoder(insn, asi, h);
478*4882a593Smuzhiyun 		}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		case DECODE_TYPE_SIMULATE: {
481*4882a593Smuzhiyun 			int err;
482*4882a593Smuzhiyun 			struct decode_simulate *d = (struct decode_simulate *)h;
483*4882a593Smuzhiyun 			int action = d->handler.action;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 			err = run_checkers(checkers, action, origin_insn, asi, h);
486*4882a593Smuzhiyun 			if (err == INSN_REJECTED)
487*4882a593Smuzhiyun 				return INSN_REJECTED;
488*4882a593Smuzhiyun 			asi->insn_handler = actions[action].handler;
489*4882a593Smuzhiyun 			return INSN_GOOD_NO_SLOT;
490*4882a593Smuzhiyun 		}
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		case DECODE_TYPE_EMULATE: {
493*4882a593Smuzhiyun 			int err;
494*4882a593Smuzhiyun 			struct decode_emulate *d = (struct decode_emulate *)h;
495*4882a593Smuzhiyun 			int action = d->handler.action;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 			err = run_checkers(checkers, action, origin_insn, asi, h);
498*4882a593Smuzhiyun 			if (err == INSN_REJECTED)
499*4882a593Smuzhiyun 				return INSN_REJECTED;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 			if (!emulate)
502*4882a593Smuzhiyun 				return actions[action].decoder(insn, asi, h);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 			asi->insn_handler = actions[action].handler;
505*4882a593Smuzhiyun 			set_emulated_insn(insn, asi, thumb);
506*4882a593Smuzhiyun 			return INSN_GOOD;
507*4882a593Smuzhiyun 		}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		case DECODE_TYPE_OR:
510*4882a593Smuzhiyun 			matched = true;
511*4882a593Smuzhiyun 			break;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 		case DECODE_TYPE_REJECT:
514*4882a593Smuzhiyun 		default:
515*4882a593Smuzhiyun 			return INSN_REJECTED;
516*4882a593Smuzhiyun 		}
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun }
519