Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* arch/sparc64/kernel/kprobes.c |
| 2 | * |
| 3 | * Copyright (C) 2004 David S. Miller <davem@davemloft.net> |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/kprobes.h> |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 8 | #include <linux/module.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 9 | #include <linux/kdebug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <asm/signal.h> |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 11 | #include <asm/cacheflush.h> |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 12 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | /* We do not have hardware single-stepping on sparc64. |
| 15 | * So we implement software single-stepping with breakpoint |
| 16 | * traps. The top-level scheme is similar to that used |
| 17 | * in the x86 kprobes implementation. |
| 18 | * |
| 19 | * In the kprobe->ainsn.insn[] array we store the original |
| 20 | * instruction at index zero and a break instruction at |
| 21 | * index one. |
| 22 | * |
| 23 | * When we hit a kprobe we: |
| 24 | * - Run the pre-handler |
| 25 | * - Remember "regs->tnpc" and interrupt level stored in |
| 26 | * "regs->tstate" so we can restore them later |
| 27 | * - Disable PIL interrupts |
| 28 | * - Set regs->tpc to point to kprobe->ainsn.insn[0] |
| 29 | * - Set regs->tnpc to point to kprobe->ainsn.insn[1] |
| 30 | * - Mark that we are actively in a kprobe |
| 31 | * |
| 32 | * At this point we wait for the second breakpoint at |
| 33 | * kprobe->ainsn.insn[1] to hit. When it does we: |
| 34 | * - Run the post-handler |
| 35 | * - Set regs->tpc to "remembered" regs->tnpc stored above, |
| 36 | * restore the PIL interrupt level in "regs->tstate" as well |
| 37 | * - Make any adjustments necessary to regs->tnpc in order |
| 38 | * to handle relative branches correctly. See below. |
| 39 | * - Mark that we are no longer actively in a kprobe. |
| 40 | */ |
| 41 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 42 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 43 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
| 44 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 45 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | p->ainsn.insn[0] = *p->addr; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 48 | flushi(&p->ainsn.insn[0]); |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 51 | flushi(&p->ainsn.insn[1]); |
| 52 | |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 53 | p->opcode = *p->addr; |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 54 | return 0; |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 57 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 58 | { |
| 59 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 60 | flushi(p->addr); |
| 61 | } |
| 62 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 63 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 64 | { |
| 65 | *p->addr = p->opcode; |
| 66 | flushi(p->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 69 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 70 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 71 | kcb->prev_kprobe.kp = kprobe_running(); |
| 72 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 73 | kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc; |
| 74 | kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil; |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 77 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 78 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 79 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 80 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 81 | kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc; |
| 82 | kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil; |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 85 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 86 | struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 88 | __get_cpu_var(current_kprobe) = p; |
| 89 | kcb->kprobe_orig_tnpc = regs->tnpc; |
| 90 | kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 93 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs, |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 94 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 95 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | regs->tstate |= TSTATE_PIL; |
| 97 | |
| 98 | /*single step inline, if it a breakpoint instruction*/ |
| 99 | if (p->opcode == BREAKPOINT_INSTRUCTION) { |
| 100 | regs->tpc = (unsigned long) p->addr; |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 101 | regs->tnpc = kcb->kprobe_orig_tnpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } else { |
| 103 | regs->tpc = (unsigned long) &p->ainsn.insn[0]; |
| 104 | regs->tnpc = (unsigned long) &p->ainsn.insn[1]; |
| 105 | } |
| 106 | } |
| 107 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 108 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | struct kprobe *p; |
| 111 | void *addr = (void *) regs->tpc; |
| 112 | int ret = 0; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 113 | struct kprobe_ctlblk *kcb; |
| 114 | |
| 115 | /* |
| 116 | * We don't want to be preempted for the entire |
| 117 | * duration of kprobe processing |
| 118 | */ |
| 119 | preempt_disable(); |
| 120 | kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (kprobe_running()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | p = get_kprobe(addr); |
| 124 | if (p) { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 125 | if (kcb->kprobe_status == KPROBE_HIT_SS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 127 | kcb->kprobe_orig_tstate_pil); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | goto no_kprobe; |
| 129 | } |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 130 | /* We have reentered the kprobe_handler(), since |
| 131 | * another probe was hit while within the handler. |
| 132 | * We here save the original kprobes variables and |
| 133 | * just single step on the instruction of the new probe |
| 134 | * without calling any user handlers. |
| 135 | */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 136 | save_previous_kprobe(kcb); |
| 137 | set_current_kprobe(p, regs, kcb); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 138 | kprobes_inc_nmissed_count(p); |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 139 | kcb->kprobe_status = KPROBE_REENTER; |
| 140 | prepare_singlestep(p, regs, kcb); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 141 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } else { |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 143 | if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) { |
| 144 | /* The breakpoint instruction was removed by |
| 145 | * another cpu right after we hit, no further |
| 146 | * handling of this interrupt is appropriate |
| 147 | */ |
| 148 | ret = 1; |
| 149 | goto no_kprobe; |
| 150 | } |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 151 | p = __get_cpu_var(current_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (p->break_handler && p->break_handler(p, regs)) |
| 153 | goto ss_probe; |
| 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | goto no_kprobe; |
| 156 | } |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | p = get_kprobe(addr); |
| 159 | if (!p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) { |
| 161 | /* |
| 162 | * The breakpoint instruction was removed right |
| 163 | * after we hit it. Another cpu has removed |
| 164 | * either a probepoint or a debugger breakpoint |
| 165 | * at this address. In either case, no further |
| 166 | * handling of this interrupt is appropriate. |
| 167 | */ |
| 168 | ret = 1; |
| 169 | } |
| 170 | /* Not one of ours: let kernel handle it */ |
| 171 | goto no_kprobe; |
| 172 | } |
| 173 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 174 | set_current_kprobe(p, regs, kcb); |
| 175 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 177 | return 1; |
| 178 | |
| 179 | ss_probe: |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 180 | prepare_singlestep(p, regs, kcb); |
| 181 | kcb->kprobe_status = KPROBE_HIT_SS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return 1; |
| 183 | |
| 184 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 185 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | /* If INSN is a relative control transfer instruction, |
| 190 | * return the corrected branch destination value. |
| 191 | * |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 192 | * regs->tpc and regs->tnpc still hold the values of the |
| 193 | * program counters at the time of trap due to the execution |
| 194 | * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1] |
| 195 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 197 | static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p, |
| 198 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 200 | unsigned long real_pc = (unsigned long) p->addr; |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* Branch not taken, no mods necessary. */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 203 | if (regs->tnpc == regs->tpc + 0x4UL) |
| 204 | return real_pc + 0x8UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* The three cases are call, branch w/prediction, |
| 207 | * and traditional branch. |
| 208 | */ |
| 209 | if ((insn & 0xc0000000) == 0x40000000 || |
| 210 | (insn & 0xc1c00000) == 0x00400000 || |
| 211 | (insn & 0xc1c00000) == 0x00800000) { |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 212 | unsigned long ainsn_addr; |
| 213 | |
| 214 | ainsn_addr = (unsigned long) &p->ainsn.insn[0]; |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* The instruction did all the work for us |
| 217 | * already, just apply the offset to the correct |
| 218 | * instruction location. |
| 219 | */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 220 | return (real_pc + (regs->tnpc - ainsn_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 223 | /* It is jmpl or some other absolute PC modification instruction, |
| 224 | * leave NPC as-is. |
| 225 | */ |
| 226 | return regs->tnpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* If INSN is an instruction which writes it's PC location |
| 230 | * into a destination register, fix that up. |
| 231 | */ |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 232 | static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn, |
| 233 | unsigned long real_pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | unsigned long *slot = NULL; |
| 236 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 237 | /* Simplest case is 'call', which always uses %o7 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if ((insn & 0xc0000000) == 0x40000000) { |
| 239 | slot = ®s->u_regs[UREG_I7]; |
| 240 | } |
| 241 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 242 | /* 'jmpl' encodes the register inside of the opcode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | if ((insn & 0xc1f80000) == 0x81c00000) { |
| 244 | unsigned long rd = ((insn >> 25) & 0x1f); |
| 245 | |
| 246 | if (rd <= 15) { |
| 247 | slot = ®s->u_regs[rd]; |
| 248 | } else { |
| 249 | /* Hard case, it goes onto the stack. */ |
| 250 | flushw_all(); |
| 251 | |
| 252 | rd -= 16; |
| 253 | slot = (unsigned long *) |
| 254 | (regs->u_regs[UREG_FP] + STACK_BIAS); |
| 255 | slot += rd; |
| 256 | } |
| 257 | } |
| 258 | if (slot != NULL) |
| 259 | *slot = real_pc; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Called after single-stepping. p->addr is the address of the |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 264 | * instruction which has been replaced by the breakpoint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | * instruction. To avoid the SMP problems that can occur when we |
| 266 | * temporarily put back the original opcode to single-step, we |
| 267 | * single-stepped a copy of the instruction. The address of this |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 268 | * copy is &p->ainsn.insn[0]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * |
| 270 | * This function prepares to return from the post-single-step |
| 271 | * breakpoint trap. |
| 272 | */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 273 | static void __kprobes resume_execution(struct kprobe *p, |
| 274 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | u32 insn = p->ainsn.insn[0]; |
| 277 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 278 | regs->tnpc = relbranch_fixup(insn, p, regs); |
| 279 | |
| 280 | /* This assignment must occur after relbranch_fixup() */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 281 | regs->tpc = kcb->kprobe_orig_tnpc; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | retpc_fixup(regs, insn, (unsigned long) p->addr); |
| 284 | |
| 285 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 286 | kcb->kprobe_orig_tstate_pil); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 289 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 291 | struct kprobe *cur = kprobe_running(); |
| 292 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 293 | |
| 294 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return 0; |
| 296 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 297 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 298 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 299 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 302 | resume_execution(cur, regs, kcb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 304 | /*Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 305 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 306 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 307 | goto out; |
| 308 | } |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 309 | reset_current_kprobe(); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 310 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | preempt_enable_no_resched(); |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
David S. Miller | 127cda1 | 2007-05-08 18:25:14 -0700 | [diff] [blame] | 316 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 318 | struct kprobe *cur = kprobe_running(); |
| 319 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 320 | const struct exception_table_entry *entry; |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 321 | |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 322 | switch(kcb->kprobe_status) { |
| 323 | case KPROBE_HIT_SS: |
| 324 | case KPROBE_REENTER: |
| 325 | /* |
| 326 | * We are here because the instruction being single |
| 327 | * stepped caused a page fault. We reset the current |
| 328 | * kprobe and the tpc points back to the probe address |
| 329 | * and allow the page fault handler to continue as a |
| 330 | * normal page fault. |
| 331 | */ |
| 332 | regs->tpc = (unsigned long)cur->addr; |
| 333 | regs->tnpc = kcb->kprobe_orig_tnpc; |
| 334 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
| 335 | kcb->kprobe_orig_tstate_pil); |
| 336 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 337 | restore_previous_kprobe(kcb); |
| 338 | else |
| 339 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 341 | break; |
| 342 | case KPROBE_HIT_ACTIVE: |
| 343 | case KPROBE_HIT_SSDONE: |
| 344 | /* |
| 345 | * We increment the nmissed count for accounting, |
| 346 | * we can also use npre/npostfault count for accouting |
| 347 | * these specific fault cases. |
| 348 | */ |
| 349 | kprobes_inc_nmissed_count(cur); |
| 350 | |
| 351 | /* |
| 352 | * We come here because instructions in the pre/post |
| 353 | * handler caused the page_fault, this could happen |
| 354 | * if handler tries to access user space by |
| 355 | * copy_from_user(), get_user() etc. Let the |
| 356 | * user-specified handler try to fix it first. |
| 357 | */ |
| 358 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 359 | return 1; |
| 360 | |
| 361 | /* |
| 362 | * In case the user-specified fault handler returned |
| 363 | * zero, try to fix up. |
| 364 | */ |
| 365 | |
| 366 | entry = search_exception_tables(regs->tpc); |
| 367 | if (entry) { |
| 368 | regs->tpc = entry->fixup; |
| 369 | regs->tnpc = regs->tpc + 4; |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * fixup_exception() could not handle it, |
| 375 | * Let do_page_fault() fix it. |
| 376 | */ |
| 377 | break; |
| 378 | default: |
| 379 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Wrapper routine to for handling exceptions. |
| 387 | */ |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 388 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 389 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
| 391 | struct die_args *args = (struct die_args *)data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 392 | int ret = NOTIFY_DONE; |
| 393 | |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame] | 394 | if (args->regs && user_mode(args->regs)) |
| 395 | return ret; |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | switch (val) { |
| 398 | case DIE_DEBUG: |
| 399 | if (kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 400 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | break; |
| 402 | case DIE_DEBUG_2: |
| 403 | if (post_kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 404 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | default: |
| 407 | break; |
| 408 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 409 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 412 | asmlinkage void __kprobes kprobe_trap(unsigned long trap_level, |
| 413 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | BUG_ON(trap_level != 0x170 && trap_level != 0x171); |
| 416 | |
| 417 | if (user_mode(regs)) { |
| 418 | local_irq_enable(); |
| 419 | bad_trap(regs, trap_level); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | /* trap_level == 0x170 --> ta 0x70 |
| 424 | * trap_level == 0x171 --> ta 0x71 |
| 425 | */ |
| 426 | if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2, |
| 427 | (trap_level == 0x170) ? "debug" : "debug_2", |
| 428 | regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP) |
| 429 | bad_trap(regs, trap_level); |
| 430 | } |
| 431 | |
| 432 | /* Jprobes support. */ |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 433 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 436 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 438 | memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | regs->tpc = (unsigned long) jp->entry; |
| 441 | regs->tnpc = ((unsigned long) jp->entry) + 0x4UL; |
| 442 | regs->tstate |= TSTATE_PIL; |
| 443 | |
| 444 | return 1; |
| 445 | } |
| 446 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 447 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 449 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 450 | register unsigned long orig_fp asm("g1"); |
| 451 | |
| 452 | orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP]; |
| 453 | __asm__ __volatile__("\n" |
| 454 | "1: cmp %%sp, %0\n\t" |
| 455 | "blu,a,pt %%xcc, 1b\n\t" |
| 456 | " restore\n\t" |
| 457 | ".globl jprobe_return_trap_instruction\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | "jprobe_return_trap_instruction:\n\t" |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 459 | "ta 0x70" |
| 460 | : /* no outputs */ |
| 461 | : "r" (orig_fp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | extern void jprobe_return_trap_instruction(void); |
| 465 | |
| 466 | extern void __show_regs(struct pt_regs * regs); |
| 467 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 468 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | u32 *addr = (u32 *) regs->tpc; |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 471 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | if (addr == (u32 *) jprobe_return_trap_instruction) { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 474 | memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 475 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | return 1; |
| 477 | } |
| 478 | return 0; |
| 479 | } |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 480 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 481 | /* architecture specific initialization */ |
| 482 | int arch_init_kprobes(void) |
| 483 | { |
| 484 | return 0; |
| 485 | } |