summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/bootsectors/bootsector2-common-routines-template-1.mac
blob: bc8802c4ab0d0d3a788c2cdd592a69dc3f599663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
; $Id: bootsector2-common-routines-template-1.mac $
;; @file
; bootsector2 common routines - template containing code common to related modes.
;

;
; Copyright (C) 2007-2023 Oracle and/or its affiliates.
;
; This file is part of VirtualBox base platform packages, as
; available from https://www.virtualbox.org.
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation, in version 3 of the
; License.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, see <https://www.gnu.org/licenses>.
;
; The contents of this file may alternatively be used under the terms
; of the Common Development and Distribution License Version 1.0
; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
; in the VirtualBox distribution, in which case the provisions of the
; CDDL are applicable instead of those of the GPL.
;
; You may elect to license modified versions of this file under the
; terms and conditions of either the GPL or the CDDL or both.
;
; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
;

%include "bootsector2-template-header.mac"
%include "VBox/bios.mac"

ALIGNCODE(32)
GLOBALNAME TMPL_NM_CMN(g_szMode)
        db TMPL_MODE_STR, 0


;;
; Shutdown routine.
;
; Does not return.
;
; @uses     N/A
;
BEGINPROC TMPL_NM_CMN(Shutdown)
%ifdef TMPL_16BIT
        jmp     Bs2Shutdown
%else
        cli
        mov     bl, 64
        mov     ax, ss
        mov     ds, ax
        mov     dx, VBOX_BIOS_SHUTDOWN_PORT
        mov     ax, VBOX_BIOS_OLD_SHUTDOWN_PORT
.retry:
        mov     ecx, 8
        mov     esi, .s_szShutdown
        rep outsb
        xchg    dx, ax                  ; alternate between the new (VBox) and old (Bochs) ports.
        dec     bl
        jnz     .retry
        ; Shutdown failed!
        jmp     Bs2Panic
.s_szShutdown:
        db      'Shutdown', 0
%endif
ENDPROC   TMPL_NM_CMN(Shutdown)


;;
; Prints a 32-bit unsigned integer on the screen.
;
; @param    eax         The value to print.
;
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(PrintU32)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sDX
        push    sCX
        push    sBX
%ifdef TMPL_16BIT
        push    ds

        mov     cx, ss
        mov     ds, cx
%endif

        ; Allocate a stack buffer and terminate it. ds:bx points ot the end.
        sub     xSP, 30h
        mov     xBX, xSP
        add     xBX, 2fh
        mov     byte [xBX], 0

        mov     ecx, 10                 ; what to divide by
.next:
        xor     edx, edx
        div     ecx                     ; edx:eax / ecx -> eax and rest in edx.
        add     dl, '0'
        dec     xBX
        mov     [xBX], dl
        cmp     eax, 0
        jnz     .next

        ; Print the string.
        mov     xAX, xBX
        call    TMPL_NM_CMN(PrintStr)

        add     xSP, 30h
%ifdef TMPL_16BIT
        pop     ds
%endif
        pop     sBX
        pop     sCX
        pop     sDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(PrintU32)


;;
; Equivalent to RTPrintf, but a max output length of 1KB.
;
; @remarks  This uses an entirely STACK BASED CALLING CONVENTION where the
;           caller does the cleanup (cdecl sans volatile regs).
;
; @param    fpszFormat          The format string (far pointer on 16-bit
;                               systems). See StrFormatV for format details.
; @param    ...                 Zero or more format string arguments.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(PrintF)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX
        push    xCX
        push    xBX
%ifdef TMPL_16BIT
        push    ds
        push    es
        push    fs
%endif
        sub     xSP, 0400h              ; string buffer (1024 bytes)

        ;
        ; Format the failure string and call TestFailed.
        ;
%ifdef TMPL_16BIT
        mov     ax, ss                  ; buffer address.
        mov     ds, ax
        mov     ax, sp
        mov     xDX, 0400h              ; buffer size.
        les     cx, [bp + 4]            ; format string
        mov     bx, ss                  ; argument list
        mov     fs, bx
        mov     bx, bp
        add     bx, 8
%else
        mov     xAX, xSP                ; buffer address
        mov     xDX, 0400h              ; buffer size
        mov     xCX, [xBP + xCB * 2]    ; format string
        lea     xBX, [xBP + xCB * 3]    ; argument list.
%endif
        call    TMPL_NM_CMN(StrFormatV)
        call    TMPL_NM_CMN(PrintStr)

        add     xSP, 0400h
%ifdef TMPL_16BIT
        pop     fs
        pop     es
        pop     ds
%endif
        pop     xBX
        pop     xCX
        pop     xDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(PrintF)


;;
; Print a string followed by a semicolon and at least one space.
;
; @param    ds:ax       The string to print.
; @param    dx          The desired minimum length of the output. That is
;                       string + colon + spaces.
; @uses     nothing.
;
BEGINPROC TMPL_NM_CMN(PrintStrColonSpaces)
        push    xBP
        mov     xBP, xSP
        push    xAX
        push    xCX

        call    TMPL_NM_CMN(PrintStr)
        call    TMPL_NM_CMN(StrLen)
        mov     cx, ax
        mov     al, ':'
        call    TMPL_NM_CMN(PrintChr)
        inc     cx
        mov     al, ' '
.next_space:
        call    TMPL_NM_CMN(PrintChr)
        inc     cx
        cmp     cx, dx
        jb      .next_space

        pop     xCX
        pop     xAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(PrintStrColonSpaces)


;;
; Print a string followed by a 0+ spaces, a semicolon and a space.
;
; @param    ds:ax       The string to print.
; @param    dx          The desired minimum length of the output. That is
;                       string + spaces + colon + space.
; @uses     nothing.
;
BEGINPROC TMPL_NM_CMN(PrintStrSpacesColonSpace)
        push    xBP
        mov     xBP, xSP
        push    xAX
        push    xCX

        call    TMPL_NM_CMN(PrintStr)
        call    TMPL_NM_CMN(StrLen)
        mov     cx, ax
        inc     cx
        mov     al, ' '
.next_space:
        inc     cx
        cmp     cx, dx
        jae     .done_spaces
        call    TMPL_NM_CMN(PrintChr)
        jmp     .next_space
.done_spaces:
        mov     al, ':'
        call    TMPL_NM_CMN(PrintChr)
        mov     al, ' '
        call    TMPL_NM_CMN(PrintChr)

        pop     xCX
        pop     xAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(PrintStrSpacesColonSpace)


;;
; Store the current nanosecond timestamp in [ax] (qword).
;
; @param    ds:ax       Where to store the 64-bit timestamp.
;
; @uses nothing
;
BEGINPROC TMPL_NM_CMN(GetNanoTS)
        push    sAX
        push    sCX
        push    xDX
%ifdef TMPL_16BIT
        movzx   ecx, ax
%else
        mov     xCX, xAX
%endif

        mov     dx, VMMDEV_TESTING_IOPORT_TS_LOW
        in      eax, dx
        mov     [sCX], eax

        mov     dx, VMMDEV_TESTING_IOPORT_TS_HIGH
        in      eax, dx
        mov     [sCX + 4], eax

        pop     xDX
        pop     sCX
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(GetNanoTS)



;;
; Calculates the time elapsed since [ax] (qword), storing it at [ax] (qword).
;
; @param    ds:ax       Where to get the start timestamp (input) and where
;                       to store the time elapsed (output). qword
;
; @uses nothing
;
BEGINPROC TMPL_NM_CMN(GetElapsedNanoTS)
        push    sAX
        push    sCX
        push    xDX
%ifdef TMPL_16BIT
        movzx   ecx, ax
%else
        mov     xCX, xAX
%endif

        mov     dx, VMMDEV_TESTING_IOPORT_TS_LOW
        in      eax, dx
        sub     eax, [sCX]
        mov     [sCX], eax

        mov     dx, VMMDEV_TESTING_IOPORT_TS_HIGH
        in      eax, dx
        sbb     eax, [sCX + 4]
        mov     [sCX + 4], eax

        pop     xDX
        pop     sCX
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(GetElapsedNanoTS)


;;
; Sends a command to VMMDev followed by a single string.
;
; If the VMMDev is not present or is not being used, this function will
; do nothing.
;
; @param    eax         The command.
; @param    ds:dx       The string (zero terminated).
; @uses     nothing
; @internal
;
BEGINPROC TMPL_NM_CMN(testSendStrCmd)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xBX
        push    xDX

        cmp     byte [g_fbBs2VMMDevTesting], 0
        je      .no_vmmdev

        mov     dx, VMMDEV_TESTING_IOPORT_CMD
        out     dx, eax

        mov     dx, VMMDEV_TESTING_IOPORT_DATA
        pop     xBX
        push    xBX
        dec     xBX
.next_char:
        inc     xBX
        mov     al, [xBX]
        out     dx, al
        test    al, al
        jnz     .next_char

.no_vmmdev:
        pop     xDX
        pop     xBX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(testSendStrCmd)


