All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Wessel <jason.wessel@windriver.com>
To: Anton Vorontsov <anton.vorontsov@linaro.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Russell King" <linux@arm.linux.org.uk>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Alan Cox" <alan@linux.intel.com>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Colin Cross" <ccross@android.com>,
	"Brian Swetland" <swetland@google.com>,
	"John Stultz" <john.stultz@linaro.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linaro-kernel@lists.linaro.org, patches@linaro.org,
	kernel-team@android.com, kgdb-bugreport@lists.sourceforge.net,
	linux-serial@vger.kernel.org
Subject: Re: [PATCH 07/11] tty/serial: Add kgdb_nmi driver
Date: Wed, 19 Sep 2012 06:52:32 -0500	[thread overview]
Message-ID: <5059B200.9040706@windriver.com> (raw)
In-Reply-To: <1347548615-18227-7-git-send-email-anton.vorontsov@linaro.org>

On 09/13/2012 10:03 AM, Anton Vorontsov wrote:
> This special driver makes it possible to temporary use NMI debugger port
> as a normal console by issuing 'nmi_console' command (assuming that the
> port is attached to KGDB).
> 
> Unlike KDB's disable_nmi command, with this driver you are always able
> to go back to the debugger using KGDB escape sequence ($3#33).  This is
> because this console driver processes the input in NMI context, and thus
> is able to intercept the magic sequence.
> 
> Note that since the console interprets input and uses polling
> communication methods, for things like PPP it is still better to fully
> detach debugger port from the KGDB NMI (i.e. disable_nmi), and use raw
> console.
> 
> Usually, to enter the debugger one have to type the magic sequence, so
> initially the kernel will print the following prompt on the NMI debugger
> console:
> 
> 	Type $3#33 to enter the debugger>
> 
> For convenience, there is a kgdb_fiq.knock kernel command line option,
> when set to 0, this turns the special command to just a return key
> press, so the kernel will be printing this:
> 
> 	Hit <return> to enter the debugger>
> 
> This is more convenient for long debugging sessions, although it makes
> nmi_console feature somewhat useless.
> 
> And for the cases when NMI connected to a dedicated button, the knocking
> can be disabled altogether by setting kgdb_fiq.knock to -1.
> 
> Suggested-by: Colin Cross <ccross@android.com>
> Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
> ---
>  drivers/tty/serial/Kconfig    |  19 ++
>  drivers/tty/serial/Makefile   |   1 +
>  drivers/tty/serial/kgdb_nmi.c | 396 ++++++++++++++++++++++++++++++++++++++++++
>  drivers/tty/serial/kgdboc.c   |   6 +
>  include/linux/kgdb.h          |  10 ++
>  5 files changed, 432 insertions(+)
>  create mode 100644 drivers/tty/serial/kgdb_nmi.c
> 
> diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
> index 26907cf..b22e45b 100644
> --- a/drivers/tty/serial/Kconfig
> +++ b/drivers/tty/serial/Kconfig
> @@ -141,6 +141,25 @@ config SERIAL_ATMEL_TTYAT
>  
>  	  Say Y if you have an external 8250/16C550 UART.  If unsure, say N.
>  
> +config SERIAL_KGDB_NMI
> +	bool "Serial console over KGDB NMI debugger port"
> +	depends on KGDB_SERIAL_CONSOLE
> +	help
> +	  This special driver allows you to temporary use NMI debugger port
> +	  as a normal console (assuming that the port is attached to KGDB).
> +
> +	  Unlike KDB's disable_nmi command, with this driver you are always
> +	  able to go back to the debugger using KGDB escape sequence ($3#33).
> +	  This is because this console driver processes the input in NMI
> +	  context, and thus is able to intercept the magic sequence.
> +
> +	  Note that since the console interprets input and uses polling
> +	  communication methods, for things like PPP you still must fully
> +	  detach debugger port from the KGDB NMI (i.e. disable_nmi), and
> +	  use raw console.
> +
> +	  If unsure, say N.
> +
>  config SERIAL_KS8695
>  	bool "Micrel KS8695 (Centaur) serial port support"
>  	depends on ARCH_KS8695
> diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
> index ce88667..4f694da 100644
> --- a/drivers/tty/serial/Makefile
> +++ b/drivers/tty/serial/Makefile
> @@ -61,6 +61,7 @@ obj-$(CONFIG_SERIAL_MSM_HS) += msm_serial_hs.o
>  obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL) += nwpserial.o
> +obj-$(CONFIG_SERIAL_KGDB_NMI) += kgdb_nmi.o
>  obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
>  obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
>  obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
> diff --git a/drivers/tty/serial/kgdb_nmi.c b/drivers/tty/serial/kgdb_nmi.c
> new file mode 100644
> index 0000000..57bf744
> --- /dev/null
> +++ b/drivers/tty/serial/kgdb_nmi.c
> @@ -0,0 +1,396 @@
> +/*
> + * KGDB NMI serial console
> + *
> + * Copyright 2010 Google, Inc.
> + *		  Arve Hjønnevåg <arve@android.com>
> + *		  Colin Cross <ccross@android.com>
> + * Copyright 2012 Linaro Ltd.
> + *		  Anton Vorontsov <anton.vorontsov@linaro.org>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published
> + * by the Free Software Foundation.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/compiler.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/errno.h>
> +#include <linux/atomic.h>
> +#include <linux/console.h>
> +#include <linux/tty.h>
> +#include <linux/tty_driver.h>
> +#include <linux/tty_flip.h>
> +#include <linux/interrupt.h>
> +#include <linux/hrtimer.h>
> +#include <linux/tick.h>
> +#include <linux/kfifo.h>
> +#include <linux/kgdb.h>
> +#include <linux/kdb.h>
> +
> +static int kgdb_nmi_knock = 1;
> +module_param_named(knock, kgdb_nmi_knock, int, 0600);
> +MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command "
> +			"must be used to enter the debugger; when set to 0, "
> +			"hitting return key is enough to enter the debugger; "
> +			"when set to -1, the debugger is entered immediately "
> +			"upon NMI");
> +
> +static char *kgdb_nmi_magic = "$3#33";
> +module_param_named(magic, kgdb_nmi_magic, charp, 0600);
> +MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
> +
> +static bool kgdb_nmi_tty_enabled;
> +
> +static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
> +{
> +	int i;
> +
> +	if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
> +		return;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(s[i]);
> +}
> +
> +static struct tty_driver *kgdb_nmi_tty_driver;
> +
> +static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
> +{
> +	*idx = co->index;
> +	return kgdb_nmi_tty_driver;
> +}
> +
> +static struct console kgdb_nmi_console = {
> +	.name	= "ttyNMI",
> +	.write	= kgdb_nmi_console_write,
> +	.device	= kgdb_nmi_console_device,
> +	.flags	= CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
> +	.index	= -1,
> +};
> +
> +/*
> + * This is usually the maximum rate on debug ports. We make fifo large enough
> + * to make copy-pasting to the terminal usable.
> + */
> +#define KGDB_NMI_BAUD		115200
> +#define KGDB_NMI_FIFO_SIZE	roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
> +
> +struct kgdb_nmi_tty_priv {
> +	struct tty_port port;
> +	struct tasklet_struct tlet;
> +	STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
> +};
> +
> +static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
> +{
> +	return container_of(port, struct kgdb_nmi_tty_priv, port);
> +}
> +
> +/*
> + * Our debugging console is polled in a tasklet, so we'll check for input
> + * every tick. In HZ-less mode, we should program the next tick.  We have
> + * to use the lowlevel stuff as no locks should be grabbed.
> + */
> +#ifdef CONFIG_HIGH_RES_TIMERS
> +static void kgdb_tty_poke(void)
> +{
> +	tick_program_event(ktime_get(), 0);
> +}
> +#else
> +static inline void kgdb_tty_poke(void) {}
> +#endif
> +
> +static struct tty_port *kgdb_nmi_port;
> +
> +static void kgdb_tty_recv(int ch)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	char c = ch;
> +
> +	if (!kgdb_nmi_port || ch < 0)
> +		return;
> +	/*
> +	 * Can't use port->tty->driver_data as tty might be not there. Tasklet
> +	 * will check for tty and will get the ref, but here we don't have to
> +	 * do that, and actually, we can't: we're in NMI context, no locks are
> +	 * possible.
> +	 */
> +	priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
> +	kfifo_in(&priv->fifo, &c, 1);
> +	kgdb_tty_poke();
> +}
> +
> +static int kgdb_nmi_poll_one_knock(void)
> +{
> +	static int n;
> +	int c = -1;
> +	const char *magic = kgdb_nmi_magic;
> +	size_t m = strlen(magic);
> +	bool printch = 0;
> +
> +	c = dbg_io_ops->read_char();
> +	if (c == NO_POLL_CHAR)
> +		return c;
> +
> +	if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
> +		return 1;
> +	} else if (c == magic[n]) {
> +		n = (n + 1) % m;
> +		if (!n)
> +			return 1;
> +		printch = 1;
> +	} else {
> +		n = 0;
> +	}
> +
> +	if (kgdb_nmi_tty_enabled) {
> +		kgdb_tty_recv(c);
> +		return 0;
> +	}
> +
> +	if (printch) {
> +		kdb_printf("%c", c);
> +		return 0;
> +	}
> +
> +	kdb_printf("\r%s %s to enter the debugger> %*s",
> +		   kgdb_nmi_knock ? "Type" : "Hit",
> +		   kgdb_nmi_knock ? magic  : "<return>", m, "");
> +	while (m--)
> +		kdb_printf("\b");
> +	return 0;
> +}
> +
> +/**
> + * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
> + *
> + * "Serial ports are often noisy, especially when muxed over another port (we
> + * often use serial over the headset connector). Noise on the async command
> + * line just causes characters that are ignored, on a command line that blocked
> + * execution noise would be catastrophic." -- Colin Cross
> + *
> + * So, this function implements KGDB/KDB knocking on the serial line: we won't
> + * enter the debugger until we receive a known magic phrase (which is actually
> + * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
> + * of knocking, i.e. just pressing the return key is enough to enter the
> + * debugger. And if knocking is disabled, the function always returns 1.
> + */
> +bool kgdb_nmi_poll_knock(void)
> +{
> +	if (kgdb_nmi_knock < 0)
> +		return 1;
> +
> +	while (1) {
> +		int ret;
> +
> +		ret = kgdb_nmi_poll_one_knock();
> +		if (ret == NO_POLL_CHAR)
> +			return 0;
> +		else if (ret == 1)
> +			break;
> +	}
> +	return 1;
> +}
> +
> +/*
> + * The tasklet is cheap, it does not cause wakeups when reschedules itself,
> + * instead it waits for the next tick.
> + */
> +static void kgdb_nmi_tty_receiver(unsigned long data)
> +{
> +	struct kgdb_nmi_tty_priv *priv = (void *)data;
> +	struct tty_struct *tty;
> +	char ch;
> +
> +	tasklet_schedule(&priv->tlet);
> +
> +	if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
> +		return;
> +
> +	/* Port is there, but tty might be hung up, check. */
> +	tty = tty_port_tty_get(kgdb_nmi_port);
> +	if (!tty)
> +		return;
> +
> +	while (kfifo_out(&priv->fifo, &ch, 1))
> +		tty_insert_flip_char(priv->port.tty, ch, TTY_NORMAL);
> +	tty_flip_buffer_push(priv->port.tty);
> +
> +	tty_kref_put(tty);
> +}
> +
> +static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	kgdb_nmi_port = port;
> +	tasklet_schedule(&priv->tlet);
> +	return 0;
> +}
> +
> +static void kgdb_nmi_tty_shutdown(struct tty_port *port)
> +{
> +	struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
> +
> +	tasklet_kill(&priv->tlet);
> +	kgdb_nmi_port = NULL;
> +}
> +
> +static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
> +	.activate	= kgdb_nmi_tty_activate,
> +	.shutdown	= kgdb_nmi_tty_shutdown,
> +};
> +
> +static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	int ret;
> +
> +	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	INIT_KFIFO(priv->fifo);
> +	tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
> +	tty_port_init(&priv->port);
> +	priv->port.ops = &kgdb_nmi_tty_port_ops;
> +	tty->driver_data = priv;
> +
> +	ret = tty_port_install(&priv->port, drv, tty);
> +	if (ret) {
> +		pr_err("%s: can't nstall tty port: %d\n", __func__, ret);
> +		goto err;
> +	}
> +	return 0;
> +err:
> +	kfree(priv);
> +	return ret;
> +}
> +
> +static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty->driver_data = NULL;
> +	kfree(priv);
> +}
> +
> +static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	return tty_port_open(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_close(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_hangup(&priv->port);
> +}
> +
> +static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
> +{
> +	/* Actually, we can handle any amount as we use polled writes. */
> +	return 2048;
> +}
> +
> +static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
> +{
> +	int i;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(buf[i]);
> +	return c;
> +}
> +
> +static const struct tty_operations kgdb_nmi_tty_ops = {
> +	.open		= kgdb_nmi_tty_open,
> +	.close		= kgdb_nmi_tty_close,
> +	.install	= kgdb_nmi_tty_install,
> +	.cleanup	= kgdb_nmi_tty_cleanup,
> +	.hangup		= kgdb_nmi_tty_hangup,
> +	.write_room	= kgdb_nmi_tty_write_room,
> +	.write		= kgdb_nmi_tty_write,
> +};
> +
> +static int kgdb_nmi_enable_console(int argc, const char *argv[])
> +{
> +	kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
> +	return 0;
> +}
> +
> +int kgdb_register_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_nmi_tty_driver = alloc_tty_driver(1);
> +	if (!kgdb_nmi_tty_driver) {
> +		pr_err("%s: cannot allocate tty\n", __func__);
> +		return -ENOMEM;
> +	}
> +	kgdb_nmi_tty_driver->driver_name	= "ttyNMI";
> +	kgdb_nmi_tty_driver->name		= "ttyNMI";
> +	kgdb_nmi_tty_driver->num		= 1;
> +	kgdb_nmi_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
> +	kgdb_nmi_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
> +	kgdb_nmi_tty_driver->flags		= TTY_DRIVER_REAL_RAW;
> +	kgdb_nmi_tty_driver->init_termios	= tty_std_termios;
> +	tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
> +				     KGDB_NMI_BAUD, KGDB_NMI_BAUD);
> +	tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
> +
> +	ret = tty_register_driver(kgdb_nmi_tty_driver);
> +	if (ret) {
> +		pr_err("%s: can't register tty driver: %d\n", __func__, ret);
> +		goto err_drv_reg;
> +	}
> +
> +	ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
> +			   "switch to Linux NMI console", 0);
> +	if (ret) {
> +		pr_err("%s: can't register kdb command: %d\n", __func__, ret);
> +		goto err_kdb_reg;
> +	}
> +
> +	register_console(&kgdb_nmi_console);
> +	kgdb_enable_nmi(1);
> +
> +	return 0;
> +err_kdb_reg:
> +	tty_unregister_driver(kgdb_nmi_tty_driver);
> +err_drv_reg:
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
> +
> +int kgdb_unregister_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_enable_nmi(0);
> +	kdb_unregister("nmi_console");
> +
> +	ret = unregister_console(&kgdb_nmi_console);
> +	if (ret)
> +		return ret;
> +
> +	ret = tty_unregister_driver(kgdb_nmi_tty_driver);
> +	if (ret)
> +		return ret;
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);
> diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
> index 2b42a01..ed97cfd 100644
> --- a/drivers/tty/serial/kgdboc.c
> +++ b/drivers/tty/serial/kgdboc.c
> @@ -145,6 +145,8 @@ __setup("kgdboc=", kgdboc_option_setup);
>  
>  static void cleanup_kgdboc(void)
>  {
> +	if (kgdb_unregister_nmi_console())
> +		return;
>  	kgdboc_unregister_kbd();
>  	if (configured == 1)
>  		kgdb_unregister_io_module(&kgdboc_io_ops);
> @@ -198,6 +200,10 @@ do_register:
>  	if (err)
>  		goto noconfig;
>  
> +	err = kgdb_register_nmi_console();
> +	if (err)
> +		goto noconfig;
> +


Just one question on this one, what do you expect to happen, or how does this work when you use a different serial port for example on a board that has a real serial port and an FIQ port?   It was not obvious that there was a path to check if the port you are registering is an FIQ enabled port and then call the kgdb_register_nmi_console at that point.

Jason.

WARNING: multiple messages have this Message-ID (diff)
From: Jason Wessel <jason.wessel@windriver.com>
To: Anton Vorontsov <anton.vorontsov@linaro.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Russell King" <linux@arm.linux.org.uk>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Alan Cox" <alan@linux.intel.com>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Colin Cross" <ccross@android.com>,
	"Brian Swetland" <swetland@google.com>,
	"John Stultz" <john.stultz@linaro.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linaro-kernel@lists.linaro.org, patches@linaro.org,
	kernel-team@android.com, kgdb-bugreport@lists.sourceforge.net,
	linux-serial@vger.kernel.org
Subject: Re: [PATCH 07/11] tty/serial: Add kgdb_nmi driver
Date: Wed, 19 Sep 2012 06:52:32 -0500	[thread overview]
Message-ID: <5059B200.9040706@windriver.com> (raw)
In-Reply-To: <1347548615-18227-7-git-send-email-anton.vorontsov@linaro.org>

On 09/13/2012 10:03 AM, Anton Vorontsov wrote:
> This special driver makes it possible to temporary use NMI debugger port
> as a normal console by issuing 'nmi_console' command (assuming that the
> port is attached to KGDB).
> 
> Unlike KDB's disable_nmi command, with this driver you are always able
> to go back to the debugger using KGDB escape sequence ($3#33).  This is
> because this console driver processes the input in NMI context, and thus
> is able to intercept the magic sequence.
> 
> Note that since the console interprets input and uses polling
> communication methods, for things like PPP it is still better to fully
> detach debugger port from the KGDB NMI (i.e. disable_nmi), and use raw
> console.
> 
> Usually, to enter the debugger one have to type the magic sequence, so
> initially the kernel will print the following prompt on the NMI debugger
> console:
> 
> 	Type $3#33 to enter the debugger>
> 
> For convenience, there is a kgdb_fiq.knock kernel command line option,
> when set to 0, this turns the special command to just a return key
> press, so the kernel will be printing this:
> 
> 	Hit <return> to enter the debugger>
> 
> This is more convenient for long debugging sessions, although it makes
> nmi_console feature somewhat useless.
> 
> And for the cases when NMI connected to a dedicated button, the knocking
> can be disabled altogether by setting kgdb_fiq.knock to -1.
> 
> Suggested-by: Colin Cross <ccross@android.com>
> Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
> ---
>  drivers/tty/serial/Kconfig    |  19 ++
>  drivers/tty/serial/Makefile   |   1 +
>  drivers/tty/serial/kgdb_nmi.c | 396 ++++++++++++++++++++++++++++++++++++++++++
>  drivers/tty/serial/kgdboc.c   |   6 +
>  include/linux/kgdb.h          |  10 ++
>  5 files changed, 432 insertions(+)
>  create mode 100644 drivers/tty/serial/kgdb_nmi.c
> 
> diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
> index 26907cf..b22e45b 100644
> --- a/drivers/tty/serial/Kconfig
> +++ b/drivers/tty/serial/Kconfig
> @@ -141,6 +141,25 @@ config SERIAL_ATMEL_TTYAT
>  
>  	  Say Y if you have an external 8250/16C550 UART.  If unsure, say N.
>  
> +config SERIAL_KGDB_NMI
> +	bool "Serial console over KGDB NMI debugger port"
> +	depends on KGDB_SERIAL_CONSOLE
> +	help
> +	  This special driver allows you to temporary use NMI debugger port
> +	  as a normal console (assuming that the port is attached to KGDB).
> +
> +	  Unlike KDB's disable_nmi command, with this driver you are always
> +	  able to go back to the debugger using KGDB escape sequence ($3#33).
> +	  This is because this console driver processes the input in NMI
> +	  context, and thus is able to intercept the magic sequence.
> +
> +	  Note that since the console interprets input and uses polling
> +	  communication methods, for things like PPP you still must fully
> +	  detach debugger port from the KGDB NMI (i.e. disable_nmi), and
> +	  use raw console.
> +
> +	  If unsure, say N.
> +
>  config SERIAL_KS8695
>  	bool "Micrel KS8695 (Centaur) serial port support"
>  	depends on ARCH_KS8695
> diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
> index ce88667..4f694da 100644
> --- a/drivers/tty/serial/Makefile
> +++ b/drivers/tty/serial/Makefile
> @@ -61,6 +61,7 @@ obj-$(CONFIG_SERIAL_MSM_HS) += msm_serial_hs.o
>  obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL) += nwpserial.o
> +obj-$(CONFIG_SERIAL_KGDB_NMI) += kgdb_nmi.o
>  obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
>  obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
>  obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
> diff --git a/drivers/tty/serial/kgdb_nmi.c b/drivers/tty/serial/kgdb_nmi.c
> new file mode 100644
> index 0000000..57bf744
> --- /dev/null
> +++ b/drivers/tty/serial/kgdb_nmi.c
> @@ -0,0 +1,396 @@
> +/*
> + * KGDB NMI serial console
> + *
> + * Copyright 2010 Google, Inc.
> + *		  Arve Hjønnevåg <arve@android.com>
> + *		  Colin Cross <ccross@android.com>
> + * Copyright 2012 Linaro Ltd.
> + *		  Anton Vorontsov <anton.vorontsov@linaro.org>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published
> + * by the Free Software Foundation.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/compiler.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/errno.h>
> +#include <linux/atomic.h>
> +#include <linux/console.h>
> +#include <linux/tty.h>
> +#include <linux/tty_driver.h>
> +#include <linux/tty_flip.h>
> +#include <linux/interrupt.h>
> +#include <linux/hrtimer.h>
> +#include <linux/tick.h>
> +#include <linux/kfifo.h>
> +#include <linux/kgdb.h>
> +#include <linux/kdb.h>
> +
> +static int kgdb_nmi_knock = 1;
> +module_param_named(knock, kgdb_nmi_knock, int, 0600);
> +MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command "
> +			"must be used to enter the debugger; when set to 0, "
> +			"hitting return key is enough to enter the debugger; "
> +			"when set to -1, the debugger is entered immediately "
> +			"upon NMI");
> +
> +static char *kgdb_nmi_magic = "$3#33";
> +module_param_named(magic, kgdb_nmi_magic, charp, 0600);
> +MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
> +
> +static bool kgdb_nmi_tty_enabled;
> +
> +static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
> +{
> +	int i;
> +
> +	if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
> +		return;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(s[i]);
> +}
> +
> +static struct tty_driver *kgdb_nmi_tty_driver;
> +
> +static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
> +{
> +	*idx = co->index;
> +	return kgdb_nmi_tty_driver;
> +}
> +
> +static struct console kgdb_nmi_console = {
> +	.name	= "ttyNMI",
> +	.write	= kgdb_nmi_console_write,
> +	.device	= kgdb_nmi_console_device,
> +	.flags	= CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
> +	.index	= -1,
> +};
> +
> +/*
> + * This is usually the maximum rate on debug ports. We make fifo large enough
> + * to make copy-pasting to the terminal usable.
> + */
> +#define KGDB_NMI_BAUD		115200
> +#define KGDB_NMI_FIFO_SIZE	roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
> +
> +struct kgdb_nmi_tty_priv {
> +	struct tty_port port;
> +	struct tasklet_struct tlet;
> +	STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
> +};
> +
> +static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
> +{
> +	return container_of(port, struct kgdb_nmi_tty_priv, port);
> +}
> +
> +/*
> + * Our debugging console is polled in a tasklet, so we'll check for input
> + * every tick. In HZ-less mode, we should program the next tick.  We have
> + * to use the lowlevel stuff as no locks should be grabbed.
> + */
> +#ifdef CONFIG_HIGH_RES_TIMERS
> +static void kgdb_tty_poke(void)
> +{
> +	tick_program_event(ktime_get(), 0);
> +}
> +#else
> +static inline void kgdb_tty_poke(void) {}
> +#endif
> +
> +static struct tty_port *kgdb_nmi_port;
> +
> +static void kgdb_tty_recv(int ch)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	char c = ch;
> +
> +	if (!kgdb_nmi_port || ch < 0)
> +		return;
> +	/*
> +	 * Can't use port->tty->driver_data as tty might be not there. Tasklet
> +	 * will check for tty and will get the ref, but here we don't have to
> +	 * do that, and actually, we can't: we're in NMI context, no locks are
> +	 * possible.
> +	 */
> +	priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
> +	kfifo_in(&priv->fifo, &c, 1);
> +	kgdb_tty_poke();
> +}
> +
> +static int kgdb_nmi_poll_one_knock(void)
> +{
> +	static int n;
> +	int c = -1;
> +	const char *magic = kgdb_nmi_magic;
> +	size_t m = strlen(magic);
> +	bool printch = 0;
> +
> +	c = dbg_io_ops->read_char();
> +	if (c == NO_POLL_CHAR)
> +		return c;
> +
> +	if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
> +		return 1;
> +	} else if (c == magic[n]) {
> +		n = (n + 1) % m;
> +		if (!n)
> +			return 1;
> +		printch = 1;
> +	} else {
> +		n = 0;
> +	}
> +
> +	if (kgdb_nmi_tty_enabled) {
> +		kgdb_tty_recv(c);
> +		return 0;
> +	}
> +
> +	if (printch) {
> +		kdb_printf("%c", c);
> +		return 0;
> +	}
> +
> +	kdb_printf("\r%s %s to enter the debugger> %*s",
> +		   kgdb_nmi_knock ? "Type" : "Hit",
> +		   kgdb_nmi_knock ? magic  : "<return>", m, "");
> +	while (m--)
> +		kdb_printf("\b");
> +	return 0;
> +}
> +
> +/**
> + * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
> + *
> + * "Serial ports are often noisy, especially when muxed over another port (we
> + * often use serial over the headset connector). Noise on the async command
> + * line just causes characters that are ignored, on a command line that blocked
> + * execution noise would be catastrophic." -- Colin Cross
> + *
> + * So, this function implements KGDB/KDB knocking on the serial line: we won't
> + * enter the debugger until we receive a known magic phrase (which is actually
> + * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
> + * of knocking, i.e. just pressing the return key is enough to enter the
> + * debugger. And if knocking is disabled, the function always returns 1.
> + */
> +bool kgdb_nmi_poll_knock(void)
> +{
> +	if (kgdb_nmi_knock < 0)
> +		return 1;
> +
> +	while (1) {
> +		int ret;
> +
> +		ret = kgdb_nmi_poll_one_knock();
> +		if (ret == NO_POLL_CHAR)
> +			return 0;
> +		else if (ret == 1)
> +			break;
> +	}
> +	return 1;
> +}
> +
> +/*
> + * The tasklet is cheap, it does not cause wakeups when reschedules itself,
> + * instead it waits for the next tick.
> + */
> +static void kgdb_nmi_tty_receiver(unsigned long data)
> +{
> +	struct kgdb_nmi_tty_priv *priv = (void *)data;
> +	struct tty_struct *tty;
> +	char ch;
> +
> +	tasklet_schedule(&priv->tlet);
> +
> +	if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
> +		return;
> +
> +	/* Port is there, but tty might be hung up, check. */
> +	tty = tty_port_tty_get(kgdb_nmi_port);
> +	if (!tty)
> +		return;
> +
> +	while (kfifo_out(&priv->fifo, &ch, 1))
> +		tty_insert_flip_char(priv->port.tty, ch, TTY_NORMAL);
> +	tty_flip_buffer_push(priv->port.tty);
> +
> +	tty_kref_put(tty);
> +}
> +
> +static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	kgdb_nmi_port = port;
> +	tasklet_schedule(&priv->tlet);
> +	return 0;
> +}
> +
> +static void kgdb_nmi_tty_shutdown(struct tty_port *port)
> +{
> +	struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
> +
> +	tasklet_kill(&priv->tlet);
> +	kgdb_nmi_port = NULL;
> +}
> +
> +static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
> +	.activate	= kgdb_nmi_tty_activate,
> +	.shutdown	= kgdb_nmi_tty_shutdown,
> +};
> +
> +static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	int ret;
> +
> +	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	INIT_KFIFO(priv->fifo);
> +	tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
> +	tty_port_init(&priv->port);
> +	priv->port.ops = &kgdb_nmi_tty_port_ops;
> +	tty->driver_data = priv;
> +
> +	ret = tty_port_install(&priv->port, drv, tty);
> +	if (ret) {
> +		pr_err("%s: can't nstall tty port: %d\n", __func__, ret);
> +		goto err;
> +	}
> +	return 0;
> +err:
> +	kfree(priv);
> +	return ret;
> +}
> +
> +static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty->driver_data = NULL;
> +	kfree(priv);
> +}
> +
> +static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	return tty_port_open(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_close(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_hangup(&priv->port);
> +}
> +
> +static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
> +{
> +	/* Actually, we can handle any amount as we use polled writes. */
> +	return 2048;
> +}
> +
> +static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
> +{
> +	int i;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(buf[i]);
> +	return c;
> +}
> +
> +static const struct tty_operations kgdb_nmi_tty_ops = {
> +	.open		= kgdb_nmi_tty_open,
> +	.close		= kgdb_nmi_tty_close,
> +	.install	= kgdb_nmi_tty_install,
> +	.cleanup	= kgdb_nmi_tty_cleanup,
> +	.hangup		= kgdb_nmi_tty_hangup,
> +	.write_room	= kgdb_nmi_tty_write_room,
> +	.write		= kgdb_nmi_tty_write,
> +};
> +
> +static int kgdb_nmi_enable_console(int argc, const char *argv[])
> +{
> +	kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
> +	return 0;
> +}
> +
> +int kgdb_register_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_nmi_tty_driver = alloc_tty_driver(1);
> +	if (!kgdb_nmi_tty_driver) {
> +		pr_err("%s: cannot allocate tty\n", __func__);
> +		return -ENOMEM;
> +	}
> +	kgdb_nmi_tty_driver->driver_name	= "ttyNMI";
> +	kgdb_nmi_tty_driver->name		= "ttyNMI";
> +	kgdb_nmi_tty_driver->num		= 1;
> +	kgdb_nmi_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
> +	kgdb_nmi_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
> +	kgdb_nmi_tty_driver->flags		= TTY_DRIVER_REAL_RAW;
> +	kgdb_nmi_tty_driver->init_termios	= tty_std_termios;
> +	tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
> +				     KGDB_NMI_BAUD, KGDB_NMI_BAUD);
> +	tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
> +
> +	ret = tty_register_driver(kgdb_nmi_tty_driver);
> +	if (ret) {
> +		pr_err("%s: can't register tty driver: %d\n", __func__, ret);
> +		goto err_drv_reg;
> +	}
> +
> +	ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
> +			   "switch to Linux NMI console", 0);
> +	if (ret) {
> +		pr_err("%s: can't register kdb command: %d\n", __func__, ret);
> +		goto err_kdb_reg;
> +	}
> +
> +	register_console(&kgdb_nmi_console);
> +	kgdb_enable_nmi(1);
> +
> +	return 0;
> +err_kdb_reg:
> +	tty_unregister_driver(kgdb_nmi_tty_driver);
> +err_drv_reg:
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
> +
> +int kgdb_unregister_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_enable_nmi(0);
> +	kdb_unregister("nmi_console");
> +
> +	ret = unregister_console(&kgdb_nmi_console);
> +	if (ret)
> +		return ret;
> +
> +	ret = tty_unregister_driver(kgdb_nmi_tty_driver);
> +	if (ret)
> +		return ret;
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);
> diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
> index 2b42a01..ed97cfd 100644
> --- a/drivers/tty/serial/kgdboc.c
> +++ b/drivers/tty/serial/kgdboc.c
> @@ -145,6 +145,8 @@ __setup("kgdboc=", kgdboc_option_setup);
>  
>  static void cleanup_kgdboc(void)
>  {
> +	if (kgdb_unregister_nmi_console())
> +		return;
>  	kgdboc_unregister_kbd();
>  	if (configured == 1)
>  		kgdb_unregister_io_module(&kgdboc_io_ops);
> @@ -198,6 +200,10 @@ do_register:
>  	if (err)
>  		goto noconfig;
>  
> +	err = kgdb_register_nmi_console();
> +	if (err)
> +		goto noconfig;
> +


Just one question on this one, what do you expect to happen, or how does this work when you use a different serial port for example on a board that has a real serial port and an FIQ port?   It was not obvious that there was a path to check if the port you are registering is an FIQ enabled port and then call the kgdb_register_nmi_console at that point.

Jason.
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

WARNING: multiple messages have this Message-ID (diff)
From: jason.wessel@windriver.com (Jason Wessel)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 07/11] tty/serial: Add kgdb_nmi driver
Date: Wed, 19 Sep 2012 06:52:32 -0500	[thread overview]
Message-ID: <5059B200.9040706@windriver.com> (raw)
In-Reply-To: <1347548615-18227-7-git-send-email-anton.vorontsov@linaro.org>

