blob: 20d21d09ee864a69313b65443729da757dbd12d0 [file] [log] [blame]
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -07001/*
2 * KGDB NMI serial console
3 *
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>
9 *
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.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/compiler.h>
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070018#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>
Greg Kroah-Hartman16559ae2013-02-04 15:35:26 -080025#include <linux/serial_core.h>
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070026#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>
32
33static int kgdb_nmi_knock = 1;
34module_param_named(knock, kgdb_nmi_knock, int, 0600);
35MODULE_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 " \
39 "upon NMI");
40
41static char *kgdb_nmi_magic = "$3#33";
42module_param_named(magic, kgdb_nmi_magic, charp, 0600);
43MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
44
45static bool kgdb_nmi_tty_enabled;
46
47static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
48{
49 int i;
50
51 if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
52 return;
53
54 for (i = 0; i < c; i++)
55 dbg_io_ops->write_char(s[i]);
56}
57
58static struct tty_driver *kgdb_nmi_tty_driver;
59
60static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
61{
62 *idx = co->index;
63 return kgdb_nmi_tty_driver;
64}
65
66static struct console kgdb_nmi_console = {
67 .name = "ttyNMI",
68 .write = kgdb_nmi_console_write,
69 .device = kgdb_nmi_console_device,
70 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
71 .index = -1,
72};
73
74/*
75 * This is usually the maximum rate on debug ports. We make fifo large enough
76 * to make copy-pasting to the terminal usable.
77 */
78#define KGDB_NMI_BAUD 115200
79#define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
80
81struct kgdb_nmi_tty_priv {
82 struct tty_port port;
Daniel Thompson8a0ff602014-05-29 09:48:45 +010083 struct timer_list timer;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070084 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
85};
86
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070087static struct tty_port *kgdb_nmi_port;
88
89static void kgdb_tty_recv(int ch)
90{
91 struct kgdb_nmi_tty_priv *priv;
92 char c = ch;
93
94 if (!kgdb_nmi_port || ch < 0)
95 return;
96 /*
Daniel Thompson8a0ff602014-05-29 09:48:45 +010097 * Can't use port->tty->driver_data as tty might be not there. Timer
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070098 * will check for tty and will get the ref, but here we don't have to
99 * do that, and actually, we can't: we're in NMI context, no locks are
100 * possible.
101 */
Daniel Thompson06d18282014-05-29 09:48:44 +0100102 priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700103 kfifo_in(&priv->fifo, &c, 1);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700104}
105
106static int kgdb_nmi_poll_one_knock(void)
107{
108 static int n;
109 int c = -1;
110 const char *magic = kgdb_nmi_magic;
111 size_t m = strlen(magic);
112 bool printch = 0;
113
114 c = dbg_io_ops->read_char();
115 if (c == NO_POLL_CHAR)
116 return c;
117
118 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
119 return 1;
120 } else if (c == magic[n]) {
121 n = (n + 1) % m;
122 if (!n)
123 return 1;
124 printch = 1;
125 } else {
126 n = 0;
127 }
128
129 if (kgdb_nmi_tty_enabled) {
130 kgdb_tty_recv(c);
131 return 0;
132 }
133
134 if (printch) {
135 kdb_printf("%c", c);
136 return 0;
137 }
138
139 kdb_printf("\r%s %s to enter the debugger> %*s",
140 kgdb_nmi_knock ? "Type" : "Hit",
141 kgdb_nmi_knock ? magic : "<return>", (int)m, "");
142 while (m--)
143 kdb_printf("\b");
144 return 0;
145}
146
147/**
148 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
149 *
150 * "Serial ports are often noisy, especially when muxed over another port (we
151 * often use serial over the headset connector). Noise on the async command
152 * line just causes characters that are ignored, on a command line that blocked
153 * execution noise would be catastrophic." -- Colin Cross
154 *
155 * So, this function implements KGDB/KDB knocking on the serial line: we won't
156 * enter the debugger until we receive a known magic phrase (which is actually
157 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
158 * of knocking, i.e. just pressing the return key is enough to enter the
159 * debugger. And if knocking is disabled, the function always returns 1.
160 */
161bool kgdb_nmi_poll_knock(void)
162{
163 if (kgdb_nmi_knock < 0)
164 return 1;
165
166 while (1) {
167 int ret;
168
169 ret = kgdb_nmi_poll_one_knock();
170 if (ret == NO_POLL_CHAR)
171 return 0;
172 else if (ret == 1)
173 break;
174 }
175 return 1;
176}
177
178/*
179 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
180 * instead it waits for the next tick.
181 */
182static void kgdb_nmi_tty_receiver(unsigned long data)
183{
184 struct kgdb_nmi_tty_priv *priv = (void *)data;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700185 char ch;
186
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100187 priv->timer.expires = jiffies + (HZ/100);
188 add_timer(&priv->timer);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700189
190 if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
191 return;
192
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700193 while (kfifo_out(&priv->fifo, &ch, 1))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100194 tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100195 tty_flip_buffer_push(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700196}
197
198static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
199{
Daniel Thompson06d18282014-05-29 09:48:44 +0100200 struct kgdb_nmi_tty_priv *priv =
201 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700202
203 kgdb_nmi_port = port;
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100204 priv->timer.expires = jiffies + (HZ/100);
205 add_timer(&priv->timer);
206
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700207 return 0;
208}
209
210static void kgdb_nmi_tty_shutdown(struct tty_port *port)
211{
Daniel Thompson06d18282014-05-29 09:48:44 +0100212 struct kgdb_nmi_tty_priv *priv =
213 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700214
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100215 del_timer(&priv->timer);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700216 kgdb_nmi_port = NULL;
217}
218
219static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
220 .activate = kgdb_nmi_tty_activate,
221 .shutdown = kgdb_nmi_tty_shutdown,
222};
223
224static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
225{
226 struct kgdb_nmi_tty_priv *priv;
227 int ret;
228
229 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
230 if (!priv)
231 return -ENOMEM;
232
233 INIT_KFIFO(priv->fifo);
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100234 setup_timer(&priv->timer, kgdb_nmi_tty_receiver, (unsigned long)priv);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700235 tty_port_init(&priv->port);
236 priv->port.ops = &kgdb_nmi_tty_port_ops;
237 tty->driver_data = priv;
238
239 ret = tty_port_install(&priv->port, drv, tty);
240 if (ret) {
241 pr_err("%s: can't install tty port: %d\n", __func__, ret);
242 goto err;
243 }
244 return 0;
245err:
Jiri Slaby191c5f12012-11-15 09:49:56 +0100246 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700247 kfree(priv);
248 return ret;
249}
250
251static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
252{
253 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
254
255 tty->driver_data = NULL;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100256 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700257 kfree(priv);
258}
259
260static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
261{
262 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
263
264 return tty_port_open(&priv->port, tty, file);
265}
266
267static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
268{
269 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
270
271 tty_port_close(&priv->port, tty, file);
272}
273
274static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
275{
276 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
277
278 tty_port_hangup(&priv->port);
279}
280
281static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
282{
283 /* Actually, we can handle any amount as we use polled writes. */
284 return 2048;
285}
286
287static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
288{
289 int i;
290
291 for (i = 0; i < c; i++)
292 dbg_io_ops->write_char(buf[i]);
293 return c;
294}
295
296static const struct tty_operations kgdb_nmi_tty_ops = {
297 .open = kgdb_nmi_tty_open,
298 .close = kgdb_nmi_tty_close,
299 .install = kgdb_nmi_tty_install,
300 .cleanup = kgdb_nmi_tty_cleanup,
301 .hangup = kgdb_nmi_tty_hangup,
302 .write_room = kgdb_nmi_tty_write_room,
303 .write = kgdb_nmi_tty_write,
304};
305
306static int kgdb_nmi_enable_console(int argc, const char *argv[])
307{
308 kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
309 return 0;
310}
311
312int kgdb_register_nmi_console(void)
313{
314 int ret;
315
316 if (!arch_kgdb_ops.enable_nmi)
317 return 0;
318
319 kgdb_nmi_tty_driver = alloc_tty_driver(1);
320 if (!kgdb_nmi_tty_driver) {
321 pr_err("%s: cannot allocate tty\n", __func__);
322 return -ENOMEM;
323 }
324 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
325 kgdb_nmi_tty_driver->name = "ttyNMI";
326 kgdb_nmi_tty_driver->num = 1;
327 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
328 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
329 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
330 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
331 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
332 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
333 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
334
335 ret = tty_register_driver(kgdb_nmi_tty_driver);
336 if (ret) {
337 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
338 goto err_drv_reg;
339 }
340
341 ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
342 "switch to Linux NMI console", 0);
343 if (ret) {
344 pr_err("%s: can't register kdb command: %d\n", __func__, ret);
345 goto err_kdb_reg;
346 }
347
348 register_console(&kgdb_nmi_console);
349 arch_kgdb_ops.enable_nmi(1);
350
351 return 0;
352err_kdb_reg:
353 tty_unregister_driver(kgdb_nmi_tty_driver);
354err_drv_reg:
355 put_tty_driver(kgdb_nmi_tty_driver);
356 return ret;
357}
358EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
359
360int kgdb_unregister_nmi_console(void)
361{
362 int ret;
363
364 if (!arch_kgdb_ops.enable_nmi)
365 return 0;
366 arch_kgdb_ops.enable_nmi(0);
367
368 kdb_unregister("nmi_console");
369
370 ret = unregister_console(&kgdb_nmi_console);
371 if (ret)
372 return ret;
373
374 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
375 if (ret)
376 return ret;
377 put_tty_driver(kgdb_nmi_tty_driver);
378
379 return 0;
380}
381EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);