Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 16559ae | 2013-02-04 15:35:26 -0800 | [diff] [blame] | 25 | #include <linux/serial_core.h> |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 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> |
| 32 | |
| 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 " \ |
| 39 | "upon NMI"); |
| 40 | |
| 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)"); |
| 44 | |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 45 | static atomic_t kgdb_nmi_num_readers = ATOMIC_INIT(0); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 46 | |
Daniel Thompson | bd71a1c | 2014-05-29 09:48:46 +0100 | [diff] [blame] | 47 | static int kgdb_nmi_console_setup(struct console *co, char *options) |
| 48 | { |
Daniel Thompson | 287f03c | 2014-09-03 12:57:52 +0100 | [diff] [blame] | 49 | arch_kgdb_ops.enable_nmi(1); |
| 50 | |
Daniel Thompson | bd71a1c | 2014-05-29 09:48:46 +0100 | [diff] [blame] | 51 | /* The NMI console uses the dbg_io_ops to issue console messages. To |
| 52 | * avoid duplicate messages during kdb sessions we must inform kdb's |
| 53 | * I/O utilities that messages sent to the console will automatically |
| 54 | * be displayed on the dbg_io. |
| 55 | */ |
| 56 | dbg_io_ops->is_console = true; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 61 | static void kgdb_nmi_console_write(struct console *co, const char *s, uint c) |
| 62 | { |
| 63 | int i; |
| 64 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 65 | for (i = 0; i < c; i++) |
| 66 | dbg_io_ops->write_char(s[i]); |
| 67 | } |
| 68 | |
| 69 | static struct tty_driver *kgdb_nmi_tty_driver; |
| 70 | |
| 71 | static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx) |
| 72 | { |
| 73 | *idx = co->index; |
| 74 | return kgdb_nmi_tty_driver; |
| 75 | } |
| 76 | |
| 77 | static struct console kgdb_nmi_console = { |
| 78 | .name = "ttyNMI", |
Daniel Thompson | bd71a1c | 2014-05-29 09:48:46 +0100 | [diff] [blame] | 79 | .setup = kgdb_nmi_console_setup, |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 80 | .write = kgdb_nmi_console_write, |
| 81 | .device = kgdb_nmi_console_device, |
Daniel Thompson | 287f03c | 2014-09-03 12:57:52 +0100 | [diff] [blame] | 82 | .flags = CON_PRINTBUFFER | CON_ANYTIME, |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 83 | .index = -1, |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * This is usually the maximum rate on debug ports. We make fifo large enough |
| 88 | * to make copy-pasting to the terminal usable. |
| 89 | */ |
| 90 | #define KGDB_NMI_BAUD 115200 |
| 91 | #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ) |
| 92 | |
| 93 | struct kgdb_nmi_tty_priv { |
| 94 | struct tty_port port; |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 95 | struct timer_list timer; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 96 | STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo; |
| 97 | }; |
| 98 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 99 | static struct tty_port *kgdb_nmi_port; |
| 100 | |
| 101 | static void kgdb_tty_recv(int ch) |
| 102 | { |
| 103 | struct kgdb_nmi_tty_priv *priv; |
| 104 | char c = ch; |
| 105 | |
| 106 | if (!kgdb_nmi_port || ch < 0) |
| 107 | return; |
| 108 | /* |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 109 | * Can't use port->tty->driver_data as tty might be not there. Timer |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 110 | * will check for tty and will get the ref, but here we don't have to |
| 111 | * do that, and actually, we can't: we're in NMI context, no locks are |
| 112 | * possible. |
| 113 | */ |
Daniel Thompson | 06d1828 | 2014-05-29 09:48:44 +0100 | [diff] [blame] | 114 | priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 115 | kfifo_in(&priv->fifo, &c, 1); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int kgdb_nmi_poll_one_knock(void) |
| 119 | { |
| 120 | static int n; |
| 121 | int c = -1; |
| 122 | const char *magic = kgdb_nmi_magic; |
| 123 | size_t m = strlen(magic); |
| 124 | bool printch = 0; |
| 125 | |
| 126 | c = dbg_io_ops->read_char(); |
| 127 | if (c == NO_POLL_CHAR) |
| 128 | return c; |
| 129 | |
| 130 | if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) { |
| 131 | return 1; |
| 132 | } else if (c == magic[n]) { |
| 133 | n = (n + 1) % m; |
| 134 | if (!n) |
| 135 | return 1; |
| 136 | printch = 1; |
| 137 | } else { |
| 138 | n = 0; |
| 139 | } |
| 140 | |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 141 | if (atomic_read(&kgdb_nmi_num_readers)) { |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 142 | kgdb_tty_recv(c); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | if (printch) { |
| 147 | kdb_printf("%c", c); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | kdb_printf("\r%s %s to enter the debugger> %*s", |
| 152 | kgdb_nmi_knock ? "Type" : "Hit", |
| 153 | kgdb_nmi_knock ? magic : "<return>", (int)m, ""); |
| 154 | while (m--) |
| 155 | kdb_printf("\b"); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * kgdb_nmi_poll_knock - Check if it is time to enter the debugger |
| 161 | * |
| 162 | * "Serial ports are often noisy, especially when muxed over another port (we |
| 163 | * often use serial over the headset connector). Noise on the async command |
| 164 | * line just causes characters that are ignored, on a command line that blocked |
| 165 | * execution noise would be catastrophic." -- Colin Cross |
| 166 | * |
| 167 | * So, this function implements KGDB/KDB knocking on the serial line: we won't |
| 168 | * enter the debugger until we receive a known magic phrase (which is actually |
| 169 | * "$3#33", known as "escape to KDB" command. There is also a relaxed variant |
| 170 | * of knocking, i.e. just pressing the return key is enough to enter the |
| 171 | * debugger. And if knocking is disabled, the function always returns 1. |
| 172 | */ |
| 173 | bool kgdb_nmi_poll_knock(void) |
| 174 | { |
| 175 | if (kgdb_nmi_knock < 0) |
Joe Perches | cbc7f6b | 2015-03-30 10:43:23 -0700 | [diff] [blame] | 176 | return true; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 177 | |
| 178 | while (1) { |
| 179 | int ret; |
| 180 | |
| 181 | ret = kgdb_nmi_poll_one_knock(); |
| 182 | if (ret == NO_POLL_CHAR) |
Joe Perches | cbc7f6b | 2015-03-30 10:43:23 -0700 | [diff] [blame] | 183 | return false; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 184 | else if (ret == 1) |
| 185 | break; |
| 186 | } |
Joe Perches | cbc7f6b | 2015-03-30 10:43:23 -0700 | [diff] [blame] | 187 | return true; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
| 191 | * The tasklet is cheap, it does not cause wakeups when reschedules itself, |
| 192 | * instead it waits for the next tick. |
| 193 | */ |
| 194 | static void kgdb_nmi_tty_receiver(unsigned long data) |
| 195 | { |
| 196 | struct kgdb_nmi_tty_priv *priv = (void *)data; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 197 | char ch; |
| 198 | |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 199 | priv->timer.expires = jiffies + (HZ/100); |
| 200 | add_timer(&priv->timer); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 201 | |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 202 | if (likely(!atomic_read(&kgdb_nmi_num_readers) || |
| 203 | !kfifo_len(&priv->fifo))) |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 204 | return; |
| 205 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 206 | while (kfifo_out(&priv->fifo, &ch, 1)) |
Jiri Slaby | 92a19f9 | 2013-01-03 15:53:03 +0100 | [diff] [blame] | 207 | tty_insert_flip_char(&priv->port, ch, TTY_NORMAL); |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 208 | tty_flip_buffer_push(&priv->port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty) |
| 212 | { |
Daniel Thompson | 06d1828 | 2014-05-29 09:48:44 +0100 | [diff] [blame] | 213 | struct kgdb_nmi_tty_priv *priv = |
| 214 | container_of(port, struct kgdb_nmi_tty_priv, port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 215 | |
| 216 | kgdb_nmi_port = port; |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 217 | priv->timer.expires = jiffies + (HZ/100); |
| 218 | add_timer(&priv->timer); |
| 219 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static void kgdb_nmi_tty_shutdown(struct tty_port *port) |
| 224 | { |
Daniel Thompson | 06d1828 | 2014-05-29 09:48:44 +0100 | [diff] [blame] | 225 | struct kgdb_nmi_tty_priv *priv = |
| 226 | container_of(port, struct kgdb_nmi_tty_priv, port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 227 | |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 228 | del_timer(&priv->timer); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 229 | kgdb_nmi_port = NULL; |
| 230 | } |
| 231 | |
| 232 | static const struct tty_port_operations kgdb_nmi_tty_port_ops = { |
| 233 | .activate = kgdb_nmi_tty_activate, |
| 234 | .shutdown = kgdb_nmi_tty_shutdown, |
| 235 | }; |
| 236 | |
| 237 | static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty) |
| 238 | { |
| 239 | struct kgdb_nmi_tty_priv *priv; |
| 240 | int ret; |
| 241 | |
| 242 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 243 | if (!priv) |
| 244 | return -ENOMEM; |
| 245 | |
| 246 | INIT_KFIFO(priv->fifo); |
Daniel Thompson | 8a0ff60 | 2014-05-29 09:48:45 +0100 | [diff] [blame] | 247 | setup_timer(&priv->timer, kgdb_nmi_tty_receiver, (unsigned long)priv); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 248 | tty_port_init(&priv->port); |
| 249 | priv->port.ops = &kgdb_nmi_tty_port_ops; |
| 250 | tty->driver_data = priv; |
| 251 | |
| 252 | ret = tty_port_install(&priv->port, drv, tty); |
| 253 | if (ret) { |
| 254 | pr_err("%s: can't install tty port: %d\n", __func__, ret); |
| 255 | goto err; |
| 256 | } |
| 257 | return 0; |
| 258 | err: |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 259 | tty_port_destroy(&priv->port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 260 | kfree(priv); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static void kgdb_nmi_tty_cleanup(struct tty_struct *tty) |
| 265 | { |
| 266 | struct kgdb_nmi_tty_priv *priv = tty->driver_data; |
| 267 | |
| 268 | tty->driver_data = NULL; |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 269 | tty_port_destroy(&priv->port); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 270 | kfree(priv); |
| 271 | } |
| 272 | |
| 273 | static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file) |
| 274 | { |
| 275 | struct kgdb_nmi_tty_priv *priv = tty->driver_data; |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 276 | unsigned int mode = file->f_flags & O_ACCMODE; |
| 277 | int ret; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 278 | |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 279 | ret = tty_port_open(&priv->port, tty, file); |
| 280 | if (!ret && (mode == O_RDONLY || mode == O_RDWR)) |
| 281 | atomic_inc(&kgdb_nmi_num_readers); |
| 282 | |
| 283 | return ret; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file) |
| 287 | { |
| 288 | struct kgdb_nmi_tty_priv *priv = tty->driver_data; |
Daniel Thompson | c8b29f0 | 2014-07-16 14:30:13 +0100 | [diff] [blame] | 289 | unsigned int mode = file->f_flags & O_ACCMODE; |
| 290 | |
| 291 | if (mode == O_RDONLY || mode == O_RDWR) |
| 292 | atomic_dec(&kgdb_nmi_num_readers); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 293 | |
| 294 | tty_port_close(&priv->port, tty, file); |
| 295 | } |
| 296 | |
| 297 | static void kgdb_nmi_tty_hangup(struct tty_struct *tty) |
| 298 | { |
| 299 | struct kgdb_nmi_tty_priv *priv = tty->driver_data; |
| 300 | |
| 301 | tty_port_hangup(&priv->port); |
| 302 | } |
| 303 | |
| 304 | static int kgdb_nmi_tty_write_room(struct tty_struct *tty) |
| 305 | { |
| 306 | /* Actually, we can handle any amount as we use polled writes. */ |
| 307 | return 2048; |
| 308 | } |
| 309 | |
| 310 | static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = 0; i < c; i++) |
| 315 | dbg_io_ops->write_char(buf[i]); |
| 316 | return c; |
| 317 | } |
| 318 | |
| 319 | static const struct tty_operations kgdb_nmi_tty_ops = { |
| 320 | .open = kgdb_nmi_tty_open, |
| 321 | .close = kgdb_nmi_tty_close, |
| 322 | .install = kgdb_nmi_tty_install, |
| 323 | .cleanup = kgdb_nmi_tty_cleanup, |
| 324 | .hangup = kgdb_nmi_tty_hangup, |
| 325 | .write_room = kgdb_nmi_tty_write_room, |
| 326 | .write = kgdb_nmi_tty_write, |
| 327 | }; |
| 328 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 329 | int kgdb_register_nmi_console(void) |
| 330 | { |
| 331 | int ret; |
| 332 | |
| 333 | if (!arch_kgdb_ops.enable_nmi) |
| 334 | return 0; |
| 335 | |
| 336 | kgdb_nmi_tty_driver = alloc_tty_driver(1); |
| 337 | if (!kgdb_nmi_tty_driver) { |
| 338 | pr_err("%s: cannot allocate tty\n", __func__); |
| 339 | return -ENOMEM; |
| 340 | } |
| 341 | kgdb_nmi_tty_driver->driver_name = "ttyNMI"; |
| 342 | kgdb_nmi_tty_driver->name = "ttyNMI"; |
| 343 | kgdb_nmi_tty_driver->num = 1; |
| 344 | kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 345 | kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 346 | kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW; |
| 347 | kgdb_nmi_tty_driver->init_termios = tty_std_termios; |
| 348 | tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios, |
| 349 | KGDB_NMI_BAUD, KGDB_NMI_BAUD); |
| 350 | tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops); |
| 351 | |
| 352 | ret = tty_register_driver(kgdb_nmi_tty_driver); |
| 353 | if (ret) { |
| 354 | pr_err("%s: can't register tty driver: %d\n", __func__, ret); |
| 355 | goto err_drv_reg; |
| 356 | } |
| 357 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 358 | register_console(&kgdb_nmi_console); |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 359 | |
| 360 | return 0; |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 361 | err_drv_reg: |
| 362 | put_tty_driver(kgdb_nmi_tty_driver); |
| 363 | return ret; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(kgdb_register_nmi_console); |
| 366 | |
| 367 | int kgdb_unregister_nmi_console(void) |
| 368 | { |
| 369 | int ret; |
| 370 | |
| 371 | if (!arch_kgdb_ops.enable_nmi) |
| 372 | return 0; |
| 373 | arch_kgdb_ops.enable_nmi(0); |
| 374 | |
Anton Vorontsov | 0c57dfc | 2012-09-24 14:27:56 -0700 | [diff] [blame] | 375 | ret = unregister_console(&kgdb_nmi_console); |
| 376 | if (ret) |
| 377 | return ret; |
| 378 | |
| 379 | ret = tty_unregister_driver(kgdb_nmi_tty_driver); |
| 380 | if (ret) |
| 381 | return ret; |
| 382 | put_tty_driver(kgdb_nmi_tty_driver); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console); |