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
|
/* $NetBSD: trap.c,v 1.50 1996/10/13 21:37:49 jonathan Exp $ */
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department and Ralph Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Utah Hdr: trap.c 1.32 91/04/06
*
* @(#)trap.c 8.5 (Berkeley) 1/11/94
*/
#if !defined(MIPS1) && !defined(MIPS3)
#error Neither "MIPS1" (r2000 family), "MIP3" (r4000 family) was configured.
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/signalvar.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <sys/buf.h>
#ifdef KTRACE
#include <sys/ktrace.h>
#endif
#include <net/netisr.h>
#include <pmax/locore.h>
#include <machine/trap.h>
#include <machine/psl.h>
#include <machine/reg.h>
#include <machine/cpu.h>
#include <machine/locore.h>
#include <machine/pte.h>
#include <machine/mips_opcode.h>
#include <vm/vm.h>
#include <vm/vm_kern.h>
#include <vm/vm_page.h>
#include <sys/cdefs.h>
#include <sys/syslog.h>
#include <miscfs/procfs/procfs.h>
/* all this to get prototypes for ipintr() and arpintr() */
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ip_var.h>
#include "ppp.h"
struct proc *machFPCurProcPtr; /* pointer to last proc to use FP */
/*
* Port-specific hardware interrupt handler
*/
int (*mips_hardware_intr) __P((u_int mask, u_int pc, u_int status,
u_int cause)) =
( int (*) __P((u_int, u_int, u_int, u_int)) ) 0;
/*
* Exception-handling functions, called via machExceptionTable from locore
*/
extern void MachTLBModException __P((void));
extern void MachTLBMissException __P((void));
extern void mips1_KernGenException __P((void));
extern void mips1_UserGenException __P((void));
extern void mips1_KernIntr __P((void));
extern void mips1_UserIntr __P((void));
extern void mips1_TLBModException __P((void));
extern void mips1_TLBMissException __P((void));
/* marks end of vector code */
extern void mips1_UTLBMiss __P((void));
extern void mips1_exceptionentry_end __P((void));
extern void mips3_KernGenException __P((void));
extern void mips3_UserGenException __P((void));
extern void mips3_KernIntr __P((void));
extern void mips3_UserIntr __P((void));
extern void mips3_TLBModException __P((void));
extern void mips3_TLBMissException __P((void));
/* marks end of vector code */
extern void mips3_TLBMiss __P((void));
extern void mips3_exceptionentry_end __P((void));
void (*mips1_ExceptionTable[]) __P((void)) = {
/*
* The kernel exception handlers.
*/
mips1_KernIntr, /* 0 external interrupt */
mips1_KernGenException, /* 1 TLB modification */
mips1_TLBMissException, /* 2 TLB miss (load or instr. fetch) */
mips1_TLBMissException, /* 3 TLB miss (store) */
mips1_KernGenException, /* 4 address error (load or I-fetch) */
mips1_KernGenException, /* 5 address error (store) */
mips1_KernGenException, /* 6 bus error (I-fetch) */
mips1_KernGenException, /* 7 bus error (load or store) */
mips1_KernGenException, /* 8 system call */
mips1_KernGenException, /* 9 breakpoint */
mips1_KernGenException, /* 10 reserved instruction */
mips1_KernGenException, /* 11 coprocessor unusable */
mips1_KernGenException, /* 12 arithmetic overflow */
mips1_KernGenException, /* 13 r4k trap excpt, r3k reserved */
mips1_KernGenException, /* 14 r4k virt coherence, r3k reserved */
mips1_KernGenException, /* 15 r4k FP exception, r3k reserved */
mips1_KernGenException, /* 16 reserved */
mips1_KernGenException, /* 17 reserved */
mips1_KernGenException, /* 18 reserved */
mips1_KernGenException, /* 19 reserved */
mips1_KernGenException, /* 20 reserved */
mips1_KernGenException, /* 21 reserved */
mips1_KernGenException, /* 22 reserved */
mips1_KernGenException, /* 23 watch exception */
mips1_KernGenException, /* 24 reserved */
mips1_KernGenException, /* 25 reserved */
mips1_KernGenException, /* 26 reserved */
mips1_KernGenException, /* 27 reserved */
mips1_KernGenException, /* 28 reserved */
mips1_KernGenException, /* 29 reserved */
mips1_KernGenException, /* 30 reserved */
mips1_KernGenException, /* 31 virt. coherence exception data */
/*
* The user exception handlers.
*/
mips1_UserIntr, /* 0 */
mips1_UserGenException, /* 1 */
mips1_UserGenException, /* 2 */
mips1_UserGenException, /* 3 */
mips1_UserGenException, /* 4 */
mips1_UserGenException, /* 5 */
mips1_UserGenException, /* 6 */
mips1_UserGenException, /* 7 */
mips1_UserGenException, /* 8 */
mips1_UserGenException, /* 9 */
mips1_UserGenException, /* 10 */
mips1_UserGenException, /* 11 */
mips1_UserGenException, /* 12 */
mips1_UserGenException, /* 13 */
mips1_UserGenException, /* 14 */
mips1_UserGenException, /* 15 */
mips1_UserGenException, /* 16 */
mips1_UserGenException, /* 17 */
mips1_UserGenException, /* 18 */
mips1_UserGenException, /* 19 */
mips1_UserGenException, /* 20 */
mips1_UserGenException, /* 21 */
mips1_UserGenException, /* 22 */
mips1_UserGenException, /* 23 */
mips1_UserGenException, /* 24 */
mips1_UserGenException, /* 25 */
mips1_UserGenException, /* 26 */
mips1_UserGenException, /* 27 */
mips1_UserGenException, /* 28 */
mips1_UserGenException, /* 29 */
mips1_UserGenException, /* 20 */
mips1_UserGenException, /* 31 */
};
#ifdef MIPS3 /* r4000 family (mips-III cpu) */
void (*mips3_ExceptionTable[]) __P((void)) = {
/*
* The kernel exception handlers.
*/
mips3_KernIntr, /* 0 external interrupt */
mips3_KernGenException, /* 1 TLB modification */
mips3_TLBMissException, /* 2 TLB miss (load or instr. fetch) */
mips3_TLBMissException, /* 3 TLB miss (store) */
mips3_KernGenException, /* 4 address error (load or I-fetch) */
mips3_KernGenException, /* 5 address error (store) */
mips3_KernGenException, /* 6 bus error (I-fetch) */
mips3_KernGenException, /* 7 bus error (load or store) */
mips3_KernGenException, /* 8 system call */
mips3_KernGenException, /* 9 breakpoint */
mips3_KernGenException, /* 10 reserved instruction */
mips3_KernGenException, /* 11 coprocessor unusable */
mips3_KernGenException, /* 12 arithmetic overflow */
mips3_KernGenException, /* 13 r4k trap excpt, r3k reserved */
mips3_KernGenException, /* 14 r4k virt coherence, r3k reserved */
mips3_KernGenException, /* 15 r4k FP exception, r3k reserved */
mips3_KernGenException, /* 16 reserved */
mips3_KernGenException, /* 17 reserved */
mips3_KernGenException, /* 18 reserved */
mips3_KernGenException, /* 19 reserved */
mips3_KernGenException, /* 20 reserved */
mips3_KernGenException, /* 21 reserved */
mips3_KernGenException, /* 22 reserved */
mips3_KernGenException, /* 23 watch exception */
mips3_KernGenException, /* 24 reserved */
mips3_KernGenException, /* 25 reserved */
mips3_KernGenException, /* 26 reserved */
mips3_KernGenException, /* 27 reserved */
mips3_KernGenException, /* 28 reserved */
mips3_KernGenException, /* 29 reserved */
mips3_KernGenException, /* 30 reserved */
mips3_KernGenException, /* 31 virt. coherence exception data */
/*
* The user exception handlers.
*/
mips3_UserIntr, /* 0 */
mips3_UserGenException, /* 1 */
mips3_UserGenException, /* 2 */
mips3_UserGenException, /* 3 */
mips3_UserGenException, /* 4 */
mips3_UserGenException, /* 5 */
mips3_UserGenException, /* 6 */
mips3_UserGenException, /* 7 */
mips3_UserGenException, /* 8 */
mips3_UserGenException, /* 9 */
mips3_UserGenException, /* 10 */
mips3_UserGenException, /* 11 */
mips3_UserGenException, /* 12 */
mips3_UserGenException, /* 13 */
mips3_UserGenException, /* 14 */
mips3_UserGenException, /* 15 */
mips3_UserGenException, /* 16 */
mips3_UserGenException, /* 17 */
mips3_UserGenException, /* 18 */
mips3_UserGenException, /* 19 */
mips3_UserGenException, /* 20 */
mips3_UserGenException, /* 21 */
mips3_UserGenException, /* 22 */
mips3_UserGenException, /* 23 */
mips3_UserGenException, /* 24 */
mips3_UserGenException, /* 25 */
mips3_UserGenException, /* 26 */
mips3_UserGenException, /* 27 */
mips3_UserGenException, /* 28 */
mips3_UserGenException, /* 29 */
mips3_UserGenException, /* 20 */
mips3_UserGenException, /* 31 */
};
#endif /* MIPS3 */
char *trap_type[] = {
"external interrupt",
"TLB modification",
"TLB miss (load or instr. fetch)",
"TLB miss (store)",
"address error (load or I-fetch)",
"address error (store)",
"bus error (I-fetch)",
"bus error (load or store)",
"system call",
"breakpoint",
"reserved instruction",
"coprocessor unusable",
"arithmetic overflow",
"r4k trap/r3k reserved 13",
"r4k virtual coherency instruction/r3k reserved 14",
"r4k floating point/ r3k reserved 15",
"reserved 16",
"reserved 17",
"reserved 18",
"reserved 19",
"reserved 20",
"reserved 21",
"reserved 22",
"r4000 watch",
"reserved 24",
"reserved 25",
"reserved 26",
"reserved 27",
"reserved 28",
"reserved 29",
"reserved 30",
"r4000 virtual coherency data",
};
#ifdef DEBUG
#define TRAPSIZE 10
struct trapdebug { /* trap history buffer for debugging */
u_int status;
u_int cause;
u_int vadr;
u_int pc;
u_int ra;
u_int sp;
u_int code;
} trapdebug[TRAPSIZE], *trp = trapdebug;
void trapDump __P((char * msg));
void cpu_getregs __P((int *regs));
#endif /* DEBUG */
/*
* Other forward declarations.
*/
u_int MachEmulateBranch __P((unsigned *regsPtr,
unsigned instPC,
unsigned fpcCSR,
int allowNonBranch));
/* extern functions used but not declared elsewhere */
extern void MachFPInterrupt __P((u_int status, u_int cause, u_int pc));
extern void clearsoftclock __P((void));
extern void clearsoftnet __P((void));
extern int splx __P((int));
extern int splhigh __P((void));
extern void MachSwitchFPState __P((struct proc *from, struct user *to));
/* only called by locore */
extern u_int trap __P((u_int status, u_int cause, u_int vaddr, u_int pc,
int args));
#ifdef DEBUG /* stack trace code, also useful to DDB one day */
extern void stacktrace __P(()); /*XXX*/
extern void logstacktrace __P(()); /*XXX*/
/* extern functions printed by name in stack backtraces */
extern void idle __P((void)), cpu_switch __P(( struct proc *p));
extern void MachEmptyWriteBuffer __P((void));
extern void MachUTLBMiss __P((void));
extern void setsoftclock __P((void));
extern int main __P((void*));
extern void am7990_meminit __P((void*)); /* XXX */
#endif /* DEBUG */
extern volatile struct chiptime *Mach_clock_addr;
extern u_long kernelfaults;
u_long kernelfaults = 0;
extern u_long intrcnt[];
/*
* Index into intrcnt[], which is defined in locore
*/
typedef enum {
SOFTCLOCK_INTR =0,
SOFTNET_INTR =1,
SERIAL0_INTR=2,
SERIAL1_INTR = 3,
SERIAL2_INTR = 4,
LANCE_INTR =5,
SCSI_INTR = 6,
ERROR_INTR=7,
HARDCLOCK = 8,
FPU_INTR =9,
SLOT0_INTR =10,
SLOT1_INTR =11,
SLOT2_INTR =12,
DTOP_INTR = 13, /* XXX */
ISDN_INTR = 14, /* XXX */
FLOPPY_INTR = 15,
STRAY_INTR = 16
} decstation_intr_t;
/*
* Handle an exception.
* Called from MachKernGenException() or MachUserGenException()
* when a processor trap occurs.
* In the case of a kernel trap, we return the pc where to resume if
* ((struct pcb *)UADDR)->pcb_onfault is set, otherwise, return old pc.
*/
u_int
trap(statusReg, causeReg, vadr, pc, args)
unsigned statusReg; /* status register at time of the exception */
unsigned causeReg; /* cause register at time of exception */
unsigned vadr; /* address (if any) the fault occured on */
unsigned pc; /* program counter where to continue */
{
register int type, i;
unsigned ucode = 0;
register struct proc *p = curproc;
u_quad_t sticks;
vm_prot_t ftype;
extern unsigned onfault_table[];
int typ = 0;
#ifdef DEBUG
trp->status = statusReg;
trp->cause = causeReg;
trp->vadr = vadr;
trp->pc = pc;
trp->ra = !USERMODE(statusReg) ? ((int *)&args)[19] :
p->p_md.md_regs[RA];
trp->sp = (int)&args;
trp->code = 0;
if (++trp == &trapdebug[TRAPSIZE])
trp = trapdebug;
#endif
cnt.v_trap++;
type = (causeReg & MIPS_3K_CR_EXC_CODE) >> MACH_CR_EXC_CODE_SHIFT;
if (USERMODE(statusReg)) {
type |= T_USER;
sticks = p->p_sticks;
}
/*
* Enable hardware interrupts if they were on before.
* We only respond to software interrupts when returning to user mode.
*/
if (statusReg & MIPS_3K_SR_INT_ENA_PREV)
splx((statusReg & MACH_HARD_INT_MASK) | MIPS_SR_INT_IE);
switch (type) {
case T_TLB_MOD:
/* check for kernel address */
if ((int)vadr < 0) {
register pt_entry_t *pte;
register unsigned entry;
register vm_offset_t pa;
pte = kvtopte(vadr);
entry = pte->pt_entry;
#ifdef DIAGNOSTIC
if (!(entry & PG_V) || (entry & PG_M))
panic("trap: ktlbmod: invalid pte");
#endif
if (PAGE_IS_RDONLY(entry, vadr)) {
/* write to read only page in the kernel */
ftype = VM_PROT_WRITE;
goto kernel_fault;
}
entry |= PG_M;
pte->pt_entry = entry;
vadr &= ~PGOFSET;
MachTLBUpdate(vadr, entry);
pa = PTE_TO_PADDR(entry);
#ifdef ATTR
pmap_attributes[atop(pa)] |= PMAP_ATTR_MOD;
#else
if (!IS_VM_PHYSADDR(pa))
panic("trap: ktlbmod: unmanaged page");
PHYS_TO_VM_PAGE(pa)->flags &= ~PG_CLEAN;
#endif
return (pc);
}
/* FALLTHROUGH */
case T_TLB_MOD+T_USER:
{
register pt_entry_t *pte;
register unsigned entry;
register vm_offset_t pa;
pmap_t pmap = &p->p_vmspace->vm_pmap;
if (!(pte = pmap_segmap(pmap, vadr)))
panic("trap: utlbmod: invalid segmap");
pte += (vadr >> PGSHIFT) & (NPTEPG - 1);
entry = pte->pt_entry;
#ifdef DIAGNOSTIC
if (!(entry & PG_V) || (entry & PG_M)) {
panic("trap: utlbmod: invalid pte");
}
#endif
if (PAGE_IS_RDONLY(entry, vadr)) {
/* write to read only page */
ftype = VM_PROT_WRITE;
goto dofault;
}
entry |= PG_M;
pte->pt_entry = entry;
vadr = (vadr & ~PGOFSET) |
(pmap->pm_tlbpid << VMMACH_TLB_PID_SHIFT);
MachTLBUpdate(vadr, entry);
pa = PTE_TO_PADDR(entry);
#ifdef ATTR
pmap_attributes[atop(pa)] |= PMAP_ATTR_MOD;
#else
if (!IS_VM_PHYSADDR(pa)) {
panic("trap: utlbmod: unmanaged page");
}
PHYS_TO_VM_PAGE(pa)->flags &= ~PG_CLEAN;
#endif
if (!USERMODE(statusReg))
return (pc);
goto out;
}
case T_TLB_LD_MISS:
case T_TLB_ST_MISS:
ftype = (type == T_TLB_ST_MISS) ? VM_PROT_WRITE : VM_PROT_READ;
/* check for kernel address */
if ((int)vadr < 0) {
register vm_offset_t va;
int rv;
kernel_fault:
kernelfaults++;
va = trunc_page((vm_offset_t)vadr);
rv = vm_fault(kernel_map, va, ftype, FALSE);
if (rv == KERN_SUCCESS)
return (pc);
if ((i = ((struct pcb *)UADDR)->pcb_onfault) != 0) {
((struct pcb *)UADDR)->pcb_onfault = 0;
return (onfault_table[i]);
}
goto err;
}
/*
* It is an error for the kernel to access user space except
* through the copyin/copyout routines.
*/
if ((i = ((struct pcb *)UADDR)->pcb_onfault) == 0)
goto err;
/* check for fuswintr() or suswintr() getting a page fault */
if (i == 4)
return (onfault_table[i]);
goto dofault;
case T_TLB_LD_MISS+T_USER:
ftype = VM_PROT_READ;
goto dofault;
case T_TLB_ST_MISS+T_USER:
ftype = VM_PROT_WRITE;
dofault:
{
register vm_offset_t va;
register struct vmspace *vm;
register vm_map_t map;
int rv;
vm = p->p_vmspace;
map = &vm->vm_map;
va = trunc_page((vm_offset_t)vadr);
rv = vm_fault(map, va, ftype, FALSE);
#ifdef VMFAULT_TRACE
printf("vm_fault(%x (pmap %x), %x (%x), %x, %d) -> %x at pc %x\n",
map, &vm->vm_pmap, va, vadr, ftype, FALSE, rv, pc);
#endif
/*
* If this was a stack access we keep track of the maximum
* accessed stack size. Also, if vm_fault gets a protection
* failure it is due to accessing the stack region outside
* the current limit and we need to reflect that as an access
* error.
*/
if ((caddr_t)va >= vm->vm_maxsaddr) {
if (rv == KERN_SUCCESS) {
unsigned nss;
nss = clrnd(btoc(USRSTACK-(unsigned)va));
if (nss > vm->vm_ssize)
vm->vm_ssize = nss;
} else if (rv == KERN_PROTECTION_FAILURE)
rv = KERN_INVALID_ADDRESS;
}
if (rv == KERN_SUCCESS) {
if (!USERMODE(statusReg))
return (pc);
goto out;
}
if (!USERMODE(statusReg)) {
if ((i = ((struct pcb *)UADDR)->pcb_onfault) != 0) {
((struct pcb *)UADDR)->pcb_onfault = 0;
return (onfault_table[i]);
}
goto err;
}
ucode = ftype;
i = SIGSEGV;
typ = SEGV_MAPERR;
break;
}
case T_ADDR_ERR_LD+T_USER: /* misaligned or kseg access */
case T_ADDR_ERR_ST+T_USER: /* misaligned or kseg access */
ucode = 0; /* XXX should be VM_PROT_something */
i = SIGBUS;
typ = BUS_ADRALN;
break;
case T_BUS_ERR_IFETCH+T_USER: /* BERR asserted to cpu */
case T_BUS_ERR_LD_ST+T_USER: /* BERR asserted to cpu */
ucode = 0; /* XXX should be VM_PROT_something */
i = SIGBUS;
typ = BUS_OBJERR;
break;
case T_SYSCALL+T_USER:
{
register int *locr0 = p->p_md.md_regs;
register struct sysent *callp;
unsigned int code;
int numsys;
struct args {
int i[8];
} args;
int rval[2];
cnt.v_syscall++;
/* compute next PC after syscall instruction */
if ((int)causeReg < 0)
locr0[PC] = MachEmulateBranch(locr0, pc, 0, 0);
else
locr0[PC] += 4;
callp = p->p_emul->e_sysent;
numsys = p->p_emul->e_nsysent;
code = locr0[V0];
switch (code) {
case SYS_syscall:
/*
* Code is first argument, followed by actual args.
*/
code = locr0[A0];
if (code >= numsys)
callp += p->p_emul->e_nosys; /* (illegal) */
else
callp += code;
i = callp->sy_argsize / sizeof(int);
args.i[0] = locr0[A1];
args.i[1] = locr0[A2];
args.i[2] = locr0[A3];
if (i > 3) {
i = copyin((caddr_t)(locr0[SP] +
4 * sizeof(int)),
(caddr_t)&args.i[3],
(u_int)(i - 3) * sizeof(int));
if (i) {
locr0[V0] = i;
locr0[A3] = 1;
#ifdef SYSCALL_DEBUG
scdebug_call(p, code, args.i);
#endif
#ifdef KTRACE
if (KTRPOINT(p, KTR_SYSCALL))
ktrsyscall(p->p_tracep, code,
callp->sy_argsize,
args.i);
#endif
goto done;
}
}
break;
case SYS___syscall:
/*
* Like syscall, but code is a quad, so as to maintain
* quad alignment for the rest of the arguments.
*/
code = locr0[A0 + _QUAD_LOWWORD];
if (code >= numsys)
callp += p->p_emul->e_nosys; /* (illegal) */
else
callp += code;
i = callp->sy_argsize / sizeof(int);
args.i[0] = locr0[A2];
args.i[1] = locr0[A3];
if (i > 2) {
i = copyin((caddr_t)(locr0[SP] +
4 * sizeof(int)),
(caddr_t)&args.i[2],
(u_int)(i - 2) * sizeof(int));
if (i) {
locr0[V0] = i;
locr0[A3] = 1;
#ifdef SYSCALL_DEBUG
scdebug_call(p, code, args.i);
#endif
#ifdef KTRACE
if (KTRPOINT(p, KTR_SYSCALL))
ktrsyscall(p->p_tracep, code,
callp->sy_argsize,
args.i);
#endif
goto done;
}
}
break;
default:
if (code >= numsys)
callp += p->p_emul->e_nosys; /* (illegal) */
else
callp += code;
i = callp->sy_narg;
args.i[0] = locr0[A0];
args.i[1] = locr0[A1];
args.i[2] = locr0[A2];
args.i[3] = locr0[A3];
if (i > 4) {
i = copyin((caddr_t)(locr0[SP] +
4 * sizeof(int)),
(caddr_t)&args.i[4],
(u_int)(i - 4) * sizeof(int));
if (i) {
locr0[V0] = i;
locr0[A3] = 1;
#ifdef SYSCALL_DEBUG
scdebug_call(p, code, args.i);
#endif
#ifdef KTRACE
if (KTRPOINT(p, KTR_SYSCALL))
ktrsyscall(p->p_tracep, code,
callp->sy_argsize,
args.i);
#endif
goto done;
}
}
}
#ifdef SYSCALL_DEBUG
scdebug_call(p, code, args.i);
#endif
#ifdef KTRACE
if (KTRPOINT(p, KTR_SYSCALL))
ktrsyscall(p->p_tracep, code, callp->sy_argsize, args.i);
#endif
rval[0] = 0;
rval[1] = locr0[V1];
#ifdef DEBUG
if (trp == trapdebug)
trapdebug[TRAPSIZE - 1].code = code;
else
trp[-1].code = code;
#endif
i = (*callp->sy_call)(p, &args, rval);
/*
* Reinitialize proc pointer `p' as it may be different
* if this is a child returning from fork syscall.
*/
p = curproc;
locr0 = p->p_md.md_regs;
#ifdef DEBUG
{ int s;
s = splhigh();
trp->status = statusReg;
trp->cause = causeReg;
trp->vadr = locr0[SP];
trp->pc = locr0[PC];
trp->ra = locr0[RA];
/*trp->sp = (int)&args;*/
trp->code = -code;
if (++trp == &trapdebug[TRAPSIZE])
trp = trapdebug;
splx(s);
}
#endif
switch (i) {
case 0:
locr0[V0] = rval[0];
locr0[V1] = rval[1];
locr0[A3] = 0;
break;
case ERESTART:
locr0[PC] = pc;
break;
case EJUSTRETURN:
break; /* nothing to do */
default:
locr0[V0] = i;
locr0[A3] = 1;
}
/*
* If we modified code or data, flush caches.
* XXX code unyderling ptrace() and/or proc fs should do this?
*/
if (code == SYS_ptrace)
MachFlushCache();
done:
#ifdef SYSCALL_DEBUG
scdebug_ret(p, code, i, rval);
#endif
#ifdef KTRACE
if (KTRPOINT(p, KTR_SYSRET))
ktrsysret(p->p_tracep, code, i, rval[0]); /*XXX*/
#endif
goto out;
}
case T_BREAK+T_USER:
{
register unsigned va, instr;
/* compute address of break instruction */
va = pc;
if ((int)causeReg < 0)
va += 4;
/* read break instruction */
instr = fuiword((caddr_t)va);
#if 0
printf("trap: %s (%d) breakpoint %x at %x: (adr %x ins %x)\n",
p->p_comm, p->p_pid, instr, pc,
p->p_md.md_ss_addr, p->p_md.md_ss_instr); /* XXX */
#endif
#ifdef KADB
if (instr == MACH_BREAK_BRKPT || instr == MACH_BREAK_SSTEP)
goto err;
#endif
if (p->p_md.md_ss_addr != va || instr != MACH_BREAK_SSTEP) {
i = SIGTRAP;
typ = TRAP_TRACE;
break;
}
/* restore original instruction and clear BP */
i = suiword((caddr_t)va, p->p_md.md_ss_instr);
if (i < 0) {
vm_offset_t sa, ea;
int rv;
sa = trunc_page((vm_offset_t)va);
ea = round_page((vm_offset_t)va+sizeof(int)-1);
rv = vm_map_protect(&p->p_vmspace->vm_map, sa, ea,
VM_PROT_DEFAULT, FALSE);
if (rv == KERN_SUCCESS) {
i = suiword((caddr_t)va, p->p_md.md_ss_instr);
(void) vm_map_protect(&p->p_vmspace->vm_map,
sa, ea, VM_PROT_READ|VM_PROT_EXECUTE,
FALSE);
}
}
if (i < 0)
printf("Warning: can't restore instruction at %x: %x\n",
p->p_md.md_ss_addr, p->p_md.md_ss_instr);
p->p_md.md_ss_addr = 0;
i = SIGTRAP;
typ = TRAP_BRKPT;
break;
}
case T_RES_INST+T_USER:
i = SIGILL;
typ = ILL_ILLOPC;
break;
case T_COP_UNUSABLE+T_USER:
if ((causeReg & MACH_CR_COP_ERR) != 0x10000000) {
i = SIGILL; /* only FPU instructions allowed */
typ = ILL_ILLOPC;
break;
}
MachSwitchFPState(machFPCurProcPtr,
(struct user*)p->p_md.md_regs);
machFPCurProcPtr = p;
p->p_md.md_regs[PS] |= MACH_SR_COP_1_BIT;
p->p_md.md_flags |= MDP_FPUSED;
goto out;
case T_FPE:
#ifdef DEBUG
trapDump("fpintr");
#else
printf("FPU Trap: PC %x CR %x SR %x\n",
pc, causeReg, statusReg);
goto err;
#endif
case T_FPE+T_USER:
MachFPTrap(statusReg, causeReg, pc);
goto out;
case T_OVFLOW+T_USER:
i = SIGFPE;
typ = FPE_FLTOVF;
break;
case T_ADDR_ERR_LD: /* misaligned access */
case T_ADDR_ERR_ST: /* misaligned access */
case T_BUS_ERR_LD_ST: /* BERR asserted to cpu */
if ((i = ((struct pcb *)UADDR)->pcb_onfault) != 0) {
((struct pcb *)UADDR)->pcb_onfault = 0;
return (onfault_table[i]);
}
/* FALLTHROUGH */
default:
err:
#ifdef KADB
{
extern struct pcb kdbpcb;
if (USERMODE(statusReg))
kdbpcb = p->p_addr->u_pcb;
else {
kdbpcb.pcb_regs[ZERO] = 0;
kdbpcb.pcb_regs[AST] = ((int *)&args)[2];
kdbpcb.pcb_regs[V0] = ((int *)&args)[3];
kdbpcb.pcb_regs[V1] = ((int *)&args)[4];
kdbpcb.pcb_regs[A0] = ((int *)&args)[5];
kdbpcb.pcb_regs[A1] = ((int *)&args)[6];
kdbpcb.pcb_regs[A2] = ((int *)&args)[7];
kdbpcb.pcb_regs[A3] = ((int *)&args)[8];
kdbpcb.pcb_regs[T0] = ((int *)&args)[9];
kdbpcb.pcb_regs[T1] = ((int *)&args)[10];
kdbpcb.pcb_regs[T2] = ((int *)&args)[11];
kdbpcb.pcb_regs[T3] = ((int *)&args)[12];
kdbpcb.pcb_regs[T4] = ((int *)&args)[13];
kdbpcb.pcb_regs[T5] = ((int *)&args)[14];
kdbpcb.pcb_regs[T6] = ((int *)&args)[15];
kdbpcb.pcb_regs[T7] = ((int *)&args)[16];
kdbpcb.pcb_regs[T8] = ((int *)&args)[17];
kdbpcb.pcb_regs[T9] = ((int *)&args)[18];
kdbpcb.pcb_regs[RA] = ((int *)&args)[19];
kdbpcb.pcb_regs[MULLO] = ((int *)&args)[21];
kdbpcb.pcb_regs[MULHI] = ((int *)&args)[22];
kdbpcb.pcb_regs[PC] = pc;
kdbpcb.pcb_regs[SR] = statusReg;
bzero((caddr_t)&kdbpcb.pcb_regs[F0], 33 * sizeof(int));
}
if (kdb(causeReg, vadr, p, !USERMODE(statusReg)))
return (kdbpcb.pcb_regs[PC]);
}
#else
#ifdef DEBUG
stacktrace();
trapDump("trap");
#endif
#endif
panic("trap");
}
p->p_md.md_regs [PC] = pc;
p->p_md.md_regs [CAUSE] = causeReg;
p->p_md.md_regs [BADVADDR] = vadr;
trapsignal(p, i, ucode, typ, (caddr_t)vadr);
out:
/*
* Note: we should only get here if returning to user mode.
*/
/* take pending signals */
while ((i = CURSIG(p)) != 0)
postsig(i);
p->p_priority = p->p_usrpri;
astpending = 0;
if (want_resched) {
int s;
/*
* Since we are curproc, clock will normally just change
* our priority without moving us from one queue to another
* (since the running process is not on a queue.)
* If that happened after we put ourselves on the run queue
* but before we switched, we might not be on the queue
* indicated by our priority.
*/
s = splstatclock();
setrunqueue(p);
p->p_stats->p_ru.ru_nivcsw++;
mi_switch();
splx(s);
while ((i = CURSIG(p)) != 0)
postsig(i);
}
/*
* If profiling, charge system time to the trapped pc.
*/
if (p->p_flag & P_PROFIL) {
extern int psratio;
addupc_task(p, pc, (int)(p->p_sticks - sticks) * psratio);
}
curpriority = p->p_priority;
return (pc);
}
/*
* Handle an interrupt.
* Called from MachKernIntr() or MachUserIntr()
* Note: curproc might be NULL.
*/
void
interrupt(statusReg, causeReg, pc /* XXX what, args */ )
unsigned statusReg; /* status register at time of the exception */
unsigned causeReg; /* cause register at time of exception */
unsigned pc; /* program counter where to continue */
{
register unsigned mask;
/*struct clockframe cf;*/
#ifdef DEBUG
trp->status = statusReg;
trp->cause = causeReg;
trp->vadr = 0;
trp->pc = pc;
trp->ra = 0;
trp->sp = /* (int)&args */ 0; /* XXX pass args in */
trp->code = 0;
if (++trp == &trapdebug[TRAPSIZE])
trp = trapdebug;
#endif
cnt.v_intr++;
mask = causeReg & statusReg; /* pending interrupts & enable mask */
if (mips_hardware_intr)
splx((*mips_hardware_intr)(mask, pc, statusReg, causeReg));
if (mask & MACH_INT_MASK_5) {
intrcnt[FPU_INTR]++;
if (!USERMODE(statusReg)) {
#ifdef DEBUG
trapDump("fpintr");
#else
printf("FPU interrupt: PC %x CR %x SR %x\n",
pc, causeReg, statusReg);
#endif
} else
MachFPInterrupt(statusReg, causeReg, pc);
}
/* process network interrupt if we trapped or will very soon */
/* XXX fixme: operator precedence botch? */
if ((mask & MACH_SOFT_INT_MASK_1) ||
netisr && (statusReg & MACH_SOFT_INT_MASK_1)) {
clearsoftnet();
cnt.v_soft++;
intrcnt[SOFTNET_INTR]++;
#ifdef INET
if (netisr & (1 << NETISR_ARP)) {
netisr &= ~(1 << NETISR_ARP);
arpintr();
}
if (netisr & (1 << NETISR_IP)) {
netisr &= ~(1 << NETISR_IP);
ipintr();
}
#endif
#ifdef NS
if (netisr & (1 << NETISR_NS)) {
netisr &= ~(1 << NETISR_NS);
nsintr();
}
#endif
#ifdef ISO
if (netisr & (1 << NETISR_ISO)) {
netisr &= ~(1 << NETISR_ISO);
clnlintr();
}
#endif
#if NPPP > 0
if (netisr & (1 << NETISR_PPP)) {
netisr &= ~(1 << NETISR_PPP);
pppintr();
}
#endif
}
if (mask & MACH_SOFT_INT_MASK_0) {
clearsoftclock();
intrcnt[SOFTCLOCK_INTR]++;
cnt.v_soft++;
softclock();
}
}
/*
* This is called from MachUserIntr() if astpending is set.
* This is very similar to the tail of trap().
*/
void
softintr(statusReg, pc)
unsigned statusReg; /* status register at time of the exception */
unsigned pc; /* program counter where to continue */
{
register struct proc *p = curproc;
int sig;
cnt.v_soft++;
/* take pending signals */
while ((sig = CURSIG(p)) != 0)
postsig(sig);
p->p_priority = p->p_usrpri;
astpending = 0;
if (p->p_flag & P_OWEUPC) {
p->p_flag &= ~P_OWEUPC;
ADDUPROF(p);
}
if (want_resched) {
int s;
/*
* Since we are curproc, clock will normally just change
* our priority without moving us from one queue to another
* (since the running process is not on a queue.)
* If that happened after we put ourselves on the run queue
* but before we switched, we might not be on the queue
* indicated by our priority.
*/
s = splstatclock();
setrunqueue(p);
p->p_stats->p_ru.ru_nivcsw++;
mi_switch();
splx(s);
while ((sig = CURSIG(p)) != 0)
postsig(sig);
}
curpriority = p->p_priority;
}
#ifdef DEBUG
void
trapDump(msg)
char *msg;
{
register int i;
int s;
s = splhigh();
printf("trapDump(%s)\n", msg);
for (i = 0; i < TRAPSIZE; i++) {
if (trp == trapdebug)
trp = &trapdebug[TRAPSIZE - 1];
else
trp--;
if (trp->cause == 0)
break;
printf("%s: ADR %x PC %x CR %x SR %x\n",
trap_type[(trp->cause & MIPS_3K_CR_EXC_CODE) >>
MACH_CR_EXC_CODE_SHIFT],
trp->vadr, trp->pc, trp->cause, trp->status);
printf(" RA %x SP %x code %d\n", trp->ra, trp->sp, trp->code);
}
bzero(trapdebug, sizeof(trapdebug));
trp = trapdebug;
splx(s);
}
#endif
/*
* forward declaration
*/
static unsigned GetBranchDest __P((InstFmt *InstPtr));
/*
* Compute destination of a branch instruction.
* XXX Compute desination of r4000 squashed branches?
*/
static unsigned
GetBranchDest(InstPtr)
InstFmt *InstPtr;
{
return ((unsigned)InstPtr + 4 + ((short)InstPtr->IType.imm << 2));
}
/*
* Return the resulting PC as if the branch was executed.
*/
unsigned
MachEmulateBranch(regsPtr, instPC, fpcCSR, allowNonBranch)
unsigned *regsPtr;
unsigned instPC;
unsigned fpcCSR;
int allowNonBranch;
{
InstFmt inst;
unsigned retAddr;
int condition;
inst.word = (instPC < MACH_CACHED_MEMORY_ADDR) ?
fuiword((caddr_t)instPC) : *(unsigned*)instPC;
#if 0
printf("regsPtr=%x PC=%x Inst=%x fpcCsr=%x\n", regsPtr, instPC,
inst.word, fpcCSR); /* XXX */
#endif
switch ((int)inst.JType.op) {
case OP_SPECIAL:
switch ((int)inst.RType.func) {
case OP_JR:
case OP_JALR:
retAddr = regsPtr[inst.RType.rs];
break;
default:
if (!allowNonBranch)
panic("MachEmulateBranch: Non-branch");
retAddr = instPC + 4;
break;
}
break;
case OP_BCOND:
switch ((int)inst.IType.rt) {
case OP_BLTZ:
case OP_BLTZAL:
case OP_BLTZL: /* squashed */
case OP_BLTZALL: /* squashed */
if ((int)(regsPtr[inst.RType.rs]) < 0)
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
case OP_BGEZ:
case OP_BGEZAL:
case OP_BGEZL: /* squashed */
case OP_BGEZALL: /* squashed */
if ((int)(regsPtr[inst.RType.rs]) >= 0)
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
default:
panic("MachEmulateBranch: Bad branch cond");
}
break;
case OP_J:
case OP_JAL:
retAddr = (inst.JType.target << 2) |
((unsigned)instPC & 0xF0000000);
break;
case OP_BEQ:
case OP_BEQL: /* squashed */
if (regsPtr[inst.RType.rs] == regsPtr[inst.RType.rt])
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
case OP_BNE:
case OP_BNEL: /* squashed */
if (regsPtr[inst.RType.rs] != regsPtr[inst.RType.rt])
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
case OP_BLEZ:
case OP_BLEZL: /* squashed */
if ((int)(regsPtr[inst.RType.rs]) <= 0)
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
case OP_BGTZ:
case OP_BGTZL: /* squashed */
if ((int)(regsPtr[inst.RType.rs]) > 0)
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
case OP_COP1:
switch (inst.RType.rs) {
case OP_BCx:
case OP_BCy:
if ((inst.RType.rt & COPz_BC_TF_MASK) == COPz_BC_TRUE)
condition = fpcCSR & MACH_FPC_COND_BIT;
else
condition = !(fpcCSR & MACH_FPC_COND_BIT);
if (condition)
retAddr = GetBranchDest((InstFmt *)instPC);
else
retAddr = instPC + 8;
break;
default:
if (!allowNonBranch)
panic("MachEmulateBranch: Bad coproc branch instruction");
retAddr = instPC + 4;
}
break;
default:
if (!allowNonBranch)
panic("MachEmulateBranch: Non-branch instruction");
retAddr = instPC + 4;
}
#if 0
printf("Target addr=%x\n", retAddr); /* XXX */
#endif
return (retAddr);
}
/*
* This routine is called by procxmt() to single step one instruction.
* We do this by storing a break instruction after the current instruction,
* resuming execution, and then restoring the old instruction.
*/
int
cpu_singlestep(p)
register struct proc *p;
{
register unsigned va;
register int *locr0 = p->p_md.md_regs;
int i;
#if notanymore
/* compute next address after current location */
va = MachEmulateBranch(locr0, locr0[PC], locr0[FSR], 1);
if (p->p_md.md_ss_addr || p->p_md.md_ss_addr == va ||
!useracc((caddr_t)va, 4, B_READ)) {
printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
p->p_comm, p->p_pid, p->p_md.md_ss_addr, va); /* XXX */
return (EFAULT);
}
p->p_md.md_ss_addr = va;
p->p_md.md_ss_instr = fuiword((caddr_t)va);
i = suiword((caddr_t)va, MACH_BREAK_SSTEP);
if (i < 0) {
vm_offset_t sa, ea;
int rv;
sa = trunc_page((vm_offset_t)va);
ea = round_page((vm_offset_t)va+sizeof(int)-1);
rv = vm_map_protect(&p->p_vmspace->vm_map, sa, ea,
VM_PROT_DEFAULT, FALSE);
if (rv == KERN_SUCCESS) {
i = suiword((caddr_t)va, MACH_BREAK_SSTEP);
(void) vm_map_protect(&p->p_vmspace->vm_map,
sa, ea, VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
}
}
#endif
int bpinstr = MACH_BREAK_SSTEP;
int curinstr;
struct uio uio;
struct iovec iov;
/*
* Fetch what's at the current location.
*/
iov.iov_base = (caddr_t)&curinstr;
iov.iov_len = sizeof(int);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_offset = (off_t)locr0[PC];
uio.uio_resid = sizeof(int);
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_rw = UIO_READ;
uio.uio_procp = curproc;
procfs_domem(curproc, p, NULL, &uio);
/* compute next address after current location */
if(curinstr != 0) {
va = MachEmulateBranch(locr0, locr0[PC], locr0[FSR], curinstr);
}
else {
va = locr0[PC] + 4;
}
if (p->p_md.md_ss_addr) {
printf("SS %s (%d): breakpoint already set at %x (va %x)\n",
p->p_comm, p->p_pid, p->p_md.md_ss_addr, va); /* XXX */
return (EFAULT);
}
p->p_md.md_ss_addr = va;
/*
* Fetch what's at the current location.
*/
iov.iov_base = (caddr_t)&p->p_md.md_ss_instr;
iov.iov_len = sizeof(int);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_offset = (off_t)va;
uio.uio_resid = sizeof(int);
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_rw = UIO_READ;
uio.uio_procp = curproc;
procfs_domem(curproc, p, NULL, &uio);
/*
* Store breakpoint instruction at the "next" location now.
*/
iov.iov_base = (caddr_t)&bpinstr;
iov.iov_len = sizeof(int);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_offset = (off_t)va;
uio.uio_resid = sizeof(int);
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_rw = UIO_WRITE;
uio.uio_procp = curproc;
i = procfs_domem(curproc, p, NULL, &uio);
MachFlushCache(); /* XXX memory barrier followed by flush icache? */
if (i < 0)
return (EFAULT);
#if 0
printf("SS %s (%d): breakpoint set at %x: %x (pc %x) br %x\n",
p->p_comm, p->p_pid, p->p_md.md_ss_addr,
p->p_md.md_ss_instr, locr0[PC], fuword((caddr_t)va)); /* XXX */
#endif
return (0);
}
#ifdef DEBUG
int
kdbpeek(addr)
{
if (addr & 3) {
printf("kdbpeek: unaligned address %x\n", addr);
return (-1);
}
return (*(int *)addr);
}
#endif
#ifdef DEBUG
#define MIPS_JR_RA 0x03e00008 /* instruction code for jr ra */
/* forward */
char *fn_name(unsigned addr);
void stacktrace_subr __P((int, int, int, int, void (*)(const char*, ...)));
/*
* Print a stack backtrace.
*/
void
stacktrace(a0, a1, a2, a3)
int a0, a1, a2, a3;
{
stacktrace_subr(a0, a1, a2, a3, printf);
}
void
logstacktrace(a0, a1, a2, a3)
int a0, a1, a2, a3;
{
stacktrace_subr(a0, a1, a2, a3, addlog);
}
void
stacktrace_subr(a0, a1, a2, a3, printfn)
int a0, a1, a2, a3;
void (*printfn) __P((const char*, ...));
{
unsigned pc, sp, fp, ra, va, subr;
unsigned instr, mask;
InstFmt i;
int more, stksize;
int regs[3];
extern char start[], edata[];
unsigned int frames = 0;
cpu_getregs(regs);
/* get initial values from the exception frame */
sp = regs[0];
pc = regs[1];
ra = 0;
fp = regs[2];
/* Jump here when done with a frame, to start a new one */
loop:
ra = 0;
/* Jump here after a nonstandard (interrupt handler) frame */
specialframe:
stksize = 0;
subr = 0;
if (frames++ > 100) {
(*printfn)("\nstackframe count exceeded\n");
/* return breaks stackframe-size heuristics with gcc -O2 */
goto finish; /*XXX*/
}
/* check for bad SP: could foul up next frame */
if (sp & 3 || sp < 0x80000000) {
(*printfn)("SP 0x%x: not in kernel\n", sp);
ra = 0;
subr = 0;
goto done;
}
/*
* check for PC between two entry points
*/
# define Between(x, y, z) \
( ((x) <= (y)) && ((y) < (z)) )
# define pcBetween(a,b) \
Between((unsigned)a, pc, (unsigned)b)
/* Backtraces should continue through interrupts from kernel mode */
#ifdef MIPS1 /* r2000 family (mips-I cpu) */
if (pcBetween(mips1_KernIntr, mips1_UserIntr)) {
/* NOTE: the offsets depend on the code in locore.s */
(*printfn)("r3000 KernIntr+%x: (%x, %x ,%x) -------\n",
pc-(unsigned)mips1_KernIntr, a0, a1, a2);
a0 = kdbpeek(sp + 36);
a1 = kdbpeek(sp + 40);
a2 = kdbpeek(sp + 44);
a3 = kdbpeek(sp + 48);
pc = kdbpeek(sp + 20); /* exc_pc - pc at time of exception */
ra = kdbpeek(sp + 92); /* ra at time of exception */
sp = sp + 108;
goto specialframe;
}
#endif /* MIPS1 */
#ifdef MIPS3 /* r4000 family (mips-III cpu) */
if (pcBetween(mips3_KernIntr, mips3_UserIntr)) {
/* NOTE: the offsets depend on the code in locore.s */
(*printfn)("R4000 KernIntr+%x: (%x, %x ,%x) -------\n",
pc-(unsigned)mips3_KernIntr, a0, a1, a2);
a0 = kdbpeek(sp + 36);
a1 = kdbpeek(sp + 40);
a2 = kdbpeek(sp + 44);
a3 = kdbpeek(sp + 48);
pc = kdbpeek(sp + 20); /* exc_pc - pc at time of exception */
ra = kdbpeek(sp + 92); /* ra at time of exception */
sp = sp + 108;
goto specialframe;
}
#endif /* MIPS3 */
/*
* Check for current PC in exception handler code that don't
* have a preceding "j ra" at the tail of the preceding function.
* Depends on relative ordering of functions in locore.
*/
/* XXX fixup tests after cutting and pasting in locore.S */
/* R4000 exception handlers */
#ifdef MIPS1 /* r2000 family (mips-I cpu) */
if (pcBetween(mips1_KernGenException, mips1_UserGenException))
subr = (unsigned) mips1_KernGenException;
else if (pcBetween(mips1_UserGenException,mips1_KernIntr))
subr = (unsigned) mips1_UserGenException;
else if (pcBetween(mips1_KernIntr, mips1_UserIntr))
subr = (unsigned) mips1_KernIntr;
else if (pcBetween(mips1_UserIntr, mips1_TLBMissException))
subr = (unsigned) mips1_UserIntr;
else if (pcBetween(mips1_UserIntr, mips1_TLBMissException))
subr = (unsigned) mips1_UserIntr;
else if (pcBetween(mips1_UTLBMiss, mips1_exceptionentry_end)) {
(*printfn)("<<mips1 locore>>");
goto done;
}
else
#endif /* MIPS1 */
#ifdef MIPS3 /* r4000 family (mips-III cpu) */
/* R4000 exception handlers */
if (pcBetween(mips3_KernGenException, mips3_UserGenException))
subr = (unsigned) mips3_KernGenException;
else if (pcBetween(mips3_UserGenException,mips3_KernIntr))
subr = (unsigned) mips3_UserGenException;
else if (pcBetween(mips3_KernIntr, mips3_UserIntr))
subr = (unsigned) mips3_KernIntr;
else if (pcBetween(mips3_UserIntr, mips3_TLBMissException))
subr = (unsigned) mips3_UserIntr;
else if (pcBetween(mips3_TLBMiss, mips3_exceptionentry_end)) {
(*printfn)("<<mips3 locore>>");
goto done;
} else
#endif /* MIPS3 */
if (pcBetween(splx, wbflush))
subr = (unsigned) splx;
else if (pcBetween(cpu_switch, fuword))
subr = (unsigned) cpu_switch;
else if (pcBetween(idle, cpu_switch)) {
subr = (unsigned) idle;
ra = 0;
goto done;
}
/* Check for bad PC */
if (pc & 3 || pc < 0x80000000 || pc >= (unsigned)edata) {
(*printfn)("PC 0x%x: not in kernel space\n", pc);
ra = 0;
goto done;
}
if (!pcBetween(start, (unsigned) edata)) {
(*printfn)("PC 0x%x: not in kernel text\n", pc);
ra = 0;
goto done;
}
/*
* Find the beginning of the current subroutine by scanning backwards
* from the current PC for the end of the previous subroutine.
*/
if (!subr) {
va = pc - sizeof(int);
while ((instr = kdbpeek(va)) != MIPS_JR_RA)
va -= sizeof(int);
va += 2 * sizeof(int); /* skip back over branch & delay slot */
/* skip over nulls which might separate .o files */
while ((instr = kdbpeek(va)) == 0)
va += sizeof(int);
subr = va;
}
/*
* Jump here for locore entry pointsn for which the preceding
* function doesn't end in "j ra"
*/
#if 0
stackscan:
#endif
/* scan forwards to find stack size and any saved registers */
stksize = 0;
more = 3;
mask = 0;
for (va = subr; more; va += sizeof(int),
more = (more == 3) ? 3 : more - 1) {
/* stop if hit our current position */
if (va >= pc)
break;
instr = kdbpeek(va);
i.word = instr;
switch (i.JType.op) {
case OP_SPECIAL:
switch (i.RType.func) {
case OP_JR:
case OP_JALR:
more = 2; /* stop after next instruction */
break;
case OP_SYSCALL:
case OP_BREAK:
more = 1; /* stop now */
};
break;
case OP_BCOND:
case OP_J:
case OP_JAL:
case OP_BEQ:
case OP_BNE:
case OP_BLEZ:
case OP_BGTZ:
more = 2; /* stop after next instruction */
break;
case OP_COP0:
case OP_COP1:
case OP_COP2:
case OP_COP3:
switch (i.RType.rs) {
case OP_BCx:
case OP_BCy:
more = 2; /* stop after next instruction */
};
break;
case OP_SW:
/* look for saved registers on the stack */
if (i.IType.rs != 29)
break;
/* only restore the first one */
if (mask & (1 << i.IType.rt))
break;
mask |= (1 << i.IType.rt);
switch (i.IType.rt) {
case 4: /* a0 */
a0 = kdbpeek(sp + (short)i.IType.imm);
break;
case 5: /* a1 */
a1 = kdbpeek(sp + (short)i.IType.imm);
break;
case 6: /* a2 */
a2 = kdbpeek(sp + (short)i.IType.imm);
break;
case 7: /* a3 */
a3 = kdbpeek(sp + (short)i.IType.imm);
break;
case 30: /* fp */
fp = kdbpeek(sp + (short)i.IType.imm);
break;
case 31: /* ra */
ra = kdbpeek(sp + (short)i.IType.imm);
}
break;
case OP_ADDI:
case OP_ADDIU:
/* look for stack pointer adjustment */
if (i.IType.rs != 29 || i.IType.rt != 29)
break;
stksize = - ((short)i.IType.imm);
}
}
done:
(*printfn)("%s+%x (%x,%x,%x,%x) ra %x sz %d\n",
fn_name(subr), pc - subr, a0, a1, a2, a3, ra, stksize);
if (ra) {
if (pc == ra && stksize == 0)
(*printfn)("stacktrace: loop!\n");
else {
pc = ra;
sp += stksize;
ra = 0;
goto loop;
}
} else {
finish:
if (curproc)
(*printfn)("User-level: pid %d\n", curproc->p_pid);
else
(*printfn)("User-level: curproc NULL\n");
}
}
/*
* Functions ``special'' enough to print by name
*/
#ifdef __STDC__
#define Name(_fn) { (void*)_fn, # _fn }
#else
#define Name(_fn) { _fn, "_fn"}
#endif
static struct { void *addr; char *name;} names[] = {
Name(stacktrace),
Name(stacktrace_subr),
Name(main),
Name(interrupt),
Name(trap),
#ifdef pmax
Name(am7990_meminit),
#endif
#ifdef MIPS1 /* r2000 family (mips-I cpu) */
Name(mips1_KernGenException),
Name(mips1_UserGenException),
Name(mips1_KernIntr),
Name(mips1_UserIntr),
#endif /* MIPS1 */
#ifdef MIPS3 /* r4000 family (mips-III cpu) */
Name(mips3_KernGenException),
Name(mips3_UserGenException),
Name(mips3_KernIntr),
Name(mips3_UserIntr),
#endif /* MIPS3 */
Name(splx),
Name(idle),
Name(cpu_switch),
{0, 0}
};
/*
* Map a function address to a string name, if known; or a hex string.
*/
char *
fn_name(unsigned addr)
{
static char buf[17];
int i = 0;
for (i = 0; names[i].name; i++)
if (names[i].addr == (void*)addr)
return (names[i].name);
sprintf(buf, "%x", addr);
return (buf);
}
#endif /* DEBUG */
|