summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/mips/shared/gdbstub/mips-stub.c
blob: 494af262ac61432fef0c8e390468efe3e264aa70 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
#define  GDB_STUB_ENABLE_THREAD_SUPPORT 1
/*******************************************************************************

                     THIS SOFTWARE IS NOT COPYRIGHTED

    The following software is offered for use in the public domain.
    There is no warranty with regard to this software or its performance
    and the user must accept the software "AS IS" with all faults.

    THE CONTRIBUTORS DISCLAIM ANY WARRANTIES, EXPRESS OR IMPLIED, WITH
    REGARD TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

    $Id$

********************************************************************************
*
*   r46kstub.c -- target debugging stub for the IDT R4600 Orion processor
*
*   This module is based on the stub for the Hitachi SH processor written by
*   Ben Lee and Steve Chamberlain and supplied with gdb 4.16.  The latter
*   in turn "is originally based on an m68k software stub written by Glenn
*   Engel at HP, but has changed quite a bit."  The changes for the R4600
*   were written by C. M. Heard at VVNET.  They were based in part on the
*   Algorithmics R4000 version of Phil Bunce's PMON program.
*
*  Remote communication protocol:
*
*  A debug packet whose contents are <data>
*  is encapsulated for transmission in the form:
*
*       $ <data> # CSUM1 CSUM2
*
*       <data> must be ASCII alphanumeric and cannot include characters
*       '$' or '#'.  If <data> starts with two characters followed by
*       ':', then the existing stubs interpret this as a sequence number.
*
*       CSUM1 and CSUM2 are ascii hex representation of an 8-bit
*       checksum of <data>, the most significant nibble is sent first.
*       the hex digits 0-9,a-f are used.
*
*  Receiver responds with:
*
*       +    if CSUM is correct
*       -    if CSUM is incorrect
*
*  <data> is as follows.  All values are encoded in ascii hex digits.
*
*       Request         Packet
*
*       read registers  g
*       reply           XX....X         Each byte of register data
*                                       is described by two hex digits.
*                                       Registers are in the internal order
*                                       for GDB, and the bytes in a register
*                                       are in the same order the machine uses.
*                       or ENN          for an error.
*
*       write regs      GXX..XX         Each byte of register data
*                                       is described by two hex digits.
*       reply           OK              for success
*                       ENN             for an error
*
*       write reg       Pn...=r...      Write register n... with value r....
*       reply           OK              for success
*                       ENN             for an error
*
*       read mem        mAA..AA,LLLL    AA..AA is address, LLLL is length.
*       reply           XX..XX          XX..XX is mem contents
*                                       Can be fewer bytes than requested
*                                       if able to read only part of the data.
*                       or ENN          NN is errno
*
*       write mem       MAA..AA,LLLL:XX..XX
*                                       AA..AA is address,
*                                       LLLL is number of bytes,
*                                       XX..XX is data
*       reply           OK              for success
*                       ENN             for an error (this includes the case
*                                       where only part of the data was
*                                       written).
*
*       cont            cAA..AA         AA..AA is address to resume
*                                       If AA..AA is omitted,
*                                       resume at same address.
*
*       step            sAA..AA         AA..AA is address to resume
*                                       If AA..AA is omitted,
*                                       resume at same address.
*
*       There is no immediate reply to step or cont.
*       The reply comes when the machine stops.
*       It is           SAA             AA is the "signal number"
*
*       last signal     ?               Reply with the reason for stopping.
*                                       This is the same reply as is generated
*                                       for step or cont: SAA where AA is the
*                                       signal number.
*
*       detach          D               Host is detaching.  Reply OK and
*                                       end remote debugging session.
*
*       reserved        <other>         On other requests, the stub should
*                                       ignore the request and send an empty
*                                       response ($#<checksum>).  This way
*                                       we can extend the protocol and GDB
*                                       can tell whether the stub it is
*                                       talking to uses the old or the new.
*
*       Responses can be run-length encoded to save space.  A '*' means that
*       the next character is an ASCII encoding giving a repeat count which
*       stands for that many repetitions of the character preceding the '*'.
*       The encoding is n+29, yielding a printable character when n >=3
*       (which is where rle starts to win).  Don't use n > 99 since gdb
*       masks each character is receives with 0x7f in order to strip off
*       the parity bit.
*
*       As an example, "0* " means the same thing as "0000".
*
*******************************************************************************/


#include <string.h>
#include <signal.h>
#include "mips_opcode.h"
#include "memlimits.h"
#include <rtems.h>
#include "gdb_if.h"

