Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 23 | #include <linux/kprobes.h> |
| 24 | #include <linux/ptrace.h> |
| 25 | #include <linux/preempt.h> |
| 26 | #include <linux/stop_machine.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 27 | #include <linux/kdebug.h> |
Heiko Carstens | a2b5367 | 2009-06-12 10:26:43 +0200 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 29 | #include <asm/cacheflush.h> |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 30 | #include <asm/sections.h> |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 31 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 33 | #include <linux/hardirq.h> |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 34 | |
| 35 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 36 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
| 37 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 38 | struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; |
| 39 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | |
Martin Schwidefsky | 5532bd0 | 2008-07-14 09:59:41 +0200 | [diff] [blame] | 46 | if ((unsigned long)p->addr & 0x01) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 47 | return -EINVAL; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 */ |
Heiko Carstens | bac9f15 | 2010-05-26 23:26:20 +0200 | [diff] [blame] | 67 | case 0xac: /* stnsm */ |
| 68 | case 0xad: /* stosm */ |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 */ |
Heiko Carstens | bac9f15 | 2010-05-26 23:26:20 +0200 | [diff] [blame] | 78 | case 0xb98d: /* epsw */ |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 */ |
David Wilder | 9c5f225 | 2007-08-22 13:51:44 +0200 | [diff] [blame] | 93 | switch (*(__u8 *) (ainsn->insn) >> 6) { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | { |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 162 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 163 | unsigned long status = kcb->kprobe_status; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 164 | struct ins_replace_args *args = aref; |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 165 | int rc; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 166 | |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 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; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
| 174 | { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 175 | struct ins_replace_args args; |
| 176 | |
| 177 | args.ptr = p->addr; |
| 178 | args.old = p->opcode; |
| 179 | args.new = BREAKPOINT_INSTRUCTION; |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 180 | stop_machine(swap_instruction, &args, NULL); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
| 184 | { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 185 | struct ins_replace_args args; |
| 186 | |
| 187 | args.ptr = p->addr; |
| 188 | args.old = BREAKPOINT_INSTRUCTION; |
| 189 | args.new = p->opcode; |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 190 | stop_machine(swap_instruction, &args, NULL); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
| 194 | { |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 195 | if (p->ainsn.insn) { |
| 196 | free_insn_slot(p->ainsn.insn, 0); |
| 197 | p->ainsn.insn = NULL; |
| 198 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 201 | static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb, |
| 202 | struct pt_regs *regs, |
| 203 | unsigned long ip) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 204 | { |
| 205 | per_cr_bits kprobe_per_regs[1]; |
| 206 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 207 | /* Set up the per control reg info, will pass to lctl */ |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 208 | memset(kprobe_per_regs, 0, sizeof(per_cr_bits)); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 209 | kprobe_per_regs[0].em_instruction_fetch = 1; |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 210 | kprobe_per_regs[0].starting_addr = ip; |
| 211 | kprobe_per_regs[0].ending_addr = ip; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 212 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 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 */ |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 219 | __ctl_load(kprobe_per_regs, 9, 11); |
| 220 | regs->psw.mask |= PSW_MASK_PER; |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 221 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 222 | regs->psw.addr = ip | PSW_ADDR_AMODE; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 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 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 255 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 256 | struct pt_regs *regs) |
| 257 | { |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 258 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 259 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 260 | /* Replace the return addr with trampoline addr */ |
| 261 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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) { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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); |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 292 | enable_singlestep(kcb, regs, |
| 293 | (unsigned long) p->ainsn.insn); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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); |
Martin Schwidefsky | f794c82 | 2007-03-05 23:35:38 +0100 | [diff] [blame] | 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 |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 311 | * use stop_machine to arm/disarm the breakpoints. |
Martin Schwidefsky | f794c82 | 2007-03-05 23:35:38 +0100 | [diff] [blame] | 312 | */ |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 313 | goto no_kprobe; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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: |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 322 | enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | */ |
Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 337 | static void __used kretprobe_trampoline_holder(void) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 346 | static int __kprobes trampoline_probe_handler(struct kprobe *p, |
| 347 | struct pt_regs *regs) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 348 | { |
| 349 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 350 | struct hlist_head *head, empty_rp; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 351 | struct hlist_node *node, *tmp; |
| 352 | unsigned long flags, orig_ret_address = 0; |
| 353 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 354 | kprobe_opcode_t *correct_ret_addr = NULL; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 355 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 356 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 357 | kretprobe_hash_lock(current, &head, &flags); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 362 | * have a return probe installed on them, and/or more than one return |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 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; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 395 | |
| 396 | orig_ret_address = (unsigned long)ri->ret_addr; |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 397 | |
| 398 | if (ri->rp && ri->rp->handler) { |
| 399 | ri->ret_addr = correct_ret_addr; |
| 400 | ri->rp->handler(ri, regs); |
| 401 | } |
| 402 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 403 | recycle_rp_inst(ri, &empty_rp); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | } |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 414 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 415 | regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE; |
| 416 | |
| 417 | reset_current_kprobe(); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 418 | kretprobe_hash_unlock(current, &flags); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 419 | preempt_enable_no_resched(); |
| 420 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 421 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 422 | hlist_del(&ri->hlist); |
| 423 | kfree(ri); |
| 424 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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(); |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 444 | unsigned long ip = regs->psw.addr & PSW_ADDR_INSN; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 445 | |
| 446 | if (p->ainsn.fixup & FIXUP_PSW_NORMAL) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 447 | ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 448 | |
| 449 | if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 450 | if (ip - (unsigned long) p->ainsn.insn == p->ainsn.ilen) |
| 451 | ip = (unsigned long) p->addr + p->ainsn.ilen; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 452 | |
| 453 | if (p->ainsn.fixup & FIXUP_RETURN_REGISTER) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 454 | regs->gprs[p->ainsn.reg] += (unsigned long) p->addr - |
| 455 | (unsigned long) p->ainsn.insn; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 456 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 457 | disable_singlestep(kcb, regs, ip); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 496 | static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | */ |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame^] | 515 | disable_singlestep(kcb, regs, (unsigned long) cur->addr); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 516 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 517 | restore_previous_kprobe(kcb); |
Martin Schwidefsky | 9ec2708 | 2010-10-29 16:50:45 +0200 | [diff] [blame] | 518 | else { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 519 | reset_current_kprobe(); |
Martin Schwidefsky | 9ec2708 | 2010-10-29 16:50:45 +0200 | [diff] [blame] | 520 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 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 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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; |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 582 | struct pt_regs *regs = args->regs; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 583 | int ret = NOTIFY_DONE; |
| 584 | |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 585 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) |
| 586 | local_irq_disable(); |
| 587 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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: |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 598 | if (!preemptible() && kprobe_running() && |
| 599 | kprobe_trap_handler(args->regs, args->trapnr)) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 600 | ret = NOTIFY_STOP; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 601 | break; |
| 602 | default: |
| 603 | break; |
| 604 | } |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 605 | |
| 606 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) |
| 607 | local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); |
| 608 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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; |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 622 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 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 | } |
Ananth N Mavinakayanahalli | bf8f6e5 | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 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 | } |