kgdb_nmi.c 9.8 KB

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