summaryrefslogtreecommitdiffstats
path: root/virtio_loopback.c
blob: 0eaac5031c6a179a8518379779bc255f5d61b722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
/*
 *
 * Based on:
 *
 *  1) virtio.c of QEMU project
 *
 *     Copyright IBM, Corp. 2007
 *
 *     Authors:
 *      Anthony Liguori   <aliguori@us.ibm.com>
 *
 *
 *  2) virtio-mmio.c of QEMU project
 *
 *     Copyright (c) 2011 Linaro Limited
 *
 *     Author:
 *      Peter Maydell <peter.maydell@linaro.org>
 *
 *
 * Copyright 2022 Virtual Open Systems SAS.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/eventfd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/param.h>

/* For socket */
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

/* Project header files */
#include "virtio_loopback.h"
#include "virtio_rng.h"

#include <stddef.h>
#include <pthread.h>
#include <limits.h>

/* TODO: Deleteit, only for testing */
#include <linux/virtio_blk.h>

#ifdef DEBUG
#define DBG(...) printf("virtio-loopback: " __VA_ARGS__)
#else
#define DBG(...)
#endif /* DEBUG */

/* Global variables */

int s; /* To be deleted */
int efd; /* Eventfd file descriptor */
int efd_notify; /* Eventfd file descriptor */
uint64_t eftd_ctr;
fd_set rfds;
int fd;
int loopback_fd;

virtio_device_info_struct_t device_info;
virtio_neg_t *address;

VirtIOMMIOProxy *proxy;

void virtio_add_feature(uint64_t *features, unsigned int fbit)
{
    *features |= (1ULL << fbit);
}

bool virtio_has_feature(uint64_t features, unsigned int fbit)
{
    return !!(features & (1ULL << fbit));
}

static int virtio_validate_features(VirtIODevice *vdev)
{
    if (virtio_has_feature(vdev->host_features, VIRTIO_F_IOMMU_PLATFORM) &&
        !virtio_has_feature(vdev->guest_features, VIRTIO_F_IOMMU_PLATFORM)) {
        return -EFAULT;
    }

    return 0;
}

bool virtio_device_started(VirtIODevice *vdev, uint8_t status)
{

    DBG("virtio_device_started: %d\n", status & VIRTIO_CONFIG_S_DRIVER_OK);
    DBG("status: %d\n", status);

    return status & VIRTIO_CONFIG_S_DRIVER_OK;
}


void virtio_set_started(VirtIODevice *vdev, bool started)
{
    if (started) {
        vdev->start_on_kick = false;
    }

    if (vdev->use_started) {
        vdev->started = started;
    }
}

int virtio_set_status(VirtIODevice *vdev, uint8_t val)
{
    VirtioDeviceClass *k = vdev->vdev_class;

    DBG("virtio_set_status(...)\n");

    if (virtio_has_feature(vdev->guest_features, VIRTIO_F_VERSION_1)) {
        if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
            val & VIRTIO_CONFIG_S_FEATURES_OK) {
            int ret = virtio_validate_features(vdev);

            if (ret) {
                return ret;
            }
        }
    }

    if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) !=
        (val & VIRTIO_CONFIG_S_DRIVER_OK)) {
        virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK);
    }

    DBG("set vdev->status :%u \n", vdev->status);

    if (k->set_status) {
        DBG("k->set_status\n");
        k->set_status(vdev, val);
    }

    vdev->status = val;

    return 0;
}

uint64_t vring_align(uint64_t addr, unsigned long align)
{
    return QEMU_ALIGN_UP(addr, align);
}

uint64_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
{
    return sizeof(VRingDesc) * vdev->vq[n].vring.num;
}

uint64_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.desc;
}

uint64_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.avail;
}

uint64_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.used;
}


int virtio_queue_get_num(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.num;
}


uint64_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
{
    int s;

    s = virtio_has_feature(vdev->guest_features,
                           VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
    return offsetof(VRingAvail, ring) +
        sizeof(uint16_t) * vdev->vq[n].vring.num + s;
}

uint64_t virtio_queue_get_used_size(VirtIODevice *vdev, int n)
{
    int s;

    s = virtio_has_feature(vdev->guest_features,
                           VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
    return offsetof(VRingUsed, ring) +
        sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s;
}

/* virt queue functions */
void virtio_queue_update_rings(VirtIODevice *vdev, int n)
{
    VRing *vring = &vdev->vq[n].vring;

    if (!vring->num || !vring->desc || !vring->align) {
        /* not yet setup -> nothing to do */
        return;
    }
    vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
    vring->used = vring_align(vring->avail +
                              offsetof(VRingAvail, ring[vring->num]),
                              vring->align);
}

static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev,
                                                      int n)
{
    return vdev->vq[n].last_avail_idx;
}


unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
{
    return virtio_queue_split_get_last_avail_idx(vdev, n);
}

void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
{
    /*
     * Don't allow guest to flip queue between existent and
     * nonexistent states, or to set it to an invalid size.
     */
    if (!!num != !!vdev->vq[n].vring.num ||
        num > VIRTQUEUE_MAX_SIZE ||
        num < 0) {
        return;
    }
    vdev->vq[n].vring.num = num;
}

uint64_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.desc;
}


void virtio_queue_set_addr(VirtIODevice *vdev, int n, uint64_t addr)
{
    if (!vdev->vq[n].vring.num) {
        return;
    }
    vdev->vq[n].vring.desc = addr;
    virtio_queue_update_rings(vdev, n);
}

int virtio_queue_ready(VirtQueue *vq)
{
    return vq->vring.avail != 0;
}


uint16_t vring_avail_idx(VirtQueue *vq)
{
    vq->shadow_avail_idx = ((VRingAvail *)vq->vring.avail)->idx;

    return vq->shadow_avail_idx;
}

uint16_t vring_avail_ring(VirtQueue *vq, int i)
{
    return ((VRingAvail *)vq->vring.avail)->ring[i];
}

int virtio_queue_split_empty(VirtQueue *vq)
{
    bool empty;

    if (!vq->vring.avail) {
        return 1;
    }

    if (vq->shadow_avail_idx != vq->last_avail_idx) {
        return 0;
    }

    empty = vring_avail_idx(vq) == vq->last_avail_idx;
    return empty;
}

int virtio_queue_empty(VirtQueue *vq)
{
    return virtio_queue_split_empty(vq);
}

size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
                         size_t offset, const void *buf, size_t bytes)
{
    size_t done;
    unsigned int i;
    for (i = 0, done = 0; (offset || done < bytes) && i < iov_cnt; i++) {
        if (offset < iov[i].iov_len) {
            size_t len = MIN(iov[i].iov_len - offset, bytes - done);
            memcpy(iov[i].iov_base + offset, buf + done, len);
            done += len;
            offset = 0;
        } else {
            offset -= iov[i].iov_len;
        }
    }
    return done;
}


size_t qemu_iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
             size_t offset, const void *buf, size_t bytes)
{
    if (__builtin_constant_p(bytes) && iov_cnt &&
        offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
        memcpy(iov[0].iov_base + offset, buf, bytes);
        return bytes;
    } else {
        return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes);
    }
}


