]>
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> |
b46b3d70 | 51 | #include <linux/kallsyms.h> |
c0f7ac3a | 52 | #include <linux/ftrace.h> |
9ec4b1f3 | 53 | |
8533bbe9 MH |
54 | #include <asm/cacheflush.h> |
55 | #include <asm/desc.h> | |
1da177e4 | 56 | #include <asm/pgtable.h> |
c28f8966 | 57 | #include <asm/uaccess.h> |
19d36ccd | 58 | #include <asm/alternative.h> |
b46b3d70 | 59 | #include <asm/insn.h> |
62edab90 | 60 | #include <asm/debugreg.h> |
1da177e4 | 61 | |
1da177e4 LT |
62 | void jprobe_return_end(void); |
63 | ||
e7a510f9 AM |
64 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
65 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
1da177e4 | 66 | |
98272ed0 | 67 | #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs)) |
8533bbe9 MH |
68 | |
69 | #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\ | |
70 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ | |
71 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ | |
72 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ | |
73 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ | |
74 | << (row % 32)) | |
75 | /* | |
76 | * Undefined/reserved opcodes, conditional jump, Opcode Extension | |
77 | * Groups, and some special opcodes can not boost. | |
78 | */ | |
79 | static const u32 twobyte_is_boostable[256 / 32] = { | |
80 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
81 | /* ---------------------------------------------- */ | |
82 | W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */ | |
83 | W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */ | |
84 | W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */ | |
85 | W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */ | |
86 | W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */ | |
87 | W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */ | |
88 | W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */ | |
89 | W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */ | |
90 | W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */ | |
91 | W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */ | |
92 | W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */ | |
93 | W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */ | |
94 | W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */ | |
95 | W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */ | |
96 | W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */ | |
97 | W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */ | |
98 | /* ----------------------------------------------- */ | |
99 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
100 | }; | |
8533bbe9 MH |
101 | #undef W |
102 | ||
f438d914 MH |
103 | struct kretprobe_blackpoint kretprobe_blacklist[] = { |
104 | {"__switch_to", }, /* This function switches only current task, but | |
105 | doesn't switch kernel stack.*/ | |
106 | {NULL, NULL} /* Terminator */ | |
107 | }; | |
108 | const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist); | |
109 | ||
c0f7ac3a | 110 | static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op) |
aa470140 | 111 | { |
c0f7ac3a MH |
112 | struct __arch_relative_insn { |
113 | u8 op; | |
aa470140 | 114 | s32 raddr; |
c0f7ac3a MH |
115 | } __attribute__((packed)) *insn; |
116 | ||
117 | insn = (struct __arch_relative_insn *)from; | |
118 | insn->raddr = (s32)((long)(to) - ((long)(from) + 5)); | |
119 | insn->op = op; | |
120 | } | |
121 | ||
122 | /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/ | |
123 | static void __kprobes synthesize_reljump(void *from, void *to) | |
124 | { | |
125 | __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE); | |
aa470140 MH |
126 | } |
127 | ||
9930927f | 128 | /* |
567a9fd8 | 129 | * Skip the prefixes of the instruction. |
9930927f | 130 | */ |
567a9fd8 | 131 | static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn) |
9930927f | 132 | { |
567a9fd8 MH |
133 | insn_attr_t attr; |
134 | ||
135 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); | |
136 | while (inat_is_legacy_prefix(attr)) { | |
137 | insn++; | |
138 | attr = inat_get_opcode_attribute((insn_byte_t)*insn); | |
139 | } | |
9930927f | 140 | #ifdef CONFIG_X86_64 |
567a9fd8 MH |
141 | if (inat_is_rex_prefix(attr)) |
142 | insn++; | |
9930927f | 143 | #endif |
567a9fd8 | 144 | return insn; |
9930927f HH |
145 | } |
146 | ||
aa470140 | 147 | /* |
d6be29b8 MH |
148 | * Returns non-zero if opcode is boostable. |
149 | * RIP relative instructions are adjusted at copying time in 64 bits mode | |
aa470140 | 150 | */ |
e7b5e11e | 151 | static int __kprobes can_boost(kprobe_opcode_t *opcodes) |
aa470140 | 152 | { |
aa470140 MH |
153 | kprobe_opcode_t opcode; |
154 | kprobe_opcode_t *orig_opcodes = opcodes; | |
155 | ||
cde5edbd | 156 | if (search_exception_tables((unsigned long)opcodes)) |
30390880 MH |
157 | return 0; /* Page fault may occur on this address. */ |
158 | ||
aa470140 MH |
159 | retry: |
160 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
161 | return 0; | |
162 | opcode = *(opcodes++); | |
163 | ||
164 | /* 2nd-byte opcode */ | |
165 | if (opcode == 0x0f) { | |
166 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
167 | return 0; | |
8533bbe9 MH |
168 | return test_bit(*opcodes, |
169 | (unsigned long *)twobyte_is_boostable); | |
aa470140 MH |
170 | } |
171 | ||
172 | switch (opcode & 0xf0) { | |
d6be29b8 | 173 | #ifdef CONFIG_X86_64 |
aa470140 MH |
174 | case 0x40: |
175 | goto retry; /* REX prefix is boostable */ | |
d6be29b8 | 176 | #endif |
aa470140 MH |
177 | case 0x60: |
178 | if (0x63 < opcode && opcode < 0x67) | |
179 | goto retry; /* prefixes */ | |
180 | /* can't boost Address-size override and bound */ | |
181 | return (opcode != 0x62 && opcode != 0x67); | |
182 | case 0x70: | |
183 | return 0; /* can't boost conditional jump */ | |
184 | case 0xc0: | |
185 | /* can't boost software-interruptions */ | |
186 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; | |
187 | case 0xd0: | |
188 | /* can boost AA* and XLAT */ | |
189 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); | |
190 | case 0xe0: | |
191 | /* can boost in/out and absolute jmps */ | |
192 | return ((opcode & 0x04) || opcode == 0xea); | |
193 | case 0xf0: | |
194 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) | |
195 | goto retry; /* lock/rep(ne) prefix */ | |
196 | /* clear and set flags are boostable */ | |
197 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); | |
198 | default: | |
199 | /* segment override prefixes are boostable */ | |
200 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) | |
201 | goto retry; /* prefixes */ | |
202 | /* CS override prefix and call are not boostable */ | |
203 | return (opcode != 0x2e && opcode != 0x9a); | |
204 | } | |
205 | } | |
206 | ||
b46b3d70 MH |
207 | /* Recover the probed instruction at addr for further analysis. */ |
208 | static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr) | |
209 | { | |
210 | struct kprobe *kp; | |
211 | kp = get_kprobe((void *)addr); | |
212 | if (!kp) | |
213 | return -EINVAL; | |
214 | ||
215 | /* | |
216 | * Basically, kp->ainsn.insn has an original instruction. | |
217 | * However, RIP-relative instruction can not do single-stepping | |
c0f7ac3a | 218 | * at different place, __copy_instruction() tweaks the displacement of |
b46b3d70 MH |
219 | * that instruction. In that case, we can't recover the instruction |
220 | * from the kp->ainsn.insn. | |
221 | * | |
222 | * On the other hand, kp->opcode has a copy of the first byte of | |
223 | * the probed instruction, which is overwritten by int3. And | |
224 | * the instruction at kp->addr is not modified by kprobes except | |
225 | * for the first byte, we can recover the original instruction | |
226 | * from it and kp->opcode. | |
227 | */ | |
228 | memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); | |
229 | buf[0] = kp->opcode; | |
230 | return 0; | |
231 | } | |
232 | ||
233 | /* Dummy buffers for kallsyms_lookup */ | |
234 | static char __dummy_buf[KSYM_NAME_LEN]; | |
235 | ||
236 | /* Check if paddr is at an instruction boundary */ | |
237 | static int __kprobes can_probe(unsigned long paddr) | |
238 | { | |
239 | int ret; | |
240 | unsigned long addr, offset = 0; | |
241 | struct insn insn; | |
242 | kprobe_opcode_t buf[MAX_INSN_SIZE]; | |
243 | ||
244 | if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf)) | |
245 | return 0; | |
246 | ||
247 | /* Decode instructions */ | |
248 | addr = paddr - offset; | |
249 | while (addr < paddr) { | |
250 | kernel_insn_init(&insn, (void *)addr); | |
251 | insn_get_opcode(&insn); | |
252 | ||
253 | /* | |
254 | * Check if the instruction has been modified by another | |
255 | * kprobe, in which case we replace the breakpoint by the | |
256 | * original instruction in our buffer. | |
257 | */ | |
258 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { | |
259 | ret = recover_probed_instruction(buf, addr); | |
260 | if (ret) | |
261 | /* | |
262 | * Another debugging subsystem might insert | |
263 | * this breakpoint. In that case, we can't | |
264 | * recover it. | |
265 | */ | |
266 | return 0; | |
267 | kernel_insn_init(&insn, buf); | |
268 | } | |
269 | insn_get_length(&insn); | |
270 | addr += insn.length; | |
271 | } | |
272 | ||
273 | return (addr == paddr); | |
274 | } | |
275 | ||
1da177e4 | 276 | /* |
d6be29b8 | 277 | * Returns non-zero if opcode modifies the interrupt flag. |
1da177e4 | 278 | */ |
8645419c | 279 | static int __kprobes is_IF_modifier(kprobe_opcode_t *insn) |
1da177e4 | 280 | { |
567a9fd8 MH |
281 | /* Skip prefixes */ |
282 | insn = skip_prefixes(insn); | |
283 | ||
1da177e4 LT |
284 | switch (*insn) { |
285 | case 0xfa: /* cli */ | |
286 | case 0xfb: /* sti */ | |
287 | case 0xcf: /* iret/iretd */ | |
288 | case 0x9d: /* popf/popfd */ | |
289 | return 1; | |
290 | } | |
9930927f | 291 | |
1da177e4 LT |
292 | return 0; |
293 | } | |
294 | ||
295 | /* | |
c0f7ac3a MH |
296 | * Copy an instruction and adjust the displacement if the instruction |
297 | * uses the %rip-relative addressing mode. | |
aa470140 | 298 | * If it does, Return the address of the 32-bit displacement word. |
1da177e4 | 299 | * If not, return null. |
31f80e45 | 300 | * Only applicable to 64-bit x86. |
1da177e4 | 301 | */ |
c0f7ac3a | 302 | static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover) |
1da177e4 | 303 | { |
89ae465b | 304 | struct insn insn; |
c0f7ac3a MH |
305 | int ret; |
306 | kprobe_opcode_t buf[MAX_INSN_SIZE]; | |
1da177e4 | 307 | |
c0f7ac3a MH |
308 | kernel_insn_init(&insn, src); |
309 | if (recover) { | |
310 | insn_get_opcode(&insn); | |
311 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { | |
312 | ret = recover_probed_instruction(buf, | |
313 | (unsigned long)src); | |
314 | if (ret) | |
315 | return 0; | |
316 | kernel_insn_init(&insn, buf); | |
317 | } | |
318 | } | |
319 | insn_get_length(&insn); | |
320 | memcpy(dest, insn.kaddr, insn.length); | |
321 | ||
322 | #ifdef CONFIG_X86_64 | |
89ae465b MH |
323 | if (insn_rip_relative(&insn)) { |
324 | s64 newdisp; | |
325 | u8 *disp; | |
c0f7ac3a | 326 | kernel_insn_init(&insn, dest); |
89ae465b MH |
327 | insn_get_displacement(&insn); |
328 | /* | |
329 | * The copied instruction uses the %rip-relative addressing | |
330 | * mode. Adjust the displacement for the difference between | |
331 | * the original location of this instruction and the location | |
332 | * of the copy that will actually be run. The tricky bit here | |
333 | * is making sure that the sign extension happens correctly in | |
334 | * this calculation, since we need a signed 32-bit result to | |
335 | * be sign-extended to 64 bits when it's added to the %rip | |
336 | * value and yield the same 64-bit result that the sign- | |
337 | * extension of the original signed 32-bit displacement would | |
338 | * have given. | |
339 | */ | |
c0f7ac3a MH |
340 | newdisp = (u8 *) src + (s64) insn.displacement.value - |
341 | (u8 *) dest; | |
89ae465b | 342 | BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */ |
c0f7ac3a | 343 | disp = (u8 *) dest + insn_offset_displacement(&insn); |
89ae465b | 344 | *(s32 *) disp = (s32) newdisp; |
1da177e4 | 345 | } |
d6be29b8 | 346 | #endif |
c0f7ac3a | 347 | return insn.length; |
31f80e45 | 348 | } |
1da177e4 | 349 | |
f709b122 | 350 | static void __kprobes arch_copy_kprobe(struct kprobe *p) |
1da177e4 | 351 | { |
c0f7ac3a MH |
352 | /* |
353 | * Copy an instruction without recovering int3, because it will be | |
354 | * put by another subsystem. | |
355 | */ | |
356 | __copy_instruction(p->ainsn.insn, p->addr, 0); | |
31f80e45 | 357 | |
8533bbe9 | 358 | if (can_boost(p->addr)) |
aa470140 | 359 | p->ainsn.boostable = 0; |
8533bbe9 | 360 | else |
aa470140 | 361 | p->ainsn.boostable = -1; |
8533bbe9 | 362 | |
7e1048b1 | 363 | p->opcode = *p->addr; |
1da177e4 LT |
364 | } |
365 | ||
8533bbe9 MH |
366 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
367 | { | |
4554dbcb MH |
368 | if (alternatives_text_reserved(p->addr, p->addr)) |
369 | return -EINVAL; | |
370 | ||
b46b3d70 MH |
371 | if (!can_probe((unsigned long)p->addr)) |
372 | return -EILSEQ; | |
8533bbe9 MH |
373 | /* insn: must be on special executable page on x86. */ |
374 | p->ainsn.insn = get_insn_slot(); | |
375 | if (!p->ainsn.insn) | |
376 | return -ENOMEM; | |
377 | arch_copy_kprobe(p); | |
378 | return 0; | |
379 | } | |
380 | ||
0f2fbdcb | 381 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 382 | { |
19d36ccd | 383 | text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1); |
1da177e4 LT |
384 | } |
385 | ||
0f2fbdcb | 386 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
1da177e4 | 387 | { |
19d36ccd | 388 | text_poke(p->addr, &p->opcode, 1); |
7e1048b1 RL |
389 | } |
390 | ||
0498b635 | 391 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
7e1048b1 | 392 | { |
12941560 MH |
393 | if (p->ainsn.insn) { |
394 | free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1)); | |
395 | p->ainsn.insn = NULL; | |
396 | } | |
1da177e4 LT |
397 | } |
398 | ||
3b60211c | 399 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 400 | { |
e7a510f9 AM |
401 | kcb->prev_kprobe.kp = kprobe_running(); |
402 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
8533bbe9 MH |
403 | kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags; |
404 | kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags; | |
aa3d7e3d PP |
405 | } |
406 | ||
3b60211c | 407 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
aa3d7e3d | 408 | { |
e7a510f9 AM |
409 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
410 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
8533bbe9 MH |
411 | kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags; |
412 | kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags; | |
aa3d7e3d PP |
413 | } |
414 | ||
3b60211c | 415 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
e7a510f9 | 416 | struct kprobe_ctlblk *kcb) |
aa3d7e3d | 417 | { |
e7a510f9 | 418 | __get_cpu_var(current_kprobe) = p; |
8533bbe9 | 419 | kcb->kprobe_saved_flags = kcb->kprobe_old_flags |
053de044 | 420 | = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF)); |
aa3d7e3d | 421 | if (is_IF_modifier(p->ainsn.insn)) |
053de044 | 422 | kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF; |
aa3d7e3d PP |
423 | } |
424 | ||
e7b5e11e | 425 | static void __kprobes clear_btf(void) |
1ecc798c | 426 | { |
ea8e61b7 PZ |
427 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
428 | unsigned long debugctl = get_debugctlmsr(); | |
429 | ||
430 | debugctl &= ~DEBUGCTLMSR_BTF; | |
431 | update_debugctlmsr(debugctl); | |
432 | } | |
1ecc798c RM |
433 | } |
434 | ||
e7b5e11e | 435 | static void __kprobes restore_btf(void) |
1ecc798c | 436 | { |
ea8e61b7 PZ |
437 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
438 | unsigned long debugctl = get_debugctlmsr(); | |
439 | ||
440 | debugctl |= DEBUGCTLMSR_BTF; | |
441 | update_debugctlmsr(debugctl); | |
442 | } | |
1ecc798c RM |
443 | } |
444 | ||
4c4308cb | 445 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
0f2fbdcb | 446 | struct pt_regs *regs) |
73649dab | 447 | { |
8533bbe9 | 448 | unsigned long *sara = stack_addr(regs); |
ba8af12f | 449 | |
4c4308cb | 450 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
8533bbe9 | 451 | |
4c4308cb CH |
452 | /* Replace the return addr with trampoline addr */ |
453 | *sara = (unsigned long) &kretprobe_trampoline; | |
73649dab | 454 | } |
f315decb | 455 | |
c0f7ac3a MH |
456 | #ifdef CONFIG_OPTPROBES |
457 | static int __kprobes setup_detour_execution(struct kprobe *p, | |
458 | struct pt_regs *regs, | |
459 | int reenter); | |
460 | #else | |
461 | #define setup_detour_execution(p, regs, reenter) (0) | |
462 | #endif | |
463 | ||
f315decb | 464 | static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs, |
0f94eb63 | 465 | struct kprobe_ctlblk *kcb, int reenter) |
f315decb | 466 | { |
c0f7ac3a MH |
467 | if (setup_detour_execution(p, regs, reenter)) |
468 | return; | |
469 | ||
615d0ebb | 470 | #if !defined(CONFIG_PREEMPT) |
f315decb AS |
471 | if (p->ainsn.boostable == 1 && !p->post_handler) { |
472 | /* Boost up -- we can execute copied instructions directly */ | |
0f94eb63 MH |
473 | if (!reenter) |
474 | reset_current_kprobe(); | |
475 | /* | |
476 | * Reentering boosted probe doesn't reset current_kprobe, | |
477 | * nor set current_kprobe, because it doesn't use single | |
478 | * stepping. | |
479 | */ | |
f315decb AS |
480 | regs->ip = (unsigned long)p->ainsn.insn; |
481 | preempt_enable_no_resched(); | |
482 | return; | |
483 | } | |
484 | #endif | |
0f94eb63 MH |
485 | if (reenter) { |
486 | save_previous_kprobe(kcb); | |
487 | set_current_kprobe(p, regs, kcb); | |
488 | kcb->kprobe_status = KPROBE_REENTER; | |
489 | } else | |
490 | kcb->kprobe_status = KPROBE_HIT_SS; | |
491 | /* Prepare real single stepping */ | |
492 | clear_btf(); | |
493 | regs->flags |= X86_EFLAGS_TF; | |
494 | regs->flags &= ~X86_EFLAGS_IF; | |
495 | /* single step inline if the instruction is an int3 */ | |
496 | if (p->opcode == BREAKPOINT_INSTRUCTION) | |
497 | regs->ip = (unsigned long)p->addr; | |
498 | else | |
499 | regs->ip = (unsigned long)p->ainsn.insn; | |
f315decb AS |
500 | } |
501 | ||
40102d4a HH |
502 | /* |
503 | * We have reentered the kprobe_handler(), since another probe was hit while | |
504 | * within the handler. We save the original kprobes variables and just single | |
505 | * step on the instruction of the new probe without calling any user handlers. | |
506 | */ | |
59e87cdc MH |
507 | static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs, |
508 | struct kprobe_ctlblk *kcb) | |
40102d4a | 509 | { |
f315decb AS |
510 | switch (kcb->kprobe_status) { |
511 | case KPROBE_HIT_SSDONE: | |
f315decb | 512 | case KPROBE_HIT_ACTIVE: |
fb8830e7 | 513 | kprobes_inc_nmissed_count(p); |
0f94eb63 | 514 | setup_singlestep(p, regs, kcb, 1); |
f315decb AS |
515 | break; |
516 | case KPROBE_HIT_SS: | |
e9afe9e1 MH |
517 | /* A probe has been hit in the codepath leading up to, or just |
518 | * after, single-stepping of a probed instruction. This entire | |
519 | * codepath should strictly reside in .kprobes.text section. | |
520 | * Raise a BUG or we'll continue in an endless reentering loop | |
521 | * and eventually a stack overflow. | |
522 | */ | |
523 | printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n", | |
524 | p->addr); | |
525 | dump_kprobe(p); | |
526 | BUG(); | |
f315decb AS |
527 | default: |
528 | /* impossible cases */ | |
529 | WARN_ON(1); | |
fb8830e7 | 530 | return 0; |
59e87cdc | 531 | } |
f315decb | 532 | |
59e87cdc | 533 | return 1; |
40102d4a | 534 | } |
73649dab | 535 | |
8533bbe9 MH |
536 | /* |
537 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
af901ca1 | 538 | * remain disabled throughout this function. |
8533bbe9 MH |
539 | */ |
540 | static int __kprobes kprobe_handler(struct pt_regs *regs) | |
1da177e4 | 541 | { |
8533bbe9 | 542 | kprobe_opcode_t *addr; |
f315decb | 543 | struct kprobe *p; |
d217d545 AM |
544 | struct kprobe_ctlblk *kcb; |
545 | ||
8533bbe9 | 546 | addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t)); |
d217d545 AM |
547 | /* |
548 | * We don't want to be preempted for the entire | |
f315decb AS |
549 | * duration of kprobe processing. We conditionally |
550 | * re-enable preemption at the end of this function, | |
551 | * and also in reenter_kprobe() and setup_singlestep(). | |
d217d545 AM |
552 | */ |
553 | preempt_disable(); | |
1da177e4 | 554 | |
f315decb | 555 | kcb = get_kprobe_ctlblk(); |
b9760156 | 556 | p = get_kprobe(addr); |
f315decb | 557 | |
b9760156 | 558 | if (p) { |
b9760156 | 559 | if (kprobe_running()) { |
f315decb AS |
560 | if (reenter_kprobe(p, regs, kcb)) |
561 | return 1; | |
1da177e4 | 562 | } else { |
b9760156 HH |
563 | set_current_kprobe(p, regs, kcb); |
564 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
f315decb | 565 | |
1da177e4 | 566 | /* |
f315decb AS |
567 | * If we have no pre-handler or it returned 0, we |
568 | * continue with normal processing. If we have a | |
569 | * pre-handler and it returned non-zero, it prepped | |
570 | * for calling the break_handler below on re-entry | |
571 | * for jprobe processing, so get out doing nothing | |
572 | * more here. | |
1da177e4 | 573 | */ |
f315decb | 574 | if (!p->pre_handler || !p->pre_handler(p, regs)) |
0f94eb63 | 575 | setup_singlestep(p, regs, kcb, 0); |
f315decb | 576 | return 1; |
b9760156 | 577 | } |
829e9245 MH |
578 | } else if (*addr != BREAKPOINT_INSTRUCTION) { |
579 | /* | |
580 | * The breakpoint instruction was removed right | |
581 | * after we hit it. Another cpu has removed | |
582 | * either a probepoint or a debugger breakpoint | |
583 | * at this address. In either case, no further | |
584 | * handling of this interrupt is appropriate. | |
585 | * Back up over the (now missing) int3 and run | |
586 | * the original instruction. | |
587 | */ | |
588 | regs->ip = (unsigned long)addr; | |
589 | preempt_enable_no_resched(); | |
590 | return 1; | |
f315decb AS |
591 | } else if (kprobe_running()) { |
592 | p = __get_cpu_var(current_kprobe); | |
593 | if (p->break_handler && p->break_handler(p, regs)) { | |
0f94eb63 | 594 | setup_singlestep(p, regs, kcb, 0); |
f315decb | 595 | return 1; |
1da177e4 | 596 | } |
f315decb | 597 | } /* else: not a kprobe fault; let the kernel handle it */ |
1da177e4 | 598 | |
d217d545 | 599 | preempt_enable_no_resched(); |
f315decb | 600 | return 0; |
1da177e4 LT |
601 | } |
602 | ||
f007ea26 MH |
603 | #ifdef CONFIG_X86_64 |
604 | #define SAVE_REGS_STRING \ | |
605 | /* Skip cs, ip, orig_ax. */ \ | |
606 | " subq $24, %rsp\n" \ | |
607 | " pushq %rdi\n" \ | |
608 | " pushq %rsi\n" \ | |
609 | " pushq %rdx\n" \ | |
610 | " pushq %rcx\n" \ | |
611 | " pushq %rax\n" \ | |
612 | " pushq %r8\n" \ | |
613 | " pushq %r9\n" \ | |
614 | " pushq %r10\n" \ | |
615 | " pushq %r11\n" \ | |
616 | " pushq %rbx\n" \ | |
617 | " pushq %rbp\n" \ | |
618 | " pushq %r12\n" \ | |
619 | " pushq %r13\n" \ | |
620 | " pushq %r14\n" \ | |
621 | " pushq %r15\n" | |
622 | #define RESTORE_REGS_STRING \ | |
623 | " popq %r15\n" \ | |
624 | " popq %r14\n" \ | |
625 | " popq %r13\n" \ | |
626 | " popq %r12\n" \ | |
627 | " popq %rbp\n" \ | |
628 | " popq %rbx\n" \ | |
629 | " popq %r11\n" \ | |
630 | " popq %r10\n" \ | |
631 | " popq %r9\n" \ | |
632 | " popq %r8\n" \ | |
633 | " popq %rax\n" \ | |
634 | " popq %rcx\n" \ | |
635 | " popq %rdx\n" \ | |
636 | " popq %rsi\n" \ | |
637 | " popq %rdi\n" \ | |
638 | /* Skip orig_ax, ip, cs */ \ | |
639 | " addq $24, %rsp\n" | |
640 | #else | |
641 | #define SAVE_REGS_STRING \ | |
642 | /* Skip cs, ip, orig_ax and gs. */ \ | |
643 | " subl $16, %esp\n" \ | |
644 | " pushl %fs\n" \ | |
f007ea26 | 645 | " pushl %es\n" \ |
a1974798 | 646 | " pushl %ds\n" \ |
f007ea26 MH |
647 | " pushl %eax\n" \ |
648 | " pushl %ebp\n" \ | |
649 | " pushl %edi\n" \ | |
650 | " pushl %esi\n" \ | |
651 | " pushl %edx\n" \ | |
652 | " pushl %ecx\n" \ | |
653 | " pushl %ebx\n" | |
654 | #define RESTORE_REGS_STRING \ | |
655 | " popl %ebx\n" \ | |
656 | " popl %ecx\n" \ | |
657 | " popl %edx\n" \ | |
658 | " popl %esi\n" \ | |
659 | " popl %edi\n" \ | |
660 | " popl %ebp\n" \ | |
661 | " popl %eax\n" \ | |
662 | /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\ | |
663 | " addl $24, %esp\n" | |
664 | #endif | |
665 | ||
73649dab | 666 | /* |
da07ab03 MH |
667 | * When a retprobed function returns, this code saves registers and |
668 | * calls trampoline_handler() runs, which calls the kretprobe's handler. | |
73649dab | 669 | */ |
f1452d42 | 670 | static void __used __kprobes kretprobe_trampoline_holder(void) |
1017579a | 671 | { |
d6be29b8 MH |
672 | asm volatile ( |
673 | ".global kretprobe_trampoline\n" | |
da07ab03 | 674 | "kretprobe_trampoline: \n" |
d6be29b8 | 675 | #ifdef CONFIG_X86_64 |
da07ab03 MH |
676 | /* We don't bother saving the ss register */ |
677 | " pushq %rsp\n" | |
678 | " pushfq\n" | |
f007ea26 | 679 | SAVE_REGS_STRING |
da07ab03 MH |
680 | " movq %rsp, %rdi\n" |
681 | " call trampoline_handler\n" | |
682 | /* Replace saved sp with true return address. */ | |
683 | " movq %rax, 152(%rsp)\n" | |
f007ea26 | 684 | RESTORE_REGS_STRING |
da07ab03 | 685 | " popfq\n" |
d6be29b8 MH |
686 | #else |
687 | " pushf\n" | |
f007ea26 | 688 | SAVE_REGS_STRING |
d6be29b8 MH |
689 | " movl %esp, %eax\n" |
690 | " call trampoline_handler\n" | |
691 | /* Move flags to cs */ | |
fee039a1 MH |
692 | " movl 56(%esp), %edx\n" |
693 | " movl %edx, 52(%esp)\n" | |
d6be29b8 | 694 | /* Replace saved flags with true return address. */ |
fee039a1 | 695 | " movl %eax, 56(%esp)\n" |
f007ea26 | 696 | RESTORE_REGS_STRING |
d6be29b8 MH |
697 | " popf\n" |
698 | #endif | |
da07ab03 | 699 | " ret\n"); |
1017579a | 700 | } |
73649dab RL |
701 | |
702 | /* | |
da07ab03 | 703 | * Called from kretprobe_trampoline |
73649dab | 704 | */ |
f1452d42 | 705 | static __used __kprobes void *trampoline_handler(struct pt_regs *regs) |
73649dab | 706 | { |
62c27be0 | 707 | struct kretprobe_instance *ri = NULL; |
99219a3f | 708 | struct hlist_head *head, empty_rp; |
62c27be0 | 709 | struct hlist_node *node, *tmp; |
991a51d8 | 710 | unsigned long flags, orig_ret_address = 0; |
d6be29b8 | 711 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
73649dab | 712 | |
99219a3f | 713 | INIT_HLIST_HEAD(&empty_rp); |
ef53d9c5 | 714 | kretprobe_hash_lock(current, &head, &flags); |
8533bbe9 | 715 | /* fixup registers */ |
d6be29b8 | 716 | #ifdef CONFIG_X86_64 |
da07ab03 | 717 | regs->cs = __KERNEL_CS; |
d6be29b8 MH |
718 | #else |
719 | regs->cs = __KERNEL_CS | get_kernel_rpl(); | |
fee039a1 | 720 | regs->gs = 0; |
d6be29b8 | 721 | #endif |
da07ab03 | 722 | regs->ip = trampoline_address; |
8533bbe9 | 723 | regs->orig_ax = ~0UL; |
73649dab | 724 | |
ba8af12f RL |
725 | /* |
726 | * It is possible to have multiple instances associated with a given | |
8533bbe9 | 727 | * task either because multiple functions in the call path have |
025dfdaf | 728 | * return probes installed on them, and/or more than one |
ba8af12f RL |
729 | * return probe was registered for a target function. |
730 | * | |
731 | * We can handle this because: | |
8533bbe9 | 732 | * - instances are always pushed into the head of the list |
ba8af12f | 733 | * - when multiple return probes are registered for the same |
8533bbe9 MH |
734 | * function, the (chronologically) first instance's ret_addr |
735 | * will be the real return address, and all the rest will | |
736 | * point to kretprobe_trampoline. | |
ba8af12f RL |
737 | */ |
738 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
62c27be0 | 739 | if (ri->task != current) |
ba8af12f | 740 | /* another task is sharing our hash bucket */ |
62c27be0 | 741 | continue; |
ba8af12f | 742 | |
da07ab03 MH |
743 | if (ri->rp && ri->rp->handler) { |
744 | __get_cpu_var(current_kprobe) = &ri->rp->kp; | |
745 | get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; | |
ba8af12f | 746 | ri->rp->handler(ri, regs); |
da07ab03 MH |
747 | __get_cpu_var(current_kprobe) = NULL; |
748 | } | |
ba8af12f RL |
749 | |
750 | orig_ret_address = (unsigned long)ri->ret_addr; | |
99219a3f | 751 | recycle_rp_inst(ri, &empty_rp); |
ba8af12f RL |
752 | |
753 | if (orig_ret_address != trampoline_address) | |
754 | /* | |
755 | * This is the real return address. Any other | |
756 | * instances associated with this task are for | |
757 | * other calls deeper on the call stack | |
758 | */ | |
759 | break; | |
73649dab | 760 | } |
ba8af12f | 761 | |
0f95b7fc | 762 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
ba8af12f | 763 | |
ef53d9c5 | 764 | kretprobe_hash_unlock(current, &flags); |
ba8af12f | 765 | |
99219a3f | 766 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
767 | hlist_del(&ri->hlist); | |
768 | kfree(ri); | |
769 | } | |
da07ab03 | 770 | return (void *)orig_ret_address; |
73649dab RL |
771 | } |
772 | ||
1da177e4 LT |
773 | /* |
774 | * Called after single-stepping. p->addr is the address of the | |
775 | * instruction whose first byte has been replaced by the "int 3" | |
776 | * instruction. To avoid the SMP problems that can occur when we | |
777 | * temporarily put back the original opcode to single-step, we | |
778 | * single-stepped a copy of the instruction. The address of this | |
779 | * copy is p->ainsn.insn. | |
780 | * | |
781 | * This function prepares to return from the post-single-step | |
782 | * interrupt. We have to fix up the stack as follows: | |
783 | * | |
784 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
65ea5b03 | 785 | * the new ip is relative to the copied instruction. We need to make |
1da177e4 LT |
786 | * it relative to the original instruction. |
787 | * | |
788 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
65ea5b03 | 789 | * flags are set in the just-pushed flags, and may need to be cleared. |
1da177e4 LT |
790 | * |
791 | * 2) If the single-stepped instruction was a call, the return address | |
792 | * that is atop the stack is the address following the copied instruction. | |
793 | * We need to make it the address following the original instruction. | |
aa470140 MH |
794 | * |
795 | * If this is the first time we've single-stepped the instruction at | |
796 | * this probepoint, and the instruction is boostable, boost it: add a | |
797 | * jump instruction after the copied instruction, that jumps to the next | |
798 | * instruction after the probepoint. | |
1da177e4 | 799 | */ |
e7a510f9 AM |
800 | static void __kprobes resume_execution(struct kprobe *p, |
801 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) | |
1da177e4 | 802 | { |
8533bbe9 MH |
803 | unsigned long *tos = stack_addr(regs); |
804 | unsigned long copy_ip = (unsigned long)p->ainsn.insn; | |
805 | unsigned long orig_ip = (unsigned long)p->addr; | |
1da177e4 LT |
806 | kprobe_opcode_t *insn = p->ainsn.insn; |
807 | ||
567a9fd8 MH |
808 | /* Skip prefixes */ |
809 | insn = skip_prefixes(insn); | |
1da177e4 | 810 | |
053de044 | 811 | regs->flags &= ~X86_EFLAGS_TF; |
1da177e4 | 812 | switch (*insn) { |
0b0122fa | 813 | case 0x9c: /* pushfl */ |
053de044 | 814 | *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF); |
8533bbe9 | 815 | *tos |= kcb->kprobe_old_flags; |
1da177e4 | 816 | break; |
0b0122fa MH |
817 | case 0xc2: /* iret/ret/lret */ |
818 | case 0xc3: | |
0b9e2cac | 819 | case 0xca: |
0b0122fa MH |
820 | case 0xcb: |
821 | case 0xcf: | |
822 | case 0xea: /* jmp absolute -- ip is correct */ | |
823 | /* ip is already adjusted, no more changes required */ | |
aa470140 | 824 | p->ainsn.boostable = 1; |
0b0122fa MH |
825 | goto no_change; |
826 | case 0xe8: /* call relative - Fix return addr */ | |
8533bbe9 | 827 | *tos = orig_ip + (*tos - copy_ip); |
1da177e4 | 828 | break; |
e7b5e11e | 829 | #ifdef CONFIG_X86_32 |
d6be29b8 MH |
830 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
831 | *tos = orig_ip + (*tos - copy_ip); | |
832 | goto no_change; | |
833 | #endif | |
1da177e4 | 834 | case 0xff: |
dc49e344 | 835 | if ((insn[1] & 0x30) == 0x10) { |
8533bbe9 MH |
836 | /* |
837 | * call absolute, indirect | |
838 | * Fix return addr; ip is correct. | |
839 | * But this is not boostable | |
840 | */ | |
841 | *tos = orig_ip + (*tos - copy_ip); | |
0b0122fa | 842 | goto no_change; |
8533bbe9 MH |
843 | } else if (((insn[1] & 0x31) == 0x20) || |
844 | ((insn[1] & 0x31) == 0x21)) { | |
845 | /* | |
846 | * jmp near and far, absolute indirect | |
847 | * ip is correct. And this is boostable | |
848 | */ | |
aa470140 | 849 | p->ainsn.boostable = 1; |
0b0122fa | 850 | goto no_change; |
1da177e4 | 851 | } |
1da177e4 LT |
852 | default: |
853 | break; | |
854 | } | |
855 | ||
aa470140 | 856 | if (p->ainsn.boostable == 0) { |
8533bbe9 MH |
857 | if ((regs->ip > copy_ip) && |
858 | (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) { | |
aa470140 MH |
859 | /* |
860 | * These instructions can be executed directly if it | |
861 | * jumps back to correct address. | |
862 | */ | |
c0f7ac3a MH |
863 | synthesize_reljump((void *)regs->ip, |
864 | (void *)orig_ip + (regs->ip - copy_ip)); | |
aa470140 MH |
865 | p->ainsn.boostable = 1; |
866 | } else { | |
867 | p->ainsn.boostable = -1; | |
868 | } | |
869 | } | |
870 | ||
8533bbe9 | 871 | regs->ip += orig_ip - copy_ip; |
65ea5b03 | 872 | |
0b0122fa | 873 | no_change: |
1ecc798c | 874 | restore_btf(); |
1da177e4 LT |
875 | } |
876 | ||
8533bbe9 MH |
877 | /* |
878 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
af901ca1 | 879 | * remain disabled throughout this function. |
8533bbe9 MH |
880 | */ |
881 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | |
1da177e4 | 882 | { |
e7a510f9 AM |
883 | struct kprobe *cur = kprobe_running(); |
884 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
885 | ||
886 | if (!cur) | |
1da177e4 LT |
887 | return 0; |
888 | ||
acb5b8a2 YL |
889 | resume_execution(cur, regs, kcb); |
890 | regs->flags |= kcb->kprobe_saved_flags; | |
acb5b8a2 | 891 | |
e7a510f9 AM |
892 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
893 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
894 | cur->post_handler(cur, regs, 0); | |
aa3d7e3d | 895 | } |
1da177e4 | 896 | |
8533bbe9 | 897 | /* Restore back the original saved kprobes variables and continue. */ |
e7a510f9 AM |
898 | if (kcb->kprobe_status == KPROBE_REENTER) { |
899 | restore_previous_kprobe(kcb); | |
aa3d7e3d | 900 | goto out; |
aa3d7e3d | 901 | } |
e7a510f9 | 902 | reset_current_kprobe(); |
aa3d7e3d | 903 | out: |
1da177e4 LT |
904 | preempt_enable_no_resched(); |
905 | ||
906 | /* | |
65ea5b03 | 907 | * if somebody else is singlestepping across a probe point, flags |
1da177e4 LT |
908 | * will have TF set, in which case, continue the remaining processing |
909 | * of do_debug, as if this is not a probe hit. | |
910 | */ | |
053de044 | 911 | if (regs->flags & X86_EFLAGS_TF) |
1da177e4 LT |
912 | return 0; |
913 | ||
914 | return 1; | |
915 | } | |
916 | ||
0f2fbdcb | 917 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
1da177e4 | 918 | { |
e7a510f9 AM |
919 | struct kprobe *cur = kprobe_running(); |
920 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
921 | ||
d6be29b8 | 922 | switch (kcb->kprobe_status) { |
c28f8966 PP |
923 | case KPROBE_HIT_SS: |
924 | case KPROBE_REENTER: | |
925 | /* | |
926 | * We are here because the instruction being single | |
927 | * stepped caused a page fault. We reset the current | |
65ea5b03 | 928 | * kprobe and the ip points back to the probe address |
c28f8966 PP |
929 | * and allow the page fault handler to continue as a |
930 | * normal page fault. | |
931 | */ | |
65ea5b03 | 932 | regs->ip = (unsigned long)cur->addr; |
8533bbe9 | 933 | regs->flags |= kcb->kprobe_old_flags; |
c28f8966 PP |
934 | if (kcb->kprobe_status == KPROBE_REENTER) |
935 | restore_previous_kprobe(kcb); | |
936 | else | |
937 | reset_current_kprobe(); | |
1da177e4 | 938 | preempt_enable_no_resched(); |
c28f8966 PP |
939 | break; |
940 | case KPROBE_HIT_ACTIVE: | |
941 | case KPROBE_HIT_SSDONE: | |
942 | /* | |
943 | * We increment the nmissed count for accounting, | |
8533bbe9 | 944 | * we can also use npre/npostfault count for accounting |
c28f8966 PP |
945 | * these specific fault cases. |
946 | */ | |
947 | kprobes_inc_nmissed_count(cur); | |
948 | ||
949 | /* | |
950 | * We come here because instructions in the pre/post | |
951 | * handler caused the page_fault, this could happen | |
952 | * if handler tries to access user space by | |
953 | * copy_from_user(), get_user() etc. Let the | |
954 | * user-specified handler try to fix it first. | |
955 | */ | |
956 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
957 | return 1; | |
958 | ||
959 | /* | |
960 | * In case the user-specified fault handler returned | |
961 | * zero, try to fix up. | |
962 | */ | |
d6be29b8 MH |
963 | if (fixup_exception(regs)) |
964 | return 1; | |
6d48583b | 965 | |
c28f8966 | 966 | /* |
8533bbe9 | 967 | * fixup routine could not handle it, |
c28f8966 PP |
968 | * Let do_page_fault() fix it. |
969 | */ | |
970 | break; | |
971 | default: | |
972 | break; | |
1da177e4 LT |
973 | } |
974 | return 0; | |
975 | } | |
976 | ||
977 | /* | |
978 | * Wrapper routine for handling exceptions. | |
979 | */ | |
0f2fbdcb PP |
980 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
981 | unsigned long val, void *data) | |
1da177e4 | 982 | { |
ade1af77 | 983 | struct die_args *args = data; |
66ff2d06 AM |
984 | int ret = NOTIFY_DONE; |
985 | ||
8533bbe9 | 986 | if (args->regs && user_mode_vm(args->regs)) |
2326c770 | 987 | return ret; |
988 | ||
1da177e4 LT |
989 | switch (val) { |
990 | case DIE_INT3: | |
991 | if (kprobe_handler(args->regs)) | |
66ff2d06 | 992 | ret = NOTIFY_STOP; |
1da177e4 LT |
993 | break; |
994 | case DIE_DEBUG: | |
62edab90 P |
995 | if (post_kprobe_handler(args->regs)) { |
996 | /* | |
997 | * Reset the BS bit in dr6 (pointed by args->err) to | |
998 | * denote completion of processing | |
999 | */ | |
1000 | (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP; | |
66ff2d06 | 1001 | ret = NOTIFY_STOP; |
62edab90 | 1002 | } |
1da177e4 LT |
1003 | break; |
1004 | case DIE_GPF: | |
b506a9d0 QB |
1005 | /* |
1006 | * To be potentially processing a kprobe fault and to | |
1007 | * trust the result from kprobe_running(), we have | |
1008 | * be non-preemptible. | |
1009 | */ | |
1010 | if (!preemptible() && kprobe_running() && | |
1da177e4 | 1011 | kprobe_fault_handler(args->regs, args->trapnr)) |
66ff2d06 | 1012 | ret = NOTIFY_STOP; |
1da177e4 LT |
1013 | break; |
1014 | default: | |
1015 | break; | |
1016 | } | |
66ff2d06 | 1017 | return ret; |
1da177e4 LT |
1018 | } |
1019 | ||
0f2fbdcb | 1020 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
1021 | { |
1022 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
1023 | unsigned long addr; | |
e7a510f9 | 1024 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 1025 | |
e7a510f9 | 1026 | kcb->jprobe_saved_regs = *regs; |
8533bbe9 MH |
1027 | kcb->jprobe_saved_sp = stack_addr(regs); |
1028 | addr = (unsigned long)(kcb->jprobe_saved_sp); | |
1029 | ||
1da177e4 LT |
1030 | /* |
1031 | * As Linus pointed out, gcc assumes that the callee | |
1032 | * owns the argument space and could overwrite it, e.g. | |
1033 | * tailcall optimization. So, to be absolutely safe | |
1034 | * we also save and restore enough stack bytes to cover | |
1035 | * the argument area. | |
1036 | */ | |
e7a510f9 | 1037 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
d6be29b8 | 1038 | MIN_STACK_SIZE(addr)); |
053de044 | 1039 | regs->flags &= ~X86_EFLAGS_IF; |
58dfe883 | 1040 | trace_hardirqs_off(); |
65ea5b03 | 1041 | regs->ip = (unsigned long)(jp->entry); |
1da177e4 LT |
1042 | return 1; |
1043 | } | |
1044 | ||
0f2fbdcb | 1045 | void __kprobes jprobe_return(void) |
1da177e4 | 1046 | { |
e7a510f9 AM |
1047 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1048 | ||
d6be29b8 MH |
1049 | asm volatile ( |
1050 | #ifdef CONFIG_X86_64 | |
1051 | " xchg %%rbx,%%rsp \n" | |
1052 | #else | |
1053 | " xchgl %%ebx,%%esp \n" | |
1054 | #endif | |
1055 | " int3 \n" | |
1056 | " .globl jprobe_return_end\n" | |
1057 | " jprobe_return_end: \n" | |
1058 | " nop \n"::"b" | |
1059 | (kcb->jprobe_saved_sp):"memory"); | |
1da177e4 LT |
1060 | } |
1061 | ||
0f2fbdcb | 1062 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 1063 | { |
e7a510f9 | 1064 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
65ea5b03 | 1065 | u8 *addr = (u8 *) (regs->ip - 1); |
1da177e4 LT |
1066 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
1067 | ||
d6be29b8 MH |
1068 | if ((addr > (u8 *) jprobe_return) && |
1069 | (addr < (u8 *) jprobe_return_end)) { | |
8533bbe9 | 1070 | if (stack_addr(regs) != kcb->jprobe_saved_sp) { |
29b6cd79 | 1071 | struct pt_regs *saved_regs = &kcb->jprobe_saved_regs; |
d6be29b8 MH |
1072 | printk(KERN_ERR |
1073 | "current sp %p does not match saved sp %p\n", | |
8533bbe9 | 1074 | stack_addr(regs), kcb->jprobe_saved_sp); |
d6be29b8 | 1075 | printk(KERN_ERR "Saved registers for jprobe %p\n", jp); |
1da177e4 | 1076 | show_registers(saved_regs); |
d6be29b8 | 1077 | printk(KERN_ERR "Current registers\n"); |
1da177e4 LT |
1078 | show_registers(regs); |
1079 | BUG(); | |
1080 | } | |
e7a510f9 | 1081 | *regs = kcb->jprobe_saved_regs; |
8533bbe9 MH |
1082 | memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp), |
1083 | kcb->jprobes_stack, | |
1084 | MIN_STACK_SIZE(kcb->jprobe_saved_sp)); | |
d217d545 | 1085 | preempt_enable_no_resched(); |
1da177e4 LT |
1086 | return 1; |
1087 | } | |
1088 | return 0; | |
1089 | } | |
ba8af12f | 1090 | |
c0f7ac3a MH |
1091 | |
1092 | #ifdef CONFIG_OPTPROBES | |
1093 | ||
1094 | /* Insert a call instruction at address 'from', which calls address 'to'.*/ | |
1095 | static void __kprobes synthesize_relcall(void *from, void *to) | |
1096 | { | |
1097 | __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE); | |
1098 | } | |
1099 | ||
1100 | /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */ | |
1101 | static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr, | |
1102 | unsigned long val) | |
1103 | { | |
1104 | #ifdef CONFIG_X86_64 | |
1105 | *addr++ = 0x48; | |
1106 | *addr++ = 0xbf; | |
1107 | #else | |
1108 | *addr++ = 0xb8; | |
1109 | #endif | |
1110 | *(unsigned long *)addr = val; | |
1111 | } | |
1112 | ||
1113 | void __kprobes kprobes_optinsn_template_holder(void) | |
1114 | { | |
1115 | asm volatile ( | |
1116 | ".global optprobe_template_entry\n" | |
1117 | "optprobe_template_entry: \n" | |
1118 | #ifdef CONFIG_X86_64 | |
1119 | /* We don't bother saving the ss register */ | |
1120 | " pushq %rsp\n" | |
1121 | " pushfq\n" | |
1122 | SAVE_REGS_STRING | |
1123 | " movq %rsp, %rsi\n" | |
1124 | ".global optprobe_template_val\n" | |
1125 | "optprobe_template_val: \n" | |
1126 | ASM_NOP5 | |
1127 | ASM_NOP5 | |
1128 | ".global optprobe_template_call\n" | |
1129 | "optprobe_template_call: \n" | |
1130 | ASM_NOP5 | |
1131 | /* Move flags to rsp */ | |
1132 | " movq 144(%rsp), %rdx\n" | |
1133 | " movq %rdx, 152(%rsp)\n" | |
1134 | RESTORE_REGS_STRING | |
1135 | /* Skip flags entry */ | |
1136 | " addq $8, %rsp\n" | |
1137 | " popfq\n" | |
1138 | #else /* CONFIG_X86_32 */ | |
1139 | " pushf\n" | |
1140 | SAVE_REGS_STRING | |
1141 | " movl %esp, %edx\n" | |
1142 | ".global optprobe_template_val\n" | |
1143 | "optprobe_template_val: \n" | |
1144 | ASM_NOP5 | |
1145 | ".global optprobe_template_call\n" | |
1146 | "optprobe_template_call: \n" | |
1147 | ASM_NOP5 | |
1148 | RESTORE_REGS_STRING | |
1149 | " addl $4, %esp\n" /* skip cs */ | |
1150 | " popf\n" | |
1151 | #endif | |
1152 | ".global optprobe_template_end\n" | |
1153 | "optprobe_template_end: \n"); | |
1154 | } | |
1155 | ||
1156 | #define TMPL_MOVE_IDX \ | |
1157 | ((long)&optprobe_template_val - (long)&optprobe_template_entry) | |
1158 | #define TMPL_CALL_IDX \ | |
1159 | ((long)&optprobe_template_call - (long)&optprobe_template_entry) | |
1160 | #define TMPL_END_IDX \ | |
1161 | ((long)&optprobe_template_end - (long)&optprobe_template_entry) | |
1162 | ||
1163 | #define INT3_SIZE sizeof(kprobe_opcode_t) | |
1164 | ||
1165 | /* Optimized kprobe call back function: called from optinsn */ | |
1166 | static void __kprobes optimized_callback(struct optimized_kprobe *op, | |
1167 | struct pt_regs *regs) | |
1168 | { | |
1169 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
1170 | ||
1171 | preempt_disable(); | |
1172 | if (kprobe_running()) { | |
1173 | kprobes_inc_nmissed_count(&op->kp); | |
1174 | } else { | |
1175 | /* Save skipped registers */ | |
1176 | #ifdef CONFIG_X86_64 | |
1177 | regs->cs = __KERNEL_CS; | |
1178 | #else | |
1179 | regs->cs = __KERNEL_CS | get_kernel_rpl(); | |
1180 | regs->gs = 0; | |
1181 | #endif | |
1182 | regs->ip = (unsigned long)op->kp.addr + INT3_SIZE; | |
1183 | regs->orig_ax = ~0UL; | |
1184 | ||
1185 | __get_cpu_var(current_kprobe) = &op->kp; | |
1186 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
1187 | opt_pre_handler(&op->kp, regs); | |
1188 | __get_cpu_var(current_kprobe) = NULL; | |
1189 | } | |
1190 | preempt_enable_no_resched(); | |
1191 | } | |
1192 | ||
1193 | static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src) | |
1194 | { | |
1195 | int len = 0, ret; | |
1196 | ||
1197 | while (len < RELATIVEJUMP_SIZE) { | |
1198 | ret = __copy_instruction(dest + len, src + len, 1); | |
1199 | if (!ret || !can_boost(dest + len)) | |
1200 | return -EINVAL; | |
1201 | len += ret; | |
1202 | } | |
1203 | /* Check whether the address range is reserved */ | |
1204 | if (ftrace_text_reserved(src, src + len - 1) || | |
1205 | alternatives_text_reserved(src, src + len - 1)) | |
1206 | return -EBUSY; | |
1207 | ||
1208 | return len; | |
1209 | } | |
1210 | ||
1211 | /* Check whether insn is indirect jump */ | |
1212 | static int __kprobes insn_is_indirect_jump(struct insn *insn) | |
1213 | { | |
1214 | return ((insn->opcode.bytes[0] == 0xff && | |
1215 | (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */ | |
1216 | insn->opcode.bytes[0] == 0xea); /* Segment based jump */ | |
1217 | } | |
1218 | ||
1219 | /* Check whether insn jumps into specified address range */ | |
1220 | static int insn_jump_into_range(struct insn *insn, unsigned long start, int len) | |
1221 | { | |
1222 | unsigned long target = 0; | |
1223 | ||
1224 | switch (insn->opcode.bytes[0]) { | |
1225 | case 0xe0: /* loopne */ | |
1226 | case 0xe1: /* loope */ | |
1227 | case 0xe2: /* loop */ | |
1228 | case 0xe3: /* jcxz */ | |
1229 | case 0xe9: /* near relative jump */ | |
1230 | case 0xeb: /* short relative jump */ | |
1231 | break; | |
1232 | case 0x0f: | |
1233 | if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */ | |
1234 | break; | |
1235 | return 0; | |
1236 | default: | |
1237 | if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */ | |
1238 | break; | |
1239 | return 0; | |
1240 | } | |
1241 | target = (unsigned long)insn->next_byte + insn->immediate.value; | |
1242 | ||
1243 | return (start <= target && target <= start + len); | |
1244 | } | |
1245 | ||
1246 | /* Decode whole function to ensure any instructions don't jump into target */ | |
1247 | static int __kprobes can_optimize(unsigned long paddr) | |
1248 | { | |
1249 | int ret; | |
1250 | unsigned long addr, size = 0, offset = 0; | |
1251 | struct insn insn; | |
1252 | kprobe_opcode_t buf[MAX_INSN_SIZE]; | |
1253 | /* Dummy buffers for lookup_symbol_attrs */ | |
1254 | static char __dummy_buf[KSYM_NAME_LEN]; | |
1255 | ||
1256 | /* Lookup symbol including addr */ | |
1257 | if (!kallsyms_lookup(paddr, &size, &offset, NULL, __dummy_buf)) | |
1258 | return 0; | |
1259 | ||
1260 | /* Check there is enough space for a relative jump. */ | |
1261 | if (size - offset < RELATIVEJUMP_SIZE) | |
1262 | return 0; | |
1263 | ||
1264 | /* Decode instructions */ | |
1265 | addr = paddr - offset; | |
1266 | while (addr < paddr - offset + size) { /* Decode until function end */ | |
1267 | if (search_exception_tables(addr)) | |
1268 | /* | |
1269 | * Since some fixup code will jumps into this function, | |
1270 | * we can't optimize kprobe in this function. | |
1271 | */ | |
1272 | return 0; | |
1273 | kernel_insn_init(&insn, (void *)addr); | |
1274 | insn_get_opcode(&insn); | |
1275 | if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) { | |
1276 | ret = recover_probed_instruction(buf, addr); | |
1277 | if (ret) | |
1278 | return 0; | |
1279 | kernel_insn_init(&insn, buf); | |
1280 | } | |
1281 | insn_get_length(&insn); | |
1282 | /* Recover address */ | |
1283 | insn.kaddr = (void *)addr; | |
1284 | insn.next_byte = (void *)(addr + insn.length); | |
1285 | /* Check any instructions don't jump into target */ | |
1286 | if (insn_is_indirect_jump(&insn) || | |
1287 | insn_jump_into_range(&insn, paddr + INT3_SIZE, | |
1288 | RELATIVE_ADDR_SIZE)) | |
1289 | return 0; | |
1290 | addr += insn.length; | |
1291 | } | |
1292 | ||
1293 | return 1; | |
1294 | } | |
1295 | ||
1296 | /* Check optimized_kprobe can actually be optimized. */ | |
1297 | int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op) | |
1298 | { | |
1299 | int i; | |
1300 | struct kprobe *p; | |
1301 | ||
1302 | for (i = 1; i < op->optinsn.size; i++) { | |
1303 | p = get_kprobe(op->kp.addr + i); | |
1304 | if (p && !kprobe_disabled(p)) | |
1305 | return -EEXIST; | |
1306 | } | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | /* Check the addr is within the optimized instructions. */ | |
1312 | int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op, | |
1313 | unsigned long addr) | |
1314 | { | |
1315 | return ((unsigned long)op->kp.addr <= addr && | |
1316 | (unsigned long)op->kp.addr + op->optinsn.size > addr); | |
1317 | } | |
1318 | ||
1319 | /* Free optimized instruction slot */ | |
1320 | static __kprobes | |
1321 | void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty) | |
1322 | { | |
1323 | if (op->optinsn.insn) { | |
1324 | free_optinsn_slot(op->optinsn.insn, dirty); | |
1325 | op->optinsn.insn = NULL; | |
1326 | op->optinsn.size = 0; | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op) | |
1331 | { | |
1332 | __arch_remove_optimized_kprobe(op, 1); | |
1333 | } | |
1334 | ||
1335 | /* | |
1336 | * Copy replacing target instructions | |
1337 | * Target instructions MUST be relocatable (checked inside) | |
1338 | */ | |
1339 | int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op) | |
1340 | { | |
1341 | u8 *buf; | |
1342 | int ret; | |
1343 | long rel; | |
1344 | ||
1345 | if (!can_optimize((unsigned long)op->kp.addr)) | |
1346 | return -EILSEQ; | |
1347 | ||
1348 | op->optinsn.insn = get_optinsn_slot(); | |
1349 | if (!op->optinsn.insn) | |
1350 | return -ENOMEM; | |
1351 | ||
1352 | /* | |
1353 | * Verify if the address gap is in 2GB range, because this uses | |
1354 | * a relative jump. | |
1355 | */ | |
1356 | rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE; | |
1357 | if (abs(rel) > 0x7fffffff) | |
1358 | return -ERANGE; | |
1359 | ||
1360 | buf = (u8 *)op->optinsn.insn; | |
1361 | ||
1362 | /* Copy instructions into the out-of-line buffer */ | |
1363 | ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr); | |
1364 | if (ret < 0) { | |
1365 | __arch_remove_optimized_kprobe(op, 0); | |
1366 | return ret; | |
1367 | } | |
1368 | op->optinsn.size = ret; | |
1369 | ||
1370 | /* Copy arch-dep-instance from template */ | |
1371 | memcpy(buf, &optprobe_template_entry, TMPL_END_IDX); | |
1372 | ||
1373 | /* Set probe information */ | |
1374 | synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op); | |
1375 | ||
1376 | /* Set probe function call */ | |
1377 | synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback); | |
1378 | ||
1379 | /* Set returning jmp instruction at the tail of out-of-line buffer */ | |
1380 | synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size, | |
1381 | (u8 *)op->kp.addr + op->optinsn.size); | |
1382 | ||
1383 | flush_icache_range((unsigned long) buf, | |
1384 | (unsigned long) buf + TMPL_END_IDX + | |
1385 | op->optinsn.size + RELATIVEJUMP_SIZE); | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | /* Replace a breakpoint (int3) with a relative jump. */ | |
1390 | int __kprobes arch_optimize_kprobe(struct optimized_kprobe *op) | |
1391 | { | |
1392 | unsigned char jmp_code[RELATIVEJUMP_SIZE]; | |
1393 | s32 rel = (s32)((long)op->optinsn.insn - | |
1394 | ((long)op->kp.addr + RELATIVEJUMP_SIZE)); | |
1395 | ||
1396 | /* Backup instructions which will be replaced by jump address */ | |
1397 | memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE, | |
1398 | RELATIVE_ADDR_SIZE); | |
1399 | ||
1400 | jmp_code[0] = RELATIVEJUMP_OPCODE; | |
1401 | *(s32 *)(&jmp_code[1]) = rel; | |
1402 | ||
1403 | /* | |
1404 | * text_poke_smp doesn't support NMI/MCE code modifying. | |
1405 | * However, since kprobes itself also doesn't support NMI/MCE | |
1406 | * code probing, it's not a problem. | |
1407 | */ | |
1408 | text_poke_smp(op->kp.addr, jmp_code, RELATIVEJUMP_SIZE); | |
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | /* Replace a relative jump with a breakpoint (int3). */ | |
1413 | void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op) | |
1414 | { | |
1415 | u8 buf[RELATIVEJUMP_SIZE]; | |
1416 | ||
1417 | /* Set int3 to first byte for kprobes */ | |
1418 | buf[0] = BREAKPOINT_INSTRUCTION; | |
1419 | memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE); | |
1420 | text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE); | |
1421 | } | |
1422 | ||
1423 | static int __kprobes setup_detour_execution(struct kprobe *p, | |
1424 | struct pt_regs *regs, | |
1425 | int reenter) | |
1426 | { | |
1427 | struct optimized_kprobe *op; | |
1428 | ||
1429 | if (p->flags & KPROBE_FLAG_OPTIMIZED) { | |
1430 | /* This kprobe is really able to run optimized path. */ | |
1431 | op = container_of(p, struct optimized_kprobe, kp); | |
1432 | /* Detour through copied instructions */ | |
1433 | regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX; | |
1434 | if (!reenter) | |
1435 | reset_current_kprobe(); | |
1436 | preempt_enable_no_resched(); | |
1437 | return 1; | |
1438 | } | |
1439 | return 0; | |
1440 | } | |
1441 | #endif | |
1442 | ||
6772926b | 1443 | int __init arch_init_kprobes(void) |
ba8af12f | 1444 | { |
da07ab03 | 1445 | return 0; |
ba8af12f | 1446 | } |
bf8f6e5b AM |
1447 | |
1448 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) | |
1449 | { | |
bf8f6e5b AM |
1450 | return 0; |
1451 | } |