Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * KGDB NMI serial console
0004  *
0005  * Copyright 2010 Google, Inc.
0006  *        Arve Hjønnevåg <arve@android.com>
0007  *        Colin Cross <ccross@android.com>
0008  * Copyright 2012 Linaro Ltd.
0009  *        Anton Vorontsov <anton.vorontsov@linaro.org>
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/compiler.h>
0015 #include <linux/slab.h>
0016 #include <linux/errno.h>
0017 #include <linux/atomic.h>
0018 #include <linux/console.h>
0019 #include <linux/tty.h>
0020 #include <linux/tty_driver.h>
0021 #include <linux/tty_flip.h>
0022 #include <linux/serial_core.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/hrtimer.h>
0025 #include <linux/tick.h>
0026 #include <linux/kfifo.h>
0027 #include <linux/kgdb.h>
0028 #include <linux/kdb.h>
0029 
0030 static int kgdb_nmi_knock = 1;
0031 module_param_named(knock, kgdb_nmi_knock, int, 0600);
0032 MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
0033             "must be used to enter the debugger; when set to 0, " \
0034             "hitting return key is enough to enter the debugger; " \
0035             "when set to -1, the debugger is entered immediately " \
0036             "upon NMI");
0037 
0038 static char *kgdb_nmi_magic = "$3#33";
0039 module_param_named(magic, kgdb_nmi_magic, charp, 0600);
0040 MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
0041 
0042 static atomic_t kgdb_nmi_num_readers = ATOMIC_INIT(0);
0043 
0044 static int kgdb_nmi_console_setup(struct console *co, char *options)
0045 {
0046     arch_kgdb_ops.enable_nmi(1);
0047 
0048     /* The NMI console uses the dbg_io_ops to issue console messages. To
0049      * avoid duplicate messages during kdb sessions we must inform kdb's
0050      * I/O utilities that messages sent to the console will automatically
0051      * be displayed on the dbg_io.
0052      */
0053     dbg_io_ops->cons = co;
0054 
0055     return 0;
0056 }
0057 
0058 static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
0059 {
0060     int i;
0061 
0062     for (i = 0; i < c; i++)
0063         dbg_io_ops->write_char(s[i]);
0064 }
0065 
0066 static struct tty_driver *kgdb_nmi_tty_driver;
0067 
0068 static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
0069 {
0070     *idx = co->index;
0071     return kgdb_nmi_tty_driver;
0072 }
0073 
0074 static struct console kgdb_nmi_console = {
0075     .name   = "ttyNMI",
0076     .setup  = kgdb_nmi_console_setup,
0077     .write  = kgdb_nmi_console_write,
0078     .device = kgdb_nmi_console_device,
0079     .flags  = CON_PRINTBUFFER | CON_ANYTIME,
0080     .index  = -1,
0081 };
0082 
0083 /*
0084  * This is usually the maximum rate on debug ports. We make fifo large enough
0085  * to make copy-pasting to the terminal usable.
0086  */
0087 #define KGDB_NMI_BAUD       115200
0088 #define KGDB_NMI_FIFO_SIZE  roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
0089 
0090 struct kgdb_nmi_tty_priv {
0091     struct tty_port port;
0092     struct timer_list timer;
0093     STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
0094 };
0095 
0096 static struct tty_port *kgdb_nmi_port;
0097 
0098 static void kgdb_tty_recv(int ch)
0099 {
0100     struct kgdb_nmi_tty_priv *priv;
0101     char c = ch;
0102 
0103     if (!kgdb_nmi_port || ch < 0)
0104         return;
0105     /*
0106      * Can't use port->tty->driver_data as tty might be not there. Timer
0107      * will check for tty and will get the ref, but here we don't have to
0108      * do that, and actually, we can't: we're in NMI context, no locks are
0109      * possible.
0110      */
0111     priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
0112     kfifo_in(&priv->fifo, &c, 1);
0113 }
0114 
0115 static int kgdb_nmi_poll_one_knock(void)
0116 {
0117     static int n;
0118     int c;
0119     const char *magic = kgdb_nmi_magic;
0120     size_t m = strlen(magic);
0121     bool printch = false;
0122 
0123     c = dbg_io_ops->read_char();
0124     if (c == NO_POLL_CHAR)
0125         return c;
0126 
0127     if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
0128         return 1;
0129     } else if (c == magic[n]) {
0130         n = (n + 1) % m;
0131         if (!n)
0132             return 1;
0133         printch = true;
0134     } else {
0135         n = 0;
0136     }
0137 
0138     if (atomic_read(&kgdb_nmi_num_readers)) {
0139         kgdb_tty_recv(c);
0140         return 0;
0141     }
0142 
0143     if (printch) {
0144         kdb_printf("%c", c);
0145         return 0;
0146     }
0147 
0148     kdb_printf("\r%s %s to enter the debugger> %*s",
0149            kgdb_nmi_knock ? "Type" : "Hit",
0150            kgdb_nmi_knock ? magic  : "<return>", (int)m, "");
0151     while (m--)
0152         kdb_printf("\b");
0153     return 0;
0154 }
0155 
0156 /**
0157  * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
0158  *
0159  * "Serial ports are often noisy, especially when muxed over another port (we
0160  * often use serial over the headset connector). Noise on the async command
0161  * line just causes characters that are ignored, on a command line that blocked
0162  * execution noise would be catastrophic." -- Colin Cross
0163  *
0164  * So, this function implements KGDB/KDB knocking on the serial line: we won't
0165  * enter the debugger until we receive a known magic phrase (which is actually
0166  * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
0167  * of knocking, i.e. just pressing the return key is enough to enter the
0168  * debugger. And if knocking is disabled, the function always returns 1.
0169  */
0170 bool kgdb_nmi_poll_knock(void)
0171 {
0172     if (kgdb_nmi_knock < 0)
0173         return true;
0174 
0175     while (1) {
0176         int ret;
0177 
0178         ret = kgdb_nmi_poll_one_knock();
0179         if (ret == NO_POLL_CHAR)
0180             return false;
0181         else if (ret == 1)
0182             break;
0183     }
0184     return true;
0185 }
0186 
0187 /*
0188  * The tasklet is cheap, it does not cause wakeups when reschedules itself,
0189  * instead it waits for the next tick.
0190  */
0191 static void kgdb_nmi_tty_receiver(struct timer_list *t)
0192 {
0193     struct kgdb_nmi_tty_priv *priv = from_timer(priv, t, timer);
0194     char ch;
0195 
0196     priv->timer.expires = jiffies + (HZ/100);
0197     add_timer(&priv->timer);
0198 
0199     if (likely(!atomic_read(&kgdb_nmi_num_readers) ||
0200            !kfifo_len(&priv->fifo)))
0201         return;
0202 
0203     while (kfifo_out(&priv->fifo, &ch, 1))
0204         tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
0205     tty_flip_buffer_push(&priv->port);
0206 }
0207 
0208 static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
0209 {
0210     struct kgdb_nmi_tty_priv *priv =
0211         container_of(port, struct kgdb_nmi_tty_priv, port);
0212 
0213     kgdb_nmi_port = port;
0214     priv->timer.expires = jiffies + (HZ/100);
0215     add_timer(&priv->timer);
0216 
0217     return 0;
0218 }
0219 
0220 static void kgdb_nmi_tty_shutdown(struct tty_port *port)
0221 {
0222     struct kgdb_nmi_tty_priv *priv =
0223         container_of(port, struct kgdb_nmi_tty_priv, port);
0224 
0225     del_timer(&priv->timer);
0226     kgdb_nmi_port = NULL;
0227 }
0228 
0229 static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
0230     .activate   = kgdb_nmi_tty_activate,
0231     .shutdown   = kgdb_nmi_tty_shutdown,
0232 };
0233 
0234 static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
0235 {
0236     struct kgdb_nmi_tty_priv *priv;
0237     int ret;
0238 
0239     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0240     if (!priv)
0241         return -ENOMEM;
0242 
0243     INIT_KFIFO(priv->fifo);
0244     timer_setup(&priv->timer, kgdb_nmi_tty_receiver, 0);
0245     tty_port_init(&priv->port);
0246     priv->port.ops = &kgdb_nmi_tty_port_ops;
0247     tty->driver_data = priv;
0248 
0249     ret = tty_port_install(&priv->port, drv, tty);
0250     if (ret) {
0251         pr_err("%s: can't install tty port: %d\n", __func__, ret);
0252         goto err;
0253     }
0254     return 0;
0255 err:
0256     tty_port_destroy(&priv->port);
0257     kfree(priv);
0258     return ret;
0259 }
0260 
0261 static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
0262 {
0263     struct kgdb_nmi_tty_priv *priv = tty->driver_data;
0264 
0265     tty->driver_data = NULL;
0266     tty_port_destroy(&priv->port);
0267     kfree(priv);
0268 }
0269 
0270 static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
0271 {
0272     struct kgdb_nmi_tty_priv *priv = tty->driver_data;
0273     unsigned int mode = file->f_flags & O_ACCMODE;
0274     int ret;
0275 
0276     ret = tty_port_open(&priv->port, tty, file);
0277     if (!ret && (mode == O_RDONLY || mode == O_RDWR))
0278         atomic_inc(&kgdb_nmi_num_readers);
0279 
0280     return ret;
0281 }
0282 
0283 static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
0284 {
0285     struct kgdb_nmi_tty_priv *priv = tty->driver_data;
0286     unsigned int mode = file->f_flags & O_ACCMODE;
0287 
0288     if (mode == O_RDONLY || mode == O_RDWR)
0289         atomic_dec(&kgdb_nmi_num_readers);
0290 
0291     tty_port_close(&priv->port, tty, file);
0292 }
0293 
0294 static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
0295 {
0296     struct kgdb_nmi_tty_priv *priv = tty->driver_data;
0297 
0298     tty_port_hangup(&priv->port);
0299 }
0300 
0301 static unsigned int kgdb_nmi_tty_write_room(struct tty_struct *tty)
0302 {
0303     /* Actually, we can handle any amount as we use polled writes. */
0304     return 2048;
0305 }
0306 
0307 static int kgdb_nmi_tty_write(struct tty_struct *tty, const unchar *buf, int c)
0308 {
0309     int i;
0310 
0311     for (i = 0; i < c; i++)
0312         dbg_io_ops->write_char(buf[i]);
0313     return c;
0314 }
0315 
0316 static const struct tty_operations kgdb_nmi_tty_ops = {
0317     .open       = kgdb_nmi_tty_open,
0318     .close      = kgdb_nmi_tty_close,
0319     .install    = kgdb_nmi_tty_install,
0320     .cleanup    = kgdb_nmi_tty_cleanup,
0321     .hangup     = kgdb_nmi_tty_hangup,
0322     .write_room = kgdb_nmi_tty_write_room,
0323     .write      = kgdb_nmi_tty_write,
0324 };
0325 
0326 int kgdb_register_nmi_console(void)
0327 {
0328     int ret;
0329 
0330     if (!arch_kgdb_ops.enable_nmi)
0331         return 0;
0332 
0333     kgdb_nmi_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
0334     if (IS_ERR(kgdb_nmi_tty_driver)) {
0335         pr_err("%s: cannot allocate tty\n", __func__);
0336         return PTR_ERR(kgdb_nmi_tty_driver);
0337     }
0338     kgdb_nmi_tty_driver->driver_name    = "ttyNMI";
0339     kgdb_nmi_tty_driver->name       = "ttyNMI";
0340     kgdb_nmi_tty_driver->num        = 1;
0341     kgdb_nmi_tty_driver->type       = TTY_DRIVER_TYPE_SERIAL;
0342     kgdb_nmi_tty_driver->subtype        = SERIAL_TYPE_NORMAL;
0343     kgdb_nmi_tty_driver->init_termios   = tty_std_termios;
0344     tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
0345                      KGDB_NMI_BAUD, KGDB_NMI_BAUD);
0346     tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
0347 
0348     ret = tty_register_driver(kgdb_nmi_tty_driver);
0349     if (ret) {
0350         pr_err("%s: can't register tty driver: %d\n", __func__, ret);
0351         goto err_drv_reg;
0352     }
0353 
0354     register_console(&kgdb_nmi_console);
0355 
0356     return 0;
0357 err_drv_reg:
0358     tty_driver_kref_put(kgdb_nmi_tty_driver);
0359     return ret;
0360 }
0361 EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
0362 
0363 int kgdb_unregister_nmi_console(void)
0364 {
0365     int ret;
0366 
0367     if (!arch_kgdb_ops.enable_nmi)
0368         return 0;
0369     arch_kgdb_ops.enable_nmi(0);
0370 
0371     ret = unregister_console(&kgdb_nmi_console);
0372     if (ret)
0373         return ret;
0374 
0375     tty_unregister_driver(kgdb_nmi_tty_driver);
0376     tty_driver_kref_put(kgdb_nmi_tty_driver);
0377 
0378     return 0;
0379 }
0380 EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);