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