xref: /openbmc/linux/arch/csky/kernel/probes/uprobes.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014-2016 Pratyush Anand <panand@redhat.com>
4  */
5 #include <linux/highmem.h>
6 #include <linux/ptrace.h>
7 #include <linux/uprobes.h>
8 #include <asm/cacheflush.h>
9 
10 #include "decode-insn.h"
11 
12 #define UPROBE_TRAP_NR	UINT_MAX
13 
14 unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
15 {
16 	return instruction_pointer(regs);
17 }
18 
19 int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
20 		unsigned long addr)
21 {
22 	probe_opcode_t insn;
23 
24 	insn = *(probe_opcode_t *)(&auprobe->insn[0]);
25 
26 	auprobe->insn_size = is_insn32(insn) ? 4 : 2;
27 
28 	switch (csky_probe_decode_insn(&insn, &auprobe->api)) {
29 	case INSN_REJECTED:
30 		return -EINVAL;
31 
32 	case INSN_GOOD_NO_SLOT:
33 		auprobe->simulate = true;
34 		break;
35 
36 	default:
37 		break;
38 	}
39 
40 	return 0;
41 }
42 
43 int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
44 {
45 	struct uprobe_task *utask = current->utask;
46 
47 	utask->autask.saved_trap_no = current->thread.trap_no;
48 	current->thread.trap_no = UPROBE_TRAP_NR;
49 
50 	instruction_pointer_set(regs, utask->xol_vaddr);
51 
52 	user_enable_single_step(current);
53 
54 	return 0;
55 }
56 
57 int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
58 {
59 	struct uprobe_task *utask = current->utask;
60 
61 	WARN_ON_ONCE(current->thread.trap_no != UPROBE_TRAP_NR);
62 
63 	instruction_pointer_set(regs, utask->vaddr + auprobe->insn_size);
64 
65 	user_disable_single_step(current);
66 
67 	return 0;
68 }
69 
70 bool arch_uprobe_xol_was_trapped(struct task_struct *t)
71 {
72 	if (t->thread.trap_no != UPROBE_TRAP_NR)
73 		return true;
74 
75 	return false;
76 }
77 
78 bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
79 {
80 	probe_opcode_t insn;
81 	unsigned long addr;
82 
83 	if (!auprobe->simulate)
84 		return false;
85 
86 	insn = *(probe_opcode_t *)(&auprobe->insn[0]);
87 	addr = instruction_pointer(regs);
88 
89 	if (auprobe->api.handler)
90 		auprobe->api.handler(insn, addr, regs);
91 
92 	return true;
93 }
94 
95 void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
96 {
97 	struct uprobe_task *utask = current->utask;
98 
99 	/*
100 	 * Task has received a fatal signal, so reset back to probbed
101 	 * address.
102 	 */
103 	instruction_pointer_set(regs, utask->vaddr);
104 
105 	user_disable_single_step(current);
106 }
107 
108 bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
109 		struct pt_regs *regs)
110 {
111 	if (ctx == RP_CHECK_CHAIN_CALL)
112 		return regs->usp <= ret->stack;
113 	else
114 		return regs->usp < ret->stack;
115 }
116 
117 unsigned long
118 arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
119 				  struct pt_regs *regs)
120 {
121 	unsigned long ra;
122 
123 	ra = regs->lr;
124 
125 	regs->lr = trampoline_vaddr;
126 
127 	return ra;
128 }
129 
130 int arch_uprobe_exception_notify(struct notifier_block *self,
131 				 unsigned long val, void *data)
132 {
133 	return NOTIFY_DONE;
134 }
135 
136 int uprobe_breakpoint_handler(struct pt_regs *regs)
137 {
138 	if (uprobe_pre_sstep_notifier(regs))
139 		return 1;
140 
141 	return 0;
142 }
143 
144 int uprobe_single_step_handler(struct pt_regs *regs)
145 {
146 	if (uprobe_post_sstep_notifier(regs))
147 		return 1;
148 
149 	return 0;
150 }
151