kgdboc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/ctype.h>
  14. #include <linux/kgdb.h>
  15. #include <linux/kdb.h>
  16. #include <linux/tty.h>
  17. #include <linux/console.h>
  18. #include <linux/vt_kern.h>
  19. #include <linux/input.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #define MAX_CONFIG_LEN 40
  23. static struct kgdb_io kgdboc_io_ops;
  24. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  25. static int configured = -1;
  26. static DEFINE_MUTEX(config_mutex);
  27. static char config[MAX_CONFIG_LEN];
  28. static struct kparam_string kps = {
  29. .string = config,
  30. .maxlen = MAX_CONFIG_LEN,
  31. };
  32. static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
  33. static struct tty_driver *kgdb_tty_driver;
  34. static int kgdb_tty_line;
  35. static struct platform_device *kgdboc_pdev;
  36. #ifdef CONFIG_KDB_KEYBOARD
  37. static int kgdboc_reset_connect(struct input_handler *handler,
  38. struct input_dev *dev,
  39. const struct input_device_id *id)
  40. {
  41. input_reset_device(dev);
  42. /* Return an error - we do not want to bind, just to reset */
  43. return -ENODEV;
  44. }
  45. static void kgdboc_reset_disconnect(struct input_handle *handle)
  46. {
  47. /* We do not expect anyone to actually bind to us */
  48. BUG();
  49. }
  50. static const struct input_device_id kgdboc_reset_ids[] = {
  51. {
  52. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  53. .evbit = { BIT_MASK(EV_KEY) },
  54. },
  55. { }
  56. };
  57. static struct input_handler kgdboc_reset_handler = {
  58. .connect = kgdboc_reset_connect,
  59. .disconnect = kgdboc_reset_disconnect,
  60. .name = "kgdboc_reset",
  61. .id_table = kgdboc_reset_ids,
  62. };
  63. static DEFINE_MUTEX(kgdboc_reset_mutex);
  64. static void kgdboc_restore_input_helper(struct work_struct *dummy)
  65. {
  66. /*
  67. * We need to take a mutex to prevent several instances of
  68. * this work running on different CPUs so they don't try
  69. * to register again already registered handler.
  70. */
  71. mutex_lock(&kgdboc_reset_mutex);
  72. if (input_register_handler(&kgdboc_reset_handler) == 0)
  73. input_unregister_handler(&kgdboc_reset_handler);
  74. mutex_unlock(&kgdboc_reset_mutex);
  75. }
  76. static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
  77. static void kgdboc_restore_input(void)
  78. {
  79. if (likely(system_state == SYSTEM_RUNNING))
  80. schedule_work(&kgdboc_restore_input_work);
  81. }
  82. static int kgdboc_register_kbd(char **cptr)
  83. {
  84. if (strncmp(*cptr, "kbd", 3) == 0 ||
  85. strncmp(*cptr, "kdb", 3) == 0) {
  86. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  87. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  88. kdb_poll_idx++;
  89. if (cptr[0][3] == ',')
  90. *cptr += 4;
  91. else
  92. return 1;
  93. }
  94. }
  95. return 0;
  96. }
  97. static void kgdboc_unregister_kbd(void)
  98. {
  99. int i;
  100. for (i = 0; i < kdb_poll_idx; i++) {
  101. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  102. kdb_poll_idx--;
  103. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  104. kdb_poll_funcs[kdb_poll_idx] = NULL;
  105. i--;
  106. }
  107. }
  108. flush_work(&kgdboc_restore_input_work);
  109. }
  110. #else /* ! CONFIG_KDB_KEYBOARD */
  111. #define kgdboc_register_kbd(x) 0
  112. #define kgdboc_unregister_kbd()
  113. #define kgdboc_restore_input()
  114. #endif /* ! CONFIG_KDB_KEYBOARD */
  115. static void cleanup_kgdboc(void)
  116. {
  117. if (configured != 1)
  118. return;
  119. if (kgdb_unregister_nmi_console())
  120. return;
  121. kgdboc_unregister_kbd();
  122. kgdb_unregister_io_module(&kgdboc_io_ops);
  123. }
  124. static int configure_kgdboc(void)
  125. {
  126. struct tty_driver *p;
  127. int tty_line = 0;
  128. int err = -ENODEV;
  129. char *cptr = config;
  130. struct console *cons;
  131. if (!strlen(config) || isspace(config[0])) {
  132. err = 0;
  133. goto noconfig;
  134. }
  135. kgdboc_io_ops.is_console = 0;
  136. kgdb_tty_driver = NULL;
  137. kgdboc_use_kms = 0;
  138. if (strncmp(cptr, "kms,", 4) == 0) {
  139. cptr += 4;
  140. kgdboc_use_kms = 1;
  141. }
  142. if (kgdboc_register_kbd(&cptr))
  143. goto do_register;
  144. p = tty_find_polling_driver(cptr, &tty_line);
  145. if (!p)
  146. goto noconfig;
  147. cons = console_drivers;
  148. while (cons) {
  149. int idx;
  150. if (cons->device && cons->device(cons, &idx) == p &&
  151. idx == tty_line) {
  152. kgdboc_io_ops.is_console = 1;
  153. break;
  154. }
  155. cons = cons->next;
  156. }
  157. kgdb_tty_driver = p;
  158. kgdb_tty_line = tty_line;
  159. do_register:
  160. err = kgdb_register_io_module(&kgdboc_io_ops);
  161. if (err)
  162. goto noconfig;
  163. err = kgdb_register_nmi_console();
  164. if (err)
  165. goto nmi_con_failed;
  166. configured = 1;
  167. return 0;
  168. nmi_con_failed:
  169. kgdb_unregister_io_module(&kgdboc_io_ops);
  170. noconfig:
  171. kgdboc_unregister_kbd();
  172. configured = 0;
  173. return err;
  174. }
  175. static int kgdboc_probe(struct platform_device *pdev)
  176. {
  177. int ret = 0;
  178. mutex_lock(&config_mutex);
  179. if (configured != 1) {
  180. ret = configure_kgdboc();
  181. /* Convert "no device" to "defer" so we'll keep trying */
  182. if (ret == -ENODEV)
  183. ret = -EPROBE_DEFER;
  184. }
  185. mutex_unlock(&config_mutex);
  186. return ret;
  187. }
  188. static struct platform_driver kgdboc_platform_driver = {
  189. .probe = kgdboc_probe,
  190. .driver = {
  191. .name = "kgdboc",
  192. .suppress_bind_attrs = true,
  193. },
  194. };
  195. static int __init init_kgdboc(void)
  196. {
  197. int ret;
  198. /*
  199. * kgdboc is a little bit of an odd "platform_driver". It can be
  200. * up and running long before the platform_driver object is
  201. * created and thus doesn't actually store anything in it. There's
  202. * only one instance of kgdb so anything is stored as global state.
  203. * The platform_driver is only created so that we can leverage the
  204. * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
  205. * underlying tty is ready. Here we init our platform driver and
  206. * then create the single kgdboc instance.
  207. */
  208. ret = platform_driver_register(&kgdboc_platform_driver);
  209. if (ret)
  210. return ret;
  211. kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
  212. if (!kgdboc_pdev) {
  213. ret = -ENOMEM;
  214. goto err_did_register;
  215. }
  216. ret = platform_device_add(kgdboc_pdev);
  217. if (!ret)
  218. return 0;
  219. platform_device_put(kgdboc_pdev);
  220. err_did_register:
  221. platform_driver_unregister(&kgdboc_platform_driver);
  222. return ret;
  223. }
  224. static void exit_kgdboc(void)
  225. {
  226. mutex_lock(&config_mutex);
  227. cleanup_kgdboc();
  228. mutex_unlock(&config_mutex);
  229. platform_device_unregister(kgdboc_pdev);
  230. platform_driver_unregister(&kgdboc_platform_driver);
  231. }
  232. static int kgdboc_get_char(void)
  233. {
  234. if (!kgdb_tty_driver)
  235. return -1;
  236. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  237. kgdb_tty_line);
  238. }
  239. static void kgdboc_put_char(u8 chr)
  240. {
  241. if (!kgdb_tty_driver)
  242. return;
  243. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  244. kgdb_tty_line, chr);
  245. }
  246. static int param_set_kgdboc_var(const char *kmessage,
  247. const struct kernel_param *kp)
  248. {
  249. size_t len = strlen(kmessage);
  250. int ret = 0;
  251. if (len >= MAX_CONFIG_LEN) {
  252. pr_err("config string too long\n");
  253. return -ENOSPC;
  254. }
  255. if (kgdb_connected) {
  256. pr_err("Cannot reconfigure while KGDB is connected.\n");
  257. return -EBUSY;
  258. }
  259. mutex_lock(&config_mutex);
  260. strcpy(config, kmessage);
  261. /* Chop out \n char as a result of echo */
  262. if (len && config[len - 1] == '\n')
  263. config[len - 1] = '\0';
  264. if (configured == 1)
  265. cleanup_kgdboc();
  266. /*
  267. * Configure with the new params as long as init already ran.
  268. * Note that we can get called before init if someone loads us
  269. * with "modprobe kgdboc kgdboc=..." or if they happen to use the
  270. * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
  271. */
  272. if (configured >= 0)
  273. ret = configure_kgdboc();
  274. /*
  275. * If we couldn't configure then clear out the config. Note that
  276. * specifying an invalid config on the kernel command line vs.
  277. * through sysfs have slightly different behaviors. If we fail
  278. * to configure what was specified on the kernel command line
  279. * we'll leave it in the 'config' and return -EPROBE_DEFER from
  280. * our probe. When specified through sysfs userspace is
  281. * responsible for loading the tty driver before setting up.
  282. */
  283. if (ret)
  284. config[0] = '\0';
  285. mutex_unlock(&config_mutex);
  286. return ret;
  287. }
  288. static int dbg_restore_graphics;
  289. static void kgdboc_pre_exp_handler(void)
  290. {
  291. if (!dbg_restore_graphics && kgdboc_use_kms) {
  292. dbg_restore_graphics = 1;
  293. con_debug_enter(vc_cons[fg_console].d);
  294. }
  295. /* Increment the module count when the debugger is active */
  296. if (!kgdb_connected)
  297. try_module_get(THIS_MODULE);
  298. atomic_inc(&ignore_console_lock_warning);
  299. }
  300. static void kgdboc_post_exp_handler(void)
  301. {
  302. atomic_dec(&ignore_console_lock_warning);
  303. /* decrement the module count when the debugger detaches */
  304. if (!kgdb_connected)
  305. module_put(THIS_MODULE);
  306. if (kgdboc_use_kms && dbg_restore_graphics) {
  307. dbg_restore_graphics = 0;
  308. con_debug_leave();
  309. }
  310. kgdboc_restore_input();
  311. }
  312. static struct kgdb_io kgdboc_io_ops = {
  313. .name = "kgdboc",
  314. .read_char = kgdboc_get_char,
  315. .write_char = kgdboc_put_char,
  316. .pre_exception = kgdboc_pre_exp_handler,
  317. .post_exception = kgdboc_post_exp_handler,
  318. };
  319. #ifdef CONFIG_KGDB_SERIAL_CONSOLE
  320. static int kgdboc_option_setup(char *opt)
  321. {
  322. if (!opt) {
  323. pr_err("config string not provided\n");
  324. return -EINVAL;
  325. }
  326. if (strlen(opt) >= MAX_CONFIG_LEN) {
  327. pr_err("config string too long\n");
  328. return -ENOSPC;
  329. }
  330. strcpy(config, opt);
  331. return 0;
  332. }
  333. __setup("kgdboc=", kgdboc_option_setup);
  334. /* This is only available if kgdboc is a built in for early debugging */
  335. static int __init kgdboc_early_init(char *opt)
  336. {
  337. kgdboc_option_setup(opt);
  338. configure_kgdboc();
  339. return 0;
  340. }
  341. early_param("ekgdboc", kgdboc_early_init);
  342. #endif /* CONFIG_KGDB_SERIAL_CONSOLE */
  343. module_init(init_kgdboc);
  344. module_exit(exit_kgdboc);
  345. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  346. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  347. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  348. MODULE_LICENSE("GPL");