Merge branch 'core/softlockup-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6] / arch / arm / kernel / kgdb.c
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/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 };