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
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::sync::Arc;

use anyhow::bail;
use anyhow::Context;
use base::custom_serde::deserialize_seq_to_arr;
use base::custom_serde::serialize_arr;
use base::error;
use base::warn;
use base::MemoryMapping;
use base::MemoryMappingBuilder;
use base::SharedMemory;
use downcast_rs::impl_downcast;
use downcast_rs::Downcast;
use remain::sorted;
use serde::Deserialize;
use serde::Serialize;
use sync::Mutex;
use thiserror::Error;

use crate::pci::PciInterruptPin;

// The number of 32bit registers in the config space, 256 bytes.
const NUM_CONFIGURATION_REGISTERS: usize = 64;

pub const PCI_ID_REG: usize = 0;
pub const COMMAND_REG: usize = 1;
pub const COMMAND_REG_IO_SPACE_MASK: u32 = 0x0000_0001;
pub const COMMAND_REG_MEMORY_SPACE_MASK: u32 = 0x0000_0002;
const STATUS_REG: usize = 1;
pub const STATUS_REG_CAPABILITIES_USED_MASK: u32 = 0x0010_0000;
#[allow(dead_code)]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub const CLASS_REG: usize = 2;
pub const HEADER_TYPE_REG: usize = 3;
pub const HEADER_TYPE_REG_OFFSET: usize = 2;
pub const HEADER_TYPE_MULTIFUNCTION_MASK: u8 = 0x80;
pub const BAR0_REG: usize = 4;
const BAR_IO_ADDR_MASK: u32 = 0xffff_fffc;
const BAR_IO_MIN_SIZE: u64 = 4;
const BAR_MEM_ADDR_MASK: u32 = 0xffff_fff0;
const BAR_MEM_MIN_SIZE: u64 = 16;
const BAR_ROM_MIN_SIZE: u64 = 2048;
pub const NUM_BAR_REGS: usize = 7; // 6 normal BARs + expansion ROM BAR.
pub const ROM_BAR_IDX: PciBarIndex = 6;
pub const ROM_BAR_REG: usize = 12;
pub const CAPABILITY_LIST_HEAD_OFFSET: usize = 0x34;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub const PCI_CAP_NEXT_POINTER: usize = 0x1;
const FIRST_CAPABILITY_OFFSET: usize = 0x40;
pub const CAPABILITY_MAX_OFFSET: usize = 255;

const INTERRUPT_LINE_PIN_REG: usize = 15;

/// Represents the types of PCI headers allowed in the configuration registers.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciHeaderType {
    Device,
    Bridge,
}

/// Classes of PCI nodes.
#[allow(dead_code)]
#[derive(Copy, Clone, Debug, enumn::N, Serialize, Deserialize, PartialEq, Eq)]
pub enum PciClassCode {
    TooOld,
    MassStorage,
    NetworkController,
    DisplayController,
    MultimediaController,
    MemoryController,
    BridgeDevice,
    SimpleCommunicationController,
    BaseSystemPeripheral,
    InputDevice,
    DockingStation,
    Processor,
    SerialBusController,
    WirelessController,
    IntelligentIoController,
    SatelliteCommunicationController,
    EncryptionController,
    DataAcquisitionSignalProcessing,
    ProcessingAccelerator,
    NonEssentialInstrumentation,
    Other = 0xff,
}

impl PciClassCode {
    pub fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

#[sorted]
#[derive(Error, Debug)]
pub enum PciClassCodeParseError {
    #[error("Unknown class code")]
    Unknown,
}

impl TryFrom<u8> for PciClassCode {
    type Error = PciClassCodeParseError;
    fn try_from(v: u8) -> std::result::Result<PciClassCode, PciClassCodeParseError> {
        match PciClassCode::n(v) {
            Some(class) => Ok(class),
            None => Err(PciClassCodeParseError::Unknown),
        }
    }
}

/// A PCI sublcass. Each class in `PciClassCode` can specify a unique set of subclasses. This trait
/// is implemented by each subclass. It allows use of a trait object to generate configurations.
pub trait PciSubclass {
    /// Convert this subclass to the value used in the PCI specification.
    fn get_register_value(&self) -> u8;
}

/// Subclasses of the MassStorage class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciMassStorageSubclass {
    Scsi = 0x00,
    NonVolatileMemory = 0x08,
    Other = 0x80,
}

impl PciSubclass for PciMassStorageSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the NetworkController class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciNetworkControllerSubclass {
    Other = 0x80,
}

impl PciSubclass for PciNetworkControllerSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the DisplayController class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciDisplaySubclass {
    VgaCompatibleController = 0x00,
    XgaCompatibleController = 0x01,
    ThreeDController = 0x02,
    Other = 0x80,
}

impl PciSubclass for PciDisplaySubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the MultimediaController class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciMultimediaSubclass {
    VideoController = 0x00,
    AudioController = 0x01,
    TelephonyDevice = 0x02,
    AudioDevice = 0x03,
    Other = 0x80,
}

impl PciSubclass for PciMultimediaSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the BridgeDevice
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciBridgeSubclass {
    HostBridge = 0x00,
    IsaBridge = 0x01,
    EisaBridge = 0x02,
    McaBridge = 0x03,
    PciToPciBridge = 0x04,
    PcmciaBridge = 0x05,
    NuBusBridge = 0x06,
    CardBusBridge = 0x07,
    RaceWayBridge = 0x08,
    PciToPciSemiTransparentBridge = 0x09,
    InfiniBrandToPciHostBridge = 0x0a,
    OtherBridgeDevice = 0x80,
}

impl PciSubclass for PciBridgeSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the SimpleCommunicationController class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciSimpleCommunicationControllerSubclass {
    Other = 0x80,
}

impl PciSubclass for PciSimpleCommunicationControllerSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the BaseSystemPeripheral class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciBaseSystemPeripheralSubclass {
    Iommu = 0x06,
    Other = 0x80,
}

impl PciSubclass for PciBaseSystemPeripheralSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the InputDevice class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciInputDeviceSubclass {
    Other = 0x80,
}

impl PciSubclass for PciInputDeviceSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclass of the SerialBus
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciSerialBusSubClass {
    Firewire = 0x00,
    AccessBus = 0x01,
    Ssa = 0x02,
    Usb = 0x03,
}

impl PciSubclass for PciSerialBusSubClass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses of the WirelessController class.
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum PciWirelessControllerSubclass {
    Other = 0x80,
}