extern int printk(const char *fmt, ...);

/* Change it to something meaningful when debugging */
#undef ASSERT
#define ASSERT(x) if(!(x)) printk("ASSERT: stub: %d\n", __LINE__)

/***************/
/* Exception Codes */
#define	EXC_INT		0		/* External interrupt */
#define	EXC_MOD		1		/* TLB modification exception */
#define	EXC_TLBL	2		/* TLB miss (Load or Ifetch) */
#define	EXC_TLBS	3		/* TLB miss (Store) */
#define	EXC_ADEL	4		/* Address error (Load or Ifetch) */
#define	EXC_ADES	5		/* Address error (Store) */
#define	EXC_IBE		6		/* Bus error (Ifetch) */
#define	EXC_DBE		7		/* Bus error (data load or store) */
#define	EXC_SYS		8		/* System call */
#define	EXC_BP		9		/* Break point */
#define	EXC_RI		10		/* Reserved instruction */
#define	EXC_CPU		11		/* Coprocessor unusable */
#define	EXC_OVF		12		/* Arithmetic overflow */
#define	EXC_TRAP	13		/* Trap exception */
#define	EXC_FPE		15		/* Floating Point Exception */

/* FPU Control/Status register fields */
#define	CSR_FS		0x01000000	/* Set to flush denormals to zero */
#define	CSR_C		0x00800000	/* Condition bit (set by FP compare) */

#define	CSR_CMASK	(0x3f<<12)
#define	CSR_CE		0x00020000
#define	CSR_CV		0x00010000
#define	CSR_CZ		0x00008000
#define	CSR_CO		0x00004000
#define	CSR_CU		0x00002000
#define	CSR_CI		0x00001000

#define	CSR_EMASK	(0x1f<<7)
#define	CSR_EV		0x00000800
#define	CSR_EZ		0x00000400
#define	CSR_EO		0x00000200
#define	CSR_EU		0x00000100
#define	CSR_EI		0x00000080

#define	CSR_FMASK	(0x1f<<2)
#define	CSR_FV		0x00000040
#define	CSR_FZ		0x00000020
#define	CSR_FO		0x00000010
#define	CSR_FU		0x00000008
#define	CSR_FI		0x00000004

#define	CSR_RMODE_MASK	(0x3<<0)
#define	CSR_RM		0x00000003
#define	CSR_RP		0x00000002
#define	CSR_RZ		0x00000001
#define	CSR_RN		0x00000000

/***************/

/*
 * Saved register information.  Must be prepared by the exception
 * preprocessor before handle_exception is invoked.
 */
#if (__mips == 3)
typedef long long mips_register_t;
#define R_SZ 8
#elif (__mips == 1)
typedef unsigned int mips_register_t;
#define R_SZ 4
#else
#error "unknown MIPS ISA"
#endif
static mips_register_t *registers;

#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
static char do_threads;      /* != 0 means we are supporting threads */
#endif

/*
 * The following external functions provide character input and output.
 */
extern char getDebugChar (void);

extern void putDebugChar (char);


/*
 * BUFMAX defines the maximum number of characters in the inbound & outbound
 * packet buffers.  At least 4+(sizeof registers)*2 bytes will be needed for
 * register packets.  Memory dump packets can profitably use even more.
 */
#define BUFMAX 1500

static char inBuffer[BUFMAX];
static char outBuffer[BUFMAX];

/* Structure to keep info on a z-breaks */
#define BREAKNUM 32
struct z0break
{
  /* List support */
  struct z0break *next;
  struct z0break *prev;

  /* Location, preserved data */
  unsigned char *address;
  char     buf[2];
};

static struct z0break z0break_arr[BREAKNUM];
static struct z0break *z0break_avail = NULL;
static struct z0break *z0break_list  = NULL;


/*
 * Convert an int to hex.
 */
const char gdb_hexchars[] = "0123456789abcdef";

#define highhex(x) gdb_hexchars [(x >> 4) & 0xf]
#define lowhex(x) gdb_hexchars [x & 0xf]


/*
 * Convert length bytes of data starting at addr into hex, placing the
 * result in buf.  Return a pointer to the last (null) char in buf.
 */
