kgdb_nmi.c 9.5 KB

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