impl PciSubclass for PciWirelessControllerSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// Subclasses for PciClassCode Other.
#[allow(dead_code)]
#[derive(Copy, Clone)]
#[repr(u8)]
pub enum PciOtherSubclass {
    Other = 0xff,
}

impl PciSubclass for PciOtherSubclass {
    fn get_register_value(&self) -> u8 {
        *self as u8
    }
}

/// A PCI class programming interface. Each combination of `PciClassCode` and
/// `PciSubclass` can specify a set of register-level programming interfaces.
/// This trait is implemented by each programming interface.
/// It allows use of a trait object to generate configurations.
pub trait PciProgrammingInterface {
    /// Convert this programming interface to the value used in the PCI specification.
    fn get_register_value(&self) -> u8;
}

/// Types of PCI capabilities.
pub enum PciCapabilityID {
    ListID = 0,
    PowerManagement = 0x01,
    AcceleratedGraphicsPort = 0x02,
    VitalProductData = 0x03,
    SlotIdentification = 0x04,
    MessageSignalledInterrupts = 0x05,
    CompactPciHotSwap = 0x06,
    Pcix = 0x07,
    HyperTransport = 0x08,
    VendorSpecific = 0x09,
    Debugport = 0x0A,
    CompactPciCentralResourceControl = 0x0B,
    PciStandardHotPlugController = 0x0C,
    BridgeSubsystemVendorDeviceID = 0x0D,
    AgpTargetPciPciBridge = 0x0E,
    SecureDevice = 0x0F,
    PciExpress = 0x10,
    Msix = 0x11,
    SataDataIndexConf = 0x12,
    PciAdvancedFeatures = 0x13,
    PciEnhancedAllocation = 0x14,
}

/// A PCI capability list. Devices can optionally specify capabilities in their configuration space.
pub trait PciCapability {
    fn bytes(&self) -> &[u8];
    fn id(&self) -> PciCapabilityID;
    fn writable_bits(&self) -> Vec<u32>;
}

pub trait PciCapConfigWriteResult: Downcast {}
impl_downcast!(PciCapConfigWriteResult);

/// A trait for implementing complex PCI capabilities.
pub trait PciCapConfig: Send {
    /// Reads a 32bit register from the capability. Only the bits set in the
    /// read mask will be used, while the rest of the bits will be taken from
    /// the `PciConfiguration`'s register data.
    /// `reg_idx` - index into the capability
    fn read_reg(&self, reg_idx: usize) -> u32;

    /// Returns the read mask used by `read_reg`.
    fn read_mask(&self) -> &'static [u32];

    /// Writes data to the capability.
    /// `reg_idx` - index into PciConfiguration.registers.
    /// `offset`  - PciConfiguration.registers is in unit of DWord, offset define byte
    ///             offset in the DWord.
    /// `data`    - The data to write.
    fn write_reg(
        &mut self,
        reg_idx: usize,
        offset: u64,
        data: &[u8],
    ) -> Option<Box<dyn PciCapConfigWriteResult>>;

    /// Used to pass the mmio region for the capability to the implementation.
    /// If any external events update the capability's registers, then
    /// `PciCapMapping.set_reg` must be called to make the changes visible
    /// to the guest.
    fn set_cap_mapping(&mut self, _mapping: PciCapMapping) {}

    fn num_regs(&self) -> usize {
        self.read_mask().len()
    }
}

/// Contains the configuration space of a PCI node.
/// See the [specification](https://en.wikipedia.org/wiki/PCI_configuration_space).
/// The configuration space is accessed with DWORD reads and writes from the guest.
pub struct PciConfiguration {
    registers: [u32; NUM_CONFIGURATION_REGISTERS],
    writable_bits: [u32; NUM_CONFIGURATION_REGISTERS], // writable bits for each register.
    bar_used: [bool; NUM_BAR_REGS],
    bar_configs: [Option<PciBarConfiguration>; NUM_BAR_REGS],
    // Contains the byte offset and size of the last capability.
    last_capability: Option<(usize, usize)>,
    capability_configs: BTreeMap<usize, Box<dyn PciCapConfig>>,
    mmio_mapping: Option<(Arc<Mutex<MemoryMapping>>, usize)>,
}

#[derive(Serialize, Deserialize)]
pub struct PciConfigurationSerialized {
    #[serde(
        serialize_with = "serialize_arr",
        deserialize_with = "deserialize_seq_to_arr"
    )]
    registers: [u32; NUM_CONFIGURATION_REGISTERS],
    #[serde(
        serialize_with = "serialize_arr",
        deserialize_with = "deserialize_seq_to_arr"
    )]
    writable_bits: [u32; NUM_CONFIGURATION_REGISTERS],
    bar_used: [bool; NUM_BAR_REGS],
    bar_configs: [Option<PciBarConfiguration>; NUM_BAR_REGS],
    last_capability: Option<(usize, usize)>,
}

/// See pci_regs.h in kernel
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PciBarRegionType {
    Memory32BitRegion = 0,
    IoRegion = 0x01,
    Memory64BitRegion = 0x04,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum PciBarPrefetchable {
    NotPrefetchable = 0,
    Prefetchable = 0x08,
}

pub type PciBarIndex = usize;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PciBarConfiguration {
    addr: u64,
    size: u64,
    bar_idx: PciBarIndex,
    region_type: PciBarRegionType,
    prefetchable: PciBarPrefetchable,
}

pub struct PciBarIter<'a> {
    config: &'a PciConfiguration,
    bar_num: PciBarIndex,
}

impl<'a> Iterator for PciBarIter<'a> {
    type Item = PciBarConfiguration;

    fn next(&mut self) -> Option<Self::Item> {
        while self.bar_num < NUM_BAR_REGS {
            let bar_config = self.config.get_bar_configuration(self.bar_num);
            self.bar_num += 1;
            if let Some(bar_config) = bar_config {
                return Some(bar_config);
            }
        }

        None
    }
}

