Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Kernel Probes (KProbes) | |
1da177e4 LT |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright (C) IBM Corporation, 2002, 2004 | |
19 | * | |
20 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel | |
21 | * Probes initial implementation ( includes contributions from | |
22 | * Rusty Russell). | |
23 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes | |
24 | * interface to access function arguments. | |
d6be29b8 MH |
25 | * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi |
26 | * <prasanna@in.ibm.com> adapted for x86_64 from i386. | |
1da177e4 LT |
27 | * 2005-Mar Roland McGrath <roland@redhat.com> |
28 | * Fixed to handle %rip-relative addressing mode correctly. | |
d6be29b8 MH |
29 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
30 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi | |
31 | * <prasanna@in.ibm.com> added function-return probes. | |
32 | * 2005-May Rusty Lynch <rusty.lynch@intel.com> | |
33 | * Added function return probes functionality | |
34 | * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added | |
35 | * kprobe-booster and kretprobe-booster for i386. | |
da07ab03 MH |
36 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster |
37 | * and kretprobe-booster for x86-64 | |
d6be29b8 MH |
38 | * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven |
39 | * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com> | |
40 | * unified x86 kprobes code. | |
1da177e4 LT |
41 | */ |
42 | ||
1da177e4 LT |
43 | #include <linux/kprobes.h> |
44 | #include <linux/ptrace.h> | |
1da177e4 LT |
45 | #include <linux/string.h> |
46 | #include <linux/slab.h> | |
b506a9d0 | 47 | #include <linux/hardirq.h> |
1da177e4 | 48 | #include <linux/preempt.h> |
c28f8966 | 49 | #include <linux/module.h> |
1eeb66a1 | 50 | #include <linux/kdebug.h> |
9ec4b1f3 | 51 | |
8533bbe9 MH |
52 | #include <asm/cacheflush.h> |
53 | #include <asm/desc.h> | |
1da177e4 | 54 | #include <asm/pgtable.h> |
c28f8966 | 55 | #include <asm/uaccess.h> |
19d36ccd | 56 | #include <asm/alternative.h> |
62edab90 | 57 | #include <asm/debugreg.h> |
1da177e4 | 58 | |
1da177e4 LT |
59 | void jprobe_return_end(void); |
60 | ||
e7a510f9 AM |
61 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
62 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
1da177e4 | 63 | |
d6be29b8 | 64 | #ifdef CONFIG_X86_64 |
8533bbe9 | 65 | #define stack_addr(regs) ((unsigned long *)regs->sp) |
d6be29b8 MH |
66 | #else |
67 | /* | |
68 | * "®s->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs | |
69 | * don't save the ss and esp registers if the CPU is already in kernel | |
70 | * mode when it traps. So for kprobes, regs->sp and regs->ss are not | |
71 | * the [nonexistent] saved stack pointer and ss register, but rather | |
72 | * the top 8 bytes of the pre-int3 stack. So ®s->sp happens to | |
73 | * point to the top of the pre-int3 stack. | |
74 | */ | |
75 | #define stack_addr(regs) ((unsigned long *)®s->sp) | |
76 | #endif | |
8533bbe9 MH |
77 | |
78 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ | |
79 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ | |
80 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ | |
81 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ | |
82 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ | |
83 | << (row % 32)) | |
84 | /* | |
85 | * Undefined/reserved opcodes, conditional jump, Opcode Extension | |
86 | * Groups, and some special opcodes can not boost. | |
87 | */ | |
88 | static const u32 twobyte_is_boostable[256 / 32] = { | |
89 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
90 | /* ---------------------------------------------- */ | |
91 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ | |
92 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */ | |
93 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ | |
94 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ | |
95 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ | |
96 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ | |
97 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ | |
98 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ | |
99 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ | |
100 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ | |
101 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ | |
102 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ | |
103 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ | |
104 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ | |
105 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ | |
106 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ | |
107 | /* ----------------------------------------------- */ | |
108 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
109 | }; | |
110 | static const u32 onebyte_has_modrm[256 / 32] = { | |
111 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
112 | /* ----------------------------------------------- */ | |
113 | W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */ | |
114 | W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */ | |
115 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */ | |
116 | W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */ | |
117 | W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */ | |
118 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ | |
119 | W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */ | |
120 | W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */ | |
121 | W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */ | |
122 | W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */ | |
123 | W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */ | |
124 | W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */ | |
125 | W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */ | |
126 | W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */ | |
127 | W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */ | |
128 | W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */ | |
129 | /* ----------------------------------------------- */ | |
130 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
131 | }; | |
132 | static const u32 twobyte_has_modrm[256 / 32] = { | |
133 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
134 | /* ----------------------------------------------- */ | |
135 | W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */ | |
136 | W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */ | |
137 | W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */ | |
138 | W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */ | |
139 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */ | |
140 | W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */ | |
141 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */ | |
142 | W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */ | |
143 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */ | |
144 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */ | |
145 | W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */ | |
146 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */ | |
147 | W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */ | |
148 | W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */ | |
149 | W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */ | |
150 | W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */ | |
151 | /* ----------------------------------------------- */ | |
152 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
153 | }; | |
154 | #undef W | |
155 | ||
f438d914 MH |
156 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
157 | {"__switch_to", }, /* This function switches only current task, but | |
158 | doesn't switch kernel stack.*/ | |
159 | {NULL, NULL} /* Terminator */ | |
160 | }; | |
161 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); | |
162 | ||
aa470140 | 163 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ |
e7b5e11e | 164 | static void __kprobes set_jmp_op(void *from, void *to) |
aa470140 MH |
165 | { |
166 | struct __arch_jmp_op { | |
167 | char op; | |
168 | s32 raddr; | |
169 | } __attribute__((packed)) * jop; | |
170 | jop = (struct __arch_jmp_op *)from; | |
171 | jop->raddr = (s32)((long)(to) - ((long)(from) + 5)); | |
172 | jop->op = RELATIVEJUMP_INSTRUCTION; | |
173 | } | |
174 | ||
9930927f HH |
175 | /* |
176 | * Check for the REX prefix which can only exist on X86_64 | |
177 | * X86_32 always returns 0 | |
178 | */ | |
179 | static int __kprobes is_REX_prefix(kprobe_opcode_t *insn) | |
180 | { | |
181 | #ifdef CONFIG_X86_64 | |
182 | if ((*insn & 0xf0) == 0x40) | |
183 | return 1; | |
184 | #endif | |
185 | return 0; | |
186 | } | |
187 | ||
aa470140 | 188 | /* |
d6be29b8 MH |
189 | * Returns non-zero if opcode is boostable. |
190 | * RIP relative instructions are adjusted at copying time in 64 bits mode | |
aa470140 | 191 | */ |
e7b5e11e | 192 | static int __kprobes can_boost(kprobe_opcode_t *opcodes) |
aa470140 | 193 | { |
aa470140 MH |
194 | kprobe_opcode_t opcode; |
195 | kprobe_opcode_t *orig_opcodes = opcodes; | |
196 | ||
cde5edbd | 197 | if (search_exception_tables((unsigned long)opcodes)) |
30390880 MH |
198 | return 0; /* Page fault may occur on this address. */ |
199 | ||
aa470140 MH |
200 | retry: |
201 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
202 | return 0; | |
203 | opcode = *(opcodes++); | |
204 | ||
205 | /* 2nd-byte opcode */ | |
206 | if (opcode == 0x0f) { | |
207 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
208 | return 0; | |
8533bbe9 MH |
209 | return test_bit(*opcodes, |
210 | (unsigned long *)twobyte_is_boostable); | |
aa470140 MH |
211 | } |
212 | ||
213 | switch (opcode & 0xf0) { | |
d6be29b8 | 214 | #ifdef CONFIG_X86_64 |
aa470140 MH |
215 | case 0x40: |
216 | goto retry; /* REX prefix is boostable */ | |
d6be29b8 | 217 | #endif |
aa470140 MH |
218 | case 0x60: |
219 | if (0x63 < opcode && opcode < 0x67) | |
220 | goto retry; /* prefixes */ | |
221 | /* can't boost Address-size override and bound */ | |
222 | return (opcode != 0x62 && opcode != 0x67); | |
223 | case 0x70: | |
224 | return 0; /* can't boost conditional jump */ | |
225 | case 0xc0: | |
226 | /* can't boost software-interruptions */ | |
227 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; | |
228 | case 0xd0: | |
229 | /* can boost AA* and XLAT */ | |
230 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); | |
231 | case 0xe0: | |
232 | /* can boost in/out and absolute jmps */ | |
233 | return ((opcode & 0x04) || opcode == 0xea); | |
234 | case 0xf0: | |
235 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) | |
236 | goto retry; /* lock/rep(ne) prefix */ | |
237 | /* clear and set flags are boostable */ | |
238 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); | |
239 | default: | |
240 | /* segment override prefixes are boostable */ | |
241 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) | |
242 | goto retry; /* prefixes */ | |
243 | /* CS override prefix and call are not boostable */ | |
244 | return (opcode != 0x2e && opcode != 0x9a); | |
245 | } | |
246 | } | |
247 | ||
1da177e4 | 248 | /* |
d6be29b8 | 249 | * Returns non-zero if opcode modifies the interrupt flag. |
1da177e4 | 250 | */ |
8645419c | 251 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
1da177e4 LT |
252 | { |
253 | switch (*insn) { | |
254 | case 0xfa: /* cli */ | |
255 | case 0xfb: /* sti */ | |
256 | case 0xcf: /* iret/iretd */ | |
257 | case 0x9d: /* popf/popfd */ | |
258 | return 1; | |
259 | } | |
9930927f | 260 | |
8533bbe9 | 261 | /* |
9930927f | 262 | * on X86_64, 0x40-0x4f are REX prefixes so we need to look |
8533bbe9 MH |
263 | * at the next byte instead.. but of course not recurse infinitely |
264 | */ | |
9930927f | 265 | if (is_REX_prefix(insn)) |
8533bbe9 | 266 | return is_IF_modifier(++insn); |
9930927f | 267 | |
1da177e4 LT |
268 | return 0; |
269 | } | |
270 | ||
271 | /* | |
8533bbe9 MH |
272 | * Adjust the displacement if the instruction uses the %rip-relative |
273 | * addressing mode. | |
aa470140 | 274 | * If it does, Return the address of the 32-bit displacement word. |
1da177e4 | 275 | * If not, return null. |
31f80e45 | 276 | * Only applicable to 64-bit x86. |
1da177e4 | 277 | */ |
8533bbe9 | 278 | static void __kprobes fix_riprel(struct kprobe *p) |
1da177e4 | 279 | { |
31f80e45 | 280 | #ifdef CONFIG_X86_64 |
8533bbe9 MH |
281 | u8 *insn = p->ainsn.insn; |
282 | s64 disp; | |
1da177e4 LT |
283 | int need_modrm; |
284 | ||
285 | /* Skip legacy instruction prefixes. */ | |
286 | while (1) { | |
287 | switch (*insn) { | |
288 | case 0x66: | |
289 | case 0x67: | |
290 | case 0x2e: | |
291 | case 0x3e: | |
292 | case 0x26: | |
293 | case 0x64: | |
294 | case 0x65: | |
295 | case 0x36: | |
296 | case 0xf0: | |
297 | case 0xf3: | |
298 | case 0xf2: | |
299 | ++insn; | |
300 | continue; | |
301 | } | |
302 | break; | |
303 | } | |
304 | ||
305 | /* Skip REX instruction prefix. */ | |
9930927f | 306 | if (is_REX_prefix(insn)) |
1da177e4 LT |
307 | ++insn; |
308 | ||
d6be29b8 MH |
309 | if (*insn == 0x0f) { |
310 | /* Two-byte opcode. */ | |
1da177e4 | 311 | ++insn; |
8533bbe9 MH |
312 | need_modrm = test_bit(*insn, |
313 | (unsigned long *)twobyte_has_modrm); | |
d6be29b8 MH |
314 | } else |
315 | /* One-byte opcode. */ | |
8533bbe9 MH |
316 | need_modrm = test_bit(*insn, |
317 | (unsigned long *)onebyte_has_modrm); | |
1da177e4 LT |
318 | |
319 | if (need_modrm) { | |
320 | u8 modrm = *++insn; | |
d6be29b8 MH |
321 | if ((modrm & 0xc7) == 0x05) { |
322 | /* %rip+disp32 addressing mode */ | |
1da177e4 | 323 | /* Displacement follows ModRM byte. */ |
8533bbe9 MH |
324 | ++insn; |
325 | /* | |
326 | * The copied instruction uses the %rip-relative | |
327 | * addressing mode. Adjust the displacement for the | |
328 | * difference between the original location of this | |
329 | * instruction and the location of the copy that will | |
330 | * actually be run. The tricky bit here is making sure | |
331 | * that the sign extension happens correctly in this | |
332 | * calculation, since we need a signed 32-bit result to | |
333 | * be sign-extended to 64 bits when it's added to the | |
334 | * %rip value and yield the same 64-bit result that the | |
335 | * sign-extension of the original signed 32-bit | |
336 | * displacement would have given. | |
337 | */ | |
338 | disp = (u8 *) p->addr + *((s32 *) insn) - | |
339 | (u8 *) p->ainsn.insn; | |
340 | BUG_ON((s64) (s32) disp != disp); /* Sanity check. */ | |
341 | *(s32 *)insn = (s32) disp; | |
1da177e4 LT |
342 | } |
343 | } | |
d6be29b8 | 344 | #endif |
31f80e45 | 345 | } |
1da177e4 | 346 | |
f709b122 | 347 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
1da177e4 | 348 | { |
8533bbe9 | 349 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
31f80e45 | 350 | |
8533bbe9 | 351 | fix_riprel(p); |
31f80e45 | 352 | |
8533bbe9 | 353 | if (can_boost(p->addr)) |
aa470140 | 354 | p->ainsn.boostable = 0; |
8533bbe9 | 355 | else |
aa470140 | 356 | p->ainsn.boostable = -1; |
8533bbe9 | 357 | |
7e1048b1 | 358 | p->opcode = *p->addr; |
1da177e4 LT |
359 | } |
360 | ||
8533bbe9 MH |
361 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
362 | { | |
363 | /* insn: must be on special executable page on x86. */ | |
364 | p->ainsn.insn = get_insn_slot(); | |
365 | if (!p->ainsn.insn) | |
366 | return -ENOMEM; | |
367 | arch_copy_kprobe(p); | |
368 | return 0; | |
369 | } | |
370 | ||
0f2fbdcb | 371 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 372 | { |
19d36ccd | 373 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
1da177e4 LT |
374 | } |
375 | ||
0f2fbdcb | 376 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
1da177e4 | 377 | { |
19d36ccd | 378 | text_poke(p->addr, &p->opcode, 1); |
7e1048b1 RL |
379 | } |
380 | ||
0498b635 | 381 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
7e1048b1 | 382 | { |
12941560 MH |
383 | if (p->ainsn.insn) { |
384 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); | |
385 | p->ainsn.insn = NULL; | |
386 | } | |
1da177e4 LT |
387 | } |
388 | ||
3b60211c | 389 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 390 | { |
e7a510f9 AM |
391 | kcb->prev_kprobe.kp = kprobe_running(); |
392 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
8533bbe9 MH |
393 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
394 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; | |
aa3d7e3d PP |
395 | } |
396 | ||
3b60211c | 397 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 398 | { |
e7a510f9 AM |
399 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
400 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
8533bbe9 MH |
401 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
402 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; | |
aa3d7e3d PP |
403 | } |
404 | ||
3b60211c | 405 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
e7a510f9 | 406 | struct kprobe_ctlblk *kcb) |
aa3d7e3d | 407 | { |
e7a510f9 | 408 | __get_cpu_var(current_kprobe) = p; |
8533bbe9 | 409 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
053de044 | 410 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
aa3d7e3d | 411 | if (is_IF_modifier(p->ainsn.insn)) |
053de044 | 412 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
aa3d7e3d PP |
413 | } |
414 | ||
e7b5e11e | 415 | static void __kprobes clear_btf(void) |
1ecc798c RM |
416 | { |
417 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | |
5b0e5084 | 418 | update_debugctlmsr(0); |
1ecc798c RM |
419 | } |
420 | ||
e7b5e11e | 421 | static void __kprobes restore_btf(void) |
1ecc798c RM |
422 | { |
423 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | |
5b0e5084 | 424 | update_debugctlmsr(current->thread.debugctlmsr); |
1ecc798c RM |
425 | } |
426 | ||
0f2fbdcb | 427 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 428 | { |
1ecc798c | 429 | clear_btf(); |
053de044 GOC |
430 | regs->flags |= X86_EFLAGS_TF; |
431 | regs->flags &= ~X86_EFLAGS_IF; | |
e7b5e11e | 432 | /* single step inline if the instruction is an int3 */ |
1da177e4 | 433 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
65ea5b03 | 434 | regs->ip = (unsigned long)p->addr; |
1da177e4 | 435 | else |
65ea5b03 | 436 | regs->ip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
437 | } |
438 | ||
4c4308cb | 439 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
0f2fbdcb | 440 | struct pt_regs *regs) |
73649dab | 441 | { |
8533bbe9 | 442 | unsigned long *sara = stack_addr(regs); |
ba8af12f | 443 | |
4c4308cb | 444 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
8533bbe9 | 445 | |
4c4308cb CH |
446 | /* Replace the return addr with trampoline addr */ |
447 | *sara = (unsigned long) &kretprobe_trampoline; | |
73649dab | 448 | } |
f315decb | 449 | |
f315decb AS |
450 | static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
451 | struct kprobe_ctlblk *kcb) | |
452 | { | |
5a4ccaf3 | 453 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER) |
f315decb AS |
454 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
455 | /* Boost up -- we can execute copied instructions directly */ | |
456 | reset_current_kprobe(); | |
457 | regs->ip = (unsigned long)p->ainsn.insn; | |
458 | preempt_enable_no_resched(); | |
459 | return; | |
460 | } | |
461 | #endif | |
462 | prepare_singlestep(p, regs); | |
463 | kcb->kprobe_status = KPROBE_HIT_SS; | |
464 | } | |
465 | ||
40102d4a HH |
466 | /* |
467 | * We have reentered the kprobe_handler(), since another probe was hit while | |
468 | * within the handler. We save the original kprobes variables and just single | |
469 | * step on the instruction of the new probe without calling any user handlers. | |
470 | */ | |
59e87cdc MH |
471 | static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
472 | struct kprobe_ctlblk *kcb) | |
40102d4a | 473 | { |
f315decb AS |
474 | switch (kcb->kprobe_status) { |
475 | case KPROBE_HIT_SSDONE: | |
59e87cdc | 476 | #ifdef CONFIG_X86_64 |
59e87cdc MH |
477 | /* TODO: Provide re-entrancy from post_kprobes_handler() and |
478 | * avoid exception stack corruption while single-stepping on | |
479 | * the instruction of the new probe. | |
480 | */ | |
481 | arch_disarm_kprobe(p); | |
482 | regs->ip = (unsigned long)p->addr; | |
483 | reset_current_kprobe(); | |
f315decb AS |
484 | preempt_enable_no_resched(); |
485 | break; | |
59e87cdc | 486 | #endif |
f315decb | 487 | case KPROBE_HIT_ACTIVE: |
fb8830e7 AS |
488 | save_previous_kprobe(kcb); |
489 | set_current_kprobe(p, regs, kcb); | |
490 | kprobes_inc_nmissed_count(p); | |
491 | prepare_singlestep(p, regs); | |
492 | kcb->kprobe_status = KPROBE_REENTER; | |
f315decb AS |
493 | break; |
494 | case KPROBE_HIT_SS: | |
fb8830e7 | 495 | if (p == kprobe_running()) { |
a5c15d41 | 496 | regs->flags &= ~X86_EFLAGS_TF; |
f315decb AS |
497 | regs->flags |= kcb->kprobe_saved_flags; |
498 | return 0; | |
499 | } else { | |
fb8830e7 AS |
500 | /* A probe has been hit in the codepath leading up |
501 | * to, or just after, single-stepping of a probed | |
502 | * instruction. This entire codepath should strictly | |
503 | * reside in .kprobes.text section. Raise a warning | |
504 | * to highlight this peculiar case. | |
505 | */ | |
f315decb | 506 | } |
f315decb AS |
507 | default: |
508 | /* impossible cases */ | |
509 | WARN_ON(1); | |
fb8830e7 | 510 | return 0; |
59e87cdc | 511 | } |
f315decb | 512 | |
59e87cdc | 513 | return 1; |
40102d4a | 514 | } |
73649dab | 515 | |
8533bbe9 MH |
516 | /* |
517 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
518 | * remain disabled thorough out this function. | |
519 | */ | |
520 | static int __kprobes kprobe_handler(struct pt_regs *regs) | |
1da177e4 | 521 | { |
8533bbe9 | 522 | kprobe_opcode_t *addr; |
f315decb | 523 | struct kprobe *p; |
d217d545 AM |
524 | struct kprobe_ctlblk *kcb; |
525 | ||
8533bbe9 | 526 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
f315decb AS |
527 | if (*addr != BREAKPOINT_INSTRUCTION) { |
528 | /* | |
529 | * The breakpoint instruction was removed right | |
530 | * after we hit it. Another cpu has removed | |
531 | * either a probepoint or a debugger breakpoint | |
532 | * at this address. In either case, no further | |
533 | * handling of this interrupt is appropriate. | |
534 | * Back up over the (now missing) int3 and run | |
535 | * the original instruction. | |
536 | */ | |
537 | regs->ip = (unsigned long)addr; | |
538 | return 1; | |
539 | } | |
8533bbe9 | 540 | |
d217d545 AM |
541 | /* |
542 | * We don't want to be preempted for the entire | |
f315decb AS |
543 | * duration of kprobe processing. We conditionally |
544 | * re-enable preemption at the end of this function, | |
545 | * and also in reenter_kprobe() and setup_singlestep(). | |
d217d545 AM |
546 | */ |
547 | preempt_disable(); | |
1da177e4 | 548 | |
f315decb | 549 | kcb = get_kprobe_ctlblk(); |
b9760156 | 550 | p = get_kprobe(addr); |
f315decb | 551 | |
b9760156 | 552 | if (p) { |
b9760156 | 553 | if (kprobe_running()) { |
f315decb AS |
554 | if (reenter_kprobe(p, regs, kcb)) |
555 | return 1; | |
1da177e4 | 556 | } else { |
b9760156 HH |
557 | set_current_kprobe(p, regs, kcb); |
558 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
f315decb | 559 | |
1da177e4 | 560 | /* |
f315decb AS |
561 | * If we have no pre-handler or it returned 0, we |
562 | * continue with normal processing. If we have a | |
563 | * pre-handler and it returned non-zero, it prepped | |
564 | * for calling the break_handler below on re-entry | |
565 | * for jprobe processing, so get out doing nothing | |
566 | * more here. | |
1da177e4 | 567 | */ |
f315decb AS |
568 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
569 | setup_singlestep(p, regs, kcb); | |
570 | return 1; | |
b9760156 | 571 | } |
f315decb AS |
572 | } else if (kprobe_running()) { |
573 | p = __get_cpu_var(current_kprobe); | |
574 | if (p->break_handler && p->break_handler(p, regs)) { | |
575 | setup_singlestep(p, regs, kcb); | |
576 | return 1; | |
1da177e4 | 577 | } |
f315decb | 578 | } /* else: not a kprobe fault; let the kernel handle it */ |
1da177e4 | 579 | |
d217d545 | 580 | preempt_enable_no_resched(); |
f315decb | 581 | return 0; |
1da177e4 LT |
582 | } |
583 | ||
73649dab | 584 | /* |
da07ab03 MH |
585 | * When a retprobed function returns, this code saves registers and |
586 | * calls trampoline_handler() runs, which calls the kretprobe's handler. | |
73649dab | 587 | */ |
f1452d42 | 588 | static void __used __kprobes kretprobe_trampoline_holder(void) |
1017579a | 589 | { |
d6be29b8 MH |
590 | asm volatile ( |
591 | ".global kretprobe_trampoline\n" | |
da07ab03 | 592 | "kretprobe_trampoline: \n" |
d6be29b8 | 593 | #ifdef CONFIG_X86_64 |
da07ab03 MH |
594 | /* We don't bother saving the ss register */ |
595 | " pushq %rsp\n" | |
596 | " pushfq\n" | |
597 | /* | |
598 | * Skip cs, ip, orig_ax. | |
599 | * trampoline_handler() will plug in these values | |
600 | */ | |
601 | " subq $24, %rsp\n" | |
602 | " pushq %rdi\n" | |
603 | " pushq %rsi\n" | |
604 | " pushq %rdx\n" | |
605 | " pushq %rcx\n" | |
606 | " pushq %rax\n" | |
607 | " pushq %r8\n" | |
608 | " pushq %r9\n" | |
609 | " pushq %r10\n" | |
610 | " pushq %r11\n" | |
611 | " pushq %rbx\n" | |
612 | " pushq %rbp\n" | |
613 | " pushq %r12\n" | |
614 | " pushq %r13\n" | |
615 | " pushq %r14\n" | |
616 | " pushq %r15\n" | |
617 | " movq %rsp, %rdi\n" | |
618 | " call trampoline_handler\n" | |
619 | /* Replace saved sp with true return address. */ | |
620 | " movq %rax, 152(%rsp)\n" | |
621 | " popq %r15\n" | |
622 | " popq %r14\n" | |
623 | " popq %r13\n" | |
624 | " popq %r12\n" | |
625 | " popq %rbp\n" | |
626 | " popq %rbx\n" | |
627 | " popq %r11\n" | |
628 | " popq %r10\n" | |
629 | " popq %r9\n" | |
630 | " popq %r8\n" | |
631 | " popq %rax\n" | |
632 | " popq %rcx\n" | |
633 | " popq %rdx\n" | |
634 | " popq %rsi\n" | |
635 | " popq %rdi\n" | |
636 | /* Skip orig_ax, ip, cs */ | |
637 | " addq $24, %rsp\n" | |
638 | " popfq\n" | |
d6be29b8 MH |
639 | #else |
640 | " pushf\n" | |
641 | /* | |
fee039a1 | 642 | * Skip cs, ip, orig_ax and gs. |
d6be29b8 MH |
643 | * trampoline_handler() will plug in these values |
644 | */ | |
fee039a1 | 645 | " subl $16, %esp\n" |
d6be29b8 | 646 | " pushl %fs\n" |
d6be29b8 | 647 | " pushl %es\n" |
fee039a1 | 648 | " pushl %ds\n" |
d6be29b8 MH |
649 | " pushl %eax\n" |
650 | " pushl %ebp\n" | |
651 | " pushl %edi\n" | |
652 | " pushl %esi\n" | |
653 | " pushl %edx\n" | |
654 | " pushl %ecx\n" | |
655 | " pushl %ebx\n" | |
656 | " movl %esp, %eax\n" | |
657 | " call trampoline_handler\n" | |
658 | /* Move flags to cs */ | |
fee039a1 MH |
659 | " movl 56(%esp), %edx\n" |
660 | " movl %edx, 52(%esp)\n" | |
d6be29b8 | 661 | /* Replace saved flags with true return address. */ |
fee039a1 | 662 | " movl %eax, 56(%esp)\n" |
d6be29b8 MH |
663 | " popl %ebx\n" |
664 | " popl %ecx\n" | |
665 | " popl %edx\n" | |
666 | " popl %esi\n" | |
667 | " popl %edi\n" | |
668 | " popl %ebp\n" | |
669 | " popl %eax\n" | |
fee039a1 MH |
670 | /* Skip ds, es, fs, gs, orig_ax and ip */ |
671 | " addl $24, %esp\n" | |
d6be29b8 MH |
672 | " popf\n" |
673 | #endif | |
da07ab03 | 674 | " ret\n"); |
1017579a | 675 | } |
73649dab RL |
676 | |
677 | /* | |
da07ab03 | 678 | * Called from kretprobe_trampoline |
73649dab | 679 | */ |
f1452d42 | 680 | static __used __kprobes void *trampoline_handler(struct pt_regs *regs) |
73649dab | 681 | { |
62c27be0 | 682 | struct kretprobe_instance *ri = NULL; |
99219a3f | 683 | struct hlist_head *head, empty_rp; |
62c27be0 | 684 | struct hlist_node *node, *tmp; |
991a51d8 | 685 | unsigned long flags, orig_ret_address = 0; |
d6be29b8 | 686 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
73649dab | 687 | |
99219a3f | 688 | INIT_HLIST_HEAD(&empty_rp); |
ef53d9c5 | 689 | kretprobe_hash_lock(current, &head, &flags); |
8533bbe9 | 690 | /* fixup registers */ |
d6be29b8 | 691 | #ifdef CONFIG_X86_64 |
da07ab03 | 692 | regs->cs = __KERNEL_CS; |
d6be29b8 MH |
693 | #else |
694 | regs->cs = __KERNEL_CS | get_kernel_rpl(); | |
fee039a1 | 695 | regs->gs = 0; |
d6be29b8 | 696 | #endif |
da07ab03 | 697 | regs->ip = trampoline_address; |
8533bbe9 | 698 | regs->orig_ax = ~0UL; |
73649dab | 699 | |
ba8af12f RL |
700 | /* |
701 | * It is possible to have multiple instances associated with a given | |
8533bbe9 | 702 | * task either because multiple functions in the call path have |
025dfdaf | 703 | * return probes installed on them, and/or more than one |
ba8af12f RL |
704 | * return probe was registered for a target function. |
705 | * | |
706 | * We can handle this because: | |
8533bbe9 | 707 | * - instances are always pushed into the head of the list |
ba8af12f | 708 | * - when multiple return probes are registered for the same |
8533bbe9 MH |
709 | * function, the (chronologically) first instance's ret_addr |
710 | * will be the real return address, and all the rest will | |
711 | * point to kretprobe_trampoline. | |
ba8af12f RL |
712 | */ |
713 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
62c27be0 | 714 | if (ri->task != current) |
ba8af12f | 715 | /* another task is sharing our hash bucket */ |
62c27be0 | 716 | continue; |
ba8af12f | 717 | |
da07ab03 MH |
718 | if (ri->rp && ri->rp->handler) { |
719 | __get_cpu_var(current_kprobe) = &ri->rp->kp; | |
720 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; | |
ba8af12f | 721 | ri->rp->handler(ri, regs); |
da07ab03 MH |
722 | __get_cpu_var(current_kprobe) = NULL; |
723 | } | |
ba8af12f RL |
724 | |
725 | orig_ret_address = (unsigned long)ri->ret_addr; | |
99219a3f | 726 | recycle_rp_inst(ri, &empty_rp); |
ba8af12f RL |
727 | |
728 | if (orig_ret_address != trampoline_address) | |
729 | /* | |
730 | * This is the real return address. Any other | |
731 | * instances associated with this task are for | |
732 | * other calls deeper on the call stack | |
733 | */ | |
734 | break; | |
73649dab | 735 | } |
ba8af12f | 736 | |
0f95b7fc | 737 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
ba8af12f | 738 | |
ef53d9c5 | 739 | kretprobe_hash_unlock(current, &flags); |
ba8af12f | 740 | |
99219a3f | 741 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
742 | hlist_del(&ri->hlist); | |
743 | kfree(ri); | |
744 | } | |
da07ab03 | 745 | return (void *)orig_ret_address; |
73649dab RL |
746 | } |
747 | ||
1da177e4 LT |
748 | /* |
749 | * Called after single-stepping. p->addr is the address of the | |
750 | * instruction whose first byte has been replaced by the "int 3" | |
751 | * instruction. To avoid the SMP problems that can occur when we | |
752 | * temporarily put back the original opcode to single-step, we | |
753 | * single-stepped a copy of the instruction. The address of this | |
754 | * copy is p->ainsn.insn. | |
755 | * | |
756 | * This function prepares to return from the post-single-step | |
757 | * interrupt. We have to fix up the stack as follows: | |
758 | * | |
759 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
65ea5b03 | 760 | * the new ip is relative to the copied instruction. We need to make |
1da177e4 LT |
761 | * it relative to the original instruction. |
762 | * | |
763 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
65ea5b03 | 764 | * flags are set in the just-pushed flags, and may need to be cleared. |
1da177e4 LT |
765 | * |
766 | * 2) If the single-stepped instruction was a call, the return address | |
767 | * that is atop the stack is the address following the copied instruction. | |
768 | * We need to make it the address following the original instruction. | |
aa470140 MH |
769 | * |
770 | * If this is the first time we've single-stepped the instruction at | |
771 | * this probepoint, and the instruction is boostable, boost it: add a | |
772 | * jump instruction after the copied instruction, that jumps to the next | |
773 | * instruction after the probepoint. | |
1da177e4 | 774 | */ |
e7a510f9 AM |
775 | static void __kprobes resume_execution(struct kprobe *p, |
776 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) | |
1da177e4 | 777 | { |
8533bbe9 MH |
778 | unsigned long *tos = stack_addr(regs); |
779 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; | |
780 | unsigned long orig_ip = (unsigned long)p->addr; | |
1da177e4 LT |
781 | kprobe_opcode_t *insn = p->ainsn.insn; |
782 | ||
783 | /*skip the REX prefix*/ | |
9930927f | 784 | if (is_REX_prefix(insn)) |
1da177e4 LT |
785 | insn++; |
786 | ||
053de044 | 787 | regs->flags &= ~X86_EFLAGS_TF; |
1da177e4 | 788 | switch (*insn) { |
0b0122fa | 789 | case 0x9c: /* pushfl */ |
053de044 | 790 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
8533bbe9 | 791 | *tos |= kcb->kprobe_old_flags; |
1da177e4 | 792 | break; |
0b0122fa MH |
793 | case 0xc2: /* iret/ret/lret */ |
794 | case 0xc3: | |
0b9e2cac | 795 | case 0xca: |
0b0122fa MH |
796 | case 0xcb: |
797 | case 0xcf: | |
798 | case 0xea: /* jmp absolute -- ip is correct */ | |
799 | /* ip is already adjusted, no more changes required */ | |
aa470140 | 800 | p->ainsn.boostable = 1; |
0b0122fa MH |
801 | goto no_change; |
802 | case 0xe8: /* call relative - Fix return addr */ | |
8533bbe9 | 803 | *tos = orig_ip + (*tos - copy_ip); |
1da177e4 | 804 | break; |
e7b5e11e | 805 | #ifdef CONFIG_X86_32 |
d6be29b8 MH |
806 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
807 | *tos = orig_ip + (*tos - copy_ip); | |
808 | goto no_change; | |
809 | #endif | |
1da177e4 | 810 | case 0xff: |
dc49e344 | 811 | if ((insn[1] & 0x30) == 0x10) { |
8533bbe9 MH |
812 | /* |
813 | * call absolute, indirect | |
814 | * Fix return addr; ip is correct. | |
815 | * But this is not boostable | |
816 | */ | |
817 | *tos = orig_ip + (*tos - copy_ip); | |
0b0122fa | 818 | goto no_change; |
8533bbe9 MH |
819 | } else if (((insn[1] & 0x31) == 0x20) || |
820 | ((insn[1] & 0x31) == 0x21)) { | |
821 | /* | |
822 | * jmp near and far, absolute indirect | |
823 | * ip is correct. And this is boostable | |
824 | */ | |
aa470140 | 825 | p->ainsn.boostable = 1; |
0b0122fa | 826 | goto no_change; |
1da177e4 | 827 | } |
1da177e4 LT |
828 | default: |
829 | break; | |
830 | } | |
831 | ||
aa470140 | 832 | if (p->ainsn.boostable == 0) { |
8533bbe9 MH |
833 | if ((regs->ip > copy_ip) && |
834 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { | |
aa470140 MH |
835 | /* |
836 | * These instructions can be executed directly if it | |
837 | * jumps back to correct address. | |
838 | */ | |
839 | set_jmp_op((void *)regs->ip, | |
8533bbe9 | 840 | (void *)orig_ip + (regs->ip - copy_ip)); |
aa470140 MH |
841 | p->ainsn.boostable = 1; |
842 | } else { | |
843 | p->ainsn.boostable = -1; | |
844 | } | |
845 | } | |
846 | ||
8533bbe9 | 847 | regs->ip += orig_ip - copy_ip; |
65ea5b03 | 848 | |
0b0122fa | 849 | no_change: |
1ecc798c | 850 | restore_btf(); |
1da177e4 LT |
851 | } |
852 | ||
8533bbe9 MH |
853 | /* |
854 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
855 | * remain disabled thoroughout this function. | |
856 | */ | |
857 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | |
1da177e4 | 858 | { |
e7a510f9 AM |
859 | struct kprobe *cur = kprobe_running(); |
860 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
861 | ||
862 | if (!cur) | |
1da177e4 LT |
863 | return 0; |
864 | ||
acb5b8a2 YL |
865 | resume_execution(cur, regs, kcb); |
866 | regs->flags |= kcb->kprobe_saved_flags; | |
acb5b8a2 | 867 | |
e7a510f9 AM |
868 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
869 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
870 | cur->post_handler(cur, regs, 0); | |
aa3d7e3d | 871 | } |
1da177e4 | 872 | |
8533bbe9 | 873 | /* Restore back the original saved kprobes variables and continue. */ |
e7a510f9 AM |
874 | if (kcb->kprobe_status == KPROBE_REENTER) { |
875 | restore_previous_kprobe(kcb); | |
aa3d7e3d | 876 | goto out; |
aa3d7e3d | 877 | } |
e7a510f9 | 878 | reset_current_kprobe(); |
aa3d7e3d | 879 | out: |
1da177e4 LT |
880 | preempt_enable_no_resched(); |
881 | ||
882 | /* | |
65ea5b03 | 883 | * if somebody else is singlestepping across a probe point, flags |
1da177e4 LT |
884 | * will have TF set, in which case, continue the remaining processing |
885 | * of do_debug, as if this is not a probe hit. | |
886 | */ | |
053de044 | 887 | if (regs->flags & X86_EFLAGS_TF) |
1da177e4 LT |
888 | return 0; |
889 | ||
890 | return 1; | |
891 | } | |
892 | ||
0f2fbdcb | 893 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
1da177e4 | 894 | { |
e7a510f9 AM |
895 | struct kprobe *cur = kprobe_running(); |
896 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
897 | ||
d6be29b8 | 898 | switch (kcb->kprobe_status) { |
c28f8966 PP |
899 | case KPROBE_HIT_SS: |
900 | case KPROBE_REENTER: | |
901 | /* | |
902 | * We are here because the instruction being single | |
903 | * stepped caused a page fault. We reset the current | |
65ea5b03 | 904 | * kprobe and the ip points back to the probe address |
c28f8966 PP |
905 | * and allow the page fault handler to continue as a |
906 | * normal page fault. | |
907 | */ | |
65ea5b03 | 908 | regs->ip = (unsigned long)cur->addr; |
8533bbe9 | 909 | regs->flags |= kcb->kprobe_old_flags; |
c28f8966 PP |
910 | if (kcb->kprobe_status == KPROBE_REENTER) |
911 | restore_previous_kprobe(kcb); | |
912 | else | |
913 | reset_current_kprobe(); | |
1da177e4 | 914 | preempt_enable_no_resched(); |
c28f8966 PP |
915 | break; |
916 | case KPROBE_HIT_ACTIVE: | |
917 | case KPROBE_HIT_SSDONE: | |
918 | /* | |
919 | * We increment the nmissed count for accounting, | |
8533bbe9 | 920 | * we can also use npre/npostfault count for accounting |
c28f8966 PP |
921 | * these specific fault cases. |
922 | */ | |
923 | kprobes_inc_nmissed_count(cur); | |
924 | ||
925 | /* | |
926 | * We come here because instructions in the pre/post | |
927 | * handler caused the page_fault, this could happen | |
928 | * if handler tries to access user space by | |
929 | * copy_from_user(), get_user() etc. Let the | |
930 | * user-specified handler try to fix it first. | |
931 | */ | |
932 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
933 | return 1; | |
934 | ||
935 | /* | |
936 | * In case the user-specified fault handler returned | |
937 | * zero, try to fix up. | |
938 | */ | |
d6be29b8 MH |
939 | if (fixup_exception(regs)) |
940 | return 1; | |
6d48583b | 941 | |
c28f8966 | 942 | /* |
8533bbe9 | 943 | * fixup routine could not handle it, |
c28f8966 PP |
944 | * Let do_page_fault() fix it. |
945 | */ | |
946 | break; | |
947 | default: | |
948 | break; | |
1da177e4 LT |
949 | } |
950 | return 0; | |
951 | } | |
952 | ||
953 | /* | |
954 | * Wrapper routine for handling exceptions. | |
955 | */ | |
0f2fbdcb PP |
956 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
957 | unsigned long val, void *data) | |
1da177e4 | 958 | { |
ade1af77 | 959 | struct die_args *args = data; |
66ff2d06 AM |
960 | int ret = NOTIFY_DONE; |
961 | ||
8533bbe9 | 962 | if (args->regs && user_mode_vm(args->regs)) |
2326c770 | 963 | return ret; |
964 | ||
1da177e4 LT |
965 | switch (val) { |
966 | case DIE_INT3: | |
967 | if (kprobe_handler(args->regs)) | |
66ff2d06 | 968 | ret = NOTIFY_STOP; |
1da177e4 LT |
969 | break; |
970 | case DIE_DEBUG: | |
62edab90 P |
971 | if (post_kprobe_handler(args->regs)) { |
972 | /* | |
973 | * Reset the BS bit in dr6 (pointed by args->err) to | |
974 | * denote completion of processing | |
975 | */ | |
976 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; | |
66ff2d06 | 977 | ret = NOTIFY_STOP; |
62edab90 | 978 | } |
1da177e4 LT |
979 | break; |
980 | case DIE_GPF: | |
b506a9d0 QB |
981 | /* |
982 | * To be potentially processing a kprobe fault and to | |
983 | * trust the result from kprobe_running(), we have | |
984 | * be non-preemptible. | |
985 | */ | |
986 | if (!preemptible() && kprobe_running() && | |
1da177e4 | 987 | kprobe_fault_handler(args->regs, args->trapnr)) |
66ff2d06 | 988 | ret = NOTIFY_STOP; |
1da177e4 LT |
989 | break; |
990 | default: | |
991 | break; | |
992 | } | |
66ff2d06 | 993 | return ret; |
1da177e4 LT |
994 | } |
995 | ||
0f2fbdcb | 996 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
997 | { |
998 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
999 | unsigned long addr; | |
e7a510f9 | 1000 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 1001 | |
e7a510f9 | 1002 | kcb->jprobe_saved_regs = *regs; |
8533bbe9 MH |
1003 | kcb->jprobe_saved_sp = stack_addr(regs); |
1004 | addr = (unsigned long)(kcb->jprobe_saved_sp); | |
1005 | ||
1da177e4 LT |
1006 | /* |
1007 | * As Linus pointed out, gcc assumes that the callee | |
1008 | * owns the argument space and could overwrite it, e.g. | |
1009 | * tailcall optimization. So, to be absolutely safe | |
1010 | * we also save and restore enough stack bytes to cover | |
1011 | * the argument area. | |
1012 | */ | |
e7a510f9 | 1013 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
d6be29b8 | 1014 | MIN_STACK_SIZE(addr)); |
053de044 | 1015 | regs->flags &= ~X86_EFLAGS_IF; |
58dfe883 | 1016 | trace_hardirqs_off(); |
65ea5b03 | 1017 | regs->ip = (unsigned long)(jp->entry); |
1da177e4 LT |
1018 | return 1; |
1019 | } | |
1020 | ||
0f2fbdcb | 1021 | void __kprobes jprobe_return(void) |
1da177e4 | 1022 | { |
e7a510f9 AM |
1023 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1024 | ||
d6be29b8 MH |
1025 | asm volatile ( |
1026 | #ifdef CONFIG_X86_64 | |
1027 | " xchg %%rbx,%%rsp \n" | |
1028 | #else | |
1029 | " xchgl %%ebx,%%esp \n" | |
1030 | #endif | |
1031 | " int3 \n" | |
1032 | " .globl jprobe_return_end\n" | |
1033 | " jprobe_return_end: \n" | |
1034 | " nop \n"::"b" | |
1035 | (kcb->jprobe_saved_sp):"memory"); | |
1da177e4 LT |
1036 | } |
1037 | ||
0f2fbdcb | 1038 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 1039 | { |
e7a510f9 | 1040 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
65ea5b03 | 1041 | u8 *addr = (u8 *) (regs->ip - 1); |
1da177e4 LT |
1042 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
1043 | ||
d6be29b8 MH |
1044 | if ((addr > (u8 *) jprobe_return) && |
1045 | (addr < (u8 *) jprobe_return_end)) { | |
8533bbe9 | 1046 | if (stack_addr(regs) != kcb->jprobe_saved_sp) { |
29b6cd79 | 1047 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
d6be29b8 MH |
1048 | printk(KERN_ERR |
1049 | "current sp %p does not match saved sp %p\n", | |
8533bbe9 | 1050 | stack_addr(regs), kcb->jprobe_saved_sp); |
d6be29b8 | 1051 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
1da177e4 | 1052 | show_registers(saved_regs); |
d6be29b8 | 1053 | printk(KERN_ERR "Current registers\n"); |
1da177e4 LT |
1054 | show_registers(regs); |
1055 | BUG(); | |
1056 | } | |
e7a510f9 | 1057 | *regs = kcb->jprobe_saved_regs; |
8533bbe9 MH |
1058 | memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), |
1059 | kcb->jprobes_stack, | |
1060 | MIN_STACK_SIZE(kcb->jprobe_saved_sp)); | |
d217d545 | 1061 | preempt_enable_no_resched(); |
1da177e4 LT |
1062 | return 1; |
1063 | } | |
1064 | return 0; | |
1065 | } | |
ba8af12f | 1066 | |
6772926b | 1067 | int __init arch_init_kprobes(void) |
ba8af12f | 1068 | { |
da07ab03 | 1069 | return 0; |
ba8af12f | 1070 | } |
bf8f6e5b AM |
1071 | |
1072 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) | |
1073 | { | |
bf8f6e5b AM |
1074 | return 0; |
1075 | } |