blob: c4679a16f340f572c2ffe708d1320f076c922271 [file] [log] [blame]
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001/*
2 * arch/arm/common/fiq_debugger.c
3 *
4 * Serial Debugger Interface accessed through an FIQ interrupt.
5 *
6 * Copyright (C) 2008 Google, Inc.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <stdarg.h>
19#include <linux/module.h>
20#include <linux/io.h>
21#include <linux/interrupt.h>
22#include <linux/clk.h>
23#include <linux/platform_device.h>
24#include <linux/kernel_debugger.h>
25#include <linux/kernel_stat.h>
26#include <linux/irq.h>
27#include <linux/delay.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/timer.h>
31#include <linux/wakelock.h>
32
33#include <asm/fiq_debugger.h>
34#include <asm/fiq_glue.h>
35#include <asm/stacktrace.h>
36
37#include <mach/system.h>
38
39#include <linux/uaccess.h>
40
41#define DEBUG_MAX 64
42
43struct fiq_debugger_state {
44 struct fiq_glue_handler handler;
45
46 int fiq;
47 int signal_irq;
48 int wakeup_irq;
49 bool wakeup_irq_no_set_wake;
50 struct clk *clk;
51 struct fiq_debugger_pdata *pdata;
52 struct platform_device *pdev;
53
54 char debug_cmd[DEBUG_MAX];
55 int debug_busy;
56 int debug_abort;
57
58 char debug_buf[DEBUG_MAX];
59 int debug_count;
60
61 bool no_sleep;
62 bool debug_enable;
63 bool ignore_next_wakeup_irq;
64 struct timer_list sleep_timer;
65 bool uart_clk_enabled;
66 struct wake_lock debugger_wake_lock;
67
68 unsigned int last_irqs[NR_IRQS];
69};
70
71#ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
72static bool initial_no_sleep = true;
73#else
74static bool initial_no_sleep;
75#endif
76static bool initial_debug_enable;
77
78module_param_named(no_sleep, initial_no_sleep, bool, 0644);
79module_param_named(debug_enable, initial_debug_enable, bool, 0644);
80
81#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
82static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
83static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
84#else
85static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
86{
87 if (state->wakeup_irq < 0)
88 return;
89 enable_irq(state->wakeup_irq);
90 if (!state->wakeup_irq_no_set_wake)
91 enable_irq_wake(state->wakeup_irq);
92}
93static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
94{
95 if (state->wakeup_irq < 0)
96 return;
97 disable_irq_nosync(state->wakeup_irq);
98 if (!state->wakeup_irq_no_set_wake)
99 disable_irq_wake(state->wakeup_irq);
100}
101#endif
102
103static void debug_force_irq(struct fiq_debugger_state *state)
104{
105 unsigned int irq = state->signal_irq;
106 if (state->pdata->force_irq)
107 state->pdata->force_irq(state->pdev, irq);
108 else {
109 struct irq_chip *chip = get_irq_chip(irq);
110 if (chip && chip->retrigger)
111 chip->retrigger(irq);
112 }
113}
114
115static void debug_uart_flush(struct fiq_debugger_state *state)
116{
117 if (state->pdata->uart_flush)
118 state->pdata->uart_flush(state->pdev);
119}
120
121static void debug_puts(struct fiq_debugger_state *state, char *s)
122{
123 unsigned c;
124 while ((c = *s++)) {
125 if (c == '\n')
126 state->pdata->uart_putc(state->pdev, '\r');
127 state->pdata->uart_putc(state->pdev, c);
128 }
129}
130
131static void debug_prompt(struct fiq_debugger_state *state)
132{
133 debug_puts(state, "debug> ");
134}
135
136int log_buf_copy(char *dest, int idx, int len);
137static void dump_kernel_log(struct fiq_debugger_state *state)
138{
139 char buf[1024];
140 int idx = 0;
141 int ret;
142 int saved_oip;
143
144 /* setting oops_in_progress prevents log_buf_copy()
145 * from trying to take a spinlock which will make it
146 * very unhappy in some cases...
147 */
148 saved_oip = oops_in_progress;
149 oops_in_progress = 1;
150 for (;;) {
151 ret = log_buf_copy(buf, idx, 1023);
152 if (ret <= 0)
153 break;
154 buf[ret] = 0;
155 debug_puts(state, buf);
156 idx += ret;
157 }
158 oops_in_progress = saved_oip;
159}
160
161static char *mode_name(unsigned cpsr)
162{
163 switch (cpsr & MODE_MASK) {
164 case USR_MODE: return "USR";
165 case FIQ_MODE: return "FIQ";
166 case IRQ_MODE: return "IRQ";
167 case SVC_MODE: return "SVC";
168 case ABT_MODE: return "ABT";
169 case UND_MODE: return "UND";
170 case SYSTEM_MODE: return "SYS";
171 default: return "???";
172 }
173}
174
175static int debug_printf(void *cookie, const char *fmt, ...)
176{
177 struct fiq_debugger_state *state = cookie;
178 char buf[256];
179 va_list ap;
180
181 va_start(ap, fmt);
182 vsnprintf(buf, sizeof(buf), fmt, ap);
183 va_end(ap);
184
185 debug_puts(state, buf);
186 return state->debug_abort;
187}
188
189/* Safe outside fiq context */
190static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
191{
192 struct fiq_debugger_state *state = cookie;
193 char buf[256];
194 va_list ap;
195 unsigned long irq_flags;
196
197 va_start(ap, fmt);
198 vsnprintf(buf, 128, fmt, ap);
199 va_end(ap);
200
201 local_irq_save(irq_flags);
202 debug_puts(state, buf);
203 debug_uart_flush(state);
204 local_irq_restore(irq_flags);
205 return state->debug_abort;
206}
207
208static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
209{
210 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
211 regs[0], regs[1], regs[2], regs[3]);
212 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
213 regs[4], regs[5], regs[6], regs[7]);
214 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
215 regs[8], regs[9], regs[10], regs[11],
216 mode_name(regs[16]));
217 if ((regs[16] & MODE_MASK) == USR_MODE)
218 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
219 "cpsr %08x\n", regs[12], regs[13], regs[14],
220 regs[15], regs[16]);
221 else
222 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
223 "cpsr %08x spsr %08x\n", regs[12], regs[13],
224 regs[14], regs[15], regs[16], regs[17]);
225}
226
227struct mode_regs {
228 unsigned long sp_svc;
229 unsigned long lr_svc;
230 unsigned long spsr_svc;
231
232 unsigned long sp_abt;
233 unsigned long lr_abt;
234 unsigned long spsr_abt;
235
236 unsigned long sp_und;
237 unsigned long lr_und;
238 unsigned long spsr_und;
239
240 unsigned long sp_irq;
241 unsigned long lr_irq;
242 unsigned long spsr_irq;
243
244 unsigned long r8_fiq;
245 unsigned long r9_fiq;
246 unsigned long r10_fiq;
247 unsigned long r11_fiq;
248 unsigned long r12_fiq;
249 unsigned long sp_fiq;
250 unsigned long lr_fiq;
251 unsigned long spsr_fiq;
252};
253
254void __naked get_mode_regs(struct mode_regs *regs)
255{
256 asm volatile (
257 "mrs r1, cpsr\n"
258 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
259 "stmia r0!, {r13 - r14}\n"
260 "mrs r2, spsr\n"
261 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
262 "stmia r0!, {r2, r13 - r14}\n"
263 "mrs r2, spsr\n"
264 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
265 "stmia r0!, {r2, r13 - r14}\n"
266 "mrs r2, spsr\n"
267 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
268 "stmia r0!, {r2, r13 - r14}\n"
269 "mrs r2, spsr\n"
270 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
271 "stmia r0!, {r2, r8 - r14}\n"
272 "mrs r2, spsr\n"
273 "stmia r0!, {r2}\n"
274 "msr cpsr_c, r1\n"
275 "bx lr\n");
276}
277
278
279static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
280{
281 struct mode_regs mode_regs;
282 dump_regs(state, regs);
283 get_mode_regs(&mode_regs);
284 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
285 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
286 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
287 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
288 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
289 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
290 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
291 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
292 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
293 "r12 %08x\n",
294 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
295 mode_regs.r11_fiq, mode_regs.r12_fiq);
296 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
297 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
298}
299
300static void dump_irqs(struct fiq_debugger_state *state)
301{
302 int n;
303 debug_printf(state, "irqnr total since-last status name\n");
304 for (n = 0; n < NR_IRQS; n++) {
305 struct irqaction *act = irq_desc[n].action;
306 if (!act && !kstat_irqs(n))
307 continue;
308 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
309 kstat_irqs(n),
310 kstat_irqs(n) - state->last_irqs[n],
311 irq_desc[n].status,
312 (act && act->name) ? act->name : "???");
313 state->last_irqs[n] = kstat_irqs(n);
314 }
315}
316
317struct stacktrace_state {
318 struct fiq_debugger_state *state;
319 unsigned int depth;
320};
321
322static int report_trace(struct stackframe *frame, void *d)
323{
324 struct stacktrace_state *sts = d;
325
326 if (sts->depth) {
327 debug_printf(sts->state,
328 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
329 frame->pc, frame->pc, frame->lr, frame->lr,
330 frame->sp, frame->fp);
331 sts->depth--;
332 return 0;
333 }
334 debug_printf(sts->state, " ...\n");
335
336 return sts->depth == 0;
337}
338
339struct frame_tail {
340 struct frame_tail *fp;
341 unsigned long sp;
342 unsigned long lr;
343} __attribute__((packed));
344
345static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
346 struct frame_tail *tail)
347{
348 struct frame_tail buftail[2];
349
350 /* Also check accessibility of one struct frame_tail beyond */
351 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
352 debug_printf(state, " invalid frame pointer %p\n", tail);
353 return NULL;
354 }
355 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
356 debug_printf(state,
357 " failed to copy frame pointer %p\n", tail);
358 return NULL;
359 }
360
361 debug_printf(state, " %p\n", buftail[0].lr);
362
363 /* frame pointers should strictly progress back up the stack
364 * (towards higher addresses) */
365 if (tail >= buftail[0].fp)
366 return NULL;
367
368 return buftail[0].fp-1;
369}
370
371void dump_stacktrace(struct fiq_debugger_state *state,
372 struct pt_regs * const regs, unsigned int depth, void *ssp)
373{
374 struct frame_tail *tail;
375 struct thread_info *real_thread_info = (struct thread_info *)
376 ((unsigned long)ssp & ~(THREAD_SIZE - 1));
377 struct stacktrace_state sts;
378
379 sts.depth = depth;
380 sts.state = state;
381 *current_thread_info() = *real_thread_info;
382
383 if (!current)
384 debug_printf(state, "current NULL\n");
385 else
386 debug_printf(state, "pid: %d comm: %s\n",
387 current->pid, current->comm);
388 dump_regs(state, (unsigned *)regs);
389
390 if (!user_mode(regs)) {
391 struct stackframe frame;
392 frame.fp = regs->ARM_fp;
393 frame.sp = regs->ARM_sp;
394 frame.lr = regs->ARM_lr;
395 frame.pc = regs->ARM_pc;
396 debug_printf(state,
397 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
398 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
399 regs->ARM_sp, regs->ARM_fp);
400 walk_stackframe(&frame, report_trace, &sts);
401 return;
402 }
403
404 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
405 while (depth-- && tail && !((unsigned long) tail & 3))
406 tail = user_backtrace(state, tail);
407}
408
409static void debug_exec(struct fiq_debugger_state *state,
410 const char *cmd, unsigned *regs, void *svc_sp)
411{
412 if (!strcmp(cmd, "pc")) {
413 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
414 regs[15], regs[16], mode_name(regs[16]));
415 } else if (!strcmp(cmd, "regs")) {
416 dump_regs(state, regs);
417 } else if (!strcmp(cmd, "allregs")) {
418 dump_allregs(state, regs);
419 } else if (!strcmp(cmd, "bt")) {
420 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
421 } else if (!strcmp(cmd, "reboot")) {
422 arch_reset(0, 0);
423 } else if (!strcmp(cmd, "irqs")) {
424 dump_irqs(state);
425 } else if (!strcmp(cmd, "kmsg")) {
426 dump_kernel_log(state);
427 } else if (!strcmp(cmd, "version")) {
428 debug_printf(state, "%s\n", linux_banner);
429 } else if (!strcmp(cmd, "sleep")) {
430 state->no_sleep = false;
431 } else if (!strcmp(cmd, "nosleep")) {
432 state->no_sleep = true;
433 } else {
434 if (state->debug_busy) {
435 debug_printf(state,
436 "command processor busy. trying to abort.\n");
437 state->debug_abort = -1;
438 } else {
439 strcpy(state->debug_cmd, cmd);
440 state->debug_busy = 1;
441 }
442
443 debug_force_irq(state);
444
445 return;
446 }
447 debug_prompt(state);
448}
449
450static void sleep_timer_expired(unsigned long data)
451{
452 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
453
454 if (state->uart_clk_enabled && !state->no_sleep) {
455 if (state->debug_enable) {
456 state->debug_enable = false;
457 debug_printf_nfiq(state, "suspending fiq debugger\n");
458 }
459 state->ignore_next_wakeup_irq = true;
460 if (state->clk)
461 clk_disable(state->clk);
462 state->uart_clk_enabled = false;
463 enable_wakeup_irq(state);
464 }
465 wake_unlock(&state->debugger_wake_lock);
466}
467
468static irqreturn_t wakeup_irq_handler(int irq, void *dev)
469{
470 struct fiq_debugger_state *state = dev;
471
472 if (!state->no_sleep)
473 debug_puts(state, "WAKEUP\n");
474 if (state->ignore_next_wakeup_irq)
475 state->ignore_next_wakeup_irq = false;
476 else if (!state->uart_clk_enabled) {
477 wake_lock(&state->debugger_wake_lock);
478 if (state->clk)
479 clk_enable(state->clk);
480 state->uart_clk_enabled = true;
481 disable_wakeup_irq(state);
482 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
483 }
484 return IRQ_HANDLED;
485}
486
487static irqreturn_t debug_irq(int irq, void *dev)
488{
489 struct fiq_debugger_state *state = dev;
490 if (state->pdata->force_irq_ack)
491 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
492
493 if (!state->no_sleep) {
494 wake_lock(&state->debugger_wake_lock);
495 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
496 }
497 if (state->debug_busy) {
498 struct kdbg_ctxt ctxt;
499
500 ctxt.printf = debug_printf_nfiq;
501 ctxt.cookie = state;
502 kernel_debugger(&ctxt, state->debug_cmd);
503 debug_prompt(state);
504
505 state->debug_busy = 0;
506 }
507 return IRQ_HANDLED;
508}
509
510static int debug_getc(struct fiq_debugger_state *state)
511{
512 return state->pdata->uart_getc(state->pdev);
513}
514
515static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
516{
517 struct fiq_debugger_state *state =
518 container_of(h, struct fiq_debugger_state, handler);
519 int c;
520 static int last_c;
521 int count = 0;
522
523 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
524 count++;
525 if (!state->debug_enable) {
526 if ((c == 13) || (c == 10)) {
527 state->debug_enable = true;
528 state->debug_count = 0;
529 debug_prompt(state);
530 }
531 } else if ((c >= ' ') && (c < 127)) {
532 if (state->debug_count < (DEBUG_MAX - 1)) {
533 state->debug_buf[state->debug_count++] = c;
534 state->pdata->uart_putc(state->pdev, c);
535 }
536 } else if ((c == 8) || (c == 127)) {
537 if (state->debug_count > 0) {
538 state->debug_count--;
539 state->pdata->uart_putc(state->pdev, 8);
540 state->pdata->uart_putc(state->pdev, ' ');
541 state->pdata->uart_putc(state->pdev, 8);
542 }
543 } else if ((c == 13) || (c == 10)) {
544 if (c == '\r' || (c == '\n' && last_c != '\r')) {
545 state->pdata->uart_putc(state->pdev, '\r');
546 state->pdata->uart_putc(state->pdev, '\n');
547 }
548 if (state->debug_count) {
549 state->debug_buf[state->debug_count] = 0;
550 state->debug_count = 0;
551 debug_exec(state, state->debug_buf,
552 regs, svc_sp);
553 } else {
554 debug_prompt(state);
555 }
556 }
557 last_c = c;
558 }
559 debug_uart_flush(state);
560 if (state->pdata->fiq_ack)
561 state->pdata->fiq_ack(state->pdev, state->fiq);
562
563 /* poke sleep timer if necessary */
564 if (state->debug_enable && !state->no_sleep)
565 debug_force_irq(state);
566}
567
568static void debug_resume(struct fiq_glue_handler *h)
569{
570 struct fiq_debugger_state *state =
571 container_of(h, struct fiq_debugger_state, handler);
572 if (state->pdata->uart_resume)
573 state->pdata->uart_resume(state->pdev);
574}
575
576static int fiq_debugger_probe(struct platform_device *pdev)
577{
578 int ret;
579 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
580 struct fiq_debugger_state *state;
581
582 if (!pdata->uart_getc || !pdata->uart_putc || !pdata->fiq_enable)
583 return -EINVAL;
584
585 state = kzalloc(sizeof(*state), GFP_KERNEL);
586 state->handler.fiq = debug_fiq;
587 state->handler.resume = debug_resume;
588 setup_timer(&state->sleep_timer, sleep_timer_expired,
589 (unsigned long)state);
590 state->pdata = pdata;
591 state->pdev = pdev;
592 state->no_sleep = initial_no_sleep;
593 state->debug_enable = initial_debug_enable;
594
595 state->fiq = platform_get_irq_byname(pdev, "fiq");
596 state->signal_irq = platform_get_irq_byname(pdev, "signal");
597 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
598
599 if (state->wakeup_irq < 0)
600 state->no_sleep = true;
601 state->ignore_next_wakeup_irq = !state->no_sleep;
602
603 wake_lock_init(&state->debugger_wake_lock,
604 WAKE_LOCK_SUSPEND, "serial-debug");
605
606 state->clk = clk_get(&pdev->dev, NULL);
607 if (IS_ERR(state->clk))
608 state->clk = NULL;
609
610 if (state->clk)
611 clk_enable(state->clk);
612
613 if (pdata->uart_init) {
614 ret = pdata->uart_init(pdev);
615 if (ret)
616 goto err_uart_init;
617 }
618
619 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
620 state->no_sleep ? "" : "twice ");
621
622 ret = fiq_glue_register_handler(&state->handler);
623 if (ret) {
624 pr_err("serial_debugger: could not install fiq handler\n");
625 goto err_register_fiq;
626 }
627
628 pdata->fiq_enable(pdev, state->fiq, 1);
629
630 if (state->clk)
631 clk_disable(state->clk);
632
633 ret = request_irq(state->signal_irq, debug_irq,
634 IRQF_TRIGGER_RISING, "debug", state);
635 if (ret)
636 pr_err("serial_debugger: could not install signal_irq");
637
638 if (state->wakeup_irq >= 0) {
639 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
640 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
641 "debug-wakeup", state);
642 if (ret) {
643 pr_err("serial_debugger: "
644 "could not install wakeup irq\n");
645 state->wakeup_irq = -1;
646 } else {
647 ret = enable_irq_wake(state->wakeup_irq);
648 if (ret) {
649 pr_err("serial_debugger: "
650 "could not enable wakeup\n");
651 state->wakeup_irq_no_set_wake = true;
652 }
653 }
654 }
655 if (state->no_sleep)
656 wakeup_irq_handler(state->wakeup_irq, state);
657
658 return 0;
659
660err_register_fiq:
661 if (pdata->uart_free)
662 pdata->uart_free(pdev);
663err_uart_init:
664 kfree(state);
665 if (state->clk)
666 clk_put(state->clk);
667 return ret;
668}
669
670static struct platform_driver fiq_debugger_driver = {
671 .probe = fiq_debugger_probe,
672 .driver.name = "fiq_debugger",
673};
674
675static int __init fiq_debugger_init(void)
676{
677 return platform_driver_register(&fiq_debugger_driver);
678}
679
680postcore_initcall(fiq_debugger_init);