/* Called within rcu_read_lock().  */
static inline uint16_t vring_avail_flags(VirtQueue *vq)
{
    return ((VRingAvail *)vq->vring.avail)->flags;
}

/* Called within rcu_read_lock().  */
static inline uint16_t vring_get_used_event(VirtQueue *vq)
{
    return vring_avail_ring(vq, vq->vring.num);
}

/* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */
/*
 * Assuming a given event_idx value from the other side, if
 * we have just incremented index from old to new_idx,
 * should we trigger an event?
 */
static inline int vring_need_event(uint16_t event_idx,
                                   uint16_t new_idx, uint16_t old)
{
    /*
     * Note: Xen has similar logic for notification hold-off
     * in include/xen/interface/io/ring.h with req_event and req_prod
     * corresponding to event_idx + 1 and new_idx respectively.
     * Note also that req_event and req_prod in Xen start at 1,
     * event indexes in virtio start at 0.
     */
    return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
}

/* Called within rcu_read_lock(). */
static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq)
{
    uint16_t old, new;
    bool v;

    /* Always notify when queue is empty (when feature acknowledge) */
    if (virtio_has_feature(vdev->guest_features, VIRTIO_F_NOTIFY_ON_EMPTY) &&
        !vq->inuse && virtio_queue_empty(vq)) {
        return true;
    }

    if (!virtio_has_feature(vdev->guest_features, VIRTIO_RING_F_EVENT_IDX)) {
        return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
    }

    v = vq->signalled_used_valid;
    vq->signalled_used_valid = true;
    old = vq->signalled_used;
    new = vq->signalled_used = vq->used_idx;
    return !v || vring_need_event(vring_get_used_event(vq), new, old);
}

/* Called within rcu_read_lock().  */
static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
{
    return virtio_split_should_notify(vdev, vq);
}


void virtio_set_isr(VirtIODevice *vdev, int value)
{
    uint8_t old = vdev->isr;

    /*
     * Do not write ISR if it does not change, so that its cacheline remains
     * shared in the common case where the guest does not read it.
     */
    if ((old & value) != value) {
        vdev->isr |= value;
    }

    DBG("Update isr: %d\n", vdev->isr);
}

static void virtio_irq(VirtQueue *vq)
{
    virtio_set_isr(vq->vdev, 0x1);
    virtio_notify_vector(vq->vdev);
}

void virtio_notify_config(VirtIODevice *vdev)
{

    DBG("virtio_notify_config\n");

    if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
        return;
    }

    virtio_set_isr(vdev, 0x3);
    vdev->generation++;
    /*
     * MMIO does not use vector parameter:
     * virtio_notify_vector(vdev, vdev->config_vector);
     */
    virtio_notify_vector(vdev);
}

void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
{
    if (!virtio_should_notify(vdev, vq)) {
        DBG("Do not notify!\n");
        return;
    }
    DBG("Go on and notify!\n");

    virtio_irq(vq);
}

static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem, int i)
{
    VRingUsed *used = (VRingUsed *)vq->vring.used;

    used->ring[i] = *uelem;
}

void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *elem,
                    unsigned int len, unsigned int idx)
{
    VRingUsedElem uelem;

    if (!vq->vring.used) {
        return;
    }

    idx = (idx + vq->used_idx) % vq->vring.num;

    uelem.id = elem->index;
    uelem.len = len;
    vring_used_write(vq, &uelem, idx);
}

void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                    unsigned int len, unsigned int idx)
{
    virtqueue_split_fill(vq, elem, len, idx);
}

static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
{
    ((VRingUsed *)vq->vring.used)->idx = val;
    vq->used_idx = val;
}

static void virtqueue_split_flush(VirtQueue *vq, unsigned int count)
{
    uint16_t old, new;

    if (!vq->vring.used) {
        return;
    }

    old = vq->used_idx;
    new = old + count;
    vring_used_idx_set(vq, new);
    vq->inuse -= count;
    if ((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)) {
        vq->signalled_used_valid = false;
    }
}

void virtqueue_flush(VirtQueue *vq, unsigned int count)
{
    virtqueue_split_flush(vq, count);
}

void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
                    unsigned int len)
{
    virtqueue_fill(vq, elem, len, 0);
    virtqueue_flush(vq, 1);
}


void vring_set_avail_event(VirtQueue *vq, uint16_t val)
{
    uint16_t *avail;

    avail = (uint16_t *)&((VRingUsed *)vq->vring.used)->ring[vq->vring.num];
    *avail = val;
}

static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
                               uint64_t *addr, struct iovec *iov,
                               unsigned int max_num_sg, bool is_write,
                               uint64_t pa, size_t sz)
{
    unsigned num_sg = *p_num_sg;
    bool ok = false;
    uint64_t mmap_addr;
    int ioctl_res;

    if (!sz) {
        DBG("virtio: zero sized buffers are not allowed\n");
        goto out;
    }

    while (sz) {
        uint64_t len = sz;

        if (num_sg == max_num_sg) {
            DBG("virtio: too many write descriptors in\n"
                               "indirect table");
            goto out;
        }

        DBG("\tpa address is: 0x%lx\n", pa);

        memcpy(&mmap_addr, &pa, sizeof(uint64_t));
        ioctl_res = ioctl(loopback_fd, SHARE_BUF, &mmap_addr);

        /* Notify the loopback driver what you want to' mmap' */
        if (ioctl_res < 0) {
            DBG("SHARE_BUF failed\n");
            exit(1);
        } else {
            if (mmap_addr == 0) {

                if ((pa & 0xff) == 0) {
                    ioctl(loopback_fd, MAP_BLK);
                }

                DBG("Try to mmap pa: 0x%lx, size: %lx\n", pa, len);
                iov[num_sg].iov_base = mmap(NULL, len, PROT_READ | PROT_WRITE,
                                            MAP_SHARED, loopback_fd, 0);
                int retries = 5;
                while ((retries > 0) && ((int64_t)iov[num_sg].iov_base < 0)) {
                    iov[num_sg].iov_base = mmap(NULL, len,
                                                PROT_READ | PROT_WRITE,
                                                MAP_SHARED, loopback_fd, 0);
                    retries--;
                }

                if ((int64_t)iov[num_sg].iov_base < 0) {
                    DBG("Bad mapping\n");
                    exit(1);
                }
             } else {
                iov[num_sg].iov_base = (void *)mmap_addr;
             }
        }

        /* Fix the offset */
        iov[num_sg].iov_base += pa & 0xfff;
        DBG("\tMMap address (iov_base): 0x%lx\n",
            (uint64_t)iov[num_sg].iov_base);

        /* Update len: Remaining size in the current page */
        if (sz > PAGE_SIZE - (pa & 0xfff)) {
            len = PAGE_SIZE - (pa & 0xfff);
        }

        if (!iov[num_sg].iov_base) {
            DBG("virtio: bogus descriptor or out of resources\n");
            goto out;
        }

        iov[num_sg].iov_len = len;
        addr[num_sg] = pa;

        sz -= len;
        pa += len;
        num_sg++;
    }
    ok = true;

out:
    *p_num_sg = num_sg;
    return ok;
}

