blob: 2d13a4fc038489c7cee8068bc19c6c4419219e0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* arch/sparc64/kernel/kprobes.c
2 *
3 * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/kernel.h>
7#include <linux/kprobes.h>
Paul Gortmakercdd4f4c2016-09-19 17:36:29 -04008#include <linux/extable.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07009#include <linux/kdebug.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Kirill Tkhai812cb832013-09-14 16:02:11 +040011#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/signal.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070013#include <asm/cacheflush.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080014#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/* We do not have hardware single-stepping on sparc64.
17 * So we implement software single-stepping with breakpoint
18 * traps. The top-level scheme is similar to that used
19 * in the x86 kprobes implementation.
20 *
21 * In the kprobe->ainsn.insn[] array we store the original
22 * instruction at index zero and a break instruction at
23 * index one.
24 *
25 * When we hit a kprobe we:
26 * - Run the pre-handler
27 * - Remember "regs->tnpc" and interrupt level stored in
28 * "regs->tstate" so we can restore them later
29 * - Disable PIL interrupts
30 * - Set regs->tpc to point to kprobe->ainsn.insn[0]
31 * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
32 * - Mark that we are actively in a kprobe
33 *
34 * At this point we wait for the second breakpoint at
35 * kprobe->ainsn.insn[1] to hit. When it does we:
36 * - Run the post-handler
37 * - Set regs->tpc to "remembered" regs->tnpc stored above,
38 * restore the PIL interrupt level in "regs->tstate" as well
39 * - Make any adjustments necessary to regs->tnpc in order
40 * to handle relative branches correctly. See below.
41 * - Mark that we are no longer actively in a kprobe.
42 */
43
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080044DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
45DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
46
Masami Hiramatsuf438d912007-10-16 01:27:49 -070047struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
48
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070049int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
David S. Miller936cf252009-12-09 23:57:24 -080051 if ((unsigned long) p->addr & 0x3UL)
52 return -EILSEQ;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 p->ainsn.insn[0] = *p->addr;
David S. Millerf08825892006-12-10 02:42:03 -080055 flushi(&p->ainsn.insn[0]);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
David S. Millerf08825892006-12-10 02:42:03 -080058 flushi(&p->ainsn.insn[1]);
59
Rusty Lynch7e1048b2005-06-23 00:09:25 -070060 p->opcode = *p->addr;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -080061 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070062}
63
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070064void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070065{
66 *p->addr = BREAKPOINT_INSTRUCTION;
67 flushi(p->addr);
68}
69
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070070void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070071{
72 *p->addr = p->opcode;
73 flushi(p->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070076static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070077{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080078 kcb->prev_kprobe.kp = kprobe_running();
79 kcb->prev_kprobe.status = kcb->kprobe_status;
80 kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
81 kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070082}
83
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070084static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070085{
Christoph Lameter494fc422014-08-17 12:30:54 -050086 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080087 kcb->kprobe_status = kcb->prev_kprobe.status;
88 kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
89 kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070090}
91
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -070092static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080093 struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Christoph Lameter494fc422014-08-17 12:30:54 -050095 __this_cpu_write(current_kprobe, p);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -080096 kcb->kprobe_orig_tnpc = regs->tnpc;
97 kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -070098}
99
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -0700100static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800101 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 regs->tstate |= TSTATE_PIL;
104
105 /*single step inline, if it a breakpoint instruction*/
106 if (p->opcode == BREAKPOINT_INSTRUCTION) {
107 regs->tpc = (unsigned long) p->addr;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800108 regs->tnpc = kcb->kprobe_orig_tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 } else {
110 regs->tpc = (unsigned long) &p->ainsn.insn[0];
111 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
112 }
113}
114
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700115static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct kprobe *p;
118 void *addr = (void *) regs->tpc;
119 int ret = 0;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800120 struct kprobe_ctlblk *kcb;
121
122 /*
123 * We don't want to be preempted for the entire
124 * duration of kprobe processing
125 */
126 preempt_disable();
127 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 p = get_kprobe(addr);
131 if (p) {
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800132 if (kcb->kprobe_status == KPROBE_HIT_SS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800134 kcb->kprobe_orig_tstate_pil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto no_kprobe;
136 }
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700137 /* We have reentered the kprobe_handler(), since
138 * another probe was hit while within the handler.
139 * We here save the original kprobes variables and
140 * just single step on the instruction of the new probe
141 * without calling any user handlers.
142 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800143 save_previous_kprobe(kcb);
144 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800145 kprobes_inc_nmissed_count(p);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800146 kcb->kprobe_status = KPROBE_REENTER;
147 prepare_singlestep(p, regs, kcb);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700148 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800150 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
151 /* The breakpoint instruction was removed by
152 * another cpu right after we hit, no further
153 * handling of this interrupt is appropriate
154 */
155 ret = 1;
156 goto no_kprobe;
157 }
Christoph Lameter494fc422014-08-17 12:30:54 -0500158 p = __this_cpu_read(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (p->break_handler && p->break_handler(p, regs))
160 goto ss_probe;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 goto no_kprobe;
163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 p = get_kprobe(addr);
166 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
168 /*
169 * The breakpoint instruction was removed right
170 * after we hit it. Another cpu has removed
171 * either a probepoint or a debugger breakpoint
172 * at this address. In either case, no further
173 * handling of this interrupt is appropriate.
174 */
175 ret = 1;
176 }
177 /* Not one of ours: let kernel handle it */
178 goto no_kprobe;
179 }
180
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800181 set_current_kprobe(p, regs, kcb);
182 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (p->pre_handler && p->pre_handler(p, regs))
184 return 1;
185
186ss_probe:
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800187 prepare_singlestep(p, regs, kcb);
188 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 1;
190
191no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800192 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return ret;
194}
195
196/* If INSN is a relative control transfer instruction,
197 * return the corrected branch destination value.
198 *
David S. Millerf08825892006-12-10 02:42:03 -0800199 * regs->tpc and regs->tnpc still hold the values of the
200 * program counters at the time of trap due to the execution
201 * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1]
202 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
David S. Millerf08825892006-12-10 02:42:03 -0800204static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
205 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
David S. Millerf08825892006-12-10 02:42:03 -0800207 unsigned long real_pc = (unsigned long) p->addr;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Branch not taken, no mods necessary. */
David S. Millerf08825892006-12-10 02:42:03 -0800210 if (regs->tnpc == regs->tpc + 0x4UL)
211 return real_pc + 0x8UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* The three cases are call, branch w/prediction,
214 * and traditional branch.
215 */
216 if ((insn & 0xc0000000) == 0x40000000 ||
217 (insn & 0xc1c00000) == 0x00400000 ||
218 (insn & 0xc1c00000) == 0x00800000) {
David S. Millerf08825892006-12-10 02:42:03 -0800219 unsigned long ainsn_addr;
220
221 ainsn_addr = (unsigned long) &p->ainsn.insn[0];
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* The instruction did all the work for us
224 * already, just apply the offset to the correct
225 * instruction location.
226 */
David S. Millerf08825892006-12-10 02:42:03 -0800227 return (real_pc + (regs->tnpc - ainsn_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
David S. Millerf08825892006-12-10 02:42:03 -0800230 /* It is jmpl or some other absolute PC modification instruction,
231 * leave NPC as-is.
232 */
233 return regs->tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/* If INSN is an instruction which writes it's PC location
237 * into a destination register, fix that up.
238 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700239static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
240 unsigned long real_pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 unsigned long *slot = NULL;
243
David S. Millerf08825892006-12-10 02:42:03 -0800244 /* Simplest case is 'call', which always uses %o7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if ((insn & 0xc0000000) == 0x40000000) {
246 slot = &regs->u_regs[UREG_I7];
247 }
248
David S. Millerf08825892006-12-10 02:42:03 -0800249 /* 'jmpl' encodes the register inside of the opcode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if ((insn & 0xc1f80000) == 0x81c00000) {
251 unsigned long rd = ((insn >> 25) & 0x1f);
252
253 if (rd <= 15) {
254 slot = &regs->u_regs[rd];
255 } else {
256 /* Hard case, it goes onto the stack. */
257 flushw_all();
258
259 rd -= 16;
260 slot = (unsigned long *)
261 (regs->u_regs[UREG_FP] + STACK_BIAS);
262 slot += rd;
263 }
264 }
265 if (slot != NULL)
266 *slot = real_pc;
267}
268
269/*
270 * Called after single-stepping. p->addr is the address of the
David S. Millerf08825892006-12-10 02:42:03 -0800271 * instruction which has been replaced by the breakpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * instruction. To avoid the SMP problems that can occur when we
273 * temporarily put back the original opcode to single-step, we
274 * single-stepped a copy of the instruction. The address of this
David S. Millerf08825892006-12-10 02:42:03 -0800275 * copy is &p->ainsn.insn[0].
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
277 * This function prepares to return from the post-single-step
278 * breakpoint trap.
279 */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800280static void __kprobes resume_execution(struct kprobe *p,
281 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 u32 insn = p->ainsn.insn[0];
284
David S. Millerf08825892006-12-10 02:42:03 -0800285 regs->tnpc = relbranch_fixup(insn, p, regs);
286
287 /* This assignment must occur after relbranch_fixup() */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800288 regs->tpc = kcb->kprobe_orig_tnpc;
David S. Millerf08825892006-12-10 02:42:03 -0800289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 retpc_fixup(regs, insn, (unsigned long) p->addr);
291
292 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800293 kcb->kprobe_orig_tstate_pil);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Prasanna S Panchamukhi07fab8d2006-04-18 22:22:03 -0700296static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800298 struct kprobe *cur = kprobe_running();
299 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
300
301 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800304 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
305 kcb->kprobe_status = KPROBE_HIT_SSDONE;
306 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800309 resume_execution(cur, regs, kcb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700311 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800312 if (kcb->kprobe_status == KPROBE_REENTER) {
313 restore_previous_kprobe(kcb);
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700314 goto out;
315 }
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800316 reset_current_kprobe();
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700317out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 preempt_enable_no_resched();
319
320 return 1;
321}
322
David S. Miller127cda12007-05-08 18:25:14 -0700323int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800325 struct kprobe *cur = kprobe_running();
326 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800327 const struct exception_table_entry *entry;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800328
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800329 switch(kcb->kprobe_status) {
330 case KPROBE_HIT_SS:
331 case KPROBE_REENTER:
332 /*
333 * We are here because the instruction being single
334 * stepped caused a page fault. We reset the current
335 * kprobe and the tpc points back to the probe address
336 * and allow the page fault handler to continue as a
337 * normal page fault.
338 */
339 regs->tpc = (unsigned long)cur->addr;
340 regs->tnpc = kcb->kprobe_orig_tnpc;
341 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
342 kcb->kprobe_orig_tstate_pil);
343 if (kcb->kprobe_status == KPROBE_REENTER)
344 restore_previous_kprobe(kcb);
345 else
346 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 preempt_enable_no_resched();
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800348 break;
349 case KPROBE_HIT_ACTIVE:
350 case KPROBE_HIT_SSDONE:
351 /*
352 * We increment the nmissed count for accounting,
Anoop Thomas Mathew23d6d3d2013-09-20 09:25:41 +0530353 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800354 * these specific fault cases.
355 */
356 kprobes_inc_nmissed_count(cur);
357
358 /*
359 * We come here because instructions in the pre/post
360 * handler caused the page_fault, this could happen
361 * if handler tries to access user space by
362 * copy_from_user(), get_user() etc. Let the
363 * user-specified handler try to fix it first.
364 */
365 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
366 return 1;
367
368 /*
369 * In case the user-specified fault handler returned
370 * zero, try to fix up.
371 */
372
373 entry = search_exception_tables(regs->tpc);
374 if (entry) {
375 regs->tpc = entry->fixup;
376 regs->tnpc = regs->tpc + 4;
377 return 1;
378 }
379
380 /*
381 * fixup_exception() could not handle it,
382 * Let do_page_fault() fix it.
383 */
384 break;
385 default:
386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Prasanna S Panchamukhib6700092006-03-26 01:38:26 -0800388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
392/*
393 * Wrapper routine to for handling exceptions.
394 */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700395int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
396 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800399 int ret = NOTIFY_DONE;
400
bibo,mao2326c772006-03-26 01:38:21 -0800401 if (args->regs && user_mode(args->regs))
402 return ret;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 switch (val) {
405 case DIE_DEBUG:
406 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800407 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409 case DIE_DEBUG_2:
410 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800411 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 default:
414 break;
415 }
Ananth N Mavinakayanahalli66ff2d062005-11-07 01:00:07 -0800416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700419asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
420 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Kirill Tkhai812cb832013-09-14 16:02:11 +0400422 enum ctx_state prev_state = exception_enter();
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
425
426 if (user_mode(regs)) {
427 local_irq_enable();
428 bad_trap(regs, trap_level);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 /* trap_level == 0x170 --> ta 0x70
433 * trap_level == 0x171 --> ta 0x71
434 */
435 if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
436 (trap_level == 0x170) ? "debug" : "debug_2",
437 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
438 bad_trap(regs, trap_level);
Kirill Tkhai812cb832013-09-14 16:02:11 +0400439out:
440 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/* Jprobes support. */
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700444int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct jprobe *jp = container_of(p, struct jprobe, kp);
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800447 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800449 memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 regs->tpc = (unsigned long) jp->entry;
452 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
453 regs->tstate |= TSTATE_PIL;
454
455 return 1;
456}
457
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700458void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David S. Millerf08825892006-12-10 02:42:03 -0800460 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
461 register unsigned long orig_fp asm("g1");
462
463 orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
464 __asm__ __volatile__("\n"
465"1: cmp %%sp, %0\n\t"
466 "blu,a,pt %%xcc, 1b\n\t"
467 " restore\n\t"
468 ".globl jprobe_return_trap_instruction\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469"jprobe_return_trap_instruction:\n\t"
David S. Millerf08825892006-12-10 02:42:03 -0800470 "ta 0x70"
471 : /* no outputs */
472 : "r" (orig_fp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475extern void jprobe_return_trap_instruction(void);
476
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700477int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 u32 *addr = (u32 *) regs->tpc;
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800480 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (addr == (u32 *) jprobe_return_trap_instruction) {
Ananth N Mavinakayanahallif215d982005-11-07 01:00:11 -0800483 memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800484 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 1;
486 }
487 return 0;
488}
Prasanna S Panchamukhie539c232005-06-23 00:09:39 -0700489
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700490/* The value stored in the return address register is actually 2
491 * instructions before where the callee will return to.
492 * Sequences usually look something like this
David S. Millerd38f1222008-02-09 03:40:55 -0800493 *
494 * call some_function <--- return register points here
495 * nop <--- call delay slot
496 * whatever <--- where callee returns to
497 *
498 * To keep trampoline_probe_handler logic simpler, we normalize the
499 * value kept in ri->ret_addr so we don't need to keep adjusting it
500 * back and forth.
501 */
502void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
503 struct pt_regs *regs)
Rusty Lynch67729262005-07-05 18:54:50 -0700504{
David S. Millerd38f1222008-02-09 03:40:55 -0800505 ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
506
507 /* Replace the return addr with trampoline addr */
508 regs->u_regs[UREG_RETPC] =
509 ((unsigned long)kretprobe_trampoline) - 8;
510}
511
512/*
513 * Called when the probe at kretprobe trampoline is hit
514 */
Sam Ravnborg2f827ea2014-05-16 23:26:03 +0200515static int __kprobes trampoline_probe_handler(struct kprobe *p,
516 struct pt_regs *regs)
David S. Millerd38f1222008-02-09 03:40:55 -0800517{
518 struct kretprobe_instance *ri = NULL;
519 struct hlist_head *head, empty_rp;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800520 struct hlist_node *tmp;
David S. Millerd38f1222008-02-09 03:40:55 -0800521 unsigned long flags, orig_ret_address = 0;
522 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
523
524 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700525 kretprobe_hash_lock(current, &head, &flags);
David S. Millerd38f1222008-02-09 03:40:55 -0800526
527 /*
528 * It is possible to have multiple instances associated with a given
529 * task either because an multiple functions in the call path
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200530 * have a return probe installed on them, and/or more than one return
David S. Millerd38f1222008-02-09 03:40:55 -0800531 * return probe was registered for a target function.
532 *
533 * We can handle this because:
534 * - instances are always inserted at the head of the list
535 * - when multiple return probes are registered for the same
536 * function, the first instance's ret_addr will point to the
537 * real return address, and all the rest will point to
538 * kretprobe_trampoline
539 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800540 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
David S. Millerd38f1222008-02-09 03:40:55 -0800541 if (ri->task != current)
542 /* another task is sharing our hash bucket */
543 continue;
544
545 if (ri->rp && ri->rp->handler)
546 ri->rp->handler(ri, regs);
547
548 orig_ret_address = (unsigned long)ri->ret_addr;
549 recycle_rp_inst(ri, &empty_rp);
550
551 if (orig_ret_address != trampoline_address)
552 /*
553 * This is the real return address. Any other
554 * instances associated with this task are for
555 * other calls deeper on the call stack
556 */
557 break;
558 }
559
560 kretprobe_assert(ri, orig_ret_address, trampoline_address);
561 regs->tpc = orig_ret_address;
562 regs->tnpc = orig_ret_address + 4;
563
564 reset_current_kprobe();
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700565 kretprobe_hash_unlock(current, &flags);
David S. Millerd38f1222008-02-09 03:40:55 -0800566 preempt_enable_no_resched();
567
Sasha Levinb67bfe02013-02-27 17:06:00 -0800568 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
David S. Millerd38f1222008-02-09 03:40:55 -0800569 hlist_del(&ri->hlist);
570 kfree(ri);
571 }
572 /*
573 * By returning a non-zero value, we are telling
574 * kprobe_handler() that we don't want the post_handler
575 * to run (and have re-enabled preemption)
576 */
577 return 1;
578}
579
Sam Ravnborg2f827ea2014-05-16 23:26:03 +0200580static void __used kretprobe_trampoline_holder(void)
David S. Millerd38f1222008-02-09 03:40:55 -0800581{
582 asm volatile(".global kretprobe_trampoline\n"
583 "kretprobe_trampoline:\n"
584 "\tnop\n"
585 "\tnop\n");
586}
587static struct kprobe trampoline_p = {
588 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
589 .pre_handler = trampoline_probe_handler
590};
591
592int __init arch_init_kprobes(void)
593{
594 return register_kprobe(&trampoline_p);
595}
596
597int __kprobes arch_trampoline_kprobe(struct kprobe *p)
598{
599 if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
600 return 1;
601
Rusty Lynch67729262005-07-05 18:54:50 -0700602 return 0;
603}