;;
; Equivalent to RTTestCreate + RTTestBanner
;
; @param    DS16:xAX            Pointer to a zero terminated string naming the
;                               test.  Must be a global constant.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestInit)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX

        ; Initialize the globals.
        mov     [g_npszBs2Test], xAX
        xor     eax, eax
        mov     [g_uscBs2TestErrors], ax
        mov     [g_npszBs2SubTest], eax
        mov     [g_uscBs2SubTestAtErrors], ax
        mov     byte [g_fbBs2SubTestReported], 1
        mov     [g_uscBs2SubTests], ax
        mov     [g_uscBs2SubTestsFailed], ax

        ; Print the name. RTTestBanner
        mov     xAX, [g_npszBs2Test]
        call    TMPL_NM_CMN(PrintStr)
        mov     xAX, .s_szTesting
        call    TMPL_NM_CMN(PrintStr)

        ; Report it to the VMMDev.
        mov     eax, VMMDEV_TESTING_CMD_INIT
        mov     xDX, [g_npszBs2Test]
        call    TMPL_NM_CMN(testSendStrCmd)

        pop     xDX
        pop     sAX
        leave
        ret
.s_szTesting:
        db ': TESTING...', 13, 10, 0
ENDPROC   TMPL_NM_CMN(TestInit)


;;
; rtTestSubTestReport
; @uses     nothing
; @internal
BEGINPROC TMPL_NM_CMN(testSubTestReport)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sCX
        push    xDX

        ; Check if there is anything to do.
        cmp     byte [g_fbBs2SubTestReported], 0
        jne     .done
        xor     xAX, xAX                ; load the sub test name pointer for later
        mov     xAX, [g_npszBs2SubTest]
        test    xAX, xAX
        jz      .done

        ; Start the printing.
        mov     dx, 48
        call    TMPL_NM_CMN(PrintStrSpacesColonSpace)

        mov     byte [g_fbBs2SubTestReported], 1
        mov     cx, [g_uscBs2TestErrors]
        sub     cx, [g_uscBs2SubTestAtErrors]
        and     ecx, 0ffffh
        jnz     .failed

        ; passed
        mov     xAX, .s_szPassed
        call    TMPL_NM_CMN(PrintStr)
        jmp     .vmmdev

        ; failed
.failed:
        mov     xAX, .s_szFailure
        call    TMPL_NM_CMN(PrintStr)
        mov     eax, ecx
        call    TMPL_NM_CMN(PrintU32)
        mov     xAX, .s_szFailureEnd
        call    TMPL_NM_CMN(PrintStr)

        ; report to VMMDev
.vmmdev:
        cmp     byte [g_fbBs2VMMDevTesting], 0
        je      .no_vmmdev

        mov     dx, VMMDEV_TESTING_IOPORT_CMD
        mov     eax, VMMDEV_TESTING_CMD_SUB_DONE
        out     dx, eax

        mov     dx, VMMDEV_TESTING_IOPORT_DATA
        mov     eax, ecx
        out     dx, eax

.no_vmmdev:
.done:
        pop     xDX
        pop     sCX
        pop     sAX
        leave
        ret
.s_szPassed:
        db 'PASSED', 13, 10, 0
.s_szFailure:
        db 'FAILED (', 0
.s_szFailureEnd:
        db ' errors)', 13, 10, 0
ENDPROC   TMPL_NM_CMN(testSubTestReport)


;;
; rtTestSubCleanup
; @uses     nothing
; @internal
BEGINPROC TMPL_NM_CMN(testSubCleanup)
        push    xBP
        mov     xBP, xSP

        cmp     dword [g_npszBs2SubTest], 0
        je      .cleaned_up

        call    TMPL_NM_CMN(testSubTestReport)
        mov     dword [g_npszBs2SubTest], 0
        mov     byte [g_fbBs2SubTestReported], 0

.cleaned_up:
        leave
        ret
ENDPROC   TMPL_NM_CMN(testSubCleanup)


;;
; Equivalent to RTTestSub.
;
; @param    ds:xAX      Pointer to a zero terminated string naming the sub test.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestSub)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX

        ; Complete and cleanup any current sub test.
        call    TMPL_NM_CMN(testSubCleanup)

        ; Start a new sub test.
        inc     word [g_uscBs2SubTests]
        mov     dx, [g_uscBs2TestErrors]
        mov     [g_uscBs2SubTestAtErrors], dx
        mov     [g_npszBs2SubTest], xAX
        mov     byte [g_fbBs2SubTestReported], 0

        ; Report it to the VMMDev.
        mov     xDX, xAX
        mov     eax, VMMDEV_TESTING_CMD_SUB_NEW
        call    TMPL_NM_CMN(testSendStrCmd)

        pop     xDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestSub)


;;
; Calculates the error count for the current sub test.
;
; @returns  ax          Error count for the current sub test.
; @uses     ax
;
BEGINPROC TMPL_NM_CMN(TestSubErrorCount)
        pushf

        mov     ax, [g_uscBs2TestErrors]
        sub     ax, [g_uscBs2SubTestAtErrors]

        popf
        ret
ENDPROC   TMPL_NM_CMN(TestSubErrorCount)



;;
; Equivalent to RTTestValue.
;
; @param    ds:ax       The value name.
; @param    edx         The 32-bit value to report.
; @param    cl          The unit (VMMDEV_TESTING_UNIT_XXX).
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestValueU32)
        push    xBP
        mov     xBP, xSP
        push    sDX
        push    sCX
        push    sAX
        push    sSI
        pushf
        cld

        mov     xSI, xAX                ; xSI = name

        ; Print it.
        mov     dx, 48
        call    TMPL_NM_CMN(PrintStrSpacesColonSpace)
        mov     eax, [xBP - sCB]
        call    TMPL_NM_CMN(PrintU32)
        mov     al, ' '
        call    TMPL_NM_CMN(PrintChr)
        movzx   sAX, cl                 ; ASSUMES correct input.
        mov     edx, eax                ; edx = unit
        shl     xAX, 4                  ; * 16
        add     xAX, g_aszBs2TestUnitNames
        call    TMPL_NM_CMN(PrintStr)
        mov     al, 13
        call    TMPL_NM_CMN(PrintChr)
        mov     al, 10
        call    TMPL_NM_CMN(PrintChr)

        ; Report it to the host.
        cmp     byte [g_fbBs2VMMDevTesting], 0
        je      .no_vmmdev
        mov     ecx, edx                ; ecx = unit

        mov     dx, VMMDEV_TESTING_IOPORT_CMD
        mov     eax, VMMDEV_TESTING_CMD_VALUE
        out     dx, eax

        mov     dx, VMMDEV_TESTING_IOPORT_DATA
        mov     eax, [xBP - sCB]
        out     dx, eax                 ; value - low dword
        xor     eax, eax
        out     dx, eax                 ; value - high dword
        mov     eax, ecx
        out     dx, eax                 ; unit
.next_char:
        lodsb
        out     dx, al
        test    al, al
        jnz     .next_char

.no_vmmdev:
        popf
        pop     sSI
        pop     sAX
        pop     sCX
        pop     sDX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestValueU32)


;;
; RTTestValue + DBGFR3RegNmQueryU64.
;
; @param    ds:ax       The value name and register name separated by a colon.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestValueReg)
        push    xBP
        mov     xBP, xSP
        push    sDX
        push    sAX
        push    sSI
        pushf
        cld

        mov     xSI, xAX                ; xSI = name

        ; Report it to the host.
        cmp     byte [g_fbBs2VMMDevTesting], 0
        je      .no_vmmdev

        mov     dx, VMMDEV_TESTING_IOPORT_CMD
        mov     eax, VMMDEV_TESTING_CMD_VALUE_REG
        out     dx, eax

        mov     dx, VMMDEV_TESTING_IOPORT_DATA
.next_char:
        lodsb
        out     dx, al
        test    al, al
        jnz     .next_char

.no_vmmdev:
        popf
        pop     sSI
        pop     sAX
        pop     sDX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestValueReg)


;;
; Equivalent to RTTestFailed("%s", ds:xAX).
;
; @param    ds:xAX      Failure explanation.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestFailed)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX

        ; Increment the error count.
        inc     word [g_uscBs2TestErrors]

        ; Print failure message.
        call    TMPL_NM_CMN(PrintStr)

        ; Report it to the VMMDev.
        mov     xDX, xAX
        mov     eax, VMMDEV_TESTING_CMD_FAILED
        call    TMPL_NM_CMN(testSendStrCmd)

        pop     xDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestFailed)


;;
; Equivalent to RTTestFailed.
;
; @remarks  This uses an entirely STACK BASED CALLING CONVENTION where the
;           caller does the cleanup (cdecl sans volatile regs).
;
; @param    fpszFormat          The format string (far pointer on 16-bit
;                               systems). See StrFormatV for format details.
; @param    ...                 Zero or more format string arguments.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestFailedF)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX
        push    xCX
        push    xBX
%ifdef TMPL_16BIT
        push    ds
        push    es
        push    fs
%endif
        sub     xSP, 0400h              ; string buffer (1024 bytes)

        ;
        ; Format the failure string and call TestFailed.
        ;