static void *virtqueue_alloc_element(size_t sz, unsigned out_num,
                                     unsigned in_num)
{
    VirtQueueElement *elem;
    size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
    size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
    size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
    size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
    size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
    size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);

    /*
     * TODO: Add check for requested size
     *
     * assert(sz >= sizeof(VirtQueueElement));
     */
    elem = malloc(out_sg_end);
    elem->out_num = out_num;
    elem->in_num = in_num;
    elem->in_addr = (void *)elem + in_addr_ofs;
    elem->out_addr = (void *)elem + out_addr_ofs;
    elem->in_sg = (void *)elem + in_sg_ofs;
    elem->out_sg = (void *)elem + out_sg_ofs;
    return elem;
}

void *virtqueue_split_pop(VirtQueue *vq, size_t sz)
{
    unsigned int i, head, max;
    int64_t len;
    VirtIODevice *vdev = vq->vdev;
    VirtQueueElement *elem = NULL;
    unsigned out_num, in_num, elem_entries;
    uint64_t addr[VIRTQUEUE_MAX_SIZE];
    struct iovec iov[VIRTQUEUE_MAX_SIZE];
    VRingDesc *desc;
    int rc;

    if (virtio_queue_split_empty(vq)) {
        goto done;
    }

    /* When we start there are none of either input nor output. */
    out_num = in_num = elem_entries = 0;

    max = vq->vring.num;

    if (vq->inuse >= vq->vring.num) {
        DBG("Virtqueue size exceeded\n");
        goto done;
    }

    if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
        goto done;
    }

    if (virtio_has_feature(vdev->guest_features, VIRTIO_RING_F_EVENT_IDX)) {
        vring_set_avail_event(vq, vq->last_avail_idx);
    }

    i = head;

    desc = (VRingDesc *)vq->vring.desc + i;

    /* Collect all the descriptors */
    do {
        bool map_ok;

        if (desc->flags & VRING_DESC_F_WRITE) {
            map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
                                        iov + out_num,
                                        VIRTQUEUE_MAX_SIZE - out_num, true,
                                        desc->addr, desc->len);
        } else {
            if (in_num) {
                DBG("Incorrect order for descriptors\n");
                goto err_undo_map;
            }
            map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
                                        VIRTQUEUE_MAX_SIZE, false,
                                        desc->addr, desc->len);
        }
        if (!map_ok) {
            goto err_undo_map;
        }

        /* If we've got too many, that implies a descriptor loop. */
        if (++elem_entries > max) {
            goto err_undo_map;
        }

        rc = virtqueue_split_read_next_desc(vdev, desc, max, &i);
    } while (rc == VIRTQUEUE_READ_DESC_MORE);

    if (rc == VIRTQUEUE_READ_DESC_ERROR) {
        goto err_undo_map;
    }

    /* Now copy what we have collected and mapped */
    elem = virtqueue_alloc_element(sz, out_num, in_num);
    elem->index = head;
    elem->ndescs = 1;
    for (i = 0; i < out_num; i++) {
        elem->out_addr[i] = addr[i];
        elem->out_sg[i] = iov[i];
    }
    for (i = 0; i < in_num; i++) {
        elem->in_addr[i] = addr[out_num + i];
        elem->in_sg[i] = iov[out_num + i];
    }

    vq->inuse++;

done:
    return elem;

err_undo_map:
    goto done;
}

void *virtqueue_pop(VirtQueue *vq, size_t sz)
{
    return virtqueue_split_pop(vq, sz);
}

bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
                               unsigned int *head)
{

    /*
     * Grab the next descriptor number they're advertising, and increment
     * the index we've seen.
     */
    *head = vring_avail_ring(vq, idx % vq->vring.num);

    /* If their number is silly, that's a fatal mistake. */
    if (*head >= vq->vring.num) {
        DBG("Guest says index %u is available", *head);
        return false;
    }

    return true;
}

uint32_t get_vqs_max_size(VirtIODevice *vdev)
{
    uint32_t vq_max_size = VIRTQUEUE_MAX_SIZE;
    uint32_t total_size, temp_size, total_p2 = 1;
    int i, log_res = 0;

    total_size = VIRTQUEUE_MAX_SIZE * sizeof(VRingDesc);
    total_size += offsetof(VRingAvail, ring) +
                   VIRTQUEUE_MAX_SIZE * sizeof(uint16_t);
    total_size += offsetof(VRingUsed, ring) +
                   VIRTQUEUE_MAX_SIZE * sizeof(uint16_t);

    temp_size = total_size;

    /* Compute log2 of total_size (Needs to be power of 2) */
    while ((temp_size /= 2) > 0) {
        log_res++;
        total_p2 *= 2;
    }

    /* if total_size is not a power of 2: (total_size > 8) -> 16 */
    if (total_size > total_p2) {
        total_size = 2 * total_p2;
    }

    /*
     * Align to page size:  This needed only in case total_size
     * is less than 4096 (PAGE_SIZE)
     */
    if (total_size % PAGE_SIZE > 0) {
        total_size = (total_size / PAGE_SIZE) * PAGE_SIZE + PAGE_SIZE;
    }

    DBG("Total vqs size to mmap is: %u\n", total_size);

    return total_size;
}

int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
{
    uint16_t num_heads = vring_avail_idx(vq) - idx;

    /* Check it isn't doing very strange things with descriptor numbers. */
    if (num_heads > vq->vring.num) {
        DBG("Guest moved used index from %u to %u",
                     idx, vq->shadow_avail_idx);
        return -EINVAL;
    }

    return num_heads;
}

int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
                                          unsigned int max, unsigned int *next)
{
    /* If this descriptor says it doesn't chain, we're done. */
    if (!(desc->flags & VRING_DESC_F_NEXT)) {
        return VIRTQUEUE_READ_DESC_DONE;
    }

    /* Check they're not leading us off end of descriptors. */
    *next = desc->next;

    if (*next >= max) {
        DBG("Desc next is %u", *next);
        return VIRTQUEUE_READ_DESC_ERROR;
    }

    desc = (VRingDesc *)desc + *next;
    return VIRTQUEUE_READ_DESC_MORE;
}


static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
                            unsigned int *in_bytes, unsigned int *out_bytes,
                            unsigned max_in_bytes, unsigned max_out_bytes)
{
    VirtIODevice *vdev = vq->vdev;
    unsigned int max, idx;
    unsigned int total_bufs, in_total, out_total;
    int64_t len = 0;
    int rc;

    idx = vq->last_avail_idx;
    total_bufs = in_total = out_total = 0;

    max = vq->vring.num;

    while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
        unsigned int num_bufs;
        VRingDesc *desc;
        unsigned int i;

        num_bufs = total_bufs;

        if (!virtqueue_get_head(vq, idx++, &i)) {
            goto err;
        }

        /* there is no need to copy anything form the cache struct */
        desc = (VRingDesc *)vq->vring.desc + i;

        if (desc->flags & VRING_DESC_F_INDIRECT) {
            if (!desc->len || (desc->len % sizeof(VRingDesc))) {
                DBG("Invalid size for indirect buffer table\n");
                goto err;
            }

            /* If we've got too many, that implies a descriptor loop. */
            if (num_bufs >= max) {
                goto err;
            }
        }

        do {
            /* If we've got too many, that implies a descriptor loop. */
            if (++num_bufs > max) {
                goto err;
            }

            if (desc->flags & VRING_DESC_F_WRITE) {
                in_total += desc->len;
            } else {
                out_total += desc->len;
            }
            if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
                goto done;
            }

            rc = virtqueue_split_read_next_desc(vdev, desc, max, &i);
        } while (rc == VIRTQUEUE_READ_DESC_MORE);

        if (rc == VIRTQUEUE_READ_DESC_ERROR) {
            goto err;
        }

        total_bufs = num_bufs;
    }

    if (rc < 0) {
        goto err;
    }

