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