aboutsummaryrefslogtreecommitdiffstats
path: root/arch/frv/kernel/gdb-stub.c
blob: 508601fad0791749933b5da42864d746e1984971 (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
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
/* gdb-stub.c: FRV GDB stub
 *
 * Copyright (C) 2003,4 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 * - Derived from Linux/MIPS version, Copyright (C) 1995 Andreas Busse
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

/*
 *  To enable debugger support, two things need to happen.  One, a
 *  call to set_debug_traps() is necessary in order to allow any breakpoints
 *  or error conditions to be properly intercepted and reported to gdb.
 *  Two, a breakpoint needs to be generated to begin communication.  This
 *  is most easily accomplished by a call to breakpoint().  Breakpoint()
 *  simulates a breakpoint by executing a BREAK instruction.
 *
 *
 *    The following gdb commands are supported:
 *
 * command          function                               Return value
 *
 *    g             return the value of the CPU registers  hex data or ENN
 *    G             set the value of the CPU registers     OK or ENN
 *
 *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
 *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
 *
 *    c             Resume at current address              SNN   ( signal NN)
 *    cAA..AA       Continue at address AA..AA             SNN
 *
 *    s             Step one instruction                   SNN
 *    sAA..AA       Step one instruction from AA..AA       SNN
 *
 *    k             kill
 *
 *    ?             What was the last sigval ?             SNN   (signal NN)
 *
 *    bBB..BB	    Set baud rate to BB..BB		   OK or BNN, then sets
 *							   baud rate
 *
 * All commands and responses are sent with a packet which includes a
 * checksum.  A packet consists of
 *
 * $<packet info>#<checksum>.
 *
 * where
 * <packet info> :: <characters representing the command or response>
 * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
 *
 * When a packet is received, it is first acknowledged with either '+' or '-'.
 * '+' indicates a successful transfer.  '-' indicates a failed transfer.
 *
 * Example:
 *
 * Host:                  Reply:
 * $m0,10#2a               +$00010203040506070809101112131415#42
 *
 *
 *  ==============
 *  MORE EXAMPLES:
 *  ==============
 *
 *  For reference -- the following are the steps that one
 *  company took (RidgeRun Inc) to get remote gdb debugging
 *  going. In this scenario the host machine was a PC and the
 *  target platform was a Galileo EVB64120A MIPS evaluation
 *  board.
 *
 *  Step 1:
 *  First download gdb-5.0.tar.gz from the internet.
 *  and then build/install the package.
 *
 *  Example:
 *    $ tar zxf gdb-5.0.tar.gz
 *    $ cd gdb-5.0
 *    $ ./configure --target=frv-elf-gdb
 *    $ make
 *    $ frv-elf-gdb
 *
 *  Step 2:
 *  Configure linux for remote debugging and build it.
 *
 *  Example:
 *    $ cd ~/linux
 *    $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
 *    $ make dep; make vmlinux
 *
 *  Step 3:
 *  Download the kernel to the remote target and start
 *  the kernel running. It will promptly halt and wait
 *  for the host gdb session to connect. It does this
 *  since the "Kernel Hacking" option has defined
 *  CONFIG_REMOTE_DEBUG which in turn enables your calls
 *  to:
 *     set_debug_traps();
 *     breakpoint();
 *
 *  Step 4:
 *  Start the gdb session on the host.
 *
 *  Example:
 *    $ frv-elf-gdb vmlinux
 *    (gdb) set remotebaud 115200
 *    (gdb) target remote /dev/ttyS1
 *    ...at this point you are connected to
 *       the remote target and can use gdb
 *       in the normal fasion. Setting
 *       breakpoints, single stepping,
 *       printing variables, etc.
 *
 */

#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/nmi.h>

#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/gdb-stub.h>

#define LEDS(x) do { /* *(u32*)0xe1200004 = ~(x); mb(); */ } while(0)

#undef GDBSTUB_DEBUG_PROTOCOL

extern void debug_to_serial(const char *p, int n);
extern void gdbstub_console_write(struct console *co, const char *p, unsigned n);

extern volatile uint32_t __break_error_detect[3]; /* ESFR1, ESR15, EAR15 */
extern struct user_context __break_user_context;

struct __debug_amr {
	unsigned long L, P;
} __attribute__((aligned(8)));

struct __debug_mmu {
	struct {
		unsigned long	hsr0, pcsr, esr0, ear0, epcr0;
#ifdef CONFIG_MMU
		unsigned long	tplr, tppr, tpxr, cxnr;
#endif
	} regs;

	struct __debug_amr	iamr[16];
	struct __debug_amr	damr[16];

#ifdef CONFIG_MMU
	struct __debug_amr	tlb[64*2];
#endif
};

static struct __debug_mmu __debug_mmu;

/*
 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
 * at least NUMREGBYTES*2 are needed for register packets
 */
#define BUFMAX 2048

#define BREAK_INSN	0x801000c0	/* use "break" as bkpt */

static const char gdbstub_banner[] = "Linux/FR-V GDB Stub (c) RedHat 2003\n";

volatile u8	gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
volatile u32	gdbstub_rx_inp = 0;
volatile u32	gdbstub_rx_outp = 0;
volatile u8	gdbstub_rx_overflow = 0;
u8		gdbstub_rx_unget = 0;

/* set with GDB whilst running to permit step through exceptions */
extern volatile u32 __attribute__((section(".bss"))) gdbstub_trace_through_exceptions;

static char	input_buffer[BUFMAX];
static char	output_buffer[BUFMAX];

static const char hexchars[] = "0123456789abcdef";

static const char *regnames[] = {
	"PSR ", "ISR ", "CCR ", "CCCR",
	"LR  ", "LCR ", "PC  ", "_stt",
	"sys ", "GR8*", "GNE0", "GNE1",
	"IACH", "IACL",
	"TBR ", "SP  ", "FP  ", "GR3 ",
	"GR4 ", "GR5 ", "GR6 ", "GR7 ",
	"GR8 ", "GR9 ", "GR10", "GR11",
	"GR12", "GR13", "GR14", "GR15",
	"GR16", "GR17", "GR18", "GR19",
	"GR20", "GR21", "GR22", "GR23",
	"GR24", "GR25", "GR26", "GR27",
	"EFRM", "CURR", "GR30", "BFRM"
};

struct gdbstub_bkpt {
	unsigned long	addr;		/* address of breakpoint */
	unsigned	len;		/* size of breakpoint */
	uint32_t	originsns[7];	/* original instructions */
};

static struct gdbstub_bkpt gdbstub_bkpts[256];

/*
 * local prototypes
 */

static void gdbstub_recv_packet(char *buffer);
static int gdbstub_send_packet(char *buffer);
static int gdbstub_compute_signal(unsigned long tbr);
static int hex(unsigned char ch);
static int hexToInt(char **ptr, unsigned long *intValue);
static unsigned char *mem2hex(const void *mem, char *buf, int count, int may_fault);
static char *hex2mem(const char *buf, void *_mem, int count);

/*
 * Convert ch from a hex digit to an int
 */
static int hex(unsigned 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;
}

void gdbstub_printk(const char *fmt, ...)
{
	static char buf[1024];
	va_list args;
	int len;

	/* Emit the output into the temporary buffer */
	va_start(args, fmt);
	len = vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	debug_to_serial(buf, len);
}

static inline char *gdbstub_strcpy(char *dst, const char *src)
{
	int loop = 0;
	while ((dst[loop] = src[loop]))
	       loop++;
	return dst;
}

static void gdbstub_purge_cache(void)
{
	asm volatile("	dcef	@(gr0,gr0),#1	\n"
		     "	icei	@(gr0,gr0),#1	\n"
		     "	membar			\n"
		     "	bar			\n"
		     );
}

/*****************************************************************************/
/*
 * scan for the sequence $<data>#<checksum>
 */
static void gdbstub_recv_packet(char *buffer)
{
	unsigned char checksum;
	unsigned char xmitcsum;
	unsigned char ch;
	int count, i, ret, error;

	for (;;) {
		/* wait around for the start character, ignore all other characters */
		do {
			gdbstub_rx_char(&ch, 0);
		} while (ch != '$');

		checksum = 0;
		xmitcsum = -1;
		count = 0;
		error = 0;

		/* now, read until a # or end of buffer is found */
		while (count < BUFMAX) {
			ret = gdbstub_rx_char(&ch, 0);
			if (ret < 0)
				error = ret;

			if (ch == '#')
				break;
			checksum += ch;
			buffer[count] = ch;
			count++;
		}

		if (error == -EIO) {
			gdbstub_proto("### GDB Rx Error - Skipping packet ###\n");
			gdbstub_proto("### GDB Tx NAK\n");
			gdbstub_tx_char('-');
			continue;
		}

		if (count >= BUFMAX || error)
			continue;

		buffer[count] = 0;

		/* read the checksum */
		ret = gdbstub_rx_char(&ch, 0);
		if (ret < 0)
			error = ret;
		xmitcsum = hex(ch) << 4;

		ret = gdbstub_rx_char(&ch, 0);
		if (ret < 0)
			error = ret;
		xmitcsum |= hex(ch);

		if (error) {
			if (error == -EIO)
				gdbstub_proto("### GDB Rx Error - Skipping packet\n");
			gdbstub_proto("### GDB Tx NAK\n");
			gdbstub_tx_char('-');
			continue;
		}

		/* check the checksum */
		if (checksum != xmitcsum) {
			gdbstub_proto("### GDB Tx NAK\n");
			gdbstub_tx_char('-');	/* failed checksum */
			continue;
		}

		gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
		gdbstub_proto("### GDB Tx ACK\n");
		gdbstub_tx_char('+'); /* successful transfer */

		/* if a sequence char is present, reply the sequence ID */
		if (buffer[2] == ':') {
			gdbstub_tx_char(buffer[0]);
			gdbstub_tx_char(buffer[1]);

			/* remove sequence chars from buffer */
			count = 0;
			while (buffer[count]) count++;
			for (i=3; i <= count; i++)
				buffer[i - 3] = buffer[i];
		}

		break;
	}
} /* end gdbstub_recv_packet() */

/*****************************************************************************/
/*
 * send the packet in buffer.
 * - return 0 if successfully ACK'd
 * - return 1 if abandoned due to new incoming packet
 */
static int gdbstub_send_packet(char *buffer)
{
	unsigned char checksum;
	int count;
	unsigned char ch;

	/* $<packet info>#<checksum> */
	gdbstub_proto("### GDB Tx '%s' ###\n", buffer);

	do {
		gdbstub_tx_char('$');
		checksum = 0;
		count = 0;

		while ((ch = buffer[count]) != 0) {
			gdbstub_tx_char(ch);
			checksum += ch;
			count += 1;
		}

		gdbstub_tx_char('#');
		gdbstub_tx_char(hexchars[checksum >> 4]);
		gdbstub_tx_char(hexchars[checksum & 0xf]);

	} while (gdbstub_rx_char(&ch,0),
#ifdef GDBSTUB_DEBUG_PROTOCOL
		 ch=='-' && (gdbstub_proto("### GDB Rx NAK\n"),0),
		 ch!='-' && ch!='+' && (gdbstub_proto("### GDB Rx ??? %02x\n",ch),0),
#endif
		 ch!='+' && ch!='$');

	if (ch=='+') {
		gdbstub_proto("### GDB Rx ACK\n");
		return 0;
	}

	gdbstub_proto("### GDB Tx Abandoned\n");
	gdbstub_rx_unget = ch;
	return 1;
} /* end gdbstub_send_packet() */

/*
 * While we find nice hex chars, build an int.
 * Return number of chars processed.
 */
static int hexToInt(char **ptr, unsigned long *_value)
{
	int count = 0, ch;

	*_value = 0;
	while (**ptr) {
		ch = hex(**ptr);
		if (ch < 0)
			break;

		*_value = (*_value << 4) | ((uint8_t) ch & 0xf);
		count++;

		(*ptr)++;
	}

	return count;
}

/*****************************************************************************/
/*
 * probe an address to see whether it maps to anything
 */
static inline int gdbstub_addr_probe(const void *vaddr)
{
#ifdef CONFIG_MMU
	unsigned long paddr;

	asm("lrad %1,%0,#1,#0,#0" : "=r"(paddr) : "r"(vaddr));
	if (!(paddr & xAMPRx_V))
		return 0;
#endif

	return 1;
} /* end gdbstub_addr_probe() */

#ifdef CONFIG_MMU
static unsigned long __saved_dampr, __saved_damlr;

static inline unsigned long gdbstub_virt_to_pte(unsigned long vaddr)
{
	pgd_t *pgd;
	pud_t *pud;
	pmd_t *pmd;
	pte_t *pte;
	unsigned long val, dampr5;

	pgd = (pgd_t *) __get_DAMLR(3) + pgd_index(vaddr);
	pud = pud_offset(pgd, vaddr);
	pmd = pmd_offset(pud, vaddr);

	if (pmd_bad(*pmd) || !pmd_present(*pmd))
		return 0;

	/* make sure dampr5 maps to the correct pmd */
	dampr5 = __get_DAMPR(5);
	val = pmd_val(*pmd);
	__set_DAMPR(5, val | xAMPRx_L | xAMPRx_SS_16Kb | xAMPRx_S | xAMPRx_C | xAMPRx_V);

	/* now its safe to access pmd */
	pte = (pte_t *)__get_DAMLR(5) + __pte_index(vaddr);
	if (pte_present(*pte))
		val = pte_val(*pte);
	else
		val = 0;

	/* restore original dampr5 */
	__set_DAMPR(5, dampr5);

	return val;
}
#endif

static inline int gdbstub_addr_map(const void *vaddr)
{
#ifdef CONFIG_MMU
	unsigned long pte;

	__saved_dampr = __get_DAMPR(2);
	__saved_damlr = __get_DAMLR(2);
#endif
	if (gdbstub_addr_probe(vaddr))
		return 1;
#ifdef CONFIG_MMU
	pte = gdbstub_virt_to_pte((unsigned long) vaddr);
	if (pte) {
		__set_DAMPR(2, pte);
		__set_DAMLR(2, (unsigned long) vaddr & PAGE_MASK);
		return 1;
	}
#endif
	return 0;
}

static inline void gdbstub_addr_unmap(void)
{
#ifdef CONFIG_MMU
	__set_DAMPR(2, __saved_dampr);
	__set_DAMLR(2, __saved_damlr);
#endif
}

/*
 * access potentially dodgy memory through a potentially dodgy pointer
 */
static inline int gdbstub_read_dword(const void *addr, uint32_t *_res)
{
	unsigned long brr;
	uint32_t res;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	ld%I2	%M2,%0	\n"
		     "	movsg	brr,%1	\n"
		     : "=r"(res), "=r"(brr)
		     : "m"(*(uint32_t *) addr));
	*_res = res;
	gdbstub_addr_unmap();
	return likely(!brr);
}

