xref: /linux/arch/arm/kernel/kgdb.c (revision f8324e20f8289dffc646d64366332e05eaacab25)
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 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
102 {
103 	regs->ARM_pc = pc;
104 }
105 
106 static int compiled_break;
107 
108 int kgdb_arch_handle_exception(int exception_vector, int signo,
109 			       int err_code, char *remcom_in_buffer,
110 			       char *remcom_out_buffer,
111 			       struct pt_regs *linux_regs)
112 {
113 	unsigned long addr;
114 	char *ptr;
115 
116 	switch (remcom_in_buffer[0]) {
117 	case 'D':
118 	case 'k':
119 	case 'c':
120 		/*
121 		 * Try to read optional parameter, pc unchanged if no parm.
122 		 * If this was a compiled breakpoint, we need to move
123 		 * to the next instruction or we will just breakpoint
124 		 * over and over again.
125 		 */
126 		ptr = &remcom_in_buffer[1];
127 		if (kgdb_hex2long(&ptr, &addr))
128 			linux_regs->ARM_pc = addr;
129 		else if (compiled_break == 1)
130 			linux_regs->ARM_pc += 4;
131 
132 		compiled_break = 0;
133 
134 		return 0;
135 	}
136 
137 	return -1;
138 }
139 
140 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int instr)
141 {
142 	kgdb_handle_exception(1, SIGTRAP, 0, regs);
143 
144 	return 0;
145 }
146 
147 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int instr)
148 {
149 	compiled_break = 1;
150 	kgdb_handle_exception(1, SIGTRAP, 0, regs);
151 
152 	return 0;
153 }
154 
155 static struct undef_hook kgdb_brkpt_hook = {
156 	.instr_mask		= 0xffffffff,
157 	.instr_val		= KGDB_BREAKINST,
158 	.fn			= kgdb_brk_fn
159 };
160 
161 static struct undef_hook kgdb_compiled_brkpt_hook = {
162 	.instr_mask		= 0xffffffff,
163 	.instr_val		= KGDB_COMPILED_BREAK,
164 	.fn			= kgdb_compiled_brk_fn
165 };
166 
167 static void kgdb_call_nmi_hook(void *ignored)
168 {
169        kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
170 }
171 
172 void kgdb_roundup_cpus(unsigned long flags)
173 {
174        local_irq_enable();
175        smp_call_function(kgdb_call_nmi_hook, NULL, 0);
176        local_irq_disable();
177 }
178 
179 /**
180  *	kgdb_arch_init - Perform any architecture specific initalization.
181  *
182  *	This function will handle the initalization of any architecture
183  *	specific callbacks.
184  */
185 int kgdb_arch_init(void)
186 {
187 	register_undef_hook(&kgdb_brkpt_hook);
188 	register_undef_hook(&kgdb_compiled_brkpt_hook);
189 
190 	return 0;
191 }
192 
193 /**
194  *	kgdb_arch_exit - Perform any architecture specific uninitalization.
195  *
196  *	This function will handle the uninitalization of any architecture
197  *	specific callbacks, for dynamic registration and unregistration.
198  */
199 void kgdb_arch_exit(void)
200 {
201 	unregister_undef_hook(&kgdb_brkpt_hook);
202 	unregister_undef_hook(&kgdb_compiled_brkpt_hook);
203 }
204 
205 /*
206  * Register our undef instruction hooks with ARM undef core.
207  * We regsiter a hook specifically looking for the KGB break inst
208  * and we handle the normal undef case within the do_undefinstr
209  * handler.
210  */
211 struct kgdb_arch arch_kgdb_ops = {
212 #ifndef __ARMEB__
213 	.gdb_bpt_instr		= {0xfe, 0xde, 0xff, 0xe7}
214 #else /* ! __ARMEB__ */
215 	.gdb_bpt_instr		= {0xe7, 0xff, 0xde, 0xfe}
216 #endif
217 };
218