Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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