shuru-linux 0.4.1

KVM backend for shuru
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
use std::io;
use std::os::fd::{AsRawFd, RawFd};
use std::sync::atomic::{fence, AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use kvm_ioctls::{IoEventAddress, VmFd};
use vm_memory::{Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};
use vmm_sys_util::eventfd::EventFd;

use super::layout;

// ===========================================================================
// MMIO device traits + bus
// ===========================================================================

/// A device whose registers are guarded as a whole.
pub trait MmioDevice: Send {
    fn read(&mut self, offset: u64, data: &mut [u8]);
    fn write(&mut self, offset: u64, data: &[u8]);
}

/// What the bus dispatches to. Every device does its own locking, so vCPUs
/// touching different devices never wait on each other.
pub trait MmioHandler: Send + Sync {
    fn read(&self, offset: u64, data: &mut [u8]);
    fn write(&self, offset: u64, data: &[u8]);
}

impl<T: MmioDevice> MmioHandler for Mutex<T> {
    fn read(&self, offset: u64, data: &mut [u8]) {
        self.lock().unwrap().read(offset, data);
    }

    fn write(&self, offset: u64, data: &[u8]) {
        self.lock().unwrap().write(offset, data);
    }
}

pub struct MmioEntry {
    pub base: u64,
    pub size: u64,
    pub device: Box<dyn MmioHandler>,
}

pub struct MmioBus {
    pub devices: Vec<MmioEntry>,
}

impl MmioBus {
    pub fn new() -> Self {
        MmioBus {
            devices: Vec::new(),
        }
    }

    pub fn add(&mut self, base: u64, size: u64, device: Box<dyn MmioHandler>) {
        self.devices.push(MmioEntry { base, size, device });
    }

    pub fn handle_read(&self, addr: u64, data: &mut [u8]) {
        for entry in &self.devices {
            if addr >= entry.base && addr < entry.base + entry.size {
                entry.device.read(addr - entry.base, data);
                return;
            }
        }
        for b in data.iter_mut() {
            *b = 0;
        }
    }

    pub fn handle_write(&self, addr: u64, data: &[u8]) {
        for entry in &self.devices {
            if addr >= entry.base && addr < entry.base + entry.size {
                entry.device.write(addr - entry.base, data);
                return;
            }
        }
    }
}

// ===========================================================================
// PL011 UART (minimal — transmit + basic RX for console mode)
// ===========================================================================

const UARTDR: u64 = 0x000;
const UARTFR: u64 = 0x018;
const UARTIBRD: u64 = 0x024;
const UARTFBRD: u64 = 0x028;
const UARTLCR_H: u64 = 0x02C;
const UARTCR: u64 = 0x030;
const UARTIMSC: u64 = 0x038;
const UARTICR: u64 = 0x048;

const UARTPERIPHID0: u64 = 0xFE0;
const UARTPERIPHID1: u64 = 0xFE4;
const UARTPERIPHID2: u64 = 0xFE8;
const UARTPERIPHID3: u64 = 0xFEC;
const UARTPCELLID0: u64 = 0xFF0;
const UARTPCELLID1: u64 = 0xFF4;
const UARTPCELLID2: u64 = 0xFF8;
const UARTPCELLID3: u64 = 0xFFC;

const UARTFR_TXFE: u32 = 1 << 7;
const UARTFR_RXFE: u32 = 1 << 4;

pub struct Pl011 {
    write_fd: Option<RawFd>,
    #[allow(dead_code)]
    read_fd: Option<RawFd>,
    lcr: u32,
    cr: u32,
    imsc: u32,
    ibrd: u32,
    fbrd: u32,
}

impl Pl011 {
    pub fn new(write_fd: Option<RawFd>, read_fd: Option<RawFd>) -> Self {
        Pl011 {
            write_fd,
            read_fd,
            lcr: 0,
            cr: 0x0300,
            imsc: 0,
            ibrd: 0,
            fbrd: 0,
        }
    }

    fn read_u32(&self, offset: u64) -> u32 {
        match offset {
            UARTDR => 0,
            UARTFR => UARTFR_TXFE | UARTFR_RXFE,
            UARTIBRD => self.ibrd,
            UARTFBRD => self.fbrd,
            UARTLCR_H => self.lcr,
            UARTCR => self.cr,
            UARTIMSC => self.imsc,
            UARTPERIPHID0 => 0x11,
            UARTPERIPHID1 => 0x10,
            UARTPERIPHID2 => 0x34,
            UARTPERIPHID3 => 0x00,
            UARTPCELLID0 => 0x0D,
            UARTPCELLID1 => 0xF0,
            UARTPCELLID2 => 0x05,
            UARTPCELLID3 => 0xB1,
            _ => 0,
        }
    }

    fn write_u32(&mut self, offset: u64, val: u32) {
        match offset {
            UARTDR => {
                if let Some(fd) = self.write_fd {
                    let byte = (val & 0xFF) as u8;
                    unsafe {
                        libc::write(fd, &byte as *const u8 as *const libc::c_void, 1);
                    }
                }
            }
            UARTIBRD => self.ibrd = val,
            UARTFBRD => self.fbrd = val,
            UARTLCR_H => self.lcr = val,
            UARTCR => self.cr = val,
            UARTIMSC => self.imsc = val,
            UARTICR => {}
            _ => {}
        }
    }
}

impl MmioDevice for Pl011 {
    fn read(&mut self, offset: u64, data: &mut [u8]) {
        let val = self.read_u32(offset);
        let len = data.len().min(4);
        data[..len].copy_from_slice(&val.to_le_bytes()[..len]);
    }

    fn write(&mut self, offset: u64, data: &[u8]) {
        let mut bytes = [0u8; 4];
        let len = data.len().min(4);
        bytes[..len].copy_from_slice(&data[..len]);
        self.write_u32(offset, u32::from_le_bytes(bytes));
    }
}

// ===========================================================================
// PL031 RTC (read-only wall clock from host, no alarm/IRQ support)
// ===========================================================================

const RTCDR: u64 = 0x000; // Data register: seconds since Unix epoch
const RTCMR: u64 = 0x004; // Match register
const RTCLR: u64 = 0x008; // Load register
const RTCCR: u64 = 0x00C; // Control register (bit 0: enable)
const RTCIMSC: u64 = 0x010; // Interrupt mask
const RTCRIS: u64 = 0x014; // Raw interrupt status
const RTCMIS: u64 = 0x018; // Masked interrupt status
const RTCICR: u64 = 0x01C; // Interrupt clear

const RTCPERIPHID0: u64 = 0xFE0;
const RTCPERIPHID1: u64 = 0xFE4;
const RTCPERIPHID2: u64 = 0xFE8;
const RTCPERIPHID3: u64 = 0xFEC;
const RTCPCELLID0: u64 = 0xFF0;
const RTCPCELLID1: u64 = 0xFF4;
const RTCPCELLID2: u64 = 0xFF8;
const RTCPCELLID3: u64 = 0xFFC;

pub struct Pl031 {
    mr: u32,
    cr: u32,
    imsc: u32,
}

impl Pl031 {
    pub fn new() -> Self {
        Pl031 {
            mr: 0,
            cr: 1, // enabled
            imsc: 0,
        }
    }

    fn now_unix(&self) -> u32 {
        use std::time::{SystemTime, UNIX_EPOCH};
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs() as u32)
            .unwrap_or(0)
    }
}