On 09/13/2012 10:03 AM, Anton Vorontsov wrote:
> This special driver makes it possible to temporary use NMI debugger port
> as a normal console by issuing 'nmi_console' command (assuming that the
> port is attached to KGDB).
> 
> Unlike KDB's disable_nmi command, with this driver you are always able
> to go back to the debugger using KGDB escape sequence ($3#33).  This is
> because this console driver processes the input in NMI context, and thus
> is able to intercept the magic sequence.
> 
> Note that since the console interprets input and uses polling
> communication methods, for things like PPP it is still better to fully
> detach debugger port from the KGDB NMI (i.e. disable_nmi), and use raw
> console.
> 
> Usually, to enter the debugger one have to type the magic sequence, so
> initially the kernel will print the following prompt on the NMI debugger
> console:
> 
> 	Type $3#33 to enter the debugger>
> 
> For convenience, there is a kgdb_fiq.knock kernel command line option,
> when set to 0, this turns the special command to just a return key
> press, so the kernel will be printing this:
> 
> 	Hit <return> to enter the debugger>
> 
> This is more convenient for long debugging sessions, although it makes
> nmi_console feature somewhat useless.
> 
> And for the cases when NMI connected to a dedicated button, the knocking
> can be disabled altogether by setting kgdb_fiq.knock to -1.
> 
> Suggested-by: Colin Cross <ccross@android.com>
> Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
> ---
>  drivers/tty/serial/Kconfig    |  19 ++
>  drivers/tty/serial/Makefile   |   1 +
>  drivers/tty/serial/kgdb_nmi.c | 396 ++++++++++++++++++++++++++++++++++++++++++
>  drivers/tty/serial/kgdboc.c   |   6 +
>  include/linux/kgdb.h          |  10 ++
>  5 files changed, 432 insertions(+)
>  create mode 100644 drivers/tty/serial/kgdb_nmi.c
> 
> diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
> index 26907cf..b22e45b 100644
> --- a/drivers/tty/serial/Kconfig
> +++ b/drivers/tty/serial/Kconfig
> @@ -141,6 +141,25 @@ config SERIAL_ATMEL_TTYAT
>  
>  	  Say Y if you have an external 8250/16C550 UART.  If unsure, say N.
>  
> +config SERIAL_KGDB_NMI
> +	bool "Serial console over KGDB NMI debugger port"
> +	depends on KGDB_SERIAL_CONSOLE
> +	help
> +	  This special driver allows you to temporary use NMI debugger port
> +	  as a normal console (assuming that the port is attached to KGDB).
> +
> +	  Unlike KDB's disable_nmi command, with this driver you are always
> +	  able to go back to the debugger using KGDB escape sequence ($3#33).
> +	  This is because this console driver processes the input in NMI
> +	  context, and thus is able to intercept the magic sequence.
> +
> +	  Note that since the console interprets input and uses polling
> +	  communication methods, for things like PPP you still must fully
> +	  detach debugger port from the KGDB NMI (i.e. disable_nmi), and
> +	  use raw console.
> +
> +	  If unsure, say N.
> +
>  config SERIAL_KS8695
>  	bool "Micrel KS8695 (Centaur) serial port support"
>  	depends on ARCH_KS8695
> diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
> index ce88667..4f694da 100644
> --- a/drivers/tty/serial/Makefile
> +++ b/drivers/tty/serial/Makefile
> @@ -61,6 +61,7 @@ obj-$(CONFIG_SERIAL_MSM_HS) += msm_serial_hs.o
>  obj-$(CONFIG_SERIAL_NETX) += netx-serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM) += of_serial.o
>  obj-$(CONFIG_SERIAL_OF_PLATFORM_NWPSERIAL) += nwpserial.o
> +obj-$(CONFIG_SERIAL_KGDB_NMI) += kgdb_nmi.o
>  obj-$(CONFIG_SERIAL_KS8695) += serial_ks8695.o
>  obj-$(CONFIG_SERIAL_OMAP) += omap-serial.o
>  obj-$(CONFIG_SERIAL_ALTERA_UART) += altera_uart.o
> diff --git a/drivers/tty/serial/kgdb_nmi.c b/drivers/tty/serial/kgdb_nmi.c
> new file mode 100644
> index 0000000..57bf744
> --- /dev/null
> +++ b/drivers/tty/serial/kgdb_nmi.c
> @@ -0,0 +1,396 @@
> +/*
> + * KGDB NMI serial console
> + *
> + * Copyright 2010 Google, Inc.
> + *		  Arve Hj?nnev?g <arve@android.com>
> + *		  Colin Cross <ccross@android.com>
> + * Copyright 2012 Linaro Ltd.
> + *		  Anton Vorontsov <anton.vorontsov@linaro.org>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU General Public License version 2 as published
> + * by the Free Software Foundation.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/compiler.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/errno.h>
> +#include <linux/atomic.h>
> +#include <linux/console.h>
> +#include <linux/tty.h>
> +#include <linux/tty_driver.h>
> +#include <linux/tty_flip.h>
> +#include <linux/interrupt.h>
> +#include <linux/hrtimer.h>
> +#include <linux/tick.h>
> +#include <linux/kfifo.h>
> +#include <linux/kgdb.h>
> +#include <linux/kdb.h>
> +
> +static int kgdb_nmi_knock = 1;
> +module_param_named(knock, kgdb_nmi_knock, int, 0600);
> +MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command "
> +			"must be used to enter the debugger; when set to 0, "
> +			"hitting return key is enough to enter the debugger; "
> +			"when set to -1, the debugger is entered immediately "
> +			"upon NMI");
> +
> +static char *kgdb_nmi_magic = "$3#33";
> +module_param_named(magic, kgdb_nmi_magic, charp, 0600);
> +MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
> +
> +static bool kgdb_nmi_tty_enabled;
> +
> +static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
> +{
> +	int i;
> +
> +	if (!kgdb_nmi_tty_enabled || atomic_read(&kgdb_active) >= 0)
> +		return;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(s[i]);
> +}
> +
> +static struct tty_driver *kgdb_nmi_tty_driver;
> +
> +static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
> +{
> +	*idx = co->index;
> +	return kgdb_nmi_tty_driver;
> +}
> +
> +static struct console kgdb_nmi_console = {
> +	.name	= "ttyNMI",
> +	.write	= kgdb_nmi_console_write,
> +	.device	= kgdb_nmi_console_device,
> +	.flags	= CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
> +	.index	= -1,
> +};
> +
> +/*
> + * This is usually the maximum rate on debug ports. We make fifo large enough
> + * to make copy-pasting to the terminal usable.
> + */
> +#define KGDB_NMI_BAUD		115200
> +#define KGDB_NMI_FIFO_SIZE	roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
> +
> +struct kgdb_nmi_tty_priv {
> +	struct tty_port port;
> +	struct tasklet_struct tlet;
> +	STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
> +};
> +
> +static struct kgdb_nmi_tty_priv *kgdb_nmi_port_to_priv(struct tty_port *port)
> +{
> +	return container_of(port, struct kgdb_nmi_tty_priv, port);
> +}
> +
> +/*
> + * Our debugging console is polled in a tasklet, so we'll check for input
> + * every tick. In HZ-less mode, we should program the next tick.  We have
> + * to use the lowlevel stuff as no locks should be grabbed.
> + */
> +#ifdef CONFIG_HIGH_RES_TIMERS
> +static void kgdb_tty_poke(void)
> +{
> +	tick_program_event(ktime_get(), 0);
> +}
> +#else
> +static inline void kgdb_tty_poke(void) {}
> +#endif
> +
> +static struct tty_port *kgdb_nmi_port;
> +
> +static void kgdb_tty_recv(int ch)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	char c = ch;
> +
> +	if (!kgdb_nmi_port || ch < 0)
> +		return;
> +	/*
> +	 * Can't use port->tty->driver_data as tty might be not there. Tasklet
> +	 * will check for tty and will get the ref, but here we don't have to
> +	 * do that, and actually, we can't: we're in NMI context, no locks are
> +	 * possible.
> +	 */
> +	priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
> +	kfifo_in(&priv->fifo, &c, 1);
> +	kgdb_tty_poke();
> +}
> +
> +static int kgdb_nmi_poll_one_knock(void)
> +{
> +	static int n;
> +	int c = -1;
> +	const char *magic = kgdb_nmi_magic;
> +	size_t m = strlen(magic);
> +	bool printch = 0;
> +
> +	c = dbg_io_ops->read_char();
> +	if (c == NO_POLL_CHAR)
> +		return c;
> +
> +	if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
> +		return 1;
> +	} else if (c == magic[n]) {
> +		n = (n + 1) % m;
> +		if (!n)
> +			return 1;
> +		printch = 1;
> +	} else {
> +		n = 0;
> +	}
> +
> +	if (kgdb_nmi_tty_enabled) {
> +		kgdb_tty_recv(c);
> +		return 0;
> +	}
> +
> +	if (printch) {
> +		kdb_printf("%c", c);
> +		return 0;
> +	}
> +
> +	kdb_printf("\r%s %s to enter the debugger> %*s",
> +		   kgdb_nmi_knock ? "Type" : "Hit",
> +		   kgdb_nmi_knock ? magic  : "<return>", m, "");
> +	while (m--)
> +		kdb_printf("\b");
> +	return 0;
> +}
> +
> +/**
> + * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
> + *
> + * "Serial ports are often noisy, especially when muxed over another port (we
> + * often use serial over the headset connector). Noise on the async command
> + * line just causes characters that are ignored, on a command line that blocked
> + * execution noise would be catastrophic." -- Colin Cross
> + *
> + * So, this function implements KGDB/KDB knocking on the serial line: we won't
> + * enter the debugger until we receive a known magic phrase (which is actually
> + * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
> + * of knocking, i.e. just pressing the return key is enough to enter the
> + * debugger. And if knocking is disabled, the function always returns 1.
> + */
> +bool kgdb_nmi_poll_knock(void)
> +{
> +	if (kgdb_nmi_knock < 0)
> +		return 1;
> +
> +	while (1) {
> +		int ret;
> +
> +		ret = kgdb_nmi_poll_one_knock();
> +		if (ret == NO_POLL_CHAR)
> +			return 0;
> +		else if (ret == 1)
> +			break;
> +	}
> +	return 1;
> +}
> +
> +/*
> + * The tasklet is cheap, it does not cause wakeups when reschedules itself,
> + * instead it waits for the next tick.
> + */
> +static void kgdb_nmi_tty_receiver(unsigned long data)
> +{
> +	struct kgdb_nmi_tty_priv *priv = (void *)data;
> +	struct tty_struct *tty;
> +	char ch;
> +
> +	tasklet_schedule(&priv->tlet);
> +
> +	if (likely(!kgdb_nmi_tty_enabled || !kfifo_len(&priv->fifo)))
> +		return;
> +
> +	/* Port is there, but tty might be hung up, check. */
> +	tty = tty_port_tty_get(kgdb_nmi_port);
> +	if (!tty)
> +		return;
> +
> +	while (kfifo_out(&priv->fifo, &ch, 1))
> +		tty_insert_flip_char(priv->port.tty, ch, TTY_NORMAL);
> +	tty_flip_buffer_push(priv->port.tty);
> +
> +	tty_kref_put(tty);
> +}
> +
> +static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	kgdb_nmi_port = port;
> +	tasklet_schedule(&priv->tlet);
> +	return 0;
> +}
> +
> +static void kgdb_nmi_tty_shutdown(struct tty_port *port)
> +{
> +	struct kgdb_nmi_tty_priv *priv = port->tty->driver_data;
> +
> +	tasklet_kill(&priv->tlet);
> +	kgdb_nmi_port = NULL;
> +}
> +
> +static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
> +	.activate	= kgdb_nmi_tty_activate,
> +	.shutdown	= kgdb_nmi_tty_shutdown,
> +};
> +
> +static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv;
> +	int ret;
> +
> +	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	INIT_KFIFO(priv->fifo);
> +	tasklet_init(&priv->tlet, kgdb_nmi_tty_receiver, (unsigned long)priv);
> +	tty_port_init(&priv->port);
> +	priv->port.ops = &kgdb_nmi_tty_port_ops;
> +	tty->driver_data = priv;
> +
> +	ret = tty_port_install(&priv->port, drv, tty);
> +	if (ret) {
> +		pr_err("%s: can't nstall tty port: %d\n", __func__, ret);
> +		goto err;
> +	}
> +	return 0;
> +err:
> +	kfree(priv);
> +	return ret;
> +}
> +
> +static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty->driver_data = NULL;
> +	kfree(priv);
> +}
> +
> +static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	return tty_port_open(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_close(&priv->port, tty, file);
> +}
> +
> +static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
> +{
> +	struct kgdb_nmi_tty_priv *priv = tty->driver_data;
> +
> +	tty_port_hangup(&priv->port);
> +}
> +
> +static int kgdb_nmi_tty_write_room(struct tty_struct *tty)
> +{
> +	/* Actually, we can handle any amount as we use polled writes. */
> +	return 2048;
> +}
> +
> +static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
> +{
> +	int i;
> +
> +	for (i = 0; i < c; i++)
> +		dbg_io_ops->write_char(buf[i]);
> +	return c;
> +}
> +
> +static const struct tty_operations kgdb_nmi_tty_ops = {
> +	.open		= kgdb_nmi_tty_open,
> +	.close		= kgdb_nmi_tty_close,
> +	.install	= kgdb_nmi_tty_install,
> +	.cleanup	= kgdb_nmi_tty_cleanup,
> +	.hangup		= kgdb_nmi_tty_hangup,
> +	.write_room	= kgdb_nmi_tty_write_room,
> +	.write		= kgdb_nmi_tty_write,
> +};
> +
> +static int kgdb_nmi_enable_console(int argc, const char *argv[])
> +{
> +	kgdb_nmi_tty_enabled = !(argc == 1 && !strcmp(argv[1], "off"));
> +	return 0;
> +}
> +
> +int kgdb_register_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_nmi_tty_driver = alloc_tty_driver(1);
> +	if (!kgdb_nmi_tty_driver) {
> +		pr_err("%s: cannot allocate tty\n", __func__);
> +		return -ENOMEM;
> +	}
> +	kgdb_nmi_tty_driver->driver_name	= "ttyNMI";
> +	kgdb_nmi_tty_driver->name		= "ttyNMI";
> +	kgdb_nmi_tty_driver->num		= 1;
> +	kgdb_nmi_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
> +	kgdb_nmi_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
> +	kgdb_nmi_tty_driver->flags		= TTY_DRIVER_REAL_RAW;
> +	kgdb_nmi_tty_driver->init_termios	= tty_std_termios;
> +	tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
> +				     KGDB_NMI_BAUD, KGDB_NMI_BAUD);
> +	tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
> +
> +	ret = tty_register_driver(kgdb_nmi_tty_driver);
> +	if (ret) {
> +		pr_err("%s: can't register tty driver: %d\n", __func__, ret);
> +		goto err_drv_reg;
> +	}
> +
> +	ret = kdb_register("nmi_console", kgdb_nmi_enable_console, "[off]",
> +			   "switch to Linux NMI console", 0);
> +	if (ret) {
> +		pr_err("%s: can't register kdb command: %d\n", __func__, ret);
> +		goto err_kdb_reg;
> +	}
> +
> +	register_console(&kgdb_nmi_console);
> +	kgdb_enable_nmi(1);
> +
> +	return 0;
> +err_kdb_reg:
> +	tty_unregister_driver(kgdb_nmi_tty_driver);
> +err_drv_reg:
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
> +
> +int kgdb_unregister_nmi_console(void)
> +{
> +	int ret;
> +
> +	kgdb_enable_nmi(0);
> +	kdb_unregister("nmi_console");
> +
> +	ret = unregister_console(&kgdb_nmi_console);
> +	if (ret)
> +		return ret;
> +
> +	ret = tty_unregister_driver(kgdb_nmi_tty_driver);
> +	if (ret)
> +		return ret;
> +	put_tty_driver(kgdb_nmi_tty_driver);
> +
> +	return 0;
> +}
> +EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);
> diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
> index 2b42a01..ed97cfd 100644
> --- a/drivers/tty/serial/kgdboc.c
> +++ b/drivers/tty/serial/kgdboc.c
> @@ -145,6 +145,8 @@ __setup("kgdboc=", kgdboc_option_setup);
>  
>  static void cleanup_kgdboc(void)
>  {
> +	if (kgdb_unregister_nmi_console())
> +		return;
>  	kgdboc_unregister_kbd();
>  	if (configured == 1)
>  		kgdb_unregister_io_module(&kgdboc_io_ops);
> @@ -198,6 +200,10 @@ do_register:
>  	if (err)
>  		goto noconfig;
>  
> +	err = kgdb_register_nmi_console();
> +	if (err)
> +		goto noconfig;
> +


Just one question on this one, what do you expect to happen, or how does this work when you use a different serial port for example on a board that has a real serial port and an FIQ port?   It was not obvious that there was a path to check if the port you are registering is an FIQ enabled port and then call the kgdb_register_nmi_console at that point.

Jason.

  parent reply	other threads:[~2012-09-19 11:53 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-13 15:01 [PATCH v7 0/11] KGDB/KDB FIQ (NMI) debugger Anton Vorontsov
2012-09-13 15:01 ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 01/11] kernel/debug: Mask KGDB NMI upon entry Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-19 11:52   ` Jason Wessel
2012-09-19 11:52     ` Jason Wessel
2012-09-13 15:03 ` [PATCH 02/11] kdb: Implement disable_nmi command Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-19 11:52   ` Jason Wessel
2012-09-19 11:52     ` Jason Wessel
2012-09-13 15:03 ` [PATCH 03/11] kdb: Turn KGDB_KDB=n stubs into static inlines Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 04/11] tty/serial/core: Introduce poll_init callback Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 05/11] tty/serial/amba-pl011: Implement " Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 06/11] tty/serial/amba-pl011: Quiesce interrupts in poll_get_char Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 07/11] tty/serial: Add kgdb_nmi driver Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-15 15:52   ` Francesco Lavra
2012-09-15 15:52     ` Francesco Lavra
2012-09-17  1:07     ` [PATCH updated " Anton Vorontsov
2012-09-17  1:07       ` Anton Vorontsov
2012-09-19 11:52   ` Jason Wessel [this message]
2012-09-19 11:52     ` [PATCH " Jason Wessel
2012-09-19 11:52     ` Jason Wessel
2012-09-19 16:54     ` Anton Vorontsov
2012-09-19 16:54       ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 08/11] ARM: Move some macros from entry-armv to entry-header Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 09/11] ARM: Add KGDB/KDB FIQ debugger generic code Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 10/11] ARM: VIC: Add a couple of low-level FIQ management helpers Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 15:03 ` [PATCH 11/11] ARM: versatile: Make able to use UART ports for KGDB FIQ debugger Anton Vorontsov
2012-09-13 15:03   ` Anton Vorontsov
2012-09-13 17:04 ` [PATCH v7 0/11] KGDB/KDB FIQ (NMI) debugger Alan Cox
2012-09-13 17:04   ` Alan Cox
2012-09-13 16:55   ` Anton Vorontsov
2012-09-13 16:55     ` Anton Vorontsov
2012-09-17 11:37 ` Greg Kroah-Hartman
2012-09-17 11:37   ` Greg Kroah-Hartman
2012-09-17 12:43   ` Jason Wessel
2012-09-17 12:43     ` Jason Wessel
2012-09-17 12:54     ` Greg Kroah-Hartman
2012-09-17 12:54       ` Greg Kroah-Hartman
2012-09-17 15:39       ` Anton Vorontsov
2012-09-17 15:39         ` Anton Vorontsov
2012-09-17 15:54         ` Greg Kroah-Hartman
2012-09-17 15:54           ` Greg Kroah-Hartman
2012-09-19 11:57           ` Jason Wessel
2012-09-19 11:57             ` Jason Wessel
2012-09-19 16:57             ` Anton Vorontsov
2012-09-19 16:57               ` Anton Vorontsov
2012-09-19 23:40 [PATCH v8 " Anton Vorontsov
2012-09-19 23:41 ` [PATCH 07/11] tty/serial: Add kgdb_nmi driver Anton Vorontsov
2012-09-19 23:41   ` Anton Vorontsov
2012-09-19 23:41   ` Anton Vorontsov
2012-09-20 13:57   ` Jason Wessel
2012-09-20 13:57     ` Jason Wessel
2012-09-24 21:26 [PATCH v9 0/11] KGDB/KDB FIQ (NMI) debugger Anton Vorontsov
2012-09-24 21:27 ` [PATCH 07/11] tty/serial: Add kgdb_nmi driver Anton Vorontsov
2012-09-24 21:27   ` Anton Vorontsov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5059B200.9040706@windriver.com \
    --to=jason.wessel@windriver.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@linux.intel.com \
    --cc=anton.vorontsov@linaro.org \
    --cc=arve@android.com \
    --cc=ccross@android.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.stultz@linaro.org \
    --cc=kernel-team@android.com \
    --cc=kgdb-bugreport@lists.sourceforge.net \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=patches@linaro.org \
    --cc=swetland@google.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.