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
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
|
/* $Id: APICAll.cpp $ */
/** @file
* APIC - Advanced Programmable Interrupt Controller - All Contexts.
*/
/*
* Copyright (C) 2016-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>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_APIC
#define VMCPU_INCL_CPUM_GST_CTX /* for macOS hack */
#include "APICInternal.h"
#include <VBox/vmm/apic.h>
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pdmapi.h>
#include <VBox/vmm/vmcc.h>
#include <VBox/vmm/vmm.h>
#include <VBox/vmm/vmcpuset.h>
#ifdef IN_RING0
# include <VBox/vmm/gvmm.h>
#endif
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static void apicSetInterruptFF(PVMCPUCC pVCpu, PDMAPICIRQ enmType);
static void apicStopTimer(PVMCPUCC pVCpu);
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
/** An ordered array of valid LVT masks. */
static const uint32_t g_au32LvtValidMasks[] =
{
XAPIC_LVT_TIMER_VALID,
XAPIC_LVT_THERMAL_VALID,
XAPIC_LVT_PERF_VALID,
XAPIC_LVT_LINT_VALID, /* LINT0 */
XAPIC_LVT_LINT_VALID, /* LINT1 */
XAPIC_LVT_ERROR_VALID
};
#endif
#if 0
/** @todo CMCI */
static const uint32_t g_au32LvtExtValidMask[] =
{
XAPIC_LVT_CMCI_VALID
};
#endif
/**
* Checks if a vector is set in an APIC 256-bit sparse register.
*
* @returns true if the specified vector is set, false otherwise.
* @param pApicReg The APIC 256-bit spare register.
* @param uVector The vector to check if set.
*/
DECLINLINE(bool) apicTestVectorInReg(const volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
{
const volatile uint8_t *pbBitmap = (const volatile uint8_t *)&pApicReg->u[0];
return ASMBitTest(pbBitmap, (XAPIC_REG256_VECTOR_OFF(uVector) << 3) + XAPIC_REG256_VECTOR_BIT(uVector));
}
/**
* Sets the vector in an APIC 256-bit sparse register.
*
* @param pApicReg The APIC 256-bit spare register.
* @param uVector The vector to set.
*/
DECLINLINE(void) apicSetVectorInReg(volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
{
volatile uint8_t *pbBitmap = (volatile uint8_t *)&pApicReg->u[0];
ASMAtomicBitSet(pbBitmap, (XAPIC_REG256_VECTOR_OFF(uVector) << 3) + XAPIC_REG256_VECTOR_BIT(uVector));
}
/**
* Clears the vector in an APIC 256-bit sparse register.
*
* @param pApicReg The APIC 256-bit spare register.
* @param uVector The vector to clear.
*/
DECLINLINE(void) apicClearVectorInReg(volatile XAPIC256BITREG *pApicReg, uint8_t uVector)
{
volatile uint8_t *pbBitmap = (volatile uint8_t *)&pApicReg->u[0];
ASMAtomicBitClear(pbBitmap, (XAPIC_REG256_VECTOR_OFF(uVector) << 3) + XAPIC_REG256_VECTOR_BIT(uVector));
}
#if 0 /* unused */
/**
* Checks if a vector is set in an APIC Pending-Interrupt Bitmap (PIB).
*
* @returns true if the specified vector is set, false otherwise.
* @param pvPib Opaque pointer to the PIB.
* @param uVector The vector to check if set.
*/
DECLINLINE(bool) apicTestVectorInPib(volatile void *pvPib, uint8_t uVector)
{
return ASMBitTest(pvPib, uVector);
}
#endif /* unused */
/**
* Atomically sets the PIB notification bit.
*
* @returns non-zero if the bit was already set, 0 otherwise.
* @param pApicPib Pointer to the PIB.
*/
DECLINLINE(uint32_t) apicSetNotificationBitInPib(PAPICPIB pApicPib)
{
return ASMAtomicXchgU32(&pApicPib->fOutstandingNotification, RT_BIT_32(31));
}
/**
* Atomically tests and clears the PIB notification bit.
*
* @returns non-zero if the bit was already set, 0 otherwise.
* @param pApicPib Pointer to the PIB.
*/
DECLINLINE(uint32_t) apicClearNotificationBitInPib(PAPICPIB pApicPib)
{
return ASMAtomicXchgU32(&pApicPib->fOutstandingNotification, UINT32_C(0));
}
/**
* Sets the vector in an APIC Pending-Interrupt Bitmap (PIB).
*
* @param pvPib Opaque pointer to the PIB.
* @param uVector The vector to set.
*/
DECLINLINE(void) apicSetVectorInPib(volatile void *pvPib, uint8_t uVector)
{
ASMAtomicBitSet(pvPib, uVector);
}
#if 0 /* unused */
/**
* Clears the vector in an APIC Pending-Interrupt Bitmap (PIB).
*
* @param pvPib Opaque pointer to the PIB.
* @param uVector The vector to clear.
*/
DECLINLINE(void) apicClearVectorInPib(volatile void *pvPib, uint8_t uVector)
{
ASMAtomicBitClear(pvPib, uVector);
}
#endif /* unused */
#if 0 /* unused */
/**
* Atomically OR's a fragment (32 vectors) into an APIC 256-bit sparse
* register.
*
* @param pApicReg The APIC 256-bit spare register.
* @param idxFragment The index of the 32-bit fragment in @a
* pApicReg.
* @param u32Fragment The 32-bit vector fragment to OR.
*/
DECLINLINE(void) apicOrVectorsToReg(volatile XAPIC256BITREG *pApicReg, size_t idxFragment, uint32_t u32Fragment)
{
Assert(idxFragment < RT_ELEMENTS(pApicReg->u));
ASMAtomicOrU32(&pApicReg->u[idxFragment].u32Reg, u32Fragment);
}
#endif /* unused */
#if 0 /* unused */
/**
* Atomically AND's a fragment (32 vectors) into an APIC
* 256-bit sparse register.
*
* @param pApicReg The APIC 256-bit spare register.
* @param idxFragment The index of the 32-bit fragment in @a
* pApicReg.
* @param u32Fragment The 32-bit vector fragment to AND.
*/
DECLINLINE(void) apicAndVectorsToReg(volatile XAPIC256BITREG *pApicReg, size_t idxFragment, uint32_t u32Fragment)
{
Assert(idxFragment < RT_ELEMENTS(pApicReg->u));
ASMAtomicAndU32(&pApicReg->u[idxFragment].u32Reg, u32Fragment);
}
#endif /* unused */
/**
* Reports and returns appropriate error code for invalid MSR accesses.
*
* @returns VERR_CPUM_RAISE_GP_0
*
* @param pVCpu The cross context virtual CPU structure.
* @param u32Reg The MSR being accessed.
* @param enmAccess The invalid-access type.
*/
static int apicMsrAccessError(PVMCPUCC pVCpu, uint32_t u32Reg, APICMSRACCESS enmAccess)
{
static struct
{
const char *pszBefore; /* The error message before printing the MSR index */
const char *pszAfter; /* The error message after printing the MSR index */
} const s_aAccess[] =
{
/* enmAccess pszBefore pszAfter */
/* 0 */ { "read MSR", " while not in x2APIC mode" },
/* 1 */ { "write MSR", " while not in x2APIC mode" },
/* 2 */ { "read reserved/unknown MSR", "" },
/* 3 */ { "write reserved/unknown MSR", "" },
/* 4 */ { "read write-only MSR", "" },
/* 5 */ { "write read-only MSR", "" },
/* 6 */ { "read reserved bits of MSR", "" },
/* 7 */ { "write reserved bits of MSR", "" },
/* 8 */ { "write an invalid value to MSR", "" },
/* 9 */ { "write MSR", " disallowed by configuration" },
/* 10 */ { "read MSR", " disallowed by configuration" },
};
AssertCompile(RT_ELEMENTS(s_aAccess) == APICMSRACCESS_COUNT);
size_t const i = enmAccess;
Assert(i < RT_ELEMENTS(s_aAccess));
if (pVCpu->apic.s.cLogMaxAccessError++ < 5)
LogRel(("APIC%u: Attempt to %s (%#x)%s -> #GP(0)\n", pVCpu->idCpu, s_aAccess[i].pszBefore, u32Reg, s_aAccess[i].pszAfter));
return VERR_CPUM_RAISE_GP_0;
}
/**
* Gets the descriptive APIC mode.
*
* @returns The name.
* @param enmMode The xAPIC mode.
*/
const char *apicGetModeName(APICMODE enmMode)
{
switch (enmMode)
{
case APICMODE_DISABLED: return "Disabled";
case APICMODE_XAPIC: return "xAPIC";
case APICMODE_X2APIC: return "x2APIC";
default: break;
}
return "Invalid";
}
/**
* Gets the descriptive destination format name.
*
* @returns The destination format name.
* @param enmDestFormat The destination format.
*/
const char *apicGetDestFormatName(XAPICDESTFORMAT enmDestFormat)
{
switch (enmDestFormat)
{
case XAPICDESTFORMAT_FLAT: return "Flat";
case XAPICDESTFORMAT_CLUSTER: return "Cluster";
default: break;
}
return "Invalid";
}
/**
* Gets the descriptive delivery mode name.
*
* @returns The delivery mode name.
* @param enmDeliveryMode The delivery mode.
*/
const char *apicGetDeliveryModeName(XAPICDELIVERYMODE enmDeliveryMode)
{
switch (enmDeliveryMode)
{
case XAPICDELIVERYMODE_FIXED: return "Fixed";
case XAPICDELIVERYMODE_LOWEST_PRIO: return "Lowest-priority";
case XAPICDELIVERYMODE_SMI: return "SMI";
case XAPICDELIVERYMODE_NMI: return "NMI";
case XAPICDELIVERYMODE_INIT: return "INIT";
case XAPICDELIVERYMODE_STARTUP: return "SIPI";
case XAPICDELIVERYMODE_EXTINT: return "ExtINT";
default: break;
}
return "Invalid";
}
/**
* Gets the descriptive destination mode name.
*
* @returns The destination mode name.
* @param enmDestMode The destination mode.
*/
const char *apicGetDestModeName(XAPICDESTMODE enmDestMode)
{
switch (enmDestMode)
{
case XAPICDESTMODE_PHYSICAL: return "Physical";
case XAPICDESTMODE_LOGICAL: return "Logical";
default: break;
}
return "Invalid";
}
/**
* Gets the descriptive trigger mode name.
*
* @returns The trigger mode name.
* @param enmTriggerMode The trigger mode.
*/
const char *apicGetTriggerModeName(XAPICTRIGGERMODE enmTriggerMode)
{
switch (enmTriggerMode)
{
case XAPICTRIGGERMODE_EDGE: return "Edge";
case XAPICTRIGGERMODE_LEVEL: return "Level";
default: break;
}
return "Invalid";
}
/**
* Gets the destination shorthand name.
*
* @returns The destination shorthand name.
* @param enmDestShorthand The destination shorthand.
*/
const char *apicGetDestShorthandName(XAPICDESTSHORTHAND enmDestShorthand)
{
switch (enmDestShorthand)
{
case XAPICDESTSHORTHAND_NONE: return "None";
case XAPICDESTSHORTHAND_SELF: return "Self";
case XAPIDDESTSHORTHAND_ALL_INCL_SELF: return "All including self";
case XAPICDESTSHORTHAND_ALL_EXCL_SELF: return "All excluding self";
default: break;
}
return "Invalid";
}
/**
* Gets the timer mode name.
*
* @returns The timer mode name.
* @param enmTimerMode The timer mode.
*/
const char *apicGetTimerModeName(XAPICTIMERMODE enmTimerMode)
{
switch (enmTimerMode)
{
case XAPICTIMERMODE_ONESHOT: return "One-shot";
case XAPICTIMERMODE_PERIODIC: return "Periodic";
case XAPICTIMERMODE_TSC_DEADLINE: return "TSC deadline";
default: break;
}
return "Invalid";
}
/**
* Gets the APIC mode given the base MSR value.
*
* @returns The APIC mode.
* @param uApicBaseMsr The APIC Base MSR value.
*/
APICMODE apicGetMode(uint64_t uApicBaseMsr)
{
uint32_t const uMode = (uApicBaseMsr >> 10) & UINT64_C(3);
APICMODE const enmMode = (APICMODE)uMode;
#ifdef VBOX_STRICT
/* Paranoia. */
switch (uMode)
{
case APICMODE_DISABLED:
case APICMODE_INVALID:
case APICMODE_XAPIC:
case APICMODE_X2APIC:
break;
default:
AssertMsgFailed(("Invalid mode"));
}
#endif
return enmMode;
}
/**
* Returns whether the APIC is hardware enabled or not.
*
* @returns true if enabled, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
*/
VMM_INT_DECL(bool) APICIsEnabled(PCVMCPUCC pVCpu)
{
PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
return RT_BOOL(pApicCpu->uApicBaseMsr & MSR_IA32_APICBASE_EN);
}
/**
* Finds the most significant set bit in an APIC 256-bit sparse register.
*
* @returns @a rcNotFound if no bit was set, 0-255 otherwise.
* @param pReg The APIC 256-bit sparse register.
* @param rcNotFound What to return when no bit is set.
*/
static int apicGetHighestSetBitInReg(volatile const XAPIC256BITREG *pReg, int rcNotFound)
{
ssize_t const cFragments = RT_ELEMENTS(pReg->u);
unsigned const uFragmentShift = 5;
AssertCompile(1 << uFragmentShift == sizeof(pReg->u[0].u32Reg) * 8);
for (ssize_t i = cFragments - 1; i >= 0; i--)
{
uint32_t const uFragment = pReg->u[i].u32Reg;
if (uFragment)
{
unsigned idxSetBit = ASMBitLastSetU32(uFragment);
--idxSetBit;
idxSetBit |= i << uFragmentShift;
return idxSetBit;
}
}
return rcNotFound;
}
/**
* Reads a 32-bit register at a specified offset.
*
* @returns The value at the specified offset.
* @param pXApicPage The xAPIC page.
* @param offReg The offset of the register being read.
*/
DECLINLINE(uint32_t) apicReadRaw32(PCXAPICPAGE pXApicPage, uint16_t offReg)
{
Assert(offReg < sizeof(*pXApicPage) - sizeof(uint32_t));
uint8_t const *pbXApic = (const uint8_t *)pXApicPage;
uint32_t const uValue = *(const uint32_t *)(pbXApic + offReg);
return uValue;
}
/**
* Writes a 32-bit register at a specified offset.
*
* @param pXApicPage The xAPIC page.
* @param offReg The offset of the register being written.
* @param uReg The value of the register.
*/
DECLINLINE(void) apicWriteRaw32(PXAPICPAGE pXApicPage, uint16_t offReg, uint32_t uReg)
{
Assert(offReg < sizeof(*pXApicPage) - sizeof(uint32_t));
uint8_t *pbXApic = (uint8_t *)pXApicPage;
*(uint32_t *)(pbXApic + offReg) = uReg;
}
/**
* Sets an error in the internal ESR of the specified APIC.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uError The error.
* @thread Any.
*/
DECLINLINE(void) apicSetError(PVMCPUCC pVCpu, uint32_t uError)
{
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
ASMAtomicOrU32(&pApicCpu->uEsrInternal, uError);
}
/**
* Clears all errors in the internal ESR.
*
* @returns The value of the internal ESR before clearing.
* @param pVCpu The cross context virtual CPU structure.
*/
DECLINLINE(uint32_t) apicClearAllErrors(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT(pVCpu);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
return ASMAtomicXchgU32(&pApicCpu->uEsrInternal, 0);
}
/**
* Signals the guest if a pending interrupt is ready to be serviced.
*
* @param pVCpu The cross context virtual CPU structure.
*/
static void apicSignalNextPendingIntr(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
if (pXApicPage->svr.u.fApicSoftwareEnable)
{
int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1 /* rcNotFound */);
if (irrv >= 0)
{
Assert(irrv <= (int)UINT8_MAX);
uint8_t const uVector = irrv;
int const isrv = apicGetHighestSetBitInReg(&pXApicPage->isr, 0 /* rcNotFound */);
Assert(isrv <= (int)UINT8_MAX);
uint8_t const uIsrVec = isrv;
/* uIsrVect reflects the highest interrupt vector currently serviced (i.e. in ISR),
* or zero if there's none. We want to report a pending interrupt only if IRR > ISR but
* regardless of TPR. Hence we can't look at the PPR value, since that also reflects TPR.
* NB: The APIC emulation will know when ISR changes, but not necessarily when TPR does.
*/
if (XAPIC_PPR_GET_PP(uVector) > XAPIC_PPR_GET_PP(uIsrVec))
{
Log2(("APIC%u: apicSignalNextPendingIntr: Signalling pending interrupt. uVector=%#x\n", pVCpu->idCpu, uVector));
apicSetInterruptFF(pVCpu, PDMAPICIRQ_HARDWARE);
}
else
Log2(("APIC%u: apicSignalNextPendingIntr: Nothing to signal yet. uVector=%#x uIsrVec=%#x\n", pVCpu->idCpu, uVector, uIsrVec));
}
}
else
{
Log2(("APIC%u: apicSignalNextPendingIntr: APIC software-disabled, clearing pending interrupt\n", pVCpu->idCpu));
apicClearInterruptFF(pVCpu, PDMAPICIRQ_HARDWARE);
}
}
/**
* Sets the Spurious-Interrupt Vector Register (SVR).
*
* @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
* @param pVCpu The cross context virtual CPU structure.
* @param uSvr The SVR value.
*/
static int apicSetSvr(PVMCPUCC pVCpu, uint32_t uSvr)
{
VMCPU_ASSERT_EMT(pVCpu);
uint32_t uValidMask = XAPIC_SVR_VALID;
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
if (pXApicPage->version.u.fEoiBroadcastSupression)
uValidMask |= XAPIC_SVR_SUPRESS_EOI_BROADCAST;
if ( XAPIC_IN_X2APIC_MODE(pVCpu)
&& (uSvr & ~uValidMask))
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_SVR, APICMSRACCESS_WRITE_RSVD_BITS);
Log2(("APIC%u: apicSetSvr: uSvr=%#RX32\n", pVCpu->idCpu, uSvr));
apicWriteRaw32(pXApicPage, XAPIC_OFF_SVR, uSvr);
if (!pXApicPage->svr.u.fApicSoftwareEnable)
{
/** @todo CMCI. */
pXApicPage->lvt_timer.u.u1Mask = 1;
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
pXApicPage->lvt_thermal.u.u1Mask = 1;
#endif
pXApicPage->lvt_perf.u.u1Mask = 1;
pXApicPage->lvt_lint0.u.u1Mask = 1;
pXApicPage->lvt_lint1.u.u1Mask = 1;
pXApicPage->lvt_error.u.u1Mask = 1;
}
apicSignalNextPendingIntr(pVCpu);
return VINF_SUCCESS;
}
/**
* Sends an interrupt to one or more APICs.
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure, can be
* NULL if the source of the interrupt is not an
* APIC (for e.g. a bus).
* @param uVector The interrupt vector.
* @param enmTriggerMode The trigger mode.
* @param enmDeliveryMode The delivery mode.
* @param pDestCpuSet The destination CPU set.
* @param pfIntrAccepted Where to store whether this interrupt was
* accepted by the target APIC(s) or not.
* Optional, can be NULL.
* @param uSrcTag The interrupt source tag (debugging).
* @param rcRZ The return code if the operation cannot be
* performed in the current context.
*/
static VBOXSTRICTRC apicSendIntr(PVMCC pVM, PVMCPUCC pVCpu, uint8_t uVector, XAPICTRIGGERMODE enmTriggerMode,
XAPICDELIVERYMODE enmDeliveryMode, PCVMCPUSET pDestCpuSet, bool *pfIntrAccepted,
uint32_t uSrcTag, int rcRZ)
{
AssertCompile(sizeof(pVM->apic.s) <= sizeof(pVM->apic.padding));
AssertCompile(sizeof(pVCpu->apic.s) <= sizeof(pVCpu->apic.padding));
#ifdef IN_RING0
AssertCompile(sizeof(pVM->apicr0.s) <= sizeof(pVM->apicr0.padding));
#endif
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
VMCPUID const cCpus = pVM->cCpus;
bool fAccepted = false;
switch (enmDeliveryMode)
{
case XAPICDELIVERYMODE_FIXED:
{
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
PVMCPUCC pItVCpu = pVM->CTX_SUFF(apCpus)[idCpu];
if (APICIsEnabled(pItVCpu))
fAccepted = apicPostInterrupt(pItVCpu, uVector, enmTriggerMode, uSrcTag);
}
break;
}
case XAPICDELIVERYMODE_LOWEST_PRIO:
{
VMCPUID const idCpu = VMCPUSET_FIND_FIRST_PRESENT(pDestCpuSet);
AssertMsgBreak(idCpu < pVM->cCpus, ("APIC: apicSendIntr: No CPU found for lowest-priority delivery mode! idCpu=%u\n", idCpu));
PVMCPUCC pVCpuDst = pVM->CTX_SUFF(apCpus)[idCpu];
if (APICIsEnabled(pVCpuDst))
fAccepted = apicPostInterrupt(pVCpuDst, uVector, enmTriggerMode, uSrcTag);
else
AssertMsgFailed(("APIC: apicSendIntr: Target APIC not enabled in lowest-priority delivery mode! idCpu=%u\n", idCpu));
break;
}
case XAPICDELIVERYMODE_SMI:
{
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
Log2(("APIC: apicSendIntr: Raising SMI on VCPU%u\n", idCpu));
apicSetInterruptFF(pVM->CTX_SUFF(apCpus)[idCpu], PDMAPICIRQ_SMI);
fAccepted = true;
}
break;
}
case XAPICDELIVERYMODE_NMI:
{
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
PVMCPUCC pItVCpu = pVM->CTX_SUFF(apCpus)[idCpu];
if (APICIsEnabled(pItVCpu))
{
Log2(("APIC: apicSendIntr: Raising NMI on VCPU%u\n", idCpu));
apicSetInterruptFF(pItVCpu, PDMAPICIRQ_NMI);
fAccepted = true;
}
}
break;
}
case XAPICDELIVERYMODE_INIT:
{
#ifdef IN_RING3
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
Log2(("APIC: apicSendIntr: Issuing INIT to VCPU%u\n", idCpu));
VMMR3SendInitIpi(pVM, idCpu);
fAccepted = true;
}
#else
/* We need to return to ring-3 to deliver the INIT. */
rcStrict = rcRZ;
fAccepted = true;
#endif
break;
}
case XAPICDELIVERYMODE_STARTUP:
{
#ifdef IN_RING3
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
Log2(("APIC: apicSendIntr: Issuing SIPI to VCPU%u\n", idCpu));
VMMR3SendStartupIpi(pVM, idCpu, uVector);
fAccepted = true;
}
#else
/* We need to return to ring-3 to deliver the SIPI. */
rcStrict = rcRZ;
fAccepted = true;
Log2(("APIC: apicSendIntr: SIPI issued, returning to RZ. rc=%Rrc\n", rcRZ));
#endif
break;
}
case XAPICDELIVERYMODE_EXTINT:
{
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
if (VMCPUSET_IS_PRESENT(pDestCpuSet, idCpu))
{
Log2(("APIC: apicSendIntr: Raising EXTINT on VCPU%u\n", idCpu));
apicSetInterruptFF(pVM->CTX_SUFF(apCpus)[idCpu], PDMAPICIRQ_EXTINT);
fAccepted = true;
}
break;
}
default:
{
AssertMsgFailed(("APIC: apicSendIntr: Unsupported delivery mode %#x (%s)\n", enmDeliveryMode,
apicGetDeliveryModeName(enmDeliveryMode)));
break;
}
}
/*
* If an illegal vector is programmed, set the 'send illegal vector' error here if the
* interrupt is being sent by an APIC.
*
* The 'receive illegal vector' will be set on the target APIC when the interrupt
* gets generated, see apicPostInterrupt().
*
* See Intel spec. 10.5.3 "Error Handling".
*/
if ( rcStrict != rcRZ
&& pVCpu)
{
/*
* Flag only errors when the delivery mode is fixed and not others.
*
* Ubuntu 10.04-3 amd64 live CD with 2 VCPUs gets upset as it sends an SIPI to the
* 2nd VCPU with vector 6 and checks the ESR for no errors, see @bugref{8245#c86}.
*/
/** @todo The spec says this for LVT, but not explcitly for ICR-lo
* but it probably is true. */
if (enmDeliveryMode == XAPICDELIVERYMODE_FIXED)
{
if (RT_UNLIKELY(uVector <= XAPIC_ILLEGAL_VECTOR_END))
apicSetError(pVCpu, XAPIC_ESR_SEND_ILLEGAL_VECTOR);
}
}
if (pfIntrAccepted)
*pfIntrAccepted = fAccepted;
return rcStrict;
}
/**
* Checks if this APIC belongs to a logical destination.
*
* @returns true if the APIC belongs to the logical
* destination, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param fDest The destination mask.
*
* @thread Any.
*/
static bool apicIsLogicalDest(PVMCPUCC pVCpu, uint32_t fDest)
{
if (XAPIC_IN_X2APIC_MODE(pVCpu))
{
/*
* Flat logical mode is not supported in x2APIC mode.
* In clustered logical mode, the 32-bit logical ID in the LDR is interpreted as follows:
* - High 16 bits is the cluster ID.
* - Low 16 bits: each bit represents a unique APIC within the cluster.
*/
PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpu);
uint32_t const u32Ldr = pX2ApicPage->ldr.u32LogicalApicId;
if (X2APIC_LDR_GET_CLUSTER_ID(u32Ldr) == (fDest & X2APIC_LDR_CLUSTER_ID))
return RT_BOOL(u32Ldr & fDest & X2APIC_LDR_LOGICAL_ID);
return false;
}
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
/*
* In both flat and clustered logical mode, a destination mask of all set bits indicates a broadcast.
* See AMD spec. 16.6.1 "Receiving System and IPI Interrupts".
*/
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
if ((fDest & XAPIC_LDR_FLAT_LOGICAL_ID) == XAPIC_LDR_FLAT_LOGICAL_ID)
return true;
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
XAPICDESTFORMAT enmDestFormat = (XAPICDESTFORMAT)pXApicPage->dfr.u.u4Model;
if (enmDestFormat == XAPICDESTFORMAT_FLAT)
{
/* The destination mask is interpreted as a bitmap of 8 unique logical APIC IDs. */
uint8_t const u8Ldr = pXApicPage->ldr.u.u8LogicalApicId;
return RT_BOOL(u8Ldr & fDest & XAPIC_LDR_FLAT_LOGICAL_ID);
}
/*
* In clustered logical mode, the 8-bit logical ID in the LDR is interpreted as follows:
* - High 4 bits is the cluster ID.
* - Low 4 bits: each bit represents a unique APIC within the cluster.
*/
Assert(enmDestFormat == XAPICDESTFORMAT_CLUSTER);
uint8_t const u8Ldr = pXApicPage->ldr.u.u8LogicalApicId;
if (XAPIC_LDR_CLUSTERED_GET_CLUSTER_ID(u8Ldr) == (fDest & XAPIC_LDR_CLUSTERED_CLUSTER_ID))
return RT_BOOL(u8Ldr & fDest & XAPIC_LDR_CLUSTERED_LOGICAL_ID);
return false;
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
}
/**
* Figures out the set of destination CPUs for a given destination mode, format
* and delivery mode setting.
*
* @param pVM The cross context VM structure.
* @param fDestMask The destination mask.
* @param fBroadcastMask The broadcast mask.
* @param enmDestMode The destination mode.
* @param enmDeliveryMode The delivery mode.
* @param pDestCpuSet The destination CPU set to update.
*/
static void apicGetDestCpuSet(PVMCC pVM, uint32_t fDestMask, uint32_t fBroadcastMask, XAPICDESTMODE enmDestMode,
XAPICDELIVERYMODE enmDeliveryMode, PVMCPUSET pDestCpuSet)
{
VMCPUSET_EMPTY(pDestCpuSet);
/*
* Physical destination mode only supports either a broadcast or a single target.
* - Broadcast with lowest-priority delivery mode is not supported[1], we deliver it
* as a regular broadcast like in fixed delivery mode.
* - For a single target, lowest-priority delivery mode makes no sense. We deliver
* to the target like in fixed delivery mode.
*
* [1] See Intel spec. 10.6.2.1 "Physical Destination Mode".
*/
if ( enmDestMode == XAPICDESTMODE_PHYSICAL
&& enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO)
{
AssertMsgFailed(("APIC: Lowest-priority delivery using physical destination mode!"));
enmDeliveryMode = XAPICDELIVERYMODE_FIXED;
}
uint32_t const cCpus = pVM->cCpus;
if (enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO)
{
Assert(enmDestMode == XAPICDESTMODE_LOGICAL);
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
VMCPUID idCpuLowestTpr = NIL_VMCPUID;
uint8_t u8LowestTpr = UINT8_C(0xff);
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
{
PVMCPUCC pVCpuDst = pVM->CTX_SUFF(apCpus)[idCpu];
if (apicIsLogicalDest(pVCpuDst, fDestMask))
{
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpuDst);
uint8_t const u8Tpr = pXApicPage->tpr.u8Tpr; /* PAV */
/*
* If there is a tie for lowest priority, the local APIC with the highest ID is chosen.
* Hence the use of "<=" in the check below.
* See AMD spec. 16.6.2 "Lowest Priority Messages and Arbitration".
*/
if (u8Tpr <= u8LowestTpr)
{
u8LowestTpr = u8Tpr;
idCpuLowestTpr = idCpu;
}
}
}
if (idCpuLowestTpr != NIL_VMCPUID)
VMCPUSET_ADD(pDestCpuSet, idCpuLowestTpr);
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
return;
}
/*
* x2APIC:
* - In both physical and logical destination mode, a destination mask of 0xffffffff implies a broadcast[1].
* xAPIC:
* - In physical destination mode, a destination mask of 0xff implies a broadcast[2].
* - In both flat and clustered logical mode, a destination mask of 0xff implies a broadcast[3].
*
* [1] See Intel spec. 10.12.9 "ICR Operation in x2APIC Mode".
* [2] See Intel spec. 10.6.2.1 "Physical Destination Mode".
* [2] See AMD spec. 16.6.1 "Receiving System and IPI Interrupts".
*/
if ((fDestMask & fBroadcastMask) == fBroadcastMask)
{
VMCPUSET_FILL(pDestCpuSet);
return;
}
if (enmDestMode == XAPICDESTMODE_PHYSICAL)
{
/* The destination mask is interpreted as the physical APIC ID of a single target. */
#if 1
/* Since our physical APIC ID is read-only to software, set the corresponding bit in the CPU set. */
if (RT_LIKELY(fDestMask < cCpus))
VMCPUSET_ADD(pDestCpuSet, fDestMask);
#else
/* The physical APIC ID may not match our VCPU ID, search through the list of targets. */
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
{
PVMCPUCC pVCpuDst = &pVM->aCpus[idCpu];
if (XAPIC_IN_X2APIC_MODE(pVCpuDst))
{
PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpuDst);
if (pX2ApicPage->id.u32ApicId == fDestMask)
VMCPUSET_ADD(pDestCpuSet, pVCpuDst->idCpu);
}
else
{
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpuDst);
if (pXApicPage->id.u8ApicId == (uint8_t)fDestMask)
VMCPUSET_ADD(pDestCpuSet, pVCpuDst->idCpu);
}
}
#endif
}
else
{
Assert(enmDestMode == XAPICDESTMODE_LOGICAL);
/* A destination mask of all 0's implies no target APICs (since it's interpreted as a bitmap or partial bitmap). */
if (RT_UNLIKELY(!fDestMask))
return;
/* The destination mask is interpreted as a bitmap of software-programmable logical APIC ID of the target APICs. */
for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
{
PVMCPUCC pVCpuDst = pVM->CTX_SUFF(apCpus)[idCpu];
if (apicIsLogicalDest(pVCpuDst, fDestMask))
VMCPUSET_ADD(pDestCpuSet, pVCpuDst->idCpu);
}
}
}
/**
* Sends an Interprocessor Interrupt (IPI) using values from the Interrupt
* Command Register (ICR).
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param rcRZ The return code if the operation cannot be
* performed in the current context.
*/
DECLINLINE(VBOXSTRICTRC) apicSendIpi(PVMCPUCC pVCpu, int rcRZ)
{
VMCPU_ASSERT_EMT(pVCpu);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
XAPICDELIVERYMODE const enmDeliveryMode = (XAPICDELIVERYMODE)pXApicPage->icr_lo.u.u3DeliveryMode;
XAPICDESTMODE const enmDestMode = (XAPICDESTMODE)pXApicPage->icr_lo.u.u1DestMode;
XAPICINITLEVEL const enmInitLevel = (XAPICINITLEVEL)pXApicPage->icr_lo.u.u1Level;
XAPICTRIGGERMODE const enmTriggerMode = (XAPICTRIGGERMODE)pXApicPage->icr_lo.u.u1TriggerMode;
XAPICDESTSHORTHAND const enmDestShorthand = (XAPICDESTSHORTHAND)pXApicPage->icr_lo.u.u2DestShorthand;
uint8_t const uVector = pXApicPage->icr_lo.u.u8Vector;
PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
uint32_t const fDest = XAPIC_IN_X2APIC_MODE(pVCpu) ? pX2ApicPage->icr_hi.u32IcrHi : pXApicPage->icr_hi.u.u8Dest;
Log5(("apicSendIpi: delivery=%u mode=%u init=%u trigger=%u short=%u vector=%#x fDest=%#x\n",
enmDeliveryMode, enmDestMode, enmInitLevel, enmTriggerMode, enmDestShorthand, uVector, fDest));
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
/*
* INIT Level De-assert is not support on Pentium 4 and Xeon processors.
* Apparently, this also applies to NMI, SMI, lowest-priority and fixed delivery modes,
* see @bugref{8245#c116}.
*
* See AMD spec. 16.5 "Interprocessor Interrupts (IPI)" for a table of valid ICR combinations.
*/
if ( enmTriggerMode == XAPICTRIGGERMODE_LEVEL
&& enmInitLevel == XAPICINITLEVEL_DEASSERT
&& ( enmDeliveryMode == XAPICDELIVERYMODE_FIXED
|| enmDeliveryMode == XAPICDELIVERYMODE_LOWEST_PRIO
|| enmDeliveryMode == XAPICDELIVERYMODE_SMI
|| enmDeliveryMode == XAPICDELIVERYMODE_NMI
|| enmDeliveryMode == XAPICDELIVERYMODE_INIT))
{
Log2(("APIC%u: %s level de-assert unsupported, ignoring!\n", pVCpu->idCpu, apicGetDeliveryModeName(enmDeliveryMode)));
return VINF_SUCCESS;
}
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
/*
* The destination and delivery modes are ignored/by-passed when a destination shorthand is specified.
* See Intel spec. 10.6.2.3 "Broadcast/Self Delivery Mode".
*/
VMCPUSET DestCpuSet;
switch (enmDestShorthand)
{
case XAPICDESTSHORTHAND_NONE:
{
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
uint32_t const fBroadcastMask = XAPIC_IN_X2APIC_MODE(pVCpu) ? X2APIC_ID_BROADCAST_MASK : XAPIC_ID_BROADCAST_MASK;
apicGetDestCpuSet(pVM, fDest, fBroadcastMask, enmDestMode, enmDeliveryMode, &DestCpuSet);
break;
}
case XAPICDESTSHORTHAND_SELF:
{
VMCPUSET_EMPTY(&DestCpuSet);
VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
break;
}
case XAPIDDESTSHORTHAND_ALL_INCL_SELF:
{
VMCPUSET_FILL(&DestCpuSet);
break;
}
case XAPICDESTSHORTHAND_ALL_EXCL_SELF:
{
VMCPUSET_FILL(&DestCpuSet);
VMCPUSET_DEL(&DestCpuSet, pVCpu->idCpu);
break;
}
}
return apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
}
/**
* Sets the Interrupt Command Register (ICR) high dword.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uIcrHi The ICR high dword.
*/
static VBOXSTRICTRC apicSetIcrHi(PVMCPUCC pVCpu, uint32_t uIcrHi)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
pXApicPage->icr_hi.all.u32IcrHi = uIcrHi & XAPIC_ICR_HI_DEST;
STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrHiWrite);
Log2(("APIC%u: apicSetIcrHi: uIcrHi=%#RX32\n", pVCpu->idCpu, pXApicPage->icr_hi.all.u32IcrHi));
return VINF_SUCCESS;
}
/**
* Sets the Interrupt Command Register (ICR) low dword.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uIcrLo The ICR low dword.
* @param rcRZ The return code if the operation cannot be performed
* in the current context.
* @param fUpdateStat Whether to update the ICR low write statistics
* counter.
*/
static VBOXSTRICTRC apicSetIcrLo(PVMCPUCC pVCpu, uint32_t uIcrLo, int rcRZ, bool fUpdateStat)
{
VMCPU_ASSERT_EMT(pVCpu);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
pXApicPage->icr_lo.all.u32IcrLo = uIcrLo & XAPIC_ICR_LO_WR_VALID;
Log2(("APIC%u: apicSetIcrLo: uIcrLo=%#RX32\n", pVCpu->idCpu, pXApicPage->icr_lo.all.u32IcrLo));
if (fUpdateStat)
STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrLoWrite);
RT_NOREF(fUpdateStat);
return apicSendIpi(pVCpu, rcRZ);
}
/**
* Sets the Interrupt Command Register (ICR).
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param u64Icr The ICR (High and Low combined).
* @param rcRZ The return code if the operation cannot be performed
* in the current context.
*
* @remarks This function is used by both x2APIC interface and the Hyper-V
* interface, see APICHvSetIcr. The Hyper-V spec isn't clear what
* happens when invalid bits are set. For the time being, it will
* \#GP like a regular x2APIC access.
*/
static VBOXSTRICTRC apicSetIcr(PVMCPUCC pVCpu, uint64_t u64Icr, int rcRZ)
{
VMCPU_ASSERT_EMT(pVCpu);
/* Validate. */
uint32_t const uLo = RT_LO_U32(u64Icr);
if (RT_LIKELY(!(uLo & ~XAPIC_ICR_LO_WR_VALID)))
{
/* Update high dword first, then update the low dword which sends the IPI. */
PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
pX2ApicPage->icr_hi.u32IcrHi = RT_HI_U32(u64Icr);
STAM_COUNTER_INC(&pVCpu->apic.s.StatIcrFullWrite);
return apicSetIcrLo(pVCpu, uLo, rcRZ, false /* fUpdateStat */);
}
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_ICR, APICMSRACCESS_WRITE_RSVD_BITS);
}
/**
* Sets the Error Status Register (ESR).
*
* @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
* @param pVCpu The cross context virtual CPU structure.
* @param uEsr The ESR value.
*/
static int apicSetEsr(PVMCPUCC pVCpu, uint32_t uEsr)
{
VMCPU_ASSERT_EMT(pVCpu);
Log2(("APIC%u: apicSetEsr: uEsr=%#RX32\n", pVCpu->idCpu, uEsr));
if ( XAPIC_IN_X2APIC_MODE(pVCpu)
&& (uEsr & ~XAPIC_ESR_WO_VALID))
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_ESR, APICMSRACCESS_WRITE_RSVD_BITS);
/*
* Writes to the ESR causes the internal state to be updated in the register,
* clearing the original state. See AMD spec. 16.4.6 "APIC Error Interrupts".
*/
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
pXApicPage->esr.all.u32Errors = apicClearAllErrors(pVCpu);
return VINF_SUCCESS;
}
/**
* Updates the Processor Priority Register (PPR).
*
* @param pVCpu The cross context virtual CPU structure.
*/
static void apicUpdatePpr(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT(pVCpu);
/* See Intel spec 10.8.3.1 "Task and Processor Priorities". */
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
uint8_t const uIsrv = apicGetHighestSetBitInReg(&pXApicPage->isr, 0 /* rcNotFound */);
uint8_t uPpr;
if (XAPIC_TPR_GET_TP(pXApicPage->tpr.u8Tpr) >= XAPIC_PPR_GET_PP(uIsrv))
uPpr = pXApicPage->tpr.u8Tpr;
else
uPpr = XAPIC_PPR_GET_PP(uIsrv);
pXApicPage->ppr.u8Ppr = uPpr;
}
/**
* Gets the Processor Priority Register (PPR).
*
* @returns The PPR value.
* @param pVCpu The cross context virtual CPU structure.
*/
static uint8_t apicGetPpr(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT(pVCpu);
STAM_COUNTER_INC(&pVCpu->apic.s.StatTprRead);
/*
* With virtualized APIC registers or with TPR virtualization, the hardware may
* update ISR/TPR transparently. We thus re-calculate the PPR which may be out of sync.
* See Intel spec. 29.2.2 "Virtual-Interrupt Delivery".
*
* In all other instances, whenever the TPR or ISR changes, we need to update the PPR
* as well (e.g. like we do manually in apicR3InitIpi and by calling apicUpdatePpr).
*/
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
if (pApic->fVirtApicRegsEnabled) /** @todo re-think this */
apicUpdatePpr(pVCpu);
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
return pXApicPage->ppr.u8Ppr;
}
/**
* Sets the Task Priority Register (TPR).
*
* @returns VINF_SUCCESS or VERR_CPUM_RAISE_GP_0.
* @param pVCpu The cross context virtual CPU structure.
* @param uTpr The TPR value.
* @param fForceX2ApicBehaviour Pretend the APIC is in x2APIC mode during
* this write.
*/
static int apicSetTprEx(PVMCPUCC pVCpu, uint32_t uTpr, bool fForceX2ApicBehaviour)
{
VMCPU_ASSERT_EMT(pVCpu);
Log2(("APIC%u: apicSetTprEx: uTpr=%#RX32\n", pVCpu->idCpu, uTpr));
STAM_COUNTER_INC(&pVCpu->apic.s.StatTprWrite);
bool const fX2ApicMode = XAPIC_IN_X2APIC_MODE(pVCpu) || fForceX2ApicBehaviour;
if ( fX2ApicMode
&& (uTpr & ~XAPIC_TPR_VALID))
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_TPR, APICMSRACCESS_WRITE_RSVD_BITS);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
pXApicPage->tpr.u8Tpr = uTpr;
apicUpdatePpr(pVCpu);
apicSignalNextPendingIntr(pVCpu);
return VINF_SUCCESS;
}
/**
* Sets the End-Of-Interrupt (EOI) register.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uEoi The EOI value.
* @param fForceX2ApicBehaviour Pretend the APIC is in x2APIC mode during
* this write.
*/
static VBOXSTRICTRC apicSetEoi(PVMCPUCC pVCpu, uint32_t uEoi, bool fForceX2ApicBehaviour)
{
VMCPU_ASSERT_EMT(pVCpu);
Log2(("APIC%u: apicSetEoi: uEoi=%#RX32\n", pVCpu->idCpu, uEoi));
STAM_COUNTER_INC(&pVCpu->apic.s.StatEoiWrite);
bool const fX2ApicMode = XAPIC_IN_X2APIC_MODE(pVCpu) || fForceX2ApicBehaviour;
if ( fX2ApicMode
&& (uEoi & ~XAPIC_EOI_WO_VALID))
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_EOI, APICMSRACCESS_WRITE_RSVD_BITS);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
int isrv = apicGetHighestSetBitInReg(&pXApicPage->isr, -1 /* rcNotFound */);
if (isrv >= 0)
{
/*
* Broadcast the EOI to the I/O APIC(s).
*
* We'll handle the EOI broadcast first as there is tiny chance we get rescheduled to
* ring-3 due to contention on the I/O APIC lock. This way we don't mess with the rest
* of the APIC state and simply restart the EOI write operation from ring-3.
*/
Assert(isrv <= (int)UINT8_MAX);
uint8_t const uVector = isrv;
bool const fLevelTriggered = apicTestVectorInReg(&pXApicPage->tmr, uVector);
if (fLevelTriggered)
{
PDMIoApicBroadcastEoi(pVCpu->CTX_SUFF(pVM), uVector);
/*
* Clear the vector from the TMR.
*
* The broadcast to I/O APIC can re-trigger new interrupts to arrive via the bus. However,
* APICUpdatePendingInterrupts() which updates TMR can only be done from EMT which we
* currently are on, so no possibility of concurrent updates.
*/
apicClearVectorInReg(&pXApicPage->tmr, uVector);
/*
* Clear the remote IRR bit for level-triggered, fixed mode LINT0 interrupt.
* The LINT1 pin does not support level-triggered interrupts.
* See Intel spec. 10.5.1 "Local Vector Table".
*/
uint32_t const uLvtLint0 = pXApicPage->lvt_lint0.all.u32LvtLint0;
if ( XAPIC_LVT_GET_REMOTE_IRR(uLvtLint0)
&& XAPIC_LVT_GET_VECTOR(uLvtLint0) == uVector
&& XAPIC_LVT_GET_DELIVERY_MODE(uLvtLint0) == XAPICDELIVERYMODE_FIXED)
{
ASMAtomicAndU32((volatile uint32_t *)&pXApicPage->lvt_lint0.all.u32LvtLint0, ~XAPIC_LVT_REMOTE_IRR);
Log2(("APIC%u: apicSetEoi: Cleared remote-IRR for LINT0. uVector=%#x\n", pVCpu->idCpu, uVector));
}
Log2(("APIC%u: apicSetEoi: Cleared level triggered interrupt from TMR. uVector=%#x\n", pVCpu->idCpu, uVector));
}
/*
* Mark interrupt as serviced, update the PPR and signal pending interrupts.
*/
Log2(("APIC%u: apicSetEoi: Clearing interrupt from ISR. uVector=%#x\n", pVCpu->idCpu, uVector));
apicClearVectorInReg(&pXApicPage->isr, uVector);
apicUpdatePpr(pVCpu);
apicSignalNextPendingIntr(pVCpu);
}
else
{
#ifdef DEBUG_ramshankar
/** @todo Figure out if this is done intentionally by guests or is a bug
* in our emulation. Happened with Win10 SMP VM during reboot after
* installation of guest additions with 3D support. */
AssertMsgFailed(("APIC%u: apicSetEoi: Failed to find any ISR bit\n", pVCpu->idCpu));
#endif
}
return VINF_SUCCESS;
}
/**
* Sets the Logical Destination Register (LDR).
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uLdr The LDR value.
*
* @remarks LDR is read-only in x2APIC mode.
*/
static VBOXSTRICTRC apicSetLdr(PVMCPUCC pVCpu, uint32_t uLdr)
{
VMCPU_ASSERT_EMT(pVCpu);
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu) || pApic->fHyperVCompatMode); RT_NOREF_PV(pApic);
Log2(("APIC%u: apicSetLdr: uLdr=%#RX32\n", pVCpu->idCpu, uLdr));
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
apicWriteRaw32(pXApicPage, XAPIC_OFF_LDR, uLdr & XAPIC_LDR_VALID);
STAM_COUNTER_INC(&pVCpu->apic.s.StatLdrWrite);
return VINF_SUCCESS;
}
/**
* Sets the Destination Format Register (DFR).
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uDfr The DFR value.
*
* @remarks DFR is not available in x2APIC mode.
*/
static VBOXSTRICTRC apicSetDfr(PVMCPUCC pVCpu, uint32_t uDfr)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
uDfr &= XAPIC_DFR_VALID;
uDfr |= XAPIC_DFR_RSVD_MB1;
Log2(("APIC%u: apicSetDfr: uDfr=%#RX32\n", pVCpu->idCpu, uDfr));
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
apicWriteRaw32(pXApicPage, XAPIC_OFF_DFR, uDfr);
STAM_COUNTER_INC(&pVCpu->apic.s.StatDfrWrite);
return VINF_SUCCESS;
}
/**
* Sets the Timer Divide Configuration Register (DCR).
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uTimerDcr The timer DCR value.
*/
static VBOXSTRICTRC apicSetTimerDcr(PVMCPUCC pVCpu, uint32_t uTimerDcr)
{
VMCPU_ASSERT_EMT(pVCpu);
if ( XAPIC_IN_X2APIC_MODE(pVCpu)
&& (uTimerDcr & ~XAPIC_TIMER_DCR_VALID))
return apicMsrAccessError(pVCpu, MSR_IA32_X2APIC_TIMER_DCR, APICMSRACCESS_WRITE_RSVD_BITS);
Log2(("APIC%u: apicSetTimerDcr: uTimerDcr=%#RX32\n", pVCpu->idCpu, uTimerDcr));
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
apicWriteRaw32(pXApicPage, XAPIC_OFF_TIMER_DCR, uTimerDcr);
STAM_COUNTER_INC(&pVCpu->apic.s.StatDcrWrite);
return VINF_SUCCESS;
}
/**
* Gets the timer's Current Count Register (CCR).
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pVCpu The cross context virtual CPU structure.
* @param rcBusy The busy return code for the timer critical section.
* @param puValue Where to store the LVT timer CCR.
*/
static VBOXSTRICTRC apicGetTimerCcr(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, int rcBusy, uint32_t *puValue)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(puValue);
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
*puValue = 0;
/* In TSC-deadline mode, CCR returns 0, see Intel spec. 10.5.4.1 "TSC-Deadline Mode". */
if (pXApicPage->lvt_timer.u.u2TimerMode == XAPIC_TIMER_MODE_TSC_DEADLINE)
return VINF_SUCCESS;
/* If the initial-count register is 0, CCR returns 0 as it cannot exceed the ICR. */
uint32_t const uInitialCount = pXApicPage->timer_icr.u32InitialCount;
if (!uInitialCount)
return VINF_SUCCESS;
/*
* Reading the virtual-sync clock requires locking its timer because it's not
* a simple atomic operation, see tmVirtualSyncGetEx().
*
* We also need to lock before reading the timer CCR, see apicR3TimerCallback().
*/
PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
TMTIMERHANDLE hTimer = pApicCpu->hTimer;
VBOXSTRICTRC rc = PDMDevHlpTimerLockClock(pDevIns, hTimer, rcBusy);
if (rc == VINF_SUCCESS)
{
/* If the current-count register is 0, it implies the timer expired. */
uint32_t const uCurrentCount = pXApicPage->timer_ccr.u32CurrentCount;
if (uCurrentCount)
{
uint64_t const cTicksElapsed = PDMDevHlpTimerGet(pDevIns, hTimer) - pApicCpu->u64TimerInitial;
PDMDevHlpTimerUnlockClock(pDevIns, hTimer);
uint8_t const uTimerShift = apicGetTimerShift(pXApicPage);
uint64_t const uDelta = cTicksElapsed >> uTimerShift;
if (uInitialCount > uDelta)
*puValue = uInitialCount - uDelta;
}
else
PDMDevHlpTimerUnlockClock(pDevIns, hTimer);
}
return rc;
}
/**
* Sets the timer's Initial-Count Register (ICR).
*
* @returns Strict VBox status code.
* @param pDevIns The device instance.
* @param pVCpu The cross context virtual CPU structure.
* @param rcBusy The busy return code for the timer critical section.
* @param uInitialCount The timer ICR.
*/
static VBOXSTRICTRC apicSetTimerIcr(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, int rcBusy, uint32_t uInitialCount)
{
VMCPU_ASSERT_EMT(pVCpu);
PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
Log2(("APIC%u: apicSetTimerIcr: uInitialCount=%#RX32\n", pVCpu->idCpu, uInitialCount));
STAM_COUNTER_INC(&pApicCpu->StatTimerIcrWrite);
/* In TSC-deadline mode, timer ICR writes are ignored, see Intel spec. 10.5.4.1 "TSC-Deadline Mode". */
if ( pApic->fSupportsTscDeadline
&& pXApicPage->lvt_timer.u.u2TimerMode == XAPIC_TIMER_MODE_TSC_DEADLINE)
return VINF_SUCCESS;
/*
* The timer CCR may be modified by apicR3TimerCallback() in parallel,
* so obtain the lock -before- updating it here to be consistent with the
* timer ICR. We rely on CCR being consistent in apicGetTimerCcr().
*/
TMTIMERHANDLE hTimer = pApicCpu->hTimer;
VBOXSTRICTRC rc = PDMDevHlpTimerLockClock(pDevIns, hTimer, rcBusy);
if (rc == VINF_SUCCESS)
{
pXApicPage->timer_icr.u32InitialCount = uInitialCount;
pXApicPage->timer_ccr.u32CurrentCount = uInitialCount;
if (uInitialCount)
apicStartTimer(pVCpu, uInitialCount);
else
apicStopTimer(pVCpu);
PDMDevHlpTimerUnlockClock(pDevIns, hTimer);
}
return rc;
}
/**
* Sets an LVT entry.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param offLvt The LVT entry offset in the xAPIC page.
* @param uLvt The LVT value to set.
*/
static VBOXSTRICTRC apicSetLvtEntry(PVMCPUCC pVCpu, uint16_t offLvt, uint32_t uLvt)
{
VMCPU_ASSERT_EMT(pVCpu);
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
AssertMsg( offLvt == XAPIC_OFF_LVT_TIMER
|| offLvt == XAPIC_OFF_LVT_THERMAL
|| offLvt == XAPIC_OFF_LVT_PERF
|| offLvt == XAPIC_OFF_LVT_LINT0
|| offLvt == XAPIC_OFF_LVT_LINT1
|| offLvt == XAPIC_OFF_LVT_ERROR,
("APIC%u: apicSetLvtEntry: invalid offset, offLvt=%#RX16, uLvt=%#RX32\n", pVCpu->idCpu, offLvt, uLvt));
/*
* If TSC-deadline mode isn't support, ignore the bit in xAPIC mode
* and raise #GP(0) in x2APIC mode.
*/
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
if (offLvt == XAPIC_OFF_LVT_TIMER)
{
STAM_COUNTER_INC(&pVCpu->apic.s.StatLvtTimerWrite);
if ( !pApic->fSupportsTscDeadline
&& (uLvt & XAPIC_LVT_TIMER_TSCDEADLINE))
{
if (XAPIC_IN_X2APIC_MODE(pVCpu))
return apicMsrAccessError(pVCpu, XAPIC_GET_X2APIC_MSR(offLvt), APICMSRACCESS_WRITE_RSVD_BITS);
uLvt &= ~XAPIC_LVT_TIMER_TSCDEADLINE;
/** @todo TSC-deadline timer mode transition */
}
}
/*
* Validate rest of the LVT bits.
*/
uint16_t const idxLvt = (offLvt - XAPIC_OFF_LVT_START) >> 4;
AssertReturn(idxLvt < RT_ELEMENTS(g_au32LvtValidMasks), VERR_OUT_OF_RANGE);
/*
* For x2APIC, disallow setting of invalid/reserved bits.
* For xAPIC, mask out invalid/reserved bits (i.e. ignore them).
*/
if ( XAPIC_IN_X2APIC_MODE(pVCpu)
&& (uLvt & ~g_au32LvtValidMasks[idxLvt]))
return apicMsrAccessError(pVCpu, XAPIC_GET_X2APIC_MSR(offLvt), APICMSRACCESS_WRITE_RSVD_BITS);
uLvt &= g_au32LvtValidMasks[idxLvt];
/*
* In the software-disabled state, LVT mask-bit must remain set and attempts to clear the mask
* bit must be ignored. See Intel spec. 10.4.7.2 "Local APIC State After It Has Been Software Disabled".
*/
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
if (!pXApicPage->svr.u.fApicSoftwareEnable)
uLvt |= XAPIC_LVT_MASK;
/*
* It is unclear whether we should signal a 'send illegal vector' error here and ignore updating
* the LVT entry when the delivery mode is 'fixed'[1] or update it in addition to signalling the
* error or not signal the error at all. For now, we'll allow setting illegal vectors into the LVT
* but set the 'send illegal vector' error here. The 'receive illegal vector' error will be set if
* the interrupt for the vector happens to be generated, see apicPostInterrupt().
*
* [1] See Intel spec. 10.5.2 "Valid Interrupt Vectors".
*/
if (RT_UNLIKELY( XAPIC_LVT_GET_VECTOR(uLvt) <= XAPIC_ILLEGAL_VECTOR_END
&& XAPIC_LVT_GET_DELIVERY_MODE(uLvt) == XAPICDELIVERYMODE_FIXED))
apicSetError(pVCpu, XAPIC_ESR_SEND_ILLEGAL_VECTOR);
Log2(("APIC%u: apicSetLvtEntry: offLvt=%#RX16 uLvt=%#RX32\n", pVCpu->idCpu, offLvt, uLvt));
apicWriteRaw32(pXApicPage, offLvt, uLvt);
return VINF_SUCCESS;
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif /* XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4 */
}
#if 0
/**
* Sets an LVT entry in the extended LVT range.
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param offLvt The LVT entry offset in the xAPIC page.
* @param uValue The LVT value to set.
*/
static int apicSetLvtExtEntry(PVMCPUCC pVCpu, uint16_t offLvt, uint32_t uLvt)
{
VMCPU_ASSERT_EMT(pVCpu);
AssertMsg(offLvt == XAPIC_OFF_CMCI, ("APIC%u: apicSetLvt1Entry: invalid offset %#RX16\n", pVCpu->idCpu, offLvt));
/** @todo support CMCI. */
return VERR_NOT_IMPLEMENTED;
}
#endif
/**
* Hints TM about the APIC timer frequency.
*
* @param pDevIns The device instance.
* @param pApicCpu The APIC CPU state.
* @param uInitialCount The new initial count.
* @param uTimerShift The new timer shift.
* @thread Any.
*/
void apicHintTimerFreq(PPDMDEVINS pDevIns, PAPICCPU pApicCpu, uint32_t uInitialCount, uint8_t uTimerShift)
{
Assert(pApicCpu);
if ( pApicCpu->uHintedTimerInitialCount != uInitialCount
|| pApicCpu->uHintedTimerShift != uTimerShift)
{
uint32_t uHz;
if (uInitialCount)
{
uint64_t cTicksPerPeriod = (uint64_t)uInitialCount << uTimerShift;
uHz = PDMDevHlpTimerGetFreq(pDevIns, pApicCpu->hTimer) / cTicksPerPeriod;
}
else
uHz = 0;
PDMDevHlpTimerSetFrequencyHint(pDevIns, pApicCpu->hTimer, uHz);
pApicCpu->uHintedTimerInitialCount = uInitialCount;
pApicCpu->uHintedTimerShift = uTimerShift;
}
}
/**
* Gets the Interrupt Command Register (ICR), without performing any interface
* checks.
*
* @returns The ICR value.
* @param pVCpu The cross context virtual CPU structure.
*/
DECLINLINE(uint64_t) apicGetIcrNoCheck(PVMCPUCC pVCpu)
{
PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpu);
uint64_t const uHi = pX2ApicPage->icr_hi.u32IcrHi;
uint64_t const uLo = pX2ApicPage->icr_lo.all.u32IcrLo;
uint64_t const uIcr = RT_MAKE_U64(uLo, uHi);
return uIcr;
}
/**
* Reads an APIC register.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pVCpu The cross context virtual CPU structure.
* @param offReg The offset of the register being read.
* @param puValue Where to store the register value.
*/
DECLINLINE(VBOXSTRICTRC) apicReadRegister(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t *puValue)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(offReg <= XAPIC_OFF_MAX_VALID);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
uint32_t uValue = 0;
VBOXSTRICTRC rc = VINF_SUCCESS;
switch (offReg)
{
case XAPIC_OFF_ID:
case XAPIC_OFF_VERSION:
case XAPIC_OFF_TPR:
case XAPIC_OFF_EOI:
case XAPIC_OFF_RRD:
case XAPIC_OFF_LDR:
case XAPIC_OFF_DFR:
case XAPIC_OFF_SVR:
case XAPIC_OFF_ISR0: case XAPIC_OFF_ISR1: case XAPIC_OFF_ISR2: case XAPIC_OFF_ISR3:
case XAPIC_OFF_ISR4: case XAPIC_OFF_ISR5: case XAPIC_OFF_ISR6: case XAPIC_OFF_ISR7:
case XAPIC_OFF_TMR0: case XAPIC_OFF_TMR1: case XAPIC_OFF_TMR2: case XAPIC_OFF_TMR3:
case XAPIC_OFF_TMR4: case XAPIC_OFF_TMR5: case XAPIC_OFF_TMR6: case XAPIC_OFF_TMR7:
case XAPIC_OFF_IRR0: case XAPIC_OFF_IRR1: case XAPIC_OFF_IRR2: case XAPIC_OFF_IRR3:
case XAPIC_OFF_IRR4: case XAPIC_OFF_IRR5: case XAPIC_OFF_IRR6: case XAPIC_OFF_IRR7:
case XAPIC_OFF_ESR:
case XAPIC_OFF_ICR_LO:
case XAPIC_OFF_ICR_HI:
case XAPIC_OFF_LVT_TIMER:
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
case XAPIC_OFF_LVT_THERMAL:
#endif
case XAPIC_OFF_LVT_PERF:
case XAPIC_OFF_LVT_LINT0:
case XAPIC_OFF_LVT_LINT1:
case XAPIC_OFF_LVT_ERROR:
case XAPIC_OFF_TIMER_ICR:
case XAPIC_OFF_TIMER_DCR:
{
Assert( !XAPIC_IN_X2APIC_MODE(pVCpu)
|| ( offReg != XAPIC_OFF_DFR
&& offReg != XAPIC_OFF_ICR_HI
&& offReg != XAPIC_OFF_EOI));
uValue = apicReadRaw32(pXApicPage, offReg);
Log2(("APIC%u: apicReadRegister: offReg=%#x uValue=%#x\n", pVCpu->idCpu, offReg, uValue));
break;
}
case XAPIC_OFF_PPR:
{
uValue = apicGetPpr(pVCpu);
break;
}
case XAPIC_OFF_TIMER_CCR:
{
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
rc = apicGetTimerCcr(pDevIns, pVCpu, VINF_IOM_R3_MMIO_READ, &uValue);
break;
}
case XAPIC_OFF_APR:
{
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
/* Unsupported on Pentium 4 and Xeon CPUs, invalid in x2APIC mode. */
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
break;
}
default:
{
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "VCPU[%u]: offReg=%#RX16\n", pVCpu->idCpu, offReg);
apicSetError(pVCpu, XAPIC_ESR_ILLEGAL_REG_ADDRESS);
break;
}
}
*puValue = uValue;
return rc;
}
/**
* Writes an APIC register.
*
* @returns Strict VBox status code.
* @param pDevIns The device instance.
* @param pVCpu The cross context virtual CPU structure.
* @param offReg The offset of the register being written.
* @param uValue The register value.
*/
DECLINLINE(VBOXSTRICTRC) apicWriteRegister(PPDMDEVINS pDevIns, PVMCPUCC pVCpu, uint16_t offReg, uint32_t uValue)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(offReg <= XAPIC_OFF_MAX_VALID);
Assert(!XAPIC_IN_X2APIC_MODE(pVCpu));
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
switch (offReg)
{
case XAPIC_OFF_TPR:
{
rcStrict = apicSetTprEx(pVCpu, uValue, false /* fForceX2ApicBehaviour */);
break;
}
case XAPIC_OFF_LVT_TIMER:
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
case XAPIC_OFF_LVT_THERMAL:
#endif
case XAPIC_OFF_LVT_PERF:
case XAPIC_OFF_LVT_LINT0:
case XAPIC_OFF_LVT_LINT1:
case XAPIC_OFF_LVT_ERROR:
{
rcStrict = apicSetLvtEntry(pVCpu, offReg, uValue);
break;
}
case XAPIC_OFF_TIMER_ICR:
{
rcStrict = apicSetTimerIcr(pDevIns, pVCpu, VINF_IOM_R3_MMIO_WRITE, uValue);
break;
}
case XAPIC_OFF_EOI:
{
rcStrict = apicSetEoi(pVCpu, uValue, false /* fForceX2ApicBehaviour */);
break;
}
case XAPIC_OFF_LDR:
{
rcStrict = apicSetLdr(pVCpu, uValue);
break;
}
case XAPIC_OFF_DFR:
{
rcStrict = apicSetDfr(pVCpu, uValue);
break;
}
case XAPIC_OFF_SVR:
{
rcStrict = apicSetSvr(pVCpu, uValue);
break;
}
case XAPIC_OFF_ICR_LO:
{
rcStrict = apicSetIcrLo(pVCpu, uValue, VINF_IOM_R3_MMIO_WRITE, true /* fUpdateStat */);
break;
}
case XAPIC_OFF_ICR_HI:
{
rcStrict = apicSetIcrHi(pVCpu, uValue);
break;
}
case XAPIC_OFF_TIMER_DCR:
{
rcStrict = apicSetTimerDcr(pVCpu, uValue);
break;
}
case XAPIC_OFF_ESR:
{
rcStrict = apicSetEsr(pVCpu, uValue);
break;
}
case XAPIC_OFF_APR:
case XAPIC_OFF_RRD:
{
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
/* Unsupported on Pentium 4 and Xeon CPUs but writes do -not- set an illegal register access error. */
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
break;
}
/* Read-only, write ignored: */
case XAPIC_OFF_VERSION:
case XAPIC_OFF_ID:
break;
/* Unavailable/reserved in xAPIC mode: */
case X2APIC_OFF_SELF_IPI:
/* Read-only registers: */
case XAPIC_OFF_PPR:
case XAPIC_OFF_ISR0: case XAPIC_OFF_ISR1: case XAPIC_OFF_ISR2: case XAPIC_OFF_ISR3:
case XAPIC_OFF_ISR4: case XAPIC_OFF_ISR5: case XAPIC_OFF_ISR6: case XAPIC_OFF_ISR7:
case XAPIC_OFF_TMR0: case XAPIC_OFF_TMR1: case XAPIC_OFF_TMR2: case XAPIC_OFF_TMR3:
case XAPIC_OFF_TMR4: case XAPIC_OFF_TMR5: case XAPIC_OFF_TMR6: case XAPIC_OFF_TMR7:
case XAPIC_OFF_IRR0: case XAPIC_OFF_IRR1: case XAPIC_OFF_IRR2: case XAPIC_OFF_IRR3:
case XAPIC_OFF_IRR4: case XAPIC_OFF_IRR5: case XAPIC_OFF_IRR6: case XAPIC_OFF_IRR7:
case XAPIC_OFF_TIMER_CCR:
default:
{
rcStrict = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "APIC%u: offReg=%#RX16\n", pVCpu->idCpu, offReg);
apicSetError(pVCpu, XAPIC_ESR_ILLEGAL_REG_ADDRESS);
break;
}
}
return rcStrict;
}
/**
* Reads an APIC MSR.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param u32Reg The MSR being read.
* @param pu64Value Where to store the read value.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICReadMsr(PVMCPUCC pVCpu, uint32_t u32Reg, uint64_t *pu64Value)
{
/*
* Validate.
*/
VMCPU_ASSERT_EMT(pVCpu);
Assert(u32Reg >= MSR_IA32_X2APIC_ID && u32Reg <= MSR_IA32_X2APIC_SELF_IPI);
Assert(pu64Value);
/*
* Is the APIC enabled?
*/
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
if (APICIsEnabled(pVCpu))
{ /* likely */ }
else
return apicMsrAccessError(pVCpu, u32Reg, pApic->enmMaxMode == PDMAPICMODE_NONE ?
APICMSRACCESS_READ_DISALLOWED_CONFIG : APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
#ifndef IN_RING3
if (pApic->CTXALLMID(f,Enabled))
{ /* likely */}
else
return VINF_CPUM_R3_MSR_READ;
#endif
STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMsrRead));
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
if (RT_LIKELY( XAPIC_IN_X2APIC_MODE(pVCpu)
|| pApic->fHyperVCompatMode))
{
switch (u32Reg)
{
/* Special handling for x2APIC: */
case MSR_IA32_X2APIC_ICR:
{
*pu64Value = apicGetIcrNoCheck(pVCpu);
break;
}
/* Special handling, compatible with xAPIC: */
case MSR_IA32_X2APIC_TIMER_CCR:
{
uint32_t uValue;
rcStrict = apicGetTimerCcr(VMCPU_TO_DEVINS(pVCpu), pVCpu, VINF_CPUM_R3_MSR_READ, &uValue);
*pu64Value = uValue;
break;
}
/* Special handling, compatible with xAPIC: */
case MSR_IA32_X2APIC_PPR:
{
*pu64Value = apicGetPpr(pVCpu);
break;
}
/* Raw read, compatible with xAPIC: */
case MSR_IA32_X2APIC_ID:
{
STAM_COUNTER_INC(&pVCpu->apic.s.StatIdMsrRead);
/* Horrible macOS hack (sample rdmsr addres: 0008:ffffff801686f21a). */
if ( !pApic->fMacOSWorkaround
|| pVCpu->cpum.GstCtx.cs.Sel != 8
|| pVCpu->cpum.GstCtx.rip < UINT64_C(0xffffff8000000000))
{ /* likely */ }
else
{
PCX2APICPAGE pX2ApicPage = VMCPU_TO_CX2APICPAGE(pVCpu);
uint32_t const idApic = pX2ApicPage->id.u32ApicId;
*pu64Value = (idApic << 24) | idApic;
Log(("APIC: Applying macOS hack to MSR_IA32_X2APIC_ID: %#RX64\n", *pu64Value));
break;
}
RT_FALL_THRU();
}
case MSR_IA32_X2APIC_VERSION:
case MSR_IA32_X2APIC_TPR:
case MSR_IA32_X2APIC_LDR:
case MSR_IA32_X2APIC_SVR:
case MSR_IA32_X2APIC_ISR0: case MSR_IA32_X2APIC_ISR1: case MSR_IA32_X2APIC_ISR2: case MSR_IA32_X2APIC_ISR3:
case MSR_IA32_X2APIC_ISR4: case MSR_IA32_X2APIC_ISR5: case MSR_IA32_X2APIC_ISR6: case MSR_IA32_X2APIC_ISR7:
case MSR_IA32_X2APIC_TMR0: case MSR_IA32_X2APIC_TMR1: case MSR_IA32_X2APIC_TMR2: case MSR_IA32_X2APIC_TMR3:
case MSR_IA32_X2APIC_TMR4: case MSR_IA32_X2APIC_TMR5: case MSR_IA32_X2APIC_TMR6: case MSR_IA32_X2APIC_TMR7:
case MSR_IA32_X2APIC_IRR0: case MSR_IA32_X2APIC_IRR1: case MSR_IA32_X2APIC_IRR2: case MSR_IA32_X2APIC_IRR3:
case MSR_IA32_X2APIC_IRR4: case MSR_IA32_X2APIC_IRR5: case MSR_IA32_X2APIC_IRR6: case MSR_IA32_X2APIC_IRR7:
case MSR_IA32_X2APIC_ESR:
case MSR_IA32_X2APIC_LVT_TIMER:
case MSR_IA32_X2APIC_LVT_THERMAL:
case MSR_IA32_X2APIC_LVT_PERF:
case MSR_IA32_X2APIC_LVT_LINT0:
case MSR_IA32_X2APIC_LVT_LINT1:
case MSR_IA32_X2APIC_LVT_ERROR:
case MSR_IA32_X2APIC_TIMER_ICR:
case MSR_IA32_X2APIC_TIMER_DCR:
{
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
uint16_t const offReg = X2APIC_GET_XAPIC_OFF(u32Reg);
*pu64Value = apicReadRaw32(pXApicPage, offReg);
break;
}
/* Write-only MSRs: */
case MSR_IA32_X2APIC_SELF_IPI:
case MSR_IA32_X2APIC_EOI:
{
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_WRITE_ONLY);
break;
}
/*
* Windows guest using Hyper-V x2APIC MSR compatibility mode tries to read the "high"
* LDR bits, which is quite absurd (as it's a 32-bit register) using this invalid MSR
* index (0x80E), see @bugref{8382#c175}.
*/
case MSR_IA32_X2APIC_LDR + 1:
{
if (pApic->fHyperVCompatMode)
*pu64Value = 0;
else
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
break;
}
/* Reserved MSRs: */
case MSR_IA32_X2APIC_LVT_CMCI:
default:
{
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_READ_RSVD_OR_UNKNOWN);
break;
}
}
}
else
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_INVALID_READ_MODE);
return rcStrict;
}
/**
* Writes an APIC MSR.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param u32Reg The MSR being written.
* @param u64Value The value to write.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICWriteMsr(PVMCPUCC pVCpu, uint32_t u32Reg, uint64_t u64Value)
{
/*
* Validate.
*/
VMCPU_ASSERT_EMT(pVCpu);
Assert(u32Reg >= MSR_IA32_X2APIC_ID && u32Reg <= MSR_IA32_X2APIC_SELF_IPI);
/*
* Is the APIC enabled?
*/
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
if (APICIsEnabled(pVCpu))
{ /* likely */ }
else
return apicMsrAccessError(pVCpu, u32Reg, pApic->enmMaxMode == PDMAPICMODE_NONE ?
APICMSRACCESS_WRITE_DISALLOWED_CONFIG : APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
#ifndef IN_RING3
if (pApic->CTXALLMID(f,Enabled))
{ /* likely */ }
else
return VINF_CPUM_R3_MSR_WRITE;
#endif
STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMsrWrite));
/*
* In x2APIC mode, we need to raise #GP(0) for writes to reserved bits, unlike MMIO
* accesses where they are ignored. Hence, we need to validate each register before
* invoking the generic/xAPIC write functions.
*
* Bits 63:32 of all registers except the ICR are reserved, we'll handle this common
* case first and handle validating the remaining bits on a per-register basis.
* See Intel spec. 10.12.1.2 "x2APIC Register Address Space".
*/
if ( u32Reg != MSR_IA32_X2APIC_ICR
&& RT_HI_U32(u64Value))
return apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_BITS);
uint32_t u32Value = RT_LO_U32(u64Value);
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
if (RT_LIKELY( XAPIC_IN_X2APIC_MODE(pVCpu)
|| pApic->fHyperVCompatMode))
{
switch (u32Reg)
{
case MSR_IA32_X2APIC_TPR:
{
rcStrict = apicSetTprEx(pVCpu, u32Value, false /* fForceX2ApicBehaviour */);
break;
}
case MSR_IA32_X2APIC_ICR:
{
rcStrict = apicSetIcr(pVCpu, u64Value, VINF_CPUM_R3_MSR_WRITE);
break;
}
case MSR_IA32_X2APIC_SVR:
{
rcStrict = apicSetSvr(pVCpu, u32Value);
break;
}
case MSR_IA32_X2APIC_ESR:
{
rcStrict = apicSetEsr(pVCpu, u32Value);
break;
}
case MSR_IA32_X2APIC_TIMER_DCR:
{
rcStrict = apicSetTimerDcr(pVCpu, u32Value);
break;
}
case MSR_IA32_X2APIC_LVT_TIMER:
case MSR_IA32_X2APIC_LVT_THERMAL:
case MSR_IA32_X2APIC_LVT_PERF:
case MSR_IA32_X2APIC_LVT_LINT0:
case MSR_IA32_X2APIC_LVT_LINT1:
case MSR_IA32_X2APIC_LVT_ERROR:
{
rcStrict = apicSetLvtEntry(pVCpu, X2APIC_GET_XAPIC_OFF(u32Reg), u32Value);
break;
}
case MSR_IA32_X2APIC_TIMER_ICR:
{
rcStrict = apicSetTimerIcr(VMCPU_TO_DEVINS(pVCpu), pVCpu, VINF_CPUM_R3_MSR_WRITE, u32Value);
break;
}
/* Write-only MSRs: */
case MSR_IA32_X2APIC_SELF_IPI:
{
uint8_t const uVector = XAPIC_SELF_IPI_GET_VECTOR(u32Value);
apicPostInterrupt(pVCpu, uVector, XAPICTRIGGERMODE_EDGE, 0 /* uSrcTag */);
rcStrict = VINF_SUCCESS;
break;
}
case MSR_IA32_X2APIC_EOI:
{
rcStrict = apicSetEoi(pVCpu, u32Value, false /* fForceX2ApicBehaviour */);
break;
}
/*
* Windows guest using Hyper-V x2APIC MSR compatibility mode tries to write the "high"
* LDR bits, which is quite absurd (as it's a 32-bit register) using this invalid MSR
* index (0x80E). The write value was 0xffffffff on a Windows 8.1 64-bit guest. We can
* safely ignore this nonsense, See @bugref{8382#c7}.
*/
case MSR_IA32_X2APIC_LDR + 1:
{
if (pApic->fHyperVCompatMode)
rcStrict = VINF_SUCCESS;
else
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
break;
}
/* Special-treament (read-only normally, but not with Hyper-V) */
case MSR_IA32_X2APIC_LDR:
{
if (pApic->fHyperVCompatMode)
{
rcStrict = apicSetLdr(pVCpu, u32Value);
break;
}
}
RT_FALL_THRU();
/* Read-only MSRs: */
case MSR_IA32_X2APIC_ID:
case MSR_IA32_X2APIC_VERSION:
case MSR_IA32_X2APIC_PPR:
case MSR_IA32_X2APIC_ISR0: case MSR_IA32_X2APIC_ISR1: case MSR_IA32_X2APIC_ISR2: case MSR_IA32_X2APIC_ISR3:
case MSR_IA32_X2APIC_ISR4: case MSR_IA32_X2APIC_ISR5: case MSR_IA32_X2APIC_ISR6: case MSR_IA32_X2APIC_ISR7:
case MSR_IA32_X2APIC_TMR0: case MSR_IA32_X2APIC_TMR1: case MSR_IA32_X2APIC_TMR2: case MSR_IA32_X2APIC_TMR3:
case MSR_IA32_X2APIC_TMR4: case MSR_IA32_X2APIC_TMR5: case MSR_IA32_X2APIC_TMR6: case MSR_IA32_X2APIC_TMR7:
case MSR_IA32_X2APIC_IRR0: case MSR_IA32_X2APIC_IRR1: case MSR_IA32_X2APIC_IRR2: case MSR_IA32_X2APIC_IRR3:
case MSR_IA32_X2APIC_IRR4: case MSR_IA32_X2APIC_IRR5: case MSR_IA32_X2APIC_IRR6: case MSR_IA32_X2APIC_IRR7:
case MSR_IA32_X2APIC_TIMER_CCR:
{
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_READ_ONLY);
break;
}
/* Reserved MSRs: */
case MSR_IA32_X2APIC_LVT_CMCI:
default:
{
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_WRITE_RSVD_OR_UNKNOWN);
break;
}
}
}
else
rcStrict = apicMsrAccessError(pVCpu, u32Reg, APICMSRACCESS_INVALID_WRITE_MODE);
return rcStrict;
}
/**
* Resets the APIC base MSR.
*
* @param pVCpu The cross context virtual CPU structure.
*/
static void apicResetBaseMsr(PVMCPUCC pVCpu)
{
/*
* Initialize the APIC base MSR. The APIC enable-bit is set upon power-up or reset[1].
*
* A Reset (in xAPIC and x2APIC mode) brings up the local APIC in xAPIC mode.
* An INIT IPI does -not- cause a transition between xAPIC and x2APIC mode[2].
*
* [1] See AMD spec. 14.1.3 "Processor Initialization State"
* [2] See Intel spec. 10.12.5.1 "x2APIC States".
*/
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
/* Construct. */
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
uint64_t uApicBaseMsr = MSR_IA32_APICBASE_ADDR;
if (pVCpu->idCpu == 0)
uApicBaseMsr |= MSR_IA32_APICBASE_BSP;
/* If the VM was configured with no APIC, don't enable xAPIC mode, obviously. */
if (pApic->enmMaxMode != PDMAPICMODE_NONE)
{
uApicBaseMsr |= MSR_IA32_APICBASE_EN;
/*
* While coming out of a reset the APIC is enabled and in xAPIC mode. If software had previously
* disabled the APIC (which results in the CPUID bit being cleared as well) we re-enable it here.
* See Intel spec. 10.12.5.1 "x2APIC States".
*/
if (CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, true /*fVisible*/) == false)
LogRel(("APIC%u: Resetting mode to xAPIC\n", pVCpu->idCpu));
}
/* Commit. */
ASMAtomicWriteU64(&pApicCpu->uApicBaseMsr, uApicBaseMsr);
}
/**
* Initializes per-VCPU APIC to the state following an INIT reset
* ("Wait-for-SIPI" state).
*
* @param pVCpu The cross context virtual CPU structure.
*/
void apicInitIpi(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
/*
* See Intel spec. 10.4.7.3 "Local APIC State After an INIT Reset (Wait-for-SIPI State)"
* and AMD spec 16.3.2 "APIC Registers".
*
* The reason we don't simply zero out the entire APIC page and only set the non-zero members
* is because there are some registers that are not touched by the INIT IPI (e.g. version)
* operation and this function is only a subset of the reset operation.
*/
RT_ZERO(pXApicPage->irr);
RT_ZERO(pXApicPage->irr);
RT_ZERO(pXApicPage->isr);
RT_ZERO(pXApicPage->tmr);
RT_ZERO(pXApicPage->icr_hi);
RT_ZERO(pXApicPage->icr_lo);
RT_ZERO(pXApicPage->ldr);
RT_ZERO(pXApicPage->tpr);
RT_ZERO(pXApicPage->ppr);
RT_ZERO(pXApicPage->timer_icr);
RT_ZERO(pXApicPage->timer_ccr);
RT_ZERO(pXApicPage->timer_dcr);
pXApicPage->dfr.u.u4Model = XAPICDESTFORMAT_FLAT;
pXApicPage->dfr.u.u28ReservedMb1 = UINT32_C(0xfffffff);
/** @todo CMCI. */
RT_ZERO(pXApicPage->lvt_timer);
pXApicPage->lvt_timer.u.u1Mask = 1;
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
RT_ZERO(pXApicPage->lvt_thermal);
pXApicPage->lvt_thermal.u.u1Mask = 1;
#endif
RT_ZERO(pXApicPage->lvt_perf);
pXApicPage->lvt_perf.u.u1Mask = 1;
RT_ZERO(pXApicPage->lvt_lint0);
pXApicPage->lvt_lint0.u.u1Mask = 1;
RT_ZERO(pXApicPage->lvt_lint1);
pXApicPage->lvt_lint1.u.u1Mask = 1;
RT_ZERO(pXApicPage->lvt_error);
pXApicPage->lvt_error.u.u1Mask = 1;
RT_ZERO(pXApicPage->svr);
pXApicPage->svr.u.u8SpuriousVector = 0xff;
/* The self-IPI register is reset to 0. See Intel spec. 10.12.5.1 "x2APIC States" */
PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
RT_ZERO(pX2ApicPage->self_ipi);
/* Clear the pending-interrupt bitmaps. */
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
RT_BZERO(&pApicCpu->ApicPibLevel, sizeof(APICPIB));
RT_BZERO(pApicCpu->CTX_SUFF(pvApicPib), sizeof(APICPIB));
/* Clear the interrupt line states for LINT0 and LINT1 pins. */
pApicCpu->fActiveLint0 = false;
pApicCpu->fActiveLint1 = false;
}
/**
* Initializes per-VCPU APIC to the state following a power-up or hardware
* reset.
*
* @param pVCpu The cross context virtual CPU structure.
* @param fResetApicBaseMsr Whether to reset the APIC base MSR.
*/
void apicResetCpu(PVMCPUCC pVCpu, bool fResetApicBaseMsr)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
LogFlow(("APIC%u: apicR3ResetCpu: fResetApicBaseMsr=%RTbool\n", pVCpu->idCpu, fResetApicBaseMsr));
#ifdef VBOX_STRICT
/* Verify that the initial APIC ID reported via CPUID matches our VMCPU ID assumption. */
uint32_t uEax, uEbx, uEcx, uEdx;
uEax = uEbx = uEcx = uEdx = UINT32_MAX;
CPUMGetGuestCpuId(pVCpu, 1, 0, -1 /*f64BitMode*/, &uEax, &uEbx, &uEcx, &uEdx);
Assert(((uEbx >> 24) & 0xff) == pVCpu->idCpu);
#endif
/*
* The state following a power-up or reset is a superset of the INIT state.
* See Intel spec. 10.4.7.3 "Local APIC State After an INIT Reset ('Wait-for-SIPI' State)"
*/
apicInitIpi(pVCpu);
/*
* The APIC version register is read-only, so just initialize it here.
* It is not clear from the specs, where exactly it is initialized.
* The version determines the number of LVT entries and size of the APIC ID (8 bits for P4).
*/
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
#if XAPIC_HARDWARE_VERSION == XAPIC_HARDWARE_VERSION_P4
pXApicPage->version.u.u8MaxLvtEntry = XAPIC_MAX_LVT_ENTRIES_P4 - 1;
pXApicPage->version.u.u8Version = XAPIC_HARDWARE_VERSION_P4;
AssertCompile(sizeof(pXApicPage->id.u8ApicId) >= XAPIC_APIC_ID_BIT_COUNT_P4 / 8);
#else
# error "Implement Pentium and P6 family APIC architectures"
#endif
/** @todo It isn't clear in the spec. where exactly the default base address
* is (re)initialized, atm we do it here in Reset. */
if (fResetApicBaseMsr)
apicResetBaseMsr(pVCpu);
/*
* Initialize the APIC ID register to xAPIC format.
*/
ASMMemZero32(&pXApicPage->id, sizeof(pXApicPage->id));
pXApicPage->id.u8ApicId = pVCpu->idCpu;
}
/**
* Sets the APIC base MSR.
*
* @returns VBox status code - no informational ones, esp. not
* VINF_CPUM_R3_MSR_WRITE. Only the following two:
* @retval VINF_SUCCESS
* @retval VERR_CPUM_RAISE_GP_0
*
* @param pVCpu The cross context virtual CPU structure.
* @param u64BaseMsr The value to set.
*/
VMM_INT_DECL(int) APICSetBaseMsr(PVMCPUCC pVCpu, uint64_t u64BaseMsr)
{
Assert(pVCpu);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
APICMODE enmOldMode = apicGetMode(pApicCpu->uApicBaseMsr);
APICMODE enmNewMode = apicGetMode(u64BaseMsr);
uint64_t uBaseMsr = pApicCpu->uApicBaseMsr;
Log2(("APIC%u: ApicSetBaseMsr: u64BaseMsr=%#RX64 enmNewMode=%s enmOldMode=%s\n", pVCpu->idCpu, u64BaseMsr,
apicGetModeName(enmNewMode), apicGetModeName(enmOldMode)));
/*
* We do not support re-mapping the APIC base address because:
* - We'll have to manage all the mappings ourselves in the APIC (reference counting based unmapping etc.)
* i.e. we can only unmap the MMIO region if no other APIC is mapped on that location.
* - It's unclear how/if IOM can fallback to handling regions as regular memory (if the MMIO
* region remains mapped but doesn't belong to the called VCPU's APIC).
*/
/** @todo Handle per-VCPU APIC base relocation. */
if (MSR_IA32_APICBASE_GET_ADDR(uBaseMsr) != MSR_IA32_APICBASE_ADDR)
{
if (pVCpu->apic.s.cLogMaxSetApicBaseAddr++ < 5)
LogRel(("APIC%u: Attempt to relocate base to %#RGp, unsupported -> #GP(0)\n", pVCpu->idCpu,
MSR_IA32_APICBASE_GET_ADDR(uBaseMsr)));
return VERR_CPUM_RAISE_GP_0;
}
/* Don't allow enabling xAPIC/x2APIC if the VM is configured with the APIC disabled. */
if (pApic->enmMaxMode == PDMAPICMODE_NONE)
{
LogRel(("APIC%u: Disallowing APIC base MSR write as the VM is configured with APIC disabled!\n", pVCpu->idCpu));
return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_DISALLOWED_CONFIG);
}
/*
* Act on state transition.
*/
if (enmNewMode != enmOldMode)
{
switch (enmNewMode)
{
case APICMODE_DISABLED:
{
/*
* The APIC state needs to be reset (especially the APIC ID as x2APIC APIC ID bit layout
* is different). We can start with a clean slate identical to the state after a power-up/reset.
*
* See Intel spec. 10.4.3 "Enabling or Disabling the Local APIC".
*
* We'll also manually manage the APIC base MSR here. We want a single-point of commit
* at the end of this function rather than updating it in apicR3ResetCpu. This means we also
* need to update the CPUID leaf ourselves.
*/
apicResetCpu(pVCpu, false /* fResetApicBaseMsr */);
uBaseMsr &= ~(MSR_IA32_APICBASE_EN | MSR_IA32_APICBASE_EXTD);
CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, false /*fVisible*/);
LogRel(("APIC%u: Switched mode to disabled\n", pVCpu->idCpu));
break;
}
case APICMODE_XAPIC:
{
if (enmOldMode != APICMODE_DISABLED)
{
LogRel(("APIC%u: Can only transition to xAPIC state from disabled state\n", pVCpu->idCpu));
return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
}
uBaseMsr |= MSR_IA32_APICBASE_EN;
CPUMSetGuestCpuIdPerCpuApicFeature(pVCpu, true /*fVisible*/);
LogRel(("APIC%u: Switched mode to xAPIC\n", pVCpu->idCpu));
break;
}
case APICMODE_X2APIC:
{
if (pApic->enmMaxMode != PDMAPICMODE_X2APIC)
{
LogRel(("APIC%u: Disallowing transition to x2APIC mode as the VM is configured with the x2APIC disabled!\n",
pVCpu->idCpu));
return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
}
if (enmOldMode != APICMODE_XAPIC)
{
LogRel(("APIC%u: Can only transition to x2APIC state from xAPIC state\n", pVCpu->idCpu));
return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
}
uBaseMsr |= MSR_IA32_APICBASE_EN | MSR_IA32_APICBASE_EXTD;
/*
* The APIC ID needs updating when entering x2APIC mode.
* Software written APIC ID in xAPIC mode isn't preserved.
* The APIC ID becomes read-only to software in x2APIC mode.
*
* See Intel spec. 10.12.5.1 "x2APIC States".
*/
PX2APICPAGE pX2ApicPage = VMCPU_TO_X2APICPAGE(pVCpu);
ASMMemZero32(&pX2ApicPage->id, sizeof(pX2ApicPage->id));
pX2ApicPage->id.u32ApicId = pVCpu->idCpu;
/*
* LDR initialization occurs when entering x2APIC mode.
* See Intel spec. 10.12.10.2 "Deriving Logical x2APIC ID from the Local x2APIC ID".
*/
pX2ApicPage->ldr.u32LogicalApicId = ((pX2ApicPage->id.u32ApicId & UINT32_C(0xffff0)) << 16)
| (UINT32_C(1) << pX2ApicPage->id.u32ApicId & UINT32_C(0xf));
LogRel(("APIC%u: Switched mode to x2APIC\n", pVCpu->idCpu));
break;
}
case APICMODE_INVALID:
default:
{
Log(("APIC%u: Invalid state transition attempted\n", pVCpu->idCpu));
return apicMsrAccessError(pVCpu, MSR_IA32_APICBASE, APICMSRACCESS_WRITE_INVALID);
}
}
}
ASMAtomicWriteU64(&pApicCpu->uApicBaseMsr, uBaseMsr);
return VINF_SUCCESS;
}
/**
* Gets the APIC base MSR (no checks are performed wrt APIC hardware or its
* state).
*
* @returns The base MSR value.
* @param pVCpu The cross context virtual CPU structure.
*/
VMM_INT_DECL(uint64_t) APICGetBaseMsrNoCheck(PCVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
return pApicCpu->uApicBaseMsr;
}
/**
* Gets the APIC base MSR.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param pu64Value Where to store the MSR value.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICGetBaseMsr(PVMCPUCC pVCpu, uint64_t *pu64Value)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
PCAPIC pApic = VM_TO_APIC(pVCpu->CTX_SUFF(pVM));
if (pApic->enmMaxMode != PDMAPICMODE_NONE)
{
*pu64Value = APICGetBaseMsrNoCheck(pVCpu);
return VINF_SUCCESS;
}
if (pVCpu->apic.s.cLogMaxGetApicBaseAddr++ < 5)
LogRel(("APIC%u: Reading APIC base MSR (%#x) when there is no APIC -> #GP(0)\n", pVCpu->idCpu, MSR_IA32_APICBASE));
return VERR_CPUM_RAISE_GP_0;
}
/**
* Sets the TPR (Task Priority Register).
*
* @retval VINF_SUCCESS
* @retval VERR_CPUM_RAISE_GP_0
* @retval VERR_PDM_NO_APIC_INSTANCE
*
* @param pVCpu The cross context virtual CPU structure.
* @param u8Tpr The TPR value to set.
*/
VMMDECL(int) APICSetTpr(PVMCPUCC pVCpu, uint8_t u8Tpr)
{
if (APICIsEnabled(pVCpu))
return apicSetTprEx(pVCpu, u8Tpr, false /* fForceX2ApicBehaviour */);
return VERR_PDM_NO_APIC_INSTANCE;
}
/**
* Gets the highest priority pending interrupt.
*
* @returns true if any interrupt is pending, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param pu8PendingIntr Where to store the interrupt vector if the
* interrupt is pending (optional, can be NULL).
*/
static bool apicGetHighestPendingInterrupt(PCVMCPUCC pVCpu, uint8_t *pu8PendingIntr)
{
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1);
if (irrv >= 0)
{
Assert(irrv <= (int)UINT8_MAX);
if (pu8PendingIntr)
*pu8PendingIntr = (uint8_t)irrv;
return true;
}
return false;
}
/**
* Gets the APIC TPR (Task Priority Register).
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param pu8Tpr Where to store the TPR.
* @param pfPending Where to store whether there is a pending interrupt
* (optional, can be NULL).
* @param pu8PendingIntr Where to store the highest-priority pending
* interrupt (optional, can be NULL).
*/
VMMDECL(int) APICGetTpr(PCVMCPUCC pVCpu, uint8_t *pu8Tpr, bool *pfPending, uint8_t *pu8PendingIntr)
{
VMCPU_ASSERT_EMT(pVCpu);
if (APICIsEnabled(pVCpu))
{
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
if (pfPending)
{
/*
* Just return whatever the highest pending interrupt is in the IRR.
* The caller is responsible for figuring out if it's masked by the TPR etc.
*/
*pfPending = apicGetHighestPendingInterrupt(pVCpu, pu8PendingIntr);
}
*pu8Tpr = pXApicPage->tpr.u8Tpr;
return VINF_SUCCESS;
}
*pu8Tpr = 0;
return VERR_PDM_NO_APIC_INSTANCE;
}
/**
* Gets the APIC timer frequency.
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pu64Value Where to store the timer frequency.
*/
VMM_INT_DECL(int) APICGetTimerFreq(PVMCC pVM, uint64_t *pu64Value)
{
/*
* Validate.
*/
Assert(pVM);
AssertPtrReturn(pu64Value, VERR_INVALID_PARAMETER);
PVMCPUCC pVCpu = pVM->CTX_SUFF(apCpus)[0];
if (APICIsEnabled(pVCpu))
{
PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
*pu64Value = PDMDevHlpTimerGetFreq(VMCPU_TO_DEVINS(pVCpu), pApicCpu->hTimer);
return VINF_SUCCESS;
}
return VERR_PDM_NO_APIC_INSTANCE;
}
/**
* Delivers an interrupt message via the system bus.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param uDest The destination mask.
* @param uDestMode The destination mode.
* @param uDeliveryMode The delivery mode.
* @param uVector The interrupt vector.
* @param uPolarity The interrupt line polarity.
* @param uTriggerMode The trigger mode.
* @param uSrcTag The interrupt source tag (debugging).
*/
VMM_INT_DECL(int) APICBusDeliver(PVMCC pVM, uint8_t uDest, uint8_t uDestMode, uint8_t uDeliveryMode, uint8_t uVector,
uint8_t uPolarity, uint8_t uTriggerMode, uint32_t uSrcTag)
{
NOREF(uPolarity);
/*
* If the APIC isn't enabled, do nothing and pretend success.
*/
if (APICIsEnabled(pVM->CTX_SUFF(apCpus)[0]))
{ /* likely */ }
else
return VINF_SUCCESS;
/*
* The destination field (mask) in the IO APIC redirectable table entry is 8-bits.
* Hence, the broadcast mask is 0xff.
* See IO APIC spec. 3.2.4. "IOREDTBL[23:0] - I/O Redirectable Table Registers".
*/
XAPICTRIGGERMODE enmTriggerMode = (XAPICTRIGGERMODE)uTriggerMode;
XAPICDELIVERYMODE enmDeliveryMode = (XAPICDELIVERYMODE)uDeliveryMode;
XAPICDESTMODE enmDestMode = (XAPICDESTMODE)uDestMode;
uint32_t fDestMask = uDest;
uint32_t fBroadcastMask = UINT32_C(0xff);
Log2(("APIC: apicBusDeliver: fDestMask=%#x enmDestMode=%s enmTriggerMode=%s enmDeliveryMode=%s uVector=%#x uSrcTag=%#x\n",
fDestMask, apicGetDestModeName(enmDestMode), apicGetTriggerModeName(enmTriggerMode),
apicGetDeliveryModeName(enmDeliveryMode), uVector, uSrcTag));
bool fIntrAccepted;
VMCPUSET DestCpuSet;
apicGetDestCpuSet(pVM, fDestMask, fBroadcastMask, enmDestMode, enmDeliveryMode, &DestCpuSet);
VBOXSTRICTRC rcStrict = apicSendIntr(pVM, NULL /* pVCpu */, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
&fIntrAccepted, uSrcTag, VINF_SUCCESS /* rcRZ */);
if (fIntrAccepted)
return VBOXSTRICTRC_VAL(rcStrict);
return VERR_APIC_INTR_DISCARDED;
}
/**
* Assert/de-assert the local APIC's LINT0/LINT1 interrupt pins.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param u8Pin The interrupt pin (0 for LINT0 or 1 for LINT1).
* @param u8Level The level (0 for low or 1 for high).
* @param rcRZ The return code if the operation cannot be performed in
* the current context.
*
* @note All callers totally ignores the status code!
*/
VMM_INT_DECL(VBOXSTRICTRC) APICLocalInterrupt(PVMCPUCC pVCpu, uint8_t u8Pin, uint8_t u8Level, int rcRZ)
{
AssertReturn(u8Pin <= 1, VERR_INVALID_PARAMETER);
AssertReturn(u8Level <= 1, VERR_INVALID_PARAMETER);
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
/* If the APIC is enabled, the interrupt is subject to LVT programming. */
if (APICIsEnabled(pVCpu))
{
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
/* Pick the LVT entry corresponding to the interrupt pin. */
static const uint16_t s_au16LvtOffsets[] =
{
XAPIC_OFF_LVT_LINT0,
XAPIC_OFF_LVT_LINT1
};
Assert(u8Pin < RT_ELEMENTS(s_au16LvtOffsets));
uint16_t const offLvt = s_au16LvtOffsets[u8Pin];
uint32_t const uLvt = apicReadRaw32(pXApicPage, offLvt);
/* If software hasn't masked the interrupt in the LVT entry, proceed interrupt processing. */
if (!XAPIC_LVT_IS_MASKED(uLvt))
{
XAPICDELIVERYMODE const enmDeliveryMode = XAPIC_LVT_GET_DELIVERY_MODE(uLvt);
XAPICTRIGGERMODE enmTriggerMode = XAPIC_LVT_GET_TRIGGER_MODE(uLvt);
switch (enmDeliveryMode)
{
case XAPICDELIVERYMODE_INIT:
{
/** @todo won't work in R0/RC because callers don't care about rcRZ. */
AssertMsgFailed(("INIT through LINT0/LINT1 is not yet supported\n"));
}
RT_FALL_THRU();
case XAPICDELIVERYMODE_FIXED:
{
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
uint8_t const uVector = XAPIC_LVT_GET_VECTOR(uLvt);
bool fActive = RT_BOOL(u8Level & 1);
bool volatile *pfActiveLine = u8Pin == 0 ? &pApicCpu->fActiveLint0 : &pApicCpu->fActiveLint1;
/** @todo Polarity is busted elsewhere, we need to fix that
* first. See @bugref{8386#c7}. */
#if 0
uint8_t const u8Polarity = XAPIC_LVT_GET_POLARITY(uLvt);
fActive ^= u8Polarity; */
#endif
if (!fActive)
{
ASMAtomicCmpXchgBool(pfActiveLine, false, true);
break;
}
/* Level-sensitive interrupts are not supported for LINT1. See Intel spec. 10.5.1 "Local Vector Table". */
if (offLvt == XAPIC_OFF_LVT_LINT1)
enmTriggerMode = XAPICTRIGGERMODE_EDGE;
/** @todo figure out what "If the local APIC is not used in conjunction with an I/O APIC and fixed
delivery mode is selected; the Pentium 4, Intel Xeon, and P6 family processors will always
use level-sensitive triggering, regardless if edge-sensitive triggering is selected."
means. */
bool fSendIntr;
if (enmTriggerMode == XAPICTRIGGERMODE_EDGE)
{
/* Recognize and send the interrupt only on an edge transition. */
fSendIntr = ASMAtomicCmpXchgBool(pfActiveLine, true, false);
}
else
{
/* For level-triggered interrupts, redundant interrupts are not a problem. */
Assert(enmTriggerMode == XAPICTRIGGERMODE_LEVEL);
ASMAtomicCmpXchgBool(pfActiveLine, true, false);
/* Only when the remote IRR isn't set, set it and send the interrupt. */
if (!(pXApicPage->lvt_lint0.all.u32LvtLint0 & XAPIC_LVT_REMOTE_IRR))
{
Assert(offLvt == XAPIC_OFF_LVT_LINT0);
ASMAtomicOrU32((volatile uint32_t *)&pXApicPage->lvt_lint0.all.u32LvtLint0, XAPIC_LVT_REMOTE_IRR);
fSendIntr = true;
}
else
fSendIntr = false;
}
if (fSendIntr)
{
VMCPUSET DestCpuSet;
VMCPUSET_EMPTY(&DestCpuSet);
VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
rcStrict = apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode,
&DestCpuSet, NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
}
break;
}
case XAPICDELIVERYMODE_SMI:
case XAPICDELIVERYMODE_NMI:
{
VMCPUSET DestCpuSet;
VMCPUSET_EMPTY(&DestCpuSet);
VMCPUSET_ADD(&DestCpuSet, pVCpu->idCpu);
uint8_t const uVector = XAPIC_LVT_GET_VECTOR(uLvt);
rcStrict = apicSendIntr(pVCpu->CTX_SUFF(pVM), pVCpu, uVector, enmTriggerMode, enmDeliveryMode, &DestCpuSet,
NULL /* pfIntrAccepted */, 0 /* uSrcTag */, rcRZ);
break;
}
case XAPICDELIVERYMODE_EXTINT:
{
Log2(("APIC%u: apicLocalInterrupt: %s ExtINT through LINT%u\n", pVCpu->idCpu,
u8Level ? "Raising" : "Lowering", u8Pin));
if (u8Level)
apicSetInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
else
apicClearInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
break;
}
/* Reserved/unknown delivery modes: */
case XAPICDELIVERYMODE_LOWEST_PRIO:
case XAPICDELIVERYMODE_STARTUP:
default:
{
AssertMsgFailed(("APIC%u: LocalInterrupt: Invalid delivery mode %#x (%s) on LINT%d\n", pVCpu->idCpu,
enmDeliveryMode, apicGetDeliveryModeName(enmDeliveryMode), u8Pin));
rcStrict = VERR_INTERNAL_ERROR_3;
break;
}
}
}
}
else
{
/* The APIC is hardware disabled. The CPU behaves as though there is no on-chip APIC. */
if (u8Pin == 0)
{
/* LINT0 behaves as an external interrupt pin. */
Log2(("APIC%u: apicLocalInterrupt: APIC hardware-disabled, %s INTR\n", pVCpu->idCpu,
u8Level ? "raising" : "lowering"));
if (u8Level)
apicSetInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
else
apicClearInterruptFF(pVCpu, PDMAPICIRQ_EXTINT);
}
else
{
/* LINT1 behaves as NMI. */
Log2(("APIC%u: apicLocalInterrupt: APIC hardware-disabled, raising NMI\n", pVCpu->idCpu));
apicSetInterruptFF(pVCpu, PDMAPICIRQ_NMI);
}
}
return rcStrict;
}
/**
* Gets the next highest-priority interrupt from the APIC, marking it as an
* "in-service" interrupt.
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param pu8Vector Where to store the vector.
* @param puSrcTag Where to store the interrupt source tag (debugging).
*/
VMM_INT_DECL(int) APICGetInterrupt(PVMCPUCC pVCpu, uint8_t *pu8Vector, uint32_t *puSrcTag)
{
VMCPU_ASSERT_EMT(pVCpu);
Assert(pu8Vector);
LogFlow(("APIC%u: apicGetInterrupt:\n", pVCpu->idCpu));
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
bool const fApicHwEnabled = APICIsEnabled(pVCpu);
if ( fApicHwEnabled
&& pXApicPage->svr.u.fApicSoftwareEnable)
{
int const irrv = apicGetHighestSetBitInReg(&pXApicPage->irr, -1);
if (RT_LIKELY(irrv >= 0))
{
Assert(irrv <= (int)UINT8_MAX);
uint8_t const uVector = irrv;
/*
* This can happen if the APIC receives an interrupt when the CPU has interrupts
* disabled but the TPR is raised by the guest before re-enabling interrupts.
*/
uint8_t const uTpr = pXApicPage->tpr.u8Tpr;
if ( uTpr > 0
&& XAPIC_TPR_GET_TP(uVector) <= XAPIC_TPR_GET_TP(uTpr))
{
Log2(("APIC%u: apicGetInterrupt: Interrupt masked. uVector=%#x uTpr=%#x SpuriousVector=%#x\n", pVCpu->idCpu,
uVector, uTpr, pXApicPage->svr.u.u8SpuriousVector));
*pu8Vector = uVector;
*puSrcTag = 0;
STAM_COUNTER_INC(&pVCpu->apic.s.StatMaskedByTpr);
return VERR_APIC_INTR_MASKED_BY_TPR;
}
/*
* The PPR should be up-to-date at this point through apicSetEoi().
* We're on EMT so no parallel updates possible.
* Subject the pending vector to PPR prioritization.
*/
uint8_t const uPpr = pXApicPage->ppr.u8Ppr;
if ( !uPpr
|| XAPIC_PPR_GET_PP(uVector) > XAPIC_PPR_GET_PP(uPpr))
{
apicClearVectorInReg(&pXApicPage->irr, uVector);
apicSetVectorInReg(&pXApicPage->isr, uVector);
apicUpdatePpr(pVCpu);
apicSignalNextPendingIntr(pVCpu);
/* Retrieve the interrupt source tag associated with this interrupt. */
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
AssertCompile(RT_ELEMENTS(pApicCpu->auSrcTags) > UINT8_MAX);
*puSrcTag = pApicCpu->auSrcTags[uVector];
pApicCpu->auSrcTags[uVector] = 0;
Log2(("APIC%u: apicGetInterrupt: Valid Interrupt. uVector=%#x uSrcTag=%#x\n", pVCpu->idCpu, uVector, *puSrcTag));
*pu8Vector = uVector;
return VINF_SUCCESS;
}
STAM_COUNTER_INC(&pVCpu->apic.s.StatMaskedByPpr);
Log2(("APIC%u: apicGetInterrupt: Interrupt's priority is not higher than the PPR. uVector=%#x PPR=%#x\n",
pVCpu->idCpu, uVector, uPpr));
}
else
Log2(("APIC%u: apicGetInterrupt: No pending bits in IRR\n", pVCpu->idCpu));
}
else
Log2(("APIC%u: apicGetInterrupt: APIC %s disabled\n", pVCpu->idCpu, !fApicHwEnabled ? "hardware" : "software"));
*pu8Vector = 0;
*puSrcTag = 0;
return VERR_APIC_INTR_NOT_PENDING;
}
/**
* @callback_method_impl{FNIOMMMIONEWREAD}
*/
DECLCALLBACK(VBOXSTRICTRC) apicReadMmio(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
{
NOREF(pvUser);
Assert(!(off & 0xf));
Assert(cb == 4); RT_NOREF_PV(cb);
PVMCPUCC pVCpu = PDMDevHlpGetVMCPU(pDevIns);
uint16_t offReg = off & 0xff0;
uint32_t uValue = 0;
STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMmioRead));
VBOXSTRICTRC rc = VBOXSTRICTRC_VAL(apicReadRegister(pDevIns, pVCpu, offReg, &uValue));
*(uint32_t *)pv = uValue;
Log2(("APIC%u: apicReadMmio: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
return rc;
}
/**
* @callback_method_impl{FNIOMMMIONEWWRITE}
*/
DECLCALLBACK(VBOXSTRICTRC) apicWriteMmio(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
{
NOREF(pvUser);
Assert(!(off & 0xf));
Assert(cb == 4); RT_NOREF_PV(cb);
PVMCPUCC pVCpu = PDMDevHlpGetVMCPU(pDevIns);
uint16_t offReg = off & 0xff0;
uint32_t uValue = *(uint32_t *)pv;
STAM_COUNTER_INC(&pVCpu->apic.s.CTX_SUFF_Z(StatMmioWrite));
Log2(("APIC%u: apicWriteMmio: offReg=%#RX16 uValue=%#RX32\n", pVCpu->idCpu, offReg, uValue));
return apicWriteRegister(pDevIns, pVCpu, offReg, uValue);
}
/**
* Sets the interrupt pending force-flag and pokes the EMT if required.
*
* @param pVCpu The cross context virtual CPU structure.
* @param enmType The IRQ type.
*/
static void apicSetInterruptFF(PVMCPUCC pVCpu, PDMAPICIRQ enmType)
{
#ifdef IN_RING3
/* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
#endif
switch (enmType)
{
case PDMAPICIRQ_HARDWARE:
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC);
break;
case PDMAPICIRQ_UPDATE_PENDING: VMCPU_FF_SET(pVCpu, VMCPU_FF_UPDATE_APIC); break;
case PDMAPICIRQ_NMI: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI); break;
case PDMAPICIRQ_SMI: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI); break;
case PDMAPICIRQ_EXTINT: VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC); break;
default:
AssertMsgFailed(("enmType=%d\n", enmType));
break;
}
/*
* We need to wake up the target CPU if we're not on EMT.
*/
/** @todo r=bird: Why do we skip this waking up for PDMAPICIRQ_HARDWARE? */
/** @todo r=bird: We could just use RTThreadNativeSelf() here, couldn't we? */
#if defined(IN_RING0)
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
VMCPUID idCpu = pVCpu->idCpu;
if ( enmType != PDMAPICIRQ_HARDWARE
&& VMMGetCpuId(pVM) != idCpu)
{
switch (VMCPU_GET_STATE(pVCpu))
{
case VMCPUSTATE_STARTED_EXEC:
Log7Func(("idCpu=%u VMCPUSTATE_STARTED_EXEC\n", idCpu));
GVMMR0SchedPokeNoGVMNoLock(pVM, idCpu);
break;
case VMCPUSTATE_STARTED_HALTED:
Log7Func(("idCpu=%u VMCPUSTATE_STARTED_HALTED\n", idCpu));
GVMMR0SchedWakeUpNoGVMNoLock(pVM, idCpu);
break;
default:
Log7Func(("idCpu=%u enmState=%d\n", idCpu, pVCpu->enmState));
break; /* nothing to do in other states. */
}
}
#elif defined(IN_RING3)
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
VMCPUID idCpu = pVCpu->idCpu;
if ( enmType != PDMAPICIRQ_HARDWARE
&& VMMGetCpuId(pVM) != idCpu)
{
Log7Func(("idCpu=%u enmState=%d\n", idCpu, pVCpu->enmState));
VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_DONE_REM | VMNOTIFYFF_FLAGS_POKE);
}
#endif
}
/**
* Clears the interrupt pending force-flag.
*
* @param pVCpu The cross context virtual CPU structure.
* @param enmType The IRQ type.
*/
void apicClearInterruptFF(PVMCPUCC pVCpu, PDMAPICIRQ enmType)
{
#ifdef IN_RING3
/* IRQ state should be loaded as-is by "LoadExec". Changes can be made from LoadDone. */
Assert(pVCpu->pVMR3->enmVMState != VMSTATE_LOADING || PDMR3HasLoadedState(pVCpu->pVMR3));
#endif
/* NMI/SMI can't be cleared. */
switch (enmType)
{
case PDMAPICIRQ_HARDWARE: VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_APIC); break;
case PDMAPICIRQ_EXTINT: VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_PIC); break;
default:
AssertMsgFailed(("enmType=%d\n", enmType));
break;
}
}
/**
* Posts an interrupt to a target APIC.
*
* This function handles interrupts received from the system bus or
* interrupts generated locally from the LVT or via a self IPI.
*
* Don't use this function to try and deliver ExtINT style interrupts.
*
* @returns true if the interrupt was accepted, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param uVector The vector of the interrupt to be posted.
* @param enmTriggerMode The trigger mode of the interrupt.
* @param uSrcTag The interrupt source tag (debugging).
*
* @thread Any.
*/
bool apicPostInterrupt(PVMCPUCC pVCpu, uint8_t uVector, XAPICTRIGGERMODE enmTriggerMode, uint32_t uSrcTag)
{
Assert(pVCpu);
Assert(uVector > XAPIC_ILLEGAL_VECTOR_END);
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
PCAPIC pApic = VM_TO_APIC(pVM);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
bool fAccepted = true;
STAM_PROFILE_START(&pApicCpu->StatPostIntr, a);
STAM_REL_COUNTER_INC(&pApicCpu->StatPostIntrCnt);
STAM_REL_COUNTER_INC(&pApicCpu->aStatVectors[uVector]);
/*
* Only post valid interrupt vectors.
* See Intel spec. 10.5.2 "Valid Interrupt Vectors".
*/
if (RT_LIKELY(uVector > XAPIC_ILLEGAL_VECTOR_END))
{
/*
* If the interrupt is already pending in the IRR we can skip the
* potential expensive operation of poking the guest EMT out of execution.
*/
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
if (!apicTestVectorInReg(&pXApicPage->irr, uVector)) /* PAV */
{
/* Update the interrupt source tag (debugging). */
if (!pApicCpu->auSrcTags[uVector])
pApicCpu->auSrcTags[uVector] = uSrcTag;
else
pApicCpu->auSrcTags[uVector] |= RT_BIT_32(31);
Log2(("APIC: apicPostInterrupt: SrcCpu=%u TargetCpu=%u uVector=%#x %s\n",
VMMGetCpuId(pVM), pVCpu->idCpu, uVector, enmTriggerMode == XAPICTRIGGERMODE_EDGE ? "edge" : "lvl"));
if (enmTriggerMode == XAPICTRIGGERMODE_EDGE)
{
if (pApic->fPostedIntrsEnabled)
{ /** @todo posted-interrupt call to hardware */ }
else
{
apicSetVectorInPib(pApicCpu->CTX_SUFF(pvApicPib), uVector);
uint32_t const fAlreadySet = apicSetNotificationBitInPib((PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib));
if (!fAlreadySet)
{
Log2(("APIC: apicPostInterrupt: Setting UPDATE_APIC FF for edge-triggered intr. uVector=%#x\n", uVector));
apicSetInterruptFF(pVCpu, PDMAPICIRQ_UPDATE_PENDING);
}
}
}
else
{
/*
* Level-triggered interrupts requires updating of the TMR and thus cannot be
* delivered asynchronously.
*/
apicSetVectorInPib(&pApicCpu->ApicPibLevel, uVector);
uint32_t const fAlreadySet = apicSetNotificationBitInPib(&pApicCpu->ApicPibLevel);
if (!fAlreadySet)
{
Log2(("APIC: apicPostInterrupt: Setting UPDATE_APIC FF for level-triggered intr. uVector=%#x\n", uVector));
apicSetInterruptFF(pVCpu, PDMAPICIRQ_UPDATE_PENDING);
}
}
}
else
{
Log2(("APIC: apicPostInterrupt: SrcCpu=%u TargetCpu=%u. Vector %#x Already in IRR, skipping\n", VMMGetCpuId(pVM),
pVCpu->idCpu, uVector));
STAM_COUNTER_INC(&pApicCpu->StatPostIntrAlreadyPending);
}
}
else
{
fAccepted = false;
apicSetError(pVCpu, XAPIC_ESR_RECV_ILLEGAL_VECTOR);
}
STAM_PROFILE_STOP(&pApicCpu->StatPostIntr, a);
return fAccepted;
}
/**
* Starts the APIC timer.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uInitialCount The timer's Initial-Count Register (ICR), must be >
* 0.
* @thread Any.
*/
void apicStartTimer(PVMCPUCC pVCpu, uint32_t uInitialCount)
{
Assert(pVCpu);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
Assert(PDMDevHlpTimerIsLockOwner(pDevIns, pApicCpu->hTimer));
Assert(uInitialCount > 0);
PCXAPICPAGE pXApicPage = APICCPU_TO_CXAPICPAGE(pApicCpu);
uint8_t const uTimerShift = apicGetTimerShift(pXApicPage);
uint64_t const cTicksToNext = (uint64_t)uInitialCount << uTimerShift;
Log2(("APIC%u: apicStartTimer: uInitialCount=%#RX32 uTimerShift=%u cTicksToNext=%RU64\n", pVCpu->idCpu, uInitialCount,
uTimerShift, cTicksToNext));
/*
* The assumption here is that the timer doesn't tick during this call
* and thus setting a relative time to fire next is accurate. The advantage
* however is updating u64TimerInitial 'atomically' while setting the next
* tick.
*/
PDMDevHlpTimerSetRelative(pDevIns, pApicCpu->hTimer, cTicksToNext, &pApicCpu->u64TimerInitial);
apicHintTimerFreq(pDevIns, pApicCpu, uInitialCount, uTimerShift);
}
/**
* Stops the APIC timer.
*
* @param pVCpu The cross context virtual CPU structure.
* @thread Any.
*/
static void apicStopTimer(PVMCPUCC pVCpu)
{
Assert(pVCpu);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PPDMDEVINS pDevIns = VMCPU_TO_DEVINS(pVCpu);
Assert(PDMDevHlpTimerIsLockOwner(pDevIns, pApicCpu->hTimer));
Log2(("APIC%u: apicStopTimer\n", pVCpu->idCpu));
PDMDevHlpTimerStop(pDevIns, pApicCpu->hTimer); /* This will reset the hint, no need to explicitly call TMTimerSetFrequencyHint(). */
pApicCpu->uHintedTimerInitialCount = 0;
pApicCpu->uHintedTimerShift = 0;
}
/**
* Queues a pending interrupt as in-service.
*
* This function should only be needed without virtualized APIC
* registers. With virtualized APIC registers, it's sufficient to keep
* the interrupts pending in the IRR as the hardware takes care of
* virtual interrupt delivery.
*
* @returns true if the interrupt was queued to in-service interrupts,
* false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param u8PendingIntr The pending interrupt to queue as
* in-service.
*
* @remarks This assumes the caller has done the necessary checks and
* is ready to take actually service the interrupt (TPR,
* interrupt shadow etc.)
*/
VMM_INT_DECL(bool) APICQueueInterruptToService(PVMCPUCC pVCpu, uint8_t u8PendingIntr)
{
VMCPU_ASSERT_EMT(pVCpu);
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
PAPIC pApic = VM_TO_APIC(pVM);
Assert(!pApic->fVirtApicRegsEnabled);
NOREF(pApic);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
bool const fIsPending = apicTestVectorInReg(&pXApicPage->irr, u8PendingIntr);
if (fIsPending)
{
apicClearVectorInReg(&pXApicPage->irr, u8PendingIntr);
apicSetVectorInReg(&pXApicPage->isr, u8PendingIntr);
apicUpdatePpr(pVCpu);
return true;
}
return false;
}
/**
* De-queues a pending interrupt from in-service.
*
* This undoes APICQueueInterruptToService() for premature VM-exits before event
* injection.
*
* @param pVCpu The cross context virtual CPU structure.
* @param u8PendingIntr The pending interrupt to de-queue from
* in-service.
*/
VMM_INT_DECL(void) APICDequeueInterruptFromService(PVMCPUCC pVCpu, uint8_t u8PendingIntr)
{
VMCPU_ASSERT_EMT(pVCpu);
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
PAPIC pApic = VM_TO_APIC(pVM);
Assert(!pApic->fVirtApicRegsEnabled);
NOREF(pApic);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
bool const fInService = apicTestVectorInReg(&pXApicPage->isr, u8PendingIntr);
if (fInService)
{
apicClearVectorInReg(&pXApicPage->isr, u8PendingIntr);
apicSetVectorInReg(&pXApicPage->irr, u8PendingIntr);
apicUpdatePpr(pVCpu);
}
}
/**
* Updates pending interrupts from the pending-interrupt bitmaps to the IRR.
*
* @param pVCpu The cross context virtual CPU structure.
*
* @note NEM/win is ASSUMING the an up to date TPR is not required here.
*/
VMMDECL(void) APICUpdatePendingInterrupts(PVMCPUCC pVCpu)
{
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
PAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
PXAPICPAGE pXApicPage = VMCPU_TO_XAPICPAGE(pVCpu);
bool fHasPendingIntrs = false;
Log3(("APIC%u: APICUpdatePendingInterrupts:\n", pVCpu->idCpu));
STAM_PROFILE_START(&pApicCpu->StatUpdatePendingIntrs, a);
/* Update edge-triggered pending interrupts. */
PAPICPIB pPib = (PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib);
for (;;)
{
uint32_t const fAlreadySet = apicClearNotificationBitInPib((PAPICPIB)pApicCpu->CTX_SUFF(pvApicPib));
if (!fAlreadySet)
break;
AssertCompile(RT_ELEMENTS(pXApicPage->irr.u) == 2 * RT_ELEMENTS(pPib->au64VectorBitmap));
for (size_t idxPib = 0, idxReg = 0; idxPib < RT_ELEMENTS(pPib->au64VectorBitmap); idxPib++, idxReg += 2)
{
uint64_t const u64Fragment = ASMAtomicXchgU64(&pPib->au64VectorBitmap[idxPib], 0);
if (u64Fragment)
{
uint32_t const u32FragmentLo = RT_LO_U32(u64Fragment);
uint32_t const u32FragmentHi = RT_HI_U32(u64Fragment);
Log6Func(("edge[%u/%u]: %'016RX64: irr=%08RX32'%08RX32 |; tmr=%08RX32'%08RX32 &~\n", idxPib, idxReg, u64Fragment,
pXApicPage->irr.u[idxReg].u32Reg, pXApicPage->irr.u[idxReg + 1].u32Reg,
pXApicPage->tmr.u[idxReg].u32Reg, pXApicPage->tmr.u[idxReg + 1].u32Reg));
pXApicPage->irr.u[idxReg].u32Reg |= u32FragmentLo;
pXApicPage->irr.u[idxReg + 1].u32Reg |= u32FragmentHi;
pXApicPage->tmr.u[idxReg].u32Reg &= ~u32FragmentLo;
pXApicPage->tmr.u[idxReg + 1].u32Reg &= ~u32FragmentHi;
fHasPendingIntrs = true;
}
}
}
/* Update level-triggered pending interrupts. */
pPib = (PAPICPIB)&pApicCpu->ApicPibLevel;
for (;;)
{
uint32_t const fAlreadySet = apicClearNotificationBitInPib((PAPICPIB)&pApicCpu->ApicPibLevel);
if (!fAlreadySet)
break;
AssertCompile(RT_ELEMENTS(pXApicPage->irr.u) == 2 * RT_ELEMENTS(pPib->au64VectorBitmap));
for (size_t idxPib = 0, idxReg = 0; idxPib < RT_ELEMENTS(pPib->au64VectorBitmap); idxPib++, idxReg += 2)
{
uint64_t const u64Fragment = ASMAtomicXchgU64(&pPib->au64VectorBitmap[idxPib], 0);
if (u64Fragment)
{
Log6Func(("level[%u/%u]: %'016RX64: irr=%08RX32'%08RX32 |; tmr=%08RX32'%08RX32 |\n", idxPib, idxReg, u64Fragment,
pXApicPage->irr.u[idxReg].u32Reg, pXApicPage->irr.u[idxReg + 1].u32Reg,
pXApicPage->tmr.u[idxReg].u32Reg, pXApicPage->tmr.u[idxReg + 1].u32Reg));
uint32_t const u32FragmentLo = RT_LO_U32(u64Fragment);
uint32_t const u32FragmentHi = RT_HI_U32(u64Fragment);
pXApicPage->irr.u[idxReg].u32Reg |= u32FragmentLo;
pXApicPage->irr.u[idxReg + 1].u32Reg |= u32FragmentHi;
pXApicPage->tmr.u[idxReg].u32Reg |= u32FragmentLo;
pXApicPage->tmr.u[idxReg + 1].u32Reg |= u32FragmentHi;
fHasPendingIntrs = true;
}
}
}
STAM_PROFILE_STOP(&pApicCpu->StatUpdatePendingIntrs, a);
Log3(("APIC%u: APICUpdatePendingInterrupts: fHasPendingIntrs=%RTbool\n", pVCpu->idCpu, fHasPendingIntrs));
if ( fHasPendingIntrs
&& !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC))
apicSignalNextPendingIntr(pVCpu);
}
/**
* Gets the highest priority pending interrupt.
*
* @returns true if any interrupt is pending, false otherwise.
* @param pVCpu The cross context virtual CPU structure.
* @param pu8PendingIntr Where to store the interrupt vector if the
* interrupt is pending.
*/
VMM_INT_DECL(bool) APICGetHighestPendingInterrupt(PVMCPUCC pVCpu, uint8_t *pu8PendingIntr)
{
VMCPU_ASSERT_EMT(pVCpu);
return apicGetHighestPendingInterrupt(pVCpu, pu8PendingIntr);
}
/**
* Posts an interrupt to a target APIC, Hyper-V interface.
*
* @param pVCpu The cross context virtual CPU structure.
* @param uVector The vector of the interrupt to be posted.
* @param fAutoEoi Whether this interrupt has automatic EOI
* treatment.
* @param enmTriggerMode The trigger mode of the interrupt.
*
* @thread Any.
*/
VMM_INT_DECL(void) APICHvSendInterrupt(PVMCPUCC pVCpu, uint8_t uVector, bool fAutoEoi, XAPICTRIGGERMODE enmTriggerMode)
{
Assert(pVCpu);
Assert(!fAutoEoi); /** @todo AutoEOI. */
RT_NOREF(fAutoEoi);
apicPostInterrupt(pVCpu, uVector, enmTriggerMode, 0 /* uSrcTag */);
}
/**
* Sets the Task Priority Register (TPR), Hyper-V interface.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uTpr The TPR value to set.
*
* @remarks Validates like in x2APIC mode.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICHvSetTpr(PVMCPUCC pVCpu, uint8_t uTpr)
{
Assert(pVCpu);
VMCPU_ASSERT_EMT(pVCpu);
return apicSetTprEx(pVCpu, uTpr, true /* fForceX2ApicBehaviour */);
}
/**
* Gets the Task Priority Register (TPR), Hyper-V interface.
*
* @returns The TPR value.
* @param pVCpu The cross context virtual CPU structure.
*/
VMM_INT_DECL(uint8_t) APICHvGetTpr(PVMCPUCC pVCpu)
{
Assert(pVCpu);
VMCPU_ASSERT_EMT(pVCpu);
/*
* The APIC could be operating in xAPIC mode and thus we should not use the apicReadMsr()
* interface which validates the APIC mode and will throw a #GP(0) if not in x2APIC mode.
* We could use the apicReadRegister() MMIO interface, but why bother getting the PDMDEVINS
* pointer, so just directly read the APIC page.
*/
PCXAPICPAGE pXApicPage = VMCPU_TO_CXAPICPAGE(pVCpu);
return apicReadRaw32(pXApicPage, XAPIC_OFF_TPR);
}
/**
* Sets the Interrupt Command Register (ICR), Hyper-V interface.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uIcr The ICR value to set.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICHvSetIcr(PVMCPUCC pVCpu, uint64_t uIcr)
{
Assert(pVCpu);
VMCPU_ASSERT_EMT(pVCpu);
return apicSetIcr(pVCpu, uIcr, VINF_CPUM_R3_MSR_WRITE);
}
/**
* Gets the Interrupt Command Register (ICR), Hyper-V interface.
*
* @returns The ICR value.
* @param pVCpu The cross context virtual CPU structure.
*/
VMM_INT_DECL(uint64_t) APICHvGetIcr(PVMCPUCC pVCpu)
{
Assert(pVCpu);
VMCPU_ASSERT_EMT(pVCpu);
return apicGetIcrNoCheck(pVCpu);
}
/**
* Sets the End-Of-Interrupt (EOI) register, Hyper-V interface.
*
* @returns Strict VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param uEoi The EOI value.
*/
VMM_INT_DECL(VBOXSTRICTRC) APICHvSetEoi(PVMCPUCC pVCpu, uint32_t uEoi)
{
Assert(pVCpu);
VMCPU_ASSERT_EMT_OR_NOT_RUNNING(pVCpu);
return apicSetEoi(pVCpu, uEoi, true /* fForceX2ApicBehaviour */);
}
/**
* Gets the APIC page pointers for the specified VCPU.
*
* @returns VBox status code.
* @param pVCpu The cross context virtual CPU structure.
* @param pHCPhys Where to store the host-context physical address.
* @param pR0Ptr Where to store the ring-0 address.
* @param pR3Ptr Where to store the ring-3 address (optional).
*/
VMM_INT_DECL(int) APICGetApicPageForCpu(PCVMCPUCC pVCpu, PRTHCPHYS pHCPhys, PRTR0PTR pR0Ptr, PRTR3PTR pR3Ptr)
{
AssertReturn(pVCpu, VERR_INVALID_PARAMETER);
AssertReturn(pHCPhys, VERR_INVALID_PARAMETER);
AssertReturn(pR0Ptr, VERR_INVALID_PARAMETER);
Assert(PDMHasApic(pVCpu->CTX_SUFF(pVM)));
PCAPICCPU pApicCpu = VMCPU_TO_APICCPU(pVCpu);
*pHCPhys = pApicCpu->HCPhysApicPage;
*pR0Ptr = pApicCpu->pvApicPageR0;
if (pR3Ptr)
*pR3Ptr = pApicCpu->pvApicPageR3;
return VINF_SUCCESS;
}
#ifndef IN_RING3
/**
* @callback_method_impl{PDMDEVREGR0,pfnConstruct}
*/
static DECLCALLBACK(int) apicRZConstruct(PPDMDEVINS pDevIns)
{
PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
PAPICDEV pThis = PDMDEVINS_2_DATA(pDevIns, PAPICDEV);
PVMCC pVM = PDMDevHlpGetVM(pDevIns);
pVM->apicr0.s.pDevInsR0 = pDevIns;
int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
AssertRCReturn(rc, rc);
rc = PDMDevHlpApicSetUpContext(pDevIns);
AssertRCReturn(rc, rc);
rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, apicWriteMmio, apicReadMmio, NULL /*pvUser*/);
AssertRCReturn(rc, rc);
return VINF_SUCCESS;
}
#endif /* !IN_RING3 */
/**
* APIC device registration structure.
*/
const PDMDEVREG g_DeviceAPIC =
{
/* .u32Version = */ PDM_DEVREG_VERSION,
/* .uReserved0 = */ 0,
/* .szName = */ "apic",
/* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE
| PDM_DEVREG_FLAGS_REQUIRE_R0 | PDM_DEVREG_FLAGS_REQUIRE_RC,
/* .fClass = */ PDM_DEVREG_CLASS_PIC,
/* .cMaxInstances = */ 1,
/* .uSharedVersion = */ 42,
/* .cbInstanceShared = */ sizeof(APICDEV),
/* .cbInstanceCC = */ 0,
/* .cbInstanceRC = */ 0,
/* .cMaxPciDevices = */ 0,
/* .cMaxMsixVectors = */ 0,
/* .pszDescription = */ "Advanced Programmable Interrupt Controller",
#if defined(IN_RING3)
/* .szRCMod = */ "VMMRC.rc",
/* .szR0Mod = */ "VMMR0.r0",
/* .pfnConstruct = */ apicR3Construct,
/* .pfnDestruct = */ apicR3Destruct,
/* .pfnRelocate = */ apicR3Relocate,
/* .pfnMemSetup = */ NULL,
/* .pfnPowerOn = */ NULL,
/* .pfnReset = */ apicR3Reset,
/* .pfnSuspend = */ NULL,
/* .pfnResume = */ NULL,
/* .pfnAttach = */ NULL,
/* .pfnDetach = */ NULL,
/* .pfnQueryInterface = */ NULL,
/* .pfnInitComplete = */ apicR3InitComplete,
/* .pfnPowerOff = */ NULL,
/* .pfnSoftReset = */ NULL,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#elif defined(IN_RING0)
/* .pfnEarlyConstruct = */ NULL,
/* .pfnConstruct = */ apicRZConstruct,
/* .pfnDestruct = */ NULL,
/* .pfnFinalDestruct = */ NULL,
/* .pfnRequest = */ NULL,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#elif defined(IN_RC)
/* .pfnConstruct = */ apicRZConstruct,
/* .pfnReserved0 = */ NULL,
/* .pfnReserved1 = */ NULL,
/* .pfnReserved2 = */ NULL,
/* .pfnReserved3 = */ NULL,
/* .pfnReserved4 = */ NULL,
/* .pfnReserved5 = */ NULL,
/* .pfnReserved6 = */ NULL,
/* .pfnReserved7 = */ NULL,
#else
# error "Not in IN_RING3, IN_RING0 or IN_RC!"
#endif
/* .u32VersionEnd = */ PDM_DEVREG_VERSION
};
|