%ifdef TMPL_16BIT
        mov     ax, ss                  ; buffer address.
        mov     ds, ax
        mov     ax, sp
        mov     xDX, 0400h              ; buffer size.
        les     cx, [bp + 4]            ; format string
        mov     bx, ss                  ; argument list
        mov     fs, bx
        mov     bx, bp
        add     bx, 8
%else
        mov     xAX, xSP                ; buffer address
        mov     xDX, 0400h              ; buffer size
        mov     xCX, [xBP + xCB * 2]    ; format string
        lea     xBX, [xBP + xCB * 3]    ; argument list.
%endif
        call    TMPL_NM_CMN(StrFormatV)
        call    TMPL_NM_CMN(TestFailed)

        add     xSP, 0400h
%ifdef TMPL_16BIT
        pop     fs
        pop     es
        pop     ds
%endif
        pop     xBX
        pop     xCX
        pop     xDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestFailedF)


;;
; Equivalent to RTTestSkipped("%s", ds:xAX).
;
; @param    ds:xAX      Explanation.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestSkipped)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xDX

        ; Print reason.
        call    TMPL_NM_CMN(PrintStr)

        ; Report it to the VMMDev.
        mov     xDX, xAX
        mov     eax, VMMDEV_TESTING_CMD_SKIPPED
        call    TMPL_NM_CMN(testSendStrCmd)

        pop     xDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestSkipped)



;;
; Equivalent to RTTestSubDone.
;
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(TestSubDone)
        jmp     TMPL_NM_CMN(testSubCleanup)
ENDPROC   TMPL_NM_CMN(TestSubDone)


;;
; Equivalent to RTTestSummaryAndDestroy, does not return.
;
BEGINPROC TMPL_NM_CMN(TestTerm)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sCX
        push    xDX

        ; Complete and cleanup any current sub test.
        call    TMPL_NM_CMN(testSubCleanup)

        ; Print test summary.
        mov     xAX, [g_npszBs2Test]
        call    TMPL_NM_CMN(PrintStr)

        mov     cx, [g_uscBs2TestErrors]
        and     ecx, 0ffffh
        jnz     .failure

        ; success
        mov     xAX, .s_szSuccess
        call    TMPL_NM_CMN(PrintStr)
        jmp     .vmmdev

        ; failure
.failure:
        mov     xAX, .s_szFailure
        call    TMPL_NM_CMN(PrintStr)
        mov     eax, ecx
        call    TMPL_NM_CMN(PrintU32)
        mov     xAX, .s_szFailureEnd
        call    TMPL_NM_CMN(PrintStr)

        ; report to VMMDev
.vmmdev:
        cmp     byte [g_fbBs2VMMDevTesting], 0
        je      .no_vmmdev

        mov     dx, VMMDEV_TESTING_IOPORT_CMD
        mov     eax, VMMDEV_TESTING_CMD_TERM
        out     dx, eax

        mov     dx, VMMDEV_TESTING_IOPORT_DATA
        mov     eax, ecx
        out     dx, eax
.no_vmmdev:

        ; Shut down the VM by default.
        call    TMPL_NM_CMN(Shutdown)

        pop     xDX
        pop     sCX
        pop     sAX
        leave
        ret
.s_szSuccess:
        db ': SUCCESS', 13, 10, 0
.s_szFailure:
        db ': FAILURE - ', 0
.s_szFailureEnd:
        db ' errors', 13, 10, 0
ENDPROC TMPL_NM_CMN(TestTerm)




;;
; Report a result (elapsed time).
;
; @param    ds:ax       Pointer to the elapsed time.
; @param    edx         The number of tests executed.
; @param    ds:cx       The test description.
;
; @users    nothing
;
BEGINPROC TMPL_NM_CMN(ReportResult)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sDX
        push    xCX

%if 0
        push    sDX
        push    xCX
        push    sDX
        mov     edx, [sAX]
        push    sDX
        mov     edx, [sAX + 4]
        push    sDX
        push    .szDbg
        call    TMPL_NM_CMN(PrintF)
        add     xSP, 4 * sCB + xCB
        pop     sDX
        jmp     .end_debug
.szDbg:
        db 'ReportResult(%RX32.%RX32, %RX32, %s)', 13, 10, 0
.end_debug:
%endif

        call    TMPL_NM_CMN(CalcTestPerSecond)
        mov     edx, eax
        mov     xAX, xCX
        mov     cl, VMMDEV_TESTING_UNIT_INSTRS_PER_SEC
        call    TMPL_NM_CMN(TestValueU32)

        pop     xCX
        pop     sDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(ReportResult)


%ifdef BS2_WITH_TRAPS
;;
; Checks a trap, complains if not the expected one.
;
; @param    al          The expected trap number.
; @param    sDX         The expected trap error code.
; @param    sCX         The expected fault eip.
; @param    sBX         The expected fault address.
; @returns  al=1 and ZF=0 on success.
; @returns  al=0 and ZF=1 on failure
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(TestCheckTrap)
        push    xBP
        mov     xBP, xSP
%define a_u8ExpectedTrapNo      byte  [xBP - xCB]
        push    xAX
%define a_uExpectedErr          sPRE [xBP - xCB - sCB*1]
        push    sDX
%define a_uExpectedFaultPC      sPRE [xBP - xCB - sCB*2]
        push    sCX
%define a_uExpectedFaultAddr    sPRE [xBP - xCB - sCB*3]
        push    sBX

        ; Any traps at all?
        cmp     dword [g_u32cTraps], 0
        jne     .trapped
        mov     xAX, .s_szNoTrap
        jmp     .failed
.trapped:

        ; Exactly one trap.
        cmp     dword [g_u32cTraps], 1
        je      .one_trap
        mov     xAX, .s_szToManyTraps
        jmp     .failed
.one_trap:

        ; The right trap.
        cmp     byte [g_u8LastTrapNo], al
        je      .right_trap_no
        mov     xAX, .s_szWrongTrapNo
        jmp     .failed
.right_trap_no:

        ; The right error code.
        cmp     [g_u64LastTrapErr], sDX
%ifndef TMPL_64BIT
        jne     .bad_err_cd
        cmp     dword [g_u64LastTrapErr + 4], 0
%endif
        je      .right_err_cd
.bad_err_cd:
        mov     xAX, .s_szWrongErrCd
        jmp     .failed
.right_err_cd:

        ; The fault PC.
        cmp     [g_LastTrapRegs + BS2REGS.rip], sCX
%ifndef TMPL_64BIT
        jne     .bad_pc
        cmp     dword [g_LastTrapRegs + BS2REGS.rip + 4], 0
%endif
        je      .right_pc
.bad_pc:
        mov     xAX, .s_szWrongPc
        jmp     .failed
.right_pc:


        ; The fault address (PF only).
        cmp     al, 0eh
        jne     .right_fault_address
        cmp     [g_LastTrapRegs + BS2REGS.cr2], sBX
%ifndef TMPL_64BIT
        jne     .bad_fault_address
        cmp     dword [g_LastTrapRegs + BS2REGS.cr2 + 4], 0
%endif
        je      .right_fault_address
.bad_fault_address:
        mov     xAX, .s_szWrongFaultAddress
        jmp     .failed
.right_fault_address:

        pop     sBX
        pop     sCX
        pop     sDX
        pop     xAX
        leave
        mov     al, 1
        test    al, al
        ret

        ;
        ; Reportfailure
        ;
.failed:
        mov     xDX, xSP                ; save xSP - lazy bird.
        cmp     a_u8ExpectedTrapNo, 0eh
        jne     .not_pf2
%ifndef TMPL_64BIT
        push    dword 0
%endif
        push    a_uExpectedFaultAddr
.not_pf1:
%ifndef TMPL_64BIT
        push    dword 0
%endif
        push    a_uExpectedErr
%ifndef TMPL_64BIT
        push    dword 0
%endif
        push    a_uExpectedFaultPC
        movzx   xBX, a_u8ExpectedTrapNo
        push    xBX
        ; line break
        cmp     a_u8ExpectedTrapNo, 0eh
        jne     .not_pf2
%ifndef TMPL_64BIT
        push    dword [g_LastTrapRegs + BS2REGS.cr2 + 4]
%endif
        push    sPRE [g_LastTrapRegs + BS2REGS.cr2]
.not_pf2:
%ifndef TMPL_64BIT
        push    dword [g_u64LastTrapErr + 4]
%endif
        push    sPRE [g_u64LastTrapErr]
%ifndef TMPL_64BIT
        push    dword [g_LastTrapRegs + BS2REGS.rip + 4]
%endif
        push    sPRE [g_LastTrapRegs + BS2REGS.rip]
        movzx   xBX, byte [g_u8LastTrapNo]
        push    xBX
        push    xAX                     ; msg
        mov     xAX, .s_szFailureMsg
        cmp     a_u8ExpectedTrapNo, 0eh
        jne     .not_pf3
        mov     xAX, .s_szFailurePfMsg
.not_pf3:
        push    xAX                     ; format string
        call    TMPL_NM_CMN(TestFailedF)
        mov     xSP, xDX                ; clean up call frame

.done:
        pop     sBX
        pop     sCX
        pop     sDX
        pop     xAX
        leave
        xor     al, al
        ret