static inline int gdbstub_write_dword(void *addr, uint32_t val)
{
	unsigned long brr;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	st%I2	%1,%M2	\n"
		     "	movsg	brr,%0	\n"
		     : "=r"(brr)
		     : "r"(val), "m"(*(uint32_t *) addr));
	gdbstub_addr_unmap();
	return likely(!brr);
}

static inline int gdbstub_read_word(const void *addr, uint16_t *_res)
{
	unsigned long brr;
	uint16_t res;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	lduh%I2	%M2,%0	\n"
		     "	movsg	brr,%1	\n"
		     : "=r"(res), "=r"(brr)
		     : "m"(*(uint16_t *) addr));
	*_res = res;
	gdbstub_addr_unmap();
	return likely(!brr);
}

static inline int gdbstub_write_word(void *addr, uint16_t val)
{
	unsigned long brr;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	sth%I2	%1,%M2	\n"
		     "	movsg	brr,%0	\n"
		     : "=r"(brr)
		     : "r"(val), "m"(*(uint16_t *) addr));
	gdbstub_addr_unmap();
	return likely(!brr);
}

static inline int gdbstub_read_byte(const void *addr, uint8_t *_res)
{
	unsigned long brr;
	uint8_t res;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	ldub%I2	%M2,%0	\n"
		     "	movsg	brr,%1	\n"
		     : "=r"(res), "=r"(brr)
		     : "m"(*(uint8_t *) addr));
	*_res = res;
	gdbstub_addr_unmap();
	return likely(!brr);
}

static inline int gdbstub_write_byte(void *addr, uint8_t val)
{
	unsigned long brr;

	if (!gdbstub_addr_map(addr))
		return 0;

	asm volatile("	movgs	gr0,brr	\n"
		     "	stb%I2	%1,%M2	\n"
		     "	movsg	brr,%0	\n"
		     : "=r"(brr)
		     : "r"(val), "m"(*(uint8_t *) addr));
	gdbstub_addr_unmap();
	return likely(!brr);
}

static void __gdbstub_console_write(struct console *co, const char *p, unsigned n)
{
	char outbuf[26];
	int qty;

	outbuf[0] = 'O';

	while (n > 0) {
		qty = 1;

		while (n > 0 && qty < 20) {
			mem2hex(p, outbuf + qty, 2, 0);
			qty += 2;
			if (*p == 0x0a) {
				outbuf[qty++] = '0';
				outbuf[qty++] = 'd';
			}
			p++;
			n--;
		}

		outbuf[qty] = 0;
		gdbstub_send_packet(outbuf);
	}
}

#if 0
void debug_to_serial(const char *p, int n)
{
	gdbstub_console_write(NULL,p,n);
}
#endif

#ifdef CONFIG_GDBSTUB_CONSOLE

static kdev_t gdbstub_console_dev(struct console *con)
{
	return MKDEV(1,3); /* /dev/null */
}

