| 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> | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 28 | #include <asm/cacheflush.h> | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 29 | #include <asm/sections.h> | 
|  | 30 | #include <asm/uaccess.h> | 
|  | 31 | #include <linux/module.h> | 
|  | 32 |  | 
|  | 33 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; | 
|  | 34 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | 
|  | 35 |  | 
| Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 36 | struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; | 
|  | 37 |  | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 38 | int __kprobes arch_prepare_kprobe(struct kprobe *p) | 
|  | 39 | { | 
|  | 40 | /* Make sure the probe isn't going on a difficult instruction */ | 
|  | 41 | if (is_prohibited_opcode((kprobe_opcode_t *) p->addr)) | 
|  | 42 | return -EINVAL; | 
|  | 43 |  | 
|  | 44 | if ((unsigned long)p->addr & 0x01) { | 
|  | 45 | printk("Attempt to register kprobe at an unaligned address\n"); | 
|  | 46 | return -EINVAL; | 
|  | 47 | } | 
|  | 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	 */ | 
|  | 67 | return -EINVAL; | 
|  | 68 | } | 
|  | 69 | switch (*(__u16 *) instruction) { | 
|  | 70 | case 0x0101:	/* pr	 */ | 
|  | 71 | case 0xb25a:	/* bsa	 */ | 
|  | 72 | case 0xb240:	/* bakr  */ | 
|  | 73 | case 0xb258:	/* bsg	 */ | 
|  | 74 | case 0xb218:	/* pc	 */ | 
|  | 75 | case 0xb228:	/* pt	 */ | 
|  | 76 | return -EINVAL; | 
|  | 77 | } | 
|  | 78 | return 0; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | void __kprobes get_instruction_type(struct arch_specific_insn *ainsn) | 
|  | 82 | { | 
|  | 83 | /* default fixup method */ | 
|  | 84 | ainsn->fixup = FIXUP_PSW_NORMAL; | 
|  | 85 |  | 
|  | 86 | /* save r1 operand */ | 
|  | 87 | ainsn->reg = (*ainsn->insn & 0xf0) >> 4; | 
|  | 88 |  | 
|  | 89 | /* save the instruction length (pop 5-5) in bytes */ | 
| David Wilder | 9c5f225 | 2007-08-22 13:51:44 +0200 | [diff] [blame] | 90 | switch (*(__u8 *) (ainsn->insn) >> 6) { | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 91 | case 0: | 
|  | 92 | ainsn->ilen = 2; | 
|  | 93 | break; | 
|  | 94 | case 1: | 
|  | 95 | case 2: | 
|  | 96 | ainsn->ilen = 4; | 
|  | 97 | break; | 
|  | 98 | case 3: | 
|  | 99 | ainsn->ilen = 6; | 
|  | 100 | break; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | switch (*(__u8 *) ainsn->insn) { | 
|  | 104 | case 0x05:	/* balr	*/ | 
|  | 105 | case 0x0d:	/* basr */ | 
|  | 106 | ainsn->fixup = FIXUP_RETURN_REGISTER; | 
|  | 107 | /* if r2 = 0, no branch will be taken */ | 
|  | 108 | if ((*ainsn->insn & 0x0f) == 0) | 
|  | 109 | ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN; | 
|  | 110 | break; | 
|  | 111 | case 0x06:	/* bctr	*/ | 
|  | 112 | case 0x07:	/* bcr	*/ | 
|  | 113 | ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; | 
|  | 114 | break; | 
|  | 115 | case 0x45:	/* bal	*/ | 
|  | 116 | case 0x4d:	/* bas	*/ | 
|  | 117 | ainsn->fixup = FIXUP_RETURN_REGISTER; | 
|  | 118 | break; | 
|  | 119 | case 0x47:	/* bc	*/ | 
|  | 120 | case 0x46:	/* bct	*/ | 
|  | 121 | case 0x86:	/* bxh	*/ | 
|  | 122 | case 0x87:	/* bxle	*/ | 
|  | 123 | ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; | 
|  | 124 | break; | 
|  | 125 | case 0x82:	/* lpsw	*/ | 
|  | 126 | ainsn->fixup = FIXUP_NOT_REQUIRED; | 
|  | 127 | break; | 
|  | 128 | case 0xb2:	/* lpswe */ | 
|  | 129 | if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) { | 
|  | 130 | ainsn->fixup = FIXUP_NOT_REQUIRED; | 
|  | 131 | } | 
|  | 132 | break; | 
|  | 133 | case 0xa7:	/* bras	*/ | 
|  | 134 | if ((*ainsn->insn & 0x0f) == 0x05) { | 
|  | 135 | ainsn->fixup |= FIXUP_RETURN_REGISTER; | 
|  | 136 | } | 
|  | 137 | break; | 
|  | 138 | case 0xc0: | 
|  | 139 | if ((*ainsn->insn & 0x0f) == 0x00  /* larl  */ | 
|  | 140 | || (*ainsn->insn & 0x0f) == 0x05) /* brasl */ | 
|  | 141 | ainsn->fixup |= FIXUP_RETURN_REGISTER; | 
|  | 142 | break; | 
|  | 143 | case 0xeb: | 
|  | 144 | if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 ||	/* bxhg  */ | 
|  | 145 | *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */ | 
|  | 146 | ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; | 
|  | 147 | } | 
|  | 148 | break; | 
|  | 149 | case 0xe3:	/* bctg	*/ | 
|  | 150 | if (*(((__u8 *) ainsn->insn) + 5) == 0x46) { | 
|  | 151 | ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN; | 
|  | 152 | } | 
|  | 153 | break; | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | static int __kprobes swap_instruction(void *aref) | 
|  | 158 | { | 
|  | 159 | struct ins_replace_args *args = aref; | 
| Heiko Carstens | 162e006 | 2007-02-05 21:18:41 +0100 | [diff] [blame] | 160 | u32 *addr; | 
|  | 161 | u32 instr; | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 162 | int err = -EFAULT; | 
|  | 163 |  | 
| Heiko Carstens | 162e006 | 2007-02-05 21:18:41 +0100 | [diff] [blame] | 164 | /* | 
|  | 165 | * Text segment is read-only, hence we use stura to bypass dynamic | 
|  | 166 | * address translation to exchange the instruction. Since stura | 
|  | 167 | * always operates on four bytes, but we only want to exchange two | 
|  | 168 | * bytes do some calculations to get things right. In addition we | 
|  | 169 | * shall not cross any page boundaries (vmalloc area!) when writing | 
|  | 170 | * the new instruction. | 
|  | 171 | */ | 
| David Wilder | b70842d | 2007-03-26 20:42:40 +0200 | [diff] [blame] | 172 | addr = (u32 *)((unsigned long)args->ptr & -4UL); | 
| Heiko Carstens | 162e006 | 2007-02-05 21:18:41 +0100 | [diff] [blame] | 173 | if ((unsigned long)args->ptr & 2) | 
|  | 174 | instr = ((*addr) & 0xffff0000) | args->new; | 
|  | 175 | else | 
|  | 176 | instr = ((*addr) & 0x0000ffff) | args->new << 16; | 
|  | 177 |  | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 178 | asm volatile( | 
| Heiko Carstens | 162e006 | 2007-02-05 21:18:41 +0100 | [diff] [blame] | 179 | "	lra	%1,0(%1)\n" | 
|  | 180 | "0:	stura	%2,%1\n" | 
|  | 181 | "1:	la	%0,0\n" | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 182 | "2:\n" | 
|  | 183 | EX_TABLE(0b,2b) | 
| Heiko Carstens | 162e006 | 2007-02-05 21:18:41 +0100 | [diff] [blame] | 184 | : "+d" (err) | 
|  | 185 | : "a" (addr), "d" (instr) | 
|  | 186 | : "memory", "cc"); | 
|  | 187 |  | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 188 | return err; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | void __kprobes arch_arm_kprobe(struct kprobe *p) | 
|  | 192 | { | 
|  | 193 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 194 | unsigned long status = kcb->kprobe_status; | 
|  | 195 | struct ins_replace_args args; | 
|  | 196 |  | 
|  | 197 | args.ptr = p->addr; | 
|  | 198 | args.old = p->opcode; | 
|  | 199 | args.new = BREAKPOINT_INSTRUCTION; | 
|  | 200 |  | 
|  | 201 | kcb->kprobe_status = KPROBE_SWAP_INST; | 
|  | 202 | stop_machine_run(swap_instruction, &args, NR_CPUS); | 
|  | 203 | kcb->kprobe_status = status; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | void __kprobes arch_disarm_kprobe(struct kprobe *p) | 
|  | 207 | { | 
|  | 208 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 209 | unsigned long status = kcb->kprobe_status; | 
|  | 210 | struct ins_replace_args args; | 
|  | 211 |  | 
|  | 212 | args.ptr = p->addr; | 
|  | 213 | args.old = BREAKPOINT_INSTRUCTION; | 
|  | 214 | args.new = p->opcode; | 
|  | 215 |  | 
|  | 216 | kcb->kprobe_status = KPROBE_SWAP_INST; | 
|  | 217 | stop_machine_run(swap_instruction, &args, NR_CPUS); | 
|  | 218 | kcb->kprobe_status = status; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | void __kprobes arch_remove_kprobe(struct kprobe *p) | 
|  | 222 | { | 
|  | 223 | mutex_lock(&kprobe_mutex); | 
| Masami Hiramatsu | b4c6c34 | 2006-12-06 20:38:11 -0800 | [diff] [blame] | 224 | free_insn_slot(p->ainsn.insn, 0); | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 225 | mutex_unlock(&kprobe_mutex); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) | 
|  | 229 | { | 
|  | 230 | per_cr_bits kprobe_per_regs[1]; | 
|  | 231 |  | 
|  | 232 | memset(kprobe_per_regs, 0, sizeof(per_cr_bits)); | 
|  | 233 | regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE; | 
|  | 234 |  | 
|  | 235 | /* Set up the per control reg info, will pass to lctl */ | 
|  | 236 | kprobe_per_regs[0].em_instruction_fetch = 1; | 
|  | 237 | kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn; | 
|  | 238 | kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1; | 
|  | 239 |  | 
|  | 240 | /* Set the PER control regs, turns on single step for this address */ | 
|  | 241 | __ctl_load(kprobe_per_regs, 9, 11); | 
|  | 242 | regs->psw.mask |= PSW_MASK_PER; | 
|  | 243 | regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK); | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) | 
|  | 247 | { | 
|  | 248 | kcb->prev_kprobe.kp = kprobe_running(); | 
|  | 249 | kcb->prev_kprobe.status = kcb->kprobe_status; | 
|  | 250 | kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask; | 
|  | 251 | memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl, | 
|  | 252 | sizeof(kcb->kprobe_saved_ctl)); | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) | 
|  | 256 | { | 
|  | 257 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; | 
|  | 258 | kcb->kprobe_status = kcb->prev_kprobe.status; | 
|  | 259 | kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask; | 
|  | 260 | memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl, | 
|  | 261 | sizeof(kcb->kprobe_saved_ctl)); | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | 
|  | 265 | struct kprobe_ctlblk *kcb) | 
|  | 266 | { | 
|  | 267 | __get_cpu_var(current_kprobe) = p; | 
|  | 268 | /* Save the interrupt and per flags */ | 
|  | 269 | kcb->kprobe_saved_imask = regs->psw.mask & | 
|  | 270 | (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK); | 
|  | 271 | /* Save the control regs that govern PER */ | 
|  | 272 | __ctl_store(kcb->kprobe_saved_ctl, 9, 11); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | /* Called with kretprobe_lock held */ | 
| Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 276 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 277 | struct pt_regs *regs) | 
|  | 278 | { | 
| Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 279 | ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14]; | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 280 |  | 
| Christoph Hellwig | 4c4308c | 2007-05-08 00:34:14 -0700 | [diff] [blame] | 281 | /* Replace the return addr with trampoline addr */ | 
|  | 282 | regs->gprs[14] = (unsigned long)&kretprobe_trampoline; | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
|  | 285 | static int __kprobes kprobe_handler(struct pt_regs *regs) | 
|  | 286 | { | 
|  | 287 | struct kprobe *p; | 
|  | 288 | int ret = 0; | 
|  | 289 | unsigned long *addr = (unsigned long *) | 
|  | 290 | ((regs->psw.addr & PSW_ADDR_INSN) - 2); | 
|  | 291 | struct kprobe_ctlblk *kcb; | 
|  | 292 |  | 
|  | 293 | /* | 
|  | 294 | * We don't want to be preempted for the entire | 
|  | 295 | * duration of kprobe processing | 
|  | 296 | */ | 
|  | 297 | preempt_disable(); | 
|  | 298 | kcb = get_kprobe_ctlblk(); | 
|  | 299 |  | 
|  | 300 | /* Check we're not actually recursing */ | 
|  | 301 | if (kprobe_running()) { | 
|  | 302 | p = get_kprobe(addr); | 
|  | 303 | if (p) { | 
|  | 304 | if (kcb->kprobe_status == KPROBE_HIT_SS && | 
|  | 305 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { | 
|  | 306 | regs->psw.mask &= ~PSW_MASK_PER; | 
|  | 307 | regs->psw.mask |= kcb->kprobe_saved_imask; | 
|  | 308 | goto no_kprobe; | 
|  | 309 | } | 
|  | 310 | /* We have reentered the kprobe_handler(), since | 
|  | 311 | * another probe was hit while within the handler. | 
|  | 312 | * We here save the original kprobes variables and | 
|  | 313 | * just single step on the instruction of the new probe | 
|  | 314 | * without calling any user handlers. | 
|  | 315 | */ | 
|  | 316 | save_previous_kprobe(kcb); | 
|  | 317 | set_current_kprobe(p, regs, kcb); | 
|  | 318 | kprobes_inc_nmissed_count(p); | 
|  | 319 | prepare_singlestep(p, regs); | 
|  | 320 | kcb->kprobe_status = KPROBE_REENTER; | 
|  | 321 | return 1; | 
|  | 322 | } else { | 
|  | 323 | p = __get_cpu_var(current_kprobe); | 
|  | 324 | if (p->break_handler && p->break_handler(p, regs)) { | 
|  | 325 | goto ss_probe; | 
|  | 326 | } | 
|  | 327 | } | 
|  | 328 | goto no_kprobe; | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | p = get_kprobe(addr); | 
| Martin Schwidefsky | f794c82 | 2007-03-05 23:35:38 +0100 | [diff] [blame] | 332 | if (!p) | 
|  | 333 | /* | 
|  | 334 | * No kprobe at this address. The fault has not been | 
|  | 335 | * caused by a kprobe breakpoint. The race of breakpoint | 
|  | 336 | * vs. kprobe remove does not exist because on s390 we | 
|  | 337 | * use stop_machine_run to arm/disarm the breakpoints. | 
|  | 338 | */ | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 339 | goto no_kprobe; | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 340 |  | 
|  | 341 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; | 
|  | 342 | set_current_kprobe(p, regs, kcb); | 
|  | 343 | if (p->pre_handler && p->pre_handler(p, regs)) | 
|  | 344 | /* handler has already set things up, so skip ss setup */ | 
|  | 345 | return 1; | 
|  | 346 |  | 
|  | 347 | ss_probe: | 
|  | 348 | prepare_singlestep(p, regs); | 
|  | 349 | kcb->kprobe_status = KPROBE_HIT_SS; | 
|  | 350 | return 1; | 
|  | 351 |  | 
|  | 352 | no_kprobe: | 
|  | 353 | preempt_enable_no_resched(); | 
|  | 354 | return ret; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | /* | 
|  | 358 | * Function return probe trampoline: | 
|  | 359 | *	- init_kprobes() establishes a probepoint here | 
|  | 360 | *	- When the probed function returns, this probe | 
|  | 361 | *		causes the handlers to fire | 
|  | 362 | */ | 
| Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 363 | static void __used kretprobe_trampoline_holder(void) | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 364 | { | 
|  | 365 | asm volatile(".global kretprobe_trampoline\n" | 
|  | 366 | "kretprobe_trampoline: bcr 0,0\n"); | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | /* | 
|  | 370 | * Called when the probe at kretprobe trampoline is hit | 
|  | 371 | */ | 
| Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 372 | static int __kprobes trampoline_probe_handler(struct kprobe *p, | 
|  | 373 | struct pt_regs *regs) | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 374 | { | 
|  | 375 | struct kretprobe_instance *ri = NULL; | 
| bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 376 | struct hlist_head *head, empty_rp; | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 377 | struct hlist_node *node, *tmp; | 
|  | 378 | unsigned long flags, orig_ret_address = 0; | 
|  | 379 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; | 
|  | 380 |  | 
| bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 381 | INIT_HLIST_HEAD(&empty_rp); | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 382 | spin_lock_irqsave(&kretprobe_lock, flags); | 
|  | 383 | head = kretprobe_inst_table_head(current); | 
|  | 384 |  | 
|  | 385 | /* | 
|  | 386 | * It is possible to have multiple instances associated with a given | 
|  | 387 | * task either because an multiple functions in the call path | 
|  | 388 | * have a return probe installed on them, and/or more then one return | 
|  | 389 | * return probe was registered for a target function. | 
|  | 390 | * | 
|  | 391 | * We can handle this because: | 
|  | 392 | *     - instances are always inserted at the head of the list | 
|  | 393 | *     - when multiple return probes are registered for the same | 
|  | 394 | *	 function, the first instance's ret_addr will point to the | 
|  | 395 | *	 real return address, and all the rest will point to | 
|  | 396 | *	 kretprobe_trampoline | 
|  | 397 | */ | 
|  | 398 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { | 
|  | 399 | if (ri->task != current) | 
|  | 400 | /* another task is sharing our hash bucket */ | 
|  | 401 | continue; | 
|  | 402 |  | 
|  | 403 | if (ri->rp && ri->rp->handler) | 
|  | 404 | ri->rp->handler(ri, regs); | 
|  | 405 |  | 
|  | 406 | orig_ret_address = (unsigned long)ri->ret_addr; | 
| bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 407 | recycle_rp_inst(ri, &empty_rp); | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 408 |  | 
|  | 409 | if (orig_ret_address != trampoline_address) { | 
|  | 410 | /* | 
|  | 411 | * This is the real return address. Any other | 
|  | 412 | * instances associated with this task are for | 
|  | 413 | * other calls deeper on the call stack | 
|  | 414 | */ | 
|  | 415 | break; | 
|  | 416 | } | 
|  | 417 | } | 
| Heiko Carstens | a5a60a2 | 2007-05-21 11:25:22 +0200 | [diff] [blame] | 418 | kretprobe_assert(ri, orig_ret_address, trampoline_address); | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 419 | regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE; | 
|  | 420 |  | 
|  | 421 | reset_current_kprobe(); | 
|  | 422 | spin_unlock_irqrestore(&kretprobe_lock, flags); | 
|  | 423 | preempt_enable_no_resched(); | 
|  | 424 |  | 
| bibo,mao | 99219a3 | 2006-10-02 02:17:35 -0700 | [diff] [blame] | 425 | hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) { | 
|  | 426 | hlist_del(&ri->hlist); | 
|  | 427 | kfree(ri); | 
|  | 428 | } | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 429 | /* | 
|  | 430 | * By returning a non-zero value, we are telling | 
|  | 431 | * kprobe_handler() that we don't want the post_handler | 
|  | 432 | * to run (and have re-enabled preemption) | 
|  | 433 | */ | 
|  | 434 | return 1; | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | /* | 
|  | 438 | * Called after single-stepping.  p->addr is the address of the | 
|  | 439 | * instruction whose first byte has been replaced by the "breakpoint" | 
|  | 440 | * instruction.  To avoid the SMP problems that can occur when we | 
|  | 441 | * temporarily put back the original opcode to single-step, we | 
|  | 442 | * single-stepped a copy of the instruction.  The address of this | 
|  | 443 | * copy is p->ainsn.insn. | 
|  | 444 | */ | 
|  | 445 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) | 
|  | 446 | { | 
|  | 447 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 448 |  | 
|  | 449 | regs->psw.addr &= PSW_ADDR_INSN; | 
|  | 450 |  | 
|  | 451 | if (p->ainsn.fixup & FIXUP_PSW_NORMAL) | 
|  | 452 | regs->psw.addr = (unsigned long)p->addr + | 
|  | 453 | ((unsigned long)regs->psw.addr - | 
|  | 454 | (unsigned long)p->ainsn.insn); | 
|  | 455 |  | 
|  | 456 | if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN) | 
|  | 457 | if ((unsigned long)regs->psw.addr - | 
|  | 458 | (unsigned long)p->ainsn.insn == p->ainsn.ilen) | 
|  | 459 | regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen; | 
|  | 460 |  | 
|  | 461 | if (p->ainsn.fixup & FIXUP_RETURN_REGISTER) | 
|  | 462 | regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr + | 
|  | 463 | (regs->gprs[p->ainsn.reg] - | 
|  | 464 | (unsigned long)p->ainsn.insn)) | 
|  | 465 | | PSW_ADDR_AMODE; | 
|  | 466 |  | 
|  | 467 | regs->psw.addr |= PSW_ADDR_AMODE; | 
|  | 468 | /* turn off PER mode */ | 
|  | 469 | regs->psw.mask &= ~PSW_MASK_PER; | 
|  | 470 | /* Restore the original per control regs */ | 
|  | 471 | __ctl_load(kcb->kprobe_saved_ctl, 9, 11); | 
|  | 472 | regs->psw.mask |= kcb->kprobe_saved_imask; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | 
|  | 476 | { | 
|  | 477 | struct kprobe *cur = kprobe_running(); | 
|  | 478 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 479 |  | 
|  | 480 | if (!cur) | 
|  | 481 | return 0; | 
|  | 482 |  | 
|  | 483 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { | 
|  | 484 | kcb->kprobe_status = KPROBE_HIT_SSDONE; | 
|  | 485 | cur->post_handler(cur, regs, 0); | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | resume_execution(cur, regs); | 
|  | 489 |  | 
|  | 490 | /*Restore back the original saved kprobes variables and continue. */ | 
|  | 491 | if (kcb->kprobe_status == KPROBE_REENTER) { | 
|  | 492 | restore_previous_kprobe(kcb); | 
|  | 493 | goto out; | 
|  | 494 | } | 
|  | 495 | reset_current_kprobe(); | 
|  | 496 | out: | 
|  | 497 | preempt_enable_no_resched(); | 
|  | 498 |  | 
|  | 499 | /* | 
|  | 500 | * if somebody else is singlestepping across a probe point, psw mask | 
|  | 501 | * will have PER set, in which case, continue the remaining processing | 
|  | 502 | * of do_single_step, as if this is not a probe hit. | 
|  | 503 | */ | 
|  | 504 | if (regs->psw.mask & PSW_MASK_PER) { | 
|  | 505 | return 0; | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | return 1; | 
|  | 509 | } | 
|  | 510 |  | 
| Christoph Hellwig | 33464e3 | 2007-05-04 18:47:46 +0200 | [diff] [blame] | 511 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 512 | { | 
|  | 513 | struct kprobe *cur = kprobe_running(); | 
|  | 514 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 515 | const struct exception_table_entry *entry; | 
|  | 516 |  | 
|  | 517 | switch(kcb->kprobe_status) { | 
|  | 518 | case KPROBE_SWAP_INST: | 
|  | 519 | /* We are here because the instruction replacement failed */ | 
|  | 520 | return 0; | 
|  | 521 | case KPROBE_HIT_SS: | 
|  | 522 | case KPROBE_REENTER: | 
|  | 523 | /* | 
|  | 524 | * We are here because the instruction being single | 
|  | 525 | * stepped caused a page fault. We reset the current | 
|  | 526 | * kprobe and the nip points back to the probe address | 
|  | 527 | * and allow the page fault handler to continue as a | 
|  | 528 | * normal page fault. | 
|  | 529 | */ | 
|  | 530 | regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE; | 
|  | 531 | regs->psw.mask &= ~PSW_MASK_PER; | 
|  | 532 | regs->psw.mask |= kcb->kprobe_saved_imask; | 
|  | 533 | if (kcb->kprobe_status == KPROBE_REENTER) | 
|  | 534 | restore_previous_kprobe(kcb); | 
|  | 535 | else | 
|  | 536 | reset_current_kprobe(); | 
|  | 537 | preempt_enable_no_resched(); | 
|  | 538 | break; | 
|  | 539 | case KPROBE_HIT_ACTIVE: | 
|  | 540 | case KPROBE_HIT_SSDONE: | 
|  | 541 | /* | 
|  | 542 | * We increment the nmissed count for accounting, | 
|  | 543 | * we can also use npre/npostfault count for accouting | 
|  | 544 | * these specific fault cases. | 
|  | 545 | */ | 
|  | 546 | kprobes_inc_nmissed_count(cur); | 
|  | 547 |  | 
|  | 548 | /* | 
|  | 549 | * We come here because instructions in the pre/post | 
|  | 550 | * handler caused the page_fault, this could happen | 
|  | 551 | * if handler tries to access user space by | 
|  | 552 | * copy_from_user(), get_user() etc. Let the | 
|  | 553 | * user-specified handler try to fix it first. | 
|  | 554 | */ | 
|  | 555 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | 
|  | 556 | return 1; | 
|  | 557 |  | 
|  | 558 | /* | 
|  | 559 | * In case the user-specified fault handler returned | 
|  | 560 | * zero, try to fix up. | 
|  | 561 | */ | 
|  | 562 | entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN); | 
|  | 563 | if (entry) { | 
|  | 564 | regs->psw.addr = entry->fixup | PSW_ADDR_AMODE; | 
|  | 565 | return 1; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | /* | 
|  | 569 | * fixup_exception() could not handle it, | 
|  | 570 | * Let do_page_fault() fix it. | 
|  | 571 | */ | 
|  | 572 | break; | 
|  | 573 | default: | 
|  | 574 | break; | 
|  | 575 | } | 
|  | 576 | return 0; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | /* | 
|  | 580 | * Wrapper routine to for handling exceptions. | 
|  | 581 | */ | 
|  | 582 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, | 
|  | 583 | unsigned long val, void *data) | 
|  | 584 | { | 
|  | 585 | struct die_args *args = (struct die_args *)data; | 
|  | 586 | int ret = NOTIFY_DONE; | 
|  | 587 |  | 
|  | 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: | 
| Michael Grundy | 4ba069b | 2006-09-20 15:58:39 +0200 | [diff] [blame] | 598 | /* kprobe_running() needs smp_processor_id() */ | 
|  | 599 | preempt_disable(); | 
|  | 600 | if (kprobe_running() && | 
|  | 601 | kprobe_fault_handler(args->regs, args->trapnr)) | 
|  | 602 | ret = NOTIFY_STOP; | 
|  | 603 | preempt_enable(); | 
|  | 604 | break; | 
|  | 605 | default: | 
|  | 606 | break; | 
|  | 607 | } | 
|  | 608 | return ret; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) | 
|  | 612 | { | 
|  | 613 | struct jprobe *jp = container_of(p, struct jprobe, kp); | 
|  | 614 | unsigned long addr; | 
|  | 615 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 616 |  | 
|  | 617 | memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs)); | 
|  | 618 |  | 
|  | 619 | /* setup return addr to the jprobe handler routine */ | 
|  | 620 | regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE; | 
|  | 621 |  | 
|  | 622 | /* r14 is the function return address */ | 
|  | 623 | kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14]; | 
|  | 624 | /* r15 is the stack pointer */ | 
|  | 625 | kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15]; | 
|  | 626 | addr = (unsigned long)kcb->jprobe_saved_r15; | 
|  | 627 |  | 
|  | 628 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr, | 
|  | 629 | MIN_STACK_SIZE(addr)); | 
|  | 630 | return 1; | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | void __kprobes jprobe_return(void) | 
|  | 634 | { | 
|  | 635 | asm volatile(".word 0x0002"); | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | void __kprobes jprobe_return_end(void) | 
|  | 639 | { | 
|  | 640 | asm volatile("bcr 0,0"); | 
|  | 641 | } | 
|  | 642 |  | 
|  | 643 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) | 
|  | 644 | { | 
|  | 645 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); | 
|  | 646 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15); | 
|  | 647 |  | 
|  | 648 | /* Put the regs back */ | 
|  | 649 | memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs)); | 
|  | 650 | /* put the stack back */ | 
|  | 651 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, | 
|  | 652 | MIN_STACK_SIZE(stack_addr)); | 
|  | 653 | preempt_enable_no_resched(); | 
|  | 654 | return 1; | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | static struct kprobe trampoline_p = { | 
|  | 658 | .addr = (kprobe_opcode_t *) & kretprobe_trampoline, | 
|  | 659 | .pre_handler = trampoline_probe_handler | 
|  | 660 | }; | 
|  | 661 |  | 
|  | 662 | int __init arch_init_kprobes(void) | 
|  | 663 | { | 
|  | 664 | return register_kprobe(&trampoline_p); | 
|  | 665 | } | 
| Ananth N Mavinakayanahalli | bf8f6e5b | 2007-05-08 00:34:16 -0700 | [diff] [blame] | 666 |  | 
|  | 667 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) | 
|  | 668 | { | 
|  | 669 | if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline) | 
|  | 670 | return 1; | 
|  | 671 | return 0; | 
|  | 672 | } |