Skip to content

Commit 5cbad0e

Browse files
committed
kgdb: support for ARCH=arm
This patch adds the ARCH=arm specific a kgdb backend, originally written by Deepak Saxena <[email protected]> and George Davis <[email protected]>. Geoff Levand <[email protected]>, Nicolas Pitre, Manish Lachwani, and Jason Wessel have contributed various fixups here as well. The KGDB patch makes one change to the core ARM architecture such that the traps are initialized early for use with the debugger or other subsystems. [ [email protected]: small cleanups. ] [ [email protected]: fixed early_trap_init ] Signed-off-by: Jason Wessel <[email protected]> Acked-by: Deepak Saxena <[email protected]>
1 parent 68afab1 commit 5cbad0e

File tree

7 files changed

+316
-0
lines changed

7 files changed

+316
-0
lines changed

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ config ARM
1212
select RTC_LIB
1313
select SYS_SUPPORTS_APM_EMULATION
1414
select HAVE_OPROFILE
15+
select HAVE_ARCH_KGDB
1516
select HAVE_KPROBES if (!XIP_KERNEL)
1617
select HAVE_KRETPROBES if (HAVE_KPROBES)
1718
select HAVE_FTRACE if (!XIP_KERNEL)

arch/arm/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ obj-$(CONFIG_KPROBES) += kprobes.o kprobes-decode.o
2828
obj-$(CONFIG_ATAGS_PROC) += atags.o
2929
obj-$(CONFIG_OABI_COMPAT) += sys_oabi-compat.o
3030
obj-$(CONFIG_ARM_THUMBEE) += thumbee.o
31+
obj-$(CONFIG_KGDB) += kgdb.o
3132

3233
obj-$(CONFIG_CRUNCH) += crunch.o crunch-bits.o
3334
AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312