static struct console gdbstub_console = {
	.name	= "gdb",
	.write	= gdbstub_console_write,	/* in break.S */
	.device	= gdbstub_console_dev,
	.flags	= CON_PRINTBUFFER,
	.index	= -1,
};

#endif

/*****************************************************************************/
/*
 * Convert the memory pointed to by mem into hex, placing result in buf.
 * - if successful, return a pointer to the last char put in buf (NUL)
 * - in case of mem fault, return NULL
 * may_fault is non-zero if we are reading from arbitrary memory, but is currently
 * not used.
 */
static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
{
	const uint8_t *mem = _mem;
	uint8_t ch[4] __attribute__((aligned(4)));

	if ((uint32_t)mem&1 && count>=1) {
		if (!gdbstub_read_byte(mem,ch))
			return NULL;
		*buf++ = hexchars[ch[0] >> 4];
		*buf++ = hexchars[ch[0] & 0xf];
		mem++;
		count--;
	}

	if ((uint32_t)mem&3 && count>=2) {
		if (!gdbstub_read_word(mem,(uint16_t *)ch))
			return NULL;
		*buf++ = hexchars[ch[0] >> 4];
		*buf++ = hexchars[ch[0] & 0xf];
		*buf++ = hexchars[ch[1] >> 4];
		*buf++ = hexchars[ch[1] & 0xf];
		mem += 2;
		count -= 2;
	}

	while (count>=4) {
		if (!gdbstub_read_dword(mem,(uint32_t *)ch))
			return NULL;
		*buf++ = hexchars[ch[0] >> 4];
		*buf++ = hexchars[ch[0] & 0xf];
		*buf++ = hexchars[ch[1] >> 4];
		*buf++ = hexchars[ch[1] & 0xf];
		*buf++ = hexchars[ch[2] >> 4];
		*buf++ = hexchars[ch[2] & 0xf];
		*buf++ = hexchars[ch[3] >> 4];
		*buf++ = hexchars[ch[3] & 0xf];
		mem += 4;
		count -= 4;
	}

	if (count>=2) {
		if (!gdbstub_read_word(mem,(uint16_t *)ch))
			return NULL;
		*buf++ = hexchars[ch[0] >> 4];
		*buf++ = hexchars[ch[0] & 0xf];
		*buf++ = hexchars[ch[1] >> 4];
		*buf++ = hexchars[ch[1] & 0xf];
		mem += 2;
		count -= 2;
	}

	if (count>=1) {
		if (!gdbstub_read_byte(mem,ch))
			return NULL;
		*buf++ = hexchars[ch[0] >> 4];
		*buf++ = hexchars[ch[0] & 0xf];
	}

	*buf = 0;

	return buf;
} /* end mem2hex() */

/*****************************************************************************/
/*
 * convert the hex array pointed to by buf into binary to be placed in mem
 * return a pointer to the character AFTER the last byte of buffer consumed
 */
static char *hex2mem(const char *buf, void *_mem, int count)
{
	uint8_t *mem = _mem;
	union {
		uint32_t l;
		uint16_t w;
		uint8_t  b[4];
	} ch;

	if ((u32)mem&1 && count>=1) {
		ch.b[0]  = hex(*buf++) << 4;
		ch.b[0] |= hex(*buf++);
		if (!gdbstub_write_byte(mem,ch.b[0]))
			return NULL;
		mem++;
		count--;
	}

	if ((u32)mem&3 && count>=2) {
		ch.b[0]  = hex(*buf++) << 4;
		ch.b[0] |= hex(*buf++);
		ch.b[1]  = hex(*buf++) << 4;
		ch.b[1] |= hex(*buf++);
		if (!gdbstub_write_word(mem,ch.w))
			return NULL;
		mem += 2;
		count -= 2;
	}

	while (count>=4) {
		ch.b[0]  = hex(*buf++) << 4;
		ch.b[0] |= hex(*buf++);
		ch.b[1]  = hex(*buf++) << 4;
		ch.b[1] |= hex(*buf++);
		ch.b[2]  = hex(*buf++) << 4;
		ch.b[2] |= hex(*buf++);
		ch.b[3]  = hex(*buf++) << 4;
		ch.b[3] |= hex(*buf++);
		if (!gdbstub_write_dword(mem,ch.l))
			return NULL;
		mem += 4;
		count -= 4;
	}

	if (count>=2) {
		ch.b[0]  = hex(*buf++) << 4;
		ch.b[0] |= hex(*buf++);
		ch.b[1]  = hex(*buf++) << 4;
		ch.b[1] |= hex(*buf++);
		if (!gdbstub_write_word(mem,ch.w))
			return NULL;
		mem += 2;
		count -= 2;
	}

	if (count>=1) {
		ch.b[0]  = hex(*buf++) << 4;
		ch.b[0] |= hex(*buf++);
		if (!gdbstub_write_byte(mem,ch.b[0]))
			return NULL;
	}

	return (char *) buf;
} /* end hex2mem() */

/*****************************************************************************/
/*
 * This table contains the mapping between FRV TBR.TT exception codes,
 * and signals, which are primarily what GDB understands.  It also
 * indicates which hardware traps we need to commandeer when
 * initializing the stub.
 */
static const struct brr_to_sig_map {
	unsigned long	brr_mask;	/* BRR bitmask */
	unsigned long	tbr_tt;		/* TBR.TT code (in BRR.EBTT) */
	unsigned int	signo;		/* Signal that we map this into */
} brr_to_sig_map[] = {
	{ BRR_EB,	TBR_TT_INSTR_ACC_ERROR,	SIGSEGV		},
	{ BRR_EB,	TBR_TT_ILLEGAL_INSTR,	SIGILL		},
	{ BRR_EB,	TBR_TT_PRIV_INSTR,	SIGILL		},
	{ BRR_EB,	TBR_TT_MP_EXCEPTION,	SIGFPE		},
	{ BRR_EB,	TBR_TT_DATA_ACC_ERROR,	SIGSEGV		},
	{ BRR_EB,	TBR_TT_DATA_STR_ERROR,	SIGSEGV		},
	{ BRR_EB,	TBR_TT_DIVISION_EXCEP,	SIGFPE		},
	{ BRR_EB,	TBR_TT_COMPOUND_EXCEP,	SIGSEGV		},
	{ BRR_EB,	TBR_TT_INTERRUPT_13,	SIGALRM		},	/* watchdog */
	{ BRR_EB,	TBR_TT_INTERRUPT_14,	SIGINT		},	/* GDB serial */
	{ BRR_EB,	TBR_TT_INTERRUPT_15,	SIGQUIT		},	/* NMI */
	{ BRR_CB,	0,			SIGUSR1		},
	{ BRR_TB,	0,			SIGUSR2		},
	{ BRR_DBNEx,	0,			SIGTRAP		},
	{ BRR_DBx,	0,			SIGTRAP		},	/* h/w watchpoint */
	{ BRR_IBx,	0,			SIGTRAP		},	/* h/w breakpoint */
	{ BRR_CBB,	0,			SIGTRAP		},
	{ BRR_SB,	0,			SIGTRAP		},
	{ BRR_ST,	0,			SIGTRAP		},	/* single step */
	{ 0,		0,			SIGHUP		}	/* default */
};

/*****************************************************************************/
/*
 * convert the FRV BRR register contents into a UNIX signal number
 */
static inline int gdbstub_compute_signal(unsigned long brr)
{
	const struct brr_to_sig_map *map;
	unsigned long tbr = (brr & BRR_EBTT) >> 12;

	for (map = brr_to_sig_map; map->brr_mask; map++)
		if (map->brr_mask & brr)
			if (!map->tbr_tt || map->tbr_tt == tbr)
				break;

	return map->signo;
} /* end gdbstub_compute_signal() */

/*****************************************************************************/
/*
 * set a software breakpoint or a hardware breakpoint or watchpoint
 */