#[sorted]
#[derive(Error, Debug, PartialEq, Eq)]
pub enum Error {
    #[error("address {0} size {1} too big")]
    BarAddressInvalid(u64, u64),
    #[error("address {0} is not aligned to size {1}")]
    BarAlignmentInvalid(u64, u64),
    #[error("bar {0} already used")]
    BarInUse(PciBarIndex),
    #[error("64bit bar {0} already used (requires two regs)")]
    BarInUse64(PciBarIndex),
    #[error("bar {0} invalid, max {}", NUM_BAR_REGS - 1)]
    BarInvalid(PciBarIndex),
    #[error("64bitbar {0} invalid, requires two regs, max {}", ROM_BAR_IDX - 1)]
    BarInvalid64(PciBarIndex),
    #[error("expansion rom bar must be a memory region")]
    BarInvalidRomType,
    #[error("bar address {0} not a power of two")]
    BarSizeInvalid(u64),
    #[error("empty capabilities are invalid")]
    CapabilityEmpty,
    #[error("Invalid capability length {0}")]
    CapabilityLengthInvalid(usize),
    #[error("capability of size {0} doesn't fit")]
    CapabilitySpaceFull(usize),
}

pub type Result<T> = std::result::Result<T, Error>;

impl PciConfiguration {
    pub fn new(
        vendor_id: u16,
        device_id: u16,
        class_code: PciClassCode,
        subclass: &dyn PciSubclass,
        programming_interface: Option<&dyn PciProgrammingInterface>,
        header_type: PciHeaderType,
        subsystem_vendor_id: u16,
        subsystem_id: u16,
        revision_id: u8,
    ) -> Self {
        let mut registers = [0u32; NUM_CONFIGURATION_REGISTERS];
        let mut writable_bits = [0u32; NUM_CONFIGURATION_REGISTERS];
        registers[0] = u32::from(device_id) << 16 | u32::from(vendor_id);
        // TODO(dverkamp): Status should be write-1-to-clear
        writable_bits[1] = 0x0000_ffff; // Status (r/o), command (r/w)
        let pi = if let Some(pi) = programming_interface {
            pi.get_register_value()
        } else {
            0
        };
        registers[2] = u32::from(class_code.get_register_value()) << 24
            | u32::from(subclass.get_register_value()) << 16
            | u32::from(pi) << 8
            | u32::from(revision_id);
        writable_bits[3] = 0x0000_00ff; // Cacheline size (r/w)
        match header_type {
            PciHeaderType::Device => {
                registers[3] = 0x0000_0000; // Header type 0 (device)
                writable_bits[15] = 0x0000_00ff; // Interrupt line (r/w)
                registers[11] = u32::from(subsystem_id) << 16 | u32::from(subsystem_vendor_id);
            }
            PciHeaderType::Bridge => {
                registers[3] = 0x0001_0000; // Header type 1 (bridge)
                writable_bits[6] = 0x00ff_ffff; // Primary/secondary/subordinate bus number,
                                                // secondary latency timer
                registers[7] = 0x0000_00f0; // IO base > IO Limit, no IO address on secondary side at initialize
                writable_bits[7] = 0xf900_0000; // IO base and limit, secondary status,
                registers[8] = 0x0000_fff0; // mem base > mem Limit, no MMIO address on secondary side at initialize
                writable_bits[8] = 0xfff0_fff0; // Memory base and limit
                registers[9] = 0x0001_fff1; // pmem base > pmem Limit, no prefetch MMIO address on secondary side at initialize
                writable_bits[9] = 0xfff0_fff0; // Prefetchable base and limit
                writable_bits[10] = 0xffff_ffff; // Prefetchable base upper 32 bits
                writable_bits[11] = 0xffff_ffff; // Prefetchable limit upper 32 bits
                writable_bits[15] = 0xffff_00ff; // Bridge control (r/w), interrupt line (r/w)
            }
        };

        PciConfiguration {
            registers,
            writable_bits,
            bar_used: [false; NUM_BAR_REGS],
            bar_configs: [None; NUM_BAR_REGS],
            last_capability: None,
            capability_configs: BTreeMap::new(),
            mmio_mapping: None,
        }
    }

    /// Reads a 32bit register from `reg_idx` in the register map.
    pub fn read_reg(&self, reg_idx: usize) -> u32 {
        let mut data = *(self.registers.get(reg_idx).unwrap_or(&0xffff_ffff));
        if let Some((idx, cfg)) = self.capability_configs.range(..=reg_idx).last() {
            if reg_idx < idx + cfg.num_regs() {
                let cap_idx = reg_idx - idx;
                let mask = cfg.read_mask()[cap_idx];
                data = (data & !mask) | (cfg.read_reg(cap_idx) & mask);
            }
        }
        data
    }

    /// Writes data to PciConfiguration.registers.
    /// `reg_idx` - index into PciConfiguration.registers.
    /// `offset`  - PciConfiguration.registers is in unit of DWord, offset define byte
    ///             offset in the DWord.
    /// `data`    - The data to write.
    pub fn write_reg(
        &mut self,
        reg_idx: usize,
        offset: u64,
        data: &[u8],
    ) -> Option<Box<dyn PciCapConfigWriteResult>> {
        let reg_offset = reg_idx * 4 + offset as usize;
        match data.len() {
            1 => self.write_byte(reg_offset, data[0]),
            2 => self.write_word(reg_offset, u16::from_le_bytes(data.try_into().unwrap())),
            4 => self.write_dword(reg_offset, u32::from_le_bytes(data.try_into().unwrap())),
            _ => (),
        }
        if let Some((idx, cfg)) = self.capability_configs.range_mut(..=reg_idx).last() {
            if reg_idx < idx + cfg.num_regs() {
                let cap_idx = reg_idx - idx;
                let ret = cfg.write_reg(cap_idx, offset, data);
                let new_val = cfg.read_reg(cap_idx);
                let mask = cfg.read_mask()[cap_idx];
                self.set_reg(reg_idx, new_val, mask);
                return ret;
            }
        }
        None
    }

    /// Writes a 32bit dword to `offset`. `offset` must be 32bit aligned.
    fn write_dword(&mut self, offset: usize, value: u32) {
        if offset % 4 != 0 {
            warn!("bad PCI config dword write offset {}", offset);
            return;
        }
        let reg_idx = offset / 4;
        if reg_idx < NUM_CONFIGURATION_REGISTERS {
            let old_value = self.registers[reg_idx];
            let new_value =
                (old_value & !self.writable_bits[reg_idx]) | (value & self.writable_bits[reg_idx]);
            self.do_write(reg_idx, new_value)
        } else {
            warn!("bad PCI dword write {}", offset);
        }
    }