done:
    if (in_bytes) {
        *in_bytes = in_total;
    }
    if (out_bytes) {
        *out_bytes = out_total;
    }
    return;

err:
    in_total = out_total = 0;
    goto done;
}

void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
                               unsigned int *out_bytes,
                               unsigned max_in_bytes, unsigned max_out_bytes)
{
    if (!vq->vring.desc) {
        goto err;
    }

    virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
                                    max_in_bytes, max_out_bytes);

    return;
err:
    if (in_bytes) {
        *in_bytes = 0;
    }
    if (out_bytes) {
        *out_bytes = 0;
    }
}

void print_neg_flag(uint64_t neg_flag, bool read)
{
    if (read) {
        DBG("Read:\n\t");
    } else {
        DBG("Write:\n\t");
    }

    switch (neg_flag) {
    case VIRTIO_MMIO_MAGIC_VALUE:           /* 0x000 */
        DBG("VIRTIO_MMIO_MAGIC_VALUE\n");
        break;
    case VIRTIO_MMIO_VERSION:               /* 0x004 */
        DBG("VIRTIO_MMIO_VERSION\n");
        break;
    case VIRTIO_MMIO_DEVICE_ID:             /* 0x008 */
        DBG("VIRTIO_MMIO_DEVICE_ID\n");
        break;
    case VIRTIO_MMIO_VENDOR_ID:             /* 0x00c */
        DBG("VIRTIO_MMIO_VENDOR_ID\n");
        break;
    case VIRTIO_MMIO_DEVICE_FEATURES:       /* 0x010 */
        DBG("VIRTIO_MMIO_DEVICE_FEATURES\n");
        break;
    case VIRTIO_MMIO_DEVICE_FEATURES_SEL:   /* 0x014 */
        DBG("VIRTIO_MMIO_DEVICE_FEATURES_SEL\n");
        break;
    case VIRTIO_MMIO_DRIVER_FEATURES:       /* 0x020 */
        DBG("VIRTIO_MMIO_DRIVER_FEATURES\n");
        break;
    case VIRTIO_MMIO_DRIVER_FEATURES_SEL:   /* 0x024 */
        DBG("VIRTIO_MMIO_DRIVER_FEATURES_SEL\n");
        break;
    case VIRTIO_MMIO_GUEST_PAGE_SIZE:       /* 0x028 */
        DBG("VIRTIO_MMIO_GUEST_PAGE_SIZE\n");
        break;
    case VIRTIO_MMIO_QUEUE_SEL:             /* 0x030 */
        DBG("VIRTIO_MMIO_QUEUE_SEL\n");
        break;
    case VIRTIO_MMIO_QUEUE_NUM_MAX:         /* 0x034 */
        DBG("VIRTIO_MMIO_QUEUE_NUM_MAX\n");
        break;
    case VIRTIO_MMIO_QUEUE_NUM:             /* 0x038 */
        DBG("VIRTIO_MMIO_QUEUE_NUM\n");
        break;
    case VIRTIO_MMIO_QUEUE_ALIGN:           /* 0x03c */
        DBG("VIRTIO_MMIO_QUEUE_ALIGN\n");
        break;
    case VIRTIO_MMIO_QUEUE_PFN:             /* 0x040 */
        DBG("VIRTIO_MMIO_QUEUE_PFN\n");
        break;
    case VIRTIO_MMIO_QUEUE_READY:           /* 0x044 */
        DBG("VIRTIO_MMIO_QUEUE_READY\n");
        break;
    case VIRTIO_MMIO_QUEUE_NOTIFY:          /* 0x050 */
        DBG("VIRTIO_MMIO_QUEUE_NOTIFY\n");
        break;
    case VIRTIO_MMIO_INTERRUPT_STATUS:      /* 0x060 */
        DBG("VIRTIO_MMIO_INTERRUPT_STATUS\n");
        break;
    case VIRTIO_MMIO_INTERRUPT_ACK:         /* 0x064 */
        DBG("VIRTIO_MMIO_INTERRUPT_ACK\n");
        break;
    case VIRTIO_MMIO_STATUS:                /* 0x070 */
        DBG("VIRTIO_MMIO_STATUS\n");
        break;
    case VIRTIO_MMIO_QUEUE_DESC_LOW:        /* 0x080 */
        DBG("VIRTIO_MMIO_QUEUE_DESC_LOW\n");
        break;
    case VIRTIO_MMIO_QUEUE_DESC_HIGH:       /* 0x084 */
        DBG("VIRTIO_MMIO_QUEUE_DESC_HIGH\n");
        break;
    case VIRTIO_MMIO_QUEUE_AVAIL_LOW:       /* 0x090 */
        DBG("VIRTIO_MMIO_QUEUE_AVAIL_LOW\n");
        break;
    case VIRTIO_MMIO_QUEUE_AVAIL_HIGH:      /* 0x094 */
        DBG("VIRTIO_MMIO_QUEUE_AVAIL_HIGH\n");
        break;
    case VIRTIO_MMIO_QUEUE_USED_LOW:        /* 0x0a0 */
        DBG("VIRTIO_MMIO_QUEUE_USED_LOW\n");
        break;
    case VIRTIO_MMIO_QUEUE_USED_HIGH:       /* 0x0a4 */
        DBG("VIRTIO_MMIO_QUEUE_USED_HIGH\n");
        break;
    case VIRTIO_MMIO_SHM_SEL:               /* 0x0ac */
        DBG("VIRTIO_MMIO_SHM_SEL\n");
        break;
    case VIRTIO_MMIO_SHM_LEN_LOW:           /* 0x0b0 */
        DBG("VIRTIO_MMIO_SHM_LEN_LOW\n");
        break;
    case VIRTIO_MMIO_SHM_LEN_HIGH:          /* 0x0b4 */
        DBG("VIRTIO_MMIO_SHM_LEN_HIGH\n");
        break;
    case VIRTIO_MMIO_SHM_BASE_LOW:          /* 0x0b8 */
        DBG("VIRTIO_MMIO_SHM_BASE_LOW\n");
        break;
    case VIRTIO_MMIO_SHM_BASE_HIGH:         /* 0x0bc */
        DBG("VIRTIO_MMIO_SHM_BASE_HIGH\n");
        break;
    case VIRTIO_MMIO_CONFIG_GENERATION:     /* 0x0fc */
        DBG("VIRTIO_MMIO_CONFIG_GENERATION\n");
        break;
    default:
        if (neg_flag >= VIRTIO_MMIO_CONFIG) {
            DBG("\tVIRTIO_MMIO_CONFIG\n");
        } else {
            DBG("\tNegotiation flag Unknown: %ld\n", neg_flag);
        }
        return;
    }


}

