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