kgdboc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 <[email protected]>
  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. #include <linux/serial_core.h>
  23. #define MAX_CONFIG_LEN 40
  24. static struct kgdb_io kgdboc_io_ops;
  25. /* -1 = init not run yet, 0 = unconfigured, 1 = configured. */
  26. static int configured = -1;
  27. static DEFINE_MUTEX(config_mutex);
  28. static char config[MAX_CONFIG_LEN];
  29. static struct kparam_string kps = {
  30. .string = config,
  31. .maxlen = MAX_CONFIG_LEN,
  32. };
  33. static int kgdboc_use_kms; /* 1 if we use kernel mode switching */
  34. static struct tty_driver *kgdb_tty_driver;
  35. static int kgdb_tty_line;
  36. static struct platform_device *kgdboc_pdev;
  37. #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
  38. static struct kgdb_io kgdboc_earlycon_io_ops;
  39. static int (*earlycon_orig_exit)(struct console *con);
  40. #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
  41. #ifdef CONFIG_KDB_KEYBOARD
  42. static int kgdboc_reset_connect(struct input_handler *handler,
  43. struct input_dev *dev,
  44. const struct input_device_id *id)
  45. {
  46. input_reset_device(dev);
  47. /* Return an error - we do not want to bind, just to reset */
  48. return -ENODEV;
  49. }
  50. static void kgdboc_reset_disconnect(struct input_handle *handle)
  51. {
  52. /* We do not expect anyone to actually bind to us */
  53. BUG();
  54. }
  55. static const struct input_device_id kgdboc_reset_ids[] = {
  56. {
  57. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  58. .evbit = { BIT_MASK(EV_KEY) },
  59. },
  60. { }
  61. };
  62. static struct input_handler kgdboc_reset_handler = {
  63. .connect = kgdboc_reset_connect,
  64. .disconnect = kgdboc_reset_disconnect,
  65. .name = "kgdboc_reset",
  66. .id_table = kgdboc_reset_ids,
  67. };
  68. static DEFINE_MUTEX(kgdboc_reset_mutex);
  69. static void kgdboc_restore_input_helper(struct work_struct *dummy)
  70. {
  71. /*
  72. * We need to take a mutex to prevent several instances of
  73. * this work running on different CPUs so they don't try
  74. * to register again already registered handler.
  75. */
  76. mutex_lock(&kgdboc_reset_mutex);
  77. if (input_register_handler(&kgdboc_reset_handler) == 0)
  78. input_unregister_handler(&kgdboc_reset_handler);
  79. mutex_unlock(&kgdboc_reset_mutex);
  80. }
  81. static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
  82. static void kgdboc_restore_input(void)
  83. {
  84. if (likely(system_state == SYSTEM_RUNNING))
  85. schedule_work(&kgdboc_restore_input_work);
  86. }
  87. static int kgdboc_register_kbd(char **cptr)
  88. {
  89. if (strncmp(*cptr, "kbd", 3) == 0 ||
  90. strncmp(*cptr, "kdb", 3) == 0) {
  91. if (kdb_poll_idx < KDB_POLL_FUNC_MAX) {
  92. kdb_poll_funcs[kdb_poll_idx] = kdb_get_kbd_char;
  93. kdb_poll_idx++;
  94. if (cptr[0][3] == ',')
  95. *cptr += 4;
  96. else
  97. return 1;
  98. }
  99. }
  100. return 0;
  101. }
  102. static void kgdboc_unregister_kbd(void)
  103. {
  104. int i;
  105. for (i = 0; i < kdb_poll_idx; i++) {
  106. if (kdb_poll_funcs[i] == kdb_get_kbd_char) {
  107. kdb_poll_idx--;
  108. kdb_poll_funcs[i] = kdb_poll_funcs[kdb_poll_idx];
  109. kdb_poll_funcs[kdb_poll_idx] = NULL;
  110. i--;
  111. }
  112. }
  113. flush_work(&kgdboc_restore_input_work);
  114. }
  115. #else /* ! CONFIG_KDB_KEYBOARD */
  116. #define kgdboc_register_kbd(x) 0
  117. #define kgdboc_unregister_kbd()
  118. #define kgdboc_restore_input()
  119. #endif /* ! CONFIG_KDB_KEYBOARD */
  120. #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
  121. static void cleanup_earlycon(void)
  122. {
  123. if (kgdboc_earlycon_io_ops.cons)
  124. kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
  125. }
  126. #else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
  127. static inline void cleanup_earlycon(void) { }
  128. #endif /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
  129. static void cleanup_kgdboc(void)
  130. {
  131. cleanup_earlycon();
  132. if (configured != 1)
  133. return;
  134. if (kgdb_unregister_nmi_console())
  135. return;
  136. kgdboc_unregister_kbd();
  137. kgdb_unregister_io_module(&kgdboc_io_ops);
  138. }
  139. static int configure_kgdboc(void)
  140. {
  141. struct tty_driver *p;
  142. int tty_line = 0;
  143. int err = -ENODEV;
  144. char *cptr = config;
  145. struct console *cons;
  146. if (!strlen(config) || isspace(config[0])) {
  147. err = 0;
  148. goto noconfig;
  149. }
  150. kgdboc_io_ops.cons = NULL;
  151. kgdb_tty_driver = NULL;
  152. kgdboc_use_kms = 0;
  153. if (strncmp(cptr, "kms,", 4) == 0) {
  154. cptr += 4;
  155. kgdboc_use_kms = 1;
  156. }
  157. if (kgdboc_register_kbd(&cptr))
  158. goto do_register;
  159. p = tty_find_polling_driver(cptr, &tty_line);
  160. if (!p)
  161. goto noconfig;
  162. for_each_console(cons) {
  163. int idx;
  164. if (cons->device && cons->device(cons, &idx) == p &&
  165. idx == tty_line) {
  166. kgdboc_io_ops.cons = cons;
  167. break;
  168. }
  169. }
  170. kgdb_tty_driver = p;
  171. kgdb_tty_line = tty_line;
  172. do_register:
  173. err = kgdb_register_io_module(&kgdboc_io_ops);
  174. if (err)
  175. goto noconfig;
  176. err = kgdb_register_nmi_console();
  177. if (err)
  178. goto nmi_con_failed;
  179. configured = 1;
  180. return 0;
  181. nmi_con_failed:
  182. kgdb_unregister_io_module(&kgdboc_io_ops);
  183. noconfig:
  184. kgdboc_unregister_kbd();
  185. configured = 0;
  186. return err;
  187. }
  188. static int kgdboc_probe(struct platform_device *pdev)
  189. {
  190. int ret = 0;
  191. mutex_lock(&config_mutex);
  192. if (configured != 1) {
  193. ret = configure_kgdboc();
  194. /* Convert "no device" to "defer" so we'll keep trying */
  195. if (ret == -ENODEV)
  196. ret = -EPROBE_DEFER;
  197. }
  198. mutex_unlock(&config_mutex);
  199. return ret;
  200. }
  201. static struct platform_driver kgdboc_platform_driver = {
  202. .probe = kgdboc_probe,
  203. .driver = {
  204. .name = "kgdboc",
  205. .suppress_bind_attrs = true,
  206. },
  207. };
  208. static int __init init_kgdboc(void)
  209. {
  210. int ret;
  211. /*
  212. * kgdboc is a little bit of an odd "platform_driver". It can be
  213. * up and running long before the platform_driver object is
  214. * created and thus doesn't actually store anything in it. There's
  215. * only one instance of kgdb so anything is stored as global state.
  216. * The platform_driver is only created so that we can leverage the
  217. * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
  218. * underlying tty is ready. Here we init our platform driver and
  219. * then create the single kgdboc instance.
  220. */
  221. ret = platform_driver_register(&kgdboc_platform_driver);
  222. if (ret)
  223. return ret;
  224. kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
  225. if (!kgdboc_pdev) {
  226. ret = -ENOMEM;
  227. goto err_did_register;
  228. }
  229. ret = platform_device_add(kgdboc_pdev);
  230. if (!ret)
  231. return 0;
  232. platform_device_put(kgdboc_pdev);
  233. err_did_register:
  234. platform_driver_unregister(&kgdboc_platform_driver);
  235. return ret;
  236. }
  237. static void exit_kgdboc(void)
  238. {
  239. mutex_lock(&config_mutex);
  240. cleanup_kgdboc();
  241. mutex_unlock(&config_mutex);
  242. platform_device_unregister(kgdboc_pdev);
  243. platform_driver_unregister(&kgdboc_platform_driver);
  244. }
  245. static int kgdboc_get_char(void)
  246. {
  247. if (!kgdb_tty_driver)
  248. return -1;
  249. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  250. kgdb_tty_line);
  251. }
  252. static void kgdboc_put_char(u8 chr)
  253. {
  254. if (!kgdb_tty_driver)
  255. return;
  256. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  257. kgdb_tty_line, chr);
  258. }
  259. static int param_set_kgdboc_var(const char *kmessage,
  260. const struct kernel_param *kp)
  261. {
  262. size_t len = strlen(kmessage);
  263. int ret = 0;
  264. if (len >= MAX_CONFIG_LEN) {
  265. pr_err("config string too long\n");
  266. return -ENOSPC;
  267. }
  268. if (kgdb_connected) {
  269. pr_err("Cannot reconfigure while KGDB is connected.\n");
  270. return -EBUSY;
  271. }
  272. mutex_lock(&config_mutex);
  273. strcpy(config, kmessage);
  274. /* Chop out \n char as a result of echo */
  275. if (len && config[len - 1] == '\n')
  276. config[len - 1] = '\0';
  277. if (configured == 1)
  278. cleanup_kgdboc();
  279. /*
  280. * Configure with the new params as long as init already ran.
  281. * Note that we can get called before init if someone loads us
  282. * with "modprobe kgdboc kgdboc=..." or if they happen to use
  283. * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
  284. */
  285. if (configured >= 0)
  286. ret = configure_kgdboc();
  287. /*
  288. * If we couldn't configure then clear out the config. Note that
  289. * specifying an invalid config on the kernel command line vs.
  290. * through sysfs have slightly different behaviors. If we fail
  291. * to configure what was specified on the kernel command line
  292. * we'll leave it in the 'config' and return -EPROBE_DEFER from
  293. * our probe. When specified through sysfs userspace is
  294. * responsible for loading the tty driver before setting up.
  295. */
  296. if (ret)
  297. config[0] = '\0';
  298. mutex_unlock(&config_mutex);
  299. return ret;
  300. }
  301. static int dbg_restore_graphics;
  302. static void kgdboc_pre_exp_handler(void)
  303. {
  304. if (!dbg_restore_graphics && kgdboc_use_kms) {
  305. dbg_restore_graphics = 1;
  306. con_debug_enter(vc_cons[fg_console].d);
  307. }
  308. /* Increment the module count when the debugger is active */
  309. if (!kgdb_connected)
  310. try_module_get(THIS_MODULE);
  311. }
  312. static void kgdboc_post_exp_handler(void)
  313. {
  314. /* decrement the module count when the debugger detaches */
  315. if (!kgdb_connected)
  316. module_put(THIS_MODULE);
  317. if (kgdboc_use_kms && dbg_restore_graphics) {
  318. dbg_restore_graphics = 0;
  319. con_debug_leave();
  320. }
  321. kgdboc_restore_input();
  322. }
  323. static struct kgdb_io kgdboc_io_ops = {
  324. .name = "kgdboc",
  325. .read_char = kgdboc_get_char,
  326. .write_char = kgdboc_put_char,
  327. .pre_exception = kgdboc_pre_exp_handler,
  328. .post_exception = kgdboc_post_exp_handler,
  329. };
  330. #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
  331. static int kgdboc_option_setup(char *opt)
  332. {
  333. if (!opt) {
  334. pr_err("config string not provided\n");
  335. return 1;
  336. }
  337. if (strlen(opt) >= MAX_CONFIG_LEN) {
  338. pr_err("config string too long\n");
  339. return 1;
  340. }
  341. strcpy(config, opt);
  342. return 1;
  343. }
  344. __setup("kgdboc=", kgdboc_option_setup);
  345. /* This is only available if kgdboc is a built in for early debugging */
  346. static int __init kgdboc_early_init(char *opt)
  347. {
  348. kgdboc_option_setup(opt);
  349. configure_kgdboc();
  350. return 0;
  351. }
  352. early_param("ekgdboc", kgdboc_early_init);
  353. static int kgdboc_earlycon_get_char(void)
  354. {
  355. char c;
  356. if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
  357. &c, 1))
  358. return NO_POLL_CHAR;
  359. return c;
  360. }
  361. static void kgdboc_earlycon_put_char(u8 chr)
  362. {
  363. kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
  364. 1);
  365. }
  366. static void kgdboc_earlycon_pre_exp_handler(void)
  367. {
  368. struct console *con;
  369. static bool already_warned;
  370. if (already_warned)
  371. return;
  372. /*
  373. * When the first normal console comes up the kernel will take all
  374. * the boot consoles out of the list. Really, we should stop using
  375. * the boot console when it does that but until a TTY is registered
  376. * we have no other choice so we keep using it. Since not all
  377. * serial drivers might be OK with this, print a warning once per
  378. * boot if we detect this case.
  379. */
  380. for_each_console(con)
  381. if (con == kgdboc_earlycon_io_ops.cons)
  382. return;
  383. already_warned = true;
  384. pr_warn("kgdboc_earlycon is still using bootconsole\n");
  385. }
  386. static int kgdboc_earlycon_deferred_exit(struct console *con)
  387. {
  388. /*
  389. * If we get here it means the boot console is going away but we
  390. * don't yet have a suitable replacement. Don't pass through to
  391. * the original exit routine. We'll call it later in our deinit()
  392. * function. For now, restore the original exit() function pointer
  393. * as a sentinal that we've hit this point.
  394. */
  395. con->exit = earlycon_orig_exit;
  396. return 0;
  397. }
  398. static void kgdboc_earlycon_deinit(void)
  399. {
  400. if (!kgdboc_earlycon_io_ops.cons)
  401. return;
  402. if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
  403. /*
  404. * kgdboc_earlycon is exiting but original boot console exit
  405. * was never called (AKA kgdboc_earlycon_deferred_exit()
  406. * didn't ever run). Undo our trap.
  407. */
  408. kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
  409. else if (kgdboc_earlycon_io_ops.cons->exit)
  410. /*
  411. * We skipped calling the exit() routine so we could try to
  412. * keep using the boot console even after it went away. We're
  413. * finally done so call the function now.
  414. */
  415. kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
  416. kgdboc_earlycon_io_ops.cons = NULL;
  417. }
  418. static struct kgdb_io kgdboc_earlycon_io_ops = {
  419. .name = "kgdboc_earlycon",
  420. .read_char = kgdboc_earlycon_get_char,
  421. .write_char = kgdboc_earlycon_put_char,
  422. .pre_exception = kgdboc_earlycon_pre_exp_handler,
  423. .deinit = kgdboc_earlycon_deinit,
  424. };
  425. #define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
  426. static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
  427. static bool kgdboc_earlycon_late_enable __initdata;
  428. static int __init kgdboc_earlycon_init(char *opt)
  429. {
  430. struct console *con;
  431. kdb_init(KDB_INIT_EARLY);
  432. /*
  433. * Look for a matching console, or if the name was left blank just
  434. * pick the first one we find.
  435. */
  436. console_lock();
  437. for_each_console(con) {
  438. if (con->write && con->read &&
  439. (con->flags & (CON_BOOT | CON_ENABLED)) &&
  440. (!opt || !opt[0] || strcmp(con->name, opt) == 0))
  441. break;
  442. }
  443. if (!con) {
  444. /*
  445. * Both earlycon and kgdboc_earlycon are initialized during
  446. * early parameter parsing. We cannot guarantee earlycon gets
  447. * in first and, in any case, on ACPI systems earlycon may
  448. * defer its own initialization (usually to somewhere within
  449. * setup_arch() ). To cope with either of these situations
  450. * we can defer our own initialization to a little later in
  451. * the boot.
  452. */
  453. if (!kgdboc_earlycon_late_enable) {
  454. pr_info("No suitable earlycon yet, will try later\n");
  455. if (opt)
  456. strscpy(kgdboc_earlycon_param, opt,
  457. sizeof(kgdboc_earlycon_param));
  458. kgdboc_earlycon_late_enable = true;
  459. } else {
  460. pr_info("Couldn't find kgdb earlycon\n");
  461. }
  462. goto unlock;
  463. }
  464. kgdboc_earlycon_io_ops.cons = con;
  465. pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
  466. if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
  467. kgdboc_earlycon_io_ops.cons = NULL;
  468. pr_info("Failed to register kgdb with earlycon\n");
  469. } else {
  470. /* Trap exit so we can keep earlycon longer if needed. */
  471. earlycon_orig_exit = con->exit;
  472. con->exit = kgdboc_earlycon_deferred_exit;
  473. }
  474. unlock:
  475. console_unlock();
  476. /* Non-zero means malformed option so we always return zero */
  477. return 0;
  478. }
  479. early_param("kgdboc_earlycon", kgdboc_earlycon_init);
  480. /*
  481. * This is only intended for the late adoption of an early console.
  482. *
  483. * It is not a reliable way to adopt regular consoles because we can not
  484. * control what order console initcalls are made and, in any case, many
  485. * regular consoles are registered much later in the boot process than
  486. * the console initcalls!
  487. */
  488. static int __init kgdboc_earlycon_late_init(void)
  489. {
  490. if (kgdboc_earlycon_late_enable)
  491. kgdboc_earlycon_init(kgdboc_earlycon_param);
  492. return 0;
  493. }
  494. console_initcall(kgdboc_earlycon_late_init);
  495. #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
  496. module_init(init_kgdboc);
  497. module_exit(exit_kgdboc);
  498. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  499. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  500. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  501. MODULE_LICENSE("GPL");