xref: /linux/arch/arm/probes/kprobes/checkers-thumb.c (revision bfd5bb6f90af092aa345b15cd78143956a13c2a8)
1 /*
2  * arch/arm/probes/kprobes/checkers-thumb.c
3  *
4  * Copyright (C) 2014 Huawei Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  */
15 
16 #include <linux/kernel.h>
17 #include "../decode.h"
18 #include "../decode-thumb.h"
19 #include "checkers.h"
20 
21 static enum probes_insn __kprobes t32_check_stack(probes_opcode_t insn,
22 		struct arch_probes_insn *asi,
23 		const struct decode_header *h)
24 {
25 	/*
26 	 * PROBES_T32_LDMSTM, PROBES_T32_LDRDSTRD and PROBES_T32_LDRSTR
27 	 * may get here. Simply mark all normal insns as STACK_USE_NONE.
28 	 */
29 	static const union decode_item table[] = {
30 
31 		/*
32 		 * First, filter out all ldr insns to make our life easier.
33 		 * Following load insns may come here:
34 		 * LDM, LDRD, LDR.
35 		 * In T32 encoding, bit 20 is enough for distinguishing
36 		 * load and store. All load insns have this bit set, when
37 		 * all store insns have this bit clear.
38 		 */
39 		DECODE_CUSTOM	(0x00100000, 0x00100000, STACK_USE_NONE),
40 
41 		/*
42 		 * Mark all 'STR{,B,H}, Rt, [Rn, Rm]' as STACK_USE_UNKNOWN
43 		 * if Rn or Rm is SP. T32 doesn't encode STRD.
44 		 */
45 		/*                                 xx | Rn | Rt |         | Rm |*/
46 		/* STR (register)	1111 1000 0100 xxxx xxxx 0000 00xx xxxx */
47 		/* STRB (register)	1111 1000 0000 xxxx xxxx 0000 00xx xxxx */
48 		/* STRH (register)	1111 1000 0010 xxxx xxxx 0000 00xx xxxx */
49 		/* INVALID INSN		1111 1000 0110 xxxx xxxx 0000 00xx xxxx */
50 		/* By Introducing INVALID INSN, bit 21 and 22 can be ignored. */
51 		DECODE_OR	(0xff9f0fc0, 0xf80d0000),
52 		DECODE_CUSTOM	(0xff900fcf, 0xf800000d, STACK_USE_UNKNOWN),
53 
54 
55 		/*                                 xx | Rn | Rt | PUW|   imm8  |*/
56 		/* STR (imm 8)		1111 1000 0100 1101 xxxx 110x xxxx xxxx */
57 		/* STRB (imm 8)		1111 1000 0000 1101 xxxx 110x xxxx xxxx */
58 		/* STRH (imm 8)		1111 1000 0010 1101 xxxx 110x xxxx xxxx */
59 		/* INVALID INSN		1111 1000 0110 1101 xxxx 110x xxxx xxxx */
60 		/* Only consider U == 0 and P == 1: strx rx, [sp, #-<imm>] */
61 		DECODE_CUSTOM	(0xff9f0e00, 0xf80d0c00, STACK_USE_FIXED_0XX),
62 
63 		/* For STR{,B,H} (imm 12), offset is always positive, so ignore them. */
64 
65 		/*                              P U W | Rn | Rt | Rt2|   imm8  |*/
66 		/* STRD (immediate)	1110 1001 01x0 1101 xxxx xxxx xxxx xxxx */
67 		/*
68 		 * Only consider U == 0 and P == 1.
69 		 * Also note that STRD in T32 encoding is special:
70 		 * imm = ZeroExtend(imm8:'00', 32)
71 		 */
72 		DECODE_CUSTOM	(0xffdf0000, 0xe94d0000, STACK_USE_T32STRD),
73 
74 		/*                                    | Rn | */
75 		/* STMDB		1110 1001 00x0 1101 xxxx xxxx xxxx xxxx */
76 		DECODE_CUSTOM	(0xffdf0000, 0xe90d0000, STACK_USE_STMDX),
77 
78 		/* fall through */
79 		DECODE_CUSTOM	(0, 0, STACK_USE_NONE),
80 		DECODE_END
81 	};
82 
83 	return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
84 }
85 
86 const struct decode_checker t32_stack_checker[NUM_PROBES_T32_ACTIONS] = {
87 	[PROBES_T32_LDMSTM] = {.checker = t32_check_stack},
88 	[PROBES_T32_LDRDSTRD] = {.checker = t32_check_stack},
89 	[PROBES_T32_LDRSTR] = {.checker = t32_check_stack},
90 };
91 
92 /*
93  * See following comments. This insn must be 'push'.
94  */
95 static enum probes_insn __kprobes t16_check_stack(probes_opcode_t insn,
96 		struct arch_probes_insn *asi,
97 		const struct decode_header *h)
98 {
99 	unsigned int reglist = insn & 0x1ff;
100 	asi->stack_space = hweight32(reglist) * 4;
101 	return INSN_GOOD;
102 }
103 
104 /*
105  * T16 encoding is simple: only the 'push' insn can need extra stack space.
106  * Other insns, like str, can only use r0-r7 as Rn.
107  */
108 const struct decode_checker t16_stack_checker[NUM_PROBES_T16_ACTIONS] = {
109 	[PROBES_T16_PUSH] = {.checker = t16_check_stack},
110 };
111