aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / virtio_net.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Pure parsing of virtio PCI vendor capabilities. |
| 17 | |
| 18 | /// A memory mapped virtio structure described by a PCI vendor capability. |
| 19 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 20 | pub struct VirtioPciRegion { |
| 21 | pub bar: u8, |
| 22 | pub offset: u32, |
| 23 | pub length: u32, |
| 24 | } |
| 25 | |
| 26 | /// The virtio PCI regions advertised by a device. |
| 27 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 28 | pub struct VirtioPciCapabilities { |
| 29 | pub common_cfg: Option<VirtioPciRegion>, |
| 30 | pub notify_cfg: Option<VirtioPciRegion>, |
| 31 | pub notify_off_multiplier: Option<u32>, |
| 32 | pub isr_cfg: Option<VirtioPciRegion>, |
| 33 | pub device_cfg: Option<VirtioPciRegion>, |
| 34 | } |
| 35 | |
| 36 | /// Errors found while walking a PCI capability list. |
| 37 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 38 | pub enum VirtioPciParseError { |
| 39 | InvalidConfigLength, |
| 40 | CapabilitiesNotPresent, |
| 41 | InvalidPointer, |
| 42 | CapabilityLoop, |
| 43 | ShortCapability, |
| 44 | InvalidRegion, |
| 45 | DuplicateCapability, |
| 46 | } |
| 47 | |
| 48 | /// Parse virtio 1.x PCI vendor capabilities from a PCI configuration image. |
| 49 | /// |
| 50 | /// `config` must contain either the 256-byte conventional config space or the |
| 51 | /// 4096-byte extended config space. This function performs no device access. |
| 52 | pub fn parse_virtio_pci_capabilities( |
| 53 | config: &[u8], |
| 54 | ) -> Result<VirtioPciCapabilities, VirtioPciParseError> { |
| 55 | if config.len() != 256 && config.len() != 4096 { |
| 56 | return Err(VirtioPciParseError::InvalidConfigLength); |
| 57 | } |
| 58 | if read_u16(config, 0x06) & (1 << 4) == 0 { |
| 59 | return Err(VirtioPciParseError::CapabilitiesNotPresent); |
| 60 | } |
| 61 | |
| 62 | let mut result = VirtioPciCapabilities::default(); |
| 63 | let mut pointer = config[0x34] & !0x03; |
| 64 | let mut visited = [false; 256]; |
| 65 | let mut steps = 0; |
| 66 | while pointer != 0 { |
| 67 | let index = usize::from(pointer); |
| 68 | if index < 0x40 |
| 69 | || index |
| 70 | .checked_add(2) |
| 71 | .filter(|end| *end <= config.len()) |
| 72 | .is_none() |
| 73 | { |
| 74 | return Err(VirtioPciParseError::InvalidPointer); |
| 75 | } |
| 76 | if visited[index] || steps >= 48 { |
| 77 | return Err(VirtioPciParseError::CapabilityLoop); |
| 78 | } |
| 79 | visited[index] = true; |
| 80 | steps += 1; |
| 81 | |
| 82 | let next = config[index + 1] & !0x03; |
| 83 | if config[index] == 0x09 { |
| 84 | if index |
| 85 | .checked_add(16) |
| 86 | .filter(|end| *end <= config.len()) |
| 87 | .is_none() |
| 88 | { |
| 89 | return Err(VirtioPciParseError::ShortCapability); |
| 90 | } |
| 91 | let cap_len = usize::from(config[index + 2]); |
| 92 | let kind = config[index + 3]; |
| 93 | let minimum = if kind == 2 { 20 } else { 16 }; |
| 94 | if cap_len < minimum |
| 95 | || index |
| 96 | .checked_add(cap_len) |
| 97 | .filter(|end| *end <= config.len()) |
| 98 | .is_none() |
| 99 | { |
| 100 | return Err(VirtioPciParseError::ShortCapability); |
| 101 | } |
| 102 | let region = VirtioPciRegion { |
| 103 | bar: config[index + 4], |
| 104 | offset: read_u32(config, index + 8), |
| 105 | length: read_u32(config, index + 12), |
| 106 | }; |
| 107 | if region.bar > 5 |
| 108 | || region.length == 0 |
| 109 | || region.offset.checked_add(region.length).is_none() |
| 110 | { |
| 111 | return Err(VirtioPciParseError::InvalidRegion); |
| 112 | } |
| 113 | match kind { |
| 114 | 1 => set_region(&mut result.common_cfg, region)?, |
| 115 | 2 => { |
| 116 | set_region(&mut result.notify_cfg, region)?; |
| 117 | if result.notify_off_multiplier.is_some() { |
| 118 | return Err(VirtioPciParseError::DuplicateCapability); |
| 119 | } |
| 120 | result.notify_off_multiplier = Some(read_u32(config, index + 16)); |
| 121 | } |
| 122 | 3 => set_region(&mut result.isr_cfg, region)?, |
| 123 | 4 => set_region(&mut result.device_cfg, region)?, |
| 124 | _ => {} |
| 125 | } |
| 126 | } |
| 127 | pointer = next; |
| 128 | } |
| 129 | Ok(result) |
| 130 | } |
| 131 | |
| 132 | fn set_region( |
| 133 | slot: &mut Option<VirtioPciRegion>, |
| 134 | region: VirtioPciRegion, |
| 135 | ) -> Result<(), VirtioPciParseError> { |
| 136 | if slot.replace(region).is_some() { |
| 137 | return Err(VirtioPciParseError::DuplicateCapability); |
| 138 | } |
| 139 | Ok(()) |
| 140 | } |
| 141 | |
| 142 | fn read_u16(bytes: &[u8], offset: usize) -> u16 { |
| 143 | u16::from_le_bytes([bytes[offset], bytes[offset + 1]]) |
| 144 | } |
| 145 | |
| 146 | fn read_u32(bytes: &[u8], offset: usize) -> u32 { |
| 147 | u32::from_le_bytes([ |
| 148 | bytes[offset], |
| 149 | bytes[offset + 1], |
| 150 | bytes[offset + 2], |
| 151 | bytes[offset + 3], |
| 152 | ]) |
| 153 | } |
| 154 | |
| 155 | #[cfg(test)] |
| 156 | mod tests { |
| 157 | use super::*; |
| 158 | |
| 159 | fn put_u32(config: &mut [u8], offset: usize, value: u32) { |
| 160 | config[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); |
| 161 | } |
| 162 | |
| 163 | fn put_cap(config: &mut [u8], at: usize, next: u8, kind: u8, bar: u8, offset: u32, len: u32) { |
| 164 | let cap_len = if kind == 2 { 20 } else { 16 }; |
| 165 | config[at..at + cap_len].fill(0); |
| 166 | config[at] = 0x09; |
| 167 | config[at + 1] = next; |
| 168 | config[at + 2] = cap_len as u8; |
| 169 | config[at + 3] = kind; |
| 170 | config[at + 4] = bar; |
| 171 | put_u32(config, at + 8, offset); |
| 172 | put_u32(config, at + 12, len); |
| 173 | if kind == 2 { |
| 174 | put_u32(config, at + 16, 4); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn parses_modern_virtio_net_capabilities() { |
| 180 | let mut config = [0u8; 256]; |
| 181 | config[0..2].copy_from_slice(&0x1af4u16.to_le_bytes()); |
| 182 | config[2..4].copy_from_slice(&0x1041u16.to_le_bytes()); |
| 183 | config[6] = 1 << 4; |
| 184 | config[0x34] = 0x40; |
| 185 | put_cap(&mut config, 0x40, 0x50, 1, 0, 0x1000, 0x100); |
| 186 | put_cap(&mut config, 0x50, 0x64, 2, 2, 0x2000, 0x100); |
| 187 | put_cap(&mut config, 0x64, 0x74, 3, 0, 0x3000, 1); |
| 188 | put_cap(&mut config, 0x74, 0, 4, 0, 0x4000, 0x80); |
| 189 | |
| 190 | let parsed = parse_virtio_pci_capabilities(&config).unwrap(); |
| 191 | assert_eq!( |
| 192 | parsed.common_cfg, |
| 193 | Some(VirtioPciRegion { |
| 194 | bar: 0, |
| 195 | offset: 0x1000, |
| 196 | length: 0x100 |
| 197 | }) |
| 198 | ); |
| 199 | assert_eq!( |
| 200 | parsed.notify_cfg, |
| 201 | Some(VirtioPciRegion { |
| 202 | bar: 2, |
| 203 | offset: 0x2000, |
| 204 | length: 0x100 |
| 205 | }) |
| 206 | ); |
| 207 | assert_eq!(parsed.notify_off_multiplier, Some(4)); |
| 208 | assert_eq!( |
| 209 | parsed.isr_cfg, |
| 210 | Some(VirtioPciRegion { |
| 211 | bar: 0, |
| 212 | offset: 0x3000, |
| 213 | length: 1 |
| 214 | }) |
| 215 | ); |
| 216 | assert_eq!( |
| 217 | parsed.device_cfg, |
| 218 | Some(VirtioPciRegion { |
| 219 | bar: 0, |
| 220 | offset: 0x4000, |
| 221 | length: 0x80 |
| 222 | }) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn rejects_loops_and_short_vendor_capabilities() { |
| 228 | let mut config = [0u8; 256]; |
| 229 | config[6] = 1 << 4; |
| 230 | config[0x34] = 0x40; |
| 231 | put_cap(&mut config, 0x40, 0x40, 1, 0, 0x1000, 0x100); |
| 232 | assert_eq!( |
| 233 | parse_virtio_pci_capabilities(&config), |
| 234 | Err(VirtioPciParseError::CapabilityLoop) |
| 235 | ); |
| 236 | |
| 237 | config[0x41] = 0; |
| 238 | config[0x42] = 8; |
| 239 | assert_eq!( |
| 240 | parse_virtio_pci_capabilities(&config), |
| 241 | Err(VirtioPciParseError::ShortCapability) |
| 242 | ); |
| 243 | } |
| 244 | } |