int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
{
    bool bad = (val & ~(vdev->host_features)) != 0;

    val &= vdev->host_features;

    vdev->guest_features = val;
    return bad ? -1 : 0;
}

int virtio_set_features(VirtIODevice *vdev, uint64_t val)
{
    int ret;
    /*
     * The driver must not attempt to set features after feature negotiation
     * has finished.
     */
    if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
        DBG("virtio_set_features: vdev->status "
            "& VIRTIO_CONFIG_S_FEATURES_OK\n");
        return -EINVAL;
    }
    ret = virtio_set_features_nocheck(vdev, val);
    return ret;
}


/* TODO: MMIO notifiers -- This might not be needed anymore  */
static void virtio_queue_guest_notifier_read(EventNotifier *n)
{
    VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
    if (event_notifier_test_and_clear(n)) {
        virtio_irq(vq);
    }
}


int eventfd_count = 0;




void *loopback_event_select(void *_e)
{
    int retval;
    uint64_t eftd_ctr;
    fd_set rfds;
    int s;
    EventNotifier *e = (EventNotifier *)_e;
    int rfd = e->rfd;
    VirtQueue *vq = container_of(e, VirtQueue, guest_notifier);

    DBG("\nWaiting event from vhost-user-device\n");

    FD_ZERO(&rfds);
    FD_SET(rfd, &rfds);

    while (1) {

        retval = select(rfd + 1, &rfds, NULL, NULL, NULL);

        if (retval == -1) {
            DBG("select() error. Exiting...\n");
            exit(1);
        }
        if (retval > 0) {

            DBG("\n\nEvent has come from the vhost-user-device "
                "(eventfd: %d) -> event_count: %d\n\n",
                                    rfd, eventfd_count);

            eventfd_count++;
            if (event_notifier_test_and_clear(e)) {
                virtio_irq(vq);
            }

        }
    }
}


void event_notifier_set_handler(EventNotifier *e,
                                void *handler)
{
    int ret;
    pthread_t thread_id;

    if (e->wfd > 0) {
        ret = pthread_create(&thread_id, NULL, loopback_event_select,
                             (void *)e);
        if (ret != 0) {
            exit(1);
        }
    }
}


void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
                                                bool with_irqfd)
{
    if (assign && !with_irqfd) {
        event_notifier_set_handler(&vq->guest_notifier,
                                   virtio_queue_guest_notifier_read);
    } else {
        event_notifier_set_handler(&vq->guest_notifier, NULL);
    }
    if (!assign) {
        /*
         * Test and clear notifier before closing it,
         * in case poll callback didn't have time to run.
         */
        virtio_queue_guest_notifier_read(&vq->guest_notifier);
    }
}

EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
{
    return &vq->guest_notifier;
}

int virtio_loopback_set_guest_notifier(VirtIODevice *vdev, int n, bool assign,
                                          bool with_irqfd)
{
    VirtioDeviceClass *vdc = vdev->vdev_class;
    VirtQueue *vq = virtio_get_queue(vdev, n);
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
    } else {
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
    }

    return 0;
}

int virtio_loopback_set_guest_notifiers(VirtIODevice *vdev, int nvqs,
                                           bool assign)
{
    bool with_irqfd = false;
    int r, n;

    nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);

    for (n = 0; n < nvqs; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

        r = virtio_loopback_set_guest_notifier(vdev, n, assign, with_irqfd);
        if (r < 0) {
            goto assign_error;
        }
    }

    return 0;

assign_error:
    DBG("Error return virtio_loopback_set_guest_notifiers\n");
    return r;
}

EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
{
    return &vq->host_notifier;
}

void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
{
    vq->host_notifier_enabled = enabled;
}

int virtio_bus_set_host_notifier(VirtioBus *vbus, int n, bool assign)
{
    VirtIODevice *vdev = vbus->vdev;
    VirtQueue *vq = virtio_get_queue(vdev, n);

    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
    int r = 0;


    if (!vbus->ioeventfd_assign) {
        return -ENOSYS;
    }

    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
            DBG("unable to init event notifier: %d", r);
            return r;
        }
        r = vbus->ioeventfd_assign(proxy, notifier, n, true);
        if (r < 0) {
            DBG("unable to assign ioeventfd: %d", r);
        }
    } else {
        vbus->ioeventfd_assign(proxy, notifier, n, false);
    }

    if (r == 0) {
        virtio_queue_set_host_notifier_enabled(vq, assign);
    }

    return r;
}



/* On success, ioeventfd ownership belongs to the caller.  */
int virtio_bus_grab_ioeventfd(VirtioBus *bus)
{
    /*
     * vhost can be used even if ioeventfd=off in the proxy device,
     * so do not check k->ioeventfd_enabled.
     */
    if (!bus->ioeventfd_assign) {
        return -ENOSYS;
    }

    if (bus->ioeventfd_grabbed == 0 && bus->ioeventfd_started) {
        /*
         * Remember that we need to restart ioeventfd
         * when ioeventfd_grabbed becomes zero.
         */
        bus->ioeventfd_started = true;
    }
    bus->ioeventfd_grabbed++;
    return 0;
}

int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
{
    return virtio_bus_grab_ioeventfd(vdev->vbus);
}

bool virtio_device_disabled(VirtIODevice *vdev)
{
    return vdev->disabled || vdev->broken;
}

int prev_level = 0;

void *my_notify(void *data) {

    int irq_num = 44;
    (void) data;
    (void) ioctl(fd, IRQ, &irq_num);

    pthread_exit(NULL);
}

int int_count = 0;

void virtio_loopback_update_irq(VirtIODevice *vdev)
{
    int level, irq_num = 44;
    pthread_t my_thread_id;

    if (!vdev) {
        return;
    }

    level = (vdev->isr != 0);

    DBG("level: %d\n", level);
    DBG("prev_level: %d\n", prev_level);

    if (!((level == 1) && (prev_level == 0))) {
        prev_level = level;
        return;
    }
    prev_level = level;

    DBG("Trigger interrupt (ioctl)\n");
    DBG("Interrupt counter: %d\n", int_count++);

    //(void)pthread_create(&my_thread_id, NULL, my_notify, NULL);
    (void) ioctl(fd, IRQ, &irq_num);

}

bool enable_virtio_interrupt = false;

/* virtio device */
void virtio_notify_vector(VirtIODevice *vdev)
{

    /* TODO: Check if this is still needed */
    if (virtio_device_disabled(vdev)) {
        DBG("Device is disabled\n");
        return;
    }

    virtio_loopback_update_irq(vdev);

    /*
     * TODO: substitue the previous line with the
     *       following when it's implemented
     *
     * if (k->notify) {
     *     k->notify(qbus->parent, vector);
     * }
     */
}

