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