impl MmioDevice for Pl031 {
    fn read(&mut self, offset: u64, data: &mut [u8]) {
        let val: u32 = match offset {
            RTCDR => self.now_unix(),
            RTCMR => self.mr,
            RTCLR => self.now_unix(),
            RTCCR => self.cr,
            RTCIMSC => self.imsc,
            RTCRIS => 0,
            RTCMIS => 0,
            // PrimeCell ID: PL031, revision 1
            RTCPERIPHID0 => 0x31,
            RTCPERIPHID1 => 0x10,
            RTCPERIPHID2 => 0x04,
            RTCPERIPHID3 => 0x00,
            RTCPCELLID0 => 0x0D,
            RTCPCELLID1 => 0xF0,
            RTCPCELLID2 => 0x05,
            RTCPCELLID3 => 0xB1,
            _ => 0,
        };
        let len = data.len().min(4);
        data[..len].copy_from_slice(&val.to_le_bytes()[..len]);
    }

    fn write(&mut self, offset: u64, data: &[u8]) {
        let mut bytes = [0u8; 4];
        let len = data.len().min(4);
        bytes[..len].copy_from_slice(&data[..len]);
        let val = u32::from_le_bytes(bytes);
        match offset {
            RTCMR => self.mr = val,
            RTCLR => {} // guest attempts to set time - ignore, host clock is authoritative
            RTCCR => self.cr = val & 1,
            RTCIMSC => self.imsc = val & 1,
            RTCICR => {}
            _ => {}
        }
    }
}

// ===========================================================================
// Virtio MMIO transport (v2)
// ===========================================================================

const VIRTIO_MMIO_MAGIC: u64 = 0x000;
const VIRTIO_MMIO_VERSION: u64 = 0x004;
const VIRTIO_MMIO_DEVICE_ID: u64 = 0x008;
const VIRTIO_MMIO_VENDOR_ID: u64 = 0x00C;
const VIRTIO_MMIO_DEV_FEATURES: u64 = 0x010;
const VIRTIO_MMIO_DEV_FEATURES_SEL: u64 = 0x014;
const VIRTIO_MMIO_DRV_FEATURES: u64 = 0x020;
const VIRTIO_MMIO_DRV_FEATURES_SEL: u64 = 0x024;
const VIRTIO_MMIO_QUEUE_SEL: u64 = 0x030;
const VIRTIO_MMIO_QUEUE_NUM_MAX: u64 = 0x034;
const VIRTIO_MMIO_QUEUE_NUM: u64 = 0x038;
const VIRTIO_MMIO_QUEUE_READY: u64 = 0x044;
const VIRTIO_MMIO_QUEUE_NOTIFY: u64 = 0x050;
const VIRTIO_MMIO_INTERRUPT_STATUS: u64 = 0x060;
const VIRTIO_MMIO_INTERRUPT_ACK: u64 = 0x064;
const VIRTIO_MMIO_STATUS: u64 = 0x070;
const VIRTIO_MMIO_QUEUE_DESC_LOW: u64 = 0x080;
const VIRTIO_MMIO_QUEUE_DESC_HIGH: u64 = 0x084;
const VIRTIO_MMIO_QUEUE_DRIVER_LOW: u64 = 0x090;
const VIRTIO_MMIO_QUEUE_DRIVER_HIGH: u64 = 0x094;
const VIRTIO_MMIO_QUEUE_DEVICE_LOW: u64 = 0x0A0;
const VIRTIO_MMIO_QUEUE_DEVICE_HIGH: u64 = 0x0A4;
const VIRTIO_MMIO_CONFIG_GENERATION: u64 = 0x0FC;
const VIRTIO_MMIO_CONFIG: u64 = 0x100;

const VIRTIO_STATUS_FEATURES_OK: u32 = 8;
const VIRTIO_STATUS_DRIVER_OK: u32 = 4;
const VRING_AVAIL_F_NO_INTERRUPT: u16 = 1;

/// Upper bound for every device's queue size, so chain walks can track
/// visited descriptors in a fixed array.
const MAX_QUEUE_SIZE: u16 = 256;

#[derive(Clone)]
pub struct VirtioQueueState {
    pub max_size: u16,
    pub size: u16,
    pub ready: bool,
    pub desc_addr: u64,
    pub avail_addr: u64,
    pub used_addr: u64,
    pub last_avail_idx: u16,
}

impl VirtioQueueState {
    fn new(max_size: u16) -> Self {
        assert!(max_size <= MAX_QUEUE_SIZE);
        VirtioQueueState {
            max_size,
            size: 0,
            ready: false,
            desc_addr: 0,
            avail_addr: 0,
            used_addr: 0,
            last_avail_idx: 0,
        }
    }

    /// The avail index to process up to, or None when the queue is not set
    /// up in a way the device can use: size outside 1..=max_size, rings
    /// outside guest memory, or an avail index more than one ring ahead.
    pub fn pending(&self, mem: &GuestMemoryMmap) -> Option<u16> {
        if !self.ready || self.size == 0 || self.size > self.max_size {
            return None;
        }
        let size = self.size as usize;
        if !mem.check_range(GuestAddress(self.desc_addr), size * 16)
            || !mem.check_range(GuestAddress(self.avail_addr), 6 + size * 2)
            || !mem.check_range(GuestAddress(self.used_addr), 6 + size * 8)
        {
            return None;
        }
        let current = avail_idx(mem, self.avail_addr);
        if current.wrapping_sub(self.last_avail_idx) > self.size {
            return None;
        }
        Some(current)
    }

    /// The descriptors of the chain at `head`, or None if it leaves the
    /// table, loops, or carries a flag outside `flags`. Only valid after
    /// `pending` accepted the queue.
    pub(super) fn chain(
        &self,
        mem: &GuestMemoryMmap,
        head: u16,
        flags: u16,
    ) -> Option<Vec<VringDesc>> {
        let mut chain = Vec::new();
        let mut seen = [false; MAX_QUEUE_SIZE as usize];
        let mut next = head;
        loop {
            if next >= self.size || seen[next as usize] {
                return None;
            }
            seen[next as usize] = true;
            let desc = read_desc(mem, self.desc_addr, next);
            if desc.flags & !flags != 0 {
                return None;
            }
            chain.push(desc);
            if desc.flags & VRING_DESC_F_NEXT == 0 {
                return Some(chain);
            }
            next = desc.next;
        }
    }

