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