static char *
mem2hex (void *_addr, int length, char *buf)
{
  unsigned int addr = (unsigned int) _addr;

  if (((addr & 0x7) == 0) && ((length & 0x7) == 0))      /* dword aligned */
    {
      long long *source = (long long *) (addr);
      long long *limit  = (long long *) (addr + length);

      while (source < limit)
        {
          int i;
          long long k = *source++;

          for (i = 15; i >= 0; i--)
            *buf++ = gdb_hexchars [(k >> (i*4)) & 0xf];
        }
    }
  else if (((addr & 0x3) == 0) && ((length & 0x3) == 0)) /* word aligned */
    {
      int *source = (int *) (addr);
      int *limit  = (int *) (addr + length);

      while (source < limit)
        {
          int i;
          int k = *source++;

          for (i = 7; i >= 0; i--)
            *buf++ = gdb_hexchars [(k >> (i*4)) & 0xf];
        }
    }
  else if (((addr & 0x1) == 0) && ((length & 0x1) == 0)) /* halfword aligned */
    {
      short *source = (short *) (addr);
      short *limit  = (short *) (addr + length);

      while (source < limit)
        {
          int i;
          short k = *source++;

          for (i = 3; i >= 0; i--)
            *buf++ = gdb_hexchars [(k >> (i*4)) & 0xf];
        }
    }
  else                                                   /* byte aligned */
    {
      char *source = (char *) (addr);
      char *limit  = (char *) (addr + length);

      while (source < limit)
        {
          int i;
          char k = *source++;

          for (i = 1; i >= 0; i--)
            *buf++ = gdb_hexchars [(k >> (i*4)) & 0xf];
        }
    }

  *buf = '\0';
  return (buf);
}


/*
 * Convert a hex character to an int.
 */
static int
hex (char ch)
{
  if ((ch >= 'a') && (ch <= 'f'))
    return (ch - 'a' + 10);
  if ((ch >= '0') && (ch <= '9'))
    return (ch - '0');
  if ((ch >= 'A') && (ch <= 'F'))
    return (ch - 'A' + 10);
  return (-1);
}


/*
 * Convert a string from hex to int until a non-hex digit
 * is found.  Return the number of characters processed.
 */
static int
hexToInt (char **ptr, int *intValue)
{
  int numChars = 0;
  int hexValue;

  *intValue = 0;

  while (**ptr)
    {
      hexValue = hex (**ptr);
      if (hexValue >= 0)
        {
          *intValue = (*intValue << 4) | hexValue;
          numChars++;
        }
      else
        break;

      (*ptr)++;
    }

  return (numChars);
}


/*
 * Convert a string from hex to long long until a non-hex
 * digit is found.  Return the number of characters processed.
 */
static int
hexToLongLong (char **ptr, long long *intValue)
{
  int numChars = 0;
  int hexValue;

  *intValue = 0;

  while (**ptr)
    {
      hexValue = hex (**ptr);
      if (hexValue >= 0)
        {
          *intValue = (*intValue << 4) | hexValue;
          numChars++;
        }
      else
        break;

      (*ptr)++;
    }

  return (numChars);
}


/*
 * Convert the hex array buf into binary, placing the result at the
 * specified address.  If the conversion fails at any point (i.e.,
 * if fewer bytes are written than indicated by the size parameter)
 * then return 0;  otherwise return 1.
 */
static int
hex2mem (char *buf, void *_addr, int length)
{
  unsigned int addr = (unsigned int) _addr;
  if (((addr & 0x7) == 0) && ((length & 0x7) == 0))      /* dword aligned */
    {
      long long *target = (long long *) (addr);
      long long *limit  = (long long *) (addr + length);

      while (target < limit)
        {
          int i, j;
          long long k = 0;

          for (i = 0; i < 16; i++)
            if ((j = hex(*buf++)) < 0)
              return 0;
            else
              k = (k << 4) + j;
          *target++ = k;
        }
    }
  else if (((addr & 0x3) == 0) && ((length & 0x3) == 0)) /* word aligned */
    {
      int *target = (int *) (addr);
      int *limit  = (int *) (addr + length);

      while (target < limit)
        {
          int i, j;
          int k = 0;

          for (i = 0; i < 8; i++)
            if ((j = hex(*buf++)) < 0)
              return 0;
            else
              k = (k << 4) + j;
          *target++ = k;
        }
    }
  else if (((addr & 0x1) == 0) && ((length & 0x1) == 0)) /* halfword aligned */
    {
      short *target = (short *) (addr);
      short *limit  = (short *) (addr + length);

      while (target < limit)
        {
          int i, j;
          short k = 0;

          for (i = 0; i < 4; i++)
            if ((j = hex(*buf++)) < 0)
              return 0;
            else
              k = (k << 4) + j;
          *target++ = k;
        }
    }
  else                                                   /* byte aligned */
    {
      char *target = (char *) (addr);
      char *limit  = (char *) (addr + length);

      while (target < limit)
        {
          int i, j;
          char k = 0;

          for (i = 0; i < 2; i++)
            if ((j = hex(*buf++)) < 0)
              return 0;
            else
              k = (k << 4) + j;
          *target++ = k;
        }
    }

  return 1;
}