.s_szFailureMsg:
        db '%s', 13, 10
        db ' got trap %RX8 pc=%RX64 err=%RX64', 13, 10
        db ' expected %RX8 pc=%RX64 err=%RX64', 13, 10, 0
.s_szFailurePfMsg:
        db '%s', 13, 10
        db ' got trap %RX8 pc=%RX64 err=%RX64 cr2=%RX64', 13, 10,
        db ' expected %RX8 pc=%RX64 err=%RX64 cr2=%RX64', 13, 10, 0
.s_szNoTrap:
        db 'no traps', 0
.s_szToManyTraps:
        db 'too many traps', 0
.s_szWrongTrapNo:
        db 'wrong trap number', 0
.s_szWrongErrCd:
        db 'wrong error code', 0
.s_szWrongPc:
        db 'wrong xIP', 0
.s_szWrongFaultAddress:
        db 'wrong fault address', 0
%undef a_u8ExpectedTrapNo
%undef a_u32ExpectedErr
%undef a_u32ExpectedFaultPC
%undef a_u32ExpectedFaultAddr
ENDPROC   TMPL_NM_CMN(TestCheckTrap)
%endif ; BS2_WITH_TRAPS


%ifdef BS2_WITH_TRAPS
;;
; Installs the active list of trap records (BS2TRAPREC).
;
; @param    sAX         Flat address of the trap records (BS2TRAPREC).
;                       Assumes that DS is FLAT.
; @param    edx         The number of trap records.
; @param    sCX         Flat image base address, i.e. what BS2TRAPREC.offWhere
;                       is relative to.
; @returns  al=1 and ZF=0 on success.
; @returns  al=0 and ZF=1 on failure
;
; @uses     sAX (return value)
;
BEGINPROC TMPL_NM_CMN(TestInstallTrapRecs)
        push    xBP
        mov     xBP, xSP
        push    sDX
        push    sBX
        push    sCX
        push    sDI
        push    sSI

        ; Make sure the record array is within limits.
        cmp     edx, _4M
        jae     .nok

        ; Scan the trap records.
        mov     sDI, sAX
        mov     esi, edx
        or      esi, esi
        jnz     .ok
.next:
        cmp     dword [sDI + BS2TRAPREC.offWhere], _2G
        jae     .nok

        cmp     dword [sDI + BS2TRAPREC.offResumeAddend], 0
        je      .nok
        cmp     dword [sDI + BS2TRAPREC.offResumeAddend], 0xff
        je      .nok

        cmp     dword [sDI + BS2TRAPREC.u8TrapNo], X86_XCPT_LAST
        ja      .nok

        ; next.
        add     sDI, BS2TRAPREC_size
        dec     esi
        jnz     .next

        ; Set the global variables.
.ok:
        xor     esi, esi
        mov     [g_paTrapRecs + 4], esi
        mov     [g_paTrapRecs], sAX
        mov     [g_cTrapRecs], edx
        mov     [g_iTrapRecLast], esi
        mov     [g_pTrapRecBase + 4], esi
        mov     [g_pTrapRecBase], sCX
        mov     eax, 1
        or      eax, eax

.done:
        pop     sSI
        pop     sDI
        pop     sBX
        pop     sCX
        pop     sDX
        leave
        ret

.nok:
        xor     eax, eax
        jmp     .done
ENDPROC   TMPL_NM_CMN(TestInstallTrapRecs)
%endif ; BS2_WITH_TRAPS


;;
; Calculate the number of tests executed per second.
;
; @param    ds:ax       Pointer to the elapsed time.
; @param    edx         The number of tests executed.
; @returns  The tests per second in eax.
;
; @uses     eax (return value)
;
BEGINPROC TMPL_NM_CMN(CalcTestPerSecond)
        push    xBP
        mov     xBP, xSP
        push    sDX
        push    sCX
%ifdef TMPL_16BIT
        movzx   eax, ax
%endif

        ; Calc NS per test.
        mov     ecx, edx
        cmp     ecx, 0
        jz      .div_zero
        movzx   eax, ax
        mov     edx, [sAX + 4]
        cmp     edx, ecx
        jae     .div_overflow
        mov     eax, [sAX]
        shld    edx, eax, 10
        shl     eax,10
        div     ecx                     ; eax = NS per test

        ; Calc tests per second.
        mov     ecx, eax
        cmp     ecx, 0
        jz      .div_zero
        mov     edx, 0xee
        mov     eax, 0x6b280000         ; 1024G
        div     ecx                     ; eax = tests per second

.done:
        pop     sCX
        pop     sDX
        leave
        ret

.div_zero:
        mov     eax, 0
        jmp     .done

.div_overflow:
        mov     eax, 4242424242
        jmp     .done
ENDPROC   TMPL_NM_CMN(CalcTestPerSecond)


;;
; Calculate the number of iterations for a benchmark based on the time a short
; calibration run too.
;
; @param    ds:xAX      Pointer to the elapsed time.
; @param    edx         The number of tests iterations executed.
; @param    ecx         The desired test length, in seconds.
; @returns  The tests iterations in eax.
;
; @uses     eax (return value)
;
BEGINPROC TMPL_NM_CMN(CalcBenchmarkIterations)
        push    xBP
        mov     xBP, xSP
        push    sDX
        push    sCX
%ifdef TMPL_16BIT
        movzx   eax, ax
%endif

        ; Calc NS per test.
        mov     ecx, edx
        cmp     ecx, 0
        jz      .div_zero
        movzx   eax, ax
        mov     edx, [sAX + 4]
        cmp     edx, ecx
        jae     .div_overflow
        mov     eax, [sAX]
        div     ecx                     ; eax = NS per test

        ; Calc tests per second.
        mov     ecx, eax
        cmp     ecx, 0
        jz      .div_zero
        xor     edx, edx
        mov     eax, 1000000000         ; 1G
        div     ecx                     ; eax = tests per second

        ; Multiply this up to the desired number of seconds.
        mov     edx, [xBP - xCB*2]
        mul     edx
        test    edx, edx
        jnz      .mult_32bit_overflow
        cmp     eax, _64K
        jle     .too_small

.done:
        pop     sCX
        pop     sDX
        leave
        ret

.too_small:
        mov     eax, _64K
        jmp     .done

.mult_32bit_overflow:
        mov     eax, 0ffff0000h
        jmp     .done

.div_zero:
        mov     eax, [xBP - xCB]
        shl     eax, 8
        jmp     .fudge

.div_overflow:
        mov     eax, [xBP - xCB]
        shr     eax, 4
.fudge:
        add     eax, 10
        jmp     .done
ENDPROC   TMPL_NM_CMN(CalcBenchmarkIterations)


;;
; Prints a string on the screen.
;
; @param    ds:ax       The string to find the length of.
; @returns  The string length in ax.
;
; @uses     ax (return value)
;
BEGINPROC TMPL_NM_CMN(StrLen)
        push    xBP
        mov     xBP, xSP
        push    xBX

        mov     xBX, xAX
        dec     xBX
.next:
        inc     xBX
        cmp     byte [xBX], byte 0
        jnz     .next

        xchg    xAX, xBX
        sub     xAX, xBX

        pop     xBX
        leave
        ret
ENDPROC   TMPL_NM_CMN(StrLen)



;;
; Simple string format, taking an argument list.
;
; Implemented:
;       - %RX8
;       - %RX16
;       - %RX32
;       - %RX64
;       - %s
;
; Planned:
;       - %RU8
;       - %RU16
;       - %RU32
;       - %RU64
;       - %RI8
;       - %RI16
;       - %RI32
;       - %RI64
;
; @param        ds:xAX          The buffer address.
; @param        xDX             The buffer size.
; @param        es:xCX          The format string.
; @param        fs:xBX          The argument list.
; @uses         nothing
;
BEGINPROC TMPL_NM_CMN(StrFormatV)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sDX
        push    sCX
        push    sBX
        push    sDI
        push    sSI
        pushf
        cld
%ifdef TMPL_16BIT
        push    ds
        push    es
        push    fs
        push    gs

        mov     si, ds
        mov     di, es
        mov     ds, di
        mov     es, si
        mov     di, ax                  ; es:di    -> output buffer.
        mov     si, cx                  ; ds:si    -> format string.
%define a_pArgs                 [fs:bx]
%define a_pu32ArgsHighDW        dword [fs:bx + 4]
%else
        mov     xDI, xAX                ; (es:)xDI -> output buffer.
        mov     xSI, xCX                ; (ds:)xSI -> format string.
%define a_pArgs                 [xBX]
%define a_pu32ArgsHighDW        dword [xBX + 4]
%endif
        xchg    xCX, xDX                ; xCX=buffer size.

        ;
        ; Make sure we've got space for a terminator char in the output buffer.
        ;
        test    xCX, xCX
        jz      .return
        dec     xCX
        jz      .done

        ;
        ; In this loop we're free to use sDX and (with some caution) sAX.
        ;
.format_loop:
        lodsb
        test    al, al
        jz      .done
        cmp     al, '%'
        je      .switch

        ; Emit the character in al if there is room for it.
