xref: /openbmc/linux/arch/csky/kernel/ptrace.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3 
4 #include <linux/audit.h>
5 #include <linux/elf.h>
6 #include <linux/errno.h>
7 #include <linux/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/ptrace.h>
10 #include <linux/regset.h>
11 #include <linux/sched.h>
12 #include <linux/sched/task_stack.h>
13 #include <linux/signal.h>
14 #include <linux/smp.h>
15 #include <linux/tracehook.h>
16 #include <linux/uaccess.h>
17 #include <linux/user.h>
18 
19 #include <asm/thread_info.h>
20 #include <asm/page.h>
21 #include <asm/pgtable.h>
22 #include <asm/processor.h>
23 #include <asm/asm-offsets.h>
24 
25 #include <abi/regdef.h>
26 
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/syscalls.h>
29 
30 /* sets the trace bits. */
31 #define TRACE_MODE_SI      (1 << 14)
32 #define TRACE_MODE_RUN     0
33 #define TRACE_MODE_MASK    ~(0x3 << 14)
34 
35 /*
36  * Make sure the single step bit is not set.
37  */
38 static void singlestep_disable(struct task_struct *tsk)
39 {
40 	struct pt_regs *regs;
41 
42 	regs = task_pt_regs(tsk);
43 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
44 }
45 
46 static void singlestep_enable(struct task_struct *tsk)
47 {
48 	struct pt_regs *regs;
49 
50 	regs = task_pt_regs(tsk);
51 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
52 }
53 
54 /*
55  * Make sure the single step bit is set.
56  */
57 void user_enable_single_step(struct task_struct *child)
58 {
59 	singlestep_enable(child);
60 }
61 
62 void user_disable_single_step(struct task_struct *child)
63 {
64 	singlestep_disable(child);
65 }
66 
67 enum csky_regset {
68 	REGSET_GPR,
69 	REGSET_FPR,
70 };
71 
72 static int gpr_get(struct task_struct *target,
73 		   const struct user_regset *regset,
74 		   unsigned int pos, unsigned int count,
75 		   void *kbuf, void __user *ubuf)
76 {
77 	struct pt_regs *regs;
78 
79 	regs = task_pt_regs(target);
80 
81 	/* Abiv1 regs->tls is fake and we need sync here. */
82 	regs->tls = task_thread_info(target)->tp_value;
83 
84 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
85 }
86 
87 static int gpr_set(struct task_struct *target,
88 		    const struct user_regset *regset,
89 		    unsigned int pos, unsigned int count,
90 		    const void *kbuf, const void __user *ubuf)
91 {
92 	int ret;
93 	struct pt_regs regs;
94 
95 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
96 	if (ret)
97 		return ret;
98 
99 	regs.sr = task_pt_regs(target)->sr;
100 #ifdef CONFIG_CPU_HAS_HILO
101 	regs.dcsr = task_pt_regs(target)->dcsr;
102 #endif
103 	task_thread_info(target)->tp_value = regs.tls;
104 
105 	*task_pt_regs(target) = regs;
106 
107 	return 0;
108 }
109 
110 static int fpr_get(struct task_struct *target,
111 		   const struct user_regset *regset,
112 		   unsigned int pos, unsigned int count,
113 		   void *kbuf, void __user *ubuf)
114 {
115 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
116 
117 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
118 	int i;
119 	struct user_fp tmp = *regs;
120 
121 	for (i = 0; i < 16; i++) {
122 		tmp.vr[i*4] = regs->vr[i*2];
123 		tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
124 	}
125 
126 	for (i = 0; i < 32; i++)
127 		tmp.vr[64 + i] = regs->vr[32 + i];
128 
129 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
130 #else
131 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
132 #endif
133 }
134 
135 static int fpr_set(struct task_struct *target,
136 		   const struct user_regset *regset,
137 		   unsigned int pos, unsigned int count,
138 		   const void *kbuf, const void __user *ubuf)
139 {
140 	int ret;
141 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
142 
143 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
144 	int i;
145 	struct user_fp tmp;
146 
147 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
148 
149 	*regs = tmp;
150 
151 	for (i = 0; i < 16; i++) {
152 		regs->vr[i*2] = tmp.vr[i*4];
153 		regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
154 	}
155 
156 	for (i = 0; i < 32; i++)
157 		regs->vr[32 + i] = tmp.vr[64 + i];
158 #else
159 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
160 #endif
161 
162 	return ret;
163 }
164 
165 static const struct user_regset csky_regsets[] = {
166 	[REGSET_GPR] = {
167 		.core_note_type = NT_PRSTATUS,
168 		.n = sizeof(struct pt_regs) / sizeof(u32),
169 		.size = sizeof(u32),
170 		.align = sizeof(u32),
171 		.get = &gpr_get,
172 		.set = &gpr_set,
173 	},
174 	[REGSET_FPR] = {
175 		.core_note_type = NT_PRFPREG,
176 		.n = sizeof(struct user_fp) / sizeof(u32),
177 		.size = sizeof(u32),
178 		.align = sizeof(u32),
179 		.get = &fpr_get,
180 		.set = &fpr_set,
181 	},
182 };
183 
184 static const struct user_regset_view user_csky_view = {
185 	.name = "csky",
186 	.e_machine = ELF_ARCH,
187 	.regsets = csky_regsets,
188 	.n = ARRAY_SIZE(csky_regsets),
189 };
190 
191 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
192 {
193 	return &user_csky_view;
194 }
195 
196 struct pt_regs_offset {
197 	const char *name;
198 	int offset;
199 };
200 
201 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
202 #define REG_OFFSET_END {.name = NULL, .offset = 0}
203 
204 static const struct pt_regs_offset regoffset_table[] = {
205 	REG_OFFSET_NAME(tls),
206 	REG_OFFSET_NAME(lr),
207 	REG_OFFSET_NAME(pc),
208 	REG_OFFSET_NAME(sr),
209 	REG_OFFSET_NAME(usp),
210 	REG_OFFSET_NAME(orig_a0),
211 	REG_OFFSET_NAME(a0),
212 	REG_OFFSET_NAME(a1),
213 	REG_OFFSET_NAME(a2),
214 	REG_OFFSET_NAME(a3),
215 	REG_OFFSET_NAME(regs[0]),
216 	REG_OFFSET_NAME(regs[1]),
217 	REG_OFFSET_NAME(regs[2]),
218 	REG_OFFSET_NAME(regs[3]),
219 	REG_OFFSET_NAME(regs[4]),
220 	REG_OFFSET_NAME(regs[5]),
221 	REG_OFFSET_NAME(regs[6]),
222 	REG_OFFSET_NAME(regs[7]),
223 	REG_OFFSET_NAME(regs[8]),
224 	REG_OFFSET_NAME(regs[9]),
225 #if defined(__CSKYABIV2__)
226 	REG_OFFSET_NAME(exregs[0]),
227 	REG_OFFSET_NAME(exregs[1]),
228 	REG_OFFSET_NAME(exregs[2]),
229 	REG_OFFSET_NAME(exregs[3]),
230 	REG_OFFSET_NAME(exregs[4]),
231 	REG_OFFSET_NAME(exregs[5]),
232 	REG_OFFSET_NAME(exregs[6]),
233 	REG_OFFSET_NAME(exregs[7]),
234 	REG_OFFSET_NAME(exregs[8]),
235 	REG_OFFSET_NAME(exregs[9]),
236 	REG_OFFSET_NAME(exregs[10]),
237 	REG_OFFSET_NAME(exregs[11]),
238 	REG_OFFSET_NAME(exregs[12]),
239 	REG_OFFSET_NAME(exregs[13]),
240 	REG_OFFSET_NAME(exregs[14]),
241 	REG_OFFSET_NAME(rhi),
242 	REG_OFFSET_NAME(rlo),
243 	REG_OFFSET_NAME(dcsr),
244 #endif
245 	REG_OFFSET_END,
246 };
247 
248 /**
249  * regs_query_register_offset() - query register offset from its name
250  * @name:	the name of a register
251  *
252  * regs_query_register_offset() returns the offset of a register in struct
253  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
254  */
255 int regs_query_register_offset(const char *name)
256 {
257 	const struct pt_regs_offset *roff;
258 
259 	for (roff = regoffset_table; roff->name != NULL; roff++)
260 		if (!strcmp(roff->name, name))
261 			return roff->offset;
262 	return -EINVAL;
263 }
264 
265 /**
266  * regs_within_kernel_stack() - check the address in the stack
267  * @regs:      pt_regs which contains kernel stack pointer.
268  * @addr:      address which is checked.
269  *
270  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
271  * If @addr is within the kernel stack, it returns true. If not, returns false.
272  */
273 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
274 {
275 	return (addr & ~(THREAD_SIZE - 1))  ==
276 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
277 }
278 
279 /**
280  * regs_get_kernel_stack_nth() - get Nth entry of the stack
281  * @regs:	pt_regs which contains kernel stack pointer.
282  * @n:		stack entry number.
283  *
284  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
285  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
286  * this returns 0.
287  */
288 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
289 {
290 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
291 
292 	addr += n;
293 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
294 		return *addr;
295 	else
296 		return 0;
297 }
298 
299 void ptrace_disable(struct task_struct *child)
300 {
301 	singlestep_disable(child);
302 }
303 
304 long arch_ptrace(struct task_struct *child, long request,
305 		 unsigned long addr, unsigned long data)
306 {
307 	long ret = -EIO;
308 
309 	switch (request) {
310 	default:
311 		ret = ptrace_request(child, request, addr, data);
312 		break;
313 	}
314 
315 	return ret;
316 }
317 
318 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
319 {
320 	if (test_thread_flag(TIF_SYSCALL_TRACE))
321 		if (tracehook_report_syscall_entry(regs))
322 			syscall_set_nr(current, regs, -1);
323 
324 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
325 		trace_sys_enter(regs, syscall_get_nr(current, regs));
326 
327 	audit_syscall_entry(regs_syscallid(regs), regs->a0, regs->a1, regs->a2, regs->a3);
328 }
329 
330 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
331 {
332 	audit_syscall_exit(regs);
333 
334 	if (test_thread_flag(TIF_SYSCALL_TRACE))
335 		tracehook_report_syscall_exit(regs, 0);
336 
337 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
338 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
339 }
340 
341 extern void show_stack(struct task_struct *task, unsigned long *stack);
342 void show_regs(struct pt_regs *fp)
343 {
344 	unsigned long   *sp;
345 	unsigned char   *tp;
346 	int	i;
347 
348 	pr_info("\nCURRENT PROCESS:\n\n");
349 	pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
350 
351 	if (current->mm) {
352 		pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
353 		       (int) current->mm->start_code,
354 		       (int) current->mm->end_code,
355 		       (int) current->mm->start_data,
356 		       (int) current->mm->end_data,
357 		       (int) current->mm->end_data,
358 		       (int) current->mm->brk);
359 		pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
360 		       (int) current->mm->start_stack,
361 		       (int) (((unsigned long) current) + 2 * PAGE_SIZE));
362 	}
363 
364 	pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
365 	pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
366 	pr_info("SP: 0x%08lx\n", (long)fp);
367 	pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
368 	pr_info("PSR: 0x%08lx\n", (long)fp->sr);
369 
370 	pr_info(" a0: 0x%08lx   a1: 0x%08lx   a2: 0x%08lx   a3: 0x%08lx\n",
371 		fp->a0, fp->a1, fp->a2, fp->a3);
372 #if defined(__CSKYABIV2__)
373 	pr_info(" r4: 0x%08lx   r5: 0x%08lx   r6: 0x%08lx   r7: 0x%08lx\n",
374 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
375 	pr_info(" r8: 0x%08lx   r9: 0x%08lx  r10: 0x%08lx  r11: 0x%08lx\n",
376 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
377 	pr_info("r12: 0x%08lx  r13: 0x%08lx  r15: 0x%08lx\n",
378 		fp->regs[8], fp->regs[9], fp->lr);
379 	pr_info("r16: 0x%08lx  r17: 0x%08lx  r18: 0x%08lx  r19: 0x%08lx\n",
380 		fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
381 	pr_info("r20: 0x%08lx  r21: 0x%08lx  r22: 0x%08lx  r23: 0x%08lx\n",
382 		fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
383 	pr_info("r24: 0x%08lx  r25: 0x%08lx  r26: 0x%08lx  r27: 0x%08lx\n",
384 		fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
385 	pr_info("r28: 0x%08lx  r29: 0x%08lx  r30: 0x%08lx  tls: 0x%08lx\n",
386 		fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
387 	pr_info(" hi: 0x%08lx   lo: 0x%08lx\n",
388 		fp->rhi, fp->rlo);
389 #else
390 	pr_info(" r6: 0x%08lx   r7: 0x%08lx   r8: 0x%08lx   r9: 0x%08lx\n",
391 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
392 	pr_info("r10: 0x%08lx  r11: 0x%08lx  r12: 0x%08lx  r13: 0x%08lx\n",
393 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
394 	pr_info("r14: 0x%08lx   r1: 0x%08lx  r15: 0x%08lx\n",
395 		fp->regs[8], fp->regs[9], fp->lr);
396 #endif
397 
398 	pr_info("\nCODE:");
399 	tp = ((unsigned char *) fp->pc) - 0x20;
400 	tp += ((int)tp % 4) ? 2 : 0;
401 	for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
402 		if ((i % 0x10) == 0)
403 			pr_cont("\n%08x: ", (int) (tp + i));
404 		pr_cont("%08x ", (int) *sp++);
405 	}
406 	pr_cont("\n");
407 
408 	pr_info("\nKERNEL STACK:");
409 	tp = ((unsigned char *) fp) - 0x40;
410 	for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
411 		if ((i % 0x10) == 0)
412 			pr_cont("\n%08x: ", (int) (tp + i));
413 		pr_cont("%08x ", (int) *sp++);
414 	}
415 	pr_cont("\n");
416 
417 	show_stack(NULL, (unsigned long *)fp->regs[4]);
418 	return;
419 }
420