blob: d75e84e13de19324041e9fe81643d1f7aba02aad [file] [log] [blame]
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +00001// 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
Jiyong Parkc5d2ef22023-04-11 01:23:46 +090017use crate::bootargs::BootArgsIterator;
Jaewan Kim50246682024-03-11 23:18:54 +090018use crate::device_assignment::{self, DeviceAssignmentInfo, VmDtbo};
Jiyong Park00ceff32023-03-13 05:43:23 +000019use crate::helpers::GUEST_PAGE_SIZE;
Jiyong Parkc5d2ef22023-04-11 01:23:46 +090020use crate::Box;
Jiyong Park00ceff32023-03-13 05:43:23 +000021use crate::RebootReason;
Seungjae Yoo013f4c42024-01-02 13:04:19 +090022use alloc::collections::BTreeMap;
Jiyong Parke9d87e82023-03-21 19:28:40 +090023use alloc::ffi::CString;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +000024use alloc::format;
Jiyong Parkc23426b2023-04-10 17:32:27 +090025use alloc::vec::Vec;
Jiyong Park0ee65392023-03-27 20:52:45 +090026use core::cmp::max;
27use core::cmp::min;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000028use core::ffi::CStr;
Alice Wangabc7d632023-06-14 09:10:14 +000029use core::fmt;
Jiyong Park9c63cd12023-03-21 17:53:07 +090030use core::mem::size_of;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000031use core::ops::Range;
Pierre-Clément Tosi1bf532b2023-11-13 11:06:20 +000032use cstr::cstr;
Jiyong Park00ceff32023-03-13 05:43:23 +000033use libfdt::AddressRange;
34use libfdt::CellIterator;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +000035use libfdt::Fdt;
36use libfdt::FdtError;
David Dai9bdb10c2024-02-01 22:42:54 -080037use libfdt::FdtNode;
Alice Wang56ec45b2023-06-15 08:30:32 +000038use libfdt::FdtNodeMut;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +000039use libfdt::Phandle;
Jiyong Park83316122023-03-21 09:39:39 +090040use log::debug;
Jiyong Park00ceff32023-03-13 05:43:23 +000041use log::error;
Jiyong Parkc23426b2023-04-10 17:32:27 +090042use log::info;
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +000043use log::warn;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +000044use static_assertions::const_assert;
Jiyong Park00ceff32023-03-13 05:43:23 +000045use tinyvec::ArrayVec;
Pierre-Clément Tosif2c19d42024-10-01 17:42:04 +010046use vmbase::fdt::pci::PciMemoryFlags;
47use vmbase::fdt::pci::PciRangeType;
Alice Wanga3971062023-06-13 11:48:53 +000048use vmbase::fdt::SwiotlbInfo;
Pierre-Clément Tosia9b345f2024-04-27 01:01:42 +010049use vmbase::hyp;
Alice Wang63f4c9e2023-06-12 09:36:43 +000050use vmbase::layout::{crosvm::MEM_START, MAX_VIRT_ADDR};
Alice Wangeacb7382023-06-05 12:53:54 +000051use vmbase::memory::SIZE_4KB;
52use vmbase::util::flatten;
Alice Wang4be4dd02023-06-07 07:50:40 +000053use vmbase::util::RangeExt as _;
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +000054use zerocopy::AsBytes as _;
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +000055
Pierre-Clément Tosi84ba1a82024-10-30 11:27:32 +000056// SAFETY: The template DT is automatically generated through DTC, which should produce valid DTBs.
57const FDT_TEMPLATE: &Fdt = unsafe { Fdt::unchecked_from_slice(pvmfw_fdt_template::RAW) };
58
Alice Wangabc7d632023-06-14 09:10:14 +000059/// An enumeration of errors that can occur during the FDT validation.
60#[derive(Clone, Debug)]
61pub enum FdtValidationError {
62 /// Invalid CPU count.
63 InvalidCpuCount(usize),
David Dai9bdb10c2024-02-01 22:42:54 -080064 /// Invalid VCpufreq Range.
65 InvalidVcpufreq(u64, u64),
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +000066 /// Forbidden /avf/untrusted property.
67 ForbiddenUntrustedProp(&'static CStr),
Alice Wangabc7d632023-06-14 09:10:14 +000068}
69
70impl fmt::Display for FdtValidationError {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 match self {
73 Self::InvalidCpuCount(num_cpus) => write!(f, "Invalid CPU count: {num_cpus}"),
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +000074 Self::InvalidVcpufreq(addr, size) => {
75 write!(f, "Invalid vcpufreq region: ({addr:#x}, {size:#x})")
76 }
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +000077 Self::ForbiddenUntrustedProp(name) => {
78 write!(f, "Forbidden /avf/untrusted property '{name:?}'")
79 }
Alice Wangabc7d632023-06-14 09:10:14 +000080 }
81 }
82}
83
Jiyong Park6a8789a2023-03-21 14:50:59 +090084/// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is
85/// not an error.
86fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +090087 let addr = cstr!("kernel-address");
88 let size = cstr!("kernel-size");
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000089
Jiyong Parkb87f3302023-03-21 10:03:11 +090090 if let Some(config) = fdt.node(cstr!("/config"))? {
Pierre-Clément Tosic3811b82022-11-29 11:24:16 +000091 if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) {
92 let addr = addr as usize;
93 let size = size as usize;
94
95 return Ok(Some(addr..(addr + size)));
96 }
97 }
98
99 Ok(None)
100}
101
Jiyong Park6a8789a2023-03-21 14:50:59 +0900102/// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an
103/// error as there can be initrd-less VM.
104fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> {
Jiyong Parkb87f3302023-03-21 10:03:11 +0900105 let start = cstr!("linux,initrd-start");
106 let end = cstr!("linux,initrd-end");
Pierre-Clément Tosia0934c12022-11-25 20:54:11 +0000107
108 if let Some(chosen) = fdt.chosen()? {
109 if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) {
110 return Ok(Some((start as usize)..(end as usize)));
111 }
112 }
113
114 Ok(None)
115}
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +0000116
Jiyong Park9c63cd12023-03-21 17:53:07 +0900117fn patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()> {
118 let start = u32::try_from(initrd_range.start).unwrap();
119 let end = u32::try_from(initrd_range.end).unwrap();
120
121 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
122 node.setprop(cstr!("linux,initrd-start"), &start.to_be_bytes())?;
123 node.setprop(cstr!("linux,initrd-end"), &end.to_be_bytes())?;
124 Ok(())
125}
126
Jiyong Parke9d87e82023-03-21 19:28:40 +0900127fn read_bootargs_from(fdt: &Fdt) -> libfdt::Result<Option<CString>> {
128 if let Some(chosen) = fdt.chosen()? {
129 if let Some(bootargs) = chosen.getprop_str(cstr!("bootargs"))? {
130 // We need to copy the string to heap because the original fdt will be invalidated
131 // by the templated DT
132 let copy = CString::new(bootargs.to_bytes()).map_err(|_| FdtError::BadValue)?;
133 return Ok(Some(copy));
134 }
135 }
136 Ok(None)
137}
138
139fn patch_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
140 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
Jiyong Parkc5d2ef22023-04-11 01:23:46 +0900141 // This function is called before the verification is done. So, we just copy the bootargs to
142 // the new FDT unmodified. This will be filtered again in the modify_for_next_stage function
143 // if the VM is not debuggable.
Jiyong Parke9d87e82023-03-21 19:28:40 +0900144 node.setprop(cstr!("bootargs"), bootargs.to_bytes_with_nul())
145}
146
Alice Wang0d527472023-06-13 14:55:38 +0000147/// Reads and validates the memory range in the DT.
148///
149/// Only one memory range is expected with the crosvm setup for now.
150fn read_and_validate_memory_range(fdt: &Fdt) -> Result<Range<usize>, RebootReason> {
151 let mut memory = fdt.memory().map_err(|e| {
152 error!("Failed to read memory range from DT: {e}");
153 RebootReason::InvalidFdt
154 })?;
155 let range = memory.next().ok_or_else(|| {
156 error!("The /memory node in the DT contains no range.");
157 RebootReason::InvalidFdt
158 })?;
159 if memory.next().is_some() {
160 warn!(
161 "The /memory node in the DT contains more than one memory range, \
162 while only one is expected."
163 );
164 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900165 let base = range.start;
Alice Wange243d462023-06-06 15:18:12 +0000166 if base != MEM_START {
167 error!("Memory base address {:#x} is not {:#x}", base, MEM_START);
Jiyong Park00ceff32023-03-13 05:43:23 +0000168 return Err(RebootReason::InvalidFdt);
169 }
170
Jiyong Park6a8789a2023-03-21 14:50:59 +0900171 let size = range.len();
Jiyong Park00ceff32023-03-13 05:43:23 +0000172 if size % GUEST_PAGE_SIZE != 0 {
173 error!("Memory size {:#x} is not a multiple of page size {:#x}", size, GUEST_PAGE_SIZE);
174 return Err(RebootReason::InvalidFdt);
175 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000176
Jiyong Park6a8789a2023-03-21 14:50:59 +0900177 if size == 0 {
178 error!("Memory size is 0");
179 return Err(RebootReason::InvalidFdt);
180 }
Alice Wang0d527472023-06-13 14:55:38 +0000181 Ok(range)
Jiyong Park00ceff32023-03-13 05:43:23 +0000182}
183
Jiyong Park9c63cd12023-03-21 17:53:07 +0900184fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> {
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +0000185 let addr = u64::try_from(MEM_START).unwrap();
186 let size = u64::try_from(memory_range.len()).unwrap();
Jiyong Park0ee65392023-03-27 20:52:45 +0900187 fdt.node_mut(cstr!("/memory"))?
188 .ok_or(FdtError::NotFound)?
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +0000189 .setprop_inplace(cstr!("reg"), [addr.to_be(), size.to_be()].as_bytes())
Jiyong Park9c63cd12023-03-21 17:53:07 +0900190}
191
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000192#[derive(Debug, Default)]
David Dai9bdb10c2024-02-01 22:42:54 -0800193struct CpuInfo {
194 opptable_info: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
David Dai50168a32024-02-14 17:00:48 -0800195 cpu_capacity: Option<u32>,
David Dai9bdb10c2024-02-01 22:42:54 -0800196}
197
198impl CpuInfo {
David Dai622c05d2024-02-14 14:03:26 -0800199 const MAX_OPPTABLES: usize = 20;
David Dai9bdb10c2024-02-01 22:42:54 -0800200}
201
202fn read_opp_info_from(
203 opp_node: FdtNode,
204) -> libfdt::Result<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>> {
205 let mut table = ArrayVec::new();
Pierre-Clément Tosidf272a52024-04-15 16:07:58 +0100206 let mut opp_nodes = opp_node.subnodes()?;
207 for subnode in opp_nodes.by_ref().take(table.capacity()) {
David Dai9bdb10c2024-02-01 22:42:54 -0800208 let prop = subnode.getprop_u64(cstr!("opp-hz"))?.ok_or(FdtError::NotFound)?;
209 table.push(prop);
210 }
211
Pierre-Clément Tosidf272a52024-04-15 16:07:58 +0100212 if opp_nodes.next().is_some() {
213 warn!("OPP table has more than {} entries: discarding extra nodes.", table.capacity());
214 }
215
David Dai9bdb10c2024-02-01 22:42:54 -0800216 Ok(table)
217}
Jiyong Park6a8789a2023-03-21 14:50:59 +0900218
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000219#[derive(Debug, Default)]
220struct ClusterTopology {
221 // TODO: Support multi-level clusters & threads.
222 cores: [Option<usize>; ClusterTopology::MAX_CORES_PER_CLUSTER],
223}
224
225impl ClusterTopology {
David Daib19fd082024-04-19 16:33:26 -0700226 const MAX_CORES_PER_CLUSTER: usize = 10;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000227}
228
229#[derive(Debug, Default)]
230struct CpuTopology {
231 // TODO: Support sockets.
232 clusters: [Option<ClusterTopology>; CpuTopology::MAX_CLUSTERS],
233}
234
235impl CpuTopology {
236 const MAX_CLUSTERS: usize = 3;
237}
238
239fn read_cpu_map_from(fdt: &Fdt) -> libfdt::Result<Option<BTreeMap<Phandle, (usize, usize)>>> {
240 let Some(cpu_map) = fdt.node(cstr!("/cpus/cpu-map"))? else {
241 return Ok(None);
242 };
243
244 let mut topology = BTreeMap::new();
245 for n in 0..CpuTopology::MAX_CLUSTERS {
246 let name = CString::new(format!("cluster{n}")).unwrap();
247 let Some(cluster) = cpu_map.subnode(&name)? else {
248 break;
249 };
250 for m in 0..ClusterTopology::MAX_CORES_PER_CLUSTER {
David Dai8f476cb2024-02-15 21:57:01 -0800251 let name = CString::new(format!("core{m}")).unwrap();
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000252 let Some(core) = cluster.subnode(&name)? else {
253 break;
254 };
255 let cpu = core.getprop_u32(cstr!("cpu"))?.ok_or(FdtError::NotFound)?;
256 let prev = topology.insert(cpu.try_into()?, (n, m));
257 if prev.is_some() {
258 return Err(FdtError::BadValue);
259 }
260 }
261 }
262
263 Ok(Some(topology))
264}
265
266fn read_cpu_info_from(
267 fdt: &Fdt,
268) -> libfdt::Result<(ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>, Option<CpuTopology>)> {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000269 let mut cpus = ArrayVec::new();
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000270
271 let cpu_map = read_cpu_map_from(fdt)?;
272 let mut topology: CpuTopology = Default::default();
273
Pierre-Clément Tosi6ae8fe22024-04-17 20:02:23 +0100274 let mut cpu_nodes = fdt.compatible_nodes(cstr!("arm,armv8"))?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000275 for (idx, cpu) in cpu_nodes.by_ref().take(cpus.capacity()).enumerate() {
David Dai50168a32024-02-14 17:00:48 -0800276 let cpu_capacity = cpu.getprop_u32(cstr!("capacity-dmips-mhz"))?;
David Dai9bdb10c2024-02-01 22:42:54 -0800277 let opp_phandle = cpu.getprop_u32(cstr!("operating-points-v2"))?;
278 let opptable_info = if let Some(phandle) = opp_phandle {
279 let phandle = phandle.try_into()?;
280 let node = fdt.node_with_phandle(phandle)?.ok_or(FdtError::NotFound)?;
281 Some(read_opp_info_from(node)?)
282 } else {
283 None
284 };
David Dai50168a32024-02-14 17:00:48 -0800285 let info = CpuInfo { opptable_info, cpu_capacity };
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000286 cpus.push(info);
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000287
288 if let Some(ref cpu_map) = cpu_map {
289 let phandle = cpu.get_phandle()?.ok_or(FdtError::NotFound)?;
David Dai8f476cb2024-02-15 21:57:01 -0800290 let (cluster, core_idx) = cpu_map.get(&phandle).ok_or(FdtError::BadValue)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000291 let cluster = topology.clusters[*cluster].get_or_insert(Default::default());
David Dai8f476cb2024-02-15 21:57:01 -0800292 if cluster.cores[*core_idx].is_some() {
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000293 return Err(FdtError::BadValue);
294 }
David Dai8f476cb2024-02-15 21:57:01 -0800295 cluster.cores[*core_idx] = Some(idx);
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000296 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900297 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000298
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000299 if cpu_nodes.next().is_some() {
300 warn!("DT has more than {} CPU nodes: discarding extra nodes.", cpus.capacity());
301 }
302
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000303 Ok((cpus, cpu_map.map(|_| topology)))
Jiyong Park9c63cd12023-03-21 17:53:07 +0900304}
305
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000306fn validate_cpu_info(cpus: &[CpuInfo]) -> Result<(), FdtValidationError> {
307 if cpus.is_empty() {
308 return Err(FdtValidationError::InvalidCpuCount(0));
309 }
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000310 Ok(())
311}
312
David Dai9bdb10c2024-02-01 22:42:54 -0800313fn read_vcpufreq_info(fdt: &Fdt) -> libfdt::Result<Option<VcpufreqInfo>> {
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000314 let mut nodes = fdt.compatible_nodes(cstr!("virtual,android-v-only-cpufreq"))?;
315 let Some(node) = nodes.next() else {
316 return Ok(None);
David Dai9bdb10c2024-02-01 22:42:54 -0800317 };
318
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000319 if nodes.next().is_some() {
320 warn!("DT has more than 1 cpufreq node: discarding extra nodes.");
321 }
322
323 let mut regs = node.reg()?.ok_or(FdtError::NotFound)?;
324 let reg = regs.next().ok_or(FdtError::NotFound)?;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000325 let size = reg.size.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000326
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000327 Ok(Some(VcpufreqInfo { addr: reg.addr, size }))
David Dai9bdb10c2024-02-01 22:42:54 -0800328}
329
330fn validate_vcpufreq_info(
331 vcpufreq_info: &VcpufreqInfo,
332 cpus: &[CpuInfo],
333) -> Result<(), FdtValidationError> {
334 const VCPUFREQ_BASE_ADDR: u64 = 0x1040000;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000335 const VCPUFREQ_SIZE_PER_CPU: u64 = 0x8;
David Dai9bdb10c2024-02-01 22:42:54 -0800336
337 let base = vcpufreq_info.addr;
338 let size = vcpufreq_info.size;
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000339 let expected_size = VCPUFREQ_SIZE_PER_CPU * cpus.len() as u64;
340
341 if (base, size) != (VCPUFREQ_BASE_ADDR, expected_size) {
David Dai9bdb10c2024-02-01 22:42:54 -0800342 return Err(FdtValidationError::InvalidVcpufreq(base, size));
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000343 }
David Dai9bdb10c2024-02-01 22:42:54 -0800344
345 Ok(())
346}
347
348fn patch_opptable(
349 node: FdtNodeMut,
David Dai622c05d2024-02-14 14:03:26 -0800350 opptable: Option<ArrayVec<[u64; CpuInfo::MAX_OPPTABLES]>>,
David Dai9bdb10c2024-02-01 22:42:54 -0800351) -> libfdt::Result<()> {
352 let oppcompat = cstr!("operating-points-v2");
353 let next = node.next_compatible(oppcompat)?.ok_or(FdtError::NoSpace)?;
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000354
355 let Some(opptable) = opptable else {
356 return next.nop();
357 };
358
David Dai9bdb10c2024-02-01 22:42:54 -0800359 let mut next_subnode = next.first_subnode()?;
360
361 for entry in opptable {
362 let mut subnode = next_subnode.ok_or(FdtError::NoSpace)?;
363 subnode.setprop_inplace(cstr!("opp-hz"), &entry.to_be_bytes())?;
364 next_subnode = subnode.next_subnode()?;
365 }
366
367 while let Some(current) = next_subnode {
368 next_subnode = current.delete_and_next_subnode()?;
369 }
Pierre-Clément Tosi8ba89802024-02-14 12:26:01 +0000370
David Dai9bdb10c2024-02-01 22:42:54 -0800371 Ok(())
372}
373
374// TODO(ptosi): Rework FdtNodeMut and replace this function.
375fn get_nth_compatible<'a>(
376 fdt: &'a mut Fdt,
377 n: usize,
378 compat: &CStr,
379) -> libfdt::Result<Option<FdtNodeMut<'a>>> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000380 let mut node = fdt.root_mut().next_compatible(compat)?;
David Dai9bdb10c2024-02-01 22:42:54 -0800381 for _ in 0..n {
382 node = node.ok_or(FdtError::NoSpace)?.next_compatible(compat)?;
383 }
384 Ok(node)
385}
386
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000387fn patch_cpus(
388 fdt: &mut Fdt,
389 cpus: &[CpuInfo],
390 topology: &Option<CpuTopology>,
391) -> libfdt::Result<()> {
Pierre-Clément Tosi6ae8fe22024-04-17 20:02:23 +0100392 const COMPAT: &CStr = cstr!("arm,armv8");
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000393 let mut cpu_phandles = Vec::new();
David Dai9bdb10c2024-02-01 22:42:54 -0800394 for (idx, cpu) in cpus.iter().enumerate() {
David Dai50168a32024-02-14 17:00:48 -0800395 let mut cur = get_nth_compatible(fdt, idx, COMPAT)?.ok_or(FdtError::NoSpace)?;
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000396 let phandle = cur.as_node().get_phandle()?.unwrap();
397 cpu_phandles.push(phandle);
David Dai50168a32024-02-14 17:00:48 -0800398 if let Some(cpu_capacity) = cpu.cpu_capacity {
399 cur.setprop_inplace(cstr!("capacity-dmips-mhz"), &cpu_capacity.to_be_bytes())?;
400 }
Pierre-Clément Tosic37c72e2024-02-14 12:18:12 +0000401 patch_opptable(cur, cpu.opptable_info)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900402 }
David Dai9bdb10c2024-02-01 22:42:54 -0800403 let mut next = get_nth_compatible(fdt, cpus.len(), COMPAT)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900404 while let Some(current) = next {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000405 next = current.delete_and_next_compatible(COMPAT)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900406 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000407
408 if let Some(topology) = topology {
409 for (n, cluster) in topology.clusters.iter().enumerate() {
410 let path = CString::new(format!("/cpus/cpu-map/cluster{n}")).unwrap();
411 let cluster_node = fdt.node_mut(&path)?.unwrap();
412 if let Some(cluster) = cluster {
413 let mut iter = cluster_node.first_subnode()?;
414 for core in cluster.cores {
415 let mut core_node = iter.unwrap();
416 iter = if let Some(core_idx) = core {
417 let phandle = *cpu_phandles.get(core_idx).unwrap();
418 let value = u32::from(phandle).to_be_bytes();
419 core_node.setprop_inplace(cstr!("cpu"), &value)?;
420 core_node.next_subnode()?
421 } else {
422 core_node.delete_and_next_subnode()?
423 };
424 }
425 assert!(iter.is_none());
426 } else {
427 cluster_node.nop()?;
428 }
429 }
430 } else {
431 fdt.node_mut(cstr!("/cpus/cpu-map"))?.unwrap().nop()?;
432 }
433
Jiyong Park6a8789a2023-03-21 14:50:59 +0900434 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000435}
436
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000437/// Reads the /avf/untrusted DT node, which the host can use to pass properties (no subnodes) to
438/// the guest that don't require being validated by pvmfw.
439fn parse_untrusted_props(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
440 let mut props = BTreeMap::new();
441 if let Some(node) = fdt.node(cstr!("/avf/untrusted"))? {
442 for property in node.properties()? {
443 let name = property.name()?;
444 let value = property.value()?;
445 props.insert(CString::from(name), value.to_vec());
446 }
447 if node.subnodes()?.next().is_some() {
448 warn!("Discarding unexpected /avf/untrusted subnodes.");
449 }
450 }
451
452 Ok(props)
453}
454
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900455/// Read candidate properties' names from DT which could be overlaid
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900456fn parse_vm_ref_dt(fdt: &Fdt) -> libfdt::Result<BTreeMap<CString, Vec<u8>>> {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900457 let mut property_map = BTreeMap::new();
Seungjae Yooed67fd52023-11-29 18:54:36 +0900458 if let Some(avf_node) = fdt.node(cstr!("/avf"))? {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900459 for property in avf_node.properties()? {
460 let name = property.name()?;
461 let value = property.value()?;
462 property_map.insert(
463 CString::new(name.to_bytes()).map_err(|_| FdtError::BadValue)?,
464 value.to_vec(),
465 );
Seungjae Yooed67fd52023-11-29 18:54:36 +0900466 }
467 }
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900468 Ok(property_map)
Seungjae Yooed67fd52023-11-29 18:54:36 +0900469}
470
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000471fn validate_untrusted_props(props: &BTreeMap<CString, Vec<u8>>) -> Result<(), FdtValidationError> {
472 const FORBIDDEN_PROPS: &[&CStr] =
473 &[cstr!("compatible"), cstr!("linux,phandle"), cstr!("phandle")];
474
475 for name in FORBIDDEN_PROPS {
476 if props.contains_key(*name) {
477 return Err(FdtValidationError::ForbiddenUntrustedProp(name));
478 }
479 }
480
481 Ok(())
482}
483
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900484/// Overlay VM reference DT into VM DT based on the props_info. Property is overlaid in vm_dt only
485/// when it exists both in vm_ref_dt and props_info. If the values mismatch, it returns error.
486fn validate_vm_ref_dt(
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900487 vm_dt: &mut Fdt,
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900488 vm_ref_dt: &Fdt,
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900489 props_info: &BTreeMap<CString, Vec<u8>>,
Seungjae Yoo192e99c2023-12-15 16:42:39 +0900490) -> libfdt::Result<()> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000491 let root_vm_dt = vm_dt.root_mut();
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900492 let mut avf_vm_dt = root_vm_dt.add_subnode(cstr!("avf"))?;
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900493 // TODO(b/318431677): Validate nodes beyond /avf.
494 let avf_node = vm_ref_dt.node(cstr!("/avf"))?.ok_or(FdtError::NotFound)?;
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900495 for (name, value) in props_info.iter() {
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900496 if let Some(ref_value) = avf_node.getprop(name)? {
497 if value != ref_value {
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900498 error!(
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900499 "Property mismatches while applying overlay VM reference DT. \
500 Name:{:?}, Value from host as hex:{:x?}, Value from VM reference DT as hex:{:x?}",
501 name, value, ref_value
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900502 );
503 return Err(FdtError::BadValue);
504 }
Seungjae Yoof0af81d2024-01-17 13:48:36 +0900505 avf_vm_dt.setprop(name, ref_value)?;
Seungjae Yoo013f4c42024-01-02 13:04:19 +0900506 }
507 }
Seungjae Yooed67fd52023-11-29 18:54:36 +0900508 Ok(())
509}
510
Jiyong Park00ceff32023-03-13 05:43:23 +0000511#[derive(Debug)]
Jiyong Park00ceff32023-03-13 05:43:23 +0000512struct PciInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900513 ranges: [PciAddrRange; 2],
514 irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>,
515 irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000516}
517
Jiyong Park6a8789a2023-03-21 14:50:59 +0900518impl PciInfo {
519 const IRQ_MASK_CELLS: usize = 4;
520 const IRQ_MAP_CELLS: usize = 10;
Nikita Ioffe2d0969c2024-06-06 12:59:12 +0000521 const MAX_IRQS: usize = 16;
Jiyong Park00ceff32023-03-13 05:43:23 +0000522}
523
Jiyong Park6a8789a2023-03-21 14:50:59 +0900524type PciAddrRange = AddressRange<(u32, u64), u64, u64>;
525type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS];
526type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS];
Jiyong Park00ceff32023-03-13 05:43:23 +0000527
528/// Iterator that takes N cells as a chunk
529struct CellChunkIterator<'a, const N: usize> {
530 cells: CellIterator<'a>,
531}
532
533impl<'a, const N: usize> CellChunkIterator<'a, N> {
534 fn new(cells: CellIterator<'a>) -> Self {
535 Self { cells }
536 }
537}
538
539impl<'a, const N: usize> Iterator for CellChunkIterator<'a, N> {
540 type Item = [u32; N];
541 fn next(&mut self) -> Option<Self::Item> {
542 let mut ret: Self::Item = [0; N];
543 for i in ret.iter_mut() {
544 *i = self.cells.next()?;
545 }
546 Some(ret)
547 }
548}
549
Jiyong Park6a8789a2023-03-21 14:50:59 +0900550/// Read pci host controller ranges, irq maps, and irq map masks from DT
551fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> {
552 let node =
553 fdt.compatible_nodes(cstr!("pci-host-cam-generic"))?.next().ok_or(FdtError::NotFound)?;
554
555 let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?;
556 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
557 let range1 = ranges.next().ok_or(FdtError::NotFound)?;
558
559 let irq_masks = node.getprop_cells(cstr!("interrupt-map-mask"))?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosiaa0f6552023-07-12 14:49:35 +0000560 let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks);
561 let irq_masks = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
562
563 if chunks.next().is_some() {
564 warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
565 return Err(FdtError::NoSpace);
566 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900567
568 let irq_maps = node.getprop_cells(cstr!("interrupt-map"))?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosiaa0f6552023-07-12 14:49:35 +0000569 let mut chunks = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps);
570 let irq_maps = (&mut chunks).take(PciInfo::MAX_IRQS).collect();
571
572 if chunks.next().is_some() {
573 warn!("Input DT has more than {} PCI entries!", PciInfo::MAX_IRQS);
574 return Err(FdtError::NoSpace);
575 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900576
577 Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps })
578}
579
Jiyong Park0ee65392023-03-27 20:52:45 +0900580fn validate_pci_info(pci_info: &PciInfo, memory_range: &Range<usize>) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900581 for range in pci_info.ranges.iter() {
Jiyong Park0ee65392023-03-27 20:52:45 +0900582 validate_pci_addr_range(range, memory_range)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900583 }
584 for irq_mask in pci_info.irq_masks.iter() {
585 validate_pci_irq_mask(irq_mask)?;
586 }
587 for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() {
588 validate_pci_irq_map(irq_map, idx)?;
589 }
590 Ok(())
591}
592
Jiyong Park0ee65392023-03-27 20:52:45 +0900593fn validate_pci_addr_range(
594 range: &PciAddrRange,
595 memory_range: &Range<usize>,
596) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900597 let mem_flags = PciMemoryFlags(range.addr.0);
598 let range_type = mem_flags.range_type();
Jiyong Park6a8789a2023-03-21 14:50:59 +0900599 let bus_addr = range.addr.1;
600 let cpu_addr = range.parent_addr;
601 let size = range.size;
602
603 if range_type != PciRangeType::Memory64 {
604 error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr);
605 return Err(RebootReason::InvalidFdt);
606 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900607 // Enforce ID bus-to-cpu mappings, as used by crosvm.
608 if bus_addr != cpu_addr {
609 error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr);
610 return Err(RebootReason::InvalidFdt);
611 }
612
Jiyong Park0ee65392023-03-27 20:52:45 +0900613 let Some(bus_end) = bus_addr.checked_add(size) else {
614 error!("PCI address range size {:#x} overflows", size);
615 return Err(RebootReason::InvalidFdt);
616 };
Alice Wang63f4c9e2023-06-12 09:36:43 +0000617 if bus_end > MAX_VIRT_ADDR.try_into().unwrap() {
Jiyong Park0ee65392023-03-27 20:52:45 +0900618 error!("PCI address end {:#x} is outside of translatable range", bus_end);
619 return Err(RebootReason::InvalidFdt);
620 }
621
622 let memory_start = memory_range.start.try_into().unwrap();
623 let memory_end = memory_range.end.try_into().unwrap();
624
625 if max(bus_addr, memory_start) < min(bus_end, memory_end) {
626 error!(
627 "PCI address range {:#x}-{:#x} overlaps with main memory range {:#x}-{:#x}",
628 bus_addr, bus_end, memory_start, memory_end
629 );
Jiyong Park6a8789a2023-03-21 14:50:59 +0900630 return Err(RebootReason::InvalidFdt);
631 }
632
633 Ok(())
634}
635
636fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000637 const IRQ_MASK_ADDR_HI: u32 = 0xf800;
638 const IRQ_MASK_ADDR_ME: u32 = 0x0;
639 const IRQ_MASK_ADDR_LO: u32 = 0x0;
640 const IRQ_MASK_ANY_IRQ: u32 = 0x7;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900641 const EXPECTED: PciIrqMask =
Jiyong Park00ceff32023-03-13 05:43:23 +0000642 [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ];
Jiyong Park6a8789a2023-03-21 14:50:59 +0900643 if *irq_mask != EXPECTED {
644 error!("Invalid PCI irq mask {:#?}", irq_mask);
645 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000646 }
Jiyong Park6a8789a2023-03-21 14:50:59 +0900647 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000648}
649
Jiyong Park6a8789a2023-03-21 14:50:59 +0900650fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> {
Jiyong Park00ceff32023-03-13 05:43:23 +0000651 const PCI_DEVICE_IDX: usize = 11;
652 const PCI_IRQ_ADDR_ME: u32 = 0;
653 const PCI_IRQ_ADDR_LO: u32 = 0;
654 const PCI_IRQ_INTC: u32 = 1;
655 const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs
656 const GIC_SPI: u32 = 0;
657 const IRQ_TYPE_LEVEL_HIGH: u32 = 4;
658
Jiyong Park6a8789a2023-03-21 14:50:59 +0900659 let pci_addr = (irq_map[0], irq_map[1], irq_map[2]);
660 let pci_irq_number = irq_map[3];
661 let _controller_phandle = irq_map[4]; // skipped.
662 let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC
663 // interrupt-cells is <3> for GIC
664 let gic_peripheral_interrupt_type = irq_map[7];
665 let gic_irq_number = irq_map[8];
666 let gic_irq_type = irq_map[9];
Jiyong Park00ceff32023-03-13 05:43:23 +0000667
Jiyong Park6a8789a2023-03-21 14:50:59 +0900668 let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32;
669 let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO);
Jiyong Park00ceff32023-03-13 05:43:23 +0000670
Jiyong Park6a8789a2023-03-21 14:50:59 +0900671 if pci_addr != expected_pci_addr {
672 error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \
673 {:#x} {:#x} {:#x}",
674 pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2);
675 return Err(RebootReason::InvalidFdt);
676 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000677
Jiyong Park6a8789a2023-03-21 14:50:59 +0900678 if pci_irq_number != PCI_IRQ_INTC {
679 error!(
680 "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}",
681 pci_irq_number, PCI_IRQ_INTC
682 );
683 return Err(RebootReason::InvalidFdt);
684 }
Jiyong Park00ceff32023-03-13 05:43:23 +0000685
Jiyong Park6a8789a2023-03-21 14:50:59 +0900686 if gic_addr != (0, 0) {
687 error!(
688 "GIC address {:#x} {:#x} in interrupt-map is different from expected address \
689 {:#x} {:#x}",
690 gic_addr.0, gic_addr.1, 0, 0
691 );
692 return Err(RebootReason::InvalidFdt);
693 }
694
695 if gic_peripheral_interrupt_type != GIC_SPI {
696 error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \
697 {:#x}", gic_peripheral_interrupt_type, GIC_SPI);
698 return Err(RebootReason::InvalidFdt);
699 }
700
701 let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32);
702 if gic_irq_number != irq_nr {
703 error!(
704 "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}",
705 gic_irq_number, irq_nr
706 );
707 return Err(RebootReason::InvalidFdt);
708 }
709
710 if gic_irq_type != IRQ_TYPE_LEVEL_HIGH {
711 error!(
712 "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}",
713 gic_irq_type, IRQ_TYPE_LEVEL_HIGH
714 );
715 return Err(RebootReason::InvalidFdt);
Jiyong Park00ceff32023-03-13 05:43:23 +0000716 }
717 Ok(())
718}
719
Jiyong Park9c63cd12023-03-21 17:53:07 +0900720fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000721 let mut node =
722 fdt.root_mut().next_compatible(cstr!("pci-host-cam-generic"))?.ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900723
724 let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>();
725 node.trimprop(cstr!("interrupt-map-mask"), irq_masks_size)?;
726
727 let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>();
728 node.trimprop(cstr!("interrupt-map"), irq_maps_size)?;
729
730 node.setprop_inplace(
731 cstr!("ranges"),
732 flatten(&[pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()]),
733 )
734}
735
Jiyong Park00ceff32023-03-13 05:43:23 +0000736#[derive(Default, Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900737struct SerialInfo {
738 addrs: ArrayVec<[u64; Self::MAX_SERIALS]>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000739}
740
741impl SerialInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900742 const MAX_SERIALS: usize = 4;
Jiyong Park00ceff32023-03-13 05:43:23 +0000743}
744
Jiyong Park6a8789a2023-03-21 14:50:59 +0900745fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> {
Pierre-Clément Tosibe893612024-02-05 14:23:44 +0000746 let mut addrs = ArrayVec::new();
747
748 let mut serial_nodes = fdt.compatible_nodes(cstr!("ns16550a"))?;
749 for node in serial_nodes.by_ref().take(addrs.capacity()) {
Alice Wang6ff2d0c2023-09-19 15:28:43 +0000750 let reg = node.first_reg()?;
Jiyong Park6a8789a2023-03-21 14:50:59 +0900751 addrs.push(reg.addr);
Jiyong Park00ceff32023-03-13 05:43:23 +0000752 }
Pierre-Clément Tosibe893612024-02-05 14:23:44 +0000753 if serial_nodes.next().is_some() {
754 warn!("DT has more than {} UART nodes: discarding extra nodes.", addrs.capacity());
755 }
756
Jiyong Park6a8789a2023-03-21 14:50:59 +0900757 Ok(SerialInfo { addrs })
Jiyong Park00ceff32023-03-13 05:43:23 +0000758}
759
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000760#[derive(Default, Debug, PartialEq)]
761struct WdtInfo {
762 addr: u64,
763 size: u64,
764 irq: [u32; WdtInfo::IRQ_CELLS],
765}
766
767impl WdtInfo {
768 const IRQ_CELLS: usize = 3;
769 const IRQ_NR: u32 = 0xf;
770 const ADDR: u64 = 0x3000;
771 const SIZE: u64 = 0x1000;
772 const GIC_PPI: u32 = 1;
773 const IRQ_TYPE_EDGE_RISING: u32 = 1;
774 const GIC_FDT_IRQ_PPI_CPU_SHIFT: u32 = 8;
Pierre-Clément Tosi3ad82742024-07-04 10:23:00 +0100775 // TODO(b/350498812): Rework this for >8 vCPUs.
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000776 const GIC_FDT_IRQ_PPI_CPU_MASK: u32 = 0xff << Self::GIC_FDT_IRQ_PPI_CPU_SHIFT;
777
778 const fn get_expected(num_cpus: usize) -> Self {
779 Self {
780 addr: Self::ADDR,
781 size: Self::SIZE,
782 irq: [
783 Self::GIC_PPI,
784 Self::IRQ_NR,
785 ((((1 << num_cpus) - 1) << Self::GIC_FDT_IRQ_PPI_CPU_SHIFT)
786 & Self::GIC_FDT_IRQ_PPI_CPU_MASK)
787 | Self::IRQ_TYPE_EDGE_RISING,
788 ],
789 }
790 }
791}
792
793fn read_wdt_info_from(fdt: &Fdt) -> libfdt::Result<WdtInfo> {
794 let mut node_iter = fdt.compatible_nodes(cstr!("qemu,vcpu-stall-detector"))?;
795 let node = node_iter.next().ok_or(FdtError::NotFound)?;
796 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
797
798 let reg = ranges.next().ok_or(FdtError::NotFound)?;
799 let size = reg.size.ok_or(FdtError::NotFound)?;
800 if ranges.next().is_some() {
801 warn!("Discarding extra vmwdt <reg> entries.");
802 }
803
804 let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?;
805 let mut chunks = CellChunkIterator::<{ WdtInfo::IRQ_CELLS }>::new(interrupts);
806 let irq = chunks.next().ok_or(FdtError::NotFound)?;
807
808 if chunks.next().is_some() {
809 warn!("Discarding extra vmwdt <interrupts> entries.");
810 }
811
812 Ok(WdtInfo { addr: reg.addr, size, irq })
813}
814
815fn validate_wdt_info(wdt: &WdtInfo, num_cpus: usize) -> Result<(), RebootReason> {
816 if *wdt != WdtInfo::get_expected(num_cpus) {
817 error!("Invalid watchdog timer: {wdt:?}");
818 return Err(RebootReason::InvalidFdt);
819 }
820
821 Ok(())
822}
823
824fn patch_wdt_info(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
825 let mut interrupts = WdtInfo::get_expected(num_cpus).irq;
826 for v in interrupts.iter_mut() {
827 *v = v.to_be();
828 }
829
830 let mut node = fdt
831 .root_mut()
832 .next_compatible(cstr!("qemu,vcpu-stall-detector"))?
833 .ok_or(libfdt::FdtError::NotFound)?;
834 node.setprop_inplace(cstr!("interrupts"), interrupts.as_bytes())?;
835 Ok(())
836}
837
Jiyong Park9c63cd12023-03-21 17:53:07 +0900838/// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown
839fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> {
840 let name = cstr!("ns16550a");
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000841 let mut next = fdt.root_mut().next_compatible(name);
Jiyong Park9c63cd12023-03-21 17:53:07 +0900842 while let Some(current) = next? {
Pierre-Clément Tosic01fd0d2024-01-25 22:26:22 +0000843 let reg =
844 current.as_node().reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900845 next = if !serial_info.addrs.contains(&reg.addr) {
846 current.delete_and_next_compatible(name)
847 } else {
848 current.next_compatible(name)
849 }
850 }
851 Ok(())
852}
853
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700854fn validate_swiotlb_info(
855 swiotlb_info: &SwiotlbInfo,
856 memory: &Range<usize>,
857) -> Result<(), RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +0900858 let size = swiotlb_info.size;
859 let align = swiotlb_info.align;
Jiyong Park00ceff32023-03-13 05:43:23 +0000860
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700861 if size == 0 || (size % GUEST_PAGE_SIZE) != 0 {
Jiyong Park00ceff32023-03-13 05:43:23 +0000862 error!("Invalid swiotlb size {:#x}", size);
863 return Err(RebootReason::InvalidFdt);
864 }
865
Pierre-Clément Tosibe3a97b2023-05-19 14:56:23 +0000866 if let Some(align) = align.filter(|&a| a % GUEST_PAGE_SIZE != 0) {
Jiyong Park00ceff32023-03-13 05:43:23 +0000867 error!("Invalid swiotlb alignment {:#x}", align);
868 return Err(RebootReason::InvalidFdt);
869 }
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700870
Alice Wang9cfbfd62023-06-14 11:19:03 +0000871 if let Some(addr) = swiotlb_info.addr {
872 if addr.checked_add(size).is_none() {
873 error!("Invalid swiotlb range: addr:{addr:#x} size:{size:#x}");
874 return Err(RebootReason::InvalidFdt);
875 }
876 }
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700877 if let Some(range) = swiotlb_info.fixed_range() {
878 if !range.is_within(memory) {
879 error!("swiotlb range {range:#x?} not part of memory range {memory:#x?}");
880 return Err(RebootReason::InvalidFdt);
881 }
882 }
883
Jiyong Park6a8789a2023-03-21 14:50:59 +0900884 Ok(())
Jiyong Park00ceff32023-03-13 05:43:23 +0000885}
886
Jiyong Park9c63cd12023-03-21 17:53:07 +0900887fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> {
888 let mut node =
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000889 fdt.root_mut().next_compatible(cstr!("restricted-dma-pool"))?.ok_or(FdtError::NotFound)?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700890
891 if let Some(range) = swiotlb_info.fixed_range() {
Pierre-Clément Tosic27c4272023-05-19 15:46:26 +0000892 node.setprop_addrrange_inplace(
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700893 cstr!("reg"),
894 range.start.try_into().unwrap(),
895 range.len().try_into().unwrap(),
896 )?;
Pierre-Clément Tosibe3a97b2023-05-19 14:56:23 +0000897 node.nop_property(cstr!("size"))?;
898 node.nop_property(cstr!("alignment"))?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700899 } else {
Pierre-Clément Tosic27c4272023-05-19 15:46:26 +0000900 node.nop_property(cstr!("reg"))?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700901 node.setprop_inplace(cstr!("size"), &swiotlb_info.size.to_be_bytes())?;
Pierre-Clément Tosibe3a97b2023-05-19 14:56:23 +0000902 node.setprop_inplace(cstr!("alignment"), &swiotlb_info.align.unwrap().to_be_bytes())?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -0700903 }
904
Jiyong Park9c63cd12023-03-21 17:53:07 +0900905 Ok(())
906}
907
908fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
909 let node = fdt.compatible_nodes(cstr!("arm,gic-v3"))?.next().ok_or(FdtError::NotFound)?;
910 let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?;
911 let range0 = ranges.next().ok_or(FdtError::NotFound)?;
912 let mut range1 = ranges.next().ok_or(FdtError::NotFound)?;
913
914 let addr = range0.addr;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000915 // `read_cpu_info_from()` guarantees that we have at most MAX_CPUS.
916 const_assert!(DeviceTreeInfo::gic_patched_size(DeviceTreeInfo::MAX_CPUS).is_some());
Alice Wangabc7d632023-06-14 09:10:14 +0000917 let size = u64::try_from(DeviceTreeInfo::gic_patched_size(num_cpus).unwrap()).unwrap();
Jiyong Park9c63cd12023-03-21 17:53:07 +0900918
919 // range1 is just below range0
920 range1.addr = addr - size;
921 range1.size = Some(size);
922
Pierre-Clément Tosieea2a982024-02-05 15:10:59 +0000923 let (addr0, size0) = range0.to_cells();
924 let (addr1, size1) = range1.to_cells();
925 let value = [addr0, size0.unwrap(), addr1, size1.unwrap()];
Jiyong Park9c63cd12023-03-21 17:53:07 +0900926
927 let mut node =
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000928 fdt.root_mut().next_compatible(cstr!("arm,gic-v3"))?.ok_or(FdtError::NotFound)?;
Jiyong Park9c63cd12023-03-21 17:53:07 +0900929 node.setprop_inplace(cstr!("reg"), flatten(&value))
930}
931
932fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> {
933 const NUM_INTERRUPTS: usize = 4;
934 const CELLS_PER_INTERRUPT: usize = 3;
935 let node = fdt.compatible_nodes(cstr!("arm,armv8-timer"))?.next().ok_or(FdtError::NotFound)?;
936 let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?;
937 let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> =
938 interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect();
939
940 let num_cpus: u32 = num_cpus.try_into().unwrap();
Pierre-Clément Tosi3ad82742024-07-04 10:23:00 +0100941 // TODO(b/350498812): Rework this for >8 vCPUs.
Jiyong Park9c63cd12023-03-21 17:53:07 +0900942 let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8;
Sebastian Enee8e99fa2024-05-23 14:49:41 +0000943
Jiyong Park9c63cd12023-03-21 17:53:07 +0900944 for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) {
945 *v |= cpu_mask;
946 }
947 for v in value.iter_mut() {
948 *v = v.to_be();
949 }
950
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +0000951 let value = value.into_inner();
Jiyong Park9c63cd12023-03-21 17:53:07 +0900952
953 let mut node =
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000954 fdt.root_mut().next_compatible(cstr!("arm,armv8-timer"))?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosi0edc4d62024-02-05 14:13:53 +0000955 node.setprop_inplace(cstr!("interrupts"), value.as_bytes())
Jiyong Park9c63cd12023-03-21 17:53:07 +0900956}
957
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000958fn patch_untrusted_props(fdt: &mut Fdt, props: &BTreeMap<CString, Vec<u8>>) -> libfdt::Result<()> {
959 let avf_node = if let Some(node) = fdt.node_mut(cstr!("/avf"))? {
960 node
961 } else {
Pierre-Clément Tosi244efea2024-02-16 14:48:14 +0000962 fdt.root_mut().add_subnode(cstr!("avf"))?
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +0000963 };
964
965 // The node shouldn't already be present; if it is, return the error.
966 let mut node = avf_node.add_subnode(cstr!("untrusted"))?;
967
968 for (name, value) in props {
969 node.setprop(name, value)?;
970 }
971
972 Ok(())
973}
974
Jiyong Park00ceff32023-03-13 05:43:23 +0000975#[derive(Debug)]
David Dai9bdb10c2024-02-01 22:42:54 -0800976struct VcpufreqInfo {
977 addr: u64,
978 size: u64,
979}
980
981fn patch_vcpufreq(fdt: &mut Fdt, vcpufreq_info: &Option<VcpufreqInfo>) -> libfdt::Result<()> {
982 let mut node = fdt.node_mut(cstr!("/cpufreq"))?.unwrap();
983 if let Some(info) = vcpufreq_info {
984 node.setprop_addrrange_inplace(cstr!("reg"), info.addr, info.size)
985 } else {
986 node.nop()
987 }
988}
989
990#[derive(Debug)]
Jiyong Park6a8789a2023-03-21 14:50:59 +0900991pub struct DeviceTreeInfo {
992 pub kernel_range: Option<Range<usize>>,
993 pub initrd_range: Option<Range<usize>>,
994 pub memory_range: Range<usize>,
Jiyong Parke9d87e82023-03-21 19:28:40 +0900995 bootargs: Option<CString>,
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +0000996 cpus: ArrayVec<[CpuInfo; DeviceTreeInfo::MAX_CPUS]>,
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +0000997 cpu_topology: Option<CpuTopology>,
Jiyong Park00ceff32023-03-13 05:43:23 +0000998 pci_info: PciInfo,
999 serial_info: SerialInfo,
Srivatsa Vaddagiri37713ec2023-04-20 04:04:08 -07001000 pub swiotlb_info: SwiotlbInfo,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001001 device_assignment: Option<DeviceAssignmentInfo>,
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001002 untrusted_props: BTreeMap<CString, Vec<u8>>,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001003 vm_ref_dt_props_info: BTreeMap<CString, Vec<u8>>,
David Dai9bdb10c2024-02-01 22:42:54 -08001004 vcpufreq_info: Option<VcpufreqInfo>,
Jiyong Park00ceff32023-03-13 05:43:23 +00001005}
1006
1007impl DeviceTreeInfo {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001008 const MAX_CPUS: usize = 16;
1009
1010 const fn gic_patched_size(num_cpus: usize) -> Option<usize> {
Alice Wangabc7d632023-06-14 09:10:14 +00001011 const GIC_REDIST_SIZE_PER_CPU: usize = 32 * SIZE_4KB;
1012
1013 GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus)
1014 }
Jiyong Park00ceff32023-03-13 05:43:23 +00001015}
1016
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001017pub fn sanitize_device_tree(
1018 fdt: &mut [u8],
1019 vm_dtbo: Option<&mut [u8]>,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001020 vm_ref_dt: Option<&[u8]>,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001021) -> Result<DeviceTreeInfo, RebootReason> {
1022 let fdt = Fdt::from_mut_slice(fdt).map_err(|e| {
1023 error!("Failed to load FDT: {e}");
1024 RebootReason::InvalidFdt
1025 })?;
1026
1027 let vm_dtbo = match vm_dtbo {
1028 Some(vm_dtbo) => Some(VmDtbo::from_mut_slice(vm_dtbo).map_err(|e| {
1029 error!("Failed to load VM DTBO: {e}");
1030 RebootReason::InvalidFdt
1031 })?),
1032 None => None,
1033 };
1034
1035 let info = parse_device_tree(fdt, vm_dtbo.as_deref())?;
Jiyong Park83316122023-03-21 09:39:39 +09001036
Pierre-Clément Tosi84ba1a82024-10-30 11:27:32 +00001037 fdt.clone_from(FDT_TEMPLATE).map_err(|e| {
Jiyong Parke9d87e82023-03-21 19:28:40 +09001038 error!("Failed to instantiate FDT from the template DT: {e}");
1039 RebootReason::InvalidFdt
1040 })?;
1041
Jaewan Kim9220e852023-12-01 10:58:40 +09001042 fdt.unpack().map_err(|e| {
1043 error!("Failed to unpack DT for patching: {e}");
1044 RebootReason::InvalidFdt
1045 })?;
1046
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001047 if let Some(device_assignment_info) = &info.device_assignment {
1048 let vm_dtbo = vm_dtbo.unwrap();
1049 device_assignment_info.filter(vm_dtbo).map_err(|e| {
1050 error!("Failed to filter VM DTBO: {e}");
1051 RebootReason::InvalidFdt
1052 })?;
1053 // SAFETY: Damaged VM DTBO isn't used in this API after this unsafe block.
1054 // VM DTBO can't be reused in any way as Fdt nor VmDtbo outside of this API because
1055 // it can only be instantiated after validation.
1056 unsafe {
1057 fdt.apply_overlay(vm_dtbo.as_mut()).map_err(|e| {
1058 error!("Failed to apply filtered VM DTBO: {e}");
1059 RebootReason::InvalidFdt
1060 })?;
1061 }
1062 }
1063
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001064 if let Some(vm_ref_dt) = vm_ref_dt {
1065 let vm_ref_dt = Fdt::from_slice(vm_ref_dt).map_err(|e| {
1066 error!("Failed to load VM reference DT: {e}");
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001067 RebootReason::InvalidFdt
1068 })?;
1069
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001070 validate_vm_ref_dt(fdt, vm_ref_dt, &info.vm_ref_dt_props_info).map_err(|e| {
1071 error!("Failed to apply VM reference DT: {e}");
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001072 RebootReason::InvalidFdt
1073 })?;
1074 }
1075
Jiyong Park9c63cd12023-03-21 17:53:07 +09001076 patch_device_tree(fdt, &info)?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001077
Jaewan Kim19b984f2023-12-04 15:16:50 +09001078 // TODO(b/317201360): Ensure no overlapping in <reg> among devices
1079
Jaewan Kim9220e852023-12-01 10:58:40 +09001080 fdt.pack().map_err(|e| {
1081 error!("Failed to unpack DT after patching: {e}");
1082 RebootReason::InvalidFdt
1083 })?;
1084
Jiyong Park6a8789a2023-03-21 14:50:59 +09001085 Ok(info)
Jiyong Park83316122023-03-21 09:39:39 +09001086}
1087
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001088fn parse_device_tree(fdt: &Fdt, vm_dtbo: Option<&VmDtbo>) -> Result<DeviceTreeInfo, RebootReason> {
Jiyong Park6a8789a2023-03-21 14:50:59 +09001089 let kernel_range = read_kernel_range_from(fdt).map_err(|e| {
1090 error!("Failed to read kernel range from DT: {e}");
1091 RebootReason::InvalidFdt
1092 })?;
1093
1094 let initrd_range = read_initrd_range_from(fdt).map_err(|e| {
1095 error!("Failed to read initrd range from DT: {e}");
1096 RebootReason::InvalidFdt
1097 })?;
1098
Alice Wang0d527472023-06-13 14:55:38 +00001099 let memory_range = read_and_validate_memory_range(fdt)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001100
Jiyong Parke9d87e82023-03-21 19:28:40 +09001101 let bootargs = read_bootargs_from(fdt).map_err(|e| {
1102 error!("Failed to read bootargs from DT: {e}");
1103 RebootReason::InvalidFdt
1104 })?;
1105
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001106 let (cpus, cpu_topology) = read_cpu_info_from(fdt).map_err(|e| {
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001107 error!("Failed to read CPU info from DT: {e}");
Jiyong Park6a8789a2023-03-21 14:50:59 +09001108 RebootReason::InvalidFdt
1109 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001110 validate_cpu_info(&cpus).map_err(|e| {
1111 error!("Failed to validate CPU info from DT: {e}");
Alice Wangabc7d632023-06-14 09:10:14 +00001112 RebootReason::InvalidFdt
1113 })?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001114
David Dai9bdb10c2024-02-01 22:42:54 -08001115 let vcpufreq_info = read_vcpufreq_info(fdt).map_err(|e| {
1116 error!("Failed to read vcpufreq info from DT: {e}");
1117 RebootReason::InvalidFdt
1118 })?;
1119 if let Some(ref info) = vcpufreq_info {
1120 validate_vcpufreq_info(info, &cpus).map_err(|e| {
1121 error!("Failed to validate vcpufreq info from DT: {e}");
1122 RebootReason::InvalidFdt
1123 })?;
1124 }
1125
Jiyong Park6a8789a2023-03-21 14:50:59 +09001126 let pci_info = read_pci_info_from(fdt).map_err(|e| {
1127 error!("Failed to read pci info from DT: {e}");
1128 RebootReason::InvalidFdt
1129 })?;
Jiyong Park0ee65392023-03-27 20:52:45 +09001130 validate_pci_info(&pci_info, &memory_range)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001131
Sebastian Enee8e99fa2024-05-23 14:49:41 +00001132 let wdt_info = read_wdt_info_from(fdt).map_err(|e| {
1133 error!("Failed to read vCPU stall detector info from DT: {e}");
1134 RebootReason::InvalidFdt
1135 })?;
1136 validate_wdt_info(&wdt_info, cpus.len())?;
1137
Jiyong Park6a8789a2023-03-21 14:50:59 +09001138 let serial_info = read_serial_info_from(fdt).map_err(|e| {
1139 error!("Failed to read serial info from DT: {e}");
1140 RebootReason::InvalidFdt
1141 })?;
1142
Alice Wang9cfbfd62023-06-14 11:19:03 +00001143 let swiotlb_info = SwiotlbInfo::new_from_fdt(fdt).map_err(|e| {
Jiyong Park6a8789a2023-03-21 14:50:59 +09001144 error!("Failed to read swiotlb info from DT: {e}");
1145 RebootReason::InvalidFdt
1146 })?;
Srivatsa Vaddagiri2df297f2023-04-12 03:11:05 -07001147 validate_swiotlb_info(&swiotlb_info, &memory_range)?;
Jiyong Park6a8789a2023-03-21 14:50:59 +09001148
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001149 let device_assignment = match vm_dtbo {
Jaewan Kim52477ae2023-11-21 21:20:52 +09001150 Some(vm_dtbo) => {
1151 if let Some(hypervisor) = hyp::get_device_assigner() {
1152 DeviceAssignmentInfo::parse(fdt, vm_dtbo, hypervisor).map_err(|e| {
1153 error!("Failed to parse device assignment from DT and VM DTBO: {e}");
1154 RebootReason::InvalidFdt
1155 })?
1156 } else {
1157 warn!(
1158 "Device assignment is ignored because device assigning hypervisor is missing"
1159 );
1160 None
1161 }
1162 }
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001163 None => None,
1164 };
1165
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001166 let untrusted_props = parse_untrusted_props(fdt).map_err(|e| {
1167 error!("Failed to read untrusted properties: {e}");
1168 RebootReason::InvalidFdt
1169 })?;
1170 validate_untrusted_props(&untrusted_props).map_err(|e| {
1171 error!("Failed to validate untrusted properties: {e}");
1172 RebootReason::InvalidFdt
1173 })?;
1174
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001175 let vm_ref_dt_props_info = parse_vm_ref_dt(fdt).map_err(|e| {
Seungjae Yoo013f4c42024-01-02 13:04:19 +09001176 error!("Failed to read names of properties under /avf from DT: {e}");
1177 RebootReason::InvalidFdt
1178 })?;
Seungjae Yooed67fd52023-11-29 18:54:36 +09001179
Jiyong Park00ceff32023-03-13 05:43:23 +00001180 Ok(DeviceTreeInfo {
Jiyong Park6a8789a2023-03-21 14:50:59 +09001181 kernel_range,
1182 initrd_range,
1183 memory_range,
Jiyong Parke9d87e82023-03-21 19:28:40 +09001184 bootargs,
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001185 cpus,
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001186 cpu_topology,
Jiyong Park6a8789a2023-03-21 14:50:59 +09001187 pci_info,
1188 serial_info,
1189 swiotlb_info,
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001190 device_assignment,
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001191 untrusted_props,
Seungjae Yoof0af81d2024-01-17 13:48:36 +09001192 vm_ref_dt_props_info,
David Dai9bdb10c2024-02-01 22:42:54 -08001193 vcpufreq_info,
Jiyong Park00ceff32023-03-13 05:43:23 +00001194 })
1195}
1196
Jiyong Park9c63cd12023-03-21 17:53:07 +09001197fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> {
1198 if let Some(initrd_range) = &info.initrd_range {
1199 patch_initrd_range(fdt, initrd_range).map_err(|e| {
1200 error!("Failed to patch initrd range to DT: {e}");
1201 RebootReason::InvalidFdt
1202 })?;
1203 }
1204 patch_memory_range(fdt, &info.memory_range).map_err(|e| {
1205 error!("Failed to patch memory range to DT: {e}");
1206 RebootReason::InvalidFdt
1207 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +09001208 if let Some(bootargs) = &info.bootargs {
1209 patch_bootargs(fdt, bootargs.as_c_str()).map_err(|e| {
1210 error!("Failed to patch bootargs to DT: {e}");
1211 RebootReason::InvalidFdt
1212 })?;
1213 }
Pierre-Clément Tosia0823f12024-02-15 16:41:05 +00001214 patch_cpus(fdt, &info.cpus, &info.cpu_topology).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001215 error!("Failed to patch cpus to DT: {e}");
1216 RebootReason::InvalidFdt
1217 })?;
David Dai9bdb10c2024-02-01 22:42:54 -08001218 patch_vcpufreq(fdt, &info.vcpufreq_info).map_err(|e| {
1219 error!("Failed to patch vcpufreq info to DT: {e}");
1220 RebootReason::InvalidFdt
1221 })?;
Jiyong Park9c63cd12023-03-21 17:53:07 +09001222 patch_pci_info(fdt, &info.pci_info).map_err(|e| {
1223 error!("Failed to patch pci info to DT: {e}");
1224 RebootReason::InvalidFdt
1225 })?;
Sebastian Enee8e99fa2024-05-23 14:49:41 +00001226 patch_wdt_info(fdt, info.cpus.len()).map_err(|e| {
1227 error!("Failed to patch wdt info to DT: {e}");
1228 RebootReason::InvalidFdt
1229 })?;
Jiyong Park9c63cd12023-03-21 17:53:07 +09001230 patch_serial_info(fdt, &info.serial_info).map_err(|e| {
1231 error!("Failed to patch serial info to DT: {e}");
1232 RebootReason::InvalidFdt
1233 })?;
1234 patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| {
1235 error!("Failed to patch swiotlb info to DT: {e}");
1236 RebootReason::InvalidFdt
1237 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001238 patch_gic(fdt, info.cpus.len()).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001239 error!("Failed to patch gic info to DT: {e}");
1240 RebootReason::InvalidFdt
1241 })?;
Pierre-Clément Tosi689e4732024-02-05 14:39:51 +00001242 patch_timer(fdt, info.cpus.len()).map_err(|e| {
Jiyong Park9c63cd12023-03-21 17:53:07 +09001243 error!("Failed to patch timer info to DT: {e}");
1244 RebootReason::InvalidFdt
1245 })?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001246 if let Some(device_assignment) = &info.device_assignment {
1247 // Note: We patch values after VM DTBO is overlaid because patch may require more space
1248 // then VM DTBO's underlying slice is allocated.
1249 device_assignment.patch(fdt).map_err(|e| {
1250 error!("Failed to patch device assignment info to DT: {e}");
1251 RebootReason::InvalidFdt
1252 })?;
Jaewan Kim50246682024-03-11 23:18:54 +09001253 } else {
1254 device_assignment::clean(fdt).map_err(|e| {
1255 error!("Failed to clean pre-polulated DT nodes for device assignment: {e}");
1256 RebootReason::InvalidFdt
1257 })?;
Jaewan Kimc6e023b2023-10-12 15:11:05 +09001258 }
Pierre-Clément Tosi54e84b52024-02-15 20:06:22 +00001259 patch_untrusted_props(fdt, &info.untrusted_props).map_err(|e| {
1260 error!("Failed to patch untrusted properties: {e}");
1261 RebootReason::InvalidFdt
1262 })?;
Jiyong Parke9d87e82023-03-21 19:28:40 +09001263
Jiyong Park9c63cd12023-03-21 17:53:07 +09001264 Ok(())
1265}
1266
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001267/// Modifies the input DT according to the fields of the configuration.
1268pub fn modify_for_next_stage(
1269 fdt: &mut Fdt,
1270 bcc: &[u8],
1271 new_instance: bool,
1272 strict_boot: bool,
Alan Stokes65618332023-12-15 14:09:25 +00001273 debug_policy: Option<&[u8]>,
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001274 debuggable: bool,
Pierre-Clément Tosi80251972023-07-12 12:51:12 +00001275 kaslr_seed: u64,
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001276) -> libfdt::Result<()> {
Pierre-Clément Tosieb887ac2023-05-02 13:33:37 +00001277 if let Some(debug_policy) = debug_policy {
1278 let backup = Vec::from(fdt.as_slice());
1279 fdt.unpack()?;
1280 let backup_fdt = Fdt::from_slice(backup.as_slice()).unwrap();
1281 if apply_debug_policy(fdt, backup_fdt, debug_policy)? {
1282 info!("Debug policy applied.");
1283 } else {
1284 // apply_debug_policy restored fdt to backup_fdt so unpack it again.
1285 fdt.unpack()?;
1286 }
1287 } else {
1288 info!("No debug policy found.");
1289 fdt.unpack()?;
1290 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001291
Jiyong Parke9d87e82023-03-21 19:28:40 +09001292 patch_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?;
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001293
Alice Wang56ec45b2023-06-15 08:30:32 +00001294 if let Some(mut chosen) = fdt.chosen_mut()? {
1295 empty_or_delete_prop(&mut chosen, cstr!("avf,strict-boot"), strict_boot)?;
1296 empty_or_delete_prop(&mut chosen, cstr!("avf,new-instance"), new_instance)?;
Pierre-Clément Tosi80251972023-07-12 12:51:12 +00001297 chosen.setprop_inplace(cstr!("kaslr-seed"), &kaslr_seed.to_be_bytes())?;
Alice Wang56ec45b2023-06-15 08:30:32 +00001298 };
Jiyong Park32f37ef2023-05-17 16:15:58 +09001299 if !debuggable {
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001300 if let Some(bootargs) = read_bootargs_from(fdt)? {
1301 filter_out_dangerous_bootargs(fdt, &bootargs)?;
1302 }
1303 }
1304
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001305 fdt.pack()?;
1306
1307 Ok(())
1308}
1309
Jiyong Parke9d87e82023-03-21 19:28:40 +09001310/// Patch the "google,open-dice"-compatible reserved-memory node to point to the bcc range
1311fn patch_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> {
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001312 // We reject DTs with missing reserved-memory node as validation should have checked that the
1313 // "swiotlb" subnode (compatible = "restricted-dma-pool") was present.
Jiyong Parke9d87e82023-03-21 19:28:40 +09001314 let node = fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001315
Jiyong Parke9d87e82023-03-21 19:28:40 +09001316 let mut node = node.next_compatible(cstr!("google,open-dice"))?.ok_or(FdtError::NotFound)?;
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001317
Jiyong Parke9d87e82023-03-21 19:28:40 +09001318 let addr: u64 = addr.try_into().unwrap();
1319 let size: u64 = size.try_into().unwrap();
1320 node.setprop_inplace(cstr!("reg"), flatten(&[addr.to_be_bytes(), size.to_be_bytes()]))
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001321}
1322
Alice Wang56ec45b2023-06-15 08:30:32 +00001323fn empty_or_delete_prop(
1324 fdt_node: &mut FdtNodeMut,
1325 prop_name: &CStr,
1326 keep_prop: bool,
1327) -> libfdt::Result<()> {
1328 if keep_prop {
1329 fdt_node.setprop_empty(prop_name)
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001330 } else {
Alice Wang56ec45b2023-06-15 08:30:32 +00001331 fdt_node
1332 .delprop(prop_name)
1333 .or_else(|e| if e == FdtError::NotFound { Ok(()) } else { Err(e) })
Pierre-Clément Tosi4ba79662023-02-13 11:22:41 +00001334 }
Pierre-Clément Tosidb74cb12022-12-08 13:56:25 +00001335}
Jiyong Parkc23426b2023-04-10 17:32:27 +09001336
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001337/// Apply the debug policy overlay to the guest DT.
1338///
1339/// Returns Ok(true) on success, Ok(false) on recovered failure and Err(_) on corruption of the DT.
Pierre-Clément Tosieb887ac2023-05-02 13:33:37 +00001340fn apply_debug_policy(
1341 fdt: &mut Fdt,
1342 backup_fdt: &Fdt,
1343 debug_policy: &[u8],
1344) -> libfdt::Result<bool> {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001345 let mut debug_policy = Vec::from(debug_policy);
1346 let overlay = match Fdt::from_mut_slice(debug_policy.as_mut_slice()) {
Jiyong Parkc23426b2023-04-10 17:32:27 +09001347 Ok(overlay) => overlay,
1348 Err(e) => {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001349 warn!("Corrupted debug policy found: {e}. Not applying.");
1350 return Ok(false);
Jiyong Parkc23426b2023-04-10 17:32:27 +09001351 }
1352 };
Jiyong Parkc23426b2023-04-10 17:32:27 +09001353
Andrew Walbran20bb4e42023-07-07 13:55:55 +01001354 // SAFETY: on failure, the corrupted DT is restored using the backup.
Jiyong Parkc23426b2023-04-10 17:32:27 +09001355 if let Err(e) = unsafe { fdt.apply_overlay(overlay) } {
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001356 warn!("Failed to apply debug policy: {e}. Recovering...");
Pierre-Clément Tosice0b36d2024-01-26 10:50:05 +00001357 fdt.clone_from(backup_fdt)?;
Jiyong Parkc23426b2023-04-10 17:32:27 +09001358 // A successful restoration is considered success because an invalid debug policy
1359 // shouldn't DOS the pvmfw
Pierre-Clément Tosia50167b2023-05-02 13:19:29 +00001360 Ok(false)
1361 } else {
1362 Ok(true)
Jiyong Parkc23426b2023-04-10 17:32:27 +09001363 }
Jiyong Parkc23426b2023-04-10 17:32:27 +09001364}
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001365
Pierre-Clément Tosi1fbc2e92023-05-02 17:28:17 +00001366fn has_common_debug_policy(fdt: &Fdt, debug_feature_name: &CStr) -> libfdt::Result<bool> {
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001367 if let Some(node) = fdt.node(cstr!("/avf/guest/common"))? {
1368 if let Some(value) = node.getprop_u32(debug_feature_name)? {
1369 return Ok(value == 1);
1370 }
1371 }
1372 Ok(false) // if the policy doesn't exist or not 1, don't enable the debug feature
1373}
1374
1375fn filter_out_dangerous_bootargs(fdt: &mut Fdt, bootargs: &CStr) -> libfdt::Result<()> {
Pierre-Clément Tosi1fbc2e92023-05-02 17:28:17 +00001376 let has_crashkernel = has_common_debug_policy(fdt, cstr!("ramdump"))?;
1377 let has_console = has_common_debug_policy(fdt, cstr!("log"))?;
Jiyong Parkc5d2ef22023-04-11 01:23:46 +09001378
1379 let accepted: &[(&str, Box<dyn Fn(Option<&str>) -> bool>)] = &[
1380 ("panic", Box::new(|v| if let Some(v) = v { v == "=-1" } else { false })),
1381 ("crashkernel", Box::new(|_| has_crashkernel)),
1382 ("console", Box::new(|_| has_console)),
1383 ];
1384
1385 // parse and filter out unwanted
1386 let mut filtered = Vec::new();
1387 for arg in BootArgsIterator::new(bootargs).map_err(|e| {
1388 info!("Invalid bootarg: {e}");
1389 FdtError::BadValue
1390 })? {
1391 match accepted.iter().find(|&t| t.0 == arg.name()) {
1392 Some((_, pred)) if pred(arg.value()) => filtered.push(arg),
1393 _ => debug!("Rejected bootarg {}", arg.as_ref()),
1394 }
1395 }
1396
1397 // flatten into a new C-string
1398 let mut new_bootargs = Vec::new();
1399 for (i, arg) in filtered.iter().enumerate() {
1400 if i != 0 {
1401 new_bootargs.push(b' '); // separator
1402 }
1403 new_bootargs.extend_from_slice(arg.as_ref().as_bytes());
1404 }
1405 new_bootargs.push(b'\0');
1406
1407 let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?;
1408 node.setprop(cstr!("bootargs"), new_bootargs.as_slice())
1409}