Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 *  Kernel Probes (KProbes)
  3 *  arch/mips/kernel/kprobes.c
  4 *
  5 *  Copyright 2006 Sony Corp.
  6 *  Copyright 2010 Cavium Networks
  7 *
  8 *  Some portions copied from the powerpc version.
  9 *
 10 *   Copyright (C) IBM Corporation, 2002, 2004
 11 *
 12 *  This program is free software; you can redistribute it and/or modify
 13 *  it under the terms of the GNU General Public License as published by
 14 *  the Free Software Foundation; version 2 of the License.
 15 *
 16 *  This program is distributed in the hope that it will be useful,
 17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 *  GNU General Public License for more details.
 20 *
 21 *  You should have received a copy of the GNU General Public License
 22 *  along with this program; if not, write to the Free Software
 23 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 */
 25
 
 
 26#include <linux/kprobes.h>
 27#include <linux/preempt.h>
 
 28#include <linux/kdebug.h>
 29#include <linux/slab.h>
 30
 31#include <asm/ptrace.h>
 
 32#include <asm/break.h>
 33#include <asm/inst.h>
 
 34
 35static const union mips_instruction breakpoint_insn = {
 36	.b_format = {
 37		.opcode = spec_op,
 38		.code = BRK_KPROBE_BP,
 39		.func = break_op
 40	}
 41};
 42
 43static const union mips_instruction breakpoint2_insn = {
 44	.b_format = {
 45		.opcode = spec_op,
 46		.code = BRK_KPROBE_SSTEPBP,
 47		.func = break_op
 48	}
 49};
 50
 51DEFINE_PER_CPU(struct kprobe *, current_kprobe);
 52DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 53
 54static int __kprobes insn_has_delayslot(union mips_instruction insn)
 55{
 56	switch (insn.i_format.opcode) {
 
 
 57
 58		/*
 59		 * This group contains:
 60		 * jr and jalr are in r_format format.
 61		 */
 62	case spec_op:
 63		switch (insn.r_format.func) {
 64		case jr_op:
 65		case jalr_op:
 66			break;
 67		default:
 68			goto insn_ok;
 69		}
 70
 71		/*
 72		 * This group contains:
 73		 * bltz_op, bgez_op, bltzl_op, bgezl_op,
 74		 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
 75		 */
 76	case bcond_op:
 77
 78		/*
 79		 * These are unconditional and in j_format.
 80		 */
 81	case jal_op:
 82	case j_op:
 83
 84		/*
 85		 * These are conditional and in i_format.
 86		 */
 87	case beq_op:
 88	case beql_op:
 89	case bne_op:
 90	case bnel_op:
 91	case blez_op:
 92	case blezl_op:
 93	case bgtz_op:
 94	case bgtzl_op:
 95
 96		/*
 97		 * These are the FPA/cp1 branch instructions.
 98		 */
 99	case cop1_op:
100
101#ifdef CONFIG_CPU_CAVIUM_OCTEON
102	case lwc2_op: /* This is bbit0 on Octeon */
103	case ldc2_op: /* This is bbit032 on Octeon */
104	case swc2_op: /* This is bbit1 on Octeon */
105	case sdc2_op: /* This is bbit132 on Octeon */
106#endif
107		return 1;
108	default:
109		break;
110	}
111insn_ok:
112	return 0;
113}
 