arch/arm/kernel/kgdb.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* arch/arm/kernel/kgdb.c
3+
*
4+
* ARM KGDB support
5+
*
6+
* Copyright (c) 2002-2004 MontaVista Software, Inc
7+
* Copyright (c) 2008 Wind River Systems, Inc.
8+
*
9+
* Authors: George Davis <[email protected]>
10+
* Deepak Saxena <[email protected]>
11+
*/
12+
#include <linux/kgdb.h>
13+
#include <asm/traps.h>
14+
15+
/* Make a local copy of the registers passed into the handler (bletch) */
16+
void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
17+
{
18+
int regno;
19+
20+
/* Initialize all to zero. */
21+
for (regno = 0; regno < GDB_MAX_REGS; regno++)
22+
gdb_regs[regno] = 0;
23+
24+
gdb_regs[_R0] = kernel_regs->ARM_r0;
25+
gdb_regs[_R1] = kernel_regs->ARM_r1;
26+
gdb_regs[_R2] = kernel_regs->ARM_r2;
27+
gdb_regs[_R3] = kernel_regs->ARM_r3;
28+
gdb_regs[_R4] = kernel_regs->ARM_r4;
29+
gdb_regs[_R5] = kernel_regs->ARM_r5;
30+
gdb_regs[_R6] = kernel_regs->ARM_r6;
31+
gdb_regs[_R7] = kernel_regs->ARM_r7;
32+
gdb_regs[_R8] = kernel_regs->ARM_r8;
33+
gdb_regs[_R9] = kernel_regs->ARM_r9;
34+
gdb_regs[_R10] = kernel_regs->ARM_r10;
35+
gdb_regs[_FP] = kernel_regs->ARM_fp;
36+
gdb_regs[_IP] = kernel_regs->ARM_ip;
37+
gdb_regs[_SPT] = kernel_regs->ARM_sp;
38+
gdb_regs[_LR] = kernel_regs->ARM_lr;
39+
gdb_regs[_PC] = kernel_regs->ARM_pc;
40+
gdb_regs[_CPSR] = kernel_regs->ARM_cpsr;
41+
}
42+
43+
/* Copy local gdb registers back to kgdb regs, for later copy to kernel */
44+
void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *kernel_regs)
45+
{
46+
kernel_regs->ARM_r0 = gdb_regs[_R0];
47+
kernel_regs->ARM_r1 = gdb_regs[_R1];
48+
kernel_regs->ARM_r2 = gdb_regs[_R2];
49+
kernel_regs->ARM_r3 = gdb_regs[_R3];
50+
kernel_regs->ARM_r4 = gdb_regs[_R4];
51+
kernel_regs->ARM_r5 = gdb_regs[_R5];
52+
kernel_regs->ARM_r6 = gdb_regs[_R6];
53+
kernel_regs->ARM_r7 = gdb_regs[_R7];
54+
kernel_regs->ARM_r8 = gdb_regs[_R8];
55+
kernel_regs->ARM_r9 = gdb_regs[_R9];
56+
kernel_regs->ARM_r10 = gdb_regs[_R10];
57+
kernel_regs->ARM_fp = gdb_regs[_FP];
58+
kernel_regs->ARM_ip = gdb_regs[_IP];
59+
kernel_regs->ARM_sp = gdb_regs[_SPT];
60+
kernel_regs->ARM_lr = gdb_regs[_LR];
61+
kernel_regs->ARM_pc = gdb_regs[_PC];
62+
kernel_regs->ARM_cpsr = gdb_regs[_CPSR];
63+
}
64+
65+
void
66+
sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
67+
{
68+
struct pt_regs *thread_regs;
69+
int regno;
70+
71+
/* Just making sure... */
72+
if (task == NULL)
73+
return;
74+
75+
/* Initialize to zero */
76+
for (regno = 0; regno < GDB_MAX_REGS; regno++)
77+
gdb_regs[regno] = 0;
78+
79+
/* Otherwise, we have only some registers from switch_to() */
80+
thread_regs = task_pt_regs(task);
81+
gdb_regs[_R0] = thread_regs->ARM_r0;
82+
gdb_regs[_R1] = thread_regs->ARM_r1;
83+
gdb_regs[_R2] = thread_regs->ARM_r2;
84+
gdb_regs[_R3] = thread_regs->ARM_r3;
85+
gdb_regs[_R4] = thread_regs->ARM_r4;
86+
gdb_regs[_R5] = thread_regs->ARM_r5;
87+
gdb_regs[_R6] = thread_regs->ARM_r6;
88+
gdb_regs[_R7] = thread_regs->ARM_r7;
89+
gdb_regs[_R8] = thread_regs->ARM_r8;
90+
gdb_regs[_R9] = thread_regs->ARM_r9;
91+
gdb_regs[_R10] = thread_regs->ARM_r10;
92+
gdb_regs[_FP] = thread_regs->ARM_fp;
93+
gdb_regs[_IP] = thread_regs->ARM_ip;
94+
gdb_regs[_SPT] = thread_regs->ARM_sp;
95+
gdb_regs[_LR] = thread_regs->ARM_lr;
96+
gdb_regs[_PC] = thread_regs->ARM_pc;
97+
gdb_regs[_CPSR] = thread_regs->ARM_cpsr;
98+
}
99+
100+
static int compiled_break;
101+
102+
int kgdb_arch_handle_exception(int exception_vector, int signo,
103+
int err_code, char *remcom_in_buffer,
104+
char *remcom_out_buffer,
105+
struct pt_regs *linux_regs)
106+
{
107+
unsigned long addr;
108+
char *ptr;
109+
110+
switch (remcom_in_buffer[0]) {
111+
case 'D':
112+
case 'k':
113+
case 'c':
114+
kgdb_contthread = NULL;
115+
116+
/*
117+
* Try to read optional parameter, pc unchanged if no parm.
118+
* If this was a compiled breakpoint, we need to move
119+
* to the next instruction or we will just breakpoint
120+
* over and over again.
121+
*/
122+
ptr = &remcom_in_buffer[1];
123+
if (kgdb_hex2long(&ptr, &addr))
124+
linux_regs->ARM_pc = addr;
125+
else if (compiled_break == 1)
126+
linux_regs->ARM_pc += 4;
127+
128+
compiled_break = 0;
129+
130+
return 0;
131+
}
132+
133+
return -1;
134+
}
135+
136+
static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
137+
{
138+
kgdb_handle_exception(1, SIGTRAP, 0, regs);
139+
140+
return 0;
141+
}
142+
143+
static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
144+
{
145+
compiled_break = 1;
146+
kgdb_handle_exception(1, SIGTRAP, 0, regs);
147+
148+
return 0;
149+
}
150+
151+
static struct undef_hook kgdb_brkpt_hook = {
152+
.instr_mask = 0xffffffff,
153+
.instr_val = KGDB_BREAKINST,
154+
.fn = kgdb_brk_fn
155+
};
156+
157+
static struct undef_hook kgdb_compiled_brkpt_hook = {
158+
.instr_mask = 0xffffffff,
159+
.instr_val = KGDB_COMPILED_BREAK,
160+
.fn = kgdb_compiled_brk_fn
161+
};
162+
163+
/**
164+
* kgdb_arch_init - Perform any architecture specific initalization.
165+
*
166+
* This function will handle the initalization of any architecture
167+
* specific callbacks.
168+
*/
169+
int kgdb_arch_init(void)
170+
{
171+
register_undef_hook(&kgdb_brkpt_hook);
172+
register_undef_hook(&kgdb_compiled_brkpt_hook);
173+
174+
return 0;
175+
}
176+
177+
/**
178+
* kgdb_arch_exit - Perform any architecture specific uninitalization.
179+
*
180+
* This function will handle the uninitalization of any architecture
181+
* specific callbacks, for dynamic registration and unregistration.
182+
*/
183+
void kgdb_arch_exit(void)
184+
{
185+
unregister_undef_hook(&kgdb_brkpt_hook);
186+
unregister_undef_hook(&kgdb_compiled_brkpt_hook);
187+
}
188+
189+
/*
190+
* Register our undef instruction hooks with ARM undef core.
191+
* We regsiter a hook specifically looking for the KGB break inst
192+
* and we handle the normal undef case within the do_undefinstr
193+
* handler.
194+
*/
195+
struct kgdb_arch arch_kgdb_ops = {
196+
#ifndef __ARMEB__
197+
.gdb_bpt_instr = {0xfe, 0xde, 0xff, 0xe7}
198+
#else /* ! __ARMEB__ */
199+
.gdb_bpt_instr = {0xe7, 0xff, 0xde, 0xfe}
200+
#endif
201+
};

