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