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