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