blob: 1e75ec5235779dad205c7f08e4118e9ee6c4d764 [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
Martin Schwidefsky5a8b5892011-01-05 12:47:18 +0100160struct ins_replace_args {
161 kprobe_opcode_t *ptr;
162 kprobe_opcode_t opcode;
163};
164
Michael Grundy4ba069b2006-09-20 15:58:39 +0200165static int __kprobes swap_instruction(void *aref)
166{
Heiko Carstensacf01802009-06-22 12:08:23 +0200167 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
168 unsigned long status = kcb->kprobe_status;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200169 struct ins_replace_args *args = aref;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200170
Heiko Carstensacf01802009-06-22 12:08:23 +0200171 kcb->kprobe_status = KPROBE_SWAP_INST;
Martin Schwidefsky5a8b5892011-01-05 12:47:18 +0100172 probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode));
Heiko Carstensacf01802009-06-22 12:08:23 +0200173 kcb->kprobe_status = status;
Martin Schwidefsky5a8b5892011-01-05 12:47:18 +0100174 return 0;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200175}
176
177void __kprobes arch_arm_kprobe(struct kprobe *p)
178{
Michael Grundy4ba069b2006-09-20 15:58:39 +0200179 struct ins_replace_args args;
180
181 args.ptr = p->addr;
Martin Schwidefsky5a8b5892011-01-05 12:47:18 +0100182 args.opcode = BREAKPOINT_INSTRUCTION;
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500183 stop_machine(swap_instruction, &args, NULL);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200184}
185
186void __kprobes arch_disarm_kprobe(struct kprobe *p)
187{
Michael Grundy4ba069b2006-09-20 15:58:39 +0200188 struct ins_replace_args args;
189
190 args.ptr = p->addr;
Martin Schwidefsky5a8b5892011-01-05 12:47:18 +0100191 args.opcode = p->opcode;
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500192 stop_machine(swap_instruction, &args, NULL);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200193}
194
195void __kprobes arch_remove_kprobe(struct kprobe *p)
196{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800197 if (p->ainsn.insn) {
198 free_insn_slot(p->ainsn.insn, 0);
199 p->ainsn.insn = NULL;
200 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200201}
202
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100203static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb,
204 struct pt_regs *regs,
205 unsigned long ip)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200206{
207 per_cr_bits kprobe_per_regs[1];
208
Michael Grundy4ba069b2006-09-20 15:58:39 +0200209 /* Set up the per control reg info, will pass to lctl */
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100210 memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
Michael Grundy4ba069b2006-09-20 15:58:39 +0200211 kprobe_per_regs[0].em_instruction_fetch = 1;
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100212 kprobe_per_regs[0].starting_addr = ip;
213 kprobe_per_regs[0].ending_addr = ip;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200214
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100215 /* Save control regs and psw mask */
216 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
217 kcb->kprobe_saved_imask = regs->psw.mask &
218 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
219
220 /* Set PER control regs, turns on single step for the given address */
Michael Grundy4ba069b2006-09-20 15:58:39 +0200221 __ctl_load(kprobe_per_regs, 9, 11);
222 regs->psw.mask |= PSW_MASK_PER;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100223 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100224 regs->psw.addr = ip | PSW_ADDR_AMODE;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200225}
226
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100227static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
228 struct pt_regs *regs,
229 unsigned long ip)
230{
231 /* Restore control regs and psw mask, set new psw address */
232 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
233 regs->psw.mask &= ~PSW_MASK_PER;
234 regs->psw.mask |= kcb->kprobe_saved_imask;
235 regs->psw.addr = ip | PSW_ADDR_AMODE;
236}
237
238
Michael Grundy4ba069b2006-09-20 15:58:39 +0200239static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
240{
241 kcb->prev_kprobe.kp = kprobe_running();
242 kcb->prev_kprobe.status = kcb->kprobe_status;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200243}
244
245static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
246{
247 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
248 kcb->kprobe_status = kcb->prev_kprobe.status;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200249}
250
251static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
252 struct kprobe_ctlblk *kcb)
253{
254 __get_cpu_var(current_kprobe) = p;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200255}
256
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700257void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Michael Grundy4ba069b2006-09-20 15:58:39 +0200258 struct pt_regs *regs)
259{
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700260 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
Michael Grundy4ba069b2006-09-20 15:58:39 +0200261
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700262 /* Replace the return addr with trampoline addr */
263 regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200264}
265
266static int __kprobes kprobe_handler(struct pt_regs *regs)
267{
268 struct kprobe *p;
269 int ret = 0;
270 unsigned long *addr = (unsigned long *)
271 ((regs->psw.addr & PSW_ADDR_INSN) - 2);
272 struct kprobe_ctlblk *kcb;
273
274 /*
275 * We don't want to be preempted for the entire
276 * duration of kprobe processing
277 */
278 preempt_disable();
279 kcb = get_kprobe_ctlblk();
280
281 /* Check we're not actually recursing */
282 if (kprobe_running()) {
283 p = get_kprobe(addr);
284 if (p) {
Michael Grundy4ba069b2006-09-20 15:58:39 +0200285 /* We have reentered the kprobe_handler(), since
286 * another probe was hit while within the handler.
287 * We here save the original kprobes variables and
288 * just single step on the instruction of the new probe
289 * without calling any user handlers.
290 */
291 save_previous_kprobe(kcb);
292 set_current_kprobe(p, regs, kcb);
293 kprobes_inc_nmissed_count(p);
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100294 enable_singlestep(kcb, regs,
295 (unsigned long) p->ainsn.insn);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200296 kcb->kprobe_status = KPROBE_REENTER;
297 return 1;
298 } else {
299 p = __get_cpu_var(current_kprobe);
300 if (p->break_handler && p->break_handler(p, regs)) {
301 goto ss_probe;
302 }
303 }
304 goto no_kprobe;
305 }
306
307 p = get_kprobe(addr);
Martin Schwidefskyf794c822007-03-05 23:35:38 +0100308 if (!p)
309 /*
310 * No kprobe at this address. The fault has not been
311 * caused by a kprobe breakpoint. The race of breakpoint
312 * vs. kprobe remove does not exist because on s390 we
Rusty Russell9b1a4d32008-07-28 12:16:30 -0500313 * use stop_machine to arm/disarm the breakpoints.
Martin Schwidefskyf794c822007-03-05 23:35:38 +0100314 */
Michael Grundy4ba069b2006-09-20 15:58:39 +0200315 goto no_kprobe;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200316
317 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
318 set_current_kprobe(p, regs, kcb);
319 if (p->pre_handler && p->pre_handler(p, regs))
320 /* handler has already set things up, so skip ss setup */
321 return 1;
322
323ss_probe:
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100324 enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200325 kcb->kprobe_status = KPROBE_HIT_SS;
326 return 1;
327
328no_kprobe:
329 preempt_enable_no_resched();
330 return ret;
331}
332
333/*
334 * Function return probe trampoline:
335 * - init_kprobes() establishes a probepoint here
336 * - When the probed function returns, this probe
337 * causes the handlers to fire
338 */
Heiko Carstensa8061702008-04-17 07:46:26 +0200339static void __used kretprobe_trampoline_holder(void)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200340{
341 asm volatile(".global kretprobe_trampoline\n"
342 "kretprobe_trampoline: bcr 0,0\n");
343}
344
345/*
346 * Called when the probe at kretprobe trampoline is hit
347 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100348static int __kprobes trampoline_probe_handler(struct kprobe *p,
349 struct pt_regs *regs)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200350{
351 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700352 struct hlist_head *head, empty_rp;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200353 struct hlist_node *node, *tmp;
354 unsigned long flags, orig_ret_address = 0;
355 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
Martin Schwidefsky89480802010-11-10 10:05:58 +0100356 kprobe_opcode_t *correct_ret_addr = NULL;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200357
bibo,mao99219a32006-10-02 02:17:35 -0700358 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700359 kretprobe_hash_lock(current, &head, &flags);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200360
361 /*
362 * It is possible to have multiple instances associated with a given
363 * task either because an multiple functions in the call path
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200364 * have a return probe installed on them, and/or more than one return
Michael Grundy4ba069b2006-09-20 15:58:39 +0200365 * return probe was registered for a target function.
366 *
367 * We can handle this because:
368 * - instances are always inserted at the head of the list
369 * - when multiple return probes are registered for the same
370 * function, the first instance's ret_addr will point to the
371 * real return address, and all the rest will point to
372 * kretprobe_trampoline
373 */
374 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
375 if (ri->task != current)
376 /* another task is sharing our hash bucket */
377 continue;
378
Martin Schwidefsky89480802010-11-10 10:05:58 +0100379 orig_ret_address = (unsigned long)ri->ret_addr;
380
381 if (orig_ret_address != trampoline_address)
382 /*
383 * This is the real return address. Any other
384 * instances associated with this task are for
385 * other calls deeper on the call stack
386 */
387 break;
388 }
389
390 kretprobe_assert(ri, orig_ret_address, trampoline_address);
391
392 correct_ret_addr = ri->ret_addr;
393 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
394 if (ri->task != current)
395 /* another task is sharing our hash bucket */
396 continue;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200397
398 orig_ret_address = (unsigned long)ri->ret_addr;
Martin Schwidefsky89480802010-11-10 10:05:58 +0100399
400 if (ri->rp && ri->rp->handler) {
401 ri->ret_addr = correct_ret_addr;
402 ri->rp->handler(ri, regs);
403 }
404
bibo,mao99219a32006-10-02 02:17:35 -0700405 recycle_rp_inst(ri, &empty_rp);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200406
407 if (orig_ret_address != trampoline_address) {
408 /*
409 * This is the real return address. Any other
410 * instances associated with this task are for
411 * other calls deeper on the call stack
412 */
413 break;
414 }
415 }
Martin Schwidefsky89480802010-11-10 10:05:58 +0100416
Michael Grundy4ba069b2006-09-20 15:58:39 +0200417 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
418
419 reset_current_kprobe();
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700420 kretprobe_hash_unlock(current, &flags);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200421 preempt_enable_no_resched();
422
bibo,mao99219a32006-10-02 02:17:35 -0700423 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
424 hlist_del(&ri->hlist);
425 kfree(ri);
426 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200427 /*
428 * By returning a non-zero value, we are telling
429 * kprobe_handler() that we don't want the post_handler
430 * to run (and have re-enabled preemption)
431 */
432 return 1;
433}
434
435/*
436 * Called after single-stepping. p->addr is the address of the
437 * instruction whose first byte has been replaced by the "breakpoint"
438 * instruction. To avoid the SMP problems that can occur when we
439 * temporarily put back the original opcode to single-step, we
440 * single-stepped a copy of the instruction. The address of this
441 * copy is p->ainsn.insn.
442 */
443static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
444{
445 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100446 unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200447
448 if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100449 ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200450
451 if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100452 if (ip - (unsigned long) p->ainsn.insn == p->ainsn.ilen)
453 ip = (unsigned long) p->addr + p->ainsn.ilen;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200454
455 if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100456 regs->gprs[p->ainsn.reg] += (unsigned long) p->addr -
457 (unsigned long) p->ainsn.insn;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200458
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100459 disable_singlestep(kcb, regs, ip);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200460}
461
462static int __kprobes post_kprobe_handler(struct pt_regs *regs)
463{
464 struct kprobe *cur = kprobe_running();
465 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
466
467 if (!cur)
468 return 0;
469
470 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
471 kcb->kprobe_status = KPROBE_HIT_SSDONE;
472 cur->post_handler(cur, regs, 0);
473 }
474
475 resume_execution(cur, regs);
476
477 /*Restore back the original saved kprobes variables and continue. */
478 if (kcb->kprobe_status == KPROBE_REENTER) {
479 restore_previous_kprobe(kcb);
480 goto out;
481 }
482 reset_current_kprobe();
483out:
484 preempt_enable_no_resched();
485
486 /*
487 * if somebody else is singlestepping across a probe point, psw mask
488 * will have PER set, in which case, continue the remaining processing
489 * of do_single_step, as if this is not a probe hit.
490 */
491 if (regs->psw.mask & PSW_MASK_PER) {
492 return 0;
493 }
494
495 return 1;
496}
497
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100498static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
Michael Grundy4ba069b2006-09-20 15:58:39 +0200499{
500 struct kprobe *cur = kprobe_running();
501 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
502 const struct exception_table_entry *entry;
503
504 switch(kcb->kprobe_status) {
505 case KPROBE_SWAP_INST:
506 /* We are here because the instruction replacement failed */
507 return 0;
508 case KPROBE_HIT_SS:
509 case KPROBE_REENTER:
510 /*
511 * We are here because the instruction being single
512 * stepped caused a page fault. We reset the current
513 * kprobe and the nip points back to the probe address
514 * and allow the page fault handler to continue as a
515 * normal page fault.
516 */
Martin Schwidefskyfc0a1fe2011-01-05 12:47:17 +0100517 disable_singlestep(kcb, regs, (unsigned long) cur->addr);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200518 if (kcb->kprobe_status == KPROBE_REENTER)
519 restore_previous_kprobe(kcb);
Martin Schwidefsky9ec27082010-10-29 16:50:45 +0200520 else {
Michael Grundy4ba069b2006-09-20 15:58:39 +0200521 reset_current_kprobe();
Martin Schwidefsky9ec27082010-10-29 16:50:45 +0200522 }
Michael Grundy4ba069b2006-09-20 15:58:39 +0200523 preempt_enable_no_resched();
524 break;
525 case KPROBE_HIT_ACTIVE:
526 case KPROBE_HIT_SSDONE:
527 /*
528 * We increment the nmissed count for accounting,
529 * we can also use npre/npostfault count for accouting
530 * these specific fault cases.
531 */
532 kprobes_inc_nmissed_count(cur);
533
534 /*
535 * We come here because instructions in the pre/post
536 * handler caused the page_fault, this could happen
537 * if handler tries to access user space by
538 * copy_from_user(), get_user() etc. Let the
539 * user-specified handler try to fix it first.
540 */
541 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
542 return 1;
543
544 /*
545 * In case the user-specified fault handler returned
546 * zero, try to fix up.
547 */
548 entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
549 if (entry) {
550 regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
551 return 1;
552 }
553
554 /*
555 * fixup_exception() could not handle it,
556 * Let do_page_fault() fix it.
557 */
558 break;
559 default:
560 break;
561 }
562 return 0;
563}
564
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100565int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
566{
567 int ret;
568
569 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
570 local_irq_disable();
571 ret = kprobe_trap_handler(regs, trapnr);
572 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
573 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
574 return ret;
575}
576
Michael Grundy4ba069b2006-09-20 15:58:39 +0200577/*
578 * Wrapper routine to for handling exceptions.
579 */
580int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
581 unsigned long val, void *data)
582{
583 struct die_args *args = (struct die_args *)data;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100584 struct pt_regs *regs = args->regs;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200585 int ret = NOTIFY_DONE;
586
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100587 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
588 local_irq_disable();
589
Michael Grundy4ba069b2006-09-20 15:58:39 +0200590 switch (val) {
591 case DIE_BPT:
592 if (kprobe_handler(args->regs))
593 ret = NOTIFY_STOP;
594 break;
595 case DIE_SSTEP:
596 if (post_kprobe_handler(args->regs))
597 ret = NOTIFY_STOP;
598 break;
599 case DIE_TRAP:
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100600 if (!preemptible() && kprobe_running() &&
601 kprobe_trap_handler(args->regs, args->trapnr))
Michael Grundy4ba069b2006-09-20 15:58:39 +0200602 ret = NOTIFY_STOP;
Michael Grundy4ba069b2006-09-20 15:58:39 +0200603 break;
604 default:
605 break;
606 }
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100607
608 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
609 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
610
Michael Grundy4ba069b2006-09-20 15:58:39 +0200611 return ret;
612}
613
614int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
615{
616 struct jprobe *jp = container_of(p, struct jprobe, kp);
617 unsigned long addr;
618 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
619
620 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
621
622 /* setup return addr to the jprobe handler routine */
623 regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
Martin Schwidefskyadb45832010-11-10 10:05:57 +0100624 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
Michael Grundy4ba069b2006-09-20 15:58:39 +0200625
626 /* r14 is the function return address */
627 kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
628 /* r15 is the stack pointer */
629 kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
630 addr = (unsigned long)kcb->jprobe_saved_r15;
631
632 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
633 MIN_STACK_SIZE(addr));
634 return 1;
635}
636
637void __kprobes jprobe_return(void)
638{
639 asm volatile(".word 0x0002");
640}
641
642void __kprobes jprobe_return_end(void)
643{
644 asm volatile("bcr 0,0");
645}
646
647int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
648{
649 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
650 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
651
652 /* Put the regs back */
653 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
654 /* put the stack back */
655 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
656 MIN_STACK_SIZE(stack_addr));
657 preempt_enable_no_resched();
658 return 1;
659}
660
661static struct kprobe trampoline_p = {
662 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
663 .pre_handler = trampoline_probe_handler
664};
665
666int __init arch_init_kprobes(void)
667{
668 return register_kprobe(&trampoline_p);
669}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -0700670
671int __kprobes arch_trampoline_kprobe(struct kprobe *p)
672{
673 if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline)
674 return 1;
675 return 0;
676}