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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166#include <linux/string.h>
167#include <linux/signal.h>
168#include <linux/kernel.h>
169#include <linux/delay.h>
170#include <linux/linkage.h>
171#include <linux/reboot.h>
172
173#include <asm/setup.h>
174#include <asm/ptrace.h>
175
176#include <asm/irq.h>
177#include <arch/hwregs/reg_map.h>
178#include <arch/hwregs/reg_rdwr.h>
179#include <arch/hwregs/intr_vect_defs.h>
180#include <arch/hwregs/ser_defs.h>
181
182
183extern void gdb_handle_exception(void);
184
185extern void kgdb_handle_exception(void);
186
187static int kgdb_started = 0;
188
189
190
191typedef
192struct register_image
193{
194
195 unsigned int r0;
196 unsigned int r1;
197 unsigned int r2;
198 unsigned int r3;
199 unsigned int r4;
200 unsigned int r5;
201 unsigned int r6;
202 unsigned int r7;
203 unsigned int r8;
204 unsigned int r9;
205 unsigned int r10;
206 unsigned int r11;
207 unsigned int r12;
208 unsigned int r13;
209 unsigned int sp;
210 unsigned int acr;
211
212 unsigned char bz;
213 unsigned char vr;
214 unsigned int pid;
215 unsigned char srs;
216 unsigned short wz;
217 unsigned int exs;
218 unsigned int eda;
219 unsigned int mof;
220 unsigned int dz;
221 unsigned int ebp;
222 unsigned int erp;
223 unsigned int srp;
224 unsigned int nrp;
225 unsigned int ccs;
226 unsigned int usp;
227 unsigned int spc;
228 unsigned int pc;
229
230} registers;
231
232typedef
233struct bp_register_image
234{
235
236 unsigned int s0_0;
237 unsigned int s1_0;
238 unsigned int s2_0;
239 unsigned int s3_0;
240 unsigned int s4_0;
241 unsigned int s5_0;
242 unsigned int s6_0;
243 unsigned int s7_0;
244 unsigned int s8_0;
245 unsigned int s9_0;
246 unsigned int s10_0;
247 unsigned int s11_0;
248 unsigned int s12_0;
249 unsigned int s13_0;
250 unsigned int s14_0;
251 unsigned int s15_0;
252
253
254 unsigned int s0_1;
255 unsigned int s1_1;
256 unsigned int s2_1;
257 unsigned int s3_1;
258 unsigned int s4_1;
259 unsigned int s5_1;
260 unsigned int s6_1;
261 unsigned int s7_1;
262 unsigned int s8_1;
263 unsigned int s9_1;
264 unsigned int s10_1;
265 unsigned int s11_1;
266 unsigned int s12_1;
267 unsigned int s13_1;
268 unsigned int s14_1;
269 unsigned int s15_1;
270
271
272 unsigned int s0_2;
273 unsigned int s1_2;
274 unsigned int s2_2;
275 unsigned int s3_2;
276 unsigned int s4_2;
277 unsigned int s5_2;
278 unsigned int s6_2;
279 unsigned int s7_2;
280 unsigned int s8_2;
281 unsigned int s9_2;
282 unsigned int s10_2;
283 unsigned int s11_2;
284 unsigned int s12_2;
285 unsigned int s13_2;
286 unsigned int s14_2;
287 unsigned int s15_2;
288
289
290 unsigned int s0_3;
291 unsigned int s1_3;
292 unsigned int s2_3;
293 unsigned int s3_3;
294 unsigned int s4_3;
295 unsigned int s5_3;
296 unsigned int s6_3;
297 unsigned int s7_3;
298 unsigned int s8_3;
299 unsigned int s9_3;
300 unsigned int s10_3;
301 unsigned int s11_3;
302 unsigned int s12_3;
303 unsigned int s13_3;
304 unsigned int s14_3;
305 unsigned int s15_3;
306
307} support_registers;
308
309enum register_name
310{
311 R0, R1, R2, R3,
312 R4, R5, R6, R7,
313 R8, R9, R10, R11,
314 R12, R13, SP, ACR,
315
316 BZ, VR, PID, SRS,
317 WZ, EXS, EDA, MOF,
318 DZ, EBP, ERP, SRP,
319 NRP, CCS, USP, SPC,
320 PC,
321
322 S0, S1, S2, S3,
323 S4, S5, S6, S7,
324 S8, S9, S10, S11,
325 S12, S13, S14, S15
326
327};
328
329
330
331static int register_size[] =
332{
333 4, 4, 4, 4,
334 4, 4, 4, 4,
335 4, 4, 4, 4,
336 4, 4, 4, 4,
337
338 1, 1, 4, 1,
339 2, 4, 4, 4,
340 4, 4, 4, 4,
341 4, 4, 4, 4,
342
343 4,
344
345 4, 4, 4, 4,
346 4, 4, 4, 4,
347 4, 4, 4, 4,
348 4, 4, 4
349
350};
351
352
353
354registers reg;
355support_registers sreg;
356
357
358
359
360static char *gdb_cris_strcpy(char *s1, const char *s2);
361
362
363static int gdb_cris_strlen(const char *s);
364
365
366static void *gdb_cris_memchr(const void *s, int c, int n);
367
368
369static int gdb_cris_strtol(const char *s, char **endptr, int base);
370
371
372
373
374
375static int write_register(int regno, char *val);
376
377
378
379static int read_register(char regno, unsigned int *valptr);
380
381
382int getDebugChar(void);
383
384#ifdef CONFIG_ETRAX_VCS_SIM
385int getDebugChar(void)
386{
387 return socketread();
388}
389#endif
390
391
392void putDebugChar(int val);
393
394#ifdef CONFIG_ETRAX_VCS_SIM
395void putDebugChar(int val)
396{
397 socketwrite((char *)&val, 1);
398}
399#endif
400
401
402static int hex(char ch);
403
404
405
406
407static char *mem2hex(char *buf, unsigned char *mem, int count);
408
409
410
411
412static unsigned char *hex2mem(unsigned char *mem, char *buf, int count);
413
414
415
416
417static unsigned char *bin2mem(unsigned char *mem, unsigned char *buf, int count);
418
419
420
421static void getpacket(char *buffer);
422
423
424static void putpacket(char *buffer);
425
426
427
428static void stub_is_stopped(int sigval);
429
430
431
432
433void handle_exception(int sigval);
434
435
436static void kill_restart(void);
437
438
439
440
441void putDebugString(const unsigned char *str, int len);
442
443
444void breakpoint(void);
445
446
447#define USEDVAR(name) { if (name) { ; } }
448#define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
449
450
451
452
453
454#define BUFMAX 512
455
456
457#define RUNLENMAX 64
458
459
460static char input_buffer[BUFMAX];
461static char output_buffer[BUFMAX];
462
463
464enum error_type
465{
466 SUCCESS, E01, E02, E03, E04, E05, E06,
467};
468
469static char *error_message[] =
470{
471 "",
472 "E01 Set current or general thread - H[c,g] - internal error.",
473 "E02 Change register content - P - cannot change read-only register.",
474 "E03 Thread is not alive.",
475 "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
476 "E05 Change register content - P - the register is not implemented..",
477 "E06 Change memory content - M - internal error.",
478};
479
480
481
482
483
484#define INTERNAL_STACK_SIZE 1024
485char internal_stack[INTERNAL_STACK_SIZE];
486
487
488
489
490
491
492
493static int dynamic_bp = 0;
494
495
496
497
498
499static char*
500gdb_cris_strcpy(char *s1, const char *s2)
501{
502 char *s = s1;
503
504 for (s = s1; (*s++ = *s2++) != '\0'; )
505 ;
506 return s1;
507}
508
509
510static int
511gdb_cris_strlen(const char *s)
512{
513 const char *sc;
514
515 for (sc = s; *sc != '\0'; sc++)
516 ;
517 return (sc - s);
518}
519
520
521static void*
522gdb_cris_memchr(const void *s, int c, int n)
523{
524 const unsigned char uc = c;
525 const unsigned char *su;
526
527 for (su = s; 0 < n; ++su, --n)
528 if (*su == uc)
529 return (void *)su;
530 return NULL;
531}
532
533
534
535static int
536gdb_cris_strtol(const char *s, char **endptr, int base)
537{
538 char *s1;
539 char *sd;
540 int x = 0;
541
542 for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
543 x = x * base + (sd - hex_asc);
544
545 if (endptr) {
546
547 *endptr = s1;
548 }
549
550 return x;
551}
552
553
554
555
556
557static int
558write_register(int regno, char *val)
559{
560 int status = SUCCESS;
561
562 if (regno >= R0 && regno <= ACR) {
563
564 hex2mem((unsigned char *)®.r0 + (regno - R0) * sizeof(unsigned int),
565 val, sizeof(unsigned int));
566
567 } else if (regno == BZ || regno == VR || regno == WZ || regno == DZ) {
568
569 status = E02;
570
571 } else if (regno == PID) {
572
573
574 hex2mem((unsigned char *)®.pid, val, sizeof(unsigned int));
575
576 } else if (regno == SRS) {
577
578 hex2mem((unsigned char *)®.srs, val, sizeof(unsigned char));
579
580 } else if (regno >= EXS && regno <= SPC) {
581
582 hex2mem((unsigned char *)®.exs + (regno - EXS) * sizeof(unsigned int),
583 val, sizeof(unsigned int));
584
585 } else if (regno == PC) {
586
587 status = E02;
588
589 } else if (regno >= S0 && regno <= S15) {
590
591 hex2mem((unsigned char *)&sreg.s0_0 + (reg.srs * 16 * sizeof(unsigned int)) + (regno - S0) * sizeof(unsigned int), val, sizeof(unsigned int));
592 } else {
593
594 status = E05;
595 }
596 return status;
597}
598
599
600
601static int
602read_register(char regno, unsigned int *valptr)
603{
604 int status = SUCCESS;
605
606
607
608
609 if (regno >= R0 && regno <= ACR) {
610
611 *valptr = *(unsigned int *)((char *)®.r0 + (regno - R0) * sizeof(unsigned int));
612
613 } else if (regno == BZ || regno == VR) {
614
615 *valptr = (unsigned int)(*(unsigned char *)
616 ((char *)®.bz + (regno - BZ) * sizeof(char)));
617
618 } else if (regno == PID) {
619
620 *valptr = *(unsigned int *)((char *)®.pid);
621
622 } else if (regno == SRS) {
623
624 *valptr = (unsigned int)(*(unsigned char *)((char *)®.srs));
625
626 } else if (regno == WZ) {
627
628 *valptr = (unsigned int)(*(unsigned short *)(char *)®.wz);
629
630 } else if (regno >= EXS && regno <= PC) {
631
632 *valptr = *(unsigned int *)((char *)®.exs + (regno - EXS) * sizeof(unsigned int));
633
634 } else if (regno >= S0 && regno <= S15) {
635
636 *valptr = *(unsigned int *)((char *)&sreg.s0_0 + (reg.srs * 16 * sizeof(unsigned int)) + (regno - S0) * sizeof(unsigned int));
637
638 } else {
639
640 status = E05;
641 }
642 return status;
643
644}
645
646
647
648static int
649hex(char ch)
650{
651 if ((ch >= 'a') && (ch <= 'f'))
652 return (ch - 'a' + 10);
653 if ((ch >= '0') && (ch <= '9'))
654 return (ch - '0');
655 if ((ch >= 'A') && (ch <= 'F'))
656 return (ch - 'A' + 10);
657 return -1;
658}
659
660
661
662
663
664static char *
665mem2hex(char *buf, unsigned char *mem, int count)
666{
667 int i;
668 int ch;
669
670 if (mem == NULL) {
671
672 for (i = 0; i < count; i++) {
673 *buf++ = '0';
674 *buf++ = '0';
675 }
676 } else {
677
678 for (i = 0; i < count; i++) {
679 ch = *mem++;
680 buf = pack_hex_byte(buf, ch);
681 }
682 }
683
684 *buf = '\0';
685 return buf;
686}
687
688
689static char *
690mem2hex_nbo(char *buf, unsigned char *mem, int count)
691{
692 int i;
693 int ch;
694
695 mem += count - 1;
696 for (i = 0; i < count; i++) {
697 ch = *mem--;
698 buf = pack_hex_byte(buf, ch);
699 }
700
701
702 *buf = '\0';
703 return buf;
704}
705
706
707
708
709static unsigned char*
710hex2mem(unsigned char *mem, char *buf, int count)
711{
712 int i;
713 unsigned char ch;
714 for (i = 0; i < count; i++) {
715 ch = hex (*buf++) << 4;
716 ch = ch + hex (*buf++);
717 *mem++ = ch;
718 }
719 return mem;
720}
721
722
723
724
725
726static unsigned char*
727bin2mem(unsigned char *mem, unsigned char *buf, int count)
728{
729 int i;
730 unsigned char *next;
731 for (i = 0; i < count; i++) {
732
733
734 if (*buf == 0x7d) {
735 next = buf + 1;
736 if (*next == 0x3 || *next == 0x4 || *next == 0x5D) {
737
738 buf++;
739 *buf += 0x20;
740 }
741 }
742 *mem++ = *buf++;
743 }
744 return mem;
745}
746
747
748
749static void
750getpacket(char *buffer)
751{
752 unsigned char checksum;
753 unsigned char xmitcsum;
754 int i;
755 int count;
756 char ch;
757
758 do {
759 while((ch = getDebugChar ()) != '$')
760 ;
761 checksum = 0;
762 xmitcsum = -1;
763 count = 0;
764
765 while (count < BUFMAX) {
766 ch = getDebugChar();
767 if (ch == '#')
768 break;
769 checksum = checksum + ch;
770 buffer[count] = ch;
771 count = count + 1;
772 }
773
774 if (count >= BUFMAX)
775 continue;
776
777 buffer[count] = 0;
778
779 if (ch == '#') {
780 xmitcsum = hex(getDebugChar()) << 4;
781 xmitcsum += hex(getDebugChar());
782 if (checksum != xmitcsum) {
783
784 putDebugChar('-');
785 } else {
786
787 putDebugChar('+');
788
789 if (buffer[2] == ':') {
790 putDebugChar(buffer[0]);
791 putDebugChar(buffer[1]);
792
793 count = gdb_cris_strlen(buffer);
794 for (i = 3; i <= count; i++)
795 buffer[i - 3] = buffer[i];
796 }
797 }
798 }
799 } while (checksum != xmitcsum);
800}
801
802
803
804static void
805putpacket(char *buffer)
806{
807 int checksum;
808 int runlen;
809 int encode;
810
811 do {
812 char *src = buffer;
813 putDebugChar('$');
814 checksum = 0;
815 while (*src) {
816
817 putDebugChar(*src);
818 checksum += *src;
819 runlen = 0;
820 while (runlen < RUNLENMAX && *src == src[runlen]) {
821 runlen++;
822 }
823 if (runlen > 3) {
824
825 putDebugChar ('*');
826 checksum += '*';
827 encode = runlen + ' ' - 4;
828 putDebugChar(encode);
829 checksum += encode;
830 src += runlen;
831 } else {
832 src++;
833 }
834 }
835 putDebugChar('#');
836 putDebugChar(hex_asc_hi(checksum));
837 putDebugChar(hex_asc_lo(checksum));
838 } while(kgdb_started && (getDebugChar() != '+'));
839}
840
841
842
843void
844putDebugString(const unsigned char *str, int len)
845{
846
847 asm("spchere:");
848 asm("move $spc, $r10");
849 asm("cmp.d spchere, $r10");
850 asm("bne nosstep");
851 asm("nop");
852 asm("move.d spccont, $r10");
853 asm("move $r10, $spc");
854 asm("nosstep:");
855
856 output_buffer[0] = 'O';
857 mem2hex(&output_buffer[1], (unsigned char *)str, len);
858 putpacket(output_buffer);
859
860 asm("spccont:");
861}
862
863
864
865
866
867
868
869
870
871
872
873
874static void
875stub_is_stopped(int sigval)
876{
877 char *ptr = output_buffer;
878 unsigned int reg_cont;
879
880
881
882 *ptr++ = 'T';
883 ptr = pack_hex_byte(ptr, sigval);
884
885 if (((reg.exs & 0xff00) >> 8) == 0xc) {
886
887
888
889 int S, bp, trig_bits = 0, rw_bits = 0;
890 int trig_mask = 0;
891 unsigned int *bp_d_regs = &sreg.s3_3;
892
893
894
895 unsigned int stopped_data_address;
896
897 S = (reg.exs & 0xffff0000) >> 16;
898
899 if (S & 1) {
900
901
902 } else {
903
904 for (bp = 0; bp < 6; bp++) {
905
906
907 int bitpos_trig = 1 + bp * 2;
908
909 int bitpos_config = 2 + bp * 4;
910
911
912 trig_bits = (S & (3 << bitpos_trig)) >> bitpos_trig;
913
914
915 rw_bits = (sreg.s0_3 & (3 << bitpos_config)) >> bitpos_config;
916 if (trig_bits) {
917
918
919 if ((rw_bits == 0x1 && trig_bits != 0x1) ||
920 (rw_bits == 0x2 && trig_bits != 0x2))
921 panic("Invalid r/w trigging for this BP");
922
923
924 trig_mask |= (1 << bp);
925
926 if (reg.eda >= bp_d_regs[bp * 2] &&
927 reg.eda <= bp_d_regs[bp * 2 + 1]) {
928
929
930 stopped_data_address = reg.eda;
931 break;
932 }
933 }
934 }
935 if (bp < 6) {
936
937 } else if (trig_mask) {
938
939 for (bp = 0; bp < 6; bp++) {
940
941 int bitpos_config = 2 + bp * 4;
942
943
944 rw_bits = (sreg.s0_3 & (3 << bitpos_config)) >> bitpos_config;
945
946 if (trig_mask & (1 << bp)) {
947
948 if (reg.eda + 31 >= bp_d_regs[bp * 2]) {
949
950
951 stopped_data_address = bp_d_regs[bp * 2];
952 break;
953 } else {
954
955 printk("EDA doesn't match trigged BP's range");
956 }
957 }
958 }
959 }
960
961
962 BUG_ON(bp >= 6);
963
964
965
966
967 if (rw_bits == 0x1) {
968
969 strncpy(ptr, "rwatch", 6);
970 ptr += 6;
971 } else if (rw_bits == 0x2) {
972
973 strncpy(ptr, "watch", 5);
974 ptr += 5;
975 } else if (rw_bits == 0x3) {
976
977 strncpy(ptr, "awatch", 6);
978 ptr += 6;
979 } else {
980 panic("Invalid r/w bits for this BP.");
981 }
982
983 *ptr++ = ':';
984
985 ptr = mem2hex_nbo(ptr, (unsigned char *)&stopped_data_address, register_size[EDA]);
986 *ptr++ = ';';
987 }
988 }
989
990 read_register(PC, ®_cont);
991 ptr = pack_hex_byte(PC);
992 *ptr++ = ':';
993 ptr = mem2hex(ptr, (unsigned char *)®_cont, register_size[PC]);
994 *ptr++ = ';';
995
996 read_register(R8, ®_cont);
997 ptr = pack_hex_byte(R8);
998 *ptr++ = ':';
999 ptr = mem2hex(ptr, (unsigned char *)®_cont, register_size[R8]);
1000 *ptr++ = ';';
1001
1002 read_register(SP, ®_cont);
1003 ptr = pack_hex_byte(SP);
1004 *ptr++ = ':';
1005 ptr = mem2hex(ptr, (unsigned char *)®_cont, register_size[SP]);
1006 *ptr++ = ';';
1007
1008
1009 read_register(ERP, ®_cont);
1010 ptr = pack_hex_byte(ERP);
1011 *ptr++ = ':';
1012 ptr = mem2hex(ptr, (unsigned char *)®_cont, register_size[ERP]);
1013 *ptr++ = ';';
1014
1015
1016 *ptr = 0;
1017 putpacket(output_buffer);
1018}
1019
1020
1021
1022int insn_size(unsigned long pc)
1023{
1024 unsigned short opcode = *(unsigned short *)pc;
1025 int size = 0;
1026
1027 switch ((opcode & 0x0f00) >> 8) {
1028 case 0x0:
1029 case 0x9:
1030 case 0xb:
1031 size = 2;
1032 break;
1033 case 0xe:
1034 case 0xf:
1035 size = 6;
1036 break;
1037 case 0xd:
1038
1039 if ((opcode & 0xff) == 0xff)
1040 size = 4;
1041 else
1042 size = 6;
1043 break;
1044 default:
1045 panic("Couldn't find size of opcode 0x%x at 0x%lx\n", opcode, pc);
1046 }
1047
1048 return size;
1049}
1050
1051void register_fixup(int sigval)
1052{
1053
1054 reg.sp += 4;
1055
1056
1057 reg.pc = reg.erp;
1058 if (reg.erp & 0x1) {
1059
1060 if (reg.spc) {
1061
1062 reg.pc = reg.spc;
1063 } else {
1064
1065
1066 reg.pc += insn_size(reg.erp & ~1) - 1 ;
1067 }
1068 }
1069
1070 if ((reg.exs & 0x3) == 0x0) {
1071
1072
1073
1074 reg.eda = 0;
1075 }
1076
1077 if (sigval == SIGTRAP) {
1078
1079
1080
1081 if (((reg.exs & 0xff00) >> 8) == 0x18) {
1082
1083
1084
1085
1086
1087
1088
1089
1090 if (!dynamic_bp) {
1091
1092 dynamic_bp = 1;
1093 } else {
1094
1095
1096 if (!(reg.erp & 0x1)) {
1097 reg.erp -= 2;
1098 reg.pc -= 2;
1099 }
1100 }
1101
1102 } else if (((reg.exs & 0xff00) >> 8) == 0x3) {
1103
1104
1105
1106 } else if (((reg.exs & 0xff00) >> 8) == 0xc) {
1107
1108
1109
1110
1111
1112 reg.spc = 0;
1113
1114
1115 }
1116
1117 } else if (sigval == SIGINT) {
1118
1119 }
1120}
1121
1122static void insert_watchpoint(char type, int addr, int len)
1123{
1124
1125
1126
1127
1128
1129
1130
1131
1132 if (type < '1' || type > '4') {
1133 output_buffer[0] = 0;
1134 return;
1135 }
1136
1137
1138
1139 if (type == '3')
1140 type = '4';
1141
1142 if (type == '1') {
1143
1144
1145 if (sreg.s0_3 & 0x1) {
1146
1147 gdb_cris_strcpy(output_buffer, error_message[E04]);
1148 return;
1149 }
1150
1151 sreg.s1_3 = addr;
1152 sreg.s2_3 = (addr + len - 1);
1153 sreg.s0_3 |= 1;
1154 } else {
1155 int bp;
1156 unsigned int *bp_d_regs = &sreg.s3_3;
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166 for (bp = 0; bp < 6; bp++) {
1167
1168
1169
1170 if (!(sreg.s0_3 & (0x3 << (2 + (bp * 4))))) {
1171 break;
1172 }
1173 }
1174
1175 if (bp > 5) {
1176
1177 gdb_cris_strcpy(output_buffer, error_message[E04]);
1178 return;
1179 }
1180
1181
1182 if (type == '3' || type == '4') {
1183
1184 sreg.s0_3 |= (1 << (2 + bp * 4));
1185 }
1186 if (type == '2' || type == '4') {
1187
1188 sreg.s0_3 |= (2 << (2 + bp * 4));
1189 }
1190
1191
1192 bp_d_regs[bp * 2] = addr;
1193 bp_d_regs[bp * 2 + 1] = (addr + len - 1);
1194 }
1195
1196
1197 reg.ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
1198 gdb_cris_strcpy(output_buffer, "OK");
1199}
1200
1201static void remove_watchpoint(char type, int addr, int len)
1202{
1203
1204
1205
1206
1207
1208
1209
1210 if (type < '1' || type > '4') {
1211 output_buffer[0] = 0;
1212 return;
1213 }
1214
1215
1216
1217 if (type == '3')
1218 type = '4';
1219
1220 if (type == '1') {
1221
1222
1223 if (!(sreg.s0_3 & 0x1)) {
1224
1225 gdb_cris_strcpy(output_buffer, error_message[E04]);
1226 return;
1227 }
1228
1229 sreg.s1_3 = 0;
1230 sreg.s2_3 = 0;
1231 sreg.s0_3 &= ~1;
1232 } else {
1233 int bp;
1234 unsigned int *bp_d_regs = &sreg.s3_3;
1235
1236
1237
1238
1239
1240
1241
1242 for (bp = 0; bp < 6; bp++) {
1243 if (bp_d_regs[bp * 2] == addr &&
1244 bp_d_regs[bp * 2 + 1] == (addr + len - 1)) {
1245
1246 int bitpos = 2 + bp * 4;
1247 int rw_bits;
1248
1249
1250 rw_bits = (sreg.s0_3 & (0x3 << bitpos)) >> bitpos;
1251
1252 if ((type == '3' && rw_bits == 0x1) ||
1253 (type == '2' && rw_bits == 0x2) ||
1254 (type == '4' && rw_bits == 0x3)) {
1255
1256 break;
1257 }
1258 }
1259 }
1260
1261 if (bp > 5) {
1262
1263 gdb_cris_strcpy(output_buffer, error_message[E04]);
1264 return;
1265 }
1266
1267
1268
1269
1270 sreg.s0_3 &= ~(3 << (2 + (bp * 4)));
1271 bp_d_regs[bp * 2] = 0;
1272 bp_d_regs[bp * 2 + 1] = 0;
1273 }
1274
1275
1276 gdb_cris_strcpy(output_buffer, "OK");
1277}
1278
1279
1280
1281
1282
1283void
1284handle_exception(int sigval)
1285{
1286
1287
1288 USEDFUN(handle_exception);
1289 USEDVAR(internal_stack[0]);
1290
1291 register_fixup(sigval);
1292
1293
1294 stub_is_stopped(sigval);
1295
1296 for (;;) {
1297 output_buffer[0] = '\0';
1298 getpacket(input_buffer);
1299 switch (input_buffer[0]) {
1300 case 'g':
1301
1302
1303
1304
1305
1306 {
1307 char *buf;
1308
1309 buf = mem2hex(output_buffer, (char *)®, sizeof(registers));
1310
1311
1312 mem2hex(buf,
1313 (char *)&sreg + (reg.srs * 16 * sizeof(unsigned int)),
1314 16 * sizeof(unsigned int));
1315 break;
1316 }
1317 case 'G':
1318
1319
1320
1321
1322
1323 hex2mem((char *)®, &input_buffer[1], sizeof(registers));
1324
1325 hex2mem((char *)&sreg + (reg.srs * 16 * sizeof(unsigned int)),
1326 &input_buffer[1] + sizeof(registers),
1327 16 * sizeof(unsigned int));
1328 gdb_cris_strcpy(output_buffer, "OK");
1329 break;
1330
1331 case 'P':
1332
1333
1334
1335
1336
1337
1338
1339 {
1340 char *suffix;
1341 int regno = gdb_cris_strtol(&input_buffer[1], &suffix, 16);
1342 int status;
1343
1344 status = write_register(regno, suffix+1);
1345
1346 switch (status) {
1347 case E02:
1348
1349 gdb_cris_strcpy(output_buffer, error_message[E02]);
1350 break;
1351 case E05:
1352
1353 gdb_cris_strcpy(output_buffer, error_message[E05]);
1354 break;
1355 default:
1356
1357 gdb_cris_strcpy(output_buffer, "OK");
1358 break;
1359 }
1360 }
1361 break;
1362
1363 case 'm':
1364
1365
1366
1367
1368
1369
1370 {
1371 char *suffix;
1372 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&input_buffer[1],
1373 &suffix, 16);
1374 int len = gdb_cris_strtol(suffix+1, 0, 16);
1375
1376
1377
1378 if (!((unsigned int)addr >= 0xc0000000 &&
1379 (unsigned int)addr < 0xd0000000))
1380 addr = NULL;
1381
1382 mem2hex(output_buffer, addr, len);
1383 }
1384 break;
1385
1386 case 'X':
1387
1388
1389
1390
1391
1392 case 'M':
1393
1394
1395
1396
1397
1398 {
1399 char *lenptr;
1400 char *dataptr;
1401 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&input_buffer[1],
1402 &lenptr, 16);
1403 int len = gdb_cris_strtol(lenptr+1, &dataptr, 16);
1404 if (*lenptr == ',' && *dataptr == ':') {
1405 if (input_buffer[0] == 'M') {
1406 hex2mem(addr, dataptr + 1, len);
1407 } else {
1408 bin2mem(addr, dataptr + 1, len);
1409 }
1410 gdb_cris_strcpy(output_buffer, "OK");
1411 }
1412 else {
1413 gdb_cris_strcpy(output_buffer, error_message[E06]);
1414 }
1415 }
1416 break;
1417
1418 case 'c':
1419
1420
1421
1422
1423
1424
1425 if (input_buffer[1] != '\0') {
1426
1427 gdb_cris_strcpy(output_buffer, error_message[E04]);
1428 break;
1429 }
1430
1431
1432
1433
1434 reg.spc = 0;
1435
1436
1437
1438 if ((sreg.s0_3 & 0x3fff) == 0) {
1439 reg.ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
1440 }
1441
1442 return;
1443
1444 case 's':
1445
1446
1447
1448
1449
1450 if (input_buffer[1] != '\0') {
1451
1452 gdb_cris_strcpy(output_buffer, error_message[E04]);
1453 break;
1454 }
1455
1456
1457
1458 reg.spc = reg.pc;
1459
1460
1461
1462 reg.ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
1463 return;
1464
1465 case 'Z':
1466
1467
1468
1469
1470 {
1471 char *lenptr;
1472 char *dataptr;
1473 int addr = gdb_cris_strtol(&input_buffer[3], &lenptr, 16);
1474 int len = gdb_cris_strtol(lenptr + 1, &dataptr, 16);
1475 char type = input_buffer[1];
1476
1477 insert_watchpoint(type, addr, len);
1478 break;
1479 }
1480
1481 case 'z':
1482
1483
1484
1485 {
1486 char *lenptr;
1487 char *dataptr;
1488 int addr = gdb_cris_strtol(&input_buffer[3], &lenptr, 16);
1489 int len = gdb_cris_strtol(lenptr + 1, &dataptr, 16);
1490 char type = input_buffer[1];
1491
1492 remove_watchpoint(type, addr, len);
1493 break;
1494 }
1495
1496
1497 case '?':
1498
1499
1500
1501 output_buffer[0] = 'S';
1502 output_buffer[1] = hex_asc_hi(sigval);
1503 output_buffer[2] = hex_asc_lo(sigval);
1504 output_buffer[3] = 0;
1505 break;
1506
1507 case 'D':
1508
1509
1510
1511 putpacket("OK");
1512 return;
1513
1514 case 'k':
1515 case 'r':
1516
1517
1518
1519 kill_restart();
1520 break;
1521
1522 case 'C':
1523 case 'S':
1524 case '!':
1525 case 'R':
1526 case 'd':
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 gdb_cris_strcpy(output_buffer, error_message[E04]);
1538 break;
1539
1540 default:
1541
1542
1543
1544 output_buffer[0] = 0;
1545 break;
1546 }
1547 putpacket(output_buffer);
1548 }
1549}
1550
1551void
1552kgdb_init(void)
1553{
1554 reg_intr_vect_rw_mask intr_mask;
1555 reg_ser_rw_intr_mask ser_intr_mask;
1556
1557
1558#if defined(CONFIG_ETRAX_KGDB_PORT0)
1559
1560
1561 set_exception_vector(SER0_INTR_VECT, kgdb_handle_exception);
1562
1563 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
1564 intr_mask.ser0 = 1;
1565 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
1566
1567 ser_intr_mask = REG_RD(ser, regi_ser0, rw_intr_mask);
1568 ser_intr_mask.dav = regk_ser_yes;
1569 REG_WR(ser, regi_ser0, rw_intr_mask, ser_intr_mask);
1570#elif defined(CONFIG_ETRAX_KGDB_PORT1)
1571
1572
1573 set_exception_vector(SER1_INTR_VECT, kgdb_handle_exception);
1574
1575 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
1576 intr_mask.ser1 = 1;
1577 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
1578
1579 ser_intr_mask = REG_RD(ser, regi_ser1, rw_intr_mask);
1580 ser_intr_mask.dav = regk_ser_yes;
1581 REG_WR(ser, regi_ser1, rw_intr_mask, ser_intr_mask);
1582#elif defined(CONFIG_ETRAX_KGDB_PORT2)
1583
1584
1585 set_exception_vector(SER2_INTR_VECT, kgdb_handle_exception);
1586
1587 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
1588 intr_mask.ser2 = 1;
1589 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
1590
1591 ser_intr_mask = REG_RD(ser, regi_ser2, rw_intr_mask);
1592 ser_intr_mask.dav = regk_ser_yes;
1593 REG_WR(ser, regi_ser2, rw_intr_mask, ser_intr_mask);
1594#elif defined(CONFIG_ETRAX_KGDB_PORT3)
1595
1596
1597 set_exception_vector(SER3_INTR_VECT, kgdb_handle_exception);
1598
1599 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
1600 intr_mask.ser3 = 1;
1601 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
1602
1603 ser_intr_mask = REG_RD(ser, regi_ser3, rw_intr_mask);
1604 ser_intr_mask.dav = regk_ser_yes;
1605 REG_WR(ser, regi_ser3, rw_intr_mask, ser_intr_mask);
1606#endif
1607
1608}
1609
1610static void
1611kill_restart(void)
1612{
1613 machine_restart("");
1614}
1615
1616
1617
1618void
1619breakpoint(void)
1620{
1621 kgdb_started = 1;
1622 dynamic_bp = 0;
1623 __asm__ volatile ("break 8");
1624}
1625
1626
1627