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