15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/compiler.h>
19 #include <linux/slab.h>
20 #include <linux/errno.h>
23 #include <linux/tty.h>
27 #include <linux/hrtimer.h>
33 static int kgdb_nmi_knock = 1;
35 MODULE_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 " \
41 static char *kgdb_nmi_magic =
"$3#33";
45 static bool kgdb_nmi_tty_enabled;
47 static void kgdb_nmi_console_write(
struct console *co,
const char *
s,
uint c)
54 for (i = 0; i <
c; i++)
63 return kgdb_nmi_tty_driver;
66 static struct console kgdb_nmi_console = {
68 .write = kgdb_nmi_console_write,
69 .device = kgdb_nmi_console_device,
78 #define KGDB_NMI_BAUD 115200
79 #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
97 #ifdef CONFIG_HIGH_RES_TIMERS
98 static void kgdb_tty_poke(
void)
103 static inline void kgdb_tty_poke(
void) {}
106 static struct tty_port *kgdb_nmi_port;
108 static void kgdb_tty_recv(
int ch)
113 if (!kgdb_nmi_port || ch < 0)
121 priv = kgdb_nmi_port_to_priv(kgdb_nmi_port);
126 static int kgdb_nmi_poll_one_knock(
void)
130 const char *
magic = kgdb_nmi_magic;
138 if (!kgdb_nmi_knock && (c ==
'\r' || c ==
'\n')) {
140 }
else if (c == magic[n]) {
149 if (kgdb_nmi_tty_enabled) {
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,
"");
183 if (kgdb_nmi_knock < 0)
189 ret = kgdb_nmi_poll_one_knock();
202 static void kgdb_nmi_tty_receiver(
unsigned long data)
208 tasklet_schedule(&priv->
tlet);
229 kgdb_nmi_port =
port;
230 tasklet_schedule(&priv->
tlet);
234 static void kgdb_nmi_tty_shutdown(
struct tty_port *
port)
239 kgdb_nmi_port =
NULL;
243 .activate = kgdb_nmi_tty_activate,
244 .shutdown = kgdb_nmi_tty_shutdown,
259 priv->
port.ops = &kgdb_nmi_tty_port_ops;
264 pr_err(
"%s: can't install tty port: %d\n", __func__, ret);
273 static void kgdb_nmi_tty_cleanup(
struct tty_struct *tty)
295 static void kgdb_nmi_tty_hangup(
struct tty_struct *tty)
302 static int kgdb_nmi_tty_write_room(
struct tty_struct *tty)
312 for (i = 0; i <
c; i++)
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,
327 static int kgdb_nmi_enable_console(
int argc,
const char *argv[])
329 kgdb_nmi_tty_enabled = !(argc == 1 && !
strcmp(argv[1],
"off"));
340 kgdb_nmi_tty_driver = alloc_tty_driver(1);
341 if (!kgdb_nmi_tty_driver) {
342 pr_err(
"%s: cannot allocate tty\n", __func__);
346 kgdb_nmi_tty_driver->
name =
"ttyNMI";
347 kgdb_nmi_tty_driver->
num = 1;
358 pr_err(
"%s: can't register tty driver: %d\n", __func__, ret);
362 ret =
kdb_register(
"nmi_console", kgdb_nmi_enable_console,
"[off]",
363 "switch to Linux NMI console", 0);
365 pr_err(
"%s: can't register kdb command: %d\n", __func__, ret);