    /// Whether the driver wants an interrupt for buffers put on this used ring.
    pub fn wants_interrupt(&self, mem: &GuestMemoryMmap) -> bool {
        // The used index must be visible to the driver before its flag is read.
        fence(Ordering::SeqCst);
        mem.read_obj::<u16>(GuestAddress(self.avail_addr))
            .map_or(true, |flags| {
                u16::from_le(flags) & VRING_AVAIL_F_NO_INTERRUPT == 0
            })
    }
}

pub const INTERRUPT_USED: u32 = 1;
pub const INTERRUPT_CONFIG: u32 = 2;

/// A device interrupt line and the status register that says why it fired.
/// Both change under one lock so an acknowledgement cannot lower the line
/// over a reason raised in the meantime.
pub struct Interrupt {
    vm_fd: Arc<VmFd>,
    irq: u32,
    status: Mutex<u32>,
}

impl Interrupt {
    fn new(vm_fd: Arc<VmFd>, irq: u32) -> Self {
        Interrupt {
            vm_fd,
            irq,
            status: Mutex::new(0),
        }
    }

    pub fn raise(&self, reason: u32) {
        let mut status = self.status.lock().unwrap();
        *status |= reason;
        let _ = self.vm_fd.set_irq_line(self.irq, true);
    }

    fn status(&self) -> u32 {
        *self.status.lock().unwrap()
    }

    fn ack(&self, reasons: u32) {
        let mut status = self.status.lock().unwrap();
        *status &= !reasons;
        if *status == 0 {
            let _ = self.vm_fd.set_irq_line(self.irq, false);
        }
    }
}

pub trait VirtioBackend: Send {
    fn device_id(&self) -> u32;
    fn device_features(&self) -> u64;
    fn queue_count(&self) -> usize;
    fn queue_max_size(&self) -> u16;
    fn config_read(&self, offset: u64) -> u32;
    fn config_write(&mut self, offset: u64, value: u32);
    /// The feature set the driver accepted, once it sets FEATURES_OK.
    fn features_ok(&mut self, _features: u64) {}
    fn activate(
        &mut self,
        queues: &[VirtioQueueState],
        mem: &GuestMemoryMmap,
        interrupt: Arc<Interrupt>,
    );
    /// Returns whether any buffer went onto a used ring.
    fn process_queue(
        &mut self,
        queue_idx: u16,
        queues: &mut [VirtioQueueState],
        mem: &GuestMemoryMmap,
    ) -> bool;
    /// A backend with a worker of its own takes the queue kicks, one per
    /// queue in queue order, and the transport leaves its queues alone.
    fn take_kicks(&mut self, _kicks: &[EventFd]) -> io::Result<bool> {
        Ok(false)
    }
    fn reset(&mut self);
}

struct Core {
    backend: Box<dyn VirtioBackend>,
    queues: Vec<VirtioQueueState>,
    mem: GuestMemoryMmap,
    dev_features_sel: u32,
    drv_features: u64,
    drv_features_sel: u32,
    queue_sel: u32,
    status: u32,
    activated: bool,
}

impl Core {
    fn selected_queue(&self) -> Option<&VirtioQueueState> {
        self.queues.get(self.queue_sel as usize)
    }

    fn selected_queue_mut(&mut self) -> Option<&mut VirtioQueueState> {
        self.queues.get_mut(self.queue_sel as usize)
    }
}

/// Guest kicks land on an eventfd per queue inside KVM, so a kick never
/// leaves the kernel and never holds up the vCPU that made it. The queues
/// are served by the backend worker or by a thread per device.
pub struct VirtioMmioDevice {
    core: Arc<Mutex<Core>>,
    interrupt: Arc<Interrupt>,
    vm_fd: Arc<VmFd>,
    notify: IoEventAddress,
    kicks: Vec<EventFd>,
    stop: EventFd,
    worker: Option<std::thread::JoinHandle<()>>,
}

impl VirtioMmioDevice {
    pub fn new(
        backend: Box<dyn VirtioBackend>,
        base: u64,
        spi: u32,
        vm_fd: Arc<VmFd>,
        mem: GuestMemoryMmap,
    ) -> io::Result<Self> {
        let queue_count = backend.queue_count();
        let queue_max = backend.queue_max_size();
        let mut device = VirtioMmioDevice {
            core: Arc::new(Mutex::new(Core {
                backend,
                queues: (0..queue_count)
                    .map(|_| VirtioQueueState::new(queue_max))
                    .collect(),
                mem,
                dev_features_sel: 0,
                drv_features: 0,
                drv_features_sel: 0,
                queue_sel: 0,
                status: 0,
                activated: false,
            })),
            // KVM_IRQ_LINE on ARM: SPI N maps to intid N+32
            interrupt: Arc::new(Interrupt::new(vm_fd.clone(), layout::spi_to_irq(spi))),
            vm_fd,
            notify: IoEventAddress::Mmio(base + VIRTIO_MMIO_QUEUE_NOTIFY),
            kicks: Vec::with_capacity(queue_count),
            stop: EventFd::new(libc::EFD_CLOEXEC)?,
            worker: None,
        };
        for index in 0..queue_count {
            let kick = EventFd::new(libc::EFD_NONBLOCK | libc::EFD_CLOEXEC)?;
            device
                .vm_fd
                .register_ioevent(&kick, &device.notify, index as u32)
                .map_err(io::Error::other)?;
            device.kicks.push(kick);
        }
        let owned = device
            .core
            .lock()
            .unwrap()
            .backend
            .take_kicks(&device.kicks)?;
        if !owned {
            let kicks = device
                .kicks
                .iter()
                .map(EventFd::try_clone)
                .collect::<io::Result<Vec<_>>>()?;
            let stop = device.stop.try_clone()?;
            let core = device.core.clone();
            let interrupt = device.interrupt.clone();
            device.worker = Some(
                std::thread::Builder::new()
                    .name("shuru-virtio".into())
                    .spawn(move || serve(core, interrupt, kicks, stop))?,
            );
        }
        Ok(device)
    }

    fn kick_all(&self) {
        for kick in &self.kicks {
            let _ = kick.write(1);
        }
    }
}

