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 | ||
1da177e4 LT |
31 | #include <linux/kprobes.h> |
32 | #include <linux/ptrace.h> | |
1da177e4 | 33 | #include <linux/preempt.h> |
7e1048b1 | 34 | #include <asm/cacheflush.h> |
1da177e4 LT |
35 | #include <asm/kdebug.h> |
36 | #include <asm/desc.h> | |
b4026513 | 37 | #include <asm/uaccess.h> |
1da177e4 | 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 | ||
311ac88f | 44 | /* insert a jmp code */ |
34c37e18 | 45 | static __always_inline void set_jmp_op(void *from, void *to) |
311ac88f MH |
46 | { |
47 | struct __arch_jmp_op { | |
48 | char op; | |
49 | long raddr; | |
50 | } __attribute__((packed)) *jop; | |
51 | jop = (struct __arch_jmp_op *)from; | |
52 | jop->raddr = (long)(to) - ((long)(from) + 5); | |
53 | jop->op = RELATIVEJUMP_INSTRUCTION; | |
54 | } | |
55 | ||
56 | /* | |
57 | * returns non-zero if opcodes can be boosted. | |
58 | */ | |
585deaca | 59 | static __always_inline int can_boost(kprobe_opcode_t *opcodes) |
311ac88f | 60 | { |
585deaca MH |
61 | #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \ |
62 | (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \ | |
63 | (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \ | |
64 | (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \ | |
65 | (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \ | |
66 | << (row % 32)) | |
67 | /* | |
68 | * Undefined/reserved opcodes, conditional jump, Opcode Extension | |
69 | * Groups, and some special opcodes can not be boost. | |
70 | */ | |
71 | static const unsigned long twobyte_is_boostable[256 / 32] = { | |
72 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
73 | /* ------------------------------- */ | |
74 | W(0x00, 0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0)| /* 00 */ | |
75 | W(0x10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 10 */ | |
76 | W(0x20, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)| /* 20 */ | |
77 | W(0x30, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 30 */ | |
78 | W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 40 */ | |
79 | W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 50 */ | |
80 | W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1)| /* 60 */ | |
81 | W(0x70, 0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1), /* 70 */ | |
82 | W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 80 */ | |
83 | W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), /* 90 */ | |
84 | W(0xa0, 1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1)| /* a0 */ | |
85 | W(0xb0, 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1), /* b0 */ | |
86 | W(0xc0, 1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1)| /* c0 */ | |
87 | W(0xd0, 0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1), /* d0 */ | |
88 | W(0xe0, 0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1)| /* e0 */ | |
89 | W(0xf0, 0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0) /* f0 */ | |
90 | /* ------------------------------- */ | |
91 | /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ | |
92 | }; | |
93 | #undef W | |
94 | kprobe_opcode_t opcode; | |
95 | kprobe_opcode_t *orig_opcodes = opcodes; | |
96 | retry: | |
97 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
98 | return 0; | |
99 | opcode = *(opcodes++); | |
100 | ||
101 | /* 2nd-byte opcode */ | |
102 | if (opcode == 0x0f) { | |
103 | if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1) | |
104 | return 0; | |
105 | return test_bit(*opcodes, twobyte_is_boostable); | |
106 | } | |
107 | ||
108 | switch (opcode & 0xf0) { | |
109 | case 0x60: | |
110 | if (0x63 < opcode && opcode < 0x67) | |
111 | goto retry; /* prefixes */ | |
112 | /* can't boost Address-size override and bound */ | |
113 | return (opcode != 0x62 && opcode != 0x67); | |
311ac88f MH |
114 | case 0x70: |
115 | return 0; /* can't boost conditional jump */ | |
311ac88f | 116 | case 0xc0: |
585deaca MH |
117 | /* can't boost software-interruptions */ |
118 | return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf; | |
311ac88f MH |
119 | case 0xd0: |
120 | /* can boost AA* and XLAT */ | |
121 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); | |
122 | case 0xe0: | |
585deaca MH |
123 | /* can boost in/out and absolute jmps */ |
124 | return ((opcode & 0x04) || opcode == 0xea); | |
311ac88f | 125 | case 0xf0: |
585deaca MH |
126 | if ((opcode & 0x0c) == 0 && opcode != 0xf1) |
127 | goto retry; /* lock/rep(ne) prefix */ | |
311ac88f MH |
128 | /* clear and set flags can be boost */ |
129 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); | |
130 | default: | |
585deaca MH |
131 | if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e) |
132 | goto retry; /* prefixes */ | |
133 | /* can't boost CS override and call */ | |
134 | return (opcode != 0x2e && opcode != 0x9a); | |
311ac88f MH |
135 | } |
136 | } | |
137 | ||
1da177e4 LT |
138 | /* |
139 | * returns non-zero if opcode modifies the interrupt flag. | |
140 | */ | |
34c37e18 | 141 | static int __kprobes is_IF_modifier(kprobe_opcode_t opcode) |
1da177e4 LT |
142 | { |
143 | switch (opcode) { | |
144 | case 0xfa: /* cli */ | |
145 | case 0xfb: /* sti */ | |
146 | case 0xcf: /* iret/iretd */ | |
147 | case 0x9d: /* popf/popfd */ | |
148 | return 1; | |
149 | } | |
150 | return 0; | |
151 | } | |
152 | ||
3d97ae5b | 153 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
1da177e4 | 154 | { |
124d90be PP |
155 | /* insn: must be on special executable page on i386. */ |
156 | p->ainsn.insn = get_insn_slot(); | |
157 | if (!p->ainsn.insn) | |
158 | return -ENOMEM; | |
159 | ||
1da177e4 | 160 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
7e1048b1 | 161 | p->opcode = *p->addr; |
585deaca | 162 | if (can_boost(p->addr)) { |
311ac88f MH |
163 | p->ainsn.boostable = 0; |
164 | } else { | |
165 | p->ainsn.boostable = -1; | |
166 | } | |
49a2a1b8 | 167 | return 0; |
1da177e4 LT |
168 | } |
169 | ||
3d97ae5b | 170 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
1da177e4 | 171 | { |
7e1048b1 RL |
172 | *p->addr = BREAKPOINT_INSTRUCTION; |
173 | flush_icache_range((unsigned long) p->addr, | |
174 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); | |
1da177e4 LT |
175 | } |
176 | ||
3d97ae5b | 177 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
1da177e4 LT |
178 | { |
179 | *p->addr = p->opcode; | |
7e1048b1 RL |
180 | flush_icache_range((unsigned long) p->addr, |
181 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); | |
182 | } | |
183 | ||
124d90be PP |
184 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
185 | { | |
7a7d1cf9 | 186 | mutex_lock(&kprobe_mutex); |
124d90be | 187 | free_insn_slot(p->ainsn.insn); |
7a7d1cf9 | 188 | mutex_unlock(&kprobe_mutex); |
124d90be PP |
189 | } |
190 | ||
34c37e18 | 191 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
417c8da6 | 192 | { |
9a0e3a86 AM |
193 | kcb->prev_kprobe.kp = kprobe_running(); |
194 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
195 | kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags; | |
196 | kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags; | |
417c8da6 PP |
197 | } |
198 | ||
34c37e18 | 199 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
417c8da6 | 200 | { |
9a0e3a86 AM |
201 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
202 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
203 | kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags; | |
204 | kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags; | |
417c8da6 PP |
205 | } |
206 | ||
34c37e18 | 207 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
9a0e3a86 | 208 | struct kprobe_ctlblk *kcb) |
417c8da6 | 209 | { |
9a0e3a86 AM |
210 | __get_cpu_var(current_kprobe) = p; |
211 | kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags | |
417c8da6 PP |
212 | = (regs->eflags & (TF_MASK | IF_MASK)); |
213 | if (is_IF_modifier(p->opcode)) | |
9a0e3a86 | 214 | kcb->kprobe_saved_eflags &= ~IF_MASK; |
417c8da6 PP |
215 | } |
216 | ||
34c37e18 | 217 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
218 | { |
219 | regs->eflags |= TF_MASK; | |
220 | regs->eflags &= ~IF_MASK; | |
221 | /*single step inline if the instruction is an int3*/ | |
222 | if (p->opcode == BREAKPOINT_INSTRUCTION) | |
223 | regs->eip = (unsigned long)p->addr; | |
224 | else | |
124d90be | 225 | regs->eip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
226 | } |
227 | ||
991a51d8 | 228 | /* Called with kretprobe_lock held */ |
3d97ae5b PP |
229 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
230 | struct pt_regs *regs) | |
b94cce92 HN |
231 | { |
232 | unsigned long *sara = (unsigned long *)®s->esp; | |
4bdbd37f RL |
233 | struct kretprobe_instance *ri; |
234 | ||
235 | if ((ri = get_free_rp_inst(rp)) != NULL) { | |
236 | ri->rp = rp; | |
237 | ri->task = current; | |
238 | ri->ret_addr = (kprobe_opcode_t *) *sara; | |
b94cce92 | 239 | |
b94cce92 HN |
240 | /* Replace the return addr with trampoline addr */ |
241 | *sara = (unsigned long) &kretprobe_trampoline; | |
b94cce92 | 242 | |
4bdbd37f RL |
243 | add_rp_inst(ri); |
244 | } else { | |
245 | rp->nmissed++; | |
246 | } | |
b94cce92 HN |
247 | } |
248 | ||
1da177e4 LT |
249 | /* |
250 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | |
251 | * remain disabled thorough out this function. | |
252 | */ | |
3d97ae5b | 253 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
1da177e4 LT |
254 | { |
255 | struct kprobe *p; | |
256 | int ret = 0; | |
2326c770 | 257 | kprobe_opcode_t *addr; |
d217d545 | 258 | struct kprobe_ctlblk *kcb; |
311ac88f MH |
259 | #ifdef CONFIG_PREEMPT |
260 | unsigned pre_preempt_count = preempt_count(); | |
36721656 | 261 | #else |
262 | unsigned pre_preempt_count = 1; | |
263 | #endif | |
d217d545 | 264 | |
2326c770 | 265 | addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t)); |
266 | ||
d217d545 AM |
267 | /* |
268 | * We don't want to be preempted for the entire | |
269 | * duration of kprobe processing | |
270 | */ | |
271 | preempt_disable(); | |
272 | kcb = get_kprobe_ctlblk(); | |
1da177e4 | 273 | |
1da177e4 LT |
274 | /* Check we're not actually recursing */ |
275 | if (kprobe_running()) { | |
1da177e4 LT |
276 | p = get_kprobe(addr); |
277 | if (p) { | |
9a0e3a86 | 278 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
deac66ae | 279 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { |
1da177e4 | 280 | regs->eflags &= ~TF_MASK; |
9a0e3a86 | 281 | regs->eflags |= kcb->kprobe_saved_eflags; |
1da177e4 LT |
282 | goto no_kprobe; |
283 | } | |
417c8da6 PP |
284 | /* We have reentered the kprobe_handler(), since |
285 | * another probe was hit while within the handler. | |
286 | * We here save the original kprobes variables and | |
287 | * just single step on the instruction of the new probe | |
288 | * without calling any user handlers. | |
289 | */ | |
9a0e3a86 AM |
290 | save_previous_kprobe(kcb); |
291 | set_current_kprobe(p, regs, kcb); | |
bf8d5c52 | 292 | kprobes_inc_nmissed_count(p); |
417c8da6 | 293 | prepare_singlestep(p, regs); |
9a0e3a86 | 294 | kcb->kprobe_status = KPROBE_REENTER; |
417c8da6 | 295 | return 1; |
1da177e4 | 296 | } else { |
eb3a7292 KA |
297 | if (*addr != BREAKPOINT_INSTRUCTION) { |
298 | /* The breakpoint instruction was removed by | |
299 | * another cpu right after we hit, no further | |
300 | * handling of this interrupt is appropriate | |
301 | */ | |
302 | regs->eip -= sizeof(kprobe_opcode_t); | |
303 | ret = 1; | |
304 | goto no_kprobe; | |
305 | } | |
9a0e3a86 | 306 | p = __get_cpu_var(current_kprobe); |
1da177e4 LT |
307 | if (p->break_handler && p->break_handler(p, regs)) { |
308 | goto ss_probe; | |
309 | } | |
310 | } | |
1da177e4 LT |
311 | goto no_kprobe; |
312 | } | |
313 | ||
1da177e4 LT |
314 | p = get_kprobe(addr); |
315 | if (!p) { | |
1da177e4 LT |
316 | if (*addr != BREAKPOINT_INSTRUCTION) { |
317 | /* | |
318 | * The breakpoint instruction was removed right | |
319 | * after we hit it. Another cpu has removed | |
320 | * either a probepoint or a debugger breakpoint | |
321 | * at this address. In either case, no further | |
322 | * handling of this interrupt is appropriate. | |
bce06494 JK |
323 | * Back up over the (now missing) int3 and run |
324 | * the original instruction. | |
1da177e4 | 325 | */ |
bce06494 | 326 | regs->eip -= sizeof(kprobe_opcode_t); |
1da177e4 LT |
327 | ret = 1; |
328 | } | |
329 | /* Not one of ours: let kernel handle it */ | |
330 | goto no_kprobe; | |
331 | } | |
332 | ||
9a0e3a86 AM |
333 | set_current_kprobe(p, regs, kcb); |
334 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
1da177e4 LT |
335 | |
336 | if (p->pre_handler && p->pre_handler(p, regs)) | |
337 | /* handler has already set things up, so skip ss setup */ | |
338 | return 1; | |
339 | ||
36721656 | 340 | ss_probe: |
341 | if (pre_preempt_count && p->ainsn.boostable == 1 && !p->post_handler){ | |
311ac88f MH |
342 | /* Boost up -- we can execute copied instructions directly */ |
343 | reset_current_kprobe(); | |
344 | regs->eip = (unsigned long)p->ainsn.insn; | |
345 | preempt_enable_no_resched(); | |
346 | return 1; | |
347 | } | |
1da177e4 | 348 | prepare_singlestep(p, regs); |
9a0e3a86 | 349 | kcb->kprobe_status = KPROBE_HIT_SS; |
1da177e4 LT |
350 | return 1; |
351 | ||
352 | no_kprobe: | |
d217d545 | 353 | preempt_enable_no_resched(); |
1da177e4 LT |
354 | return ret; |
355 | } | |
356 | ||
b94cce92 HN |
357 | /* |
358 | * For function-return probes, init_kprobes() establishes a probepoint | |
359 | * here. When a retprobed function returns, this probe is hit and | |
360 | * trampoline_probe_handler() runs, calling the kretprobe's handler. | |
361 | */ | |
c9becf58 | 362 | void __kprobes kretprobe_trampoline_holder(void) |
b94cce92 | 363 | { |
c9becf58 | 364 | asm volatile ( ".global kretprobe_trampoline\n" |
b94cce92 | 365 | "kretprobe_trampoline: \n" |
c9becf58 MH |
366 | " pushf\n" |
367 | /* skip cs, eip, orig_eax, es, ds */ | |
368 | " subl $20, %esp\n" | |
369 | " pushl %eax\n" | |
370 | " pushl %ebp\n" | |
371 | " pushl %edi\n" | |
372 | " pushl %esi\n" | |
373 | " pushl %edx\n" | |
374 | " pushl %ecx\n" | |
375 | " pushl %ebx\n" | |
376 | " movl %esp, %eax\n" | |
377 | " call trampoline_handler\n" | |
378 | /* move eflags to cs */ | |
379 | " movl 48(%esp), %edx\n" | |
380 | " movl %edx, 44(%esp)\n" | |
381 | /* save true return address on eflags */ | |
382 | " movl %eax, 48(%esp)\n" | |
383 | " popl %ebx\n" | |
384 | " popl %ecx\n" | |
385 | " popl %edx\n" | |
386 | " popl %esi\n" | |
387 | " popl %edi\n" | |
388 | " popl %ebp\n" | |
389 | " popl %eax\n" | |
390 | /* skip eip, orig_eax, es, ds */ | |
391 | " addl $16, %esp\n" | |
392 | " popf\n" | |
393 | " ret\n"); | |
394 | } | |
b94cce92 HN |
395 | |
396 | /* | |
c9becf58 | 397 | * Called from kretprobe_trampoline |
b94cce92 | 398 | */ |
c9becf58 | 399 | fastcall void *__kprobes trampoline_handler(struct pt_regs *regs) |
b94cce92 | 400 | { |
4bdbd37f RL |
401 | struct kretprobe_instance *ri = NULL; |
402 | struct hlist_head *head; | |
403 | struct hlist_node *node, *tmp; | |
991a51d8 | 404 | unsigned long flags, orig_ret_address = 0; |
4bdbd37f | 405 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
b94cce92 | 406 | |
991a51d8 | 407 | spin_lock_irqsave(&kretprobe_lock, flags); |
4bdbd37f | 408 | head = kretprobe_inst_table_head(current); |
b94cce92 | 409 | |
4bdbd37f RL |
410 | /* |
411 | * It is possible to have multiple instances associated with a given | |
412 | * task either because an multiple functions in the call path | |
413 | * have a return probe installed on them, and/or more then one return | |
414 | * return probe was registered for a target function. | |
415 | * | |
416 | * We can handle this because: | |
417 | * - instances are always inserted at the head of the list | |
418 | * - when multiple return probes are registered for the same | |
419 | * function, the first instance's ret_addr will point to the | |
420 | * real return address, and all the rest will point to | |
421 | * kretprobe_trampoline | |
422 | */ | |
423 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
424 | if (ri->task != current) | |
425 | /* another task is sharing our hash bucket */ | |
426 | continue; | |
427 | ||
c9becf58 MH |
428 | if (ri->rp && ri->rp->handler){ |
429 | __get_cpu_var(current_kprobe) = &ri->rp->kp; | |
4bdbd37f | 430 | ri->rp->handler(ri, regs); |
c9becf58 MH |
431 | __get_cpu_var(current_kprobe) = NULL; |
432 | } | |
4bdbd37f RL |
433 | |
434 | orig_ret_address = (unsigned long)ri->ret_addr; | |
b94cce92 | 435 | recycle_rp_inst(ri); |
4bdbd37f RL |
436 | |
437 | if (orig_ret_address != trampoline_address) | |
438 | /* | |
439 | * This is the real return address. Any other | |
440 | * instances associated with this task are for | |
441 | * other calls deeper on the call stack | |
442 | */ | |
443 | break; | |
b94cce92 | 444 | } |
4bdbd37f RL |
445 | |
446 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); | |
4bdbd37f | 447 | |
991a51d8 | 448 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
4bdbd37f | 449 | |
c9becf58 | 450 | return (void*)orig_ret_address; |
b94cce92 HN |
451 | } |
452 | ||
1da177e4 LT |
453 | /* |
454 | * Called after single-stepping. p->addr is the address of the | |
455 | * instruction whose first byte has been replaced by the "int 3" | |
456 | * instruction. To avoid the SMP problems that can occur when we | |
457 | * temporarily put back the original opcode to single-step, we | |
458 | * single-stepped a copy of the instruction. The address of this | |
459 | * copy is p->ainsn.insn. | |
460 | * | |
461 | * This function prepares to return from the post-single-step | |
462 | * interrupt. We have to fix up the stack as follows: | |
463 | * | |
464 | * 0) Except in the case of absolute or indirect jump or call instructions, | |
465 | * the new eip is relative to the copied instruction. We need to make | |
466 | * it relative to the original instruction. | |
467 | * | |
468 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | |
469 | * flags are set in the just-pushed eflags, and may need to be cleared. | |
470 | * | |
471 | * 2) If the single-stepped instruction was a call, the return address | |
472 | * that is atop the stack is the address following the copied instruction. | |
473 | * We need to make it the address following the original instruction. | |
311ac88f MH |
474 | * |
475 | * This function also checks instruction size for preparing direct execution. | |
1da177e4 | 476 | */ |
9a0e3a86 AM |
477 | static void __kprobes resume_execution(struct kprobe *p, |
478 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) | |
1da177e4 LT |
479 | { |
480 | unsigned long *tos = (unsigned long *)®s->esp; | |
124d90be | 481 | unsigned long copy_eip = (unsigned long)p->ainsn.insn; |
1da177e4 LT |
482 | unsigned long orig_eip = (unsigned long)p->addr; |
483 | ||
b50ea74c | 484 | regs->eflags &= ~TF_MASK; |
1da177e4 LT |
485 | switch (p->ainsn.insn[0]) { |
486 | case 0x9c: /* pushfl */ | |
487 | *tos &= ~(TF_MASK | IF_MASK); | |
9a0e3a86 | 488 | *tos |= kcb->kprobe_old_eflags; |
1da177e4 | 489 | break; |
bcff5cd6 MH |
490 | case 0xc2: /* iret/ret/lret */ |
491 | case 0xc3: | |
0b9e2cac | 492 | case 0xca: |
bcff5cd6 MH |
493 | case 0xcb: |
494 | case 0xcf: | |
b50ea74c MH |
495 | case 0xea: /* jmp absolute -- eip is correct */ |
496 | /* eip is already adjusted, no more changes required */ | |
311ac88f | 497 | p->ainsn.boostable = 1; |
b50ea74c | 498 | goto no_change; |
1da177e4 LT |
499 | case 0xe8: /* call relative - Fix return addr */ |
500 | *tos = orig_eip + (*tos - copy_eip); | |
501 | break; | |
bcff5cd6 MH |
502 | case 0x9a: /* call absolute -- same as call absolute, indirect */ |
503 | *tos = orig_eip + (*tos - copy_eip); | |
504 | goto no_change; | |
1da177e4 LT |
505 | case 0xff: |
506 | if ((p->ainsn.insn[1] & 0x30) == 0x10) { | |
311ac88f | 507 | /* |
bcff5cd6 | 508 | * call absolute, indirect |
311ac88f MH |
509 | * Fix return addr; eip is correct. |
510 | * But this is not boostable | |
511 | */ | |
1da177e4 | 512 | *tos = orig_eip + (*tos - copy_eip); |
b50ea74c | 513 | goto no_change; |
1da177e4 LT |
514 | } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */ |
515 | ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */ | |
311ac88f MH |
516 | /* eip is correct. And this is boostable */ |
517 | p->ainsn.boostable = 1; | |
b50ea74c | 518 | goto no_change; |
1da177e4 | 519 | } |
1da177e4 LT |
520 | default: |
521 | break; | |
522 | } | |
523 | ||
311ac88f MH |
524 | if (p->ainsn.boostable == 0) { |
525 | if ((regs->eip > copy_eip) && | |
526 | (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) { | |
527 | /* | |
528 | * These instructions can be executed directly if it | |
529 | * jumps back to correct address. | |
530 | */ | |
531 | set_jmp_op((void *)regs->eip, | |
532 | (void *)orig_eip + (regs->eip - copy_eip)); | |
533 | p->ainsn.boostable = 1; | |
534 | } else { | |
535 | p->ainsn.boostable = -1; | |
536 | } | |
537 | } | |
538 | ||
b50ea74c MH |
539 | regs->eip = orig_eip + (regs->eip - copy_eip); |
540 | ||
541 | no_change: | |
542 | return; | |
1da177e4 LT |
543 | } |
544 | ||
545 | /* | |
546 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | |
991a51d8 | 547 | * remain disabled thoroughout this function. |
1da177e4 | 548 | */ |
34c37e18 | 549 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) |
1da177e4 | 550 | { |
9a0e3a86 AM |
551 | struct kprobe *cur = kprobe_running(); |
552 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
553 | ||
554 | if (!cur) | |
1da177e4 LT |
555 | return 0; |
556 | ||
9a0e3a86 AM |
557 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
558 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
559 | cur->post_handler(cur, regs, 0); | |
417c8da6 | 560 | } |
1da177e4 | 561 | |
9a0e3a86 AM |
562 | resume_execution(cur, regs, kcb); |
563 | regs->eflags |= kcb->kprobe_saved_eflags; | |
1da177e4 | 564 | |
417c8da6 | 565 | /*Restore back the original saved kprobes variables and continue. */ |
9a0e3a86 AM |
566 | if (kcb->kprobe_status == KPROBE_REENTER) { |
567 | restore_previous_kprobe(kcb); | |
417c8da6 PP |
568 | goto out; |
569 | } | |
9a0e3a86 | 570 | reset_current_kprobe(); |
417c8da6 | 571 | out: |
1da177e4 LT |
572 | preempt_enable_no_resched(); |
573 | ||
574 | /* | |
575 | * if somebody else is singlestepping across a probe point, eflags | |
576 | * will have TF set, in which case, continue the remaining processing | |
577 | * of do_debug, as if this is not a probe hit. | |
578 | */ | |
579 | if (regs->eflags & TF_MASK) | |
580 | return 0; | |
581 | ||
582 | return 1; | |
583 | } | |
584 | ||
34c37e18 | 585 | static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
1da177e4 | 586 | { |
9a0e3a86 AM |
587 | struct kprobe *cur = kprobe_running(); |
588 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
589 | ||
b4026513 PP |
590 | switch(kcb->kprobe_status) { |
591 | case KPROBE_HIT_SS: | |
592 | case KPROBE_REENTER: | |
593 | /* | |
594 | * We are here because the instruction being single | |
595 | * stepped caused a page fault. We reset the current | |
596 | * kprobe and the eip points back to the probe address | |
597 | * and allow the page fault handler to continue as a | |
598 | * normal page fault. | |
599 | */ | |
600 | regs->eip = (unsigned long)cur->addr; | |
9a0e3a86 | 601 | regs->eflags |= kcb->kprobe_old_eflags; |
b4026513 PP |
602 | if (kcb->kprobe_status == KPROBE_REENTER) |
603 | restore_previous_kprobe(kcb); | |
604 | else | |
605 | reset_current_kprobe(); | |
1da177e4 | 606 | preempt_enable_no_resched(); |
b4026513 PP |
607 | break; |
608 | case KPROBE_HIT_ACTIVE: | |
609 | case KPROBE_HIT_SSDONE: | |
610 | /* | |
611 | * We increment the nmissed count for accounting, | |
612 | * we can also use npre/npostfault count for accouting | |
613 | * these specific fault cases. | |
614 | */ | |
615 | kprobes_inc_nmissed_count(cur); | |
616 | ||
617 | /* | |
618 | * We come here because instructions in the pre/post | |
619 | * handler caused the page_fault, this could happen | |
620 | * if handler tries to access user space by | |
621 | * copy_from_user(), get_user() etc. Let the | |
622 | * user-specified handler try to fix it first. | |
623 | */ | |
624 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
625 | return 1; | |
626 | ||
627 | /* | |
628 | * In case the user-specified fault handler returned | |
629 | * zero, try to fix up. | |
630 | */ | |
631 | if (fixup_exception(regs)) | |
632 | return 1; | |
633 | ||
634 | /* | |
635 | * fixup_exception() could not handle it, | |
636 | * Let do_page_fault() fix it. | |
637 | */ | |
638 | break; | |
639 | default: | |
640 | break; | |
1da177e4 LT |
641 | } |
642 | return 0; | |
643 | } | |
644 | ||
645 | /* | |
646 | * Wrapper routine to for handling exceptions. | |
647 | */ | |
3d97ae5b PP |
648 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
649 | unsigned long val, void *data) | |
1da177e4 LT |
650 | { |
651 | struct die_args *args = (struct die_args *)data; | |
66ff2d06 AM |
652 | int ret = NOTIFY_DONE; |
653 | ||
64445416 | 654 | if (args->regs && user_mode_vm(args->regs)) |
2326c770 | 655 | return ret; |
656 | ||
1da177e4 LT |
657 | switch (val) { |
658 | case DIE_INT3: | |
659 | if (kprobe_handler(args->regs)) | |
66ff2d06 | 660 | ret = NOTIFY_STOP; |
1da177e4 LT |
661 | break; |
662 | case DIE_DEBUG: | |
663 | if (post_kprobe_handler(args->regs)) | |
66ff2d06 | 664 | ret = NOTIFY_STOP; |
1da177e4 LT |
665 | break; |
666 | case DIE_GPF: | |
1da177e4 | 667 | case DIE_PAGE_FAULT: |
d217d545 AM |
668 | /* kprobe_running() needs smp_processor_id() */ |
669 | preempt_disable(); | |
1da177e4 LT |
670 | if (kprobe_running() && |
671 | kprobe_fault_handler(args->regs, args->trapnr)) | |
66ff2d06 | 672 | ret = NOTIFY_STOP; |
d217d545 | 673 | preempt_enable(); |
1da177e4 LT |
674 | break; |
675 | default: | |
676 | break; | |
677 | } | |
66ff2d06 | 678 | return ret; |
1da177e4 LT |
679 | } |
680 | ||
3d97ae5b | 681 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 LT |
682 | { |
683 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
684 | unsigned long addr; | |
9a0e3a86 | 685 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 686 | |
9a0e3a86 AM |
687 | kcb->jprobe_saved_regs = *regs; |
688 | kcb->jprobe_saved_esp = ®s->esp; | |
689 | addr = (unsigned long)(kcb->jprobe_saved_esp); | |
1da177e4 LT |
690 | |
691 | /* | |
692 | * TBD: As Linus pointed out, gcc assumes that the callee | |
693 | * owns the argument space and could overwrite it, e.g. | |
694 | * tailcall optimization. So, to be absolutely safe | |
695 | * we also save and restore enough stack bytes to cover | |
696 | * the argument area. | |
697 | */ | |
9a0e3a86 AM |
698 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
699 | MIN_STACK_SIZE(addr)); | |
1da177e4 LT |
700 | regs->eflags &= ~IF_MASK; |
701 | regs->eip = (unsigned long)(jp->entry); | |
702 | return 1; | |
703 | } | |
704 | ||
3d97ae5b | 705 | void __kprobes jprobe_return(void) |
1da177e4 | 706 | { |
9a0e3a86 AM |
707 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
708 | ||
1da177e4 LT |
709 | asm volatile (" xchgl %%ebx,%%esp \n" |
710 | " int3 \n" | |
711 | " .globl jprobe_return_end \n" | |
712 | " jprobe_return_end: \n" | |
713 | " nop \n"::"b" | |
9a0e3a86 | 714 | (kcb->jprobe_saved_esp):"memory"); |
1da177e4 LT |
715 | } |
716 | ||
3d97ae5b | 717 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
1da177e4 | 718 | { |
9a0e3a86 | 719 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
1da177e4 | 720 | u8 *addr = (u8 *) (regs->eip - 1); |
9a0e3a86 | 721 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp); |
1da177e4 LT |
722 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
723 | ||
724 | if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) { | |
9a0e3a86 | 725 | if (®s->esp != kcb->jprobe_saved_esp) { |
1da177e4 | 726 | struct pt_regs *saved_regs = |
9a0e3a86 AM |
727 | container_of(kcb->jprobe_saved_esp, |
728 | struct pt_regs, esp); | |
1da177e4 | 729 | printk("current esp %p does not match saved esp %p\n", |
9a0e3a86 | 730 | ®s->esp, kcb->jprobe_saved_esp); |
1da177e4 LT |
731 | printk("Saved registers for jprobe %p\n", jp); |
732 | show_registers(saved_regs); | |
733 | printk("Current registers\n"); | |
734 | show_registers(regs); | |
735 | BUG(); | |
736 | } | |
9a0e3a86 AM |
737 | *regs = kcb->jprobe_saved_regs; |
738 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, | |
1da177e4 | 739 | MIN_STACK_SIZE(stack_addr)); |
d217d545 | 740 | preempt_enable_no_resched(); |
1da177e4 LT |
741 | return 1; |
742 | } | |
743 | return 0; | |
744 | } | |
4bdbd37f | 745 | |
6772926b | 746 | int __init arch_init_kprobes(void) |
4bdbd37f | 747 | { |
c9becf58 | 748 | return 0; |
4bdbd37f | 749 | } |