Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* arch/sparc64/kernel/kprobes.c |
| 3 | * |
| 4 | * Copyright (C) 2004 David S. Miller <davem@davemloft.net> |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/kprobes.h> |
Paul Gortmaker | cdd4f4c | 2016-09-19 17:36:29 -0400 | [diff] [blame] | 9 | #include <linux/extable.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 10 | #include <linux/kdebug.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 12 | #include <linux/context_tracking.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/signal.h> |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 14 | #include <asm/cacheflush.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 15 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* We do not have hardware single-stepping on sparc64. |
| 18 | * So we implement software single-stepping with breakpoint |
| 19 | * traps. The top-level scheme is similar to that used |
| 20 | * in the x86 kprobes implementation. |
| 21 | * |
| 22 | * In the kprobe->ainsn.insn[] array we store the original |
| 23 | * instruction at index zero and a break instruction at |
| 24 | * index one. |
| 25 | * |
| 26 | * When we hit a kprobe we: |
| 27 | * - Run the pre-handler |
| 28 | * - Remember "regs->tnpc" and interrupt level stored in |
| 29 | * "regs->tstate" so we can restore them later |
| 30 | * - Disable PIL interrupts |
| 31 | * - Set regs->tpc to point to kprobe->ainsn.insn[0] |
| 32 | * - Set regs->tnpc to point to kprobe->ainsn.insn[1] |
| 33 | * - Mark that we are actively in a kprobe |
| 34 | * |
| 35 | * At this point we wait for the second breakpoint at |
| 36 | * kprobe->ainsn.insn[1] to hit. When it does we: |
| 37 | * - Run the post-handler |
| 38 | * - Set regs->tpc to "remembered" regs->tnpc stored above, |
| 39 | * restore the PIL interrupt level in "regs->tstate" as well |
| 40 | * - Make any adjustments necessary to regs->tnpc in order |
| 41 | * to handle relative branches correctly. See below. |
| 42 | * - Mark that we are no longer actively in a kprobe. |
| 43 | */ |
| 44 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 45 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 46 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
| 47 | |
Masami Hiramatsu | f438d91 | 2007-10-16 01:27:49 -0700 | [diff] [blame] | 48 | struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}}; |
| 49 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 50 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
David S. Miller | 936cf25 | 2009-12-09 23:57:24 -0800 | [diff] [blame] | 52 | if ((unsigned long) p->addr & 0x3UL) |
| 53 | return -EILSEQ; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | p->ainsn.insn[0] = *p->addr; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 56 | flushi(&p->ainsn.insn[0]); |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 59 | flushi(&p->ainsn.insn[1]); |
| 60 | |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 61 | p->opcode = *p->addr; |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 62 | return 0; |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 65 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 66 | { |
| 67 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 68 | flushi(p->addr); |
| 69 | } |
| 70 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 71 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 72 | { |
| 73 | *p->addr = p->opcode; |
| 74 | flushi(p->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 77 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 78 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 79 | kcb->prev_kprobe.kp = kprobe_running(); |
| 80 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 81 | kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc; |
| 82 | kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil; |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 85 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 86 | { |
Christoph Lameter | 494fc42 | 2014-08-17 12:30:54 -0500 | [diff] [blame] | 87 | __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 88 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 89 | kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc; |
| 90 | kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil; |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 93 | static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 94 | struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Christoph Lameter | 494fc42 | 2014-08-17 12:30:54 -0500 | [diff] [blame] | 96 | __this_cpu_write(current_kprobe, p); |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 97 | kcb->kprobe_orig_tnpc = regs->tnpc; |
| 98 | kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 101 | 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] | 102 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 103 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | regs->tstate |= TSTATE_PIL; |
| 105 | |
| 106 | /*single step inline, if it a breakpoint instruction*/ |
| 107 | if (p->opcode == BREAKPOINT_INSTRUCTION) { |
| 108 | regs->tpc = (unsigned long) p->addr; |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 109 | regs->tnpc = kcb->kprobe_orig_tnpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } else { |
| 111 | regs->tpc = (unsigned long) &p->ainsn.insn[0]; |
| 112 | regs->tnpc = (unsigned long) &p->ainsn.insn[1]; |
| 113 | } |
| 114 | } |
| 115 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 116 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct kprobe *p; |
| 119 | void *addr = (void *) regs->tpc; |
| 120 | int ret = 0; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 121 | struct kprobe_ctlblk *kcb; |
| 122 | |
| 123 | /* |
| 124 | * We don't want to be preempted for the entire |
| 125 | * duration of kprobe processing |
| 126 | */ |
| 127 | preempt_disable(); |
| 128 | kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (kprobe_running()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | p = get_kprobe(addr); |
| 132 | if (p) { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 133 | if (kcb->kprobe_status == KPROBE_HIT_SS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 135 | kcb->kprobe_orig_tstate_pil); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | goto no_kprobe; |
| 137 | } |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 138 | /* We have reentered the kprobe_handler(), since |
| 139 | * another probe was hit while within the handler. |
| 140 | * We here save the original kprobes variables and |
| 141 | * just single step on the instruction of the new probe |
| 142 | * without calling any user handlers. |
| 143 | */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 144 | save_previous_kprobe(kcb); |
| 145 | set_current_kprobe(p, regs, kcb); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 146 | kprobes_inc_nmissed_count(p); |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 147 | kcb->kprobe_status = KPROBE_REENTER; |
| 148 | prepare_singlestep(p, regs, kcb); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 149 | return 1; |
Masami Hiramatsu | d5ad85b | 2018-06-20 01:15:17 +0900 | [diff] [blame] | 150 | } else if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) { |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 151 | /* The breakpoint instruction was removed by |
| 152 | * another cpu right after we hit, no further |
| 153 | * handling of this interrupt is appropriate |
| 154 | */ |
Masami Hiramatsu | d5ad85b | 2018-06-20 01:15:17 +0900 | [diff] [blame] | 155 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Masami Hiramatsu | cce188b | 2018-06-20 01:15:45 +0900 | [diff] [blame] | 178 | if (p->pre_handler && p->pre_handler(p, regs)) { |
| 179 | reset_current_kprobe(); |
| 180 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return 1; |
Masami Hiramatsu | cce188b | 2018-06-20 01:15:45 +0900 | [diff] [blame] | 182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 184 | prepare_singlestep(p, regs, kcb); |
| 185 | kcb->kprobe_status = KPROBE_HIT_SS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return 1; |
| 187 | |
| 188 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 189 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | /* If INSN is a relative control transfer instruction, |
| 194 | * return the corrected branch destination value. |
| 195 | * |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 196 | * regs->tpc and regs->tnpc still hold the values of the |
| 197 | * program counters at the time of trap due to the execution |
| 198 | * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1] |
| 199 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 201 | static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p, |
| 202 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 204 | unsigned long real_pc = (unsigned long) p->addr; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* Branch not taken, no mods necessary. */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 207 | if (regs->tnpc == regs->tpc + 0x4UL) |
| 208 | return real_pc + 0x8UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* The three cases are call, branch w/prediction, |
| 211 | * and traditional branch. |
| 212 | */ |
| 213 | if ((insn & 0xc0000000) == 0x40000000 || |
| 214 | (insn & 0xc1c00000) == 0x00400000 || |
| 215 | (insn & 0xc1c00000) == 0x00800000) { |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 216 | unsigned long ainsn_addr; |
| 217 | |
| 218 | ainsn_addr = (unsigned long) &p->ainsn.insn[0]; |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* The instruction did all the work for us |
| 221 | * already, just apply the offset to the correct |
| 222 | * instruction location. |
| 223 | */ |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 224 | return (real_pc + (regs->tnpc - ainsn_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 227 | /* It is jmpl or some other absolute PC modification instruction, |
| 228 | * leave NPC as-is. |
| 229 | */ |
| 230 | return regs->tnpc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* If INSN is an instruction which writes it's PC location |
| 234 | * into a destination register, fix that up. |
| 235 | */ |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 236 | static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn, |
| 237 | unsigned long real_pc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | unsigned long *slot = NULL; |
| 240 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 241 | /* Simplest case is 'call', which always uses %o7 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if ((insn & 0xc0000000) == 0x40000000) { |
| 243 | slot = ®s->u_regs[UREG_I7]; |
| 244 | } |
| 245 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 246 | /* 'jmpl' encodes the register inside of the opcode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if ((insn & 0xc1f80000) == 0x81c00000) { |
| 248 | unsigned long rd = ((insn >> 25) & 0x1f); |
| 249 | |
| 250 | if (rd <= 15) { |
| 251 | slot = ®s->u_regs[rd]; |
| 252 | } else { |
| 253 | /* Hard case, it goes onto the stack. */ |
| 254 | flushw_all(); |
| 255 | |
| 256 | rd -= 16; |
| 257 | slot = (unsigned long *) |
| 258 | (regs->u_regs[UREG_FP] + STACK_BIAS); |
| 259 | slot += rd; |
| 260 | } |
| 261 | } |
| 262 | if (slot != NULL) |
| 263 | *slot = real_pc; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Called after single-stepping. p->addr is the address of the |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 268 | * instruction which has been replaced by the breakpoint |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * instruction. To avoid the SMP problems that can occur when we |
| 270 | * temporarily put back the original opcode to single-step, we |
| 271 | * single-stepped a copy of the instruction. The address of this |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 272 | * copy is &p->ainsn.insn[0]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | * |
| 274 | * This function prepares to return from the post-single-step |
| 275 | * breakpoint trap. |
| 276 | */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 277 | static void __kprobes resume_execution(struct kprobe *p, |
| 278 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | u32 insn = p->ainsn.insn[0]; |
| 281 | |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 282 | regs->tnpc = relbranch_fixup(insn, p, regs); |
| 283 | |
| 284 | /* This assignment must occur after relbranch_fixup() */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 285 | regs->tpc = kcb->kprobe_orig_tnpc; |
David S. Miller | f088258 | 2006-12-10 02:42:03 -0800 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | retpc_fixup(regs, insn, (unsigned long) p->addr); |
| 288 | |
| 289 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 290 | kcb->kprobe_orig_tstate_pil); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Prasanna S Panchamukhi | 07fab8d | 2006-04-18 22:22:03 -0700 | [diff] [blame] | 293 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 301 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 302 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 303 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 306 | resume_execution(cur, regs, kcb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 308 | /*Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 309 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 310 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 311 | goto out; |
| 312 | } |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 313 | reset_current_kprobe(); |
Prasanna S Panchamukhi | e539c23 | 2005-06-23 00:09:39 -0700 | [diff] [blame] | 314 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | preempt_enable_no_resched(); |
| 316 | |
| 317 | return 1; |
| 318 | } |
| 319 | |
David S. Miller | 127cda1 | 2007-05-08 18:25:14 -0700 | [diff] [blame] | 320 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 322 | struct kprobe *cur = kprobe_running(); |
| 323 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 324 | const struct exception_table_entry *entry; |
Ananth N Mavinakayanahalli | f215d98 | 2005-11-07 01:00:11 -0800 | [diff] [blame] | 325 | |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 326 | switch(kcb->kprobe_status) { |
| 327 | case KPROBE_HIT_SS: |
| 328 | case KPROBE_REENTER: |
| 329 | /* |
| 330 | * We are here because the instruction being single |
| 331 | * stepped caused a page fault. We reset the current |
| 332 | * kprobe and the tpc points back to the probe address |
| 333 | * and allow the page fault handler to continue as a |
| 334 | * normal page fault. |
| 335 | */ |
| 336 | regs->tpc = (unsigned long)cur->addr; |
| 337 | regs->tnpc = kcb->kprobe_orig_tnpc; |
| 338 | regs->tstate = ((regs->tstate & ~TSTATE_PIL) | |
| 339 | kcb->kprobe_orig_tstate_pil); |
| 340 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 341 | restore_previous_kprobe(kcb); |
| 342 | else |
| 343 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | preempt_enable_no_resched(); |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 345 | break; |
| 346 | case KPROBE_HIT_ACTIVE: |
| 347 | case KPROBE_HIT_SSDONE: |
| 348 | /* |
Prasanna S Panchamukhi | b670009 | 2006-03-26 01:38:26 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | default: |
| 394 | break; |
| 395 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 396 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Prasanna S Panchamukhi | 05e14cb | 2005-09-06 15:19:30 -0700 | [diff] [blame] | 399 | asmlinkage void __kprobes kprobe_trap(unsigned long trap_level, |
| 400 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 402 | enum ctx_state prev_state = exception_enter(); |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | BUG_ON(trap_level != 0x170 && trap_level != 0x171); |
| 405 | |
| 406 | if (user_mode(regs)) { |
| 407 | local_irq_enable(); |
| 408 | bad_trap(regs, trap_level); |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 409 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* trap_level == 0x170 --> ta 0x70 |
| 413 | * trap_level == 0x171 --> ta 0x71 |
| 414 | */ |
| 415 | if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2, |
| 416 | (trap_level == 0x170) ? "debug" : "debug_2", |
| 417 | regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP) |
| 418 | bad_trap(regs, trap_level); |
Kirill Tkhai | 812cb83 | 2013-09-14 16:02:11 +0400 | [diff] [blame] | 419 | out: |
| 420 | exception_exit(prev_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Srinivasa D S | ef53d9c | 2008-07-25 01:46:04 -0700 | [diff] [blame] | 423 | /* The value stored in the return address register is actually 2 |
| 424 | * instructions before where the callee will return to. |
| 425 | * Sequences usually look something like this |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 426 | * |
| 427 | * call some_function <--- return register points here |
| 428 | * nop <--- call delay slot |
| 429 | * whatever <--- where callee returns to |
| 430 | * |
| 431 | * To keep trampoline_probe_handler logic simpler, we normalize the |
| 432 | * value kept in ri->ret_addr so we don't need to keep adjusting it |
| 433 | * back and forth. |
| 434 | */ |
| 435 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
| 436 | struct pt_regs *regs) |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 437 | { |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 438 | ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8); |
Masami Hiramatsu | 5e96ce8 | 2020-08-29 22:02:25 +0900 | [diff] [blame] | 439 | ri->fp = NULL; |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 440 | |
| 441 | /* Replace the return addr with trampoline addr */ |
| 442 | regs->u_regs[UREG_RETPC] = |
Masami Hiramatsu | adf8a61 | 2021-09-14 23:40:54 +0900 | [diff] [blame] | 443 | ((unsigned long)__kretprobe_trampoline) - 8; |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Called when the probe at kretprobe trampoline is hit |
| 448 | */ |
Sam Ravnborg | 2f827ea | 2014-05-16 23:26:03 +0200 | [diff] [blame] | 449 | static int __kprobes trampoline_probe_handler(struct kprobe *p, |
| 450 | struct pt_regs *regs) |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 451 | { |
Masami Hiramatsu | 5e96ce8 | 2020-08-29 22:02:25 +0900 | [diff] [blame] | 452 | unsigned long orig_ret_address = 0; |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 453 | |
Masami Hiramatsu | 96fed8a | 2021-09-14 23:40:45 +0900 | [diff] [blame] | 454 | orig_ret_address = __kretprobe_trampoline_handler(regs, NULL); |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 455 | regs->tpc = orig_ret_address; |
| 456 | regs->tnpc = orig_ret_address + 4; |
| 457 | |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 458 | /* |
| 459 | * By returning a non-zero value, we are telling |
| 460 | * kprobe_handler() that we don't want the post_handler |
| 461 | * to run (and have re-enabled preemption) |
| 462 | */ |
| 463 | return 1; |
| 464 | } |
| 465 | |
Sam Ravnborg | 2f827ea | 2014-05-16 23:26:03 +0200 | [diff] [blame] | 466 | static void __used kretprobe_trampoline_holder(void) |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 467 | { |
Masami Hiramatsu | adf8a61 | 2021-09-14 23:40:54 +0900 | [diff] [blame] | 468 | asm volatile(".global __kretprobe_trampoline\n" |
| 469 | "__kretprobe_trampoline:\n" |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 470 | "\tnop\n" |
| 471 | "\tnop\n"); |
| 472 | } |
| 473 | static struct kprobe trampoline_p = { |
Masami Hiramatsu | adf8a61 | 2021-09-14 23:40:54 +0900 | [diff] [blame] | 474 | .addr = (kprobe_opcode_t *) &__kretprobe_trampoline, |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 475 | .pre_handler = trampoline_probe_handler |
| 476 | }; |
| 477 | |
| 478 | int __init arch_init_kprobes(void) |
| 479 | { |
| 480 | return register_kprobe(&trampoline_p); |
| 481 | } |
| 482 | |
| 483 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 484 | { |
Masami Hiramatsu | adf8a61 | 2021-09-14 23:40:54 +0900 | [diff] [blame] | 485 | if (p->addr == (kprobe_opcode_t *)&__kretprobe_trampoline) |
David S. Miller | d38f122 | 2008-02-09 03:40:55 -0800 | [diff] [blame] | 486 | return 1; |
| 487 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 488 | return 0; |
| 489 | } |