kgdb,i386: Fix corner case access to ss with NMI watch dog exception
[deliverable/linux.git] / kernel / kgdb.c
CommitLineData
dc7d5527
JW
1/*
2 * KGDB stub.
3 *
4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
5 *
6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2008 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
15 *
16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support.
22 *
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
25 *
26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied.
29 */
30#include <linux/pid_namespace.h>
7c3078b6 31#include <linux/clocksource.h>
dc7d5527
JW
32#include <linux/interrupt.h>
33#include <linux/spinlock.h>
34#include <linux/console.h>
35#include <linux/threads.h>
36#include <linux/uaccess.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/ptrace.h>
40#include <linux/reboot.h>
41#include <linux/string.h>
42#include <linux/delay.h>
43#include <linux/sched.h>
44#include <linux/sysrq.h>
45#include <linux/init.h>
46#include <linux/kgdb.h>
47#include <linux/pid.h>
48#include <linux/smp.h>
49#include <linux/mm.h>
50
51#include <asm/cacheflush.h>
52#include <asm/byteorder.h>
53#include <asm/atomic.h>
54#include <asm/system.h>
827e609b 55#include <asm/unaligned.h>
dc7d5527
JW
56
57static int kgdb_break_asap;
58
25fc9999 59#define KGDB_MAX_THREAD_QUERY 17
dc7d5527
JW
60struct kgdb_state {
61 int ex_vector;
62 int signo;
63 int err_code;
64 int cpu;
65 int pass_exception;
25fc9999 66 unsigned long thr_query;
688b744d 67 unsigned long threadid;
dc7d5527
JW
68 long kgdb_usethreadid;
69 struct pt_regs *linux_regs;
70};
71
72static struct debuggerinfo_struct {
73 void *debuggerinfo;
74 struct task_struct *task;
75} kgdb_info[NR_CPUS];
76
77/**
78 * kgdb_connected - Is a host GDB connected to us?
79 */
80int kgdb_connected;
81EXPORT_SYMBOL_GPL(kgdb_connected);
82
83/* All the KGDB handlers are installed */
84static int kgdb_io_module_registered;
85
86/* Guard for recursive entry */
87static int exception_level;
88
89static struct kgdb_io *kgdb_io_ops;
90static DEFINE_SPINLOCK(kgdb_registration_lock);
91
92/* kgdb console driver is loaded */
93static int kgdb_con_registered;
94/* determine if kgdb console output should be used */
95static int kgdb_use_con;
96
97static int __init opt_kgdb_con(char *str)
98{
99 kgdb_use_con = 1;
100 return 0;
101}
102
103early_param("kgdbcon", opt_kgdb_con);
104
105module_param(kgdb_use_con, int, 0644);
106
107/*
108 * Holds information about breakpoints in a kernel. These breakpoints are
109 * added and removed by gdb.
110 */
111static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
112 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
113};
114
115/*
116 * The CPU# of the active CPU, or -1 if none:
117 */
118atomic_t kgdb_active = ATOMIC_INIT(-1);
119
120/*
121 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
122 * bootup code (which might not have percpu set up yet):
123 */
124static atomic_t passive_cpu_wait[NR_CPUS];
125static atomic_t cpu_in_kgdb[NR_CPUS];
126atomic_t kgdb_setting_breakpoint;
127
128struct task_struct *kgdb_usethread;
129struct task_struct *kgdb_contthread;
130
131int kgdb_single_step;
132
133/* Our I/O buffers. */
134static char remcom_in_buffer[BUFMAX];
135static char remcom_out_buffer[BUFMAX];
136
137/* Storage for the registers, in GDB format. */
138static unsigned long gdb_regs[(NUMREGBYTES +
139 sizeof(unsigned long) - 1) /
140 sizeof(unsigned long)];
141
142/* to keep track of the CPU which is doing the single stepping*/
143atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
144
145/*
146 * If you are debugging a problem where roundup (the collection of
147 * all other CPUs) is a problem [this should be extremely rare],
148 * then use the nokgdbroundup option to avoid roundup. In that case
149 * the other CPUs might interfere with your debugging context, so
150 * use this with care:
151 */
688b744d 152static int kgdb_do_roundup = 1;
dc7d5527
JW
153
154static int __init opt_nokgdbroundup(char *str)
155{
156 kgdb_do_roundup = 0;
157
158 return 0;
159}
160
161early_param("nokgdbroundup", opt_nokgdbroundup);
162
163/*
164 * Finally, some KGDB code :-)
165 */
166
167/*
168 * Weak aliases for breakpoint management,
169 * can be overriden by architectures when needed:
170 */
dc7d5527
JW
171int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
172{
173 int err;
174
175 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
176 if (err)
177 return err;
178
179 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
180 BREAK_INSTR_SIZE);
181}
182
183int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
184{
185 return probe_kernel_write((char *)addr,
186 (char *)bundle, BREAK_INSTR_SIZE);
187}
188
a9b60bf4
JW
189int __weak kgdb_validate_break_address(unsigned long addr)
190{
191 char tmp_variable[BREAK_INSTR_SIZE];
192 int err;
193 /* Validate setting the breakpoint and then removing it. In the
194 * remove fails, the kernel needs to emit a bad message because we
195 * are deep trouble not being able to put things back the way we
196 * found them.
197 */
198 err = kgdb_arch_set_breakpoint(addr, tmp_variable);
199 if (err)
200 return err;
201 err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
202 if (err)
203 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
204 "memory destroyed at: %lx", addr);
205 return err;
206}
207
dc7d5527
JW
208unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
209{
210 return instruction_pointer(regs);
211}
212
213int __weak kgdb_arch_init(void)
214{
215 return 0;
216}
217
b4b8ac52
JW
218int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
219{
220 return 0;
221}
222
223void __weak
224kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
225{
226 return;
227}
228
dc7d5527
JW
229/**
230 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
231 * @regs: Current &struct pt_regs.
232 *
233 * This function will be called if the particular architecture must
234 * disable hardware debugging while it is processing gdb packets or
235 * handling exception.
236 */
237void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
238{
239}
240
241/*
242 * GDB remote protocol parser:
243 */
244
dc7d5527
JW
245static int hex(char ch)
246{
247 if ((ch >= 'a') && (ch <= 'f'))
248 return ch - 'a' + 10;
249 if ((ch >= '0') && (ch <= '9'))
250 return ch - '0';
251 if ((ch >= 'A') && (ch <= 'F'))
252 return ch - 'A' + 10;
253 return -1;
254}
255
256/* scan for the sequence $<data>#<checksum> */
257static void get_packet(char *buffer)
258{
259 unsigned char checksum;
260 unsigned char xmitcsum;
261 int count;
262 char ch;
263
264 do {
265 /*
266 * Spin and wait around for the start character, ignore all
267 * other characters:
268 */
269 while ((ch = (kgdb_io_ops->read_char())) != '$')
270 /* nothing */;
271
272 kgdb_connected = 1;
273 checksum = 0;
274 xmitcsum = -1;
275
276 count = 0;
277
278 /*
279 * now, read until a # or end of buffer is found:
280 */
281 while (count < (BUFMAX - 1)) {
282 ch = kgdb_io_ops->read_char();
283 if (ch == '#')
284 break;
285 checksum = checksum + ch;
286 buffer[count] = ch;
287 count = count + 1;
288 }
289 buffer[count] = 0;
290
291 if (ch == '#') {
292 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
293 xmitcsum += hex(kgdb_io_ops->read_char());
294
295 if (checksum != xmitcsum)
296 /* failed checksum */
297 kgdb_io_ops->write_char('-');
298 else
299 /* successful transfer */
300 kgdb_io_ops->write_char('+');
301 if (kgdb_io_ops->flush)
302 kgdb_io_ops->flush();
303 }
304 } while (checksum != xmitcsum);
305}
306
307/*
308 * Send the packet in buffer.
309 * Check for gdb connection if asked for.
310 */
311static void put_packet(char *buffer)
312{
313 unsigned char checksum;
314 int count;
315 char ch;
316
317 /*
318 * $<packet info>#<checksum>.
319 */
320 while (1) {
321 kgdb_io_ops->write_char('$');
322 checksum = 0;
323 count = 0;
324
325 while ((ch = buffer[count])) {
326 kgdb_io_ops->write_char(ch);
327 checksum += ch;
328 count++;
329 }
330
331 kgdb_io_ops->write_char('#');
827e609b
HH
332 kgdb_io_ops->write_char(hex_asc_hi(checksum));
333 kgdb_io_ops->write_char(hex_asc_lo(checksum));
dc7d5527
JW
334 if (kgdb_io_ops->flush)
335 kgdb_io_ops->flush();
336
337 /* Now see what we get in reply. */
338 ch = kgdb_io_ops->read_char();
339
340 if (ch == 3)
341 ch = kgdb_io_ops->read_char();
342
343 /* If we get an ACK, we are done. */
344 if (ch == '+')
345 return;
346
347 /*
348 * If we get the start of another packet, this means
349 * that GDB is attempting to reconnect. We will NAK
350 * the packet being sent, and stop trying to send this
351 * packet.
352 */
353 if (ch == '$') {
354 kgdb_io_ops->write_char('-');
355 if (kgdb_io_ops->flush)
356 kgdb_io_ops->flush();
357 return;
358 }
359 }
360}
361
dc7d5527
JW
362/*
363 * Convert the memory pointed to by mem into hex, placing result in buf.
364 * Return a pointer to the last char put in buf (null). May return an error.
365 */
366int kgdb_mem2hex(char *mem, char *buf, int count)
367{
368 char *tmp;
369 int err;
370
371 /*
372 * We use the upper half of buf as an intermediate buffer for the
373 * raw memory copy. Hex conversion will work against this one.
374 */
375 tmp = buf + count;
376
377 err = probe_kernel_read(tmp, mem, count);
378 if (!err) {
379 while (count > 0) {
380 buf = pack_hex_byte(buf, *tmp);
381 tmp++;
382 count--;
383 }
384
385 *buf = 0;
386 }
387
388 return err;
389}
390
391/*
392 * Copy the binary array pointed to by buf into mem. Fix $, #, and
393 * 0x7d escaped with 0x7d. Return a pointer to the character after
394 * the last byte written.
395 */
396static int kgdb_ebin2mem(char *buf, char *mem, int count)
397{
398 int err = 0;
399 char c;
400
401 while (count-- > 0) {
402 c = *buf++;
403 if (c == 0x7d)
404 c = *buf++ ^ 0x20;
405
406 err = probe_kernel_write(mem, &c, 1);
407 if (err)
408 break;
409
410 mem++;
411 }
412
413 return err;
414}
415
416/*
417 * Convert the hex array pointed to by buf into binary to be placed in mem.
418 * Return a pointer to the character AFTER the last byte written.
419 * May return an error.
420 */
421int kgdb_hex2mem(char *buf, char *mem, int count)
422{
423 char *tmp_raw;
424 char *tmp_hex;
425
426 /*
427 * We use the upper half of buf as an intermediate buffer for the
428 * raw memory that is converted from hex.
429 */
430 tmp_raw = buf + count * 2;
431
432 tmp_hex = tmp_raw - 1;
433 while (tmp_hex >= buf) {
434 tmp_raw--;
435 *tmp_raw = hex(*tmp_hex--);
436 *tmp_raw |= hex(*tmp_hex--) << 4;
437 }
438
439 return probe_kernel_write(mem, tmp_raw, count);
440}
441
442/*
443 * While we find nice hex chars, build a long_val.
444 * Return number of chars processed.
445 */
688b744d 446int kgdb_hex2long(char **ptr, unsigned long *long_val)
dc7d5527
JW
447{
448 int hex_val;
449 int num = 0;
25fc9999 450 int negate = 0;
dc7d5527
JW
451
452 *long_val = 0;
453
25fc9999
JW
454 if (**ptr == '-') {
455 negate = 1;
456 (*ptr)++;
457 }
dc7d5527
JW
458 while (**ptr) {
459 hex_val = hex(**ptr);
460 if (hex_val < 0)
461 break;
462
463 *long_val = (*long_val << 4) | hex_val;
464 num++;
465 (*ptr)++;
466 }
467
25fc9999
JW
468 if (negate)
469 *long_val = -*long_val;
470
dc7d5527
JW
471 return num;
472}
473
474/* Write memory due to an 'M' or 'X' packet. */
475static int write_mem_msg(int binary)
476{
477 char *ptr = &remcom_in_buffer[1];
478 unsigned long addr;
479 unsigned long length;
480 int err;
481
482 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
483 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
484 if (binary)
485 err = kgdb_ebin2mem(ptr, (char *)addr, length);
486 else
487 err = kgdb_hex2mem(ptr, (char *)addr, length);
488 if (err)
489 return err;
490 if (CACHE_FLUSH_IS_SAFE)
18d6522b 491 flush_icache_range(addr, addr + length);
dc7d5527
JW
492 return 0;
493 }
494
495 return -EINVAL;
496}
497
498static void error_packet(char *pkt, int error)
499{
500 error = -error;
501 pkt[0] = 'E';
827e609b
HH
502 pkt[1] = hex_asc[(error / 10)];
503 pkt[2] = hex_asc[(error % 10)];
dc7d5527
JW
504 pkt[3] = '\0';
505}
506
507/*
508 * Thread ID accessors. We represent a flat TID space to GDB, where
509 * the per CPU idle threads (which under Linux all have PID 0) are
510 * remapped to negative TIDs.
511 */
512
513#define BUF_THREAD_ID_SIZE 16
514
515static char *pack_threadid(char *pkt, unsigned char *id)
516{
517 char *limit;
518
519 limit = pkt + BUF_THREAD_ID_SIZE;
520 while (pkt < limit)
521 pkt = pack_hex_byte(pkt, *id++);
522
523 return pkt;
524}
525
526static void int_to_threadref(unsigned char *id, int value)
527{
528 unsigned char *scan;
529 int i = 4;
530
531 scan = (unsigned char *)id;
532 while (i--)
533 *scan++ = 0;
827e609b 534 put_unaligned_be32(value, scan);
dc7d5527
JW
535}
536
537static struct task_struct *getthread(struct pt_regs *regs, int tid)
538{
539 /*
25fc9999 540 * Non-positive TIDs are remapped to the cpu shadow information
dc7d5527 541 */
25fc9999
JW
542 if (tid == 0 || tid == -1)
543 tid = -atomic_read(&kgdb_active) - 2;
84667d48 544 if (tid < -1 && tid > -NR_CPUS - 2) {
25fc9999
JW
545 if (kgdb_info[-tid - 2].task)
546 return kgdb_info[-tid - 2].task;
547 else
548 return idle_task(-tid - 2);
549 }
84667d48
JW
550 if (tid <= 0) {
551 printk(KERN_ERR "KGDB: Internal thread select error\n");
552 dump_stack();
553 return NULL;
554 }
dc7d5527
JW
555
556 /*
557 * find_task_by_pid_ns() does not take the tasklist lock anymore
558 * but is nicely RCU locked - hence is a pretty resilient
559 * thing to use:
560 */
561 return find_task_by_pid_ns(tid, &init_pid_ns);
562}
563
564/*
565 * CPU debug state control:
566 */
567
568#ifdef CONFIG_SMP
569static void kgdb_wait(struct pt_regs *regs)
570{
571 unsigned long flags;
572 int cpu;
573
574 local_irq_save(flags);
575 cpu = raw_smp_processor_id();
576 kgdb_info[cpu].debuggerinfo = regs;
577 kgdb_info[cpu].task = current;
578 /*
579 * Make sure the above info reaches the primary CPU before
580 * our cpu_in_kgdb[] flag setting does:
581 */
582 smp_wmb();
583 atomic_set(&cpu_in_kgdb[cpu], 1);
584
dc7d5527
JW
585 /* Wait till primary CPU is done with debugging */
586 while (atomic_read(&passive_cpu_wait[cpu]))
587 cpu_relax();
588
589 kgdb_info[cpu].debuggerinfo = NULL;
590 kgdb_info[cpu].task = NULL;
591
592 /* fix up hardware debug registers on local cpu */
593 if (arch_kgdb_ops.correct_hw_break)
594 arch_kgdb_ops.correct_hw_break();
595
596 /* Signal the primary CPU that we are done: */
597 atomic_set(&cpu_in_kgdb[cpu], 0);
cc1e0f4f 598 touch_softlockup_watchdog();
7c3078b6 599 clocksource_touch_watchdog();
dc7d5527
JW
600 local_irq_restore(flags);
601}
602#endif
603
604/*
605 * Some architectures need cache flushes when we set/clear a
606 * breakpoint:
607 */
608static void kgdb_flush_swbreak_addr(unsigned long addr)
609{
610 if (!CACHE_FLUSH_IS_SAFE)
611 return;
612
737a460f 613 if (current->mm && current->mm->mmap_cache) {
dc7d5527
JW
614 flush_cache_range(current->mm->mmap_cache,
615 addr, addr + BREAK_INSTR_SIZE);
dc7d5527 616 }
1a9a3e76
JW
617 /* Force flush instruction cache if it was outside the mm */
618 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
dc7d5527
JW
619}
620
621/*
622 * SW breakpoint management:
623 */
624static int kgdb_activate_sw_breakpoints(void)
625{
626 unsigned long addr;
627 int error = 0;
628 int i;
629
630 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
631 if (kgdb_break[i].state != BP_SET)
632 continue;
633
634 addr = kgdb_break[i].bpt_addr;
635 error = kgdb_arch_set_breakpoint(addr,
636 kgdb_break[i].saved_instr);
637 if (error)
638 return error;
639
640 kgdb_flush_swbreak_addr(addr);
641 kgdb_break[i].state = BP_ACTIVE;
642 }
643 return 0;
644}
645
646static int kgdb_set_sw_break(unsigned long addr)
647{
648 int err = kgdb_validate_break_address(addr);
649 int breakno = -1;
650 int i;
651
652 if (err)
653 return err;
654
655 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
656 if ((kgdb_break[i].state == BP_SET) &&
657 (kgdb_break[i].bpt_addr == addr))
658 return -EEXIST;
659 }
660 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
661 if (kgdb_break[i].state == BP_REMOVED &&
662 kgdb_break[i].bpt_addr == addr) {
663 breakno = i;
664 break;
665 }
666 }
667
668 if (breakno == -1) {
669 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
670 if (kgdb_break[i].state == BP_UNDEFINED) {
671 breakno = i;
672 break;
673 }
674 }
675 }
676
677 if (breakno == -1)
678 return -E2BIG;
679
680 kgdb_break[breakno].state = BP_SET;
681 kgdb_break[breakno].type = BP_BREAKPOINT;
682 kgdb_break[breakno].bpt_addr = addr;
683
684 return 0;
685}
686
687static int kgdb_deactivate_sw_breakpoints(void)
688{
689 unsigned long addr;
690 int error = 0;
691 int i;
692
693 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
694 if (kgdb_break[i].state != BP_ACTIVE)
695 continue;
696 addr = kgdb_break[i].bpt_addr;
697 error = kgdb_arch_remove_breakpoint(addr,
698 kgdb_break[i].saved_instr);
699 if (error)
700 return error;
701
702 kgdb_flush_swbreak_addr(addr);
703 kgdb_break[i].state = BP_SET;
704 }
705 return 0;
706}
707
708static int kgdb_remove_sw_break(unsigned long addr)
709{
710 int i;
711
712 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
713 if ((kgdb_break[i].state == BP_SET) &&
714 (kgdb_break[i].bpt_addr == addr)) {
715 kgdb_break[i].state = BP_REMOVED;
716 return 0;
717 }
718 }
719 return -ENOENT;
720}
721
722int kgdb_isremovedbreak(unsigned long addr)
723{
724 int i;
725
726 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
727 if ((kgdb_break[i].state == BP_REMOVED) &&
728 (kgdb_break[i].bpt_addr == addr))
729 return 1;
730 }
731 return 0;
732}
733
688b744d 734static int remove_all_break(void)
dc7d5527
JW
735{
736 unsigned long addr;
737 int error;
738 int i;
739
740 /* Clear memory breakpoints. */
741 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
737a460f
JW
742 if (kgdb_break[i].state != BP_ACTIVE)
743 goto setundefined;
dc7d5527
JW
744 addr = kgdb_break[i].bpt_addr;
745 error = kgdb_arch_remove_breakpoint(addr,
746 kgdb_break[i].saved_instr);
747 if (error)
737a460f
JW
748 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
749 addr);
750setundefined:
751 kgdb_break[i].state = BP_UNDEFINED;
dc7d5527
JW
752 }
753
754 /* Clear hardware breakpoints. */
755 if (arch_kgdb_ops.remove_all_hw_break)
756 arch_kgdb_ops.remove_all_hw_break();
757
758 return 0;
759}
760
761/*
25fc9999
JW
762 * Remap normal tasks to their real PID,
763 * CPU shadow threads are mapped to -CPU - 2
dc7d5527
JW
764 */
765static inline int shadow_pid(int realpid)
766{
767 if (realpid)
768 return realpid;
769
25fc9999 770 return -raw_smp_processor_id() - 2;
dc7d5527
JW
771}
772
773static char gdbmsgbuf[BUFMAX + 1];
774
775static void kgdb_msg_write(const char *s, int len)
776{
777 char *bufptr;
778 int wcount;
779 int i;
780
781 /* 'O'utput */
782 gdbmsgbuf[0] = 'O';
783
784 /* Fill and send buffers... */
785 while (len > 0) {
786 bufptr = gdbmsgbuf + 1;
787
788 /* Calculate how many this time */
789 if ((len << 1) > (BUFMAX - 2))
790 wcount = (BUFMAX - 2) >> 1;
791 else
792 wcount = len;
793
794 /* Pack in hex chars */
795 for (i = 0; i < wcount; i++)
796 bufptr = pack_hex_byte(bufptr, s[i]);
797 *bufptr = '\0';
798
799 /* Move up */
800 s += wcount;
801 len -= wcount;
802
803 /* Write packet */
804 put_packet(gdbmsgbuf);
805 }
806}
807
808/*
809 * Return true if there is a valid kgdb I/O module. Also if no
810 * debugger is attached a message can be printed to the console about
811 * waiting for the debugger to attach.
812 *
813 * The print_wait argument is only to be true when called from inside
814 * the core kgdb_handle_exception, because it will wait for the
815 * debugger to attach.
816 */
817static int kgdb_io_ready(int print_wait)
818{
819 if (!kgdb_io_ops)
820 return 0;
821 if (kgdb_connected)
822 return 1;
823 if (atomic_read(&kgdb_setting_breakpoint))
824 return 1;
825 if (print_wait)
826 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
827 return 1;
828}
829
830/*
831 * All the functions that start with gdb_cmd are the various
832 * operations to implement the handlers for the gdbserial protocol
833 * where KGDB is communicating with an external debugger
834 */
835
836/* Handle the '?' status packets */
837static void gdb_cmd_status(struct kgdb_state *ks)
838{
839 /*
840 * We know that this packet is only sent
841 * during initial connect. So to be safe,
842 * we clear out our breakpoints now in case
843 * GDB is reconnecting.
844 */
845 remove_all_break();
846
847 remcom_out_buffer[0] = 'S';
848 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
849}
850
851/* Handle the 'g' get registers request */
852static void gdb_cmd_getregs(struct kgdb_state *ks)
853{
854 struct task_struct *thread;
855 void *local_debuggerinfo;
856 int i;
857
858 thread = kgdb_usethread;
859 if (!thread) {
860 thread = kgdb_info[ks->cpu].task;
861 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
862 } else {
863 local_debuggerinfo = NULL;
25fc9999 864 for_each_online_cpu(i) {
dc7d5527
JW
865 /*
866 * Try to find the task on some other
867 * or possibly this node if we do not
868 * find the matching task then we try
869 * to approximate the results.
870 */
871 if (thread == kgdb_info[i].task)
872 local_debuggerinfo = kgdb_info[i].debuggerinfo;
873 }
874 }
875
876 /*
877 * All threads that don't have debuggerinfo should be
1477b6a7 878 * in schedule() sleeping, since all other CPUs
dc7d5527
JW
879 * are in kgdb_wait, and thus have debuggerinfo.
880 */
881 if (local_debuggerinfo) {
882 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
883 } else {
884 /*
885 * Pull stuff saved during switch_to; nothing
886 * else is accessible (or even particularly
887 * relevant).
888 *
889 * This should be enough for a stack trace.
890 */
891 sleeping_thread_to_gdb_regs(gdb_regs, thread);
892 }
893 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
894}
895
896/* Handle the 'G' set registers request */
897static void gdb_cmd_setregs(struct kgdb_state *ks)
898{
899 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
900
901 if (kgdb_usethread && kgdb_usethread != current) {
902 error_packet(remcom_out_buffer, -EINVAL);
903 } else {
904 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
905 strcpy(remcom_out_buffer, "OK");
906 }
907}
908
909/* Handle the 'm' memory read bytes */
910static void gdb_cmd_memread(struct kgdb_state *ks)
911{
912 char *ptr = &remcom_in_buffer[1];
913 unsigned long length;
914 unsigned long addr;
915 int err;
916
917 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
918 kgdb_hex2long(&ptr, &length) > 0) {
919 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
920 if (err)
921 error_packet(remcom_out_buffer, err);
922 } else {
923 error_packet(remcom_out_buffer, -EINVAL);
924 }
925}
926
927/* Handle the 'M' memory write bytes */
928static void gdb_cmd_memwrite(struct kgdb_state *ks)
929{
930 int err = write_mem_msg(0);
931
932 if (err)
933 error_packet(remcom_out_buffer, err);
934 else
935 strcpy(remcom_out_buffer, "OK");
936}
937
938/* Handle the 'X' memory binary write bytes */
939static void gdb_cmd_binwrite(struct kgdb_state *ks)
940{
941 int err = write_mem_msg(1);
942
943 if (err)
944 error_packet(remcom_out_buffer, err);
945 else
946 strcpy(remcom_out_buffer, "OK");
947}
948
949/* Handle the 'D' or 'k', detach or kill packets */
950static void gdb_cmd_detachkill(struct kgdb_state *ks)
951{
952 int error;
953
954 /* The detach case */
955 if (remcom_in_buffer[0] == 'D') {
956 error = remove_all_break();
957 if (error < 0) {
958 error_packet(remcom_out_buffer, error);
959 } else {
960 strcpy(remcom_out_buffer, "OK");
961 kgdb_connected = 0;
962 }
963 put_packet(remcom_out_buffer);
964 } else {
965 /*
966 * Assume the kill case, with no exit code checking,
967 * trying to force detach the debugger:
968 */
969 remove_all_break();
970 kgdb_connected = 0;
971 }
972}
973
974/* Handle the 'R' reboot packets */
975static int gdb_cmd_reboot(struct kgdb_state *ks)
976{
977 /* For now, only honor R0 */
978 if (strcmp(remcom_in_buffer, "R0") == 0) {
979 printk(KERN_CRIT "Executing emergency reboot\n");
980 strcpy(remcom_out_buffer, "OK");
981 put_packet(remcom_out_buffer);
982
983 /*
984 * Execution should not return from
985 * machine_emergency_restart()
986 */
987 machine_emergency_restart();
988 kgdb_connected = 0;
989
990 return 1;
991 }
992 return 0;
993}
994
995/* Handle the 'q' query packets */
996static void gdb_cmd_query(struct kgdb_state *ks)
997{
25fc9999
JW
998 struct task_struct *g;
999 struct task_struct *p;
dc7d5527
JW
1000 unsigned char thref[8];
1001 char *ptr;
1002 int i;
25fc9999
JW
1003 int cpu;
1004 int finished = 0;
dc7d5527
JW
1005
1006 switch (remcom_in_buffer[1]) {
1007 case 's':
1008 case 'f':
1009 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
1010 error_packet(remcom_out_buffer, -EINVAL);
1011 break;
1012 }
1013
25fc9999 1014 i = 0;
dc7d5527
JW
1015 remcom_out_buffer[0] = 'm';
1016 ptr = remcom_out_buffer + 1;
25fc9999
JW
1017 if (remcom_in_buffer[1] == 'f') {
1018 /* Each cpu is a shadow thread */
1019 for_each_online_cpu(cpu) {
1020 ks->thr_query = 0;
1021 int_to_threadref(thref, -cpu - 2);
dc7d5527
JW
1022 pack_threadid(ptr, thref);
1023 ptr += BUF_THREAD_ID_SIZE;
1024 *(ptr++) = ',';
1025 i++;
1026 }
1027 }
25fc9999
JW
1028
1029 do_each_thread(g, p) {
1030 if (i >= ks->thr_query && !finished) {
1031 int_to_threadref(thref, p->pid);
1032 pack_threadid(ptr, thref);
1033 ptr += BUF_THREAD_ID_SIZE;
1034 *(ptr++) = ',';
1035 ks->thr_query++;
1036 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
1037 finished = 1;
1038 }
1039 i++;
1040 } while_each_thread(g, p);
1041
dc7d5527
JW
1042 *(--ptr) = '\0';
1043 break;
1044
1045 case 'C':
1046 /* Current thread id */
1047 strcpy(remcom_out_buffer, "QC");
1048 ks->threadid = shadow_pid(current->pid);
1049 int_to_threadref(thref, ks->threadid);
1050 pack_threadid(remcom_out_buffer + 2, thref);
1051 break;
1052 case 'T':
1053 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1054 error_packet(remcom_out_buffer, -EINVAL);
1055 break;
1056 }
1057 ks->threadid = 0;
1058 ptr = remcom_in_buffer + 17;
1059 kgdb_hex2long(&ptr, &ks->threadid);
1060 if (!getthread(ks->linux_regs, ks->threadid)) {
1061 error_packet(remcom_out_buffer, -EINVAL);
1062 break;
1063 }
25fc9999 1064 if ((int)ks->threadid > 0) {
dc7d5527
JW
1065 kgdb_mem2hex(getthread(ks->linux_regs,
1066 ks->threadid)->comm,
1067 remcom_out_buffer, 16);
1068 } else {
1069 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1070
25fc9999
JW
1071 sprintf(tmpstr, "shadowCPU%d",
1072 (int)(-ks->threadid - 2));
dc7d5527
JW
1073 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1074 }
1075 break;
1076 }
1077}
1078
1079/* Handle the 'H' task query packets */
1080static void gdb_cmd_task(struct kgdb_state *ks)
1081{
1082 struct task_struct *thread;
1083 char *ptr;
1084
1085 switch (remcom_in_buffer[1]) {
1086 case 'g':
1087 ptr = &remcom_in_buffer[2];
1088 kgdb_hex2long(&ptr, &ks->threadid);
1089 thread = getthread(ks->linux_regs, ks->threadid);
1090 if (!thread && ks->threadid > 0) {
1091 error_packet(remcom_out_buffer, -EINVAL);
1092 break;
1093 }
1094 kgdb_usethread = thread;
1095 ks->kgdb_usethreadid = ks->threadid;
1096 strcpy(remcom_out_buffer, "OK");
1097 break;
1098 case 'c':
1099 ptr = &remcom_in_buffer[2];
1100 kgdb_hex2long(&ptr, &ks->threadid);
1101 if (!ks->threadid) {
1102 kgdb_contthread = NULL;
1103 } else {
1104 thread = getthread(ks->linux_regs, ks->threadid);
1105 if (!thread && ks->threadid > 0) {
1106 error_packet(remcom_out_buffer, -EINVAL);
1107 break;
1108 }
1109 kgdb_contthread = thread;
1110 }
1111 strcpy(remcom_out_buffer, "OK");
1112 break;
1113 }
1114}
1115
1116/* Handle the 'T' thread query packets */
1117static void gdb_cmd_thread(struct kgdb_state *ks)
1118{
1119 char *ptr = &remcom_in_buffer[1];
1120 struct task_struct *thread;
1121
1122 kgdb_hex2long(&ptr, &ks->threadid);
1123 thread = getthread(ks->linux_regs, ks->threadid);
1124 if (thread)
1125 strcpy(remcom_out_buffer, "OK");
1126 else
1127 error_packet(remcom_out_buffer, -EINVAL);
1128}
1129
1130/* Handle the 'z' or 'Z' breakpoint remove or set packets */
1131static void gdb_cmd_break(struct kgdb_state *ks)
1132{
1133 /*
1134 * Since GDB-5.3, it's been drafted that '0' is a software
1135 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1136 */
1137 char *bpt_type = &remcom_in_buffer[1];
1138 char *ptr = &remcom_in_buffer[2];
1139 unsigned long addr;
1140 unsigned long length;
1141 int error = 0;
1142
1143 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1144 /* Unsupported */
1145 if (*bpt_type > '4')
1146 return;
1147 } else {
1148 if (*bpt_type != '0' && *bpt_type != '1')
1149 /* Unsupported. */
1150 return;
1151 }
1152
1153 /*
1154 * Test if this is a hardware breakpoint, and
1155 * if we support it:
1156 */
1157 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1158 /* Unsupported. */
1159 return;
1160
1161 if (*(ptr++) != ',') {
1162 error_packet(remcom_out_buffer, -EINVAL);
1163 return;
1164 }
1165 if (!kgdb_hex2long(&ptr, &addr)) {
1166 error_packet(remcom_out_buffer, -EINVAL);
1167 return;
1168 }
1169 if (*(ptr++) != ',' ||
1170 !kgdb_hex2long(&ptr, &length)) {
1171 error_packet(remcom_out_buffer, -EINVAL);
1172 return;
1173 }
1174
1175 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1176 error = kgdb_set_sw_break(addr);
1177 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1178 error = kgdb_remove_sw_break(addr);
1179 else if (remcom_in_buffer[0] == 'Z')
1180 error = arch_kgdb_ops.set_hw_breakpoint(addr,
64e9ee30 1181 (int)length, *bpt_type - '0');
dc7d5527
JW
1182 else if (remcom_in_buffer[0] == 'z')
1183 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
64e9ee30 1184 (int) length, *bpt_type - '0');
dc7d5527
JW
1185
1186 if (error == 0)
1187 strcpy(remcom_out_buffer, "OK");
1188 else
1189 error_packet(remcom_out_buffer, error);
1190}
1191
1192/* Handle the 'C' signal / exception passing packets */
1193static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1194{
1195 /* C09 == pass exception
1196 * C15 == detach kgdb, pass exception
1197 */
1198 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1199
1200 ks->pass_exception = 1;
1201 remcom_in_buffer[0] = 'c';
1202
1203 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1204
1205 ks->pass_exception = 1;
1206 remcom_in_buffer[0] = 'D';
1207 remove_all_break();
1208 kgdb_connected = 0;
1209 return 1;
1210
1211 } else {
1212 error_packet(remcom_out_buffer, -EINVAL);
1213 return 0;
1214 }
1215
1216 /* Indicate fall through */
1217 return -1;
1218}
1219
1220/*
1221 * This function performs all gdbserial command procesing
1222 */
1223static int gdb_serial_stub(struct kgdb_state *ks)
1224{
1225 int error = 0;
1226 int tmp;
1227
1228 /* Clear the out buffer. */
1229 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1230
1231 if (kgdb_connected) {
1232 unsigned char thref[8];
1233 char *ptr;
1234
1235 /* Reply to host that an exception has occurred */
1236 ptr = remcom_out_buffer;
1237 *ptr++ = 'T';
1238 ptr = pack_hex_byte(ptr, ks->signo);
1239 ptr += strlen(strcpy(ptr, "thread:"));
1240 int_to_threadref(thref, shadow_pid(current->pid));
1241 ptr = pack_threadid(ptr, thref);
1242 *ptr++ = ';';
1243 put_packet(remcom_out_buffer);
1244 }
1245
1246 kgdb_usethread = kgdb_info[ks->cpu].task;
1247 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1248 ks->pass_exception = 0;
1249
1250 while (1) {
1251 error = 0;
1252
1253 /* Clear the out buffer. */
1254 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1255
1256 get_packet(remcom_in_buffer);
1257
1258 switch (remcom_in_buffer[0]) {
1259 case '?': /* gdbserial status */
1260 gdb_cmd_status(ks);
1261 break;
1262 case 'g': /* return the value of the CPU registers */
1263 gdb_cmd_getregs(ks);
1264 break;
1265 case 'G': /* set the value of the CPU registers - return OK */
1266 gdb_cmd_setregs(ks);
1267 break;
1268 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1269 gdb_cmd_memread(ks);
1270 break;
1271 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1272 gdb_cmd_memwrite(ks);
1273 break;
1274 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1275 gdb_cmd_binwrite(ks);
1276 break;
1277 /* kill or detach. KGDB should treat this like a
1278 * continue.
1279 */
1280 case 'D': /* Debugger detach */
1281 case 'k': /* Debugger detach via kill */
1282 gdb_cmd_detachkill(ks);
1283 goto default_handle;
1284 case 'R': /* Reboot */
1285 if (gdb_cmd_reboot(ks))
1286 goto default_handle;
1287 break;
1288 case 'q': /* query command */
1289 gdb_cmd_query(ks);
1290 break;
1291 case 'H': /* task related */
1292 gdb_cmd_task(ks);
1293 break;
1294 case 'T': /* Query thread status */
1295 gdb_cmd_thread(ks);
1296 break;
1297 case 'z': /* Break point remove */
1298 case 'Z': /* Break point set */
1299 gdb_cmd_break(ks);
1300 break;
1301 case 'C': /* Exception passing */
1302 tmp = gdb_cmd_exception_pass(ks);
1303 if (tmp > 0)
1304 goto default_handle;
1305 if (tmp == 0)
1306 break;
1307 /* Fall through on tmp < 0 */
1308 case 'c': /* Continue packet */
1309 case 's': /* Single step packet */
1310 if (kgdb_contthread && kgdb_contthread != current) {
1311 /* Can't switch threads in kgdb */
1312 error_packet(remcom_out_buffer, -EINVAL);
1313 break;
1314 }
1315 kgdb_activate_sw_breakpoints();
1316 /* Fall through to default processing */
1317 default:
1318default_handle:
1319 error = kgdb_arch_handle_exception(ks->ex_vector,
1320 ks->signo,
1321 ks->err_code,
1322 remcom_in_buffer,
1323 remcom_out_buffer,
1324 ks->linux_regs);
1325 /*
1326 * Leave cmd processing on error, detach,
1327 * kill, continue, or single step.
1328 */
1329 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1330 remcom_in_buffer[0] == 'k') {
1331 error = 0;
1332 goto kgdb_exit;
1333 }
1334
1335 }
1336
1337 /* reply to the request */
1338 put_packet(remcom_out_buffer);
1339 }
1340
1341kgdb_exit:
1342 if (ks->pass_exception)
1343 error = 1;
1344 return error;
1345}
1346
1347static int kgdb_reenter_check(struct kgdb_state *ks)
1348{
1349 unsigned long addr;
1350
1351 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1352 return 0;
1353
1354 /* Panic on recursive debugger calls: */
1355 exception_level++;
1356 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1357 kgdb_deactivate_sw_breakpoints();
1358
1359 /*
1360 * If the break point removed ok at the place exception
1361 * occurred, try to recover and print a warning to the end
1362 * user because the user planted a breakpoint in a place that
1363 * KGDB needs in order to function.
1364 */
1365 if (kgdb_remove_sw_break(addr) == 0) {
1366 exception_level = 0;
1367 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1368 kgdb_activate_sw_breakpoints();
67baf94c
JW
1369 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1370 addr);
dc7d5527
JW
1371 WARN_ON_ONCE(1);
1372
1373 return 1;
1374 }
1375 remove_all_break();
1376 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1377
1378 if (exception_level > 1) {
1379 dump_stack();
1380 panic("Recursive entry to debugger");
1381 }
1382
1383 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1384 dump_stack();
1385 panic("Recursive entry to debugger");
1386
1387 return 1;
1388}
1389
1390/*
1391 * kgdb_handle_exception() - main entry point from a kernel exception
1392 *
1393 * Locking hierarchy:
1394 * interface locks, if any (begin_session)
1395 * kgdb lock (kgdb_active)
1396 */
1397int
1398kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1399{
1400 struct kgdb_state kgdb_var;
1401 struct kgdb_state *ks = &kgdb_var;
1402 unsigned long flags;
1403 int error = 0;
1404 int i, cpu;
1405
1406 ks->cpu = raw_smp_processor_id();
1407 ks->ex_vector = evector;
1408 ks->signo = signo;
1409 ks->ex_vector = evector;
1410 ks->err_code = ecode;
1411 ks->kgdb_usethreadid = 0;
1412 ks->linux_regs = regs;
1413
1414 if (kgdb_reenter_check(ks))
1415 return 0; /* Ouch, double exception ! */
1416
1417acquirelock:
1418 /*
1419 * Interrupts will be restored by the 'trap return' code, except when
1420 * single stepping.
1421 */
1422 local_irq_save(flags);
1423
1424 cpu = raw_smp_processor_id();
1425
1426 /*
1427 * Acquire the kgdb_active lock:
1428 */
1429 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1430 cpu_relax();
1431
1432 /*
1433 * Do not start the debugger connection on this CPU if the last
1434 * instance of the exception handler wanted to come into the
1435 * debugger on a different CPU via a single step
1436 */
1437 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1438 atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1439
1440 atomic_set(&kgdb_active, -1);
cc1e0f4f 1441 touch_softlockup_watchdog();
7c3078b6 1442 clocksource_touch_watchdog();
dc7d5527
JW
1443 local_irq_restore(flags);
1444
1445 goto acquirelock;
1446 }
1447
1448 if (!kgdb_io_ready(1)) {
1449 error = 1;
1450 goto kgdb_restore; /* No I/O connection, so resume the system */
1451 }
1452
1453 /*
1454 * Don't enter if we have hit a removed breakpoint.
1455 */
1456 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1457 goto kgdb_restore;
1458
1459 /* Call the I/O driver's pre_exception routine */
1460 if (kgdb_io_ops->pre_exception)
1461 kgdb_io_ops->pre_exception();
1462
1463 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1464 kgdb_info[ks->cpu].task = current;
1465
1466 kgdb_disable_hw_debug(ks->linux_regs);
1467
1468 /*
1469 * Get the passive CPU lock which will hold all the non-primary
1470 * CPU in a spin state while the debugger is active
1471 */
d7161a65 1472 if (!kgdb_single_step) {
dc7d5527
JW
1473 for (i = 0; i < NR_CPUS; i++)
1474 atomic_set(&passive_cpu_wait[i], 1);
1475 }
1476
dc7d5527
JW
1477 /*
1478 * spin_lock code is good enough as a barrier so we don't
1479 * need one here:
1480 */
1481 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1482
56fb7093
JW
1483#ifdef CONFIG_SMP
1484 /* Signal the other CPUs to enter kgdb_wait() */
d7161a65 1485 if ((!kgdb_single_step) && kgdb_do_roundup)
56fb7093
JW
1486 kgdb_roundup_cpus(flags);
1487#endif
1488
dc7d5527
JW
1489 /*
1490 * Wait for the other CPUs to be notified and be waiting for us:
1491 */
1492 for_each_online_cpu(i) {
1493 while (!atomic_read(&cpu_in_kgdb[i]))
1494 cpu_relax();
1495 }
1496
1497 /*
1498 * At this point the primary processor is completely
1499 * in the debugger and all secondary CPUs are quiescent
1500 */
1501 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1502 kgdb_deactivate_sw_breakpoints();
1503 kgdb_single_step = 0;
d7161a65 1504 kgdb_contthread = current;
dc7d5527
JW
1505 exception_level = 0;
1506
1507 /* Talk to debugger with gdbserial protocol */
1508 error = gdb_serial_stub(ks);
1509
1510 /* Call the I/O driver's post_exception routine */
1511 if (kgdb_io_ops->post_exception)
1512 kgdb_io_ops->post_exception();
1513
1514 kgdb_info[ks->cpu].debuggerinfo = NULL;
1515 kgdb_info[ks->cpu].task = NULL;
1516 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1517
d7161a65 1518 if (!kgdb_single_step) {
dc7d5527
JW
1519 for (i = NR_CPUS-1; i >= 0; i--)
1520 atomic_set(&passive_cpu_wait[i], 0);
1521 /*
1522 * Wait till all the CPUs have quit
1523 * from the debugger.
1524 */
1525 for_each_online_cpu(i) {
1526 while (atomic_read(&cpu_in_kgdb[i]))
1527 cpu_relax();
1528 }
1529 }
1530
1531kgdb_restore:
1532 /* Free kgdb_active */
1533 atomic_set(&kgdb_active, -1);
cc1e0f4f 1534 touch_softlockup_watchdog();
7c3078b6 1535 clocksource_touch_watchdog();
dc7d5527
JW
1536 local_irq_restore(flags);
1537
1538 return error;
1539}
1540
1541int kgdb_nmicallback(int cpu, void *regs)
1542{
1543#ifdef CONFIG_SMP
1544 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
56fb7093
JW
1545 atomic_read(&kgdb_active) != cpu &&
1546 atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
dc7d5527
JW
1547 kgdb_wait((struct pt_regs *)regs);
1548 return 0;
1549 }
1550#endif
1551 return 1;
1552}
1553
aabdc3b8
JW
1554static void kgdb_console_write(struct console *co, const char *s,
1555 unsigned count)
dc7d5527
JW
1556{
1557 unsigned long flags;
1558
1559 /* If we're debugging, or KGDB has not connected, don't try
1560 * and print. */
1561 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1562 return;
1563
1564 local_irq_save(flags);
1565 kgdb_msg_write(s, count);
1566 local_irq_restore(flags);
1567}
1568
1569static struct console kgdbcons = {
1570 .name = "kgdb",
1571 .write = kgdb_console_write,
1572 .flags = CON_PRINTBUFFER | CON_ENABLED,
1573 .index = -1,
1574};
1575
1576#ifdef CONFIG_MAGIC_SYSRQ
1577static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1578{
1579 if (!kgdb_io_ops) {
1580 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1581 return;
1582 }
1583 if (!kgdb_connected)
1584 printk(KERN_CRIT "Entering KGDB\n");
1585
1586 kgdb_breakpoint();
1587}
1588
1589static struct sysrq_key_op sysrq_gdb_op = {
1590 .handler = sysrq_handle_gdb,
364b5b7b
JW
1591 .help_msg = "debug(G)",
1592 .action_msg = "DEBUG",
dc7d5527
JW
1593};
1594#endif
1595
1596static void kgdb_register_callbacks(void)
1597{
1598 if (!kgdb_io_module_registered) {
1599 kgdb_io_module_registered = 1;
1600 kgdb_arch_init();
1601#ifdef CONFIG_MAGIC_SYSRQ
1602 register_sysrq_key('g', &sysrq_gdb_op);
1603#endif
1604 if (kgdb_use_con && !kgdb_con_registered) {
1605 register_console(&kgdbcons);
1606 kgdb_con_registered = 1;
1607 }
1608 }
1609}
1610
1611static void kgdb_unregister_callbacks(void)
1612{
1613 /*
1614 * When this routine is called KGDB should unregister from the
1615 * panic handler and clean up, making sure it is not handling any
1616 * break exceptions at the time.
1617 */
1618 if (kgdb_io_module_registered) {
1619 kgdb_io_module_registered = 0;
1620 kgdb_arch_exit();
1621#ifdef CONFIG_MAGIC_SYSRQ
1622 unregister_sysrq_key('g', &sysrq_gdb_op);
1623#endif
1624 if (kgdb_con_registered) {
1625 unregister_console(&kgdbcons);
1626 kgdb_con_registered = 0;
1627 }
1628 }
1629}
1630
1631static void kgdb_initial_breakpoint(void)
1632{
1633 kgdb_break_asap = 0;
1634
1635 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1636 kgdb_breakpoint();
1637}
1638
1639/**
737a460f 1640 * kgdb_register_io_module - register KGDB IO module
dc7d5527
JW
1641 * @new_kgdb_io_ops: the io ops vector
1642 *
1643 * Register it with the KGDB core.
1644 */
1645int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1646{
1647 int err;
1648
1649 spin_lock(&kgdb_registration_lock);
1650
1651 if (kgdb_io_ops) {
1652 spin_unlock(&kgdb_registration_lock);
1653
1654 printk(KERN_ERR "kgdb: Another I/O driver is already "
1655 "registered with KGDB.\n");
1656 return -EBUSY;
1657 }
1658
1659 if (new_kgdb_io_ops->init) {
1660 err = new_kgdb_io_ops->init();
1661 if (err) {
1662 spin_unlock(&kgdb_registration_lock);
1663 return err;
1664 }
1665 }
1666
1667 kgdb_io_ops = new_kgdb_io_ops;
1668
1669 spin_unlock(&kgdb_registration_lock);
1670
1671 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1672 new_kgdb_io_ops->name);
1673
1674 /* Arm KGDB now. */
1675 kgdb_register_callbacks();
1676
1677 if (kgdb_break_asap)
1678 kgdb_initial_breakpoint();
1679
1680 return 0;
1681}
1682EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1683
1684/**
1685 * kkgdb_unregister_io_module - unregister KGDB IO module
1686 * @old_kgdb_io_ops: the io ops vector
1687 *
1688 * Unregister it with the KGDB core.
1689 */
1690void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1691{
1692 BUG_ON(kgdb_connected);
1693
1694 /*
1695 * KGDB is no longer able to communicate out, so
1696 * unregister our callbacks and reset state.
1697 */
1698 kgdb_unregister_callbacks();
1699
1700 spin_lock(&kgdb_registration_lock);
1701
1702 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1703 kgdb_io_ops = NULL;
1704
1705 spin_unlock(&kgdb_registration_lock);
1706
1707 printk(KERN_INFO
1708 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1709 old_kgdb_io_ops->name);
1710}
1711EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1712
1713/**
1714 * kgdb_breakpoint - generate breakpoint exception
1715 *
1716 * This function will generate a breakpoint exception. It is used at the
1717 * beginning of a program to sync up with a debugger and can be used
1718 * otherwise as a quick means to stop program execution and "break" into
1719 * the debugger.
1720 */
1721void kgdb_breakpoint(void)
1722{
1723 atomic_set(&kgdb_setting_breakpoint, 1);
1724 wmb(); /* Sync point before breakpoint */
1725 arch_kgdb_breakpoint();
1726 wmb(); /* Sync point after breakpoint */
1727 atomic_set(&kgdb_setting_breakpoint, 0);
1728}
1729EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1730
1731static int __init opt_kgdb_wait(char *str)
1732{
1733 kgdb_break_asap = 1;
1734
1735 if (kgdb_io_module_registered)
1736 kgdb_initial_breakpoint();
1737
1738 return 0;
1739}
1740
1741early_param("kgdbwait", opt_kgdb_wait);
This page took 0.211661 seconds and 5 git commands to generate.