/* Convert the binary stream in BUF to memory.

   Gdb will escape $, #, and the escape char (0x7d).
   COUNT is the total number of bytes to write into
   memory. */
static unsigned char *
bin2mem (
  unsigned char *buf,
  unsigned char *mem,
  int   count
)
{
  int i;

  for (i = 0; i < count; i++) {
      /* Check for any escaped characters. Be paranoid and
         only unescape chars that should be escaped. */
      if (*buf == 0x7d) {
          switch (*(buf+1)) {
            case 0x3:  /* # */
            case 0x4:  /* $ */
            case 0x5d: /* escape char */
              buf++;
              *buf |= 0x20;
              break;
            default:
              /* nothing */
              break;
            }
        }

      *mem++ = *buf++;
    }

  return mem;
}



/*
 * Scan the input stream for a sequence for the form $<data>#<checksum>.
 */
static void
getpacket (char *buffer)
{
  unsigned char checksum;
  unsigned char xmitcsum;
  int i;
  int count;
  char ch;
  do
    {
      /* wait around for the start character, ignore all other characters */
      while ((ch = getDebugChar ()) != '$');
      checksum = 0;
      xmitcsum = -1;

      count = 0;

      /* now, read until a # or end of buffer is found */
      while ( (count < BUFMAX-1) && ((ch = getDebugChar ()) != '#') )
          checksum += (buffer[count++] = ch);

      /* make sure that the buffer is null-terminated */
      buffer[count] = '\0';

      if (ch == '#')
        {
          xmitcsum = hex (getDebugChar ()) << 4;
          xmitcsum += hex (getDebugChar ());
          if (checksum != xmitcsum)
            putDebugChar ('-'); /* failed checksum */
          else
            {
              putDebugChar ('+');       /* successful transfer */
              /* if a sequence char is present, reply the sequence ID */
              if (buffer[2] == ':')
                {
                  putDebugChar (buffer[0]);
                  putDebugChar (buffer[1]);
                  /* remove sequence chars from buffer */
                  for (i = 3; i <= count; i++)
                    buffer[i - 3] = buffer[i];
                }
            }
        }
    }
  while (checksum != xmitcsum);
}


/*
 * Send the packet in buffer and wait for a positive acknowledgement.
 */
static void
putpacket (char *buffer)
{
  int checksum;

  /* $<packet info>#<checksum> */
  do
    {
      char *src = buffer;
      putDebugChar ('$');
      checksum = 0;

      while (*src != '\0')
        {
          int runlen = 0;

          /* Do run length encoding */
          while ((src[runlen] == src[0]) && (runlen < 99))
            runlen++;
          if (runlen > 3)
            {
              int encode;
              /* Got a useful amount */
              putDebugChar (*src);
              checksum += *src;
              putDebugChar ('*');
              checksum += '*';
              checksum += (encode = (runlen - 4) + ' ');
              putDebugChar (encode);
              src += runlen;
            }
          else
            {
              putDebugChar (*src);
              checksum += *src;
              src++;
             }
        }

      putDebugChar ('#');
      putDebugChar (highhex (checksum));
      putDebugChar (lowhex (checksum));
    }
  while  (getDebugChar () != '+');
}


/*
 * Saved instruction data for single step support
 */
static struct
  {
    unsigned *targetAddr;
    unsigned savedInstr;
  }
instrBuffer;


/*
 * If a step breakpoint was planted restore the saved instruction.
 */
static void
undoSStep (void)
{
  if (instrBuffer.targetAddr != NULL)
    {
      *instrBuffer.targetAddr = instrBuffer.savedInstr;
      instrBuffer.targetAddr = NULL;
    }
  instrBuffer.savedInstr = NOP_INSTR;
}


/*
 * If a single step is requested put a temporary breakpoint at the instruction
 * which logically follows the next one to be executed.  If the next instruction
 * is a branch instruction then skip the instruction in the delay slot.  NOTE:
 * ERET instructions are NOT handled, as it is impossible to single-step through
 * the exit code in an exception handler.  In addition, no attempt is made to
 * do anything about BC0T and BC0F, since a condition bit for coprocessor 0
 * is not defined on the R4600.  Finally, BC2T and BC2F are ignored since there
 * is no coprocessor 2 on a 4600.
 */
