Linux 3.12.39
[linux/fpc-iii.git] / drivers / tty / serial / kgdb_nmi.c
blob5dafcf1c227ba88ed9cf5025e6c965203464dc4b
1 /*
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/serial_core.h>
27 #include <linux/interrupt.h>
28 #include <linux/hrtimer.h>
29 #include <linux/tick.h>
30 #include <linux/kfifo.h>
31 #include <linux/kgdb.h>
32 #include <linux/kdb.h>
34 static int kgdb_nmi_knock = 1;
35 module_param_named(knock, kgdb_nmi_knock, int, 0600);
36 MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
37 "must be used to enter the debugger; when set to 0, " \
38 "hitting return key is enough to enter the debugger; " \
39 "when set to -1, the debugger is entered immediately " \
40 "upon NMI");
42 static char *kgdb_nmi_magic = "$3#33";
43 module_param_named(magic, kgdb_nmi_magic, charp, 0600);
44 MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
46 static bool kgdb_nmi_tty_enabled;
48 static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
50 int i;
52 if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
53 return;
55 for (i = 0; i < c; i++)
56 dbg_io_ops->write_char(s[i]);
59 static struct tty_driver *kgdb_nmi_tty_driver;
61 static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
63 *idx = co->index;
64 return kgdb_nmi_tty_driver;
67 static struct console kgdb_nmi_console = {
68 .name = "ttyNMI",
69 .write = kgdb_nmi_console_write,
70 .device = kgdb_nmi_console_device,
71 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
72 .index = -1,
76 * This is usually the maximum rate on debug ports. We make fifo large enough
77 * to make copy-pasting to the terminal usable.
79 #define KGDB_NMI_BAUD 115200
80 #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
82 struct kgdb_nmi_tty_priv {
83 struct tty_port port;
84 struct tasklet_struct tlet;
85 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
88 static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
90 return container_of(port, struct kgdb_nmi_tty_priv, port);
94 * Our debugging console is polled in a tasklet, so we'll check for input
95 * every tick. In HZ-less mode, we should program the next tick. We have
96 * to use the lowlevel stuff as no locks should be grabbed.
98 #ifdef CONFIG_HIGH_RES_TIMERS
99 static void kgdb_tty_poke(void)
101 tick_program_event(ktime_get(), 0);
103 #else
104 static inline void kgdb_tty_poke(void) {}
105 #endif
107 static struct tty_port *kgdb_nmi_port;
109 static void kgdb_tty_recv(int ch)
111 struct kgdb_nmi_tty_priv *priv;
112 char c = ch;
114 if (!kgdb_nmi_port || ch < 0)
115 return;
117 * Can't use port->tty->driver_data as tty might be not there. Tasklet
118 * will check for tty and will get the ref, but here we don't have to
119 * do that, and actually, we can't: we're in NMI context, no locks are
120 * possible.
122 priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
123 kfifo_in(&priv->fifo, &c, 1);
124 kgdb_tty_poke();
127 static int kgdb_nmi_poll_one_knock(void)
129 static int n;
130 int c = -1;
131 const char *magic = kgdb_nmi_magic;
132 size_t m = strlen(magic);
133 bool printch = 0;
135 c = dbg_io_ops->read_char();
136 if (c == NO_POLL_CHAR)
137 return c;
139 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
140 return 1;
141 } else if (c == magic[n]) {
142 n = (n + 1) % m;
143 if (!n)
144 return 1;
145 printch = 1;
146 } else {
147 n = 0;
150 if (kgdb_nmi_tty_enabled) {
151 kgdb_tty_recv(c);
152 return 0;
155 if (printch) {
156 kdb_printf("%c", c);
157 return 0;
160 kdb_printf("\r%s %s to enter the debugger> %*s",
161 kgdb_nmi_knock ? "Type" : "Hit",
162 kgdb_nmi_knock ? magic : "<return>", (int)m, "");
163 while (m--)
164 kdb_printf("\b");
165 return 0;
169 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
171 * "Serial ports are often noisy, especially when muxed over another port (we
172 * often use serial over the headset connector). Noise on the async command
173 * line just causes characters that are ignored, on a command line that blocked
174 * execution noise would be catastrophic." -- Colin Cross
176 * So, this function implements KGDB/KDB knocking on the serial line: we won't
177 * enter the debugger until we receive a known magic phrase (which is actually
178 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
179 * of knocking, i.e. just pressing the return key is enough to enter the
180 * debugger. And if knocking is disabled, the function always returns 1.
182 bool kgdb_nmi_poll_knock(void)
184 if (kgdb_nmi_knock < 0)
185 return 1;
187 while (1) {
188 int ret;
190 ret = kgdb_nmi_poll_one_knock();
191 if (ret == NO_POLL_CHAR)
192 return 0;
193 else if (ret == 1)
194 break;
196 return 1;
200 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
201 * instead it waits for the next tick.
203 static void kgdb_nmi_tty_receiver(unsigned long data)
205 struct kgdb_nmi_tty_priv *priv = (void *)data;
206 char ch;
208 tasklet_schedule(&priv->tlet);
210 if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
211 return;
213 while (kfifo_out(&priv->fifo, &ch, 1))
214 tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
215 tty_flip_buffer_push(&priv->port);
218 static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
220 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
222 kgdb_nmi_port = port;
223 tasklet_schedule(&priv->tlet);
224 return 0;
227 static void kgdb_nmi_tty_shutdown(struct tty_port *port)
229 struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
231 tasklet_kill(&priv->tlet);
232 kgdb_nmi_port = NULL;
235 static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
236 .activate = kgdb_nmi_tty_activate,
237 .shutdown = kgdb_nmi_tty_shutdown,
240 static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
242 struct kgdb_nmi_tty_priv *priv;
243 int ret;
245 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
246 if (!priv)
247 return -ENOMEM;
249 INIT_KFIFO(priv->fifo);
250 tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
251 tty_port_init(&priv->port);
252 priv->port.ops = &kgdb_nmi_tty_port_ops;
253 tty->driver_data = priv;
255 ret = tty_port_install(&priv->port, drv, tty);
256 if (ret) {
257 pr_err("%s: can't install tty port: %d\n", __func__, ret);
258 goto err;
260 return 0;
261 err:
262 tty_port_destroy(&priv->port);
263 kfree(priv);
264 return ret;
267 static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
269 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
271 tty->driver_data = NULL;
272 tty_port_destroy(&priv->port);
273 kfree(priv);
276 static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
278 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
280 return tty_port_open(&priv->port, tty, file);
283 static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
285 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
287 tty_port_close(&priv->port, tty, file);
290 static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
292 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
294 tty_port_hangup(&priv->port);
297 static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
299 /* Actually, we can handle any amount as we use polled writes. */
300 return 2048;
303 static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
305 int i;
307 for (i = 0; i < c; i++)
308 dbg_io_ops->write_char(buf[i]);
309 return c;
312 static const struct tty_operations kgdb_nmi_tty_ops = {
313 .open = kgdb_nmi_tty_open,
314 .close = kgdb_nmi_tty_close,
315 .install = kgdb_nmi_tty_install,
316 .cleanup = kgdb_nmi_tty_cleanup,
317 .hangup = kgdb_nmi_tty_hangup,
318 .write_room = kgdb_nmi_tty_write_room,
319 .write = kgdb_nmi_tty_write,
322 static int kgdb_nmi_enable_console(int argc, const char *argv[])
324 kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
325 return 0;
328 int kgdb_register_nmi_console(void)
330 int ret;
332 if (!arch_kgdb_ops.enable_nmi)
333 return 0;
335 kgdb_nmi_tty_driver = alloc_tty_driver(1);
336 if (!kgdb_nmi_tty_driver) {
337 pr_err("%s: cannot allocate tty\n", __func__);
338 return -ENOMEM;
340 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
341 kgdb_nmi_tty_driver->name = "ttyNMI";
342 kgdb_nmi_tty_driver->num = 1;
343 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
344 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
345 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
346 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
347 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
348 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
349 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
351 ret = tty_register_driver(kgdb_nmi_tty_driver);
352 if (ret) {
353 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
354 goto err_drv_reg;
357 ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
358 "switch to Linux NMI console", 0);
359 if (ret) {
360 pr_err("%s: can't register kdb command: %d\n", __func__, ret);
361 goto err_kdb_reg;
364 register_console(&kgdb_nmi_console);
365 arch_kgdb_ops.enable_nmi(1);
367 return 0;
368 err_kdb_reg:
369 tty_unregister_driver(kgdb_nmi_tty_driver);
370 err_drv_reg:
371 put_tty_driver(kgdb_nmi_tty_driver);
372 return ret;
374 EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
376 int kgdb_unregister_nmi_console(void)
378 int ret;
380 if (!arch_kgdb_ops.enable_nmi)
381 return 0;
382 arch_kgdb_ops.enable_nmi(0);
384 kdb_unregister("nmi_console");
386 ret = unregister_console(&kgdb_nmi_console);
387 if (ret)
388 return ret;
390 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
391 if (ret)
392 return ret;
393 put_tty_driver(kgdb_nmi_tty_driver);
395 return 0;
397 EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);