blob: 6ec7501b464d7cad00729bec1fd1c044db7f2e83 [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
Daniel Thompsonc8b29f02014-07-16 14:30:13 +010045static atomic_t kgdb_nmi_num_readers = ATOMIC_INIT(0);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070046
Daniel Thompsonbd71a1c2014-05-29 09:48:46 +010047static int kgdb_nmi_console_setup(struct console *co, char *options)
48{
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.
53 */
54 dbg_io_ops->is_console = true;
55
56 return 0;
57}
58
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070059static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
60{
61 int i;
62
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070063 for (i = 0; i < c; i++)
64 dbg_io_ops->write_char(s[i]);
65}
66
67static struct tty_driver *kgdb_nmi_tty_driver;
68
69static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
70{
71 *idx = co->index;
72 return kgdb_nmi_tty_driver;
73}
74
75static struct console kgdb_nmi_console = {
76 .name = "ttyNMI",
Daniel Thompsonbd71a1c2014-05-29 09:48:46 +010077 .setup = kgdb_nmi_console_setup,
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070078 .write = kgdb_nmi_console_write,
79 .device = kgdb_nmi_console_device,
80 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
81 .index = -1,
82};
83
84/*
85 * This is usually the maximum rate on debug ports. We make fifo large enough
86 * to make copy-pasting to the terminal usable.
87 */
88#define KGDB_NMI_BAUD 115200
89#define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
90
91struct kgdb_nmi_tty_priv {
92 struct tty_port port;
Daniel Thompson8a0ff602014-05-29 09:48:45 +010093 struct timer_list timer;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070094 STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
95};
96
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -070097static struct tty_port *kgdb_nmi_port;
98
99static void kgdb_tty_recv(int ch)
100{
101 struct kgdb_nmi_tty_priv *priv;
102 char c = ch;
103
104 if (!kgdb_nmi_port || ch < 0)
105 return;
106 /*
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100107 * Can't use port->tty->driver_data as tty might be not there. Timer
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700108 * 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
110 * possible.
111 */
Daniel Thompson06d18282014-05-29 09:48:44 +0100112 priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700113 kfifo_in(&priv->fifo, &c, 1);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700114}
115
116static int kgdb_nmi_poll_one_knock(void)
117{
118 static int n;
119 int c = -1;
120 const char *magic = kgdb_nmi_magic;
121 size_t m = strlen(magic);
122 bool printch = 0;
123
124 c = dbg_io_ops->read_char();
125 if (c == NO_POLL_CHAR)
126 return c;
127
128 if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
129 return 1;
130 } else if (c == magic[n]) {
131 n = (n + 1) % m;
132 if (!n)
133 return 1;
134 printch = 1;
135 } else {
136 n = 0;
137 }
138
Daniel Thompsonc8b29f02014-07-16 14:30:13 +0100139 if (atomic_read(&kgdb_nmi_num_readers)) {
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700140 kgdb_tty_recv(c);
141 return 0;
142 }
143
144 if (printch) {
145 kdb_printf("%c", c);
146 return 0;
147 }
148
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, "");
152 while (m--)
153 kdb_printf("\b");
154 return 0;
155}
156
157/**
158 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
159 *
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
164 *
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.
170 */
171bool kgdb_nmi_poll_knock(void)
172{
173 if (kgdb_nmi_knock < 0)
174 return 1;
175
176 while (1) {
177 int ret;
178
179 ret = kgdb_nmi_poll_one_knock();
180 if (ret == NO_POLL_CHAR)
181 return 0;
182 else if (ret == 1)
183 break;
184 }
185 return 1;
186}
187
188/*
189 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
190 * instead it waits for the next tick.
191 */
192static void kgdb_nmi_tty_receiver(unsigned long data)
193{
194 struct kgdb_nmi_tty_priv *priv = (void *)data;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700195 char ch;
196
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100197 priv->timer.expires = jiffies + (HZ/100);
198 add_timer(&priv->timer);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700199
Daniel Thompsonc8b29f02014-07-16 14:30:13 +0100200 if (likely(!atomic_read(&kgdb_nmi_num_readers) ||
201 !kfifo_len(&priv->fifo)))
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700202 return;
203
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700204 while (kfifo_out(&priv->fifo, &ch, 1))
Jiri Slaby92a19f92013-01-03 15:53:03 +0100205 tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100206 tty_flip_buffer_push(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700207}
208
209static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
210{
Daniel Thompson06d18282014-05-29 09:48:44 +0100211 struct kgdb_nmi_tty_priv *priv =
212 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700213
214 kgdb_nmi_port = port;
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100215 priv->timer.expires = jiffies + (HZ/100);
216 add_timer(&priv->timer);
217
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700218 return 0;
219}
220
221static void kgdb_nmi_tty_shutdown(struct tty_port *port)
222{
Daniel Thompson06d18282014-05-29 09:48:44 +0100223 struct kgdb_nmi_tty_priv *priv =
224 container_of(port, struct kgdb_nmi_tty_priv, port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700225
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100226 del_timer(&priv->timer);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700227 kgdb_nmi_port = NULL;
228}
229
230static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
231 .activate = kgdb_nmi_tty_activate,
232 .shutdown = kgdb_nmi_tty_shutdown,
233};
234
235static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
236{
237 struct kgdb_nmi_tty_priv *priv;
238 int ret;
239
240 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
241 if (!priv)
242 return -ENOMEM;
243
244 INIT_KFIFO(priv->fifo);
Daniel Thompson8a0ff602014-05-29 09:48:45 +0100245 setup_timer(&priv->timer, kgdb_nmi_tty_receiver, (unsigned long)priv);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700246 tty_port_init(&priv->port);
247 priv->port.ops = &kgdb_nmi_tty_port_ops;
248 tty->driver_data = priv;
249
250 ret = tty_port_install(&priv->port, drv, tty);
251 if (ret) {
252 pr_err("%s: can't install tty port: %d\n", __func__, ret);
253 goto err;
254 }
255 return 0;
256err:
Jiri Slaby191c5f12012-11-15 09:49:56 +0100257 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700258 kfree(priv);
259 return ret;
260}
261
262static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
263{
264 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
265
266 tty->driver_data = NULL;
Jiri Slaby191c5f12012-11-15 09:49:56 +0100267 tty_port_destroy(&priv->port);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700268 kfree(priv);
269}
270
271static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
272{
273 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
Daniel Thompsonc8b29f02014-07-16 14:30:13 +0100274 unsigned int mode = file->f_flags & O_ACCMODE;
275 int ret;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700276
Daniel Thompsonc8b29f02014-07-16 14:30:13 +0100277 ret = tty_port_open(&priv->port, tty, file);
278 if (!ret && (mode == O_RDONLY || mode == O_RDWR))
279 atomic_inc(&kgdb_nmi_num_readers);
280
281 return ret;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700282}
283
284static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
285{
286 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
Daniel Thompsonc8b29f02014-07-16 14:30:13 +0100287 unsigned int mode = file->f_flags & O_ACCMODE;
288
289 if (mode == O_RDONLY || mode == O_RDWR)
290 atomic_dec(&kgdb_nmi_num_readers);
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700291
292 tty_port_close(&priv->port, tty, file);
293}
294
295static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
296{
297 struct kgdb_nmi_tty_priv *priv = tty->driver_data;
298
299 tty_port_hangup(&priv->port);
300}
301
302static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
303{
304 /* Actually, we can handle any amount as we use polled writes. */
305 return 2048;
306}
307
308static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
309{
310 int i;
311
312 for (i = 0; i < c; i++)
313 dbg_io_ops->write_char(buf[i]);
314 return c;
315}
316
317static const struct tty_operations kgdb_nmi_tty_ops = {
318 .open = kgdb_nmi_tty_open,
319 .close = kgdb_nmi_tty_close,
320 .install = kgdb_nmi_tty_install,
321 .cleanup = kgdb_nmi_tty_cleanup,
322 .hangup = kgdb_nmi_tty_hangup,
323 .write_room = kgdb_nmi_tty_write_room,
324 .write = kgdb_nmi_tty_write,
325};
326
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700327int kgdb_register_nmi_console(void)
328{
329 int ret;
330
331 if (!arch_kgdb_ops.enable_nmi)
332 return 0;
333
334 kgdb_nmi_tty_driver = alloc_tty_driver(1);
335 if (!kgdb_nmi_tty_driver) {
336 pr_err("%s: cannot allocate tty\n", __func__);
337 return -ENOMEM;
338 }
339 kgdb_nmi_tty_driver->driver_name = "ttyNMI";
340 kgdb_nmi_tty_driver->name = "ttyNMI";
341 kgdb_nmi_tty_driver->num = 1;
342 kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
343 kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
344 kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
345 kgdb_nmi_tty_driver->init_termios = tty_std_termios;
346 tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
347 KGDB_NMI_BAUD, KGDB_NMI_BAUD);
348 tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
349
350 ret = tty_register_driver(kgdb_nmi_tty_driver);
351 if (ret) {
352 pr_err("%s: can't register tty driver: %d\n", __func__, ret);
353 goto err_drv_reg;
354 }
355
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700356 register_console(&kgdb_nmi_console);
357 arch_kgdb_ops.enable_nmi(1);
358
359 return 0;
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700360err_drv_reg:
361 put_tty_driver(kgdb_nmi_tty_driver);
362 return ret;
363}
364EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
365
366int kgdb_unregister_nmi_console(void)
367{
368 int ret;
369
370 if (!arch_kgdb_ops.enable_nmi)
371 return 0;
372 arch_kgdb_ops.enable_nmi(0);
373
Anton Vorontsov0c57dfc2012-09-24 14:27:56 -0700374 ret = unregister_console(&kgdb_nmi_console);
375 if (ret)
376 return ret;
377
378 ret = tty_unregister_driver(kgdb_nmi_tty_driver);
379 if (ret)
380 return ret;
381 put_tty_driver(kgdb_nmi_tty_driver);
382
383 return 0;
384}
385EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);