]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm64/kernel/kgdb.c
Merge branch 'for-4.8/core' of git://git.kernel.dk/linux-block
[karo-tx-linux.git] / arch / arm64 / kernel / kgdb.c
1 /*
2  * AArch64 KGDB support
3  *
4  * Based on arch/arm/kernel/kgdb.c
5  *
6  * Copyright (C) 2013 Cavium Inc.
7  * Author: Vijaya Kumar K <vijaya.kumar@caviumnetworks.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <linux/irq.h>
23 #include <linux/kdebug.h>
24 #include <linux/kgdb.h>
25 #include <asm/traps.h>
26
27 struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] = {
28         { "x0", 8, offsetof(struct pt_regs, regs[0])},
29         { "x1", 8, offsetof(struct pt_regs, regs[1])},
30         { "x2", 8, offsetof(struct pt_regs, regs[2])},
31         { "x3", 8, offsetof(struct pt_regs, regs[3])},
32         { "x4", 8, offsetof(struct pt_regs, regs[4])},
33         { "x5", 8, offsetof(struct pt_regs, regs[5])},
34         { "x6", 8, offsetof(struct pt_regs, regs[6])},
35         { "x7", 8, offsetof(struct pt_regs, regs[7])},
36         { "x8", 8, offsetof(struct pt_regs, regs[8])},
37         { "x9", 8, offsetof(struct pt_regs, regs[9])},
38         { "x10", 8, offsetof(struct pt_regs, regs[10])},
39         { "x11", 8, offsetof(struct pt_regs, regs[11])},
40         { "x12", 8, offsetof(struct pt_regs, regs[12])},
41         { "x13", 8, offsetof(struct pt_regs, regs[13])},
42         { "x14", 8, offsetof(struct pt_regs, regs[14])},
43         { "x15", 8, offsetof(struct pt_regs, regs[15])},
44         { "x16", 8, offsetof(struct pt_regs, regs[16])},
45         { "x17", 8, offsetof(struct pt_regs, regs[17])},
46         { "x18", 8, offsetof(struct pt_regs, regs[18])},
47         { "x19", 8, offsetof(struct pt_regs, regs[19])},
48         { "x20", 8, offsetof(struct pt_regs, regs[20])},
49         { "x21", 8, offsetof(struct pt_regs, regs[21])},
50         { "x22", 8, offsetof(struct pt_regs, regs[22])},
51         { "x23", 8, offsetof(struct pt_regs, regs[23])},
52         { "x24", 8, offsetof(struct pt_regs, regs[24])},
53         { "x25", 8, offsetof(struct pt_regs, regs[25])},
54         { "x26", 8, offsetof(struct pt_regs, regs[26])},
55         { "x27", 8, offsetof(struct pt_regs, regs[27])},
56         { "x28", 8, offsetof(struct pt_regs, regs[28])},
57         { "x29", 8, offsetof(struct pt_regs, regs[29])},
58         { "x30", 8, offsetof(struct pt_regs, regs[30])},
59         { "sp", 8, offsetof(struct pt_regs, sp)},
60         { "pc", 8, offsetof(struct pt_regs, pc)},
61         /*
62          * struct pt_regs thinks PSTATE is 64-bits wide but gdb remote
63          * protocol disagrees. Therefore we must extract only the lower
64          * 32-bits. Look for the big comment in asm/kgdb.h for more
65          * detail.
66          */
67         { "pstate", 4, offsetof(struct pt_regs, pstate)
68 #ifdef CONFIG_CPU_BIG_ENDIAN
69                                                         + 4
70 #endif
71         },
72         { "v0", 16, -1 },
73         { "v1", 16, -1 },
74         { "v2", 16, -1 },
75         { "v3", 16, -1 },
76         { "v4", 16, -1 },
77         { "v5", 16, -1 },
78         { "v6", 16, -1 },
79         { "v7", 16, -1 },
80         { "v8", 16, -1 },
81         { "v9", 16, -1 },
82         { "v10", 16, -1 },
83         { "v11", 16, -1 },
84         { "v12", 16, -1 },
85         { "v13", 16, -1 },
86         { "v14", 16, -1 },
87         { "v15", 16, -1 },
88         { "v16", 16, -1 },
89         { "v17", 16, -1 },
90         { "v18", 16, -1 },
91         { "v19", 16, -1 },
92         { "v20", 16, -1 },
93         { "v21", 16, -1 },
94         { "v22", 16, -1 },
95         { "v23", 16, -1 },
96         { "v24", 16, -1 },
97         { "v25", 16, -1 },
98         { "v26", 16, -1 },
99         { "v27", 16, -1 },
100         { "v28", 16, -1 },
101         { "v29", 16, -1 },
102         { "v30", 16, -1 },
103         { "v31", 16, -1 },
104         { "fpsr", 4, -1 },
105         { "fpcr", 4, -1 },
106 };
107
108 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
109 {
110         if (regno >= DBG_MAX_REG_NUM || regno < 0)
111                 return NULL;
112
113         if (dbg_reg_def[regno].offset != -1)
114                 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
115                        dbg_reg_def[regno].size);
116         else
117                 memset(mem, 0, dbg_reg_def[regno].size);
118         return dbg_reg_def[regno].name;
119 }
120
121 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
122 {
123         if (regno >= DBG_MAX_REG_NUM || regno < 0)
124                 return -EINVAL;
125
126         if (dbg_reg_def[regno].offset != -1)
127                 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
128                        dbg_reg_def[regno].size);
129         return 0;
130 }
131
132 void
133 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
134 {
135         struct pt_regs *thread_regs;
136
137         /* Initialize to zero */
138         memset((char *)gdb_regs, 0, NUMREGBYTES);
139         thread_regs = task_pt_regs(task);
140         memcpy((void *)gdb_regs, (void *)thread_regs->regs, GP_REG_BYTES);
141         /* Special case for PSTATE (check comments in asm/kgdb.h for details) */
142         dbg_get_reg(33, gdb_regs + GP_REG_BYTES, thread_regs);
143 }
144
145 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
146 {
147         regs->pc = pc;
148 }
149
150 static int compiled_break;
151
152 static void kgdb_arch_update_addr(struct pt_regs *regs,
153                                 char *remcom_in_buffer)
154 {
155         unsigned long addr;
156         char *ptr;
157
158         ptr = &remcom_in_buffer[1];
159         if (kgdb_hex2long(&ptr, &addr))
160                 kgdb_arch_set_pc(regs, addr);
161         else if (compiled_break == 1)
162                 kgdb_arch_set_pc(regs, regs->pc + 4);
163
164         compiled_break = 0;
165 }
166
167 int kgdb_arch_handle_exception(int exception_vector, int signo,
168                                int err_code, char *remcom_in_buffer,
169                                char *remcom_out_buffer,
170                                struct pt_regs *linux_regs)
171 {
172         int err;
173
174         switch (remcom_in_buffer[0]) {
175         case 'D':
176         case 'k':
177                 /*
178                  * Packet D (Detach), k (kill). No special handling
179                  * is required here. Handle same as c packet.
180                  */
181         case 'c':
182                 /*
183                  * Packet c (Continue) to continue executing.
184                  * Set pc to required address.
185                  * Try to read optional parameter and set pc.
186                  * If this was a compiled breakpoint, we need to move
187                  * to the next instruction else we will just breakpoint
188                  * over and over again.
189                  */
190                 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
191                 atomic_set(&kgdb_cpu_doing_single_step, -1);
192                 kgdb_single_step =  0;
193
194                 /*
195                  * Received continue command, disable single step
196                  */
197                 if (kernel_active_single_step())
198                         kernel_disable_single_step();
199
200                 err = 0;
201                 break;
202         case 's':
203                 /*
204                  * Update step address value with address passed
205                  * with step packet.
206                  * On debug exception return PC is copied to ELR
207                  * So just update PC.
208                  * If no step address is passed, resume from the address
209                  * pointed by PC. Do not update PC
210                  */
211                 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
212                 atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
213                 kgdb_single_step =  1;
214
215                 /*
216                  * Enable single step handling
217                  */
218                 if (!kernel_active_single_step())
219                         kernel_enable_single_step(linux_regs);
220                 err = 0;
221                 break;
222         default:
223                 err = -1;
224         }
225         return err;
226 }
227
228 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
229 {
230         kgdb_handle_exception(1, SIGTRAP, 0, regs);
231         return 0;
232 }
233
234 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
235 {
236         compiled_break = 1;
237         kgdb_handle_exception(1, SIGTRAP, 0, regs);
238
239         return 0;
240 }
241
242 static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
243 {
244         kgdb_handle_exception(1, SIGTRAP, 0, regs);
245         return 0;
246 }
247
248 static struct break_hook kgdb_brkpt_hook = {
249         .esr_mask       = 0xffffffff,
250         .esr_val        = (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
251         .fn             = kgdb_brk_fn
252 };
253
254 static struct break_hook kgdb_compiled_brkpt_hook = {
255         .esr_mask       = 0xffffffff,
256         .esr_val        = (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
257         .fn             = kgdb_compiled_brk_fn
258 };
259
260 static struct step_hook kgdb_step_hook = {
261         .fn             = kgdb_step_brk_fn
262 };
263
264 static void kgdb_call_nmi_hook(void *ignored)
265 {
266         kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
267 }
268
269 void kgdb_roundup_cpus(unsigned long flags)
270 {
271         local_irq_enable();
272         smp_call_function(kgdb_call_nmi_hook, NULL, 0);
273         local_irq_disable();
274 }
275
276 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
277 {
278         struct pt_regs *regs = args->regs;
279
280         if (kgdb_handle_exception(1, args->signr, cmd, regs))
281                 return NOTIFY_DONE;
282         return NOTIFY_STOP;
283 }
284
285 static int
286 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
287 {
288         unsigned long flags;
289         int ret;
290
291         local_irq_save(flags);
292         ret = __kgdb_notify(ptr, cmd);
293         local_irq_restore(flags);
294
295         return ret;
296 }
297
298 static struct notifier_block kgdb_notifier = {
299         .notifier_call  = kgdb_notify,
300         /*
301          * Want to be lowest priority
302          */
303         .priority       = -INT_MAX,
304 };
305
306 /*
307  * kgdb_arch_init - Perform any architecture specific initialization.
308  * This function will handle the initialization of any architecture
309  * specific callbacks.
310  */
311 int kgdb_arch_init(void)
312 {
313         int ret = register_die_notifier(&kgdb_notifier);
314
315         if (ret != 0)
316                 return ret;
317
318         register_break_hook(&kgdb_brkpt_hook);
319         register_break_hook(&kgdb_compiled_brkpt_hook);
320         register_step_hook(&kgdb_step_hook);
321         return 0;
322 }
323
324 /*
325  * kgdb_arch_exit - Perform any architecture specific uninitalization.
326  * This function will handle the uninitalization of any architecture
327  * specific callbacks, for dynamic registration and unregistration.
328  */
329 void kgdb_arch_exit(void)
330 {
331         unregister_break_hook(&kgdb_brkpt_hook);
332         unregister_break_hook(&kgdb_compiled_brkpt_hook);
333         unregister_step_hook(&kgdb_step_hook);
334         unregister_die_notifier(&kgdb_notifier);
335 }
336
337 /*
338  * ARM instructions are always in LE.
339  * Break instruction is encoded in LE format
340  */
341 struct kgdb_arch arch_kgdb_ops = {
342         .gdb_bpt_instr = {
343                 KGDB_DYN_BRK_INS_BYTE(0),
344                 KGDB_DYN_BRK_INS_BYTE(1),
345                 KGDB_DYN_BRK_INS_BYTE(2),
346                 KGDB_DYN_BRK_INS_BYTE(3),
347         }
348 };