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