    /// Writes a 16bit word to `offset`. `offset` must be 16bit aligned.
    fn write_word(&mut self, offset: usize, value: u16) {
        let shift = match offset % 4 {
            0 => 0,
            2 => 16,
            _ => {
                warn!("bad PCI config word write offset {}", offset);
                return;
            }
        };
        let reg_idx = offset / 4;

        if reg_idx < NUM_CONFIGURATION_REGISTERS {
            let old_value = self.registers[reg_idx];
            let writable_mask = self.writable_bits[reg_idx];
            let mask = (0xffffu32 << shift) & writable_mask;
            let shifted_value = (u32::from(value) << shift) & writable_mask;
            let new_value = old_value & !mask | shifted_value;
            self.do_write(reg_idx, new_value)
        } else {
            warn!("bad PCI config word write offset {}", offset);
        }
    }

    /// Writes a byte to `offset`.
    fn write_byte(&mut self, offset: usize, value: u8) {
        self.write_byte_internal(offset, value, true);
    }

    /// Writes a byte to `offset`, optionally enforcing read-only bits.
    fn write_byte_internal(&mut self, offset: usize, value: u8, apply_writable_mask: bool) {
        let shift = (offset % 4) * 8;
        let reg_idx = offset / 4;

        if reg_idx < NUM_CONFIGURATION_REGISTERS {
            let writable_mask = if apply_writable_mask {
                self.writable_bits[reg_idx]
            } else {
                0xffff_ffff
            };
            let old_value = self.registers[reg_idx];
            let mask = (0xffu32 << shift) & writable_mask;
            let shifted_value = (u32::from(value) << shift) & writable_mask;
            let new_value = old_value & !mask | shifted_value;
            self.do_write(reg_idx, new_value)
        } else {
            warn!("bad PCI config byte write offset {}", offset);
        }
    }

    /// Sets the value of a PciConfiguration register. This should be used when
    /// device-internal events require changing the configuration space - as such,
    /// the writable bits masks do not apply.
    /// `reg_idx` - index into PciConfiguration.registers.
    /// `data`    - The data to write.
    /// `mask`    - The mask of which bits to modify.
    pub fn set_reg(&mut self, reg_idx: usize, data: u32, mask: u32) {
        if reg_idx >= NUM_CONFIGURATION_REGISTERS {
            return;
        }
        let new_val = (self.registers[reg_idx] & !mask) | (data & mask);
        self.do_write(reg_idx, new_val);
    }

    /// Adds a region specified by `config`.  Configures the specified BAR(s) to
    /// report this region and size to the guest kernel.  Enforces a few constraints
    /// (i.e, region size must be power of two, register not already used). Returns 'None' on
    /// failure all, `Some(BarIndex)` on success.
    pub fn add_pci_bar(&mut self, config: PciBarConfiguration) -> Result<PciBarIndex> {
        if config.bar_idx >= NUM_BAR_REGS {
            return Err(Error::BarInvalid(config.bar_idx));
        }

        if self.bar_used[config.bar_idx] {
            return Err(Error::BarInUse(config.bar_idx));
        }

        if config.size.count_ones() != 1 {
            return Err(Error::BarSizeInvalid(config.size));
        }

        if config.is_expansion_rom() && config.region_type != PciBarRegionType::Memory32BitRegion {
            return Err(Error::BarInvalidRomType);
        }

        let min_size = if config.is_expansion_rom() {
            BAR_ROM_MIN_SIZE
        } else if config.region_type == PciBarRegionType::IoRegion {
            BAR_IO_MIN_SIZE
        } else {
            BAR_MEM_MIN_SIZE
        };

        if config.size < min_size {
            return Err(Error::BarSizeInvalid(config.size));
        }

        if config.addr % config.size != 0 {
            return Err(Error::BarAlignmentInvalid(config.addr, config.size));
        }

        let reg_idx = config.reg_index();
        let end_addr = config
            .addr
            .checked_add(config.size)
            .ok_or(Error::BarAddressInvalid(config.addr, config.size))?;
        match config.region_type {
            PciBarRegionType::Memory32BitRegion | PciBarRegionType::IoRegion => {
                if end_addr > u64::from(u32::max_value()) {
                    return Err(Error::BarAddressInvalid(config.addr, config.size));
                }
            }
            PciBarRegionType::Memory64BitRegion => {
                // The expansion ROM BAR cannot be used for part of a 64-bit BAR.
                if config.bar_idx + 1 >= ROM_BAR_IDX {
                    return Err(Error::BarInvalid64(config.bar_idx));
                }

                if end_addr > u64::max_value() {
                    return Err(Error::BarAddressInvalid(config.addr, config.size));
                }

                if self.bar_used[config.bar_idx + 1] {
                    return Err(Error::BarInUse64(config.bar_idx));
                }

                self.do_write(reg_idx + 1, (config.addr >> 32) as u32);
                self.writable_bits[reg_idx + 1] = !((config.size - 1) >> 32) as u32;
                self.bar_used[config.bar_idx + 1] = true;
            }
        }

        let (mask, lower_bits) = match config.region_type {
            PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => {
                self.registers[COMMAND_REG] |= COMMAND_REG_MEMORY_SPACE_MASK;
                (
                    BAR_MEM_ADDR_MASK,
                    config.prefetchable as u32 | config.region_type as u32,
                )
            }
            PciBarRegionType::IoRegion => {
                self.registers[COMMAND_REG] |= COMMAND_REG_IO_SPACE_MASK;
                (BAR_IO_ADDR_MASK, config.region_type as u32)
            }
        };

        self.do_write(reg_idx, ((config.addr as u32) & mask) | lower_bits);
        self.writable_bits[reg_idx] = !(config.size - 1) as u32;
        if config.is_expansion_rom() {
            self.writable_bits[reg_idx] |= 1; // Expansion ROM enable bit.
        }
        self.bar_used[config.bar_idx] = true;
        self.bar_configs[config.bar_idx] = Some(config);
        Ok(config.bar_idx)
    }

    /// Returns an iterator of the currently configured base address registers.
    #[allow(dead_code)] // TODO(dverkamp): remove this once used
    pub fn get_bars(&self) -> PciBarIter {
        PciBarIter {
            config: self,
            bar_num: 0,
        }
    }

    /// Returns the configuration of a base address register, if present.
    pub fn get_bar_configuration(&self, bar_num: usize) -> Option<PciBarConfiguration> {
        let config = self.bar_configs.get(bar_num)?;

        if let Some(mut config) = config {
            let command = self.read_reg(COMMAND_REG);
            if (config.is_memory() && (command & COMMAND_REG_MEMORY_SPACE_MASK == 0))
                || (config.is_io() && (command & COMMAND_REG_IO_SPACE_MASK == 0))
            {
                return None;
            }

            // The address may have been modified by the guest, so the value in bar_configs
            // may be outdated. Replace it with the current value.
            config.addr = self.get_bar_addr(bar_num);
            Some(config)
        } else {
            None
        }
    }

