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