xref: /linux/arch/csky/kernel/ptrace.c (revision 789154c2ad74c29c3c60c5136c1785745abe4897)
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 	if (child->thread.esp0 == 0)
54 		return;
55 	singlestep_enable(child);
56 }
57 
58 void user_disable_single_step(struct task_struct *child)
59 {
60 	if (child->thread.esp0 == 0)
61 		return;
62 	singlestep_disable(child);
63 }
64 
65 enum csky_regset {
66 	REGSET_GPR,
67 	REGSET_FPR,
68 };
69 
70 static int gpr_get(struct task_struct *target,
71 		   const struct user_regset *regset,
72 		   unsigned int pos, unsigned int count,
73 		   void *kbuf, void __user *ubuf)
74 {
75 	struct pt_regs *regs;
76 
77 	regs = task_pt_regs(target);
78 
79 	/* Abiv1 regs->tls is fake and we need sync here. */
80 	regs->tls = task_thread_info(target)->tp_value;
81 
82 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
83 }
84 
85 static int gpr_set(struct task_struct *target,
86 		    const struct user_regset *regset,
87 		    unsigned int pos, unsigned int count,
88 		    const void *kbuf, const void __user *ubuf)
89 {
90 	int ret;
91 	struct pt_regs regs;
92 
93 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
94 	if (ret)
95 		return ret;
96 
97 	regs.sr = task_pt_regs(target)->sr;
98 #ifdef CONFIG_CPU_HAS_HILO
99 	regs.dcsr = task_pt_regs(target)->dcsr;
100 #endif
101 	task_thread_info(target)->tp_value = regs.tls;
102 
103 	*task_pt_regs(target) = regs;
104 
105 	return 0;
106 }
107 
108 static int fpr_get(struct task_struct *target,
109 		   const struct user_regset *regset,
110 		   unsigned int pos, unsigned int count,
111 		   void *kbuf, void __user *ubuf)
112 {
113 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
114 
115 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
116 	int i;
117 	struct user_fp tmp = *regs;
118 
119 	for (i = 0; i < 16; i++) {
120 		tmp.vr[i*4] = regs->vr[i*2];
121 		tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
122 	}
123 
124 	for (i = 0; i < 32; i++)
125 		tmp.vr[64 + i] = regs->vr[32 + i];
126 
127 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
128 #else
129 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
130 #endif
131 }
132 
133 static int fpr_set(struct task_struct *target,
134 		   const struct user_regset *regset,
135 		   unsigned int pos, unsigned int count,
136 		   const void *kbuf, const void __user *ubuf)
137 {
138 	int ret;
139 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
140 
141 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
142 	int i;
143 	struct user_fp tmp;
144 
145 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
146 
147 	*regs = tmp;
148 
149 	for (i = 0; i < 16; i++) {
150 		regs->vr[i*2] = tmp.vr[i*4];
151 		regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
152 	}
153 
154 	for (i = 0; i < 32; i++)
155 		regs->vr[32 + i] = tmp.vr[64 + i];
156 #else
157 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
158 #endif
159 
160 	return ret;
161 }
162 
163 static const struct user_regset csky_regsets[] = {
164 	[REGSET_GPR] = {
165 		.core_note_type = NT_PRSTATUS,
166 		.n = ELF_NGREG,
167 		.size = sizeof(u32),
168 		.align = sizeof(u32),
169 		.get = &gpr_get,
170 		.set = &gpr_set,
171 	},
172 	[REGSET_FPR] = {
173 		.core_note_type = NT_PRFPREG,
174 		.n = sizeof(struct user_fp) / sizeof(u32),
175 		.size = sizeof(u32),
176 		.align = sizeof(u32),
177 		.get = &fpr_get,
178 		.set = &fpr_set,
179 	},
180 };
181 
182 static const struct user_regset_view user_csky_view = {
183 	.name = "csky",
184 	.e_machine = ELF_ARCH,
185 	.regsets = csky_regsets,
186 	.n = ARRAY_SIZE(csky_regsets),
187 };
188 
189 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
190 {
191 	return &user_csky_view;
192 }
193 
194 void ptrace_disable(struct task_struct *child)
195 {
196 	singlestep_disable(child);
197 }
198 
199 long arch_ptrace(struct task_struct *child, long request,
200 		 unsigned long addr, unsigned long data)
201 {
202 	long ret = -EIO;
203 
204 	switch (request) {
205 	default:
206 		ret = ptrace_request(child, request, addr, data);
207 		break;
208 	}
209 
210 	return ret;
211 }
212 
213 /*
214  * If process's system calls is traces, do some corresponding handles in this
215  * function before entering system call function and after exiting system call
216  * function.
217  */
218 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
219 {
220 	long saved_why;
221 	/*
222 	 * Save saved_why, why is used to denote syscall entry/exit;
223 	 * why = 0:entry, why = 1: exit
224 	 */
225 	saved_why = regs->regs[SYSTRACE_SAVENUM];
226 	regs->regs[SYSTRACE_SAVENUM] = why;
227 
228 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
229 					? 0x80 : 0));
230 
231 	/*
232 	 * this isn't the same as continuing with a signal, but it will do
233 	 * for normal use.  strace only continues with a signal if the
234 	 * stopping signal is not SIGTRAP.  -brl
235 	 */
236 	if (current->exit_code) {
237 		send_sig(current->exit_code, current, 1);
238 		current->exit_code = 0;
239 	}
240 
241 	regs->regs[SYSTRACE_SAVENUM] = saved_why;
242 }
243 
244 void show_regs(struct pt_regs *fp)
245 {
246 	unsigned long   *sp;
247 	unsigned char   *tp;
248 	int	i;
249 
250 	pr_info("\nCURRENT PROCESS:\n\n");
251 	pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
252 
253 	if (current->mm) {
254 		pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
255 		       (int) current->mm->start_code,
256 		       (int) current->mm->end_code,
257 		       (int) current->mm->start_data,
258 		       (int) current->mm->end_data,
259 		       (int) current->mm->end_data,
260 		       (int) current->mm->brk);
261 		pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
262 		       (int) current->mm->start_stack,
263 		       (int) (((unsigned long) current) + 2 * PAGE_SIZE));
264 	}
265 
266 	pr_info("PC: 0x%08lx\n", (long)fp->pc);
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