void virtio_update_irq(VirtIODevice *vdev)
{
    virtio_notify_vector(vdev);
}

void virtio_queue_notify(VirtIODevice *vdev, int n)
{
    VirtQueue *vq = &vdev->vq[n];

    DBG("virtio_queue_notify(..., vq_n: %d)\n", n);

    if (!vq->vring.desc || vdev->broken) {
        DBG("virtio_queue_notify: broken\n");
        return;
    }

    if (vq->host_notifier_enabled) {
        DBG("vq->host_notifier_enabled\n");
        event_notifier_set(&vq->host_notifier);
    } else if (vq->handle_output) {
        DBG("vq->handle_output\n");
        vq->handle_output(vdev, vq);

        if (vdev->start_on_kick) {
            virtio_set_started(vdev, true);
        }
    }
}


uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint8_t val;

    if (addr + sizeof(val) > vdev->config_len) {
        DBG("virtio_config_readb failed\n");
        return (uint32_t)-1;
    }

    k->get_config(vdev, vdev->config);

    memcpy(&val, (uint8_t *)(vdev->config + addr), sizeof(uint8_t));

    return val;
}

uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint16_t val;

    if (addr + sizeof(val) > vdev->config_len) {
        DBG("virtio_config_readw failed\n");
        return (uint32_t)-1;
    }

    k->get_config(vdev, vdev->config);

    memcpy(&val, (uint16_t *)(vdev->config + addr), sizeof(uint16_t));
    return val;
}

uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint32_t val;

    if (addr + sizeof(val) > vdev->config_len) {
        DBG("virtio_config_readl failed\n");
        return (uint32_t)-1;
    }

    k->get_config(vdev, vdev->config);

    memcpy(&val, (uint32_t *)(vdev->config + addr), sizeof(uint32_t));
    return val;
}

void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint8_t val = data;

    if (addr + sizeof(val) > vdev->config_len) {
        return;
    }

    memcpy((uint8_t *)(vdev->config + addr), &val, sizeof(uint8_t));

    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
}

void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint16_t val = data;

    if (addr + sizeof(val) > vdev->config_len) {
        return;
    }

    memcpy((uint16_t *)(vdev->config + addr), &val, sizeof(uint16_t));

    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
}

void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
{
    VirtioDeviceClass *k = vdev->vdev_class;
    uint32_t val = data;

    if (addr + sizeof(val) > vdev->config_len) {
        return;
    }

    memcpy((uint32_t *)(vdev->config + addr), &val, sizeof(uint32_t));

    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
}



static uint64_t virtio_loopback_read(VirtIODevice *vdev, uint64_t offset,
                                 unsigned size)
{

    uint64_t ret;

    print_neg_flag(offset, 1);

    if (!vdev) {
        /*
         * If no backend is present, we treat most registers as
         * read-as-zero, except for the magic number, version and
         * vendor ID. This is not strictly sanctioned by the virtio
         * spec, but it allows us to provide transports with no backend
         * plugged in which don't confuse Linux's virtio code: the
         * probe won't complain about the bad magic number, but the
         * device ID of zero means no backend will claim it.
         */
        switch (offset) {
        case VIRTIO_MMIO_MAGIC_VALUE:
            return VIRT_MAGIC;
        case VIRTIO_MMIO_VERSION:
            if (proxy->legacy) {
                return VIRT_VERSION_LEGACY;
            } else {
                return VIRT_VERSION;
            }
        case VIRTIO_MMIO_VENDOR_ID:
            return VIRT_VENDOR;
        default:
            return 0;
        }
    }

    if (offset >= VIRTIO_MMIO_CONFIG) {
        offset -= VIRTIO_MMIO_CONFIG;

        /* TODO: To be implemented */
        DBG("VIRTIO_MMIO_CONFIG: size: %u, offset: %lu\n", size, offset);

        if (proxy->legacy) {
            switch (size) {
            case 1:
                ret = virtio_config_readb(vdev, offset);
                break;
            case 2:
                ret = virtio_config_readw(vdev, offset);
                break;
            case 4:
                ret = virtio_config_readl(vdev, offset);
                break;
            default:
                abort();
            }
            DBG("VIRTIO_MMIO_CONFIG: ret: %lu\n", ret);
            return ret;
        }

        return 4;
    }

    if (size != 4) {
        DBG("wrong size access to register!\n");
        return 0;
    }

    switch (offset) {
    case VIRTIO_MMIO_MAGIC_VALUE:
        return VIRT_MAGIC;
    case VIRTIO_MMIO_VERSION:
        if (proxy->legacy) {
            return VIRT_VERSION_LEGACY;
        } else {
            return VIRT_VERSION;
        }
    case VIRTIO_MMIO_DEVICE_ID:
        return vdev->device_id;
    case VIRTIO_MMIO_VENDOR_ID:
        return VIRT_VENDOR;
    case VIRTIO_MMIO_DEVICE_FEATURES:
        if (proxy->legacy) {
            if (proxy->host_features_sel) {
                DBG("attempt to read host features with "
                       "host_features_sel > 0 in legacy mode\n");
                DBG("vdev->host_features: 0x%lx\n", vdev->host_features);
                return 0;
            } else {
                DBG("vdev->host_features: 0x%lx\n", vdev->host_features);
                return vdev->host_features;
            }
        } else {
             /* TODO: To be implemented */
        }
    case VIRTIO_MMIO_QUEUE_NUM_MAX:
        /* TODO: To be implemented */
        return VIRTQUEUE_MAX_SIZE;
    case VIRTIO_MMIO_QUEUE_PFN:
        if (!proxy->legacy) {
            DBG("VIRTIO_MMIO_QUEUE_PFN: read from legacy register (0x%lx) "
                "in non-legacy mode\n", offset);
            return 0;
        }
        return virtio_queue_get_addr(vdev, vdev->queue_sel) >>
                                            proxy->guest_page_shift;

    case VIRTIO_MMIO_QUEUE_READY:
        if (proxy->legacy) {
            DBG("VIRTIO_MMIO_QUEUE_READY: read from legacy register (0x%lx) "
                "in non-legacy mode\n", offset);
            return 0;
        }
        /* TODO: To be implemented */
    case VIRTIO_MMIO_INTERRUPT_STATUS:
        return vdev->isr;
    case VIRTIO_MMIO_STATUS:
        return vdev->status;
    case VIRTIO_MMIO_CONFIG_GENERATION:
        if (proxy->legacy) {
            DBG("VIRTIO_MMIO_CONFIG_GENERATION: read from legacy "
                "register (0x%lx) in non-legacy mode\n", offset);
            return 0;
        }
        return vdev->generation;
    case VIRTIO_MMIO_SHM_LEN_LOW:
    case VIRTIO_MMIO_SHM_LEN_HIGH:
        /*
         * VIRTIO_MMIO_SHM_SEL is unimplemented
         * according to the linux driver, if region length is -1
         * the shared memory doesn't exist
         */
        return -1;
    case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
    case VIRTIO_MMIO_DRIVER_FEATURES:
    case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
    case VIRTIO_MMIO_GUEST_PAGE_SIZE:
    case VIRTIO_MMIO_QUEUE_SEL:
    case VIRTIO_MMIO_QUEUE_NUM:
    case VIRTIO_MMIO_QUEUE_ALIGN:
    case VIRTIO_MMIO_QUEUE_NOTIFY:
    case VIRTIO_MMIO_INTERRUPT_ACK:
    case VIRTIO_MMIO_QUEUE_DESC_LOW:
    case VIRTIO_MMIO_QUEUE_DESC_HIGH:
    case VIRTIO_MMIO_QUEUE_AVAIL_LOW:
    case VIRTIO_MMIO_QUEUE_AVAIL_HIGH:
    case VIRTIO_MMIO_QUEUE_USED_LOW:
    case VIRTIO_MMIO_QUEUE_USED_HIGH:
        DBG("VIRTIO_MMIO_QUEUE_USED_HIGH: read of write-only "
            "register (0x%lx)\n", offset);
        return 0;
    default:
        DBG("read: bad register offset (0x%lx)\n", offset);
        return 0;
    }
    return 0;
}

