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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123#include <linux/string.h>
124#include <linux/kernel.h>
125#include <linux/signal.h>
126#include <linux/sched.h>
127#include <linux/mm.h>
128#include <linux/console.h>
129#include <linux/init.h>
130#include <linux/smp.h>
131#include <linux/spinlock.h>
132#include <linux/slab.h>
133#include <linux/reboot.h>
134
135#include <asm/asm.h>
136#include <asm/cacheflush.h>
137#include <asm/mipsregs.h>
138#include <asm/pgtable.h>
139#include <asm/system.h>
140#include <asm/gdb-stub.h>
141#include <asm/inst.h>
142#include <asm/smp.h>
143
144
145
146
147
148extern int putDebugChar(char c);
149extern char getDebugChar(void);
150extern void trap_low(void);
151
152
153
154
155extern void breakpoint(void);
156extern void breakinst(void);
157extern void async_breakpoint(void);
158extern void async_breakinst(void);
159extern void adel(void);
160
161
162
163
164
165static void getpacket(char *buffer);
166static void putpacket(char *buffer);
167static int computeSignal(int tt);
168static int hex(unsigned char ch);
169static int hexToInt(char **ptr, int *intValue);
170static int hexToLong(char **ptr, long *longValue);
171static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault);
172void handle_exception(struct gdb_regs *regs);
173
174int kgdb_enabled;
175
176
177
178
179static DEFINE_SPINLOCK(kgdb_lock);
180static raw_spinlock_t kgdb_cpulock[NR_CPUS] = {
181 [0 ... NR_CPUS-1] = __RAW_SPIN_LOCK_UNLOCKED,
182};
183
184
185
186
187
188#define BUFMAX 2048
189
190static char input_buffer[BUFMAX];
191static char output_buffer[BUFMAX];
192static int initialized;
193static int kgdb_started;
194static const char hexchars[]="0123456789abcdef";
195
196
197
198int kgdb_read_byte(unsigned char *address, unsigned char *dest);
199int kgdb_write_byte(unsigned char val, unsigned char *dest);
200
201
202
203
204static int hex(unsigned char ch)
205{
206 if (ch >= 'a' && ch <= 'f')
207 return ch-'a'+10;
208 if (ch >= '0' && ch <= '9')
209 return ch-'0';
210 if (ch >= 'A' && ch <= 'F')
211 return ch-'A'+10;
212 return -1;
213}
214
215
216
217
218static void getpacket(char *buffer)
219{
220 unsigned char checksum;
221 unsigned char xmitcsum;
222 int i;
223 int count;
224 unsigned char ch;
225
226 do {
227
228
229
230
231 while ((ch = (getDebugChar() & 0x7f)) != '$') ;
232
233 checksum = 0;
234 xmitcsum = -1;
235 count = 0;
236
237
238
239
240 while (count < BUFMAX) {
241 ch = getDebugChar();
242 if (ch == '#')
243 break;
244 checksum = checksum + ch;
245 buffer[count] = ch;
246 count = count + 1;
247 }
248
249 if (count >= BUFMAX)
250 continue;
251
252 buffer[count] = 0;
253
254 if (ch == '#') {
255 xmitcsum = hex(getDebugChar() & 0x7f) << 4;
256 xmitcsum |= hex(getDebugChar() & 0x7f);
257
258 if (checksum != xmitcsum)
259 putDebugChar('-');
260 else {
261 putDebugChar('+');
262
263
264
265
266
267 if (buffer[2] == ':') {
268 putDebugChar(buffer[0]);
269 putDebugChar(buffer[1]);
270
271
272
273
274 count = strlen(buffer);
275 for (i=3; i <= count; i++)
276 buffer[i-3] = buffer[i];
277 }
278 }
279 }
280 }
281 while (checksum != xmitcsum);
282}
283
284
285
286
287static void putpacket(char *buffer)
288{
289 unsigned char checksum;
290 int count;
291 unsigned char ch;
292
293
294
295
296
297 do {
298 putDebugChar('$');
299 checksum = 0;
300 count = 0;
301
302 while ((ch = buffer[count]) != 0) {
303 if (!(putDebugChar(ch)))
304 return;
305 checksum += ch;
306 count += 1;
307 }
308
309 putDebugChar('#');
310 putDebugChar(hexchars[checksum >> 4]);
311 putDebugChar(hexchars[checksum & 0xf]);
312
313 }
314 while ((getDebugChar() & 0x7f) != '+');
315}
316
317
318
319
320
321
322
323
324
325static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault)
326{
327 unsigned char ch;
328
329 while (count-- > 0) {
330 if (kgdb_read_byte(mem++, &ch) != 0)
331 return 0;
332 *buf++ = hexchars[ch >> 4];
333 *buf++ = hexchars[ch & 0xf];
334 }
335
336 *buf = 0;
337
338 return buf;
339}
340
341
342
343
344
345
346
347static char *hex2mem(char *buf, char *mem, int count, int binary, int may_fault)
348{
349 int i;
350 unsigned char ch;
351
352 for (i=0; i<count; i++)
353 {
354 if (binary) {
355 ch = *buf++;
356 if (ch == 0x7d)
357 ch = 0x20 ^ *buf++;
358 }
359 else {
360 ch = hex(*buf++) << 4;
361 ch |= hex(*buf++);
362 }
363 if (kgdb_write_byte(ch, mem++) != 0)
364 return 0;
365 }
366
367 return mem;
368}
369
370
371
372
373
374
375static struct hard_trap_info {
376 unsigned char tt;
377 unsigned char signo;
378} hard_trap_info[] = {
379 { 6, SIGBUS },
380 { 7, SIGBUS },
381 { 9, SIGTRAP },
382 { 10, SIGILL },
383
384 { 12, SIGFPE },
385 { 13, SIGTRAP },
386 { 14, SIGSEGV },
387 { 15, SIGFPE },
388 { 23, SIGSEGV },
389 { 31, SIGSEGV },
390 { 0, 0}
391};
392
393
394void *saved_vectors[32];
395
396
397
398
399void set_debug_traps(void)
400{
401 struct hard_trap_info *ht;
402 unsigned long flags;
403 unsigned char c;
404
405 local_irq_save(flags);
406 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
407 saved_vectors[ht->tt] = set_except_vector(ht->tt, trap_low);
408
409 putDebugChar('+');
410
411
412
413
414 while((c = getDebugChar()) != '$');
415 while((c = getDebugChar()) != '#');
416 c = getDebugChar();
417 c = getDebugChar();
418 putDebugChar('+');
419
420 initialized = 1;
421 local_irq_restore(flags);
422}
423
424void restore_debug_traps(void)
425{
426 struct hard_trap_info *ht;
427 unsigned long flags;
428
429 local_irq_save(flags);
430 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
431 set_except_vector(ht->tt, saved_vectors[ht->tt]);
432 local_irq_restore(flags);
433}
434
435
436
437
438static int computeSignal(int tt)
439{
440 struct hard_trap_info *ht;
441
442 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
443 if (ht->tt == tt)
444 return ht->signo;
445
446 return SIGHUP;
447}
448
449
450
451
452
453static int hexToInt(char **ptr, int *intValue)
454{
455 int numChars = 0;
456 int hexValue;
457
458 *intValue = 0;
459
460 while (**ptr) {
461 hexValue = hex(**ptr);
462 if (hexValue < 0)
463 break;
464
465 *intValue = (*intValue << 4) | hexValue;
466 numChars ++;
467
468 (*ptr)++;
469 }
470
471 return (numChars);
472}
473
474static int hexToLong(char **ptr, long *longValue)
475{
476 int numChars = 0;
477 int hexValue;
478
479 *longValue = 0;
480
481 while (**ptr) {
482 hexValue = hex(**ptr);
483 if (hexValue < 0)
484 break;
485
486 *longValue = (*longValue << 4) | hexValue;
487 numChars ++;
488
489 (*ptr)++;
490 }
491
492 return numChars;
493}
494
495
496#if 0
497
498
499
500
501void show_gdbregs(struct gdb_regs * regs)
502{
503
504
505
506 printk("$0 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
507 regs->reg0, regs->reg1, regs->reg2, regs->reg3,
508 regs->reg4, regs->reg5, regs->reg6, regs->reg7);
509 printk("$8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
510 regs->reg8, regs->reg9, regs->reg10, regs->reg11,
511 regs->reg12, regs->reg13, regs->reg14, regs->reg15);
512 printk("$16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
513 regs->reg16, regs->reg17, regs->reg18, regs->reg19,
514 regs->reg20, regs->reg21, regs->reg22, regs->reg23);
515 printk("$24: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
516 regs->reg24, regs->reg25, regs->reg26, regs->reg27,
517 regs->reg28, regs->reg29, regs->reg30, regs->reg31);
518
519
520
521
522 printk("epc : %08lx\nStatus: %08lx\nCause : %08lx\n",
523 regs->cp0_epc, regs->cp0_status, regs->cp0_cause);
524}
525#endif
526
527
528
529
530
531
532
533
534static struct gdb_bp_save {
535 unsigned long addr;
536 unsigned int val;
537} step_bp[2];
538
539#define BP 0x0000000d
540
541
542
543
544static void single_step(struct gdb_regs *regs)
545{
546 union mips_instruction insn;
547 unsigned long targ;
548 int is_branch, is_cond, i;
549
550 targ = regs->cp0_epc;
551 insn.word = *(unsigned int *)targ;
552 is_branch = is_cond = 0;
553
554 switch (insn.i_format.opcode) {
555
556
557
558 case spec_op:
559 switch (insn.r_format.func) {
560 case jalr_op:
561 case jr_op:
562 targ = *(®s->reg0 + insn.r_format.rs);
563 is_branch = 1;
564 break;
565 }
566 break;
567
568
569
570
571
572
573 case bcond_op:
574 is_branch = is_cond = 1;
575 targ += 4 + (insn.i_format.simmediate << 2);
576 break;
577
578
579
580
581 case jal_op:
582 case j_op:
583 is_branch = 1;
584 targ += 4;
585 targ >>= 28;
586 targ <<= 28;
587 targ |= (insn.j_format.target << 2);
588 break;
589
590
591
592
593 case beq_op:
594 case beql_op:
595 case bne_op:
596 case bnel_op:
597 case blez_op:
598 case blezl_op:
599 case bgtz_op:
600 case bgtzl_op:
601 case cop0_op:
602 case cop1_op:
603 case cop2_op:
604 case cop1x_op:
605 is_branch = is_cond = 1;
606 targ += 4 + (insn.i_format.simmediate << 2);
607 break;
608 }
609
610 if (is_branch) {
611 i = 0;
612 if (is_cond && targ != (regs->cp0_epc + 8)) {
613 step_bp[i].addr = regs->cp0_epc + 8;
614 step_bp[i++].val = *(unsigned *)(regs->cp0_epc + 8);
615 *(unsigned *)(regs->cp0_epc + 8) = BP;
616 }
617 step_bp[i].addr = targ;
618 step_bp[i].val = *(unsigned *)targ;
619 *(unsigned *)targ = BP;
620 } else {
621 step_bp[0].addr = regs->cp0_epc + 4;
622 step_bp[0].val = *(unsigned *)(regs->cp0_epc + 4);
623 *(unsigned *)(regs->cp0_epc + 4) = BP;
624 }
625}
626
627
628
629
630
631
632static struct gdb_bp_save async_bp;
633
634
635
636
637
638
639
640
641extern spinlock_t smp_call_lock;
642
643void set_async_breakpoint(unsigned long *epc)
644{
645
646 if ((*epc & 0x80000000) == 0)
647 return;
648
649#ifdef CONFIG_SMP
650
651 if (spin_is_locked(&smp_call_lock))
652 return;
653#endif
654
655 async_bp.addr = *epc;
656 *epc = (unsigned long)async_breakpoint;
657}
658
659static void kgdb_wait(void *arg)
660{
661 unsigned flags;
662 int cpu = smp_processor_id();
663
664 local_irq_save(flags);
665
666 __raw_spin_lock(&kgdb_cpulock[cpu]);
667 __raw_spin_unlock(&kgdb_cpulock[cpu]);
668
669 local_irq_restore(flags);
670}
671
672
673
674
675
676static int kgdb_smp_call_kgdb_wait(void)
677{
678#ifdef CONFIG_SMP
679 cpumask_t mask = cpu_online_map;
680 struct call_data_struct data;
681 int cpu = smp_processor_id();
682 int cpus;
683
684
685
686
687 BUG_ON(!cpu_online(cpu));
688
689 cpu_clear(cpu, mask);
690 cpus = cpus_weight(mask);
691 if (!cpus)
692 return 0;
693
694 if (spin_is_locked(&smp_call_lock)) {
695
696
697
698
699 return -1;
700 }
701
702
703
704
705
706
707
708 data.func = kgdb_wait;
709 data.info = NULL;
710 atomic_set(&data.started, 0);
711 data.wait = 0;
712
713 spin_lock(&smp_call_lock);
714 call_data = &data;
715 mb();
716
717 core_send_ipi_mask(mask, SMP_CALL_FUNCTION);
718
719
720
721 while (atomic_read(&data.started) != cpus)
722 barrier();
723
724 call_data = NULL;
725 spin_unlock(&smp_call_lock);
726#endif
727
728 return 0;
729}
730
731
732
733
734
735
736void handle_exception(struct gdb_regs *regs)
737{
738 int trap;
739 int sigval;
740 long addr;
741 int length;
742 char *ptr;
743 unsigned long *stack;
744 int i;
745 int bflag = 0;
746
747 kgdb_started = 1;
748
749
750
751
752 if (!spin_trylock(&kgdb_lock)) {
753
754
755
756
757 return;
758 }
759
760
761
762
763
764 if (regs->cp0_epc == (unsigned long)async_breakinst) {
765 regs->cp0_epc = async_bp.addr;
766 async_bp.addr = 0;
767 }
768
769
770
771
772 for_each_online_cpu(i)
773 if (__raw_spin_trylock(&kgdb_cpulock[i]) == 0)
774 panic("kgdb: couldn't get cpulock %d\n", i);
775
776
777
778
779 kgdb_smp_call_kgdb_wait();
780
781
782
783
784 trap = (regs->cp0_cause & 0x7c) >> 2;
785 if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst)
786 regs->cp0_epc += 4;
787
788
789
790
791
792 if (step_bp[0].addr) {
793 *(unsigned *)step_bp[0].addr = step_bp[0].val;
794 step_bp[0].addr = 0;
795
796 if (step_bp[1].addr) {
797 *(unsigned *)step_bp[1].addr = step_bp[1].val;
798 step_bp[1].addr = 0;
799 }
800 }
801
802 stack = (long *)regs->reg29;
803 sigval = computeSignal(trap);
804
805
806
807
808 ptr = output_buffer;
809
810
811
812
813 *ptr++ = 'T';
814 *ptr++ = hexchars[sigval >> 4];
815 *ptr++ = hexchars[sigval & 0xf];
816
817
818
819
820 *ptr++ = hexchars[REG_EPC >> 4];
821 *ptr++ = hexchars[REG_EPC & 0xf];
822 *ptr++ = ':';
823 ptr = mem2hex((char *)®s->cp0_epc, ptr, sizeof(long), 0);
824 *ptr++ = ';';
825
826
827
828
829 *ptr++ = hexchars[REG_FP >> 4];
830 *ptr++ = hexchars[REG_FP & 0xf];
831 *ptr++ = ':';
832 ptr = mem2hex((char *)®s->reg30, ptr, sizeof(long), 0);
833 *ptr++ = ';';
834
835
836
837
838 *ptr++ = hexchars[REG_SP >> 4];
839 *ptr++ = hexchars[REG_SP & 0xf];
840 *ptr++ = ':';
841 ptr = mem2hex((char *)®s->reg29, ptr, sizeof(long), 0);
842 *ptr++ = ';';
843
844 *ptr++ = 0;
845 putpacket(output_buffer);
846
847
848
849
850 while (1) {
851 output_buffer[0] = 0;
852 getpacket(input_buffer);
853
854 switch (input_buffer[0])
855 {
856 case '?':
857 output_buffer[0] = 'S';
858 output_buffer[1] = hexchars[sigval >> 4];
859 output_buffer[2] = hexchars[sigval & 0xf];
860 output_buffer[3] = 0;
861 break;
862
863
864
865
866 case 'D':
867 putpacket(output_buffer);
868 goto finish_kgdb;
869 break;
870
871 case 'd':
872
873 break;
874
875
876
877
878 case 'g':
879 ptr = output_buffer;
880 ptr = mem2hex((char *)®s->reg0, ptr, 32*sizeof(long), 0);
881 ptr = mem2hex((char *)®s->cp0_status, ptr, 6*sizeof(long), 0);
882 ptr = mem2hex((char *)®s->fpr0, ptr, 32*sizeof(long), 0);
883 ptr = mem2hex((char *)®s->cp1_fsr, ptr, 2*sizeof(long), 0);
884 ptr = mem2hex((char *)®s->frame_ptr, ptr, 2*sizeof(long), 0);
885 ptr = mem2hex((char *)®s->cp0_index, ptr, 16*sizeof(long), 0);
886 break;
887
888
889
890
891 case 'G':
892 {
893 ptr = &input_buffer[1];
894 hex2mem(ptr, (char *)®s->reg0, 32*sizeof(long), 0, 0);
895 ptr += 32*(2*sizeof(long));
896 hex2mem(ptr, (char *)®s->cp0_status, 6*sizeof(long), 0, 0);
897 ptr += 6*(2*sizeof(long));
898 hex2mem(ptr, (char *)®s->fpr0, 32*sizeof(long), 0, 0);
899 ptr += 32*(2*sizeof(long));
900 hex2mem(ptr, (char *)®s->cp1_fsr, 2*sizeof(long), 0, 0);
901 ptr += 2*(2*sizeof(long));
902 hex2mem(ptr, (char *)®s->frame_ptr, 2*sizeof(long), 0, 0);
903 ptr += 2*(2*sizeof(long));
904 hex2mem(ptr, (char *)®s->cp0_index, 16*sizeof(long), 0, 0);
905 strcpy(output_buffer, "OK");
906 }
907 break;
908
909
910
911
912 case 'm':
913 ptr = &input_buffer[1];
914
915 if (hexToLong(&ptr, &addr)
916 && *ptr++ == ','
917 && hexToInt(&ptr, &length)) {
918 if (mem2hex((char *)addr, output_buffer, length, 1))
919 break;
920 strcpy(output_buffer, "E03");
921 } else
922 strcpy(output_buffer, "E01");
923 break;
924
925
926
927
928 case 'X':
929 bflag = 1;
930
931
932
933
934
935 case 'M':
936 ptr = &input_buffer[1];
937
938 if (hexToLong(&ptr, &addr)
939 && *ptr++ == ','
940 && hexToInt(&ptr, &length)
941 && *ptr++ == ':') {
942 if (hex2mem(ptr, (char *)addr, length, bflag, 1))
943 strcpy(output_buffer, "OK");
944 else
945 strcpy(output_buffer, "E03");
946 }
947 else
948 strcpy(output_buffer, "E02");
949 break;
950
951
952
953
954 case 'c':
955
956
957 ptr = &input_buffer[1];
958 if (hexToLong(&ptr, &addr))
959 regs->cp0_epc = addr;
960
961 goto exit_kgdb_exception;
962 break;
963
964
965
966
967
968 case 'k':
969 case 'r':
970 machine_restart("kgdb restarts machine");
971 break;
972
973
974
975
976 case 's':
977
978
979
980
981 single_step(regs);
982 goto exit_kgdb_exception;
983
984 break;
985
986
987
988
989
990 case 'b':
991 {
992#if 0
993 int baudrate;
994 extern void set_timer_3();
995
996 ptr = &input_buffer[1];
997 if (!hexToInt(&ptr, &baudrate))
998 {
999 strcpy(output_buffer, "B01");
1000 break;
1001 }
1002
1003
1004
1005 switch (baudrate)
1006 {
1007 case 38400:
1008 baudrate = 16;
1009 break;
1010 case 19200:
1011 baudrate = 33;
1012 break;
1013 case 9600:
1014 baudrate = 65;
1015 break;
1016 default:
1017 baudrate = 0;
1018 strcpy(output_buffer, "B02");
1019 goto x1;
1020 }
1021
1022 if (baudrate) {
1023 putpacket("OK");
1024 set_timer_3(baudrate);
1025 }
1026#endif
1027 }
1028 break;
1029
1030 }
1031
1032
1033
1034
1035
1036 putpacket(output_buffer);
1037
1038 }
1039
1040 return;
1041
1042finish_kgdb:
1043 restore_debug_traps();
1044
1045exit_kgdb_exception:
1046
1047 for_each_online_cpu(i)
1048 __raw_spin_unlock(&kgdb_cpulock[i]);
1049 spin_unlock(&kgdb_lock);
1050
1051 __flush_cache_all();
1052 return;
1053}
1054
1055
1056
1057
1058
1059
1060
1061void breakpoint(void)
1062{
1063 if (!initialized)
1064 return;
1065
1066 __asm__ __volatile__(
1067 ".globl breakinst\n\t"
1068 ".set\tnoreorder\n\t"
1069 "nop\n"
1070 "breakinst:\tbreak\n\t"
1071 "nop\n\t"
1072 ".set\treorder"
1073 );
1074}
1075
1076
1077void async_breakpoint(void)
1078{
1079 __asm__ __volatile__(
1080 ".globl async_breakinst\n\t"
1081 ".set\tnoreorder\n\t"
1082 "nop\n"
1083 "async_breakinst:\tbreak\n\t"
1084 "nop\n\t"
1085 ".set\treorder"
1086 );
1087}
1088
1089void adel(void)
1090{
1091 __asm__ __volatile__(
1092 ".globl\tadel\n\t"
1093 "lui\t$8,0x8000\n\t"
1094 "lw\t$9,1($8)\n\t"
1095 );
1096}
1097
1098
1099
1100
1101
1102static void __used *malloc(size_t size)
1103{
1104 return kmalloc(size, GFP_ATOMIC);
1105}
1106
1107static void __used free(void *where)
1108{
1109 kfree(where);
1110}
1111
1112#ifdef CONFIG_GDB_CONSOLE
1113
1114void gdb_putsn(const char *str, int l)
1115{
1116 char outbuf[18];
1117
1118 if (!kgdb_started)
1119 return;
1120
1121 outbuf[0]='O';
1122
1123 while(l) {
1124 int i = (l>8)?8:l;
1125 mem2hex((char *)str, &outbuf[1], i, 0);
1126 outbuf[(i*2)+1]=0;
1127 putpacket(outbuf);
1128 str += i;
1129 l -= i;
1130 }
1131}
1132
1133static void gdb_console_write(struct console *con, const char *s, unsigned n)
1134{
1135 gdb_putsn(s, n);
1136}
1137
1138static struct console gdb_console = {
1139 .name = "gdb",
1140 .write = gdb_console_write,
1141 .flags = CON_PRINTBUFFER,
1142 .index = -1
1143};
1144
1145static int __init register_gdb_console(void)
1146{
1147 register_console(&gdb_console);
1148
1149 return 0;
1150}
1151
1152console_initcall(register_gdb_console);
1153
1154#endif
1155