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