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
|
/*
* qemu/kvm integration
*
* Copyright (C) 2006-2008 Qumranet Technologies
*
* Licensed under the terms of the GNU GPL version 2 or higher.
*/
#include "config.h"
#include "config-host.h"
#include <assert.h>
#include <string.h>
#include "hw/hw.h"
#include "sysemu.h"
#include "qemu-common.h"
#include "console.h"
#include "block.h"
#include "compatfd.h"
#include "gdbstub.h"
#include "qemu-kvm.h"
#include "libkvm.h"
#include <pthread.h>
#include <sys/utsname.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "compatfd.h"
#include <sys/prctl.h>
#define false 0
#define true 1
#ifndef PR_MCE_KILL
#define PR_MCE_KILL 33
#endif
#ifndef BUS_MCEERR_AR
#define BUS_MCEERR_AR 4
#endif
#ifndef BUS_MCEERR_AO
#define BUS_MCEERR_AO 5
#endif
#define EXPECTED_KVM_API_VERSION 12
#if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
#error libkvm: userspace and kernel version mismatch
#endif
int kvm_allowed = 1;
int kvm_irqchip = 1;
int kvm_pit = 1;
int kvm_pit_reinject = 1;
int kvm_nested = 0;
KVMState *kvm_state;
kvm_context_t kvm_context;
pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_system_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_pause_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t qemu_work_cond = PTHREAD_COND_INITIALIZER;
__thread CPUState *current_env;
static int qemu_system_ready;
#define SIG_IPI (SIGRTMIN+4)
pthread_t io_thread;
static int io_thread_fd = -1;
static int io_thread_sigfd = -1;
static CPUState *kvm_debug_cpu_requested;
static uint64_t phys_ram_size;
#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
/* The list of ioperm_data */
static QLIST_HEAD(, ioperm_data) ioperm_head;
#endif
//#define DEBUG_MEMREG
#ifdef DEBUG_MEMREG
#define DPRINTF(fmt, args...) \
do { fprintf(stderr, "%s:%d " fmt , __func__, __LINE__, ##args); } while (0)
#else
#define DPRINTF(fmt, args...) do {} while (0)
#endif
#define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
int kvm_abi = EXPECTED_KVM_API_VERSION;
int kvm_page_size;
#ifdef KVM_CAP_SET_GUEST_DEBUG
static int kvm_debug(CPUState *env,
struct kvm_debug_exit_arch *arch_info)
{
int handle = kvm_arch_debug(arch_info);
if (handle) {
kvm_debug_cpu_requested = env;
env->stopped = 1;
}
return handle;
}
#endif
static int handle_unhandled(uint64_t reason)
{
fprintf(stderr, "kvm: unhandled exit %" PRIx64 "\n", reason);
return -EINVAL;
}
static inline void set_gsi(kvm_context_t kvm, unsigned int gsi)
{
uint32_t *bitmap = kvm->used_gsi_bitmap;
if (gsi < kvm->max_gsi)
bitmap[gsi / 32] |= 1U << (gsi % 32);
else
DPRINTF("Invalid GSI %u\n", gsi);
}
static inline void clear_gsi(kvm_context_t kvm, unsigned int gsi)
{
uint32_t *bitmap = kvm->used_gsi_bitmap;
if (gsi < kvm->max_gsi)
bitmap[gsi / 32] &= ~(1U << (gsi % 32));
else
DPRINTF("Invalid GSI %u\n", gsi);
}
struct slot_info {
unsigned long phys_addr;
unsigned long len;
unsigned long userspace_addr;
unsigned flags;
int logging_count;
};
struct slot_info slots[KVM_MAX_NUM_MEM_REGIONS];
static void init_slots(void)
{
int i;
for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
slots[i].len = 0;
}
static int get_free_slot(kvm_context_t kvm)
{
int i;
int tss_ext;
#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
tss_ext = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
#else
tss_ext = 0;
#endif
/*
* on older kernels where the set tss ioctl is not supprted we must save
* slot 0 to hold the extended memory, as the vmx will use the last 3
* pages of this slot.
*/
if (tss_ext > 0)
i = 0;
else
i = 1;
for (; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
if (!slots[i].len)
return i;
return -1;
}
static void register_slot(int slot, unsigned long phys_addr,
unsigned long len, unsigned long userspace_addr,
unsigned flags)
{
slots[slot].phys_addr = phys_addr;
slots[slot].len = len;
slots[slot].userspace_addr = userspace_addr;
slots[slot].flags = flags;
}
static void free_slot(int slot)
{
slots[slot].len = 0;
slots[slot].logging_count = 0;
}
static int get_slot(unsigned long phys_addr)
{
int i;
for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS; ++i) {
if (slots[i].len && slots[i].phys_addr <= phys_addr &&
(slots[i].phys_addr + slots[i].len - 1) >= phys_addr)
return i;
}
return -1;
}
/* Returns -1 if this slot is not totally contained on any other,
* and the number of the slot otherwise */
static int get_container_slot(uint64_t phys_addr, unsigned long size)
{
int i;
for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
if (slots[i].len && slots[i].phys_addr <= phys_addr &&
(slots[i].phys_addr + slots[i].len) >= phys_addr + size)
return i;
return -1;
}
int kvm_is_containing_region(kvm_context_t kvm, unsigned long phys_addr,
unsigned long size)
{
int slot = get_container_slot(phys_addr, size);
if (slot == -1)
return 0;
return 1;
}
/*
* dirty pages logging control
*/
static int kvm_dirty_pages_log_change(kvm_context_t kvm,
unsigned long phys_addr, unsigned flags,
unsigned mask)
{
int r = -1;
int slot = get_slot(phys_addr);
if (slot == -1) {
fprintf(stderr, "BUG: %s: invalid parameters\n", __FUNCTION__);
return 1;
}
flags = (slots[slot].flags & ~mask) | flags;
if (flags == slots[slot].flags)
return 0;
slots[slot].flags = flags;
{
struct kvm_userspace_memory_region mem = {
.slot = slot,
.memory_size = slots[slot].len,
.guest_phys_addr = slots[slot].phys_addr,
.userspace_addr = slots[slot].userspace_addr,
.flags = slots[slot].flags,
};
DPRINTF("slot %d start %llx len %llx flags %x\n",
mem.slot, mem.guest_phys_addr, mem.memory_size, mem.flags);
r = kvm_vm_ioctl(kvm_state, KVM_SET_USER_MEMORY_REGION, &mem);
if (r < 0)
fprintf(stderr, "%s: %m\n", __FUNCTION__);
}
return r;
}
static int kvm_dirty_pages_log_change_all(kvm_context_t kvm,
int (*change)(kvm_context_t kvm,
uint64_t start,
uint64_t len))
{
int i, r;
for (i = r = 0; i < KVM_MAX_NUM_MEM_REGIONS && r == 0; i++) {
if (slots[i].len)
r = change(kvm, slots[i].phys_addr, slots[i].len);
}
return r;
}
int kvm_dirty_pages_log_enable_slot(kvm_context_t kvm, uint64_t phys_addr,
uint64_t len)
{
int slot = get_slot(phys_addr);
DPRINTF("start %" PRIx64 " len %" PRIx64 "\n", phys_addr, len);
if (slot == -1) {
fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
return -EINVAL;
}
if (slots[slot].logging_count++)
return 0;
return kvm_dirty_pages_log_change(kvm, slots[slot].phys_addr,
KVM_MEM_LOG_DIRTY_PAGES,
KVM_MEM_LOG_DIRTY_PAGES);
}
int kvm_dirty_pages_log_disable_slot(kvm_context_t kvm, uint64_t phys_addr,
uint64_t len)
{
int slot = get_slot(phys_addr);
if (slot == -1) {
fprintf(stderr, "BUG: %s: invalid parameters\n", __func__);
return -EINVAL;
}
if (--slots[slot].logging_count)
return 0;
return kvm_dirty_pages_log_change(kvm, slots[slot].phys_addr, 0,
KVM_MEM_LOG_DIRTY_PAGES);
}
/**
* Enable dirty page logging for all memory regions
*/
int kvm_dirty_pages_log_enable_all(kvm_context_t kvm)
{
if (kvm->dirty_pages_log_all)
return 0;
kvm->dirty_pages_log_all = 1;
return kvm_dirty_pages_log_change_all(kvm, kvm_dirty_pages_log_enable_slot);
}
/**
* Enable dirty page logging only for memory regions that were created with
* dirty logging enabled (disable for all other memory regions).
*/
int kvm_dirty_pages_log_reset(kvm_context_t kvm)
{
if (!kvm->dirty_pages_log_all)
return 0;
kvm->dirty_pages_log_all = 0;
return kvm_dirty_pages_log_change_all(kvm,
kvm_dirty_pages_log_disable_slot);
}
static int kvm_create_context(void);
int kvm_init(int smp_cpus)
{
int fd;
int r, gsi_count;
fd = open("/dev/kvm", O_RDWR);
if (fd == -1) {
perror("open /dev/kvm");
return -1;
}
r = ioctl(fd, KVM_GET_API_VERSION, 0);
if (r == -1) {
fprintf(stderr,
"kvm kernel version too old: "
"KVM_GET_API_VERSION ioctl not supported\n");
goto out_close;
}
if (r < EXPECTED_KVM_API_VERSION) {
fprintf(stderr, "kvm kernel version too old: "
"We expect API version %d or newer, but got "
"version %d\n", EXPECTED_KVM_API_VERSION, r);
goto out_close;
}
if (r > EXPECTED_KVM_API_VERSION) {
fprintf(stderr, "kvm userspace version too old\n");
goto out_close;
}
kvm_abi = r;
kvm_page_size = getpagesize();
kvm_state = qemu_mallocz(sizeof(*kvm_state));
kvm_context = &kvm_state->kvm_context;
kvm_state->fd = fd;
kvm_state->vmfd = -1;
kvm_context->opaque = cpu_single_env;
kvm_context->dirty_pages_log_all = 0;
kvm_context->no_irqchip_creation = 0;
kvm_context->no_pit_creation = 0;
#ifdef KVM_CAP_SET_GUEST_DEBUG
QTAILQ_INIT(&kvm_state->kvm_sw_breakpoints);
#endif
gsi_count = kvm_get_gsi_count(kvm_context);
if (gsi_count > 0) {
int gsi_bits, i;
/* Round up so we can search ints using ffs */
gsi_bits = ALIGN(gsi_count, 32);
kvm_context->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
kvm_context->max_gsi = gsi_bits;
/* Mark any over-allocated bits as already in use */
for (i = gsi_count; i < gsi_bits; i++)
set_gsi(kvm_context, i);
}
pthread_mutex_lock(&qemu_mutex);
return kvm_create_context();
out_close:
close(fd);
return -1;
}
static void kvm_finalize(KVMState *s)
{
/* FIXME
if (kvm->vcpu_fd[0] != -1)
close(kvm->vcpu_fd[0]);
if (kvm->vm_fd != -1)
close(kvm->vm_fd);
*/
close(s->fd);
free(s);
}
void kvm_disable_irqchip_creation(kvm_context_t kvm)
{
kvm->no_irqchip_creation = 1;
}
void kvm_disable_pit_creation(kvm_context_t kvm)
{
kvm->no_pit_creation = 1;
}
static void kvm_create_vcpu(CPUState *env, int id)
{
long mmap_size;
int r;
r = kvm_vm_ioctl(kvm_state, KVM_CREATE_VCPU, id);
if (r < 0) {
fprintf(stderr, "kvm_create_vcpu: %m\n");
fprintf(stderr, "Failed to create vCPU. Check the -smp parameter.\n");
goto err;
}
env->kvm_fd = r;
env->kvm_state = kvm_state;
mmap_size = kvm_ioctl(kvm_state, KVM_GET_VCPU_MMAP_SIZE, 0);
if (mmap_size < 0) {
fprintf(stderr, "get vcpu mmap size: %m\n");
goto err_fd;
}
env->kvm_run =
mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, env->kvm_fd,
0);
if (env->kvm_run == MAP_FAILED) {
fprintf(stderr, "mmap vcpu area: %m\n");
goto err_fd;
}
return;
err_fd:
close(env->kvm_fd);
err:
/* We're no good with semi-broken states. */
abort();
}
static int kvm_set_boot_vcpu_id(kvm_context_t kvm, uint32_t id)
{
#ifdef KVM_CAP_SET_BOOT_CPU_ID
int r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_SET_BOOT_CPU_ID);
if (r > 0)
return kvm_vm_ioctl(kvm_state, KVM_SET_BOOT_CPU_ID, id);
return -ENOSYS;
#else
return -ENOSYS;
#endif
}
int kvm_create_vm(kvm_context_t kvm)
{
int fd;
#ifdef KVM_CAP_IRQ_ROUTING
kvm->irq_routes = qemu_mallocz(sizeof(*kvm->irq_routes));
kvm->nr_allocated_irq_routes = 0;
#endif
fd = kvm_ioctl(kvm_state, KVM_CREATE_VM, 0);
if (fd < 0) {
fprintf(stderr, "kvm_create_vm: %m\n");
return -1;
}
kvm_state->vmfd = fd;
return 0;
}
static int kvm_create_default_phys_mem(kvm_context_t kvm,
unsigned long phys_mem_bytes,
void **vm_mem)
{
#ifdef KVM_CAP_USER_MEMORY
int r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);
if (r > 0)
return 0;
fprintf(stderr,
"Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
#else
#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
#endif
return -1;
}
void kvm_create_irqchip(kvm_context_t kvm)
{
int r;
kvm->irqchip_in_kernel = 0;
#ifdef KVM_CAP_IRQCHIP
if (!kvm->no_irqchip_creation) {
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_IRQCHIP);
if (r > 0) { /* kernel irqchip supported */
r = kvm_vm_ioctl(kvm_state, KVM_CREATE_IRQCHIP);
if (r >= 0) {
kvm->irqchip_inject_ioctl = KVM_IRQ_LINE;
#if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION,
KVM_CAP_IRQ_INJECT_STATUS);
if (r > 0)
kvm->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
#endif
kvm->irqchip_in_kernel = 1;
} else
fprintf(stderr, "Create kernel PIC irqchip failed\n");
}
}
#endif
kvm_state->irqchip_in_kernel = kvm->irqchip_in_kernel;
}
int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem)
{
int r;
r = kvm_create_vm(kvm);
if (r < 0)
return r;
r = kvm_arch_create(kvm, phys_mem_bytes, vm_mem);
if (r < 0)
return r;
init_slots();
r = kvm_create_default_phys_mem(kvm, phys_mem_bytes, vm_mem);
if (r < 0)
return r;
kvm_create_irqchip(kvm);
return 0;
}
int kvm_register_phys_mem(kvm_context_t kvm,
unsigned long phys_start, void *userspace_addr,
unsigned long len, int log)
{
struct kvm_userspace_memory_region memory = {
.memory_size = len,
.guest_phys_addr = phys_start,
.userspace_addr = (unsigned long) (uintptr_t) userspace_addr,
.flags = log ? KVM_MEM_LOG_DIRTY_PAGES : 0,
};
int r;
memory.slot = get_free_slot(kvm);
DPRINTF
("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %x\n",
memory.guest_phys_addr, memory.memory_size, memory.userspace_addr,
memory.slot, memory.flags);
r = kvm_vm_ioctl(kvm_state, KVM_SET_USER_MEMORY_REGION, &memory);
if (r < 0) {
fprintf(stderr, "create_userspace_phys_mem: %s\n", strerror(-r));
return -1;
}
register_slot(memory.slot, memory.guest_phys_addr, memory.memory_size,
memory.userspace_addr, memory.flags);
return 0;
}
/* destroy/free a whole slot.
* phys_start, len and slot are the params passed to kvm_create_phys_mem()
*/
void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start,
unsigned long len)
{
int slot;
int r;
struct kvm_userspace_memory_region memory = {
.memory_size = 0,
.guest_phys_addr = phys_start,
.userspace_addr = 0,
.flags = 0,
};
slot = get_slot(phys_start);
if ((slot >= KVM_MAX_NUM_MEM_REGIONS) || (slot == -1)) {
fprintf(stderr, "BUG: %s: invalid parameters (slot=%d)\n", __FUNCTION__,
slot);
return;
}
if (phys_start != slots[slot].phys_addr) {
fprintf(stderr,
"WARNING: %s: phys_start is 0x%lx expecting 0x%lx\n",
__FUNCTION__, phys_start, slots[slot].phys_addr);
phys_start = slots[slot].phys_addr;
}
memory.slot = slot;
DPRINTF("slot %d start %llx len %llx flags %x\n",
memory.slot, memory.guest_phys_addr, memory.memory_size,
memory.flags);
r = kvm_vm_ioctl(kvm_state, KVM_SET_USER_MEMORY_REGION, &memory);
if (r < 0) {
fprintf(stderr, "destroy_userspace_phys_mem: %s", strerror(-r));
return;
}
free_slot(memory.slot);
}
void kvm_unregister_memory_area(kvm_context_t kvm, uint64_t phys_addr,
unsigned long size)
{
int slot = get_container_slot(phys_addr, size);
if (slot != -1) {
DPRINTF("Unregistering memory region %" PRIx64 " (%lx)\n", phys_addr, size);
kvm_destroy_phys_mem(kvm, phys_addr, size);
return;
}
}
static int kvm_get_map(kvm_context_t kvm, int ioctl_num, int slot, void *buf)
{
int r;
struct kvm_dirty_log log = {
.slot = slot,
};
log.dirty_bitmap = buf;
r = kvm_vm_ioctl(kvm_state, ioctl_num, &log);
if (r < 0)
return r;
return 0;
}
int kvm_get_dirty_pages(kvm_context_t kvm, unsigned long phys_addr, void *buf)
{
int slot;
slot = get_slot(phys_addr);
return kvm_get_map(kvm, KVM_GET_DIRTY_LOG, slot, buf);
}
int kvm_get_dirty_pages_range(kvm_context_t kvm, unsigned long phys_addr,
unsigned long len, void *opaque,
int (*cb)(unsigned long start,
unsigned long len, void *bitmap,
void *opaque))
{
int i;
int r;
unsigned long end_addr = phys_addr + len;
void *buf;
for (i = 0; i < KVM_MAX_NUM_MEM_REGIONS; ++i) {
if ((slots[i].len && (uint64_t) slots[i].phys_addr >= phys_addr)
&& ((uint64_t) slots[i].phys_addr + slots[i].len <= end_addr)) {
buf = qemu_malloc(BITMAP_SIZE(slots[i].len));
r = kvm_get_map(kvm, KVM_GET_DIRTY_LOG, i, buf);
if (r) {
qemu_free(buf);
return r;
}
r = cb(slots[i].phys_addr, slots[i].len, buf, opaque);
qemu_free(buf);
if (r)
return r;
}
}
return 0;
}
#ifdef KVM_CAP_IRQCHIP
int kvm_set_irq_level(kvm_context_t kvm, int irq, int level, int *status)
{
struct kvm_irq_level event;
int r;
if (!kvm->irqchip_in_kernel)
return 0;
event.level = level;
event.irq = irq;
r = kvm_vm_ioctl(kvm_state, kvm->irqchip_inject_ioctl, &event);
if (r < 0)
perror("kvm_set_irq_level");
if (status) {
#ifdef KVM_CAP_IRQ_INJECT_STATUS
*status =
(kvm->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
#else
*status = 1;
#endif
}
return 1;
}
int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip)
{
int r;
if (!kvm->irqchip_in_kernel)
return 0;
r = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, chip);
if (r < 0) {
perror("kvm_get_irqchip\n");
}
return r;
}
int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip)
{
int r;
if (!kvm->irqchip_in_kernel)
return 0;
r = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, chip);
if (r < 0) {
perror("kvm_set_irqchip\n");
}
return r;
}
#endif
static int handle_debug(CPUState *env)
{
#ifdef KVM_CAP_SET_GUEST_DEBUG
struct kvm_run *run = env->kvm_run;
return kvm_debug(env, &run->debug.arch);
#else
return 0;
#endif
}
int kvm_get_regs(CPUState *env, struct kvm_regs *regs)
{
return kvm_vcpu_ioctl(env, KVM_GET_REGS, regs);
}
int kvm_set_regs(CPUState *env, struct kvm_regs *regs)
{
return kvm_vcpu_ioctl(env, KVM_SET_REGS, regs);
}
int kvm_get_fpu(CPUState *env, struct kvm_fpu *fpu)
{
return kvm_vcpu_ioctl(env, KVM_GET_FPU, fpu);
}
int kvm_set_fpu(CPUState *env, struct kvm_fpu *fpu)
{
return kvm_vcpu_ioctl(env, KVM_SET_FPU, fpu);
}
int kvm_get_sregs(CPUState *env, struct kvm_sregs *sregs)
{
return kvm_vcpu_ioctl(env, KVM_GET_SREGS, sregs);
}
int kvm_set_sregs(CPUState *env, struct kvm_sregs *sregs)
{
return kvm_vcpu_ioctl(env, KVM_SET_SREGS, sregs);
}
#ifdef KVM_CAP_MP_STATE
int kvm_get_mpstate(CPUState *env, struct kvm_mp_state *mp_state)
{
int r;
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_MP_STATE);
if (r > 0)
return kvm_vcpu_ioctl(env, KVM_GET_MP_STATE, mp_state);
return -ENOSYS;
}
int kvm_set_mpstate(CPUState *env, struct kvm_mp_state *mp_state)
{
int r;
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_MP_STATE);
if (r > 0)
return kvm_vcpu_ioctl(env, KVM_SET_MP_STATE, mp_state);
return -ENOSYS;
}
#endif
static int handle_mmio(CPUState *env)
{
unsigned long addr = env->kvm_run->mmio.phys_addr;
struct kvm_run *kvm_run = env->kvm_run;
void *data = kvm_run->mmio.data;
/* hack: Red Hat 7.1 generates these weird accesses. */
if ((addr > 0xa0000 - 4 && addr <= 0xa0000) && kvm_run->mmio.len == 3)
return 0;
cpu_physical_memory_rw(addr, data, kvm_run->mmio.len, kvm_run->mmio.is_write);
return 0;
}
int handle_io_window(kvm_context_t kvm)
{
return 1;
}
int handle_shutdown(kvm_context_t kvm, CPUState *env)
{
/* stop the current vcpu from going back to guest mode */
env->stopped = 1;
qemu_system_reset_request();
return 1;
}
static inline void push_nmi(kvm_context_t kvm)
{
#ifdef KVM_CAP_USER_NMI
kvm_arch_push_nmi(kvm->opaque);
#endif /* KVM_CAP_USER_NMI */
}
void post_kvm_run(kvm_context_t kvm, CPUState *env)
{
pthread_mutex_lock(&qemu_mutex);
kvm_arch_post_run(env, env->kvm_run);
cpu_single_env = env;
}
int pre_kvm_run(kvm_context_t kvm, CPUState *env)
{
kvm_arch_pre_run(env, env->kvm_run);
if (env->kvm_cpu_state.regs_modified) {
kvm_arch_put_registers(env);
env->kvm_cpu_state.regs_modified = 0;
}
pthread_mutex_unlock(&qemu_mutex);
return 0;
}
int kvm_is_ready_for_interrupt_injection(CPUState *env)
{
return env->kvm_run->ready_for_interrupt_injection;
}
static int kvm_handle_internal_error(kvm_context_t kvm,
CPUState *env,
struct kvm_run *run)
{
fprintf(stderr, "KVM internal error. Suberror: %d\n",
run->internal.suberror);
#ifdef KVM_CAP_INTERNAL_ERROR_DATA
if (kvm_check_extension(kvm_state, KVM_CAP_INTERNAL_ERROR_DATA)) {
int i;
for (i = 0; i < run->internal.ndata; ++i) {
fprintf(stderr, "extra data[%d]: %"PRIx64"\n",
i, (uint64_t)run->internal.data[i]);
}
}
#endif
kvm_show_regs(env);
if (run->internal.suberror == KVM_INTERNAL_ERROR_EMULATION)
fprintf(stderr, "emulation failure, check dmesg for details\n");
vm_stop(0);
return 1;
}
int kvm_run(CPUState *env)
{
int r;
kvm_context_t kvm = &env->kvm_state->kvm_context;
struct kvm_run *run = env->kvm_run;
int fd = env->kvm_fd;
again:
push_nmi(kvm);
#if !defined(__s390__)
if (!kvm->irqchip_in_kernel)
run->request_interrupt_window = kvm_arch_try_push_interrupts(env);
#endif
r = pre_kvm_run(kvm, env);
if (r)
return r;
r = ioctl(fd, KVM_RUN, 0);
if (r == -1 && errno != EINTR && errno != EAGAIN) {
r = -errno;
post_kvm_run(kvm, env);
fprintf(stderr, "kvm_run: %s\n", strerror(-r));
return r;
}
post_kvm_run(kvm, env);
#if defined(KVM_CAP_COALESCED_MMIO)
if (kvm_state->coalesced_mmio) {
struct kvm_coalesced_mmio_ring *ring =
(void *) run + kvm_state->coalesced_mmio * PAGE_SIZE;
while (ring->first != ring->last) {
cpu_physical_memory_rw(ring->coalesced_mmio[ring->first].phys_addr,
&ring->coalesced_mmio[ring->first].data[0],
ring->coalesced_mmio[ring->first].len, 1);
smp_wmb();
ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
}
}
#endif
#if !defined(__s390__)
if (r == -1) {
r = handle_io_window(kvm);
goto more;
}
#endif
if (1) {
switch (run->exit_reason) {
case KVM_EXIT_UNKNOWN:
r = handle_unhandled(run->hw.hardware_exit_reason);
break;
case KVM_EXIT_FAIL_ENTRY:
r = handle_unhandled(run->fail_entry.hardware_entry_failure_reason);
break;
case KVM_EXIT_EXCEPTION:
fprintf(stderr, "exception %d (%x)\n", run->ex.exception,
run->ex.error_code);
kvm_show_regs(env);
kvm_show_code(env);
abort();
break;
case KVM_EXIT_IO:
r = kvm_handle_io(run->io.port,
(uint8_t *)run + run->io.data_offset,
run->io.direction,
run->io.size,
run->io.count);
r = 0;
break;
case KVM_EXIT_DEBUG:
r = handle_debug(env);
break;
case KVM_EXIT_MMIO:
r = handle_mmio(env);
break;
case KVM_EXIT_HLT:
r = kvm_arch_halt(env);
break;
case KVM_EXIT_IRQ_WINDOW_OPEN:
break;
case KVM_EXIT_SHUTDOWN:
r = handle_shutdown(kvm, env);
break;
#if defined(__s390__)
case KVM_EXIT_S390_SIEIC:
r = kvm_s390_handle_intercept(kvm, env, run);
break;
case KVM_EXIT_S390_RESET:
r = kvm_s390_handle_reset(kvm, env, run);
break;
#endif
case KVM_EXIT_INTERNAL_ERROR:
r = kvm_handle_internal_error(kvm, env, run);
break;
default:
if (kvm_arch_run(env)) {
fprintf(stderr, "unhandled vm exit: 0x%x\n", run->exit_reason);
kvm_show_regs(env);
abort();
}
break;
}
}
more:
if (!r)
goto again;
return r;
}
int kvm_inject_irq(CPUState *env, unsigned irq)
{
struct kvm_interrupt intr;
intr.irq = irq;
return kvm_vcpu_ioctl(env, KVM_INTERRUPT, &intr);
}
#ifdef KVM_CAP_SET_GUEST_DEBUG
int kvm_set_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
{
return kvm_vcpu_ioctl(env, KVM_SET_GUEST_DEBUG, dbg);
}
#endif
int kvm_set_signal_mask(CPUState *env, const sigset_t *sigset)
{
struct kvm_signal_mask *sigmask;
int r;
if (!sigset) {
return kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, NULL);
}
sigmask = qemu_malloc(sizeof(*sigmask) + sizeof(*sigset));
sigmask->len = 8;
memcpy(sigmask->sigset, sigset, sizeof(*sigset));
r = kvm_vcpu_ioctl(env, KVM_SET_SIGNAL_MASK, sigmask);
free(sigmask);
return r;
}
int kvm_pit_in_kernel(kvm_context_t kvm)
{
return kvm->pit_in_kernel;
}
int kvm_inject_nmi(CPUState *env)
{
#ifdef KVM_CAP_USER_NMI
return kvm_vcpu_ioctl(env, KVM_NMI);
#else
return -ENOSYS;
#endif
}
int kvm_init_coalesced_mmio(kvm_context_t kvm)
{
int r = 0;
kvm_state->coalesced_mmio = 0;
#ifdef KVM_CAP_COALESCED_MMIO
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_COALESCED_MMIO);
if (r > 0) {
kvm_state->coalesced_mmio = r;
return 0;
}
#endif
return r;
}
#ifdef KVM_CAP_DEVICE_ASSIGNMENT
int kvm_assign_pci_device(kvm_context_t kvm,
struct kvm_assigned_pci_dev *assigned_dev)
{
return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_PCI_DEVICE, assigned_dev);
}
static int kvm_old_assign_irq(kvm_context_t kvm,
struct kvm_assigned_irq *assigned_irq)
{
return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_IRQ, assigned_irq);
}
#ifdef KVM_CAP_ASSIGN_DEV_IRQ
int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
{
int ret;
ret = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_ASSIGN_DEV_IRQ);
if (ret > 0) {
return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_DEV_IRQ, assigned_irq);
}
return kvm_old_assign_irq(kvm, assigned_irq);
}
int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
{
return kvm_vm_ioctl(kvm_state, KVM_DEASSIGN_DEV_IRQ, assigned_irq);
}
#else
int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
{
return kvm_old_assign_irq(kvm, assigned_irq);
}
#endif
#endif
#ifdef KVM_CAP_DEVICE_DEASSIGNMENT
int kvm_deassign_pci_device(kvm_context_t kvm,
struct kvm_assigned_pci_dev *assigned_dev)
{
return kvm_vm_ioctl(kvm_state, KVM_DEASSIGN_PCI_DEVICE, assigned_dev);
}
#endif
int kvm_destroy_memory_region_works(kvm_context_t kvm)
{
int ret = 0;
#ifdef KVM_CAP_DESTROY_MEMORY_REGION_WORKS
ret =
kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION,
KVM_CAP_DESTROY_MEMORY_REGION_WORKS);
if (ret <= 0)
ret = 0;
#endif
return ret;
}
int kvm_reinject_control(kvm_context_t kvm, int pit_reinject)
{
#ifdef KVM_CAP_REINJECT_CONTROL
int r;
struct kvm_reinject_control control;
control.pit_reinject = pit_reinject;
r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_REINJECT_CONTROL);
if (r > 0) {
return kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
}
#endif
return -ENOSYS;
}
int kvm_has_gsi_routing(kvm_context_t kvm)
{
int r = 0;
#ifdef KVM_CAP_IRQ_ROUTING
r = kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
#endif
return r;
}
int kvm_get_gsi_count(kvm_context_t kvm)
{
#ifdef KVM_CAP_IRQ_ROUTING
return kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
#else
return -EINVAL;
#endif
}
int kvm_clear_gsi_routes(kvm_context_t kvm)
{
#ifdef KVM_CAP_IRQ_ROUTING
kvm->irq_routes->nr = 0;
return 0;
#else
return -EINVAL;
#endif
}
int kvm_add_routing_entry(kvm_context_t kvm,
struct kvm_irq_routing_entry *entry)
{
#ifdef KVM_CAP_IRQ_ROUTING
struct kvm_irq_routing *z;
struct kvm_irq_routing_entry *new;
int n, size;
if (kvm->irq_routes->nr == kvm->nr_allocated_irq_routes) {
n = kvm->nr_allocated_irq_routes * 2;
if (n < 64)
n = 64;
size = sizeof(struct kvm_irq_routing);
size += n * sizeof(*new);
z = realloc(kvm->irq_routes, size);
if (!z)
return -ENOMEM;
kvm->nr_allocated_irq_routes = n;
kvm->irq_routes = z;
}
n = kvm->irq_routes->nr++;
new = &kvm->irq_routes->entries[n];
memset(new, 0, sizeof(*new));
new->gsi = entry->gsi;
new->type = entry->type;
new->flags = entry->flags;
new->u = entry->u;
set_gsi(kvm, entry->gsi);
return 0;
#else
return -ENOSYS;
#endif
}
int kvm_add_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin)
{
#ifdef KVM_CAP_IRQ_ROUTING
struct kvm_irq_routing_entry e;
e.gsi = gsi;
e.type = KVM_IRQ_ROUTING_IRQCHIP;
e.flags = 0;
e.u.irqchip.irqchip = irqchip;
e.u.irqchip.pin = pin;
return kvm_add_routing_entry(kvm, &e);
#else
return -ENOSYS;
#endif
}
int kvm_del_routing_entry(kvm_context_t kvm,
struct kvm_irq_routing_entry *entry)
{
#ifdef KVM_CAP_IRQ_ROUTING
struct kvm_irq_routing_entry *e, *p;
int i, gsi, found = 0;
gsi = entry->gsi;
for (i = 0; i < kvm->irq_routes->nr; ++i) {
e = &kvm->irq_routes->entries[i];
if (e->type == entry->type && e->gsi == gsi) {
switch (e->type) {
case KVM_IRQ_ROUTING_IRQCHIP:{
if (e->u.irqchip.irqchip ==
entry->u.irqchip.irqchip
&& e->u.irqchip.pin == entry->u.irqchip.pin) {
p = &kvm->irq_routes->entries[--kvm->irq_routes->nr];
*e = *p;
found = 1;
}
break;
}
case KVM_IRQ_ROUTING_MSI:{
if (e->u.msi.address_lo ==
entry->u.msi.address_lo
&& e->u.msi.address_hi ==
entry->u.msi.address_hi
&& e->u.msi.data == entry->u.msi.data) {
p = &kvm->irq_routes->entries[--kvm->irq_routes->nr];
*e = *p;
found = 1;
}
break;
}
default:
break;
}
if (found) {
/* If there are no other users of this GSI
* mark it available in the bitmap */
for (i = 0; i < kvm->irq_routes->nr; i++) {
e = &kvm->irq_routes->entries[i];
if (e->gsi == gsi)
break;
}
if (i == kvm->irq_routes->nr)
clear_gsi(kvm, gsi);
return 0;
}
}
}
return -ESRCH;
#else
return -ENOSYS;
#endif
}
int kvm_update_routing_entry(kvm_context_t kvm,
struct kvm_irq_routing_entry *entry,
struct kvm_irq_routing_entry *newentry)
{
#ifdef KVM_CAP_IRQ_ROUTING
struct kvm_irq_routing_entry *e;
int i;
if (entry->gsi != newentry->gsi || entry->type != newentry->type) {
return -EINVAL;
}
for (i = 0; i < kvm->irq_routes->nr; ++i) {
e = &kvm->irq_routes->entries[i];
if (e->type != entry->type || e->gsi != entry->gsi) {
continue;
}
switch (e->type) {
case KVM_IRQ_ROUTING_IRQCHIP:
if (e->u.irqchip.irqchip == entry->u.irqchip.irqchip &&
e->u.irqchip.pin == entry->u.irqchip.pin) {
memcpy(&e->u.irqchip, &newentry->u.irqchip,
sizeof e->u.irqchip);
return 0;
}
break;
case KVM_IRQ_ROUTING_MSI:
if (e->u.msi.address_lo == entry->u.msi.address_lo &&
e->u.msi.address_hi == entry->u.msi.address_hi &&
e->u.msi.data == entry->u.msi.data) {
memcpy(&e->u.msi, &newentry->u.msi, sizeof e->u.msi);
return 0;
}
break;
default:
break;
}
}
return -ESRCH;
#else
return -ENOSYS;
#endif
}
int kvm_del_irq_route(kvm_context_t kvm, int gsi, int irqchip, int pin)
{
#ifdef KVM_CAP_IRQ_ROUTING
struct kvm_irq_routing_entry e;
e.gsi = gsi;
e.type = KVM_IRQ_ROUTING_IRQCHIP;
e.flags = 0;
e.u.irqchip.irqchip = irqchip;
e.u.irqchip.pin = pin;
return kvm_del_routing_entry(kvm, &e);
#else
return -ENOSYS;
#endif
}
int kvm_commit_irq_routes(kvm_context_t kvm)
{
#ifdef KVM_CAP_IRQ_ROUTING
kvm->irq_routes->flags = 0;
return kvm_vm_ioctl(kvm_state, KVM_SET_GSI_ROUTING, kvm->irq_routes);
#else
return -ENOSYS;
#endif
}
int kvm_get_irq_route_gsi(kvm_context_t kvm)
{
int i, bit;
uint32_t *buf = kvm->used_gsi_bitmap;
/* Return the lowest unused GSI in the bitmap */
for (i = 0; i < kvm->max_gsi / 32; i++) {
bit = ffs(~buf[i]);
if (!bit)
continue;
return bit - 1 + i * 32;
}
return -ENOSPC;
}
#ifdef KVM_CAP_DEVICE_MSIX
int kvm_assign_set_msix_nr(kvm_context_t kvm,
struct kvm_assigned_msix_nr *msix_nr)
{
return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_SET_MSIX_NR, msix_nr);
}
int kvm_assign_set_msix_entry(kvm_context_t kvm,
struct kvm_assigned_msix_entry *entry)
{
return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_SET_MSIX_ENTRY, entry);
}
#endif
#if defined(KVM_CAP_IRQFD) && defined(CONFIG_EVENTFD)
#include <sys/eventfd.h>
static int _kvm_irqfd(kvm_context_t kvm, int fd, int gsi, int flags)
{
struct kvm_irqfd data = {
.fd = fd,
.gsi = gsi,
.flags = flags,
};
return kvm_vm_ioctl(kvm_state, KVM_IRQFD, &data);
}
int kvm_irqfd(kvm_context_t kvm, int gsi, int flags)
{
int r;
int fd;
if (!kvm_check_extension(kvm_state, KVM_CAP_IRQFD))
return -ENOENT;
fd = eventfd(0, 0);
if (fd < 0)
return -errno;
r = _kvm_irqfd(kvm, fd, gsi, 0);
if (r < 0) {
close(fd);
return -errno;
}
return fd;
}
#else /* KVM_CAP_IRQFD */
int kvm_irqfd(kvm_context_t kvm, int gsi, int flags)
{
return -ENOSYS;
}
#endif /* KVM_CAP_IRQFD */
static inline unsigned long kvm_get_thread_id(void)
{
return syscall(SYS_gettid);
}
static void qemu_cond_wait(pthread_cond_t *cond)
{
CPUState *env = cpu_single_env;
pthread_cond_wait(cond, &qemu_mutex);
cpu_single_env = env;
}
static void sig_ipi_handler(int n)
{
}
static void hardware_memory_error(void)
{
fprintf(stderr, "Hardware memory error!\n");
exit(1);
}
static void sigbus_reraise(void)
{
sigset_t set;
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_DFL;
if (!sigaction(SIGBUS, &action, NULL)) {
raise(SIGBUS);
sigemptyset(&set);
sigaddset(&set, SIGBUS);
sigprocmask(SIG_UNBLOCK, &set, NULL);
}
perror("Failed to re-raise SIGBUS!\n");
abort();
}
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
void *ctx)
{
#if defined(KVM_CAP_MCE) && defined(TARGET_I386)
if (first_cpu->mcg_cap && siginfo->ssi_addr
&& siginfo->ssi_code == BUS_MCEERR_AO) {
uint64_t status;
unsigned long paddr;
CPUState *cenv;
/* Hope we are lucky for AO MCE */
if (do_qemu_ram_addr_from_host((void *)(intptr_t)siginfo->ssi_addr,
&paddr)) {
fprintf(stderr, "Hardware memory error for memory used by "
"QEMU itself instead of guest system!: %llx\n",
(unsigned long long)siginfo->ssi_addr);
return;
}
status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN
| MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S
| 0xc0;
kvm_inject_x86_mce(first_cpu, 9, status,
MCG_STATUS_MCIP | MCG_STATUS_RIPV, paddr,
(MCM_ADDR_PHYS << 6) | 0xc, 1);
for (cenv = first_cpu->next_cpu; cenv != NULL; cenv = cenv->next_cpu)
kvm_inject_x86_mce(cenv, 1, MCI_STATUS_VAL | MCI_STATUS_UC,
MCG_STATUS_MCIP | MCG_STATUS_RIPV, 0, 0, 1);
} else
#endif
{
if (siginfo->ssi_code == BUS_MCEERR_AO)
return;
else if (siginfo->ssi_code == BUS_MCEERR_AR)
hardware_memory_error();
else
sigbus_reraise();
}
}
static void on_vcpu(CPUState *env, void (*func)(void *data), void *data)
{
struct qemu_work_item wi;
if (env == current_env) {
func(data);
return;
}
wi.func = func;
wi.data = data;
if (!env->kvm_cpu_state.queued_work_first)
env->kvm_cpu_state.queued_work_first = &wi;
else
env->kvm_cpu_state.queued_work_last->next = &wi;
env->kvm_cpu_state.queued_work_last = &wi;
wi.next = NULL;
wi.done = false;
pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
while (!wi.done)
qemu_cond_wait(&qemu_work_cond);
}
void kvm_arch_get_registers(CPUState *env)
{
kvm_arch_save_regs(env);
}
static void do_kvm_cpu_synchronize_state(void *_env)
{
CPUState *env = _env;
if (!env->kvm_cpu_state.regs_modified) {
kvm_arch_get_registers(env);
env->kvm_cpu_state.regs_modified = 1;
}
}
void kvm_cpu_synchronize_state(CPUState *env)
{
if (!env->kvm_cpu_state.regs_modified)
on_vcpu(env, do_kvm_cpu_synchronize_state, env);
}
static void inject_interrupt(void *data)
{
cpu_interrupt(current_env, (long) data);
}
void kvm_inject_interrupt(CPUState *env, int mask)
{
on_vcpu(env, inject_interrupt, (void *) (long) mask);
}
void kvm_update_interrupt_request(CPUState *env)
{
int signal = 0;
if (env) {
if (!current_env || !current_env->created)
signal = 1;
/*
* Testing for created here is really redundant
*/
if (current_env && current_env->created &&
env != current_env && !env->kvm_cpu_state.signalled)
signal = 1;
if (signal) {
env->kvm_cpu_state.signalled = 1;
if (env->kvm_cpu_state.thread)
pthread_kill(env->kvm_cpu_state.thread, SIG_IPI);
}
}
}
static void kvm_do_load_registers(void *_env)
{
CPUState *env = _env;
kvm_arch_load_regs(env);
}
void kvm_load_registers(CPUState *env)
{
if (kvm_enabled() && qemu_system_ready)
on_vcpu(env, kvm_do_load_registers, env);
}
static void kvm_do_save_registers(void *_env)
{
CPUState *env = _env;
kvm_arch_save_regs(env);
}
void kvm_save_registers(CPUState *env)
{
if (kvm_enabled())
on_vcpu(env, kvm_do_save_registers, env);
}
static void kvm_do_load_mpstate(void *_env)
{
CPUState *env = _env;
kvm_arch_load_mpstate(env);
}
void kvm_load_mpstate(CPUState *env)
{
if (kvm_enabled() && qemu_system_ready && kvm_vcpu_inited(env))
on_vcpu(env, kvm_do_load_mpstate, env);
}
static void kvm_do_save_mpstate(void *_env)
{
CPUState *env = _env;
kvm_arch_save_mpstate(env);
#ifdef KVM_CAP_MP_STATE
if (kvm_irqchip_in_kernel())
env->halted = (env->mp_state == KVM_MP_STATE_HALTED);
#endif
}
void kvm_save_mpstate(CPUState *env)
{
if (kvm_enabled())
on_vcpu(env, kvm_do_save_mpstate, env);
}
int kvm_cpu_exec(CPUState *env)
{
int r;
r = kvm_run(env);
if (r < 0) {
printf("kvm_run returned %d\n", r);
vm_stop(0);
}
return 0;
}
static int is_cpu_stopped(CPUState *env)
{
return !vm_running || env->stopped;
}
static void flush_queued_work(CPUState *env)
{
struct qemu_work_item *wi;
if (!env->kvm_cpu_state.queued_work_first)
return;
while ((wi = env->kvm_cpu_state.queued_work_first)) {
env->kvm_cpu_state.queued_work_first = wi->next;
wi->func(wi->data);
wi->done = true;
}
env->kvm_cpu_state.queued_work_last = NULL;
pthread_cond_broadcast(&qemu_work_cond);
}
static void kvm_on_sigbus(CPUState *env, siginfo_t *siginfo)
{
#if defined(KVM_CAP_MCE) && defined(TARGET_I386)
struct kvm_x86_mce mce = {
.bank = 9,
};
unsigned long paddr;
int r;
if (env->mcg_cap && siginfo->si_addr
&& (siginfo->si_code == BUS_MCEERR_AR
|| siginfo->si_code == BUS_MCEERR_AO)) {
if (siginfo->si_code == BUS_MCEERR_AR) {
/* Fake an Intel architectural Data Load SRAR UCR */
mce.status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN
| MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S
| MCI_STATUS_AR | 0x134;
mce.misc = (MCM_ADDR_PHYS << 6) | 0xc;
mce.mcg_status = MCG_STATUS_MCIP | MCG_STATUS_EIPV;
} else {
/* Fake an Intel architectural Memory scrubbing UCR */
mce.status = MCI_STATUS_VAL | MCI_STATUS_UC | MCI_STATUS_EN
| MCI_STATUS_MISCV | MCI_STATUS_ADDRV | MCI_STATUS_S
| 0xc0;
mce.misc = (MCM_ADDR_PHYS << 6) | 0xc;
mce.mcg_status = MCG_STATUS_MCIP | MCG_STATUS_RIPV;
}
if (do_qemu_ram_addr_from_host((void *)siginfo->si_addr, &paddr)) {
fprintf(stderr, "Hardware memory error for memory used by "
"QEMU itself instaed of guest system!\n");
/* Hope we are lucky for AO MCE */
if (siginfo->si_code == BUS_MCEERR_AO)
return;
else
hardware_memory_error();
}
mce.addr = paddr;
r = kvm_set_mce(env, &mce);
if (r < 0) {
fprintf(stderr, "kvm_set_mce: %s\n", strerror(errno));
abort();
}
} else
#endif
{
if (siginfo->si_code == BUS_MCEERR_AO)
return;
else if (siginfo->si_code == BUS_MCEERR_AR)
hardware_memory_error();
else
sigbus_reraise();
}
}
static void kvm_main_loop_wait(CPUState *env, int timeout)
{
struct timespec ts;
int r, e;
siginfo_t siginfo;
sigset_t waitset;
sigset_t chkset;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
sigemptyset(&waitset);
sigaddset(&waitset, SIG_IPI);
sigaddset(&waitset, SIGBUS);
do {
pthread_mutex_unlock(&qemu_mutex);
r = sigtimedwait(&waitset, &siginfo, &ts);
e = errno;
pthread_mutex_lock(&qemu_mutex);
if (r == -1 && !(e == EAGAIN || e == EINTR)) {
printf("sigtimedwait: %s\n", strerror(e));
exit(1);
}
switch (r) {
case SIGBUS:
kvm_on_sigbus(env, &siginfo);
break;
default:
break;
}
r = sigpending(&chkset);
if (r == -1) {
printf("sigpending: %s\n", strerror(e));
exit(1);
}
} while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
cpu_single_env = env;
flush_queued_work(env);
if (env->stop) {
env->stop = 0;
env->stopped = 1;
pthread_cond_signal(&qemu_pause_cond);
}
env->kvm_cpu_state.signalled = 0;
}
static int all_threads_paused(void)
{
CPUState *penv = first_cpu;
while (penv) {
if (penv->stop)
return 0;
penv = (CPUState *) penv->next_cpu;
}
return 1;
}
static void pause_all_threads(void)
{
CPUState *penv = first_cpu;
while (penv) {
if (penv != cpu_single_env) {
penv->stop = 1;
pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
} else {
penv->stop = 0;
penv->stopped = 1;
cpu_exit(penv);
}
penv = (CPUState *) penv->next_cpu;
}
while (!all_threads_paused())
qemu_cond_wait(&qemu_pause_cond);
}
static void resume_all_threads(void)
{
CPUState *penv = first_cpu;
assert(!cpu_single_env);
while (penv) {
penv->stop = 0;
penv->stopped = 0;
pthread_kill(penv->kvm_cpu_state.thread, SIG_IPI);
penv = (CPUState *) penv->next_cpu;
}
}
static void kvm_vm_state_change_handler(void *context, int running, int reason)
{
if (running)
resume_all_threads();
else
pause_all_threads();
}
static void setup_kernel_sigmask(CPUState *env)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR2);
sigaddset(&set, SIGIO);
sigaddset(&set, SIGALRM);
sigprocmask(SIG_BLOCK, &set, NULL);
sigprocmask(SIG_BLOCK, NULL, &set);
sigdelset(&set, SIG_IPI);
sigdelset(&set, SIGBUS);
kvm_set_signal_mask(env, &set);
}
static void qemu_kvm_system_reset(void)
{
CPUState *penv = first_cpu;
pause_all_threads();
qemu_system_reset();
while (penv) {
kvm_arch_cpu_reset(penv);
penv = (CPUState *) penv->next_cpu;
}
resume_all_threads();
}
static void process_irqchip_events(CPUState *env)
{
kvm_arch_process_irqchip_events(env);
if (kvm_arch_has_work(env))
env->halted = 0;
}
static int kvm_main_loop_cpu(CPUState *env)
{
while (1) {
int run_cpu = !is_cpu_stopped(env);
if (run_cpu && !kvm_irqchip_in_kernel()) {
process_irqchip_events(env);
run_cpu = !env->halted;
}
if (run_cpu) {
kvm_cpu_exec(env);
kvm_main_loop_wait(env, 0);
} else {
kvm_main_loop_wait(env, 1000);
}
}
pthread_mutex_unlock(&qemu_mutex);
return 0;
}
static void *ap_main_loop(void *_env)
{
CPUState *env = _env;
sigset_t signals;
#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
struct ioperm_data *data = NULL;
#endif
current_env = env;
env->thread_id = kvm_get_thread_id();
sigfillset(&signals);
sigprocmask(SIG_BLOCK, &signals, NULL);
kvm_create_vcpu(env, env->cpu_index);
#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
/* do ioperm for io ports of assigned devices */
QLIST_FOREACH(data, &ioperm_head, entries)
on_vcpu(env, kvm_arch_do_ioperm, data);
#endif
setup_kernel_sigmask(env);
pthread_mutex_lock(&qemu_mutex);
cpu_single_env = env;
kvm_arch_init_vcpu(env);
kvm_arch_load_regs(env);
/* signal VCPU creation */
current_env->created = 1;
pthread_cond_signal(&qemu_vcpu_cond);
/* and wait for machine initialization */
while (!qemu_system_ready)
qemu_cond_wait(&qemu_system_cond);
/* re-initialize cpu_single_env after re-acquiring qemu_mutex */
cpu_single_env = env;
kvm_main_loop_cpu(env);
return NULL;
}
void kvm_init_vcpu(CPUState *env)
{
pthread_create(&env->kvm_cpu_state.thread, NULL, ap_main_loop, env);
while (env->created == 0)
qemu_cond_wait(&qemu_vcpu_cond);
}
int kvm_vcpu_inited(CPUState *env)
{
return env->created;
}
#ifdef TARGET_I386
void kvm_hpet_disable_kpit(void)
{
struct kvm_pit_state2 ps2;
kvm_get_pit2(kvm_context, &ps2);
ps2.flags |= KVM_PIT_FLAGS_HPET_LEGACY;
kvm_set_pit2(kvm_context, &ps2);
}
void kvm_hpet_enable_kpit(void)
{
struct kvm_pit_state2 ps2;
kvm_get_pit2(kvm_context, &ps2);
ps2.flags &= ~KVM_PIT_FLAGS_HPET_LEGACY;
kvm_set_pit2(kvm_context, &ps2);
}
#endif
int kvm_init_ap(void)
{
struct sigaction action;
qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);
signal(SIG_IPI, sig_ipi_handler);
memset(&action, 0, sizeof(action));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
sigaction(SIGBUS, &action, NULL);
prctl(PR_MCE_KILL, 1, 1);
return 0;
}
void qemu_kvm_notify_work(void)
{
uint64_t value = 1;
char buffer[8];
size_t offset = 0;
if (io_thread_fd == -1)
return;
memcpy(buffer, &value, sizeof(value));
while (offset < 8) {
ssize_t len;
len = write(io_thread_fd, buffer + offset, 8 - offset);
if (len == -1 && errno == EINTR)
continue;
/* In case we have a pipe, there is not reason to insist writing
* 8 bytes
*/
if (len == -1 && errno == EAGAIN)
break;
if (len <= 0)
break;
offset += len;
}
}
/* If we have signalfd, we mask out the signals we want to handle and then
* use signalfd to listen for them. We rely on whatever the current signal
* handler is to dispatch the signals when we receive them.
*/
static void sigfd_handler(void *opaque)
{
int fd = (unsigned long) opaque;
struct qemu_signalfd_siginfo info;
struct sigaction action;
ssize_t len;
while (1) {
do {
len = read(fd, &info, sizeof(info));
} while (len == -1 && errno == EINTR);
if (len == -1 && errno == EAGAIN)
break;
if (len != sizeof(info)) {
printf("read from sigfd returned %zd: %m\n", len);
return;
}
sigaction(info.ssi_signo, NULL, &action);
if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction)
action.sa_sigaction(info.ssi_signo,
(siginfo_t *)&info, NULL);
else if (action.sa_handler)
action.sa_handler(info.ssi_signo);
}
}
/* Used to break IO thread out of select */
static void io_thread_wakeup(void *opaque)
{
int fd = (unsigned long) opaque;
char buffer[4096];
/* Drain the pipe/(eventfd) */
while (1) {
ssize_t len;
len = read(fd, buffer, sizeof(buffer));
if (len == -1 && errno == EINTR)
continue;
if (len <= 0)
break;
}
}
int kvm_main_loop(void)
{
int fds[2];
sigset_t mask;
int sigfd;
io_thread = pthread_self();
qemu_system_ready = 1;
if (qemu_eventfd(fds) == -1) {
fprintf(stderr, "failed to create eventfd\n");
return -errno;
}
fcntl(fds[0], F_SETFL, O_NONBLOCK);
fcntl(fds[1], F_SETFL, O_NONBLOCK);
qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
(void *)(unsigned long) fds[0]);
io_thread_fd = fds[1];
sigemptyset(&mask);
sigaddset(&mask, SIGIO);
sigaddset(&mask, SIGALRM);
sigaddset(&mask, SIGBUS);
sigprocmask(SIG_BLOCK, &mask, NULL);
sigfd = qemu_signalfd(&mask);
if (sigfd == -1) {
fprintf(stderr, "failed to create signalfd\n");
return -errno;
}
fcntl(sigfd, F_SETFL, O_NONBLOCK);
qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
(void *)(unsigned long) sigfd);
pthread_cond_broadcast(&qemu_system_cond);
io_thread_sigfd = sigfd;
cpu_single_env = NULL;
while (1) {
main_loop_wait(1000);
if (qemu_shutdown_requested()) {
if (qemu_no_shutdown()) {
vm_stop(0);
} else
break;
} else if (qemu_powerdown_requested())
qemu_irq_raise(qemu_system_powerdown);
else if (qemu_reset_requested())
qemu_kvm_system_reset();
else if (kvm_debug_cpu_requested) {
gdb_set_stop_cpu(kvm_debug_cpu_requested);
vm_stop(EXCP_DEBUG);
kvm_debug_cpu_requested = NULL;
}
}
pause_all_threads();
pthread_mutex_unlock(&qemu_mutex);
return 0;
}
#ifdef TARGET_I386
static int destroy_region_works = 0;
#endif
#if !defined(TARGET_I386)
int kvm_arch_init_irq_routing(void)
{
return 0;
}
#endif
extern int no_hpet;
static int kvm_create_context(void)
{
int r;
if (!kvm_irqchip) {
kvm_disable_irqchip_creation(kvm_context);
}
if (!kvm_pit) {
kvm_disable_pit_creation(kvm_context);
}
if (kvm_create(kvm_context, 0, NULL) < 0) {
kvm_finalize(kvm_state);
return -1;
}
r = kvm_arch_qemu_create_context();
if (r < 0)
kvm_finalize(kvm_state);
if (kvm_pit && !kvm_pit_reinject) {
if (kvm_reinject_control(kvm_context, 0)) {
fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
return -1;
}
}
#ifdef TARGET_I386
destroy_region_works = kvm_destroy_memory_region_works(kvm_context);
#endif
r = kvm_arch_init_irq_routing();
if (r < 0) {
return r;
}
kvm_state->vcpu_events = 0;
#ifdef KVM_CAP_VCPU_EVENTS
kvm_state->vcpu_events = kvm_check_extension(kvm_state, KVM_CAP_VCPU_EVENTS);
#endif
kvm_init_ap();
if (kvm_irqchip) {
if (!qemu_kvm_has_gsi_routing()) {
irq0override = 0;
#ifdef TARGET_I386
/* if kernel can't do irq routing, interrupt source
* override 0->2 can not be set up as required by hpet,
* so disable hpet.
*/
no_hpet = 1;
} else if (!qemu_kvm_has_pit_state2()) {
no_hpet = 1;
}
#else
}
#endif
}
return 0;
}
#ifdef TARGET_I386
static int must_use_aliases_source(target_phys_addr_t addr)
{
if (destroy_region_works)
return false;
if (addr == 0xa0000 || addr == 0xa8000)
return true;
return false;
}
static int must_use_aliases_target(target_phys_addr_t addr)
{
if (destroy_region_works)
return false;
if (addr >= 0xe0000000 && addr < 0x100000000ull)
return true;
return false;
}
static struct mapping {
target_phys_addr_t phys;
ram_addr_t ram;
ram_addr_t len;
} mappings[50];
static int nr_mappings;
static struct mapping *find_ram_mapping(ram_addr_t ram_addr)
{
struct mapping *p;
for (p = mappings; p < mappings + nr_mappings; ++p) {
if (p->ram <= ram_addr && ram_addr < p->ram + p->len) {
return p;
}
}
return NULL;
}
static struct mapping *find_mapping(target_phys_addr_t start_addr)
{
struct mapping *p;
for (p = mappings; p < mappings + nr_mappings; ++p) {
if (p->phys <= start_addr && start_addr < p->phys + p->len) {
return p;
}
}
return NULL;
}
static void drop_mapping(target_phys_addr_t start_addr)
{
struct mapping *p = find_mapping(start_addr);
if (p)
*p = mappings[--nr_mappings];
}
#endif
void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
ram_addr_t phys_offset)
{
int r = 0;
unsigned long area_flags;
#ifdef TARGET_I386
struct mapping *p;
#endif
if (start_addr + size > phys_ram_size) {
phys_ram_size = start_addr + size;
}
phys_offset &= ~IO_MEM_ROM;
area_flags = phys_offset & ~TARGET_PAGE_MASK;
if (area_flags != IO_MEM_RAM) {
#ifdef TARGET_I386
if (must_use_aliases_source(start_addr)) {
kvm_destroy_memory_alias(kvm_context, start_addr);
return;
}
if (must_use_aliases_target(start_addr))
return;
#endif
while (size > 0) {
p = find_mapping(start_addr);
if (p) {
kvm_unregister_memory_area(kvm_context, p->phys, p->len);
drop_mapping(p->phys);
}
start_addr += TARGET_PAGE_SIZE;
if (size > TARGET_PAGE_SIZE) {
size -= TARGET_PAGE_SIZE;
} else {
size = 0;
}
}
return;
}
r = kvm_is_containing_region(kvm_context, start_addr, size);
if (r)
return;
if (area_flags >= TLB_MMIO)
return;
#ifdef TARGET_I386
if (must_use_aliases_source(start_addr)) {
p = find_ram_mapping(phys_offset);
if (p) {
kvm_create_memory_alias(kvm_context, start_addr, size,
p->phys + (phys_offset - p->ram));
}
return;
}
#endif
r = kvm_register_phys_mem(kvm_context, start_addr,
qemu_get_ram_ptr(phys_offset), size, 0);
if (r < 0) {
printf("kvm_cpu_register_physical_memory: failed\n");
exit(1);
}
#ifdef TARGET_I386
drop_mapping(start_addr);
p = &mappings[nr_mappings++];
p->phys = start_addr;
p->ram = phys_offset;
p->len = size;
#endif
return;
}
int kvm_setup_guest_memory(void *area, unsigned long size)
{
int ret = 0;
#ifdef MADV_DONTFORK
if (kvm_enabled() && !kvm_has_sync_mmu())
ret = madvise(area, size, MADV_DONTFORK);
#endif
if (ret)
perror("madvise");
return ret;
}
#ifdef KVM_CAP_SET_GUEST_DEBUG
struct kvm_set_guest_debug_data {
struct kvm_guest_debug dbg;
int err;
};
static void kvm_invoke_set_guest_debug(void *data)
{
struct kvm_set_guest_debug_data *dbg_data = data;
if (cpu_single_env->kvm_cpu_state.regs_modified) {
kvm_arch_put_registers(cpu_single_env);
cpu_single_env->kvm_cpu_state.regs_modified = 0;
}
dbg_data->err =
kvm_set_guest_debug(cpu_single_env,
&dbg_data->dbg);
}
int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap)
{
struct kvm_set_guest_debug_data data;
data.dbg.control = 0;
if (env->singlestep_enabled)
data.dbg.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
kvm_arch_update_guest_debug(env, &data.dbg);
data.dbg.control |= reinject_trap;
on_vcpu(env, kvm_invoke_set_guest_debug, &data);
return data.err;
}
#endif
/*
* dirty pages logging
*/
/* FIXME: use unsigned long pointer instead of unsigned char */
unsigned char *kvm_dirty_bitmap = NULL;
int kvm_physical_memory_set_dirty_tracking(int enable)
{
int r = 0;
if (!kvm_enabled())
return 0;
if (enable) {
if (!kvm_dirty_bitmap) {
unsigned bitmap_size = BITMAP_SIZE(phys_ram_size);
kvm_dirty_bitmap = qemu_malloc(bitmap_size);
r = kvm_dirty_pages_log_enable_all(kvm_context);
}
} else {
if (kvm_dirty_bitmap) {
r = kvm_dirty_pages_log_reset(kvm_context);
qemu_free(kvm_dirty_bitmap);
kvm_dirty_bitmap = NULL;
}
}
return r;
}
/* get kvm's dirty pages bitmap and update qemu's */
static int kvm_get_dirty_pages_log_range(unsigned long start_addr,
unsigned char *bitmap,
unsigned long offset,
unsigned long mem_size)
{
unsigned int i, j, n = 0;
unsigned char c;
unsigned long page_number, addr, addr1;
ram_addr_t ram_addr;
unsigned int len = ((mem_size / TARGET_PAGE_SIZE) + 7) / 8;
/*
* bitmap-traveling is faster than memory-traveling (for addr...)
* especially when most of the memory is not dirty.
*/
for (i = 0; i < len; i++) {
c = bitmap[i];
while (c > 0) {
j = ffsl(c) - 1;
c &= ~(1u << j);
page_number = i * 8 + j;
addr1 = page_number * TARGET_PAGE_SIZE;
addr = offset + addr1;
ram_addr = cpu_get_physical_page_desc(addr);
cpu_physical_memory_set_dirty(ram_addr);
n++;
}
}
return 0;
}
static int kvm_get_dirty_bitmap_cb(unsigned long start, unsigned long len,
void *bitmap, void *opaque)
{
return kvm_get_dirty_pages_log_range(start, bitmap, start, len);
}
/*
* get kvm's dirty pages bitmap and update qemu's
* we only care about physical ram, which resides in slots 0 and 3
*/
int kvm_update_dirty_pages_log(void)
{
int r = 0;
r = kvm_get_dirty_pages_range(kvm_context, 0, -1UL, NULL,
kvm_get_dirty_bitmap_cb);
return r;
}
void kvm_qemu_log_memory(target_phys_addr_t start, target_phys_addr_t size,
int log)
{
if (log)
kvm_dirty_pages_log_enable_slot(kvm_context, start, size);
else {
#ifdef TARGET_I386
if (must_use_aliases_target(start))
return;
#endif
kvm_dirty_pages_log_disable_slot(kvm_context, start, size);
}
}
#ifdef KVM_CAP_IRQCHIP
int kvm_set_irq(int irq, int level, int *status)
{
return kvm_set_irq_level(kvm_context, irq, level, status);
}
#endif
int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
{
return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
}
void kvm_mutex_unlock(void)
{
assert(!cpu_single_env);
pthread_mutex_unlock(&qemu_mutex);
}
void kvm_mutex_lock(void)
{
pthread_mutex_lock(&qemu_mutex);
cpu_single_env = NULL;
}
void qemu_mutex_unlock_iothread(void)
{
if (kvm_enabled())
kvm_mutex_unlock();
}
void qemu_mutex_lock_iothread(void)
{
if (kvm_enabled())
kvm_mutex_lock();
}
#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
void kvm_add_ioperm_data(struct ioperm_data *data)
{
QLIST_INSERT_HEAD(&ioperm_head, data, entries);
}
void kvm_remove_ioperm_data(unsigned long start_port, unsigned long num)
{
struct ioperm_data *data;
data = QLIST_FIRST(&ioperm_head);
while (data) {
struct ioperm_data *next = QLIST_NEXT(data, entries);
if (data->start_port == start_port && data->num == num) {
QLIST_REMOVE(data, entries);
qemu_free(data);
}
data = next;
}
}
void kvm_ioperm(CPUState *env, void *data)
{
if (kvm_enabled() && qemu_system_ready)
on_vcpu(env, kvm_arch_do_ioperm, data);
}
#endif
int kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr,
target_phys_addr_t end_addr)
{
#ifndef TARGET_IA64
#ifdef TARGET_I386
if (must_use_aliases_source(start_addr))
return 0;
#endif
kvm_get_dirty_pages_range(kvm_context, start_addr,
end_addr - start_addr, NULL,
kvm_get_dirty_bitmap_cb);
#endif
return 0;
}
int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len)
{
#ifdef TARGET_I386
if (must_use_aliases_source(phys_addr))
return 0;
#endif
#ifndef TARGET_IA64
kvm_qemu_log_memory(phys_addr, len, 1);
#endif
return 0;
}
int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len)
{
#ifdef TARGET_I386
if (must_use_aliases_source(phys_addr))
return 0;
#endif
#ifndef TARGET_IA64
kvm_qemu_log_memory(phys_addr, len, 0);
#endif
return 0;
}
int kvm_set_boot_cpu_id(uint32_t id)
{
return kvm_set_boot_vcpu_id(kvm_context, id);
}
#ifdef TARGET_I386
#ifdef KVM_CAP_MCE
struct kvm_x86_mce_data {
CPUState *env;
struct kvm_x86_mce *mce;
int abort_on_error;
};
static void kvm_do_inject_x86_mce(void *_data)
{
struct kvm_x86_mce_data *data = _data;
int r;
r = kvm_set_mce(data->env, data->mce);
if (r < 0) {
perror("kvm_set_mce FAILED");
if (data->abort_on_error)
abort();
}
}
#endif
void kvm_inject_x86_mce(CPUState *cenv, int bank, uint64_t status,
uint64_t mcg_status, uint64_t addr, uint64_t misc,
int abort_on_error)
{
#ifdef KVM_CAP_MCE
struct kvm_x86_mce mce = {
.bank = bank,
.status = status,
.mcg_status = mcg_status,
.addr = addr,
.misc = misc,
};
struct kvm_x86_mce_data data = {
.env = cenv,
.mce = &mce,
.abort_on_error = abort_on_error,
};
if (!cenv->mcg_cap) {
fprintf(stderr, "MCE support is not enabled!\n");
return;
}
on_vcpu(cenv, kvm_do_inject_x86_mce, &data);
#else
if (abort_on_error)
abort();
#endif
}
#endif
|