uint64_t vring_phys_addrs[2] = {0};
uint32_t vring_phys_addrs_idx = 0;

void virtio_loopback_write(VirtIODevice *vdev, uint64_t offset,
                       uint64_t value, unsigned size)
{

    print_neg_flag(offset, 0);

    if (!vdev) {
        /*
         * If no backend is present, we just make all registers
         * write-ignored. This allows us to provide transports with
         * no backend plugged in.
         */
        return;
    }

    if (offset >= VIRTIO_MMIO_CONFIG) {
        offset -= VIRTIO_MMIO_CONFIG;

        /* TODO: To be implemented */
        DBG("VIRTIO_MMIO_CONFIG flag write\n");

        if (proxy->legacy) {
            switch (size) {
            case 1:
                virtio_config_writeb(vdev, offset, value);
                break;
            case 2:
                virtio_config_writew(vdev, offset, value);
                break;
            case 4:
                virtio_config_writel(vdev, offset, value);
                break;
            default:
                DBG("VIRTIO_MMIO_CONFIG abort\n");
                abort();
            }
            return;
        }
        DBG("write: VIRTIO_MMIO_CONFIG\n");

        return;
    }
    if (size != 4) {
        DBG("write: wrong size access to register!\n");
        return;
    }
    switch (offset) {
    case VIRTIO_MMIO_DEVICE_FEATURES_SEL:
        if (value) {
            proxy->host_features_sel = 1;
        } else {
            proxy->host_features_sel = 0;
        }
        break;
    case VIRTIO_MMIO_DRIVER_FEATURES:
        if (proxy->legacy) {
            if (proxy->guest_features_sel) {
                DBG("attempt to write guest features with "
                       "guest_features_sel > 0 in legacy mode\n");
                DBG("Set driver features: 0x%lx\n", value);
            } else {
                DBG("Set driver features: 0x%lx\n", value);
                virtio_set_features(vdev, value);
            }
        } else {
            /* TODO: To be implemented */
        }
        break;
    case VIRTIO_MMIO_DRIVER_FEATURES_SEL:
        if (value) {
            proxy->guest_features_sel = 1;
        } else {
            proxy->guest_features_sel = 0;
        }
        break;
    case VIRTIO_MMIO_GUEST_PAGE_SIZE:
        if (!proxy->legacy) {
            DBG("write to legacy register (0x%lx"
                    ") in non-legacy mode\n", offset);
            return;
        }
        if (proxy->guest_page_shift > 31) {
            proxy->guest_page_shift = 0;
        }
        break;
    case VIRTIO_MMIO_QUEUE_SEL:
        if (value < VIRTIO_QUEUE_MAX) {
            vdev->queue_sel = value;
        }
        break;
    case VIRTIO_MMIO_QUEUE_NUM:

        virtio_queue_set_num(vdev, vdev->queue_sel, value);

        if (proxy->legacy) {
            virtio_queue_update_rings(vdev, vdev->queue_sel);
        } else {
            /* TODO: To be implemented */
            exit(1);
        }
        break;
    case VIRTIO_MMIO_QUEUE_ALIGN:
        if (!proxy->legacy) {
            DBG("write to legacy register (0x%lx) in "
                "non-legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_PFN:
        if (!proxy->legacy) {
            DBG("write to legacy register (0x%lx) in "
                "non-legacy mode\n", offset);
            return;
        }
        if (value == 0) {
            /* TODO: To be implemented */
        } else {

            DBG("desc_addr: 0x%lx\n", value);
            vring_phys_addrs[vring_phys_addrs_idx++] = value;

            uint64_t desc_addr;
            uint32_t vqs_size = get_vqs_max_size(global_vdev);

            ioctl(fd, SHARE_VQS, &vdev->queue_sel);

            desc_addr = (uint64_t)mmap(NULL, vqs_size,
                                       PROT_READ | PROT_WRITE,
                                       MAP_SHARED, fd, 0);

            virtio_queue_set_addr(vdev, vdev->queue_sel,
                                  desc_addr);
        }
        break;
    case VIRTIO_MMIO_QUEUE_READY:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_NOTIFY:
        if (value < VIRTIO_QUEUE_MAX) {
            virtio_queue_notify(vdev, value);
        }
        break;
    case VIRTIO_MMIO_INTERRUPT_ACK:
        vdev->isr = vdev->isr & ~value;
        virtio_update_irq(vdev);
        break;
    case VIRTIO_MMIO_STATUS:

        /*
         * TODO: Add it in a future release later
         *
         * if (!(value & VIRTIO_CONFIG_S_DRIVER_OK)) {
         *     virtio_loopback_stop_ioeventfd(proxy);
         * }
         */

        if (!proxy->legacy && (value & VIRTIO_CONFIG_S_FEATURES_OK)) {
            virtio_set_features(vdev,
                                ((uint64_t)proxy->guest_features[1]) << 32 |
                                proxy->guest_features[0]);
        }

        virtio_set_status(vdev, value & 0xff);

        DBG("STATUS -> %ld\n", value);

        /*
         * TODO: Check if this is still needed
         *
         * if (vdev->status == 0) {
         *     virtio_reset(vdev);
         *     virtio_loopback_soft_reset(proxy);
         * }
         */

        break;
    case VIRTIO_MMIO_QUEUE_DESC_LOW:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_DESC_HIGH:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_AVAIL_LOW:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_AVAIL_HIGH:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_USED_LOW:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_QUEUE_USED_HIGH:
        if (proxy->legacy) {
            DBG("write to non-legacy register (0x%lx) in "
                "legacy mode\n", offset);
            return;
        }
        /* TODO: To be implemented */
        break;
    case VIRTIO_MMIO_MAGIC_VALUE:
    case VIRTIO_MMIO_VERSION:
    case VIRTIO_MMIO_DEVICE_ID:
    case VIRTIO_MMIO_VENDOR_ID:
    case VIRTIO_MMIO_DEVICE_FEATURES:
    case VIRTIO_MMIO_QUEUE_NUM_MAX:
    case VIRTIO_MMIO_INTERRUPT_STATUS:
    case VIRTIO_MMIO_CONFIG_GENERATION:
        /* TODO: To be implemented */
        break;
    default:
        DBG("bad register offset (0x%lx)\n", offset);
    }
}

VirtIODevice *global_vdev;
VirtioBus *global_vbus;

void adapter_read_write_cb(void)
{
    /*
     * Enabling the next line, all the incoming
     * read/write events will be printed:
     *
     * print_neg_flag (address->notification, address->read);
     */

    if (address->read) {
        address->data = virtio_loopback_read(global_vdev,
                                         address->notification, address->size);
    } else {
        virtio_loopback_write(global_vdev, address->notification,
                                       address->data, address->size);
    }

    DBG("Return to the driver\n");

    /*
     * Note the driver that we have done
     * All the required actions.
     */
    (void)ioctl(fd, WAKEUP);

}

void *notify_select(void *data)
{
    int retval;
    fd_set rfds;
    int efd = *(int *)data;

    DBG("\nWaiting for loopback notify events\n");

    FD_ZERO(&rfds);
    FD_SET(efd, &rfds);

    while (1) {

        retval = select(efd + 1, &rfds, NULL, NULL, NULL);

        if (retval > 0) {
            s = read(efd, &eftd_ctr, sizeof(uint64_t));
            if (s != sizeof(uint64_t)) {
                DBG("Eventfd read error\n");
                exit(1);
            } else {
                //rcu_read_lock();
                virtio_queue_notify(global_vdev, 0);
                //rcu_read_unlock();
            }
        }
    }
}

void *driver_event_select(void *data)
{
    int retval;
    int efd = *(int *)data;

    DBG("\nWaiting for loopback read/write events\n");

    FD_ZERO(&rfds);
    FD_SET(efd, &rfds);

    while (1) {

        retval = select(efd + 1, &rfds, NULL, NULL, NULL);

        if (retval == -1) {
            DBG("\nselect() error. Exiting...");
            exit(EXIT_FAILURE);
        } else if (retval > 0) {

            s = read(efd, &eftd_ctr, sizeof(uint64_t));
            if (s != sizeof(uint64_t)) {
                DBG("\neventfd read error. Exiting...");
                exit(1);
            } else {
                adapter_read_write_cb();
            }

        } else if (retval == 0) {
            DBG("\nselect() says that no data was available");
        }
    }
}

void create_rng_struct(void)
{
    device_info.magic = 0x74726976;
    device_info.version = 0x1;
    device_info.device_id = 0x4;
    device_info.vendor = 0x554d4551;
}

VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
{
    return vdev->vq + n;
}

VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
                            VirtIOHandleOutput handle_output)
{
    int i;

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        if (vdev->vq[i].vring.num == 0) {
            break;
        }
    }

    if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) {
        exit(1);
    }

    vdev->vq[i].vring.num = queue_size;
    vdev->vq[i].vring.num_default = queue_size;
    vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
    vdev->vq[i].handle_output = handle_output;
    vdev->vq[i].used_elems = (VirtQueueElement *)malloc(sizeof(VirtQueueElement)
                                                        * queue_size);

    return &vdev->vq[i];
}

