• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! High-level FDT functions.
16 
17 use crate::bootargs::BootArgsIterator;
18 use crate::device_assignment::{self, DeviceAssignmentInfo, VmDtbo};
19 use crate::helpers::GUEST_PAGE_SIZE;
20 use crate::Box;
21 use crate::RebootReason;
22 use alloc::collections::BTreeMap;
23 use alloc::ffi::CString;
24 use alloc::format;
25 use alloc::vec::Vec;
26 use core::cmp::max;
27 use core::cmp::min;
28 use core::ffi::CStr;
29 use core::fmt;
30 use core::mem::size_of;
31 use core::ops::Range;
32 use cstr::cstr;
33 use fdtpci::PciMemoryFlags;
34 use fdtpci::PciRangeType;
35 use libfdt::AddressRange;
36 use libfdt::CellIterator;
37 use libfdt::Fdt;
38 use libfdt::FdtError;
39 use libfdt::FdtNode;
40 use libfdt::FdtNodeMut;
41 use libfdt::Phandle;
42 use log::debug;
43 use log::error;
44 use log::info;
45 use log::warn;
46 use static_assertions::const_assert;
47 use tinyvec::ArrayVec;
48 use vmbase::fdt::SwiotlbInfo;
49 use vmbase::hyp;
50 use vmbase::layout::{crosvm::MEM_START, MAX_VIRT_ADDR};
51 use vmbase::memory::SIZE_4KB;
52 use vmbase::util::flatten;
53 use vmbase::util::RangeExt as _;
54 use zerocopy::AsBytes as _;
55 
56 /// An enumeration of errors that can occur during the FDT validation.
57 #[derive(Clone, Debug)]
58 pub enum FdtValidationError {
59     /// Invalid CPU count.
60     InvalidCpuCount(usize),
61     /// Invalid VCpufreq Range.
62     InvalidVcpufreq(u64, u64),
63     /// Forbidden /avf/untrusted property.
64     ForbiddenUntrustedProp(&'static CStr),
65 }
66 
67 impl fmt::Display for FdtValidationError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result68     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69         match self {
70             Self::InvalidCpuCount(num_cpus) => write!(f, "Invalid CPU count: {num_cpus}"),
71             Self::InvalidVcpufreq(addr, size) => {
72                 write!(f, "Invalid vcpufreq region: ({addr:#x}, {size:#x})")
73             }
74             Self::ForbiddenUntrustedProp(name) => {
75                 write!(f, "Forbidden /avf/untrusted property '{name:?}'")
76             }
77         }
78     }
79 }
80 
81 /// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is
82 /// not an error.
read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>>83 fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
84     let addr = cstr!("kernel-address");
85     let size = cstr!("kernel-size");
86 
87     if let Some(config) = fdt.node(cstr!("/config"))? {
88         if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) {
89             let addr = addr as usize;
90             let size = size as usize;
91 
92             return Ok(Some(addr..(addr + size)));
93         }
94     }
95 
96     Ok(None)
97 }
98 
99 /// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an
100 /// error as there can be initrd-less VM.
read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>>101 fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
102     let start = cstr!("linux,initrd-start");
103     let end = cstr!("linux,initrd-end");
104 
105     if let Some(chosen) = fdt.chosen()? {
106         if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) {
107             return Ok(Some((start as usize)..(end as usize)));
108         }
109     }
110 
111     Ok(None)
112 }
113 
patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()>114 fn patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()> {
115     let start = u32::try_from(initrd_range.start).unwrap();
116     let end = u32::try_from(initrd_range.end).unwrap();
117 
118     let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
119     node.setprop(cstr!("linux,initrd-start"), &start.to_be_bytes())?;
120     node.setprop(cstr!("linux,initrd-end"), &end.to_be_bytes())?;
121     Ok(())
122 }
123 
read_bootargs_from(fdt: &Fdt) -> libfdt::Result<Option<CString>>124 fn read_bootargs_from(fdt: &Fdt) -> libfdt::Result<Option<CString>> {
125     if let Some(chosen) = fdt.chosen()? {
126         if let Some(bootargs) = chosen.getprop_str(cstr!("bootargs"))? {
127             // We need to copy the string to heap because the original fdt will be invalidated
128             // by the templated DT
129             let copy = CString::new(bootargs.to_bytes()).map_err(|_| FdtError::BadValue)?;
130             return Ok(Some(copy));
131         }
132     }
133     Ok(None)
134 }
135 
patch_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()>136 fn patch_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
137     let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
138     // This function is called before the verification is done. So, we just copy the bootargs to
139     // the new FDT unmodified. This will be filtered again in the modify_for_next_stage function
140     // if the VM is not debuggable.
141     node.setprop(cstr!("bootargs"), bootargs.to_bytes_with_nul())
142 }
143 
144 /// Reads and validates the memory range in the DT.
145 ///
146 /// Only one memory range is expected with the crosvm setup for now.
read_and_validate_memory_range(fdt: &Fdt) -> Result<Range<usize>, RebootReason>147 fn read_and_validate_memory_range(fdt: &Fdt) -> Result<Range<usize>, RebootReason> {
148     let mut memory = fdt.memory().map_err(|e| {
149         error!("Failed to read memory range from DT: {e}");
150         RebootReason::InvalidFdt
151     })?;
152     let range = memory.next().ok_or_else(|| {
153         error!("The /memory node in the DT contains no range.");
154         RebootReason::InvalidFdt
155     })?;
156     if memory.next().is_some() {
157         warn!(
158             "The /memory node in the DT contains more than one memory range, \
159              while only one is expected."
160         );
161     }
162     let base = range.start;
163     if base != MEM_START {
164         error!("Memory base address {:#x} is not {:#x}", base, MEM_START);
165         return Err(RebootReason::InvalidFdt);
166     }
167 
168     let size = range.len();
169     if size % GUEST_PAGE_SIZE != 0 {
170         error!("Memory size {:#x} is not a multiple of page size {:#x}", size, GUEST_PAGE_SIZE);
171         return Err(RebootReason::InvalidFdt);
172     }
173 
174     if size == 0 {
175         error!("Memory size is 0");
176         return Err(RebootReason::InvalidFdt);
177     }
178     Ok(range)
179 }
180 
patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()>181 fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> {
182     let addr = u64::try_from(MEM_START).unwrap();
183     let size = u64::try_from(memory_range.len()).unwrap();
184     fdt.node_mut(cstr!("/memory"))?
185         .ok_or(FdtError::NotFound)?
186         .setprop_inplace(cstr!("reg"), [addr.to_be(), size.to_be()].as_bytes())
187 }
188 
189 #[derive(Debug, Default)]
190 struct CpuInfo {
191     opptable_info: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
192     cpu_capacity: Option<u32>,
193 }
194 
195 impl CpuInfo {
196     const MAX_OPPTABLES: usize = 20;
197 }
198 
read_opp_info_from( opp_node: FdtNode, ) -> libfdt::Result<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>199 fn read_opp_info_from(
200     opp_node: FdtNode,
201 ) -> libfdt::Result<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>> {
202     let mut table = ArrayVec::new();
203     let mut opp_nodes = opp_node.subnodes()?;
204     for subnode in opp_nodes.by_ref().take(table.capacity()) {
205         let prop = subnode.getprop_u64(cstr!("opp-hz"))?.ok_or(FdtError::NotFound)?;
206         table.push(prop);
207     }
208 
209     if opp_nodes.next().is_some() {
210         warn!("OPP table has more than {} entries: discarding extra nodes.", table.capacity());
211     }
212 
213     Ok(table)
214 }
215 
216 #[derive(Debug, Default)]
217 struct ClusterTopology {
218     // TODO: Support multi-level clusters & threads.
219     cores: [Option<usize>; ClusterTopology::MAX_CORES_PER_CLUSTER],
220 }
221 
222 impl ClusterTopology {
223     const MAX_CORES_PER_CLUSTER: usize = 10;
224 }
225 
226 #[derive(Debug, Default)]
227 struct CpuTopology {
228     // TODO: Support sockets.
229     clusters: [Option<ClusterTopology>; CpuTopology::MAX_CLUSTERS],
230 }
231 
232 impl CpuTopology {
233     const MAX_CLUSTERS: usize = 3;
234 }
235 
read_cpu_map_from(fdt: &Fdt) -> libfdt::Result<Option<BTreeMap<Phandle, (usize, usize)>>>236 fn read_cpu_map_from(fdt: &Fdt) -> libfdt::Result<Option<BTreeMap<Phandle, (usize, usize)>>> {
237     let Some(cpu_map) = fdt.node(cstr!("/cpus/cpu-map"))? else {
238         return Ok(None);
239     };
240 
241     let mut topology = BTreeMap::new();
242     for n in 0..CpuTopology::MAX_CLUSTERS {
243         let name = CString::new(format!("cluster{n}")).unwrap();
244         let Some(cluster) = cpu_map.subnode(&name)? else {
245             break;
246         };
247         for m in 0..ClusterTopology::MAX_CORES_PER_CLUSTER {
248             let name = CString::new(format!("core{m}")).unwrap();
249             let Some(core) = cluster.subnode(&name)? else {
250                 break;
251             };
252             let cpu = core.getprop_u32(cstr!("cpu"))?.ok_or(FdtError::NotFound)?;
253             let prev = topology.insert(cpu.try_into()?, (n, m));
254             if prev.is_some() {
255                 return Err(FdtError::BadValue);
256             }
257         }
258     }
259 
260     Ok(Some(topology))
261 }
262 
read_cpu_info_from( fdt: &Fdt, ) -> libfdt::Result<(ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>, Option<CpuTopology>)>263 fn read_cpu_info_from(
264     fdt: &Fdt,
265 ) -> libfdt::Result<(ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>, Option<CpuTopology>)> {
266     let mut cpus = ArrayVec::new();
267 
268     let cpu_map = read_cpu_map_from(fdt)?;
269     let mut topology: CpuTopology = Default::default();
270 
271     let mut cpu_nodes = fdt.compatible_nodes(cstr!("arm,armv8"))?;
272     for (idx, cpu) in cpu_nodes.by_ref().take(cpus.capacity()).enumerate() {
273         let cpu_capacity = cpu.getprop_u32(cstr!("capacity-dmips-mhz"))?;
274         let opp_phandle = cpu.getprop_u32(cstr!("operating-points-v2"))?;
275         let opptable_info = if let Some(phandle) = opp_phandle {
276             let phandle = phandle.try_into()?;
277             let node = fdt.node_with_phandle(phandle)?.ok_or(FdtError::NotFound)?;
278             Some(read_opp_info_from(node)?)
279         } else {
280             None
281         };
282         let info = CpuInfo { opptable_info, cpu_capacity };
283         cpus.push(info);
284 
285         if let Some(ref cpu_map) = cpu_map {
286             let phandle = cpu.get_phandle()?.ok_or(FdtError::NotFound)?;
287             let (cluster, core_idx) = cpu_map.get(&phandle).ok_or(FdtError::BadValue)?;
288             let cluster = topology.clusters[*cluster].get_or_insert(Default::default());
289             if cluster.cores[*core_idx].is_some() {
290                 return Err(FdtError::BadValue);
291             }
292             cluster.cores[*core_idx] = Some(idx);
293         }
294     }
295 
296     if cpu_nodes.next().is_some() {
297         warn!("DT has more than {} CPU nodes: discarding extra nodes.", cpus.capacity());
298     }
299 
300     Ok((cpus, cpu_map.map(|_| topology)))
301 }
302 
validate_cpu_info(cpus: &[CpuInfo]) -> Result<(), FdtValidationError>303 fn validate_cpu_info(cpus: &[CpuInfo]) -> Result<(), FdtValidationError> {
304     if cpus.is_empty() {
305         return Err(FdtValidationError::InvalidCpuCount(0));
306     }
307     Ok(())
308 }
309 
read_vcpufreq_info(fdt: &Fdt) -> libfdt::Result<Option<VcpufreqInfo>>310 fn read_vcpufreq_info(fdt: &Fdt) -> libfdt::Result<Option<VcpufreqInfo>> {
311     let mut nodes = fdt.compatible_nodes(cstr!("virtual,android-v-only-cpufreq"))?;
312     let Some(node) = nodes.next() else {
313         return Ok(None);
314     };
315 
316     if nodes.next().is_some() {
317         warn!("DT has more than 1 cpufreq node: discarding extra nodes.");
318     }
319 
320     let mut regs = node.reg()?.ok_or(FdtError::NotFound)?;
321     let reg = regs.next().ok_or(FdtError::NotFound)?;
322     let size = reg.size.ok_or(FdtError::NotFound)?;
323 
324     Ok(Some(VcpufreqInfo { addr: reg.addr, size }))
325 }
326 
validate_vcpufreq_info( vcpufreq_info: &VcpufreqInfo, cpus: &[CpuInfo], ) -> Result<(), FdtValidationError>327 fn validate_vcpufreq_info(
328     vcpufreq_info: &VcpufreqInfo,
329     cpus: &[CpuInfo],
330 ) -> Result<(), FdtValidationError> {
331     const VCPUFREQ_BASE_ADDR: u64 = 0x1040000;
332     const VCPUFREQ_SIZE_PER_CPU: u64 = 0x8;
333 
334     let base = vcpufreq_info.addr;
335     let size = vcpufreq_info.size;
336     let expected_size = VCPUFREQ_SIZE_PER_CPU * cpus.len() as u64;
337 
338     if (base, size) != (VCPUFREQ_BASE_ADDR, expected_size) {
339         return Err(FdtValidationError::InvalidVcpufreq(base, size));
340     }
341 
342     Ok(())
343 }
344 
patch_opptable( node: FdtNodeMut, opptable: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>, ) -> libfdt::Result<()>345 fn patch_opptable(
346     node: FdtNodeMut,
347     opptable: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
348 ) -> libfdt::Result<()> {
349     let oppcompat = cstr!("operating-points-v2");
350     let next = node.next_compatible(oppcompat)?.ok_or(FdtError::NoSpace)?;
351 
352     let Some(opptable) = opptable else {
353         return next.nop();
354     };
355 
356     let mut next_subnode = next.first_subnode()?;
357 
358     for entry in opptable {
359         let mut subnode = next_subnode.ok_or(FdtError::NoSpace)?;
360         subnode.setprop_inplace(cstr!("opp-hz"), &entry.to_be_bytes())?;
361         next_subnode = subnode.next_subnode()?;
362     }
363 
364     while let Some(current) = next_subnode {
365         next_subnode = current.delete_and_next_subnode()?;
366     }
367 
368     Ok(())
369 }
370 
371 // TODO(ptosi): Rework FdtNodeMut and replace this function.
get_nth_compatible<'a>( fdt: &'a mut Fdt, n: usize, compat: &CStr, ) -> libfdt::Result<Option<FdtNodeMut<'a>>>372 fn get_nth_compatible<'a>(
373     fdt: &'a mut Fdt,
374     n: usize,
375     compat: &CStr,
376 ) -> libfdt::Result<Option<FdtNodeMut<'a>>> {
377     let mut node = fdt.root_mut().next_compatible(compat)?;
378     for _ in 0..n {
379         node = node.ok_or(FdtError::NoSpace)?.next_compatible(compat)?;
380     }
381     Ok(node)
382 }
383 
patch_cpus( fdt: &mut Fdt, cpus: &[CpuInfo], topology: &Option<CpuTopology>, ) -> libfdt::Result<()>384 fn patch_cpus(
385     fdt: &mut Fdt,
386     cpus: &[CpuInfo],
387     topology: &Option<CpuTopology>,
388 ) -> libfdt::Result<()> {
389     const COMPAT: &CStr = cstr!("arm,armv8");
390     let mut cpu_phandles = Vec::new();
391     for (idx, cpu) in cpus.iter().enumerate() {
392         let mut cur = get_nth_compatible(fdt, idx, COMPAT)?.ok_or(FdtError::NoSpace)?;
393         let phandle = cur.as_node().get_phandle()?.unwrap();
394         cpu_phandles.push(phandle);
395         if let Some(cpu_capacity) = cpu.cpu_capacity {
396             cur.setprop_inplace(cstr!("capacity-dmips-mhz"), &cpu_capacity.to_be_bytes())?;
397         }
398         patch_opptable(cur, cpu.opptable_info)?;
399     }
400     let mut next = get_nth_compatible(fdt, cpus.len(), COMPAT)?;
401     while let Some(current) = next {
402         next = current.delete_and_next_compatible(COMPAT)?;
403     }
404 
405     if let Some(topology) = topology {
406         for (n, cluster) in topology.clusters.iter().enumerate() {
407             let path = CString::new(format!("/cpus/cpu-map/cluster{n}")).unwrap();
408             let cluster_node = fdt.node_mut(&path)?.unwrap();
409             if let Some(cluster) = cluster {
410                 let mut iter = cluster_node.first_subnode()?;
411                 for core in cluster.cores {
412                     let mut core_node = iter.unwrap();
413                     iter = if let Some(core_idx) = core {
414                         let phandle = *cpu_phandles.get(core_idx).unwrap();
415                         let value = u32::from(phandle).to_be_bytes();
416                         core_node.setprop_inplace(cstr!("cpu"), &value)?;
417                         core_node.next_subnode()?
418                     } else {
419                         core_node.delete_and_next_subnode()?
420                     };
421                 }
422                 assert!(iter.is_none());
423             } else {
424                 cluster_node.nop()?;
425             }
426         }
427     } else {
428         fdt.node_mut(cstr!("/cpus/cpu-map"))?.unwrap().nop()?;
429     }
430 
431     Ok(())
432 }
433 
434 /// Reads the /avf/untrusted DT node, which the host can use to pass properties (no subnodes) to
435 /// the guest that don't require being validated by pvmfw.
parse_untrusted_props(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>>436 fn parse_untrusted_props(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
437     let mut props = BTreeMap::new();
438     if let Some(node) = fdt.node(cstr!("/avf/untrusted"))? {
439         for property in node.properties()? {
440             let name = property.name()?;
441             let value = property.value()?;
442             props.insert(CString::from(name), value.to_vec());
443         }
444         if node.subnodes()?.next().is_some() {
445             warn!("Discarding unexpected /avf/untrusted subnodes.");
446         }
447     }
448 
449     Ok(props)
450 }
451 
452 /// Read candidate properties' names from DT which could be overlaid
parse_vm_ref_dt(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>>453 fn parse_vm_ref_dt(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
454     let mut property_map = BTreeMap::new();
455     if let Some(avf_node) = fdt.node(cstr!("/avf"))? {
456         for property in avf_node.properties()? {
457             let name = property.name()?;
458             let value = property.value()?;
459             property_map.insert(
460                 CString::new(name.to_bytes()).map_err(|_| FdtError::BadValue)?,
461                 value.to_vec(),
462             );
463         }
464     }
465     Ok(property_map)
466 }
467 
validate_untrusted_props(props: &BTreeMap<CString, Vec<u8>>) -> Result<(), FdtValidationError>468 fn validate_untrusted_props(props: &BTreeMap<CString, Vec<u8>>) -> Result<(), FdtValidationError> {
469     const FORBIDDEN_PROPS: &[&CStr] =
470         &[cstr!("compatible"), cstr!("linux,phandle"), cstr!("phandle")];
471 
472     for name in FORBIDDEN_PROPS {
473         if props.contains_key(*name) {
474             return Err(FdtValidationError::ForbiddenUntrustedProp(name));
475         }
476     }
477 
478     Ok(())
479 }
480 
481 /// Overlay VM reference DT into VM DT based on the props_info. Property is overlaid in vm_dt only
482 /// when it exists both in vm_ref_dt and props_info. If the values mismatch, it returns error.
validate_vm_ref_dt( vm_dt: &mut Fdt, vm_ref_dt: &Fdt, props_info: &BTreeMap<CString, Vec<u8>>, ) -> libfdt::Result<()>483 fn validate_vm_ref_dt(
484     vm_dt: &mut Fdt,
485     vm_ref_dt: &Fdt,
486     props_info: &BTreeMap<CString, Vec<u8>>,
487 ) -> libfdt::Result<()> {
488     let root_vm_dt = vm_dt.root_mut();
489     let mut avf_vm_dt = root_vm_dt.add_subnode(cstr!("avf"))?;
490     // TODO(b/318431677): Validate nodes beyond /avf.
491     let avf_node = vm_ref_dt.node(cstr!("/avf"))?.ok_or(FdtError::NotFound)?;
492     for (name, value) in props_info.iter() {
493         if let Some(ref_value) = avf_node.getprop(name)? {
494             if value != ref_value {
495                 error!(
496                     "Property mismatches while applying overlay VM reference DT. \
497                     Name:{:?}, Value from host as hex:{:x?}, Value from VM reference DT as hex:{:x?}",
498                     name, value, ref_value
499                 );
500                 return Err(FdtError::BadValue);
501             }
502             avf_vm_dt.setprop(name, ref_value)?;
503         }
504     }
505     Ok(())
506 }
507 
508 #[derive(Debug)]
509 struct PciInfo {
510     ranges: [PciAddrRange; 2],
511     irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>,
512     irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>,
513 }
514 
515 impl PciInfo {
516     const IRQ_MASK_CELLS: usize = 4;
517     const IRQ_MAP_CELLS: usize = 10;
518     const MAX_IRQS: usize = 16;
519 }
520 
521 type PciAddrRange = AddressRange<(u32, u64), u64, u64>;
522 type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS];
523 type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS];
524 
525 /// Iterator that takes N cells as a chunk
526 struct CellChunkIterator<'a, const N: usize> {
527     cells: CellIterator<'a>,
528 }
529 
530 impl<'a, const N: usize> CellChunkIterator<'a, N> {
new(cells: CellIterator<'a>) -> Self531     fn new(cells: CellIterator<'a>) -> Self {
532         Self { cells }
533     }
534 }
535 
536 impl<'a, const N: usize> Iterator for CellChunkIterator<'a, N> {
537     type Item = [u32; N];
next(&mut self) -> Option<Self::Item>538     fn next(&mut self) -> Option<Self::Item> {
539         let mut ret: Self::Item = [0; N];
540         for i in ret.iter_mut() {
541             *i = self.cells.next()?;
542         }
543         Some(ret)
544     }
545 }
546 
547 /// Read pci host controller ranges, irq maps, and irq map masks from DT
read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo>548 fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> {
549     let node =
550         fdt.compatible_nodes(cstr!("pci-host-cam-generic"))?.next().ok_or(FdtError::NotFound)?;
551 
552     let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?;
553     let range0 = ranges.next().ok_or(FdtError::NotFound)?;
554     let range1 = ranges.next().ok_or(FdtError::NotFound)?;
555 
556     let irq_masks = node.getprop_cells(cstr!("interrupt-map-mask"))?.ok_or(FdtError::NotFound)?;
557     let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks);
558     let irq_masks = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
559 
560     if chunks.next().is_some() {
561         warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
562         return Err(FdtError::NoSpace);
563     }
564 
565     let irq_maps = node.getprop_cells(cstr!("interrupt-map"))?.ok_or(FdtError::NotFound)?;
566     let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps);
567     let irq_maps = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
568 
569     if chunks.next().is_some() {
570         warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
571         return Err(FdtError::NoSpace);
572     }
573 
574     Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps })
575 }
576 
validate_pci_info(pci_info: &PciInfo, memory_range: &Range<usize>) -> Result<(), RebootReason>577 fn validate_pci_info(pci_info: &PciInfo, memory_range: &Range<usize>) -> Result<(), RebootReason> {
578     for range in pci_info.ranges.iter() {
579         validate_pci_addr_range(range, memory_range)?;
580     }
581     for irq_mask in pci_info.irq_masks.iter() {
582         validate_pci_irq_mask(irq_mask)?;
583     }
584     for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() {
585         validate_pci_irq_map(irq_map, idx)?;
586     }
587     Ok(())
588 }
589 
validate_pci_addr_range( range: &PciAddrRange, memory_range: &Range<usize>, ) -> Result<(), RebootReason>590 fn validate_pci_addr_range(
591     range: &PciAddrRange,
592     memory_range: &Range<usize>,
593 ) -> Result<(), RebootReason> {
594     let mem_flags = PciMemoryFlags(range.addr.0);
595     let range_type = mem_flags.range_type();
596     let prefetchable = mem_flags.prefetchable();
597     let bus_addr = range.addr.1;
598     let cpu_addr = range.parent_addr;
599     let size = range.size;
600 
601     if range_type != PciRangeType::Memory64 {
602         error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr);
603         return Err(RebootReason::InvalidFdt);
604     }
605     if prefetchable {
606         error!("PCI bus address {:#x} in PCI node is prefetchable", bus_addr);
607         return Err(RebootReason::InvalidFdt);
608     }
609     // Enforce ID bus-to-cpu mappings, as used by crosvm.
610     if bus_addr != cpu_addr {
611         error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr);
612         return Err(RebootReason::InvalidFdt);
613     }
614 
615     let Some(bus_end) = bus_addr.checked_add(size) else {
616         error!("PCI address range size {:#x} overflows", size);
617         return Err(RebootReason::InvalidFdt);
618     };
619     if bus_end > MAX_VIRT_ADDR.try_into().unwrap() {
620         error!("PCI address end {:#x} is outside of translatable range", bus_end);
621         return Err(RebootReason::InvalidFdt);
622     }
623 
624     let memory_start = memory_range.start.try_into().unwrap();
625     let memory_end = memory_range.end.try_into().unwrap();
626 
627     if max(bus_addr, memory_start) < min(bus_end, memory_end) {
628         error!(
629             "PCI address range {:#x}-{:#x} overlaps with main memory range {:#x}-{:#x}",
630             bus_addr, bus_end, memory_start, memory_end
631         );
632         return Err(RebootReason::InvalidFdt);
633     }
634 
635     Ok(())
636 }
637 
validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason>638 fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> {
639     const IRQ_MASK_ADDR_HI: u32 = 0xf800;
640     const IRQ_MASK_ADDR_ME: u32 = 0x0;
641     const IRQ_MASK_ADDR_LO: u32 = 0x0;
642     const IRQ_MASK_ANY_IRQ: u32 = 0x7;
643     const EXPECTED: PciIrqMask =
644         [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ];
645     if *irq_mask != EXPECTED {
646         error!("Invalid PCI irq mask {:#?}", irq_mask);
647         return Err(RebootReason::InvalidFdt);
648     }
649     Ok(())
650 }
651 
validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason>652 fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> {
653     const PCI_DEVICE_IDX: usize = 11;
654     const PCI_IRQ_ADDR_ME: u32 = 0;
655     const PCI_IRQ_ADDR_LO: u32 = 0;
656     const PCI_IRQ_INTC: u32 = 1;
657     const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs
658     const GIC_SPI: u32 = 0;
659     const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
660 
661     let pci_addr = (irq_map[0], irq_map[1], irq_map[2]);
662     let pci_irq_number = irq_map[3];
663     let _controller_phandle = irq_map[4]; // skipped.
664     let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC
665                                              // interrupt-cells is <3> for GIC
666     let gic_peripheral_interrupt_type = irq_map[7];
667     let gic_irq_number = irq_map[8];
668     let gic_irq_type = irq_map[9];
669 
670     let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32;
671     let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO);
672 
673     if pci_addr != expected_pci_addr {
674         error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \
675                {:#x} {:#x} {:#x}",
676                pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2);
677         return Err(RebootReason::InvalidFdt);
678     }
679 
680     if pci_irq_number != PCI_IRQ_INTC {
681         error!(
682             "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}",
683             pci_irq_number, PCI_IRQ_INTC
684         );
685         return Err(RebootReason::InvalidFdt);
686     }
687 
688     if gic_addr != (0, 0) {
689         error!(
690             "GIC address {:#x} {:#x} in interrupt-map is different from expected address \
691                {:#x} {:#x}",
692             gic_addr.0, gic_addr.1, 0, 0
693         );
694         return Err(RebootReason::InvalidFdt);
695     }
696 
697     if gic_peripheral_interrupt_type != GIC_SPI {
698         error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \
699                {:#x}", gic_peripheral_interrupt_type, GIC_SPI);
700         return Err(RebootReason::InvalidFdt);
701     }
702 
703     let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32);
704     if gic_irq_number != irq_nr {
705         error!(
706             "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}",
707             gic_irq_number, irq_nr
708         );
709         return Err(RebootReason::InvalidFdt);
710     }
711 
712     if gic_irq_type != IRQ_TYPE_LEVEL_HIGH {
713         error!(
714             "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}",
715             gic_irq_type, IRQ_TYPE_LEVEL_HIGH
716         );
717         return Err(RebootReason::InvalidFdt);
718     }
719     Ok(())
720 }
721 
patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()>722 fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> {
723     let mut node =
724         fdt.root_mut().next_compatible(cstr!("pci-host-cam-generic"))?.ok_or(FdtError::NotFound)?;
725 
726     let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>();
727     node.trimprop(cstr!("interrupt-map-mask"), irq_masks_size)?;
728 
729     let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>();
730     node.trimprop(cstr!("interrupt-map"), irq_maps_size)?;
731 
732     node.setprop_inplace(
733         cstr!("ranges"),
734         flatten(&[pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()]),
735     )
736 }
737 
738 #[derive(Default, Debug)]
739 struct SerialInfo {
740     addrs: ArrayVec<[u64; Self::MAX_SERIALS]>,
741 }
742 
743 impl SerialInfo {
744     const MAX_SERIALS: usize = 4;
745 }
746 
read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo>747 fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> {
748     let mut addrs = ArrayVec::new();
749 
750     let mut serial_nodes = fdt.compatible_nodes(cstr!("ns16550a"))?;
751     for node in serial_nodes.by_ref().take(addrs.capacity()) {
752         let reg = node.first_reg()?;
753         addrs.push(reg.addr);
754     }
755     if serial_nodes.next().is_some() {
756         warn!("DT has more than {} UART nodes: discarding extra nodes.", addrs.capacity());
757     }
758 
759     Ok(SerialInfo { addrs })
760 }
761 
762 /// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown
patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()>763 fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> {
764     let name = cstr!("ns16550a");
765     let mut next = fdt.root_mut().next_compatible(name);
766     while let Some(current) = next? {
767         let reg =
768             current.as_node().reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?;
769         next = if !serial_info.addrs.contains(&reg.addr) {
770             current.delete_and_next_compatible(name)
771         } else {
772             current.next_compatible(name)
773         }
774     }
775     Ok(())
776 }
777 
validate_swiotlb_info( swiotlb_info: &SwiotlbInfo, memory: &Range<usize>, ) -> Result<(), RebootReason>778 fn validate_swiotlb_info(
779     swiotlb_info: &SwiotlbInfo,
780     memory: &Range<usize>,
781 ) -> Result<(), RebootReason> {
782     let size = swiotlb_info.size;
783     let align = swiotlb_info.align;
784 
785     if size == 0 || (size % GUEST_PAGE_SIZE) != 0 {
786         error!("Invalid swiotlb size {:#x}", size);
787         return Err(RebootReason::InvalidFdt);
788     }
789 
790     if let Some(align) = align.filter(|&a| a % GUEST_PAGE_SIZE != 0) {
791         error!("Invalid swiotlb alignment {:#x}", align);
792         return Err(RebootReason::InvalidFdt);
793     }
794 
795     if let Some(addr) = swiotlb_info.addr {
796         if addr.checked_add(size).is_none() {
797             error!("Invalid swiotlb range: addr:{addr:#x} size:{size:#x}");
798             return Err(RebootReason::InvalidFdt);
799         }
800     }
801     if let Some(range) = swiotlb_info.fixed_range() {
802         if !range.is_within(memory) {
803             error!("swiotlb range {range:#x?} not part of memory range {memory:#x?}");
804             return Err(RebootReason::InvalidFdt);
805         }
806     }
807 
808     Ok(())
809 }
810 
patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()>811 fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> {
812     let mut node =
813         fdt.root_mut().next_compatible(cstr!("restricted-dma-pool"))?.ok_or(FdtError::NotFound)?;
814 
815     if let Some(range) = swiotlb_info.fixed_range() {
816         node.setprop_addrrange_inplace(
817             cstr!("reg"),
818             range.start.try_into().unwrap(),
819             range.len().try_into().unwrap(),
820         )?;
821         node.nop_property(cstr!("size"))?;
822         node.nop_property(cstr!("alignment"))?;
823     } else {
824         node.nop_property(cstr!("reg"))?;
825         node.setprop_inplace(cstr!("size"), &swiotlb_info.size.to_be_bytes())?;
826         node.setprop_inplace(cstr!("alignment"), &swiotlb_info.align.unwrap().to_be_bytes())?;
827     }
828 
829     Ok(())
830 }
831 
patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()>832 fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
833     let node = fdt.compatible_nodes(cstr!("arm,gic-v3"))?.next().ok_or(FdtError::NotFound)?;
834     let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
835     let range0 = ranges.next().ok_or(FdtError::NotFound)?;
836     let mut range1 = ranges.next().ok_or(FdtError::NotFound)?;
837 
838     let addr = range0.addr;
839     // `read_cpu_info_from()` guarantees that we have at most MAX_CPUS.
840     const_assert!(DeviceTreeInfo::gic_patched_size(DeviceTreeInfo::MAX_CPUS).is_some());
841     let size = u64::try_from(DeviceTreeInfo::gic_patched_size(num_cpus).unwrap()).unwrap();
842 
843     // range1 is just below range0
844     range1.addr = addr - size;
845     range1.size = Some(size);
846 
847     let (addr0, size0) = range0.to_cells();
848     let (addr1, size1) = range1.to_cells();
849     let value = [addr0, size0.unwrap(), addr1, size1.unwrap()];
850 
851     let mut node =
852         fdt.root_mut().next_compatible(cstr!("arm,gic-v3"))?.ok_or(FdtError::NotFound)?;
853     node.setprop_inplace(cstr!("reg"), flatten(&value))
854 }
855 
patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()>856 fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
857     const NUM_INTERRUPTS: usize = 4;
858     const CELLS_PER_INTERRUPT: usize = 3;
859     let node = fdt.compatible_nodes(cstr!("arm,armv8-timer"))?.next().ok_or(FdtError::NotFound)?;
860     let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?;
861     let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> =
862         interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect();
863 
864     let num_cpus: u32 = num_cpus.try_into().unwrap();
865     let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8;
866     for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) {
867         *v |= cpu_mask;
868     }
869     for v in value.iter_mut() {
870         *v = v.to_be();
871     }
872 
873     let value = value.into_inner();
874 
875     let mut node =
876         fdt.root_mut().next_compatible(cstr!("arm,armv8-timer"))?.ok_or(FdtError::NotFound)?;
877     node.setprop_inplace(cstr!("interrupts"), value.as_bytes())
878 }
879 
patch_untrusted_props(fdt: &mut Fdt, props: &BTreeMap<CString, Vec<u8>>) -> libfdt::Result<()>880 fn patch_untrusted_props(fdt: &mut Fdt, props: &BTreeMap<CString, Vec<u8>>) -> libfdt::Result<()> {
881     let avf_node = if let Some(node) = fdt.node_mut(cstr!("/avf"))? {
882         node
883     } else {
884         fdt.root_mut().add_subnode(cstr!("avf"))?
885     };
886 
887     // The node shouldn't already be present; if it is, return the error.
888     let mut node = avf_node.add_subnode(cstr!("untrusted"))?;
889 
890     for (name, value) in props {
891         node.setprop(name, value)?;
892     }
893 
894     Ok(())
895 }
896 
897 #[derive(Debug)]
898 struct VcpufreqInfo {
899     addr: u64,
900     size: u64,
901 }
902 
patch_vcpufreq(fdt: &mut Fdt, vcpufreq_info: &Option<VcpufreqInfo>) -> libfdt::Result<()>903 fn patch_vcpufreq(fdt: &mut Fdt, vcpufreq_info: &Option<VcpufreqInfo>) -> libfdt::Result<()> {
904     let mut node = fdt.node_mut(cstr!("/cpufreq"))?.unwrap();
905     if let Some(info) = vcpufreq_info {
906         node.setprop_addrrange_inplace(cstr!("reg"), info.addr, info.size)
907     } else {
908         node.nop()
909     }
910 }
911 
912 #[derive(Debug)]
913 pub struct DeviceTreeInfo {
914     pub kernel_range: Option<Range<usize>>,
915     pub initrd_range: Option<Range<usize>>,
916     pub memory_range: Range<usize>,
917     bootargs: Option<CString>,
918     cpus: ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>,
919     cpu_topology: Option<CpuTopology>,
920     pci_info: PciInfo,
921     serial_info: SerialInfo,
922     pub swiotlb_info: SwiotlbInfo,
923     device_assignment: Option<DeviceAssignmentInfo>,
924     untrusted_props: BTreeMap<CString, Vec<u8>>,
925     vm_ref_dt_props_info: BTreeMap<CString, Vec<u8>>,
926     vcpufreq_info: Option<VcpufreqInfo>,
927 }
928 
929 impl DeviceTreeInfo {
930     const MAX_CPUS: usize = 16;
931 
gic_patched_size(num_cpus: usize) -> Option<usize>932     const fn gic_patched_size(num_cpus: usize) -> Option<usize> {
933         const GIC_REDIST_SIZE_PER_CPU: usize = 32 * SIZE_4KB;
934 
935         GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus)
936     }
937 }
938 
sanitize_device_tree( fdt: &mut [u8], vm_dtbo: Option<&mut [u8]>, vm_ref_dt: Option<&[u8]>, ) -> Result<DeviceTreeInfo, RebootReason>939 pub fn sanitize_device_tree(
940     fdt: &mut [u8],
941     vm_dtbo: Option<&mut [u8]>,
942     vm_ref_dt: Option<&[u8]>,
943 ) -> Result<DeviceTreeInfo, RebootReason> {
944     let fdt = Fdt::from_mut_slice(fdt).map_err(|e| {
945         error!("Failed to load FDT: {e}");
946         RebootReason::InvalidFdt
947     })?;
948 
949     let vm_dtbo = match vm_dtbo {
950         Some(vm_dtbo) => Some(VmDtbo::from_mut_slice(vm_dtbo).map_err(|e| {
951             error!("Failed to load VM DTBO: {e}");
952             RebootReason::InvalidFdt
953         })?),
954         None => None,
955     };
956 
957     let info = parse_device_tree(fdt, vm_dtbo.as_deref())?;
958 
959     // SAFETY: We trust that the template (hardcoded in our RO data) is a valid DT.
960     let fdt_template = unsafe { Fdt::unchecked_from_slice(pvmfw_fdt_template::RAW) };
961     fdt.clone_from(fdt_template).map_err(|e| {
962         error!("Failed to instantiate FDT from the template DT: {e}");
963         RebootReason::InvalidFdt
964     })?;
965 
966     fdt.unpack().map_err(|e| {
967         error!("Failed to unpack DT for patching: {e}");
968         RebootReason::InvalidFdt
969     })?;
970 
971     if let Some(device_assignment_info) = &info.device_assignment {
972         let vm_dtbo = vm_dtbo.unwrap();
973         device_assignment_info.filter(vm_dtbo).map_err(|e| {
974             error!("Failed to filter VM DTBO: {e}");
975             RebootReason::InvalidFdt
976         })?;
977         // SAFETY: Damaged VM DTBO isn't used in this API after this unsafe block.
978         // VM DTBO can't be reused in any way as Fdt nor VmDtbo outside of this API because
979         // it can only be instantiated after validation.
980         unsafe {
981             fdt.apply_overlay(vm_dtbo.as_mut()).map_err(|e| {
982                 error!("Failed to apply filtered VM DTBO: {e}");
983                 RebootReason::InvalidFdt
984             })?;
985         }
986     }
987 
988     if let Some(vm_ref_dt) = vm_ref_dt {
989         let vm_ref_dt = Fdt::from_slice(vm_ref_dt).map_err(|e| {
990             error!("Failed to load VM reference DT: {e}");
991             RebootReason::InvalidFdt
992         })?;
993 
994         validate_vm_ref_dt(fdt, vm_ref_dt, &info.vm_ref_dt_props_info).map_err(|e| {
995             error!("Failed to apply VM reference DT: {e}");
996             RebootReason::InvalidFdt
997         })?;
998     }
999 
1000     patch_device_tree(fdt, &info)?;
1001 
1002     // TODO(b/317201360): Ensure no overlapping in <reg> among devices
1003 
1004     fdt.pack().map_err(|e| {
1005         error!("Failed to unpack DT after patching: {e}");
1006         RebootReason::InvalidFdt
1007     })?;
1008 
1009     Ok(info)
1010 }
1011 
parse_device_tree(fdt: &Fdt, vm_dtbo: Option<&VmDtbo>) -> Result<DeviceTreeInfo, RebootReason>1012 fn parse_device_tree(fdt: &Fdt, vm_dtbo: Option<&VmDtbo>) -> Result<DeviceTreeInfo, RebootReason> {
1013     let kernel_range = read_kernel_range_from(fdt).map_err(|e| {
1014         error!("Failed to read kernel range from DT: {e}");
1015         RebootReason::InvalidFdt
1016     })?;
1017 
1018     let initrd_range = read_initrd_range_from(fdt).map_err(|e| {
1019         error!("Failed to read initrd range from DT: {e}");
1020         RebootReason::InvalidFdt
1021     })?;
1022 
1023     let memory_range = read_and_validate_memory_range(fdt)?;
1024 
1025     let bootargs = read_bootargs_from(fdt).map_err(|e| {
1026         error!("Failed to read bootargs from DT: {e}");
1027         RebootReason::InvalidFdt
1028     })?;
1029 
1030     let (cpus, cpu_topology) = read_cpu_info_from(fdt).map_err(|e| {
1031         error!("Failed to read CPU info from DT: {e}");
1032         RebootReason::InvalidFdt
1033     })?;
1034     validate_cpu_info(&cpus).map_err(|e| {
1035         error!("Failed to validate CPU info from DT: {e}");
1036         RebootReason::InvalidFdt
1037     })?;
1038 
1039     let vcpufreq_info = read_vcpufreq_info(fdt).map_err(|e| {
1040         error!("Failed to read vcpufreq info from DT: {e}");
1041         RebootReason::InvalidFdt
1042     })?;
1043     if let Some(ref info) = vcpufreq_info {
1044         validate_vcpufreq_info(info, &cpus).map_err(|e| {
1045             error!("Failed to validate vcpufreq info from DT: {e}");
1046             RebootReason::InvalidFdt
1047         })?;
1048     }
1049 
1050     let pci_info = read_pci_info_from(fdt).map_err(|e| {
1051         error!("Failed to read pci info from DT: {e}");
1052         RebootReason::InvalidFdt
1053     })?;
1054     validate_pci_info(&pci_info, &memory_range)?;
1055 
1056     let serial_info = read_serial_info_from(fdt).map_err(|e| {
1057         error!("Failed to read serial info from DT: {e}");
1058         RebootReason::InvalidFdt
1059     })?;
1060 
1061     let swiotlb_info = SwiotlbInfo::new_from_fdt(fdt).map_err(|e| {
1062         error!("Failed to read swiotlb info from DT: {e}");
1063         RebootReason::InvalidFdt
1064     })?;
1065     validate_swiotlb_info(&swiotlb_info, &memory_range)?;
1066 
1067     let device_assignment = match vm_dtbo {
1068         Some(vm_dtbo) => {
1069             if let Some(hypervisor) = hyp::get_device_assigner() {
1070                 DeviceAssignmentInfo::parse(fdt, vm_dtbo, hypervisor).map_err(|e| {
1071                     error!("Failed to parse device assignment from DT and VM DTBO: {e}");
1072                     RebootReason::InvalidFdt
1073                 })?
1074             } else {
1075                 warn!(
1076                     "Device assignment is ignored because device assigning hypervisor is missing"
1077                 );
1078                 None
1079             }
1080         }
1081         None => None,
1082     };
1083 
1084     let untrusted_props = parse_untrusted_props(fdt).map_err(|e| {
1085         error!("Failed to read untrusted properties: {e}");
1086         RebootReason::InvalidFdt
1087     })?;
1088     validate_untrusted_props(&untrusted_props).map_err(|e| {
1089         error!("Failed to validate untrusted properties: {e}");
1090         RebootReason::InvalidFdt
1091     })?;
1092 
1093     let vm_ref_dt_props_info = parse_vm_ref_dt(fdt).map_err(|e| {
1094         error!("Failed to read names of properties under /avf from DT: {e}");
1095         RebootReason::InvalidFdt
1096     })?;
1097 
1098     Ok(DeviceTreeInfo {
1099         kernel_range,
1100         initrd_range,
1101         memory_range,
1102         bootargs,
1103         cpus,
1104         cpu_topology,
1105         pci_info,
1106         serial_info,
1107         swiotlb_info,
1108         device_assignment,
1109         untrusted_props,
1110         vm_ref_dt_props_info,
1111         vcpufreq_info,
1112     })
1113 }
1114 
patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason>1115 fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> {
1116     if let Some(initrd_range) = &info.initrd_range {
1117         patch_initrd_range(fdt, initrd_range).map_err(|e| {
1118             error!("Failed to patch initrd range to DT: {e}");
1119             RebootReason::InvalidFdt
1120         })?;
1121     }
1122     patch_memory_range(fdt, &info.memory_range).map_err(|e| {
1123         error!("Failed to patch memory range to DT: {e}");
1124         RebootReason::InvalidFdt
1125     })?;
1126     if let Some(bootargs) = &info.bootargs {
1127         patch_bootargs(fdt, bootargs.as_c_str()).map_err(|e| {
1128             error!("Failed to patch bootargs to DT: {e}");
1129             RebootReason::InvalidFdt
1130         })?;
1131     }
1132     patch_cpus(fdt, &info.cpus, &info.cpu_topology).map_err(|e| {
1133         error!("Failed to patch cpus to DT: {e}");
1134         RebootReason::InvalidFdt
1135     })?;
1136     patch_vcpufreq(fdt, &info.vcpufreq_info).map_err(|e| {
1137         error!("Failed to patch vcpufreq info to DT: {e}");
1138         RebootReason::InvalidFdt
1139     })?;
1140     patch_pci_info(fdt, &info.pci_info).map_err(|e| {
1141         error!("Failed to patch pci info to DT: {e}");
1142         RebootReason::InvalidFdt
1143     })?;
1144     patch_serial_info(fdt, &info.serial_info).map_err(|e| {
1145         error!("Failed to patch serial info to DT: {e}");
1146         RebootReason::InvalidFdt
1147     })?;
1148     patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| {
1149         error!("Failed to patch swiotlb info to DT: {e}");
1150         RebootReason::InvalidFdt
1151     })?;
1152     patch_gic(fdt, info.cpus.len()).map_err(|e| {
1153         error!("Failed to patch gic info to DT: {e}");
1154         RebootReason::InvalidFdt
1155     })?;
1156     patch_timer(fdt, info.cpus.len()).map_err(|e| {
1157         error!("Failed to patch timer info to DT: {e}");
1158         RebootReason::InvalidFdt
1159     })?;
1160     if let Some(device_assignment) = &info.device_assignment {
1161         // Note: We patch values after VM DTBO is overlaid because patch may require more space
1162         // then VM DTBO's underlying slice is allocated.
1163         device_assignment.patch(fdt).map_err(|e| {
1164             error!("Failed to patch device assignment info to DT: {e}");
1165             RebootReason::InvalidFdt
1166         })?;
1167     } else {
1168         device_assignment::clean(fdt).map_err(|e| {
1169             error!("Failed to clean pre-polulated DT nodes for device assignment: {e}");
1170             RebootReason::InvalidFdt
1171         })?;
1172     }
1173     patch_untrusted_props(fdt, &info.untrusted_props).map_err(|e| {
1174         error!("Failed to patch untrusted properties: {e}");
1175         RebootReason::InvalidFdt
1176     })?;
1177 
1178     Ok(())
1179 }
1180 
1181 /// Modifies the input DT according to the fields of the configuration.
modify_for_next_stage( fdt: &mut Fdt, bcc: &[u8], new_instance: bool, strict_boot: bool, debug_policy: Option<&[u8]>, debuggable: bool, kaslr_seed: u64, ) -> libfdt::Result<()>1182 pub fn modify_for_next_stage(
1183     fdt: &mut Fdt,
1184     bcc: &[u8],
1185     new_instance: bool,
1186     strict_boot: bool,
1187     debug_policy: Option<&[u8]>,
1188     debuggable: bool,
1189     kaslr_seed: u64,
1190 ) -> libfdt::Result<()> {
1191     if let Some(debug_policy) = debug_policy {
1192         let backup = Vec::from(fdt.as_slice());
1193         fdt.unpack()?;
1194         let backup_fdt = Fdt::from_slice(backup.as_slice()).unwrap();
1195         if apply_debug_policy(fdt, backup_fdt, debug_policy)? {
1196             info!("Debug policy applied.");
1197         } else {
1198             // apply_debug_policy restored fdt to backup_fdt so unpack it again.
1199             fdt.unpack()?;
1200         }
1201     } else {
1202         info!("No debug policy found.");
1203         fdt.unpack()?;
1204     }
1205 
1206     patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
1207 
1208     if let Some(mut chosen) = fdt.chosen_mut()? {
1209         empty_or_delete_prop(&mut chosen, cstr!("avf,strict-boot"), strict_boot)?;
1210         empty_or_delete_prop(&mut chosen, cstr!("avf,new-instance"), new_instance)?;
1211         chosen.setprop_inplace(cstr!("kaslr-seed"), &kaslr_seed.to_be_bytes())?;
1212     };
1213     if !debuggable {
1214         if let Some(bootargs) = read_bootargs_from(fdt)? {
1215             filter_out_dangerous_bootargs(fdt, &bootargs)?;
1216         }
1217     }
1218 
1219     fdt.pack()?;
1220 
1221     Ok(())
1222 }
1223 
1224 /// Patch the "google,open-dice"-compatible reserved-memory node to point to the bcc range
patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()>1225 fn patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
1226     // We reject DTs with missing reserved-memory node as validation should have checked that the
1227     // "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
1228     let node = fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?;
1229 
1230     let mut node = node.next_compatible(cstr!("google,open-dice"))?.ok_or(FdtError::NotFound)?;
1231 
1232     let addr: u64 = addr.try_into().unwrap();
1233     let size: u64 = size.try_into().unwrap();
1234     node.setprop_inplace(cstr!("reg"), flatten(&[addr.to_be_bytes(), size.to_be_bytes()]))
1235 }
1236 
empty_or_delete_prop( fdt_node: &mut FdtNodeMut, prop_name: &CStr, keep_prop: bool, ) -> libfdt::Result<()>1237 fn empty_or_delete_prop(
1238     fdt_node: &mut FdtNodeMut,
1239     prop_name: &CStr,
1240     keep_prop: bool,
1241 ) -> libfdt::Result<()> {
1242     if keep_prop {
1243         fdt_node.setprop_empty(prop_name)
1244     } else {
1245         fdt_node
1246             .delprop(prop_name)
1247             .or_else(|e| if e == FdtError::NotFound { Ok(()) } else { Err(e) })
1248     }
1249 }
1250 
1251 /// Apply the debug policy overlay to the guest DT.
1252 ///
1253 /// Returns Ok(true) on success, Ok(false) on recovered failure and Err(_) on corruption of the DT.
apply_debug_policy( fdt: &mut Fdt, backup_fdt: &Fdt, debug_policy: &[u8], ) -> libfdt::Result<bool>1254 fn apply_debug_policy(
1255     fdt: &mut Fdt,
1256     backup_fdt: &Fdt,
1257     debug_policy: &[u8],
1258 ) -> libfdt::Result<bool> {
1259     let mut debug_policy = Vec::from(debug_policy);
1260     let overlay = match Fdt::from_mut_slice(debug_policy.as_mut_slice()) {
1261         Ok(overlay) => overlay,
1262         Err(e) => {
1263             warn!("Corrupted debug policy found: {e}. Not applying.");
1264             return Ok(false);
1265         }
1266     };
1267 
1268     // SAFETY: on failure, the corrupted DT is restored using the backup.
1269     if let Err(e) = unsafe { fdt.apply_overlay(overlay) } {
1270         warn!("Failed to apply debug policy: {e}. Recovering...");
1271         fdt.clone_from(backup_fdt)?;
1272         // A successful restoration is considered success because an invalid debug policy
1273         // shouldn't DOS the pvmfw
1274         Ok(false)
1275     } else {
1276         Ok(true)
1277     }
1278 }
1279 
has_common_debug_policy(fdt: &Fdt, debug_feature_name: &CStr) -> libfdt::Result<bool>1280 fn has_common_debug_policy(fdt: &Fdt, debug_feature_name: &CStr) -> libfdt::Result<bool> {
1281     if let Some(node) = fdt.node(cstr!("/avf/guest/common"))? {
1282         if let Some(value) = node.getprop_u32(debug_feature_name)? {
1283             return Ok(value == 1);
1284         }
1285     }
1286     Ok(false) // if the policy doesn't exist or not 1, don't enable the debug feature
1287 }
1288 
filter_out_dangerous_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()>1289 fn filter_out_dangerous_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
1290     let has_crashkernel = has_common_debug_policy(fdt, cstr!("ramdump"))?;
1291     let has_console = has_common_debug_policy(fdt, cstr!("log"))?;
1292 
1293     let accepted: &[(&str, Box<dyn Fn(Option<&str>) -> bool>)] = &[
1294         ("panic", Box::new(|v| if let Some(v) = v { v == "=-1" } else { false })),
1295         ("crashkernel", Box::new(|_| has_crashkernel)),
1296         ("console", Box::new(|_| has_console)),
1297     ];
1298 
1299     // parse and filter out unwanted
1300     let mut filtered = Vec::new();
1301     for arg in BootArgsIterator::new(bootargs).map_err(|e| {
1302         info!("Invalid bootarg: {e}");
1303         FdtError::BadValue
1304     })? {
1305         match accepted.iter().find(|&t| t.0 == arg.name()) {
1306             Some((_, pred)) if pred(arg.value()) => filtered.push(arg),
1307             _ => debug!("Rejected bootarg {}", arg.as_ref()),
1308         }
1309     }
1310 
1311     // flatten into a new C-string
1312     let mut new_bootargs = Vec::new();
1313     for (i, arg) in filtered.iter().enumerate() {
1314         if i != 0 {
1315             new_bootargs.push(b' '); // separator
1316         }
1317         new_bootargs.extend_from_slice(arg.as_ref().as_bytes());
1318     }
1319     new_bootargs.push(b'\0');
1320 
1321     let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
1322     node.setprop(cstr!("bootargs"), new_bootargs.as_slice())
1323 }
1324