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