.emit_al:
        test    xCX, xCX
        jz      .done
        stosb
        dec     xCX
        jmp     .format_loop

        ; Try recognize the format specifier.
.switch:
        lodsb
        cmp     al, 's'
        je      .switch_case_string
        cmp     al, 'c'
        je      .switch_case_char
        cmp     al, 'R'
        jne     .switch_default
        lodsb
        cmp     al, 'X'
        jne     .switch_case_number
        cmp     al, 'U'
        jne     .switch_case_number
        cmp     al, 'I'
        jne     .switch_case_number

.switch_default:
        test    al, al
        jz      .done
        mov     al, '!'
        jmp     .emit_al

        ; parse the number part.
.switch_case_number:
        mov     ah, al                  ; ah = {X,U,I}
        lodsb
        cmp     al, '8'
        je      .switch_case_number_8bit
        cmp     al, '1'
        je      .switch_case_number_16bit
        cmp     al, '3'
        je      .switch_case_number_32bit
        cmp     al, '6'
        je      .switch_case_number_64bit
        jmp     .switch_default


        ;
        ; Common code for 8-bit, 16-bit and 32-bit.
        ;
        ; The first part load the value into edx, ah={X,U,I},
        ; al=(max-hex, max-unsigned-dec).
        ;
.switch_case_number_8bit:
        mov     al, (2 << 4) | 2
        movzx   edx, byte a_pArgs
        add     xBX, xCB
        cmp     ah, 'I'
        jne     .switch_case_number_common_32bit_hex_or_unsigned
        movsx   edx, dl
        jmp     .switch_case_number_common_32bit_signed

.switch_case_number_16bit:
        lodsb
        cmp     al, '6'
        jne     .switch_default
        mov     al, (4 << 4) | 5
        movzx   edx, word a_pArgs
        add     xBX, xCB
        cmp     ah, 'I'
        jne     .switch_case_number_common_32bit_hex_or_unsigned
        movsx   edx, dx
        jmp     .switch_case_number_common_32bit_signed

.switch_case_number_32bit:
        lodsb
        cmp     al, '2'
        jne     .switch_default
        mov     al, (8 << 4) | 10
        mov     edx, dword a_pArgs
        add     xBX, sCB
        cmp     ah, 'I'
        je      .switch_case_number_common_32bit_signed

.switch_case_number_common_32bit_hex_or_unsigned:
        cmp     ah, 'X'
        jne     .switch_case_number_common_32bit_unsigned
        shr     al, 4
        and     xAX, 0fh
        cmp     xCX, xAX
        jb      .switch_case_number_bad_buf
        call    .format_32bit_hex_subroutine
        jmp     .format_loop

.switch_case_number_common_32bit_unsigned:
        and     xAX, 0fh
        cmp     xCX, xAX
        jb      .switch_case_number_bad_buf
        call    .format_32bit_dec_subroutine
        jmp     .format_loop

.switch_case_number_common_32bit_signed:
        cmp     edx, 0
        jge     .switch_case_number_common_32bit_unsigned
        and     xAX, 0fh
        inc     xAX                     ; sign char
        cmp     xCX, xAX
        jb      .switch_case_number_bad_buf
        ; Emit the minus sign, invert the value and join the unsigned formatting.
        push    xAX
        mov     al, '-'
        stosb
        dec     xCX
        pop     xAX
        neg     edx
        call    .format_32bit_dec_subroutine
        jmp     .format_loop


        ;
        ; 64-bit is special, to simplify we treat all formats as hex...
        ;
.switch_case_number_64bit:
        lodsb
        cmp     al, '4'
        jne     .switch_default
        cmp     ah, 'X'
        je      .switch_case_number_64bit_hex
        cmp     ah, 'I'
        je      .switch_case_number_64bit_signed
        jmp     .switch_case_number_64bit_unsigned

.switch_case_number_64bit_hex:
        mov     eax, dword a_pArgs
        mov     edx, a_pu32ArgsHighDW
        add     xBX, 8
        cmp     xCX, 8+1+8
        jb      .switch_case_number_bad_buf
        ; Simple, format it as two 32-bit hex values.
        push    sAX
        mov     eax, 8
        call    .format_32bit_hex_subroutine
        mov     al, 27h                 ; '\'' - how do we escape this with yasm?
        stosb
        dec     xCX
        pop     sDX
        mov     eax, 8
        call    .format_32bit_hex_subroutine
        jmp     .format_loop

.switch_case_number_64bit_unsigned:
        cmp     xCX, 19
        jb      .switch_case_number_bad_buf
.switch_case_number_64bit_unsigned_format_it:
        ;; @todo implement me
        jmp     .switch_case_number_64bit_hex

.switch_case_number_64bit_signed:
        mov     eax, dword a_pArgs
        mov     edx, a_pu32ArgsHighDW
        add     xBX, 8
        cmp     xCX, 20
        jb      .switch_case_number_bad_buf
        test    edx, 080000000h
        jz      .switch_case_number_64bit_unsigned_format_it
        ; Emit the minus sign, invert the value and join the unsigned formatting.
        push    xAX
        mov     al, '-'
        stosb
        dec     xCX
        pop     xAX
        neg     eax
        neg     edx
        jmp     .switch_case_number_64bit_unsigned_format_it


        ; The remaining buffer is too small to hold the number.
.switch_case_number_bad_buf:
        mov     al, '^'
        jmp     .emit_al


        ;
        ; Emit a string.
        ;
.switch_case_string:
%ifdef TMPL_16BIT
        lgs     dx, a_pArgs
        add     xBX, 4
%else
        mov     xDX, a_pArgs
        add     xBX, xCB
%endif
        test    xCX, xCX
.switch_case_string_loop:
        jz      .done
%ifdef TMPL_16BIT
        mov     al, [gs:edx]
%else
        mov     al, [xDX]
%endif
        test    al, al
        jz      .format_loop
        inc     xDX
        stosb
        dec     xCX
        jmp     .switch_case_string_loop

        ;
        ; Emit a char.
        ;
.switch_case_char:
        mov     al, byte a_pArgs
        add     xBX, xCB
        jmp     .emit_al


        ;
        ; Done, just emit the terminator char.
        ;
.done:
        xor     al, al
        stosb

.return:
%ifdef TMPL_16BIT
        pop     gs
        pop     fs
        pop     es
        pop     ds
%endif
        popf
        pop     sSI
        pop     sDI
        pop     sBX
        pop     sCX
        pop     sDX
        pop     sAX
        leave
        ret
%undef a_pArgs
%undef a_pu32ArgsHighDW

;;
; Internal subroutine for formatting a hex number into the buffer.
; @param        al      The precision (2, 4, 8).
; @param        edx     The value.
;
; @uses         ecx, edi
;
.format_32bit_hex_subroutine:
        push    xAX
        push    sDX
        push    xBX

        ; Rotate edx into position.
        mov     ebx, 8
        sub     bl, al
        shl     bl, 2
        xchg    cl, bl
        rol     edx, cl
        xchg    bl, cl

        mov     bl, al                  ; Width counter
.format_32bit_hex_subroutine_next:
        rol     edx, 4
        mov     eax, edx
        and     eax, 0fh
        add     sAX, g_achHex
        mov     al, [cs:sAX]
        stosb
        dec     xCX
        dec     bl
        jnz     .format_32bit_hex_subroutine_next

        pop     xBX
        pop     sDX
        pop     xAX
        ret


;;
; Internal subroutine for formatting a hex number into the buffer.
; @param        al      The max precision (2, 5, 10).
; @param        edx     The value.
; @param        xCX     Counter register to decrement as characters are emited.
; @param        es:xDI  Where to write the output, xDI is updated.
;
; @uses         xCX, xDI
;
.format_32bit_dec_subroutine:
%if 0   ;; @todo implement this
        sub     xSP, 20h
        ; Format in reverse order into a stack buffer.

        ; Append the stack buffer to the string, reversing it in the process.

        add     xSP, 20h
%else
        call    .format_32bit_hex_subroutine
%endif
        ret

ENDPROC   TMPL_NM_CMN(StrFormatV)


;;
; Very limited RTStrPrintf version.
;
; @remarks  This uses an entirely STACK BASED CALLING CONVENTION where the
;           caller does the cleanup (cdecl sans volatile regs).
;
; @param    fpszBuf             The output buffer.
; @param    cbBuf               The output buffer size (natural size).
; @param    fpszFormat          The format string (far pointer in 16-bit).
;                               See StrFormatV for format.
; @param    ...                 Zero or more format string arguments.
; @uses     nothing
;
BEGINPROC TMPL_NM_CMN(StrFormatF)
        push    xBP
        mov     xBP, xSP
        push    xAX
        push    xDX
        push    xCX
        push    xBX
%ifdef TMPL_16BIT
        push    ds
        push    es
        push    fs

        lds     xAX, [bp + 04h]
        mov     dx,  [bp + 08h]
        les     xCX, [bp + 0ah]
        mov     bx, ss
        mov     fs, bx
        mov     bx, bp
        add     bx, 0eh
%else
        mov     xAX, [xBP + xCB * 2]
        mov     xDX, [xBP + xCB * 3]
        mov     xCX, [xBP + xCB * 4]
        lea     xBX, [xBP + xCB * 5]
