kgdb_nmi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/compiler.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include <linux/atomic.h>
  20. #include <linux/console.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_driver.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/hrtimer.h>
  27. #include <linux/tick.h>
  28. #include <linux/kfifo.h>
  29. #include <linux/kgdb.h>
  30. #include <linux/kdb.h>
  31. static int kgdb_nmi_knock = 1;
  32. module_param_named(knock, kgdb_nmi_knock, int, 0600);
  33. MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
  34. "must be used to enter the debugger; when set to 0, " \
  35. "hitting return key is enough to enter the debugger; " \
  36. "when set to -1, the debugger is entered immediately " \
  37. "upon NMI");
  38. static char *kgdb_nmi_magic = "$3#33";
  39. module_param_named(magic, kgdb_nmi_magic, charp, 0600);
  40. MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
  41. static bool kgdb_nmi_tty_enabled;
  42. static int kgdb_nmi_console_setup(struct console *co, char *options)
  43. {
  44. /* The NMI console uses the dbg_io_ops to issue console messages. To
  45. * avoid duplicate messages during kdb sessions we must inform kdb's
  46. * I/O utilities that messages sent to the console will automatically
  47. * be displayed on the dbg_io.
  48. */
  49. dbg_io_ops->is_console = true;
  50. return 0;
  51. }
  52. static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
  53. {
  54. int i;
  55. for (i = 0; i < c; i++)
  56. dbg_io_ops->write_char(s[i]);
  57. }
  58. static struct tty_driver *kgdb_nmi_tty_driver;
  59. static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
  60. {
  61. *idx = co->index;
  62. return kgdb_nmi_tty_driver;
  63. }
  64. static struct console kgdb_nmi_console = {
  65. .name = "ttyNMI",
  66. .setup = kgdb_nmi_console_setup,
  67. .write = kgdb_nmi_console_write,
  68. .device = kgdb_nmi_console_device,
  69. .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
  70. .index = -1,
  71. };
  72. /*
  73. * This is usually the maximum rate on debug ports. We make fifo large enough
  74. * to make copy-pasting to the terminal usable.
  75. */
  76. #define KGDB_NMI_BAUD 115200
  77. #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
  78. struct kgdb_nmi_tty_priv {
  79. struct tty_port port;
  80. struct timer_list timer;
  81. STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
  82. };
  83. static struct tty_port *kgdb_nmi_port;
  84. static void kgdb_tty_recv(int ch)
  85. {
  86. struct kgdb_nmi_tty_priv *priv;
  87. char c = ch;
  88. if (!kgdb_nmi_port || ch < 0)
  89. return;
  90. /*
  91. * Can't use port->tty->driver_data as tty might be not there. Timer
  92. * will check for tty and will get the ref, but here we don't have to
  93. * do that, and actually, we can't: we're in NMI context, no locks are
  94. * possible.
  95. */
  96. priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
  97. kfifo_in(&priv->fifo, &c, 1);
  98. }
  99. static int kgdb_nmi_poll_one_knock(void)
  100. {
  101. static int n;
  102. int c = -1;
  103. const char *magic = kgdb_nmi_magic;
  104. size_t m = strlen(magic);
  105. bool printch = 0;
  106. c = dbg_io_ops->read_char();
  107. if (c == NO_POLL_CHAR)
  108. return c;
  109. if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
  110. return 1;
  111. } else if (c == magic[n]) {
  112. n = (n + 1) % m;
  113. if (!n)
  114. return 1;
  115. printch = 1;
  116. } else {
  117. n = 0;
  118. }
  119. if (kgdb_nmi_tty_enabled) {
  120. kgdb_tty_recv(c);
  121. return 0;
  122. }
  123. if (printch) {
  124. kdb_printf("%c", c);
  125. return 0;
  126. }
  127. kdb_printf("\r%s %s to enter the debugger> %*s",
  128. kgdb_nmi_knock ? "Type" : "Hit",
  129. kgdb_nmi_knock ? magic : "<return>", (int)m, "");
  130. while (m--)
  131. kdb_printf("\b");
  132. return 0;
  133. }
  134. /**
  135. * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
  136. *
  137. * "Serial ports are often noisy, especially when muxed over another port (we
  138. * often use serial over the headset connector). Noise on the async command
  139. * line just causes characters that are ignored, on a command line that blocked
  140. * execution noise would be catastrophic." -- Colin Cross
  141. *
  142. * So, this function implements KGDB/KDB knocking on the serial line: we won't
  143. * enter the debugger until we receive a known magic phrase (which is actually
  144. * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
  145. * of knocking, i.e. just pressing the return key is enough to enter the
  146. * debugger. And if knocking is disabled, the function always returns 1.
  147. */
  148. bool kgdb_nmi_poll_knock(void)
  149. {
  150. if (kgdb_nmi_knock < 0)
  151. return 1;
  152. while (1) {
  153. int ret;
  154. ret = kgdb_nmi_poll_one_knock();
  155. if (ret == NO_POLL_CHAR)
  156. return 0;
  157. else if (ret == 1)
  158. break;
  159. }
  160. return 1;
  161. }
  162. /*
  163. * The tasklet is cheap, it does not cause wakeups when reschedules itself,
  164. * instead it waits for the next tick.
  165. */
  166. static void kgdb_nmi_tty_receiver(unsigned long data)
  167. {
  168. struct kgdb_nmi_tty_priv *priv = (void *)data;
  169. char ch;
  170. priv->timer.expires = jiffies + (HZ/100);
  171. add_timer(&priv->timer);
  172. if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
  173. return;
  174. while (kfifo_out(&priv->fifo, &ch, 1))
  175. tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
  176. tty_flip_buffer_push(&priv->port);
  177. }
  178. static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
  179. {
  180. struct kgdb_nmi_tty_priv *priv =
  181. container_of(port, struct kgdb_nmi_tty_priv, port);
  182. kgdb_nmi_port = port;
  183. priv->timer.expires = jiffies + (HZ/100);
  184. add_timer(&priv->timer);
  185. return 0;
  186. }
  187. static void kgdb_nmi_tty_shutdown(struct tty_port *port)
  188. {
  189. struct kgdb_nmi_tty_priv *priv =
  190. container_of(port, struct kgdb_nmi_tty_priv, port);
  191. del_timer(&priv->timer);
  192. kgdb_nmi_port = NULL;
  193. }
  194. static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
  195. .activate = kgdb_nmi_tty_activate,
  196. .shutdown = kgdb_nmi_tty_shutdown,
  197. };
  198. static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
  199. {
  200. struct kgdb_nmi_tty_priv *priv;
  201. int ret;
  202. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  203. if (!priv)
  204. return -ENOMEM;
  205. INIT_KFIFO(priv->fifo);
  206. setup_timer(&priv->timer, kgdb_nmi_tty_receiver, (unsigned long)priv);
  207. tty_port_init(&priv->port);
  208. priv->port.ops = &kgdb_nmi_tty_port_ops;
  209. tty->driver_data = priv;
  210. ret = tty_port_install(&priv->port, drv, tty);
  211. if (ret) {
  212. pr_err("%s: can't install tty port: %d\n", __func__, ret);
  213. goto err;
  214. }
  215. return 0;
  216. err:
  217. tty_port_destroy(&priv->port);
  218. kfree(priv);
  219. return ret;
  220. }
  221. static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
  222. {
  223. struct kgdb_nmi_tty_priv *priv = tty->driver_data;
  224. tty->driver_data = NULL;
  225. tty_port_destroy(&priv->port);
  226. kfree(priv);
  227. }
  228. static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
  229. {
  230. struct kgdb_nmi_tty_priv *priv = tty->driver_data;
  231. return tty_port_open(&priv->port, tty, file);
  232. }
  233. static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
  234. {
  235. struct kgdb_nmi_tty_priv *priv = tty->driver_data;
  236. tty_port_close(&priv->port, tty, file);
  237. }
  238. static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
  239. {
  240. struct kgdb_nmi_tty_priv *priv = tty->driver_data;
  241. tty_port_hangup(&priv->port);
  242. }
  243. static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
  244. {
  245. /* Actually, we can handle any amount as we use polled writes. */
  246. return 2048;
  247. }
  248. static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
  249. {
  250. int i;
  251. for (i = 0; i < c; i++)
  252. dbg_io_ops->write_char(buf[i]);
  253. return c;
  254. }
  255. static const struct tty_operations kgdb_nmi_tty_ops = {
  256. .open = kgdb_nmi_tty_open,
  257. .close = kgdb_nmi_tty_close,
  258. .install = kgdb_nmi_tty_install,
  259. .cleanup = kgdb_nmi_tty_cleanup,
  260. .hangup = kgdb_nmi_tty_hangup,
  261. .write_room = kgdb_nmi_tty_write_room,
  262. .write = kgdb_nmi_tty_write,
  263. };
  264. static int kgdb_nmi_enable_console(int argc, const char *argv[])
  265. {
  266. kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
  267. return 0;
  268. }
  269. int kgdb_register_nmi_console(void)
  270. {
  271. int ret;
  272. if (!arch_kgdb_ops.enable_nmi)
  273. return 0;
  274. kgdb_nmi_tty_driver = alloc_tty_driver(1);
  275. if (!kgdb_nmi_tty_driver) {
  276. pr_err("%s: cannot allocate tty\n", __func__);
  277. return -ENOMEM;
  278. }
  279. kgdb_nmi_tty_driver->driver_name = "ttyNMI";
  280. kgdb_nmi_tty_driver->name = "ttyNMI";
  281. kgdb_nmi_tty_driver->num = 1;
  282. kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  283. kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  284. kgdb_nmi_tty_driver->flags = TTY_DRIVER_REAL_RAW;
  285. kgdb_nmi_tty_driver->init_termios = tty_std_termios;
  286. tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
  287. KGDB_NMI_BAUD, KGDB_NMI_BAUD);
  288. tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
  289. ret = tty_register_driver(kgdb_nmi_tty_driver);
  290. if (ret) {
  291. pr_err("%s: can't register tty driver: %d\n", __func__, ret);
  292. goto err_drv_reg;
  293. }
  294. ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
  295. "switch to Linux NMI console", 0);
  296. if (ret) {
  297. pr_err("%s: can't register kdb command: %d\n", __func__, ret);
  298. goto err_kdb_reg;
  299. }
  300. register_console(&kgdb_nmi_console);
  301. arch_kgdb_ops.enable_nmi(1);
  302. return 0;
  303. err_kdb_reg:
  304. tty_unregister_driver(kgdb_nmi_tty_driver);
  305. err_drv_reg:
  306. put_tty_driver(kgdb_nmi_tty_driver);
  307. return ret;
  308. }
  309. EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
  310. int kgdb_unregister_nmi_console(void)
  311. {
  312. int ret;
  313. if (!arch_kgdb_ops.enable_nmi)
  314. return 0;
  315. arch_kgdb_ops.enable_nmi(0);
  316. kdb_unregister("nmi_console");
  317. ret = unregister_console(&kgdb_nmi_console);
  318. if (ret)
  319. return ret;
  320. ret = tty_unregister_driver(kgdb_nmi_tty_driver);
  321. if (ret)
  322. return ret;
  323. put_tty_driver(kgdb_nmi_tty_driver);
  324. return 0;
  325. }
  326. EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);