kgdboc.c 17 KB

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