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 | |
Martin Schwidefsky | 5a8b589 | 2011-01-05 12:47:18 +0100 | [diff] [blame^] | 160 | struct ins_replace_args { |
| 161 | kprobe_opcode_t *ptr; |
| 162 | kprobe_opcode_t opcode; |
| 163 | }; |
| 164 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 165 | static int __kprobes swap_instruction(void *aref) |
| 166 | { |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 167 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 168 | unsigned long status = kcb->kprobe_status; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 169 | struct ins_replace_args *args = aref; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 170 | |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 171 | kcb->kprobe_status = KPROBE_SWAP_INST; |
Martin Schwidefsky | 5a8b589 | 2011-01-05 12:47:18 +0100 | [diff] [blame^] | 172 | probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode)); |
Heiko Carstens | acf0180 | 2009-06-22 12:08:23 +0200 | [diff] [blame] | 173 | kcb->kprobe_status = status; |
Martin Schwidefsky | 5a8b589 | 2011-01-05 12:47:18 +0100 | [diff] [blame^] | 174 | return 0; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
| 178 | { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 179 | struct ins_replace_args args; |
| 180 | |
| 181 | args.ptr = p->addr; |
Martin Schwidefsky | 5a8b589 | 2011-01-05 12:47:18 +0100 | [diff] [blame^] | 182 | args.opcode = BREAKPOINT_INSTRUCTION; |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 183 | stop_machine(swap_instruction, &args, NULL); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
| 187 | { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 188 | struct ins_replace_args args; |
| 189 | |
| 190 | args.ptr = p->addr; |
Martin Schwidefsky | 5a8b589 | 2011-01-05 12:47:18 +0100 | [diff] [blame^] | 191 | args.opcode = p->opcode; |
Rusty Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 192 | stop_machine(swap_instruction, &args, NULL); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
| 196 | { |
Masami Hiramatsu | 1294156 | 2009-01-06 14:41:50 -0800 | [diff] [blame] | 197 | if (p->ainsn.insn) { |
| 198 | free_insn_slot(p->ainsn.insn, 0); |
| 199 | p->ainsn.insn = NULL; |
| 200 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 203 | static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb, |
| 204 | struct pt_regs *regs, |
| 205 | unsigned long ip) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 206 | { |
| 207 | per_cr_bits kprobe_per_regs[1]; |
| 208 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 209 | /* Set up the per control reg info, will pass to lctl */ |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 210 | memset(kprobe_per_regs, 0, sizeof(per_cr_bits)); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 211 | kprobe_per_regs[0].em_instruction_fetch = 1; |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 212 | kprobe_per_regs[0].starting_addr = ip; |
| 213 | kprobe_per_regs[0].ending_addr = ip; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 214 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 215 | /* 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 221 | __ctl_load(kprobe_per_regs, 9, 11); |
| 222 | regs->psw.mask |= PSW_MASK_PER; |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 223 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 224 | regs->psw.addr = ip | PSW_ADDR_AMODE; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 227 | static 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 239 | static 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 257 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 258 | struct pt_regs *regs) |
| 259 | { |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 260 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 261 | |
Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 262 | /* Replace the return addr with trampoline addr */ |
| 263 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 285 | /* 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 Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 294 | enable_singlestep(kcb, regs, |
| 295 | (unsigned long) p->ainsn.insn); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 296 | 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 Schwidefsky | f794c82 | 2007-03-05 23:35:38 +0100 | [diff] [blame] | 308 | 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 Russell | 9b1a4d3 | 2008-07-28 12:16:30 -0500 | [diff] [blame] | 313 | * use stop_machine to arm/disarm the breakpoints. |
Martin Schwidefsky | f794c82 | 2007-03-05 23:35:38 +0100 | [diff] [blame] | 314 | */ |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 315 | goto no_kprobe; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 316 | |
| 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 | |
| 323 | ss_probe: |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 324 | enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 325 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 326 | return 1; |
| 327 | |
| 328 | no_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 Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 339 | static void __used kretprobe_trampoline_holder(void) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 340 | { |
| 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 Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 348 | static int __kprobes trampoline_probe_handler(struct kprobe *p, |
| 349 | struct pt_regs *regs) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 350 | { |
| 351 | struct kretprobe_instance *ri = NULL; |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 352 | struct hlist_head *head, empty_rp; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 353 | struct hlist_node *node, *tmp; |
| 354 | unsigned long flags, orig_ret_address = 0; |
| 355 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 356 | kprobe_opcode_t *correct_ret_addr = NULL; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 357 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 358 | INIT_HLIST_HEAD(&empty_rp); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 359 | kretprobe_hash_lock(current, &head, &flags); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 360 | |
| 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 Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 364 | * 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] | 365 | * 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 Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 379 | 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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 397 | |
| 398 | orig_ret_address = (unsigned long)ri->ret_addr; |
Martin Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 399 | |
| 400 | if (ri->rp && ri->rp->handler) { |
| 401 | ri->ret_addr = correct_ret_addr; |
| 402 | ri->rp->handler(ri, regs); |
| 403 | } |
| 404 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 405 | recycle_rp_inst(ri, &empty_rp); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 406 | |
| 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 Schwidefsky | 8948080 | 2010-11-10 10:05:58 +0100 | [diff] [blame] | 416 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 417 | regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE; |
| 418 | |
| 419 | reset_current_kprobe(); |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 420 | kretprobe_hash_unlock(current, &flags); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 421 | preempt_enable_no_resched(); |
| 422 | |
bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 423 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { |
| 424 | hlist_del(&ri->hlist); |
| 425 | kfree(ri); |
| 426 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 427 | /* |
| 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 | */ |
| 443 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) |
| 444 | { |
| 445 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 446 | unsigned long ip = regs->psw.addr & PSW_ADDR_INSN; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 447 | |
| 448 | if (p->ainsn.fixup & FIXUP_PSW_NORMAL) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 449 | ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 450 | |
| 451 | if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 452 | if (ip - (unsigned long) p->ainsn.insn == p->ainsn.ilen) |
| 453 | ip = (unsigned long) p->addr + p->ainsn.ilen; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 454 | |
| 455 | if (p->ainsn.fixup & FIXUP_RETURN_REGISTER) |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 456 | regs->gprs[p->ainsn.reg] += (unsigned long) p->addr - |
| 457 | (unsigned long) p->ainsn.insn; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 458 | |
Martin Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 459 | disable_singlestep(kcb, regs, ip); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static 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(); |
| 483 | out: |
| 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 Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 498 | static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 499 | { |
| 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 Schwidefsky | fc0a1fe | 2011-01-05 12:47:17 +0100 | [diff] [blame] | 517 | disable_singlestep(kcb, regs, (unsigned long) cur->addr); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 518 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 519 | restore_previous_kprobe(kcb); |
Martin Schwidefsky | 9ec2708 | 2010-10-29 16:50:45 +0200 | [diff] [blame] | 520 | else { |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 521 | reset_current_kprobe(); |
Martin Schwidefsky | 9ec2708 | 2010-10-29 16:50:45 +0200 | [diff] [blame] | 522 | } |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 523 | 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 Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 565 | int __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 Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 577 | /* |
| 578 | * Wrapper routine to for handling exceptions. |
| 579 | */ |
| 580 | int __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 Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 584 | struct pt_regs *regs = args->regs; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 585 | int ret = NOTIFY_DONE; |
| 586 | |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 587 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) |
| 588 | local_irq_disable(); |
| 589 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 590 | 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 Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 600 | if (!preemptible() && kprobe_running() && |
| 601 | kprobe_trap_handler(args->regs, args->trapnr)) |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 602 | ret = NOTIFY_STOP; |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 603 | break; |
| 604 | default: |
| 605 | break; |
| 606 | } |
Martin Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 607 | |
| 608 | if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT)) |
| 609 | local_irq_restore(regs->psw.mask & ~PSW_MASK_PER); |
| 610 | |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 611 | return ret; |
| 612 | } |
| 613 | |
| 614 | int __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 Schwidefsky | adb4583 | 2010-11-10 10:05:57 +0100 | [diff] [blame] | 624 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT); |
Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 625 | |
| 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 | |
| 637 | void __kprobes jprobe_return(void) |
| 638 | { |
| 639 | asm volatile(".word 0x0002"); |
| 640 | } |
| 641 | |
| 642 | void __kprobes jprobe_return_end(void) |
| 643 | { |
| 644 | asm volatile("bcr 0,0"); |
| 645 | } |
| 646 | |
| 647 | int __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 | |
| 661 | static struct kprobe trampoline_p = { |
| 662 | .addr = (kprobe_opcode_t *) & kretprobe_trampoline, |
| 663 | .pre_handler = trampoline_probe_handler |
| 664 | }; |
| 665 | |
| 666 | int __init arch_init_kprobes(void) |
| 667 | { |
| 668 | return register_kprobe(&trampoline_p); |
| 669 | } |
Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 670 | |
| 671 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 672 | { |
| 673 | if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline) |
| 674 | return 1; |
| 675 | return 0; |
| 676 | } |