Commit | Line | Data |
---|---|---|
4ba069b8 MG |
1 | /* |
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 | ||
4ba069b8 MG |
23 | #include <linux/kprobes.h> |
24 | #include <linux/ptrace.h> | |
25 | #include <linux/preempt.h> | |
26 | #include <linux/stop_machine.h> | |
1eeb66a1 | 27 | #include <linux/kdebug.h> |
a2b53673 | 28 | #include <linux/uaccess.h> |
4ba069b8 | 29 | #include <asm/cacheflush.h> |
4ba069b8 | 30 | #include <asm/sections.h> |
4ba069b8 | 31 | #include <linux/module.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
adb45839 | 33 | #include <linux/hardirq.h> |
4ba069b8 MG |
34 | |
35 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; | |
36 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
37 | ||
f438d914 MH |
38 | struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; |
39 | ||
4ba069b8 MG |
40 | int __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 | ||
5532bd0f | 46 | if ((unsigned long)p->addr & 0x01) |
4ba069b8 | 47 | return -EINVAL; |
4ba069b8 MG |
48 | |
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 | ||
60 | int __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 */ | |
bac9f154 HC |
67 | case 0xac: /* stnsm */ |
68 | case 0xad: /* stosm */ | |
4ba069b8 MG |
69 | 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 */ | |
bac9f154 | 78 | case 0xb98d: /* epsw */ |
4ba069b8 MG |
79 | return -EINVAL; |
80 | } | |
81 | return 0; | |
82 | } | |
83 | ||
84 | void __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 */ | |
9c5f225f | 93 | switch (*(__u8 *) (ainsn->insn) >> 6) { |
4ba069b8 MG |
94 | 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 | ||
160 | static int __kprobes swap_instruction(void *aref) | |
161 | { | |
acf01800 HC |
162 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
163 | unsigned long status = kcb->kprobe_status; | |
4ba069b8 | 164 | struct ins_replace_args *args = aref; |
acf01800 | 165 | int rc; |
a2b53673 | 166 | |
acf01800 HC |
167 | 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; | |
4ba069b8 MG |
171 | } |
172 | ||
173 | void __kprobes arch_arm_kprobe(struct kprobe *p) | |
174 | { | |
4ba069b8 MG |
175 | struct ins_replace_args args; |
176 | ||
177 | args.ptr = p->addr; | |
178 | args.old = p->opcode; | |
179 | args.new = BREAKPOINT_INSTRUCTION; | |
9b1a4d38 | 180 | stop_machine(swap_instruction, &args, NULL); |
4ba069b8 MG |
181 | } |
182 | ||
183 | void __kprobes arch_disarm_kprobe(struct kprobe *p) | |
184 | { | |
4ba069b8 MG |
185 | struct ins_replace_args args; |
186 | ||
187 | args.ptr = p->addr; | |
188 | args.old = BREAKPOINT_INSTRUCTION; | |
189 | args.new = p->opcode; | |
9b1a4d38 | 190 | stop_machine(swap_instruction, &args, NULL); |
4ba069b8 MG |
191 | } |
192 | ||
193 | void __kprobes arch_remove_kprobe(struct kprobe *p) | |
194 | { | |
12941560 MH |
195 | if (p->ainsn.insn) { |
196 | free_insn_slot(p->ainsn.insn, 0); | |
197 | p->ainsn.insn = NULL; | |
198 | } | |
4ba069b8 MG |
199 | } |
200 | ||
fc0a1fea MS |
201 | static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb, |
202 | struct pt_regs *regs, | |
203 | unsigned long ip) | |
4ba069b8 MG |
204 | { |
205 | per_cr_bits kprobe_per_regs[1]; | |
206 | ||
4ba069b8 | 207 | /* Set up the per control reg info, will pass to lctl */ |
fc0a1fea | 208 | memset(kprobe_per_regs, 0, sizeof(per_cr_bits)); |
4ba069b8 | 209 | kprobe_per_regs[0].em_instruction_fetch = 1; |
fc0a1fea MS |
210 | kprobe_per_regs[0].starting_addr = ip; |
211 | kprobe_per_regs[0].ending_addr = ip; | |
4ba069b8 | 212 | |
fc0a1fea MS |
213 | /* Save control regs and psw mask */ |
214 | __ctl_store(kcb->kprobe_saved_ctl, 9, 11); | |
215 | kcb->kprobe_saved_imask = regs->psw.mask & | |
216 | (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT); | |
217 | ||
218 | /* Set PER control regs, turns on single step for the given address */ | |
4ba069b8 MG |
219 | __ctl_load(kprobe_per_regs, 9, 11); |
220 | regs->psw.mask |= PSW_MASK_PER; | |
adb45839 | 221 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
fc0a1fea | 222 | regs->psw.addr = ip | PSW_ADDR_AMODE; |
4ba069b8 MG |
223 | } |
224 | ||
fc0a1fea MS |
225 | static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb, |
226 | struct pt_regs *regs, | |
227 | unsigned long ip) | |
228 | { | |
229 | /* Restore control regs and psw mask, set new psw address */ | |
230 | __ctl_load(kcb->kprobe_saved_ctl, 9, 11); | |
231 | regs->psw.mask &= ~PSW_MASK_PER; | |
232 | regs->psw.mask |= kcb->kprobe_saved_imask; | |
233 | regs->psw.addr = ip | PSW_ADDR_AMODE; | |
234 | } | |
235 | ||
236 | ||
4ba069b8 MG |
237 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
238 | { | |
239 | kcb->prev_kprobe.kp = kprobe_running(); | |
240 | kcb->prev_kprobe.status = kcb->kprobe_status; | |
4ba069b8 MG |
241 | } |
242 | ||
243 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) | |
244 | { | |
245 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; | |
246 | kcb->kprobe_status = kcb->prev_kprobe.status; | |
4ba069b8 MG |
247 | } |
248 | ||
249 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | |
250 | struct kprobe_ctlblk *kcb) | |
251 | { | |
252 | __get_cpu_var(current_kprobe) = p; | |
4ba069b8 MG |
253 | } |
254 | ||
4c4308cb | 255 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
4ba069b8 MG |
256 | struct pt_regs *regs) |
257 | { | |
4c4308cb | 258 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; |
4ba069b8 | 259 | |
4c4308cb CH |
260 | /* Replace the return addr with trampoline addr */ |
261 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; | |
4ba069b8 MG |
262 | } |
263 | ||
264 | static int __kprobes kprobe_handler(struct pt_regs *regs) | |
265 | { | |
266 | struct kprobe *p; | |
267 | int ret = 0; | |
268 | unsigned long *addr = (unsigned long *) | |
269 | ((regs->psw.addr & PSW_ADDR_INSN) - 2); | |
270 | struct kprobe_ctlblk *kcb; | |
271 | ||
272 | /* | |
273 | * We don't want to be preempted for the entire | |
274 | * duration of kprobe processing | |
275 | */ | |
276 | preempt_disable(); | |
277 | kcb = get_kprobe_ctlblk(); | |
278 | ||
279 | /* Check we're not actually recursing */ | |
280 | if (kprobe_running()) { | |
281 | p = get_kprobe(addr); | |
282 | if (p) { | |
4ba069b8 MG |
283 | /* We have reentered the kprobe_handler(), since |
284 | * another probe was hit while within the handler. | |
285 | * We here save the original kprobes variables and | |
286 | * just single step on the instruction of the new probe | |
287 | * without calling any user handlers. | |
288 | */ | |
289 | save_previous_kprobe(kcb); | |
290 | set_current_kprobe(p, regs, kcb); | |
291 | kprobes_inc_nmissed_count(p); | |
fc0a1fea MS |
292 | enable_singlestep(kcb, regs, |
293 | (unsigned long) p->ainsn.insn); | |
4ba069b8 MG |
294 | kcb->kprobe_status = KPROBE_REENTER; |
295 | return 1; | |
296 | } else { | |
297 | p = __get_cpu_var(current_kprobe); | |
298 | if (p->break_handler && p->break_handler(p, regs)) { | |
299 | goto ss_probe; | |
300 | } | |
301 | } | |
302 | goto no_kprobe; | |
303 | } | |
304 | ||
305 | p = get_kprobe(addr); | |
f794c827 MS |
306 | if (!p) |
307 | /* | |
308 | * No kprobe at this address. The fault has not been | |
309 | * caused by a kprobe breakpoint. The race of breakpoint | |
310 | * vs. kprobe remove does not exist because on s390 we | |
9b1a4d38 | 311 | * use stop_machine to arm/disarm the breakpoints. |
f794c827 | 312 | */ |
4ba069b8 | 313 | goto no_kprobe; |
4ba069b8 MG |
314 | |
315 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | |
316 | set_current_kprobe(p, regs, kcb); | |
317 | if (p->pre_handler && p->pre_handler(p, regs)) | |
318 | /* handler has already set things up, so skip ss setup */ | |
319 | return 1; | |
320 | ||
321 | ss_probe: | |
fc0a1fea | 322 | enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn); |
4ba069b8 MG |
323 | kcb->kprobe_status = KPROBE_HIT_SS; |
324 | return 1; | |
325 | ||
326 | no_kprobe: | |
327 | preempt_enable_no_resched(); | |
328 | return ret; | |
329 | } | |
330 | ||
331 | /* | |
332 | * Function return probe trampoline: | |
333 | * - init_kprobes() establishes a probepoint here | |
334 | * - When the probed function returns, this probe | |
335 | * causes the handlers to fire | |
336 | */ | |
a806170e | 337 | static void __used kretprobe_trampoline_holder(void) |
4ba069b8 MG |
338 | { |
339 | asm volatile(".global kretprobe_trampoline\n" | |
340 | "kretprobe_trampoline: bcr 0,0\n"); | |
341 | } | |
342 | ||
343 | /* | |
344 | * Called when the probe at kretprobe trampoline is hit | |
345 | */ | |
2b67fc46 HC |
346 | static int __kprobes trampoline_probe_handler(struct kprobe *p, |
347 | struct pt_regs *regs) | |
4ba069b8 MG |
348 | { |
349 | struct kretprobe_instance *ri = NULL; | |
99219a3f | 350 | struct hlist_head *head, empty_rp; |
4ba069b8 MG |
351 | struct hlist_node *node, *tmp; |
352 | unsigned long flags, orig_ret_address = 0; | |
353 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; | |
89480801 | 354 | kprobe_opcode_t *correct_ret_addr = NULL; |
4ba069b8 | 355 | |
99219a3f | 356 | INIT_HLIST_HEAD(&empty_rp); |
ef53d9c5 | 357 | kretprobe_hash_lock(current, &head, &flags); |
4ba069b8 MG |
358 | |
359 | /* | |
360 | * It is possible to have multiple instances associated with a given | |
361 | * task either because an multiple functions in the call path | |
025dfdaf | 362 | * have a return probe installed on them, and/or more than one return |
4ba069b8 MG |
363 | * return probe was registered for a target function. |
364 | * | |
365 | * We can handle this because: | |
366 | * - instances are always inserted at the head of the list | |
367 | * - when multiple return probes are registered for the same | |
368 | * function, the first instance's ret_addr will point to the | |
369 | * real return address, and all the rest will point to | |
370 | * kretprobe_trampoline | |
371 | */ | |
372 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
373 | if (ri->task != current) | |
374 | /* another task is sharing our hash bucket */ | |
375 | continue; | |
376 | ||
89480801 MS |
377 | orig_ret_address = (unsigned long)ri->ret_addr; |
378 | ||
379 | if (orig_ret_address != trampoline_address) | |
380 | /* | |
381 | * This is the real return address. Any other | |
382 | * instances associated with this task are for | |
383 | * other calls deeper on the call stack | |
384 | */ | |
385 | break; | |
386 | } | |
387 | ||
388 | kretprobe_assert(ri, orig_ret_address, trampoline_address); | |
389 | ||
390 | correct_ret_addr = ri->ret_addr; | |
391 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | |
392 | if (ri->task != current) | |
393 | /* another task is sharing our hash bucket */ | |
394 | continue; | |
4ba069b8 MG |
395 | |
396 | orig_ret_address = (unsigned long)ri->ret_addr; | |
89480801 MS |
397 | |
398 | if (ri->rp && ri->rp->handler) { | |
399 | ri->ret_addr = correct_ret_addr; | |
400 | ri->rp->handler(ri, regs); | |
401 | } | |
402 | ||
99219a3f | 403 | recycle_rp_inst(ri, &empty_rp); |
4ba069b8 MG |
404 | |
405 | if (orig_ret_address != trampoline_address) { | |
406 | /* | |
407 | * This is the real return address. Any other | |
408 | * instances associated with this task are for | |
409 | * other calls deeper on the call stack | |
410 | */ | |
411 | break; | |
412 | } | |
413 | } | |
89480801 | 414 | |
4ba069b8 MG |
415 | regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE; |
416 | ||
417 | reset_current_kprobe(); | |
ef53d9c5 | 418 | kretprobe_hash_unlock(current, &flags); |
4ba069b8 MG |
419 | preempt_enable_no_resched(); |
420 | ||
99219a3f | 421 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
422 | hlist_del(&ri->hlist); | |
423 | kfree(ri); | |
424 | } | |
4ba069b8 MG |
425 | /* |
426 | * By returning a non-zero value, we are telling | |
427 | * kprobe_handler() that we don't want the post_handler | |
428 | * to run (and have re-enabled preemption) | |
429 | */ | |
430 | return 1; | |
431 | } | |
432 | ||
433 | /* | |
434 | * Called after single-stepping. p->addr is the address of the | |
435 | * instruction whose first byte has been replaced by the "breakpoint" | |
436 | * instruction. To avoid the SMP problems that can occur when we | |
437 | * temporarily put back the original opcode to single-step, we | |
438 | * single-stepped a copy of the instruction. The address of this | |
439 | * copy is p->ainsn.insn. | |
440 | */ | |
441 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) | |
442 | { | |
443 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
fc0a1fea | 444 | unsigned long ip = regs->psw.addr & PSW_ADDR_INSN; |
4ba069b8 MG |
445 | |
446 | if (p->ainsn.fixup & FIXUP_PSW_NORMAL) | |
fc0a1fea | 447 | ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn; |
4ba069b8 MG |
448 | |
449 | if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN) | |
fc0a1fea MS |
450 | if (ip - (unsigned long) p->ainsn.insn == p->ainsn.ilen) |
451 | ip = (unsigned long) p->addr + p->ainsn.ilen; | |
4ba069b8 MG |
452 | |
453 | if (p->ainsn.fixup & FIXUP_RETURN_REGISTER) | |
fc0a1fea MS |
454 | regs->gprs[p->ainsn.reg] += (unsigned long) p->addr - |
455 | (unsigned long) p->ainsn.insn; | |
4ba069b8 | 456 | |
fc0a1fea | 457 | disable_singlestep(kcb, regs, ip); |
4ba069b8 MG |
458 | } |
459 | ||
460 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | |
461 | { | |
462 | struct kprobe *cur = kprobe_running(); | |
463 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
464 | ||
465 | if (!cur) | |
466 | return 0; | |
467 | ||
468 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { | |
469 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | |
470 | cur->post_handler(cur, regs, 0); | |
471 | } | |
472 | ||
473 | resume_execution(cur, regs); | |
474 | ||
475 | /*Restore back the original saved kprobes variables and continue. */ | |
476 | if (kcb->kprobe_status == KPROBE_REENTER) { | |
477 | restore_previous_kprobe(kcb); | |
478 | goto out; | |
479 | } | |
480 | reset_current_kprobe(); | |
481 | out: | |
482 | preempt_enable_no_resched(); | |
483 | ||
484 | /* | |
485 | * if somebody else is singlestepping across a probe point, psw mask | |
486 | * will have PER set, in which case, continue the remaining processing | |
487 | * of do_single_step, as if this is not a probe hit. | |
488 | */ | |
489 | if (regs->psw.mask & PSW_MASK_PER) { | |
490 | return 0; | |
491 | } | |
492 | ||
493 | return 1; | |
494 | } | |
495 | ||
adb45839 | 496 | static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr) |
4ba069b8 MG |
497 | { |
498 | struct kprobe *cur = kprobe_running(); | |
499 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
500 | const struct exception_table_entry *entry; | |
501 | ||
502 | switch(kcb->kprobe_status) { | |
503 | case KPROBE_SWAP_INST: | |
504 | /* We are here because the instruction replacement failed */ | |
505 | return 0; | |
506 | case KPROBE_HIT_SS: | |
507 | case KPROBE_REENTER: | |
508 | /* | |
509 | * We are here because the instruction being single | |
510 | * stepped caused a page fault. We reset the current | |
511 | * kprobe and the nip points back to the probe address | |
512 | * and allow the page fault handler to continue as a | |
513 | * normal page fault. | |
514 | */ | |
fc0a1fea | 515 | disable_singlestep(kcb, regs, (unsigned long) cur->addr); |
4ba069b8 MG |
516 | if (kcb->kprobe_status == KPROBE_REENTER) |
517 | restore_previous_kprobe(kcb); | |
9ec27080 | 518 | else { |
4ba069b8 | 519 | reset_current_kprobe(); |
9ec27080 | 520 | } |
4ba069b8 MG |
521 | preempt_enable_no_resched(); |
522 | break; | |
523 | case KPROBE_HIT_ACTIVE: | |
524 | case KPROBE_HIT_SSDONE: | |
525 | /* | |
526 | * We increment the nmissed count for accounting, | |
527 | * we can also use npre/npostfault count for accouting | |
528 | * these specific fault cases. | |
529 | */ | |
530 | kprobes_inc_nmissed_count(cur); | |
531 | ||
532 | /* | |
533 | * We come here because instructions in the pre/post | |
534 | * handler caused the page_fault, this could happen | |
535 | * if handler tries to access user space by | |
536 | * copy_from_user(), get_user() etc. Let the | |
537 | * user-specified handler try to fix it first. | |
538 | */ | |
539 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | |
540 | return 1; | |
541 | ||
542 | /* | |
543 | * In case the user-specified fault handler returned | |
544 | * zero, try to fix up. | |
545 | */ | |
546 | entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN); | |
547 | if (entry) { | |
548 | regs->psw.addr = entry->fixup | PSW_ADDR_AMODE; | |
549 | return 1; | |
550 | } | |
551 | ||
552 | /* | |
553 | * fixup_exception() could not handle it, | |
554 | * Let do_page_fault() fix it. | |
555 | */ | |
556 | break; | |
557 | default: | |
558 | break; | |
559 | } | |
560 | return 0; | |
561 | } | |
562 | ||
adb45839 MS |
563 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
564 | { | |
565 | int ret; | |
566 | ||
567 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) | |
568 | local_irq_disable(); | |
569 | ret = kprobe_trap_handler(regs, trapnr); | |
570 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) | |
571 | local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); | |
572 | return ret; | |
573 | } | |
574 | ||
4ba069b8 MG |
575 | /* |
576 | * Wrapper routine to for handling exceptions. | |
577 | */ | |
578 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, | |
579 | unsigned long val, void *data) | |
580 | { | |
581 | struct die_args *args = (struct die_args *)data; | |
adb45839 | 582 | struct pt_regs *regs = args->regs; |
4ba069b8 MG |
583 | int ret = NOTIFY_DONE; |
584 | ||
adb45839 MS |
585 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) |
586 | local_irq_disable(); | |
587 | ||
4ba069b8 MG |
588 | switch (val) { |
589 | case DIE_BPT: | |
590 | if (kprobe_handler(args->regs)) | |
591 | ret = NOTIFY_STOP; | |
592 | break; | |
593 | case DIE_SSTEP: | |
594 | if (post_kprobe_handler(args->regs)) | |
595 | ret = NOTIFY_STOP; | |
596 | break; | |
597 | case DIE_TRAP: | |
adb45839 MS |
598 | if (!preemptible() && kprobe_running() && |
599 | kprobe_trap_handler(args->regs, args->trapnr)) | |
4ba069b8 | 600 | ret = NOTIFY_STOP; |
4ba069b8 MG |
601 | break; |
602 | default: | |
603 | break; | |
604 | } | |
adb45839 MS |
605 | |
606 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) | |
607 | local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); | |
608 | ||
4ba069b8 MG |
609 | return ret; |
610 | } | |
611 | ||
612 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) | |
613 | { | |
614 | struct jprobe *jp = container_of(p, struct jprobe, kp); | |
615 | unsigned long addr; | |
616 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
617 | ||
618 | memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs)); | |
619 | ||
620 | /* setup return addr to the jprobe handler routine */ | |
621 | regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE; | |
adb45839 | 622 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
4ba069b8 MG |
623 | |
624 | /* r14 is the function return address */ | |
625 | kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14]; | |
626 | /* r15 is the stack pointer */ | |
627 | kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15]; | |
628 | addr = (unsigned long)kcb->jprobe_saved_r15; | |
629 | ||
630 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr, | |
631 | MIN_STACK_SIZE(addr)); | |
632 | return 1; | |
633 | } | |
634 | ||
635 | void __kprobes jprobe_return(void) | |
636 | { | |
637 | asm volatile(".word 0x0002"); | |
638 | } | |
639 | ||
640 | void __kprobes jprobe_return_end(void) | |
641 | { | |
642 | asm volatile("bcr 0,0"); | |
643 | } | |
644 | ||
645 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) | |
646 | { | |
647 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | |
648 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15); | |
649 | ||
650 | /* Put the regs back */ | |
651 | memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs)); | |
652 | /* put the stack back */ | |
653 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, | |
654 | MIN_STACK_SIZE(stack_addr)); | |
655 | preempt_enable_no_resched(); | |
656 | return 1; | |
657 | } | |
658 | ||
659 | static struct kprobe trampoline_p = { | |
660 | .addr = (kprobe_opcode_t *) & kretprobe_trampoline, | |
661 | .pre_handler = trampoline_probe_handler | |
662 | }; | |
663 | ||
664 | int __init arch_init_kprobes(void) | |
665 | { | |
666 | return register_kprobe(&trampoline_p); | |
667 | } | |
bf8f6e5b AM |
668 | |
669 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) | |
670 | { | |
671 | if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline) | |
672 | return 1; | |
673 | return 0; | |
674 | } |