Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * KGDB NMI serial console
  4 *
  5 * Copyright 2010 Google, Inc.
  6 *		  Arve Hjønnevåg <arve@android.com>
  7 *		  Colin Cross <ccross@android.com>
  8 * Copyright 2012 Linaro Ltd.
  9 *		  Anton Vorontsov <anton.vorontsov@linaro.org>
 
 
 
 
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/compiler.h>
 15#include <linux/slab.h>
 16#include <linux/errno.h>
 17#include <linux/atomic.h>
 18#include <linux/console.h>
 19#include <linux/tty.h>
 20#include <linux/tty_driver.h>
 21#include <linux/tty_flip.h>
 22#include <linux/serial_core.h>
 23#include <linux/interrupt.h>
 24#include <linux/hrtimer.h>
 25#include <linux/tick.h>
 26#include <linux/kfifo.h>
 27#include <linux/kgdb.h>
 28#include <linux/kdb.h>
 29
 30static int kgdb_nmi_knock = 1;
 31module_param_named(knock, kgdb_nmi_knock, int, 0600);
 32MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
 33			"must be used to enter the debugger; when set to 0, " \
 34			"hitting return key is enough to enter the debugger; " \
 35			"when set to -1, the debugger is entered immediately " \
 36			"upon NMI");
 37
 38static char *kgdb_nmi_magic = "$3#33";
 39module_param_named(magic, kgdb_nmi_magic, charp, 0600);
 40MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
 41
 42static atomic_t kgdb_nmi_num_readers = ATOMIC_INIT(0);
 43
 44static int kgdb_nmi_console_setup(struct console *co, char *options)
 45{
 46	arch_kgdb_ops.enable_nmi(1);
 47
 48	/* The NMI console uses the dbg_io_ops to issue console messages. To
 49	 * avoid duplicate messages during kdb sessions we must inform kdb's
 50	 * I/O utilities that messages sent to the console will automatically
 51	 * be displayed on the dbg_io.
 52	 */
 53	dbg_io_ops->cons = co;
 54
 55	return 0;
 56}
 57
 58static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
 59{
 60	int i;
 61
 
 
 
 62	for (i = 0; i < c; i++)
 63		dbg_io_ops->write_char(s[i]);
 64}
 65
 66static struct tty_driver *kgdb_nmi_tty_driver;
 67
 68static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
 69{
 70	*idx = co->index;
 71	return kgdb_nmi_tty_driver;
 72}
 73
 74static struct console kgdb_nmi_console = {
 75	.name	= "ttyNMI",
 76	.setup  = kgdb_nmi_console_setup,
 77	.write	= kgdb_nmi_console_write,
 78	.device	= kgdb_nmi_console_device,
 79	.flags	= CON_PRINTBUFFER | CON_ANYTIME,
 80	.index	= -1,
 81};
 82
 83/*
 84 * This is usually the maximum rate on debug ports. We make fifo large enough
 85 * to make copy-pasting to the terminal usable.
 86 */
 87#define KGDB_NMI_BAUD		115200
 88#define KGDB_NMI_FIFO_SIZE	roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
 89
 90struct kgdb_nmi_tty_priv {
 91	struct tty_port port;
 92	struct timer_list timer;
 93	STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
 94};
 95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 96static struct tty_port *kgdb_nmi_port;
 97
 98static void kgdb_tty_recv(int ch)
 99{
100	struct kgdb_nmi_tty_priv *priv;
101	char c = ch;
102
103	if (!kgdb_nmi_port || ch < 0)
104		return;
105	/*
106	 * Can't use port->tty->driver_data as tty might be not there. Timer
107	 * will check for tty and will get the ref, but here we don't have to
108	 * do that, and actually, we can't: we're in NMI context, no locks are
109	 * possible.
110	 */
111	priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
112	kfifo_in(&priv->fifo, &c, 1);
 
113}
114
115static int kgdb_nmi_poll_one_knock(void)
116{
117	static int n;
118	int c;
119	const char *magic = kgdb_nmi_magic;
120	size_t m = strlen(magic);
121	bool printch = false;
122
123	c = dbg_io_ops->read_char();
124	if (c == NO_POLL_CHAR)
125		return c;
126
127	if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
128		return 1;
129	} else if (c == magic[n]) {
130		n = (n + 1) % m;
131		if (!n)
132			return 1;
133		printch = true;
134	} else {
135		n = 0;
136	}
137
138	if (atomic_read(&kgdb_nmi_num_readers)) {
139		kgdb_tty_recv(c);
140		return 0;
141	}
142
143	if (printch) {
144		kdb_printf("%c", c);
145		return 0;
146	}
147
148	kdb_printf("\r%s %s to enter the debugger> %*s",
149		   kgdb_nmi_knock ? "Type" : "Hit",
150		   kgdb_nmi_knock ? magic  : "<return>", (int)m, "");
151	while (m--)
152		kdb_printf("\b");
153	return 0;
154}
155
156/**
157 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
158 *
159 * "Serial ports are often noisy, especially when muxed over another port (we
160 * often use serial over the headset connector). Noise on the async command
161 * line just causes characters that are ignored, on a command line that blocked
162 * execution noise would be catastrophic." -- Colin Cross
163 *
164 * So, this function implements KGDB/KDB knocking on the serial line: we won't
165 * enter the debugger until we receive a known magic phrase (which is actually
166 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
167 * of knocking, i.e. just pressing the return key is enough to enter the
168 * debugger. And if knocking is disabled, the function always returns 1.
169 */
170bool kgdb_nmi_poll_knock(void)
171{
172	if (kgdb_nmi_knock < 0)
173		return true;
174
175	while (1) {
176		int ret;
177
178		ret = kgdb_nmi_poll_one_knock();
179		if (ret == NO_POLL_CHAR)
180			return false;
181		else if (ret == 1)
182			break;
183	}
184	return true;
185}
186
187/*
188 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
189 * instead it waits for the next tick.
190 */
191static void kgdb_nmi_tty_receiver(struct timer_list *t)
192{
193	struct kgdb_nmi_tty_priv *priv = from_timer(priv, t, timer);
194	char ch;
195
196	priv->timer.expires = jiffies + (HZ/100);
197	add_timer(&priv->timer);
198
199	if (likely(!atomic_read(&kgdb_nmi_num_readers) ||
200		   !kfifo_len(&priv->fifo)))
201		return;
202
203	while (kfifo_out(&priv->fifo, &ch, 1))
204		tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
205	tty_flip_buffer_push(&priv->port);
206}
207
208static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
209{
210	struct kgdb_nmi_tty_priv *priv =
211	    container_of(port, struct kgdb_nmi_tty_priv, port);
212
213	kgdb_nmi_port = port;
214	priv->timer.expires = jiffies + (HZ/100);
215	add_timer(&priv->timer);
216
217	return 0;
218}
219
220static void kgdb_nmi_tty_shutdown(struct tty_port *port)
221{
222	struct kgdb_nmi_tty_priv *priv =
223	    container_of(port, struct kgdb_nmi_tty_priv, port);
224
225	del_timer(&priv->timer);
226	kgdb_nmi_port = NULL;
227}
228
229static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
230	.activate	= kgdb_nmi_tty_activate,
231	.shutdown	= kgdb_nmi_tty_shutdown,
232};
233
234static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
235{
236	struct kgdb_nmi_tty_priv *priv;
237	int ret;
238
239	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
240	if (!priv)
241		return -ENOMEM;
242
243	INIT_KFIFO(priv->fifo);
244	timer_setup(&priv->timer, kgdb_nmi_tty_receiver, 0);
245	tty_port_init(&priv->port);
246	priv->port.ops = &kgdb_nmi_tty_port_ops;
247	tty->driver_data = priv;
248
249	ret = tty_port_install(&priv->port, drv, tty);
250	if (ret) {
251		pr_err("%s: can't install tty port: %d\n", __func__, ret);
252		goto err;
253	}
254	return 0;
255err:
256	tty_port_destroy(&priv->port);
257	kfree(priv);
258	return ret;
259}
260
261static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
262{
263	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
264
265	tty->driver_data = NULL;
266	tty_port_destroy(&priv->port);
267	kfree(priv);
268}
269
270static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
271{
272	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
273	unsigned int mode = file->f_flags & O_ACCMODE;
274	int ret;
275
276	ret = tty_port_open(&priv->port, tty, file);
277	if (!ret && (mode == O_RDONLY || mode == O_RDWR))
278		atomic_inc(&kgdb_nmi_num_readers);
279
280	return ret;
281}
282
283static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
284{
285	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
286	unsigned int mode = file->f_flags & O_ACCMODE;
287
288	if (mode == O_RDONLY || mode == O_RDWR)
289		atomic_dec(&kgdb_nmi_num_readers);
290
291	tty_port_close(&priv->port, tty, file);
292}
293
294static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
295{
296	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
297
298	tty_port_hangup(&priv->port);
299}
300
301static unsigned int kgdb_nmi_tty_write_room(struct tty_struct *tty)
302{
303	/* Actually, we can handle any amount as we use polled writes. */
304	return 2048;
305}
306
307static ssize_t kgdb_nmi_tty_write(struct tty_struct *tty, const u8 *buf,
308				  size_t c)
309{
310	int i;
311
312	for (i = 0; i < c; i++)
313		dbg_io_ops->write_char(buf[i]);
314	return c;
315}
316
317static const struct tty_operations kgdb_nmi_tty_ops = {
318	.open		= kgdb_nmi_tty_open,
319	.close		= kgdb_nmi_tty_close,
320	.install	= kgdb_nmi_tty_install,
321	.cleanup	= kgdb_nmi_tty_cleanup,
322	.hangup		= kgdb_nmi_tty_hangup,
323	.write_room	= kgdb_nmi_tty_write_room,
324	.write		= kgdb_nmi_tty_write,
325};
326
 
 
 
 
 
 
327int kgdb_register_nmi_console(void)
328{
329	int ret;
330
331	if (!arch_kgdb_ops.enable_nmi)
332		return 0;
333
334	kgdb_nmi_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
335	if (IS_ERR(kgdb_nmi_tty_driver)) {
336		pr_err("%s: cannot allocate tty\n", __func__);
337		return PTR_ERR(kgdb_nmi_tty_driver);
338	}
339	kgdb_nmi_tty_driver->driver_name	= "ttyNMI";
340	kgdb_nmi_tty_driver->name		= "ttyNMI";
341	kgdb_nmi_tty_driver->num		= 1;
342	kgdb_nmi_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
343	kgdb_nmi_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
 
344	kgdb_nmi_tty_driver->init_termios	= tty_std_termios;
345	tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
346				     KGDB_NMI_BAUD, KGDB_NMI_BAUD);
347	tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
348
349	ret = tty_register_driver(kgdb_nmi_tty_driver);
350	if (ret) {
351		pr_err("%s: can't register tty driver: %d\n", __func__, ret);
352		goto err_drv_reg;
353	}
354
 
 
 
 
 
 
 
