blob: 38806f427849f7f4dc8c76891229f952791064c3 [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>
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -080038#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040void jprobe_return_end(void);
41
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080042DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
43DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
44
Masami Hiramatsu311ac882006-03-26 01:38:17 -080045/* insert a jmp code */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -070046static __always_inline void set_jmp_op(void *from, void *to)
Masami Hiramatsu311ac882006-03-26 01:38:17 -080047{
48 struct __arch_jmp_op {
49 char op;
50 long raddr;
51 } __attribute__((packed)) *jop;
52 jop = (struct __arch_jmp_op *)from;
53 jop->raddr = (long)(to) - ((long)(from) + 5);
54 jop->op = RELATIVEJUMP_INSTRUCTION;
55}
56
57/*
58 * returns non-zero if opcodes can be boosted.
59 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -070060static __always_inline int can_boost(kprobe_opcode_t opcode)
Masami Hiramatsu311ac882006-03-26 01:38:17 -080061{
62 switch (opcode & 0xf0 ) {
63 case 0x70:
64 return 0; /* can't boost conditional jump */
65 case 0x90:
66 /* can't boost call and pushf */
67 return opcode != 0x9a && opcode != 0x9c;
68 case 0xc0:
69 /* can't boost undefined opcodes and soft-interruptions */
70 return (0xc1 < opcode && opcode < 0xc6) ||
71 (0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
72 case 0xd0:
73 /* can boost AA* and XLAT */
74 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
75 case 0xe0:
76 /* can boost in/out and (may be) jmps */
77 return (0xe3 < opcode && opcode != 0xe8);
78 case 0xf0:
79 /* clear and set flags can be boost */
80 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
81 default:
82 /* currently, can't boost 2 bytes opcodes */
83 return opcode != 0x0f;
84 }
85}
86
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * returns non-zero if opcode modifies the interrupt flag.
90 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -070091static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 switch (opcode) {
94 case 0xfa: /* cli */
95 case 0xfb: /* sti */
96 case 0xcf: /* iret/iretd */
97 case 0x9d: /* popf/popfd */
98 return 1;
99 }
100 return 0;
101}
102
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700103int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800105 /* insn: must be on special executable page on i386. */
106 p->ainsn.insn = get_insn_slot();
107 if (!p->ainsn.insn)
108 return -ENOMEM;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700111 p->opcode = *p->addr;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800112 if (can_boost(p->opcode)) {
113 p->ainsn.boostable = 0;
114 } else {
115 p->ainsn.boostable = -1;
116 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800117 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700118}
119
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700120void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700121{
122 *p->addr = BREAKPOINT_INSTRUCTION;
123 flush_icache_range((unsigned long) p->addr,
124 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
125}
126
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700127void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700128{
129 *p->addr = p->opcode;
130 flush_icache_range((unsigned long) p->addr,
131 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800134void __kprobes arch_remove_kprobe(struct kprobe *p)
135{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800136 mutex_lock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800137 free_insn_slot(p->ainsn.insn);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800138 mutex_unlock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800139}
140
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700141static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700142{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800143 kcb->prev_kprobe.kp = kprobe_running();
144 kcb->prev_kprobe.status = kcb->kprobe_status;
145 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
146 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700147}
148
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700149static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700150{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800151 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
152 kcb->kprobe_status = kcb->prev_kprobe.status;
153 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
154 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700155}
156
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700157static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800158 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700159{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800160 __get_cpu_var(current_kprobe) = p;
161 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700162 = (regs->eflags & (TF_MASK | IF_MASK));
163 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800164 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700165}
166
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700167static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 regs->eflags |= TF_MASK;
170 regs->eflags &= ~IF_MASK;
171 /*single step inline if the instruction is an int3*/
172 if (p->opcode == BREAKPOINT_INSTRUCTION)
173 regs->eip = (unsigned long)p->addr;
174 else
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800175 regs->eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800178/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700179void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
180 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700181{
182 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700183 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700184
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700185 if ((ri = get_free_rp_inst(rp)) != NULL) {
186 ri->rp = rp;
187 ri->task = current;
188 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700189
Hien Nguyenb94cce92005-06-23 00:09:19 -0700190 /* Replace the return addr with trampoline addr */
191 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700192
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700193 add_rp_inst(ri);
194 } else {
195 rp->nmissed++;
196 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
201 * remain disabled thorough out this function.
202 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700203static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct kprobe *p;
206 int ret = 0;
bibo,mao2326c772006-03-26 01:38:21 -0800207 kprobe_opcode_t *addr;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800208 struct kprobe_ctlblk *kcb;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800209#ifdef CONFIG_PREEMPT
210 unsigned pre_preempt_count = preempt_count();
211#endif /* CONFIG_PREEMPT */
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800212
bibo,mao2326c772006-03-26 01:38:21 -0800213 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
214
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800215 /*
216 * We don't want to be preempted for the entire
217 * duration of kprobe processing
218 */
219 preempt_disable();
220 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Check we're not actually recursing */
223 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 p = get_kprobe(addr);
225 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800226 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700227 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800229 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto no_kprobe;
231 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700232 /* We have reentered the kprobe_handler(), since
233 * another probe was hit while within the handler.
234 * We here save the original kprobes variables and
235 * just single step on the instruction of the new probe
236 * without calling any user handlers.
237 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800238 save_previous_kprobe(kcb);
239 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800240 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700241 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800242 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700243 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800245 if (*addr != BREAKPOINT_INSTRUCTION) {
246 /* The breakpoint instruction was removed by
247 * another cpu right after we hit, no further
248 * handling of this interrupt is appropriate
249 */
250 regs->eip -= sizeof(kprobe_opcode_t);
251 ret = 1;
252 goto no_kprobe;
253 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800254 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (p->break_handler && p->break_handler(p, regs)) {
256 goto ss_probe;
257 }
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto no_kprobe;
260 }
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 p = get_kprobe(addr);
263 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (*addr != BREAKPOINT_INSTRUCTION) {
265 /*
266 * The breakpoint instruction was removed right
267 * after we hit it. Another cpu has removed
268 * either a probepoint or a debugger breakpoint
269 * at this address. In either case, no further
270 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700271 * Back up over the (now missing) int3 and run
272 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700274 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ret = 1;
276 }
277 /* Not one of ours: let kernel handle it */
278 goto no_kprobe;
279 }
280
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800281 set_current_kprobe(p, regs, kcb);
282 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (p->pre_handler && p->pre_handler(p, regs))
285 /* handler has already set things up, so skip ss setup */
286 return 1;
287
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800288 if (p->ainsn.boostable == 1 &&
289#ifdef CONFIG_PREEMPT
290 !(pre_preempt_count) && /*
291 * This enables booster when the direct
292 * execution path aren't preempted.
293 */
294#endif /* CONFIG_PREEMPT */
295 !p->post_handler && !p->break_handler ) {
296 /* Boost up -- we can execute copied instructions directly */
297 reset_current_kprobe();
298 regs->eip = (unsigned long)p->ainsn.insn;
299 preempt_enable_no_resched();
300 return 1;
301 }
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303ss_probe:
304 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800305 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 1;
307
308no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800309 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return ret;
311}
312
313/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700314 * For function-return probes, init_kprobes() establishes a probepoint
315 * here. When a retprobed function returns, this probe is hit and
316 * trampoline_probe_handler() runs, calling the kretprobe's handler.
317 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800318 void __kprobes kretprobe_trampoline_holder(void)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700319 {
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800320 asm volatile ( ".global kretprobe_trampoline\n"
Hien Nguyenb94cce92005-06-23 00:09:19 -0700321 "kretprobe_trampoline: \n"
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800322 " pushf\n"
323 /* skip cs, eip, orig_eax, es, ds */
324 " subl $20, %esp\n"
325 " pushl %eax\n"
326 " pushl %ebp\n"
327 " pushl %edi\n"
328 " pushl %esi\n"
329 " pushl %edx\n"
330 " pushl %ecx\n"
331 " pushl %ebx\n"
332 " movl %esp, %eax\n"
333 " call trampoline_handler\n"
334 /* move eflags to cs */
335 " movl 48(%esp), %edx\n"
336 " movl %edx, 44(%esp)\n"
337 /* save true return address on eflags */
338 " movl %eax, 48(%esp)\n"
339 " popl %ebx\n"
340 " popl %ecx\n"
341 " popl %edx\n"
342 " popl %esi\n"
343 " popl %edi\n"
344 " popl %ebp\n"
345 " popl %eax\n"
346 /* skip eip, orig_eax, es, ds */
347 " addl $16, %esp\n"
348 " popf\n"
349 " ret\n");
350}
Hien Nguyenb94cce92005-06-23 00:09:19 -0700351
352/*
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800353 * Called from kretprobe_trampoline
Hien Nguyenb94cce92005-06-23 00:09:19 -0700354 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800355fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700356{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700357 struct kretprobe_instance *ri = NULL;
358 struct hlist_head *head;
359 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800360 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700361 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700362
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800363 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700364 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700365
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700366 /*
367 * It is possible to have multiple instances associated with a given
368 * task either because an multiple functions in the call path
369 * have a return probe installed on them, and/or more then one return
370 * return probe was registered for a target function.
371 *
372 * We can handle this because:
373 * - instances are always inserted at the head of the list
374 * - when multiple return probes are registered for the same
375 * function, the first instance's ret_addr will point to the
376 * real return address, and all the rest will point to
377 * kretprobe_trampoline
378 */
379 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
380 if (ri->task != current)
381 /* another task is sharing our hash bucket */
382 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700383
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800384 if (ri->rp && ri->rp->handler){
385 __get_cpu_var(current_kprobe) = &ri->rp->kp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700386 ri->rp->handler(ri, regs);
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800387 __get_cpu_var(current_kprobe) = NULL;
388 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700389
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700390 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700391 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700392
393 if (orig_ret_address != trampoline_address)
394 /*
395 * This is the real return address. Any other
396 * instances associated with this task are for
397 * other calls deeper on the call stack
398 */
399 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700400 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700401
402 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700403
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800404 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700405
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800406 return (void*)orig_ret_address;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700407}
408
409/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * Called after single-stepping. p->addr is the address of the
411 * instruction whose first byte has been replaced by the "int 3"
412 * instruction. To avoid the SMP problems that can occur when we
413 * temporarily put back the original opcode to single-step, we
414 * single-stepped a copy of the instruction. The address of this
415 * copy is p->ainsn.insn.
416 *
417 * This function prepares to return from the post-single-step
418 * interrupt. We have to fix up the stack as follows:
419 *
420 * 0) Except in the case of absolute or indirect jump or call instructions,
421 * the new eip is relative to the copied instruction. We need to make
422 * it relative to the original instruction.
423 *
424 * 1) If the single-stepped instruction was pushfl, then the TF and IF
425 * flags are set in the just-pushed eflags, and may need to be cleared.
426 *
427 * 2) If the single-stepped instruction was a call, the return address
428 * that is atop the stack is the address following the copied instruction.
429 * We need to make it the address following the original instruction.
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800430 *
431 * This function also checks instruction size for preparing direct execution.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800433static void __kprobes resume_execution(struct kprobe *p,
434 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned long *tos = (unsigned long *)&regs->esp;
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800437 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long orig_eip = (unsigned long)p->addr;
439
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800440 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 switch (p->ainsn.insn[0]) {
442 case 0x9c: /* pushfl */
443 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800444 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700446 case 0xc2: /* iret/ret/lret */
447 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700448 case 0xca:
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700449 case 0xcb:
450 case 0xcf:
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800451 case 0xea: /* jmp absolute -- eip is correct */
452 /* eip is already adjusted, no more changes required */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800453 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800454 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case 0xe8: /* call relative - Fix return addr */
456 *tos = orig_eip + (*tos - copy_eip);
457 break;
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700458 case 0x9a: /* call absolute -- same as call absolute, indirect */
459 *tos = orig_eip + (*tos - copy_eip);
460 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case 0xff:
462 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800463 /*
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700464 * call absolute, indirect
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800465 * Fix return addr; eip is correct.
466 * But this is not boostable
467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *tos = orig_eip + (*tos - copy_eip);
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800469 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
471 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800472 /* eip is correct. And this is boostable */
473 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800474 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 default:
477 break;
478 }
479
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800480 if (p->ainsn.boostable == 0) {
481 if ((regs->eip > copy_eip) &&
482 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
483 /*
484 * These instructions can be executed directly if it
485 * jumps back to correct address.
486 */
487 set_jmp_op((void *)regs->eip,
488 (void *)orig_eip + (regs->eip - copy_eip));
489 p->ainsn.boostable = 1;
490 } else {
491 p->ainsn.boostable = -1;
492 }
493 }
494
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800495 regs->eip = orig_eip + (regs->eip - copy_eip);
496
497no_change:
498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/*
502 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800503 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700505static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800507 struct kprobe *cur = kprobe_running();
508 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
509
510 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
512
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800513 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
514 kcb->kprobe_status = KPROBE_HIT_SSDONE;
515 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800518 resume_execution(cur, regs, kcb);
519 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700521 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800522 if (kcb->kprobe_status == KPROBE_REENTER) {
523 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700524 goto out;
525 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800526 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700527out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 preempt_enable_no_resched();
529
530 /*
531 * if somebody else is singlestepping across a probe point, eflags
532 * will have TF set, in which case, continue the remaining processing
533 * of do_debug, as if this is not a probe hit.
534 */
535 if (regs->eflags & TF_MASK)
536 return 0;
537
538 return 1;
539}
540
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700541static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800543 struct kprobe *cur = kprobe_running();
544 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
545
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800546 switch(kcb->kprobe_status) {
547 case KPROBE_HIT_SS:
548 case KPROBE_REENTER:
549 /*
550 * We are here because the instruction being single
551 * stepped caused a page fault. We reset the current
552 * kprobe and the eip points back to the probe address
553 * and allow the page fault handler to continue as a
554 * normal page fault.
555 */
556 regs->eip = (unsigned long)cur->addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800557 regs->eflags |= kcb->kprobe_old_eflags;
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800558 if (kcb->kprobe_status == KPROBE_REENTER)
559 restore_previous_kprobe(kcb);
560 else
561 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 preempt_enable_no_resched();
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800563 break;
564 case KPROBE_HIT_ACTIVE:
565 case KPROBE_HIT_SSDONE:
566 /*
567 * We increment the nmissed count for accounting,
568 * we can also use npre/npostfault count for accouting
569 * these specific fault cases.
570 */
571 kprobes_inc_nmissed_count(cur);
572
573 /*
574 * We come here because instructions in the pre/post
575 * handler caused the page_fault, this could happen
576 * if handler tries to access user space by
577 * copy_from_user(), get_user() etc. Let the
578 * user-specified handler try to fix it first.
579 */
580 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
581 return 1;
582
583 /*
584 * In case the user-specified fault handler returned
585 * zero, try to fix up.
586 */
587 if (fixup_exception(regs))
588 return 1;
589
590 /*
591 * fixup_exception() could not handle it,
592 * Let do_page_fault() fix it.
593 */
594 break;
595 default:
596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 return 0;
599}
600
601/*
602 * Wrapper routine to for handling exceptions.
603 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700604int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
605 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800608 int ret = NOTIFY_DONE;
609
bibo,mao2326c772006-03-26 01:38:21 -0800610 if (args->regs && user_mode(args->regs))
611 return ret;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 switch (val) {
614 case DIE_INT3:
615 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800616 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 case DIE_DEBUG:
619 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800620 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800624 /* kprobe_running() needs smp_processor_id() */
625 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (kprobe_running() &&
627 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800628 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800629 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
631 default:
632 break;
633 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700637int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct jprobe *jp = container_of(p, struct jprobe, kp);
640 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800641 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800643 kcb->jprobe_saved_regs = *regs;
644 kcb->jprobe_saved_esp = &regs->esp;
645 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * TBD: As Linus pointed out, gcc assumes that the callee
649 * owns the argument space and could overwrite it, e.g.
650 * tailcall optimization. So, to be absolutely safe
651 * we also save and restore enough stack bytes to cover
652 * the argument area.
653 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800654 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
655 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 regs->eflags &= ~IF_MASK;
657 regs->eip = (unsigned long)(jp->entry);
658 return 1;
659}
660
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700661void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800663 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 asm volatile (" xchgl %%ebx,%%esp \n"
666 " int3 \n"
667 " .globl jprobe_return_end \n"
668 " jprobe_return_end: \n"
669 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800670 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700673int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800675 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800677 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct jprobe *jp = container_of(p, struct jprobe, kp);
679
680 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800681 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800683 container_of(kcb->jprobe_saved_esp,
684 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800686 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 printk("Saved registers for jprobe %p\n", jp);
688 show_registers(saved_regs);
689 printk("Current registers\n");
690 show_registers(regs);
691 BUG();
692 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800693 *regs = kcb->jprobe_saved_regs;
694 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800696 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 1;
698 }
699 return 0;
700}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700701
Rusty Lynch67729262005-07-05 18:54:50 -0700702int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700703{
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800704 return 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700705}