kgdb_nmi.c 9.4 KB

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