355	register_console(&kgdb_nmi_console);
 
356
357	return 0;
 
 
358err_drv_reg:
359	tty_driver_kref_put(kgdb_nmi_tty_driver);
360	return ret;
361}
362EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
363
364int kgdb_unregister_nmi_console(void)
365{
366	int ret;
367
368	if (!arch_kgdb_ops.enable_nmi)
369		return 0;
370	arch_kgdb_ops.enable_nmi(0);
371
 
 
372	ret = unregister_console(&kgdb_nmi_console);
373	if (ret)
374		return ret;
375
376	tty_unregister_driver(kgdb_nmi_tty_driver);
377	tty_driver_kref_put(kgdb_nmi_tty_driver);
 
 
378
379	return 0;
380}
381EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);
v3.15
 
  1/*
  2 * KGDB NMI serial console
  3 *
  4 * Copyright 2010 Google, Inc.
  5 *		  Arve Hjønnevåg <arve@android.com>
  6 *		  Colin Cross <ccross@android.com>
  7 * Copyright 2012 Linaro Ltd.
  8 *		  Anton Vorontsov <anton.vorontsov@linaro.org>
  9 *
 10 * This program is free software; you can redistribute it and/or modify it
 11 * under the terms of the GNU General Public License version 2 as published
 12 * by the Free Software Foundation.
 13 */
 14
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/compiler.h>
 18#include <linux/slab.h>
 19#include <linux/errno.h>
 20#include <linux/atomic.h>
 21#include <linux/console.h>
 22#include <linux/tty.h>
 23#include <linux/tty_driver.h>
 24#include <linux/tty_flip.h>
 25#include <linux/serial_core.h>
 26#include <linux/interrupt.h>
 27#include <linux/hrtimer.h>
 28#include <linux/tick.h>
 29#include <linux/kfifo.h>
 30#include <linux/kgdb.h>
 31#include <linux/kdb.h>
 32
 33static int kgdb_nmi_knock = 1;
 34module_param_named(knock, kgdb_nmi_knock, int, 0600);
 35MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
 36			"must be used to enter the debugger; when set to 0, " \
 37			"hitting return key is enough to enter the debugger; " \
 38			"when set to -1, the debugger is entered immediately " \
 39			"upon NMI");
 40
 41static char *kgdb_nmi_magic = "$3#33";
 42module_param_named(magic, kgdb_nmi_magic, charp, 0600);
 43MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
 44
 45static bool kgdb_nmi_tty_enabled;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 46
 47static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
 48{
 49	int i;
 50
 51	if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
 52		return;
 53
 54	for (i = 0; i < c; i++)
 55		dbg_io_ops->write_char(s[i]);
 56}
 57
 58static struct tty_driver *kgdb_nmi_tty_driver;
 59
 60static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
 61{
 62	*idx = co->index;
 63	return kgdb_nmi_tty_driver;
 64}
 65
 66static struct console kgdb_nmi_console = {
 67	.name	= "ttyNMI",
 
 68	.write	= kgdb_nmi_console_write,
 69	.device	= kgdb_nmi_console_device,
 70	.flags	= CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
 71	.index	= -1,
 72};
 73
 74/*
 75 * This is usually the maximum rate on debug ports. We make fifo large enough
 76 * to make copy-pasting to the terminal usable.
 77 */
 78#define KGDB_NMI_BAUD		115200
 79#define KGDB_NMI_FIFO_SIZE	roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
 80
 81struct kgdb_nmi_tty_priv {
 82	struct tty_port port;
 83	struct tasklet_struct tlet;
 84	STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
 85};
 86
 87static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
 88{
 89	return container_of(port, struct kgdb_nmi_tty_priv, port);
 90}
 91
 92/*
 93 * Our debugging console is polled in a tasklet, so we'll check for input
 94 * every tick. In HZ-less mode, we should program the next tick.  We have
 95 * to use the lowlevel stuff as no locks should be grabbed.
 96 */
 97#ifdef CONFIG_HIGH_RES_TIMERS
 98static void kgdb_tty_poke(void)
 99{
100	tick_program_event(ktime_get(), 0);
101}
102#else
103static inline void kgdb_tty_poke(void) {}
104#endif
105
106static struct tty_port *kgdb_nmi_port;
107
108static void kgdb_tty_recv(int ch)
109{
110	struct kgdb_nmi_tty_priv *priv;
111	char c = ch;
112
113	if (!kgdb_nmi_port || ch < 0)
114		return;
115	/*
116	 * Can't use port->tty->driver_data as tty might be not there. Tasklet
117	 * will check for tty and will get the ref, but here we don't have to
118	 * do that, and actually, we can't: we're in NMI context, no locks are
119	 * possible.
120	 */
121	priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
122	kfifo_in(&priv->fifo, &c, 1);
123	kgdb_tty_poke();
124}
125
126static int kgdb_nmi_poll_one_knock(void)
127{
128	static int n;
129	int c = -1;
130	const char *magic = kgdb_nmi_magic;
131	size_t m = strlen(magic);
132	bool printch = 0;
133
134	c = dbg_io_ops->read_char();
135	if (c == NO_POLL_CHAR)
136		return c;
137
138	if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
139		return 1;
140	} else if (c == magic[n]) {
141		n = (n + 1) % m;
142		if (!n)
143			return 1;
144		printch = 1;
145	} else {
146		n = 0;
147	}
148
149	if (kgdb_nmi_tty_enabled) {
150		kgdb_tty_recv(c);
151		return 0;
152	}
153
154	if (printch) {
155		kdb_printf("%c", c);
156		return 0;
157	}
158
159	kdb_printf("\r%s %s to enter the debugger> %*s",
160		   kgdb_nmi_knock ? "Type" : "Hit",
161		   kgdb_nmi_knock ? magic  : "<return>", (int)m, "");
162	while (m--)
163		kdb_printf("\b");
164	return 0;
165}
166
167/**
168 * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
169 *
170 * "Serial ports are often noisy, especially when muxed over another port (we
171 * often use serial over the headset connector). Noise on the async command
172 * line just causes characters that are ignored, on a command line that blocked
173 * execution noise would be catastrophic." -- Colin Cross
174 *
175 * So, this function implements KGDB/KDB knocking on the serial line: we won't
176 * enter the debugger until we receive a known magic phrase (which is actually
177 * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
178 * of knocking, i.e. just pressing the return key is enough to enter the
179 * debugger. And if knocking is disabled, the function always returns 1.
180 */
181bool kgdb_nmi_poll_knock(void)
182{
183	if (kgdb_nmi_knock < 0)
184		return 1;
185
186	while (1) {
187		int ret;
188
189		ret = kgdb_nmi_poll_one_knock();
190		if (ret == NO_POLL_CHAR)
191			return 0;
192		else if (ret == 1)
193			break;
194	}
195	return 1;
196}
197
198/*
199 * The tasklet is cheap, it does not cause wakeups when reschedules itself,
200 * instead it waits for the next tick.
201 */
202static void kgdb_nmi_tty_receiver(unsigned long data)
203{
204	struct kgdb_nmi_tty_priv *priv = (void *)data;
205	char ch;
206
207	tasklet_schedule(&priv->tlet);
 
208
209	if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
 
210		return;
211
212	while (kfifo_out(&priv->fifo, &ch, 1))
213		tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
214	tty_flip_buffer_push(&priv->port);
215}
216
217static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
218{
219	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
 
220
221	kgdb_nmi_port = port;
222	tasklet_schedule(&priv->tlet);
 
 
223	return 0;
224}
225
226static void kgdb_nmi_tty_shutdown(struct tty_port *port)
227{
228	struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
 
229
230	tasklet_kill(&priv->tlet);
231	kgdb_nmi_port = NULL;
232}
233
234static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
235	.activate	= kgdb_nmi_tty_activate,
236	.shutdown	= kgdb_nmi_tty_shutdown,
237};
238
239static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
240{
241	struct kgdb_nmi_tty_priv *priv;
242	int ret;
243
244	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
245	if (!priv)
246		return -ENOMEM;
247
248	INIT_KFIFO(priv->fifo);
249	tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
250	tty_port_init(&priv->port);
251	priv->port.ops = &kgdb_nmi_tty_port_ops;
252	tty->driver_data = priv;
253
254	ret = tty_port_install(&priv->port, drv, tty);
255	if (ret) {
256		pr_err("%s: can't install tty port: %d\n", __func__, ret);
257		goto err;
258	}
259	return 0;
260err:
261	tty_port_destroy(&priv->port);
262	kfree(priv);
263	return ret;
264}
265
266static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
267{
268	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
269
270	tty->driver_data = NULL;
271	tty_port_destroy(&priv->port);
272	kfree(priv);
273}
274
275static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
276{
277	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
 
 
278
279	return tty_port_open(&priv->port, tty, file);
 
 
 
 
280}
281
282static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
283{
284	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
 
 
 
 
285
286	tty_port_close(&priv->port, tty, file);
287}
288
289static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
290{
291	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
292
293	tty_port_hangup(&priv->port);
294}
295
296static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
297{
298	/* Actually, we can handle any amount as we use polled writes. */
299	return 2048;
300}
301
302static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
 