static void
doSStep (void)
{
  InstFmt inst;

  instrBuffer.targetAddr = (unsigned *)(registers[PC]+4);    /* set default */

  inst.word = *(unsigned *)registers[PC];     /* read the next instruction  */

  switch (inst.RType.op) {                    /* override default if branch */
    case OP_SPECIAL:
      switch (inst.RType.func) {
        case OP_JR:
        case OP_JALR:
          instrBuffer.targetAddr =
            (unsigned *)registers[inst.RType.rs];
          break;
      };
      break;

    case OP_REGIMM:
      switch (inst.IType.rt) {
        case OP_BLTZ:
        case OP_BLTZL:
        case OP_BLTZAL:
        case OP_BLTZALL:
          if (registers[inst.IType.rs] < 0 )
            instrBuffer.targetAddr =
              (unsigned *)(((signed short)inst.IType.imm<<2)
                                        + (registers[PC]+4));
          else
            instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
          break;
        case OP_BGEZ:
        case OP_BGEZL:
        case OP_BGEZAL:
        case OP_BGEZALL:
          if (registers[inst.IType.rs] >= 0 )
            instrBuffer.targetAddr =
              (unsigned *)(((signed short)inst.IType.imm<<2)
                                        + (registers[PC]+4));
          else
            instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
          break;
      };
      break;

    case OP_J:
    case OP_JAL:
      instrBuffer.targetAddr =
        (unsigned *)((inst.JType.target<<2) + ((registers[PC]+4)&0xf0000000));
      break;

    case OP_BEQ:
    case OP_BEQL:
      if (registers[inst.IType.rs] == registers[inst.IType.rt])
        instrBuffer.targetAddr =
          (unsigned *)(((signed short)inst.IType.imm<<2) + (registers[PC]+4));
      else
        instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
      break;
    case OP_BNE:
    case OP_BNEL:
      if (registers[inst.IType.rs] != registers[inst.IType.rt])
        instrBuffer.targetAddr =
          (unsigned *)(((signed short)inst.IType.imm<<2) + (registers[PC]+4));
      else
        instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
      break;
    case OP_BLEZ:
    case OP_BLEZL:
      if (registers[inst.IType.rs] <= 0)
        instrBuffer.targetAddr =
          (unsigned *)(((signed short)inst.IType.imm<<2) + (registers[PC]+4));
      else
        instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
      break;
    case OP_BGTZ:
    case OP_BGTZL:
      if (registers[inst.IType.rs] > 0)
        instrBuffer.targetAddr =
          (unsigned *)(((signed short)inst.IType.imm<<2) + (registers[PC]+4));
      else
        instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
      break;

    case OP_COP1:
      if (inst.RType.rs == OP_BC)
          switch (inst.RType.rt) {
            case COPz_BCF:
            case COPz_BCFL:
              if (registers[FCSR] & CSR_C)
                instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
              else
                instrBuffer.targetAddr =
                  (unsigned *)(((signed short)inst.IType.imm<<2)
                                            + (registers[PC]+4));
              break;
            case COPz_BCT:
            case COPz_BCTL:
              if (registers[FCSR] & CSR_C)
                instrBuffer.targetAddr =
                  (unsigned *)(((signed short)inst.IType.imm<<2)
                                            + (registers[PC]+4));
              else
                instrBuffer.targetAddr = (unsigned*)(registers[PC]+8);
              break;
          };
      break;
  }

  if is_steppable (instrBuffer.targetAddr)
    {
      instrBuffer.savedInstr = *instrBuffer.targetAddr;
      *instrBuffer.targetAddr = BREAK_INSTR;
    }
  else
    {
      instrBuffer.targetAddr = NULL;
      instrBuffer.savedInstr = NOP_INSTR;
    }
  return;
}


/*
 * Translate the R4600 exception code into a Unix-compatible signal.
 */
