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