1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/pid_namespace.h>
31#include <linux/clocksource.h>
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>
55#include <asm/unaligned.h>
56
57static int kgdb_break_asap;
58
59#define KGDB_MAX_THREAD_QUERY 17
60struct kgdb_state {
61 int ex_vector;
62 int signo;
63 int err_code;
64 int cpu;
65 int pass_exception;
66 unsigned long thr_query;
67 unsigned long threadid;
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
79
80int kgdb_connected;
81EXPORT_SYMBOL_GPL(kgdb_connected);
82
83
84static int kgdb_io_module_registered;
85
86
87static int exception_level;
88
89static struct kgdb_io *kgdb_io_ops;
90static DEFINE_SPINLOCK(kgdb_registration_lock);
91
92
93static int kgdb_con_registered;
94
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
109
110
111static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
112 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
113};
114
115
116
117
118atomic_t kgdb_active = ATOMIC_INIT(-1);
119
120
121
122
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;
132pid_t kgdb_sstep_pid;
133
134
135static char remcom_in_buffer[BUFMAX];
136static char remcom_out_buffer[BUFMAX];
137
138
139static unsigned long gdb_regs[(NUMREGBYTES +
140 sizeof(unsigned long) - 1) /
141 sizeof(unsigned long)];
142
143
144atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
145
146
147
148
149
150
151
152
153static int kgdb_do_roundup = 1;
154
155static int __init opt_nokgdbroundup(char *str)
156{
157 kgdb_do_roundup = 0;
158
159 return 0;
160}
161
162early_param("nokgdbroundup", opt_nokgdbroundup);
163
164
165
166
167
168
169
170
171
172int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
173{
174 int err;
175
176 err = probe_kernel_read(saved_instr, (char *)addr, BREAK_INSTR_SIZE);
177 if (err)
178 return err;
179
180 return probe_kernel_write((char *)addr, arch_kgdb_ops.gdb_bpt_instr,
181 BREAK_INSTR_SIZE);
182}
183
184int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
185{
186 return probe_kernel_write((char *)addr,
187 (char *)bundle, BREAK_INSTR_SIZE);
188}
189
190int __weak kgdb_validate_break_address(unsigned long addr)
191{
192 char tmp_variable[BREAK_INSTR_SIZE];
193 int err;
194
195
196
197
198
199 err = kgdb_arch_set_breakpoint(addr, tmp_variable);
200 if (err)
201 return err;
202 err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
203 if (err)
204 printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
205 "memory destroyed at: %lx", addr);
206 return err;
207}
208
209unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
210{
211 return instruction_pointer(regs);
212}
213
214int __weak kgdb_arch_init(void)
215{
216 return 0;
217}
218
219int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
220{
221 return 0;
222}
223
224void __weak
225kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
226{
227 return;
228}
229
230
231
232
233
234
235
236
237
238void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
239{
240}
241
242
243
244
245
246static int hex(char ch)
247{
248 if ((ch >= 'a') && (ch <= 'f'))
249 return ch - 'a' + 10;
250 if ((ch >= '0') && (ch <= '9'))
251 return ch - '0';
252 if ((ch >= 'A') && (ch <= 'F'))
253 return ch - 'A' + 10;
254 return -1;
255}
256
257
258static void get_packet(char *buffer)
259{
260 unsigned char checksum;
261 unsigned char xmitcsum;
262 int count;
263 char ch;
264
265 do {
266
267
268
269
270 while ((ch = (kgdb_io_ops->read_char())) != '$')
271 ;
272
273 kgdb_connected = 1;
274 checksum = 0;
275 xmitcsum = -1;
276
277 count = 0;
278
279
280
281
282 while (count < (BUFMAX - 1)) {
283 ch = kgdb_io_ops->read_char();
284 if (ch == '#')
285 break;
286 checksum = checksum + ch;
287 buffer[count] = ch;
288 count = count + 1;
289 }
290 buffer[count] = 0;
291
292 if (ch == '#') {
293 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
294 xmitcsum += hex(kgdb_io_ops->read_char());
295
296 if (checksum != xmitcsum)
297
298 kgdb_io_ops->write_char('-');
299 else
300
301 kgdb_io_ops->write_char('+');
302 if (kgdb_io_ops->flush)
303 kgdb_io_ops->flush();
304 }
305 } while (checksum != xmitcsum);
306}
307
308
309
310
311
312static void put_packet(char *buffer)
313{
314 unsigned char checksum;
315 int count;
316 char ch;
317
318
319
320
321 while (1) {
322 kgdb_io_ops->write_char('$');
323 checksum = 0;
324 count = 0;
325
326 while ((ch = buffer[count])) {
327 kgdb_io_ops->write_char(ch);
328 checksum += ch;
329 count++;
330 }
331
332 kgdb_io_ops->write_char('#');
333 kgdb_io_ops->write_char(hex_asc_hi(checksum));
334 kgdb_io_ops->write_char(hex_asc_lo(checksum));
335 if (kgdb_io_ops->flush)
336 kgdb_io_ops->flush();
337
338
339 ch = kgdb_io_ops->read_char();
340
341 if (ch == 3)
342 ch = kgdb_io_ops->read_char();
343
344
345 if (ch == '+')
346 return;
347
348
349
350
351
352
353
354 if (ch == '$') {
355 kgdb_io_ops->write_char('-');
356 if (kgdb_io_ops->flush)
357 kgdb_io_ops->flush();
358 return;
359 }
360 }
361}
362
363
364
365
366
367int kgdb_mem2hex(char *mem, char *buf, int count)
368{
369 char *tmp;
370 int err;
371
372
373
374
375
376 tmp = buf + count;
377
378 err = probe_kernel_read(tmp, mem, count);
379 if (!err) {
380 while (count > 0) {
381 buf = pack_hex_byte(buf, *tmp);
382 tmp++;
383 count--;
384 }
385
386 *buf = 0;
387 }
388
389 return err;
390}
391
392
393
394
395
396
397static int kgdb_ebin2mem(char *buf, char *mem, int count)
398{
399 int err = 0;
400 char c;
401
402 while (count-- > 0) {
403 c = *buf++;
404 if (c == 0x7d)
405 c = *buf++ ^ 0x20;
406
407 err = probe_kernel_write(mem, &c, 1);
408 if (err)
409 break;
410
411 mem++;
412 }
413
414 return err;
415}
416
417
418
419
420
421
422int kgdb_hex2mem(char *buf, char *mem, int count)
423{
424 char *tmp_raw;
425 char *tmp_hex;
426
427
428
429
430
431 tmp_raw = buf + count * 2;
432
433 tmp_hex = tmp_raw - 1;
434 while (tmp_hex >= buf) {
435 tmp_raw--;
436 *tmp_raw = hex(*tmp_hex--);
437 *tmp_raw |= hex(*tmp_hex--) << 4;
438 }
439
440 return probe_kernel_write(mem, tmp_raw, count);
441}
442
443
444
445
446
447int kgdb_hex2long(char **ptr, unsigned long *long_val)
448{
449 int hex_val;
450 int num = 0;
451 int negate = 0;
452
453 *long_val = 0;
454
455 if (**ptr == '-') {
456 negate = 1;
457 (*ptr)++;
458 }
459 while (**ptr) {
460 hex_val = hex(**ptr);
461 if (hex_val < 0)
462 break;
463
464 *long_val = (*long_val << 4) | hex_val;
465 num++;
466 (*ptr)++;
467 }
468
469 if (negate)
470 *long_val = -*long_val;
471
472 return num;
473}
474
475
476static int write_mem_msg(int binary)
477{
478 char *ptr = &remcom_in_buffer[1];
479 unsigned long addr;
480 unsigned long length;
481 int err;
482
483 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
484 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
485 if (binary)
486 err = kgdb_ebin2mem(ptr, (char *)addr, length);
487 else
488 err = kgdb_hex2mem(ptr, (char *)addr, length);
489 if (err)
490 return err;
491 if (CACHE_FLUSH_IS_SAFE)
492 flush_icache_range(addr, addr + length);
493 return 0;
494 }
495
496 return -EINVAL;
497}
498
499static void error_packet(char *pkt, int error)
500{
501 error = -error;
502 pkt[0] = 'E';
503 pkt[1] = hex_asc[(error / 10)];
504 pkt[2] = hex_asc[(error % 10)];
505 pkt[3] = '\0';
506}
507
508
509
510
511
512
513
514#define BUF_THREAD_ID_SIZE 16
515
516static char *pack_threadid(char *pkt, unsigned char *id)
517{
518 char *limit;
519
520 limit = pkt + BUF_THREAD_ID_SIZE;
521 while (pkt < limit)
522 pkt = pack_hex_byte(pkt, *id++);
523
524 return pkt;
525}
526
527static void int_to_threadref(unsigned char *id, int value)
528{
529 unsigned char *scan;
530 int i = 4;
531
532 scan = (unsigned char *)id;
533 while (i--)
534 *scan++ = 0;
535 put_unaligned_be32(value, scan);
536}
537
538static struct task_struct *getthread(struct pt_regs *regs, int tid)
539{
540
541
542
543 if (tid == 0 || tid == -1)
544 tid = -atomic_read(&kgdb_active) - 2;
545 if (tid < -1 && tid > -NR_CPUS - 2) {
546 if (kgdb_info[-tid - 2].task)
547 return kgdb_info[-tid - 2].task;
548 else
549 return idle_task(-tid - 2);
550 }
551 if (tid <= 0) {
552 printk(KERN_ERR "KGDB: Internal thread select error\n");
553 dump_stack();
554 return NULL;
555 }
556
557
558
559
560
561
562 return find_task_by_pid_ns(tid, &init_pid_ns);
563}
564
565
566
567
568
569#ifdef CONFIG_SMP
570static void kgdb_wait(struct pt_regs *regs)
571{
572 unsigned long flags;
573 int cpu;
574
575 local_irq_save(flags);
576 cpu = raw_smp_processor_id();
577 kgdb_info[cpu].debuggerinfo = regs;
578 kgdb_info[cpu].task = current;
579
580
581
582
583 smp_wmb();
584 atomic_set(&cpu_in_kgdb[cpu], 1);
585
586
587 kgdb_disable_hw_debug(regs);
588
589
590 while (atomic_read(&passive_cpu_wait[cpu]))
591 cpu_relax();
592
593 kgdb_info[cpu].debuggerinfo = NULL;
594 kgdb_info[cpu].task = NULL;
595
596
597 if (arch_kgdb_ops.correct_hw_break)
598 arch_kgdb_ops.correct_hw_break();
599
600
601 atomic_set(&cpu_in_kgdb[cpu], 0);
602 touch_softlockup_watchdog_sync();
603 clocksource_touch_watchdog();
604 local_irq_restore(flags);
605}
606#endif
607
608
609
610
611
612static void kgdb_flush_swbreak_addr(unsigned long addr)
613{
614 if (!CACHE_FLUSH_IS_SAFE)
615 return;
616
617 if (current->mm && current->mm->mmap_cache) {
618 flush_cache_range(current->mm->mmap_cache,
619 addr, addr + BREAK_INSTR_SIZE);
620 }
621
622 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
623}
624
625
626
627
628static int kgdb_activate_sw_breakpoints(void)
629{
630 unsigned long addr;
631 int error;
632 int ret = 0;
633 int i;
634
635 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
636 if (kgdb_break[i].state != BP_SET)
637 continue;
638
639 addr = kgdb_break[i].bpt_addr;
640 error = kgdb_arch_set_breakpoint(addr,
641 kgdb_break[i].saved_instr);
642 if (error) {
643 ret = error;
644 printk(KERN_INFO "KGDB: BP install failed: %lx", addr);
645 continue;
646 }
647
648 kgdb_flush_swbreak_addr(addr);
649 kgdb_break[i].state = BP_ACTIVE;
650 }
651 return ret;
652}
653
654static int kgdb_set_sw_break(unsigned long addr)
655{
656 int err = kgdb_validate_break_address(addr);
657 int breakno = -1;
658 int i;
659
660 if (err)
661 return err;
662
663 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
664 if ((kgdb_break[i].state == BP_SET) &&
665 (kgdb_break[i].bpt_addr == addr))
666 return -EEXIST;
667 }
668 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
669 if (kgdb_break[i].state == BP_REMOVED &&
670 kgdb_break[i].bpt_addr == addr) {
671 breakno = i;
672 break;
673 }
674 }
675
676 if (breakno == -1) {
677 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
678 if (kgdb_break[i].state == BP_UNDEFINED) {
679 breakno = i;
680 break;
681 }
682 }
683 }
684
685 if (breakno == -1)
686 return -E2BIG;
687
688 kgdb_break[breakno].state = BP_SET;
689 kgdb_break[breakno].type = BP_BREAKPOINT;
690 kgdb_break[breakno].bpt_addr = addr;
691
692 return 0;
693}
694
695static int kgdb_deactivate_sw_breakpoints(void)
696{
697 unsigned long addr;
698 int error;
699 int ret = 0;
700 int i;
701
702 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
703 if (kgdb_break[i].state != BP_ACTIVE)
704 continue;
705 addr = kgdb_break[i].bpt_addr;
706 error = kgdb_arch_remove_breakpoint(addr,
707 kgdb_break[i].saved_instr);
708 if (error) {
709 printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr);
710 ret = error;
711 }
712
713 kgdb_flush_swbreak_addr(addr);
714 kgdb_break[i].state = BP_SET;
715 }
716 return ret;
717}
718
719static int kgdb_remove_sw_break(unsigned long addr)
720{
721 int i;
722
723 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
724 if ((kgdb_break[i].state == BP_SET) &&
725 (kgdb_break[i].bpt_addr == addr)) {
726 kgdb_break[i].state = BP_REMOVED;
727 return 0;
728 }
729 }
730 return -ENOENT;
731}
732
733int kgdb_isremovedbreak(unsigned long addr)
734{
735 int i;
736
737 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
738 if ((kgdb_break[i].state == BP_REMOVED) &&
739 (kgdb_break[i].bpt_addr == addr))
740 return 1;
741 }
742 return 0;
743}
744
745static int remove_all_break(void)
746{
747 unsigned long addr;
748 int error;
749 int i;
750
751
752 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
753 if (kgdb_break[i].state != BP_ACTIVE)
754 goto setundefined;
755 addr = kgdb_break[i].bpt_addr;
756 error = kgdb_arch_remove_breakpoint(addr,
757 kgdb_break[i].saved_instr);
758 if (error)
759 printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
760 addr);
761setundefined:
762 kgdb_break[i].state = BP_UNDEFINED;
763 }
764
765
766 if (arch_kgdb_ops.remove_all_hw_break)
767 arch_kgdb_ops.remove_all_hw_break();
768
769 return 0;
770}
771
772
773
774
775
776static inline int shadow_pid(int realpid)
777{
778 if (realpid)
779 return realpid;
780
781 return -raw_smp_processor_id() - 2;
782}
783
784static char gdbmsgbuf[BUFMAX + 1];
785
786static void kgdb_msg_write(const char *s, int len)
787{
788 char *bufptr;
789 int wcount;
790 int i;
791
792
793 gdbmsgbuf[0] = 'O';
794
795
796 while (len > 0) {
797 bufptr = gdbmsgbuf + 1;
798
799
800 if ((len << 1) > (BUFMAX - 2))
801 wcount = (BUFMAX - 2) >> 1;
802 else
803 wcount = len;
804
805
806 for (i = 0; i < wcount; i++)
807 bufptr = pack_hex_byte(bufptr, s[i]);
808 *bufptr = '\0';
809
810
811 s += wcount;
812 len -= wcount;
813
814
815 put_packet(gdbmsgbuf);
816 }
817}
818
819
820
821
822
823
824
825
826
827
828static int kgdb_io_ready(int print_wait)
829{
830 if (!kgdb_io_ops)
831 return 0;
832 if (kgdb_connected)
833 return 1;
834 if (atomic_read(&kgdb_setting_breakpoint))
835 return 1;
836 if (print_wait)
837 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
838 return 1;
839}
840
841
842
843
844
845
846
847
848static void gdb_cmd_status(struct kgdb_state *ks)
849{
850
851
852
853
854
855
856 remove_all_break();
857
858 remcom_out_buffer[0] = 'S';
859 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
860}
861
862
863static void gdb_cmd_getregs(struct kgdb_state *ks)
864{
865 struct task_struct *thread;
866 void *local_debuggerinfo;
867 int i;
868
869 thread = kgdb_usethread;
870 if (!thread) {
871 thread = kgdb_info[ks->cpu].task;
872 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
873 } else {
874 local_debuggerinfo = NULL;
875 for_each_online_cpu(i) {
876
877
878
879
880
881
882 if (thread == kgdb_info[i].task)
883 local_debuggerinfo = kgdb_info[i].debuggerinfo;
884 }
885 }
886
887
888
889
890
891
892 if (local_debuggerinfo) {
893 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
894 } else {
895
896
897
898
899
900
901
902 sleeping_thread_to_gdb_regs(gdb_regs, thread);
903 }
904 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
905}
906
907
908static void gdb_cmd_setregs(struct kgdb_state *ks)
909{
910 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
911
912 if (kgdb_usethread && kgdb_usethread != current) {
913 error_packet(remcom_out_buffer, -EINVAL);
914 } else {
915 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
916 strcpy(remcom_out_buffer, "OK");
917 }
918}
919
920
921static void gdb_cmd_memread(struct kgdb_state *ks)
922{
923 char *ptr = &remcom_in_buffer[1];
924 unsigned long length;
925 unsigned long addr;
926 int err;
927
928 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
929 kgdb_hex2long(&ptr, &length) > 0) {
930 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
931 if (err)
932 error_packet(remcom_out_buffer, err);
933 } else {
934 error_packet(remcom_out_buffer, -EINVAL);
935 }
936}
937
938
939static void gdb_cmd_memwrite(struct kgdb_state *ks)
940{
941 int err = write_mem_msg(0);
942
943 if (err)
944 error_packet(remcom_out_buffer, err);
945 else
946 strcpy(remcom_out_buffer, "OK");
947}
948
949
950static void gdb_cmd_binwrite(struct kgdb_state *ks)
951{
952 int err = write_mem_msg(1);
953
954 if (err)
955 error_packet(remcom_out_buffer, err);
956 else
957 strcpy(remcom_out_buffer, "OK");
958}
959
960
961static void gdb_cmd_detachkill(struct kgdb_state *ks)
962{
963 int error;
964
965
966 if (remcom_in_buffer[0] == 'D') {
967 error = remove_all_break();
968 if (error < 0) {
969 error_packet(remcom_out_buffer, error);
970 } else {
971 strcpy(remcom_out_buffer, "OK");
972 kgdb_connected = 0;
973 }
974 put_packet(remcom_out_buffer);
975 } else {
976
977
978
979
980 remove_all_break();
981 kgdb_connected = 0;
982 }
983}
984
985
986static int gdb_cmd_reboot(struct kgdb_state *ks)
987{
988
989 if (strcmp(remcom_in_buffer, "R0") == 0) {
990 printk(KERN_CRIT "Executing emergency reboot\n");
991 strcpy(remcom_out_buffer, "OK");
992 put_packet(remcom_out_buffer);
993
994
995
996
997
998 machine_emergency_restart();
999 kgdb_connected = 0;
1000
1001 return 1;
1002 }
1003 return 0;
1004}
1005
1006
1007static void gdb_cmd_query(struct kgdb_state *ks)
1008{
1009 struct task_struct *g;
1010 struct task_struct *p;
1011 unsigned char thref[8];
1012 char *ptr;
1013 int i;
1014 int cpu;
1015 int finished = 0;
1016
1017 switch (remcom_in_buffer[1]) {
1018 case 's':
1019 case 'f':
1020 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
1021 error_packet(remcom_out_buffer, -EINVAL);
1022 break;
1023 }
1024
1025 i = 0;
1026 remcom_out_buffer[0] = 'm';
1027 ptr = remcom_out_buffer + 1;
1028 if (remcom_in_buffer[1] == 'f') {
1029
1030 for_each_online_cpu(cpu) {
1031 ks->thr_query = 0;
1032 int_to_threadref(thref, -cpu - 2);
1033 pack_threadid(ptr, thref);
1034 ptr += BUF_THREAD_ID_SIZE;
1035 *(ptr++) = ',';
1036 i++;
1037 }
1038 }
1039
1040 do_each_thread(g, p) {
1041 if (i >= ks->thr_query && !finished) {
1042 int_to_threadref(thref, p->pid);
1043 pack_threadid(ptr, thref);
1044 ptr += BUF_THREAD_ID_SIZE;
1045 *(ptr++) = ',';
1046 ks->thr_query++;
1047 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
1048 finished = 1;
1049 }
1050 i++;
1051 } while_each_thread(g, p);
1052
1053 *(--ptr) = '\0';
1054 break;
1055
1056 case 'C':
1057
1058 strcpy(remcom_out_buffer, "QC");
1059 ks->threadid = shadow_pid(current->pid);
1060 int_to_threadref(thref, ks->threadid);
1061 pack_threadid(remcom_out_buffer + 2, thref);
1062 break;
1063 case 'T':
1064 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1065 error_packet(remcom_out_buffer, -EINVAL);
1066 break;
1067 }
1068 ks->threadid = 0;
1069 ptr = remcom_in_buffer + 17;
1070 kgdb_hex2long(&ptr, &ks->threadid);
1071 if (!getthread(ks->linux_regs, ks->threadid)) {
1072 error_packet(remcom_out_buffer, -EINVAL);
1073 break;
1074 }
1075 if ((int)ks->threadid > 0) {
1076 kgdb_mem2hex(getthread(ks->linux_regs,
1077 ks->threadid)->comm,
1078 remcom_out_buffer, 16);
1079 } else {
1080 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1081
1082 sprintf(tmpstr, "shadowCPU%d",
1083 (int)(-ks->threadid - 2));
1084 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1085 }
1086 break;
1087 }
1088}
1089
1090
1091static void gdb_cmd_task(struct kgdb_state *ks)
1092{
1093 struct task_struct *thread;
1094 char *ptr;
1095
1096 switch (remcom_in_buffer[1]) {
1097 case 'g':
1098 ptr = &remcom_in_buffer[2];
1099 kgdb_hex2long(&ptr, &ks->threadid);
1100 thread = getthread(ks->linux_regs, ks->threadid);
1101 if (!thread && ks->threadid > 0) {
1102 error_packet(remcom_out_buffer, -EINVAL);
1103 break;
1104 }
1105 kgdb_usethread = thread;
1106 ks->kgdb_usethreadid = ks->threadid;
1107 strcpy(remcom_out_buffer, "OK");
1108 break;
1109 case 'c':
1110 ptr = &remcom_in_buffer[2];
1111 kgdb_hex2long(&ptr, &ks->threadid);
1112 if (!ks->threadid) {
1113 kgdb_contthread = NULL;
1114 } else {
1115 thread = getthread(ks->linux_regs, ks->threadid);
1116 if (!thread && ks->threadid > 0) {
1117 error_packet(remcom_out_buffer, -EINVAL);
1118 break;
1119 }
1120 kgdb_contthread = thread;
1121 }
1122 strcpy(remcom_out_buffer, "OK");
1123 break;
1124 }
1125}
1126
1127
1128static void gdb_cmd_thread(struct kgdb_state *ks)
1129{
1130 char *ptr = &remcom_in_buffer[1];
1131 struct task_struct *thread;
1132
1133 kgdb_hex2long(&ptr, &ks->threadid);
1134 thread = getthread(ks->linux_regs, ks->threadid);
1135 if (thread)
1136 strcpy(remcom_out_buffer, "OK");
1137 else
1138 error_packet(remcom_out_buffer, -EINVAL);
1139}
1140
1141
1142static void gdb_cmd_break(struct kgdb_state *ks)
1143{
1144
1145
1146
1147
1148 char *bpt_type = &remcom_in_buffer[1];
1149 char *ptr = &remcom_in_buffer[2];
1150 unsigned long addr;
1151 unsigned long length;
1152 int error = 0;
1153
1154 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1155
1156 if (*bpt_type > '4')
1157 return;
1158 } else {
1159 if (*bpt_type != '0' && *bpt_type != '1')
1160
1161 return;
1162 }
1163
1164
1165
1166
1167
1168 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1169
1170 return;
1171
1172 if (*(ptr++) != ',') {
1173 error_packet(remcom_out_buffer, -EINVAL);
1174 return;
1175 }
1176 if (!kgdb_hex2long(&ptr, &addr)) {
1177 error_packet(remcom_out_buffer, -EINVAL);
1178 return;
1179 }
1180 if (*(ptr++) != ',' ||
1181 !kgdb_hex2long(&ptr, &length)) {
1182 error_packet(remcom_out_buffer, -EINVAL);
1183 return;
1184 }
1185
1186 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1187 error = kgdb_set_sw_break(addr);
1188 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1189 error = kgdb_remove_sw_break(addr);
1190 else if (remcom_in_buffer[0] == 'Z')
1191 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1192 (int)length, *bpt_type - '0');
1193 else if (remcom_in_buffer[0] == 'z')
1194 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1195 (int) length, *bpt_type - '0');
1196
1197 if (error == 0)
1198 strcpy(remcom_out_buffer, "OK");
1199 else
1200 error_packet(remcom_out_buffer, error);
1201}
1202
1203
1204static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1205{
1206
1207
1208
1209 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1210
1211 ks->pass_exception = 1;
1212 remcom_in_buffer[0] = 'c';
1213
1214 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1215
1216 ks->pass_exception = 1;
1217 remcom_in_buffer[0] = 'D';
1218 remove_all_break();
1219 kgdb_connected = 0;
1220 return 1;
1221
1222 } else {
1223 kgdb_msg_write("KGDB only knows signal 9 (pass)"
1224 " and 15 (pass and disconnect)\n"
1225 "Executing a continue without signal passing\n", 0);
1226 remcom_in_buffer[0] = 'c';
1227 }
1228
1229
1230 return -1;
1231}
1232
1233
1234
1235
1236static int gdb_serial_stub(struct kgdb_state *ks)
1237{
1238 int error = 0;
1239 int tmp;
1240
1241
1242 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1243
1244 if (kgdb_connected) {
1245 unsigned char thref[8];
1246 char *ptr;
1247
1248
1249 ptr = remcom_out_buffer;
1250 *ptr++ = 'T';
1251 ptr = pack_hex_byte(ptr, ks->signo);
1252 ptr += strlen(strcpy(ptr, "thread:"));
1253 int_to_threadref(thref, shadow_pid(current->pid));
1254 ptr = pack_threadid(ptr, thref);
1255 *ptr++ = ';';
1256 put_packet(remcom_out_buffer);
1257 }
1258
1259 kgdb_usethread = kgdb_info[ks->cpu].task;
1260 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1261 ks->pass_exception = 0;
1262
1263 while (1) {
1264 error = 0;
1265
1266
1267 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1268
1269 get_packet(remcom_in_buffer);
1270
1271 switch (remcom_in_buffer[0]) {
1272 case '?':
1273 gdb_cmd_status(ks);
1274 break;
1275 case 'g':
1276 gdb_cmd_getregs(ks);
1277 break;
1278 case 'G':
1279 gdb_cmd_setregs(ks);
1280 break;
1281 case 'm':
1282 gdb_cmd_memread(ks);
1283 break;
1284 case 'M':
1285 gdb_cmd_memwrite(ks);
1286 break;
1287 case 'X':
1288 gdb_cmd_binwrite(ks);
1289 break;
1290
1291
1292
1293 case 'D':
1294 case 'k':
1295 gdb_cmd_detachkill(ks);
1296 goto default_handle;
1297 case 'R':
1298 if (gdb_cmd_reboot(ks))
1299 goto default_handle;
1300 break;
1301 case 'q':
1302 gdb_cmd_query(ks);
1303 break;
1304 case 'H':
1305 gdb_cmd_task(ks);
1306 break;
1307 case 'T':
1308 gdb_cmd_thread(ks);
1309 break;
1310 case 'z':
1311 case 'Z':
1312 gdb_cmd_break(ks);
1313 break;
1314 case 'C':
1315 tmp = gdb_cmd_exception_pass(ks);
1316 if (tmp > 0)
1317 goto default_handle;
1318 if (tmp == 0)
1319 break;
1320
1321 case 'c':
1322 case 's':
1323 if (kgdb_contthread && kgdb_contthread != current) {
1324
1325 error_packet(remcom_out_buffer, -EINVAL);
1326 break;
1327 }
1328 kgdb_activate_sw_breakpoints();
1329
1330 default:
1331default_handle:
1332 error = kgdb_arch_handle_exception(ks->ex_vector,
1333 ks->signo,
1334 ks->err_code,
1335 remcom_in_buffer,
1336 remcom_out_buffer,
1337 ks->linux_regs);
1338
1339
1340
1341
1342 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1343 remcom_in_buffer[0] == 'k') {
1344 error = 0;
1345 goto kgdb_exit;
1346 }
1347
1348 }
1349
1350
1351 put_packet(remcom_out_buffer);
1352 }
1353
1354kgdb_exit:
1355 if (ks->pass_exception)
1356 error = 1;
1357 return error;
1358}
1359
1360static int kgdb_reenter_check(struct kgdb_state *ks)
1361{
1362 unsigned long addr;
1363
1364 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1365 return 0;
1366
1367
1368 exception_level++;
1369 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1370 kgdb_deactivate_sw_breakpoints();
1371
1372
1373
1374
1375
1376
1377
1378 if (kgdb_remove_sw_break(addr) == 0) {
1379 exception_level = 0;
1380 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1381 kgdb_activate_sw_breakpoints();
1382 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1383 addr);
1384 WARN_ON_ONCE(1);
1385
1386 return 1;
1387 }
1388 remove_all_break();
1389 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1390
1391 if (exception_level > 1) {
1392 dump_stack();
1393 panic("Recursive entry to debugger");
1394 }
1395
1396 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1397 dump_stack();
1398 panic("Recursive entry to debugger");
1399
1400 return 1;
1401}
1402
1403
1404
1405
1406
1407
1408
1409
1410int
1411kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1412{
1413 struct kgdb_state kgdb_var;
1414 struct kgdb_state *ks = &kgdb_var;
1415 unsigned long flags;
1416 int sstep_tries = 100;
1417 int error = 0;
1418 int i, cpu;
1419
1420 ks->cpu = raw_smp_processor_id();
1421 ks->ex_vector = evector;
1422 ks->signo = signo;
1423 ks->ex_vector = evector;
1424 ks->err_code = ecode;
1425 ks->kgdb_usethreadid = 0;
1426 ks->linux_regs = regs;
1427
1428 if (kgdb_reenter_check(ks))
1429 return 0;
1430
1431acquirelock:
1432
1433
1434
1435
1436 local_irq_save(flags);
1437
1438 cpu = raw_smp_processor_id();
1439
1440
1441
1442
1443 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1444 cpu_relax();
1445
1446
1447
1448
1449
1450
1451
1452 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1453 (kgdb_info[cpu].task &&
1454 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
1455 atomic_set(&kgdb_active, -1);
1456 touch_softlockup_watchdog_sync();
1457 clocksource_touch_watchdog();
1458 local_irq_restore(flags);
1459
1460 goto acquirelock;
1461 }
1462
1463 if (!kgdb_io_ready(1)) {
1464 error = 1;
1465 goto kgdb_restore;
1466 }
1467
1468
1469
1470
1471 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1472 goto kgdb_restore;
1473
1474
1475 if (kgdb_io_ops->pre_exception)
1476 kgdb_io_ops->pre_exception();
1477
1478 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1479 kgdb_info[ks->cpu].task = current;
1480
1481 kgdb_disable_hw_debug(ks->linux_regs);
1482
1483
1484
1485
1486
1487 if (!kgdb_single_step) {
1488 for (i = 0; i < NR_CPUS; i++)
1489 atomic_set(&passive_cpu_wait[i], 1);
1490 }
1491
1492
1493
1494
1495
1496 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1497
1498#ifdef CONFIG_SMP
1499
1500 if ((!kgdb_single_step) && kgdb_do_roundup)
1501 kgdb_roundup_cpus(flags);
1502#endif
1503
1504
1505
1506
1507 for_each_online_cpu(i) {
1508 while (!atomic_read(&cpu_in_kgdb[i]))
1509 cpu_relax();
1510 }
1511
1512
1513
1514
1515
1516 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1517 kgdb_deactivate_sw_breakpoints();
1518 kgdb_single_step = 0;
1519 kgdb_contthread = current;
1520 exception_level = 0;
1521
1522
1523 error = gdb_serial_stub(ks);
1524
1525
1526 if (kgdb_io_ops->post_exception)
1527 kgdb_io_ops->post_exception();
1528
1529 kgdb_info[ks->cpu].debuggerinfo = NULL;
1530 kgdb_info[ks->cpu].task = NULL;
1531 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1532
1533 if (!kgdb_single_step) {
1534 for (i = NR_CPUS-1; i >= 0; i--)
1535 atomic_set(&passive_cpu_wait[i], 0);
1536
1537
1538
1539
1540 for_each_online_cpu(i) {
1541 while (atomic_read(&cpu_in_kgdb[i]))
1542 cpu_relax();
1543 }
1544 }
1545
1546kgdb_restore:
1547 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
1548 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
1549 if (kgdb_info[sstep_cpu].task)
1550 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
1551 else
1552 kgdb_sstep_pid = 0;
1553 }
1554
1555 atomic_set(&kgdb_active, -1);
1556 touch_softlockup_watchdog_sync();
1557 clocksource_touch_watchdog();
1558 local_irq_restore(flags);
1559
1560 return error;
1561}
1562
1563int kgdb_nmicallback(int cpu, void *regs)
1564{
1565#ifdef CONFIG_SMP
1566 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1567 atomic_read(&kgdb_active) != cpu &&
1568 atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
1569 kgdb_wait((struct pt_regs *)regs);
1570 return 0;
1571 }
1572#endif
1573 return 1;
1574}
1575
1576static void kgdb_console_write(struct console *co, const char *s,
1577 unsigned count)
1578{
1579 unsigned long flags;
1580
1581
1582
1583 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1584 return;
1585
1586 local_irq_save(flags);
1587 kgdb_msg_write(s, count);
1588 local_irq_restore(flags);
1589}
1590
1591static struct console kgdbcons = {
1592 .name = "kgdb",
1593 .write = kgdb_console_write,
1594 .flags = CON_PRINTBUFFER | CON_ENABLED,
1595 .index = -1,
1596};
1597
1598#ifdef CONFIG_MAGIC_SYSRQ
1599static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1600{
1601 if (!kgdb_io_ops) {
1602 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1603 return;
1604 }
1605 if (!kgdb_connected)
1606 printk(KERN_CRIT "Entering KGDB\n");
1607
1608 kgdb_breakpoint();
1609}
1610
1611static struct sysrq_key_op sysrq_gdb_op = {
1612 .handler = sysrq_handle_gdb,
1613 .help_msg = "debug(G)",
1614 .action_msg = "DEBUG",
1615};
1616#endif
1617
1618static void kgdb_register_callbacks(void)
1619{
1620 if (!kgdb_io_module_registered) {
1621 kgdb_io_module_registered = 1;
1622 kgdb_arch_init();
1623#ifdef CONFIG_MAGIC_SYSRQ
1624 register_sysrq_key('g', &sysrq_gdb_op);
1625#endif
1626 if (kgdb_use_con && !kgdb_con_registered) {
1627 register_console(&kgdbcons);
1628 kgdb_con_registered = 1;
1629 }
1630 }
1631}
1632
1633static void kgdb_unregister_callbacks(void)
1634{
1635
1636
1637
1638
1639
1640 if (kgdb_io_module_registered) {
1641 kgdb_io_module_registered = 0;
1642 kgdb_arch_exit();
1643#ifdef CONFIG_MAGIC_SYSRQ
1644 unregister_sysrq_key('g', &sysrq_gdb_op);
1645#endif
1646 if (kgdb_con_registered) {
1647 unregister_console(&kgdbcons);
1648 kgdb_con_registered = 0;
1649 }
1650 }
1651}
1652
1653static void kgdb_initial_breakpoint(void)
1654{
1655 kgdb_break_asap = 0;
1656
1657 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1658 kgdb_breakpoint();
1659}
1660
1661
1662
1663
1664
1665
1666
1667int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1668{
1669 int err;
1670
1671 spin_lock(&kgdb_registration_lock);
1672
1673 if (kgdb_io_ops) {
1674 spin_unlock(&kgdb_registration_lock);
1675
1676 printk(KERN_ERR "kgdb: Another I/O driver is already "
1677 "registered with KGDB.\n");
1678 return -EBUSY;
1679 }
1680
1681 if (new_kgdb_io_ops->init) {
1682 err = new_kgdb_io_ops->init();
1683 if (err) {
1684 spin_unlock(&kgdb_registration_lock);
1685 return err;
1686 }
1687 }
1688
1689 kgdb_io_ops = new_kgdb_io_ops;
1690
1691 spin_unlock(&kgdb_registration_lock);
1692
1693 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1694 new_kgdb_io_ops->name);
1695
1696
1697 kgdb_register_callbacks();
1698
1699 if (kgdb_break_asap)
1700 kgdb_initial_breakpoint();
1701
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1705
1706
1707
1708
1709
1710
1711
1712void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1713{
1714 BUG_ON(kgdb_connected);
1715
1716
1717
1718
1719
1720 kgdb_unregister_callbacks();
1721
1722 spin_lock(&kgdb_registration_lock);
1723
1724 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1725 kgdb_io_ops = NULL;
1726
1727 spin_unlock(&kgdb_registration_lock);
1728
1729 printk(KERN_INFO
1730 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1731 old_kgdb_io_ops->name);
1732}
1733EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743void kgdb_breakpoint(void)
1744{
1745 atomic_set(&kgdb_setting_breakpoint, 1);
1746 wmb();
1747 arch_kgdb_breakpoint();
1748 wmb();
1749 atomic_set(&kgdb_setting_breakpoint, 0);
1750}
1751EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1752
1753static int __init opt_kgdb_wait(char *str)
1754{
1755 kgdb_break_asap = 1;
1756
1757 if (kgdb_io_module_registered)
1758 kgdb_initial_breakpoint();
1759
1760 return 0;
1761}
1762
1763early_param("kgdbwait", opt_kgdb_wait);
1764