fn serve(core: Arc<Mutex<Core>>, interrupt: Arc<Interrupt>, kicks: Vec<EventFd>, stop: EventFd) {
    let mut fds: Vec<libc::pollfd> = std::iter::once(&stop)
        .chain(&kicks)
        .map(|event| libc::pollfd {
            fd: event.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        })
        .collect();
    loop {
        let n = unsafe { libc::poll(fds.as_mut_ptr(), fds.len() as libc::nfds_t, -1) };
        if n < 0 {
            if io::Error::last_os_error().kind() == io::ErrorKind::Interrupted {
                continue;
            }
            return;
        }
        if fds[0].revents != 0 {
            return;
        }
        for (index, kick) in kicks.iter().enumerate() {
            if fds[index + 1].revents & libc::POLLIN == 0 {
                continue;
            }
            let _ = kick.read();
            let mut core = core.lock().unwrap();
            let Core {
                backend,
                queues,
                mem,
                activated,
                ..
            } = &mut *core;
            if *activated
                && backend.process_queue(index as u16, queues, mem)
                && queues[index].wants_interrupt(mem)
            {
                interrupt.raise(INTERRUPT_USED);
            }
        }
    }
}

impl Drop for VirtioMmioDevice {
    fn drop(&mut self) {
        let _ = self.stop.write(1);
        if let Some(worker) = self.worker.take() {
            let _ = worker.join();
        }
        for (index, kick) in self.kicks.iter().enumerate() {
            let _ = self
                .vm_fd
                .unregister_ioevent(kick, &self.notify, index as u32);
        }
    }
}

impl MmioHandler for VirtioMmioDevice {
    fn read(&self, offset: u64, data: &mut [u8]) {
        let val: u32 = if offset == VIRTIO_MMIO_INTERRUPT_STATUS {
            self.interrupt.status()
        } else {
            let core = self.core.lock().unwrap();
            match offset {
                VIRTIO_MMIO_MAGIC => 0x7472_6976,
                VIRTIO_MMIO_VERSION => 2,
                VIRTIO_MMIO_DEVICE_ID => core.backend.device_id(),
                VIRTIO_MMIO_VENDOR_ID => 0x554D_4551,
                VIRTIO_MMIO_DEV_FEATURES => {
                    let features = core.backend.device_features();
                    if core.dev_features_sel == 0 {
                        features as u32
                    } else {
                        (features >> 32) as u32
                    }
                }
                VIRTIO_MMIO_QUEUE_NUM_MAX => core.selected_queue().map_or(0, |q| q.max_size as u32),
                VIRTIO_MMIO_QUEUE_READY => core.selected_queue().map_or(0, |q| q.ready as u32),
                VIRTIO_MMIO_STATUS => core.status,
                VIRTIO_MMIO_CONFIG_GENERATION => 0,
                o if o >= VIRTIO_MMIO_CONFIG => core.backend.config_read(o - VIRTIO_MMIO_CONFIG),
                _ => 0,
            }
        };
        let len = data.len().min(4);
        data[..len].copy_from_slice(&val.to_le_bytes()[..len]);
    }

    fn write(&self, offset: u64, data: &[u8]) {
        let mut bytes = [0u8; 4];
        let len = data.len().min(4);
        bytes[..len].copy_from_slice(&data[..len]);
        let val = u32::from_le_bytes(bytes);

        match offset {
            VIRTIO_MMIO_INTERRUPT_ACK => return self.interrupt.ack(val),
            // Matching kicks never get here; KVM signals the eventfd itself.
            VIRTIO_MMIO_QUEUE_NOTIFY => return,
            _ => {}
        }

        let mut core = self.core.lock().unwrap();
        match offset {
            VIRTIO_MMIO_DEV_FEATURES_SEL => core.dev_features_sel = val,
            VIRTIO_MMIO_DRV_FEATURES => {
                if core.drv_features_sel == 0 {
                    core.drv_features = (core.drv_features & 0xFFFF_FFFF_0000_0000) | val as u64;
                } else {
                    core.drv_features =
                        (core.drv_features & 0x0000_0000_FFFF_FFFF) | ((val as u64) << 32);
                }
            }
            VIRTIO_MMIO_DRV_FEATURES_SEL => core.drv_features_sel = val,
            VIRTIO_MMIO_QUEUE_SEL => core.queue_sel = val,
            VIRTIO_MMIO_QUEUE_NUM => {
                if let Some(q) = core.selected_queue_mut() {
                    q.size = if (1..=q.max_size as u32).contains(&val) {
                        val as u16
                    } else {
                        0
                    };
                }
            }
            VIRTIO_MMIO_QUEUE_READY => {
                if let Some(q) = core.selected_queue_mut() {
                    q.ready = val == 1;
                }
            }
            VIRTIO_MMIO_STATUS => {
                let prev = core.status;
                core.status = val;
                if val == 0 {
                    core.backend.reset();
                    core.activated = false;
                    for q in &mut core.queues {
                        *q = VirtioQueueState::new(q.max_size);
                    }
                    self.interrupt.ack(u32::MAX);
                }
                if (val & VIRTIO_STATUS_FEATURES_OK) != 0 && (prev & VIRTIO_STATUS_FEATURES_OK) == 0
                {
                    let features = core.drv_features & core.backend.device_features();
                    core.backend.features_ok(features);
                }
                if !core.activated
                    && (val & VIRTIO_STATUS_DRIVER_OK) != 0
                    && (val & VIRTIO_STATUS_FEATURES_OK) != 0
                {
                    core.activated = true;
                    let Core {
                        backend,
                        queues,
                        mem,
                        ..
                    } = &mut *core;
                    backend.activate(queues, mem, self.interrupt.clone());
                    drop(core);
                    // Anything queued before the driver finished setting up.
                    self.kick_all();
                }
            }
            VIRTIO_MMIO_QUEUE_DESC_LOW => {
                if let Some(q) = core.selected_queue_mut() {
                    q.desc_addr = (q.desc_addr & !0xFFFF_FFFF) | val as u64;
                }
            }
            VIRTIO_MMIO_QUEUE_DESC_HIGH => {
                if let Some(q) = core.selected_queue_mut() {
                    q.desc_addr = (q.desc_addr & 0xFFFF_FFFF) | ((val as u64) << 32);
                }
            }
            VIRTIO_MMIO_QUEUE_DRIVER_LOW => {
                if let Some(q) = core.selected_queue_mut() {
                    q.avail_addr = (q.avail_addr & !0xFFFF_FFFF) | val as u64;
                }
            }
            VIRTIO_MMIO_QUEUE_DRIVER_HIGH => {
                if let Some(q) = core.selected_queue_mut() {
                    q.avail_addr = (q.avail_addr & 0xFFFF_FFFF) | ((val as u64) << 32);
                }
            }
            VIRTIO_MMIO_QUEUE_DEVICE_LOW => {
                if let Some(q) = core.selected_queue_mut() {
                    q.used_addr = (q.used_addr & !0xFFFF_FFFF) | val as u64;
                }
            }
            VIRTIO_MMIO_QUEUE_DEVICE_HIGH => {
                if let Some(q) = core.selected_queue_mut() {
                    q.used_addr = (q.used_addr & 0xFFFF_FFFF) | ((val as u64) << 32);
                }
            }
            o if o >= VIRTIO_MMIO_CONFIG => {
                core.backend.config_write(o - VIRTIO_MMIO_CONFIG, val);
            }
            _ => {}
        }
    }
}

