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