xref: /linux/arch/csky/kernel/ptrace.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
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/uaccess.h>
16 #include <linux/user.h>
17 
18 #include <asm/thread_info.h>
19 #include <asm/page.h>
20 #include <asm/processor.h>
21 #include <asm/asm-offsets.h>
22 
23 #include <abi/regdef.h>
24 #include <abi/ckmmu.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 		   struct membuf to)
80 {
81 	struct pt_regs *regs = task_pt_regs(target);
82 
83 	/* Abiv1 regs->tls is fake and we need sync here. */
84 	regs->tls = task_thread_info(target)->tp_value;
85 
86 	return membuf_write(&to, regs, sizeof(*regs));
87 }
88 
89 static int gpr_set(struct task_struct *target,
90 		    const struct user_regset *regset,
91 		    unsigned int pos, unsigned int count,
92 		    const void *kbuf, const void __user *ubuf)
93 {
94 	int ret;
95 	struct pt_regs regs;
96 
97 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
98 	if (ret)
99 		return ret;
100 
101 	/* BIT(0) of regs.sr is Condition Code/Carry bit */
102 	regs.sr = (regs.sr & BIT(0)) | (task_pt_regs(target)->sr & ~BIT(0));
103 #ifdef CONFIG_CPU_HAS_HILO
104 	regs.dcsr = task_pt_regs(target)->dcsr;
105 #endif
106 	task_thread_info(target)->tp_value = regs.tls;
107 
108 	*task_pt_regs(target) = regs;
109 
110 	return 0;
111 }
112 
113 static int fpr_get(struct task_struct *target,
114 		   const struct user_regset *regset,
115 		   struct membuf to)
116 {
117 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
118 
119 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
120 	int i;
121 	struct user_fp tmp = *regs;
122 
123 	for (i = 0; i < 16; i++) {
124 		tmp.vr[i*4] = regs->vr[i*2];
125 		tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
126 	}
127 
128 	for (i = 0; i < 32; i++)
129 		tmp.vr[64 + i] = regs->vr[32 + i];
130 
131 	return membuf_write(&to, &tmp, sizeof(tmp));
132 #else
133 	return membuf_write(&to, regs, sizeof(*regs));
134 #endif
135 }
136 
137 static int fpr_set(struct task_struct *target,
138 		   const struct user_regset *regset,
139 		   unsigned int pos, unsigned int count,
140 		   const void *kbuf, const void __user *ubuf)
141 {
142 	int ret;
143 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
144 
145 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
146 	int i;
147 	struct user_fp tmp;
148 
149 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
150 
151 	*regs = tmp;
152 
153 	for (i = 0; i < 16; i++) {
154 		regs->vr[i*2] = tmp.vr[i*4];
155 		regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
156 	}
157 
158 	for (i = 0; i < 32; i++)
159 		regs->vr[32 + i] = tmp.vr[64 + i];
160 #else
161 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
162 #endif
163 
164 	return ret;
165 }
166 
167 static const struct user_regset csky_regsets[] = {
168 	[REGSET_GPR] = {
169 		.core_note_type = NT_PRSTATUS,
170 		.n = sizeof(struct pt_regs) / sizeof(u32),
171 		.size = sizeof(u32),
172 		.align = sizeof(u32),
173 		.regset_get = gpr_get,
174 		.set = gpr_set,
175 	},
176 	[REGSET_FPR] = {
177 		.core_note_type = NT_PRFPREG,
178 		.n = sizeof(struct user_fp) / sizeof(u32),
179 		.size = sizeof(u32),
180 		.align = sizeof(u32),
181 		.regset_get = fpr_get,
182 		.set = fpr_set,
183 	},
184 };
185 
186 static const struct user_regset_view user_csky_view = {
187 	.name = "csky",
188 	.e_machine = ELF_ARCH,
189 	.regsets = csky_regsets,
190 	.n = ARRAY_SIZE(csky_regsets),
191 };
192 
193 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
194 {
195 	return &user_csky_view;
196 }
197 
198 struct pt_regs_offset {
199 	const char *name;
200 	int offset;
201 };
202 
203 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
204 #define REG_OFFSET_END {.name = NULL, .offset = 0}
205 
206 static const struct pt_regs_offset regoffset_table[] = {
207 	REG_OFFSET_NAME(tls),
208 	REG_OFFSET_NAME(lr),
209 	REG_OFFSET_NAME(pc),
210 	REG_OFFSET_NAME(sr),
211 	REG_OFFSET_NAME(usp),
212 	REG_OFFSET_NAME(orig_a0),
213 	REG_OFFSET_NAME(a0),
214 	REG_OFFSET_NAME(a1),
215 	REG_OFFSET_NAME(a2),
216 	REG_OFFSET_NAME(a3),
217 	REG_OFFSET_NAME(regs[0]),
218 	REG_OFFSET_NAME(regs[1]),
219 	REG_OFFSET_NAME(regs[2]),
220 	REG_OFFSET_NAME(regs[3]),
221 	REG_OFFSET_NAME(regs[4]),
222 	REG_OFFSET_NAME(regs[5]),
223 	REG_OFFSET_NAME(regs[6]),
224 	REG_OFFSET_NAME(regs[7]),
225 	REG_OFFSET_NAME(regs[8]),
226 	REG_OFFSET_NAME(regs[9]),
227 #if defined(__CSKYABIV2__)
228 	REG_OFFSET_NAME(exregs[0]),
229 	REG_OFFSET_NAME(exregs[1]),
230 	REG_OFFSET_NAME(exregs[2]),
231 	REG_OFFSET_NAME(exregs[3]),
232 	REG_OFFSET_NAME(exregs[4]),
233 	REG_OFFSET_NAME(exregs[5]),
234 	REG_OFFSET_NAME(exregs[6]),
235 	REG_OFFSET_NAME(exregs[7]),
236 	REG_OFFSET_NAME(exregs[8]),
237 	REG_OFFSET_NAME(exregs[9]),
238 	REG_OFFSET_NAME(exregs[10]),
239 	REG_OFFSET_NAME(exregs[11]),
240 	REG_OFFSET_NAME(exregs[12]),
241 	REG_OFFSET_NAME(exregs[13]),
242 	REG_OFFSET_NAME(exregs[14]),
243 	REG_OFFSET_NAME(rhi),
244 	REG_OFFSET_NAME(rlo),
245 	REG_OFFSET_NAME(dcsr),
246 #endif
247 	REG_OFFSET_END,
248 };
249 
250 /**
251  * regs_query_register_offset() - query register offset from its name
252  * @name:	the name of a register
253  *
254  * regs_query_register_offset() returns the offset of a register in struct
255  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
256  */
257 int regs_query_register_offset(const char *name)
258 {
259 	const struct pt_regs_offset *roff;
260 
261 	for (roff = regoffset_table; roff->name != NULL; roff++)
262 		if (!strcmp(roff->name, name))
263 			return roff->offset;
264 	return -EINVAL;
265 }
266 
267 /**
268  * regs_within_kernel_stack() - check the address in the stack
269  * @regs:      pt_regs which contains kernel stack pointer.
270  * @addr:      address which is checked.
271  *
272  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
273  * If @addr is within the kernel stack, it returns true. If not, returns false.
274  */
275 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
276 {
277 	return (addr & ~(THREAD_SIZE - 1))  ==
278 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
279 }
280 
281 /**
282  * regs_get_kernel_stack_nth() - get Nth entry of the stack
283  * @regs:	pt_regs which contains kernel stack pointer.
284  * @n:		stack entry number.
285  *
286  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
287  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
288  * this returns 0.
289  */
290 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
291 {
292 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
293 
294 	addr += n;
295 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
296 		return *addr;
297 	else
298 		return 0;
299 }
300 
301 void ptrace_disable(struct task_struct *child)
302 {
303 	singlestep_disable(child);
304 }
305 
306 long arch_ptrace(struct task_struct *child, long request,
307 		 unsigned long addr, unsigned long data)
308 {
309 	long ret = -EIO;
310 
311 	switch (request) {
312 	default:
313 		ret = ptrace_request(child, request, addr, data);
314 		break;
315 	}
316 
317 	return ret;
318 }
319 
320 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
321 {
322 	if (test_thread_flag(TIF_SYSCALL_TRACE))
323 		if (ptrace_report_syscall_entry(regs))
324 			return -1;
325 
326 	if (secure_computing() == -1)
327 		return -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 	return 0;
334 }
335 
336 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
337 {
338 	audit_syscall_exit(regs);
339 
340 	if (test_thread_flag(TIF_SYSCALL_TRACE))
341 		ptrace_report_syscall_exit(regs, 0);
342 
343 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
344 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
345 }
346 
347 #ifdef CONFIG_CPU_CK860
348 static void show_iutlb(void)
349 {
350 	int entry, i;
351 	unsigned long flags;
352 	unsigned long oldpid;
353 	unsigned long entryhi[16], entrylo0[16], entrylo1[16];
354 
355 	oldpid = read_mmu_entryhi();
356 
357 	entry = 0x8000;
358 
359 	local_irq_save(flags);
360 
361 	for (i = 0; i < 16; i++) {
362 		write_mmu_index(entry);
363 		tlb_read();
364 		entryhi[i]  = read_mmu_entryhi();
365 		entrylo0[i] = read_mmu_entrylo0();
366 		entrylo1[i] = read_mmu_entrylo1();
367 
368 		entry++;
369 	}
370 
371 	local_irq_restore(flags);
372 
373 	write_mmu_entryhi(oldpid);
374 
375 	printk("\n\n\n");
376 	for (i = 0; i < 16; i++)
377 		printk("iutlb[%d]:	entryhi - 0x%lx;	entrylo0 - 0x%lx;"
378 		       "	entrylo1 - 0x%lx\n",
379 			 i, entryhi[i], entrylo0[i], entrylo1[i]);
380 	printk("\n\n\n");
381 }
382 
383 static void show_dutlb(void)
384 {
385 	int entry, i;
386 	unsigned long flags;
387 	unsigned long oldpid;
388 	unsigned long entryhi[16], entrylo0[16], entrylo1[16];
389 
390 	oldpid = read_mmu_entryhi();
391 
392 	entry = 0x4000;
393 
394 	local_irq_save(flags);
395 
396 	for (i = 0; i < 16; i++) {
397 		write_mmu_index(entry);
398 		tlb_read();
399 		entryhi[i]  = read_mmu_entryhi();
400 		entrylo0[i] = read_mmu_entrylo0();
401 		entrylo1[i] = read_mmu_entrylo1();
402 
403 		entry++;
404 	}
405 
406 	local_irq_restore(flags);
407 
408 	write_mmu_entryhi(oldpid);
409 
410 	printk("\n\n\n");
411 	for (i = 0; i < 16; i++)
412 		printk("dutlb[%d]:	entryhi - 0x%lx;	entrylo0 - 0x%lx;"
413 		       "	entrylo1 - 0x%lx\n",
414 			 i, entryhi[i], entrylo0[i], entrylo1[i]);
415 	printk("\n\n\n");
416 }
417 
418 static unsigned long entryhi[1024], entrylo0[1024], entrylo1[1024];
419 static void show_jtlb(void)
420 {
421 	int entry;
422 	unsigned long flags;
423 	unsigned long oldpid;
424 
425 	oldpid = read_mmu_entryhi();
426 
427 	entry = 0;
428 
429 	local_irq_save(flags);
430 	while (entry < 1024) {
431 		write_mmu_index(entry);
432 		tlb_read();
433 		entryhi[entry]  = read_mmu_entryhi();
434 		entrylo0[entry] = read_mmu_entrylo0();
435 		entrylo1[entry] = read_mmu_entrylo1();
436 
437 		entry++;
438 	}
439 	local_irq_restore(flags);
440 
441 	write_mmu_entryhi(oldpid);
442 
443 	printk("\n\n\n");
444 
445 	for (entry = 0; entry < 1024; entry++)
446 		printk("jtlb[%x]:	entryhi - 0x%lx;	entrylo0 - 0x%lx;"
447 		       "	entrylo1 - 0x%lx\n",
448 			 entry, entryhi[entry], entrylo0[entry], entrylo1[entry]);
449 	printk("\n\n\n");
450 }
451 
452 static void show_tlb(void)
453 {
454 	show_iutlb();
455 	show_dutlb();
456 	show_jtlb();
457 }
458 #else
459 static void show_tlb(void)
460 {
461 	return;
462 }
463 #endif
464 
465 void show_regs(struct pt_regs *fp)
466 {
467 	pr_info("\nCURRENT PROCESS:\n\n");
468 	pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
469 
470 	if (current->mm) {
471 		pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
472 		       (int) current->mm->start_code,
473 		       (int) current->mm->end_code,
474 		       (int) current->mm->start_data,
475 		       (int) current->mm->end_data,
476 		       (int) current->mm->end_data,
477 		       (int) current->mm->brk);
478 		pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
479 		       (int) current->mm->start_stack,
480 		       (int) (((unsigned long) current) + 2 * PAGE_SIZE));
481 	}
482 
483 	pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
484 	pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
485 	pr_info("SP: 0x%08lx\n", (long)fp->usp);
486 	pr_info("PSR: 0x%08lx\n", (long)fp->sr);
487 	pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
488 	pr_info("PT_REGS: 0x%08lx\n", (long)fp);
489 
490 	pr_info(" a0: 0x%08lx   a1: 0x%08lx   a2: 0x%08lx   a3: 0x%08lx\n",
491 		fp->a0, fp->a1, fp->a2, fp->a3);
492 #if defined(__CSKYABIV2__)
493 	pr_info(" r4: 0x%08lx   r5: 0x%08lx   r6: 0x%08lx   r7: 0x%08lx\n",
494 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
495 	pr_info(" r8: 0x%08lx   r9: 0x%08lx  r10: 0x%08lx  r11: 0x%08lx\n",
496 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
497 	pr_info("r12: 0x%08lx  r13: 0x%08lx  r15: 0x%08lx\n",
498 		fp->regs[8], fp->regs[9], fp->lr);
499 	pr_info("r16: 0x%08lx  r17: 0x%08lx  r18: 0x%08lx  r19: 0x%08lx\n",
500 		fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
501 	pr_info("r20: 0x%08lx  r21: 0x%08lx  r22: 0x%08lx  r23: 0x%08lx\n",
502 		fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
503 	pr_info("r24: 0x%08lx  r25: 0x%08lx  r26: 0x%08lx  r27: 0x%08lx\n",
504 		fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
505 	pr_info("r28: 0x%08lx  r29: 0x%08lx  r30: 0x%08lx  tls: 0x%08lx\n",
506 		fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
507 	pr_info(" hi: 0x%08lx   lo: 0x%08lx\n",
508 		fp->rhi, fp->rlo);
509 #else
510 	pr_info(" r6: 0x%08lx   r7: 0x%08lx   r8: 0x%08lx   r9: 0x%08lx\n",
511 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
512 	pr_info("r10: 0x%08lx  r11: 0x%08lx  r12: 0x%08lx  r13: 0x%08lx\n",
513 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
514 	pr_info("r14: 0x%08lx   r1: 0x%08lx\n",
515 		fp->regs[8], fp->regs[9]);
516 #endif
517 
518 	show_tlb();
519 
520 	return;
521 }
522