// ===========================================================================
// Virtio descriptor chain helpers
// ===========================================================================

pub(super) const VRING_DESC_F_NEXT: u16 = 1;
pub(super) const VRING_DESC_F_WRITE: u16 = 2;

#[derive(Clone, Copy)]
pub(super) struct VringDesc {
    pub(super) addr: u64,
    pub(super) len: u32,
    pub(super) flags: u16,
    pub(super) next: u16,
}

pub fn avail_idx(mem: &GuestMemoryMmap, avail_addr: u64) -> u16 {
    let idx = mem
        .read_obj::<u16>(GuestAddress(avail_addr + 2))
        .unwrap_or(0);
    // Ring entries the index covers must not be read before the index.
    fence(Ordering::Acquire);
    idx
}

pub fn avail_ring_entry(mem: &GuestMemoryMmap, avail_addr: u64, queue_size: u16, idx: u16) -> u16 {
    let offset = 4 + (idx % queue_size) as u64 * 2;
    mem.read_obj::<u16>(GuestAddress(avail_addr + offset))
        .unwrap_or(0)
}

pub(super) fn read_desc(mem: &GuestMemoryMmap, desc_addr: u64, idx: u16) -> VringDesc {
    let off = idx as u64 * 16;
    VringDesc {
        addr: mem.read_obj(GuestAddress(desc_addr + off)).unwrap_or(0),
        len: mem.read_obj(GuestAddress(desc_addr + off + 8)).unwrap_or(0),
        flags: mem
            .read_obj(GuestAddress(desc_addr + off + 12))
            .unwrap_or(0),
        next: mem
            .read_obj(GuestAddress(desc_addr + off + 14))
            .unwrap_or(0),
    }
}

pub fn write_used(
    mem: &GuestMemoryMmap,
    used_addr: u64,
    queue_size: u16,
    used_idx: u16,
    desc_id: u32,
    len: u32,
) {
    let ring_off = 4 + (used_idx % queue_size) as u64 * 8;
    let _ = mem.write_obj(desc_id, GuestAddress(used_addr + ring_off));
    let _ = mem.write_obj(len, GuestAddress(used_addr + ring_off + 4));
    fence(Ordering::Release);
    let _ = mem.write_obj(used_idx.wrapping_add(1), GuestAddress(used_addr + 2));
}

// ===========================================================================
// Virtio block backend (device ID 2)
// ===========================================================================

use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};

#[path = "block_ranges.rs"]
mod block_ranges;
use block_ranges::*;
pub(super) const VIRTIO_F_VERSION_1: u64 = 1 << 32;

pub struct BlockBackend {
    disk: File,
    capacity: u64,
    read_only: bool,
    negotiated: u64,
    buf: Vec<u8>,
}

impl BlockBackend {
    pub fn new(path: &str, read_only: bool) -> std::io::Result<Self> {
        let disk = OpenOptions::new().read(true).write(!read_only).open(path)?;
        let capacity = disk.metadata()?.len() / 512;
        Ok(BlockBackend {
            disk,
            capacity,
            read_only,
            negotiated: 0,
            buf: vec![0u8; 65536],
        })
    }
    fn request(&mut self, mem: &GuestMemoryMmap, chain: &[VringDesc]) -> Result<u32, u8> {
        let fail = VIRTIO_BLK_S_IOERR;
        if chain.len() < 2 {
            return Err(fail);
        }
        let header = &chain[0];
        if header.len != 16 || header.flags & VRING_DESC_F_WRITE != 0 {
            return Err(fail);
        }
        let mut bytes = [0; 16];
        mem.read_slice(&mut bytes, GuestAddress(header.addr))
            .map_err(|_| fail)?;
        let kind = u32::from_le_bytes(bytes[..4].try_into().unwrap());
        let sector = u64::from_le_bytes(bytes[8..].try_into().unwrap());
        let data = &chain[1..chain.len() - 1];
        let mut total = 0usize;
        for desc in data {
            if (desc.flags & VRING_DESC_F_WRITE != 0) != (kind == VIRTIO_BLK_T_IN) {
                return Err(fail);
            }
            if desc.len > 1024 * 1024
                || !mem.check_range(GuestAddress(desc.addr), desc.len as usize)
            {
                return Err(fail);
            }
            total = total.checked_add(desc.len as usize).ok_or(fail)?;
        }
        match kind {
            VIRTIO_BLK_T_DISCARD | VIRTIO_BLK_T_WRITE_ZEROES => {
                if self.read_only
                    || total == 0
                    || total > MAX_RANGES * 16
                    || !total.is_multiple_of(16)
                    || sector != 0
                {
                    return Err(fail);
                }
                let feature = if kind == VIRTIO_BLK_T_DISCARD { 13 } else { 14 };
                if self.negotiated & (1 << feature) == 0 {
                    return Err(VIRTIO_BLK_S_UNSUPP);
                }
                let mut payload = [0u8; MAX_RANGES * 16];
                let mut at = 0;
                for desc in data {
                    let end = at + desc.len as usize;
                    mem.read_slice(&mut payload[at..end], GuestAddress(desc.addr))
                        .map_err(|_| fail)?;
                    at = end;
                }
                let ranges = decode_ranges(&payload[..total], self.capacity, kind)?;
                for (offset, len, unmap) in ranges {
                    if len == 0 {
                        continue;
                    }
                    let mode = if kind == VIRTIO_BLK_T_DISCARD || unmap {
                        libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE
                    } else {
                        libc::FALLOC_FL_ZERO_RANGE | libc::FALLOC_FL_KEEP_SIZE
                    };
                    if unsafe { libc::fallocate(self.disk.as_raw_fd(), mode, offset, len) } < 0 {
                        return Err(fail);
                    }
                }
                Ok(0)
            }
            VIRTIO_BLK_T_IN | VIRTIO_BLK_T_OUT => {
                if kind == VIRTIO_BLK_T_OUT && self.read_only {
                    return Err(fail);
                }
                if !total.is_multiple_of(512)
                    || sector > self.capacity
                    || total as u64 / 512 > self.capacity - sector
                {
                    return Err(fail);
                }
                self.disk
                    .seek(SeekFrom::Start(sector.checked_mul(512).ok_or(fail)?))
                    .map_err(|_| fail)?;
                let mut written = 0;
                for desc in data {
                    let mut at = 0usize;
                    while at < desc.len as usize {
                        let n = self.buf.len().min(desc.len as usize - at);
                        let buf = &mut self.buf[..n];
                        let addr = GuestAddress(desc.addr.checked_add(at as u64).ok_or(fail)?);
                        if kind == VIRTIO_BLK_T_IN {
                            self.disk.read_exact(buf).map_err(|_| fail)?;
                            mem.write_slice(buf, addr).map_err(|_| fail)?;
                            written += n as u32;
                        } else {
                            mem.read_slice(buf, addr).map_err(|_| fail)?;
                            self.disk.write_all(buf).map_err(|_| fail)?;
                        }
                        at += n;
                    }
                }
                Ok(written)
            }
            _ => Err(VIRTIO_BLK_S_UNSUPP),
        }
    }
}

