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
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
// Copyright © 2024 Institute of Software, CAS. All rights reserved.
//
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.
// Part of public API
#[cfg(target_arch = "x86_64")]
pub use kvm_bindings::nested::KvmNestedStateBuffer;
use kvm_bindings::*;
use libc::EINVAL;
use std::fs::File;
use std::os::unix::io::{AsRawFd, RawFd};
use crate::ioctls::{KvmCoalescedIoRing, KvmRunWrapper, Result};
use crate::kvm_ioctls::*;
use vmm_sys_util::errno;
use vmm_sys_util::ioctl::{ioctl, ioctl_with_mut_ref, ioctl_with_ref};
#[cfg(target_arch = "x86_64")]
use {
std::num::NonZeroUsize,
vmm_sys_util::ioctl::{ioctl_with_mut_ptr, ioctl_with_ptr, ioctl_with_val},
};
/// Helper method to obtain the size of the register through its id
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn reg_size(reg_id: u64) -> usize {
2_usize.pow(((reg_id & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT) as u32)
}
/// Information about a [`VcpuExit`] triggered by an Hypercall (`KVM_EXIT_HYPERCALL`).
#[derive(Debug)]
pub struct HypercallExit<'a> {
/// The hypercall number.
pub nr: u64,
/// The arguments for the hypercall.
pub args: [u64; 6],
/// The return code to be indicated to the guest.
pub ret: &'a mut u64,
/// Whether the hypercall was executed in long mode.
pub longmode: u32,
}
/// Information about a [`VcpuExit`] triggered by an MSR read (`KVM_EXIT_X86_RDMSR`).
#[derive(Debug)]
pub struct ReadMsrExit<'a> {
/// Must be set to 1 by the the user if the read access should fail. This
/// will inject a #GP fault into the guest when the VCPU is executed
/// again.
pub error: &'a mut u8,
/// The reason for this exit.
pub reason: MsrExitReason,
/// The MSR the guest wants to read.
pub index: u32,
/// The data to be supplied by the user as the MSR Contents to the guest.
pub data: &'a mut u64,
}
/// Information about a [`VcpuExit`] triggered by an MSR write (`KVM_EXIT_X86_WRMSR`).
#[derive(Debug)]
pub struct WriteMsrExit<'a> {
/// Must be set to 1 by the the user if the write access should fail. This
/// will inject a #GP fault into the guest when the VCPU is executed
/// again.
pub error: &'a mut u8,
/// The reason for this exit.
pub reason: MsrExitReason,
/// The MSR the guest wants to write.
pub index: u32,
/// The data the guest wants to write into the MSR.
pub data: u64,
}
bitflags::bitflags! {
/// The reason for a [`VcpuExit::X86Rdmsr`] or[`VcpuExit::X86Wrmsr`]. This
/// is also used when enabling
/// [`Cap::X86UserSpaceMsr`](crate::Cap::X86UserSpaceMsr) to specify which
/// reasons should be forwarded to the user via those exits.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MsrExitReason: u32 {
/// Corresponds to [`KVM_MSR_EXIT_REASON_UNKNOWN`]. The exit was
/// triggered by an access to an MSR that is unknown to KVM.
const Unknown = KVM_MSR_EXIT_REASON_UNKNOWN;
/// Corresponds to [`KVM_MSR_EXIT_REASON_INVAL`]. The exit was
/// triggered by an access to an invalid MSR or to reserved bits.
const Inval = KVM_MSR_EXIT_REASON_INVAL;
/// Corresponds to [`KVM_MSR_EXIT_REASON_FILTER`]. The exit was
/// triggered by an access to a filtered MSR.
const Filter = KVM_MSR_EXIT_REASON_FILTER;
}
}
/// Reasons for vCPU exits.
///
/// The exit reasons are mapped to the `KVM_EXIT_*` defines in the
/// [Linux KVM header](https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/kvm.h).
#[derive(Debug)]
pub enum VcpuExit<'a> {
/// An out port instruction was run on the given port with the given data.
IoOut(u16 /* port */, &'a [u8] /* data */),
/// An in port instruction was run on the given port.
///
/// The given slice should be filled in before [run()](struct.VcpuFd.html#method.run)
/// is called again.
IoIn(u16 /* port */, &'a mut [u8] /* data */),
/// A read instruction was run against the given MMIO address.
///
/// The given slice should be filled in before [run()](struct.VcpuFd.html#method.run)
/// is called again.
MmioRead(u64 /* address */, &'a mut [u8]),
/// A write instruction was run against the given MMIO address with the given data.
MmioWrite(u64 /* address */, &'a [u8]),
/// Corresponds to KVM_EXIT_UNKNOWN.
Unknown,
/// Corresponds to KVM_EXIT_EXCEPTION.
Exception,
/// Corresponds to KVM_EXIT_HYPERCALL.
Hypercall(HypercallExit<'a>),
/// Corresponds to KVM_EXIT_DEBUG.
///
/// Provides architecture specific information for the debug event.
Debug(kvm_debug_exit_arch),
/// Corresponds to KVM_EXIT_HLT.
Hlt,
/// Corresponds to KVM_EXIT_IRQ_WINDOW_OPEN.
IrqWindowOpen,
/// Corresponds to KVM_EXIT_SHUTDOWN.
Shutdown,
/// Corresponds to KVM_EXIT_FAIL_ENTRY.
FailEntry(
u64, /* hardware_entry_failure_reason */
u32, /* cpu */
),
/// Corresponds to KVM_EXIT_INTR.
Intr,
/// Corresponds to KVM_EXIT_SET_TPR.
SetTpr,
/// Corresponds to KVM_EXIT_TPR_ACCESS.
TprAccess,
/// Corresponds to KVM_EXIT_S390_SIEIC.
S390Sieic,
/// Corresponds to KVM_EXIT_S390_RESET.
S390Reset,
/// Corresponds to KVM_EXIT_DCR.
Dcr,
/// Corresponds to KVM_EXIT_NMI.
Nmi,
/// Corresponds to KVM_EXIT_INTERNAL_ERROR.
InternalError,
/// Corresponds to KVM_EXIT_OSI.
Osi,
/// Corresponds to KVM_EXIT_PAPR_HCALL.
PaprHcall,
/// Corresponds to KVM_EXIT_S390_UCONTROL.
S390Ucontrol,
/// Corresponds to KVM_EXIT_WATCHDOG.
Watchdog,
/// Corresponds to KVM_EXIT_S390_TSCH.
S390Tsch,
/// Corresponds to KVM_EXIT_EPR.
Epr,
/// Corresponds to KVM_EXIT_SYSTEM_EVENT.
SystemEvent(u32 /* type */, &'a [u64] /* data */),
/// Corresponds to KVM_EXIT_S390_STSI.
S390Stsi,
/// Corresponds to KVM_EXIT_IOAPIC_EOI.
IoapicEoi(u8 /* vector */),
/// Corresponds to KVM_EXIT_HYPERV.
Hyperv,
/// Corresponds to KVM_EXIT_X86_RDMSR.
X86Rdmsr(ReadMsrExit<'a>),
/// Corresponds to KVM_EXIT_X86_WRMSR.
X86Wrmsr(WriteMsrExit<'a>),
/// Corresponds to KVM_EXIT_MEMORY_FAULT.
MemoryFault {
/// flags
flags: u64,
/// gpa
gpa: u64,
/// size
size: u64,
},
/// Corresponds to an exit reason that is unknown from the current version
/// of the kvm-ioctls crate. Let the consumer decide about what to do with
/// it.
Unsupported(u32),
}
/// Wrapper over KVM vCPU ioctls.
#[derive(Debug)]
pub struct VcpuFd {
vcpu: File,
kvm_run_ptr: KvmRunWrapper,
/// A pointer to the coalesced MMIO page
coalesced_mmio_ring: Option<KvmCoalescedIoRing>,
}
/// KVM Sync Registers used to tell KVM which registers to sync
#[repr(u32)]
#[derive(Debug, Copy, Clone)]
#[cfg(target_arch = "x86_64")]
pub enum SyncReg {
/// General purpose registers,
Register = KVM_SYNC_X86_REGS,
/// System registers
SystemRegister = KVM_SYNC_X86_SREGS,
/// CPU events
VcpuEvents = KVM_SYNC_X86_EVENTS,
}
impl VcpuFd {
/// Returns the vCPU general purpose registers.
///
/// The registers are returned in a `kvm_regs` structure as defined in the
/// [KVM API documentation](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// See documentation for `KVM_GET_REGS`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let regs = vcpu.get_regs().unwrap();
/// ```
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
pub fn get_regs(&self) -> Result<kvm_regs> {
let mut regs = kvm_regs::default();
// SAFETY: Safe because we know that our file is a vCPU fd, we know the kernel will only
// read the correct amount of memory from our pointer, and we verify the return result.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_REGS(), &mut regs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(regs)
}
/// Sets a specified piece of cpu configuration and/or state.
///
/// See the documentation for `KVM_SET_DEVICE_ATTR` in
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt)
/// # Arguments
///
/// * `device_attr` - The cpu attribute to be set.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::{
/// KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT
/// };
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let dist_attr = kvm_bindings::kvm_device_attr {
/// group: KVM_ARM_VCPU_PMU_V3_CTRL,
/// attr: u64::from(KVM_ARM_VCPU_PMU_V3_INIT),
/// addr: 0x0,
/// flags: 0,
/// };
///
/// if (vcpu.has_device_attr(&dist_attr).is_ok()) {
/// vcpu.set_device_attr(&dist_attr).unwrap();
/// }
/// ```
#[cfg(target_arch = "aarch64")]
pub fn set_device_attr(&self, device_attr: &kvm_device_attr) -> Result<()> {
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_DEVICE_ATTR(), device_attr) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Tests whether a cpu supports a particular attribute.
///
/// See the documentation for `KVM_HAS_DEVICE_ATTR` in
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt)
/// # Arguments
///
/// * `device_attr` - The cpu attribute to be tested. `addr` field is ignored.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::{
/// KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT
/// };
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let dist_attr = kvm_bindings::kvm_device_attr {
/// group: KVM_ARM_VCPU_PMU_V3_CTRL,
/// attr: u64::from(KVM_ARM_VCPU_PMU_V3_INIT),
/// addr: 0x0,
/// flags: 0,
/// };
///
/// vcpu.has_device_attr(&dist_attr);
/// ```
#[cfg(target_arch = "aarch64")]
pub fn has_device_attr(&self, device_attr: &kvm_device_attr) -> Result<()> {
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl_with_ref(self, KVM_HAS_DEVICE_ATTR(), device_attr) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Sets the vCPU general purpose registers using the `KVM_SET_REGS` ioctl.
///
/// # Arguments
///
/// * `regs` - general purpose registers. For details check the `kvm_regs` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// // Get the current vCPU registers.
/// let mut regs = vcpu.get_regs().unwrap();
/// // Set a new value for the Instruction Pointer.
/// regs.rip = 0x100;
/// vcpu.set_regs(®s).unwrap();
/// ```
#[cfg(not(any(target_arch = "aarch64", target_arch = "riscv64")))]
pub fn set_regs(&self, regs: &kvm_regs) -> Result<()> {
// SAFETY: Safe because we know that our file is a vCPU fd, we know the kernel will only
// read the correct amount of memory from our pointer, and we verify the return result.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_REGS(), regs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Returns the vCPU special registers.
///
/// The registers are returned in a `kvm_sregs` structure as defined in the
/// [KVM API documentation](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// See documentation for `KVM_GET_SREGS`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let sregs = vcpu.get_sregs().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_sregs(&self) -> Result<kvm_sregs> {
let mut regs = kvm_sregs::default();
// SAFETY: Safe because we know that our file is a vCPU fd, we know the kernel will only
// write the correct amount of memory to our pointer, and we verify the return result.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_SREGS(), &mut regs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(regs)
}
/// Sets the vCPU special registers using the `KVM_SET_SREGS` ioctl.
///
/// # Arguments
///
/// * `sregs` - Special registers. For details check the `kvm_sregs` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let mut sregs = vcpu.get_sregs().unwrap();
/// // Update the code segment (cs).
/// sregs.cs.base = 0;
/// sregs.cs.selector = 0;
/// vcpu.set_sregs(&sregs).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_sregs(&self, sregs: &kvm_sregs) -> Result<()> {
// SAFETY: Safe because we know that our file is a vCPU fd, we know the kernel will only
// read the correct amount of memory from our pointer, and we verify the return result.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_SREGS(), sregs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Returns the floating point state (FPU) from the vCPU.
///
/// The state is returned in a `kvm_fpu` structure as defined in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// See the documentation for `KVM_GET_FPU`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let fpu = vcpu.get_fpu().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_fpu(&self) -> Result<kvm_fpu> {
let mut fpu = kvm_fpu::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_fpu struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_FPU(), &mut fpu) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(fpu)
}
/// Set the floating point state (FPU) of a vCPU using the `KVM_SET_FPU` ioct.
///
/// # Arguments
///
/// * `fpu` - FPU configuration. For details check the `kvm_fpu` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::kvm_fpu;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let KVM_FPU_CWD: u16 = 0x37f;
/// let fpu = kvm_fpu {
/// fcw: KVM_FPU_CWD,
/// ..Default::default()
/// };
/// vcpu.set_fpu(&fpu).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_fpu(&self, fpu: &kvm_fpu) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_fpu struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_FPU(), fpu) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// X86 specific call to setup the CPUID registers.
///
/// See the documentation for `KVM_SET_CPUID2`.
///
/// # Arguments
///
/// * `cpuid` - CPUID registers.
///
/// # Example
///
/// ```rust
/// # use kvm_bindings::KVM_MAX_CPUID_ENTRIES;
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let mut kvm_cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// // Update the CPUID entries to disable the EPB feature.
/// const ECX_EPB_SHIFT: u32 = 3;
/// let entries = kvm_cpuid.as_mut_slice();
/// for entry in entries.iter_mut() {
/// match entry.function {
/// 6 => entry.ecx &= !(1 << ECX_EPB_SHIFT),
/// _ => (),
/// }
/// }
///
/// vcpu.set_cpuid2(&kvm_cpuid).unwrap();
/// ```
///
#[cfg(target_arch = "x86_64")]
pub fn set_cpuid2(&self, cpuid: &CpuId) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_cpuid2 struct.
let ret = unsafe { ioctl_with_ptr(self, KVM_SET_CPUID2(), cpuid.as_fam_struct_ptr()) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// X86 specific call to retrieve the CPUID registers.
///
/// It requires knowledge of how many `kvm_cpuid_entry2` entries there are to get.
/// See the documentation for `KVM_GET_CPUID2` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `num_entries` - Number of CPUID entries to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_bindings::KVM_MAX_CPUID_ENTRIES;
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let cpuid = vcpu.get_cpuid2(KVM_MAX_CPUID_ENTRIES).unwrap();
/// ```
///
#[cfg(target_arch = "x86_64")]
pub fn get_cpuid2(&self, num_entries: usize) -> Result<CpuId> {
if num_entries > KVM_MAX_CPUID_ENTRIES {
// Returns the same error the underlying `ioctl` would have sent.
return Err(errno::Error::new(libc::ENOMEM));
}
let mut cpuid = CpuId::new(num_entries).map_err(|_| errno::Error::new(libc::ENOMEM))?;
let ret =
// SAFETY: Here we trust the kernel not to read past the end of the kvm_cpuid2 struct.
unsafe { ioctl_with_mut_ptr(self, KVM_GET_CPUID2(), cpuid.as_mut_fam_struct_ptr()) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(cpuid)
}
///
/// See the documentation for `KVM_ENABLE_CAP`.
///
/// # Arguments
///
/// * kvm_enable_cap - KVM capability structure. For details check the `kvm_enable_cap`
/// structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_bindings::{kvm_enable_cap, KVM_MAX_CPUID_ENTRIES, KVM_CAP_HYPERV_SYNIC, KVM_CAP_SPLIT_IRQCHIP};
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut cap: kvm_enable_cap = Default::default();
/// // KVM_CAP_HYPERV_SYNIC needs KVM_CAP_SPLIT_IRQCHIP enabled
/// cap.cap = KVM_CAP_SPLIT_IRQCHIP;
/// cap.args[0] = 24;
/// vm.enable_cap(&cap).unwrap();
///
/// let vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::HypervSynic) {
/// let mut cap: kvm_enable_cap = Default::default();
/// cap.cap = KVM_CAP_HYPERV_SYNIC;
/// vcpu.enable_cap(&cap).unwrap();
/// }
/// ```
///
#[cfg(target_arch = "x86_64")]
pub fn enable_cap(&self, cap: &kvm_enable_cap) -> Result<()> {
// SAFETY: The ioctl is safe because we allocated the struct and we know the
// kernel will write exactly the size of the struct.
let ret = unsafe { ioctl_with_ref(self, KVM_ENABLE_CAP(), cap) };
if ret == 0 {
Ok(())
} else {
Err(errno::Error::last())
}
}
/// Returns the state of the LAPIC (Local Advanced Programmable Interrupt Controller).
///
/// The state is returned in a `kvm_lapic_state` structure as defined in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// See the documentation for `KVM_GET_LAPIC`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// // For `get_lapic` to work, you first need to create a IRQ chip before creating the vCPU.
/// vm.create_irq_chip().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let lapic = vcpu.get_lapic().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_lapic(&self) -> Result<kvm_lapic_state> {
let mut klapic = kvm_lapic_state::default();
// SAFETY: The ioctl is unsafe unless you trust the kernel not to write past the end of the
// local_apic struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_LAPIC(), &mut klapic) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(klapic)
}
/// Sets the state of the LAPIC (Local Advanced Programmable Interrupt Controller).
///
/// See the documentation for `KVM_SET_LAPIC`.
///
/// # Arguments
///
/// * `klapic` - LAPIC state. For details check the `kvm_lapic_state` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// use std::io::Write;
///
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// // For `get_lapic` to work, you first need to create a IRQ chip before creating the vCPU.
/// vm.create_irq_chip().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let mut lapic = vcpu.get_lapic().unwrap();
///
/// // Write to APIC_ICR offset the value 2.
/// let apic_icr_offset = 0x300;
/// let write_value: &[u8] = &[2, 0, 0, 0];
/// let mut apic_icr_slice =
/// unsafe { &mut *(&mut lapic.regs[apic_icr_offset..] as *mut [i8] as *mut [u8]) };
/// apic_icr_slice.write(write_value).unwrap();
///
/// // Update the value of LAPIC.
/// vcpu.set_lapic(&lapic).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_lapic(&self, klapic: &kvm_lapic_state) -> Result<()> {
// SAFETY: The ioctl is safe because the kernel will only read from the klapic struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_LAPIC(), klapic) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Returns the model-specific registers (MSR) for this vCPU.
///
/// It emulates `KVM_GET_MSRS` ioctl's behavior by returning the number of MSRs
/// successfully read upon success or the last error number in case of failure.
/// The MSRs are returned in the `msr` method argument.
///
/// # Arguments
///
/// * `msrs` - MSRs (input/output). For details check the `kvm_msrs` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::{kvm_msr_entry, Msrs};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// // Configure the struct to say which entries we want to get.
/// let mut msrs = Msrs::from_entries(&[
/// kvm_msr_entry {
/// index: 0x0000_0174,
/// ..Default::default()
/// },
/// kvm_msr_entry {
/// index: 0x0000_0175,
/// ..Default::default()
/// },
/// ])
/// .unwrap();
/// let read = vcpu.get_msrs(&mut msrs).unwrap();
/// assert_eq!(read, 2);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_msrs(&self, msrs: &mut Msrs) -> Result<usize> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_msrs struct.
let ret = unsafe { ioctl_with_mut_ptr(self, KVM_GET_MSRS(), msrs.as_mut_fam_struct_ptr()) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(ret as usize)
}
/// Setup the model-specific registers (MSR) for this vCPU.
/// Returns the number of MSR entries actually written.
///
/// See the documentation for `KVM_SET_MSRS`.
///
/// # Arguments
///
/// * `msrs` - MSRs. For details check the `kvm_msrs` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::{kvm_msr_entry, Msrs};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// // Configure the entries we want to set.
/// let mut msrs = Msrs::from_entries(&[kvm_msr_entry {
/// index: 0x0000_0174,
/// ..Default::default()
/// }])
/// .unwrap();
/// let written = vcpu.set_msrs(&msrs).unwrap();
/// assert_eq!(written, 1);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_msrs(&self, msrs: &Msrs) -> Result<usize> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_msrs struct.
let ret = unsafe { ioctl_with_ptr(self, KVM_SET_MSRS(), msrs.as_fam_struct_ptr()) };
// KVM_SET_MSRS actually returns the number of msr entries written.
if ret < 0 {
return Err(errno::Error::last());
}
Ok(ret as usize)
}
/// Returns the vcpu's current "multiprocessing state".
///
/// See the documentation for `KVM_GET_MP_STATE` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_mp_state` - multiprocessing state to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let mp_state = vcpu.get_mp_state().unwrap();
/// ```
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
pub fn get_mp_state(&self) -> Result<kvm_mp_state> {
let mut mp_state = Default::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_mp_state struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_MP_STATE(), &mut mp_state) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(mp_state)
}
/// Sets the vcpu's current "multiprocessing state".
///
/// See the documentation for `KVM_SET_MP_STATE` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_mp_state` - multiprocessing state to be written.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let mp_state = Default::default();
/// // Your `mp_state` manipulation here.
/// vcpu.set_mp_state(mp_state).unwrap();
/// ```
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
pub fn set_mp_state(&self, mp_state: kvm_mp_state) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_mp_state struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_MP_STATE(), &mp_state) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// X86 specific call that returns the vcpu's current "xsave struct".
///
/// See the documentation for `KVM_GET_XSAVE` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_xsave` - xsave struct to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xsave = vcpu.get_xsave().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_xsave(&self) -> Result<kvm_xsave> {
let mut xsave = Default::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_xsave struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_XSAVE(), &mut xsave) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(xsave)
}
/// X86 specific call that gets the current vcpu's "xsave struct" via `KVM_GET_XSAVE2`.
///
/// See the documentation for `KVM_GET_XSAVE2` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `xsave` - A mutable reference to an [`Xsave`] instance that will be populated with the
/// current vcpu's "xsave struct".
///
/// # Safety
///
/// This function is unsafe because there is no guarantee `xsave` is allocated with enough space
/// to hold the entire xsave state.
///
/// The required size in bytes can be retrieved via `KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)` and
/// can vary depending on features that have been dynamically enabled by `arch_prctl()`. Thus,
/// any features must not be enabled dynamically after the required size has been confirmed.
///
/// If `xsave` is not large enough, `KVM_GET_XSAVE2` copies data beyond the allocated area,
/// possibly causing undefined behavior.
///
/// See the documentation for dynamically enabled XSTATE features in the
/// [kernel doc](https://docs.kernel.org/arch/x86/xstate.html).
///
/// # Example
///
/// ```rust
/// # extern crate vmm_sys_util;
/// # use kvm_ioctls::{Kvm, Cap};
/// # use kvm_bindings::{Xsave, kvm_xsave, kvm_xsave2};
/// # use vmm_sys_util::fam::FamStruct;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xsave_size = vm.check_extension_int(Cap::Xsave2);
/// if xsave_size > 0 {
/// let fam_size = (xsave_size as usize - std::mem::size_of::<kvm_xsave>())
/// .div_ceil(std::mem::size_of::<<kvm_xsave2 as FamStruct>::Entry>());
/// let mut xsave = Xsave::new(fam_size).unwrap();
/// unsafe { vcpu.get_xsave2(&mut xsave).unwrap() };
/// }
/// ```
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub unsafe fn get_xsave2(&self, xsave: &mut Xsave) -> Result<()> {
// SAFETY: Safe as long as `xsave` is allocated with enough space to hold the entire "xsave
// struct". That's why this function is unsafe.
let ret = unsafe {
ioctl_with_mut_ref(self, KVM_GET_XSAVE2(), &mut xsave.as_mut_fam_struct().xsave)
};
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// X86 specific call that sets the vcpu's current "xsave struct".
///
/// See the documentation for `KVM_SET_XSAVE` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `xsave` - xsave struct to be written.
///
/// # Safety
///
/// The C `kvm_xsave` struct was extended to have a flexible array member (FAM) at the end in
/// Linux 5.17. The size can vary depending on features that have been dynamically enabled via
/// `arch_prctl()` and the required size can be retrieved via
/// `KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)`. That means `KVM_SET_XSAVE` may copy data beyond the
/// size of the traditional C `kvm_xsave` struct (i.e. 4096 bytes) now.
///
/// It is safe if used on Linux prior to 5.17, if no XSTATE features are enabled dynamically or
/// if the required size is still within the traditional 4096 bytes even with dynamically
/// enabled features. However, if any features are enabled dynamically, it is recommended to use
/// `set_xsave2()` instead.
///
/// See the documentation for dynamically enabled XSTATE features in the
/// [kernel doc](https://docs.kernel.org/arch/x86/xstate.html).
///
/// Theoretically, it can be made safe by checking which features are enabled in the bit vector
/// of the XSTATE header and validating the required size is less than or equal to 4096 bytes.
/// However, to do it properly, we would need to extract the XSTATE header from the `kvm_xsave`
/// struct, check enabled features, retrieve the required size for each enabled feature (like
/// `setup_xstate_cache()` do in Linux) and calculate the total size.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xsave = Default::default();
/// // Your `xsave` manipulation here.
/// unsafe { vcpu.set_xsave(&xsave).unwrap() };
/// ```
#[cfg(target_arch = "x86_64")]
pub unsafe fn set_xsave(&self, xsave: &kvm_xsave) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_xsave struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_XSAVE(), xsave) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Convenience function for doing `KVM_SET_XSAVE` with the FAM-enabled [`Xsave`]
/// instead of the pre-5.17 plain [`kvm_xsave`].
///
/// # Arguments
///
/// * `xsave` - A reference to an [`Xsave`] instance to be set.
///
/// # Safety
///
/// This function is unsafe because there is no guarantee `xsave` is properly allocated with
/// the size that KVM assumes.
///
/// The required size in bytes can be retrieved via `KVM_CHECK_EXTENSION(KVM_CAP_XSAVE2)` and
/// can vary depending on features that have been dynamically enabled by `arch_prctl()`. Thus,
/// any features must not be enabled after the required size has been confirmed.
///
/// If `xsave` is not large enough, `KVM_SET_XSAVE` copies data beyond the allocated area to
/// the kernel, possibly causing undefined behavior.
///
/// See the documentation for dynamically enabled XSTATE features in the
/// [kernel doc](https://docs.kernel.org/arch/x86/xstate.html).
///
/// # Example
///
/// ```rust
/// # extern crate vmm_sys_util;
/// # use kvm_ioctls::{Kvm, Cap};
/// # use kvm_bindings::{Xsave, kvm_xsave, kvm_xsave2};
/// # use vmm_sys_util::fam::FamStruct;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xsave_size = vm.check_extension_int(Cap::Xsave2);
/// if xsave_size > 0 {
/// let fam_size = (xsave_size as usize - std::mem::size_of::<kvm_xsave>())
/// .div_ceil(std::mem::size_of::<<kvm_xsave2 as FamStruct>::Entry>());
/// let xsave = Xsave::new(fam_size).unwrap();
/// // Your `xsave` manipulation here.
/// unsafe { vcpu.set_xsave2(&xsave).unwrap() };
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub unsafe fn set_xsave2(&self, xsave: &Xsave) -> Result<()> {
// SAFETY: we trust the kernel and verified parameters
unsafe { self.set_xsave(&xsave.as_fam_struct_ref().xsave) }
}
/// X86 specific call that returns the vcpu's current "xcrs".
///
/// See the documentation for `KVM_GET_XCRS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_xcrs` - xcrs to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xcrs = vcpu.get_xcrs().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_xcrs(&self) -> Result<kvm_xcrs> {
let mut xcrs = Default::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_xcrs struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_XCRS(), &mut xcrs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(xcrs)
}
/// X86 specific call that sets the vcpu's current "xcrs".
///
/// See the documentation for `KVM_SET_XCRS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_xcrs` - xcrs to be written.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let xcrs = Default::default();
/// // Your `xcrs` manipulation here.
/// vcpu.set_xcrs(&xcrs).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_xcrs(&self, xcrs: &kvm_xcrs) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_xcrs struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_XCRS(), xcrs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// X86 specific call that returns the vcpu's current "debug registers".
///
/// See the documentation for `KVM_GET_DEBUGREGS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_debugregs` - debug registers to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let debug_regs = vcpu.get_debug_regs().unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn get_debug_regs(&self) -> Result<kvm_debugregs> {
let mut debug_regs = Default::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_debugregs struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_DEBUGREGS(), &mut debug_regs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(debug_regs)
}
/// X86 specific call that sets the vcpu's current "debug registers".
///
/// See the documentation for `KVM_SET_DEBUGREGS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_debugregs` - debug registers to be written.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let debug_regs = Default::default();
/// // Your `debug_regs` manipulation here.
/// vcpu.set_debug_regs(&debug_regs).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_debug_regs(&self, debug_regs: &kvm_debugregs) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_debugregs struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_DEBUGREGS(), debug_regs) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Returns currently pending exceptions, interrupts, and NMIs as well as related
/// states of the vcpu.
///
/// See the documentation for `KVM_GET_VCPU_EVENTS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_vcpu_events` - vcpu events to be read.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// if kvm.check_extension(Cap::VcpuEvents) {
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// // On arm64, vCPU needs to be initialized before accesing events
/// #[cfg(target_arch = "aarch64")]
/// {
/// let mut kvi = kvm_bindings::kvm_vcpu_init::default();
/// vm.get_preferred_target(&mut kvi).unwrap();
/// vcpu.vcpu_init(&kvi).unwrap();
/// }
/// let vcpu_events = vcpu.get_vcpu_events().unwrap();
/// }
/// ```
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn get_vcpu_events(&self) -> Result<kvm_vcpu_events> {
let mut vcpu_events = Default::default();
// SAFETY: Here we trust the kernel not to read past the end of the kvm_vcpu_events struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_VCPU_EVENTS(), &mut vcpu_events) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(vcpu_events)
}
/// Sets pending exceptions, interrupts, and NMIs as well as related states of the vcpu.
///
/// See the documentation for `KVM_SET_VCPU_EVENTS` in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Arguments
///
/// * `kvm_vcpu_events` - vcpu events to be written.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// if kvm.check_extension(Cap::VcpuEvents) {
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// // On arm64, vCPU needs to be initialized before accesing events
/// #[cfg(target_arch = "aarch64")]
/// {
/// let mut kvi = kvm_bindings::kvm_vcpu_init::default();
/// vm.get_preferred_target(&mut kvi).unwrap();
/// vcpu.vcpu_init(&kvi).unwrap();
/// }
/// let vcpu_events = Default::default();
/// // Your `vcpu_events` manipulation here.
/// vcpu.set_vcpu_events(&vcpu_events).unwrap();
/// }
/// ```
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
pub fn set_vcpu_events(&self, vcpu_events: &kvm_vcpu_events) -> Result<()> {
// SAFETY: Here we trust the kernel not to read past the end of the kvm_vcpu_events struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_VCPU_EVENTS(), vcpu_events) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Sets the type of CPU to be exposed to the guest and optional features.
///
/// This initializes an ARM vCPU to the specified type with the specified features
/// and resets the values of all of its registers to defaults. See the documentation for
/// `KVM_ARM_VCPU_INIT`.
///
/// # Arguments
///
/// * `kvi` - information about preferred CPU target type and recommended features for it.
/// For details check the `kvm_vcpu_init` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
/// ```rust
/// # use kvm_ioctls::Kvm;
/// use kvm_bindings::kvm_vcpu_init;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let mut kvi = kvm_vcpu_init::default();
/// vm.get_preferred_target(&mut kvi).unwrap();
/// vcpu.vcpu_init(&kvi).unwrap();
/// ```
#[cfg(target_arch = "aarch64")]
pub fn vcpu_init(&self, kvi: &kvm_vcpu_init) -> Result<()> {
// SAFETY: This is safe because we allocated the struct and we know the kernel will read
// exactly the size of the struct.
let ret = unsafe { ioctl_with_ref(self, KVM_ARM_VCPU_INIT(), kvi) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Finalizes the configuration of the specified vcpu feature.
///
/// The vcpu must already have been initialised, enabling the affected feature,
/// by means of a successful KVM_ARM_VCPU_INIT call with the appropriate flag set
/// in features[].
///
/// For affected vcpu features, this is a mandatory step that must be performed before
/// the vcpu is fully usable.
///
/// Between KVM_ARM_VCPU_INIT and KVM_ARM_VCPU_FINALIZE, the feature may be configured
/// by use of ioctls such as KVM_SET_ONE_REG. The exact configuration that should be
/// performaned and how to do it are feature-dependent.
///
/// Other calls that depend on a particular feature being finalized, such as KVM_RUN,
/// KVM_GET_REG_LIST, KVM_GET_ONE_REG and KVM_SET_ONE_REG, will fail with -EPERM unless
/// the feature has already been finalized by means of a KVM_ARM_VCPU_FINALIZE call.
///
/// See KVM_ARM_VCPU_INIT for details of vcpu features that require finalization using this ioctl.
/// [KVM_ARM_VCPU_FINALIZE](https://www.kernel.org/doc/html/latest/virt/kvm/api.html#kvm-arm-vcpu-finalize).
///
/// # Arguments
///
/// * `feature` - vCPU features that needs to be finalized.
///
/// # Example
/// ```rust
/// # use kvm_ioctls::Kvm;
/// use std::arch::is_aarch64_feature_detected;
///
/// use kvm_bindings::{KVM_ARM_VCPU_SVE, kvm_vcpu_init};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let mut kvi = kvm_vcpu_init::default();
/// vm.get_preferred_target(&mut kvi).unwrap();
/// kvi.features[0] |= 1 << KVM_ARM_VCPU_SVE;
/// if is_aarch64_feature_detected!("sve2") || is_aarch64_feature_detected!("sve") {
/// vcpu.vcpu_init(&kvi).unwrap();
/// let feature = KVM_ARM_VCPU_SVE as i32;
/// vcpu.vcpu_finalize(&feature).unwrap();
/// }
/// ```
#[cfg(target_arch = "aarch64")]
pub fn vcpu_finalize(&self, feature: &std::os::raw::c_int) -> Result<()> {
// SAFETY: This is safe because we know the kernel will only read this
// parameter to select the correct finalization case in KVM.
let ret = unsafe { ioctl_with_ref(self, KVM_ARM_VCPU_FINALIZE(), feature) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Returns the guest registers that are supported for the
/// KVM_GET_ONE_REG/KVM_SET_ONE_REG calls.
///
/// # Arguments
///
/// * `reg_list` - list of registers (input/output). For details check the `kvm_reg_list`
/// structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::RegList;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// // KVM_GET_REG_LIST on Aarch64 demands that the vcpus be initialized.
/// # #[cfg(target_arch = "aarch64")]
/// # {
/// let mut kvi = kvm_bindings::kvm_vcpu_init::default();
/// vm.get_preferred_target(&mut kvi).unwrap();
/// vcpu.vcpu_init(&kvi).expect("Cannot initialize vcpu");
///
/// let mut reg_list = RegList::new(500).unwrap();
/// vcpu.get_reg_list(&mut reg_list).unwrap();
/// assert!(reg_list.as_fam_struct_ref().n > 0);
/// # }
/// ```
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn get_reg_list(&self, reg_list: &mut RegList) -> Result<()> {
let ret =
// SAFETY: This is safe because we allocated the struct and we trust the kernel will read
// exactly the size of the struct.
unsafe { ioctl_with_mut_ref(self, KVM_GET_REG_LIST(), reg_list.as_mut_fam_struct()) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Sets processor-specific debug registers and configures the vcpu for handling
/// certain guest debug events using the `KVM_SET_GUEST_DEBUG` ioctl.
///
/// # Arguments
///
/// * `debug_struct` - control bitfields and debug registers, depending on the specific architecture.
/// For details check the `kvm_guest_debug` structure in the
/// [KVM API doc](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// # use kvm_bindings::{
/// # KVM_GUESTDBG_ENABLE, KVM_GUESTDBG_USE_SW_BP, kvm_guest_debug_arch, kvm_guest_debug
/// # };
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
///
/// let debug_struct = kvm_guest_debug {
/// // Configure the vcpu so that a KVM_DEBUG_EXIT would be generated
/// // when encountering a software breakpoint during execution
/// control: KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP,
/// pad: 0,
/// // Reset all arch-specific debug registers
/// arch: Default::default(),
/// };
///
/// vcpu.set_guest_debug(&debug_struct).unwrap();
/// ```
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "s390x",
target_arch = "powerpc"
))]
pub fn set_guest_debug(&self, debug_struct: &kvm_guest_debug) -> Result<()> {
// SAFETY: Safe because we allocated the structure and we trust the kernel.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_GUEST_DEBUG(), debug_struct) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Sets the value of one register for this vCPU.
///
/// The id of the register is encoded as specified in the kernel documentation
/// for `KVM_SET_ONE_REG`.
///
/// # Arguments
///
/// * `reg_id` - ID of the register for which we are setting the value.
/// * `data` - byte slice where the register value will be written to.
///
/// # Note
///
/// `data` should be equal or bigger then the register size
/// oterwise function will return EINVAL error
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn set_one_reg(&self, reg_id: u64, data: &[u8]) -> Result<usize> {
let reg_size = reg_size(reg_id);
if data.len() < reg_size {
return Err(errno::Error::new(libc::EINVAL));
}
let onereg = kvm_one_reg {
id: reg_id,
addr: data.as_ptr() as u64,
};
// SAFETY: This is safe because we allocated the struct and we know the kernel will read
// exactly the size of the struct.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_ONE_REG(), &onereg) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(reg_size)
}
/// Writes the value of the specified vCPU register into provided buffer.
///
/// The id of the register is encoded as specified in the kernel documentation
/// for `KVM_GET_ONE_REG`.
///
/// # Arguments
///
/// * `reg_id` - ID of the register.
/// * `data` - byte slice where the register value will be written to.
/// # Note
///
/// `data` should be equal or bigger then the register size
/// oterwise function will return EINVAL error
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub fn get_one_reg(&self, reg_id: u64, data: &mut [u8]) -> Result<usize> {
let reg_size = reg_size(reg_id);
if data.len() < reg_size {
return Err(errno::Error::new(libc::EINVAL));
}
let mut onereg = kvm_one_reg {
id: reg_id,
addr: data.as_ptr() as u64,
};
// SAFETY: This is safe because we allocated the struct and we know the kernel will read
// exactly the size of the struct.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_ONE_REG(), &mut onereg) };
if ret < 0 {
return Err(errno::Error::last());
}
Ok(reg_size)
}
/// Notify the guest about the vCPU being paused.
///
/// See the documentation for `KVM_KVMCLOCK_CTRL` in the
/// [KVM API documentation](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
#[cfg(target_arch = "x86_64")]
pub fn kvmclock_ctrl(&self) -> Result<()> {
// SAFETY: Safe because we know that our file is a KVM fd and that the request
// is one of the ones defined by kernel.
let ret = unsafe { ioctl(self, KVM_KVMCLOCK_CTRL()) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(())
}
/// Triggers the running of the current virtual CPU returning an exit reason.
///
/// See documentation for `KVM_RUN`.
///
/// # Example
///
/// Running some dummy code on x86_64 that immediately halts the vCPU. Based on
/// [https://lwn.net/Articles/658511/](https://lwn.net/Articles/658511/).
///
/// ```rust
/// # use std::io::Write;
/// # use std::ptr::null_mut;
/// # use std::slice;
/// # use kvm_ioctls::{Kvm, VcpuExit};
/// # use kvm_bindings::{kvm_userspace_memory_region, KVM_MEM_LOG_DIRTY_PAGES};
/// # let kvm = Kvm::new().unwrap();
/// # let vm = kvm.create_vm().unwrap();
///
/// # #[cfg(target_arch = "x86_64")]
/// # {
/// let mem_size = 0x4000;
/// let guest_addr: u64 = 0x1000;
/// let load_addr: *mut u8 = unsafe {
/// libc::mmap(
/// null_mut(),
/// mem_size,
/// libc::PROT_READ | libc::PROT_WRITE,
/// libc::MAP_ANONYMOUS | libc::MAP_SHARED | libc::MAP_NORESERVE,
/// -1,
/// 0,
/// ) as *mut u8
/// };
///
/// let mem_region = kvm_userspace_memory_region {
/// slot: 0,
/// guest_phys_addr: guest_addr,
/// memory_size: mem_size as u64,
/// userspace_addr: load_addr as u64,
/// flags: 0,
/// };
/// unsafe { vm.set_user_memory_region(mem_region).unwrap() };
///
/// // Dummy x86 code that just calls halt.
/// let x86_code = [0xf4 /* hlt */];
///
/// // Write the code in the guest memory. This will generate a dirty page.
/// unsafe {
/// let mut slice = slice::from_raw_parts_mut(load_addr, mem_size);
/// slice.write(&x86_code).unwrap();
/// }
///
/// let mut vcpu_fd = vm.create_vcpu(0).unwrap();
///
/// let mut vcpu_sregs = vcpu_fd.get_sregs().unwrap();
/// vcpu_sregs.cs.base = 0;
/// vcpu_sregs.cs.selector = 0;
/// vcpu_fd.set_sregs(&vcpu_sregs).unwrap();
///
/// let mut vcpu_regs = vcpu_fd.get_regs().unwrap();
/// // Set the Instruction Pointer to the guest address where we loaded the code.
/// vcpu_regs.rip = guest_addr;
/// vcpu_regs.rax = 2;
/// vcpu_regs.rbx = 3;
/// vcpu_regs.rflags = 2;
/// vcpu_fd.set_regs(&vcpu_regs).unwrap();
///
/// loop {
/// match vcpu_fd.run().expect("run failed") {
/// VcpuExit::Hlt => {
/// break;
/// }
/// exit_reason => panic!("unexpected exit reason: {:?}", exit_reason),
/// }
/// }
/// # }
/// ```
pub fn run(&mut self) -> Result<VcpuExit<'_>> {
// SAFETY: Safe because we know that our file is a vCPU fd and we verify the return result.
let ret = unsafe { ioctl(self, KVM_RUN()) };
if ret == 0 {
let run = self.kvm_run_ptr.as_mut_ref();
match run.exit_reason {
// make sure you treat all possible exit reasons from include/uapi/linux/kvm.h corresponding
// when upgrading to a different kernel version
KVM_EXIT_UNKNOWN => Ok(VcpuExit::Unknown),
KVM_EXIT_EXCEPTION => Ok(VcpuExit::Exception),
KVM_EXIT_IO => {
let run_start = run as *mut kvm_run as *mut u8;
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let io = unsafe { run.__bindgen_anon_1.io };
let port = io.port;
let data_size = io.count as usize * io.size as usize;
// SAFETY: The data_offset is defined by the kernel to be some number of bytes
// into the kvm_run stucture, which we have fully mmap'd.
let data_ptr = unsafe { run_start.offset(io.data_offset as isize) };
let data_slice =
// SAFETY: The slice's lifetime is limited to the lifetime of this vCPU, which is equal
// to the mmap of the `kvm_run` struct that this is slicing from.
unsafe { std::slice::from_raw_parts_mut::<u8>(data_ptr, data_size) };
match u32::from(io.direction) {
KVM_EXIT_IO_IN => Ok(VcpuExit::IoIn(port, data_slice)),
KVM_EXIT_IO_OUT => Ok(VcpuExit::IoOut(port, data_slice)),
_ => Err(errno::Error::new(EINVAL)),
}
}
KVM_EXIT_HYPERCALL => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let hypercall = unsafe { &mut run.__bindgen_anon_1.hypercall };
Ok(VcpuExit::Hypercall(HypercallExit {
nr: hypercall.nr,
args: hypercall.args,
ret: &mut hypercall.ret,
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
longmode: unsafe { hypercall.__bindgen_anon_1.longmode },
}))
}
KVM_EXIT_DEBUG => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let debug = unsafe { run.__bindgen_anon_1.debug };
Ok(VcpuExit::Debug(debug.arch))
}
KVM_EXIT_HLT => Ok(VcpuExit::Hlt),
KVM_EXIT_MMIO => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let mmio = unsafe { &mut run.__bindgen_anon_1.mmio };
let addr = mmio.phys_addr;
let len = mmio.len as usize;
let data_slice = &mut mmio.data[..len];
if mmio.is_write != 0 {
Ok(VcpuExit::MmioWrite(addr, data_slice))
} else {
Ok(VcpuExit::MmioRead(addr, data_slice))
}
}
KVM_EXIT_X86_RDMSR => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let msr = unsafe { &mut run.__bindgen_anon_1.msr };
let exit = ReadMsrExit {
error: &mut msr.error,
reason: MsrExitReason::from_bits_truncate(msr.reason),
index: msr.index,
data: &mut msr.data,
};
Ok(VcpuExit::X86Rdmsr(exit))
}
KVM_EXIT_X86_WRMSR => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let msr = unsafe { &mut run.__bindgen_anon_1.msr };
let exit = WriteMsrExit {
error: &mut msr.error,
reason: MsrExitReason::from_bits_truncate(msr.reason),
index: msr.index,
data: msr.data,
};
Ok(VcpuExit::X86Wrmsr(exit))
}
KVM_EXIT_IRQ_WINDOW_OPEN => Ok(VcpuExit::IrqWindowOpen),
KVM_EXIT_SHUTDOWN => Ok(VcpuExit::Shutdown),
KVM_EXIT_FAIL_ENTRY => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let fail_entry = unsafe { &mut run.__bindgen_anon_1.fail_entry };
Ok(VcpuExit::FailEntry(
fail_entry.hardware_entry_failure_reason,
fail_entry.cpu,
))
}
KVM_EXIT_INTR => Ok(VcpuExit::Intr),
KVM_EXIT_SET_TPR => Ok(VcpuExit::SetTpr),
KVM_EXIT_TPR_ACCESS => Ok(VcpuExit::TprAccess),
KVM_EXIT_S390_SIEIC => Ok(VcpuExit::S390Sieic),
KVM_EXIT_S390_RESET => Ok(VcpuExit::S390Reset),
KVM_EXIT_DCR => Ok(VcpuExit::Dcr),
KVM_EXIT_NMI => Ok(VcpuExit::Nmi),
KVM_EXIT_INTERNAL_ERROR => Ok(VcpuExit::InternalError),
KVM_EXIT_OSI => Ok(VcpuExit::Osi),
KVM_EXIT_PAPR_HCALL => Ok(VcpuExit::PaprHcall),
KVM_EXIT_S390_UCONTROL => Ok(VcpuExit::S390Ucontrol),
KVM_EXIT_WATCHDOG => Ok(VcpuExit::Watchdog),
KVM_EXIT_S390_TSCH => Ok(VcpuExit::S390Tsch),
KVM_EXIT_EPR => Ok(VcpuExit::Epr),
KVM_EXIT_SYSTEM_EVENT => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let system_event = unsafe { &mut run.__bindgen_anon_1.system_event };
let ndata = system_event.ndata;
// SAFETY: Safe because we only populate with valid data (based on ndata)
let data = unsafe { &system_event.__bindgen_anon_1.data[0..ndata as usize] };
Ok(VcpuExit::SystemEvent(system_event.type_, data))
}
KVM_EXIT_S390_STSI => Ok(VcpuExit::S390Stsi),
KVM_EXIT_IOAPIC_EOI => {
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let eoi = unsafe { &mut run.__bindgen_anon_1.eoi };
Ok(VcpuExit::IoapicEoi(eoi.vector))
}
KVM_EXIT_HYPERV => Ok(VcpuExit::Hyperv),
r => Ok(VcpuExit::Unsupported(r)),
}
} else {
let errno = errno::Error::last();
let run = self.kvm_run_ptr.as_mut_ref();
// From https://docs.kernel.org/virt/kvm/api.html#kvm-run :
//
// KVM_EXIT_MEMORY_FAULT is unique among all KVM exit reasons in that it accompanies
// a return code of ‘-1’, not ‘0’! errno will always be set to EFAULT or EHWPOISON
// when KVM exits with KVM_EXIT_MEMORY_FAULT, userspace should assume kvm_run.exit_reason
// is stale/undefined for all other error numbers.
if ret == -1
&& (errno == errno::Error::new(libc::EFAULT)
|| errno == errno::Error::new(libc::EHWPOISON))
&& run.exit_reason == KVM_EXIT_MEMORY_FAULT
{
// SAFETY: Safe because the exit_reason (which comes from the kernel) told us
// which union field to use.
let fault = unsafe { &mut run.__bindgen_anon_1.memory_fault };
Ok(VcpuExit::MemoryFault {
flags: fault.flags,
gpa: fault.gpa,
size: fault.size,
})
} else {
Err(errno)
}
}
}
/// Returns a mutable reference to the kvm_run structure
pub fn get_kvm_run(&mut self) -> &mut kvm_run {
self.kvm_run_ptr.as_mut_ref()
}
/// Sets the `immediate_exit` flag on the `kvm_run` struct associated with this vCPU to `val`.
pub fn set_kvm_immediate_exit(&mut self, val: u8) {
let kvm_run = self.kvm_run_ptr.as_mut_ref();
kvm_run.immediate_exit = val;
}
/// Returns the vCPU TSC frequency in KHz or an error if the host has unstable TSC.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let tsc_khz = vcpu.get_tsc_khz().unwrap();
/// ```
///
#[cfg(target_arch = "x86_64")]
pub fn get_tsc_khz(&self) -> Result<u32> {
// SAFETY: Safe because we know that our file is a KVM fd and that the request is one of
// the ones defined by kernel.
let ret = unsafe { ioctl(self, KVM_GET_TSC_KHZ()) };
if ret >= 0 {
Ok(ret as u32)
} else {
Err(errno::Error::new(ret))
}
}
/// Sets the specified vCPU TSC frequency.
///
/// # Arguments
///
/// * `freq` - The frequency unit is KHz as per the KVM API documentation
/// for `KVM_SET_TSC_KHZ`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Cap, Kvm};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::GetTscKhz) && kvm.check_extension(Cap::TscControl) {
/// vcpu.set_tsc_khz(1000).unwrap();
/// }
/// ```
///
#[cfg(target_arch = "x86_64")]
pub fn set_tsc_khz(&self, freq: u32) -> Result<()> {
// SAFETY: Safe because we know that our file is a KVM fd and that the request is one of
// the ones defined by kernel.
let ret = unsafe { ioctl_with_val(self, KVM_SET_TSC_KHZ(), freq as u64) };
if ret < 0 {
Err(errno::Error::last())
} else {
Ok(())
}
}
/// Translates a virtual address according to the vCPU's current address translation mode.
///
/// The physical address is returned in a `kvm_translation` structure as defined in the
/// [KVM API documentation](https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt).
/// See documentation for `KVM_TRANSLATE`.
///
/// # Arguments
///
/// * `gva` - The virtual address to translate.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// #[cfg(target_arch = "x86_64")]
/// let tr = vcpu.translate_gva(0x10000).unwrap();
/// ```
#[cfg(target_arch = "x86_64")]
pub fn translate_gva(&self, gva: u64) -> Result<kvm_translation> {
let mut tr = kvm_translation {
linear_address: gva,
..Default::default()
};
// SAFETY: Safe because we know that our file is a vCPU fd, we know the kernel will only
// write the correct amount of memory to our pointer, and we verify the return result.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_TRANSLATE(), &mut tr) };
if ret != 0 {
return Err(errno::Error::last());
}
Ok(tr)
}
/// Enable the given [`SyncReg`] to be copied to userspace on the next exit
///
/// # Arguments
///
/// * `reg` - The [`SyncReg`] to copy out of the guest
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// vcpu.set_sync_valid_reg(SyncReg::Register);
/// vcpu.set_sync_valid_reg(SyncReg::SystemRegister);
/// vcpu.set_sync_valid_reg(SyncReg::VcpuEvents);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_sync_valid_reg(&mut self, reg: SyncReg) {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
kvm_run.kvm_valid_regs |= reg as u64;
}
/// Tell KVM to copy the given [`SyncReg`] into the guest on the next entry
///
/// # Arguments
///
/// * `reg` - The [`SyncReg`] to copy into the guest
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// vcpu.set_sync_dirty_reg(SyncReg::Register);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_sync_dirty_reg(&mut self, reg: SyncReg) {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
kvm_run.kvm_dirty_regs |= reg as u64;
}
/// Disable the given [`SyncReg`] to be copied to userspace on the next exit
///
/// # Arguments
///
/// * `reg` - The [`SyncReg`] to not copy out of the guest
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// vcpu.clear_sync_valid_reg(SyncReg::Register);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn clear_sync_valid_reg(&mut self, reg: SyncReg) {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
kvm_run.kvm_valid_regs &= !(reg as u64);
}
/// Tell KVM to not copy the given [`SyncReg`] into the guest on the next entry
///
/// # Arguments
///
/// * `reg` - The [`SyncReg`] to not copy out into the guest
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// vcpu.clear_sync_dirty_reg(SyncReg::Register);
/// ```
#[cfg(target_arch = "x86_64")]
pub fn clear_sync_dirty_reg(&mut self, reg: SyncReg) {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
kvm_run.kvm_dirty_regs &= !(reg as u64);
}
/// Get the [`kvm_sync_regs`] from the VM
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::SyncRegs) {
/// vcpu.set_sync_valid_reg(SyncReg::Register);
/// vcpu.run();
/// let guest_rax = vcpu.sync_regs().regs.rax;
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn sync_regs(&self) -> kvm_sync_regs {
let kvm_run = self.kvm_run_ptr.as_ref();
// SAFETY: Accessing this union field could be out of bounds if the `kvm_run`
// allocation isn't large enough. The `kvm_run` region is set using
// `get_vcpu_map_size`, so this region is in bounds
unsafe { kvm_run.s.regs }
}
/// Get a mutable reference to the [`kvm_sync_regs`] from the VM
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, SyncReg, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::SyncRegs) {
/// vcpu.set_sync_valid_reg(SyncReg::Register);
/// vcpu.run();
/// // Set the guest RAX to 0xdeadbeef
/// vcpu.sync_regs_mut().regs.rax = 0xdeadbeef;
/// vcpu.set_sync_dirty_reg(SyncReg::Register);
/// vcpu.run();
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn sync_regs_mut(&mut self) -> &mut kvm_sync_regs {
let kvm_run: &mut kvm_run = self.kvm_run_ptr.as_mut_ref();
// SAFETY: Accessing this union field could be out of bounds if the `kvm_run`
// allocation isn't large enough. The `kvm_run` region is set using
// `get_vcpu_map_size`, so this region is in bounds
unsafe { &mut kvm_run.s.regs }
}
/// Triggers an SMI on the virtual CPU.
///
/// See documentation for `KVM_SMI`.
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::X86Smm) {
/// vcpu.smi().unwrap();
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn smi(&self) -> Result<()> {
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl(self, KVM_SMI()) };
match ret {
0 => Ok(()),
_ => Err(errno::Error::last()),
}
}
/// Returns the nested guest state using the `KVM_GET_NESTED_STATE` ioctl.
///
/// This only works when `KVM_CAP_NESTED_STATE` is available.
///
/// # Arguments
///
/// - `buffer`: The buffer to be filled with the new nested state.
///
/// # Return Value
/// If this returns `None`, KVM doesn't have nested state. Otherwise, the
/// actual length of the state is returned.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap, KvmNestedStateBuffer};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// let mut state_buffer = KvmNestedStateBuffer::empty();
/// if kvm.check_extension(Cap::NestedState) {
/// vcpu.nested_state(&mut state_buffer).unwrap();
/// // Next, serialize the actual state into a file or so.
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn nested_state(
&self,
buffer: &mut KvmNestedStateBuffer,
) -> Result<Option<NonZeroUsize /* actual length of state */>> {
assert_ne!(buffer.size, 0, "buffer should not report a size of zero");
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl_with_mut_ref(self, KVM_GET_NESTED_STATE(), buffer) };
match ret {
0 => {
let size = buffer.size as usize;
let just_hdr_size = size_of::<kvm_nested_state>();
if size <= just_hdr_size {
Ok(None)
} else {
Ok(Some(NonZeroUsize::new(size).unwrap()))
}
}
_ => Err(errno::Error::last()),
}
}
/// Sets the nested guest state using the `KVM_SET_NESTED_STATE` ioctl.
///
/// This only works when `KVM_CAP_NESTED_STATE` is available.
///
/// # Arguments
///
/// - `state`: The new state to be put into KVM. The header must report the
/// `size` of the state properly. The state must be retrieved first using
/// [`Self::nested_state`].
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap, KvmNestedStateBuffer};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::NestedState) {
/// let mut state_buffer = KvmNestedStateBuffer::empty();
/// vcpu.nested_state(&mut state_buffer).unwrap();
/// // Rename the variable to better reflect the role.
/// let old_state = state_buffer;
///
/// // now assume we transfer the state to a new location
/// // and load it back into kvm:
/// vcpu.set_nested_state(&old_state).unwrap();
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn set_nested_state(&self, state: &KvmNestedStateBuffer) -> Result<()> {
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl_with_ref(self, KVM_SET_NESTED_STATE(), state) };
match ret {
0 => Ok(()),
_ => Err(errno::Error::last()),
}
}
/// Queues an NMI on the thread's vcpu. Only usable if `KVM_CAP_USER_NMI`
/// is available.
///
/// See the documentation for `KVM_NMI`.
///
/// # Example
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::UserNmi) {
/// vcpu.nmi().unwrap();
/// }
/// ```
#[cfg(target_arch = "x86_64")]
pub fn nmi(&self) -> Result<()> {
// SAFETY: Safe because we call this with a Vcpu fd and we trust the kernel.
let ret = unsafe { ioctl(self, KVM_NMI()) };
match ret {
0 => Ok(()),
_ => Err(errno::Error::last()),
}
}
/// Maps the coalesced MMIO ring page. This allows reading entries from
/// the ring via [`coalesced_mmio_read()`](VcpuFd::coalesced_mmio_read).
///
/// # Returns
///
/// Returns an error if the buffer could not be mapped, usually because
/// `KVM_CAP_COALESCED_MMIO` ([`Cap::CoalescedMmio`](crate::Cap::CoalescedMmio))
/// is not available.
///
/// # Examples
///
/// ```rust
/// # use kvm_ioctls::{Kvm, Cap};
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// let mut vcpu = vm.create_vcpu(0).unwrap();
/// if kvm.check_extension(Cap::CoalescedMmio) {
/// vcpu.map_coalesced_mmio_ring().unwrap();
/// }
/// ```
pub fn map_coalesced_mmio_ring(&mut self) -> Result<()> {
if self.coalesced_mmio_ring.is_none() {
let ring = KvmCoalescedIoRing::mmap_from_fd(&self.vcpu)?;
self.coalesced_mmio_ring = Some(ring);
}
Ok(())
}
/// Read a single entry from the coalesced MMIO ring.
/// For entries to be appended to the ring by the kernel, addresses must be registered
/// via [`VmFd::register_coalesced_mmio()`](crate::VmFd::register_coalesced_mmio()).
///
/// [`map_coalesced_mmio_ring()`](VcpuFd::map_coalesced_mmio_ring) must have been called beforehand.
///
/// See the documentation for `KVM_(UN)REGISTER_COALESCED_MMIO`.
///
/// # Returns
///
/// * An error if [`map_coalesced_mmio_ring()`](VcpuFd::map_coalesced_mmio_ring)
/// was not called beforehand.
/// * [`Ok<None>`] if the ring is empty.
/// * [`Ok<Some<kvm_coalesced_mmio>>`] if an entry was successfully read.
pub fn coalesced_mmio_read(&mut self) -> Result<Option<kvm_coalesced_mmio>> {
self.coalesced_mmio_ring
.as_mut()
.ok_or(errno::Error::new(libc::EIO))
.map(|ring| ring.read_entry())
}
}
/// Helper function to create a new `VcpuFd`.
///
/// This should not be exported as a public function because the preferred way is to use
/// `create_vcpu` from `VmFd`. The function cannot be part of the `VcpuFd` implementation because
/// then it would be exported with the public `VcpuFd` interface.
pub fn new_vcpu(vcpu: File, kvm_run_ptr: KvmRunWrapper) -> VcpuFd {
VcpuFd {
vcpu,
kvm_run_ptr,
coalesced_mmio_ring: None,
}
}
impl AsRawFd for VcpuFd {
fn as_raw_fd(&self) -> RawFd {
self.vcpu.as_raw_fd()
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::undocumented_unsafe_blocks)]
use super::*;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
use crate::cap::Cap;
use crate::ioctls::system::Kvm;
use std::ptr::NonNull;
// Helper function for memory mapping `size` bytes of anonymous memory.
// Panics if the mmap fails.
fn mmap_anonymous(size: usize) -> NonNull<u8> {
use std::ptr::null_mut;
let addr = unsafe {
libc::mmap(
null_mut(),
size,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_ANONYMOUS | libc::MAP_SHARED | libc::MAP_NORESERVE,
-1,
0,
)
};
if addr == libc::MAP_FAILED {
panic!("mmap failed.");
}
NonNull::new(addr).unwrap().cast()
}
#[test]
fn test_create_vcpu() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
vm.create_vcpu(0).unwrap();
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_get_cpuid() {
let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::ExtCpuid) {
let vm = kvm.create_vm().unwrap();
let cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
let ncpuids = cpuid.as_slice().len();
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
let nr_vcpus = kvm.get_nr_vcpus();
for cpu_idx in 0..nr_vcpus {
let vcpu = vm.create_vcpu(cpu_idx as u64).unwrap();
vcpu.set_cpuid2(&cpuid).unwrap();
let retrieved_cpuid = vcpu.get_cpuid2(ncpuids).unwrap();
// Only check the first few leafs as some (e.g. 13) are reserved.
assert_eq!(cpuid.as_slice()[..3], retrieved_cpuid.as_slice()[..3]);
}
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_get_cpuid_fail_num_entries_too_high() {
let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::ExtCpuid) {
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let err_cpuid = vcpu.get_cpuid2(KVM_MAX_CPUID_ENTRIES + 1_usize).err();
assert_eq!(err_cpuid.unwrap().errno(), libc::ENOMEM);
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_get_cpuid_fail_num_entries_too_small() {
let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::ExtCpuid) {
let vm = kvm.create_vm().unwrap();
let cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
let ncpuids = cpuid.as_slice().len();
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
let nr_vcpus = kvm.get_nr_vcpus();
for cpu_idx in 0..nr_vcpus {
let vcpu = vm.create_vcpu(cpu_idx as u64).unwrap();
vcpu.set_cpuid2(&cpuid).unwrap();
let err = vcpu.get_cpuid2(ncpuids - 1_usize).err();
assert_eq!(err.unwrap().errno(), libc::E2BIG);
}
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_set_cpuid() {
let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::ExtCpuid) {
let vm = kvm.create_vm().unwrap();
let mut cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
let ncpuids = cpuid.as_slice().len();
assert!(ncpuids <= KVM_MAX_CPUID_ENTRIES);
let vcpu = vm.create_vcpu(0).unwrap();
// Setting Manufacturer ID
{
let entries = cpuid.as_mut_slice();
for entry in entries.iter_mut() {
if entry.function == 0 {
// " KVMKVMKVM "
entry.ebx = 0x4b4d564b;
entry.ecx = 0x564b4d56;
entry.edx = 0x4d;
}
}
}
vcpu.set_cpuid2(&cpuid).unwrap();
let cpuid_0 = vcpu.get_cpuid2(ncpuids).unwrap();
for entry in cpuid_0.as_slice() {
if entry.function == 0 {
assert_eq!(entry.ebx, 0x4b4d564b);
assert_eq!(entry.ecx, 0x564b4d56);
assert_eq!(entry.edx, 0x4d);
}
}
// Disabling Intel SHA extensions.
const EBX_SHA_SHIFT: u32 = 29;
let mut ebx_sha_off = 0u32;
{
let entries = cpuid.as_mut_slice();
for entry in entries.iter_mut() {
if entry.function == 7 && entry.ecx == 0 {
entry.ebx &= !(1 << EBX_SHA_SHIFT);
ebx_sha_off = entry.ebx;
}
}
}
vcpu.set_cpuid2(&cpuid).unwrap();
let cpuid_1 = vcpu.get_cpuid2(ncpuids).unwrap();
for entry in cpuid_1.as_slice() {
if entry.function == 7 && entry.ecx == 0 {
assert_eq!(entry.ebx, ebx_sha_off);
}
}
}
}
#[cfg(target_arch = "x86_64")]
#[allow(non_snake_case)]
#[test]
fn test_fpu() {
// as per https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/fpu/internal.h
let KVM_FPU_CWD: usize = 0x37f;
let KVM_FPU_MXCSR: usize = 0x1f80;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut fpu: kvm_fpu = kvm_fpu {
fcw: KVM_FPU_CWD as u16,
mxcsr: KVM_FPU_MXCSR as u32,
..Default::default()
};
fpu.fcw = KVM_FPU_CWD as u16;
fpu.mxcsr = KVM_FPU_MXCSR as u32;
vcpu.set_fpu(&fpu).unwrap();
assert_eq!(vcpu.get_fpu().unwrap().fcw, KVM_FPU_CWD as u16);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn lapic_test() {
use std::io::Cursor;
// We might get read of byteorder if we replace mem::transmute with something safer.
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
// As per https://github.com/torvalds/linux/arch/x86/kvm/lapic.c
// Try to write and read the APIC_ICR (0x300) register which is non-read only and
// one can simply write to it.
let kvm = Kvm::new().unwrap();
assert!(kvm.check_extension(Cap::Irqchip));
let vm = kvm.create_vm().unwrap();
// The get_lapic ioctl will fail if there is no irqchip created beforehand.
vm.create_irq_chip().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut klapic: kvm_lapic_state = vcpu.get_lapic().unwrap();
let reg_offset = 0x300;
let value = 2_u32;
//try to write and read the APIC_ICR 0x300
let write_slice =
unsafe { &mut *(&mut klapic.regs[reg_offset..] as *mut [i8] as *mut [u8]) };
let mut writer = Cursor::new(write_slice);
writer.write_u32::<LittleEndian>(value).unwrap();
vcpu.set_lapic(&klapic).unwrap();
klapic = vcpu.get_lapic().unwrap();
let read_slice = unsafe { &*(&klapic.regs[reg_offset..] as *const [i8] as *const [u8]) };
let mut reader = Cursor::new(read_slice);
assert_eq!(reader.read_u32::<LittleEndian>().unwrap(), value);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn msrs_test() {
use vmm_sys_util::fam::FamStruct;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
// Set the following MSRs.
let msrs_to_set = [
kvm_msr_entry {
index: 0x0000_0174,
data: 0x0,
..Default::default()
},
kvm_msr_entry {
index: 0x0000_0175,
data: 0x1,
..Default::default()
},
];
let msrs_wrapper = Msrs::from_entries(&msrs_to_set).unwrap();
vcpu.set_msrs(&msrs_wrapper).unwrap();
// Now test that GET_MSRS returns the same.
// Configure the struct to say which entries we want.
let mut returned_kvm_msrs = Msrs::from_entries(&[
kvm_msr_entry {
index: 0x0000_0174,
..Default::default()
},
kvm_msr_entry {
index: 0x0000_0175,
..Default::default()
},
])
.unwrap();
let nmsrs = vcpu.get_msrs(&mut returned_kvm_msrs).unwrap();
// Verify the lengths match.
assert_eq!(nmsrs, msrs_to_set.len());
assert_eq!(nmsrs, returned_kvm_msrs.as_fam_struct_ref().len());
// Verify the contents match.
let returned_kvm_msr_entries = returned_kvm_msrs.as_slice();
for (i, entry) in returned_kvm_msr_entries.iter().enumerate() {
assert_eq!(entry, &msrs_to_set[i]);
}
}
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x"
))]
#[test]
fn mpstate_test() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mp_state = vcpu.get_mp_state().unwrap();
vcpu.set_mp_state(mp_state).unwrap();
let other_mp_state = vcpu.get_mp_state().unwrap();
assert_eq!(mp_state, other_mp_state);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn xsave_test() {
use vmm_sys_util::fam::FamStruct;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xsave = vcpu.get_xsave().unwrap();
// SAFETY: Safe because no features are enabled dynamically and `xsave` is large enough.
unsafe { vcpu.set_xsave(&xsave).unwrap() };
let other_xsave = vcpu.get_xsave().unwrap();
assert_eq!(&xsave.region[..], &other_xsave.region[..]);
let xsave_size = vm.check_extension_int(Cap::Xsave2);
// only if KVM_CAP_XSAVE2 is supported
if xsave_size > 0 {
let fam_size = (xsave_size as usize - std::mem::size_of::<kvm_xsave>())
.div_ceil(std::mem::size_of::<<kvm_xsave2 as FamStruct>::Entry>());
let mut xsave2 = Xsave::new(fam_size).unwrap();
// SAFETY: Safe because `xsave2` is allocated with enough space.
unsafe { vcpu.get_xsave2(&mut xsave2).unwrap() };
assert_eq!(
&xsave.region[..],
&xsave2.as_fam_struct_ref().xsave.region[..]
);
// SAFETY: Safe because `xsave2` is allocated with enough space.
unsafe { vcpu.set_xsave2(&xsave2).unwrap() };
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn xcrs_test() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let xcrs = vcpu.get_xcrs().unwrap();
vcpu.set_xcrs(&xcrs).unwrap();
let other_xcrs = vcpu.get_xcrs().unwrap();
assert_eq!(xcrs, other_xcrs);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn debugregs_test() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let debugregs = vcpu.get_debug_regs().unwrap();
vcpu.set_debug_regs(&debugregs).unwrap();
let other_debugregs = vcpu.get_debug_regs().unwrap();
assert_eq!(debugregs, other_debugregs);
}
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
#[test]
fn vcpu_events_test() {
let kvm = Kvm::new().unwrap();
if kvm.check_extension(Cap::VcpuEvents) {
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
#[cfg(target_arch = "aarch64")]
{
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi).unwrap();
vcpu.vcpu_init(&kvi).unwrap();
}
let vcpu_events = vcpu.get_vcpu_events().unwrap();
vcpu.set_vcpu_events(&vcpu_events).unwrap();
let other_vcpu_events = vcpu.get_vcpu_events().unwrap();
assert_eq!(vcpu_events, other_vcpu_events);
}
}
#[cfg(target_arch = "aarch64")]
#[test]
fn test_run_code() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
#[rustfmt::skip]
let code = [
0x40, 0x20, 0x80, 0x52, /* mov w0, #0x102 */
0x00, 0x01, 0x00, 0xb9, /* str w0, [x8]; test physical memory write */
0x81, 0x60, 0x80, 0x52, /* mov w1, #0x304 */
0x02, 0x00, 0x80, 0x52, /* mov w2, #0x0 */
0x20, 0x01, 0x40, 0xb9, /* ldr w0, [x9]; test MMIO read */
0x1f, 0x18, 0x14, 0x71, /* cmp w0, #0x506 */
0x20, 0x00, 0x82, 0x1a, /* csel w0, w1, w2, eq */
0x20, 0x01, 0x00, 0xb9, /* str w0, [x9]; test MMIO write */
0x00, 0x80, 0xb0, 0x52, /* mov w0, #0x84000000 */
0x00, 0x00, 0x1d, 0x32, /* orr w0, w0, #0x08 */
0x02, 0x00, 0x00, 0xd4, /* hvc #0x0 */
0x00, 0x00, 0x00, 0x14, /* b <this address>; shouldn't get here, but if so loop forever */
];
let mem_size = 0x20000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x10000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: KVM_MEM_LOG_DIRTY_PAGES,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
}
unsafe {
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu_fd = vm.create_vcpu(0).unwrap();
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi).unwrap();
kvi.features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
vcpu_fd.vcpu_init(&kvi).unwrap();
let core_reg_base: u64 = 0x6030_0000_0010_0000;
let mmio_addr: u64 = guest_addr + mem_size as u64;
// Set the PC to the guest address where we loaded the code.
vcpu_fd
.set_one_reg(core_reg_base + 2 * 32, &(guest_addr as u128).to_le_bytes())
.unwrap();
// Set x8 and x9 to the addresses the guest test code needs
vcpu_fd
.set_one_reg(
core_reg_base + 2 * 8,
&(guest_addr as u128 + 0x10000).to_le_bytes(),
)
.unwrap();
vcpu_fd
.set_one_reg(core_reg_base + 2 * 9, &(mmio_addr as u128).to_le_bytes())
.unwrap();
loop {
match vcpu_fd.run().expect("run failed") {
VcpuExit::MmioRead(addr, data) => {
assert_eq!(addr, mmio_addr);
assert_eq!(data.len(), 4);
data[3] = 0x0;
data[2] = 0x0;
data[1] = 0x5;
data[0] = 0x6;
}
VcpuExit::MmioWrite(addr, data) => {
assert_eq!(addr, mmio_addr);
assert_eq!(data.len(), 4);
assert_eq!(data[3], 0x0);
assert_eq!(data[2], 0x0);
assert_eq!(data[1], 0x3);
assert_eq!(data[0], 0x4);
// The code snippet dirties one page at guest_addr + 0x10000.
// The code page should not be dirty, as it's not written by the guest.
let dirty_pages_bitmap = vm.get_dirty_log(slot, mem_size).unwrap();
let dirty_pages: u32 = dirty_pages_bitmap
.into_iter()
.map(|page| page.count_ones())
.sum();
assert_eq!(dirty_pages, 1);
}
VcpuExit::SystemEvent(type_, data) => {
assert_eq!(type_, KVM_SYSTEM_EVENT_SHUTDOWN);
assert_eq!(data[0], 0);
break;
}
r => panic!("unexpected exit reason: {:?}", r),
}
}
}
#[cfg(target_arch = "riscv64")]
#[test]
fn test_run_code() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
#[rustfmt::skip]
let code = [
0x13, 0x05, 0x50, 0x40, // li a0, 0x0405;
0x23, 0x20, 0xac, 0x00, // sw a0, 0(s8); test physical memory write
0x03, 0xa5, 0x0c, 0x00, // lw a0, 0(s9); test MMIO read
0x93, 0x05, 0x70, 0x60, // li a1, 0x0607;
0x23, 0xa0, 0xbc, 0x00, // sw a1, 0(s9); test MMIO write
0x6f, 0x00, 0x00, 0x00, // j .; shouldn't get here, but if so loop forever
];
let mem_size = 0x20000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x10000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: KVM_MEM_LOG_DIRTY_PAGES,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
}
unsafe {
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu_fd = vm.create_vcpu(0).unwrap();
let core_reg_base: u64 = 0x8030_0000_0200_0000;
let mmio_addr: u64 = guest_addr + mem_size as u64;
// Set the PC to the guest address where we loaded the code.
vcpu_fd
.set_one_reg(core_reg_base, &(guest_addr as u128).to_le_bytes())
.unwrap();
// Set s8 and s9 to the addresses the guest test code needs
vcpu_fd
.set_one_reg(
core_reg_base + 24,
&(guest_addr as u128 + 0x10000).to_le_bytes(),
)
.unwrap();
vcpu_fd
.set_one_reg(core_reg_base + 25, &(mmio_addr as u128).to_le_bytes())
.unwrap();
loop {
match vcpu_fd.run().expect("run failed") {
VcpuExit::MmioRead(addr, data) => {
assert_eq!(addr, mmio_addr);
assert_eq!(data.len(), 4);
data[3] = 0x0;
data[2] = 0x0;
data[1] = 0x5;
data[0] = 0x6;
}
VcpuExit::MmioWrite(addr, data) => {
assert_eq!(addr, mmio_addr);
assert_eq!(data.len(), 4);
assert_eq!(data[3], 0x0);
assert_eq!(data[2], 0x0);
assert_eq!(data[1], 0x6);
assert_eq!(data[0], 0x7);
// The code snippet dirties one page at guest_addr + 0x10000.
// The code page should not be dirty, as it's not written by the guest.
let dirty_pages_bitmap = vm.get_dirty_log(slot, mem_size).unwrap();
let dirty_pages: u32 = dirty_pages_bitmap
.into_iter()
.map(|page| page.count_ones())
.sum();
assert_eq!(dirty_pages, 1);
break;
}
r => panic!("unexpected exit reason: {:?}", r),
}
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_run_code() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
// This example is based on https://lwn.net/Articles/658511/
#[rustfmt::skip]
let code = [
0xba, 0xf8, 0x03, /* mov $0x3f8, %dx */
0x00, 0xd8, /* add %bl, %al */
0x04, b'0', /* add $'0', %al */
0xee, /* out %al, %dx */
0xec, /* in %dx, %al */
0xc6, 0x06, 0x00, 0x80, 0x00, /* movl $0, (0x8000); This generates a MMIO Write.*/
0x8a, 0x16, 0x00, 0x80, /* movl (0x8000), %dl; This generates a MMIO Read.*/
0xc6, 0x06, 0x00, 0x20, 0x00, /* movl $0, (0x2000); Dirty one page in guest mem. */
0xf4, /* hlt */
];
let expected_rips: [u64; 3] = [0x1003, 0x1005, 0x1007];
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: KVM_MEM_LOG_DIRTY_PAGES,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
}
unsafe {
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu_fd = vm.create_vcpu(0).unwrap();
let mut vcpu_sregs = vcpu_fd.get_sregs().unwrap();
assert_ne!(vcpu_sregs.cs.base, 0);
assert_ne!(vcpu_sregs.cs.selector, 0);
vcpu_sregs.cs.base = 0;
vcpu_sregs.cs.selector = 0;
vcpu_fd.set_sregs(&vcpu_sregs).unwrap();
let mut vcpu_regs = vcpu_fd.get_regs().unwrap();
// Set the Instruction Pointer to the guest address where we loaded the code.
vcpu_regs.rip = guest_addr;
vcpu_regs.rax = 2;
vcpu_regs.rbx = 3;
vcpu_regs.rflags = 2;
vcpu_fd.set_regs(&vcpu_regs).unwrap();
let mut debug_struct = kvm_guest_debug {
control: KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
pad: 0,
arch: kvm_guest_debug_arch {
debugreg: [0, 0, 0, 0, 0, 0, 0, 0],
},
};
vcpu_fd.set_guest_debug(&debug_struct).unwrap();
let mut instr_idx = 0;
loop {
match vcpu_fd.run().expect("run failed") {
VcpuExit::IoIn(addr, data) => {
assert_eq!(addr, 0x3f8);
assert_eq!(data.len(), 1);
}
VcpuExit::IoOut(addr, data) => {
assert_eq!(addr, 0x3f8);
assert_eq!(data.len(), 1);
assert_eq!(data[0], b'5');
}
VcpuExit::MmioRead(addr, data) => {
assert_eq!(addr, 0x8000);
assert_eq!(data.len(), 1);
}
VcpuExit::MmioWrite(addr, data) => {
assert_eq!(addr, 0x8000);
assert_eq!(data.len(), 1);
assert_eq!(data[0], 0);
}
VcpuExit::Debug(debug) => {
if instr_idx == expected_rips.len() - 1 {
// Disabling debugging/single-stepping
debug_struct.control = 0;
vcpu_fd.set_guest_debug(&debug_struct).unwrap();
} else if instr_idx >= expected_rips.len() {
unreachable!();
}
let vcpu_regs = vcpu_fd.get_regs().unwrap();
assert_eq!(vcpu_regs.rip, expected_rips[instr_idx]);
assert_eq!(debug.exception, 1);
assert_eq!(debug.pc, expected_rips[instr_idx]);
// Check first 15 bits of DR6
let mask = (1 << 16) - 1;
assert_eq!(debug.dr6 & mask, 0b100111111110000);
// Bit 10 in DR7 is always 1
assert_eq!(debug.dr7, 1 << 10);
instr_idx += 1;
}
VcpuExit::Hlt => {
// The code snippet dirties 2 pages:
// * one when the code itself is loaded in memory;
// * and one more from the `movl` that writes to address 0x8000
let dirty_pages_bitmap = vm.get_dirty_log(slot, mem_size).unwrap();
let dirty_pages: u32 = dirty_pages_bitmap
.into_iter()
.map(|page| page.count_ones())
.sum();
assert_eq!(dirty_pages, 2);
break;
}
r => panic!("unexpected exit reason: {:?}", r),
}
}
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_get_preferred_target() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
vcpu.vcpu_init(&kvi).unwrap();
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_set_one_reg() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
vcpu.vcpu_init(&kvi).expect("Cannot initialize vcpu");
let data: u128 = 0;
let reg_id: u64 = 0;
vcpu.set_one_reg(reg_id, &data.to_le_bytes()).unwrap_err();
// Exercising KVM_SET_ONE_REG by trying to alter the data inside the PSTATE register (which is a
// specific aarch64 register).
// This regiseter is 64 bit wide (8 bytes).
const PSTATE_REG_ID: u64 = 0x6030_0000_0010_0042;
vcpu.set_one_reg(PSTATE_REG_ID, &data.to_le_bytes())
.expect("Failed to set pstate register");
// Trying to set 8 byte register with 7 bytes must fail.
vcpu.set_one_reg(PSTATE_REG_ID, &[0_u8; 7]).unwrap_err();
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_get_one_reg() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
vcpu.vcpu_init(&kvi).expect("Cannot initialize vcpu");
// PSR (Processor State Register) bits.
// Taken from arch/arm64/include/uapi/asm/ptrace.h.
const PSR_MODE_EL1H: u64 = 0x0000_0005;
const PSR_F_BIT: u64 = 0x0000_0040;
const PSR_I_BIT: u64 = 0x0000_0080;
const PSR_A_BIT: u64 = 0x0000_0100;
const PSR_D_BIT: u64 = 0x0000_0200;
const PSTATE_FAULT_BITS_64: u64 =
PSR_MODE_EL1H | PSR_A_BIT | PSR_F_BIT | PSR_I_BIT | PSR_D_BIT;
let data: u128 = PSTATE_FAULT_BITS_64 as u128;
const PSTATE_REG_ID: u64 = 0x6030_0000_0010_0042;
vcpu.set_one_reg(PSTATE_REG_ID, &data.to_le_bytes())
.expect("Failed to set pstate register");
let mut bytes = [0_u8; 16];
vcpu.get_one_reg(PSTATE_REG_ID, &mut bytes)
.expect("Failed to get pstate register");
let data = u128::from_le_bytes(bytes);
assert_eq!(data, PSTATE_FAULT_BITS_64 as u128);
// Trying to get 8 byte register with 7 bytes must fail.
vcpu.get_one_reg(PSTATE_REG_ID, &mut [0_u8; 7]).unwrap_err();
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_get_reg_list() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut reg_list = RegList::new(1).unwrap();
// KVM_GET_REG_LIST demands that the vcpus be initalized, so we expect this to fail.
let err = vcpu.get_reg_list(&mut reg_list).unwrap_err();
assert!(err.errno() == libc::ENOEXEC);
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
vcpu.vcpu_init(&kvi).expect("Cannot initialize vcpu");
// KVM_GET_REG_LIST offers us a number of registers for which we have
// not allocated memory, so the first time it fails.
let err = vcpu.get_reg_list(&mut reg_list).unwrap_err();
assert!(err.errno() == libc::E2BIG);
// SAFETY: This structure is a result from a specific vCPU ioctl
assert!(unsafe { reg_list.as_mut_fam_struct() }.n > 0);
// We make use of the number of registers returned to allocate memory and
// try one more time.
// SAFETY: This structure is a result from a specific vCPU ioctl
let mut reg_list =
RegList::new(unsafe { reg_list.as_mut_fam_struct() }.n as usize).unwrap();
vcpu.get_reg_list(&mut reg_list).unwrap()
}
#[test]
#[cfg(target_arch = "riscv64")]
fn test_set_one_reg() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let data: u128 = 0;
let reg_id: u64 = 0;
vcpu.set_one_reg(reg_id, &data.to_le_bytes()).unwrap_err();
// Exercising KVM_SET_ONE_REG by trying to alter the data inside the A0
// register.
// This regiseter is 64 bit wide (8 bytes).
const A0_REG_ID: u64 = 0x8030_0000_0200_000a;
vcpu.set_one_reg(A0_REG_ID, &data.to_le_bytes())
.expect("Failed to set a0 register");
// Trying to set 8 byte register with 7 bytes must fail.
vcpu.set_one_reg(A0_REG_ID, &[0_u8; 7]).unwrap_err();
}
#[test]
#[cfg(target_arch = "riscv64")]
fn test_get_one_reg() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
const PRESET: u64 = 0x7;
let data: u128 = PRESET as u128;
const A0_REG_ID: u64 = 0x8030_0000_0200_000a;
vcpu.set_one_reg(A0_REG_ID, &data.to_le_bytes())
.expect("Failed to set a0 register");
let mut bytes = [0_u8; 16];
vcpu.get_one_reg(A0_REG_ID, &mut bytes)
.expect("Failed to get a0 register");
let data = u128::from_le_bytes(bytes);
assert_eq!(data, PRESET as u128);
// Trying to get 8 byte register with 7 bytes must fail.
vcpu.get_one_reg(A0_REG_ID, &mut [0_u8; 7]).unwrap_err();
}
#[test]
#[cfg(target_arch = "riscv64")]
fn test_get_reg_list() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut reg_list = RegList::new(1).unwrap();
// KVM_GET_REG_LIST offers us a number of registers for which we have
// not allocated memory, so the first time it fails.
let err = vcpu.get_reg_list(&mut reg_list).unwrap_err();
assert!(err.errno() == libc::E2BIG);
// SAFETY: This structure is a result from a specific vCPU ioctl
assert!(unsafe { reg_list.as_mut_fam_struct() }.n > 0);
// We make use of the number of registers returned to allocate memory and
// try one more time.
// SAFETY: This structure is a result from a specific vCPU ioctl
let mut reg_list =
RegList::new(unsafe { reg_list.as_mut_fam_struct() }.n as usize).unwrap();
vcpu.get_reg_list(&mut reg_list).unwrap();
// Test get a register list contains 200 registers explicitly
let mut reg_list = RegList::new(200).unwrap();
vcpu.get_reg_list(&mut reg_list).unwrap();
}
#[test]
fn test_get_kvm_run() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut vcpu = vm.create_vcpu(0).unwrap();
vcpu.kvm_run_ptr.as_mut_ref().immediate_exit = 1;
assert_eq!(vcpu.get_kvm_run().immediate_exit, 1);
}
#[test]
fn test_set_kvm_immediate_exit() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut vcpu = vm.create_vcpu(0).unwrap();
assert_eq!(vcpu.kvm_run_ptr.as_ref().immediate_exit, 0);
vcpu.set_kvm_immediate_exit(1);
assert_eq!(vcpu.kvm_run_ptr.as_ref().immediate_exit, 1);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_enable_cap() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut cap = kvm_enable_cap {
// KVM_CAP_HYPERV_SYNIC needs KVM_CAP_SPLIT_IRQCHIP enabled
cap: KVM_CAP_SPLIT_IRQCHIP,
..Default::default()
};
cap.args[0] = 24;
vm.enable_cap(&cap).unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
if kvm.check_extension(Cap::HypervSynic) {
let cap = kvm_enable_cap {
cap: KVM_CAP_HYPERV_SYNIC,
..Default::default()
};
vcpu.enable_cap(&cap).unwrap();
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_get_tsc_khz() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
if !kvm.check_extension(Cap::GetTscKhz) {
vcpu.get_tsc_khz().unwrap_err();
} else {
assert!(vcpu.get_tsc_khz().unwrap() > 0);
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_set_tsc_khz() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let freq = vcpu.get_tsc_khz().unwrap();
if !(kvm.check_extension(Cap::GetTscKhz) && kvm.check_extension(Cap::TscControl)) {
vcpu.set_tsc_khz(0).unwrap_err();
} else {
vcpu.set_tsc_khz(freq - 500000).unwrap();
assert_eq!(vcpu.get_tsc_khz().unwrap(), freq - 500000);
vcpu.set_tsc_khz(freq + 500000).unwrap();
assert_eq!(vcpu.get_tsc_khz().unwrap(), freq + 500000);
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_sync_regs() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut vcpu = vm.create_vcpu(0).unwrap();
// Test setting each valid register
let sync_regs = [
SyncReg::Register,
SyncReg::SystemRegister,
SyncReg::VcpuEvents,
];
for reg in &sync_regs {
vcpu.set_sync_valid_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_valid_regs, *reg as u64);
vcpu.clear_sync_valid_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_valid_regs, 0);
}
// Test that multiple valid SyncRegs can be set at the same time
vcpu.set_sync_valid_reg(SyncReg::Register);
vcpu.set_sync_valid_reg(SyncReg::SystemRegister);
vcpu.set_sync_valid_reg(SyncReg::VcpuEvents);
assert_eq!(
vcpu.kvm_run_ptr.as_ref().kvm_valid_regs,
SyncReg::Register as u64 | SyncReg::SystemRegister as u64 | SyncReg::VcpuEvents as u64
);
// Test setting each dirty register
let sync_regs = [
SyncReg::Register,
SyncReg::SystemRegister,
SyncReg::VcpuEvents,
];
for reg in &sync_regs {
vcpu.set_sync_dirty_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs, *reg as u64);
vcpu.clear_sync_dirty_reg(*reg);
assert_eq!(vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs, 0);
}
// Test that multiple dirty SyncRegs can be set at the same time
vcpu.set_sync_dirty_reg(SyncReg::Register);
vcpu.set_sync_dirty_reg(SyncReg::SystemRegister);
vcpu.set_sync_dirty_reg(SyncReg::VcpuEvents);
assert_eq!(
vcpu.kvm_run_ptr.as_ref().kvm_dirty_regs,
SyncReg::Register as u64 | SyncReg::SystemRegister as u64 | SyncReg::VcpuEvents as u64
);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_sync_regs_with_run() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
if kvm.check_extension(Cap::SyncRegs) {
// This example is based on https://lwn.net/Articles/658511/
#[rustfmt::skip]
let code = [
0xff, 0xc0, /* inc eax */
0xf4, /* hlt */
];
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: KVM_MEM_LOG_DIRTY_PAGES,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
}
unsafe {
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu = vm.create_vcpu(0).unwrap();
let orig_sregs = vcpu.get_sregs().unwrap();
let sync_regs = vcpu.sync_regs_mut();
// Initialize the sregs in sync_regs to be the original sregs
sync_regs.sregs = orig_sregs;
sync_regs.sregs.cs.base = 0;
sync_regs.sregs.cs.selector = 0;
// Set up the guest to attempt to `inc rax`
sync_regs.regs.rip = guest_addr;
sync_regs.regs.rax = 0x8000;
sync_regs.regs.rflags = 2;
// Initialize the sync_reg flags
vcpu.set_sync_valid_reg(SyncReg::Register);
vcpu.set_sync_valid_reg(SyncReg::SystemRegister);
vcpu.set_sync_valid_reg(SyncReg::VcpuEvents);
vcpu.set_sync_dirty_reg(SyncReg::Register);
vcpu.set_sync_dirty_reg(SyncReg::SystemRegister);
vcpu.set_sync_dirty_reg(SyncReg::VcpuEvents);
// hlt is the only expected return from guest execution
assert!(matches!(vcpu.run().expect("run failed"), VcpuExit::Hlt));
let regs = vcpu.get_regs().unwrap();
let sync_regs = vcpu.sync_regs();
assert_eq!(regs, sync_regs.regs);
assert_eq!(sync_regs.regs.rax, 0x8001);
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_translate_gva() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
vcpu.translate_gva(0x10000).unwrap();
assert_eq!(vcpu.translate_gva(0x10000).unwrap().valid, 1);
assert_eq!(
vcpu.translate_gva(0x10000).unwrap().physical_address,
0x10000
);
vcpu.translate_gva(u64::MAX).unwrap();
assert_eq!(vcpu.translate_gva(u64::MAX).unwrap().valid, 0);
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_vcpu_attr() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let dist_attr = kvm_device_attr {
group: KVM_ARM_VCPU_PMU_V3_CTRL,
attr: u64::from(KVM_ARM_VCPU_PMU_V3_INIT),
addr: 0x0,
flags: 0,
};
vcpu.has_device_attr(&dist_attr).unwrap_err();
vcpu.set_device_attr(&dist_attr).unwrap_err();
let mut kvi: kvm_vcpu_init = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
kvi.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2) | (1 << KVM_ARM_VCPU_PMU_V3);
vcpu.vcpu_init(&kvi).unwrap();
vcpu.has_device_attr(&dist_attr).unwrap();
vcpu.set_device_attr(&dist_attr).unwrap();
}
#[test]
#[cfg(target_arch = "aarch64")]
fn test_pointer_authentication() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
let mut kvi = kvm_vcpu_init::default();
vm.get_preferred_target(&mut kvi)
.expect("Cannot get preferred target");
if kvm.check_extension(Cap::ArmPtrAuthAddress) {
kvi.features[0] |= 1 << KVM_ARM_VCPU_PTRAUTH_ADDRESS;
}
if kvm.check_extension(Cap::ArmPtrAuthGeneric) {
kvi.features[0] |= 1 << KVM_ARM_VCPU_PTRAUTH_GENERIC;
}
vcpu.vcpu_init(&kvi).unwrap();
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_userspace_rdmsr_exit() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
#[rustfmt::skip]
let code = [
0x0F, 0x32, /* rdmsr */
0xF4 /* hlt */
];
if !vm.check_extension(Cap::X86UserSpaceMsr) {
return;
}
let cap = kvm_enable_cap {
cap: Cap::X86UserSpaceMsr as u32,
args: [MsrExitReason::Unknown.bits() as u64, 0, 0, 0],
..Default::default()
};
vm.enable_cap(&cap).unwrap();
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu = vm.create_vcpu(0).unwrap();
// Set up special registers
let mut vcpu_sregs = vcpu.get_sregs().unwrap();
assert_ne!(vcpu_sregs.cs.base, 0);
assert_ne!(vcpu_sregs.cs.selector, 0);
vcpu_sregs.cs.base = 0;
vcpu_sregs.cs.selector = 0;
vcpu.set_sregs(&vcpu_sregs).unwrap();
// Set the Instruction Pointer to the guest address where we loaded
// the code, and RCX to the MSR to be read.
let mut vcpu_regs = vcpu.get_regs().unwrap();
vcpu_regs.rip = guest_addr;
vcpu_regs.rcx = 0x474f4f00;
vcpu.set_regs(&vcpu_regs).unwrap();
match vcpu.run().unwrap() {
VcpuExit::X86Rdmsr(exit) => {
assert_eq!(exit.reason, MsrExitReason::Unknown);
assert_eq!(exit.index, 0x474f4f00);
}
e => panic!("Unexpected exit: {:?}", e),
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_userspace_hypercall_exit() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
// Use `vmcall` or `vmmcall` depending on what's supported.
let cpuid = kvm.get_supported_cpuid(KVM_MAX_CPUID_ENTRIES).unwrap();
let supports_vmcall = cpuid
.as_slice()
.iter()
.find(|entry| entry.function == 1)
.is_some_and(|entry| entry.ecx & (1 << 5) != 0);
let supports_vmmcall = cpuid
.as_slice()
.iter()
.find(|entry| entry.function == 0x8000_0001)
.is_some_and(|entry| entry.ecx & (1 << 2) != 0);
#[rustfmt::skip]
let code = if supports_vmcall {
[
0x0F, 0x01, 0xC1, /* vmcall */
0xF4 /* hlt */
]
} else if supports_vmmcall {
[
0x0F, 0x01, 0xD9, /* vmmcall */
0xF4 /* hlt */
]
} else {
return;
};
if !vm.check_extension(Cap::ExitHypercall) {
return;
}
const KVM_HC_MAP_GPA_RANGE: u64 = 12;
let cap = kvm_enable_cap {
cap: Cap::ExitHypercall as u32,
args: [1 << KVM_HC_MAP_GPA_RANGE, 0, 0, 0],
..Default::default()
};
vm.enable_cap(&cap).unwrap();
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu = vm.create_vcpu(0).unwrap();
// Set up special registers
let mut vcpu_sregs = vcpu.get_sregs().unwrap();
assert_ne!(vcpu_sregs.cs.base, 0);
assert_ne!(vcpu_sregs.cs.selector, 0);
vcpu_sregs.cs.base = 0;
vcpu_sregs.cs.selector = 0;
vcpu.set_sregs(&vcpu_sregs).unwrap();
// Set the Instruction Pointer to the guest address where we loaded
// the code, and RCX to the MSR to be read.
let mut vcpu_regs = vcpu.get_regs().unwrap();
vcpu_regs.rip = guest_addr;
vcpu_regs.rax = KVM_HC_MAP_GPA_RANGE;
vcpu_regs.rbx = 0x1234000;
vcpu_regs.rcx = 1;
vcpu_regs.rdx = 0;
vcpu.set_regs(&vcpu_regs).unwrap();
match vcpu.run().unwrap() {
VcpuExit::Hypercall(exit) => {
assert_eq!(exit.nr, KVM_HC_MAP_GPA_RANGE);
assert_eq!(exit.args[0], 0x1234000);
assert_eq!(exit.args[1], 1);
assert_eq!(exit.args[2], 0);
}
e => panic!("Unexpected exit: {:?}", e),
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_userspace_wrmsr_exit() {
use std::io::Write;
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
#[rustfmt::skip]
let code = [
0x0F, 0x30, /* wrmsr */
0xF4 /* hlt */
];
if !vm.check_extension(Cap::X86UserSpaceMsr) {
return;
}
let cap = kvm_enable_cap {
cap: Cap::X86UserSpaceMsr as u32,
args: [MsrExitReason::Unknown.bits() as u64, 0, 0, 0],
..Default::default()
};
vm.enable_cap(&cap).unwrap();
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let mut vcpu = vm.create_vcpu(0).unwrap();
// Set up special registers
let mut vcpu_sregs = vcpu.get_sregs().unwrap();
assert_ne!(vcpu_sregs.cs.base, 0);
assert_ne!(vcpu_sregs.cs.selector, 0);
vcpu_sregs.cs.base = 0;
vcpu_sregs.cs.selector = 0;
vcpu.set_sregs(&vcpu_sregs).unwrap();
// Set the Instruction Pointer to the guest address where we loaded
// the code, RCX to the MSR to be written, and EDX:EAX to the data to
// be written.
let mut vcpu_regs = vcpu.get_regs().unwrap();
vcpu_regs.rip = guest_addr;
vcpu_regs.rcx = 0x474f4f00;
vcpu_regs.rax = 0xdeadbeef;
vcpu_regs.rdx = 0xd0c0ffee;
vcpu.set_regs(&vcpu_regs).unwrap();
match vcpu.run().unwrap() {
VcpuExit::X86Wrmsr(exit) => {
assert_eq!(exit.reason, MsrExitReason::Unknown);
assert_eq!(exit.index, 0x474f4f00);
assert_eq!(exit.data & 0xffffffff, 0xdeadbeef);
assert_eq!((exit.data >> 32) & 0xffffffff, 0xd0c0ffee);
}
e => panic!("Unexpected exit: {:?}", e),
}
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_coalesced_pio() {
use crate::IoEventAddress;
use std::io::Write;
const PORT: u64 = 0x2c;
const DATA: u64 = 0x39;
const SIZE: u32 = 1;
#[rustfmt::skip]
let code = [
0xe6, 0x2c, // out 0x2c, al
0xf4, // hlt
0xe6, 0x2c, // out 0x2c, al
0xf4, // hlt
];
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
assert!(vm.check_extension(Cap::CoalescedPio));
// Prepare guest memory
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let addr = IoEventAddress::Pio(PORT);
vm.register_coalesced_mmio(addr, SIZE).unwrap();
let mut vcpu = vm.create_vcpu(0).unwrap();
// Map the MMIO ring
vcpu.map_coalesced_mmio_ring().unwrap();
// Set regs
let mut regs = vcpu.get_regs().unwrap();
regs.rip = guest_addr;
regs.rax = DATA;
regs.rflags = 2;
vcpu.set_regs(®s).unwrap();
// Set sregs
let mut sregs = vcpu.get_sregs().unwrap();
sregs.cs.base = 0;
sregs.cs.selector = 0;
vcpu.set_sregs(&sregs).unwrap();
// Run and check that the exit was caused by the hlt and not the port
// I/O
let exit = vcpu.run().unwrap();
assert!(matches!(exit, VcpuExit::Hlt));
// Check that the ring buffer entry is what we expect
let entry = vcpu.coalesced_mmio_read().unwrap().unwrap();
assert_eq!(entry.phys_addr, PORT);
assert_eq!(entry.len, 1);
assert_eq!(entry.data[0] as u64, DATA);
// SAFETY: this field is a u32 in all variants of the union,
// so access is always safe.
let pio = unsafe { entry.__bindgen_anon_1.pio };
assert_eq!(pio, 1);
// The ring buffer should be empty now
assert!(vcpu.coalesced_mmio_read().unwrap().is_none());
// Unregister and check that the next PIO write triggers an exit
vm.unregister_coalesced_mmio(addr, SIZE).unwrap();
let exit = vcpu.run().unwrap();
let VcpuExit::IoOut(port, data) = exit else {
panic!("Unexpected VM exit: {:?}", exit);
};
assert_eq!(port, PORT as u16);
assert_eq!(data, (DATA as u8).to_le_bytes());
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_coalesced_mmio() {
use crate::IoEventAddress;
use std::io::Write;
const ADDR: u64 = 0x124;
const DATA: u64 = 0x39;
const SIZE: u32 = 2;
#[rustfmt::skip]
let code = [
0x66, 0x31, 0xFF, // xor di,di
0x66, 0xBF, 0x24, 0x01, // mov di, 0x124
0x67, 0x66, 0x89, 0x05, // mov WORD PTR [di], ax
0xF4, // hlt
0x66, 0x31, 0xFF, // xor di,di
0x66, 0xBF, 0x24, 0x01, // mov di, 0x124
0x67, 0x66, 0x89, 0x05, // mov WORD PTR [di], ax
0xF4, // hlt
];
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
assert!(vm.check_extension(Cap::CoalescedMmio));
// Prepare guest memory
let mem_size = 0x4000;
let load_addr = mmap_anonymous(mem_size).as_ptr();
let guest_addr: u64 = 0x1000;
let slot: u32 = 0;
let mem_region = kvm_userspace_memory_region {
slot,
guest_phys_addr: guest_addr,
memory_size: mem_size as u64,
userspace_addr: load_addr as u64,
flags: 0,
};
unsafe {
vm.set_user_memory_region(mem_region).unwrap();
// Get a mutable slice of `mem_size` from `load_addr`.
// This is safe because we mapped it before.
let mut slice = std::slice::from_raw_parts_mut(load_addr, mem_size);
slice.write_all(&code).unwrap();
}
let addr = IoEventAddress::Mmio(ADDR);
vm.register_coalesced_mmio(addr, SIZE).unwrap();
let mut vcpu = vm.create_vcpu(0).unwrap();
// Map the MMIO ring
vcpu.map_coalesced_mmio_ring().unwrap();
// Set regs
let mut regs = vcpu.get_regs().unwrap();
regs.rip = guest_addr;
regs.rax = DATA;
regs.rdx = ADDR;
regs.rflags = 2;
vcpu.set_regs(®s).unwrap();
// Set sregs
let mut sregs = vcpu.get_sregs().unwrap();
sregs.cs.base = 0;
sregs.cs.selector = 0;
vcpu.set_sregs(&sregs).unwrap();
// Run and check that the exit was caused by the hlt and not the MMIO
// access
let exit = vcpu.run().unwrap();
assert!(matches!(exit, VcpuExit::Hlt));
// Check that the ring buffer entry is what we expect
let entry = vcpu.coalesced_mmio_read().unwrap().unwrap();
assert_eq!(entry.phys_addr, ADDR);
assert_eq!(entry.len, SIZE);
assert_eq!(entry.data[0] as u64, DATA);
// SAFETY: this field is a u32 in all variants of the union,
// so access is always safe.
let pio = unsafe { entry.__bindgen_anon_1.pio };
assert_eq!(pio, 0);
// The ring buffer should be empty now
assert!(vcpu.coalesced_mmio_read().unwrap().is_none());
// Unregister and check that the next MMIO write triggers an exit
vm.unregister_coalesced_mmio(addr, SIZE).unwrap();
let exit = vcpu.run().unwrap();
let VcpuExit::MmioWrite(addr, data) = exit else {
panic!("Unexpected VM exit: {:?}", exit);
};
assert_eq!(addr, ADDR);
assert_eq!(data, (DATA as u16).to_le_bytes());
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_get_and_set_nested_state() {
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let vcpu = vm.create_vcpu(0).unwrap();
// Ensure that KVM also during runtime never wants more memory than we have pre-allocated
// by the helper type. KVM is expected to report:
// - 128+4096==4224 on SVM
// - 128+8192==8320 on VMX
let kvm_nested_state_size = kvm.check_extension_int(Cap::NestedState) as usize;
assert!(kvm_nested_state_size <= size_of::<KvmNestedStateBuffer>());
let mut state_buffer = KvmNestedStateBuffer::default();
// Ensure that header shows full buffer length.
assert_eq!(
state_buffer.size as usize,
size_of::<KvmNestedStateBuffer>()
);
vcpu.nested_state(&mut state_buffer).unwrap();
let old_state = state_buffer;
// There is no nested guest in this test, so there is no payload.
assert_eq!(state_buffer.size as usize, size_of::<kvm_nested_state>());
vcpu.set_nested_state(&old_state).unwrap();
}
}