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
|
/*
* Mach Operating System
* Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
* Copyright (c) 1991 IBM Corporation
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation,
* and that the nema IBM not be used in advertising or publicity
* pertaining to distribution of the software without specific, written
* prior permission.
*
* CARNEGIE MELLON AND IBM ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON AND IBM DISCLAIM ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
#include <cpus.h>
#include <platforms.h>
#include <mach_kdb.h>
#include <mach_ttd.h>
#include <stat_time.h>
#include <mach/machine/asm.h>
#include <mach/machine/eflags.h>
#include "proc_reg.h"
#include "trap.h"
#include "seg.h"
#include "ldt.h"
#include "i386asm.h"
#include "cpu_number.h"
/*
* Fault recovery.
*/
#define RECOVER_TABLE_START \
.text 2 ;\
DATA(recover_table) ;\
.text
#define RECOVER(addr) \
.text 2 ;\
.long 9f ;\
.long addr ;\
.text ;\
9:
#define RECOVER_TABLE_END \
.text 2 ;\
.globl EXT(recover_table_end) ;\
LEXT(recover_table_end) ;\
.text
/*
* Retry table for certain successful faults.
*/
#define RETRY_TABLE_START \
.text 3 ;\
DATA(retry_table) ;\
.text
#define RETRY(addr) \
.text 3 ;\
.long 9f ;\
.long addr ;\
.text ;\
9:
#define RETRY_TABLE_END \
.text 3 ;\
.globl EXT(retry_table_end) ;\
LEXT(retry_table_end) ;\
.text
/*
* Allocate recovery and retry tables.
*/
RECOVER_TABLE_START
RETRY_TABLE_START
/*
* Timing routines.
*/
#if STAT_TIME
#define TIME_TRAP_UENTRY
#define TIME_TRAP_SENTRY
#define TIME_TRAP_UEXIT
#define TIME_INT_ENTRY
#define TIME_INT_EXIT
#else /* microsecond timing */
/*
* Microsecond timing.
* Assumes a free-running microsecond counter.
* no TIMER_MAX check needed.
*/
/*
* There is only one current time-stamp per CPU, since only
* the time-stamp in the current timer is used.
* To save time, we allocate the current time-stamps here.
*/
.comm _current_tstamp, 4*NCPUS
/*
* Update time on user trap entry.
* 11 instructions (including cli on entry)
* Assumes CPU number in %edx.
* Uses %eax, %ebx, %ecx.
*/
#define TIME_TRAP_UENTRY \
cli /* block interrupts */ ;\
movl VA_ETC,%ebx /* get timer value */ ;\
movl CX(_current_tstamp,%edx),%ecx /* get old time stamp */;\
movl %ebx,CX(_current_tstamp,%edx) /* set new time stamp */;\
subl %ecx,%ebx /* elapsed = new-old */ ;\
movl CX(_current_timer,%edx),%ecx /* get current timer */ ;\
addl %ebx,LOW_BITS(%ecx) /* add to low bits */ ;\
jns 0f /* if overflow, */ ;\
call timer_normalize /* normalize timer */ ;\
0: addl $(TH_SYS_TIMER-TH_USER_TIMER),%ecx ;\
/* switch to sys timer */;\
movl %ecx,CX(_current_timer,%edx) /* make it current */ ;\
sti /* allow interrupts */
/*
* Update time on system call entry.
* 11 instructions (including cli on entry)
* Assumes CPU number in %edx.
* Uses %ebx, %ecx.
* Same as TIME_TRAP_UENTRY, but preserves %eax.
*/
#define TIME_TRAP_SENTRY \
cli /* block interrupts */ ;\
movl VA_ETC,%ebx /* get timer value */ ;\
movl CX(_current_tstamp,%edx),%ecx /* get old time stamp */;\
movl %ebx,CX(_current_tstamp,%edx) /* set new time stamp */;\
subl %ecx,%ebx /* elapsed = new-old */ ;\
movl CX(_current_timer,%edx),%ecx /* get current timer */ ;\
addl %ebx,LOW_BITS(%ecx) /* add to low bits */ ;\
jns 0f /* if overflow, */ ;\
pushl %eax /* save %eax */ ;\
call timer_normalize /* normalize timer */ ;\
popl %eax /* restore %eax */ ;\
0: addl $(TH_SYS_TIMER-TH_USER_TIMER),%ecx ;\
/* switch to sys timer */;\
movl %ecx,CX(_current_timer,%edx) /* make it current */ ;\
sti /* allow interrupts */
/*
* update time on user trap exit.
* 10 instructions.
* Assumes CPU number in %edx.
* Uses %ebx, %ecx.
*/
#define TIME_TRAP_UEXIT \
cli /* block interrupts */ ;\
movl VA_ETC,%ebx /* get timer */ ;\
movl CX(_current_tstamp,%edx),%ecx /* get old time stamp */;\
movl %ebx,CX(_current_tstamp,%edx) /* set new time stamp */;\
subl %ecx,%ebx /* elapsed = new-old */ ;\
movl CX(_current_timer,%edx),%ecx /* get current timer */ ;\
addl %ebx,LOW_BITS(%ecx) /* add to low bits */ ;\
jns 0f /* if overflow, */ ;\
call timer_normalize /* normalize timer */ ;\
0: addl $(TH_USER_TIMER-TH_SYS_TIMER),%ecx ;\
/* switch to user timer */;\
movl %ecx,CX(_current_timer,%edx) /* make it current */
/*
* update time on interrupt entry.
* 9 instructions.
* Assumes CPU number in %edx.
* Leaves old timer in %ebx.
* Uses %ecx.
*/
#define TIME_INT_ENTRY \
movl VA_ETC,%ecx /* get timer */ ;\
movl CX(_current_tstamp,%edx),%ebx /* get old time stamp */;\
movl %ecx,CX(_current_tstamp,%edx) /* set new time stamp */;\
subl %ebx,%ecx /* elapsed = new-old */ ;\
movl CX(_current_timer,%edx),%ebx /* get current timer */ ;\
addl %ecx,LOW_BITS(%ebx) /* add to low bits */ ;\
leal CX(0,%edx),%ecx /* timer is 16 bytes */ ;\
lea CX(_kernel_timer,%edx),%ecx /* get interrupt timer*/;\
movl %ecx,CX(_current_timer,%edx) /* set timer
/*
* update time on interrupt exit.
* 11 instructions
* Assumes CPU number in %edx, old timer in %ebx.
* Uses %eax, %ecx.
*/
#define TIME_INT_EXIT \
movl VA_ETC,%eax /* get timer */ ;\
movl CX(_current_tstamp,%edx),%ecx /* get old time stamp */;\
movl %eax,CX(_current_tstamp,%edx) /* set new time stamp */;\
subl %ecx,%eax /* elapsed = new-old */ ;\
movl CX(_current_timer,%edx),%ecx /* get current timer */ ;\
addl %eax,LOW_BITS(%ecx) /* add to low bits */ ;\
jns 0f /* if overflow, */ ;\
call timer_normalize /* normalize timer */ ;\
0: testb $0x80,LOW_BITS+3(%ebx) /* old timer overflow? */;\
jz 0f /* if overflow, */ ;\
movl %ebx,%ecx /* get old timer */ ;\
call timer_normalize /* normalize timer */ ;\
0: movl %ebx,CX(_current_timer,%edx) /* set timer */
/*
* Normalize timer in ecx.
* Preserves edx; clobbers eax.
*/
.align 2
timer_high_unit:
.long TIMER_HIGH_UNIT /* div has no immediate opnd */
timer_normalize:
pushl %edx /* save register */
xorl %edx,%edx /* clear divisor high */
movl LOW_BITS(%ecx),%eax /* get divisor low */
divl timer_high_unit,%eax /* quotient in eax */
/* remainder in edx */
addl %eax,HIGH_BITS_CHECK(%ecx) /* add high_inc to check */
movl %edx,LOW_BITS(%ecx) /* remainder to low_bits */
addl %eax,HIGH_BITS(%ecx) /* add high_inc to high bits */
popl %edx /* restore register */
ret
/*
* Switch to a new timer.
*/
ENTRY(timer_switch)
CPU_NUMBER(%edx) /* get this CPU */
movl VA_ETC,%ecx /* get timer */
movl CX(_current_tstamp,%edx),%eax /* get old time stamp */
movl %ecx,CX(_current_tstamp,%edx) /* set new time stamp */
subl %ecx,%eax /* elapsed = new - old */
movl CX(_current_timer,%edx),%ecx /* get current timer */
addl %eax,LOW_BITS(%ecx) /* add to low bits */
jns 0f /* if overflow, */
call timer_normalize /* normalize timer */
0:
movl S_ARG0,%ecx /* get new timer */
movl %ecx,CX(_current_timer,%edx) /* set timer */
ret
/*
* Initialize the first timer for a CPU.
*/
ENTRY(start_timer)
CPU_NUMBER(%edx) /* get this CPU */
movl VA_ETC,%ecx /* get timer */
movl %ecx,CX(_current_tstamp,%edx) /* set initial time stamp */
movl S_ARG0,%ecx /* get timer */
movl %ecx,CX(_current_timer,%edx) /* set initial timer */
ret
#endif /* accurate timing */
/**/
/*
* Trap/interrupt entry points.
*
* All traps must create the following save area on the kernel stack:
*
* gs
* fs
* es
* ds
* edi
* esi
* ebp
* cr2 if page fault - otherwise unused
* ebx
* edx
* ecx
* eax
* trap number
* error code
* eip
* cs
* eflags
* user esp - if from user
* user ss - if from user
* es - if from V86 thread
* ds - if from V86 thread
* fs - if from V86 thread
* gs - if from V86 thread
*
*/
/*
* General protection or segment-not-present fault.
* Check for a GP/NP fault in the kernel_return
* sequence; if there, report it as a GP/NP fault on the user's instruction.
*
* esp-> 0: trap code (NP or GP)
* 4: segment number in error
* 8 eip
* 12 cs
* 16 eflags
* 20 old registers (trap is from kernel)
*/
ENTRY(t_gen_prot)
pushl $(T_GENERAL_PROTECTION) /* indicate fault type */
jmp trap_check_kernel_exit /* check for kernel exit sequence */
ENTRY(t_segnp)
pushl $(T_SEGMENT_NOT_PRESENT)
/* indicate fault type */
trap_check_kernel_exit:
testl $(EFL_VM),16(%esp) /* is trap from V86 mode? */
jnz EXT(alltraps) /* isn`t kernel trap if so */
testl $3,12(%esp) /* is trap from kernel mode? */
jne EXT(alltraps) /* if so: */
/* check for the kernel exit sequence */
cmpl $_kret_iret,8(%esp) /* on IRET? */
je fault_iret
cmpl $_kret_popl_ds,8(%esp) /* popping DS? */
je fault_popl_ds
cmpl $_kret_popl_es,8(%esp) /* popping ES? */
je fault_popl_es
cmpl $_kret_popl_fs,8(%esp) /* popping FS? */
je fault_popl_fs
cmpl $_kret_popl_gs,8(%esp) /* popping GS? */
je fault_popl_gs
take_fault: /* if none of the above: */
jmp EXT(alltraps) /* treat as normal trap. */
/*
* GP/NP fault on IRET: CS or SS is in error.
* All registers contain the user's values.
*
* on SP is
* 0 trap number
* 4 errcode
* 8 eip
* 12 cs --> trapno
* 16 efl --> errcode
* 20 user eip
* 24 user cs
* 28 user eflags
* 32 user esp
* 36 user ss
*/
fault_iret:
movl %eax,8(%esp) /* save eax (we don`t need saved eip) */
popl %eax /* get trap number */
movl %eax,12-4(%esp) /* put in user trap number */
popl %eax /* get error code */
movl %eax,16-8(%esp) /* put in user errcode */
popl %eax /* restore eax */
jmp EXT(alltraps) /* take fault */
/*
* Fault restoring a segment register. The user's registers are still
* saved on the stack. The offending segment register has not been
* popped.
*/
fault_popl_ds:
popl %eax /* get trap number */
popl %edx /* get error code */
addl $12,%esp /* pop stack to user regs */
jmp push_es /* (DS on top of stack) */
fault_popl_es:
popl %eax /* get trap number */
popl %edx /* get error code */
addl $12,%esp /* pop stack to user regs */
jmp push_fs /* (ES on top of stack) */
fault_popl_fs:
popl %eax /* get trap number */
popl %edx /* get error code */
addl $12,%esp /* pop stack to user regs */
jmp push_gs /* (FS on top of stack) */
fault_popl_gs:
popl %eax /* get trap number */
popl %edx /* get error code */
addl $12,%esp /* pop stack to user regs */
jmp push_segregs /* (GS on top of stack) */
push_es:
pushl %es /* restore es, */
push_fs:
pushl %fs /* restore fs, */
push_gs:
pushl %gs /* restore gs. */
push_segregs:
movl %eax,R_TRAPNO(%esp) /* set trap number */
movl %edx,R_ERR(%esp) /* set error code */
jmp trap_set_segs /* take trap */
/*
* Debug trap. Check for single-stepping across system call into
* kernel. If this is the case, taking the debug trap has turned
* off single-stepping - save the flags register with the trace
* bit set.
*/
ENTRY(t_debug)
testl $(EFL_VM),8(%esp) /* is trap from V86 mode? */
jnz 0f /* isn`t kernel trap if so */
testl $3,4(%esp) /* is trap from kernel mode? */
jnz 0f /* if so: */
cmpl $syscall_entry,(%esp) /* system call entry? */
jne 0f /* if so: */
/* flags are sitting where syscall */
/* wants them */
addl $8,%esp /* remove eip/cs */
jmp syscall_entry_2 /* continue system call entry */
0: pushl $0 /* otherwise: */
pushl $(T_DEBUG) /* handle as normal */
jmp EXT(alltraps) /* debug fault */
/*
* Page fault traps save cr2.
*/
ENTRY(t_page_fault)
pushl $(T_PAGE_FAULT) /* mark a page fault trap */
pusha /* save the general registers */
movl %cr2,%eax /* get the faulting address */
movl %eax,12(%esp) /* save in esp save slot */
jmp trap_push_segs /* continue fault */
/*
* All 'exceptions' enter here with:
* esp-> trap number
* error code
* old eip
* old cs
* old eflags
* old esp if trapped from user
* old ss if trapped from user
*/
ENTRY(alltraps)
pusha /* save the general registers */
trap_push_segs:
pushl %ds /* and the segment registers */
pushl %es
pushl %fs
pushl %gs
/* Note that we have to load the segment registers
even if this is a trap from the kernel,
because the kernel uses user segment registers for copyin/copyout.
(XXX Would it be smarter just to use fs or gs for that?) */
mov %ss,%ax /* switch to kernel data segment */
mov %ax,%ds /* (same as kernel stack segment) */
mov %ax,%es
mov %ax,%fs
mov %ax,%gs
trap_set_segs:
cld /* clear direction flag */
testl $(EFL_VM),R_EFLAGS(%esp) /* in V86 mode? */
jnz trap_from_user /* user mode trap if so */
testb $3,R_CS(%esp) /* user mode trap? */
jz trap_from_kernel /* kernel trap if not */
trap_from_user:
CPU_NUMBER(%edx)
TIME_TRAP_UENTRY
movl CX(EXT(kernel_stack),%edx),%ebx
xchgl %ebx,%esp /* switch to kernel stack */
/* user regs pointer already set */
_take_trap:
pushl %ebx /* pass register save area to trap */
call EXT(user_trap) /* call user trap routine */
movl 4(%esp),%esp /* switch back to PCB stack */
orl %eax,%eax /* emulated syscall? */
jz _return_from_trap /* no, just return */
movl R_EAX(%ebx),%eax /* yes, get syscall number */
jmp syscall_entry_3 /* and emulate it */
/*
* Return from trap or system call, checking for ASTs.
* On PCB stack.
*/
_return_from_trap:
CPU_NUMBER(%edx)
cmpl $0,CX(EXT(need_ast),%edx)
jz _return_to_user /* if we need an AST: */
movl CX(EXT(kernel_stack),%edx),%esp
/* switch to kernel stack */
call EXT(i386_astintr) /* take the AST */
popl %esp /* switch back to PCB stack */
jmp _return_from_trap /* and check again (rare) */
/* ASTs after this point will */
/* have to wait */
_return_to_user:
TIME_TRAP_UEXIT
/*
* Return from kernel mode to interrupted thread.
*/
_return_from_kernel:
_kret_popl_gs:
popl %gs /* restore segment registers */
_kret_popl_fs:
popl %fs
_kret_popl_es:
popl %es
_kret_popl_ds:
popl %ds
popa /* restore general registers */
addl $8,%esp /* discard trap number and error code */
_kret_iret:
iret /* return from interrupt */
/*
* Trap from kernel mode. No need to switch stacks.
*/
trap_from_kernel:
#if MACH_KDB || MACH_TTD
movl %esp,%ebx /* save current stack */
cmpl EXT(int_stack_high),%esp /* on an interrupt stack? */
jb 1f /* OK if so */
CPU_NUMBER(%edx) /* get CPU number */
cmpl CX(EXT(kernel_stack),%edx),%esp
/* already on kernel stack? */
ja 0f
cmpl CX(EXT(active_stacks),%edx),%esp
ja 1f /* switch if not */
0:
movl CX(EXT(kernel_stack),%edx),%esp
1:
pushl %ebx /* save old stack */
pushl %ebx /* pass as parameter */
call EXT(kernel_trap) /* to kernel trap routine */
addl $4,%esp /* pop parameter */
popl %esp /* return to old stack */
#else /* MACH_KDB || MACH_TTD */
pushl %esp /* pass parameter */
call EXT(kernel_trap) /* to kernel trap routine */
addl $4,%esp /* pop parameter */
#endif /* MACH_KDB || MACH_TTD */
jmp _return_from_kernel
/*
* Called as a function, makes the current thread
* return from the kernel as if from an exception.
*/
ENTRY(thread_exception_return)
ENTRY(thread_bootstrap_return)
movl %esp,%ecx /* get kernel stack */
or $(KERNEL_STACK_SIZE-1),%ecx
movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
jmp _return_from_trap
/*
* Called as a function, makes the current thread
* return from the kernel as if from a syscall.
* Takes the syscall's return code as an argument.
*/
ENTRY(thread_syscall_return)
movl S_ARG0,%eax /* get return value */
movl %esp,%ecx /* get kernel stack */
or $(KERNEL_STACK_SIZE-1),%ecx
movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
movl %eax,R_EAX(%esp) /* save return value */
jmp _return_from_trap
ENTRY(call_continuation)
movl S_ARG0,%eax /* get continuation */
movl %esp,%ecx /* get kernel stack */
or $(KERNEL_STACK_SIZE-1),%ecx
addl $(-3-IKS_SIZE),%ecx
movl %ecx,%esp /* pop the stack */
xorl %ebp,%ebp /* zero frame pointer */
jmp *%eax /* goto continuation */
#define INTERRUPT(n) \
.data 2 ;\
.long 0f ;\
.text ;\
P2ALIGN(TEXT_ALIGN) ;\
0: ;\
pushl %eax ;\
movl $(n),%eax ;\
jmp EXT(all_intrs)
.data 2
DATA(int_entry_table)
.text
INTERRUPT(0)
INTERRUPT(1)
INTERRUPT(2)
INTERRUPT(3)
INTERRUPT(4)
INTERRUPT(5)
INTERRUPT(6)
INTERRUPT(7)
INTERRUPT(8)
INTERRUPT(9)
INTERRUPT(10)
INTERRUPT(11)
INTERRUPT(12)
INTERRUPT(13)
INTERRUPT(14)
INTERRUPT(15)
/* XXX handle NMI - at least print a warning like Linux does. */
/*
* All interrupts enter here.
* old %eax on stack; interrupt number in %eax.
*/
ENTRY(all_intrs)
pushl %ecx /* save registers */
pushl %edx
cld /* clear direction flag */
cmpl %ss:EXT(int_stack_high),%esp /* on an interrupt stack? */
jb int_from_intstack /* if not: */
pushl %ds /* save segment registers */
pushl %es
mov %ss,%dx /* switch to kernel segments */
mov %dx,%ds
mov %dx,%es
CPU_NUMBER(%edx)
movl CX(EXT(int_stack_top),%edx),%ecx
xchgl %ecx,%esp /* switch to interrupt stack */
#if STAT_TIME
pushl %ecx /* save pointer to old stack */
#else
pushl %ebx /* save %ebx - out of the way */
/* so stack looks the same */
pushl %ecx /* save pointer to old stack */
TIME_INT_ENTRY /* do timing */
#endif
call EXT(interrupt) /* call generic interrupt routine */
.globl EXT(return_to_iret)
LEXT(return_to_iret) /* ( label for kdb_kintr and hardclock) */
CPU_NUMBER(%edx)
#if STAT_TIME
#else
TIME_INT_EXIT /* do timing */
movl 4(%esp),%ebx /* restore the extra reg we saved */
#endif
popl %esp /* switch back to old stack */
testl $(EFL_VM),I_EFL(%esp) /* if in V86 */
jnz 0f /* or */
testb $3,I_CS(%esp) /* user mode, */
jz 1f /* check for ASTs */
0:
cmpl $0,CX(EXT(need_ast),%edx)
jnz ast_from_interrupt /* take it if so */
1:
pop %es /* restore segment regs */
pop %ds
pop %edx
pop %ecx
pop %eax
iret /* return to caller */
int_from_intstack:
call EXT(interrupt) /* call interrupt routine */
_return_to_iret_i: /* ( label for kdb_kintr) */
pop %edx /* must have been on kernel segs */
pop %ecx
pop %eax /* no ASTs */
iret
/*
* Take an AST from an interrupt.
* On PCB stack.
* sp-> es -> edx
* ds -> ecx
* edx -> eax
* ecx -> trapno
* eax -> code
* eip
* cs
* efl
* esp
* ss
*/
ast_from_interrupt:
pop %es /* restore all registers ... */
pop %ds
popl %edx
popl %ecx
popl %eax
pushl $0 /* zero code */
pushl $0 /* zero trap number */
pusha /* save general registers */
push %ds /* save segment registers */
push %es
push %fs
push %gs
mov %ss,%dx /* switch to kernel segments */
mov %dx,%ds
mov %dx,%es
CPU_NUMBER(%edx)
TIME_TRAP_UENTRY
movl CX(EXT(kernel_stack),%edx),%esp
/* switch to kernel stack */
call EXT(i386_astintr) /* take the AST */
popl %esp /* back to PCB stack */
jmp _return_from_trap /* return */
#if MACH_KDB
/*
* kdb_kintr: enter kdb from keyboard interrupt.
* Chase down the stack frames until we find one whose return
* address is the interrupt handler. At that point, we have:
*
* frame-> saved %ebp
* return address in interrupt handler
* iunit
* [ PS2 - saved interrupt number ]
* saved SPL
* return address == return_to_iret_i
* saved %edx
* saved %ecx
* saved %eax
* saved %eip
* saved %cs
* saved %efl
*
* OR:
* frame-> saved %ebp
* return address in interrupt handler
* iunit
* [ PS2 - saved interrupt number ]
* saved SPL
* return address == return_to_iret
* pointer to save area on old stack
* [ saved %ebx, if accurate timing ]
*
* old stack: saved %es
* saved %ds
* saved %edx
* saved %ecx
* saved %eax
* saved %eip
* saved %cs
* saved %efl
*
* Call kdb, passing it that register save area.
*/
#ifdef PS2
#define RET_OFFSET 20
#else /* not PS2 */
#define RET_OFFSET 16
#endif /* PS2 */
ENTRY(kdb_kintr)
movl %ebp,%eax /* save caller`s frame pointer */
movl $EXT(return_to_iret),%ecx /* interrupt return address 1 */
movl $_return_to_iret_i,%edx /* interrupt return address 2 */
0: cmpl RET_OFFSET(%eax),%ecx /* does this frame return to */
/* interrupt handler (1)? */
je 1f
cmpl RET_OFFSET(%eax),%edx /* interrupt handler (2)? */
je 2f /* if not: */
movl (%eax),%eax /* try next frame */
jmp 0b
1: movl $kdb_from_iret,RET_OFFSET(%eax)
ret /* returns to kernel/user stack */
2: movl $kdb_from_iret_i,RET_OFFSET(%eax)
/* returns to interrupt stack */
ret
/*
* On return from keyboard interrupt, we will execute
* kdb_from_iret_i
* if returning to an interrupt on the interrupt stack
* kdb_from_iret
* if returning to an interrupt on the user or kernel stack
*/
kdb_from_iret:
/* save regs in known locations */
#if STAT_TIME
pushl %ebx /* caller`s %ebx is in reg */
#else
movl 4(%esp),%eax /* get caller`s %ebx */
pushl %eax /* push on stack */
#endif
pushl %ebp
pushl %esi
pushl %edi
push %fs
push %gs
pushl %esp /* pass regs */
call EXT(kdb_kentry) /* to kdb */
addl $4,%esp /* pop parameters */
pop %gs /* restore registers */
pop %fs
popl %edi
popl %esi
popl %ebp
#if STAT_TIME
popl %ebx
#else
popl %eax
movl %eax,4(%esp)
#endif
jmp EXT(return_to_iret) /* normal interrupt return */
kdb_from_iret_i: /* on interrupt stack */
pop %edx /* restore saved registers */
pop %ecx
pop %eax
pushl $0 /* zero error code */
pushl $0 /* zero trap number */
pusha /* save general registers */
push %ds /* save segment registers */
push %es
push %fs
push %gs
pushl %esp /* pass regs, */
pushl $0 /* code, */
pushl $-1 /* type to kdb */
call EXT(kdb_trap)
addl $12,%esp /* remove parameters */
pop %gs /* restore segment registers */
pop %fs
pop %es
pop %ds
popa /* restore general registers */
addl $8,%esp
iret
#endif /* MACH_KDB */
#if MACH_TTD
/*
* Same code as that above for the keyboard entry into kdb.
*/
ENTRY(kttd_intr)
movl %ebp,%eax /* save caller`s frame pointer */
movl $EXT(return_to_iret),%ecx /* interrupt return address 1 */
movl $_return_to_iret_i,%edx /* interrupt return address 2 */
0: cmpl 16(%eax),%ecx /* does this frame return to */
/* interrupt handler (1)? */
je 1f
cmpl 16(%eax),%edx /* interrupt handler (2)? */
je 2f /* if not: */
movl (%eax),%eax /* try next frame */
jmp 0b
1: movl $ttd_from_iret,16(%eax) /* returns to kernel/user stack */
ret
2: movl $ttd_from_iret_i,16(%eax)
/* returns to interrupt stack */
ret
/*
* On return from keyboard interrupt, we will execute
* ttd_from_iret_i
* if returning to an interrupt on the interrupt stack
* ttd_from_iret
* if returning to an interrupt on the user or kernel stack
*/
ttd_from_iret:
/* save regs in known locations */
#if STAT_TIME
pushl %ebx /* caller`s %ebx is in reg */
#else
movl 4(%esp),%eax /* get caller`s %ebx */
pushl %eax /* push on stack */
#endif
pushl %ebp
pushl %esi
pushl %edi
push %fs
push %gs
pushl %esp /* pass regs */
call _kttd_netentry /* to kdb */
addl $4,%esp /* pop parameters */
pop %gs /* restore registers */
pop %fs
popl %edi
popl %esi
popl %ebp
#if STAT_TIME
popl %ebx
#else
popl %eax
movl %eax,4(%esp)
#endif
jmp EXT(return_to_iret) /* normal interrupt return */
ttd_from_iret_i: /* on interrupt stack */
pop %edx /* restore saved registers */
pop %ecx
pop %eax
pushl $0 /* zero error code */
pushl $0 /* zero trap number */
pusha /* save general registers */
push %ds /* save segment registers */
push %es
push %fs
push %gs
pushl %esp /* pass regs, */
pushl $0 /* code, */
pushl $-1 /* type to kdb */
call _kttd_trap
addl $12,%esp /* remove parameters */
pop %gs /* restore segment registers */
pop %fs
pop %es
pop %ds
popa /* restore general registers */
addl $8,%esp
iret
#endif /* MACH_TTD */
/*
* System call enters through a call gate. Flags are not saved -
* we must shuffle stack to look like trap save area.
*
* esp-> old eip
* old cs
* old esp
* old ss
*
* eax contains system call number.
*/
ENTRY(syscall)
syscall_entry:
pushf /* save flags as soon as possible */
syscall_entry_2:
pushl %eax /* save system call number */
pushl $0 /* clear trap number slot */
pusha /* save the general registers */
pushl %ds /* and the segment registers */
pushl %es
pushl %fs
pushl %gs
mov %ss,%dx /* switch to kernel data segment */
mov %dx,%ds
mov %dx,%es
mov %dx,%fs
mov %dx,%gs
/*
* Shuffle eflags,eip,cs into proper places
*/
movl R_EIP(%esp),%ebx /* eflags are in EIP slot */
movl R_CS(%esp),%ecx /* eip is in CS slot */
movl R_EFLAGS(%esp),%edx /* cs is in EFLAGS slot */
movl %ecx,R_EIP(%esp) /* fix eip */
movl %edx,R_CS(%esp) /* fix cs */
movl %ebx,R_EFLAGS(%esp) /* fix eflags */
CPU_NUMBER(%edx)
TIME_TRAP_SENTRY
movl CX(EXT(kernel_stack),%edx),%ebx
/* get current kernel stack */
xchgl %ebx,%esp /* switch stacks - %ebx points to */
/* user registers. */
/* user regs pointer already set */
/*
* Check for MACH or emulated system call
*/
syscall_entry_3:
movl CX(EXT(active_threads),%edx),%edx
/* point to current thread */
movl TH_TASK(%edx),%edx /* point to task */
movl TASK_EMUL(%edx),%edx /* get emulation vector */
orl %edx,%edx /* if none, */
je syscall_native /* do native system call */
movl %eax,%ecx /* copy system call number */
subl DISP_MIN(%edx),%ecx /* get displacement into syscall */
/* vector table */
jl syscall_native /* too low - native system call */
cmpl DISP_COUNT(%edx),%ecx /* check range */
jnl syscall_native /* too high - native system call */
movl DISP_VECTOR(%edx,%ecx,4),%edx
/* get the emulation vector */
orl %edx,%edx /* emulated system call if not zero */
jnz syscall_emul
/*
* Native system call.
*/
syscall_native:
negl %eax /* get system call number */
jl mach_call_range /* out of range if it was positive */
cmpl EXT(mach_trap_count),%eax /* check system call table bounds */
jg mach_call_range /* error if out of range */
#if 0 /* debug hack to show the syscall number on the screen */
movb %al,%dl
shrb $4,%dl
orb $0x30,%dl
movb $0x0f,%dh
movw %dx,0xb800a
movb %al,%dl
andb $0xf,%dl
orb $0x30,%dl
movb $0xf,%dh
movw %dx,0xb800c
#endif
shll $4,%eax /* manual indexing */
movl EXT(mach_trap_table)(%eax),%ecx
/* get number of arguments */
jecxz mach_call_call /* skip argument copy if none */
movl R_UESP(%ebx),%esi /* get user stack pointer */
lea 4(%esi,%ecx,4),%esi /* skip user return address, */
/* and point past last argument */
movl $USER_DS,%edx /* use user data segment for accesses */
mov %dx,%fs
movl %esp,%edx /* save kernel ESP for error recovery */
0: subl $4,%esi
RECOVER(mach_call_addr_push)
pushl %fs:(%esi) /* push argument on stack */
loop 0b /* loop for all arguments */
mach_call_call:
#ifdef DEBUG
testb $0xff,EXT(syscall_trace)
jz 0f
pushl %eax
call EXT(syscall_trace_print)
/* will return with syscallofs still (or again) in eax */
addl $4,%esp
0:
#endif /* DEBUG */
call *EXT(mach_trap_table)+4(%eax)
/* call procedure */
movl %esp,%ecx /* get kernel stack */
or $(KERNEL_STACK_SIZE-1),%ecx
movl -3-IKS_SIZE(%ecx),%esp /* switch back to PCB stack */
movl %eax,R_EAX(%esp) /* save return value */
jmp _return_from_trap /* return to user */
/*
* Address out of range. Change to page fault.
* %esi holds failing address.
*/
mach_call_addr_push:
movl %edx,%esp /* clean parameters from stack */
mach_call_addr:
movl %esi,R_CR2(%ebx) /* set fault address */
movl $(T_PAGE_FAULT),R_TRAPNO(%ebx)
/* set page-fault trap */
movl $(T_PF_USER),R_ERR(%ebx)
/* set error code - read user space */
jmp _take_trap /* treat as a trap */
/*
* System call out of range. Treat as invalid-instruction trap.
* (? general protection?)
*/
mach_call_range:
movl $(T_INVALID_OPCODE),R_TRAPNO(%ebx)
/* set invalid-operation trap */
movl $0,R_ERR(%ebx) /* clear error code */
jmp _take_trap /* treat as a trap */
/*
* User space emulation of system calls.
* edx - user address to handle syscall
*
* User stack will become:
* uesp-> eflags
* eip
* eax still contains syscall number.
*/
syscall_emul:
movl $USER_DS,%edi /* use user data segment for accesses */
mov %di,%fs
/* XXX what about write-protected pages? */
movl R_UESP(%ebx),%edi /* get user stack pointer */
subl $8,%edi /* push space for new arguments */
movl R_EFLAGS(%ebx),%eax /* move flags */
RECOVER(syscall_addr)
movl %eax,%fs:0(%edi) /* to user stack */
movl R_EIP(%ebx),%eax /* move eip */
RECOVER(syscall_addr)
movl %eax,%fs:4(%edi) /* to user stack */
movl %edi,R_UESP(%ebx) /* set new user stack pointer */
movl %edx,R_EIP(%ebx) /* change return address to trap */
movl %ebx,%esp /* back to PCB stack */
jmp _return_from_trap /* return to user */
/*
* Address error - address is in %edi.
*/
syscall_addr:
movl %edi,R_CR2(%ebx) /* set fault address */
movl $(T_PAGE_FAULT),R_TRAPNO(%ebx)
/* set page-fault trap */
movl $(T_PF_USER),R_ERR(%ebx)
/* set error code - read user space */
jmp _take_trap /* treat as a trap */
/* Discover what kind of cpu we have; return the family number
(3, 4, 5, 6, for 386, 486, 586, 686 respectively). */
ENTRY(discover_x86_cpu_type)
pushl %ebp /* Save frame pointer */
movl %esp,%ebp /* Save stack pointer */
and $~0x3,%esp /* Align stack pointer */
inb $0xe8,%al /* Enable ID flag for Cyrix CPU ... */
andb $0x80,%al /* ... in CCR4 reg bit7 */
outb %al,$0xe8
pushfl /* Fetch flags ... */
popl %eax /* ... into eax */
movl %eax,%ecx /* Save original flags for return */
xorl $(EFL_AC+EFL_ID),%eax /* Attempt to toggle ID and AC bits */
pushl %eax /* Save flags... */
popfl /* ... In EFLAGS */
pushfl /* Fetch flags back ... */
popl %eax /* ... into eax */
pushl %ecx /* From ecx... */
popfl /* ... restore original flags */
xorl %ecx,%eax /* See if any bits didn't change */
testl $EFL_AC,%eax /* Test AC bit */
jnz 0f /* Skip next bit if AC toggled */
movl $3,%eax /* Return value is 386 */
jmp 9f /* And RETURN */
0: testl $EFL_ID,%eax /* Test ID bit */
jnz 0f /* Skip next bit if ID toggled */
movl $4,%eax /* Return value is 486 */
jmp 9f /* And RETURN */
/* We are a modern enough processor to have the CPUID instruction;
use it to find out what we are. */
0: movl $1,%eax /* Fetch CPU type info ... */
cpuid /* ... into eax */
shrl $8,%eax /* Slide family bits down */
andl $15,%eax /* And select them */
9: movl %ebp,%esp /* Restore stack pointer */
popl %ebp /* Restore frame pointer */
ret /* And return */
/**/
/*
* Utility routines.
*/
/*
* Copy from user address space.
* arg0: user address
* arg1: kernel address
* arg2: byte count
*/
ENTRY(copyin)
Entry(copyinmsg)
pushl %esi
pushl %edi /* save registers */
movl 8+S_ARG0,%esi /* get user start address */
movl 8+S_ARG1,%edi /* get kernel destination address */
movl 8+S_ARG2,%edx /* get count */
movl $USER_DS,%eax /* use user data segment for accesses */
mov %ax,%ds
/*cld*/ /* count up: default mode in all GCC code */
movl %edx,%ecx /* move by longwords first */
shrl $2,%ecx
RECOVER(copyin_fail)
rep
movsl /* move longwords */
movl %edx,%ecx /* now move remaining bytes */
andl $3,%ecx
RECOVER(copyin_fail)
rep
movsb
xorl %eax,%eax /* return 0 for success */
copyin_ret:
mov %ss,%di /* restore DS to kernel segment */
mov %di,%ds
popl %edi /* restore registers */
popl %esi
ret /* and return */
copyin_fail:
movl $1,%eax /* return 1 for failure */
jmp copyin_ret /* pop frame and return */
/*
* Copy to user address space.
* arg0: kernel address
* arg1: user address
* arg2: byte count
*/
ENTRY(copyout)
Entry(copyoutmsg)
pushl %esi
pushl %edi /* save registers */
movl 8+S_ARG0,%esi /* get kernel start address */
movl 8+S_ARG1,%edi /* get user start address */
movl 8+S_ARG2,%edx /* get count */
movl $USER_DS,%eax /* use user data segment for accesses */
mov %ax,%es
/*
* Check whether user address space is writable
* before writing to it - hardware is broken.
* XXX only have to do this on 386's.
*/
copyout_retry:
movl %cr3,%ecx /* point to page directory */
movl %edi,%eax /* get page directory bits */
shrl $(PDESHIFT),%eax /* from user address */
movl KERNELBASE(%ecx,%eax,4),%ecx
/* get page directory pointer */
testl $(PTE_V),%ecx /* present? */
jz 0f /* if not, fault is OK */
andl $(PTE_PFN),%ecx /* isolate page frame address */
movl %edi,%eax /* get page table bits */
shrl $(PTESHIFT),%eax
andl $(PTEMASK),%eax /* from user address */
leal KERNELBASE(%ecx,%eax,4),%ecx
/* point to page table entry */
movl (%ecx),%eax /* get it */
testl $(PTE_V),%eax /* present? */
jz 0f /* if not, fault is OK */
testl $(PTE_W),%eax /* writable? */
jnz 0f /* OK if so */
/*
* Not writable - must fake a fault. Turn off access to the page.
*/
andl $(PTE_INVALID),(%ecx) /* turn off valid bit */
movl %cr3,%eax /* invalidate TLB */
movl %eax,%cr3
0:
/*
* Copy only what fits on the current destination page.
* Check for write-fault again on the next page.
*/
leal NBPG(%edi),%eax /* point to */
andl $(-NBPG),%eax /* start of next page */
subl %edi,%eax /* get number of bytes to that point */
cmpl %edx,%eax /* bigger than count? */
jle 1f /* if so, */
movl %edx,%eax /* use count */
1:
/*cld*/ /* count up: always this way in GCC code */
movl %eax,%ecx /* move by longwords first */
shrl $2,%ecx
RECOVER(copyout_fail)
RETRY(copyout_retry)
rep
movsl
movl %eax,%ecx /* now move remaining bytes */
andl $3,%ecx
RECOVER(copyout_fail)
RETRY(copyout_retry)
rep
movsb /* move */
subl %eax,%edx /* decrement count */
jg copyout_retry /* restart on next page if not done */
xorl %eax,%eax /* return 0 for success */
copyout_ret:
mov %ss,%di /* restore ES to kernel segment */
mov %di,%es
popl %edi /* restore registers */
popl %esi
ret /* and return */
copyout_fail:
movl $1,%eax /* return 1 for failure */
jmp copyout_ret /* pop frame and return */
/* XXX turn the following stubs into inline functions. */
/*
* FPU routines.
*/
/*
* Initialize FPU.
*/
ENTRY(_fninit)
fninit
ret
/*
* Read control word
*/
ENTRY(_fstcw)
pushl %eax /* get stack space */
fstcw (%esp)
popl %eax
ret
/*
* Set control word
*/
ENTRY(_fldcw)
fldcw 4(%esp)
ret
/*
* Read status word
*/
ENTRY(_fnstsw)
xor %eax,%eax /* clear high 16 bits of eax */
fnstsw %ax /* read FP status */
ret
/*
* Clear FPU exceptions
*/
ENTRY(_fnclex)
fnclex
ret
/*
* Clear task-switched flag.
*/
ENTRY(_clts)
clts
ret
/*
* Save complete FPU state. Save error for later.
*/
ENTRY(_fpsave)
movl 4(%esp),%eax /* get save area pointer */
fnsave (%eax) /* save complete state, including */
/* errors */
ret
/*
* Restore FPU state.
*/
ENTRY(_fprestore)
movl 4(%esp),%eax /* get save area pointer */
frstor (%eax) /* restore complete state */
ret
/*
* Set cr3
*/
ENTRY(set_cr3)
movl 4(%esp),%eax /* get new cr3 value */
movl %eax,%cr3 /* load it */
ret
/*
* Read cr3
*/
ENTRY(get_cr3)
movl %cr3,%eax
ret
/*
* Flush TLB
*/
ENTRY(flush_tlb)
movl %cr3,%eax /* flush tlb by reloading CR3 */
movl %eax,%cr3 /* with itself */
ret
/*
* Read cr2
*/
ENTRY(get_cr2)
movl %cr2,%eax
ret
/*
* Read ldtr
*/
ENTRY(get_ldt)
xorl %eax,%eax
sldt %ax
ret
/*
* Set ldtr
*/
ENTRY(set_ldt)
lldt 4(%esp)
ret
/*
* Read task register.
*/
ENTRY(get_tr)
xorl %eax,%eax
str %ax
ret
/*
* Set task register. Also clears busy bit of task descriptor.
*/
ENTRY(set_tr)
movl S_ARG0,%eax /* get task segment number */
subl $8,%esp /* push space for SGDT */
sgdt 2(%esp) /* store GDT limit and base (linear) */
movl 4(%esp),%edx /* address GDT */
movb $(ACC_P|ACC_PL_K|ACC_TSS),5(%edx,%eax)
/* fix access byte in task descriptor */
ltr %ax /* load task register */
addl $8,%esp /* clear stack */
ret /* and return */
/*
* Set task-switched flag.
*/
ENTRY(_setts)
movl %cr0,%eax /* get cr0 */
orl $(CR0_TS),%eax /* or in TS bit */
movl %eax,%cr0 /* set cr0 */
ret
/*
* void outb(unsigned char *io_port,
* unsigned char byte)
*
* Output a byte to an IO port.
*/
ENTRY(outb)
movl S_ARG0,%edx /* IO port address */
movl S_ARG1,%eax /* data to output */
outb %al,%dx /* send it out */
#ifdef iPSC386
mull %ecx /* Delay a little to make H/W happy */
#endif /* iPSC386 */
ret
/*
* unsigned char inb(unsigned char *io_port)
*
* Input a byte from an IO port.
*/
ENTRY(inb)
movl S_ARG0,%edx /* IO port address */
xor %eax,%eax /* clear high bits of register */
inb %dx,%al /* get the byte */
#ifdef iPSC386
/ Do a long multiply to delay a little to make H/W happy. Must
/ save and restore EAX which is used to hold result of multiply
pushl %eax
mull %ecx
popl %eax
#endif /* iPSC386 */
ret
/*
* void outw(unsigned short *io_port,
* unsigned short word)
*
* Output a word to an IO port.
*/
ENTRY(outw)
movl S_ARG0,%edx /* IO port address */
movl S_ARG1,%eax /* data to output */
outw %ax,%dx /* send it out */
ret
/*
* unsigned short inw(unsigned short *io_port)
*
* Input a word from an IO port.
*/
ENTRY(inw)
movl S_ARG0,%edx /* IO port address */
xor %eax,%eax /* clear high bits of register */
inw %dx,%ax /* get the word */
ret
/*
* void outl(unsigned int *io_port,
* unsigned int byte)
*
* Output an int to an IO port.
*/
ENTRY(outl)
movl S_ARG0,%edx /* IO port address */
movl S_ARG1,%eax /* data to output */
outl %eax,%dx /* send it out */
ret
/*
* unsigned int inl(unsigned int *io_port)
*
* Input an int from an IO port.
*/
ENTRY(inl)
movl S_ARG0,%edx /* IO port address */
inl %dx,%eax /* get the int */
ret
/*
* void loutb(unsigned byte *io_port,
* unsigned byte *data,
* unsigned int count)
*
* Output an array of bytes to an IO port.
*/
ENTRY(loutb)
movl %esi,%eax /* save register */
movl S_ARG0,%edx /* get io port number */
movl S_ARG1,%esi /* get data address */
movl S_ARG2,%ecx /* get count */
cld /* count up */
rep
outsb /* output */
movl %eax,%esi /* restore register */
ret /* exit */
/*
* void loutw(unsigned short *io_port,
* unsigned short *data,
* unsigned int count)
*
* Output an array of shorts to an IO port.
*/
ENTRY(loutw)
movl %esi,%eax /* save register */
movl S_ARG0,%edx /* get io port number */
movl S_ARG1,%esi /* get data address */
movl S_ARG2,%ecx /* get count */
cld /* count up */
rep
outsw /* output */
movl %eax,%esi /* restore register */
ret /* exit */
/*
* void linb(unsigned char *io_port,
* unsigned char *data,
* unsigned int count)
*
* Input an array of bytes from an IO port.
*/
ENTRY(linb)
movl %edi,%eax /* save register */
movl S_ARG0,%edx /* get io port number */
movl S_ARG1,%edi /* get data address */
movl S_ARG2,%ecx /* get count */
cld /* count up */
rep
insb /* input */
movl %eax,%edi /* restore register */
ret /* exit */
/*
* void linw(unsigned short *io_port,
* unsigned short *data,
* unsigned int count)
*
* Input an array of shorts from an IO port.
*/
ENTRY(linw)
movl %edi,%eax /* save register */
movl S_ARG0,%edx /* get io port number */
movl S_ARG1,%edi /* get data address */
movl S_ARG2,%ecx /* get count */
cld /* count up */
rep
insw /* input */
movl %eax,%edi /* restore register */
ret /* exit */
/*
* int inst_fetch(int eip, int cs);
*
* Fetch instruction byte. Return -1 if invalid address.
*/
ENTRY(inst_fetch)
movl S_ARG1, %eax /* get segment */
movw %ax,%fs /* into FS */
movl S_ARG0, %eax /* get offset */
RETRY(EXT(inst_fetch)) /* re-load FS on retry */
RECOVER(_inst_fetch_fault)
movzbl %fs:(%eax),%eax /* load instruction byte */
ret
_inst_fetch_fault:
movl $-1,%eax /* return -1 if error */
ret
/*
* Done with recovery and retry tables.
*/
RECOVER_TABLE_END
RETRY_TABLE_END
ENTRY(dr6)
movl %db6, %eax
ret
/* dr<i>(address, type, len, persistence)
*/
ENTRY(dr0)
movl S_ARG0, %eax
movl %eax,EXT(dr_addr)
movl %eax, %db0
movl $0, %ecx
jmp 0f
ENTRY(dr1)
movl S_ARG0, %eax
movl %eax,EXT(dr_addr)+1*4
movl %eax, %db1
movl $2, %ecx
jmp 0f
ENTRY(dr2)
movl S_ARG0, %eax
movl %eax,EXT(dr_addr)+2*4
movl %eax, %db2
movl $4, %ecx
jmp 0f
ENTRY(dr3)
movl S_ARG0, %eax
movl %eax,EXT(dr_addr)+3*4
movl %eax, %db3
movl $6, %ecx
0:
pushl %ebp
movl %esp, %ebp
movl %db7, %edx
movl %edx,EXT(dr_addr)+4*4
andl dr_msk(,%ecx,2),%edx /* clear out new entry */
movl %edx,EXT(dr_addr)+5*4
movzbl B_ARG3, %eax
andb $3, %al
shll %cl, %eax
orl %eax, %edx
movzbl B_ARG1, %eax
andb $3, %al
addb $0x10, %cl
shll %cl, %eax
orl %eax, %edx
movzbl B_ARG2, %eax
andb $3, %al
addb $0x2, %cl
shll %cl, %eax
orl %eax, %edx
movl %edx, %db7
movl %edx,EXT(dr_addr)+7*4
movl %edx, %eax
leave
ret
.data
dr_msk:
.long ~0x000f0003
.long ~0x00f0000c
.long ~0x0f000030
.long ~0xf00000c0
ENTRY(dr_addr)
.long 0,0,0,0
.long 0,0,0,0
.text
/*
* cpu_shutdown()
* Force reboot
*/
null_idt:
.space 8 * 32
null_idtr:
.word 8 * 32 - 1
.long null_idt
Entry(cpu_shutdown)
lidt null_idtr /* disable the interrupt handler */
xor %ecx,%ecx /* generate a divide by zero */
div %ecx,%eax /* reboot now */
ret /* this will "never" be executed */
/*
* Allocate enough space for a kernel TSS with a complete I/O bitmap,
* for making v86-mode BIOS calls. XXX
*/
.data
.globl EXT(ktss)
.comm EXT(ktss),0x68+65536/8+1
|