static int gdbstub_set_breakpoint(unsigned long type, unsigned long addr, unsigned long len)
{
	unsigned long tmp;
	int bkpt, loop, xloop;

	union {
		struct {
			unsigned long mask0, mask1;
		};
		uint8_t bytes[8];
	} dbmr;

	//gdbstub_printk("setbkpt(%ld,%08lx,%ld)\n", type, addr, len);

	switch (type) {
		/* set software breakpoint */
	case 0:
		if (addr & 3 || len > 7*4)
			return -EINVAL;

		for (bkpt = 255; bkpt >= 0; bkpt--)
			if (!gdbstub_bkpts[bkpt].addr)
				break;
		if (bkpt < 0)
			return -ENOSPC;

		for (loop = 0; loop < len/4; loop++)
			if (!gdbstub_read_dword(&((uint32_t *) addr)[loop],
						&gdbstub_bkpts[bkpt].originsns[loop]))
				return -EFAULT;

		for (loop = 0; loop < len/4; loop++)
			if (!gdbstub_write_dword(&((uint32_t *) addr)[loop],
						 BREAK_INSN)
			    ) {
				/* need to undo the changes if possible */
				for (xloop = 0; xloop < loop; xloop++)
					gdbstub_write_dword(&((uint32_t *) addr)[xloop],
							    gdbstub_bkpts[bkpt].originsns[xloop]);
				return -EFAULT;
			}

		gdbstub_bkpts[bkpt].addr = addr;
		gdbstub_bkpts[bkpt].len = len;

#if 0
		gdbstub_printk("Set BKPT[%02x]: %08lx #%d {%04x, %04x} -> { %04x, %04x }\n",
			       bkpt,
			       gdbstub_bkpts[bkpt].addr,
			       gdbstub_bkpts[bkpt].len,
			       gdbstub_bkpts[bkpt].originsns[0],
			       gdbstub_bkpts[bkpt].originsns[1],
			       ((uint32_t *) addr)[0],
			       ((uint32_t *) addr)[1]
			       );
#endif
		return 0;

		/* set hardware breakpoint */
	case 1:
		if (addr & 3 || len != 4)
			return -EINVAL;

		if (!(__debug_regs->dcr & DCR_IBE0)) {
			//gdbstub_printk("set h/w break 0: %08lx\n", addr);
			__debug_regs->dcr |= DCR_IBE0;
			asm volatile("movgs %0,ibar0" : : "r"(addr));
			return 0;
		}

		if (!(__debug_regs->dcr & DCR_IBE1)) {
			//gdbstub_printk("set h/w break 1: %08lx\n", addr);
			__debug_regs->dcr |= DCR_IBE1;
			asm volatile("movgs %0,ibar1" : : "r"(addr));
			return 0;
		}

		if (!(__debug_regs->dcr & DCR_IBE2)) {
			//gdbstub_printk("set h/w break 2: %08lx\n", addr);
			__debug_regs->dcr |= DCR_IBE2;
			asm volatile("movgs %0,ibar2" : : "r"(addr));
			return 0;
		}

		if (!(__debug_regs->dcr & DCR_IBE3)) {
			//gdbstub_printk("set h/w break 3: %08lx\n", addr);
			__debug_regs->dcr |= DCR_IBE3;
			asm volatile("movgs %0,ibar3" : : "r"(addr));
			return 0;
		}

		return -ENOSPC;

		/* set data read/write/access watchpoint */
	case 2:
	case 3:
	case 4:
		if ((addr & ~7) != ((addr + len - 1) & ~7))
			return -EINVAL;

		tmp = addr & 7;

		memset(dbmr.bytes, 0xff, sizeof(dbmr.bytes));
		for (loop = 0; loop < len; loop++)
			dbmr.bytes[tmp + loop] = 0;

		addr &= ~7;

		if (!(__debug_regs->dcr & (DCR_DRBE0|DCR_DWBE0))) {
			//gdbstub_printk("set h/w watchpoint 0 type %ld: %08lx\n", type, addr);
			tmp = type==2 ? DCR_DWBE0 : type==3 ? DCR_DRBE0 : DCR_DRBE0|DCR_DWBE0;
			__debug_regs->dcr |= tmp;
			asm volatile("	movgs	%0,dbar0	\n"
				     "	movgs	%1,dbmr00	\n"
				     "	movgs	%2,dbmr01	\n"
				     "	movgs	gr0,dbdr00	\n"
				     "	movgs	gr0,dbdr01	\n"
				     : : "r"(addr), "r"(dbmr.mask0), "r"(dbmr.mask1));
			return 0;
		}

		if (!(__debug_regs->dcr & (DCR_DRBE1|DCR_DWBE1))) {
			//gdbstub_printk("set h/w watchpoint 1 type %ld: %08lx\n", type, addr);
			tmp = type==2 ? DCR_DWBE1 : type==3 ? DCR_DRBE1 : DCR_DRBE1|DCR_DWBE1;
			__debug_regs->dcr |= tmp;
			asm volatile("	movgs	%0,dbar1	\n"
				     "	movgs	%1,dbmr10	\n"
				     "	movgs	%2,dbmr11	\n"
				     "	movgs	gr0,dbdr10	\n"
				     "	movgs	gr0,dbdr11	\n"
				     : : "r"(addr), "r"(dbmr.mask0), "r"(dbmr.mask1));
			return 0;
		}

		return -ENOSPC;

	default:
		return -EINVAL;
	}

} /* end gdbstub_set_breakpoint() */

/*****************************************************************************/
/*
 * clear a breakpoint or watchpoint
 */
int gdbstub_clear_breakpoint(unsigned long type, unsigned long addr, unsigned long len)
{
	unsigned long tmp;
	int bkpt, loop;

	union {
		struct {
			unsigned long mask0, mask1;
		};
		uint8_t bytes[8];
	} dbmr;

	//gdbstub_printk("clearbkpt(%ld,%08lx,%ld)\n", type, addr, len);

	switch (type) {
		/* clear software breakpoint */
	case 0:
		for (bkpt = 255; bkpt >= 0; bkpt--)
			if (gdbstub_bkpts[bkpt].addr == addr && gdbstub_bkpts[bkpt].len == len)
				break;
		if (bkpt < 0)
			return -ENOENT;

		gdbstub_bkpts[bkpt].addr = 0;

		for (loop = 0; loop < len/4; loop++)
			if (!gdbstub_write_dword(&((uint32_t *) addr)[loop],
						 gdbstub_bkpts[bkpt].originsns[loop]))
				return -EFAULT;
		return 0;

		/* clear hardware breakpoint */
	case 1:
		if (addr & 3 || len != 4)
			return -EINVAL;

#define __get_ibar(X) ({ unsigned long x; asm volatile("movsg ibar"#X",%0" : "=r"(x)); x; })

		if (__debug_regs->dcr & DCR_IBE0 && __get_ibar(0) == addr) {
			//gdbstub_printk("clear h/w break 0: %08lx\n", addr);
			__debug_regs->dcr &= ~DCR_IBE0;
			asm volatile("movgs gr0,ibar0");
			return 0;
		}

		if (__debug_regs->dcr & DCR_IBE1 && __get_ibar(1) == addr) {
			//gdbstub_printk("clear h/w break 1: %08lx\n", addr);
			__debug_regs->dcr &= ~DCR_IBE1;
			asm volatile("movgs gr0,ibar1");
			return 0;
		}

		if (__debug_regs->dcr & DCR_IBE2 && __get_ibar(2) == addr) {
			//gdbstub_printk("clear h/w break 2: %08lx\n", addr);
			__debug_regs->dcr &= ~DCR_IBE2;
			asm volatile("movgs gr0,ibar2");
			return 0;
		}

		if (__debug_regs->dcr & DCR_IBE3 && __get_ibar(3) == addr) {
			//gdbstub_printk("clear h/w break 3: %08lx\n", addr);
			__debug_regs->dcr &= ~DCR_IBE3;
			asm volatile("movgs gr0,ibar3");
			return 0;
		}

		return -EINVAL;

		/* clear data read/write/access watchpoint */
	case 2:
	case 3:
	case 4:
		if ((addr & ~7) != ((addr + len - 1) & ~7))
			return -EINVAL;

		tmp = addr & 7;

		memset(dbmr.bytes, 0xff, sizeof(dbmr.bytes));
		for (loop = 0; loop < len; loop++)
			dbmr.bytes[tmp + loop] = 0;

		addr &= ~7;

#define __get_dbar(X) ({ unsigned long x; asm volatile("movsg dbar"#X",%0" : "=r"(x)); x; })
#define __get_dbmr0(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"0,%0" : "=r"(x)); x; })
#define __get_dbmr1(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"1,%0" : "=r"(x)); x; })

		/* consider DBAR 0 */
		tmp = type==2 ? DCR_DWBE0 : type==3 ? DCR_DRBE0 : DCR_DRBE0|DCR_DWBE0;

		if ((__debug_regs->dcr & (DCR_DRBE0|DCR_DWBE0)) != tmp ||
		    __get_dbar(0) != addr ||
		    __get_dbmr0(0) != dbmr.mask0 ||
		    __get_dbmr1(0) != dbmr.mask1)
			goto skip_dbar0;

		//gdbstub_printk("clear h/w watchpoint 0 type %ld: %08lx\n", type, addr);
		__debug_regs->dcr &= ~(DCR_DRBE0|DCR_DWBE0);
		asm volatile("	movgs	gr0,dbar0	\n"
			     "	movgs	gr0,dbmr00	\n"
			     "	movgs	gr0,dbmr01	\n"
			     "	movgs	gr0,dbdr00	\n"
			     "	movgs	gr0,dbdr01	\n");
		return 0;

	skip_dbar0:
		/* consider DBAR 0 */
		tmp = type==2 ? DCR_DWBE1 : type==3 ? DCR_DRBE1 : DCR_DRBE1|DCR_DWBE1;

		if ((__debug_regs->dcr & (DCR_DRBE1|DCR_DWBE1)) != tmp ||
		    __get_dbar(1) != addr ||
		    __get_dbmr0(1) != dbmr.mask0 ||
		    __get_dbmr1(1) != dbmr.mask1)
			goto skip_dbar1;

		//gdbstub_printk("clear h/w watchpoint 1 type %ld: %08lx\n", type, addr);
		__debug_regs->dcr &= ~(DCR_DRBE1|DCR_DWBE1);
		asm volatile("	movgs	gr0,dbar1	\n"
			     "	movgs	gr0,dbmr10	\n"
			     "	movgs	gr0,dbmr11	\n"
			     "	movgs	gr0,dbdr10	\n"
			     "	movgs	gr0,dbdr11	\n");
		return 0;

	skip_dbar1:
		return -ENOSPC;

	default:
		return -EINVAL;
	}
} /* end gdbstub_clear_breakpoint() */

