1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * User-space Probes (UProbes) for powerpc
5 * Copyright IBM Corporation, 2007-2012
7 * Adapted from the x86 port by Ananth N Mavinakayanahalli <ananth@in.ibm.com>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/ptrace.h>
12 #include <linux/uprobes.h>
13 #include <linux/uaccess.h>
14 #include <linux/kdebug.h>
16 #include <asm/sstep.h>
19 #define UPROBE_TRAP_NR UINT_MAX
22 * is_trap_insn - check if the instruction is a trap variant
23 * @insn: instruction to be checked.
24 * Returns true if @insn is a trap variant.
26 bool is_trap_insn(uprobe_opcode_t
*insn
)
28 return (is_trap(*insn
));
32 * arch_uprobe_analyze_insn
33 * @mm: the probed address space.
34 * @arch_uprobe: the probepoint information.
35 * @addr: vaddr to probe.
36 * Return 0 on success or a -ve number on error.
38 int arch_uprobe_analyze_insn(struct arch_uprobe
*auprobe
,
39 struct mm_struct
*mm
, unsigned long addr
)
48 * arch_uprobe_pre_xol - prepare to execute out of line.
49 * @auprobe: the probepoint information.
50 * @regs: reflects the saved user state of current task.
52 int arch_uprobe_pre_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
54 struct arch_uprobe_task
*autask
= ¤t
->utask
->autask
;
56 autask
->saved_trap_nr
= current
->thread
.trap_nr
;
57 current
->thread
.trap_nr
= UPROBE_TRAP_NR
;
58 regs
->nip
= current
->utask
->xol_vaddr
;
60 user_enable_single_step(current
);
65 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
66 * @regs: Reflects the saved state of the task after it has hit a breakpoint
68 * Return the address of the breakpoint instruction.
70 unsigned long uprobe_get_swbp_addr(struct pt_regs
*regs
)
72 return instruction_pointer(regs
);
76 * If xol insn itself traps and generates a signal (SIGILL/SIGSEGV/etc),
77 * then detect the case where a singlestepped instruction jumps back to its
78 * own address. It is assumed that anything like do_page_fault/do_trap/etc
79 * sets thread.trap_nr != UINT_MAX.
81 * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
82 * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
83 * UPROBE_TRAP_NR == UINT_MAX set by arch_uprobe_pre_xol().
85 bool arch_uprobe_xol_was_trapped(struct task_struct
*t
)
87 if (t
->thread
.trap_nr
!= UPROBE_TRAP_NR
)
94 * Called after single-stepping. To avoid the SMP problems that can
95 * occur when we temporarily put back the original opcode to
96 * single-step, we single-stepped a copy of the instruction.
98 * This function prepares to resume execution after the single-step.
100 int arch_uprobe_post_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
102 struct uprobe_task
*utask
= current
->utask
;
104 WARN_ON_ONCE(current
->thread
.trap_nr
!= UPROBE_TRAP_NR
);
106 current
->thread
.trap_nr
= utask
->autask
.saved_trap_nr
;
109 * On powerpc, except for loads and stores, most instructions
110 * including ones that alter code flow (branches, calls, returns)
111 * are emulated in the kernel. We get here only if the emulation
112 * support doesn't exist and have to fix-up the next instruction
115 regs
->nip
= (unsigned long)ppc_inst_next((void *)utask
->vaddr
, &auprobe
->insn
);
117 user_disable_single_step(current
);
121 /* callback routine for handling exceptions. */
122 int arch_uprobe_exception_notify(struct notifier_block
*self
,
123 unsigned long val
, void *data
)
125 struct die_args
*args
= data
;
126 struct pt_regs
*regs
= args
->regs
;
128 /* regs == NULL is a kernel bug */
132 /* We are only interested in userspace traps */
133 if (!user_mode(regs
))
138 if (uprobe_pre_sstep_notifier(regs
))
142 if (uprobe_post_sstep_notifier(regs
))
152 * This function gets called when XOL instruction either gets trapped or
153 * the thread has a fatal signal, so reset the instruction pointer to its
156 void arch_uprobe_abort_xol(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
158 struct uprobe_task
*utask
= current
->utask
;
160 current
->thread
.trap_nr
= utask
->autask
.saved_trap_nr
;
161 instruction_pointer_set(regs
, utask
->vaddr
);
163 user_disable_single_step(current
);
167 * See if the instruction can be emulated.
168 * Returns true if instruction was emulated, false otherwise.
170 bool arch_uprobe_skip_sstep(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)
175 * emulate_step() returns 1 if the insn was successfully emulated.
176 * For all other cases, we need to single-step in hardware.
178 ret
= emulate_step(regs
, ppc_inst_read(&auprobe
->insn
));
186 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr
, struct pt_regs
*regs
)
188 unsigned long orig_ret_vaddr
;
190 orig_ret_vaddr
= regs
->link
;
192 /* Replace the return addr with trampoline addr */
193 regs
->link
= trampoline_vaddr
;
195 return orig_ret_vaddr
;
198 bool arch_uretprobe_is_alive(struct return_instance
*ret
, enum rp_check ctx
,
199 struct pt_regs
*regs
)
201 if (ctx
== RP_CHECK_CHAIN_CALL
)
202 return regs
->gpr
[1] <= ret
->stack
;
204 return regs
->gpr
[1] < ret
->stack
;