%endif
        call    TMPL_NM_CMN(StrFormatV)

%ifdef TMPL_16BIT
        pop     fs
        pop     es
        pop     ds
%endif
        pop     xBX
        pop     xCX
        pop     xDX
        pop     xAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(StrFormatF)


;;
; Dumps the CPU registers.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(TestDumpCurrentRegisters)
%ifndef TMPL_64BIT
        push    xBP
        mov     xBP, xSP
        push    eax
        pushfd

        push    dword [xBP - sCB - 4]   ; eflags
        mov     eax, cr2
        push    eax
        xor     eax, eax
        mov     eax, gs
        push    eax
        mov     eax, fs
        push    eax
        mov     eax, es
        push    eax
        mov     eax, ds
        push    eax
        mov     eax, cs
        push    eax

        mov     eax, cr4
        push    eax
        mov     eax, cr3
        push    eax
        mov     eax, cr0
        push    eax
        mov     eax, ebp                ; return EBP
        mov     xAX, [xBP]
        push    eax

        mov     eax, ebp                ; return ESP
        add     xAX, xCB
        push    eax

        mov     xAX, [xBP + xCB]        ; return EIP
 %ifdef TMPL_16BIT
        movzx   eax, ax
 %endif
        push    eax

        push    edi
        push    esi
        push    edx
        push    ecx
        push    ebx
        push    dword [xBP - sCB]       ; eax

 %ifdef TMPL_16BIT
        push    cs
 %endif
        push    .s_szRegFmt
        call    TMPL_NM_CMN(PrintF)

        popfd
        pop     eax
        leave
        ret

.s_szRegFmt:
        db      'eax=%RX32 ebx=%RX32 ecx=%RX32 edx=%RX32 esi=%RX32 edi=%RX32', 13, 10
        db      'eip=%RX32 esp=%RX32 ebp=%RX32 cr0=%RX32 cr3=%RX32 cr4=%RX32', 13, 10
        db      'cs=%RX16 ds=%RX16 es=%RX16 fs=%RX16 gs=%RX16 ss=%RX16 cr2=%RX32  eflags=%RX32', 13, 10, 0

%else  ; 64-bit
        push    .s_szRegFmt
        call TMPL_NM_CMN(PrintF)
        ret

.s_szRegFmt:
        db      'TestDumpCurrentRegisters not implemented', 13, 10, 0

%endif ; 64-bit
ENDPROC   TMPL_NM_CMN(TestDumpCurrentRegisters)



;;
; Dumps the CPU registers.
;
; @param    ds:xAX              Pointer to the register frame to dump.
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(TestDumpRegisters)
        push    xBP
        mov     xBP, xSP
        pushf
        push    sDX
        push    sBX
        mov     xBX, xAX

        cmp     byte [xBX + BS2REGS.cBits], 64
        je      .dump_64bit_regs

        push    -1                      ; sanity
        mov     edx, [xBX + BS2REGS.rflags]
        push    sDX
        mov     edx, [xBX + BS2REGS.cr2]
        push    sDX
        xor     edx, edx
        mov     dx, [xBX + BS2REGS.ss]
        push    xDX
        mov     dx, [xBX + BS2REGS.gs]
        push    xDX
        mov     dx, [xBX + BS2REGS.fs]
        push    xDX
        mov     dx, [xBX + BS2REGS.es]
        push    xDX
        mov     dx, [xBX + BS2REGS.ds]
        push    xDX
        mov     dx, [xBX + BS2REGS.cs]
        push    xDX

        mov     edx, [xBX + BS2REGS.cr4]
        push    sDX
        mov     edx, [xBX + BS2REGS.cr3]
        push    sDX
        mov     edx, [xBX + BS2REGS.cr0]
        push    sDX
        mov     edx, [xBX + BS2REGS.rbp]
        push    sDX
        mov     edx, [xBX + BS2REGS.rsp]
        push    sDX
        mov     edx, [xBX + BS2REGS.rip]
        push    sDX

        mov     edx, [xBX + BS2REGS.rdi]
        push    sDX
        mov     edx, [xBX + BS2REGS.rsi]
        push    sDX
        mov     edx, [xBX + BS2REGS.rdx]
        push    sDX
        mov     edx, [xBX + BS2REGS.rcx]
        push    sDX
        mov     edx, [xBX + BS2REGS.rbx]
        push    sDX
        mov     edx, [xBX + BS2REGS.rax]
        push    sDX

%ifdef TMPL_16BIT
        push    cs
%endif
        push    .s_szReg32Fmt
        call    TMPL_NM_CMN(PrintF)
        jmp     .return

.dump_64bit_regs:
%ifdef TMPL_16BIT
        push    cs
%endif
        push    .s_szReg64Fmt
        call    TMPL_NM_CMN(PrintF)

.return:
        lea     xSP, [xBP - sCB*2 - xCB]
        pop     sBX
        pop     sDX
        popf
        leave
        ret

.s_szReg32Fmt:
        db      'eax=%RX32 ebx=%RX32 ecx=%RX32 edx=%RX32 esi=%RX32 edi=%RX32', 13, 10
        db      'eip=%RX32 esp=%RX32 ebp=%RX32 cr0=%RX32 cr3=%RX32 cr4=%RX32', 13, 10
        db      'cs=%RX16 ds=%RX16 es=%RX16 fs=%RX16 gs=%RX16 ss=%RX16 cr2=%RX32  eflags=%RX32', 13, 10
        db      0

.s_szReg64Fmt:
        db      'TestDumpCurrentRegisters not implemented', 13, 10, 0
ENDPROC   TMPL_NM_CMN(TestDumpRegisters)


;;
; Saves the CPU registers.
;
; @param    ds:xAX              Pointer to the register frame to dump.
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(TestSaveRegisters)
        push    xBP
        mov     xBP, xSP
%ifdef TMPL_16BIT
        pushfd
%else
        pushf                           ; - 1*sCB
%endif
        push    sBX                     ; - 2*sCB
        push    sAX                     ; - 3*sCB
        push    sDX                     ; - 4*sCB

        xor     edx, edx                ; zero register.
        mov     xBX, xAX                ; xBX for addressing, xAX for scratch.

%ifdef TMPL_64BIT
        mov     rax, [xSP + sCB*1]
        mov     [xBX + BS2REGS.rax], rax
        mov     rax, [xSP + sCB*2]
        mov     [xBX + BS2REGS.rbx], rax
        mov     [xBX + BS2REGS.rcx], rcx
        mov     rax, [xSP]
        mov     [xBX + BS2REGS.rdx], rax
        mov     [xBX + BS2REGS.rdi], rdi
        mov     [xBX + BS2REGS.rsi], rsi
        mov     rax, [xBP]
        mov     [xBX + BS2REGS.rbp], rax
        lea     rax, [xBP + 16]
        mov     [xBX + BS2REGS.rsp], rax
        mov     rax, [xBP + 8]
        mov     [xBX + BS2REGS.rip], rax
        mov     [xBX + BS2REGS.r8],  r8
        mov     [xBX + BS2REGS.r9],  r9
        mov     [xBX + BS2REGS.r10], r10
        mov     [xBX + BS2REGS.r11], r11
        mov     [xBX + BS2REGS.r12], r12
        mov     [xBX + BS2REGS.r13], r13
        mov     [xBX + BS2REGS.r14], r14
        mov     [xBX + BS2REGS.r15], r15
        mov     rax, [xBP - sCB]
        mov     [xBX + BS2REGS.rflags], rax
        mov     ax, cs
        mov     [xBX + BS2REGS.cs], ax
        mov     ax, ds
        mov     [xBX + BS2REGS.ds], ax
        mov     ax, es
        mov     [xBX + BS2REGS.es], ax
        mov     ax, fs
        mov     [xBX + BS2REGS.fs], ax
        mov     ax, gs
        mov     [xBX + BS2REGS.gs], ax
        mov     ax, ss
        mov     [xBX + BS2REGS.ss], ax
        mov     byte [xBX + BS2REGS.cBits], 64
        mov     [xBX + BS2REGS.pad    ], dl
        mov     [xBX + BS2REGS.pad + 1], dx
        mov     rax, cr0
        mov     [xBX + BS2REGS.cr0], rax
        mov     rax, cr2
        mov     [xBX + BS2REGS.cr2], rax
        mov     rax, cr3
        mov     [xBX + BS2REGS.cr3], rax
        mov     rax, cr4
        mov     [xBX + BS2REGS.cr4], rax
        mov     rax, cr8
        mov     [xBX + BS2REGS.cr8], rax
