blob: 68fe10250486caa65fdcc76ce499680f98510806 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Hien Nguyenb94cce92005-06-23 00:09:19 -070026 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/config.h>
32#include <linux/kprobes.h>
33#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/preempt.h>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070035#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/kdebug.h>
37#include <asm/desc.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039void jprobe_return_end(void);
40
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080041DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * returns non-zero if opcode modifies the interrupt flag.
46 */
47static 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
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070059int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -070062 p->opcode = *p->addr;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -080063 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070064}
65
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070066void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070067{
68 *p->addr = BREAKPOINT_INSTRUCTION;
69 flush_icache_range((unsigned long) p->addr,
70 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
71}
72
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070073void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070074{
75 *p->addr = p->opcode;
76 flush_icache_range((unsigned long) p->addr,
77 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070080void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82}
83
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080084static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070085{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080086 kcb->prev_kprobe.kp = kprobe_running();
87 kcb->prev_kprobe.status = kcb->kprobe_status;
88 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
89 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070090}
91
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080092static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070093{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080094 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
95 kcb->kprobe_status = kcb->prev_kprobe.status;
96 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
97 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070098}
99
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800100static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
101 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700102{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800103 __get_cpu_var(current_kprobe) = p;
104 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700105 = (regs->eflags & (TF_MASK | IF_MASK));
106 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800107 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
111{
112 regs->eflags |= TF_MASK;
113 regs->eflags &= ~IF_MASK;
114 /*single step inline if the instruction is an int3*/
115 if (p->opcode == BREAKPOINT_INSTRUCTION)
116 regs->eip = (unsigned long)p->addr;
117 else
118 regs->eip = (unsigned long)&p->ainsn.insn;
119}
120
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800121/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700122void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
123 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700124{
125 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700126 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700127
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700128 if ((ri = get_free_rp_inst(rp)) != NULL) {
129 ri->rp = rp;
130 ri->task = current;
131 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700132
Hien Nguyenb94cce92005-06-23 00:09:19 -0700133 /* Replace the return addr with trampoline addr */
134 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700135
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700136 add_rp_inst(ri);
137 } else {
138 rp->nmissed++;
139 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
144 * remain disabled thorough out this function.
145 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700146static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct kprobe *p;
149 int ret = 0;
150 kprobe_opcode_t *addr = NULL;
151 unsigned long *lp;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800152 struct kprobe_ctlblk *kcb;
153
154 /*
155 * We don't want to be preempted for the entire
156 * duration of kprobe processing
157 */
158 preempt_disable();
159 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Check if the application is using LDT entry for its code segment and
162 * calculate the address by reading the base address from the LDT entry.
163 */
164 if ((regs->xcs & 4) && (current->mm)) {
165 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
166 + (char *) current->mm->context.ldt);
167 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
168 sizeof(kprobe_opcode_t));
169 } else {
170 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
171 }
172 /* Check we're not actually recursing */
173 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 p = get_kprobe(addr);
175 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800176 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700177 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800179 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto no_kprobe;
181 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700182 /* We have reentered the kprobe_handler(), since
183 * another probe was hit while within the handler.
184 * We here save the original kprobes variables and
185 * just single step on the instruction of the new probe
186 * without calling any user handlers.
187 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800188 save_previous_kprobe(kcb);
189 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800190 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700191 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800192 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700193 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 } else {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800195 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (p->break_handler && p->break_handler(p, regs)) {
197 goto ss_probe;
198 }
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto no_kprobe;
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 p = get_kprobe(addr);
204 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (regs->eflags & VM_MASK) {
206 /* We are in virtual-8086 mode. Return 0 */
207 goto no_kprobe;
208 }
209
210 if (*addr != BREAKPOINT_INSTRUCTION) {
211 /*
212 * The breakpoint instruction was removed right
213 * after we hit it. Another cpu has removed
214 * either a probepoint or a debugger breakpoint
215 * at this address. In either case, no further
216 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700217 * Back up over the (now missing) int3 and run
218 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700220 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ret = 1;
222 }
223 /* Not one of ours: let kernel handle it */
224 goto no_kprobe;
225 }
226
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800227 set_current_kprobe(p, regs, kcb);
228 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (p->pre_handler && p->pre_handler(p, regs))
231 /* handler has already set things up, so skip ss setup */
232 return 1;
233
234ss_probe:
235 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800236 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 1;
238
239no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800240 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return ret;
242}
243
244/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700245 * For function-return probes, init_kprobes() establishes a probepoint
246 * here. When a retprobed function returns, this probe is hit and
247 * trampoline_probe_handler() runs, calling the kretprobe's handler.
248 */
249 void kretprobe_trampoline_holder(void)
250 {
251 asm volatile ( ".global kretprobe_trampoline\n"
252 "kretprobe_trampoline: \n"
253 "nop\n");
254 }
255
256/*
257 * Called when we hit the probe point at kretprobe_trampoline
258 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700259int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700260{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700261 struct kretprobe_instance *ri = NULL;
262 struct hlist_head *head;
263 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800264 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700265 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700266
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800267 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700268 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700269
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700270 /*
271 * It is possible to have multiple instances associated with a given
272 * task either because an multiple functions in the call path
273 * have a return probe installed on them, and/or more then one return
274 * return probe was registered for a target function.
275 *
276 * We can handle this because:
277 * - instances are always inserted at the head of the list
278 * - when multiple return probes are registered for the same
279 * function, the first instance's ret_addr will point to the
280 * real return address, and all the rest will point to
281 * kretprobe_trampoline
282 */
283 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
284 if (ri->task != current)
285 /* another task is sharing our hash bucket */
286 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700287
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700288 if (ri->rp && ri->rp->handler)
289 ri->rp->handler(ri, regs);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700290
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700291 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700292 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700293
294 if (orig_ret_address != trampoline_address)
295 /*
296 * This is the real return address. Any other
297 * instances associated with this task are for
298 * other calls deeper on the call stack
299 */
300 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700301 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700302
303 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
304 regs->eip = orig_ret_address;
305
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800306 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800307 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700308 preempt_enable_no_resched();
309
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800310 /*
311 * By returning a non-zero value, we are telling
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800312 * kprobe_handler() that we don't want the post_handler
313 * to run (and have re-enabled preemption)
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800314 */
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700315 return 1;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700316}
317
318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Called after single-stepping. p->addr is the address of the
320 * instruction whose first byte has been replaced by the "int 3"
321 * instruction. To avoid the SMP problems that can occur when we
322 * temporarily put back the original opcode to single-step, we
323 * single-stepped a copy of the instruction. The address of this
324 * copy is p->ainsn.insn.
325 *
326 * This function prepares to return from the post-single-step
327 * interrupt. We have to fix up the stack as follows:
328 *
329 * 0) Except in the case of absolute or indirect jump or call instructions,
330 * the new eip is relative to the copied instruction. We need to make
331 * it relative to the original instruction.
332 *
333 * 1) If the single-stepped instruction was pushfl, then the TF and IF
334 * flags are set in the just-pushed eflags, and may need to be cleared.
335 *
336 * 2) If the single-stepped instruction was a call, the return address
337 * that is atop the stack is the address following the copied instruction.
338 * We need to make it the address following the original instruction.
339 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800340static void __kprobes resume_execution(struct kprobe *p,
341 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 unsigned long *tos = (unsigned long *)&regs->esp;
344 unsigned long next_eip = 0;
345 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
346 unsigned long orig_eip = (unsigned long)p->addr;
347
348 switch (p->ainsn.insn[0]) {
349 case 0x9c: /* pushfl */
350 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800351 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700353 case 0xc3: /* ret/lret */
354 case 0xcb:
355 case 0xc2:
356 case 0xca:
357 regs->eflags &= ~TF_MASK;
358 /* eip is already adjusted, no more changes required*/
359 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case 0xe8: /* call relative - Fix return addr */
361 *tos = orig_eip + (*tos - copy_eip);
362 break;
363 case 0xff:
364 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
365 /* call absolute, indirect */
366 /* Fix return addr; eip is correct. */
367 next_eip = regs->eip;
368 *tos = orig_eip + (*tos - copy_eip);
369 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
370 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
371 /* eip is correct. */
372 next_eip = regs->eip;
373 }
374 break;
375 case 0xea: /* jmp absolute -- eip is correct */
376 next_eip = regs->eip;
377 break;
378 default:
379 break;
380 }
381
382 regs->eflags &= ~TF_MASK;
383 if (next_eip) {
384 regs->eip = next_eip;
385 } else {
386 regs->eip = orig_eip + (regs->eip - copy_eip);
387 }
388}
389
390/*
391 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800392 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
394static inline int post_kprobe_handler(struct pt_regs *regs)
395{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800396 struct kprobe *cur = kprobe_running();
397 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
398
399 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800402 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
403 kcb->kprobe_status = KPROBE_HIT_SSDONE;
404 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800407 resume_execution(cur, regs, kcb);
408 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700410 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800411 if (kcb->kprobe_status == KPROBE_REENTER) {
412 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700413 goto out;
414 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800415 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700416out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 preempt_enable_no_resched();
418
419 /*
420 * if somebody else is singlestepping across a probe point, eflags
421 * will have TF set, in which case, continue the remaining processing
422 * of do_debug, as if this is not a probe hit.
423 */
424 if (regs->eflags & TF_MASK)
425 return 0;
426
427 return 1;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
431{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800432 struct kprobe *cur = kprobe_running();
433 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
434
435 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 1;
437
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800438 if (kcb->kprobe_status & KPROBE_HIT_SS) {
439 resume_execution(cur, regs, kcb);
440 regs->eflags |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800442 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 preempt_enable_no_resched();
444 }
445 return 0;
446}
447
448/*
449 * Wrapper routine to for handling exceptions.
450 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700451int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
452 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800455 int ret = NOTIFY_DONE;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 switch (val) {
458 case DIE_INT3:
459 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800460 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case DIE_DEBUG:
463 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800464 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800468 /* kprobe_running() needs smp_processor_id() */
469 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (kprobe_running() &&
471 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800472 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800473 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 default:
476 break;
477 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700481int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct jprobe *jp = container_of(p, struct jprobe, kp);
484 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800485 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800487 kcb->jprobe_saved_regs = *regs;
488 kcb->jprobe_saved_esp = &regs->esp;
489 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * TBD: As Linus pointed out, gcc assumes that the callee
493 * owns the argument space and could overwrite it, e.g.
494 * tailcall optimization. So, to be absolutely safe
495 * we also save and restore enough stack bytes to cover
496 * the argument area.
497 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800498 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
499 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 regs->eflags &= ~IF_MASK;
501 regs->eip = (unsigned long)(jp->entry);
502 return 1;
503}
504
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700505void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800507 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 asm volatile (" xchgl %%ebx,%%esp \n"
510 " int3 \n"
511 " .globl jprobe_return_end \n"
512 " jprobe_return_end: \n"
513 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800514 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700517int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800519 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800521 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct jprobe *jp = container_of(p, struct jprobe, kp);
523
524 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800525 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800527 container_of(kcb->jprobe_saved_esp,
528 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800530 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 printk("Saved registers for jprobe %p\n", jp);
532 show_registers(saved_regs);
533 printk("Current registers\n");
534 show_registers(regs);
535 BUG();
536 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800537 *regs = kcb->jprobe_saved_regs;
538 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800540 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 1;
542 }
543 return 0;
544}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700545
546static struct kprobe trampoline_p = {
547 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
548 .pre_handler = trampoline_probe_handler
549};
550
Rusty Lynch67729262005-07-05 18:54:50 -0700551int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700552{
553 return register_kprobe(&trampoline_p);
554}