impl VirtioBackend for BlockBackend {
    fn device_id(&self) -> u32 {
        2
    }

    fn device_features(&self) -> u64 {
        let mut f = VIRTIO_F_VERSION_1 | (1 << 1) | (1 << 2);
        if self.read_only {
            f |= 1 << 5;
        } else {
            f |= (1 << 13) | (1 << 14);
        }
        f
    }

    fn queue_count(&self) -> usize {
        1
    }
    fn queue_max_size(&self) -> u16 {
        256
    }

    fn config_read(&self, offset: u64) -> u32 {
        match offset {
            0 => self.capacity as u32,
            4 => (self.capacity >> 32) as u32,
            8 => 1024 * 1024,
            12 => 254,
            36 | 48 if !self.read_only => MAX_RANGE_SECTORS,
            40 | 52 if !self.read_only => MAX_RANGES as u32,
            44 if !self.read_only => 8,
            56 if !self.read_only => 1,
            _ => 0,
        }
    }

    fn config_write(&mut self, _: u64, _: u32) {}

    fn features_ok(&mut self, features: u64) {
        self.negotiated = features;
    }

    fn activate(&mut self, _: &[VirtioQueueState], _: &GuestMemoryMmap, _: Arc<Interrupt>) {}

    fn process_queue(
        &mut self,
        _queue_idx: u16,
        queues: &mut [VirtioQueueState],
        mem: &GuestMemoryMmap,
    ) -> bool {
        let q = match queues.get_mut(0) {
            Some(q) => q,
            None => return false,
        };
        let current_avail = match q.pending(mem) {
            Some(idx) => idx,
            None => return false,
        };
        let used = q.last_avail_idx != current_avail;
        while q.last_avail_idx != current_avail {
            let head = avail_ring_entry(mem, q.avail_addr, q.size, q.last_avail_idx);
            let chain = q
                .chain(mem, head, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE)
                .unwrap_or_default();
            let status_desc = chain.last().filter(|d| {
                chain.len() >= 2
                    && d.flags == VRING_DESC_F_WRITE
                    && d.len >= 1
                    && mem.check_range(GuestAddress(d.addr), 1)
            });
            let mut used_len = 0;
            if let Some(desc) = status_desc {
                let result = self.request(mem, &chain);
                let status = match result {
                    Ok(n) => {
                        used_len = n;
                        VIRTIO_BLK_S_OK
                    }
                    Err(status) => status,
                };
                let _ = mem.write_obj(status, GuestAddress(desc.addr));
                used_len += 1;
            }
            write_used(
                mem,
                q.used_addr,
                q.size,
                q.last_avail_idx,
                head as u32,
                used_len,
            );
            q.last_avail_idx = q.last_avail_idx.wrapping_add(1);
        }
        used
    }

    fn reset(&mut self) {
        self.negotiated = 0;
    }
}

// ===========================================================================
// Virtio net backend (device ID 1) — socketpair with RX polling thread
// ===========================================================================

const VIRTIO_NET_F_MAC: u64 = 1 << 5;
const VIRTIO_NET_HDR_SIZE: usize = 12;

pub struct NetBackend {
    fd: RawFd,
    mac: [u8; 6],
    rx_running: Arc<AtomicBool>,
    rx_thread: Option<std::thread::JoinHandle<()>>,
    tx_buf: Vec<u8>,
}

// The virtio-net header plus one Ethernet frame at the 65535-byte IP MTU.
const MAX_TX_FRAME: usize = VIRTIO_NET_HDR_SIZE + 14 + 65535;

impl NetBackend {
    pub fn new(fd: RawFd, mac: [u8; 6]) -> Self {
        NetBackend {
            fd,
            mac,
            rx_running: Arc::new(AtomicBool::new(false)),
            tx_buf: vec![0; MAX_TX_FRAME],
            rx_thread: None,
        }
    }
}

impl VirtioBackend for NetBackend {
    fn device_id(&self) -> u32 {
        1
    }

    fn device_features(&self) -> u64 {
        VIRTIO_F_VERSION_1 | VIRTIO_NET_F_MAC
    }

    fn queue_count(&self) -> usize {
        2
    }
    fn queue_max_size(&self) -> u16 {
        256
    }

    fn config_read(&self, offset: u64) -> u32 {
        match offset {
            0 => u32::from_le_bytes([self.mac[0], self.mac[1], self.mac[2], self.mac[3]]),
            4 => u16::from_le_bytes([self.mac[4], self.mac[5]]) as u32,
            _ => 0,
        }
    }

    fn config_write(&mut self, _: u64, _: u32) {}