    /// Returns the type of the given BAR region.
    pub fn get_bar_type(&self, bar_num: PciBarIndex) -> Option<PciBarRegionType> {
        self.bar_configs.get(bar_num)?.map(|c| c.region_type)
    }

    /// Returns the address of the given BAR region.
    pub fn get_bar_addr(&self, bar_num: PciBarIndex) -> u64 {
        let bar_idx = if bar_num == ROM_BAR_IDX {
            ROM_BAR_REG
        } else {
            BAR0_REG + bar_num
        };

        let bar_type = match self.get_bar_type(bar_num) {
            Some(t) => t,
            None => return 0,
        };

        match bar_type {
            PciBarRegionType::IoRegion => u64::from(self.registers[bar_idx] & BAR_IO_ADDR_MASK),
            PciBarRegionType::Memory32BitRegion => {
                u64::from(self.registers[bar_idx] & BAR_MEM_ADDR_MASK)
            }
            PciBarRegionType::Memory64BitRegion => {
                u64::from(self.registers[bar_idx] & BAR_MEM_ADDR_MASK)
                    | u64::from(self.registers[bar_idx + 1]) << 32
            }
        }
    }

    /// Configures the IRQ line and pin used by this device.
    pub fn set_irq(&mut self, line: u8, pin: PciInterruptPin) {
        // `pin` is 1-based in the pci config space.
        let pin_idx = (pin as u32) + 1;
        let new_val = (self.registers[INTERRUPT_LINE_PIN_REG] & 0xffff_0000)
            | (pin_idx << 8)
            | u32::from(line);
        self.do_write(INTERRUPT_LINE_PIN_REG, new_val)
    }

    /// Adds the capability `cap_data` to the list of capabilities.
    /// `cap_data` should include the two-byte PCI capability header (type, next),
    /// but not populate it. Correct values will be generated automatically based
    /// on `cap_data.id()`.
    pub fn add_capability(
        &mut self,
        cap_data: &dyn PciCapability,
        cap_config: Option<Box<dyn PciCapConfig>>,
    ) -> Result<()> {
        let total_len = cap_data.bytes().len();
        // Check that the length is valid.
        if cap_data.bytes().is_empty() {
            return Err(Error::CapabilityEmpty);
        }
        let (cap_offset, tail_offset) = match self.last_capability {
            Some((offset, len)) => (Self::next_dword(offset, len), offset + 1),
            None => (FIRST_CAPABILITY_OFFSET, CAPABILITY_LIST_HEAD_OFFSET),
        };
        let end_offset = cap_offset
            .checked_add(total_len)
            .ok_or(Error::CapabilitySpaceFull(total_len))?;
        if end_offset > CAPABILITY_MAX_OFFSET {
            return Err(Error::CapabilitySpaceFull(total_len));
        }
        self.do_write(
            STATUS_REG,
            self.registers[STATUS_REG] | STATUS_REG_CAPABILITIES_USED_MASK,
        );
        self.write_byte_internal(tail_offset, cap_offset as u8, false);
        self.write_byte_internal(cap_offset, cap_data.id() as u8, false);
        self.write_byte_internal(cap_offset + 1, 0, false); // Next pointer.
        for (i, byte) in cap_data.bytes().iter().enumerate().skip(2) {
            self.write_byte_internal(cap_offset + i, *byte, false);
        }
        let reg_idx = cap_offset / 4;
        for (i, dword) in cap_data.writable_bits().iter().enumerate() {
            self.writable_bits[reg_idx + i] = *dword;
        }
        self.last_capability = Some((cap_offset, total_len));
        if let Some(mut cap_config) = cap_config {
            if let Some((mapping, offset)) = &self.mmio_mapping {
                cap_config.set_cap_mapping(PciCapMapping {
                    mapping: mapping.clone(),
                    offset: reg_idx * 4 + offset,
                    num_regs: total_len / 4,
                });
            }
            self.capability_configs.insert(cap_offset / 4, cap_config);
        }
        Ok(())
    }

    // Find the next aligned offset after the one given.
    fn next_dword(offset: usize, len: usize) -> usize {
        let next = offset + len;
        (next + 3) & !3
    }

    fn do_write(&mut self, reg_idx: usize, value: u32) {
        self.registers[reg_idx] = value;
        if let Some((mmio_mapping, offset)) = self.mmio_mapping.as_ref() {
            let mmio_mapping = mmio_mapping.lock();
            let reg_offset = offset + reg_idx * 4;
            if reg_idx == HEADER_TYPE_REG {
                // Skip writing the header type byte (reg_idx=2/offset=3) as
                // per the requirements of PciDevice.setup_pci_config_mapping.
                mmio_mapping
                    .write_obj_volatile((value & 0xffff) as u16, reg_offset)
                    .expect("bad register offset");
                // Skip HEADER_TYPE_REG_OFFSET (i.e. header+mfd byte)
                mmio_mapping
                    .write_obj_volatile(((value >> 24) & 0xff) as u8, reg_offset + 3)
                    .expect("bad register offset");
            } else {
                mmio_mapping
                    .write_obj_volatile(value, reg_offset)
                    .expect("bad register offset");
            }
            if let Err(err) = mmio_mapping.flush_region(reg_offset, 4) {
                error!(
                    "failed to flush write to pci mmio register ({}): {}",
                    reg_idx, err
                );
            }
        }
    }

    pub fn snapshot(&mut self) -> anyhow::Result<serde_json::Value> {
        serde_json::to_value(PciConfigurationSerialized {
            registers: self.registers,
            writable_bits: self.writable_bits,
            bar_used: self.bar_used,
            bar_configs: self.bar_configs,
            last_capability: self.last_capability,
        })
        .context("failed to serialize PciConfiguration")
    }