%else ; !TMPL_64
        mov     eax, [xBP - sCB*3]
        mov     dword [xBX + BS2REGS.rax], eax
        mov     dword [xBX + BS2REGS.rax + 4], edx
        mov     eax, [xBP - sCB*2]
        mov     dword [xBX + BS2REGS.rbx], eax
        mov     dword [xBX + BS2REGS.rbx + 4], edx
        mov     dword [xBX + BS2REGS.rcx], ecx
        mov     dword [xBX + BS2REGS.rcx + 4], edx
        mov     eax, [xBP - sCB*4]
        mov     dword [xBX + BS2REGS.rdx], eax
        mov     dword [xBX + BS2REGS.rdx + 4], edx
        mov     dword [xBX + BS2REGS.rdi], edi
        mov     dword [xBX + BS2REGS.rdi + 4], edx
        mov     dword [xBX + BS2REGS.rsi], esi
        mov     dword [xBX + BS2REGS.rsi + 4], edx
        mov     eax, ebp
        mov     ax, [xBP]
        mov     dword [xBX + BS2REGS.rbp], eax
        mov     dword [xBX + BS2REGS.rbp + 4], edx
 %ifdef TMPL_16BIT
        mov     eax, esp
        mov     ax, bp
        sub     ax, 4
 %else
        lea     eax, [ebp + 8]
 %endif
        mov     dword [xBX + BS2REGS.rsp], eax
        mov     dword [xBX + BS2REGS.rsp + 4], edx
 %ifdef TMPL_16BIT
        movzx   eax, word [xBP + 2]
 %else
        mov     eax, [xBP + 4]
 %endif
        mov     dword [xBX + BS2REGS.rip], eax
        mov     dword [xBX + BS2REGS.rip + 4], edx
        mov     dword [xBX + BS2REGS.r8    ], edx
        mov     dword [xBX + BS2REGS.r8 + 4], edx
        mov     dword [xBX + BS2REGS.r9    ], edx
        mov     dword [xBX + BS2REGS.r9 + 4], edx
        mov     dword [xBX + BS2REGS.r10    ], edx
        mov     dword [xBX + BS2REGS.r10 + 4], edx
        mov     dword [xBX + BS2REGS.r11    ], edx
        mov     dword [xBX + BS2REGS.r11 + 4], edx
        mov     dword [xBX + BS2REGS.r12    ], edx
        mov     dword [xBX + BS2REGS.r12 + 4], edx
        mov     dword [xBX + BS2REGS.r13    ], edx
        mov     dword [xBX + BS2REGS.r13 + 4], edx
        mov     dword [xBX + BS2REGS.r14    ], edx
        mov     dword [xBX + BS2REGS.r14 + 4], edx
        mov     dword [xBX + BS2REGS.r15    ], edx
        mov     dword [xBX + BS2REGS.r15 + 4], edx
        mov     eax, [xBP - sCB]
        mov     dword [xBX + BS2REGS.rflags], eax
        mov     dword [xBX + BS2REGS.rflags + 4], edx
        mov     ax, cs
        mov     [xBX + BS2REGS.cs], ax
        mov     ax, ds
        mov     [xBX + BS2REGS.ds], ax
        mov     ax, es
        mov     [xBX + BS2REGS.es], ax
        mov     ax, fs
        mov     [xBX + BS2REGS.fs], ax
        mov     ax, gs
        mov     [xBX + BS2REGS.gs], ax
        mov     ax, ss
        mov     [xBX + BS2REGS.ss], ax
 %ifdef TMPL_16BIT
        mov     byte [xBX + BS2REGS.cBits], 16
 %else
        mov     byte [xBX + BS2REGS.cBits], 32
 %endif
        mov     [xBX + BS2REGS.pad    ], dl
        mov     [xBX + BS2REGS.pad + 1], dx
        mov     eax, cr0
        mov     dword [xBX + BS2REGS.cr0], eax
        mov     dword [xBX + BS2REGS.cr0 + 4], edx
        mov     eax, cr2
        mov     dword [xBX + BS2REGS.cr2], eax
        mov     dword [xBX + BS2REGS.cr2 + 4], edx
        mov     eax, cr3
        mov     dword [xBX + BS2REGS.cr3], eax
        mov     dword [xBX + BS2REGS.cr3 + 4], edx
        mov     eax, cr4
        mov     dword [xBX + BS2REGS.cr4], eax
        mov     dword [xBX + BS2REGS.cr4 + 4], edx
        mov     dword [xBX + BS2REGS.cr8], edx
        mov     dword [xBX + BS2REGS.cr8 + 4], edx
%endif  ; !TMPL_64

.return:
        pop     sDX
        pop     sAX
        pop     sBX
%ifdef TMPL_16BIT
        popfd
%else
        popf
%endif
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestSaveRegisters)


;;
; Restores the CPU registers, except for rsp, rip, cs, ss and ds.
;
; @param    ds:xAX              Pointer to the register frame to dump.
; @uses     All, but RSP, RIP, CS, SS and DS.
;
BEGINPROC TMPL_NM_CMN(TestRestoreRegisters)
        push    xBP
        mov     xBP, xSP
%ifdef TMPL_16BIT
        pushfd
%else
        pushf                           ; - 1*sCB
%endif
        push    sBX                     ; - 2*sCB
        push    sAX                     ; - 3*sCB

        mov     xBX, xAX                ; xBX for addressing, xAX for scratch.

        mov     sAX, [xBX + BS2REGS.rax]
        mov     [xBP - 3*sCB], sAX
        mov     sAX, [xBX + BS2REGS.rbx]
        mov     [xBP - 2*sCB], sAX
        mov     sCX, [xBX + BS2REGS.rcx]
        mov     sDX, [xBX + BS2REGS.rdx]
        mov     sDI, [xBX + BS2REGS.rdi]
        mov     sSI, [xBX + BS2REGS.rsi]
        ; skip rsp, rbp or rip.
%ifdef TMPL_64BIT
        mov     r8,  [xBX + BS2REGS.r8]
        mov     r9,  [xBX + BS2REGS.r9]
        mov     r10, [xBX + BS2REGS.r10]
        mov     r11, [xBX + BS2REGS.r11]
        mov     r12, [xBX + BS2REGS.r12]
        mov     r13, [xBX + BS2REGS.r13]
        mov     r14, [xBX + BS2REGS.r14]
        mov     r15, [xBX + BS2REGS.r15]
%endif
        mov     sAX, [xBX + BS2REGS.rflags]
        mov     [xBP - sCB], sAX
        ; skip cs & ds.
        mov     ax, [xBX + BS2REGS.es]
        mov     es, ax
        mov     ax, [xBX + BS2REGS.fs]
        mov     fs, ax
        mov     ax, [xBX + BS2REGS.gs]
        mov     gs, ax
        ; skip ss
        mov     sAX, [xBX + BS2REGS.cr0]
        mov     cr0, sAX
        mov     sAX, [xBX + BS2REGS.cr2]
        mov     cr2, sAX
        mov     sAX, [xBX + BS2REGS.cr3]
        mov     cr3, sAX
        mov     sAX, [xBX + BS2REGS.cr4]
        mov     cr4, sAX

.return:
        pop     sAX
        pop     sBX
%ifdef TMPL_16BIT
        popfd
%else
        popf
%endif
        leave
        ret
ENDPROC   TMPL_NM_CMN(TestRestoreRegisters)


;;
; Enables the A20 gate.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2EnableA20)
        call    TMPL_NM_CMN(Bs2EnableA20ViaPortA)
;        call    TMPL_NM_CMN(Bs2EnableA20ViaKbd)
        ret
ENDPROC   TMPL_NM_CMN(Bs2EnableA20)


;;
; Disables the A20 gate.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2DisableA20)
        ; Must call both because they may be ORed together on real HW.
        call    TMPL_NM_CMN(Bs2DisableA20ViaKbd)
        call    TMPL_NM_CMN(Bs2DisableA20ViaPortA)
        ret
ENDPROC   TMPL_NM_CMN(Bs2DisableA20)


;;
; Waits for the keyboard controller to become ready.
;
; @uses         Nothing
;
BEGINPROC TMPL_NM_CMN(Bs2KbdWait)
        push    xAX

.check_status:
        in      al, 64h
        test    al, 1                   ; KBD_STAT_OBF
        jnz     .read_data_and_status
        test    al, 2                   ; KBD_STAT_IBF
        jnz     .check_status

        pop     xAX
        ret

.read_data_and_status:
        in      al, 60h
        jmp     .check_status
ENDPROC   TMPL_NM_CMN(Bs2KbdWait)


;;
; Sends a read command to the keyboard controller and gets the result.
;
; The caller is responsible for making sure the keyboard controller is ready
; for a command (call Bs2KbdWait if unsure).
;
; @param        al              The read command.
; @returns      The value read is returned.
; @uses         al (obviously)
;
BEGINPROC TMPL_NM_CMN(Bs2KbdRead)
        out     64h, al                 ; Write the command.

.check_status:
        in      al, 64h
        test    al, 1                   ; KBD_STAT_OBF
        jz      .check_status

        in      al, 60h                 ; Read the data.
        ret
ENDPROC   TMPL_NM_CMN(Bs2KbdRead)


;;
; Sends a write command to the keyboard controller and then sends the data.
;
; The caller is responsible for making sure the keyboard controller is ready
; for a command (call Bs2KbdWait if unsure).
;
; @param        al              The write command.
; @param        ah              The data to write.
; @uses         Nothing.
;
; @todo         Return status?
;
BEGINPROC TMPL_NM_CMN(Bs2KbdWrite)
        out     64h, al                 ; Write the command.
        call    TMPL_NM_CMN(Bs2KbdWait)

        xchg    al, ah
        out     60h, al                 ; Write the data.
        call    TMPL_NM_CMN(Bs2KbdWait)
        xchg    al, ah

        ret
