xref: /linux/arch/powerpc/kernel/kprobes.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27568cb4eSPaul Mackerras /*
37568cb4eSPaul Mackerras  *  Kernel Probes (KProbes)
47568cb4eSPaul Mackerras  *
57568cb4eSPaul Mackerras  * Copyright (C) IBM Corporation, 2002, 2004
67568cb4eSPaul Mackerras  *
77568cb4eSPaul Mackerras  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
87568cb4eSPaul Mackerras  *		Probes initial implementation ( includes contributions from
97568cb4eSPaul Mackerras  *		Rusty Russell).
107568cb4eSPaul Mackerras  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
117568cb4eSPaul Mackerras  *		interface to access function arguments.
127568cb4eSPaul Mackerras  * 2004-Nov	Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
137568cb4eSPaul Mackerras  *		for PPC64
147568cb4eSPaul Mackerras  */
157568cb4eSPaul Mackerras 
167568cb4eSPaul Mackerras #include <linux/kprobes.h>
177568cb4eSPaul Mackerras #include <linux/ptrace.h>
187568cb4eSPaul Mackerras #include <linux/preempt.h>
198a39b05fSPaul Gortmaker #include <linux/extable.h>
201eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
22d48567c9SPeter Zijlstra #include <linux/set_memory.h>
2312af2b83SMike Rapoport (IBM) #include <linux/execmem.h>
242f0143c9SMichael Ellerman #include <asm/code-patching.h>
257568cb4eSPaul Mackerras #include <asm/cacheflush.h>
267568cb4eSPaul Mackerras #include <asm/sstep.h>
277aa5b018SNaveen N. Rao #include <asm/sections.h>
2875346251SJordan Niethe #include <asm/inst.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
30f8279621SKumar Gala 
317568cb4eSPaul Mackerras DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
327568cb4eSPaul Mackerras DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
337568cb4eSPaul Mackerras 
34f438d914SMasami Hiramatsu struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
35f438d914SMasami Hiramatsu 
arch_within_kprobe_blacklist(unsigned long addr)367aa5b018SNaveen N. Rao bool arch_within_kprobe_blacklist(unsigned long addr)
377aa5b018SNaveen N. Rao {
387aa5b018SNaveen N. Rao 	return  (addr >= (unsigned long)__kprobes_text_start &&
397aa5b018SNaveen N. Rao 		 addr < (unsigned long)__kprobes_text_end) ||
407aa5b018SNaveen N. Rao 		(addr >= (unsigned long)_stext &&
417aa5b018SNaveen N. Rao 		 addr < (unsigned long)__head_end);
427aa5b018SNaveen N. Rao }
437aa5b018SNaveen N. Rao 
kprobe_lookup_name(const char * name,unsigned int offset)44290e3070SNaveen N. Rao kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
4549e0b465SNaveen N. Rao {
46acdfe931SNaveen N. Rao 	kprobe_opcode_t *addr = NULL;
4749e0b465SNaveen N. Rao 
487d40aff8SChristophe Leroy #ifdef CONFIG_PPC64_ELF_ABI_V2
4949e0b465SNaveen N. Rao 	/* PPC64 ABIv2 needs local entry point */
5049e0b465SNaveen N. Rao 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
5124bd909eSNaveen N. Rao 	if (addr && !offset) {
5224bd909eSNaveen N. Rao #ifdef CONFIG_KPROBES_ON_FTRACE
5324bd909eSNaveen N. Rao 		unsigned long faddr;
5424bd909eSNaveen N. Rao 		/*
5524bd909eSNaveen N. Rao 		 * Per livepatch.h, ftrace location is always within the first
5624bd909eSNaveen N. Rao 		 * 16 bytes of a function on powerpc with -mprofile-kernel.
5724bd909eSNaveen N. Rao 		 */
5824bd909eSNaveen N. Rao 		faddr = ftrace_location_range((unsigned long)addr,
5924bd909eSNaveen N. Rao 					      (unsigned long)addr + 16);
6024bd909eSNaveen N. Rao 		if (faddr)
6124bd909eSNaveen N. Rao 			addr = (kprobe_opcode_t *)faddr;
6224bd909eSNaveen N. Rao 		else
6324bd909eSNaveen N. Rao #endif
6449e0b465SNaveen N. Rao 			addr = (kprobe_opcode_t *)ppc_function_entry(addr);
6524bd909eSNaveen N. Rao 	}
667d40aff8SChristophe Leroy #elif defined(CONFIG_PPC64_ELF_ABI_V1)
6749e0b465SNaveen N. Rao 	/*
6849e0b465SNaveen N. Rao 	 * 64bit powerpc ABIv1 uses function descriptors:
6949e0b465SNaveen N. Rao 	 * - Check for the dot variant of the symbol first.
7049e0b465SNaveen N. Rao 	 * - If that fails, try looking up the symbol provided.
7149e0b465SNaveen N. Rao 	 *
7249e0b465SNaveen N. Rao 	 * This ensures we always get to the actual symbol and not
7349e0b465SNaveen N. Rao 	 * the descriptor.
7449e0b465SNaveen N. Rao 	 *
7549e0b465SNaveen N. Rao 	 * Also handle <module:symbol> format.
7649e0b465SNaveen N. Rao 	 */
7749e0b465SNaveen N. Rao 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
7849e0b465SNaveen N. Rao 	bool dot_appended = false;
79acdfe931SNaveen N. Rao 	const char *c;
80acdfe931SNaveen N. Rao 	ssize_t ret = 0;
81acdfe931SNaveen N. Rao 	int len = 0;
82acdfe931SNaveen N. Rao 
83acdfe931SNaveen N. Rao 	if ((c = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
84acdfe931SNaveen N. Rao 		c++;
85acdfe931SNaveen N. Rao 		len = c - name;
86acdfe931SNaveen N. Rao 		memcpy(dot_name, name, len);
87acdfe931SNaveen N. Rao 	} else
88acdfe931SNaveen N. Rao 		c = name;
89acdfe931SNaveen N. Rao 
90acdfe931SNaveen N. Rao 	if (*c != '\0' && *c != '.') {
91acdfe931SNaveen N. Rao 		dot_name[len++] = '.';
9249e0b465SNaveen N. Rao 		dot_appended = true;
9349e0b465SNaveen N. Rao 	}
94acdfe931SNaveen N. Rao 	ret = strscpy(dot_name + len, c, KSYM_NAME_LEN);
95acdfe931SNaveen N. Rao 	if (ret > 0)
9649e0b465SNaveen N. Rao 		addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
97acdfe931SNaveen N. Rao 
98acdfe931SNaveen N. Rao 	/* Fallback to the original non-dot symbol lookup */
99acdfe931SNaveen N. Rao 	if (!addr && dot_appended)
10049e0b465SNaveen N. Rao 		addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
10149e0b465SNaveen N. Rao #else
10249e0b465SNaveen N. Rao 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
10349e0b465SNaveen N. Rao #endif
10449e0b465SNaveen N. Rao 
10549e0b465SNaveen N. Rao 	return addr;
10649e0b465SNaveen N. Rao }
10749e0b465SNaveen N. Rao 
arch_kprobe_on_func_entry(unsigned long offset)108cc66bb91SPeter Zijlstra static bool arch_kprobe_on_func_entry(unsigned long offset)
109cc66bb91SPeter Zijlstra {
1107d40aff8SChristophe Leroy #ifdef CONFIG_PPC64_ELF_ABI_V2
111cc66bb91SPeter Zijlstra #ifdef CONFIG_KPROBES_ON_FTRACE
112cc66bb91SPeter Zijlstra 	return offset <= 16;
113cc66bb91SPeter Zijlstra #else
114cc66bb91SPeter Zijlstra 	return offset <= 8;
115cc66bb91SPeter Zijlstra #endif
116cc66bb91SPeter Zijlstra #else
117cc66bb91SPeter Zijlstra 	return !offset;
118cc66bb91SPeter Zijlstra #endif
119cc66bb91SPeter Zijlstra }
120cc66bb91SPeter Zijlstra 
121cc66bb91SPeter Zijlstra /* XXX try and fold the magic of kprobe_lookup_name() in this */
arch_adjust_kprobe_addr(unsigned long addr,unsigned long offset,bool * on_func_entry)122cc66bb91SPeter Zijlstra kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset,
123cc66bb91SPeter Zijlstra 					 bool *on_func_entry)
124cc66bb91SPeter Zijlstra {
125cc66bb91SPeter Zijlstra 	*on_func_entry = arch_kprobe_on_func_entry(offset);
126cc66bb91SPeter Zijlstra 	return (kprobe_opcode_t *)(addr + offset);
127cc66bb91SPeter Zijlstra }
128cc66bb91SPeter Zijlstra 
arch_prepare_kprobe(struct kprobe * p)12971f6e58eSNaveen N. Rao int arch_prepare_kprobe(struct kprobe *p)
1307568cb4eSPaul Mackerras {
1317568cb4eSPaul Mackerras 	int ret = 0;
132b4657f76SJordan Niethe 	struct kprobe *prev;
133c545b9f0SChristophe Leroy 	ppc_inst_t insn = ppc_inst_read(p->addr);
1347568cb4eSPaul Mackerras 
1357568cb4eSPaul Mackerras 	if ((unsigned long)p->addr & 0x03) {
1367568cb4eSPaul Mackerras 		printk("Attempt to register kprobe at an unaligned address\n");
1377568cb4eSPaul Mackerras 		ret = -EINVAL;
13854cdacd7SNaveen N. Rao 	} else if (!can_single_step(ppc_inst_val(insn))) {
13954cdacd7SNaveen N. Rao 		printk("Cannot register a kprobe on instructions that can't be single stepped\n");
1407568cb4eSPaul Mackerras 		ret = -EINVAL;
14182123a3dSNaveen N. Rao 	} else if ((unsigned long)p->addr & ~PAGE_MASK &&
14269d4d6e5SChristophe Leroy 		   ppc_inst_prefixed(ppc_inst_read(p->addr - 1))) {
143b4657f76SJordan Niethe 		printk("Cannot register a kprobe on the second word of prefixed instruction\n");
144b4657f76SJordan Niethe 		ret = -EINVAL;
145b4657f76SJordan Niethe 	}
146b4657f76SJordan Niethe 	prev = get_kprobe(p->addr - 1);
14797f88a3dSLi Huafei 
14897f88a3dSLi Huafei 	/*
14997f88a3dSLi Huafei 	 * When prev is a ftrace-based kprobe, we don't have an insn, and it
15097f88a3dSLi Huafei 	 * doesn't probe for prefixed instruction.
15197f88a3dSLi Huafei 	 */
15297f88a3dSLi Huafei 	if (prev && !kprobe_ftrace(prev) &&
15397f88a3dSLi Huafei 	    ppc_inst_prefixed(ppc_inst_read(prev->ainsn.insn))) {
154b4657f76SJordan Niethe 		printk("Cannot register a kprobe on the second word of prefixed instruction\n");
155b4657f76SJordan Niethe 		ret = -EINVAL;
1567568cb4eSPaul Mackerras 	}
1577568cb4eSPaul Mackerras 
158f8279621SKumar Gala 	/* insn must be on a special executable page on ppc64.  This is
159f8279621SKumar Gala 	 * not explicitly required on ppc32 (right now), but it doesn't hurt */
1607568cb4eSPaul Mackerras 	if (!ret) {
1617568cb4eSPaul Mackerras 		p->ainsn.insn = get_insn_slot();
1627568cb4eSPaul Mackerras 		if (!p->ainsn.insn)
1637568cb4eSPaul Mackerras 			ret = -ENOMEM;
1647568cb4eSPaul Mackerras 	}
1657568cb4eSPaul Mackerras 
16649a2a1b8SAnil S Keshavamurthy 	if (!ret) {
16769d4d6e5SChristophe Leroy 		patch_instruction(p->ainsn.insn, insn);
168f8faaffaSJordan Niethe 		p->opcode = ppc_inst_val(insn);
1697568cb4eSPaul Mackerras 	}
1707568cb4eSPaul Mackerras 
171e6349a95SAnanth N Mavinakayanahalli 	p->ainsn.boostable = 0;
17249a2a1b8SAnil S Keshavamurthy 	return ret;
17349a2a1b8SAnil S Keshavamurthy }
17471f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(arch_prepare_kprobe);
17549a2a1b8SAnil S Keshavamurthy 
arch_arm_kprobe(struct kprobe * p)17671f6e58eSNaveen N. Rao void arch_arm_kprobe(struct kprobe *p)
1777568cb4eSPaul Mackerras {
17812b58492SNaveen N. Rao 	WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(BREAKPOINT_INSTRUCTION)));
1797568cb4eSPaul Mackerras }
18071f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(arch_arm_kprobe);
1817568cb4eSPaul Mackerras 
arch_disarm_kprobe(struct kprobe * p)18271f6e58eSNaveen N. Rao void arch_disarm_kprobe(struct kprobe *p)
1837568cb4eSPaul Mackerras {
18412b58492SNaveen N. Rao 	WARN_ON_ONCE(patch_instruction(p->addr, ppc_inst(p->opcode)));
1857568cb4eSPaul Mackerras }
18671f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(arch_disarm_kprobe);
1877568cb4eSPaul Mackerras 
arch_remove_kprobe(struct kprobe * p)18871f6e58eSNaveen N. Rao void arch_remove_kprobe(struct kprobe *p)
1897568cb4eSPaul Mackerras {
19012941560SMasami Hiramatsu 	if (p->ainsn.insn) {
191b4c6c34aSMasami Hiramatsu 		free_insn_slot(p->ainsn.insn, 0);
19212941560SMasami Hiramatsu 		p->ainsn.insn = NULL;
19312941560SMasami Hiramatsu 	}
1947568cb4eSPaul Mackerras }
19571f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(arch_remove_kprobe);
1967568cb4eSPaul Mackerras 
prepare_singlestep(struct kprobe * p,struct pt_regs * regs)19771f6e58eSNaveen N. Rao static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
1987568cb4eSPaul Mackerras {
19935fd219aSSuzuki K. Poulose 	enable_single_step(regs);
2007568cb4eSPaul Mackerras 
2010ccde0a2SAnanth N Mavinakayanahalli 	/*
2020ccde0a2SAnanth N Mavinakayanahalli 	 * On powerpc we should single step on the original
2030ccde0a2SAnanth N Mavinakayanahalli 	 * instruction even if the probed insn is a trap
2040ccde0a2SAnanth N Mavinakayanahalli 	 * variant as values in regs could play a part in
2050ccde0a2SAnanth N Mavinakayanahalli 	 * if the trap is taken or not
2060ccde0a2SAnanth N Mavinakayanahalli 	 */
20759dc5bfcSNicholas Piggin 	regs_set_return_ip(regs, (unsigned long)p->ainsn.insn);
2087568cb4eSPaul Mackerras }
2097568cb4eSPaul Mackerras 
save_previous_kprobe(struct kprobe_ctlblk * kcb)21071f6e58eSNaveen N. Rao static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
2117568cb4eSPaul Mackerras {
2127568cb4eSPaul Mackerras 	kcb->prev_kprobe.kp = kprobe_running();
2137568cb4eSPaul Mackerras 	kcb->prev_kprobe.status = kcb->kprobe_status;
2147568cb4eSPaul Mackerras 	kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
2157568cb4eSPaul Mackerras }
2167568cb4eSPaul Mackerras 
restore_previous_kprobe(struct kprobe_ctlblk * kcb)21771f6e58eSNaveen N. Rao static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
2187568cb4eSPaul Mackerras {
21969111bacSChristoph Lameter 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
2207568cb4eSPaul Mackerras 	kcb->kprobe_status = kcb->prev_kprobe.status;
2217568cb4eSPaul Mackerras 	kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
2227568cb4eSPaul Mackerras }
2237568cb4eSPaul Mackerras 
set_current_kprobe(struct kprobe * p,struct pt_regs * regs,struct kprobe_ctlblk * kcb)22471f6e58eSNaveen N. Rao static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
2257568cb4eSPaul Mackerras 				struct kprobe_ctlblk *kcb)
2267568cb4eSPaul Mackerras {
22769111bacSChristoph Lameter 	__this_cpu_write(current_kprobe, p);
2287568cb4eSPaul Mackerras 	kcb->kprobe_saved_msr = regs->msr;
2297568cb4eSPaul Mackerras }
2307568cb4eSPaul Mackerras 
try_to_emulate(struct kprobe * p,struct pt_regs * regs)23122085337SNaveen N. Rao static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
2321cabd2f8SNaveen N. Rao {
2331cabd2f8SNaveen N. Rao 	int ret;
234c545b9f0SChristophe Leroy 	ppc_inst_t insn = ppc_inst_read(p->ainsn.insn);
2351cabd2f8SNaveen N. Rao 
2361cabd2f8SNaveen N. Rao 	/* regs->nip is also adjusted if emulate_step returns 1 */
2371cabd2f8SNaveen N. Rao 	ret = emulate_step(regs, insn);
2381cabd2f8SNaveen N. Rao 	if (ret > 0) {
2391cabd2f8SNaveen N. Rao 		/*
2401cabd2f8SNaveen N. Rao 		 * Once this instruction has been boosted
2411cabd2f8SNaveen N. Rao 		 * successfully, set the boostable flag
2421cabd2f8SNaveen N. Rao 		 */
2431cabd2f8SNaveen N. Rao 		if (unlikely(p->ainsn.boostable == 0))
2441cabd2f8SNaveen N. Rao 			p->ainsn.boostable = 1;
2451cabd2f8SNaveen N. Rao 	} else if (ret < 0) {
2461cabd2f8SNaveen N. Rao 		/*
2471cabd2f8SNaveen N. Rao 		 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
2481cabd2f8SNaveen N. Rao 		 * So, we should never get here... but, its still
2491cabd2f8SNaveen N. Rao 		 * good to catch them, just in case...
2501cabd2f8SNaveen N. Rao 		 */
2512a83afe7SMichael Ellerman 		printk("Can't step on instruction %08lx\n", ppc_inst_as_ulong(insn));
2521cabd2f8SNaveen N. Rao 		BUG();
253a7b44038SNaveen N. Rao 	} else {
254a7b44038SNaveen N. Rao 		/*
255a7b44038SNaveen N. Rao 		 * If we haven't previously emulated this instruction, then it
256a7b44038SNaveen N. Rao 		 * can't be boosted. Note it down so we don't try to do so again.
257a7b44038SNaveen N. Rao 		 *
258a7b44038SNaveen N. Rao 		 * If, however, we had emulated this instruction in the past,
259a7b44038SNaveen N. Rao 		 * then this is just an error with the current run (for
260a7b44038SNaveen N. Rao 		 * instance, exceptions due to a load/store). We return 0 so
261a7b44038SNaveen N. Rao 		 * that this is now single-stepped, but continue to try
262a7b44038SNaveen N. Rao 		 * emulating it in subsequent probe hits.
263a7b44038SNaveen N. Rao 		 */
264a7b44038SNaveen N. Rao 		if (unlikely(p->ainsn.boostable != 1))
2651cabd2f8SNaveen N. Rao 			p->ainsn.boostable = -1;
266a7b44038SNaveen N. Rao 	}
2671cabd2f8SNaveen N. Rao 
2681cabd2f8SNaveen N. Rao 	return ret;
2691cabd2f8SNaveen N. Rao }
2709fc84914SMichael Ellerman NOKPROBE_SYMBOL(try_to_emulate);
2711cabd2f8SNaveen N. Rao 
kprobe_handler(struct pt_regs * regs)27271f6e58eSNaveen N. Rao int kprobe_handler(struct pt_regs *regs)
2737568cb4eSPaul Mackerras {
2747568cb4eSPaul Mackerras 	struct kprobe *p;
2757568cb4eSPaul Mackerras 	int ret = 0;
2767568cb4eSPaul Mackerras 	unsigned int *addr = (unsigned int *)regs->nip;
2777568cb4eSPaul Mackerras 	struct kprobe_ctlblk *kcb;
2787568cb4eSPaul Mackerras 
2796cc89badSNaveen N. Rao 	if (user_mode(regs))
2806cc89badSNaveen N. Rao 		return 0;
2816cc89badSNaveen N. Rao 
28243e8f760SPu Lehui 	if (!IS_ENABLED(CONFIG_BOOKE) &&
28343e8f760SPu Lehui 	    (!(regs->msr & MSR_IR) || !(regs->msr & MSR_DR)))
28421f8b2faSChristophe Leroy 		return 0;
28521f8b2faSChristophe Leroy 
2867568cb4eSPaul Mackerras 	/*
2877568cb4eSPaul Mackerras 	 * We don't want to be preempted for the entire
2887568cb4eSPaul Mackerras 	 * duration of kprobe processing
2897568cb4eSPaul Mackerras 	 */
2907568cb4eSPaul Mackerras 	preempt_disable();
2917568cb4eSPaul Mackerras 	kcb = get_kprobe_ctlblk();
2927568cb4eSPaul Mackerras 
2937568cb4eSPaul Mackerras 	p = get_kprobe(addr);
2947568cb4eSPaul Mackerras 	if (!p) {
2959ed5df69SChristophe Leroy 		unsigned int instr;
2969ed5df69SChristophe Leroy 
29725f12ae4SChristoph Hellwig 		if (get_kernel_nofault(instr, addr))
2989ed5df69SChristophe Leroy 			goto no_kprobe;
2999ed5df69SChristophe Leroy 
3009ed5df69SChristophe Leroy 		if (instr != BREAKPOINT_INSTRUCTION) {
3017568cb4eSPaul Mackerras 			/*
3027568cb4eSPaul Mackerras 			 * PowerPC has multiple variants of the "trap"
3037568cb4eSPaul Mackerras 			 * instruction. If the current instruction is a
3047568cb4eSPaul Mackerras 			 * trap variant, it could belong to someone else
3057568cb4eSPaul Mackerras 			 */
3069ed5df69SChristophe Leroy 			if (is_trap(instr))
3077568cb4eSPaul Mackerras 				goto no_kprobe;
3087568cb4eSPaul Mackerras 			/*
3097568cb4eSPaul Mackerras 			 * The breakpoint instruction was removed right
3107568cb4eSPaul Mackerras 			 * after we hit it.  Another cpu has removed
3117568cb4eSPaul Mackerras 			 * either a probepoint or a debugger breakpoint
3127568cb4eSPaul Mackerras 			 * at this address.  In either case, no further
3137568cb4eSPaul Mackerras 			 * handling of this interrupt is appropriate.
3147568cb4eSPaul Mackerras 			 */
3157568cb4eSPaul Mackerras 			ret = 1;
3167568cb4eSPaul Mackerras 		}
3177568cb4eSPaul Mackerras 		/* Not one of ours: let kernel handle it */
3187568cb4eSPaul Mackerras 		goto no_kprobe;
3197568cb4eSPaul Mackerras 	}
3207568cb4eSPaul Mackerras 
321eb4f8e25SChristophe Leroy 	/* Check we're not actually recursing */
322eb4f8e25SChristophe Leroy 	if (kprobe_running()) {
323eb4f8e25SChristophe Leroy 		kprobe_opcode_t insn = *p->ainsn.insn;
324eb4f8e25SChristophe Leroy 		if (kcb->kprobe_status == KPROBE_HIT_SS && is_trap(insn)) {
325eb4f8e25SChristophe Leroy 			/* Turn off 'trace' bits */
32659dc5bfcSNicholas Piggin 			regs_set_return_msr(regs,
32759dc5bfcSNicholas Piggin 				(regs->msr & ~MSR_SINGLESTEP) |
32859dc5bfcSNicholas Piggin 				kcb->kprobe_saved_msr);
329eb4f8e25SChristophe Leroy 			goto no_kprobe;
330eb4f8e25SChristophe Leroy 		}
331eb4f8e25SChristophe Leroy 
332eb4f8e25SChristophe Leroy 		/*
333eb4f8e25SChristophe Leroy 		 * We have reentered the kprobe_handler(), since another probe
334eb4f8e25SChristophe Leroy 		 * was hit while within the handler. We here save the original
335eb4f8e25SChristophe Leroy 		 * kprobes variables and just single step on the instruction of
336eb4f8e25SChristophe Leroy 		 * the new probe without calling any user handlers.
337eb4f8e25SChristophe Leroy 		 */
338eb4f8e25SChristophe Leroy 		save_previous_kprobe(kcb);
339eb4f8e25SChristophe Leroy 		set_current_kprobe(p, regs, kcb);
340eb4f8e25SChristophe Leroy 		kprobes_inc_nmissed_count(p);
341eb4f8e25SChristophe Leroy 		kcb->kprobe_status = KPROBE_REENTER;
342eb4f8e25SChristophe Leroy 		if (p->ainsn.boostable >= 0) {
343eb4f8e25SChristophe Leroy 			ret = try_to_emulate(p, regs);
344eb4f8e25SChristophe Leroy 
345eb4f8e25SChristophe Leroy 			if (ret > 0) {
346eb4f8e25SChristophe Leroy 				restore_previous_kprobe(kcb);
347266b1991SNaveen N. Rao 				preempt_enable();
348eb4f8e25SChristophe Leroy 				return 1;
349eb4f8e25SChristophe Leroy 			}
350eb4f8e25SChristophe Leroy 		}
351eb4f8e25SChristophe Leroy 		prepare_singlestep(p, regs);
352eb4f8e25SChristophe Leroy 		return 1;
353eb4f8e25SChristophe Leroy 	}
354eb4f8e25SChristophe Leroy 
3557568cb4eSPaul Mackerras 	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
3567568cb4eSPaul Mackerras 	set_current_kprobe(p, regs, kcb);
357cce188bdSMasami Hiramatsu 	if (p->pre_handler && p->pre_handler(p, regs)) {
358cce188bdSMasami Hiramatsu 		/* handler changed execution path, so skip ss setup */
359cce188bdSMasami Hiramatsu 		reset_current_kprobe();
360266b1991SNaveen N. Rao 		preempt_enable();
3617568cb4eSPaul Mackerras 		return 1;
362cce188bdSMasami Hiramatsu 	}
3637568cb4eSPaul Mackerras 
364e6349a95SAnanth N Mavinakayanahalli 	if (p->ainsn.boostable >= 0) {
3651cabd2f8SNaveen N. Rao 		ret = try_to_emulate(p, regs);
366e6349a95SAnanth N Mavinakayanahalli 
367e6349a95SAnanth N Mavinakayanahalli 		if (ret > 0) {
368e6349a95SAnanth N Mavinakayanahalli 			if (p->post_handler)
369e6349a95SAnanth N Mavinakayanahalli 				p->post_handler(p, regs, 0);
370e6349a95SAnanth N Mavinakayanahalli 
371e6349a95SAnanth N Mavinakayanahalli 			kcb->kprobe_status = KPROBE_HIT_SSDONE;
372e6349a95SAnanth N Mavinakayanahalli 			reset_current_kprobe();
373266b1991SNaveen N. Rao 			preempt_enable();
374e6349a95SAnanth N Mavinakayanahalli 			return 1;
3751cabd2f8SNaveen N. Rao 		}
376e6349a95SAnanth N Mavinakayanahalli 	}
3777568cb4eSPaul Mackerras 	prepare_singlestep(p, regs);
3787568cb4eSPaul Mackerras 	kcb->kprobe_status = KPROBE_HIT_SS;
3797568cb4eSPaul Mackerras 	return 1;
3807568cb4eSPaul Mackerras 
3817568cb4eSPaul Mackerras no_kprobe:
382266b1991SNaveen N. Rao 	preempt_enable();
3837568cb4eSPaul Mackerras 	return ret;
3847568cb4eSPaul Mackerras }
38571f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(kprobe_handler);
3867568cb4eSPaul Mackerras 
3877568cb4eSPaul Mackerras /*
3887568cb4eSPaul Mackerras  * Called after single-stepping.  p->addr is the address of the
3897568cb4eSPaul Mackerras  * instruction whose first byte has been replaced by the "breakpoint"
3907568cb4eSPaul Mackerras  * instruction.  To avoid the SMP problems that can occur when we
3917568cb4eSPaul Mackerras  * temporarily put back the original opcode to single-step, we
3927568cb4eSPaul Mackerras  * single-stepped a copy of the instruction.  The address of this
3937568cb4eSPaul Mackerras  * copy is p->ainsn.insn.
3947568cb4eSPaul Mackerras  */
kprobe_post_handler(struct pt_regs * regs)39571f6e58eSNaveen N. Rao int kprobe_post_handler(struct pt_regs *regs)
3967568cb4eSPaul Mackerras {
397622cf6f4SJordan Niethe 	int len;
3987568cb4eSPaul Mackerras 	struct kprobe *cur = kprobe_running();
3997568cb4eSPaul Mackerras 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
4007568cb4eSPaul Mackerras 
4016cc89badSNaveen N. Rao 	if (!cur || user_mode(regs))
4027568cb4eSPaul Mackerras 		return 0;
4037568cb4eSPaul Mackerras 
40469d4d6e5SChristophe Leroy 	len = ppc_inst_len(ppc_inst_read(cur->ainsn.insn));
405b76e59d1SKumar Gala 	/* make sure we got here for instruction we have a kprobe on */
406622cf6f4SJordan Niethe 	if (((unsigned long)cur->ainsn.insn + len) != regs->nip)
407b76e59d1SKumar Gala 		return 0;
408b76e59d1SKumar Gala 
4097568cb4eSPaul Mackerras 	if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
4107568cb4eSPaul Mackerras 		kcb->kprobe_status = KPROBE_HIT_SSDONE;
4117568cb4eSPaul Mackerras 		cur->post_handler(cur, regs, 0);
4127568cb4eSPaul Mackerras 	}
4137568cb4eSPaul Mackerras 
414db97bc7fSAnanth N Mavinakayanahalli 	/* Adjust nip to after the single-stepped instruction */
41559dc5bfcSNicholas Piggin 	regs_set_return_ip(regs, (unsigned long)cur->addr + len);
41659dc5bfcSNicholas Piggin 	regs_set_return_msr(regs, regs->msr | kcb->kprobe_saved_msr);
4177568cb4eSPaul Mackerras 
4187568cb4eSPaul Mackerras 	/*Restore back the original saved kprobes variables and continue. */
4197568cb4eSPaul Mackerras 	if (kcb->kprobe_status == KPROBE_REENTER) {
4207568cb4eSPaul Mackerras 		restore_previous_kprobe(kcb);
4217568cb4eSPaul Mackerras 		goto out;
4227568cb4eSPaul Mackerras 	}
4237568cb4eSPaul Mackerras 	reset_current_kprobe();
4247568cb4eSPaul Mackerras out:
425266b1991SNaveen N. Rao 	preempt_enable();
4267568cb4eSPaul Mackerras 
4277568cb4eSPaul Mackerras 	/*
4287568cb4eSPaul Mackerras 	 * if somebody else is singlestepping across a probe point, msr
429f8279621SKumar Gala 	 * will have DE/SE set, in which case, continue the remaining processing
4307568cb4eSPaul Mackerras 	 * of do_debug, as if this is not a probe hit.
4317568cb4eSPaul Mackerras 	 */
432f8279621SKumar Gala 	if (regs->msr & MSR_SINGLESTEP)
4337568cb4eSPaul Mackerras 		return 0;
4347568cb4eSPaul Mackerras 
4357568cb4eSPaul Mackerras 	return 1;
4367568cb4eSPaul Mackerras }
43771f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(kprobe_post_handler);
4387568cb4eSPaul Mackerras 
kprobe_fault_handler(struct pt_regs * regs,int trapnr)43971f6e58eSNaveen N. Rao int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
4407568cb4eSPaul Mackerras {
4417568cb4eSPaul Mackerras 	struct kprobe *cur = kprobe_running();
4427568cb4eSPaul Mackerras 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
44350e21f2bSPrasanna S Panchamukhi 	const struct exception_table_entry *entry;
4447568cb4eSPaul Mackerras 
44550e21f2bSPrasanna S Panchamukhi 	switch(kcb->kprobe_status) {
44650e21f2bSPrasanna S Panchamukhi 	case KPROBE_HIT_SS:
44750e21f2bSPrasanna S Panchamukhi 	case KPROBE_REENTER:
44850e21f2bSPrasanna S Panchamukhi 		/*
44950e21f2bSPrasanna S Panchamukhi 		 * We are here because the instruction being single
45050e21f2bSPrasanna S Panchamukhi 		 * stepped caused a page fault. We reset the current
45150e21f2bSPrasanna S Panchamukhi 		 * kprobe and the nip points back to the probe address
45250e21f2bSPrasanna S Panchamukhi 		 * and allow the page fault handler to continue as a
45350e21f2bSPrasanna S Panchamukhi 		 * normal page fault.
45450e21f2bSPrasanna S Panchamukhi 		 */
45559dc5bfcSNicholas Piggin 		regs_set_return_ip(regs, (unsigned long)cur->addr);
45659dc5bfcSNicholas Piggin 		/* Turn off 'trace' bits */
45759dc5bfcSNicholas Piggin 		regs_set_return_msr(regs,
45859dc5bfcSNicholas Piggin 			(regs->msr & ~MSR_SINGLESTEP) |
45959dc5bfcSNicholas Piggin 			kcb->kprobe_saved_msr);
46050e21f2bSPrasanna S Panchamukhi 		if (kcb->kprobe_status == KPROBE_REENTER)
46150e21f2bSPrasanna S Panchamukhi 			restore_previous_kprobe(kcb);
46250e21f2bSPrasanna S Panchamukhi 		else
46350e21f2bSPrasanna S Panchamukhi 			reset_current_kprobe();
464266b1991SNaveen N. Rao 		preempt_enable();
46550e21f2bSPrasanna S Panchamukhi 		break;
46650e21f2bSPrasanna S Panchamukhi 	case KPROBE_HIT_ACTIVE:
46750e21f2bSPrasanna S Panchamukhi 	case KPROBE_HIT_SSDONE:
46850e21f2bSPrasanna S Panchamukhi 		/*
46950e21f2bSPrasanna S Panchamukhi 		 * In case the user-specified fault handler returned
47050e21f2bSPrasanna S Panchamukhi 		 * zero, try to fix up.
47150e21f2bSPrasanna S Panchamukhi 		 */
47250e21f2bSPrasanna S Panchamukhi 		if ((entry = search_exception_tables(regs->nip)) != NULL) {
47359dc5bfcSNicholas Piggin 			regs_set_return_ip(regs, extable_fixup(entry));
47450e21f2bSPrasanna S Panchamukhi 			return 1;
47550e21f2bSPrasanna S Panchamukhi 		}
4767568cb4eSPaul Mackerras 
47750e21f2bSPrasanna S Panchamukhi 		/*
47850e21f2bSPrasanna S Panchamukhi 		 * fixup_exception() could not handle it,
47950e21f2bSPrasanna S Panchamukhi 		 * Let do_page_fault() fix it.
48050e21f2bSPrasanna S Panchamukhi 		 */
48150e21f2bSPrasanna S Panchamukhi 		break;
48250e21f2bSPrasanna S Panchamukhi 	default:
48350e21f2bSPrasanna S Panchamukhi 		break;
4847568cb4eSPaul Mackerras 	}
4857568cb4eSPaul Mackerras 	return 0;
4867568cb4eSPaul Mackerras }
48771f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(kprobe_fault_handler);
4887568cb4eSPaul Mackerras 
arch_trampoline_kprobe(struct kprobe * p)48971f6e58eSNaveen N. Rao int arch_trampoline_kprobe(struct kprobe *p)
490bf8f6e5bSAnanth N Mavinakayanahalli {
491*19f1bc3fSAbhishek Dubey 	if (p->addr == (kprobe_opcode_t *)&arch_rethook_trampoline)
492bf8f6e5bSAnanth N Mavinakayanahalli 		return 1;
493bf8f6e5bSAnanth N Mavinakayanahalli 
494bf8f6e5bSAnanth N Mavinakayanahalli 	return 0;
495bf8f6e5bSAnanth N Mavinakayanahalli }
49671f6e58eSNaveen N. Rao NOKPROBE_SYMBOL(arch_trampoline_kprobe);
497