blob: 70cf73bdba25b0871aec47ff6ca4f835f5e6d7b3 [file] [log] [blame]
Michael Grundy4ba069b2006-09-20 15:58:39 +02001/*
2 * Kernel Probes (KProbes)
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, 2006
19 *
20 * s390 port, used ppc64 as template. Mike Grundy <grundym@us.ibm.com>
21 */
22
Michael Grundy4ba069b2006-09-20 15:58:39 +020023#include <linux/kprobes.h>
24#include <linux/ptrace.h>
25#include <linux/preempt.h>
26#include <linux/stop_machine.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070027#include <linux/kdebug.h>
Heiko Carstensa2b53672009-06-12 10:26:43 +020028#include <linux/uaccess.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020029#include <asm/cacheflush.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020030#include <asm/sections.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020031#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Martin Schwidefskyadb45832010-11-10 10:05:57 +010033#include <linux/hardirq.h>
Michael Grundy4ba069b2006-09-20 15:58:39 +020034
35DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
36DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
37
Masami Hiramatsuf438d912007-10-16 01:27:49 -070038struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
39
Michael Grundy4ba069b2006-09-20 15:58:39 +020040int __kprobes arch_prepare_kprobe(struct kprobe *p)
41{
42 /* Make sure the probe isn't going on a difficult instruction */
43 if (is_prohibited_opcode((kprobe_opcode_t *) p->addr))
44 return -EINVAL;
45
Martin Schwidefsky5532bd02008-07-14 09:59:41 +020046 if ((unsigned long)p->addr & 0x01)
Michael Grundy4ba069b2006-09-20 15:58:39 +020047 return -EINVAL;
Michael Grundy4ba069b2006-09-20 15:58:39 +020048
49 /* Use the get_insn_slot() facility for correctness */
50 if (!(p->ainsn.insn = get_insn_slot()))
51 return -ENOMEM;
52
53 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
54
55 get_instruction_type(&p->ainsn);
56 p->opcode = *p->addr;
57 return 0;
58}
59
60int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
61{
62 switch (*(__u8 *) instruction) {
63 case 0x0c: /* bassm */
64 case 0x0b: /* bsm */
65 case 0x83: /* diag */
66 case 0x44: /* ex */
Heiko Carstensbac9f152010-05-26 23:26:20 +020067 case 0xac: /* stnsm */
68 case 0xad: /* stosm */
Michael Grundy4ba069b2006-09-20 15:58:39 +020069 return -EINVAL;
70 }
71 switch (*(__u16 *) instruction) {
72 case 0x0101: /* pr */
73 case 0xb25a: /* bsa */
74 case 0xb240: /* bakr */
75 case 0xb258: /* bsg */
76 case 0xb218: /* pc */
77 case 0xb228: /* pt */
Heiko Carstensbac9f152010-05-26 23:26:20 +020078 case 0xb98d: /* epsw */
Michael Grundy4ba069b2006-09-20 15:58:39 +020079 return -EINVAL;
80 }
81 return 0;
82}
83
84void __kprobes get_instruction_type(struct arch_specific_insn *ainsn)
85{
86 /* default fixup method */
87 ainsn->fixup = FIXUP_PSW_NORMAL;
88
89 /* save r1 operand */
90 ainsn->reg = (*ainsn->insn & 0xf0) >> 4;
91
92 /* save the instruction length (pop 5-5) in bytes */
David Wilder9c5f2252007-08-22 13:51:44 +020093 switch (*(__u8 *) (ainsn->insn) >> 6) {
Michael Grundy4ba069b2006-09-20 15:58:39 +020094 case 0:
95 ainsn->ilen = 2;
96 break;
97 case 1:
98 case 2:
99 ainsn->ilen = 4;
100 break;
101 case 3:
102 ainsn->ilen = 6;
103 break;
104 }
105
106 switch (*(__u8 *) ainsn->insn) {
107 case 0x05: /* balr */
108 case 0x0d: /* basr */
109 ainsn->fixup = FIXUP_RETURN_REGISTER;
110 /* if r2 = 0, no branch will be taken */
111 if ((*ainsn->insn & 0x0f) == 0)
112 ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN;
113 break;
114 case 0x06: /* bctr */
115 case 0x07: /* bcr */
116 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
117 break;
118 case 0x45: /* bal */
119 case 0x4d: /* bas */
120 ainsn->fixup = FIXUP_RETURN_REGISTER;
121 break;
122 case 0x47: /* bc */
123 case 0x46: /* bct */
124 case 0x86: /* bxh */
125 case 0x87: /* bxle */
126 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
127 break;
128 case 0x82: /* lpsw */
129 ainsn->fixup = FIXUP_NOT_REQUIRED;
130 break;
131 case 0xb2: /* lpswe */
132 if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) {
133 ainsn->fixup = FIXUP_NOT_REQUIRED;
134 }
135 break;
136 case 0xa7: /* bras */
137 if ((*ainsn->insn & 0x0f) == 0x05) {
138 ainsn->fixup |= FIXUP_RETURN_REGISTER;
139 }
140 break;
141 case 0xc0:
142 if ((*ainsn->insn & 0x0f) == 0x00 /* larl */
143 || (*ainsn->insn & 0x0f) == 0x05) /* brasl */
144 ainsn->fixup |= FIXUP_RETURN_REGISTER;
145 break;
146 case 0xeb:
147 if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 || /* bxhg */
148 *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */
149 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
150 }
151 break;
152 case 0xe3: /* bctg */
153 if (*(((__u8 *) ainsn->insn) + 5) == 0x46) {
154 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
155 }
156 break;
157 }
158}
159
160static int __kprobes swap_instruction(void *aref)
161{
Heiko Carstensacf01802009-06-22 12:08:23 +0200162 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
163 unsigned long status = kcb->kprobe_status;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200164 struct ins_replace_args *args = aref;
Heiko Carstensacf01802009-06-22 12:08:23 +0200165 int rc;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200166
Heiko Carstensacf01802009-06-22 12:08:23 +0200167 kcb->kprobe_status = KPROBE_SWAP_INST;
168 rc = probe_kernel_write(args->ptr, &args->new, sizeof(args->new));
169 kcb->kprobe_status = status;
170 return rc;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200171}
172
173void __kprobes arch_arm_kprobe(struct kprobe *p)
174{
Michael Grundy4ba069b2006-09-20 15:58:39 +0200175 struct ins_replace_args args;
176
177 args.ptr = p->addr;
178 args.old = p->opcode;
179 args.new = BREAKPOINT_INSTRUCTION;
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500180 stop_machine(swap_instruction, &args, NULL);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200181}
182
183void __kprobes arch_disarm_kprobe(struct kprobe *p)
184{
Michael Grundy4ba069b2006-09-20 15:58:39 +0200185 struct ins_replace_args args;
186
187 args.ptr = p->addr;
188 args.old = BREAKPOINT_INSTRUCTION;
189 args.new = p->opcode;
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500190 stop_machine(swap_instruction, &args, NULL);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200191}
192
193void __kprobes arch_remove_kprobe(struct kprobe *p)
194{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800195 if (p->ainsn.insn) {
196 free_insn_slot(p->ainsn.insn, 0);
197 p->ainsn.insn = NULL;
198 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200199}
200
201static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
202{
203 per_cr_bits kprobe_per_regs[1];
204
205 memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
206 regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE;
207
208 /* Set up the per control reg info, will pass to lctl */
209 kprobe_per_regs[0].em_instruction_fetch = 1;
210 kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn;
211 kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1;
212
213 /* Set the PER control regs, turns on single step for this address */
214 __ctl_load(kprobe_per_regs, 9, 11);
215 regs->psw.mask |= PSW_MASK_PER;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100216 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200217}
218
219static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
220{
221 kcb->prev_kprobe.kp = kprobe_running();
222 kcb->prev_kprobe.status = kcb->kprobe_status;
223 kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask;
224 memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl,
225 sizeof(kcb->kprobe_saved_ctl));
226}
227
228static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
229{
230 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
231 kcb->kprobe_status = kcb->prev_kprobe.status;
232 kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask;
233 memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl,
234 sizeof(kcb->kprobe_saved_ctl));
235}
236
237static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
238 struct kprobe_ctlblk *kcb)
239{
240 __get_cpu_var(current_kprobe) = p;
241 /* Save the interrupt and per flags */
242 kcb->kprobe_saved_imask = regs->psw.mask &
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100243 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200244 /* Save the control regs that govern PER */
245 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
246}
247
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700248void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Michael Grundy4ba069b2006-09-20 15:58:39 +0200249 struct pt_regs *regs)
250{
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700251 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
Michael Grundy4ba069b2006-09-20 15:58:39 +0200252
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700253 /* Replace the return addr with trampoline addr */
254 regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200255}
256
257static int __kprobes kprobe_handler(struct pt_regs *regs)
258{
259 struct kprobe *p;
260 int ret = 0;
261 unsigned long *addr = (unsigned long *)
262 ((regs->psw.addr & PSW_ADDR_INSN) - 2);
263 struct kprobe_ctlblk *kcb;
264
265 /*
266 * We don't want to be preempted for the entire
267 * duration of kprobe processing
268 */
269 preempt_disable();
270 kcb = get_kprobe_ctlblk();
271
272 /* Check we're not actually recursing */
273 if (kprobe_running()) {
274 p = get_kprobe(addr);
275 if (p) {
276 if (kcb->kprobe_status == KPROBE_HIT_SS &&
277 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
278 regs->psw.mask &= ~PSW_MASK_PER;
279 regs->psw.mask |= kcb->kprobe_saved_imask;
280 goto no_kprobe;
281 }
282 /* We have reentered the kprobe_handler(), since
283 * another probe was hit while within the handler.
284 * We here save the original kprobes variables and
285 * just single step on the instruction of the new probe
286 * without calling any user handlers.
287 */
288 save_previous_kprobe(kcb);
289 set_current_kprobe(p, regs, kcb);
290 kprobes_inc_nmissed_count(p);
291 prepare_singlestep(p, regs);
292 kcb->kprobe_status = KPROBE_REENTER;
293 return 1;
294 } else {
295 p = __get_cpu_var(current_kprobe);
296 if (p->break_handler && p->break_handler(p, regs)) {
297 goto ss_probe;
298 }
299 }
300 goto no_kprobe;
301 }
302
303 p = get_kprobe(addr);
Martin Schwidefskyf794c822007-03-05 23:35:38 +0100304 if (!p)
305 /*
306 * No kprobe at this address. The fault has not been
307 * caused by a kprobe breakpoint. The race of breakpoint
308 * vs. kprobe remove does not exist because on s390 we
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500309 * use stop_machine to arm/disarm the breakpoints.
Martin Schwidefskyf794c822007-03-05 23:35:38 +0100310 */
Michael Grundy4ba069b2006-09-20 15:58:39 +0200311 goto no_kprobe;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200312
313 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
314 set_current_kprobe(p, regs, kcb);
315 if (p->pre_handler && p->pre_handler(p, regs))
316 /* handler has already set things up, so skip ss setup */
317 return 1;
318
319ss_probe:
320 prepare_singlestep(p, regs);
321 kcb->kprobe_status = KPROBE_HIT_SS;
322 return 1;
323
324no_kprobe:
325 preempt_enable_no_resched();
326 return ret;
327}
328
329/*
330 * Function return probe trampoline:
331 * - init_kprobes() establishes a probepoint here
332 * - When the probed function returns, this probe
333 * causes the handlers to fire
334 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200335static void __used kretprobe_trampoline_holder(void)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200336{
337 asm volatile(".global kretprobe_trampoline\n"
338 "kretprobe_trampoline: bcr 0,0\n");
339}
340
341/*
342 * Called when the probe at kretprobe trampoline is hit
343 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100344static int __kprobes trampoline_probe_handler(struct kprobe *p,
345 struct pt_regs *regs)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200346{
347 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700348 struct hlist_head *head, empty_rp;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200349 struct hlist_node *node, *tmp;
350 unsigned long flags, orig_ret_address = 0;
351 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
352
bibo,mao99219a32006-10-02 02:17:35 -0700353 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700354 kretprobe_hash_lock(current, &head, &flags);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200355
356 /*
357 * It is possible to have multiple instances associated with a given
358 * task either because an multiple functions in the call path
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200359 * have a return probe installed on them, and/or more than one return
Michael Grundy4ba069b2006-09-20 15:58:39 +0200360 * return probe was registered for a target function.
361 *
362 * We can handle this because:
363 * - instances are always inserted at the head of the list
364 * - when multiple return probes are registered for the same
365 * function, the first instance's ret_addr will point to the
366 * real return address, and all the rest will point to
367 * kretprobe_trampoline
368 */
369 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
370 if (ri->task != current)
371 /* another task is sharing our hash bucket */
372 continue;
373
374 if (ri->rp && ri->rp->handler)
375 ri->rp->handler(ri, regs);
376
377 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700378 recycle_rp_inst(ri, &empty_rp);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200379
380 if (orig_ret_address != trampoline_address) {
381 /*
382 * This is the real return address. Any other
383 * instances associated with this task are for
384 * other calls deeper on the call stack
385 */
386 break;
387 }
388 }
Heiko Carstensa5a60a22007-05-21 11:25:22 +0200389 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200390 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
391
392 reset_current_kprobe();
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700393 kretprobe_hash_unlock(current, &flags);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200394 preempt_enable_no_resched();
395
bibo,mao99219a32006-10-02 02:17:35 -0700396 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
397 hlist_del(&ri->hlist);
398 kfree(ri);
399 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200400 /*
401 * By returning a non-zero value, we are telling
402 * kprobe_handler() that we don't want the post_handler
403 * to run (and have re-enabled preemption)
404 */
405 return 1;
406}
407
408/*
409 * Called after single-stepping. p->addr is the address of the
410 * instruction whose first byte has been replaced by the "breakpoint"
411 * instruction. To avoid the SMP problems that can occur when we
412 * temporarily put back the original opcode to single-step, we
413 * single-stepped a copy of the instruction. The address of this
414 * copy is p->ainsn.insn.
415 */
416static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
417{
418 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
419
420 regs->psw.addr &= PSW_ADDR_INSN;
421
422 if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
423 regs->psw.addr = (unsigned long)p->addr +
424 ((unsigned long)regs->psw.addr -
425 (unsigned long)p->ainsn.insn);
426
427 if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
428 if ((unsigned long)regs->psw.addr -
429 (unsigned long)p->ainsn.insn == p->ainsn.ilen)
430 regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen;
431
432 if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
433 regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr +
434 (regs->gprs[p->ainsn.reg] -
435 (unsigned long)p->ainsn.insn))
436 | PSW_ADDR_AMODE;
437
438 regs->psw.addr |= PSW_ADDR_AMODE;
439 /* turn off PER mode */
440 regs->psw.mask &= ~PSW_MASK_PER;
441 /* Restore the original per control regs */
442 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
443 regs->psw.mask |= kcb->kprobe_saved_imask;
444}
445
446static int __kprobes post_kprobe_handler(struct pt_regs *regs)
447{
448 struct kprobe *cur = kprobe_running();
449 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
450
451 if (!cur)
452 return 0;
453
454 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
455 kcb->kprobe_status = KPROBE_HIT_SSDONE;
456 cur->post_handler(cur, regs, 0);
457 }
458
459 resume_execution(cur, regs);
460
461 /*Restore back the original saved kprobes variables and continue. */
462 if (kcb->kprobe_status == KPROBE_REENTER) {
463 restore_previous_kprobe(kcb);
464 goto out;
465 }
466 reset_current_kprobe();
467out:
468 preempt_enable_no_resched();
469
470 /*
471 * if somebody else is singlestepping across a probe point, psw mask
472 * will have PER set, in which case, continue the remaining processing
473 * of do_single_step, as if this is not a probe hit.
474 */
475 if (regs->psw.mask & PSW_MASK_PER) {
476 return 0;
477 }
478
479 return 1;
480}
481
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100482static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200483{
484 struct kprobe *cur = kprobe_running();
485 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
486 const struct exception_table_entry *entry;
487
488 switch(kcb->kprobe_status) {
489 case KPROBE_SWAP_INST:
490 /* We are here because the instruction replacement failed */
491 return 0;
492 case KPROBE_HIT_SS:
493 case KPROBE_REENTER:
494 /*
495 * We are here because the instruction being single
496 * stepped caused a page fault. We reset the current
497 * kprobe and the nip points back to the probe address
498 * and allow the page fault handler to continue as a
499 * normal page fault.
500 */
501 regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE;
502 regs->psw.mask &= ~PSW_MASK_PER;
503 regs->psw.mask |= kcb->kprobe_saved_imask;
504 if (kcb->kprobe_status == KPROBE_REENTER)
505 restore_previous_kprobe(kcb);
Martin Schwidefsky9ec27082010-10-29 16:50:45 +0200506 else {
Michael Grundy4ba069b2006-09-20 15:58:39 +0200507 reset_current_kprobe();
Martin Schwidefsky9ec27082010-10-29 16:50:45 +0200508 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200509 preempt_enable_no_resched();
510 break;
511 case KPROBE_HIT_ACTIVE:
512 case KPROBE_HIT_SSDONE:
513 /*
514 * We increment the nmissed count for accounting,
515 * we can also use npre/npostfault count for accouting
516 * these specific fault cases.
517 */
518 kprobes_inc_nmissed_count(cur);
519
520 /*
521 * We come here because instructions in the pre/post
522 * handler caused the page_fault, this could happen
523 * if handler tries to access user space by
524 * copy_from_user(), get_user() etc. Let the
525 * user-specified handler try to fix it first.
526 */
527 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
528 return 1;
529
530 /*
531 * In case the user-specified fault handler returned
532 * zero, try to fix up.
533 */
534 entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
535 if (entry) {
536 regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
537 return 1;
538 }
539
540 /*
541 * fixup_exception() could not handle it,
542 * Let do_page_fault() fix it.
543 */
544 break;
545 default:
546 break;
547 }
548 return 0;
549}
550
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100551int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
552{
553 int ret;
554
555 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
556 local_irq_disable();
557 ret = kprobe_trap_handler(regs, trapnr);
558 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
559 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
560 return ret;
561}
562
Michael Grundy4ba069b2006-09-20 15:58:39 +0200563/*
564 * Wrapper routine to for handling exceptions.
565 */
566int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
567 unsigned long val, void *data)
568{
569 struct die_args *args = (struct die_args *)data;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100570 struct pt_regs *regs = args->regs;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200571 int ret = NOTIFY_DONE;
572
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100573 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
574 local_irq_disable();
575
Michael Grundy4ba069b2006-09-20 15:58:39 +0200576 switch (val) {
577 case DIE_BPT:
578 if (kprobe_handler(args->regs))
579 ret = NOTIFY_STOP;
580 break;
581 case DIE_SSTEP:
582 if (post_kprobe_handler(args->regs))
583 ret = NOTIFY_STOP;
584 break;
585 case DIE_TRAP:
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100586 if (!preemptible() && kprobe_running() &&
587 kprobe_trap_handler(args->regs, args->trapnr))
Michael Grundy4ba069b2006-09-20 15:58:39 +0200588 ret = NOTIFY_STOP;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200589 break;
590 default:
591 break;
592 }
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100593
594 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
595 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
596
Michael Grundy4ba069b2006-09-20 15:58:39 +0200597 return ret;
598}
599
600int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
601{
602 struct jprobe *jp = container_of(p, struct jprobe, kp);
603 unsigned long addr;
604 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
605
606 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
607
608 /* setup return addr to the jprobe handler routine */
609 regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100610 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200611
612 /* r14 is the function return address */
613 kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
614 /* r15 is the stack pointer */
615 kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
616 addr = (unsigned long)kcb->jprobe_saved_r15;
617
618 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
619 MIN_STACK_SIZE(addr));
620 return 1;
621}
622
623void __kprobes jprobe_return(void)
624{
625 asm volatile(".word 0x0002");
626}
627
628void __kprobes jprobe_return_end(void)
629{
630 asm volatile("bcr 0,0");
631}
632
633int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
634{
635 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
636 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
637
638 /* Put the regs back */
639 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
640 /* put the stack back */
641 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
642 MIN_STACK_SIZE(stack_addr));
643 preempt_enable_no_resched();
644 return 1;
645}
646
647static struct kprobe trampoline_p = {
648 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
649 .pre_handler = trampoline_probe_handler
650};
651
652int __init arch_init_kprobes(void)
653{
654 return register_kprobe(&trampoline_p);
655}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700656
657int __kprobes arch_trampoline_kprobe(struct kprobe *p)
658{
659 if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline)
660 return 1;
661 return 0;
662}