ENDPROC   TMPL_NM_CMN(Bs2KbdWrite)


;;
; Enables the A20 gate via the keyboard controller.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2EnableA20ViaKbd)
        push    xAX
        pushf
        cli

        call    TMPL_NM_CMN(Bs2KbdWait)
        mov     al, 0d0h                ; KBD_CCMD_READ_OUTPORT
        call    TMPL_NM_CMN(Bs2KbdRead)

        mov     ah, 002h
        or      ah, al
        mov     al, 0d1h                ; KBD_CCMD_WRITE_OUTPORT
        call    TMPL_NM_CMN(Bs2KbdWrite)

        mov     al, 0ffh                ; KBD_CMD_RESET
        out     64h, al
        call    TMPL_NM_CMN(Bs2KbdWait)

        popf
        pop     xAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2EnableA20ViaKbd)


;;
; Disables the A20 gate via the keyboard controller.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2DisableA20ViaKbd)
        push    xAX
        pushf
        cli

        call    TMPL_NM_CMN(Bs2KbdWait)
        mov     al, 0d0h                ; KBD_CCMD_READ_OUTPORT
        call    TMPL_NM_CMN(Bs2KbdRead)

        mov     ah, 0fdh                ; ~2
        and     ah, al
        mov     al, 0d1h                ; KBD_CCMD_WRITE_OUTPORT
        call    TMPL_NM_CMN(Bs2KbdWrite)

        mov     al, 0ffh                ; KBD_CMD_RESET
        out     64h, al
        call    TMPL_NM_CMN(Bs2KbdWait)

        popf
        pop     xAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2DisableA20ViaKbd)


;;
; Enables the A20 gate via control port A (PS/2 style).
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2EnableA20ViaPortA)
        push    xAX

        ; Use Control port A, assuming a PS/2 style system.
        in      al, 092h
        test    al, 02h
        jnz     .done                   ; avoid trouble writing back the same value.
        or      al, 2                   ; enable the A20 gate.
        out     092h, al

.done:
        pop     xAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2EnableA20ViaPortA)


;;
; Disables the A20 gate via control port A (PS/2 style).
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2DisableA20ViaPortA)
        push    xAX

        ; Use Control port A, assuming a PS/2 style system.
        in      al, 092h
        test    al, 02h
        jz      .done                   ; avoid trouble writing back the same value.
        and     al, 0fdh                ; disable the A20 gate.
        out     092h, al

.done:
        pop     xAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2DisableA20ViaPortA)


;;
; Checks if the no-execution bit is supported.
;
; @returns  AL=1 and ZF=0 if supported.
; @returns  AL=0 and ZF=1 if not.
; @uses     al
;
BEGINPROC TMPL_NM_CMN(Bs2IsNXSupported)
        push    xBP
        mov     xBP, xSP
        push    sBX
        push    sDX
        push    sCX
        push    sAX

        mov     eax, 080000000h
        cpuid
        cmp     eax, 080000001h
        jb      .not_supported
        cmp     eax, 080001000h
        jae     .not_supported

        mov     eax, 080000001h
        cpuid
        test    edx, X86_CPUID_EXT_FEATURE_EDX_NX
        jz      .not_supported

        ; supported
        pop     sAX
        mov     al, 1

.return:
        pop     sCX
        pop     sDX
        pop     sBX
        leave
        ret
.not_supported:
        pop     sAX
        xor     al, al
        jmp .return
ENDPROC   TMPL_NM_CMN(Bs2IsNXSupported)


;;
; Sets EFER.NXE=al if NXE is supported.
;
; @param    al          0 if NXE should be disabled, non-zero if it should
;                       be enabled.
; @uses     nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2SetupNX)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    sDX
        push    sCX

        call    TMPL_NM_CMN(Bs2IsNXSupported)
        jz      .done

        mov     ecx, MSR_K6_EFER
        rdmsr
        test    byte [xBP - sCB], 0ffh
        jz      .disable_it
        or      eax, MSR_K6_EFER_NXE
        jmp     .set_it
.disable_it:
        and     eax, ~MSR_K6_EFER_NXE
.set_it:
        wrmsr

.done:
        pop     sCX
        pop     sDX
        pop     sAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(Bs2SetupNX)


;;
; Disables NX if supported.
;
; @uses     nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2DisableNX)
        push    xBP
        mov     xBP, xSP
        push    xAX

        xor     al, al
        call    TMPL_NM_CMN(Bs2SetupNX)

        pop     xAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(Bs2DisableNX)


;;
; Enables NX if supported.
;
; @uses     nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2EnableNX)
        push    xBP
        mov     xBP, xSP
        push    xAX

        mov     al, 1
        call    TMPL_NM_CMN(Bs2SetupNX)

        pop     xAX
        leave
        ret
ENDPROC   TMPL_NM_CMN(Bs2EnableNX)


;;
; Panics if the testing feature of the VMMDev is missing.
;
; A message will be printed.
;
; @uses     Nothing.
;
BEGINPROC TMPL_NM_CMN(Bs2PanicIfVMMDevTestingIsMissing)
        push    xDX
        push    sAX

        xor     eax, eax
        mov     dx, VMMDEV_TESTING_IOPORT_NOP
        in      eax, dx
        cmp     eax, VMMDEV_TESTING_NOP_RET
        je      .ok

        mov     xAX, .s_szMissingVMMDevTesting
        call    TMPL_NM_CMN(PrintStr)
        call    Bs2Panic

.ok:
        pop     sAX
        pop     xDX
        ret

.s_szMissingVMMDevTesting:
        db 'fatal error: The testing feature of the VMMDevVMMDev is not present!', 13, 10, 0
ENDPROC   TMPL_NM_CMN(Bs2PanicIfVMMDevTestingIsMissing)



%ifdef BS2_WITH_TRAPS

;;
; Switches to ring-0 from whatever the current mode is.
;
; @uses cs, ss, ds, es, fs, gs
;
BEGINPROC TMPL_NM_CMN(Bs2ToRing0)
        push    sAX
        mov     sAX, BS2_SYSCALL_TO_RING0
        int     BS2_TRAP_SYSCALL
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2ToRing0)

;;
; Switches to ring-1 from whatever the current mode is.
;
; @uses cs, ss, ds, es, fs, gs
;
BEGINPROC TMPL_NM_CMN(Bs2ToRing1)
        push    sAX
        mov     sAX, BS2_SYSCALL_TO_RING1
        int     BS2_TRAP_SYSCALL
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2ToRing1)

;;
; Switches to ring-0 from whatever the current mode is.
;
; @uses cs, ss, ds, es, fs, gs
;
BEGINPROC TMPL_NM_CMN(Bs2ToRing2)
        push    sAX
        mov     sAX, BS2_SYSCALL_TO_RING2
        int     BS2_TRAP_SYSCALL
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2ToRing2)

;;
; Switches to ring-3 from whatever the current mode is.
;
; @uses cs, ss, ds, es, fs, gs
;
BEGINPROC TMPL_NM_CMN(Bs2ToRing3)
        push    sAX
        mov     sAX, BS2_SYSCALL_TO_RING3
        int     BS2_TRAP_SYSCALL
        pop     sAX
        ret
ENDPROC   TMPL_NM_CMN(Bs2ToRing3)

;;
; Switches the given ring from whatever the current mode is.
;
; @param AL     The desired ring.
; @uses cs, ss, ds, es, fs, gs
;
BEGINPROC TMPL_NM_CMN(Bs2ToRingN)
        pushf
        cmp     al, 3
        je      .ring3
        cmp     al, 2
        je      .ring2
        cmp     al, 1
        je      .ring1
.ring0:
        call    TMPL_NM_CMN(Bs2ToRing0)
.done:
        popf
        ret

.ring1:
        call    TMPL_NM_CMN(Bs2ToRing1)
        jmp     .done
.ring2:
        call    TMPL_NM_CMN(Bs2ToRing2)
        jmp     .done
.ring3:
        call    TMPL_NM_CMN(Bs2ToRing3)
        jmp     .done
ENDPROC   TMPL_NM_CMN(Bs2ToRingN)

%endif ; BS2_WITH_TRAPS



;
; Wrapper for dynamically calling the right specific method.
; This avoid putting large portions of the code in the 2nd template.
;

TMPL_NM_CMN(g_pfnPrintStrInternal): TMPL_PTR_DEF 0
TMPL_NM_CMN(g_pfnPrintChrInternal): TMPL_PTR_DEF 0

BEGINPROC TMPL_NM_CMN(PrintStr)
        jmp     [TMPL_NM_CMN(g_pfnPrintStrInternal)]
ENDPROC   TMPL_NM_CMN(PrintStr)

BEGINPROC TMPL_NM_CMN(PrintChr)
        jmp     [TMPL_NM_CMN(g_pfnPrintChrInternal)]
ENDPROC   TMPL_NM_CMN(PrintChr)


%include "bootsector2-template-footer.mac"