Linux Audio

Check our new training course

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