1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/highmem.h>
3 #include <linux/ptrace.h>
4 #include <linux/sched.h>
5 #include <linux/uprobes.h>
6 #include <asm/cacheflush.h>
8 #define UPROBE_TRAP_NR UINT_MAX
10 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe,
11 struct mm_struct *mm, unsigned long addr)
14 union loongarch_instruction insn;
19 for (idx = ARRAY_SIZE(auprobe->insn) - 1; idx >= 0; idx--) {
20 insn.word = auprobe->insn[idx];
21 if (insns_not_supported(insn))
25 if (insns_need_simulation(insn)) {
26 auprobe->ixol[0] = larch_insn_gen_nop();
27 auprobe->simulate = true;
29 auprobe->ixol[0] = auprobe->insn[0];
30 auprobe->simulate = false;
33 auprobe->ixol[1] = UPROBE_XOLBP_INSN;
38 int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
40 struct uprobe_task *utask = current->utask;
42 utask->autask.saved_trap_nr = current->thread.trap_nr;
43 current->thread.trap_nr = UPROBE_TRAP_NR;
44 instruction_pointer_set(regs, utask->xol_vaddr);
45 user_enable_single_step(current);
50 int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
52 struct uprobe_task *utask = current->utask;
54 WARN_ON_ONCE(current->thread.trap_nr != UPROBE_TRAP_NR);
55 current->thread.trap_nr = utask->autask.saved_trap_nr;
57 if (auprobe->simulate)
58 instruction_pointer_set(regs, auprobe->resume_era);
60 instruction_pointer_set(regs, utask->vaddr + LOONGARCH_INSN_SIZE);
62 user_disable_single_step(current);
67 void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
69 struct uprobe_task *utask = current->utask;
71 current->thread.trap_nr = utask->autask.saved_trap_nr;
72 instruction_pointer_set(regs, utask->vaddr);
73 user_disable_single_step(current);
76 bool arch_uprobe_xol_was_trapped(struct task_struct *t)
78 if (t->thread.trap_nr != UPROBE_TRAP_NR)
84 bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
86 union loongarch_instruction insn;
88 if (!auprobe->simulate)
91 insn.word = auprobe->insn[0];
92 arch_simulate_insn(insn, regs);
93 auprobe->resume_era = regs->csr_era;
98 unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
101 unsigned long ra = regs->regs[1];
103 regs->regs[1] = trampoline_vaddr;
108 bool arch_uretprobe_is_alive(struct return_instance *ret,
109 enum rp_check ctx, struct pt_regs *regs)
111 if (ctx == RP_CHECK_CHAIN_CALL)
112 return regs->regs[3] <= ret->stack;
114 return regs->regs[3] < ret->stack;
117 int arch_uprobe_exception_notify(struct notifier_block *self,
118 unsigned long val, void *data)
123 bool uprobe_breakpoint_handler(struct pt_regs *regs)
125 if (uprobe_pre_sstep_notifier(regs))
131 bool uprobe_singlestep_handler(struct pt_regs *regs)
133 if (uprobe_post_sstep_notifier(regs))
139 unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
141 return instruction_pointer(regs);
144 void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
145 void *src, unsigned long len)
147 void *kaddr = kmap_local_page(page);
148 void *dst = kaddr + (vaddr & ~PAGE_MASK);
150 memcpy(dst, src, len);
151 flush_icache_range((unsigned long)dst, (unsigned long)dst + len);