static int
computeSignal (void)
{
  int exceptionCode = (registers[CAUSE] & CAUSE_EXCMASK) >> CAUSE_EXCSHIFT;

  switch (exceptionCode)
    {
    case EXC_INT:
      /* External interrupt */
      return SIGINT;

    case EXC_RI:
      /* Reserved instruction */
    case EXC_CPU:
      /* Coprocessor unusable */
      return SIGILL;

    case EXC_BP:
      /* Break point */
      return SIGTRAP;

    case EXC_OVF:
      /* Arithmetic overflow */
    case EXC_TRAP:
      /* Trap exception */
    case EXC_FPE:
      /* Floating Point Exception */
      return SIGFPE;

    case EXC_IBE:
      /* Bus error (Ifetch) */
    case EXC_DBE:
      /* Bus error (data load or store) */
      return SIGBUS;

    case EXC_MOD:
      /* TLB modification exception */
    case EXC_TLBL:
      /* TLB miss (Load or Ifetch) */
    case EXC_TLBS:
      /* TLB miss (Store) */
    case EXC_ADEL:
      /* Address error (Load or Ifetch) */
    case EXC_ADES:
      /* Address error (Store) */
      return SIGSEGV;

    case EXC_SYS:
      /* System call */
      return SIGSYS;

    default:
      return SIGTERM;
    }
}

/*
 *  This support function prepares and sends the message containing the
 *  basic information about this exception.
 */

void gdb_stub_report_exception_info(
  rtems_vector_number vector,
  CPU_Interrupt_frame *frame,
  int                  thread
)
{
  char *optr;
  int sigval;

  optr = outBuffer;
  *optr++ = 'T';
  sigval = computeSignal ();
  *optr++ = highhex (sigval);
  *optr++ = lowhex (sigval);

  *optr++ = gdb_hexchars[SP];
  *optr++ = ':';
  optr    = mem2hstr(optr, (unsigned char *)&frame->sp, R_SZ );
  *optr++ = ';';
  
  *optr++ = gdb_hexchars[PC];
  *optr++ = ':';
  optr    = mem2hstr(optr, (unsigned char *)&frame->sp, R_SZ );
  *optr++ = ';';

#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
  if (do_threads) {
    *optr++ = 't';
    *optr++ = 'h';
    *optr++ = 'r';
    *optr++ = 'e';
    *optr++ = 'a';
    *optr++ = 'd';
    *optr++ = ':';
    optr   = thread2vhstr(optr, thread);
    *optr++ = ';';
  }
#endif
  putpacket (outBuffer);
  
  *optr++ = '\0';
}



/*
 * This function handles all exceptions.  It only does two things:
 * it figures out why it was activated and tells gdb, and then it
 * reacts to gdb's requests.
 */

CPU_Interrupt_frame current_thread_registers;

