xref: /openbmc/linux/arch/csky/kernel/ptrace.c (revision ed1666f6)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3 
4 #include <linux/elf.h>
5 #include <linux/errno.h>
6 #include <linux/kernel.h>
7 #include <linux/mm.h>
8 #include <linux/ptrace.h>
9 #include <linux/regset.h>
10 #include <linux/sched.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/signal.h>
13 #include <linux/smp.h>
14 #include <linux/uaccess.h>
15 #include <linux/user.h>
16 
17 #include <asm/thread_info.h>
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <asm/processor.h>
21 #include <asm/asm-offsets.h>
22 
23 #include <abi/regdef.h>
24 
25 /* sets the trace bits. */
26 #define TRACE_MODE_SI      (1 << 14)
27 #define TRACE_MODE_RUN     0
28 #define TRACE_MODE_MASK    ~(0x3 << 14)
29 
30 /*
31  * Make sure the single step bit is not set.
32  */
33 static void singlestep_disable(struct task_struct *tsk)
34 {
35 	struct pt_regs *regs;
36 
37 	regs = task_pt_regs(tsk);
38 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
39 }
40 
41 static void singlestep_enable(struct task_struct *tsk)
42 {
43 	struct pt_regs *regs;
44 
45 	regs = task_pt_regs(tsk);
46 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
47 }
48 
49 /*
50  * Make sure the single step bit is set.
51  */
52 void user_enable_single_step(struct task_struct *child)
53 {
54 	singlestep_enable(child);
55 }
56 
57 void user_disable_single_step(struct task_struct *child)
58 {
59 	singlestep_disable(child);
60 }
61 
62 enum csky_regset {
63 	REGSET_GPR,
64 	REGSET_FPR,
65 };
66 
67 static int gpr_get(struct task_struct *target,
68 		   const struct user_regset *regset,
69 		   unsigned int pos, unsigned int count,
70 		   void *kbuf, void __user *ubuf)
71 {
72 	struct pt_regs *regs;
73 
74 	regs = task_pt_regs(target);
75 
76 	/* Abiv1 regs->tls is fake and we need sync here. */
77 	regs->tls = task_thread_info(target)->tp_value;
78 
79 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
80 }
81 
82 static int gpr_set(struct task_struct *target,
83 		    const struct user_regset *regset,
84 		    unsigned int pos, unsigned int count,
85 		    const void *kbuf, const void __user *ubuf)
86 {
87 	int ret;
88 	struct pt_regs regs;
89 
90 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
91 	if (ret)
92 		return ret;
93 
94 	regs.sr = task_pt_regs(target)->sr;
95 #ifdef CONFIG_CPU_HAS_HILO
96 	regs.dcsr = task_pt_regs(target)->dcsr;
97 #endif
98 	task_thread_info(target)->tp_value = regs.tls;
99 
100 	*task_pt_regs(target) = regs;
101 
102 	return 0;
103 }
104 
105 static int fpr_get(struct task_struct *target,
106 		   const struct user_regset *regset,
107 		   unsigned int pos, unsigned int count,
108 		   void *kbuf, void __user *ubuf)
109 {
110 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
111 
112 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
113 	int i;
114 	struct user_fp tmp = *regs;
115 
116 	for (i = 0; i < 16; i++) {
117 		tmp.vr[i*4] = regs->vr[i*2];
118 		tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
119 	}
120 
121 	for (i = 0; i < 32; i++)
122 		tmp.vr[64 + i] = regs->vr[32 + i];
123 
124 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
125 #else
126 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
127 #endif
128 }
129 
130 static int fpr_set(struct task_struct *target,
131 		   const struct user_regset *regset,
132 		   unsigned int pos, unsigned int count,
133 		   const void *kbuf, const void __user *ubuf)
134 {
135 	int ret;
136 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
137 
138 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
139 	int i;
140 	struct user_fp tmp;
141 
142 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
143 
144 	*regs = tmp;
145 
146 	for (i = 0; i < 16; i++) {
147 		regs->vr[i*2] = tmp.vr[i*4];
148 		regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
149 	}
150 
151 	for (i = 0; i < 32; i++)
152 		regs->vr[32 + i] = tmp.vr[64 + i];
153 #else
154 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
155 #endif
156 
157 	return ret;
158 }
159 
160 static const struct user_regset csky_regsets[] = {
161 	[REGSET_GPR] = {
162 		.core_note_type = NT_PRSTATUS,
163 		.n = sizeof(struct pt_regs) / sizeof(u32),
164 		.size = sizeof(u32),
165 		.align = sizeof(u32),
166 		.get = &gpr_get,
167 		.set = &gpr_set,
168 	},
169 	[REGSET_FPR] = {
170 		.core_note_type = NT_PRFPREG,
171 		.n = sizeof(struct user_fp) / sizeof(u32),
172 		.size = sizeof(u32),
173 		.align = sizeof(u32),
174 		.get = &fpr_get,
175 		.set = &fpr_set,
176 	},
177 };
178 
179 static const struct user_regset_view user_csky_view = {
180 	.name = "csky",
181 	.e_machine = ELF_ARCH,
182 	.regsets = csky_regsets,
183 	.n = ARRAY_SIZE(csky_regsets),
184 };
185 
186 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
187 {
188 	return &user_csky_view;
189 }
190 
191 void ptrace_disable(struct task_struct *child)
192 {
193 	singlestep_disable(child);
194 }
195 
196 long arch_ptrace(struct task_struct *child, long request,
197 		 unsigned long addr, unsigned long data)
198 {
199 	long ret = -EIO;
200 
201 	switch (request) {
202 	default:
203 		ret = ptrace_request(child, request, addr, data);
204 		break;
205 	}
206 
207 	return ret;
208 }
209 
210 /*
211  * If process's system calls is traces, do some corresponding handles in this
212  * function before entering system call function and after exiting system call
213  * function.
214  */
215 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
216 {
217 	long saved_why;
218 	/*
219 	 * Save saved_why, why is used to denote syscall entry/exit;
220 	 * why = 0:entry, why = 1: exit
221 	 */
222 	saved_why = regs->regs[SYSTRACE_SAVENUM];
223 	regs->regs[SYSTRACE_SAVENUM] = why;
224 
225 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
226 					? 0x80 : 0));
227 
228 	/*
229 	 * this isn't the same as continuing with a signal, but it will do
230 	 * for normal use.  strace only continues with a signal if the
231 	 * stopping signal is not SIGTRAP.  -brl
232 	 */
233 	if (current->exit_code) {
234 		send_sig(current->exit_code, current, 1);
235 		current->exit_code = 0;
236 	}
237 
238 	regs->regs[SYSTRACE_SAVENUM] = saved_why;
239 }
240 
241 extern void show_stack(struct task_struct *task, unsigned long *stack);
242 void show_regs(struct pt_regs *fp)
243 {
244 	unsigned long   *sp;
245 	unsigned char   *tp;
246 	int	i;
247 
248 	pr_info("\nCURRENT PROCESS:\n\n");
249 	pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
250 
251 	if (current->mm) {
252 		pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
253 		       (int) current->mm->start_code,
254 		       (int) current->mm->end_code,
255 		       (int) current->mm->start_data,
256 		       (int) current->mm->end_data,
257 		       (int) current->mm->end_data,
258 		       (int) current->mm->brk);
259 		pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
260 		       (int) current->mm->start_stack,
261 		       (int) (((unsigned long) current) + 2 * PAGE_SIZE));
262 	}
263 
264 	pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
265 	pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
266 	pr_info("SP: 0x%08lx\n", (long)fp);
267 	pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
268 	pr_info("PSR: 0x%08lx\n", (long)fp->sr);
269 
270 	pr_info(" a0: 0x%08lx   a1: 0x%08lx   a2: 0x%08lx   a3: 0x%08lx\n",
271 		fp->a0, fp->a1, fp->a2, fp->a3);
272 #if defined(__CSKYABIV2__)
273 	pr_info(" r4: 0x%08lx   r5: 0x%08lx   r6: 0x%08lx   r7: 0x%08lx\n",
274 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
275 	pr_info(" r8: 0x%08lx   r9: 0x%08lx  r10: 0x%08lx  r11: 0x%08lx\n",
276 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
277 	pr_info("r12: 0x%08lx  r13: 0x%08lx  r15: 0x%08lx\n",
278 		fp->regs[8], fp->regs[9], fp->lr);
279 	pr_info("r16: 0x%08lx  r17: 0x%08lx  r18: 0x%08lx  r19: 0x%08lx\n",
280 		fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
281 	pr_info("r20: 0x%08lx  r21: 0x%08lx  r22: 0x%08lx  r23: 0x%08lx\n",
282 		fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
283 	pr_info("r24: 0x%08lx  r25: 0x%08lx  r26: 0x%08lx  r27: 0x%08lx\n",
284 		fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
285 	pr_info("r28: 0x%08lx  r29: 0x%08lx  r30: 0x%08lx  tls: 0x%08lx\n",
286 		fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
287 	pr_info(" hi: 0x%08lx   lo: 0x%08lx\n",
288 		fp->rhi, fp->rlo);
289 #else
290 	pr_info(" r6: 0x%08lx   r7: 0x%08lx   r8: 0x%08lx   r9: 0x%08lx\n",
291 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
292 	pr_info("r10: 0x%08lx  r11: 0x%08lx  r12: 0x%08lx  r13: 0x%08lx\n",
293 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
294 	pr_info("r14: 0x%08lx   r1: 0x%08lx  r15: 0x%08lx\n",
295 		fp->regs[8], fp->regs[9], fp->lr);
296 #endif
297 
298 	pr_info("\nCODE:");
299 	tp = ((unsigned char *) fp->pc) - 0x20;
300 	tp += ((int)tp % 4) ? 2 : 0;
301 	for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
302 		if ((i % 0x10) == 0)
303 			pr_cont("\n%08x: ", (int) (tp + i));
304 		pr_cont("%08x ", (int) *sp++);
305 	}
306 	pr_cont("\n");
307 
308 	pr_info("\nKERNEL STACK:");
309 	tp = ((unsigned char *) fp) - 0x40;
310 	for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
311 		if ((i % 0x10) == 0)
312 			pr_cont("\n%08x: ", (int) (tp + i));
313 		pr_cont("%08x ", (int) *sp++);
314 	}
315 	pr_cont("\n");
316 
317 	show_stack(NULL, (unsigned long *)fp->regs[4]);
318 	return;
319 }
320