Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Kernel Probes (KProbes) | |
3 | * arch/i386/kernel/kprobes.c | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | * | |
19 | * Copyright (C) IBM Corporation, 2002, 2004 | |
20 | * | |
21 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel | |
22 | * Probes initial implementation ( includes contributions from | |
23 | * Rusty Russell). | |
24 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes | |
25 | * interface to access function arguments. | |
b94cce92 HN |
26 | * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston |
27 | * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi | |
28 | * <prasanna@in.ibm.com> added function-return probes. | |
1da177e4 LT |
29 | */ |
30 | ||
31 | #include <linux/config.h> | |
32 | #include <linux/kprobes.h> | |
33 | #include <linux/ptrace.h> | |
1da177e4 | 34 | #include <linux/preempt.h> |
7e1048b1 | 35 | #include <asm/cacheflush.h> |
1da177e4 LT |
36 | #include <asm/kdebug.h> |
37 | #include <asm/desc.h> | |
38 | ||
1da177e4 LT |
39 | void jprobe_return_end(void); |
40 | ||
9a0e3a86 AM |
41 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
42 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
43 | ||
1da177e4 LT |
44 | /* |
45 | * returns non-zero if opcode modifies the interrupt flag. | |
46 | */ | |
47 | static inline int is_IF_modifier(kprobe_opcode_t opcode) | |
48 | { | |
49 | switch (opcode) { | |
50 | case 0xfa: /* cli */ | |
51 | case 0xfb: /* sti */ | |
52 | case 0xcf: /* iret/iretd */ | |
53 | case 0x9d: /* popf/popfd */ | |
54 | return 1; | |
55 | } | |
56 | return 0; | |
57 | } | |
58 | ||
3d97ae5b | 59 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
1da177e4 | 60 | { |
124d90be PP |
61 | /* insn: must be on special executable page on i386. */ |
62 | p->ainsn.insn = get_insn_slot(); | |
63 | if (!p->ainsn.insn) | |
64 | return -ENOMEM; | |
65 | ||
1da177e4 | 66 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
7e1048b1 | 67 | p->opcode = *p->addr; |
49a2a1b8 | 68 | return 0; |
1da177e4 LT |
69 | } |
70 | ||
3d97ae5b | 71 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 72 | { |
7e1048b1 RL |
73 | *p->addr = BREAKPOINT_INSTRUCTION; |
74 | flush_icache_range((unsigned long) p->addr, | |
75 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); | |
1da177e4 LT |
76 | } |
77 | ||
3d97ae5b | 78 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
1da177e4 LT |
79 | { |
80 | *p->addr = p->opcode; | |
7e1048b1 RL |
81 | flush_icache_range((unsigned long) p->addr, |
82 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); | |
83 | } | |
84 | ||
124d90be PP |
85 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
86 | { | |
87 | down(&kprobe_mutex); | |
88 | free_insn_slot(p->ainsn.insn); | |
89 | up(&kprobe_mutex); | |
90 | } | |
91 | ||
9a0e3a86 | 92 | static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb) |
417c8da6 | 93 | { |
9a0e3a86 AM |
94 | kcb->prev_kprobe.kp = kprobe_running(); |
95 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
96 | kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags; | |
97 | kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags; | |
417c8da6 PP |
98 | } |
99 | ||
9a0e3a86 | 100 | static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
417c8da6 | 101 | { |
9a0e3a86 AM |
102 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
103 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
104 | kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags; | |
105 | kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags; | |
417c8da6 PP |
106 | } |
107 | ||
9a0e3a86 AM |
108 | static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
109 | struct kprobe_ctlblk *kcb) | |
417c8da6 | 110 | { |
9a0e3a86 AM |
111 | __get_cpu_var(current_kprobe) = p; |
112 | kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags | |
417c8da6 PP |
113 | = (regs->eflags & (TF_MASK | IF_MASK)); |
114 | if (is_IF_modifier(p->opcode)) | |
9a0e3a86 | 115 | kcb->kprobe_saved_eflags &= ~IF_MASK; |
417c8da6 PP |
116 | } |
117 | ||
1da177e4 LT |
118 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
119 | { | |
120 | regs->eflags |= TF_MASK; | |
121 | regs->eflags &= ~IF_MASK; | |
122 | /*single step inline if the instruction is an int3*/ | |
123 | if (p->opcode == BREAKPOINT_INSTRUCTION) | |
124 | regs->eip = (unsigned long)p->addr; | |
125 | else | |
124d90be | 126 | regs->eip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
127 | } |
128 | ||
991a51d8 | 129 | /* Called with kretprobe_lock held */ |
3d97ae5b PP |
130 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
131 | struct pt_regs *regs) | |
b94cce92 HN |
132 | { |
133 | unsigned long *sara = (unsigned long *)®s->esp; | |
4bdbd37f RL |
134 | struct kretprobe_instance *ri; |
135 | ||
136 | if ((ri = get_free_rp_inst(rp)) != NULL) { | |
137 | ri->rp = rp; | |
138 | ri->task = current; | |
139 | ri->ret_addr = (kprobe_opcode_t *) *sara; | |
b94cce92 | 140 | |
b94cce92 HN |
141 | /* Replace the return addr with trampoline addr */ |
142 | *sara = (unsigned long) &kretprobe_trampoline; | |
b94cce92 | 143 | |
4bdbd37f RL |
144 | add_rp_inst(ri); |
145 | } else { | |
146 | rp->nmissed++; | |
147 | } | |
b94cce92 HN |
148 | } |
149 | ||
1da177e4 LT |
150 | /* |
151 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
152 | * remain disabled thorough out this function. | |
153 | */ | |
3d97ae5b | 154 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
1da177e4 LT |
155 | { |
156 | struct kprobe *p; | |
157 | int ret = 0; | |
158 | kprobe_opcode_t *addr = NULL; | |
159 | unsigned long *lp; | |
d217d545 AM |
160 | struct kprobe_ctlblk *kcb; |
161 | ||
162 | /* | |
163 | * We don't want to be preempted for the entire | |
164 | * duration of kprobe processing | |
165 | */ | |
166 | preempt_disable(); | |
167 | kcb = get_kprobe_ctlblk(); | |
1da177e4 | 168 | |
1da177e4 LT |
169 | /* Check if the application is using LDT entry for its code segment and |
170 | * calculate the address by reading the base address from the LDT entry. | |
171 | */ | |
172 | if ((regs->xcs & 4) && (current->mm)) { | |
173 | lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8) | |
174 | + (char *) current->mm->context.ldt); | |
175 | addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip - | |
176 | sizeof(kprobe_opcode_t)); | |
177 | } else { | |
178 | addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t)); | |
179 | } | |
180 | /* Check we're not actually recursing */ | |
181 | if (kprobe_running()) { | |
1da177e4 LT |
182 | p = get_kprobe(addr); |
183 | if (p) { | |
9a0e3a86 | 184 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
deac66ae | 185 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { |
1da177e4 | 186 | regs->eflags &= ~TF_MASK; |
9a0e3a86 | 187 | regs->eflags |= kcb->kprobe_saved_eflags; |
1da177e4 LT |
188 | goto no_kprobe; |
189 | } | |
417c8da6 PP |
190 | /* We have reentered the kprobe_handler(), since |
191 | * another probe was hit while within the handler. | |
192 | * We here save the original kprobes variables and | |
193 | * just single step on the instruction of the new probe | |
194 | * without calling any user handlers. | |
195 | */ | |
9a0e3a86 AM |
196 | save_previous_kprobe(kcb); |
197 | set_current_kprobe(p, regs, kcb); | |
bf8d5c52 | 198 | kprobes_inc_nmissed_count(p); |
417c8da6 | 199 | prepare_singlestep(p, regs); |
9a0e3a86 | 200 | kcb->kprobe_status = KPROBE_REENTER; |
417c8da6 | 201 | return 1; |
1da177e4 | 202 | } else { |
eb3a7292 KA |
203 | if (regs->eflags & VM_MASK) { |
204 | /* We are in virtual-8086 mode. Return 0 */ | |
205 | goto no_kprobe; | |
206 | } | |
207 | if (*addr != BREAKPOINT_INSTRUCTION) { | |
208 | /* The breakpoint instruction was removed by | |
209 | * another cpu right after we hit, no further | |
210 | * handling of this interrupt is appropriate | |
211 | */ | |
212 | regs->eip -= sizeof(kprobe_opcode_t); | |
213 | ret = 1; | |
214 | goto no_kprobe; | |
215 | } | |
9a0e3a86 | 216 | p = __get_cpu_var(current_kprobe); |
1da177e4 LT |
217 | if (p->break_handler && p->break_handler(p, regs)) { |
218 | goto ss_probe; | |
219 | } | |
220 | } | |
1da177e4 LT |
221 | goto no_kprobe; |
222 | } | |
223 | ||
1da177e4 LT |
224 | p = get_kprobe(addr); |
225 | if (!p) { | |
1da177e4 LT |
226 | if (regs->eflags & VM_MASK) { |
227 | /* We are in virtual-8086 mode. Return 0 */ | |
228 | goto no_kprobe; | |
229 | } | |
230 | ||
231 | if (*addr != BREAKPOINT_INSTRUCTION) { | |
232 | /* | |
233 | * The breakpoint instruction was removed right | |
234 | * after we hit it. Another cpu has removed | |
235 | * either a probepoint or a debugger breakpoint | |
236 | * at this address. In either case, no further | |
237 | * handling of this interrupt is appropriate. | |
bce06494 JK |
238 | * Back up over the (now missing) int3 and run |
239 | * the original instruction. | |
1da177e4 | 240 | */ |
bce06494 | 241 | regs->eip -= sizeof(kprobe_opcode_t); |
1da177e4 LT |
242 | ret = 1; |
243 | } | |
244 | /* Not one of ours: let kernel handle it */ | |
245 | goto no_kprobe; | |
246 | } | |
247 | ||
9a0e3a86 AM |
248 | set_current_kprobe(p, regs, kcb); |
249 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
1da177e4 LT |
250 | |
251 | if (p->pre_handler && p->pre_handler(p, regs)) | |
252 | /* handler has already set things up, so skip ss setup */ | |
253 | return 1; | |
254 | ||
255 | ss_probe: | |
256 | prepare_singlestep(p, regs); | |
9a0e3a86 | 257 | kcb->kprobe_status = KPROBE_HIT_SS; |
1da177e4 LT |
258 | return 1; |
259 | ||
260 | no_kprobe: | |
d217d545 | 261 | preempt_enable_no_resched(); |
1da177e4 LT |
262 | return ret; |
263 | } | |
264 | ||
b94cce92 HN |
265 | /* |
266 | * For function-return probes, init_kprobes() establishes a probepoint | |
267 | * here. When a retprobed function returns, this probe is hit and | |
268 | * trampoline_probe_handler() runs, calling the kretprobe's handler. | |
269 | */ | |
270 | void kretprobe_trampoline_holder(void) | |
271 | { | |
272 | asm volatile ( ".global kretprobe_trampoline\n" | |
273 | "kretprobe_trampoline: \n" | |
274 | "nop\n"); | |
275 | } | |
276 | ||
277 | /* | |
278 | * Called when we hit the probe point at kretprobe_trampoline | |
279 | */ | |
3d97ae5b | 280 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
b94cce92 | 281 | { |
4bdbd37f RL |
282 | struct kretprobe_instance *ri = NULL; |
283 | struct hlist_head *head; | |
284 | struct hlist_node *node, *tmp; | |
991a51d8 | 285 | unsigned long flags, orig_ret_address = 0; |
4bdbd37f | 286 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
b94cce92 | 287 | |
991a51d8 | 288 | spin_lock_irqsave(&kretprobe_lock, flags); |
4bdbd37f | 289 | head = kretprobe_inst_table_head(current); |
b94cce92 | 290 | |
4bdbd37f RL |
291 | /* |
292 | * It is possible to have multiple instances associated with a given | |
293 | * task either because an multiple functions in the call path | |
294 | * have a return probe installed on them, and/or more then one return | |
295 | * return probe was registered for a target function. | |
296 | * | |
297 | * We can handle this because: | |
298 | * - instances are always inserted at the head of the list | |
299 | * - when multiple return probes are registered for the same | |
300 | * function, the first instance's ret_addr will point to the | |
301 | * real return address, and all the rest will point to | |
302 | * kretprobe_trampoline | |
303 | */ | |
304 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
305 | if (ri->task != current) | |
306 | /* another task is sharing our hash bucket */ | |
307 | continue; | |
308 | ||
309 | if (ri->rp && ri->rp->handler) | |
310 | ri->rp->handler(ri, regs); | |
311 | ||
312 | orig_ret_address = (unsigned long)ri->ret_addr; | |
b94cce92 | 313 | recycle_rp_inst(ri); |
4bdbd37f RL |
314 | |
315 | if (orig_ret_address != trampoline_address) | |
316 | /* | |
317 | * This is the real return address. Any other | |
318 | * instances associated with this task are for | |
319 | * other calls deeper on the call stack | |
320 | */ | |
321 | break; | |
b94cce92 | 322 | } |
4bdbd37f RL |
323 | |
324 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); | |
325 | regs->eip = orig_ret_address; | |
326 | ||
9a0e3a86 | 327 | reset_current_kprobe(); |
991a51d8 | 328 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
4bdbd37f RL |
329 | preempt_enable_no_resched(); |
330 | ||
66ff2d06 AM |
331 | /* |
332 | * By returning a non-zero value, we are telling | |
d217d545 AM |
333 | * kprobe_handler() that we don't want the post_handler |
334 | * to run (and have re-enabled preemption) | |
66ff2d06 | 335 | */ |
4bdbd37f | 336 | return 1; |
b94cce92 HN |
337 | } |
338 | ||
1da177e4 LT |
339 | /* |
340 | * Called after single-stepping. p->addr is the address of the | |
341 | * instruction whose first byte has been replaced by the "int 3" | |
342 | * instruction. To avoid the SMP problems that can occur when we | |
343 | * temporarily put back the original opcode to single-step, we | |
344 | * single-stepped a copy of the instruction. The address of this | |
345 | * copy is p->ainsn.insn. | |
346 | * | |
347 | * This function prepares to return from the post-single-step | |
348 | * interrupt. We have to fix up the stack as follows: | |
349 | * | |
350 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
351 | * the new eip is relative to the copied instruction. We need to make | |
352 | * it relative to the original instruction. | |
353 | * | |
354 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
355 | * flags are set in the just-pushed eflags, and may need to be cleared. | |
356 | * | |
357 | * 2) If the single-stepped instruction was a call, the return address | |
358 | * that is atop the stack is the address following the copied instruction. | |
359 | * We need to make it the address following the original instruction. | |
360 | */ | |
9a0e3a86 AM |
361 | static void __kprobes resume_execution(struct kprobe *p, |
362 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) | |
1da177e4 LT |
363 | { |
364 | unsigned long *tos = (unsigned long *)®s->esp; | |
365 | unsigned long next_eip = 0; | |
124d90be | 366 | unsigned long copy_eip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
367 | unsigned long orig_eip = (unsigned long)p->addr; |
368 | ||
369 | switch (p->ainsn.insn[0]) { | |
370 | case 0x9c: /* pushfl */ | |
371 | *tos &= ~(TF_MASK | IF_MASK); | |
9a0e3a86 | 372 | *tos |= kcb->kprobe_old_eflags; |
1da177e4 | 373 | break; |
0b9e2cac PP |
374 | case 0xc3: /* ret/lret */ |
375 | case 0xcb: | |
376 | case 0xc2: | |
377 | case 0xca: | |
378 | regs->eflags &= ~TF_MASK; | |
379 | /* eip is already adjusted, no more changes required*/ | |
380 | return; | |
1da177e4 LT |
381 | case 0xe8: /* call relative - Fix return addr */ |
382 | *tos = orig_eip + (*tos - copy_eip); | |
383 | break; | |
384 | case 0xff: | |
385 | if ((p->ainsn.insn[1] & 0x30) == 0x10) { | |
386 | /* call absolute, indirect */ | |
387 | /* Fix return addr; eip is correct. */ | |
388 | next_eip = regs->eip; | |
389 | *tos = orig_eip + (*tos - copy_eip); | |
390 | } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */ | |
391 | ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */ | |
392 | /* eip is correct. */ | |
393 | next_eip = regs->eip; | |
394 | } | |
395 | break; | |
396 | case 0xea: /* jmp absolute -- eip is correct */ | |
397 | next_eip = regs->eip; | |
398 | break; | |
399 | default: | |
400 | break; | |
401 | } | |
402 | ||
403 | regs->eflags &= ~TF_MASK; | |
404 | if (next_eip) { | |
405 | regs->eip = next_eip; | |
406 | } else { | |
407 | regs->eip = orig_eip + (regs->eip - copy_eip); | |
408 | } | |
409 | } | |
410 | ||
411 | /* | |
412 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
991a51d8 | 413 | * remain disabled thoroughout this function. |
1da177e4 LT |
414 | */ |
415 | static inline int post_kprobe_handler(struct pt_regs *regs) | |
416 | { | |
9a0e3a86 AM |
417 | struct kprobe *cur = kprobe_running(); |
418 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
419 | ||
420 | if (!cur) | |
1da177e4 LT |
421 | return 0; |
422 | ||
9a0e3a86 AM |
423 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
424 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
425 | cur->post_handler(cur, regs, 0); | |
417c8da6 | 426 | } |
1da177e4 | 427 | |
9a0e3a86 AM |
428 | resume_execution(cur, regs, kcb); |
429 | regs->eflags |= kcb->kprobe_saved_eflags; | |
1da177e4 | 430 | |
417c8da6 | 431 | /*Restore back the original saved kprobes variables and continue. */ |
9a0e3a86 AM |
432 | if (kcb->kprobe_status == KPROBE_REENTER) { |
433 | restore_previous_kprobe(kcb); | |
417c8da6 PP |
434 | goto out; |
435 | } | |
9a0e3a86 | 436 | reset_current_kprobe(); |
417c8da6 | 437 | out: |
1da177e4 LT |
438 | preempt_enable_no_resched(); |
439 | ||
440 | /* | |
441 | * if somebody else is singlestepping across a probe point, eflags | |
442 | * will have TF set, in which case, continue the remaining processing | |
443 | * of do_debug, as if this is not a probe hit. | |
444 | */ | |
445 | if (regs->eflags & TF_MASK) | |
446 | return 0; | |
447 | ||
448 | return 1; | |
449 | } | |
450 | ||
1da177e4 LT |
451 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
452 | { | |
9a0e3a86 AM |
453 | struct kprobe *cur = kprobe_running(); |
454 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
455 | ||
456 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
1da177e4 LT |
457 | return 1; |
458 | ||
9a0e3a86 AM |
459 | if (kcb->kprobe_status & KPROBE_HIT_SS) { |
460 | resume_execution(cur, regs, kcb); | |
461 | regs->eflags |= kcb->kprobe_old_eflags; | |
1da177e4 | 462 | |
9a0e3a86 | 463 | reset_current_kprobe(); |
1da177e4 LT |
464 | preempt_enable_no_resched(); |
465 | } | |
466 | return 0; | |
467 | } | |
468 | ||
469 | /* | |
470 | * Wrapper routine to for handling exceptions. | |
471 | */ | |
3d97ae5b PP |
472 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
473 | unsigned long val, void *data) | |
1da177e4 LT |
474 | { |
475 | struct die_args *args = (struct die_args *)data; | |
66ff2d06 AM |
476 | int ret = NOTIFY_DONE; |
477 | ||
1da177e4 LT |
478 | switch (val) { |
479 | case DIE_INT3: | |
480 | if (kprobe_handler(args->regs)) | |
66ff2d06 | 481 | ret = NOTIFY_STOP; |
1da177e4 LT |
482 | break; |
483 | case DIE_DEBUG: | |
484 | if (post_kprobe_handler(args->regs)) | |
66ff2d06 | 485 | ret = NOTIFY_STOP; |
1da177e4 LT |
486 | break; |
487 | case DIE_GPF: | |
1da177e4 | 488 | case DIE_PAGE_FAULT: |
d217d545 AM |
489 | /* kprobe_running() needs smp_processor_id() */ |
490 | preempt_disable(); | |
1da177e4 LT |
491 | if (kprobe_running() && |
492 | kprobe_fault_handler(args->regs, args->trapnr)) | |
66ff2d06 | 493 | ret = NOTIFY_STOP; |
d217d545 | 494 | preempt_enable(); |
1da177e4 LT |
495 | break; |
496 | default: | |
497 | break; | |
498 | } | |
66ff2d06 | 499 | return ret; |
1da177e4 LT |
500 | } |
501 | ||
3d97ae5b | 502 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
503 | { |
504 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
505 | unsigned long addr; | |
9a0e3a86 | 506 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 507 | |
9a0e3a86 AM |
508 | kcb->jprobe_saved_regs = *regs; |
509 | kcb->jprobe_saved_esp = ®s->esp; | |
510 | addr = (unsigned long)(kcb->jprobe_saved_esp); | |
1da177e4 LT |
511 | |
512 | /* | |
513 | * TBD: As Linus pointed out, gcc assumes that the callee | |
514 | * owns the argument space and could overwrite it, e.g. | |
515 | * tailcall optimization. So, to be absolutely safe | |
516 | * we also save and restore enough stack bytes to cover | |
517 | * the argument area. | |
518 | */ | |
9a0e3a86 AM |
519 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
520 | MIN_STACK_SIZE(addr)); | |
1da177e4 LT |
521 | regs->eflags &= ~IF_MASK; |
522 | regs->eip = (unsigned long)(jp->entry); | |
523 | return 1; | |
524 | } | |
525 | ||
3d97ae5b | 526 | void __kprobes jprobe_return(void) |
1da177e4 | 527 | { |
9a0e3a86 AM |
528 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
529 | ||
1da177e4 LT |
530 | asm volatile (" xchgl %%ebx,%%esp \n" |
531 | " int3 \n" | |
532 | " .globl jprobe_return_end \n" | |
533 | " jprobe_return_end: \n" | |
534 | " nop \n"::"b" | |
9a0e3a86 | 535 | (kcb->jprobe_saved_esp):"memory"); |
1da177e4 LT |
536 | } |
537 | ||
3d97ae5b | 538 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 539 | { |
9a0e3a86 | 540 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 541 | u8 *addr = (u8 *) (regs->eip - 1); |
9a0e3a86 | 542 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp); |
1da177e4 LT |
543 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
544 | ||
545 | if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) { | |
9a0e3a86 | 546 | if (®s->esp != kcb->jprobe_saved_esp) { |
1da177e4 | 547 | struct pt_regs *saved_regs = |
9a0e3a86 AM |
548 | container_of(kcb->jprobe_saved_esp, |
549 | struct pt_regs, esp); | |
1da177e4 | 550 | printk("current esp %p does not match saved esp %p\n", |
9a0e3a86 | 551 | ®s->esp, kcb->jprobe_saved_esp); |
1da177e4 LT |
552 | printk("Saved registers for jprobe %p\n", jp); |
553 | show_registers(saved_regs); | |
554 | printk("Current registers\n"); | |
555 | show_registers(regs); | |
556 | BUG(); | |
557 | } | |
9a0e3a86 AM |
558 | *regs = kcb->jprobe_saved_regs; |
559 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, | |
1da177e4 | 560 | MIN_STACK_SIZE(stack_addr)); |
d217d545 | 561 | preempt_enable_no_resched(); |
1da177e4 LT |
562 | return 1; |
563 | } | |
564 | return 0; | |
565 | } | |
4bdbd37f RL |
566 | |
567 | static struct kprobe trampoline_p = { | |
568 | .addr = (kprobe_opcode_t *) &kretprobe_trampoline, | |
569 | .pre_handler = trampoline_probe_handler | |
570 | }; | |
571 | ||
6772926b | 572 | int __init arch_init_kprobes(void) |
4bdbd37f RL |
573 | { |
574 | return register_kprobe(&trampoline_p); | |
575 | } |