/*****************************************************************************/
/*
 * check a for an internal software breakpoint, and wind the PC back if necessary
 */
static void gdbstub_check_breakpoint(void)
{
	unsigned long addr = __debug_frame->pc - 4;
	int bkpt;

	for (bkpt = 255; bkpt >= 0; bkpt--)
		if (gdbstub_bkpts[bkpt].addr == addr)
			break;
	if (bkpt >= 0)
		__debug_frame->pc = addr;

	//gdbstub_printk("alter pc [%d] %08lx\n", bkpt, __debug_frame->pc);

} /* end gdbstub_check_breakpoint() */

/*****************************************************************************/
/*
 *
 */
static void __attribute__((unused)) gdbstub_show_regs(void)
{
	uint32_t *reg;
	int loop;

	gdbstub_printk("\n");

	gdbstub_printk("Frame: @%p [%s]\n",
		       __debug_frame,
		       __debug_frame->psr & PSR_S ? "kernel" : "user");

	reg = (uint32_t *) __debug_frame;
	for (loop = 0; loop < REG__END; loop++) {
		printk("%s %08x", regnames[loop + 0], reg[loop + 0]);

		if (loop == REG__END - 1 || loop % 5 == 4)
			printk("\n");
		else
			printk(" | ");
	}

	gdbstub_printk("Process %s (pid: %d)\n", current->comm, current->pid);
} /* end gdbstub_show_regs() */

/*****************************************************************************/
/*
 * dump debugging regs
 */
static void __attribute__((unused)) gdbstub_dump_debugregs(void)
{
	unsigned long x;

	x = __debug_regs->dcr;
	gdbstub_printk("DCR    %08lx  ", x);

	x = __debug_regs->brr;
	gdbstub_printk("BRR %08lx\n", x);

	gdbstub_printk("IBAR0  %08lx  ", __get_ibar(0));
	gdbstub_printk("IBAR1  %08lx  ", __get_ibar(1));
	gdbstub_printk("IBAR2  %08lx  ", __get_ibar(2));
	gdbstub_printk("IBAR3  %08lx\n", __get_ibar(3));

	gdbstub_printk("DBAR0  %08lx  ", __get_dbar(0));
	gdbstub_printk("DBMR00 %08lx  ", __get_dbmr0(0));
	gdbstub_printk("DBMR01 %08lx\n", __get_dbmr1(0));

	gdbstub_printk("DBAR1  %08lx  ", __get_dbar(1));
	gdbstub_printk("DBMR10 %08lx  ", __get_dbmr0(1));
	gdbstub_printk("DBMR11 %08lx\n", __get_dbmr1(1));

	gdbstub_printk("\n");
} /* end gdbstub_dump_debugregs() */

/*****************************************************************************/
/*
 * dump the MMU state into a structure so that it can be accessed with GDB
 */
void gdbstub_get_mmu_state(void)
{
	asm volatile("movsg hsr0,%0" : "=r"(__debug_mmu.regs.hsr0));
	asm volatile("movsg pcsr,%0" : "=r"(__debug_mmu.regs.pcsr));
	asm volatile("movsg esr0,%0" : "=r"(__debug_mmu.regs.esr0));
	asm volatile("movsg ear0,%0" : "=r"(__debug_mmu.regs.ear0));
	asm volatile("movsg epcr0,%0" : "=r"(__debug_mmu.regs.epcr0));

	/* read the protection / SAT registers */
	__debug_mmu.iamr[0].L  = __get_IAMLR(0);
	__debug_mmu.iamr[0].P  = __get_IAMPR(0);
	__debug_mmu.iamr[1].L  = __get_IAMLR(1);
	__debug_mmu.iamr[1].P  = __get_IAMPR(1);
	__debug_mmu.iamr[2].L  = __get_IAMLR(2);
	__debug_mmu.iamr[2].P  = __get_IAMPR(2);
	__debug_mmu.iamr[3].L  = __get_IAMLR(3);
	__debug_mmu.iamr[3].P  = __get_IAMPR(3);
	__debug_mmu.iamr[4].L  = __get_IAMLR(4);
	__debug_mmu.iamr[4].P  = __get_IAMPR(4);
	__debug_mmu.iamr[5].L  = __get_IAMLR(5);
	__debug_mmu.iamr[5].P  = __get_IAMPR(5);
	__debug_mmu.iamr[6].L  = __get_IAMLR(6);
	__debug_mmu.iamr[6].P  = __get_IAMPR(6);
	__debug_mmu.iamr[7].L  = __get_IAMLR(7);
	__debug_mmu.iamr[7].P  = __get_IAMPR(7);
	__debug_mmu.iamr[8].L  = __get_IAMLR(8);
	__debug_mmu.iamr[8].P  = __get_IAMPR(8);
	__debug_mmu.iamr[9].L  = __get_IAMLR(9);
	__debug_mmu.iamr[9].P  = __get_IAMPR(9);
	__debug_mmu.iamr[10].L = __get_IAMLR(10);
	__debug_mmu.iamr[10].P = __get_IAMPR(10);
	__debug_mmu.iamr[11].L = __get_IAMLR(11);
	__debug_mmu.iamr[11].P = __get_IAMPR(11);
	__debug_mmu.iamr[12].L = __get_IAMLR(12);
	__debug_mmu.iamr[12].P = __get_IAMPR(12);
	__debug_mmu.iamr[13].L = __get_IAMLR(13);
	__debug_mmu.iamr[13].P = __get_IAMPR(13);
	__debug_mmu.iamr[14].L = __get_IAMLR(14);
	__debug_mmu.iamr[14].P = __get_IAMPR(14);
	__debug_mmu.iamr[15].L = __get_IAMLR(15);
	__debug_mmu.iamr[15].P = __get_IAMPR(15);

	__debug_mmu.damr[0].L  = __get_DAMLR(0);
	__debug_mmu.damr[0].P  = __get_DAMPR(0);
	__debug_mmu.damr[1].L  = __get_DAMLR(1);
	__debug_mmu.damr[1].P  = __get_DAMPR(1);
	__debug_mmu.damr[2].L  = __get_DAMLR(2);
	__debug_mmu.damr[2].P  = __get_DAMPR(2);
	__debug_mmu.damr[3].L  = __get_DAMLR(3);
	__debug_mmu.damr[3].P  = __get_DAMPR(3);
	__debug_mmu.damr[4].L  = __get_DAMLR(4);
	__debug_mmu.damr[4].P  = __get_DAMPR(4);
	__debug_mmu.damr[5].L  = __get_DAMLR(5);
	__debug_mmu.damr[5].P  = __get_DAMPR(5);
	__debug_mmu.damr[6].L  = __get_DAMLR(6);
	__debug_mmu.damr[6].P  = __get_DAMPR(6);
	__debug_mmu.damr[7].L  = __get_DAMLR(7);
	__debug_mmu.damr[7].P  = __get_DAMPR(7);
	__debug_mmu.damr[8].L  = __get_DAMLR(8);
	__debug_mmu.damr[8].P  = __get_DAMPR(8);
	__debug_mmu.damr[9].L  = __get_DAMLR(9);
	__debug_mmu.damr[9].P  = __get_DAMPR(9);
	__debug_mmu.damr[10].L = __get_DAMLR(10);
	__debug_mmu.damr[10].P = __get_DAMPR(10);
	__debug_mmu.damr[11].L = __get_DAMLR(11);
	__debug_mmu.damr[11].P = __get_DAMPR(11);
	__debug_mmu.damr[12].L = __get_DAMLR(12);
	__debug_mmu.damr[12].P = __get_DAMPR(12);
	__debug_mmu.damr[13].L = __get_DAMLR(13);
	__debug_mmu.damr[13].P = __get_DAMPR(13);
	__debug_mmu.damr[14].L = __get_DAMLR(14);
	__debug_mmu.damr[14].P = __get_DAMPR(14);
	__debug_mmu.damr[15].L = __get_DAMLR(15);
	__debug_mmu.damr[15].P = __get_DAMPR(15);

#ifdef CONFIG_MMU
	do {
		/* read the DAT entries from the TLB */
		struct __debug_amr *p;
		int loop;

		asm volatile("movsg tplr,%0" : "=r"(__debug_mmu.regs.tplr));
		asm volatile("movsg tppr,%0" : "=r"(__debug_mmu.regs.tppr));
		asm volatile("movsg tpxr,%0" : "=r"(__debug_mmu.regs.tpxr));
		asm volatile("movsg cxnr,%0" : "=r"(__debug_mmu.regs.cxnr));

		p = __debug_mmu.tlb;

		/* way 0 */
		asm volatile("movgs %0,tpxr" :: "r"(0 << TPXR_WAY_SHIFT));
		for (loop = 0; loop < 64; loop++) {
			asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop << PAGE_SHIFT));
			asm volatile("movsg tplr,%0" : "=r"(p->L));
			asm volatile("movsg tppr,%0" : "=r"(p->P));
			p++;
		}

		/* way 1 */
		asm volatile("movgs %0,tpxr" :: "r"(1 << TPXR_WAY_SHIFT));
		for (loop = 0; loop < 64; loop++) {
			asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop << PAGE_SHIFT));
			asm volatile("movsg tplr,%0" : "=r"(p->L));
			asm volatile("movsg tppr,%0" : "=r"(p->P));
			p++;
		}

		asm volatile("movgs %0,tplr" :: "r"(__debug_mmu.regs.tplr));
		asm volatile("movgs %0,tppr" :: "r"(__debug_mmu.regs.tppr));
		asm volatile("movgs %0,tpxr" :: "r"(__debug_mmu.regs.tpxr));
	} while(0);