114
115int __kprobes arch_prepare_kprobe(struct kprobe *p)
116{
117	union mips_instruction insn;
118	union mips_instruction prev_insn;
119	int ret = 0;
120
121	prev_insn = p->addr[-1];
122	insn = p->addr[0];
123
124	if (insn_has_delayslot(insn) || insn_has_delayslot(prev_insn)) {
125		pr_notice("Kprobes for branch and jump instructions are not supported\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126		ret = -EINVAL;
127		goto out;
128	}
129
130	/* insn: must be on special executable page on mips. */
131	p->ainsn.insn = get_insn_slot();
132	if (!p->ainsn.insn) {
133		ret = -ENOMEM;
134		goto out;
135	}
136
137	/*
138	 * In the kprobe->ainsn.insn[] array we store the original
139	 * instruction at index zero and a break trap instruction at
140	 * index one.
 
 
 
 
 
 
 
 
141	 */
 
 
 
 
142
143	memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
144	p->ainsn.insn[1] = breakpoint2_insn;
145	p->opcode = *p->addr;
146
147out:
148	return ret;
149}
 
150
151void __kprobes arch_arm_kprobe(struct kprobe *p)
152{
153	*p->addr = breakpoint_insn;
154	flush_insn_slot(p);
155}
 
156
157void __kprobes arch_disarm_kprobe(struct kprobe *p)
158{
159	*p->addr = p->opcode;
160	flush_insn_slot(p);
161}
 
162
163void __kprobes arch_remove_kprobe(struct kprobe *p)
164{
165	free_insn_slot(p->ainsn.insn, 0);
 
 
 
166}
 
167
168static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
169{
170	kcb->prev_kprobe.kp = kprobe_running();
171	kcb->prev_kprobe.status = kcb->kprobe_status;
172	kcb->prev_kprobe.old_SR = kcb->kprobe_old_SR;
173	kcb->prev_kprobe.saved_SR = kcb->kprobe_saved_SR;
174	kcb->prev_kprobe.saved_epc = kcb->kprobe_saved_epc;
175}
176
177static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
178{
179	__get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
180	kcb->kprobe_status = kcb->prev_kprobe.status;
181	kcb->kprobe_old_SR = kcb->prev_kprobe.old_SR;
182	kcb->kprobe_saved_SR = kcb->prev_kprobe.saved_SR;
183	kcb->kprobe_saved_epc = kcb->prev_kprobe.saved_epc;
184}
185
186static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
187			       struct kprobe_ctlblk *kcb)
188{
189	__get_cpu_var(current_kprobe) = p;
190	kcb->kprobe_saved_SR = kcb->kprobe_old_SR = (regs->cp0_status & ST0_IE);
191	kcb->kprobe_saved_epc = regs->cp0_epc;
192}
193
194static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195{
 
 
196	regs->cp0_status &= ~ST0_IE;
197
198	/* single step inline if the instruction is a break */
199	if (p->opcode.word == breakpoint_insn.word ||
200	    p->opcode.word == breakpoint2_insn.word)
201		regs->cp0_epc = (unsigned long)p->addr;
202	else
203		regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
 
 
 
 
204}
205
206static int __kprobes kprobe_handler(struct pt_regs *regs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207{
208	struct kprobe *p;
209	int ret = 0;
210	kprobe_opcode_t *addr;
211	struct kprobe_ctlblk *kcb;
212
213	addr = (kprobe_opcode_t *) regs->cp0_epc;
214
215	/*
216	 * We don't want to be preempted for the entire
217	 * duration of kprobe processing
218	 */
219	preempt_disable();
220	kcb = get_kprobe_ctlblk();
221
222	/* Check we're not actually recursing */
223	if (kprobe_running()) {
224		p = get_kprobe(addr);
225		if (p) {
226			if (kcb->kprobe_status == KPROBE_HIT_SS &&
227			    p->ainsn.insn->word == breakpoint_insn.word) {
228				regs->cp0_status &= ~ST0_IE;
229				regs->cp0_status |= kcb->kprobe_saved_SR;
230				goto no_kprobe;
231			}
232			/*
233			 * We have reentered the kprobe_handler(), since
234			 * another probe was hit while within the handler.
235			 * We here save the original kprobes variables and
236			 * just single step on the instruction of the new probe
237			 * without calling any user handlers.
238			 */
239			save_previous_kprobe(kcb);
240			set_current_kprobe(p, regs, kcb);
241			kprobes_inc_nmissed_count(p);
242			prepare_singlestep(p, regs);
243			kcb->kprobe_status = KPROBE_REENTER;
244			return 1;
245		} else {
246			if (addr->word != breakpoint_insn.word) {
247				/*
248				 * The breakpoint instruction was removed by
249				 * another cpu right after we hit, no further
250				 * handling of this interrupt is appropriate
251				 */
252				ret = 1;
253				goto no_kprobe;
254			}
255			p = __get_cpu_var(current_kprobe);
256			if (p->break_handler && p->break_handler(p, regs))
257				goto ss_probe;
 
 
 
 
 
258		}
259		goto no_kprobe;
260	}
261
262	p = get_kprobe(addr);
263	if (!p) {
264		if (addr->word != breakpoint_insn.word) {
265			/*
266			 * The breakpoint instruction was removed right
267			 * after we hit it.  Another cpu has removed
268			 * either a probepoint or a debugger breakpoint
269			 * at this address.  In either case, no further
270			 * handling of this interrupt is appropriate.
271			 */
272			ret = 1;
273		}
274		/* Not one of ours: let kernel handle it */
275		goto no_kprobe;
276	}
277
278	set_current_kprobe(p, regs, kcb);
279	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
280
281	if (p->pre_handler && p->pre_handler(p, regs)) {
282		/* handler has already set things up, so skip ss setup */
 
 
283		return 1;
284	}
285
286ss_probe:
287	prepare_singlestep(p, regs);
288	kcb->kprobe_status = KPROBE_HIT_SS;
 
 
 
 
 
 
 
289	return 1;
290
291no_kprobe:
292	preempt_enable_no_resched();
293	return ret;
294
295}
296
297/*
298 * Called after single-stepping.  p->addr is the address of the
299 * instruction whose first byte has been replaced by the "break 0"
300 * instruction.  To avoid the SMP problems that can occur when we
301 * temporarily put back the original opcode to single-step, we
302 * single-stepped a copy of the instruction.  The address of this
303 * copy is p->ainsn.insn.
304 *
305 * This function prepares to return from the post-single-step
306 * breakpoint trap.
307 */
308static void __kprobes resume_execution(struct kprobe *p,
309				       struct pt_regs *regs,
310				       struct kprobe_ctlblk *kcb)
311{
312	unsigned long orig_epc = kcb->kprobe_saved_epc;
313	regs->cp0_epc = orig_epc + 4;
314}
315
316static inline int post_kprobe_handler(struct pt_regs *regs)
317{
318	struct kprobe *cur = kprobe_running();
319	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
320
321	if (!cur)
322		return 0;
323
324	if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
325		kcb->kprobe_status = KPROBE_HIT_SSDONE;
326		cur->post_handler(cur, regs, 0);
327	}
328
329	resume_execution(cur, regs, kcb);
330
331	regs->cp0_status |= kcb->kprobe_saved_SR;
332
333	/* Restore back the original saved kprobes variables and continue. */
334	if (kcb->kprobe_status == KPROBE_REENTER) {
335		restore_previous_kprobe(kcb);
336		goto out;
337	}
338	reset_current_kprobe();
339out:
340	preempt_enable_no_resched();
341
342	return 1;
343}
344
345static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
346{
347	struct kprobe *cur = kprobe_running();
348	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
349
350	if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
351		return 1;
352
353	if (kcb->kprobe_status & KPROBE_HIT_SS) {
354		resume_execution(cur, regs, kcb);
355		regs->cp0_status |= kcb->kprobe_old_SR;
356
357		reset_current_kprobe();
358		preempt_enable_no_resched();
359	}
360	return 0;
361}
362
363/*
364 * Wrapper routine for handling exceptions.
365 */
366int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
367				       unsigned long val, void *data)
368{
369
370	struct die_args *args = (struct die_args *)data;
371	int ret = NOTIFY_DONE;
372
373	switch (val) {
374	case DIE_BREAK:
375		if (kprobe_handler(args->regs))
376			ret = NOTIFY_STOP;
377		break;
378	case DIE_SSTEPBP:
379		if (post_kprobe_handler(args->regs))
380			ret = NOTIFY_STOP;
381		break;
382
383	case DIE_PAGE_FAULT:
384		/* kprobe_running() needs smp_processor_id() */
385		preempt_disable();
386
387		if (kprobe_running()
388		    && kprobe_fault_handler(args->regs, args->trapnr))
389			ret = NOTIFY_STOP;
390		preempt_enable();
391		break;
392	default:
393		break;
394	}
395	return ret;
396}
397
398int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
399{
400	struct jprobe *jp = container_of(p, struct jprobe, kp);
401	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
402
403	kcb->jprobe_saved_regs = *regs;
404	kcb->jprobe_saved_sp = regs->regs[29];
405
406	memcpy(kcb->jprobes_stack, (void *)kcb->jprobe_saved_sp,
407	       MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
408
409	regs->cp0_epc = (unsigned long)(jp->entry);
410
411	return 1;
412}
413
414/* Defined in the inline asm below. */
415void jprobe_return_end(void);
416
417void __kprobes jprobe_return(void)
418{
419	/* Assembler quirk necessitates this '0,code' business.  */
420	asm volatile(
421		"break 0,%0\n\t"
422		".globl jprobe_return_end\n"
423		"jprobe_return_end:\n"
424		: : "n" (BRK_KPROBE_BP) : "memory");
425}
426
427int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
428{
429	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
430
431	if (regs->cp0_epc >= (unsigned long)jprobe_return &&
432	    regs->cp0_epc <= (unsigned long)jprobe_return_end) {
433		*regs = kcb->jprobe_saved_regs;
434		memcpy((void *)kcb->jprobe_saved_sp, kcb->jprobes_stack,
435		       MIN_JPROBES_STACK_SIZE(kcb->jprobe_saved_sp));
436		preempt_enable_no_resched();
437
438		return 1;
439	}
440	return 0;
441}
442
443/*
444 * Function return probe trampoline:
445 *	- init_kprobes() establishes a probepoint here
446 *	- When the probed function returns, this probe causes the
447 *	  handlers to fire
448 */
449static void __used kretprobe_trampoline_holder(void)
450{
451	asm volatile(
452		".set push\n\t"
453		/* Keep the assembler from reordering and placing JR here. */
454		".set noreorder\n\t"
455		"nop\n\t"
456		".global kretprobe_trampoline\n"
457		"kretprobe_trampoline:\n\t"
458		"nop\n\t"
459		".set pop"
460		: : : "memory");
461}
462
463void kretprobe_trampoline(void);
464
465void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
466				      struct pt_regs *regs)
467{
468	ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
 
469
470	/* Replace the return addr with trampoline addr */
471	regs->regs[31] = (unsigned long)kretprobe_trampoline;
472}
 