    fn activate(
        &mut self,
        queues: &[VirtioQueueState],
        mem: &GuestMemoryMmap,
        interrupt: Arc<Interrupt>,
    ) {
        // Set fd to non-blocking
        unsafe {
            let flags = libc::fcntl(self.fd, libc::F_GETFL);
            libc::fcntl(self.fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
        }

        // Spawn RX polling thread
        let rx_q = match queues.first() {
            Some(q) if q.ready => q.clone(),
            _ => return,
        };

        self.rx_running.store(true, Ordering::Release);
        let running = self.rx_running.clone();
        let fd = self.fd;
        let mem = mem.clone();

        self.rx_thread = Some(
            std::thread::Builder::new()
                .name("shuru-net-rx".into())
                .spawn(move || {
                    net_rx_loop(fd, mem, rx_q, interrupt, running);
                })
                .expect("failed to spawn net-rx thread"),
        );
    }

    fn process_queue(
        &mut self,
        queue_idx: u16,
        queues: &mut [VirtioQueueState],
        mem: &GuestMemoryMmap,
    ) -> bool {
        if queue_idx != 1 {
            return false; // RX handled by thread
        }
        let q = match queues.get_mut(1) {
            Some(q) => q,
            None => return false,
        };
        let current_avail = match q.pending(mem) {
            Some(idx) => idx,
            None => return false,
        };
        let used = q.last_avail_idx != current_avail;
        while q.last_avail_idx != current_avail {
            let head = avail_ring_entry(mem, q.avail_addr, q.size, q.last_avail_idx);

            // A chain that loops, is longer than one frame, or points
            // outside guest memory is dropped.
            let mut len = 0;
            for desc in q.chain(mem, head, VRING_DESC_F_NEXT).unwrap_or_default() {
                let end = len + desc.len as usize;
                if end > MAX_TX_FRAME
                    || mem
                        .read_slice(&mut self.tx_buf[len..end], GuestAddress(desc.addr))
                        .is_err()
                {
                    len = 0;
                    break;
                }
                len = end;
            }

            // Skip virtio-net header, send raw Ethernet frame
            if len > VIRTIO_NET_HDR_SIZE {
                let frame = &self.tx_buf[VIRTIO_NET_HDR_SIZE..len];
                unsafe {
                    libc::send(
                        self.fd,
                        frame.as_ptr() as *const _,
                        frame.len(),
                        libc::MSG_NOSIGNAL,
                    );
                }
            }

            write_used(mem, q.used_addr, q.size, q.last_avail_idx, head as u32, 0);
            q.last_avail_idx = q.last_avail_idx.wrapping_add(1);
        }
        used
    }

    fn reset(&mut self) {
        self.rx_running.store(false, Ordering::Release);
        if let Some(h) = self.rx_thread.take() {
            let _ = h.join();
        }
    }
}

impl Drop for NetBackend {
    fn drop(&mut self) {
        self.reset();
    }
}

/// RX loop: poll socketpair → push frames into guest RX queue → inject interrupt.
fn net_rx_loop(
    fd: RawFd,
    mem: GuestMemoryMmap,
    rx_q: VirtioQueueState,
    interrupt: Arc<Interrupt>,
    running: Arc<AtomicBool>,
) {
    let mut last_avail = rx_q.last_avail_idx;
    let mut used_idx: u16 = 0;
    // One Ethernet frame at the 65535-byte IP-MTU the proxy stack uses:
    // 14 bytes of header plus the IP payload. A datagram longer than the
    // buffer would be truncated by recv.
    let mut buf = vec![0u8; 14 + 65535];

    while running.load(Ordering::Acquire) {
        let mut pfd = libc::pollfd {
            fd,
            events: libc::POLLIN,
            revents: 0,
        };
        let ret = unsafe { libc::poll(&mut pfd, 1, 100) };
        if ret <= 0 {
            continue;
        }

        let n = unsafe { libc::recv(fd, buf.as_mut_ptr() as *mut _, buf.len(), 0) };
        if n <= 0 {
            continue;
        }
        let frame = &buf[..n as usize];

        // Check for available RX descriptors
        let mut ring = rx_q.clone();
        ring.last_avail_idx = last_avail;
        let current_avail = match ring.pending(&mem) {
            Some(idx) => idx,
            None => continue,
        };
        if last_avail == current_avail {
            continue; // drop frame — no buffers
        }

        let head = avail_ring_entry(&mem, rx_q.avail_addr, rx_q.size, last_avail);
        if head >= rx_q.size {
            last_avail = last_avail.wrapping_add(1);
            continue;
        }
        let desc = read_desc(&mem, rx_q.desc_addr, head);

        let total = VIRTIO_NET_HDR_SIZE + frame.len();
        if (desc.flags & VRING_DESC_F_WRITE) != 0 && desc.len as usize >= total {
            // Write virtio-net header (zeros) + frame
            let header = [0u8; VIRTIO_NET_HDR_SIZE];
            let _ = mem.write_slice(&header, GuestAddress(desc.addr));
            let _ = mem.write_slice(frame, GuestAddress(desc.addr + VIRTIO_NET_HDR_SIZE as u64));

            write_used(
                &mem,
                rx_q.used_addr,
                rx_q.size,
                used_idx,
                head as u32,
                total as u32,
            );
            used_idx = used_idx.wrapping_add(1);
            last_avail = last_avail.wrapping_add(1);

            if rx_q.wants_interrupt(&mem) {
                interrupt.raise(INTERRUPT_USED);
            }
        }
    }
}

#[cfg(test)]
mod block_tests {
    use super::*;
    use std::os::unix::fs::FileExt;

    fn backend() -> BlockBackend {
        static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let path = std::env::temp_dir().join(format!(
            "shuru-block-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        let file = OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
            .unwrap();
        file.set_len(16384).unwrap();
        file.write_all_at(&vec![7; 16384], 0).unwrap();
        let mut disk = BlockBackend::new(path.to_str().unwrap(), false).unwrap();
        disk.features_ok(disk.device_features());
        std::fs::remove_file(path).unwrap();
        disk
    }

    fn request(mem: &GuestMemoryMmap, kind: u32, ranges: &[(u64, u32, u32)]) -> Vec<VringDesc> {
        mem.write_obj(kind.to_le(), GuestAddress(0x1000)).unwrap();
        mem.write_obj(0u64, GuestAddress(0x1008)).unwrap();
        for (i, (sector, count, flags)) in ranges.iter().enumerate() {
            let at = 0x2000 + i as u64 * 16;
            mem.write_obj(sector.to_le(), GuestAddress(at)).unwrap();
            mem.write_obj(count.to_le(), GuestAddress(at + 8)).unwrap();
            mem.write_obj(flags.to_le(), GuestAddress(at + 12)).unwrap();
        }
        vec![
            VringDesc {
                addr: 0x1000,
                len: 16,
                flags: 1,
                next: 1,
            },
            VringDesc {
                addr: 0x2000,
                len: ranges.len() as u32 * 16,
                flags: 1,
                next: 2,
            },
            VringDesc {
                addr: 0x3000,
                len: 1,
                flags: 2,
                next: 0,
            },
        ]
    }

    #[test]
    fn invalid_later_range_leaves_disk_untouched() {
        let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 65536)]).unwrap();
        let mut disk = backend();
        let chain = request(&mem, VIRTIO_BLK_T_DISCARD, &[(0, 8, 0), (31, 2, 0)]);
        assert_eq!(disk.request(&mem, &chain), Err(VIRTIO_BLK_S_IOERR));
        let mut bytes = [0; 16384];
        disk.disk.read_exact_at(&mut bytes, 0).unwrap();
        assert!(bytes.iter().all(|b| *b == 7));
    }