303{
304	int i;
305
306	for (i = 0; i < c; i++)
307		dbg_io_ops->write_char(buf[i]);
308	return c;
309}
310
311static const struct tty_operations kgdb_nmi_tty_ops = {
312	.open		= kgdb_nmi_tty_open,
313	.close		= kgdb_nmi_tty_close,
314	.install	= kgdb_nmi_tty_install,
315	.cleanup	= kgdb_nmi_tty_cleanup,
316	.hangup		= kgdb_nmi_tty_hangup,
317	.write_room	= kgdb_nmi_tty_write_room,
318	.write		= kgdb_nmi_tty_write,
319};
320
321static int kgdb_nmi_enable_console(int argc, const char *argv[])
322{
323	kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
324	return 0;
325}
326
327int kgdb_register_nmi_console(void)
328{
329	int ret;
330
331	if (!arch_kgdb_ops.enable_nmi)
332		return 0;
333
334	kgdb_nmi_tty_driver = alloc_tty_driver(1);
335	if (!kgdb_nmi_tty_driver) {
336		pr_err("%s: cannot allocate tty\n", __func__);
337		return -ENOMEM;
338	}
339	kgdb_nmi_tty_driver->driver_name	= "ttyNMI";
340	kgdb_nmi_tty_driver->name		= "ttyNMI";
341	kgdb_nmi_tty_driver->num		= 1;
342	kgdb_nmi_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
343	kgdb_nmi_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
344	kgdb_nmi_tty_driver->flags		= TTY_DRIVER_REAL_RAW;
345	kgdb_nmi_tty_driver->init_termios	= tty_std_termios;
346	tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
347				     KGDB_NMI_BAUD, KGDB_NMI_BAUD);
348	tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
349
350	ret = tty_register_driver(kgdb_nmi_tty_driver);
351	if (ret) {
352		pr_err("%s: can't register tty driver: %d\n", __func__, ret);
353		goto err_drv_reg;
354	}
355
356	ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
357			   "switch to Linux NMI console", 0);
358	if (ret) {
359		pr_err("%s: can't register kdb command: %d\n", __func__, ret);
360		goto err_kdb_reg;
361	}
362
363	register_console(&kgdb_nmi_console);
364	arch_kgdb_ops.enable_nmi(1);
365
366	return 0;
367err_kdb_reg:
368	tty_unregister_driver(kgdb_nmi_tty_driver);
369err_drv_reg:
370	put_tty_driver(kgdb_nmi_tty_driver);
371	return ret;
372}
373EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
374
375int kgdb_unregister_nmi_console(void)
376{
377	int ret;
378
379	if (!arch_kgdb_ops.enable_nmi)
380		return 0;
381	arch_kgdb_ops.enable_nmi(0);
382
383	kdb_unregister("nmi_console");
384
385	ret = unregister_console(&kgdb_nmi_console);
386	if (ret)
387		return ret;
388
389	ret = tty_unregister_driver(kgdb_nmi_tty_driver);
390	if (ret)
391		return ret;
392	put_tty_driver(kgdb_nmi_tty_driver);
393
394	return 0;
395}
396EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);