@@ -831,6 +831,10 @@ impl PciConfiguration {
831
831
}
832
832
833
833
pub fn write_config_register ( & mut self , reg_idx : usize , offset : u64 , data : & [ u8 ] ) {
834
+ if reg_idx >= NUM_CONFIGURATION_REGISTERS {
835
+ return ;
836
+ }
837
+
834
838
if offset as usize + data. len ( ) > 4 {
835
839
return ;
836
840
}
@@ -1401,4 +1405,50 @@ mod tests {
1401
1405
assert_eq ! ( pci_config. get_bar_addr( 0 ) , 0x1000 ) ;
1402
1406
assert_eq ! ( pci_config. get_bar_addr( 2 ) , 0x1_0000_0000 ) ;
1403
1407
}
1408
+
1409
+ #[ test]
1410
+ fn test_access_invalid_reg ( ) {
1411
+ let mut pci_config = PciConfiguration :: new (
1412
+ 0x42 ,
1413
+ 0x0 ,
1414
+ 0x0 ,
1415
+ PciClassCode :: MassStorage ,
1416
+ & PciMassStorageSubclass :: SerialScsiController ,
1417
+ None ,
1418
+ PciHeaderType :: Device ,
1419
+ 0x13 ,
1420
+ 0x12 ,
1421
+ None ,
1422
+ None ,
1423
+ ) ;
1424
+
1425
+ // Can't read past the end of the configuration space
1426
+ assert_eq ! (
1427
+ pci_config. read_reg( NUM_CONFIGURATION_REGISTERS ) ,
1428
+ 0xffff_ffff
1429
+ ) ;
1430
+
1431
+ // Read out all of configuration space
1432
+ let config_space: Vec < u32 > = ( 0 ..NUM_CONFIGURATION_REGISTERS )
1433
+ . map ( |reg_idx| pci_config. read_reg ( reg_idx) )
1434
+ . collect ( ) ;
1435
+
1436
+ // Various invalid write accesses
1437
+
1438
+ // Past the end of config space
1439
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 0 , & [ 0x42 ] ) ;
1440
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 0 , & [ 0x42 , 0x42 ] ) ;
1441
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 0 , & [ 0x42 , 0x42 , 0x42 , 0x42 ] ) ;
1442
+
1443
+ // Past register boundaries
1444
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 1 , & [ 0x42 , 0x42 , 0x42 , 0x42 ] ) ;
1445
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 2 , & [ 0x42 , 0x42 , 0x42 ] ) ;
1446
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 3 , & [ 0x42 , 0x42 ] ) ;
1447
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 4 , & [ 0x42 ] ) ;
1448
+ pci_config. write_config_register ( NUM_CONFIGURATION_REGISTERS , 5 , & [ ] ) ;
1449
+
1450
+ for ( reg_idx, reg) in config_space. iter ( ) . enumerate ( ) {
1451
+ assert_eq ! ( * reg, pci_config. read_reg( reg_idx) ) ;
1452
+ }
1453
+ }
1404
1454
}
0 commit comments