GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / csky / kernel / ptrace.c
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         /* BIT(0) of regs.sr is Condition Code/Carry bit */
100         regs.sr = (regs.sr & BIT(0)) | (task_pt_regs(target)->sr & ~BIT(0));
101 #ifdef CONFIG_CPU_HAS_HILO
102         regs.dcsr = task_pt_regs(target)->dcsr;
103 #endif
104         task_thread_info(target)->tp_value = regs.tls;
105
106         *task_pt_regs(target) = regs;
107
108         return 0;
109 }
110
111 static int fpr_get(struct task_struct *target,
112                    const struct user_regset *regset,
113                    unsigned int pos, unsigned int count,
114                    void *kbuf, void __user *ubuf)
115 {
116         struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
117
118 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
119         int i;
120         struct user_fp tmp = *regs;
121
122         for (i = 0; i < 16; i++) {
123                 tmp.vr[i*4] = regs->vr[i*2];
124                 tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
125         }
126
127         for (i = 0; i < 32; i++)
128                 tmp.vr[64 + i] = regs->vr[32 + i];
129
130         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
131 #else
132         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
133 #endif
134 }
135
136 static int fpr_set(struct task_struct *target,
137                    const struct user_regset *regset,
138                    unsigned int pos, unsigned int count,
139                    const void *kbuf, const void __user *ubuf)
140 {
141         int ret;
142         struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
143
144 #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
145         int i;
146         struct user_fp tmp;
147
148         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
149
150         *regs = tmp;
151
152         for (i = 0; i < 16; i++) {
153                 regs->vr[i*2] = tmp.vr[i*4];
154                 regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
155         }
156
157         for (i = 0; i < 32; i++)
158                 regs->vr[32 + i] = tmp.vr[64 + i];
159 #else
160         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
161 #endif
162
163         return ret;
164 }
165
166 static const struct user_regset csky_regsets[] = {
167         [REGSET_GPR] = {
168                 .core_note_type = NT_PRSTATUS,
169                 .n = sizeof(struct pt_regs) / sizeof(u32),
170                 .size = sizeof(u32),
171                 .align = sizeof(u32),
172                 .get = &gpr_get,
173                 .set = &gpr_set,
174         },
175         [REGSET_FPR] = {
176                 .core_note_type = NT_PRFPREG,
177                 .n = sizeof(struct user_fp) / sizeof(u32),
178                 .size = sizeof(u32),
179                 .align = sizeof(u32),
180                 .get = &fpr_get,
181                 .set = &fpr_set,
182         },
183 };
184
185 static const struct user_regset_view user_csky_view = {
186         .name = "csky",
187         .e_machine = ELF_ARCH,
188         .regsets = csky_regsets,
189         .n = ARRAY_SIZE(csky_regsets),
190 };
191
192 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
193 {
194         return &user_csky_view;
195 }
196
197 void ptrace_disable(struct task_struct *child)
198 {
199         singlestep_disable(child);
200 }
201
202 long arch_ptrace(struct task_struct *child, long request,
203                  unsigned long addr, unsigned long data)
204 {
205         long ret = -EIO;
206
207         switch (request) {
208         default:
209                 ret = ptrace_request(child, request, addr, data);
210                 break;
211         }
212
213         return ret;
214 }
215
216 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
217 {
218         if (test_thread_flag(TIF_SYSCALL_TRACE))
219                 if (tracehook_report_syscall_entry(regs))
220                         syscall_set_nr(current, regs, -1);
221
222         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
223                 trace_sys_enter(regs, syscall_get_nr(current, regs));
224
225         audit_syscall_entry(regs_syscallid(regs), regs->a0, regs->a1, regs->a2, regs->a3);
226 }
227
228 asmlinkage void syscall_trace_exit(struct pt_regs *regs)
229 {
230         audit_syscall_exit(regs);
231
232         if (test_thread_flag(TIF_SYSCALL_TRACE))
233                 tracehook_report_syscall_exit(regs, 0);
234
235         if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
236                 trace_sys_exit(regs, syscall_get_return_value(current, regs));
237 }
238
239 extern void show_stack(struct task_struct *task, unsigned long *stack);
240 void show_regs(struct pt_regs *fp)
241 {
242         unsigned long   *sp;
243         unsigned char   *tp;
244         int     i;
245
246         pr_info("\nCURRENT PROCESS:\n\n");
247         pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
248
249         if (current->mm) {
250                 pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
251                        (int) current->mm->start_code,
252                        (int) current->mm->end_code,
253                        (int) current->mm->start_data,
254                        (int) current->mm->end_data,
255                        (int) current->mm->end_data,
256                        (int) current->mm->brk);
257                 pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
258                        (int) current->mm->start_stack,
259                        (int) (((unsigned long) current) + 2 * PAGE_SIZE));
260         }
261
262         pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
263         pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
264         pr_info("SP: 0x%08lx\n", (long)fp);
265         pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
266         pr_info("PSR: 0x%08lx\n", (long)fp->sr);
267
268         pr_info(" a0: 0x%08lx   a1: 0x%08lx   a2: 0x%08lx   a3: 0x%08lx\n",
269                 fp->a0, fp->a1, fp->a2, fp->a3);
270 #if defined(__CSKYABIV2__)
271         pr_info(" r4: 0x%08lx   r5: 0x%08lx   r6: 0x%08lx   r7: 0x%08lx\n",
272                 fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
273         pr_info(" r8: 0x%08lx   r9: 0x%08lx  r10: 0x%08lx  r11: 0x%08lx\n",
274                 fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
275         pr_info("r12: 0x%08lx  r13: 0x%08lx  r15: 0x%08lx\n",
276                 fp->regs[8], fp->regs[9], fp->lr);
277         pr_info("r16: 0x%08lx  r17: 0x%08lx  r18: 0x%08lx  r19: 0x%08lx\n",
278                 fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
279         pr_info("r20: 0x%08lx  r21: 0x%08lx  r22: 0x%08lx  r23: 0x%08lx\n",
280                 fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
281         pr_info("r24: 0x%08lx  r25: 0x%08lx  r26: 0x%08lx  r27: 0x%08lx\n",
282                 fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
283         pr_info("r28: 0x%08lx  r29: 0x%08lx  r30: 0x%08lx  tls: 0x%08lx\n",
284                 fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
285         pr_info(" hi: 0x%08lx   lo: 0x%08lx\n",
286                 fp->rhi, fp->rlo);
287 #else
288         pr_info(" r6: 0x%08lx   r7: 0x%08lx   r8: 0x%08lx   r9: 0x%08lx\n",
289                 fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
290         pr_info("r10: 0x%08lx  r11: 0x%08lx  r12: 0x%08lx  r13: 0x%08lx\n",
291                 fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
292         pr_info("r14: 0x%08lx   r1: 0x%08lx  r15: 0x%08lx\n",
293                 fp->regs[8], fp->regs[9], fp->lr);
294 #endif
295
296         pr_info("\nCODE:");
297         tp = ((unsigned char *) fp->pc) - 0x20;
298         tp += ((int)tp % 4) ? 2 : 0;
299         for (sp = (unsigned long *) tp, i = 0; (i < 0x40);  i += 4) {
300                 if ((i % 0x10) == 0)
301                         pr_cont("\n%08x: ", (int) (tp + i));
302                 pr_cont("%08x ", (int) *sp++);
303         }
304         pr_cont("\n");
305
306         pr_info("\nKERNEL STACK:");
307         tp = ((unsigned char *) fp) - 0x40;
308         for (sp = (unsigned long *) tp, i = 0; (i < 0xc0); i += 4) {
309                 if ((i % 0x10) == 0)
310                         pr_cont("\n%08x: ", (int) (tp + i));
311                 pr_cont("%08x ", (int) *sp++);
312         }
313         pr_cont("\n");
314
315         show_stack(NULL, (unsigned long *)fp->regs[4]);
316         return;
317 }