arch/arm/kernel/setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <asm/mach/arch.h>
3737
#include <asm/mach/irq.h>
3838
#include <asm/mach/time.h>
39+
#include <asm/traps.h>
3940

4041
#include "compat.h"
4142
#include "atags.h"
@@ -853,6 +854,7 @@ void __init setup_arch(char **cmdline_p)
853854
conswitchp = &dummy_con;
854855
#endif
855856
#endif
857+
early_trap_init();
856858
}
857859

858860

arch/arm/kernel/traps.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,11 @@ void abort(void)
707707
EXPORT_SYMBOL(abort);
708708

709709
void __init trap_init(void)
710+
{
711+
return;
712+
}
713+
714+
void __init early_trap_init(void)
710715
{
711716
unsigned long vectors = CONFIG_VECTORS_BASE;
712717
extern char __stubs_start[], __stubs_end[];

include/asm-arm/kgdb.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* ARM KGDB support
3+
*
4+
* Author: Deepak Saxena <[email protected]>
5+
*
6+
* Copyright (C) 2002 MontaVista Software Inc.
7+
*
8+
*/
9+
10+
#ifndef __ARM_KGDB_H__
11+
#define __ARM_KGDB_H__
12+
13+
#include <linux/ptrace.h>
14+
15+
/*
16+
* GDB assumes that we're a user process being debugged, so
17+
* it will send us an SWI command to write into memory as the
18+
* debug trap. When an SWI occurs, the next instruction addr is
19+
* placed into R14_svc before jumping to the vector trap.
20+
* This doesn't work for kernel debugging as we are already in SVC
21+
* we would loose the kernel's LR, which is a bad thing. This
22+
* is bad thing.
23+
*
24+
* By doing this as an undefined instruction trap, we force a mode
25+
* switch from SVC to UND mode, allowing us to save full kernel state.
26+
*
27+
* We also define a KGDB_COMPILED_BREAK which can be used to compile
28+
* in breakpoints. This is important for things like sysrq-G and for
29+
* the initial breakpoint from trap_init().
30+
*
31+
* Note to ARM HW designers: Add real trap support like SH && PPC to
32+
* make our lives much much simpler. :)
33+
*/
34+
#define BREAK_INSTR_SIZE 4
35+
#define GDB_BREAKINST 0xef9f0001
36+
#define KGDB_BREAKINST 0xe7ffdefe
37+
#define KGDB_COMPILED_BREAK 0xe7ffdeff
38+
#define CACHE_FLUSH_IS_SAFE 1
39+
40+
#ifndef __ASSEMBLY__
41+
42+
static inline void arch_kgdb_breakpoint(void)
43+
{
44+
asm(".word 0xe7ffdeff");
45+
}
46+
47+
extern void kgdb_handle_bus_error(void);
48+
extern int kgdb_fault_expected;
49+
50+
#endif /* !__ASSEMBLY__ */
51+
52+
/*
53+
* From Kevin Hilman:
54+
*
55+
* gdb is expecting the following registers layout.
56+
*
57+
* r0-r15: 1 long word each
58+
* f0-f7: unused, 3 long words each !!
59+
* fps: unused, 1 long word
60+
* cpsr: 1 long word
61+
*
62+
* Even though f0-f7 and fps are not used, they need to be
63+
* present in the registers sent for correct processing in
64+
* the host-side gdb.
65+
*
66+
* In particular, it is crucial that CPSR is in the right place,
67+
* otherwise gdb will not be able to correctly interpret stepping over
68+
* conditional branches.
69+
*/
70+
#define _GP_REGS 16
71+
#define _FP_REGS 8
72+
#define _EXTRA_REGS 2
73+
#define GDB_MAX_REGS (_GP_REGS + (_FP_REGS * 3) + _EXTRA_REGS)
74+
75+
#define KGDB_MAX_NO_CPUS 1
76+
#define BUFMAX 400
77+
#define NUMREGBYTES (GDB_MAX_REGS << 2)
78+
#define NUMCRITREGBYTES (32 << 2)
79+
80+
#define _R0 0
81+
#define _R1 1
82+
#define _R2 2
83+
#define _R3 3
84+
#define _R4 4
85+
#define _R5 5
86+
#define _R6 6
87+
#define _R7 7
88+
#define _R8 8
89+
#define _R9 9
90+
#define _R10 10
91+
#define _FP 11
92+
#define _IP 12
93+
#define _SPT 13
94+
#define _LR 14
95+
#define _PC 15
96+
#define _CPSR (GDB_MAX_REGS - 1)
97+
98+
/*
99+
* So that we can denote the end of a frame for tracing,
100+
* in the simple case:
101+
*/
102+
#define CFI_END_FRAME(func) __CFI_END_FRAME(_PC, _SPT, func)
103+
104+
#endif /* __ASM_KGDB_H__ */

include/asm-arm/traps.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ static inline int in_exception_text(unsigned long ptr)
2424
ptr < (unsigned long)&__exception_text_end;
2525
}
2626

27+
extern void __init early_trap_init(void);
28+
2729
#endif

0 commit comments

Comments
 (0)