473
474/*
475 * Called when the probe at kretprobe trampoline is hit
476 */
477static int __kprobes trampoline_probe_handler(struct kprobe *p,
478						struct pt_regs *regs)
479{
480	struct kretprobe_instance *ri = NULL;
481	struct hlist_head *head, empty_rp;
482	struct hlist_node *node, *tmp;
483	unsigned long flags, orig_ret_address = 0;
484	unsigned long trampoline_address = (unsigned long)kretprobe_trampoline;
485
486	INIT_HLIST_HEAD(&empty_rp);
487	kretprobe_hash_lock(current, &head, &flags);
488
489	/*
490	 * It is possible to have multiple instances associated with a given
491	 * task either because an multiple functions in the call path
492	 * have a return probe installed on them, and/or more than one return
493	 * return probe was registered for a target function.
494	 *
495	 * We can handle this because:
496	 *     - instances are always inserted at the head of the list
497	 *     - when multiple return probes are registered for the same
498	 *       function, the first instance's ret_addr will point to the
499	 *       real return address, and all the rest will point to
500	 *       kretprobe_trampoline
501	 */
502	hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
503		if (ri->task != current)
504			/* another task is sharing our hash bucket */
505			continue;
506
507		if (ri->rp && ri->rp->handler)
508			ri->rp->handler(ri, regs);
509
510		orig_ret_address = (unsigned long)ri->ret_addr;
511		recycle_rp_inst(ri, &empty_rp);
512
513		if (orig_ret_address != trampoline_address)
514			/*
515			 * This is the real return address. Any other
516			 * instances associated with this task are for
517			 * other calls deeper on the call stack
518			 */
519			break;
520	}
521
522	kretprobe_assert(ri, orig_ret_address, trampoline_address);
523	instruction_pointer(regs) = orig_ret_address;
524
525	reset_current_kprobe();
526	kretprobe_hash_unlock(current, &flags);
527	preempt_enable_no_resched();
528
529	hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
530		hlist_del(&ri->hlist);
531		kfree(ri);
532	}
533	/*
534	 * By returning a non-zero value, we are telling
535	 * kprobe_handler() that we don't want the post_handler
536	 * to run (and have re-enabled preemption)
537	 */
538	return 1;
539}
 
