arm64: dts: rockchip: amend usb-otg related nodes for rk3368-tb
[firefly-linux-kernel-4.4.55.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         { "pstate", 8, offsetof(struct pt_regs, pstate)},
63         { "v0", 16, -1 },
64         { "v1", 16, -1 },
65         { "v2", 16, -1 },
66         { "v3", 16, -1 },
67         { "v4", 16, -1 },
68         { "v5", 16, -1 },
69         { "v6", 16, -1 },
70         { "v7", 16, -1 },
71         { "v8", 16, -1 },
72         { "v9", 16, -1 },
73         { "v10", 16, -1 },
74         { "v11", 16, -1 },
75         { "v12", 16, -1 },
76         { "v13", 16, -1 },
77         { "v14", 16, -1 },
78         { "v15", 16, -1 },
79         { "v16", 16, -1 },
80         { "v17", 16, -1 },
81         { "v18", 16, -1 },
82         { "v19", 16, -1 },
83         { "v20", 16, -1 },
84         { "v21", 16, -1 },
85         { "v22", 16, -1 },
86         { "v23", 16, -1 },
87         { "v24", 16, -1 },
88         { "v25", 16, -1 },
89         { "v26", 16, -1 },
90         { "v27", 16, -1 },
91         { "v28", 16, -1 },
92         { "v29", 16, -1 },
93         { "v30", 16, -1 },
94         { "v31", 16, -1 },
95         { "fpsr", 4, -1 },
96         { "fpcr", 4, -1 },
97 };
98
99 char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
100 {
101         if (regno >= DBG_MAX_REG_NUM || regno < 0)
102                 return NULL;
103
104         if (dbg_reg_def[regno].offset != -1)
105                 memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
106                        dbg_reg_def[regno].size);
107         else
108                 memset(mem, 0, dbg_reg_def[regno].size);
109         return dbg_reg_def[regno].name;
110 }
111
112 int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
113 {
114         if (regno >= DBG_MAX_REG_NUM || regno < 0)
115                 return -EINVAL;
116
117         if (dbg_reg_def[regno].offset != -1)
118                 memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
119                        dbg_reg_def[regno].size);
120         return 0;
121 }
122
123 void
124 sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *task)
125 {
126         struct pt_regs *thread_regs;
127
128         /* Initialize to zero */
129         memset((char *)gdb_regs, 0, NUMREGBYTES);
130         thread_regs = task_pt_regs(task);
131         memcpy((void *)gdb_regs, (void *)thread_regs->regs, GP_REG_BYTES);
132 }
133
134 void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
135 {
136         regs->pc = pc;
137 }
138
139 static int compiled_break;
140
141 static void kgdb_arch_update_addr(struct pt_regs *regs,
142                                 char *remcom_in_buffer)
143 {
144         unsigned long addr;
145         char *ptr;
146
147         ptr = &remcom_in_buffer[1];
148         if (kgdb_hex2long(&ptr, &addr))
149                 kgdb_arch_set_pc(regs, addr);
150         else if (compiled_break == 1)
151                 kgdb_arch_set_pc(regs, regs->pc + 4);
152
153         compiled_break = 0;
154 }
155
156 int kgdb_arch_handle_exception(int exception_vector, int signo,
157                                int err_code, char *remcom_in_buffer,
158                                char *remcom_out_buffer,
159                                struct pt_regs *linux_regs)
160 {
161         int err;
162
163         switch (remcom_in_buffer[0]) {
164         case 'D':
165         case 'k':
166                 /*
167                  * Packet D (Detach), k (kill). No special handling
168                  * is required here. Handle same as c packet.
169                  */
170         case 'c':
171                 /*
172                  * Packet c (Continue) to continue executing.
173                  * Set pc to required address.
174                  * Try to read optional parameter and set pc.
175                  * If this was a compiled breakpoint, we need to move
176                  * to the next instruction else we will just breakpoint
177                  * over and over again.
178                  */
179                 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
180                 atomic_set(&kgdb_cpu_doing_single_step, -1);
181                 kgdb_single_step =  0;
182
183                 /*
184                  * Received continue command, disable single step
185                  */
186                 if (kernel_active_single_step())
187                         kernel_disable_single_step();
188
189                 err = 0;
190                 break;
191         case 's':
192                 /*
193                  * Update step address value with address passed
194                  * with step packet.
195                  * On debug exception return PC is copied to ELR
196                  * So just update PC.
197                  * If no step address is passed, resume from the address
198                  * pointed by PC. Do not update PC
199                  */
200                 kgdb_arch_update_addr(linux_regs, remcom_in_buffer);
201                 atomic_set(&kgdb_cpu_doing_single_step, raw_smp_processor_id());
202                 kgdb_single_step =  1;
203
204                 /*
205                  * Enable single step handling
206                  */
207                 if (!kernel_active_single_step())
208                         kernel_enable_single_step(linux_regs);
209                 err = 0;
210                 break;
211         default:
212                 err = -1;
213         }
214         return err;
215 }
216
217 static int kgdb_brk_fn(struct pt_regs *regs, unsigned int esr)
218 {
219         kgdb_handle_exception(1, SIGTRAP, 0, regs);
220         return 0;
221 }
222 NOKPROBE_SYMBOL(kgdb_brk_fn)
223
224 static int kgdb_compiled_brk_fn(struct pt_regs *regs, unsigned int esr)
225 {
226         compiled_break = 1;
227         kgdb_handle_exception(1, SIGTRAP, 0, regs);
228
229         return 0;
230 }
231 NOKPROBE_SYMBOL(kgdb_compiled_brk_fn);
232
233 static int kgdb_step_brk_fn(struct pt_regs *regs, unsigned int esr)
234 {
235         kgdb_handle_exception(1, SIGTRAP, 0, regs);
236         return 0;
237 }
238 NOKPROBE_SYMBOL(kgdb_step_brk_fn);
239
240 static struct break_hook kgdb_brkpt_hook = {
241         .esr_mask       = 0xffffffff,
242         .esr_val        = (u32)ESR_ELx_VAL_BRK64(KGDB_DYN_DBG_BRK_IMM),
243         .fn             = kgdb_brk_fn
244 };
245
246 static struct break_hook kgdb_compiled_brkpt_hook = {
247         .esr_mask       = 0xffffffff,
248         .esr_val        = (u32)ESR_ELx_VAL_BRK64(KGDB_COMPILED_DBG_BRK_IMM),
249         .fn             = kgdb_compiled_brk_fn
250 };
251
252 static struct step_hook kgdb_step_hook = {
253         .fn             = kgdb_step_brk_fn
254 };
255
256 static void kgdb_call_nmi_hook(void *ignored)
257 {
258         kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
259 }
260
261 void kgdb_roundup_cpus(unsigned long flags)
262 {
263         local_irq_enable();
264         smp_call_function(kgdb_call_nmi_hook, NULL, 0);
265         local_irq_disable();
266 }
267
268 static int __kgdb_notify(struct die_args *args, unsigned long cmd)
269 {
270         struct pt_regs *regs = args->regs;
271
272         if (kgdb_handle_exception(1, args->signr, cmd, regs))
273                 return NOTIFY_DONE;
274         return NOTIFY_STOP;
275 }
276
277 static int
278 kgdb_notify(struct notifier_block *self, unsigned long cmd, void *ptr)
279 {
280         unsigned long flags;
281         int ret;
282
283         local_irq_save(flags);
284         ret = __kgdb_notify(ptr, cmd);
285         local_irq_restore(flags);
286
287         return ret;
288 }
289
290 static struct notifier_block kgdb_notifier = {
291         .notifier_call  = kgdb_notify,
292         /*
293          * Want to be lowest priority
294          */
295         .priority       = -INT_MAX,
296 };
297
298 /*
299  * kgdb_arch_init - Perform any architecture specific initalization.
300  * This function will handle the initalization of any architecture
301  * specific callbacks.
302  */
303 int kgdb_arch_init(void)
304 {
305         int ret = register_die_notifier(&kgdb_notifier);
306
307         if (ret != 0)
308                 return ret;
309
310         register_break_hook(&kgdb_brkpt_hook);
311         register_break_hook(&kgdb_compiled_brkpt_hook);
312         register_step_hook(&kgdb_step_hook);
313         return 0;
314 }
315
316 /*
317  * kgdb_arch_exit - Perform any architecture specific uninitalization.
318  * This function will handle the uninitalization of any architecture
319  * specific callbacks, for dynamic registration and unregistration.
320  */
321 void kgdb_arch_exit(void)
322 {
323         unregister_break_hook(&kgdb_brkpt_hook);
324         unregister_break_hook(&kgdb_compiled_brkpt_hook);
325         unregister_step_hook(&kgdb_step_hook);
326         unregister_die_notifier(&kgdb_notifier);
327 }
328
329 /*
330  * ARM instructions are always in LE.
331  * Break instruction is encoded in LE format
332  */
333 struct kgdb_arch arch_kgdb_ops = {
334         .gdb_bpt_instr = {
335                 KGDB_DYN_BRK_INS_BYTE(0),
336                 KGDB_DYN_BRK_INS_BYTE(1),
337                 KGDB_DYN_BRK_INS_BYTE(2),
338                 KGDB_DYN_BRK_INS_BYTE(3),
339         }
340 };