//! Independent host reference for root-bus QEMU virtio-mmio IRQ bindings. //! The guest reuses the accepted platform::os_k940_virtio_spi implementation. //! This is a discovery result, not ownership of an IRQ or an MMIO mapping. //! Unsupported buses/nexuses and ambiguous phandles are refused, never guessed. const MAX_BYTES: usize = 2 * 1024 * 1024; const MAX_DEPTH: usize = 16; const MAX_HANDLES: usize = 128; pub const MAX_SLOTS: usize = 32; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Invalid, Unsupported, Missing, Ambiguous } type Result = core::result::Result; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Binding { pub base: u64, pub bytes: u64, /// Absolute GIC INTID, including the 32-ID SPI offset. pub intid: u32, /// DT binding: 1 = rising edge, 4 = high level. pub trigger: u32, pub parent: u32, } #[derive(Debug, PartialEq, Eq)] pub struct Bindings { pub distributor: u64, pub distributor_bytes: u64, pub parent: u32, entries: [Binding; MAX_SLOTS], len: usize, } impl Bindings { pub fn as_slice(&self) -> &[Binding] { &self.entries[..self.len] } pub fn for_base(&self, base: u64) -> Result<&Binding> { self.as_slice().iter().find(|item| item.base == base).ok_or(Error::Missing) } } fn word(bytes: &[u8], offset: usize) -> Result { let end = offset.checked_add(4).ok_or(Error::Invalid)?; Ok(u32::from_be_bytes(bytes.get(offset..end).ok_or(Error::Invalid)?.try_into().map_err(|_| Error::Invalid)?)) } fn scalar(bytes: &[u8]) -> Result { if bytes.len() != 4 { return Err(Error::Invalid); } word(bytes, 0) } fn align(n: usize) -> Result { n.checked_add(3).map(|v| v & !3).ok_or(Error::Invalid) } fn nul(bytes: &[u8]) -> Result<&[u8]> { Ok(&bytes[..bytes.iter().position(|v| *v == 0).ok_or(Error::Invalid)?]) } fn exact_string(bytes: &[u8]) -> Result<&[u8]> { let s = nul(bytes)?; if s.len() + 1 != bytes.len() { return Err(Error::Invalid); } Ok(s) } fn compatible(bytes: &[u8], wanted: &[u8]) -> Result { if bytes.last() != Some(&0) || bytes.len() < 2 { return Err(Error::Invalid); } let mut found = false; for value in bytes[..bytes.len()-1].split(|v| *v == 0) { if value.is_empty() { return Err(Error::Invalid); } found |= value == wanted; } Ok(found) } fn cell(bytes: &[u8], count: usize) -> Result { if !(1..=2).contains(&count) || bytes.len() != 4 * count { return Err(Error::Unsupported); } let mut n = 0; for i in 0..count { n = n << 32 | u64::from(word(bytes, i * 4)?); } Ok(n) } fn range(bytes: &[u8], ac: usize, sc: usize) -> Result<(u64, u64)> { if !(1..=2).contains(&ac) || !(1..=2).contains(&sc) || bytes.len() != 4 * (ac + sc) { return Err(Error::Unsupported); } let base = cell(&bytes[..ac*4], ac)?; let size = cell(&bytes[ac*4..], sc)?; if base == 0 || size == 0 || base.checked_add(size).is_none() { return Err(Error::Invalid); } Ok((base, size)) } fn overlap(a: (usize, usize), b: (usize, usize)) -> bool { a.0 < b.1 && b.0 < a.1 } fn sections(blob: &[u8]) -> Result<(&[u8], &[u8])> { if blob.len() < 40 || word(blob, 0)? != 0xd00d_feed { return Err(Error::Invalid); } let total = word(blob, 4)? as usize; if !(40..=MAX_BYTES).contains(&total) || blob.len() < total || word(blob, 20)? != 17 || word(blob, 24)? > 17 { return Err(Error::Invalid); } let b = &blob[..total]; let block = |start: usize, size: usize| -> Result<(usize, usize)> { let end = start.checked_add(size).ok_or(Error::Invalid)?; if start < 40 || size == 0 || end > total { return Err(Error::Invalid); } Ok((start, end)) }; let s = block(word(b, 8)? as usize, word(b, 36)? as usize)?; let t = block(word(b, 12)? as usize, word(b, 32)? as usize)?; if s.0 % 4 != 0 || s.1 % 4 != 0 || overlap(s, t) { return Err(Error::Invalid); } let rstart = word(b, 16)? as usize; if rstart < 40 || rstart % 8 != 0 { return Err(Error::Invalid); } let mut r = rstart; loop { let end = r.checked_add(16).ok_or(Error::Invalid)?; if end > total || end - rstart > 65536 || overlap((rstart, end), s) || overlap((rstart, end), t) { return Err(Error::Invalid); } let address = (u64::from(word(b, r)?) << 32) | u64::from(word(b, r+4)?); let bytes = (u64::from(word(b, r+8)?) << 32) | u64::from(word(b, r+12)?); r = end; if address == 0 && bytes == 0 { break; } if bytes == 0 || address.checked_add(bytes).is_none() { return Err(Error::Invalid); } } Ok((&b[s.0..s.1], &b[t.0..t.1])) } #[derive(Clone, Copy, Default)] struct Node<'a> { seen: u32, children: bool, virtio: bool, gic: bool, disabled: bool, handle: Option, linux_handle: Option, parent: Option, reg: Option<&'a [u8]>, interrupts: Option<&'a [u8]>, interrupt_cells: Option, controller: bool, unsupported: bool, } impl Node<'_> { fn once(&mut self, bit: u32) -> Result<()> { if self.seen & bit != 0 { return Err(Error::Invalid); } self.seen |= bit; Ok(()) } } pub fn discover(blob: &[u8]) -> Result { let (structure, strings) = sections(blob)?; let mut stack = [Node::default(); MAX_DEPTH]; let mut depth = 0usize; let mut cursor = 0usize; let mut root_seen = false; let mut ac = 2usize; let mut sc = 1usize; let mut root_parent = None; let mut handles = [0u32; MAX_HANDLES]; let mut handle_count = 0; let mut controller = None; let mut out = Bindings { distributor: 0, distributor_bytes: 0, parent: 0, entries: [Binding::default(); MAX_SLOTS], len: 0 }; loop { let token = word(structure, cursor)?; cursor += 4; match token { 1 => { if depth == 0 { if root_seen || cursor != 4 { return Err(Error::Invalid); } root_seen = true; } else { stack[depth-1].children = true; } if depth == MAX_DEPTH { return Err(Error::Unsupported); } let name = nul(structure.get(cursor..).ok_or(Error::Invalid)?)?; if (depth == 0) != name.is_empty() { return Err(Error::Invalid); } cursor = align(cursor.checked_add(name.len()+1).ok_or(Error::Invalid)?)?; if cursor > structure.len() { return Err(Error::Invalid); } stack[depth] = Node::default(); depth += 1; } 2 => { if depth == 0 { return Err(Error::Invalid); } let n = stack[depth-1]; if n.handle.is_some() && n.linux_handle.is_some() && n.handle != n.linux_handle { return Err(Error::Ambiguous); } let handle = n.handle.or(n.linux_handle); if let Some(h) = handle { if h == 0 || h == u32::MAX || handles[..handle_count].contains(&h) { return Err(Error::Ambiguous); } if handle_count == MAX_HANDLES { return Err(Error::Unsupported); } handles[handle_count] = h; handle_count += 1; } if !n.disabled && (n.virtio || n.gic) { if depth != 2 || n.unsupported || n.virtio && n.gic { return Err(Error::Unsupported); } let bytes = n.reg.ok_or(Error::Missing)?; if n.gic { let pair = 4 * (ac + sc); if !n.controller || n.interrupt_cells != Some(3) || bytes.len() < pair * 2 || bytes.len() % pair != 0 { return Err(Error::Unsupported); } let (base, size) = range(&bytes[..pair], ac, sc)?; if base % 65536 != 0 || size < 65536 { return Err(Error::Invalid); } let h = handle.ok_or(Error::Missing)?; if controller.replace((base, size, h)).is_some() { return Err(Error::Ambiguous); } } else { let (base, size) = range(bytes, ac, sc)?; if size != 0x200 || base % 0x200 != 0 || n.children { return Err(Error::Unsupported); } let irq = n.interrupts.ok_or(Error::Missing)?; if irq.len() != 12 || word(irq, 0)? != 0 { return Err(Error::Unsupported); } let spi = word(irq, 4)?; let trigger = word(irq, 8)?; if spi > 987 || !matches!(trigger, 1 | 4) { return Err(Error::Unsupported); } let parent = n.parent.or(root_parent).ok_or(Error::Missing)?; let intid = spi + 32; for old in out.as_slice() { if old.base < base + size && base < old.base + old.bytes || old.intid == intid && old.parent == parent { return Err(Error::Ambiguous); } } if out.len == MAX_SLOTS { return Err(Error::Unsupported); } out.entries[out.len] = Binding { base, bytes: size, intid, trigger, parent }; out.len += 1; } } depth -= 1; } 3 => { if depth == 0 || stack[depth-1].children { return Err(Error::Invalid); } let len = word(structure, cursor)? as usize; let name_at = word(structure, cursor+4)? as usize; cursor += 8; let end = cursor.checked_add(len).ok_or(Error::Invalid)?; let value = structure.get(cursor..end).ok_or(Error::Invalid)?; cursor = align(end)?; if cursor > structure.len() { return Err(Error::Invalid); } let name = nul(strings.get(name_at..).ok_or(Error::Invalid)?)?; if name.is_empty() { return Err(Error::Invalid); } let n = &mut stack[depth-1]; match name { b"compatible" => { n.once(1)?; n.virtio = compatible(value, b"virtio,mmio")?; n.gic = compatible(value, b"arm,gic-v3")?; } b"status" => { n.once(2)?; n.disabled = !matches!(exact_string(value)?, b"okay" | b"ok"); } b"reg" => { n.once(4)?; n.reg = Some(value); } b"phandle" => { n.once(8)?; n.handle = Some(scalar(value)?); } b"linux,phandle" => { n.once(16)?; n.linux_handle = Some(scalar(value)?); } b"interrupt-parent" => { n.once(32)?; n.parent = Some(scalar(value)?); if depth == 1 { root_parent = n.parent; } } b"interrupts" => { n.once(64)?; n.interrupts = Some(value); } b"#interrupt-cells" => { n.once(128)?; n.interrupt_cells = Some(scalar(value)?); } b"interrupt-controller" => { n.once(256)?; if !value.is_empty() { return Err(Error::Invalid); } n.controller = true; } b"#address-cells" => { n.once(512)?; if depth == 1 { ac = scalar(value)? as usize; } } b"#size-cells" => { n.once(1024)?; if depth == 1 { sc = scalar(value)? as usize; } } b"interrupts-extended" | b"interrupt-map" | b"interrupt-map-mask" | b"iommus" | b"dma-ranges" => { n.unsupported = true; if depth == 1 { return Err(Error::Unsupported); } } _ => {} } if !(1..=2).contains(&ac) || !(1..=2).contains(&sc) { return Err(Error::Unsupported); } } 4 => {} 9 if root_seen && depth == 0 && cursor == structure.len() => break, _ => return Err(Error::Invalid), } } let (base, size, parent) = controller.ok_or(Error::Missing)?; if out.len == 0 { return Err(Error::Missing); } for item in out.as_slice() { if item.parent != parent { return Err(Error::Missing); } if item.base < base + size && base < item.base + item.bytes { return Err(Error::Ambiguous); } } out.distributor = base; out.distributor_bytes = size; out.parent = parent; Ok(out) }