Merge tag 'tag-chrome-platform-for-v4.20' of git://git.kernel.org/pub/scm/linux/kerne...
[linux-2.6-block.git] / arch / csky / kernel / ptrace.c
CommitLineData
2ce36bfa
GR
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#include <linux/elf.h>
5#include <linux/errno.h>
6#include <linux/kernel.h>
7#include <linux/mm.h>
8#include <linux/ptrace.h>
9#include <linux/regset.h>
10#include <linux/sched.h>
11#include <linux/signal.h>
12#include <linux/smp.h>
13#include <linux/uaccess.h>
14#include <linux/user.h>
15
16#include <asm/thread_info.h>
17#include <asm/page.h>
18#include <asm/pgtable.h>
19#include <asm/processor.h>
20#include <asm/asm-offsets.h>
21
22#include <abi/regdef.h>
23
24/* sets the trace bits. */
25#define TRACE_MODE_SI (1 << 14)
26#define TRACE_MODE_RUN 0
27#define TRACE_MODE_MASK ~(0x3 << 14)
28
29/*
30 * Make sure the single step bit is not set.
31 */
32static void singlestep_disable(struct task_struct *tsk)
33{
34 struct pt_regs *regs;
35
36 regs = task_pt_regs(tsk);
37 regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
38}
39
40static void singlestep_enable(struct task_struct *tsk)
41{
42 struct pt_regs *regs;
43
44 regs = task_pt_regs(tsk);
45 regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
46}
47
48/*
49 * Make sure the single step bit is set.
50 */
51void user_enable_single_step(struct task_struct *child)
52{
53 if (child->thread.esp0 == 0)
54 return;
55 singlestep_enable(child);
56}
57
58void user_disable_single_step(struct task_struct *child)
59{
60 if (child->thread.esp0 == 0)
61 return;
62 singlestep_disable(child);
63}
64
65enum csky_regset {
66 REGSET_GPR,
67 REGSET_FPR,
68};
69
70static int gpr_get(struct task_struct *target,
71 const struct user_regset *regset,
72 unsigned int pos, unsigned int count,
73 void *kbuf, void __user *ubuf)
74{
75 struct pt_regs *regs;
76
77 regs = task_pt_regs(target);
78
79 /* Abiv1 regs->tls is fake and we need sync here. */
80 regs->tls = task_thread_info(target)->tp_value;
81
82 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
83}
84
85static int gpr_set(struct task_struct *target,
86 const struct user_regset *regset,
87 unsigned int pos, unsigned int count,
88 const void *kbuf, const void __user *ubuf)
89{
90 int ret;
91 struct pt_regs regs;
92
93 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
94 if (ret)
95 return ret;
96
97 regs.sr = task_pt_regs(target)->sr;
98
99 task_thread_info(target)->tp_value = regs.tls;
100
101 *task_pt_regs(target) = regs;
102
103 return 0;
104}
105
106static int fpr_get(struct task_struct *target,
107 const struct user_regset *regset,
108 unsigned int pos, unsigned int count,
109 void *kbuf, void __user *ubuf)
110{
111 struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
112
113#if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
114 int i;
115 struct user_fp tmp = *regs;
116
117 for (i = 0; i < 16; i++) {
118 tmp.vr[i*4] = regs->vr[i*2];
119 tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
120 }
121
122 for (i = 0; i < 32; i++)
123 tmp.vr[64 + i] = regs->vr[32 + i];
124
125 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
126#else
127 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
128#endif
129}
130
131static int fpr_set(struct task_struct *target,
132 const struct user_regset *regset,
133 unsigned int pos, unsigned int count,
134 const void *kbuf, const void __user *ubuf)
135{
136 int ret;
137 struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
138
139#if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
140 int i;
141 struct user_fp tmp;
142
143 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
144
145 *regs = tmp;
146
147 for (i = 0; i < 16; i++) {
148 regs->vr[i*2] = tmp.vr[i*4];
149 regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
150 }
151
152 for (i = 0; i < 32; i++)
153 regs->vr[32 + i] = tmp.vr[64 + i];
154#else
155 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
156#endif
157
158 return ret;
159}
160
161static const struct user_regset csky_regsets[] = {
162 [REGSET_GPR] = {
163 .core_note_type = NT_PRSTATUS,
164 .n = ELF_NGREG,
165 .size = sizeof(u32),
166 .align = sizeof(u32),
167 .get = &gpr_get,
168 .set = &gpr_set,
169 },
170 [REGSET_FPR] = {
171 .core_note_type = NT_PRFPREG,
172 .n = sizeof(struct user_fp) / sizeof(u32),
173 .size = sizeof(u32),
174 .align = sizeof(u32),
175 .get = &fpr_get,
176 .set = &fpr_set,
177 },
178};
179
180static const struct user_regset_view user_csky_view = {
181 .name = "csky",
182 .e_machine = ELF_ARCH,
183 .regsets = csky_regsets,
184 .n = ARRAY_SIZE(csky_regsets),
185};
186
187const struct user_regset_view *task_user_regset_view(struct task_struct *task)
188{
189 return &user_csky_view;
190}
191
192void ptrace_disable(struct task_struct *child)
193{
194 singlestep_disable(child);
195}
196
197long arch_ptrace(struct task_struct *child, long request,
198 unsigned long addr, unsigned long data)
199{
200 long ret = -EIO;
201
202 switch (request) {
203 default:
204 ret = ptrace_request(child, request, addr, data);
205 break;
206 }
207
208 return ret;
209}
210
211/*
212 * If process's system calls is traces, do some corresponding handles in this
213 * function before entering system call function and after exiting system call
214 * function.
215 */
216asmlinkage void syscall_trace(int why, struct pt_regs *regs)
217{
218 long saved_why;
219 /*
220 * Save saved_why, why is used to denote syscall entry/exit;
221 * why = 0:entry, why = 1: exit
222 */
223 saved_why = regs->regs[SYSTRACE_SAVENUM];
224 regs->regs[SYSTRACE_SAVENUM] = why;
225
226 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
227 ? 0x80 : 0));
228
229 /*
230 * this isn't the same as continuing with a signal, but it will do
231 * for normal use. strace only continues with a signal if the
232 * stopping signal is not SIGTRAP. -brl
233 */
234 if (current->exit_code) {
235 send_sig(current->exit_code, current, 1);
236 current->exit_code = 0;
237 }
238
239 regs->regs[SYSTRACE_SAVENUM] = saved_why;
240}
241
242void show_regs(struct pt_regs *fp)
243{
244 unsigned long *sp;
245 unsigned char *tp;
246 int i;
247
248 pr_info("\nCURRENT PROCESS:\n\n");
249 pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
250
251 if (current->mm) {
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));
262 }
263
264 pr_info("PC: 0x%08lx\n", (long)fp->pc);
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}