11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28b7b80b9SAnanth N Mavinakayanahalli /*
38b7b80b9SAnanth N Mavinakayanahalli * User-space Probes (UProbes) for powerpc
48b7b80b9SAnanth N Mavinakayanahalli *
58b7b80b9SAnanth N Mavinakayanahalli * Copyright IBM Corporation, 2007-2012
68b7b80b9SAnanth N Mavinakayanahalli *
78b7b80b9SAnanth N Mavinakayanahalli * Adapted from the x86 port by Ananth N Mavinakayanahalli <ananth@in.ibm.com>
88b7b80b9SAnanth N Mavinakayanahalli */
98b7b80b9SAnanth N Mavinakayanahalli #include <linux/kernel.h>
108b7b80b9SAnanth N Mavinakayanahalli #include <linux/sched.h>
118b7b80b9SAnanth N Mavinakayanahalli #include <linux/ptrace.h>
128b7b80b9SAnanth N Mavinakayanahalli #include <linux/uprobes.h>
138b7b80b9SAnanth N Mavinakayanahalli #include <linux/uaccess.h>
148b7b80b9SAnanth N Mavinakayanahalli #include <linux/kdebug.h>
158b7b80b9SAnanth N Mavinakayanahalli
168b7b80b9SAnanth N Mavinakayanahalli #include <asm/sstep.h>
1775346251SJordan Niethe #include <asm/inst.h>
188b7b80b9SAnanth N Mavinakayanahalli
198b7b80b9SAnanth N Mavinakayanahalli #define UPROBE_TRAP_NR UINT_MAX
208b7b80b9SAnanth N Mavinakayanahalli
218b7b80b9SAnanth N Mavinakayanahalli /**
22ab07e807SAnanth N Mavinakayanahalli * is_trap_insn - check if the instruction is a trap variant
23ab07e807SAnanth N Mavinakayanahalli * @insn: instruction to be checked.
24ab07e807SAnanth N Mavinakayanahalli * Returns true if @insn is a trap variant.
25ab07e807SAnanth N Mavinakayanahalli */
is_trap_insn(uprobe_opcode_t * insn)26ab07e807SAnanth N Mavinakayanahalli bool is_trap_insn(uprobe_opcode_t *insn)
27ab07e807SAnanth N Mavinakayanahalli {
28ab07e807SAnanth N Mavinakayanahalli return (is_trap(*insn));
29ab07e807SAnanth N Mavinakayanahalli }
30ab07e807SAnanth N Mavinakayanahalli
31ab07e807SAnanth N Mavinakayanahalli /**
328b7b80b9SAnanth N Mavinakayanahalli * arch_uprobe_analyze_insn
338b7b80b9SAnanth N Mavinakayanahalli * @mm: the probed address space.
348b7b80b9SAnanth N Mavinakayanahalli * @arch_uprobe: the probepoint information.
358b7b80b9SAnanth N Mavinakayanahalli * @addr: vaddr to probe.
368b7b80b9SAnanth N Mavinakayanahalli * Return 0 on success or a -ve number on error.
378b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_analyze_insn(struct arch_uprobe * auprobe,struct mm_struct * mm,unsigned long addr)388b7b80b9SAnanth N Mavinakayanahalli int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
398b7b80b9SAnanth N Mavinakayanahalli struct mm_struct *mm, unsigned long addr)
408b7b80b9SAnanth N Mavinakayanahalli {
418b7b80b9SAnanth N Mavinakayanahalli if (addr & 0x03)
428b7b80b9SAnanth N Mavinakayanahalli return -EINVAL;
438b7b80b9SAnanth N Mavinakayanahalli
44d943bc74SRavi Bangoria if (cpu_has_feature(CPU_FTR_ARCH_31) &&
4569d4d6e5SChristophe Leroy ppc_inst_prefixed(ppc_inst_read(auprobe->insn)) &&
46d943bc74SRavi Bangoria (addr & 0x3f) == 60) {
47d943bc74SRavi Bangoria pr_info_ratelimited("Cannot register a uprobe on 64 byte unaligned prefixed instruction\n");
48d943bc74SRavi Bangoria return -EINVAL;
49d943bc74SRavi Bangoria }
50d943bc74SRavi Bangoria
51*54cdacd7SNaveen N. Rao if (!can_single_step(ppc_inst_val(ppc_inst_read(auprobe->insn)))) {
52*54cdacd7SNaveen N. Rao pr_info_ratelimited("Cannot register a uprobe on instructions that can't be single stepped\n");
53*54cdacd7SNaveen N. Rao return -ENOTSUPP;
54*54cdacd7SNaveen N. Rao }
55*54cdacd7SNaveen N. Rao
568b7b80b9SAnanth N Mavinakayanahalli return 0;
578b7b80b9SAnanth N Mavinakayanahalli }
588b7b80b9SAnanth N Mavinakayanahalli
598b7b80b9SAnanth N Mavinakayanahalli /*
608b7b80b9SAnanth N Mavinakayanahalli * arch_uprobe_pre_xol - prepare to execute out of line.
618b7b80b9SAnanth N Mavinakayanahalli * @auprobe: the probepoint information.
628b7b80b9SAnanth N Mavinakayanahalli * @regs: reflects the saved user state of current task.
638b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_pre_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)648b7b80b9SAnanth N Mavinakayanahalli int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
658b7b80b9SAnanth N Mavinakayanahalli {
668b7b80b9SAnanth N Mavinakayanahalli struct arch_uprobe_task *autask = ¤t->utask->autask;
678b7b80b9SAnanth N Mavinakayanahalli
688b7b80b9SAnanth N Mavinakayanahalli autask->saved_trap_nr = current->thread.trap_nr;
698b7b80b9SAnanth N Mavinakayanahalli current->thread.trap_nr = UPROBE_TRAP_NR;
7059dc5bfcSNicholas Piggin regs_set_return_ip(regs, current->utask->xol_vaddr);
7165b2c8f0SOleg Nesterov
7265b2c8f0SOleg Nesterov user_enable_single_step(current);
738b7b80b9SAnanth N Mavinakayanahalli return 0;
748b7b80b9SAnanth N Mavinakayanahalli }
758b7b80b9SAnanth N Mavinakayanahalli
768b7b80b9SAnanth N Mavinakayanahalli /**
778b7b80b9SAnanth N Mavinakayanahalli * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
788b7b80b9SAnanth N Mavinakayanahalli * @regs: Reflects the saved state of the task after it has hit a breakpoint
798b7b80b9SAnanth N Mavinakayanahalli * instruction.
808b7b80b9SAnanth N Mavinakayanahalli * Return the address of the breakpoint instruction.
818b7b80b9SAnanth N Mavinakayanahalli */
uprobe_get_swbp_addr(struct pt_regs * regs)828b7b80b9SAnanth N Mavinakayanahalli unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
838b7b80b9SAnanth N Mavinakayanahalli {
848b7b80b9SAnanth N Mavinakayanahalli return instruction_pointer(regs);
858b7b80b9SAnanth N Mavinakayanahalli }
868b7b80b9SAnanth N Mavinakayanahalli
878b7b80b9SAnanth N Mavinakayanahalli /*
888b7b80b9SAnanth N Mavinakayanahalli * If xol insn itself traps and generates a signal (SIGILL/SIGSEGV/etc),
898b7b80b9SAnanth N Mavinakayanahalli * then detect the case where a singlestepped instruction jumps back to its
908b7b80b9SAnanth N Mavinakayanahalli * own address. It is assumed that anything like do_page_fault/do_trap/etc
918b7b80b9SAnanth N Mavinakayanahalli * sets thread.trap_nr != UINT_MAX.
928b7b80b9SAnanth N Mavinakayanahalli *
938b7b80b9SAnanth N Mavinakayanahalli * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
948b7b80b9SAnanth N Mavinakayanahalli * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
958b7b80b9SAnanth N Mavinakayanahalli * UPROBE_TRAP_NR == UINT_MAX set by arch_uprobe_pre_xol().
968b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_xol_was_trapped(struct task_struct * t)978b7b80b9SAnanth N Mavinakayanahalli bool arch_uprobe_xol_was_trapped(struct task_struct *t)
988b7b80b9SAnanth N Mavinakayanahalli {
998b7b80b9SAnanth N Mavinakayanahalli if (t->thread.trap_nr != UPROBE_TRAP_NR)
1008b7b80b9SAnanth N Mavinakayanahalli return true;
1018b7b80b9SAnanth N Mavinakayanahalli
1028b7b80b9SAnanth N Mavinakayanahalli return false;
1038b7b80b9SAnanth N Mavinakayanahalli }
1048b7b80b9SAnanth N Mavinakayanahalli
1058b7b80b9SAnanth N Mavinakayanahalli /*
1068b7b80b9SAnanth N Mavinakayanahalli * Called after single-stepping. To avoid the SMP problems that can
1078b7b80b9SAnanth N Mavinakayanahalli * occur when we temporarily put back the original opcode to
1088b7b80b9SAnanth N Mavinakayanahalli * single-step, we single-stepped a copy of the instruction.
1098b7b80b9SAnanth N Mavinakayanahalli *
1108b7b80b9SAnanth N Mavinakayanahalli * This function prepares to resume execution after the single-step.
1118b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_post_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)1128b7b80b9SAnanth N Mavinakayanahalli int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
1138b7b80b9SAnanth N Mavinakayanahalli {
1148b7b80b9SAnanth N Mavinakayanahalli struct uprobe_task *utask = current->utask;
1158b7b80b9SAnanth N Mavinakayanahalli
1168b7b80b9SAnanth N Mavinakayanahalli WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
1178b7b80b9SAnanth N Mavinakayanahalli
1188b7b80b9SAnanth N Mavinakayanahalli current->thread.trap_nr = utask->autask.saved_trap_nr;
1198b7b80b9SAnanth N Mavinakayanahalli
1208b7b80b9SAnanth N Mavinakayanahalli /*
1218b7b80b9SAnanth N Mavinakayanahalli * On powerpc, except for loads and stores, most instructions
1228b7b80b9SAnanth N Mavinakayanahalli * including ones that alter code flow (branches, calls, returns)
1238b7b80b9SAnanth N Mavinakayanahalli * are emulated in the kernel. We get here only if the emulation
1248b7b80b9SAnanth N Mavinakayanahalli * support doesn't exist and have to fix-up the next instruction
1258b7b80b9SAnanth N Mavinakayanahalli * to be executed.
1268b7b80b9SAnanth N Mavinakayanahalli */
12759dc5bfcSNicholas Piggin regs_set_return_ip(regs, (unsigned long)ppc_inst_next((void *)utask->vaddr, auprobe->insn));
12865b2c8f0SOleg Nesterov
12965b2c8f0SOleg Nesterov user_disable_single_step(current);
1308b7b80b9SAnanth N Mavinakayanahalli return 0;
1318b7b80b9SAnanth N Mavinakayanahalli }
1328b7b80b9SAnanth N Mavinakayanahalli
1338b7b80b9SAnanth N Mavinakayanahalli /* callback routine for handling exceptions. */
arch_uprobe_exception_notify(struct notifier_block * self,unsigned long val,void * data)1348b7b80b9SAnanth N Mavinakayanahalli int arch_uprobe_exception_notify(struct notifier_block *self,
1358b7b80b9SAnanth N Mavinakayanahalli unsigned long val, void *data)
1368b7b80b9SAnanth N Mavinakayanahalli {
1378b7b80b9SAnanth N Mavinakayanahalli struct die_args *args = data;
1388b7b80b9SAnanth N Mavinakayanahalli struct pt_regs *regs = args->regs;
1398b7b80b9SAnanth N Mavinakayanahalli
1408b7b80b9SAnanth N Mavinakayanahalli /* regs == NULL is a kernel bug */
1418b7b80b9SAnanth N Mavinakayanahalli if (WARN_ON(!regs))
1428b7b80b9SAnanth N Mavinakayanahalli return NOTIFY_DONE;
1438b7b80b9SAnanth N Mavinakayanahalli
1448b7b80b9SAnanth N Mavinakayanahalli /* We are only interested in userspace traps */
1458b7b80b9SAnanth N Mavinakayanahalli if (!user_mode(regs))
1468b7b80b9SAnanth N Mavinakayanahalli return NOTIFY_DONE;
1478b7b80b9SAnanth N Mavinakayanahalli
1488b7b80b9SAnanth N Mavinakayanahalli switch (val) {
1498b7b80b9SAnanth N Mavinakayanahalli case DIE_BPT:
1508b7b80b9SAnanth N Mavinakayanahalli if (uprobe_pre_sstep_notifier(regs))
1518b7b80b9SAnanth N Mavinakayanahalli return NOTIFY_STOP;
1528b7b80b9SAnanth N Mavinakayanahalli break;
1538b7b80b9SAnanth N Mavinakayanahalli case DIE_SSTEP:
1548b7b80b9SAnanth N Mavinakayanahalli if (uprobe_post_sstep_notifier(regs))
1558b7b80b9SAnanth N Mavinakayanahalli return NOTIFY_STOP;
15649a41365SNick Desaulniers break;
1578b7b80b9SAnanth N Mavinakayanahalli default:
1588b7b80b9SAnanth N Mavinakayanahalli break;
1598b7b80b9SAnanth N Mavinakayanahalli }
1608b7b80b9SAnanth N Mavinakayanahalli return NOTIFY_DONE;
1618b7b80b9SAnanth N Mavinakayanahalli }
1628b7b80b9SAnanth N Mavinakayanahalli
1638b7b80b9SAnanth N Mavinakayanahalli /*
1648b7b80b9SAnanth N Mavinakayanahalli * This function gets called when XOL instruction either gets trapped or
1658b7b80b9SAnanth N Mavinakayanahalli * the thread has a fatal signal, so reset the instruction pointer to its
1668b7b80b9SAnanth N Mavinakayanahalli * probed address.
1678b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_abort_xol(struct arch_uprobe * auprobe,struct pt_regs * regs)1688b7b80b9SAnanth N Mavinakayanahalli void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
1698b7b80b9SAnanth N Mavinakayanahalli {
1708b7b80b9SAnanth N Mavinakayanahalli struct uprobe_task *utask = current->utask;
1718b7b80b9SAnanth N Mavinakayanahalli
1728b7b80b9SAnanth N Mavinakayanahalli current->thread.trap_nr = utask->autask.saved_trap_nr;
1738b7b80b9SAnanth N Mavinakayanahalli instruction_pointer_set(regs, utask->vaddr);
17465b2c8f0SOleg Nesterov
17565b2c8f0SOleg Nesterov user_disable_single_step(current);
1768b7b80b9SAnanth N Mavinakayanahalli }
1778b7b80b9SAnanth N Mavinakayanahalli
1788b7b80b9SAnanth N Mavinakayanahalli /*
1798b7b80b9SAnanth N Mavinakayanahalli * See if the instruction can be emulated.
1808b7b80b9SAnanth N Mavinakayanahalli * Returns true if instruction was emulated, false otherwise.
1818b7b80b9SAnanth N Mavinakayanahalli */
arch_uprobe_skip_sstep(struct arch_uprobe * auprobe,struct pt_regs * regs)1828b7b80b9SAnanth N Mavinakayanahalli bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
1838b7b80b9SAnanth N Mavinakayanahalli {
1848b7b80b9SAnanth N Mavinakayanahalli int ret;
1858b7b80b9SAnanth N Mavinakayanahalli
1868b7b80b9SAnanth N Mavinakayanahalli /*
1878b7b80b9SAnanth N Mavinakayanahalli * emulate_step() returns 1 if the insn was successfully emulated.
1888b7b80b9SAnanth N Mavinakayanahalli * For all other cases, we need to single-step in hardware.
1898b7b80b9SAnanth N Mavinakayanahalli */
19069d4d6e5SChristophe Leroy ret = emulate_step(regs, ppc_inst_read(auprobe->insn));
1918b7b80b9SAnanth N Mavinakayanahalli if (ret > 0)
1928b7b80b9SAnanth N Mavinakayanahalli return true;
1938b7b80b9SAnanth N Mavinakayanahalli
1948b7b80b9SAnanth N Mavinakayanahalli return false;
1958b7b80b9SAnanth N Mavinakayanahalli }
196f15706b7SAnton Arapov
197f15706b7SAnton Arapov unsigned long
arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,struct pt_regs * regs)198f15706b7SAnton Arapov arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs)
199f15706b7SAnton Arapov {
200f15706b7SAnton Arapov unsigned long orig_ret_vaddr;
201f15706b7SAnton Arapov
202f15706b7SAnton Arapov orig_ret_vaddr = regs->link;
203f15706b7SAnton Arapov
204f15706b7SAnton Arapov /* Replace the return addr with trampoline addr */
205f15706b7SAnton Arapov regs->link = trampoline_vaddr;
206f15706b7SAnton Arapov
207f15706b7SAnton Arapov return orig_ret_vaddr;
208f15706b7SAnton Arapov }
2092dea1d9cSNaveen N. Rao
arch_uretprobe_is_alive(struct return_instance * ret,enum rp_check ctx,struct pt_regs * regs)2102dea1d9cSNaveen N. Rao bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
2112dea1d9cSNaveen N. Rao struct pt_regs *regs)
2122dea1d9cSNaveen N. Rao {
2132dea1d9cSNaveen N. Rao if (ctx == RP_CHECK_CHAIN_CALL)
2142dea1d9cSNaveen N. Rao return regs->gpr[1] <= ret->stack;
2152dea1d9cSNaveen N. Rao else
2162dea1d9cSNaveen N. Rao return regs->gpr[1] < ret->stack;
2172dea1d9cSNaveen N. Rao }
218