540
541int __kprobes arch_trampoline_kprobe(struct kprobe *p)
542{
543	if (p->addr == (kprobe_opcode_t *)kretprobe_trampoline)
544		return 1;
545
546	return 0;
547}
 
548
549static struct kprobe trampoline_p = {
550	.addr = (kprobe_opcode_t *)kretprobe_trampoline,
551	.pre_handler = trampoline_probe_handler
552};
553
554int __init arch_init_kprobes(void)
555{
556	return register_kprobe(&trampoline_p);
557}
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  Kernel Probes (KProbes)
  4 *  arch/mips/kernel/kprobes.c
  5 *
  6 *  Copyright 2006 Sony Corp.
  7 *  Copyright 2010 Cavium Networks
  8 *
  9 *  Some portions copied from the powerpc version.
 10 *
 11 *   Copyright (C) IBM Corporation, 2002, 2004
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13
 14#define pr_fmt(fmt) "kprobes: " fmt
 15
 16#include <linux/kprobes.h>
 17#include <linux/preempt.h>
 18#include <linux/uaccess.h>
 19#include <linux/kdebug.h>
 20#include <linux/slab.h>
 21
 22#include <asm/ptrace.h>
 23#include <asm/branch.h>
 24#include <asm/break.h>
 25
 26#include "probes-common.h"
 27
 28static const union mips_instruction breakpoint_insn = {
 29	.b_format = {
 30		.opcode = spec_op,
 31		.code = BRK_KPROBE_BP,
 32		.func = break_op
 33	}
 34};
 35
 36static const union mips_instruction breakpoint2_insn = {
 37	.b_format = {
 38		.opcode = spec_op,
 39		.code = BRK_KPROBE_SSTEPBP,
 40		.func = break_op
 41	}
 42};
 43
 44DEFINE_PER_CPU(struct kprobe *, current_kprobe);
 45DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
 46
 47static int insn_has_delayslot(union mips_instruction insn)
 48{
 49	return __insn_has_delay_slot(insn);
 50}
 51NOKPROBE_SYMBOL(insn_has_delayslot);
 52
 53/*
 54 * insn_has_ll_or_sc function checks whether instruction is ll or sc
 55 * one; putting breakpoint on top of atomic ll/sc pair is bad idea;
 56 * so we need to prevent it and refuse kprobes insertion for such
 57 * instructions; cannot do much about breakpoint in the middle of
 58 * ll/sc pair; it is up to user to avoid those places
 59 */
 60static int insn_has_ll_or_sc(union mips_instruction insn)
 61{
 62	int ret = 0;
 
 
 63
 64	switch (insn.i_format.opcode) {
 65	case ll_op:
 66	case lld_op:
 67	case sc_op:
 68	case scd_op:
 69		ret = 1;
 70		break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71	default:
 72		break;
 73	}
 74	return ret;
 
 75}
 76NOKPROBE_SYMBOL(insn_has_ll_or_sc);
 77
 78int arch_prepare_kprobe(struct kprobe *p)
 79{
 80	union mips_instruction insn;
 81	union mips_instruction prev_insn;
 82	int ret = 0;
 83
 
 84	insn = p->addr[0];
 85
 86	if (insn_has_ll_or_sc(insn)) {
 87		pr_notice("Kprobes for ll and sc instructions are not supported\n");
 88		ret = -EINVAL;
 89		goto out;
 90	}
 91
 92	if (copy_from_kernel_nofault(&prev_insn, p->addr - 1,
 93			sizeof(mips_instruction)) == 0 &&
 94	    insn_has_delayslot(prev_insn)) {
 95		pr_notice("Kprobes for branch delayslot are not supported\n");
 96		ret = -EINVAL;
 97		goto out;
 98	}
 99
100	if (__insn_is_compact_branch(insn)) {
101		pr_notice("Kprobes for compact branches are not supported\n");
102		ret = -EINVAL;
103		goto out;
104	}
105
106	/* insn: must be on special executable page on mips. */
107	p->ainsn.insn = get_insn_slot();
108	if (!p->ainsn.insn) {
109		ret = -ENOMEM;
110		goto out;
111	}
112
113	/*
114	 * In the kprobe->ainsn.insn[] array we store the original
115	 * instruction at index zero and a break trap instruction at
116	 * index one.
117	 *
118	 * On MIPS arch if the instruction at probed address is a
119	 * branch instruction, we need to execute the instruction at
120	 * Branch Delayslot (BD) at the time of probe hit. As MIPS also
121	 * doesn't have single stepping support, the BD instruction can
122	 * not be executed in-line and it would be executed on SSOL slot
123	 * using a normal breakpoint instruction in the next slot.
124	 * So, read the instruction and save it for later execution.
125	 */
126	if (insn_has_delayslot(insn))
127		memcpy(&p->ainsn.insn[0], p->addr + 1, sizeof(kprobe_opcode_t));
128	else
129		memcpy(&p->ainsn.insn[0], p->addr, sizeof(kprobe_opcode_t));
130
 
131	p->ainsn.insn[1] = breakpoint2_insn;
132	p->opcode = *p->addr;
133
134out:
135	return ret;
136}
137NOKPROBE_SYMBOL(arch_prepare_kprobe);
138
139void arch_arm_kprobe(struct kprobe *p)
140{
141	*p->addr = breakpoint_insn;
142	flush_insn_slot(p);
143}
144NOKPROBE_SYMBOL(arch_arm_kprobe);
145
146void arch_disarm_kprobe(struct kprobe *p)
147{
148	*p->addr = p->opcode;
149	flush_insn_slot(p);
150}
151NOKPROBE_SYMBOL(arch_disarm_kprobe);
152
153void arch_remove_kprobe(struct kprobe *p)
154{
155	if (p->ainsn.insn) {
156		free_insn_slot(p->ainsn.insn, 0);
157		p->ainsn.insn = NULL;
158	}
159}
160NOKPROBE_SYMBOL(arch_remove_kprobe);
161
162static void save_previous_kprobe(struct kprobe_ctlblk *kcb)
163{
164	kcb->prev_kprobe.kp = kprobe_running();
165	kcb->prev_kprobe.status = kcb->kprobe_status;
166	kcb->prev_kprobe.old_SR = kcb->kprobe_old_SR;
167	kcb->prev_kprobe.saved_SR = kcb->kprobe_saved_SR;
168	kcb->prev_kprobe.saved_epc = kcb->kprobe_saved_epc;
169}
170
171static void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
172{
173	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
174	kcb->kprobe_status = kcb->prev_kprobe.status;
175	kcb->kprobe_old_SR = kcb->prev_kprobe.old_SR;
176	kcb->kprobe_saved_SR = kcb->prev_kprobe.saved_SR;
177	kcb->kprobe_saved_epc = kcb->prev_kprobe.saved_epc;
178}
179
180static void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
181			       struct kprobe_ctlblk *kcb)
182{
183	__this_cpu_write(current_kprobe, p);
184	kcb->kprobe_saved_SR = kcb->kprobe_old_SR = (regs->cp0_status & ST0_IE);
185	kcb->kprobe_saved_epc = regs->cp0_epc;
186}
187
188/**
189 * evaluate_branch_instrucion -
190 *
191 * Evaluate the branch instruction at probed address during probe hit. The
192 * result of evaluation would be the updated epc. The insturction in delayslot
193 * would actually be single stepped using a normal breakpoint) on SSOL slot.
194 *
195 * The result is also saved in the kprobe control block for later use,
196 * in case we need to execute the delayslot instruction. The latter will be
197 * false for NOP instruction in dealyslot and the branch-likely instructions
198 * when the branch is taken. And for those cases we set a flag as
199 * SKIP_DELAYSLOT in the kprobe control block
200 */
201static int evaluate_branch_instruction(struct kprobe *p, struct pt_regs *regs,
202					struct kprobe_ctlblk *kcb)
203{
204	union mips_instruction insn = p->opcode;
205	long epc;
206	int ret = 0;
207
208	epc = regs->cp0_epc;
209	if (epc & 3)
210		goto unaligned;
211
212	if (p->ainsn.insn->word == 0)
213		kcb->flags |= SKIP_DELAYSLOT;
214	else
215		kcb->flags &= ~SKIP_DELAYSLOT;
216
217	ret = __compute_return_epc_for_insn(regs, insn);
218	if (ret < 0)
219		return ret;
220
221	if (ret == BRANCH_LIKELY_TAKEN)
222		kcb->flags |= SKIP_DELAYSLOT;
223
224	kcb->target_epc = regs->cp0_epc;
225
226	return 0;
227
228unaligned:
229	pr_notice("Failed to emulate branch instruction because of unaligned epc - sending SIGBUS to %s.\n", current->comm);
230	force_sig(SIGBUS);
231	return -EFAULT;
232
233}
234
235static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs,
236						struct kprobe_ctlblk *kcb)
237{
238	int ret = 0;
239
240	regs->cp0_status &= ~ST0_IE;
241
242	/* single step inline if the instruction is a break */
243	if (p->opcode.word == breakpoint_insn.word ||
244	    p->opcode.word == breakpoint2_insn.word)
245		regs->cp0_epc = (unsigned long)p->addr;
246	else if (insn_has_delayslot(p->opcode)) {
247		ret = evaluate_branch_instruction(p, regs, kcb);
248		if (ret < 0)
249			return;
250	}
251	regs->cp0_epc = (unsigned long)&p->ainsn.insn[0];
252}
253
254/*
255 * Called after single-stepping.  p->addr is the address of the
256 * instruction whose first byte has been replaced by the "break 0"
257 * instruction.	 To avoid the SMP problems that can occur when we
258 * temporarily put back the original opcode to single-step, we
259 * single-stepped a copy of the instruction.  The address of this
260 * copy is p->ainsn.insn.
261 *
262 * This function prepares to return from the post-single-step
263 * breakpoint trap. In case of branch instructions, the target
264 * epc to be restored.
265 */
266static void resume_execution(struct kprobe *p,
267				       struct pt_regs *regs,
268				       struct kprobe_ctlblk *kcb)
269{
270	if (insn_has_delayslot(p->opcode))
271		regs->cp0_epc = kcb->target_epc;
272	else {
273		unsigned long orig_epc = kcb->kprobe_saved_epc;
274		regs->cp0_epc = orig_epc + 4;
275	}
276}
277NOKPROBE_SYMBOL(resume_execution);
278
279static int kprobe_handler(struct pt_regs *regs)
280{
281	struct kprobe *p;
282	int ret = 0;
283	kprobe_opcode_t *addr;
284	struct kprobe_ctlblk *kcb;
285
286	addr = (kprobe_opcode_t *) regs->cp0_epc;
287
288	/*
289	 * We don't want to be preempted for the entire
290	 * duration of kprobe processing
291	 */
292	preempt_disable();
293	kcb = get_kprobe_ctlblk();
294
295	/* Check we're not actually recursing */
296	if (kprobe_running()) {
297		p = get_kprobe(addr);
298		if (p) {
299			if (kcb->kprobe_status == KPROBE_HIT_SS &&
300			    p->ainsn.insn->word == breakpoint_insn.word) {
301				regs->cp0_status &= ~ST0_IE;
302				regs->cp0_status |= kcb->kprobe_saved_SR;
303				goto no_kprobe;
304			}
305			/*
306			 * We have reentered the kprobe_handler(), since
307			 * another probe was hit while within the handler.
308			 * We here save the original kprobes variables and
309			 * just single step on the instruction of the new probe
310			 * without calling any user handlers.
311			 */
312			save_previous_kprobe(kcb);
313			set_current_kprobe(p, regs, kcb);
314			kprobes_inc_nmissed_count(p);
315			prepare_singlestep(p, regs, kcb);
316			kcb->kprobe_status = KPROBE_REENTER;
317			if (kcb->flags & SKIP_DELAYSLOT) {
318				resume_execution(p, regs, kcb);
319				restore_previous_kprobe(kcb);
320				preempt_enable_no_resched();
 
 
 
 
 
 
321			}
322			return 1;
323		} else if (addr->word != breakpoint_insn.word) {
324			/*
325			 * The breakpoint instruction was removed by
326			 * another cpu right after we hit, no further
327			 * handling of this interrupt is appropriate
328			 */
329			ret = 1;
330		}
331		goto no_kprobe;
332	}
333
334	p = get_kprobe(addr);
335	if (!p) {
336		if (addr->word != breakpoint_insn.word) {
337			/*
338			 * The breakpoint instruction was removed right
339			 * after we hit it.  Another cpu has removed
340			 * either a probepoint or a debugger breakpoint
341			 * at this address.  In either case, no further
342			 * handling of this interrupt is appropriate.
343			 */
344			ret = 1;
345		}
346		/* Not one of ours: let kernel handle it */
347		goto no_kprobe;
348	}
349
350	set_current_kprobe(p, regs, kcb);
351	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
352
353	if (p->pre_handler && p->pre_handler(p, regs)) {
354		/* handler has already set things up, so skip ss setup */
355		reset_current_kprobe();
356		preempt_enable_no_resched();
357		return 1;
358	}
359
360	prepare_singlestep(p, regs, kcb);
361	if (kcb->flags & SKIP_DELAYSLOT) {
362		kcb->kprobe_status = KPROBE_HIT_SSDONE;
363		if (p->post_handler)
364			p->post_handler(p, regs, 0);
365		resume_execution(p, regs, kcb);
366		preempt_enable_no_resched();
367	} else
368		kcb->kprobe_status = KPROBE_HIT_SS;
369
370	return 1;
371
372no_kprobe:
373	preempt_enable_no_resched();
374	return ret;
375
376}
377NOKPROBE_SYMBOL(kprobe_handler);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
379static inline int post_kprobe_handler(struct pt_regs *regs)
380{
381	struct kprobe *cur = kprobe_running();
382	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
383
384	if (!cur)
385		return 0;
386
387	if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
388		kcb->kprobe_status = KPROBE_HIT_SSDONE;
389		cur->post_handler(cur, regs, 0);
390	}
391
392	resume_execution(cur, regs, kcb);
393
394	regs->cp0_status |= kcb->kprobe_saved_SR;
395
396	/* Restore back the original saved kprobes variables and continue. */
397	if (kcb->kprobe_status == KPROBE_REENTER) {
398		restore_previous_kprobe(kcb);
399		goto out;
400	}
401	reset_current_kprobe();
402out:
403	preempt_enable_no_resched();
404
405	return 1;
406}
407
408int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
409{
410	struct kprobe *cur = kprobe_running();
411	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
412
 
 
 
413	if (kcb->kprobe_status & KPROBE_HIT_SS) {
414		resume_execution(cur, regs, kcb);
415		regs->cp0_status |= kcb->kprobe_old_SR;
416
417		reset_current_kprobe();
418		preempt_enable_no_resched();
419	}
420	return 0;
421}
422
423/*
424 * Wrapper routine for handling exceptions.
425 */
426int kprobe_exceptions_notify(struct notifier_block *self,
427				       unsigned long val, void *data)
428{
429
430	struct die_args *args = (struct die_args *)data;
431	int ret = NOTIFY_DONE;
432
433	switch (val) {
434	case DIE_BREAK:
435		if (kprobe_handler(args->regs))
436			ret = NOTIFY_STOP;
437		break;
438	case DIE_SSTEPBP:
439		if (post_kprobe_handler(args->regs))
440			ret = NOTIFY_STOP;
441		break;
442
443	case DIE_PAGE_FAULT:
444		/* kprobe_running() needs smp_processor_id() */
445		preempt_disable();
446
447		if (kprobe_running()
448		    && kprobe_fault_handler(args->regs, args->trapnr))
449			ret = NOTIFY_STOP;
450		preempt_enable();
451		break;
452	default:
453		break;
454	}
455	return ret;
456}
457NOKPROBE_SYMBOL(kprobe_exceptions_notify);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
459/*
460 * Function return probe trampoline:
461 *	- init_kprobes() establishes a probepoint here
462 *	- When the probed function returns, this probe causes the
463 *	  handlers to fire
464 */
465static void __used kretprobe_trampoline_holder(void)
466{
467	asm volatile(
468		".set push\n\t"
469		/* Keep the assembler from reordering and placing JR here. */
470		".set noreorder\n\t"
471		"nop\n\t"
472		".global __kretprobe_trampoline\n"
473		"__kretprobe_trampoline:\n\t"
474		"nop\n\t"
475		".set pop"
476		: : : "memory");
477}
478
479void __kretprobe_trampoline(void);
480
481void arch_prepare_kretprobe(struct kretprobe_instance *ri,
482				      struct pt_regs *regs)
483{
484	ri->ret_addr = (kprobe_opcode_t *) regs->regs[31];
485	ri->fp = NULL;
486
487	/* Replace the return addr with trampoline addr */
488	regs->regs[31] = (unsigned long)__kretprobe_trampoline;
489}
490NOKPROBE_SYMBOL(arch_prepare_kretprobe);
491
492/*
493 * Called when the probe at kretprobe trampoline is hit
494 */
495static int trampoline_probe_handler(struct kprobe *p,
496						struct pt_regs *regs)
497{
498	instruction_pointer(regs) = __kretprobe_trampoline_handler(regs, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499	/*
500	 * By returning a non-zero value, we are telling
501	 * kprobe_handler() that we don't want the post_handler
502	 * to run (and have re-enabled preemption)
503	 */
504	return 1;
505}
506NOKPROBE_SYMBOL(trampoline_probe_handler);
507
508int arch_trampoline_kprobe(struct kprobe *p)
509{
510	if (p->addr == (kprobe_opcode_t *)__kretprobe_trampoline)
511		return 1;
512
513	return 0;
514}
515NOKPROBE_SYMBOL(arch_trampoline_kprobe);
516
517static struct kprobe trampoline_p = {
518	.addr = (kprobe_opcode_t *)__kretprobe_trampoline,
519	.pre_handler = trampoline_probe_handler
520};
521
522int __init arch_init_kprobes(void)
523{
524	return register_kprobe(&trampoline_p);
525}