    pub fn restore(&mut self, data: serde_json::Value) -> anyhow::Result<()> {
        let deser: PciConfigurationSerialized =
            serde_json::from_value(data).context("failed to deserialize PciConfiguration")?;
        self.registers = deser.registers;
        self.writable_bits = deser.writable_bits;
        self.bar_used = deser.bar_used;
        self.bar_configs = deser.bar_configs;
        self.last_capability = deser.last_capability;
        // Restore everything via do_write to avoid writing to the header type register
        // and clobbering the multi-function device bit, as that bit is managed by the
        // PciRoot. Since restore doesn't change the types or layout of PCI devices, the
        // header type bits in the register are already correct anyway.
        for i in 0..NUM_CONFIGURATION_REGISTERS {
            self.do_write(i, self.registers[i]);
        }
        Ok(())
    }

    pub fn setup_mapping(
        &mut self,
        shmem: &SharedMemory,
        base: usize,
        len: usize,
    ) -> anyhow::Result<()> {
        if self.mmio_mapping.is_some() {
            bail!("PCIe config mmio mapping already initialized");
        }
        let mapping = MemoryMappingBuilder::new(base::pagesize())
            .from_shared_memory(shmem)
            .build()
            .context("Failed to create mapping")?;
        for i in 0..(len / 4) {
            let val = self.registers.get(i).unwrap_or(&0xffff_ffff);
            mapping
                .write_obj_volatile(*val, base + i * 4)
                .expect("memcpy failed");
        }
        let mapping = Arc::new(Mutex::new(mapping));
        for (idx, cap) in self.capability_configs.iter_mut() {
            let mut cap_mapping = PciCapMapping {
                mapping: mapping.clone(),
                offset: idx * 4 + base,
                num_regs: cap.num_regs(),
            };
            for i in 0..cap.num_regs() {
                let val = cap.read_reg(i);
                let mask = cap.read_mask()[i];
                cap_mapping.set_reg(i, val, mask);
            }
            cap.set_cap_mapping(cap_mapping);
        }
        self.mmio_mapping = Some((mapping, base));
        Ok(())
    }
}

impl PciBarConfiguration {
    pub fn new(
        bar_idx: PciBarIndex,
        size: u64,
        region_type: PciBarRegionType,
        prefetchable: PciBarPrefetchable,
    ) -> Self {
        PciBarConfiguration {
            bar_idx,
            addr: 0,
            size,
            region_type,
            prefetchable,
        }
    }

    pub fn bar_index(&self) -> PciBarIndex {
        self.bar_idx
    }

    pub fn reg_index(&self) -> usize {
        if self.bar_idx == ROM_BAR_IDX {
            ROM_BAR_REG
        } else {
            BAR0_REG + self.bar_idx
        }
    }

    pub fn address(&self) -> u64 {
        self.addr
    }

    pub fn address_range(&self) -> std::ops::Range<u64> {
        self.addr..self.addr + self.size
    }

    pub fn set_address(mut self, addr: u64) -> Self {
        self.addr = addr;
        self
    }

    pub fn size(&self) -> u64 {
        self.size
    }

    pub fn is_expansion_rom(&self) -> bool {
        self.bar_idx == ROM_BAR_IDX
    }

    pub fn is_memory(&self) -> bool {
        matches!(
            self.region_type,
            PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion
        )
    }

    pub fn is_64bit_memory(&self) -> bool {
        self.region_type == PciBarRegionType::Memory64BitRegion
    }

    pub fn is_io(&self) -> bool {
        self.region_type == PciBarRegionType::IoRegion
    }

    pub fn is_prefetchable(&self) -> bool {
        self.is_memory() && self.prefetchable == PciBarPrefetchable::Prefetchable
    }
}

impl<T: PciCapConfig + ?Sized> PciCapConfig for Arc<Mutex<T>> {
    fn read_mask(&self) -> &'static [u32] {
        self.lock().read_mask()
    }
    fn read_reg(&self, reg_idx: usize) -> u32 {
        self.lock().read_reg(reg_idx)
    }
    fn write_reg(
        &mut self,
        reg_idx: usize,
        offset: u64,
        data: &[u8],
    ) -> Option<Box<dyn PciCapConfigWriteResult>> {
        self.lock().write_reg(reg_idx, offset, data)
    }
    fn set_cap_mapping(&mut self, mapping: PciCapMapping) {
        self.lock().set_cap_mapping(mapping)
    }
}

/// Struct for updating a capabilitiy's mmio mapping.
pub struct PciCapMapping {
    mapping: Arc<Mutex<MemoryMapping>>,
    offset: usize,
    num_regs: usize,
}