void virtio_dev_init(VirtIODevice *vdev, const char *name,
                 uint16_t device_id, size_t config_size)
{
    int i;

    vdev->start_on_kick = false;
    vdev->started = false;
    vdev->device_id = device_id;
    vdev->status = 0;
    vdev->queue_sel = 0;
    vdev->config_vector = VIRTIO_NO_VECTOR;
    vdev->vq = (VirtQueue *) malloc(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
    vdev->vm_running = false;
    vdev->broken = false;
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        vdev->vq[i].vector = VIRTIO_NO_VECTOR;
        vdev->vq[i].vdev = vdev;
        vdev->vq[i].queue_index = i;
        vdev->vq[i].host_notifier_enabled = false;
    }

    vdev->name = name;
    vdev->config_len = config_size;
    if (vdev->config_len) {
        vdev->config = (void *) malloc(config_size);
    } else {
        vdev->config = NULL;
    }

    vdev->use_guest_notifier_mask = true;
}

static bool virtio_loopback_ioeventfd_enabled(VirtIODevice *d)
{
    return (proxy->flags & VIRTIO_IOMMIO_FLAG_USE_IOEVENTFD) != 0;
}

/* TODO: This function might not be needed anymore */
static int virtio_loopback_ioeventfd_assign(VirtIOMMIOProxy *d,
                                        EventNotifier *notifier,
                                        int n, bool assign)
{
    return 0;
}

bool virtio_bus_device_iommu_enabled(VirtIODevice *vdev)
{
    VirtioBus *k = vdev->vbus;

    if (!k->iommu_enabled) {
        return false;
    }

    return k->iommu_enabled(vdev);
}

void virtio_loopback_bus_init(VirtioBus *k)
{
    DBG("virtio_loopback_bus_init(...)\n");
    k->set_guest_notifiers = virtio_loopback_set_guest_notifiers;
    k->ioeventfd_enabled = virtio_loopback_ioeventfd_enabled;
    k->ioeventfd_assign = virtio_loopback_ioeventfd_assign;
}


int virtio_loopback_start(void)
{
    efd_data_t info;
    pthread_t thread_id;
    pthread_t thread_id_notify;
    int ret = -1;
    int flags;

    fd = open("/dev/loopback", O_RDWR);
    if (fd < 0) {
        perror("Open call failed");
        return -1;
    }
    loopback_fd = fd;

    /* Create eventfd */
    efd = eventfd(0, 0);
    if (efd == -1) {
        DBG("\nUnable to create eventfd! Exiting...\n");
        exit(EXIT_FAILURE);
    }

    efd_notify = eventfd(0, 0);
    if (efd_notify == -1) {
        DBG("\nUnable to create eventfd! Exiting...\n");
        exit(EXIT_FAILURE);
    }

    info.pid = getpid();
    info.efd[0] = efd;
    info.efd[1] = efd_notify;

    /*
     * Send the appropriate information to the driver
     * so to be able to trigger an eventfd
     */
    (void)ioctl(fd, EFD_INIT, &info);

    /* Map notification mechanism */
    /* Multiple mmaps: /dev/loopback-0/vqs, /dev/loopback-0/ctlr */
    address = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (address == MAP_FAILED) {
        perror("mmap operation failed");
        return -1;
    }

    /* Wait the eventfd */
    ret = pthread_create(&thread_id, NULL, driver_event_select, (void *)&efd);
    if (ret != 0) {
        exit(1);
    }

    /* Wait the eventfd */
    ret = pthread_create(&thread_id_notify, NULL, notify_select, (void *)&efd_notify);
    if (ret != 0) {
        exit(1);
    }

    /* Start loopback transport */
    (void)ioctl(fd, START_LOOPBACK, &device_info);

    ret = pthread_join(thread_id, NULL);
    if (ret != 0) {
        exit(1);
    }

    ret = pthread_join(thread_id_notify, NULL);
    if (ret != 0) {
        exit(1);
    }

    DBG("\nClosing eventfd. Exiting...\n");
    close(efd);

    exit(EXIT_SUCCESS);
}