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