impl PciCapMapping {
    /// Set the bits of register `reg_idx` specified by `mask` to `data`.
    pub fn set_reg(&mut self, reg_idx: usize, data: u32, mask: u32) {
        if reg_idx >= self.num_regs {
            error!(
                "out of bounds register write {} vs {}",
                self.num_regs, reg_idx
            );
            return;
        }
        let mapping = self.mapping.lock();
        let offset = self.offset + reg_idx * 4;
        let cur_value = mapping.read_obj::<u32>(offset).expect("memcpy failed");
        let new_val = (cur_value & !mask) | (data & mask);
        mapping
            .write_obj_volatile(new_val, offset)
            .expect("memcpy failed");
        if let Err(err) = mapping.flush_region(offset, 4) {
            error!(
                "failed to flush write to pci cap in mmio register ({}): {}",
                reg_idx, err
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use zerocopy::AsBytes;

    use super::*;

    #[repr(packed)]
    #[derive(Clone, Copy, AsBytes)]
    #[allow(dead_code)]
    struct TestCap {
        _vndr: u8,
        _next: u8,
        len: u8,
        foo: u8,
    }

    impl PciCapability for TestCap {
        fn bytes(&self) -> &[u8] {
            self.as_bytes()
        }

        fn id(&self) -> PciCapabilityID {
            PciCapabilityID::VendorSpecific
        }

        fn writable_bits(&self) -> Vec<u32> {
            vec![0u32; 1]
        }
    }

    #[test]
    fn add_capability() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            None,
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // Add two capabilities with different contents.
        let cap1 = TestCap {
            _vndr: 0,
            _next: 0,
            len: 4,
            foo: 0xAA,
        };
        let cap1_offset = 64;
        cfg.add_capability(&cap1, None).unwrap();

        let cap2 = TestCap {
            _vndr: 0,
            _next: 0,
            len: 0x04,
            foo: 0x55,
        };
        let cap2_offset = 68;
        cfg.add_capability(&cap2, None).unwrap();

        // The capability list head should be pointing to cap1.
        let cap_ptr = cfg.read_reg(CAPABILITY_LIST_HEAD_OFFSET / 4) & 0xFF;
        assert_eq!(cap1_offset, cap_ptr as usize);

        // Verify the contents of the capabilities.
        let cap1_data = cfg.read_reg(cap1_offset / 4);
        assert_eq!(cap1_data & 0xFF, 0x09); // capability ID
        assert_eq!((cap1_data >> 8) & 0xFF, cap2_offset as u32); // next capability pointer
        assert_eq!((cap1_data >> 16) & 0xFF, 0x04); // cap1.len
        assert_eq!((cap1_data >> 24) & 0xFF, 0xAA); // cap1.foo

        let cap2_data = cfg.read_reg(cap2_offset / 4);
        assert_eq!(cap2_data & 0xFF, 0x09); // capability ID
        assert_eq!((cap2_data >> 8) & 0xFF, 0x00); // next capability pointer
        assert_eq!((cap2_data >> 16) & 0xFF, 0x04); // cap2.len
        assert_eq!((cap2_data >> 24) & 0xFF, 0x55); // cap2.foo
    }

    #[derive(Copy, Clone)]
    enum TestPI {
        Test = 0x5a,
    }

    impl PciProgrammingInterface for TestPI {
        fn get_register_value(&self) -> u8 {
            *self as u8
        }
    }

    #[test]
    fn class_code() {
        let cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        let class_reg = cfg.read_reg(2);
        let class_code = (class_reg >> 24) & 0xFF;
        let subclass = (class_reg >> 16) & 0xFF;
        let prog_if = (class_reg >> 8) & 0xFF;
        assert_eq!(class_code, 0x04);
        assert_eq!(subclass, 0x01);
        assert_eq!(prog_if, 0x5a);
    }

    #[test]
    fn read_only_bits() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // Attempt to overwrite vendor ID and device ID, which are read-only
        cfg.write_reg(0, 0, &[0xBA, 0xAD, 0xF0, 0x0D]);
        // The original vendor and device ID should remain.
        assert_eq!(cfg.read_reg(0), 0x56781234);
    }

    #[test]
    fn query_unused_bar() {
        let cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // No BAR 0 has been configured, so these should return None or 0 as appropriate.
        assert_eq!(cfg.get_bar_type(0), None);
        assert_eq!(cfg.get_bar_addr(0), 0);

        let mut bar_iter = cfg.get_bars();
        assert_eq!(bar_iter.next(), None);
    }

    #[test]
    fn add_pci_bar_mem_64bit() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x10,
                PciBarRegionType::Memory64BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x0123_4567_89AB_CDE0),
        )
        .expect("add_pci_bar failed");

        assert_eq!(
            cfg.get_bar_type(0),
            Some(PciBarRegionType::Memory64BitRegion)
        );
        assert_eq!(cfg.get_bar_addr(0), 0x0123_4567_89AB_CDE0);
        assert_eq!(cfg.writable_bits[BAR0_REG + 1], 0xFFFFFFFF);
        assert_eq!(cfg.writable_bits[BAR0_REG + 0], 0xFFFFFFF0);

        let mut bar_iter = cfg.get_bars();
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x0123_4567_89AB_CDE0,
                size: 0x10,
                bar_idx: 0,
                region_type: PciBarRegionType::Memory64BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(bar_iter.next(), None);
    }

    #[test]
    fn add_pci_bar_mem_32bit() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x10,
                PciBarRegionType::Memory32BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x12345670),
        )
        .expect("add_pci_bar failed");

        assert_eq!(
            cfg.get_bar_type(0),
            Some(PciBarRegionType::Memory32BitRegion)
        );
        assert_eq!(cfg.get_bar_addr(0), 0x12345670);
        assert_eq!(cfg.writable_bits[BAR0_REG], 0xFFFFFFF0);

        let mut bar_iter = cfg.get_bars();
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x12345670,
                size: 0x10,
                bar_idx: 0,
                region_type: PciBarRegionType::Memory32BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(bar_iter.next(), None);
    }

    #[test]
    fn add_pci_bar_io() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x4,
                PciBarRegionType::IoRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x1230),
        )
        .expect("add_pci_bar failed");

        assert_eq!(cfg.get_bar_type(0), Some(PciBarRegionType::IoRegion));
        assert_eq!(cfg.get_bar_addr(0), 0x1230);
        assert_eq!(cfg.writable_bits[BAR0_REG], 0xFFFFFFFC);

        let mut bar_iter = cfg.get_bars();
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x1230,
                size: 0x4,
                bar_idx: 0,
                region_type: PciBarRegionType::IoRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(bar_iter.next(), None);
    }

    #[test]
    fn add_pci_bar_multiple() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // bar_num 0-1: 64-bit memory
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x10,
                PciBarRegionType::Memory64BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x0123_4567_89AB_CDE0),
        )
        .expect("add_pci_bar failed");

        // bar 2: 32-bit memory
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                2,
                0x10,
                PciBarRegionType::Memory32BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x12345670),
        )
        .expect("add_pci_bar failed");

        // bar 3: I/O
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                3,
                0x4,
                PciBarRegionType::IoRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x1230),
        )
        .expect("add_pci_bar failed");

        // Confirm default memory and I/O region configurations.
        let mut bar_iter = cfg.get_bars();
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x0123_4567_89AB_CDE0,
                size: 0x10,
                bar_idx: 0,
                region_type: PciBarRegionType::Memory64BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x12345670,
                size: 0x10,
                bar_idx: 2,
                region_type: PciBarRegionType::Memory32BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x1230,
                size: 0x4,
                bar_idx: 3,
                region_type: PciBarRegionType::IoRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(bar_iter.next(), None);

        // Reassign the address for BAR 0 and verify that get_memory_regions() matches.
        cfg.write_reg(4 + 0, 0, &0xBBAA9980u32.to_le_bytes());
        cfg.write_reg(4 + 1, 0, &0xFFEEDDCCu32.to_le_bytes());

        let mut bar_iter = cfg.get_bars();
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0xFFEE_DDCC_BBAA_9980,
                size: 0x10,
                bar_idx: 0,
                region_type: PciBarRegionType::Memory64BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x12345670,
                size: 0x10,
                bar_idx: 2,
                region_type: PciBarRegionType::Memory32BitRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(
            bar_iter.next(),
            Some(PciBarConfiguration {
                addr: 0x1230,
                size: 0x4,
                bar_idx: 3,
                region_type: PciBarRegionType::IoRegion,
                prefetchable: PciBarPrefetchable::NotPrefetchable
            })
        );
        assert_eq!(bar_iter.next(), None);
    }

    #[test]
    fn add_pci_bar_invalid_size() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // I/O BAR with size 2 (too small)
        assert_eq!(
            cfg.add_pci_bar(
                PciBarConfiguration::new(
                    0,
                    0x2,
                    PciBarRegionType::IoRegion,
                    PciBarPrefetchable::NotPrefetchable,
                )
                .set_address(0x1230),
            ),
            Err(Error::BarSizeInvalid(0x2))
        );

        // I/O BAR with size 3 (not a power of 2)
        assert_eq!(
            cfg.add_pci_bar(
                PciBarConfiguration::new(
                    0,
                    0x3,
                    PciBarRegionType::IoRegion,
                    PciBarPrefetchable::NotPrefetchable,
                )
                .set_address(0x1230),
            ),
            Err(Error::BarSizeInvalid(0x3))
        );

        // Memory BAR with size 8 (too small)
        assert_eq!(
            cfg.add_pci_bar(
                PciBarConfiguration::new(
                    0,
                    0x8,
                    PciBarRegionType::Memory32BitRegion,
                    PciBarPrefetchable::NotPrefetchable,
                )
                .set_address(0x12345670),
            ),
            Err(Error::BarSizeInvalid(0x8))
        );
    }

    #[test]
    fn add_rom_bar() {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        // Attempt to add a 64-bit memory BAR as the expansion ROM (invalid).
        assert_eq!(
            cfg.add_pci_bar(PciBarConfiguration::new(
                ROM_BAR_IDX,
                0x1000,
                PciBarRegionType::Memory64BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            ),),
            Err(Error::BarInvalidRomType)
        );

        // Attempt to add an I/O BAR as the expansion ROM (invalid).
        assert_eq!(
            cfg.add_pci_bar(PciBarConfiguration::new(
                ROM_BAR_IDX,
                0x1000,
                PciBarRegionType::IoRegion,
                PciBarPrefetchable::NotPrefetchable,
            ),),
            Err(Error::BarInvalidRomType)
        );

        // Attempt to add a 1KB memory region as the expansion ROM (too small).
        assert_eq!(
            cfg.add_pci_bar(PciBarConfiguration::new(
                ROM_BAR_IDX,
                1024,
                PciBarRegionType::Memory32BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            ),),
            Err(Error::BarSizeInvalid(1024))
        );

        // Add a 32-bit memory BAR as the expansion ROM (valid).
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                ROM_BAR_IDX,
                0x800,
                PciBarRegionType::Memory32BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x12345000),
        )
        .expect("add_pci_bar failed");

        assert_eq!(
            cfg.get_bar_type(ROM_BAR_IDX),
            Some(PciBarRegionType::Memory32BitRegion)
        );
        assert_eq!(cfg.get_bar_addr(ROM_BAR_IDX), 0x12345000);
        assert_eq!(cfg.read_reg(ROM_BAR_REG), 0x12345000);
        assert_eq!(cfg.writable_bits[ROM_BAR_REG], 0xFFFFF801);
    }

    #[test]
    fn pci_configuration_capability_snapshot_restore() -> anyhow::Result<()> {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        let snap_init = cfg.snapshot().context("failed to snapshot")?;

        // Add a capability.
        let cap1 = TestCap {
            _vndr: 0,
            _next: 0,
            len: 4,
            foo: 0xAA,
        };
        cfg.add_capability(&cap1, None).unwrap();

        let snap_mod = cfg.snapshot().context("failed to snapshot mod")?;
        cfg.restore(snap_init.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_init = cfg.snapshot().context("failed to snapshot restored_init")?;
        assert_eq!(snap_init, snap_restore_init);
        assert_ne!(snap_init, snap_mod);
        cfg.restore(snap_mod.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_mod = cfg.snapshot().context("failed to snapshot restored_mod")?;
        assert_eq!(snap_mod, snap_restore_mod);
        Ok(())
    }

    #[test]
    fn pci_configuration_pci_bar_snapshot_restore() -> anyhow::Result<()> {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        let snap_init = cfg.snapshot().context("failed to snapshot")?;

        // bar_num 0-1: 64-bit memory
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x10,
                PciBarRegionType::Memory64BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x0123_4567_89AB_CDE0),
        )
        .expect("add_pci_bar failed");

        let snap_mod = cfg.snapshot().context("failed to snapshot mod")?;
        cfg.restore(snap_init.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_init = cfg.snapshot().context("failed to snapshot restored_init")?;
        assert_eq!(snap_init, snap_restore_init);
        assert_ne!(snap_init, snap_mod);
        cfg.restore(snap_mod.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_mod = cfg.snapshot().context("failed to snapshot restored_mod")?;
        assert_eq!(snap_mod, snap_restore_mod);
        Ok(())
    }

    #[test]
    fn pci_configuration_capability_pci_bar_snapshot_restore() -> anyhow::Result<()> {
        let mut cfg = PciConfiguration::new(
            0x1234,
            0x5678,
            PciClassCode::MultimediaController,
            &PciMultimediaSubclass::AudioController,
            Some(&TestPI::Test),
            PciHeaderType::Device,
            0xABCD,
            0x2468,
            0,
        );

        let snap_init = cfg.snapshot().context("failed to snapshot")?;

        // Add a capability.
        let cap1 = TestCap {
            _vndr: 0,
            _next: 0,
            len: 4,
            foo: 0xAA,
        };
        cfg.add_capability(&cap1, None).unwrap();

        // bar_num 0-1: 64-bit memory
        cfg.add_pci_bar(
            PciBarConfiguration::new(
                0,
                0x10,
                PciBarRegionType::Memory64BitRegion,
                PciBarPrefetchable::NotPrefetchable,
            )
            .set_address(0x0123_4567_89AB_CDE0),
        )
        .expect("add_pci_bar failed");

        let snap_mod = cfg.snapshot().context("failed to snapshot mod")?;
        cfg.restore(snap_init.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_init = cfg.snapshot().context("failed to snapshot restored_init")?;
        assert_eq!(snap_init, snap_restore_init);
        assert_ne!(snap_init, snap_mod);
        cfg.restore(snap_mod.clone())
            .context("failed to restore snap_init")?;
        let snap_restore_mod = cfg.snapshot().context("failed to snapshot restored_mod")?;
        assert_eq!(snap_mod, snap_restore_mod);
        Ok(())
    }
}