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