kgdboc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. kgdboc_unregister_kbd();
  161. kgdb_unregister_io_module(&kgdboc_io_ops);
  162. }
  163. static int configure_kgdboc(void)
  164. {
  165. struct tty_driver *p;
  166. int tty_line = 0;
  167. int err = -ENODEV;
  168. char *cptr = config;
  169. struct console *cons;
  170. int cookie;
  171. if (!strlen(config) || isspace(config[0])) {
  172. err = 0;
  173. goto noconfig;
  174. }
  175. kgdboc_io_ops.cons = NULL;
  176. kgdb_tty_driver = NULL;
  177. kgdboc_use_kms = 0;
  178. if (strncmp(cptr, "kms,", 4) == 0) {
  179. cptr += 4;
  180. kgdboc_use_kms = 1;
  181. }
  182. if (kgdboc_register_kbd(&cptr))
  183. goto do_register;
  184. p = tty_find_polling_driver(cptr, &tty_line);
  185. if (!p)
  186. goto noconfig;
  187. /*
  188. * Take console_lock to serialize device() callback with
  189. * other console operations. For example, fg_console is
  190. * modified under console_lock when switching vt.
  191. */
  192. console_lock();
  193. cookie = console_srcu_read_lock();
  194. for_each_console_srcu(cons) {
  195. int idx;
  196. if (cons->device && cons->device(cons, &idx) == p &&
  197. idx == tty_line) {
  198. kgdboc_io_ops.cons = cons;
  199. break;
  200. }
  201. }
  202. console_srcu_read_unlock(cookie);
  203. console_unlock();
  204. kgdb_tty_driver = p;
  205. kgdb_tty_line = tty_line;
  206. do_register:
  207. err = kgdb_register_io_module(&kgdboc_io_ops);
  208. if (err)
  209. goto noconfig;
  210. configured = 1;
  211. return 0;
  212. noconfig:
  213. kgdboc_unregister_kbd();
  214. configured = 0;
  215. return err;
  216. }
  217. static int kgdboc_probe(struct platform_device *pdev)
  218. {
  219. int ret = 0;
  220. mutex_lock(&config_mutex);
  221. if (configured != 1) {
  222. ret = configure_kgdboc();
  223. /* Convert "no device" to "defer" so we'll keep trying */
  224. if (ret == -ENODEV)
  225. ret = -EPROBE_DEFER;
  226. }
  227. mutex_unlock(&config_mutex);
  228. return ret;
  229. }
  230. static struct platform_driver kgdboc_platform_driver = {
  231. .probe = kgdboc_probe,
  232. .driver = {
  233. .name = "kgdboc",
  234. .suppress_bind_attrs = true,
  235. },
  236. };
  237. static int __init init_kgdboc(void)
  238. {
  239. int ret;
  240. /*
  241. * kgdboc is a little bit of an odd "platform_driver". It can be
  242. * up and running long before the platform_driver object is
  243. * created and thus doesn't actually store anything in it. There's
  244. * only one instance of kgdb so anything is stored as global state.
  245. * The platform_driver is only created so that we can leverage the
  246. * kernel's mechanisms (like -EPROBE_DEFER) to call us when our
  247. * underlying tty is ready. Here we init our platform driver and
  248. * then create the single kgdboc instance.
  249. */
  250. ret = platform_driver_register(&kgdboc_platform_driver);
  251. if (ret)
  252. return ret;
  253. kgdboc_pdev = platform_device_alloc("kgdboc", PLATFORM_DEVID_NONE);
  254. if (!kgdboc_pdev) {
  255. ret = -ENOMEM;
  256. goto err_did_register;
  257. }
  258. ret = platform_device_add(kgdboc_pdev);
  259. if (!ret)
  260. return 0;
  261. platform_device_put(kgdboc_pdev);
  262. err_did_register:
  263. platform_driver_unregister(&kgdboc_platform_driver);
  264. return ret;
  265. }
  266. static void exit_kgdboc(void)
  267. {
  268. mutex_lock(&config_mutex);
  269. cleanup_kgdboc();
  270. mutex_unlock(&config_mutex);
  271. platform_device_unregister(kgdboc_pdev);
  272. platform_driver_unregister(&kgdboc_platform_driver);
  273. }
  274. static int kgdboc_get_char(void)
  275. {
  276. if (!kgdb_tty_driver)
  277. return -1;
  278. return kgdb_tty_driver->ops->poll_get_char(kgdb_tty_driver,
  279. kgdb_tty_line);
  280. }
  281. static void kgdboc_put_char(u8 chr)
  282. {
  283. if (!kgdb_tty_driver)
  284. return;
  285. kgdb_tty_driver->ops->poll_put_char(kgdb_tty_driver,
  286. kgdb_tty_line, chr);
  287. }
  288. static int param_set_kgdboc_var(const char *kmessage,
  289. const struct kernel_param *kp)
  290. {
  291. size_t len = strlen(kmessage);
  292. int ret = 0;
  293. if (len >= MAX_CONFIG_LEN) {
  294. pr_err("config string too long\n");
  295. return -ENOSPC;
  296. }
  297. if (kgdb_connected) {
  298. pr_err("Cannot reconfigure while KGDB is connected.\n");
  299. return -EBUSY;
  300. }
  301. mutex_lock(&config_mutex);
  302. strcpy(config, kmessage);
  303. /* Chop out \n char as a result of echo */
  304. if (len && config[len - 1] == '\n')
  305. config[len - 1] = '\0';
  306. if (configured == 1)
  307. cleanup_kgdboc();
  308. /*
  309. * Configure with the new params as long as init already ran.
  310. * Note that we can get called before init if someone loads us
  311. * with "modprobe kgdboc kgdboc=..." or if they happen to use
  312. * the odd syntax of "kgdboc.kgdboc=..." on the kernel command.
  313. */
  314. if (configured >= 0)
  315. ret = configure_kgdboc();
  316. /*
  317. * If we couldn't configure then clear out the config. Note that
  318. * specifying an invalid config on the kernel command line vs.
  319. * through sysfs have slightly different behaviors. If we fail
  320. * to configure what was specified on the kernel command line
  321. * we'll leave it in the 'config' and return -EPROBE_DEFER from
  322. * our probe. When specified through sysfs userspace is
  323. * responsible for loading the tty driver before setting up.
  324. */
  325. if (ret)
  326. config[0] = '\0';
  327. mutex_unlock(&config_mutex);
  328. return ret;
  329. }
  330. static int dbg_restore_graphics;
  331. static void kgdboc_pre_exp_handler(void)
  332. {
  333. if (!dbg_restore_graphics && kgdboc_use_kms) {
  334. dbg_restore_graphics = 1;
  335. con_debug_enter(vc_cons[fg_console].d);
  336. }
  337. /* Increment the module count when the debugger is active */
  338. if (!kgdb_connected)
  339. try_module_get(THIS_MODULE);
  340. }
  341. static void kgdboc_post_exp_handler(void)
  342. {
  343. /* decrement the module count when the debugger detaches */
  344. if (!kgdb_connected)
  345. module_put(THIS_MODULE);
  346. if (kgdboc_use_kms && dbg_restore_graphics) {
  347. dbg_restore_graphics = 0;
  348. con_debug_leave();
  349. }
  350. kgdboc_restore_input();
  351. }
  352. static struct kgdb_io kgdboc_io_ops = {
  353. .name = "kgdboc",
  354. .read_char = kgdboc_get_char,
  355. .write_char = kgdboc_put_char,
  356. .pre_exception = kgdboc_pre_exp_handler,
  357. .post_exception = kgdboc_post_exp_handler,
  358. };
  359. #if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
  360. static int kgdboc_option_setup(char *opt)
  361. {
  362. if (!opt) {
  363. pr_err("config string not provided\n");
  364. return 1;
  365. }
  366. if (strlen(opt) >= MAX_CONFIG_LEN) {
  367. pr_err("config string too long\n");
  368. return 1;
  369. }
  370. strcpy(config, opt);
  371. return 1;
  372. }
  373. __setup("kgdboc=", kgdboc_option_setup);
  374. /* This is only available if kgdboc is a built in for early debugging */
  375. static int __init kgdboc_early_init(char *opt)
  376. {
  377. kgdboc_option_setup(opt);
  378. configure_kgdboc();
  379. return 0;
  380. }
  381. early_param("ekgdboc", kgdboc_early_init);
  382. static int kgdboc_earlycon_get_char(void)
  383. {
  384. char c;
  385. if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
  386. &c, 1))
  387. return NO_POLL_CHAR;
  388. return c;
  389. }
  390. static void kgdboc_earlycon_put_char(u8 chr)
  391. {
  392. kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
  393. 1);
  394. }
  395. static void kgdboc_earlycon_pre_exp_handler(void)
  396. {
  397. struct console *con;
  398. static bool already_warned;
  399. int cookie;
  400. if (already_warned)
  401. return;
  402. /*
  403. * When the first normal console comes up the kernel will take all
  404. * the boot consoles out of the list. Really, we should stop using
  405. * the boot console when it does that but until a TTY is registered
  406. * we have no other choice so we keep using it. Since not all
  407. * serial drivers might be OK with this, print a warning once per
  408. * boot if we detect this case.
  409. */
  410. cookie = console_srcu_read_lock();
  411. for_each_console_srcu(con) {
  412. if (con == kgdboc_earlycon_io_ops.cons)
  413. break;
  414. }
  415. console_srcu_read_unlock(cookie);
  416. if (con)
  417. return;
  418. already_warned = true;
  419. pr_warn("kgdboc_earlycon is still using bootconsole\n");
  420. }
  421. static int kgdboc_earlycon_deferred_exit(struct console *con)
  422. {
  423. /*
  424. * If we get here it means the boot console is going away but we
  425. * don't yet have a suitable replacement. Don't pass through to
  426. * the original exit routine. We'll call it later in our deinit()
  427. * function. For now, restore the original exit() function pointer
  428. * as a sentinal that we've hit this point.
  429. */
  430. con->exit = earlycon_orig_exit;
  431. return 0;
  432. }
  433. static void kgdboc_earlycon_deinit(void)
  434. {
  435. if (!kgdboc_earlycon_io_ops.cons)
  436. return;
  437. if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
  438. /*
  439. * kgdboc_earlycon is exiting but original boot console exit
  440. * was never called (AKA kgdboc_earlycon_deferred_exit()
  441. * didn't ever run). Undo our trap.
  442. */
  443. kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
  444. else if (kgdboc_earlycon_io_ops.cons->exit)
  445. /*
  446. * We skipped calling the exit() routine so we could try to
  447. * keep using the boot console even after it went away. We're
  448. * finally done so call the function now.
  449. */
  450. kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
  451. kgdboc_earlycon_io_ops.cons = NULL;
  452. }
  453. static struct kgdb_io kgdboc_earlycon_io_ops = {
  454. .name = "kgdboc_earlycon",
  455. .read_char = kgdboc_earlycon_get_char,
  456. .write_char = kgdboc_earlycon_put_char,
  457. .pre_exception = kgdboc_earlycon_pre_exp_handler,
  458. .deinit = kgdboc_earlycon_deinit,
  459. };
  460. #define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
  461. static char kgdboc_earlycon_param[MAX_CONSOLE_NAME_LEN] __initdata;
  462. static bool kgdboc_earlycon_late_enable __initdata;
  463. static int __init kgdboc_earlycon_init(char *opt)
  464. {
  465. struct console *con;
  466. kdb_init(KDB_INIT_EARLY);
  467. /*
  468. * Look for a matching console, or if the name was left blank just
  469. * pick the first one we find.
  470. */
  471. /*
  472. * Hold the console_list_lock to guarantee that no consoles are
  473. * unregistered until the kgdboc_earlycon setup is complete.
  474. * Trapping the exit() callback relies on exit() not being
  475. * called until the trap is setup. This also allows safe
  476. * traversal of the console list and race-free reading of @flags.
  477. */
  478. console_list_lock();
  479. for_each_console(con) {
  480. if (con->write && con->read &&
  481. (!opt || !opt[0] || strcmp(con->name, opt) == 0))
  482. break;
  483. }
  484. if (!con) {
  485. /*
  486. * Both earlycon and kgdboc_earlycon are initialized during
  487. * early parameter parsing. We cannot guarantee earlycon gets
  488. * in first and, in any case, on ACPI systems earlycon may
  489. * defer its own initialization (usually to somewhere within
  490. * setup_arch() ). To cope with either of these situations
  491. * we can defer our own initialization to a little later in
  492. * the boot.
  493. */
  494. if (!kgdboc_earlycon_late_enable) {
  495. pr_info("No suitable earlycon yet, will try later\n");
  496. if (opt)
  497. strscpy(kgdboc_earlycon_param, opt,
  498. sizeof(kgdboc_earlycon_param));
  499. kgdboc_earlycon_late_enable = true;
  500. } else {
  501. pr_info("Couldn't find kgdb earlycon\n");
  502. }
  503. goto unlock;
  504. }
  505. kgdboc_earlycon_io_ops.cons = con;
  506. pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
  507. if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
  508. kgdboc_earlycon_io_ops.cons = NULL;
  509. pr_info("Failed to register kgdb with earlycon\n");
  510. } else {
  511. /* Trap exit so we can keep earlycon longer if needed. */
  512. earlycon_orig_exit = con->exit;
  513. con->exit = kgdboc_earlycon_deferred_exit;
  514. }
  515. unlock:
  516. console_list_unlock();
  517. /* Non-zero means malformed option so we always return zero */
  518. return 0;
  519. }
  520. early_param("kgdboc_earlycon", kgdboc_earlycon_init);
  521. /*
  522. * This is only intended for the late adoption of an early console.
  523. *
  524. * It is not a reliable way to adopt regular consoles because we can not
  525. * control what order console initcalls are made and, in any case, many
  526. * regular consoles are registered much later in the boot process than
  527. * the console initcalls!
  528. */
  529. static int __init kgdboc_earlycon_late_init(void)
  530. {
  531. if (kgdboc_earlycon_late_enable)
  532. kgdboc_earlycon_init(kgdboc_earlycon_param);
  533. return 0;
  534. }
  535. console_initcall(kgdboc_earlycon_late_init);
  536. #endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
  537. module_init(init_kgdboc);
  538. module_exit(exit_kgdboc);
  539. module_param_call(kgdboc, param_set_kgdboc_var, param_get_string, &kps, 0644);
  540. MODULE_PARM_DESC(kgdboc, "<serial_device>[,baud]");
  541. MODULE_DESCRIPTION("KGDB Console TTY Driver");
  542. MODULE_LICENSE("GPL");