xref: /openbmc/linux/arch/sparc/kernel/kprobes.c (revision 812cb83a56a908729c453a7db3fb2c262119bc9d)
1a88b5ba8SSam Ravnborg /* arch/sparc64/kernel/kprobes.c
2a88b5ba8SSam Ravnborg  *
3a88b5ba8SSam Ravnborg  * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
4a88b5ba8SSam Ravnborg  */
5a88b5ba8SSam Ravnborg 
6a88b5ba8SSam Ravnborg #include <linux/kernel.h>
7a88b5ba8SSam Ravnborg #include <linux/kprobes.h>
8a88b5ba8SSam Ravnborg #include <linux/module.h>
9a88b5ba8SSam Ravnborg #include <linux/kdebug.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
11*812cb83aSKirill Tkhai #include <linux/context_tracking.h>
12a88b5ba8SSam Ravnborg #include <asm/signal.h>
13a88b5ba8SSam Ravnborg #include <asm/cacheflush.h>
14a88b5ba8SSam Ravnborg #include <asm/uaccess.h>
15a88b5ba8SSam Ravnborg 
16a88b5ba8SSam Ravnborg /* We do not have hardware single-stepping on sparc64.
17a88b5ba8SSam Ravnborg  * So we implement software single-stepping with breakpoint
18a88b5ba8SSam Ravnborg  * traps.  The top-level scheme is similar to that used
19a88b5ba8SSam Ravnborg  * in the x86 kprobes implementation.
20a88b5ba8SSam Ravnborg  *
21a88b5ba8SSam Ravnborg  * In the kprobe->ainsn.insn[] array we store the original
22a88b5ba8SSam Ravnborg  * instruction at index zero and a break instruction at
23a88b5ba8SSam Ravnborg  * index one.
24a88b5ba8SSam Ravnborg  *
25a88b5ba8SSam Ravnborg  * When we hit a kprobe we:
26a88b5ba8SSam Ravnborg  * - Run the pre-handler
27a88b5ba8SSam Ravnborg  * - Remember "regs->tnpc" and interrupt level stored in
28a88b5ba8SSam Ravnborg  *   "regs->tstate" so we can restore them later
29a88b5ba8SSam Ravnborg  * - Disable PIL interrupts
30a88b5ba8SSam Ravnborg  * - Set regs->tpc to point to kprobe->ainsn.insn[0]
31a88b5ba8SSam Ravnborg  * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
32a88b5ba8SSam Ravnborg  * - Mark that we are actively in a kprobe
33a88b5ba8SSam Ravnborg  *
34a88b5ba8SSam Ravnborg  * At this point we wait for the second breakpoint at
35a88b5ba8SSam Ravnborg  * kprobe->ainsn.insn[1] to hit.  When it does we:
36a88b5ba8SSam Ravnborg  * - Run the post-handler
37a88b5ba8SSam Ravnborg  * - Set regs->tpc to "remembered" regs->tnpc stored above,
38a88b5ba8SSam Ravnborg  *   restore the PIL interrupt level in "regs->tstate" as well
39a88b5ba8SSam Ravnborg  * - Make any adjustments necessary to regs->tnpc in order
40a88b5ba8SSam Ravnborg  *   to handle relative branches correctly.  See below.
41a88b5ba8SSam Ravnborg  * - Mark that we are no longer actively in a kprobe.
42a88b5ba8SSam Ravnborg  */
43a88b5ba8SSam Ravnborg 
44a88b5ba8SSam Ravnborg DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
45a88b5ba8SSam Ravnborg DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
46a88b5ba8SSam Ravnborg 
47a88b5ba8SSam Ravnborg struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
48a88b5ba8SSam Ravnborg 
49a88b5ba8SSam Ravnborg int __kprobes arch_prepare_kprobe(struct kprobe *p)
50a88b5ba8SSam Ravnborg {
51936cf251SDavid S. Miller 	if ((unsigned long) p->addr & 0x3UL)
52936cf251SDavid S. Miller 		return -EILSEQ;
53936cf251SDavid S. Miller 
54a88b5ba8SSam Ravnborg 	p->ainsn.insn[0] = *p->addr;
55a88b5ba8SSam Ravnborg 	flushi(&p->ainsn.insn[0]);
56a88b5ba8SSam Ravnborg 
57a88b5ba8SSam Ravnborg 	p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
58a88b5ba8SSam Ravnborg 	flushi(&p->ainsn.insn[1]);
59a88b5ba8SSam Ravnborg 
60a88b5ba8SSam Ravnborg 	p->opcode = *p->addr;
61a88b5ba8SSam Ravnborg 	return 0;
62a88b5ba8SSam Ravnborg }
63a88b5ba8SSam Ravnborg 
64a88b5ba8SSam Ravnborg void __kprobes arch_arm_kprobe(struct kprobe *p)
65a88b5ba8SSam Ravnborg {
66a88b5ba8SSam Ravnborg 	*p->addr = BREAKPOINT_INSTRUCTION;
67a88b5ba8SSam Ravnborg 	flushi(p->addr);
68a88b5ba8SSam Ravnborg }
69a88b5ba8SSam Ravnborg 
70a88b5ba8SSam Ravnborg void __kprobes arch_disarm_kprobe(struct kprobe *p)
71a88b5ba8SSam Ravnborg {
72a88b5ba8SSam Ravnborg 	*p->addr = p->opcode;
73a88b5ba8SSam Ravnborg 	flushi(p->addr);
74a88b5ba8SSam Ravnborg }
75a88b5ba8SSam Ravnborg 
76a88b5ba8SSam Ravnborg static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
77a88b5ba8SSam Ravnborg {
78a88b5ba8SSam Ravnborg 	kcb->prev_kprobe.kp = kprobe_running();
79a88b5ba8SSam Ravnborg 	kcb->prev_kprobe.status = kcb->kprobe_status;
80a88b5ba8SSam Ravnborg 	kcb->prev_kprobe.orig_tnpc = kcb->kprobe_orig_tnpc;
81a88b5ba8SSam Ravnborg 	kcb->prev_kprobe.orig_tstate_pil = kcb->kprobe_orig_tstate_pil;
82a88b5ba8SSam Ravnborg }
83a88b5ba8SSam Ravnborg 
84a88b5ba8SSam Ravnborg static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
85a88b5ba8SSam Ravnborg {
86a88b5ba8SSam Ravnborg 	__get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
87a88b5ba8SSam Ravnborg 	kcb->kprobe_status = kcb->prev_kprobe.status;
88a88b5ba8SSam Ravnborg 	kcb->kprobe_orig_tnpc = kcb->prev_kprobe.orig_tnpc;
89a88b5ba8SSam Ravnborg 	kcb->kprobe_orig_tstate_pil = kcb->prev_kprobe.orig_tstate_pil;
90a88b5ba8SSam Ravnborg }
91a88b5ba8SSam Ravnborg 
92a88b5ba8SSam Ravnborg static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
93a88b5ba8SSam Ravnborg 				struct kprobe_ctlblk *kcb)
94a88b5ba8SSam Ravnborg {
95a88b5ba8SSam Ravnborg 	__get_cpu_var(current_kprobe) = p;
96a88b5ba8SSam Ravnborg 	kcb->kprobe_orig_tnpc = regs->tnpc;
97a88b5ba8SSam Ravnborg 	kcb->kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
98a88b5ba8SSam Ravnborg }
99a88b5ba8SSam Ravnborg 
100a88b5ba8SSam Ravnborg static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
101a88b5ba8SSam Ravnborg 			struct kprobe_ctlblk *kcb)
102a88b5ba8SSam Ravnborg {
103a88b5ba8SSam Ravnborg 	regs->tstate |= TSTATE_PIL;
104a88b5ba8SSam Ravnborg 
105a88b5ba8SSam Ravnborg 	/*single step inline, if it a breakpoint instruction*/
106a88b5ba8SSam Ravnborg 	if (p->opcode == BREAKPOINT_INSTRUCTION) {
107a88b5ba8SSam Ravnborg 		regs->tpc = (unsigned long) p->addr;
108a88b5ba8SSam Ravnborg 		regs->tnpc = kcb->kprobe_orig_tnpc;
109a88b5ba8SSam Ravnborg 	} else {
110a88b5ba8SSam Ravnborg 		regs->tpc = (unsigned long) &p->ainsn.insn[0];
111a88b5ba8SSam Ravnborg 		regs->tnpc = (unsigned long) &p->ainsn.insn[1];
112a88b5ba8SSam Ravnborg 	}
113a88b5ba8SSam Ravnborg }
114a88b5ba8SSam Ravnborg 
115a88b5ba8SSam Ravnborg static int __kprobes kprobe_handler(struct pt_regs *regs)
116a88b5ba8SSam Ravnborg {
117a88b5ba8SSam Ravnborg 	struct kprobe *p;
118a88b5ba8SSam Ravnborg 	void *addr = (void *) regs->tpc;
119a88b5ba8SSam Ravnborg 	int ret = 0;
120a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb;
121a88b5ba8SSam Ravnborg 
122a88b5ba8SSam Ravnborg 	/*
123a88b5ba8SSam Ravnborg 	 * We don't want to be preempted for the entire
124a88b5ba8SSam Ravnborg 	 * duration of kprobe processing
125a88b5ba8SSam Ravnborg 	 */
126a88b5ba8SSam Ravnborg 	preempt_disable();
127a88b5ba8SSam Ravnborg 	kcb = get_kprobe_ctlblk();
128a88b5ba8SSam Ravnborg 
129a88b5ba8SSam Ravnborg 	if (kprobe_running()) {
130a88b5ba8SSam Ravnborg 		p = get_kprobe(addr);
131a88b5ba8SSam Ravnborg 		if (p) {
132a88b5ba8SSam Ravnborg 			if (kcb->kprobe_status == KPROBE_HIT_SS) {
133a88b5ba8SSam Ravnborg 				regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
134a88b5ba8SSam Ravnborg 					kcb->kprobe_orig_tstate_pil);
135a88b5ba8SSam Ravnborg 				goto no_kprobe;
136a88b5ba8SSam Ravnborg 			}
137a88b5ba8SSam Ravnborg 			/* We have reentered the kprobe_handler(), since
138a88b5ba8SSam Ravnborg 			 * another probe was hit while within the handler.
139a88b5ba8SSam Ravnborg 			 * We here save the original kprobes variables and
140a88b5ba8SSam Ravnborg 			 * just single step on the instruction of the new probe
141a88b5ba8SSam Ravnborg 			 * without calling any user handlers.
142a88b5ba8SSam Ravnborg 			 */
143a88b5ba8SSam Ravnborg 			save_previous_kprobe(kcb);
144a88b5ba8SSam Ravnborg 			set_current_kprobe(p, regs, kcb);
145a88b5ba8SSam Ravnborg 			kprobes_inc_nmissed_count(p);
146a88b5ba8SSam Ravnborg 			kcb->kprobe_status = KPROBE_REENTER;
147a88b5ba8SSam Ravnborg 			prepare_singlestep(p, regs, kcb);
148a88b5ba8SSam Ravnborg 			return 1;
149a88b5ba8SSam Ravnborg 		} else {
150a88b5ba8SSam Ravnborg 			if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
151a88b5ba8SSam Ravnborg 			/* The breakpoint instruction was removed by
152a88b5ba8SSam Ravnborg 			 * another cpu right after we hit, no further
153a88b5ba8SSam Ravnborg 			 * handling of this interrupt is appropriate
154a88b5ba8SSam Ravnborg 			 */
155a88b5ba8SSam Ravnborg 				ret = 1;
156a88b5ba8SSam Ravnborg 				goto no_kprobe;
157a88b5ba8SSam Ravnborg 			}
158a88b5ba8SSam Ravnborg 			p = __get_cpu_var(current_kprobe);
159a88b5ba8SSam Ravnborg 			if (p->break_handler && p->break_handler(p, regs))
160a88b5ba8SSam Ravnborg 				goto ss_probe;
161a88b5ba8SSam Ravnborg 		}
162a88b5ba8SSam Ravnborg 		goto no_kprobe;
163a88b5ba8SSam Ravnborg 	}
164a88b5ba8SSam Ravnborg 
165a88b5ba8SSam Ravnborg 	p = get_kprobe(addr);
166a88b5ba8SSam Ravnborg 	if (!p) {
167a88b5ba8SSam Ravnborg 		if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
168a88b5ba8SSam Ravnborg 			/*
169a88b5ba8SSam Ravnborg 			 * The breakpoint instruction was removed right
170a88b5ba8SSam Ravnborg 			 * after we hit it.  Another cpu has removed
171a88b5ba8SSam Ravnborg 			 * either a probepoint or a debugger breakpoint
172a88b5ba8SSam Ravnborg 			 * at this address.  In either case, no further
173a88b5ba8SSam Ravnborg 			 * handling of this interrupt is appropriate.
174a88b5ba8SSam Ravnborg 			 */
175a88b5ba8SSam Ravnborg 			ret = 1;
176a88b5ba8SSam Ravnborg 		}
177a88b5ba8SSam Ravnborg 		/* Not one of ours: let kernel handle it */
178a88b5ba8SSam Ravnborg 		goto no_kprobe;
179a88b5ba8SSam Ravnborg 	}
180a88b5ba8SSam Ravnborg 
181a88b5ba8SSam Ravnborg 	set_current_kprobe(p, regs, kcb);
182a88b5ba8SSam Ravnborg 	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
183a88b5ba8SSam Ravnborg 	if (p->pre_handler && p->pre_handler(p, regs))
184a88b5ba8SSam Ravnborg 		return 1;
185a88b5ba8SSam Ravnborg 
186a88b5ba8SSam Ravnborg ss_probe:
187a88b5ba8SSam Ravnborg 	prepare_singlestep(p, regs, kcb);
188a88b5ba8SSam Ravnborg 	kcb->kprobe_status = KPROBE_HIT_SS;
189a88b5ba8SSam Ravnborg 	return 1;
190a88b5ba8SSam Ravnborg 
191a88b5ba8SSam Ravnborg no_kprobe:
192a88b5ba8SSam Ravnborg 	preempt_enable_no_resched();
193a88b5ba8SSam Ravnborg 	return ret;
194a88b5ba8SSam Ravnborg }
195a88b5ba8SSam Ravnborg 
196a88b5ba8SSam Ravnborg /* If INSN is a relative control transfer instruction,
197a88b5ba8SSam Ravnborg  * return the corrected branch destination value.
198a88b5ba8SSam Ravnborg  *
199a88b5ba8SSam Ravnborg  * regs->tpc and regs->tnpc still hold the values of the
200a88b5ba8SSam Ravnborg  * program counters at the time of trap due to the execution
201a88b5ba8SSam Ravnborg  * of the BREAKPOINT_INSTRUCTION_2 at p->ainsn.insn[1]
202a88b5ba8SSam Ravnborg  *
203a88b5ba8SSam Ravnborg  */
204a88b5ba8SSam Ravnborg static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
205a88b5ba8SSam Ravnborg 					       struct pt_regs *regs)
206a88b5ba8SSam Ravnborg {
207a88b5ba8SSam Ravnborg 	unsigned long real_pc = (unsigned long) p->addr;
208a88b5ba8SSam Ravnborg 
209a88b5ba8SSam Ravnborg 	/* Branch not taken, no mods necessary.  */
210a88b5ba8SSam Ravnborg 	if (regs->tnpc == regs->tpc + 0x4UL)
211a88b5ba8SSam Ravnborg 		return real_pc + 0x8UL;
212a88b5ba8SSam Ravnborg 
213a88b5ba8SSam Ravnborg 	/* The three cases are call, branch w/prediction,
214a88b5ba8SSam Ravnborg 	 * and traditional branch.
215a88b5ba8SSam Ravnborg 	 */
216a88b5ba8SSam Ravnborg 	if ((insn & 0xc0000000) == 0x40000000 ||
217a88b5ba8SSam Ravnborg 	    (insn & 0xc1c00000) == 0x00400000 ||
218a88b5ba8SSam Ravnborg 	    (insn & 0xc1c00000) == 0x00800000) {
219a88b5ba8SSam Ravnborg 		unsigned long ainsn_addr;
220a88b5ba8SSam Ravnborg 
221a88b5ba8SSam Ravnborg 		ainsn_addr = (unsigned long) &p->ainsn.insn[0];
222a88b5ba8SSam Ravnborg 
223a88b5ba8SSam Ravnborg 		/* The instruction did all the work for us
224a88b5ba8SSam Ravnborg 		 * already, just apply the offset to the correct
225a88b5ba8SSam Ravnborg 		 * instruction location.
226a88b5ba8SSam Ravnborg 		 */
227a88b5ba8SSam Ravnborg 		return (real_pc + (regs->tnpc - ainsn_addr));
228a88b5ba8SSam Ravnborg 	}
229a88b5ba8SSam Ravnborg 
230a88b5ba8SSam Ravnborg 	/* It is jmpl or some other absolute PC modification instruction,
231a88b5ba8SSam Ravnborg 	 * leave NPC as-is.
232a88b5ba8SSam Ravnborg 	 */
233a88b5ba8SSam Ravnborg 	return regs->tnpc;
234a88b5ba8SSam Ravnborg }
235a88b5ba8SSam Ravnborg 
236a88b5ba8SSam Ravnborg /* If INSN is an instruction which writes it's PC location
237a88b5ba8SSam Ravnborg  * into a destination register, fix that up.
238a88b5ba8SSam Ravnborg  */
239a88b5ba8SSam Ravnborg static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
240a88b5ba8SSam Ravnborg 				  unsigned long real_pc)
241a88b5ba8SSam Ravnborg {
242a88b5ba8SSam Ravnborg 	unsigned long *slot = NULL;
243a88b5ba8SSam Ravnborg 
244a88b5ba8SSam Ravnborg 	/* Simplest case is 'call', which always uses %o7 */
245a88b5ba8SSam Ravnborg 	if ((insn & 0xc0000000) == 0x40000000) {
246a88b5ba8SSam Ravnborg 		slot = &regs->u_regs[UREG_I7];
247a88b5ba8SSam Ravnborg 	}
248a88b5ba8SSam Ravnborg 
249a88b5ba8SSam Ravnborg 	/* 'jmpl' encodes the register inside of the opcode */
250a88b5ba8SSam Ravnborg 	if ((insn & 0xc1f80000) == 0x81c00000) {
251a88b5ba8SSam Ravnborg 		unsigned long rd = ((insn >> 25) & 0x1f);
252a88b5ba8SSam Ravnborg 
253a88b5ba8SSam Ravnborg 		if (rd <= 15) {
254a88b5ba8SSam Ravnborg 			slot = &regs->u_regs[rd];
255a88b5ba8SSam Ravnborg 		} else {
256a88b5ba8SSam Ravnborg 			/* Hard case, it goes onto the stack. */
257a88b5ba8SSam Ravnborg 			flushw_all();
258a88b5ba8SSam Ravnborg 
259a88b5ba8SSam Ravnborg 			rd -= 16;
260a88b5ba8SSam Ravnborg 			slot = (unsigned long *)
261a88b5ba8SSam Ravnborg 				(regs->u_regs[UREG_FP] + STACK_BIAS);
262a88b5ba8SSam Ravnborg 			slot += rd;
263a88b5ba8SSam Ravnborg 		}
264a88b5ba8SSam Ravnborg 	}
265a88b5ba8SSam Ravnborg 	if (slot != NULL)
266a88b5ba8SSam Ravnborg 		*slot = real_pc;
267a88b5ba8SSam Ravnborg }
268a88b5ba8SSam Ravnborg 
269a88b5ba8SSam Ravnborg /*
270a88b5ba8SSam Ravnborg  * Called after single-stepping.  p->addr is the address of the
271a88b5ba8SSam Ravnborg  * instruction which has been replaced by the breakpoint
272a88b5ba8SSam Ravnborg  * instruction.  To avoid the SMP problems that can occur when we
273a88b5ba8SSam Ravnborg  * temporarily put back the original opcode to single-step, we
274a88b5ba8SSam Ravnborg  * single-stepped a copy of the instruction.  The address of this
275a88b5ba8SSam Ravnborg  * copy is &p->ainsn.insn[0].
276a88b5ba8SSam Ravnborg  *
277a88b5ba8SSam Ravnborg  * This function prepares to return from the post-single-step
278a88b5ba8SSam Ravnborg  * breakpoint trap.
279a88b5ba8SSam Ravnborg  */
280a88b5ba8SSam Ravnborg static void __kprobes resume_execution(struct kprobe *p,
281a88b5ba8SSam Ravnborg 		struct pt_regs *regs, struct kprobe_ctlblk *kcb)
282a88b5ba8SSam Ravnborg {
283a88b5ba8SSam Ravnborg 	u32 insn = p->ainsn.insn[0];
284a88b5ba8SSam Ravnborg 
285a88b5ba8SSam Ravnborg 	regs->tnpc = relbranch_fixup(insn, p, regs);
286a88b5ba8SSam Ravnborg 
287a88b5ba8SSam Ravnborg 	/* This assignment must occur after relbranch_fixup() */
288a88b5ba8SSam Ravnborg 	regs->tpc = kcb->kprobe_orig_tnpc;
289a88b5ba8SSam Ravnborg 
290a88b5ba8SSam Ravnborg 	retpc_fixup(regs, insn, (unsigned long) p->addr);
291a88b5ba8SSam Ravnborg 
292a88b5ba8SSam Ravnborg 	regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
293a88b5ba8SSam Ravnborg 			kcb->kprobe_orig_tstate_pil);
294a88b5ba8SSam Ravnborg }
295a88b5ba8SSam Ravnborg 
296a88b5ba8SSam Ravnborg static int __kprobes post_kprobe_handler(struct pt_regs *regs)
297a88b5ba8SSam Ravnborg {
298a88b5ba8SSam Ravnborg 	struct kprobe *cur = kprobe_running();
299a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
300a88b5ba8SSam Ravnborg 
301a88b5ba8SSam Ravnborg 	if (!cur)
302a88b5ba8SSam Ravnborg 		return 0;
303a88b5ba8SSam Ravnborg 
304a88b5ba8SSam Ravnborg 	if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
305a88b5ba8SSam Ravnborg 		kcb->kprobe_status = KPROBE_HIT_SSDONE;
306a88b5ba8SSam Ravnborg 		cur->post_handler(cur, regs, 0);
307a88b5ba8SSam Ravnborg 	}
308a88b5ba8SSam Ravnborg 
309a88b5ba8SSam Ravnborg 	resume_execution(cur, regs, kcb);
310a88b5ba8SSam Ravnborg 
311a88b5ba8SSam Ravnborg 	/*Restore back the original saved kprobes variables and continue. */
312a88b5ba8SSam Ravnborg 	if (kcb->kprobe_status == KPROBE_REENTER) {
313a88b5ba8SSam Ravnborg 		restore_previous_kprobe(kcb);
314a88b5ba8SSam Ravnborg 		goto out;
315a88b5ba8SSam Ravnborg 	}
316a88b5ba8SSam Ravnborg 	reset_current_kprobe();
317a88b5ba8SSam Ravnborg out:
318a88b5ba8SSam Ravnborg 	preempt_enable_no_resched();
319a88b5ba8SSam Ravnborg 
320a88b5ba8SSam Ravnborg 	return 1;
321a88b5ba8SSam Ravnborg }
322a88b5ba8SSam Ravnborg 
323a88b5ba8SSam Ravnborg int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
324a88b5ba8SSam Ravnborg {
325a88b5ba8SSam Ravnborg 	struct kprobe *cur = kprobe_running();
326a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
327a88b5ba8SSam Ravnborg 	const struct exception_table_entry *entry;
328a88b5ba8SSam Ravnborg 
329a88b5ba8SSam Ravnborg 	switch(kcb->kprobe_status) {
330a88b5ba8SSam Ravnborg 	case KPROBE_HIT_SS:
331a88b5ba8SSam Ravnborg 	case KPROBE_REENTER:
332a88b5ba8SSam Ravnborg 		/*
333a88b5ba8SSam Ravnborg 		 * We are here because the instruction being single
334a88b5ba8SSam Ravnborg 		 * stepped caused a page fault. We reset the current
335a88b5ba8SSam Ravnborg 		 * kprobe and the tpc points back to the probe address
336a88b5ba8SSam Ravnborg 		 * and allow the page fault handler to continue as a
337a88b5ba8SSam Ravnborg 		 * normal page fault.
338a88b5ba8SSam Ravnborg 		 */
339a88b5ba8SSam Ravnborg 		regs->tpc = (unsigned long)cur->addr;
340a88b5ba8SSam Ravnborg 		regs->tnpc = kcb->kprobe_orig_tnpc;
341a88b5ba8SSam Ravnborg 		regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
342a88b5ba8SSam Ravnborg 				kcb->kprobe_orig_tstate_pil);
343a88b5ba8SSam Ravnborg 		if (kcb->kprobe_status == KPROBE_REENTER)
344a88b5ba8SSam Ravnborg 			restore_previous_kprobe(kcb);
345a88b5ba8SSam Ravnborg 		else
346a88b5ba8SSam Ravnborg 			reset_current_kprobe();
347a88b5ba8SSam Ravnborg 		preempt_enable_no_resched();
348a88b5ba8SSam Ravnborg 		break;
349a88b5ba8SSam Ravnborg 	case KPROBE_HIT_ACTIVE:
350a88b5ba8SSam Ravnborg 	case KPROBE_HIT_SSDONE:
351a88b5ba8SSam Ravnborg 		/*
352a88b5ba8SSam Ravnborg 		 * We increment the nmissed count for accounting,
353a88b5ba8SSam Ravnborg 		 * we can also use npre/npostfault count for accouting
354a88b5ba8SSam Ravnborg 		 * these specific fault cases.
355a88b5ba8SSam Ravnborg 		 */
356a88b5ba8SSam Ravnborg 		kprobes_inc_nmissed_count(cur);
357a88b5ba8SSam Ravnborg 
358a88b5ba8SSam Ravnborg 		/*
359a88b5ba8SSam Ravnborg 		 * We come here because instructions in the pre/post
360a88b5ba8SSam Ravnborg 		 * handler caused the page_fault, this could happen
361a88b5ba8SSam Ravnborg 		 * if handler tries to access user space by
362a88b5ba8SSam Ravnborg 		 * copy_from_user(), get_user() etc. Let the
363a88b5ba8SSam Ravnborg 		 * user-specified handler try to fix it first.
364a88b5ba8SSam Ravnborg 		 */
365a88b5ba8SSam Ravnborg 		if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
366a88b5ba8SSam Ravnborg 			return 1;
367a88b5ba8SSam Ravnborg 
368a88b5ba8SSam Ravnborg 		/*
369a88b5ba8SSam Ravnborg 		 * In case the user-specified fault handler returned
370a88b5ba8SSam Ravnborg 		 * zero, try to fix up.
371a88b5ba8SSam Ravnborg 		 */
372a88b5ba8SSam Ravnborg 
373a88b5ba8SSam Ravnborg 		entry = search_exception_tables(regs->tpc);
374a88b5ba8SSam Ravnborg 		if (entry) {
375a88b5ba8SSam Ravnborg 			regs->tpc = entry->fixup;
376a88b5ba8SSam Ravnborg 			regs->tnpc = regs->tpc + 4;
377a88b5ba8SSam Ravnborg 			return 1;
378a88b5ba8SSam Ravnborg 		}
379a88b5ba8SSam Ravnborg 
380a88b5ba8SSam Ravnborg 		/*
381a88b5ba8SSam Ravnborg 		 * fixup_exception() could not handle it,
382a88b5ba8SSam Ravnborg 		 * Let do_page_fault() fix it.
383a88b5ba8SSam Ravnborg 		 */
384a88b5ba8SSam Ravnborg 		break;
385a88b5ba8SSam Ravnborg 	default:
386a88b5ba8SSam Ravnborg 		break;
387a88b5ba8SSam Ravnborg 	}
388a88b5ba8SSam Ravnborg 
389a88b5ba8SSam Ravnborg 	return 0;
390a88b5ba8SSam Ravnborg }
391a88b5ba8SSam Ravnborg 
392a88b5ba8SSam Ravnborg /*
393a88b5ba8SSam Ravnborg  * Wrapper routine to for handling exceptions.
394a88b5ba8SSam Ravnborg  */
395a88b5ba8SSam Ravnborg int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
396a88b5ba8SSam Ravnborg 				       unsigned long val, void *data)
397a88b5ba8SSam Ravnborg {
398a88b5ba8SSam Ravnborg 	struct die_args *args = (struct die_args *)data;
399a88b5ba8SSam Ravnborg 	int ret = NOTIFY_DONE;
400a88b5ba8SSam Ravnborg 
401a88b5ba8SSam Ravnborg 	if (args->regs && user_mode(args->regs))
402a88b5ba8SSam Ravnborg 		return ret;
403a88b5ba8SSam Ravnborg 
404a88b5ba8SSam Ravnborg 	switch (val) {
405a88b5ba8SSam Ravnborg 	case DIE_DEBUG:
406a88b5ba8SSam Ravnborg 		if (kprobe_handler(args->regs))
407a88b5ba8SSam Ravnborg 			ret = NOTIFY_STOP;
408a88b5ba8SSam Ravnborg 		break;
409a88b5ba8SSam Ravnborg 	case DIE_DEBUG_2:
410a88b5ba8SSam Ravnborg 		if (post_kprobe_handler(args->regs))
411a88b5ba8SSam Ravnborg 			ret = NOTIFY_STOP;
412a88b5ba8SSam Ravnborg 		break;
413a88b5ba8SSam Ravnborg 	default:
414a88b5ba8SSam Ravnborg 		break;
415a88b5ba8SSam Ravnborg 	}
416a88b5ba8SSam Ravnborg 	return ret;
417a88b5ba8SSam Ravnborg }
418a88b5ba8SSam Ravnborg 
419a88b5ba8SSam Ravnborg asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
420a88b5ba8SSam Ravnborg 				      struct pt_regs *regs)
421a88b5ba8SSam Ravnborg {
422*812cb83aSKirill Tkhai 	enum ctx_state prev_state = exception_enter();
423*812cb83aSKirill Tkhai 
424a88b5ba8SSam Ravnborg 	BUG_ON(trap_level != 0x170 && trap_level != 0x171);
425a88b5ba8SSam Ravnborg 
426a88b5ba8SSam Ravnborg 	if (user_mode(regs)) {
427a88b5ba8SSam Ravnborg 		local_irq_enable();
428a88b5ba8SSam Ravnborg 		bad_trap(regs, trap_level);
429*812cb83aSKirill Tkhai 		goto out;
430a88b5ba8SSam Ravnborg 	}
431a88b5ba8SSam Ravnborg 
432a88b5ba8SSam Ravnborg 	/* trap_level == 0x170 --> ta 0x70
433a88b5ba8SSam Ravnborg 	 * trap_level == 0x171 --> ta 0x71
434a88b5ba8SSam Ravnborg 	 */
435a88b5ba8SSam Ravnborg 	if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
436a88b5ba8SSam Ravnborg 		       (trap_level == 0x170) ? "debug" : "debug_2",
437a88b5ba8SSam Ravnborg 		       regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
438a88b5ba8SSam Ravnborg 		bad_trap(regs, trap_level);
439*812cb83aSKirill Tkhai out:
440*812cb83aSKirill Tkhai 	exception_exit(prev_state);
441a88b5ba8SSam Ravnborg }
442a88b5ba8SSam Ravnborg 
443a88b5ba8SSam Ravnborg /* Jprobes support.  */
444a88b5ba8SSam Ravnborg int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
445a88b5ba8SSam Ravnborg {
446a88b5ba8SSam Ravnborg 	struct jprobe *jp = container_of(p, struct jprobe, kp);
447a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
448a88b5ba8SSam Ravnborg 
449a88b5ba8SSam Ravnborg 	memcpy(&(kcb->jprobe_saved_regs), regs, sizeof(*regs));
450a88b5ba8SSam Ravnborg 
451a88b5ba8SSam Ravnborg 	regs->tpc  = (unsigned long) jp->entry;
452a88b5ba8SSam Ravnborg 	regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
453a88b5ba8SSam Ravnborg 	regs->tstate |= TSTATE_PIL;
454a88b5ba8SSam Ravnborg 
455a88b5ba8SSam Ravnborg 	return 1;
456a88b5ba8SSam Ravnborg }
457a88b5ba8SSam Ravnborg 
458a88b5ba8SSam Ravnborg void __kprobes jprobe_return(void)
459a88b5ba8SSam Ravnborg {
460a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
461a88b5ba8SSam Ravnborg 	register unsigned long orig_fp asm("g1");
462a88b5ba8SSam Ravnborg 
463a88b5ba8SSam Ravnborg 	orig_fp = kcb->jprobe_saved_regs.u_regs[UREG_FP];
464a88b5ba8SSam Ravnborg 	__asm__ __volatile__("\n"
465a88b5ba8SSam Ravnborg "1:	cmp		%%sp, %0\n\t"
466a88b5ba8SSam Ravnborg 	"blu,a,pt	%%xcc, 1b\n\t"
467a88b5ba8SSam Ravnborg 	" restore\n\t"
468a88b5ba8SSam Ravnborg 	".globl		jprobe_return_trap_instruction\n"
469a88b5ba8SSam Ravnborg "jprobe_return_trap_instruction:\n\t"
470a88b5ba8SSam Ravnborg 	"ta		0x70"
471a88b5ba8SSam Ravnborg 	: /* no outputs */
472a88b5ba8SSam Ravnborg 	: "r" (orig_fp));
473a88b5ba8SSam Ravnborg }
474a88b5ba8SSam Ravnborg 
475a88b5ba8SSam Ravnborg extern void jprobe_return_trap_instruction(void);
476a88b5ba8SSam Ravnborg 
477a88b5ba8SSam Ravnborg int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
478a88b5ba8SSam Ravnborg {
479a88b5ba8SSam Ravnborg 	u32 *addr = (u32 *) regs->tpc;
480a88b5ba8SSam Ravnborg 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
481a88b5ba8SSam Ravnborg 
482a88b5ba8SSam Ravnborg 	if (addr == (u32 *) jprobe_return_trap_instruction) {
483a88b5ba8SSam Ravnborg 		memcpy(regs, &(kcb->jprobe_saved_regs), sizeof(*regs));
484a88b5ba8SSam Ravnborg 		preempt_enable_no_resched();
485a88b5ba8SSam Ravnborg 		return 1;
486a88b5ba8SSam Ravnborg 	}
487a88b5ba8SSam Ravnborg 	return 0;
488a88b5ba8SSam Ravnborg }
489a88b5ba8SSam Ravnborg 
490a88b5ba8SSam Ravnborg /* The value stored in the return address register is actually 2
491a88b5ba8SSam Ravnborg  * instructions before where the callee will return to.
492a88b5ba8SSam Ravnborg  * Sequences usually look something like this
493a88b5ba8SSam Ravnborg  *
494a88b5ba8SSam Ravnborg  *		call	some_function	<--- return register points here
495a88b5ba8SSam Ravnborg  *		 nop			<--- call delay slot
496a88b5ba8SSam Ravnborg  *		whatever		<--- where callee returns to
497a88b5ba8SSam Ravnborg  *
498a88b5ba8SSam Ravnborg  * To keep trampoline_probe_handler logic simpler, we normalize the
499a88b5ba8SSam Ravnborg  * value kept in ri->ret_addr so we don't need to keep adjusting it
500a88b5ba8SSam Ravnborg  * back and forth.
501a88b5ba8SSam Ravnborg  */
502a88b5ba8SSam Ravnborg void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
503a88b5ba8SSam Ravnborg 				      struct pt_regs *regs)
504a88b5ba8SSam Ravnborg {
505a88b5ba8SSam Ravnborg 	ri->ret_addr = (kprobe_opcode_t *)(regs->u_regs[UREG_RETPC] + 8);
506a88b5ba8SSam Ravnborg 
507a88b5ba8SSam Ravnborg 	/* Replace the return addr with trampoline addr */
508a88b5ba8SSam Ravnborg 	regs->u_regs[UREG_RETPC] =
509a88b5ba8SSam Ravnborg 		((unsigned long)kretprobe_trampoline) - 8;
510a88b5ba8SSam Ravnborg }
511a88b5ba8SSam Ravnborg 
512a88b5ba8SSam Ravnborg /*
513a88b5ba8SSam Ravnborg  * Called when the probe at kretprobe trampoline is hit
514a88b5ba8SSam Ravnborg  */
515a88b5ba8SSam Ravnborg int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
516a88b5ba8SSam Ravnborg {
517a88b5ba8SSam Ravnborg 	struct kretprobe_instance *ri = NULL;
518a88b5ba8SSam Ravnborg 	struct hlist_head *head, empty_rp;
519b67bfe0dSSasha Levin 	struct hlist_node *tmp;
520a88b5ba8SSam Ravnborg 	unsigned long flags, orig_ret_address = 0;
521a88b5ba8SSam Ravnborg 	unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
522a88b5ba8SSam Ravnborg 
523a88b5ba8SSam Ravnborg 	INIT_HLIST_HEAD(&empty_rp);
524a88b5ba8SSam Ravnborg 	kretprobe_hash_lock(current, &head, &flags);
525a88b5ba8SSam Ravnborg 
526a88b5ba8SSam Ravnborg 	/*
527a88b5ba8SSam Ravnborg 	 * It is possible to have multiple instances associated with a given
528a88b5ba8SSam Ravnborg 	 * task either because an multiple functions in the call path
529025dfdafSFrederik Schwarzer 	 * have a return probe installed on them, and/or more than one return
530a88b5ba8SSam Ravnborg 	 * return probe was registered for a target function.
531a88b5ba8SSam Ravnborg 	 *
532a88b5ba8SSam Ravnborg 	 * We can handle this because:
533a88b5ba8SSam Ravnborg 	 *     - instances are always inserted at the head of the list
534a88b5ba8SSam Ravnborg 	 *     - when multiple return probes are registered for the same
535a88b5ba8SSam Ravnborg 	 *       function, the first instance's ret_addr will point to the
536a88b5ba8SSam Ravnborg 	 *       real return address, and all the rest will point to
537a88b5ba8SSam Ravnborg 	 *       kretprobe_trampoline
538a88b5ba8SSam Ravnborg 	 */
539b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
540a88b5ba8SSam Ravnborg 		if (ri->task != current)
541a88b5ba8SSam Ravnborg 			/* another task is sharing our hash bucket */
542a88b5ba8SSam Ravnborg 			continue;
543a88b5ba8SSam Ravnborg 
544a88b5ba8SSam Ravnborg 		if (ri->rp && ri->rp->handler)
545a88b5ba8SSam Ravnborg 			ri->rp->handler(ri, regs);
546a88b5ba8SSam Ravnborg 
547a88b5ba8SSam Ravnborg 		orig_ret_address = (unsigned long)ri->ret_addr;
548a88b5ba8SSam Ravnborg 		recycle_rp_inst(ri, &empty_rp);
549a88b5ba8SSam Ravnborg 
550a88b5ba8SSam Ravnborg 		if (orig_ret_address != trampoline_address)
551a88b5ba8SSam Ravnborg 			/*
552a88b5ba8SSam Ravnborg 			 * This is the real return address. Any other
553a88b5ba8SSam Ravnborg 			 * instances associated with this task are for
554a88b5ba8SSam Ravnborg 			 * other calls deeper on the call stack
555a88b5ba8SSam Ravnborg 			 */
556a88b5ba8SSam Ravnborg 			break;
557a88b5ba8SSam Ravnborg 	}
558a88b5ba8SSam Ravnborg 
559a88b5ba8SSam Ravnborg 	kretprobe_assert(ri, orig_ret_address, trampoline_address);
560a88b5ba8SSam Ravnborg 	regs->tpc = orig_ret_address;
561a88b5ba8SSam Ravnborg 	regs->tnpc = orig_ret_address + 4;
562a88b5ba8SSam Ravnborg 
563a88b5ba8SSam Ravnborg 	reset_current_kprobe();
564a88b5ba8SSam Ravnborg 	kretprobe_hash_unlock(current, &flags);
565a88b5ba8SSam Ravnborg 	preempt_enable_no_resched();
566a88b5ba8SSam Ravnborg 
567b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
568a88b5ba8SSam Ravnborg 		hlist_del(&ri->hlist);
569a88b5ba8SSam Ravnborg 		kfree(ri);
570a88b5ba8SSam Ravnborg 	}
571a88b5ba8SSam Ravnborg 	/*
572a88b5ba8SSam Ravnborg 	 * By returning a non-zero value, we are telling
573a88b5ba8SSam Ravnborg 	 * kprobe_handler() that we don't want the post_handler
574a88b5ba8SSam Ravnborg 	 * to run (and have re-enabled preemption)
575a88b5ba8SSam Ravnborg 	 */
576a88b5ba8SSam Ravnborg 	return 1;
577a88b5ba8SSam Ravnborg }
578a88b5ba8SSam Ravnborg 
579a88b5ba8SSam Ravnborg void kretprobe_trampoline_holder(void)
580a88b5ba8SSam Ravnborg {
581a88b5ba8SSam Ravnborg 	asm volatile(".global kretprobe_trampoline\n"
582a88b5ba8SSam Ravnborg 		     "kretprobe_trampoline:\n"
583a88b5ba8SSam Ravnborg 		     "\tnop\n"
584a88b5ba8SSam Ravnborg 		     "\tnop\n");
585a88b5ba8SSam Ravnborg }
586a88b5ba8SSam Ravnborg static struct kprobe trampoline_p = {
587a88b5ba8SSam Ravnborg 	.addr = (kprobe_opcode_t *) &kretprobe_trampoline,
588a88b5ba8SSam Ravnborg 	.pre_handler = trampoline_probe_handler
589a88b5ba8SSam Ravnborg };
590a88b5ba8SSam Ravnborg 
591a88b5ba8SSam Ravnborg int __init arch_init_kprobes(void)
592a88b5ba8SSam Ravnborg {
593a88b5ba8SSam Ravnborg 	return register_kprobe(&trampoline_p);
594a88b5ba8SSam Ravnborg }
595a88b5ba8SSam Ravnborg 
596a88b5ba8SSam Ravnborg int __kprobes arch_trampoline_kprobe(struct kprobe *p)
597a88b5ba8SSam Ravnborg {
598a88b5ba8SSam Ravnborg 	if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
599a88b5ba8SSam Ravnborg 		return 1;
600a88b5ba8SSam Ravnborg 
601a88b5ba8SSam Ravnborg 	return 0;
602a88b5ba8SSam Ravnborg }
603