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