2 * KGDB NMI serial console
4 * Copyright 2010 Google, Inc.
7 * Copyright 2012 Linaro Ltd.
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/slab.h>
19 #include <linux/errno.h>
20 #include <linux/atomic.h>
21 #include <linux/console.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/serial_core.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 int kgdb_nmi_console_setup(struct console *co, char *options)
49 /* The NMI console uses the dbg_io_ops to issue console messages. To
50 * avoid duplicate messages during kdb sessions we must inform kdb's
51 * I/O utilities that messages sent to the console will automatically
52 * be displayed on the dbg_io.
54 dbg_io_ops->is_console = true;
59 static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
63 for (i = 0; i < c; i++)
64 dbg_io_ops->write_char(s[i]);
67 static struct tty_driver *kgdb_nmi_tty_driver;
69 static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
72 return kgdb_nmi_tty_driver;
75 static struct console kgdb_nmi_console = {
77 .setup = kgdb_nmi_console_setup,
78 .write = kgdb_nmi_console_write,
79 .device = kgdb_nmi_console_device,
80 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
85 * This is usually the maximum rate on debug ports. We make fifo large enough
86 * to make copy-pasting to the terminal usable.
88 #define KGDB_NMI_BAUD 115200
89 #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
91 struct kgdb_nmi_tty_priv {
93 struct timer_list timer;
94 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
97 static struct tty_port *kgdb_nmi_port;
99 static void kgdb_tty_recv(int ch)
101 struct kgdb_nmi_tty_priv *priv;
104 if (!kgdb_nmi_port || ch < 0)
107 * Can't use port->tty->driver_data as tty might be not there. Timer
108 * will check for tty and will get the ref, but here we don't have to
109 * do that, and actually, we can't: we're in NMI context, no locks are
112 priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
113 kfifo_in(&priv->fifo, &c, 1);
116 static int kgdb_nmi_poll_one_knock(void)
120 const char *magic = kgdb_nmi_magic;
121 size_t m = strlen(magic);
124 c = dbg_io_ops->read_char();
125 if (c == NO_POLL_CHAR)
128 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
130 } else if (c == magic[n]) {
139 if (kgdb_nmi_tty_enabled) {
149 kdb_printf("\r%s %s to enter the debugger> %*s",
150 kgdb_nmi_knock ? "Type" : "Hit",
151 kgdb_nmi_knock ? magic : "<return>", (int)m, "");
158 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
160 * "Serial ports are often noisy, especially when muxed over another port (we
161 * often use serial over the headset connector). Noise on the async command
162 * line just causes characters that are ignored, on a command line that blocked
163 * execution noise would be catastrophic." -- Colin Cross
165 * So, this function implements KGDB/KDB knocking on the serial line: we won't
166 * enter the debugger until we receive a known magic phrase (which is actually
167 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
168 * of knocking, i.e. just pressing the return key is enough to enter the
169 * debugger. And if knocking is disabled, the function always returns 1.
171 bool kgdb_nmi_poll_knock(void)
173 if (kgdb_nmi_knock < 0)
179 ret = kgdb_nmi_poll_one_knock();
180 if (ret == NO_POLL_CHAR)
189 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
190 * instead it waits for the next tick.
192 static void kgdb_nmi_tty_receiver(unsigned long data)
194 struct kgdb_nmi_tty_priv *priv = (void *)data;
197 priv->timer.expires = jiffies + (HZ/100);
198 add_timer(&priv->timer);
200 if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
203 while (kfifo_out(&priv->fifo, &ch, 1))
204 tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
205 tty_flip_buffer_push(&priv->port);
208 static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
210 struct kgdb_nmi_tty_priv *priv =
211 container_of(port, struct kgdb_nmi_tty_priv, port);
213 kgdb_nmi_port = port;
214 priv->timer.expires = jiffies + (HZ/100);
215 add_timer(&priv->timer);
220 static void kgdb_nmi_tty_shutdown(struct tty_port *port)
222 struct kgdb_nmi_tty_priv *priv =
223 container_of(port, struct kgdb_nmi_tty_priv, port);
225 del_timer(&priv->timer);
226 kgdb_nmi_port = NULL;
229 static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
230 .activate = kgdb_nmi_tty_activate,
231 .shutdown = kgdb_nmi_tty_shutdown,
234 static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
236 struct kgdb_nmi_tty_priv *priv;
239 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
243 INIT_KFIFO(priv->fifo);
244 setup_timer(&priv->timer, kgdb_nmi_tty_receiver, (unsigned long)priv);
245 tty_port_init(&priv->port);
246 priv->port.ops = &kgdb_nmi_tty_port_ops;
247 tty->driver_data = priv;
249 ret = tty_port_install(&priv->port, drv, tty);
251 pr_err("%s: can't install tty port: %d\n", __func__, ret);
256 tty_port_destroy(&priv->port);
261 static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
263 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
265 tty->driver_data = NULL;
266 tty_port_destroy(&priv->port);
270 static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
272 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
274 return tty_port_open(&priv->port, tty, file);
277 static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
279 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
281 tty_port_close(&priv->port, tty, file);
284 static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
286 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
288 tty_port_hangup(&priv->port);
291 static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
293 /* Actually, we can handle any amount as we use polled writes. */
297 static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
301 for (i = 0; i < c; i++)
302 dbg_io_ops->write_char(buf[i]);
306 static const struct tty_operations kgdb_nmi_tty_ops = {
307 .open = kgdb_nmi_tty_open,
308 .close = kgdb_nmi_tty_close,
309 .install = kgdb_nmi_tty_install,
310 .cleanup = kgdb_nmi_tty_cleanup,
311 .hangup = kgdb_nmi_tty_hangup,
312 .write_room = kgdb_nmi_tty_write_room,
313 .write = kgdb_nmi_tty_write,
316 static int kgdb_nmi_enable_console(int argc, const char *argv[])
318 kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
322 int kgdb_register_nmi_console(void)
326 if (!arch_kgdb_ops.enable_nmi)
329 kgdb_nmi_tty_driver = alloc_tty_driver(1);
330 if (!kgdb_nmi_tty_driver) {
331 pr_err("%s: cannot allocate tty\n", __func__);
334 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
335 kgdb_nmi_tty_driver->name = "ttyNMI";
336 kgdb_nmi_tty_driver->num = 1;
337 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
338 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
339 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
340 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
341 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
342 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
343 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
345 ret = tty_register_driver(kgdb_nmi_tty_driver);
347 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
351 ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
352 "switch to Linux NMI console", 0);
354 pr_err("%s: can't register kdb command: %d\n", __func__, ret);
358 register_console(&kgdb_nmi_console);
359 arch_kgdb_ops.enable_nmi(1);
363 tty_unregister_driver(kgdb_nmi_tty_driver);
365 put_tty_driver(kgdb_nmi_tty_driver);
368 EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
370 int kgdb_unregister_nmi_console(void)
374 if (!arch_kgdb_ops.enable_nmi)
376 arch_kgdb_ops.enable_nmi(0);
378 kdb_unregister("nmi_console");
380 ret = unregister_console(&kgdb_nmi_console);
384 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
387 put_tty_driver(kgdb_nmi_tty_driver);
391 EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);