    #[test]
    fn discard_and_both_zero_modes_preserve_size_and_neighbors() {
        let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 65536)]).unwrap();
        for (kind, flags) in [
            (VIRTIO_BLK_T_DISCARD, 0),
            (VIRTIO_BLK_T_WRITE_ZEROES, 0),
            (VIRTIO_BLK_T_WRITE_ZEROES, 1),
        ] {
            let mut disk = backend();
            let chain = request(&mem, kind, &[(8, 8, flags)]);
            assert_eq!(disk.request(&mem, &chain), Ok(0));
            let mut bytes = [0; 16384];
            disk.disk.read_exact_at(&mut bytes, 0).unwrap();
            assert!(bytes[..4096].iter().all(|b| *b == 7));
            assert!(bytes[4096..8192].iter().all(|b| *b == 0));
            assert!(bytes[8192..].iter().all(|b| *b == 7));
            assert_eq!(disk.disk.metadata().unwrap().len(), 16384);
        }
    }

    #[test]
    fn descriptor_cycles_and_bad_heads_finish_without_io() {
        let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 65536)]).unwrap();
        let mut disk = backend();
        let mut q = VirtioQueueState::new(8);
        q.size = 8;
        q.ready = true;
        q.desc_addr = 0x1000;
        q.avail_addr = 0x2000;
        q.used_addr = 0x3000;
        mem.write_obj(1u16, GuestAddress(q.desc_addr + 12)).unwrap();
        mem.write_obj(0u16, GuestAddress(q.desc_addr + 14)).unwrap();
        mem.write_obj(2u16, GuestAddress(q.avail_addr + 2)).unwrap();
        mem.write_obj(0u16, GuestAddress(q.avail_addr + 4)).unwrap();
        mem.write_obj(100u16, GuestAddress(q.avail_addr + 6))
            .unwrap();
        assert!(disk.process_queue(0, std::slice::from_mut(&mut q), &mem));
        assert_eq!(q.last_avail_idx, 2);
        assert_eq!(
            mem.read_obj::<u16>(GuestAddress(q.used_addr + 2)).unwrap(),
            2
        );
    }

    #[test]
    fn readonly_and_bad_data_direction_are_rejected() {
        let mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 65536)]).unwrap();
        let mut disk = backend();
        let mut chain = request(&mem, VIRTIO_BLK_T_DISCARD, &[(0, 8, 0)]);
        chain[1].flags |= VRING_DESC_F_WRITE;
        assert_eq!(disk.request(&mem, &chain), Err(VIRTIO_BLK_S_IOERR));
        chain[1].flags &= !VRING_DESC_F_WRITE;
        disk.read_only = true;
        assert_eq!(disk.request(&mem, &chain), Err(VIRTIO_BLK_S_IOERR));
        assert_eq!(disk.device_features() & ((1 << 13) | (1 << 14)), 0);
    }
}

#[cfg(test)]
mod queue_tests {
    use super::*;

    const DESC: u64 = 0x1000;
    const AVAIL: u64 = 0x2000;
    const USED: u64 = 0x3000;

    fn queue(size: u16) -> VirtioQueueState {
        VirtioQueueState {
            size,
            ready: true,
            desc_addr: DESC,
            avail_addr: AVAIL,
            used_addr: USED,
            ..VirtioQueueState::new(256)
        }
    }

    fn desc(mem: &GuestMemoryMmap, i: u16, addr: u64, len: u32, flags: u16, next: u16) {
        let at = DESC + i as u64 * 16;
        mem.write_obj(addr, GuestAddress(at)).unwrap();
        mem.write_obj(len, GuestAddress(at + 8)).unwrap();
        mem.write_obj(flags, GuestAddress(at + 12)).unwrap();
        mem.write_obj(next, GuestAddress(at + 14)).unwrap();
    }

    fn offer(mem: &GuestMemoryMmap, heads: &[u16]) {
        for (i, head) in heads.iter().enumerate() {
            mem.write_obj(*head, GuestAddress(AVAIL + 4 + i as u64 * 2))
                .unwrap();
        }
        mem.write_obj(heads.len() as u16, GuestAddress(AVAIL + 2))
            .unwrap();
    }

    fn memory() -> GuestMemoryMmap {
        GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x20000)]).unwrap()
    }

    #[test]
    fn pending_rejects_unusable_queues() {
        let mem = memory();
        offer(&mem, &[0]);
        assert_eq!(queue(8).pending(&mem), Some(1));
        assert_eq!(queue(0).pending(&mem), None);
        assert_eq!(queue(257).pending(&mem), None);
        let mut far = queue(8);
        far.desc_addr = 0x1FFC0;
        assert_eq!(far.pending(&mem), None);
        mem.write_obj(9u16, GuestAddress(AVAIL + 2)).unwrap();
        assert_eq!(queue(8).pending(&mem), None);
    }

    #[test]
    fn chain_rejects_loops_out_of_range_and_foreign_flags() {
        let mem = memory();
        let q = queue(8);
        desc(&mem, 0, 0x8000, 4, VRING_DESC_F_NEXT, 1);
        desc(&mem, 1, 0x8000, 4, 0, 0);
        assert_eq!(
            q.chain(&mem, 0, VRING_DESC_F_NEXT).map(|c| c.len()),
            Some(2)
        );
        desc(&mem, 1, 0x8000, 4, VRING_DESC_F_NEXT, 0);
        assert!(q.chain(&mem, 0, VRING_DESC_F_NEXT).is_none());
        desc(&mem, 1, 0x8000, 4, VRING_DESC_F_NEXT, 8);
        assert!(q.chain(&mem, 0, VRING_DESC_F_NEXT).is_none());
        desc(&mem, 1, 0x8000, 4, VRING_DESC_F_WRITE, 0);
        assert!(q.chain(&mem, 0, VRING_DESC_F_NEXT).is_none());
        assert!(q.chain(&mem, 8, VRING_DESC_F_NEXT).is_none());
    }

    #[test]
    fn net_tx_drops_bad_chains_and_sends_good_frames() {
        let mem = memory();
        let (host, guest) = std::os::unix::net::UnixDatagram::pair().unwrap();
        host.set_nonblocking(true).unwrap();
        let mut net = NetBackend::new(guest.as_raw_fd(), [0; 6]);
        let mut queues = [queue(8), queue(8)];

        // A chain that points at itself, then one descriptor of 4 GiB.
        desc(&mem, 0, 0x8000, 64, VRING_DESC_F_NEXT, 0);
        desc(&mem, 1, 0x8000, u32::MAX, 0, 0);
        // A frame split over two descriptors.
        mem.write_slice(&[0xAB; 64], GuestAddress(0x9000)).unwrap();
        desc(
            &mem,
            2,
            0x9000,
            VIRTIO_NET_HDR_SIZE as u32,
            VRING_DESC_F_NEXT,
            3,
        );
        desc(&mem, 3, 0x9000 + VIRTIO_NET_HDR_SIZE as u64, 20, 0, 0);
        offer(&mem, &[0, 1, 2]);

        assert!(net.process_queue(1, &mut queues, &mem));
        assert_eq!(queues[1].last_avail_idx, 3);
        let mut frame = [0u8; 128];
        assert_eq!(host.recv(&mut frame).unwrap(), 20);
        assert!(host.recv(&mut frame).is_err());
        drop(guest);
    }
}