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