blob: d51b2a1ba909e84812f3276fc50038a69751fc7f [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;
83 struct tasklet_struct tlet;
84 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
85};
86
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070087/*
88 * Our debugging console is polled in a tasklet, so we'll check for input
89 * every tick. In HZ-less mode, we should program the next tick. We have
90 * to use the lowlevel stuff as no locks should be grabbed.
91 */
92#ifdef CONFIG_HIGH_RES_TIMERS
93static void kgdb_tty_poke(void)
94{
95 tick_program_event(ktime_get(), 0);
96}
97#else
98static inline void kgdb_tty_poke(void) {}
99#endif
100
101static struct tty_port *kgdb_nmi_port;
102
103static void kgdb_tty_recv(int ch)
104{
105 struct kgdb_nmi_tty_priv *priv;
106 char c = ch;
107
108 if (!kgdb_nmi_port || ch < 0)
109 return;
110 /*
111 * Can't use port->tty->driver_data as tty might be not there. Tasklet
112 * will check for tty and will get the ref, but here we don't have to
113 * do that, and actually, we can't: we're in NMI context, no locks are
114 * possible.
115 */
Daniel Thompson06d18282014-05-29 09:48:44 +0100116 priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700117 kfifo_in(&priv->fifo, &c, 1);
118 kgdb_tty_poke();
119}
120
121static int kgdb_nmi_poll_one_knock(void)
122{
123 static int n;
124 int c = -1;
125 const char *magic = kgdb_nmi_magic;
126 size_t m = strlen(magic);
127 bool printch = 0;
128
129 c = dbg_io_ops->read_char();
130 if (c == NO_POLL_CHAR)
131 return c;
132
133 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
134 return 1;
135 } else if (c == magic[n]) {
136 n = (n + 1) % m;
137 if (!n)
138 return 1;
139 printch = 1;
140 } else {
141 n = 0;
142 }
143
144 if (kgdb_nmi_tty_enabled) {
145 kgdb_tty_recv(c);
146 return 0;
147 }
148
149 if (printch) {
150 kdb_printf("%c", c);
151 return 0;
152 }
153
154 kdb_printf("\r%s %s to enter the debugger> %*s",
155 kgdb_nmi_knock ? "Type" : "Hit",
156 kgdb_nmi_knock ? magic : "<return>", (int)m, "");
157 while (m--)
158 kdb_printf("\b");
159 return 0;
160}
161
162/**
163 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
164 *
165 * "Serial ports are often noisy, especially when muxed over another port (we
166 * often use serial over the headset connector). Noise on the async command
167 * line just causes characters that are ignored, on a command line that blocked
168 * execution noise would be catastrophic." -- Colin Cross
169 *
170 * So, this function implements KGDB/KDB knocking on the serial line: we won't
171 * enter the debugger until we receive a known magic phrase (which is actually
172 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
173 * of knocking, i.e. just pressing the return key is enough to enter the
174 * debugger. And if knocking is disabled, the function always returns 1.
175 */
176bool kgdb_nmi_poll_knock(void)
177{
178 if (kgdb_nmi_knock < 0)
179 return 1;
180
181 while (1) {
182 int ret;
183
184 ret = kgdb_nmi_poll_one_knock();
185 if (ret == NO_POLL_CHAR)
186 return 0;
187 else if (ret == 1)
188 break;
189 }
190 return 1;
191}
192
193/*
194 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
195 * instead it waits for the next tick.
196 */
197static void kgdb_nmi_tty_receiver(unsigned long data)
198{
199 struct kgdb_nmi_tty_priv *priv = (void *)data;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700200 char ch;
201
202 tasklet_schedule(&priv->tlet);
203
204 if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
205 return;
206
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700207 while (kfifo_out(&priv->fifo, &ch, 1))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100208 tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100209 tty_flip_buffer_push(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700210}
211
212static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
213{
Daniel Thompson06d18282014-05-29 09:48:44 +0100214 struct kgdb_nmi_tty_priv *priv =
215 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700216
217 kgdb_nmi_port = port;
218 tasklet_schedule(&priv->tlet);
219 return 0;
220}
221
222static void kgdb_nmi_tty_shutdown(struct tty_port *port)
223{
Daniel Thompson06d18282014-05-29 09:48:44 +0100224 struct kgdb_nmi_tty_priv *priv =
225 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700226
227 tasklet_kill(&priv->tlet);
228 kgdb_nmi_port = NULL;
229}
230
231static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
232 .activate = kgdb_nmi_tty_activate,
233 .shutdown = kgdb_nmi_tty_shutdown,
234};
235
236static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
237{
238 struct kgdb_nmi_tty_priv *priv;
239 int ret;
240
241 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
242 if (!priv)
243 return -ENOMEM;
244
245 INIT_KFIFO(priv->fifo);
246 tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
247 tty_port_init(&priv->port);
248 priv->port.ops = &kgdb_nmi_tty_port_ops;
249 tty->driver_data = priv;
250
251 ret = tty_port_install(&priv->port, drv, tty);
252 if (ret) {
253 pr_err("%s: can't install tty port: %d\n", __func__, ret);
254 goto err;
255 }
256 return 0;
257err:
Jiri Slaby191c5f12012-11-15 09:49:56 +0100258 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700259 kfree(priv);
260 return ret;
261}
262
263static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
264{
265 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
266
267 tty->driver_data = NULL;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100268 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700269 kfree(priv);
270}
271
272static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
273{
274 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
275
276 return tty_port_open(&priv->port, tty, file);
277}
278
279static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
280{
281 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
282
283 tty_port_close(&priv->port, tty, file);
284}
285
286static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
287{
288 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
289
290 tty_port_hangup(&priv->port);
291}
292
293static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
294{
295 /* Actually, we can handle any amount as we use polled writes. */
296 return 2048;
297}
298
299static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
300{
301 int i;
302
303 for (i = 0; i < c; i++)
304 dbg_io_ops->write_char(buf[i]);
305 return c;
306}
307
308static const struct tty_operations kgdb_nmi_tty_ops = {
309 .open = kgdb_nmi_tty_open,
310 .close = kgdb_nmi_tty_close,
311 .install = kgdb_nmi_tty_install,
312 .cleanup = kgdb_nmi_tty_cleanup,
313 .hangup = kgdb_nmi_tty_hangup,
314 .write_room = kgdb_nmi_tty_write_room,
315 .write = kgdb_nmi_tty_write,
316};
317
318static int kgdb_nmi_enable_console(int argc, const char *argv[])
319{
320 kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
321 return 0;
322}
323
324int kgdb_register_nmi_console(void)
325{
326 int ret;
327
328 if (!arch_kgdb_ops.enable_nmi)
329 return 0;
330
331 kgdb_nmi_tty_driver = alloc_tty_driver(1);
332 if (!kgdb_nmi_tty_driver) {
333 pr_err("%s: cannot allocate tty\n", __func__);
334 return -ENOMEM;
335 }
336 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
337 kgdb_nmi_tty_driver->name = "ttyNMI";
338 kgdb_nmi_tty_driver->num = 1;
339 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
340 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
341 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
342 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
343 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
344 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
345 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
346
347 ret = tty_register_driver(kgdb_nmi_tty_driver);
348 if (ret) {
349 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
350 goto err_drv_reg;
351 }
352
353 ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
354 "switch to Linux NMI console", 0);
355 if (ret) {
356 pr_err("%s: can't register kdb command: %d\n", __func__, ret);
357 goto err_kdb_reg;
358 }
359
360 register_console(&kgdb_nmi_console);
361 arch_kgdb_ops.enable_nmi(1);
362
363 return 0;
364err_kdb_reg:
365 tty_unregister_driver(kgdb_nmi_tty_driver);
366err_drv_reg:
367 put_tty_driver(kgdb_nmi_tty_driver);
368 return ret;
369}
370EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
371
372int kgdb_unregister_nmi_console(void)
373{
374 int ret;
375
376 if (!arch_kgdb_ops.enable_nmi)
377 return 0;
378 arch_kgdb_ops.enable_nmi(0);
379
380 kdb_unregister("nmi_console");
381
382 ret = unregister_console(&kgdb_nmi_console);
383 if (ret)
384 return ret;
385
386 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
387 if (ret)
388 return ret;
389 put_tty_driver(kgdb_nmi_tty_driver);
390
391 return 0;
392}
393EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);