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