2 * KGDB NMI serial console
4 * Copyright 2010 Google, Inc.
5 * Arve Hjønnevåg <arve@android.com>
6 * Colin Cross <ccross@android.com>
7 * Copyright 2012 Linaro Ltd.
8 * Anton Vorontsov <anton.vorontsov@linaro.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/compiler.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
21 #include <linux/atomic.h>
22 #include <linux/console.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/interrupt.h>
27 #include <linux/hrtimer.h>
28 #include <linux/tick.h>
29 #include <linux/kfifo.h>
30 #include <linux/kgdb.h>
31 #include <linux/kdb.h>
33 static int kgdb_nmi_knock = 1;
34 module_param_named(knock, kgdb_nmi_knock, int, 0600);
35 MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
36 "must be used to enter the debugger; when set to 0, " \
37 "hitting return key is enough to enter the debugger; " \
38 "when set to -1, the debugger is entered immediately " \
41 static char *kgdb_nmi_magic = "$3#33";
42 module_param_named(magic, kgdb_nmi_magic, charp, 0600);
43 MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
45 static bool kgdb_nmi_tty_enabled;
47 static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
51 if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
54 for (i = 0; i < c; i++)
55 dbg_io_ops->write_char(s[i]);
58 static struct tty_driver *kgdb_nmi_tty_driver;
60 static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
63 return kgdb_nmi_tty_driver;
66 static struct console kgdb_nmi_console = {
68 .write = kgdb_nmi_console_write,
69 .device = kgdb_nmi_console_device,
70 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
75 * This is usually the maximum rate on debug ports. We make fifo large enough
76 * to make copy-pasting to the terminal usable.
78 #define KGDB_NMI_BAUD 115200
79 #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
81 struct kgdb_nmi_tty_priv {
83 struct tasklet_struct tlet;
84 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
87 static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
89 return container_of(port, struct kgdb_nmi_tty_priv, port);
93 * Our debugging console is polled in a tasklet, so we'll check for input
94 * every tick. In HZ-less mode, we should program the next tick. We have
95 * to use the lowlevel stuff as no locks should be grabbed.
97 #ifdef CONFIG_HIGH_RES_TIMERS
98 static void kgdb_tty_poke(void)
100 tick_program_event(ktime_get(), 0);
103 static inline void kgdb_tty_poke(void) {}
106 static struct tty_port *kgdb_nmi_port;
108 static void kgdb_tty_recv(int ch)
110 struct kgdb_nmi_tty_priv *priv;
113 if (!kgdb_nmi_port || ch < 0)
116 * Can't use port->tty->driver_data as tty might be not there. Tasklet
117 * will check for tty and will get the ref, but here we don't have to
118 * do that, and actually, we can't: we're in NMI context, no locks are
121 priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
122 kfifo_in(&priv->fifo, &c, 1);
126 static int kgdb_nmi_poll_one_knock(void)
130 const char *magic = kgdb_nmi_magic;
131 size_t m = strlen(magic);
134 c = dbg_io_ops->read_char();
135 if (c == NO_POLL_CHAR)
138 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
140 } else if (c == magic[n]) {
149 if (kgdb_nmi_tty_enabled) {
159 kdb_printf("\r%s %s to enter the debugger> %*s",
160 kgdb_nmi_knock ? "Type" : "Hit",
161 kgdb_nmi_knock ? magic : "<return>", (int)m, "");
168 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
170 * "Serial ports are often noisy, especially when muxed over another port (we
171 * often use serial over the headset connector). Noise on the async command
172 * line just causes characters that are ignored, on a command line that blocked
173 * execution noise would be catastrophic." -- Colin Cross
175 * So, this function implements KGDB/KDB knocking on the serial line: we won't
176 * enter the debugger until we receive a known magic phrase (which is actually
177 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
178 * of knocking, i.e. just pressing the return key is enough to enter the
179 * debugger. And if knocking is disabled, the function always returns 1.
181 bool kgdb_nmi_poll_knock(void)
183 if (kgdb_nmi_knock < 0)
189 ret = kgdb_nmi_poll_one_knock();
190 if (ret == NO_POLL_CHAR)
199 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
200 * instead it waits for the next tick.
202 static void kgdb_nmi_tty_receiver(unsigned long data)
204 struct kgdb_nmi_tty_priv *priv = (void *)data;
205 struct tty_struct *tty;
208 tasklet_schedule(&priv->tlet);
210 if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
213 /* Port is there, but tty might be hung up, check. */
214 tty = tty_port_tty_get(kgdb_nmi_port);
218 while (kfifo_out(&priv->fifo, &ch, 1))
219 tty_insert_flip_char(priv->port.tty, ch, TTY_NORMAL);
220 tty_flip_buffer_push(priv->port.tty);
225 static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
227 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
229 kgdb_nmi_port = port;
230 tasklet_schedule(&priv->tlet);
234 static void kgdb_nmi_tty_shutdown(struct tty_port *port)
236 struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
238 tasklet_kill(&priv->tlet);
239 kgdb_nmi_port = NULL;
242 static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
243 .activate = kgdb_nmi_tty_activate,
244 .shutdown = kgdb_nmi_tty_shutdown,
247 static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
249 struct kgdb_nmi_tty_priv *priv;
252 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
256 INIT_KFIFO(priv->fifo);
257 tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
258 tty_port_init(&priv->port);
259 priv->port.ops = &kgdb_nmi_tty_port_ops;
260 tty->driver_data = priv;
262 ret = tty_port_install(&priv->port, drv, tty);
264 pr_err("%s: can't install tty port: %d\n", __func__, ret);
269 tty_port_destroy(&priv->port);
274 static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
276 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
278 tty->driver_data = NULL;
279 tty_port_destroy(&priv->port);
283 static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
285 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
287 return tty_port_open(&priv->port, tty, file);
290 static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
292 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
294 tty_port_close(&priv->port, tty, file);
297 static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
299 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
301 tty_port_hangup(&priv->port);
304 static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
306 /* Actually, we can handle any amount as we use polled writes. */
310 static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
314 for (i = 0; i < c; i++)
315 dbg_io_ops->write_char(buf[i]);
319 static const struct tty_operations kgdb_nmi_tty_ops = {
320 .open = kgdb_nmi_tty_open,
321 .close = kgdb_nmi_tty_close,
322 .install = kgdb_nmi_tty_install,
323 .cleanup = kgdb_nmi_tty_cleanup,
324 .hangup = kgdb_nmi_tty_hangup,
325 .write_room = kgdb_nmi_tty_write_room,
326 .write = kgdb_nmi_tty_write,
329 static int kgdb_nmi_enable_console(int argc, const char *argv[])
331 kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
335 int kgdb_register_nmi_console(void)
339 if (!arch_kgdb_ops.enable_nmi)
342 kgdb_nmi_tty_driver = alloc_tty_driver(1);
343 if (!kgdb_nmi_tty_driver) {
344 pr_err("%s: cannot allocate tty\n", __func__);
347 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
348 kgdb_nmi_tty_driver->name = "ttyNMI";
349 kgdb_nmi_tty_driver->num = 1;
350 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
351 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
352 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
353 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
354 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
355 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
356 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
358 ret = tty_register_driver(kgdb_nmi_tty_driver);
360 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
364 ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
365 "switch to Linux NMI console", 0);
367 pr_err("%s: can't register kdb command: %d\n", __func__, ret);
371 register_console(&kgdb_nmi_console);
372 arch_kgdb_ops.enable_nmi(1);
376 tty_unregister_driver(kgdb_nmi_tty_driver);
378 put_tty_driver(kgdb_nmi_tty_driver);
381 EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
383 int kgdb_unregister_nmi_console(void)
387 if (!arch_kgdb_ops.enable_nmi)
389 arch_kgdb_ops.enable_nmi(0);
391 kdb_unregister("nmi_console");
393 ret = unregister_console(&kgdb_nmi_console);
397 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
400 put_tty_driver(kgdb_nmi_tty_driver);
404 EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);