kgdboc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on the same principle as kgdboe using the NETPOLL api, this
  4. * driver uses a console polling api to implement a gdb serial inteface
  5. * which is multiplexed on a console port.
  6. *
  7. * Maintainer: Jason Wessel <jason.wessel@windriver.com>
  8. *
  9. * 2007-2008 (c) Jason Wessel - Wind River Systems, Inc.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/ctype.h>
  13. #include <linux/kgdb.h>
  14. #include <linux/kdb.h>
  15. #include <linux/tty.h>
  16. #include <linux/console.h>
  17. #include <linux/vt_kern.h>
  18. #include <linux/input.h>
  19. #include <linux/module.h>
  20. #define MAX_CONFIG_LEN 40
  21. static struct kgdb_io kgdboc_io_ops;
  22. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  23. static int configured = -1;
  24. static char config[MAX_CONFIG_LEN];
  25. static struct kparam_string kps = {
  26. .string = config,
  27. .maxlen = MAX_CONFIG_LEN,
  28. };
  29. static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
  30. static struct tty_driver *kgdb_tty_driver;
  31. static int kgdb_tty_line;
  32. #ifdef CONFIG_KDB_KEYBOARD
  33. static int kgdboc_reset_connect(struct input_handler *handler,
  34. struct input_dev *dev,
  35. const struct input_device_id *id)
  36. {
  37. input_reset_device(dev);
  38. /* Return an error - we do not want to bind, just to reset */
  39. return -ENODEV;
  40. }
  41. static void kgdboc_reset_disconnect(struct input_handle *handle)
  42. {
  43. /* We do not expect anyone to actually bind to us */
  44. BUG();
  45. }
  46. static const struct input_device_id kgdboc_reset_ids[] = {
  47. {
  48. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  49. .evbit = { BIT_MASK(EV_KEY) },
  50. },
  51. { }
  52. };
  53. static struct input_handler kgdboc_reset_handler = {
  54. .connect = kgdboc_reset_connect,
  55. .disconnect = kgdboc_reset_disconnect,
  56. .name = "kgdboc_reset",
  57. .id_table = kgdboc_reset_ids,
  58. };
  59. static DEFINE_MUTEX(kgdboc_reset_mutex);
  60. static void kgdboc_restore_input_helper(struct work_struct *dummy)
  61. {
  62. /*
  63. * We need to take a mutex to prevent several instances of
  64. * this work running on different CPUs so they don't try
  65. * to register again already registered handler.
  66. */
  67. mutex_lock(&kgdboc_reset_mutex);
  68. if (input_register_handler(&kgdboc_reset_handler) == 0)
  69. input_unregister_handler(&kgdboc_reset_handler);
  70. mutex_unlock(&kgdboc_reset_mutex);
  71. }
  72. static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
  73. static void kgdboc_restore_input(void)
  74. {
  75. if (likely(system_state == SYSTEM_RUNNING))
  76. schedule_work(&kgdboc_restore_input_work);
  77. }
  78. static int kgdboc_register_kbd(char **cptr)
  79. {
  80. if (strncmp(*cptr, "kbd", 3) == 0 ||
  81. strncmp(*cptr, "kdb", 3) == 0) {
  82. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  83. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  84. kdb_poll_idx++;
  85. if (cptr[0][3] == ',')
  86. *cptr += 4;
  87. else
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. static void kgdboc_unregister_kbd(void)
  94. {
  95. int i;
  96. for (i = 0; i < kdb_poll_idx; i++) {
  97. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  98. kdb_poll_idx--;
  99. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  100. kdb_poll_funcs[kdb_poll_idx] = NULL;
  101. i--;
  102. }
  103. }
  104. flush_work(&kgdboc_restore_input_work);
  105. }
  106. #else /* ! CONFIG_KDB_KEYBOARD */
  107. #define kgdboc_register_kbd(x) 0
  108. #define kgdboc_unregister_kbd()
  109. #define kgdboc_restore_input()
  110. #endif /* ! CONFIG_KDB_KEYBOARD */
  111. static int kgdboc_option_setup(char *opt)
  112. {
  113. if (strlen(opt) >= MAX_CONFIG_LEN) {
  114. printk(KERN_ERR "kgdboc: config string too long\n");
  115. return -ENOSPC;
  116. }
  117. strcpy(config, opt);
  118. return 0;
  119. }
  120. __setup("kgdboc=", kgdboc_option_setup);
  121. static void cleanup_kgdboc(void)
  122. {
  123. if (kgdb_unregister_nmi_console())
  124. return;
  125. kgdboc_unregister_kbd();
  126. if (configured == 1)
  127. kgdb_unregister_io_module(&kgdboc_io_ops);
  128. }
  129. static int configure_kgdboc(void)
  130. {
  131. struct tty_driver *p;
  132. int tty_line = 0;
  133. int err;
  134. char *cptr = config;
  135. struct console *cons;
  136. err = kgdboc_option_setup(config);
  137. if (err || !strlen(config) || isspace(config[0]))
  138. goto noconfig;
  139. err = -ENODEV;
  140. kgdboc_io_ops.is_console = 0;
  141. kgdb_tty_driver = NULL;
  142. kgdboc_use_kms = 0;
  143. if (strncmp(cptr, "kms,", 4) == 0) {
  144. cptr += 4;
  145. kgdboc_use_kms = 1;
  146. }
  147. if (kgdboc_register_kbd(&cptr))
  148. goto do_register;
  149. p = tty_find_polling_driver(cptr, &tty_line);
  150. if (!p)
  151. goto noconfig;
  152. cons = console_drivers;
  153. while (cons) {
  154. int idx;
  155. if (cons->device && cons->device(cons, &idx) == p &&
  156. idx == tty_line) {
  157. kgdboc_io_ops.is_console = 1;
  158. break;
  159. }
  160. cons = cons->next;
  161. }
  162. kgdb_tty_driver = p;
  163. kgdb_tty_line = tty_line;
  164. do_register:
  165. err = kgdb_register_io_module(&kgdboc_io_ops);
  166. if (err)
  167. goto noconfig;
  168. err = kgdb_register_nmi_console();
  169. if (err)
  170. goto nmi_con_failed;
  171. configured = 1;
  172. return 0;
  173. nmi_con_failed:
  174. kgdb_unregister_io_module(&kgdboc_io_ops);
  175. noconfig:
  176. kgdboc_unregister_kbd();
  177. config[0] = 0;
  178. configured = 0;
  179. cleanup_kgdboc();
  180. return err;
  181. }
  182. static int __init init_kgdboc(void)
  183. {
  184. /* Already configured? */
  185. if (configured == 1)
  186. return 0;
  187. return configure_kgdboc();
  188. }
  189. static int kgdboc_get_char(void)
  190. {
  191. if (!kgdb_tty_driver)
  192. return -1;
  193. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  194. kgdb_tty_line);
  195. }
  196. static void kgdboc_put_char(u8 chr)
  197. {
  198. if (!kgdb_tty_driver)
  199. return;
  200. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  201. kgdb_tty_line, chr);
  202. }
  203. static int param_set_kgdboc_var(const char *kmessage,
  204. const struct kernel_param *kp)
  205. {
  206. int len = strlen(kmessage);
  207. if (len >= MAX_CONFIG_LEN) {
  208. printk(KERN_ERR "kgdboc: config string too long\n");
  209. return -ENOSPC;
  210. }
  211. /* Only copy in the string if the init function has not run yet */
  212. if (configured < 0) {
  213. strcpy(config, kmessage);
  214. return 0;
  215. }
  216. if (kgdb_connected) {
  217. printk(KERN_ERR
  218. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  219. return -EBUSY;
  220. }
  221. strcpy(config, kmessage);
  222. /* Chop out \n char as a result of echo */
  223. if (config[len - 1] == '\n')
  224. config[len - 1] = '\0';
  225. if (configured == 1)
  226. cleanup_kgdboc();
  227. /* Go and configure with the new params. */
  228. return configure_kgdboc();
  229. }
  230. static int dbg_restore_graphics;
  231. static void kgdboc_pre_exp_handler(void)
  232. {
  233. if (!dbg_restore_graphics && kgdboc_use_kms) {
  234. dbg_restore_graphics = 1;
  235. con_debug_enter(vc_cons[fg_console].d);
  236. }
  237. /* Increment the module count when the debugger is active */
  238. if (!kgdb_connected)
  239. try_module_get(THIS_MODULE);
  240. }
  241. static void kgdboc_post_exp_handler(void)
  242. {
  243. /* decrement the module count when the debugger detaches */
  244. if (!kgdb_connected)
  245. module_put(THIS_MODULE);
  246. if (kgdboc_use_kms && dbg_restore_graphics) {
  247. dbg_restore_graphics = 0;
  248. con_debug_leave();
  249. }
  250. kgdboc_restore_input();
  251. }
  252. static struct kgdb_io kgdboc_io_ops = {
  253. .name = "kgdboc",
  254. .read_char = kgdboc_get_char,
  255. .write_char = kgdboc_put_char,
  256. .pre_exception = kgdboc_pre_exp_handler,
  257. .post_exception = kgdboc_post_exp_handler,
  258. };
  259. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  260. /* This is only available if kgdboc is a built in for early debugging */
  261. static int __init kgdboc_early_init(char *opt)
  262. {
  263. /* save the first character of the config string because the
  264. * init routine can destroy it.
  265. */
  266. char save_ch;
  267. kgdboc_option_setup(opt);
  268. save_ch = config[0];
  269. init_kgdboc();
  270. config[0] = save_ch;
  271. return 0;
  272. }
  273. early_param("ekgdboc", kgdboc_early_init);
  274. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  275. module_init(init_kgdboc);
  276. module_exit(cleanup_kgdboc);
  277. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  278. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  279. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  280. MODULE_LICENSE("GPL");