void handle_exception (rtems_vector_number vector, CPU_Interrupt_frame *frame)
{
  int host_has_detached = 0;
  int regno, addr, length;
  long long regval;
  char *ptr;
  int  current_thread;  /* current generic thread */
  int  thread;          /* stopped thread: context exception happened in */
  void *regptr;
  int  binary;

  registers = (mips_register_t *)frame;

  thread = 0;
#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
  if (do_threads) {
    thread = rtems_gdb_stub_get_current_thread();
  }
#endif
  current_thread = thread;

  /* reply to host that an exception has occurred with some basic info */
  gdb_stub_report_exception_info(vector, frame, thread);


  /*
   * Restore the saved instruction at
   * the single-step target address.
   */
  undoSStep ();

  while (!(host_has_detached)) {
      outBuffer[0] = '\0';
      getpacket (inBuffer);
      binary = 0;

      switch (inBuffer[0]) {
        case '?':
          gdb_stub_report_exception_info(vector, frame, thread);
          break;

        case 'd': /* toggle debug flag */
          /* can print ill-formed commands in valid packets & checksum errors */
          break;

        case 'D':
          /* remote system is detaching - return OK and exit from debugger */
          strcpy (outBuffer, "OK");
          host_has_detached = 1;
          break;

        case 'g':               /* return the values of the CPU registers */
          regptr = registers;
#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
          if (do_threads && current_thread != thread )
             regptr = &current_thread_registers;
#endif
          mem2hex (regptr, NUM_REGS * (sizeof registers), outBuffer);

          break;

        case 'G':       /* set the values of the CPU registers - return OK */
          regptr = registers;
#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
          if (do_threads && current_thread != thread )
             regptr = &current_thread_registers;
#endif
          if (hex2mem (&inBuffer[1], regptr, NUM_REGS * (sizeof registers)))
              strcpy (outBuffer, "OK");
          else
              strcpy (outBuffer, "E00"); /* E00 = bad "set register" command */
          break;

        case 'P':
          /* Pn...=r...  Write register n... with value r... - return OK */
          ptr = &inBuffer[1];
          if (hexToInt(&ptr, &regno) &&
              *ptr++ == '=' &&
              hexToLongLong(&ptr, &regval))
            {
              registers[regno] = regval;
              strcpy (outBuffer, "OK");
            }
          else
              strcpy (outBuffer, "E00"); /* E00 = bad "set register" command */
          break;

        case 'm':
          /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
          ptr = &inBuffer[1];
          if (hexToInt (&ptr, &addr)
              && *ptr++ == ','
              && hexToInt (&ptr, &length)
              && is_readable (addr, length)
              && (length < (BUFMAX - 4)/2))
            mem2hex ((void *)addr, length, outBuffer);
          else
            strcpy (outBuffer, "E01"); /* E01 = bad 'm' command */
          break;

        case 'X':  /* XAA..AA,LLLL:<binary data>#cs */
          binary = 1;
        case 'M':
          /* MAA..AA,LLLL:  Write LLLL bytes at address AA..AA - return OK */
          ptr = &inBuffer[1];
          if (hexToInt (&ptr, &addr)
              && *ptr++ == ','
              && hexToInt (&ptr, &length)
              && *ptr++ == ':'
              && is_writeable (addr, length) ) {
              if ( binary )
		hex2mem (ptr, (void *)addr, length);
	      else
		bin2mem (ptr, (void *)addr, length);
              strcpy (outBuffer, "OK");
          }
          else
            strcpy (outBuffer, "E02"); /* E02 = bad 'M' command */
          break;

        case 'c':
          /* cAA..AA    Continue at address AA..AA(optional) */
        case 's':
          /* sAA..AA    Step one instruction from AA..AA(optional) */
          {
            /* try to read optional parameter, pc unchanged if no parm */
            ptr = &inBuffer[1];
            if (hexToInt (&ptr, &addr))
              registers[PC] = addr;

            if (inBuffer[0] == 's')
              doSStep ();
          }
          return;

        case 'k':  /* remove all zbreaks if any */
          {
            int ret;
            struct z0break *z0, *z0last;

            ret = 1;
            z0last = NULL;

            for (z0=z0break_list; z0!=NULL; z0=z0->next) {
                if (!hstr2mem(z0->address, z0->buf, 1)) {
                   ret = 0;
                }

                z0last = z0;
            }

            /* Free entries if any */
            if (z0last != NULL) {
              z0last->next  = z0break_avail;
              z0break_avail = z0break_list;
              z0break_list  = NULL;
            }
          }

          break;

        case 'q':   /* queries */
#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
          rtems_gdb_process_query( inBuffer, outBuffer, do_threads, thread );
#endif
          break;
        
        case 'H':  /* set new thread */
#if defined(GDB_STUB_ENABLE_THREAD_SUPPORT)
	  if (inBuffer[1] != 'g') {
	    break;
	  }
	  
	  if (!do_threads) {
	    break;
	  }
	  
	  {
	    int tmp, ret;
	    
	    /* Set new generic thread */
	    if (vhstr2thread(&inBuffer[2], &tmp) == NULL) {
	      strcpy(outBuffer, "E01");
	      break;
	    }

	    /* 0 means `thread' */
	    if (tmp == 0) {
	      tmp = thread;
	    }

            if (tmp == current_thread) {
              /* No changes */
	      strcpy(outBuffer, "OK");
	      break;
	    }

            /* Save current thread registers if necessary */ 
            if (current_thread != thread) {
              ret = rtems_gdb_stub_set_thread_regs(
                   current_thread, (unsigned int *) &current_thread_registers);
	      ASSERT(ret);
	    }

	    /* Read new registers if necessary */
	    if (tmp != thread) {
		ret = rtems_gdb_stub_get_thread_regs(
                    tmp, (unsigned int *) &current_thread_registers);

		if (!ret) {
		  /* Thread does not exist */
		  strcpy(outBuffer, "E02");
		  break;
		}
	    }
	    
	    current_thread = tmp;
	    strcpy(outBuffer, "OK");
	  }

#endif
          break;

        case 'Z':  /* Add breakpoint */
          { 
            int ret, type, len;
            unsigned char *address;
            struct z0break *z0;

            ret = parse_zbreak(inBuffer, &type, &address, &len);
            if (!ret) {
              strcpy(outBuffer, "E01");
              break;
            }

            if (type != 0) {
              /* We support only software break points so far */
              break;
            }

            if (len != 1) {
               strcpy(outBuffer, "E02");
               break;
            }

            /* Let us check whether this break point already set */
            for (z0=z0break_list; z0!=NULL; z0=z0->next) {
                if (z0->address == address) {
                  break;
                }
            }

            if (z0 != NULL) {
              /* Repeated packet */
              strcpy(outBuffer, "OK");
              break;
            }

            /* Let us allocate new break point */
            if (z0break_avail == NULL) {
              strcpy(outBuffer, "E03");
              break;
            }

            /* Get entry */
            z0 = z0break_avail;
            z0break_avail = z0break_avail->next;

            /* Let us copy memory from address add stuff the break point in */
            if (mem2hstr(z0->buf, address, 1) == NULL ||
                !hstr2mem(address, "cc" , 1)) {
              /* Memory error */
              z0->next = z0break_avail;
              z0break_avail = z0;
              strcpy(outBuffer, "E03");
              break;
            }

            /* Fill it */
            z0->address = address;

            /* Add to the list */
            z0->next = z0break_list;
            z0->prev = NULL;

            if (z0break_list != NULL) {
              z0break_list->prev = z0;
            }

            z0break_list = z0;

            strcpy(outBuffer, "OK");
          }

        case 'z': /* remove breakpoint */
	  {
	    int ret, type, len;
	    unsigned char  *address;
	    struct z0break *z0, *z0last;

            if (inBuffer[1] == 'z') {
                /* zz packet - remove all breaks */
                ret = 1;
                z0last = NULL;
	        for (z0=z0break_list; z0!=NULL; z0=z0->next) {
	            if(!hstr2mem(z0->address, z0->buf, 1)) {
                        ret = 0;
                      }
                     
                     z0last = z0;
                   }

                /* Free entries if any */
                if (z0last != NULL) {
                    z0last->next  = z0break_avail;
                    z0break_avail = z0break_list;
                    z0break_list  = NULL;
                  }

                if (ret) {
	          strcpy(outBuffer, "OK");
                } else {
	          strcpy(outBuffer, "E04");
	        }

                break;
              }
              
	    ret = parse_zbreak(inBuffer, &type, &address, &len);
	    if (!ret) {
		strcpy(outBuffer, "E01");
		break;
	      }
	    
	    if (type != 0) {
	      /* We support only software break points so far */
	      break;
	    }

            if (len != 1) {
              strcpy(outBuffer, "E02");
              break;
            }
	   
	    /* Let us check whether this break point set */
	    for (z0=z0break_list; z0!=NULL; z0=z0->next) {
	      if (z0->address == address) {
		break;
	      }
	    }
              
	    if (z0 == NULL) {
	      /* Unknown breakpoint */
	      strcpy(outBuffer, "E03");
	      break;
	    }
	    
	    if (!hstr2mem(z0->address, z0->buf, 1)) {
	      strcpy(outBuffer, "E04");
	      break;
	    }
   
	    /* Unlink entry */
	    if (z0->prev == NULL) {
	      z0break_list = z0->next;
	      if (z0break_list != NULL) {
	        z0break_list->prev = NULL;
	      }
	    } else if (z0->next == NULL) {
	      z0->prev->next = NULL;
            } else {
	      z0->prev->next = z0->next;
	      z0->next->prev = z0->prev;
	   }

	   z0->next = z0break_avail;
	   z0break_avail = z0;
	    
	   strcpy(outBuffer, "OK");
	 }

          break;
        default: /* do nothing */
          break;
      }

      /* reply to the request */
      putpacket (outBuffer);
    }

   /*
    *  The original code did this in the assembly wrapper.  We should consider
    *  doing it here before we return.
    *
    *  On exit from the exception handler invalidate each line in the I-cache
    *  and write back each dirty line in the D-cache.  This needs to be done
    *  before the target program is resumed in order to ensure that software
    *  breakpoints and downloaded code will actually take effect.
    */

  return;
}

static char initialized;   /* 0 means we are not initialized */

void mips_gdb_stub_install(void) 
{
  rtems_isr_entry old;
  int i;

  if (initialized) {
    ASSERT(0);
    return;
  }

  /* z0breaks */
  for (i=0; i<(sizeof(z0break_arr)/sizeof(z0break_arr[0]))-1; i++) {
    z0break_arr[i].next = &z0break_arr[i+1];
  }

  z0break_arr[i].next = NULL;
  z0break_avail       = &z0break_arr[0];
  z0break_list        = NULL;


  rtems_interrupt_catch( (rtems_isr_entry) handle_exception, MIPS_EXCEPTION_SYSCALL, &old );
  /* rtems_interrupt_catch( handle_exception, MIPS_EXCEPTION_BREAK, &old ); */

  initialized = 1;
  /* get the attention of gdb */
  mips_break(1);
}