summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuo Ren <ren_guo@c-sky.com>2018-09-05 02:25:19 -0400
committerGuo Ren <ren_guo@c-sky.com>2018-10-25 12:54:25 -0400
commit2ce36bfa6a0904524baed699ad78706e50295f84 (patch)
tree830abbd96e53dd36881e2a80868e4c8679ca88c1
parentda551281947cb2c0ab6e0045529a706af5d79584 (diff)
csky: Debug and Ptrace GDB
This patch adds arch ptrace implementation, stack dump and bug.h. Signed-off-by: Guo Ren <ren_guo@c-sky.com> Reviewed-by: Arnd Bergmann <arnd@arndb.de>
-rw-r--r--arch/csky/include/asm/bug.h26
-rw-r--r--arch/csky/include/uapi/asm/ptrace.h104
-rw-r--r--arch/csky/kernel/dumpstack.c66
-rw-r--r--arch/csky/kernel/ptrace.c314
4 files changed, 510 insertions, 0 deletions
diff --git a/arch/csky/include/asm/bug.h b/arch/csky/include/asm/bug.h
new file mode 100644
index 000000000000..bd7b3235bb84
--- /dev/null
+++ b/arch/csky/include/asm/bug.h
@@ -0,0 +1,26 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#ifndef __ASM_CSKY_BUG_H
5#define __ASM_CSKY_BUG_H
6
7#include <linux/compiler.h>
8#include <linux/const.h>
9#include <linux/types.h>
10
11#define BUG() \
12do { \
13 asm volatile ("bkpt\n"); \
14 unreachable(); \
15} while (0)
16
17#define HAVE_ARCH_BUG
18
19#include <asm-generic/bug.h>
20
21struct pt_regs;
22
23void die_if_kernel(char *str, struct pt_regs *regs, int nr);
24void show_regs(struct pt_regs *regs);
25
26#endif /* __ASM_CSKY_BUG_H */
diff --git a/arch/csky/include/uapi/asm/ptrace.h b/arch/csky/include/uapi/asm/ptrace.h
new file mode 100644
index 000000000000..f10d02c8b09e
--- /dev/null
+++ b/arch/csky/include/uapi/asm/ptrace.h
@@ -0,0 +1,104 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#ifndef _CSKY_PTRACE_H
5#define _CSKY_PTRACE_H
6
7#ifndef __ASSEMBLY__
8
9struct pt_regs {
10 unsigned long tls;
11 unsigned long lr;
12 unsigned long pc;
13 unsigned long sr;
14 unsigned long usp;
15
16 /*
17 * a0, a1, a2, a3:
18 * abiv1: r2, r3, r4, r5
19 * abiv2: r0, r1, r2, r3
20 */
21 unsigned long orig_a0;
22 unsigned long a0;
23 unsigned long a1;
24 unsigned long a2;
25 unsigned long a3;
26
27 /*
28 * ABIV2: r4 ~ r13
29 * ABIV1: r6 ~ r14, r1
30 */
31 unsigned long regs[10];
32
33#if defined(__CSKYABIV2__)
34 /* r16 ~ r30 */
35 unsigned long exregs[15];
36
37 unsigned long rhi;
38 unsigned long rlo;
39 unsigned long pad; /* reserved */
40#endif
41};
42
43struct user_fp {
44 unsigned long vr[96];
45 unsigned long fcr;
46 unsigned long fesr;
47 unsigned long fid;
48 unsigned long reserved;
49};
50
51/*
52 * Switch stack for switch_to after push pt_regs.
53 *
54 * ABI_CSKYV2: r4 ~ r11, r15 ~ r17, r26 ~ r30;
55 * ABI_CSKYV1: r8 ~ r14, r15;
56 */
57struct switch_stack {
58#if defined(__CSKYABIV2__)
59 unsigned long r4;
60 unsigned long r5;
61 unsigned long r6;
62 unsigned long r7;
63 unsigned long r8;
64 unsigned long r9;
65 unsigned long r10;
66 unsigned long r11;
67#else
68 unsigned long r8;
69 unsigned long r9;
70 unsigned long r10;
71 unsigned long r11;
72 unsigned long r12;
73 unsigned long r13;
74 unsigned long r14;
75#endif
76 unsigned long r15;
77#if defined(__CSKYABIV2__)
78 unsigned long r16;
79 unsigned long r17;
80 unsigned long r26;
81 unsigned long r27;
82 unsigned long r28;
83 unsigned long r29;
84 unsigned long r30;
85#endif
86};
87
88#ifdef __KERNEL__
89
90#define PS_S 0x80000000 /* Supervisor Mode */
91
92#define arch_has_single_step() (1)
93#define current_pt_regs() \
94({ (struct pt_regs *)((char *)current_thread_info() + THREAD_SIZE) - 1; })
95
96#define user_stack_pointer(regs) ((regs)->usp)
97
98#define user_mode(regs) (!((regs)->sr & PS_S))
99#define instruction_pointer(regs) ((regs)->pc)
100#define profile_pc(regs) instruction_pointer(regs)
101
102#endif /* __KERNEL__ */
103#endif /* __ASSEMBLY__ */
104#endif /* _CSKY_PTRACE_H */
diff --git a/arch/csky/kernel/dumpstack.c b/arch/csky/kernel/dumpstack.c
new file mode 100644
index 000000000000..a9a03ac57ec5
--- /dev/null
+++ b/arch/csky/kernel/dumpstack.c
@@ -0,0 +1,66 @@
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#include <linux/ptrace.h>
5
6int kstack_depth_to_print = 48;
7
8void show_trace(unsigned long *stack)
9{
10 unsigned long *endstack;
11 unsigned long addr;
12 int i;
13
14 pr_info("Call Trace:\n");
15 addr = (unsigned long)stack + THREAD_SIZE - 1;
16 endstack = (unsigned long *)(addr & -THREAD_SIZE);
17 i = 0;
18 while (stack + 1 <= endstack) {
19 addr = *stack++;
20 /*
21 * If the address is either in the text segment of the
22 * kernel, or in the region which contains vmalloc'ed
23 * memory, it *may* be the address of a calling
24 * routine; if so, print it so that someone tracing
25 * down the cause of the crash will be able to figure
26 * out the call path that was taken.
27 */
28 if (__kernel_text_address(addr)) {
29#ifndef CONFIG_KALLSYMS
30 if (i % 5 == 0)
31 pr_cont("\n ");
32#endif
33 pr_cont(" [<%08lx>] %pS\n", addr, (void *)addr);
34 i++;
35 }
36 }
37 pr_cont("\n");
38}
39
40void show_stack(struct task_struct *task, unsigned long *stack)
41{
42 unsigned long *p;
43 unsigned long *endstack;
44 int i;
45
46 if (!stack) {
47 if (task)
48 stack = (unsigned long *)task->thread.esp0;
49 else
50 stack = (unsigned long *)&stack;
51 }
52 endstack = (unsigned long *)
53 (((unsigned long)stack + THREAD_SIZE - 1) & -THREAD_SIZE);
54
55 pr_info("Stack from %08lx:", (unsigned long)stack);
56 p = stack;
57 for (i = 0; i < kstack_depth_to_print; i++) {
58 if (p + 1 > endstack)
59 break;
60 if (i % 8 == 0)
61 pr_cont("\n ");
62 pr_cont(" %08lx", *p++);
63 }
64 pr_cont("\n");
65 show_trace(stack);
66}
diff --git a/arch/csky/kernel/ptrace.c b/arch/csky/kernel/ptrace.c
new file mode 100644
index 000000000000..34b30257298f
--- /dev/null
+++ b/arch/csky/kernel/ptrace.c
@@ -0,0 +1,314 @@
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}