#endif

} /* end gdbstub_get_mmu_state() */

/*****************************************************************************/
/*
 * handle event interception and GDB remote protocol processing
 * - on entry:
 *	PSR.ET==0, PSR.S==1 and the CPU is in debug mode
 *	__debug_frame points to the saved registers
 *	__frame points to the kernel mode exception frame, if it was in kernel
 *      mode when the break happened
 */
void gdbstub(int sigval)
{
	unsigned long addr, length, loop, dbar, temp, temp2, temp3;
	uint32_t zero;
	char *ptr;
	int flush_cache = 0;

	LEDS(0x5000);

	if (sigval < 0) {
#ifndef CONFIG_GDBSTUB_IMMEDIATE
		/* return immediately if GDB immediate activation option not set */
		return;
#else
		sigval = SIGINT;
#endif
	}

	save_user_regs(&__break_user_context);

#if 0
	gdbstub_printk("--> gdbstub() %08x %p %08x %08x\n",
		       __debug_frame->pc,
		       __debug_frame,
		       __debug_regs->brr,
		       __debug_regs->bpsr);
//	gdbstub_show_regs();
#endif

	LEDS(0x5001);

	/* if we were interrupted by input on the serial gdbstub serial port,
	 * restore the context prior to the interrupt so that we return to that
	 * directly
	 */
	temp = (unsigned long) __entry_kerneltrap_table;
	temp2 = (unsigned long) __entry_usertrap_table;
	temp3 = __debug_frame->pc & ~15;

	if (temp3 == temp + TBR_TT_INTERRUPT_15 ||
	    temp3 == temp2 + TBR_TT_INTERRUPT_15
	    ) {
		asm volatile("movsg pcsr,%0" : "=r"(__debug_frame->pc));
		__debug_frame->psr |= PSR_ET;
		__debug_frame->psr &= ~PSR_S;
		if (__debug_frame->psr & PSR_PS)
			__debug_frame->psr |= PSR_S;
		__debug_regs->brr = (__debug_frame->tbr & TBR_TT) << 12;
		__debug_regs->brr |= BRR_EB;
		sigval = SIGINT;
	}

	/* handle the decrement timer going off (FR451 only) */
	if (temp3 == temp + TBR_TT_DECREMENT_TIMER ||
	    temp3 == temp2 + TBR_TT_DECREMENT_TIMER
	    ) {
		asm volatile("movgs %0,timerd" :: "r"(10000000));
		asm volatile("movsg pcsr,%0" : "=r"(__debug_frame->pc));
		__debug_frame->psr |= PSR_ET;
		__debug_frame->psr &= ~PSR_S;
		if (__debug_frame->psr & PSR_PS)
			__debug_frame->psr |= PSR_S;
		__debug_regs->brr = (__debug_frame->tbr & TBR_TT) << 12;
		__debug_regs->brr |= BRR_EB;
		sigval = SIGXCPU;
	}

	LEDS(0x5002);

	/* after a BREAK insn, the PC lands on the far side of it */
	if (__debug_regs->brr & BRR_SB)
		gdbstub_check_breakpoint();

	LEDS(0x5003);

	/* handle attempts to write console data via GDB "O" commands */
	if (__debug_frame->pc == (unsigned long) gdbstub_console_write + 4) {
		__gdbstub_console_write((struct console *) __debug_frame->gr8,
					(const char *) __debug_frame->gr9,
					(unsigned) __debug_frame->gr10);
		goto done;
	}

	if (gdbstub_rx_unget) {
		sigval = SIGINT;
		goto packet_waiting;
	}

	if (!sigval)
		sigval = gdbstub_compute_signal(__debug_regs->brr);

	LEDS(0x5004);

	/* send a message to the debugger's user saying what happened if it may
	 * not be clear cut (we can't map exceptions onto signals properly)
	 */
	if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
		static const char title[] = "Break ";
		static const char crlf[] = "\r\n";
		unsigned long brr = __debug_regs->brr;
		char hx;

		ptr = output_buffer;
		*ptr++ = 'O';
		ptr = mem2hex(title, ptr, sizeof(title) - 1,0);

		hx = hexchars[(brr & 0xf0000000) >> 28];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x0f000000) >> 24];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x00f00000) >> 20];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x000f0000) >> 16];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x0000f000) >> 12];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x00000f00) >> 8];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x000000f0) >> 4];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];
		hx = hexchars[(brr & 0x0000000f)];
		*ptr++ = hexchars[hx >> 4];	*ptr++ = hexchars[hx & 0xf];

		ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
		*ptr = 0;
		gdbstub_send_packet(output_buffer);	/* send it off... */
	}

	LEDS(0x5005);

	/* tell the debugger that an exception has occurred */
	ptr = output_buffer;

	/* Send trap type (converted to signal) */
	*ptr++ = 'T';
	*ptr++ = hexchars[sigval >> 4];
	*ptr++ = hexchars[sigval & 0xf];

	/* Send Error PC */
	*ptr++ = hexchars[GDB_REG_PC >> 4];
	*ptr++ = hexchars[GDB_REG_PC & 0xf];
	*ptr++ = ':';
	ptr = mem2hex(&__debug_frame->pc, ptr, 4, 0);
	*ptr++ = ';';

	/*
	 * Send frame pointer
	 */
	*ptr++ = hexchars[GDB_REG_FP >> 4];
	*ptr++ = hexchars[GDB_REG_FP & 0xf];
	*ptr++ = ':';
	ptr = mem2hex(&__debug_frame->fp, ptr, 4, 0);
	*ptr++ = ';';

	/*
	 * Send stack pointer
	 */
	*ptr++ = hexchars[GDB_REG_SP >> 4];
	*ptr++ = hexchars[GDB_REG_SP & 0xf];
	*ptr++ = ':';
	ptr = mem2hex(&__debug_frame->sp, ptr, 4, 0);
	*ptr++ = ';';

	*ptr++ = 0;
	gdbstub_send_packet(output_buffer);	/* send it off... */

	LEDS(0x5006);

 packet_waiting:
	gdbstub_get_mmu_state();

	/* wait for input from remote GDB */
	while (1) {
		output_buffer[0] = 0;

		LEDS(0x5007);
		gdbstub_recv_packet(input_buffer);
		LEDS(0x5600 | input_buffer[0]);

		switch (input_buffer[0]) {
			/* request repeat of last signal number */
		case '?':
			output_buffer[0] = 'S';
			output_buffer[1] = hexchars[sigval >> 4];
			output_buffer[2] = hexchars[sigval & 0xf];
			output_buffer[3] = 0;
			break;

		case 'd':
			/* toggle debug flag */
			break;

			/* return the value of the CPU registers
			 * - GR0,  GR1,  GR2,  GR3,  GR4,  GR5,  GR6,  GR7,
			 * - GR8,  GR9,  GR10, GR11, GR12, GR13, GR14, GR15,
			 * - GR16, GR17, GR18, GR19, GR20, GR21, GR22, GR23,
			 * - GR24, GR25, GR26, GR27, GR28, GR29, GR30, GR31,
			 * - GR32, GR33, GR34, GR35, GR36, GR37, GR38, GR39,
			 * - GR40, GR41, GR42, GR43, GR44, GR45, GR46, GR47,
			 * - GR48, GR49, GR50, GR51, GR52, GR53, GR54, GR55,
			 * - GR56, GR57, GR58, GR59, GR60, GR61, GR62, GR63,
			 * - FP0,  FP1,  FP2,  FP3,  FP4,  FP5,  FP6,  FP7,
			 * - FP8,  FP9,  FP10, FP11, FP12, FP13, FP14, FP15,
			 * - FP16, FP17, FP18, FP19, FP20, FP21, FP22, FP23,
			 * - FP24, FP25, FP26, FP27, FP28, FP29, FP30, FP31,
			 * - FP32, FP33, FP34, FP35, FP36, FP37, FP38, FP39,
			 * - FP40, FP41, FP42, FP43, FP44, FP45, FP46, FP47,
			 * - FP48, FP49, FP50, FP51, FP52, FP53, FP54, FP55,
			 * - FP56, FP57, FP58, FP59, FP60, FP61, FP62, FP63,
			 * - PC, PSR, CCR, CCCR,
			 * - _X132, _X133, _X134
			 * - TBR, BRR, DBAR0, DBAR1, DBAR2, DBAR3,
			 * - _X141, _X142, _X143, _X144,
			 * - LR, LCR
			 */
		case 'g':
			zero = 0;
			ptr = output_buffer;

			/* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
			ptr = mem2hex(&zero, ptr, 4, 0);

			for (loop = 1; loop <= 27; loop++)
				ptr = mem2hex((unsigned long *)__debug_frame + REG_GR(loop),
					      ptr, 4, 0);
			temp = (unsigned long) __frame;
			ptr = mem2hex(&temp, ptr, 4, 0);
			ptr = mem2hex((unsigned long *)__debug_frame + REG_GR(29), ptr, 4, 0);
			ptr = mem2hex((unsigned long *)__debug_frame + REG_GR(30), ptr, 4, 0);
#ifdef CONFIG_MMU
			ptr = mem2hex((unsigned long *)__debug_frame + REG_GR(31), ptr, 4, 0);
#else
			temp = (unsigned long) __debug_frame;
			ptr = mem2hex(&temp, ptr, 4, 0);
#endif

			for (loop = 32; loop <= 63; loop++)
				ptr = mem2hex((unsigned long *)__debug_frame + REG_GR(loop),
					      ptr, 4, 0);

			/* deal with FR0-FR63 */
			for (loop = 0; loop <= 63; loop++)
				ptr = mem2hex((unsigned long *)&__break_user_context +
					      __FPMEDIA_FR(loop),
					      ptr, 4, 0);

			/* deal with special registers */
			ptr = mem2hex(&__debug_frame->pc,    ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->psr,   ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->ccr,   ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->cccr,  ptr, 4, 0);
			ptr = mem2hex(&zero, ptr, 4, 0);
			ptr = mem2hex(&zero, ptr, 4, 0);
			ptr = mem2hex(&zero, ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->tbr,   ptr, 4, 0);
			ptr = mem2hex(&__debug_regs->brr ,   ptr, 4, 0);

			asm volatile("movsg dbar0,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg dbar1,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg dbar2,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg dbar3,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);

			asm volatile("movsg scr0,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg scr1,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg scr2,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);
			asm volatile("movsg scr3,%0" : "=r"(dbar));
			ptr = mem2hex(&dbar, ptr, 4, 0);

			ptr = mem2hex(&__debug_frame->lr, ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->lcr, ptr, 4, 0);

			ptr = mem2hex(&__debug_frame->iacc0, ptr, 8, 0);

			ptr = mem2hex(&__break_user_context.f.fsr[0], ptr, 4, 0);

			for (loop = 0; loop <= 7; loop++)
				ptr = mem2hex(&__break_user_context.f.acc[loop], ptr, 4, 0);

			ptr = mem2hex(&__break_user_context.f.accg, ptr, 8, 0);

			for (loop = 0; loop <= 1; loop++)
				ptr = mem2hex(&__break_user_context.f.msr[loop], ptr, 4, 0);

			ptr = mem2hex(&__debug_frame->gner0, ptr, 4, 0);
			ptr = mem2hex(&__debug_frame->gner1, ptr, 4, 0);

			ptr = mem2hex(&__break_user_context.f.fner[0], ptr, 4, 0);
			ptr = mem2hex(&__break_user_context.f.fner[1], ptr, 4, 0);

			break;

			/* set the values of the CPU registers */
		case 'G':
			ptr = &input_buffer[1];

			/* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
			ptr = hex2mem(ptr, &temp, 4);

			for (loop = 1; loop <= 27; loop++)
				ptr = hex2mem(ptr, (unsigned long *)__debug_frame + REG_GR(loop),
					      4);

			ptr = hex2mem(ptr, &temp, 4);
			__frame = (struct pt_regs *) temp;
			ptr = hex2mem(ptr, &__debug_frame->gr29, 4);
			ptr = hex2mem(ptr, &__debug_frame->gr30, 4);
#ifdef CONFIG_MMU
			ptr = hex2mem(ptr, &__debug_frame->gr31, 4);
#else
			ptr = hex2mem(ptr, &temp, 4);
#endif

			for (loop = 32; loop <= 63; loop++)
				ptr = hex2mem(ptr, (unsigned long *)__debug_frame + REG_GR(loop),
					      4);

			/* deal with FR0-FR63 */
			for (loop = 0; loop <= 63; loop++)
				ptr = mem2hex((unsigned long *)&__break_user_context +
					      __FPMEDIA_FR(loop),
					      ptr, 4, 0);

			/* deal with special registers */
			ptr = hex2mem(ptr, &__debug_frame->pc,  4);
			ptr = hex2mem(ptr, &__debug_frame->psr, 4);
			ptr = hex2mem(ptr, &__debug_frame->ccr, 4);
			ptr = hex2mem(ptr, &__debug_frame->cccr,4);

			for (loop = 132; loop <= 140; loop++)
				ptr = hex2mem(ptr, &temp, 4);

			ptr = hex2mem(ptr, &temp, 4);
			asm volatile("movgs %0,scr0" :: "r"(temp));
			ptr = hex2mem(ptr, &temp, 4);
			asm volatile("movgs %0,scr1" :: "r"(temp));
			ptr = hex2mem(ptr, &temp, 4);
			asm volatile("movgs %0,scr2" :: "r"(temp));
			ptr = hex2mem(ptr, &temp, 4);
			asm volatile("movgs %0,scr3" :: "r"(temp));

			ptr = hex2mem(ptr, &__debug_frame->lr,  4);
			ptr = hex2mem(ptr, &__debug_frame->lcr, 4);

			ptr = hex2mem(ptr, &__debug_frame->iacc0, 8);

			ptr = hex2mem(ptr, &__break_user_context.f.fsr[0], 4);

			for (loop = 0; loop <= 7; loop++)
				ptr = hex2mem(ptr, &__break_user_context.f.acc[loop], 4);

			ptr = hex2mem(ptr, &__break_user_context.f.accg, 8);

			for (loop = 0; loop <= 1; loop++)
				ptr = hex2mem(ptr, &__break_user_context.f.msr[loop], 4);

			ptr = hex2mem(ptr, &__debug_frame->gner0, 4);
			ptr = hex2mem(ptr, &__debug_frame->gner1, 4);

			ptr = hex2mem(ptr, &__break_user_context.f.fner[0], 4);
			ptr = hex2mem(ptr, &__break_user_context.f.fner[1], 4);

			gdbstub_strcpy(output_buffer,"OK");
			break;

			/* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
		case 'm':
			ptr = &input_buffer[1];

			if (hexToInt(&ptr, &addr) &&
			    *ptr++ == ',' &&
			    hexToInt(&ptr, &length)
			    ) {
				if (mem2hex((char *)addr, output_buffer, length, 1))
					break;
				gdbstub_strcpy (output_buffer, "E03");
			}
			else {
				gdbstub_strcpy(output_buffer,"E01");
			}
			break;

			/* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
		case 'M':
			ptr = &input_buffer[1];

			if (hexToInt(&ptr, &addr) &&
			    *ptr++ == ',' &&
			    hexToInt(&ptr, &length) &&
			    *ptr++ == ':'
			    ) {
				if (hex2mem(ptr, (char *)addr, length)) {
					gdbstub_strcpy(output_buffer, "OK");
				}
				else {
					gdbstub_strcpy(output_buffer, "E03");
				}
			}
			else
				gdbstub_strcpy(output_buffer, "E02");

			flush_cache = 1;
			break;

			/* PNN,=RRRRRRRR: Write value R to reg N return OK */
		case 'P':
			ptr = &input_buffer[1];

			if (!hexToInt(&ptr, &addr) ||
			    *ptr++ != '=' ||
			    !hexToInt(&ptr, &temp)
			    ) {
				gdbstub_strcpy(output_buffer, "E01");
				break;
			}

			temp2 = 1;
			switch (addr) {
			case GDB_REG_GR(0):
				break;
			case GDB_REG_GR(1) ... GDB_REG_GR(63):
				__break_user_context.i.gr[addr - GDB_REG_GR(0)] = temp;
				break;
			case GDB_REG_FR(0) ... GDB_REG_FR(63):
				__break_user_context.f.fr[addr - GDB_REG_FR(0)] = temp;
				break;
			case GDB_REG_PC:
				__break_user_context.i.pc = temp;
				break;
			case GDB_REG_PSR:
				__break_user_context.i.psr = temp;
				break;
			case GDB_REG_CCR:
				__break_user_context.i.ccr = temp;
				break;
			case GDB_REG_CCCR:
				__break_user_context.i.cccr = temp;
				break;
			case GDB_REG_BRR:
				__debug_regs->brr = temp;
				break;
			case GDB_REG_LR:
				__break_user_context.i.lr = temp;
				break;
			case GDB_REG_LCR:
				__break_user_context.i.lcr = temp;
				break;
			case GDB_REG_FSR0:
				__break_user_context.f.fsr[0] = temp;
				break;
			case GDB_REG_ACC(0) ... GDB_REG_ACC(7):
				__break_user_context.f.acc[addr - GDB_REG_ACC(0)] = temp;
				break;
			case GDB_REG_ACCG(0):
				*(uint32_t *) &__break_user_context.f.accg[0] = temp;
				break;
			case GDB_REG_ACCG(4):
				*(uint32_t *) &__break_user_context.f.accg[4] = temp;
				break;
			case GDB_REG_MSR(0) ... GDB_REG_MSR(1):
				__break_user_context.f.msr[addr - GDB_REG_MSR(0)] = temp;
				break;
			case GDB_REG_GNER(0) ... GDB_REG_GNER(1):
				__break_user_context.i.gner[addr - GDB_REG_GNER(0)] = temp;
				break;
			case GDB_REG_FNER(0) ... GDB_REG_FNER(1):
				__break_user_context.f.fner[addr - GDB_REG_FNER(0)] = temp;
				break;
			default:
				temp2 = 0;
				break;
			}

			if (temp2) {
				gdbstub_strcpy(output_buffer, "OK");
			}
			else {
				gdbstub_strcpy(output_buffer, "E02");
			}
			break;

			/* cAA..AA    Continue at address AA..AA(optional) */
		case 'c':
			/* try to read optional parameter, pc unchanged if no parm */
			ptr = &input_buffer[1];
			if (hexToInt(&ptr, &addr))
				__debug_frame->pc = addr;
			goto done;

			/* kill the program */
		case 'k' :
			goto done;	/* just continue */


			/* reset the whole machine (FIXME: system dependent) */
		case 'r':
			break;


			/* step to next instruction */
		case 's':
			__debug_regs->dcr |= DCR_SE;
			goto done;

			/* set baud rate (bBB) */
		case 'b':
			ptr = &input_buffer[1];
			if (!hexToInt(&ptr, &temp)) {
				gdbstub_strcpy(output_buffer,"B01");
				break;
			}

			if (temp) {
				/* ack before changing speed */
				gdbstub_send_packet("OK");
				gdbstub_set_baud(temp);
			}
			break;

			/* set breakpoint */
		case 'Z':
			ptr = &input_buffer[1];

			if (!hexToInt(&ptr,&temp) || *ptr++ != ',' ||
			    !hexToInt(&ptr,&addr) || *ptr++ != ',' ||
			    !hexToInt(&ptr,&length)
			    ) {
				gdbstub_strcpy(output_buffer,"E01");
				break;
			}

			if (temp >= 5) {
				gdbstub_strcpy(output_buffer,"E03");
				break;
			}

			if (gdbstub_set_breakpoint(temp, addr, length) < 0) {
				gdbstub_strcpy(output_buffer,"E03");
				break;
			}

			if (temp == 0)
				flush_cache = 1; /* soft bkpt by modified memory */

			gdbstub_strcpy(output_buffer,"OK");
			break;

			/* clear breakpoint */
		case 'z':
			ptr = &input_buffer[1];

			if (!hexToInt(&ptr,&temp) || *ptr++ != ',' ||
			    !hexToInt(&ptr,&addr) || *ptr++ != ',' ||
			    !hexToInt(&ptr,&length)
			    ) {
				gdbstub_strcpy(output_buffer,"E01");
				break;
			}

			if (temp >= 5) {
				gdbstub_strcpy(output_buffer,"E03");
				break;
			}

			if (gdbstub_clear_breakpoint(temp, addr, length) < 0) {
				gdbstub_strcpy(output_buffer,"E03");
				break;
			}

			if (temp == 0)
				flush_cache = 1; /* soft bkpt by modified memory */

			gdbstub_strcpy(output_buffer,"OK");
			break;

		default:
			gdbstub_proto("### GDB Unsupported Cmd '%s'\n",input_buffer);
			break;
		}

		/* reply to the request */
		LEDS(0x5009);
		gdbstub_send_packet(output_buffer);
	}

 done:
	restore_user_regs(&__break_user_context);

	//gdbstub_dump_debugregs();
	//gdbstub_printk("<-- gdbstub() %08x\n", __debug_frame->pc);

	/* need to flush the instruction cache before resuming, as we may have
	 * deposited a breakpoint, and the icache probably has no way of
	 * knowing that a data ref to some location may have changed something
	 * that is in the instruction cache.  NB: We flush both caches, just to
	 * be sure...
	 */

	/* note: flushing the icache will clobber EAR0 on the FR451 */
	if (flush_cache)
		gdbstub_purge_cache();

	LEDS(0x5666);

} /* end gdbstub() */

/*****************************************************************************/
/*
 * initialise the GDB stub
 */
void __init gdbstub_init(void)
{
#ifdef CONFIG_GDBSTUB_IMMEDIATE
	unsigned char ch;
	int ret;
#endif

	gdbstub_printk("%s", gdbstub_banner);
	gdbstub_printk("DCR: %x\n", __debug_regs->dcr);

	gdbstub_io_init();

	/* try to talk to GDB (or anyone insane enough to want to type GDB protocol by hand) */
	gdbstub_proto("### GDB Tx ACK\n");
	gdbstub_tx_char('+'); /* 'hello world' */

#ifdef CONFIG_GDBSTUB_IMMEDIATE
	gdbstub_printk("GDB Stub waiting for packet\n");

	/*
	 * In case GDB is started before us, ack any packets
	 * (presumably "$?#xx") sitting there.
	 */
	do { gdbstub_rx_char(&ch, 0); } while (ch != '$');
	do { gdbstub_rx_char(&ch, 0); } while (ch != '#');
	do { ret = gdbstub_rx_char(&ch, 0); } while (ret != 0); /* eat first csum byte */
	do { ret = gdbstub_rx_char(&ch, 0); } while (ret != 0); /* eat second csum byte */

	gdbstub_proto("### GDB Tx NAK\n");
	gdbstub_tx_char('-'); /* nak it */

#else
	gdbstub_printk("GDB Stub set\n");
#endif

#if 0
	/* send banner */
	ptr = output_buffer;
	*ptr++ = 'O';
	ptr = mem2hex(gdbstub_banner, ptr, sizeof(gdbstub_banner) - 1, 0);
	gdbstub_send_packet(output_buffer);
#endif
#if defined(CONFIG_GDBSTUB_CONSOLE) && defined(CONFIG_GDBSTUB_IMMEDIATE)
	register_console(&gdbstub_console);
#endif

} /* end gdbstub_init() */

/*****************************************************************************/
/*
 * register the console at a more appropriate time
 */
#if defined (CONFIG_GDBSTUB_CONSOLE) && !defined(CONFIG_GDBSTUB_IMMEDIATE)
static int __init gdbstub_postinit(void)
{
	printk("registering console\n");
	register_console(&gdbstub_console);
	return 0;
} /* end gdbstub_postinit() */

__initcall(gdbstub_postinit);
#endif

/*****************************************************************************/
/*
 * send an exit message to GDB
 */
void gdbstub_exit(int status)
{
	unsigned char checksum;
	int count;
	unsigned char ch;

	sprintf(output_buffer,"W%02x",status&0xff);

	gdbstub_tx_char('$');
	checksum = 0;
	count = 0;

	while ((ch = output_buffer[count]) != 0) {
		gdbstub_tx_char(ch);
		checksum += ch;
		count += 1;
	}

	gdbstub_tx_char('#');
	gdbstub_tx_char(hexchars[checksum >> 4]);
	gdbstub_tx_char(hexchars[checksum & 0xf]);

	/* make sure the output is flushed, or else RedBoot might clobber it */
	gdbstub_tx_char('-');
	gdbstub_tx_flush();

} /* end gdbstub_exit() */

/*****************************************************************************/
/*
 * GDB wants to call malloc() and free() to allocate memory for calling kernel
 * functions directly from its command line
 */
static void *malloc(size_t size) __attribute__((unused));
static void *malloc(size_t size)
{
	return kmalloc(size, GFP_ATOMIC);
}

static void free(void *p) __attribute__((unused));
static void free(void *p)
{
	kfree(p);
}

static uint32_t ___get_HSR0(void) __attribute__((unused));
static uint32_t ___get_HSR0(void)
{
	return __get_HSR(0);
}

static uint32_t ___set_HSR0(uint32_t x) __attribute__((unused));
static uint32_t ___set_HSR0(uint32_t x)
{
	__set_HSR(0, x);
	return __get_HSR(0);
}