1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
5 #include <linux/errno.h>
6 #include <linux/kernel.h>
8 #include <linux/ptrace.h>
9 #include <linux/regset.h>
10 #include <linux/sched.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/signal.h>
13 #include <linux/smp.h>
14 #include <linux/uaccess.h>
15 #include <linux/user.h>
17 #include <asm/thread_info.h>
19 #include <asm/pgtable.h>
20 #include <asm/processor.h>
21 #include <asm/asm-offsets.h>
23 #include <abi/regdef.h>
25 /* sets the trace bits. */
26 #define TRACE_MODE_SI (1 << 14)
27 #define TRACE_MODE_RUN 0
28 #define TRACE_MODE_MASK ~(0x3 << 14)
31 * Make sure the single step bit is not set.
33 static void singlestep_disable(struct task_struct *tsk)
37 regs = task_pt_regs(tsk);
38 regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
41 static void singlestep_enable(struct task_struct *tsk)
45 regs = task_pt_regs(tsk);
46 regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
50 * Make sure the single step bit is set.
52 void user_enable_single_step(struct task_struct *child)
54 singlestep_enable(child);
57 void user_disable_single_step(struct task_struct *child)
59 singlestep_disable(child);
67 static int gpr_get(struct task_struct *target,
68 const struct user_regset *regset,
69 unsigned int pos, unsigned int count,
70 void *kbuf, void __user *ubuf)
74 regs = task_pt_regs(target);
76 /* Abiv1 regs->tls is fake and we need sync here. */
77 regs->tls = task_thread_info(target)->tp_value;
79 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
82 static int gpr_set(struct task_struct *target,
83 const struct user_regset *regset,
84 unsigned int pos, unsigned int count,
85 const void *kbuf, const void __user *ubuf)
90 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, ®s, 0, -1);
94 regs.sr = task_pt_regs(target)->sr;
95 #ifdef CONFIG_CPU_HAS_HILO
96 regs.dcsr = task_pt_regs(target)->dcsr;
98 task_thread_info(target)->tp_value = regs.tls;
100 *task_pt_regs(target) = regs;
105 static int fpr_get(struct task_struct *target,
106 const struct user_regset *regset,
107 unsigned int pos, unsigned int count,
108 void *kbuf, void __user *ubuf)
110 struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
112 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
114 struct user_fp tmp = *regs;
116 for (i = 0; i < 16; i++) {
117 tmp.vr[i*4] = regs->vr[i*2];
118 tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
121 for (i = 0; i < 32; i++)
122 tmp.vr[64 + i] = regs->vr[32 + i];
124 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
126 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
130 static int fpr_set(struct task_struct *target,
131 const struct user_regset *regset,
132 unsigned int pos, unsigned int count,
133 const void *kbuf, const void __user *ubuf)
136 struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
138 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
142 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
146 for (i = 0; i < 16; i++) {
147 regs->vr[i*2] = tmp.vr[i*4];
148 regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
151 for (i = 0; i < 32; i++)
152 regs->vr[32 + i] = tmp.vr[64 + i];
154 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
160 static const struct user_regset csky_regsets[] = {
162 .core_note_type = NT_PRSTATUS,
163 .n = sizeof(struct pt_regs) / sizeof(u32),
165 .align = sizeof(u32),
170 .core_note_type = NT_PRFPREG,
171 .n = sizeof(struct user_fp) / sizeof(u32),
173 .align = sizeof(u32),
179 static const struct user_regset_view user_csky_view = {
181 .e_machine = ELF_ARCH,
182 .regsets = csky_regsets,
183 .n = ARRAY_SIZE(csky_regsets),
186 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
188 return &user_csky_view;
191 void ptrace_disable(struct task_struct *child)
193 singlestep_disable(child);
196 long arch_ptrace(struct task_struct *child, long request,
197 unsigned long addr, unsigned long data)
203 ret = ptrace_request(child, request, addr, data);
211 * If process's system calls is traces, do some corresponding handles in this
212 * function before entering system call function and after exiting system call
215 asmlinkage void syscall_trace(int why, struct pt_regs *regs)
219 * Save saved_why, why is used to denote syscall entry/exit;
220 * why = 0:entry, why = 1: exit
222 saved_why = regs->regs[SYSTRACE_SAVENUM];
223 regs->regs[SYSTRACE_SAVENUM] = why;
225 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
229 * this isn't the same as continuing with a signal, but it will do
230 * for normal use. strace only continues with a signal if the
231 * stopping signal is not SIGTRAP. -brl
233 if (current->exit_code) {
234 send_sig(current->exit_code, current, 1);
235 current->exit_code = 0;
238 regs->regs[SYSTRACE_SAVENUM] = saved_why;
241 extern void show_stack(struct task_struct *task, unsigned long *stack);
242 void show_regs(struct pt_regs *fp)
248 pr_info("\nCURRENT PROCESS:\n\n");
249 pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
252 pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
253 (int) current->mm->start_code,
254 (int) current->mm->end_code,
255 (int) current->mm->start_data,
256 (int) current->mm->end_data,
257 (int) current->mm->end_data,
258 (int) current->mm->brk);
259 pr_info("USER-STACK=%08x KERNEL-STACK=%08x\n\n",
260 (int) current->mm->start_stack,
261 (int) (((unsigned long) current) + 2 * PAGE_SIZE));
264 pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
265 pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
266 pr_info("SP: 0x%08lx\n", (long)fp);
267 pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
268 pr_info("PSR: 0x%08lx\n", (long)fp->sr);
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",
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);
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) {
303 pr_cont("\n%08x: ", (int) (tp + i));
304 pr_cont("%08x ", (int) *sp++);
308 pr_info("\nKERNEL STACK:");
309 tp = ((unsigned char *) fp) - 0x40;
310 for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
312 pr_cont("\n%08x: ", (int) (tp + i));
313 pr_cont("%08x ", (int) *sp++);
317 show_stack(NULL, (unsigned long *)fp->regs[4]);