kgdboc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 void cleanup_kgdboc(void)
  112. {
  113. if (kgdb_unregister_nmi_console())
  114. return;
  115. kgdboc_unregister_kbd();
  116. if (configured == 1)
  117. kgdb_unregister_io_module(&kgdboc_io_ops);
  118. }
  119. static int configure_kgdboc(void)
  120. {
  121. struct tty_driver *p;
  122. int tty_line = 0;
  123. int err = -ENODEV;
  124. char *cptr = config;
  125. struct console *cons;
  126. if (!strlen(config) || isspace(config[0])) {
  127. err = 0;
  128. goto noconfig;
  129. }
  130. kgdboc_io_ops.is_console = 0;
  131. kgdb_tty_driver = NULL;
  132. kgdboc_use_kms = 0;
  133. if (strncmp(cptr, "kms,", 4) == 0) {
  134. cptr += 4;
  135. kgdboc_use_kms = 1;
  136. }
  137. if (kgdboc_register_kbd(&cptr))
  138. goto do_register;
  139. p = tty_find_polling_driver(cptr, &tty_line);
  140. if (!p)
  141. goto noconfig;
  142. cons = console_drivers;
  143. while (cons) {
  144. int idx;
  145. if (cons->device && cons->device(cons, &idx) == p &&
  146. idx == tty_line) {
  147. kgdboc_io_ops.is_console = 1;
  148. break;
  149. }
  150. cons = cons->next;
  151. }
  152. kgdb_tty_driver = p;
  153. kgdb_tty_line = tty_line;
  154. do_register:
  155. err = kgdb_register_io_module(&kgdboc_io_ops);
  156. if (err)
  157. goto noconfig;
  158. err = kgdb_register_nmi_console();
  159. if (err)
  160. goto nmi_con_failed;
  161. configured = 1;
  162. return 0;
  163. nmi_con_failed:
  164. kgdb_unregister_io_module(&kgdboc_io_ops);
  165. noconfig:
  166. kgdboc_unregister_kbd();
  167. config[0] = 0;
  168. configured = 0;
  169. cleanup_kgdboc();
  170. return err;
  171. }
  172. static int __init init_kgdboc(void)
  173. {
  174. /* Already configured? */
  175. if (configured == 1)
  176. return 0;
  177. return configure_kgdboc();
  178. }
  179. static int kgdboc_get_char(void)
  180. {
  181. if (!kgdb_tty_driver)
  182. return -1;
  183. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  184. kgdb_tty_line);
  185. }
  186. static void kgdboc_put_char(u8 chr)
  187. {
  188. if (!kgdb_tty_driver)
  189. return;
  190. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  191. kgdb_tty_line, chr);
  192. }
  193. static int param_set_kgdboc_var(const char *kmessage,
  194. const struct kernel_param *kp)
  195. {
  196. size_t len = strlen(kmessage);
  197. if (len >= MAX_CONFIG_LEN) {
  198. printk(KERN_ERR "kgdboc: config string too long\n");
  199. return -ENOSPC;
  200. }
  201. /* Only copy in the string if the init function has not run yet */
  202. if (configured < 0) {
  203. strcpy(config, kmessage);
  204. return 0;
  205. }
  206. if (kgdb_connected) {
  207. printk(KERN_ERR
  208. "kgdboc: Cannot reconfigure while KGDB is connected.\n");
  209. return -EBUSY;
  210. }
  211. strcpy(config, kmessage);
  212. /* Chop out \n char as a result of echo */
  213. if (len && config[len - 1] == '\n')
  214. config[len - 1] = '\0';
  215. if (configured == 1)
  216. cleanup_kgdboc();
  217. /* Go and configure with the new params. */
  218. return configure_kgdboc();
  219. }
  220. static int dbg_restore_graphics;
  221. static void kgdboc_pre_exp_handler(void)
  222. {
  223. if (!dbg_restore_graphics && kgdboc_use_kms) {
  224. dbg_restore_graphics = 1;
  225. con_debug_enter(vc_cons[fg_console].d);
  226. }
  227. /* Increment the module count when the debugger is active */
  228. if (!kgdb_connected)
  229. try_module_get(THIS_MODULE);
  230. }
  231. static void kgdboc_post_exp_handler(void)
  232. {
  233. /* decrement the module count when the debugger detaches */
  234. if (!kgdb_connected)
  235. module_put(THIS_MODULE);
  236. if (kgdboc_use_kms && dbg_restore_graphics) {
  237. dbg_restore_graphics = 0;
  238. con_debug_leave();
  239. }
  240. kgdboc_restore_input();
  241. }
  242. static struct kgdb_io kgdboc_io_ops = {
  243. .name = "kgdboc",
  244. .read_char = kgdboc_get_char,
  245. .write_char = kgdboc_put_char,
  246. .pre_exception = kgdboc_pre_exp_handler,
  247. .post_exception = kgdboc_post_exp_handler,
  248. };
  249. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  250. static int kgdboc_option_setup(char *opt)
  251. {
  252. if (!opt) {
  253. pr_err("config string not provided\n");
  254. return -EINVAL;
  255. }
  256. if (strlen(opt) >= MAX_CONFIG_LEN) {
  257. pr_err("config string too long\n");
  258. return -ENOSPC;
  259. }
  260. strcpy(config, opt);
  261. return 0;
  262. }
  263. __setup("kgdboc=", kgdboc_option_setup);
  264. /* This is only available if kgdboc is a built in for early debugging */
  265. static int __init kgdboc_early_init(char *opt)
  266. {
  267. /* save the first character of the config string because the
  268. * init routine can destroy it.
  269. */
  270. char save_ch;
  271. kgdboc_option_setup(opt);
  272. save_ch = config[0];
  273. init_kgdboc();
  274. config[0] = save_ch;
  275. return 0;
  276. }
  277. early_param("ekgdboc", kgdboc_early_init);
  278. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  279. module_init(init_kgdboc);
  280. module_exit(cleanup_kgdboc);
  281. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  282. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  283. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  284. MODULE_LICENSE("GPL");