Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [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 | //! Low-level entry and exit points of pvmfw. |
| 16 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 17 | use crate::config; |
Pierre-Clément Tosi | 41748ed | 2023-03-31 18:20:40 +0100 | [diff] [blame] | 18 | use crate::crypto; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 19 | use crate::fdt; |
Pierre-Clément Tosi | fc53115 | 2022-10-20 12:22:23 +0100 | [diff] [blame] | 20 | use crate::heap; |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame] | 21 | use crate::memory; |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 22 | use crate::rand; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 23 | use core::arch::asm; |
Jakob Vukalovic | 4c1edbe | 2023-04-17 19:10:57 +0100 | [diff] [blame] | 24 | use core::mem::{drop, size_of}; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 25 | use core::num::NonZeroUsize; |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 26 | use core::ops::Range; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 27 | use core::slice; |
Srivatsa Vaddagiri | 37713ec | 2023-04-20 04:04:08 -0700 | [diff] [blame] | 28 | use hyp::{get_hypervisor, HypervisorCap}; |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 29 | use log::debug; |
| 30 | use log::error; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 31 | use log::info; |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 32 | use log::warn; |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 33 | use log::LevelFilter; |
Alice Wang | 4be4dd0 | 2023-06-07 07:50:40 +0000 | [diff] [blame] | 34 | use vmbase::util::RangeExt as _; |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 35 | use vmbase::{ |
Alice Wang | 4c70d14 | 2023-06-06 11:52:33 +0000 | [diff] [blame] | 36 | console, |
| 37 | layout::{self, crosvm}, |
| 38 | logger, main, |
Alice Wang | 93ee98a | 2023-06-08 08:20:39 +0000 | [diff] [blame] | 39 | memory::{min_dcache_line_size, MemoryTracker, MEMORY, SIZE_2MB, SIZE_4KB}, |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 40 | power::reboot, |
| 41 | }; |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 42 | use zeroize::Zeroize; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 43 | |
| 44 | #[derive(Debug, Clone)] |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 45 | pub enum RebootReason { |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 46 | /// A malformed BCC was received. |
| 47 | InvalidBcc, |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 48 | /// An invalid configuration was appended to pvmfw. |
| 49 | InvalidConfig, |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 50 | /// An unexpected internal error happened. |
| 51 | InternalError, |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 52 | /// The provided FDT was invalid. |
| 53 | InvalidFdt, |
| 54 | /// The provided payload was invalid. |
| 55 | InvalidPayload, |
| 56 | /// The provided ramdisk was invalid. |
| 57 | InvalidRamdisk, |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 58 | /// Failed to verify the payload. |
| 59 | PayloadVerificationError, |
Pierre-Clément Tosi | 4f4f5eb | 2022-12-08 14:31:42 +0000 | [diff] [blame] | 60 | /// DICE layering process failed. |
| 61 | SecretDerivationError, |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | main!(start); |
| 65 | |
| 66 | /// Entry point for pVM firmware. |
| 67 | pub fn start(fdt_address: u64, payload_start: u64, payload_size: u64, _arg3: u64) { |
| 68 | // Limitations in this function: |
| 69 | // - can't access non-pvmfw memory (only statically-mapped memory) |
| 70 | // - can't access MMIO (therefore, no logging) |
| 71 | |
Pierre-Clément Tosi | f03089c | 2023-05-15 17:33:39 +0000 | [diff] [blame] | 72 | // SAFETY - This function should and will only be called once, here. |
| 73 | unsafe { heap::init() }; |
| 74 | |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 75 | match main_wrapper(fdt_address as usize, payload_start as usize, payload_size as usize) { |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 76 | Ok((entry, bcc)) => jump_to_payload(fdt_address, entry.try_into().unwrap(), bcc), |
Pierre-Clément Tosi | d836b5b | 2022-12-05 10:49:38 +0000 | [diff] [blame] | 77 | Err(_) => reboot(), // TODO(b/220071963) propagate the reason back to the host. |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // if we reach this point and return, vmbase::entry::rust_entry() will call power::shutdown(). |
| 81 | } |
| 82 | |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 83 | struct MemorySlices<'a> { |
| 84 | fdt: &'a mut libfdt::Fdt, |
| 85 | kernel: &'a [u8], |
| 86 | ramdisk: Option<&'a [u8]>, |
| 87 | } |
| 88 | |
| 89 | impl<'a> MemorySlices<'a> { |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 90 | fn new(fdt: usize, kernel: usize, kernel_size: usize) -> Result<Self, RebootReason> { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 91 | // SAFETY - SIZE_2MB is non-zero. |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 92 | const FDT_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(SIZE_2MB) }; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 93 | // TODO - Only map the FDT as read-only, until we modify it right before jump_to_payload() |
| 94 | // e.g. by generating a DTBO for a template DT in main() and, on return, re-map DT as RW, |
| 95 | // overwrite with the template DT and apply the DTBO. |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 96 | let range = MEMORY.lock().as_mut().unwrap().alloc_mut(fdt, FDT_SIZE).map_err(|e| { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 97 | error!("Failed to allocate the FDT range: {e}"); |
| 98 | RebootReason::InternalError |
| 99 | })?; |
| 100 | |
| 101 | // SAFETY - The tracker validated the range to be in main memory, mapped, and not overlap. |
| 102 | let fdt = unsafe { slice::from_raw_parts_mut(range.start as *mut u8, range.len()) }; |
| 103 | let fdt = libfdt::Fdt::from_mut_slice(fdt).map_err(|e| { |
| 104 | error!("Failed to spawn the FDT wrapper: {e}"); |
| 105 | RebootReason::InvalidFdt |
| 106 | })?; |
| 107 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 108 | let info = fdt::sanitize_device_tree(fdt)?; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 109 | debug!("Fdt passed validation!"); |
| 110 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 111 | let memory_range = info.memory_range; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 112 | debug!("Resizing MemoryTracker to range {memory_range:#x?}"); |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 113 | MEMORY.lock().as_mut().unwrap().shrink(&memory_range).map_err(|e| { |
| 114 | error!("Failed to use memory range value from DT: {memory_range:#x?}: {e}"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 115 | RebootReason::InvalidFdt |
| 116 | })?; |
| 117 | |
Pierre-Clément Tosi | f19c0e6 | 2023-05-02 13:56:58 +0000 | [diff] [blame] | 118 | if get_hypervisor().has_cap(HypervisorCap::DYNAMIC_MEM_SHARE) { |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 119 | MEMORY.lock().as_mut().unwrap().init_dynamic_shared_pool().map_err(|e| { |
Pierre-Clément Tosi | f19c0e6 | 2023-05-02 13:56:58 +0000 | [diff] [blame] | 120 | error!("Failed to initialize dynamically shared pool: {e}"); |
| 121 | RebootReason::InternalError |
| 122 | })?; |
| 123 | } else { |
Srivatsa Vaddagiri | 37713ec | 2023-04-20 04:04:08 -0700 | [diff] [blame] | 124 | let range = info.swiotlb_info.fixed_range().ok_or_else(|| { |
| 125 | error!("Pre-shared pool range not specified in swiotlb node"); |
| 126 | RebootReason::InvalidFdt |
| 127 | })?; |
| 128 | |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 129 | MEMORY.lock().as_mut().unwrap().init_static_shared_pool(range).map_err(|e| { |
Srivatsa Vaddagiri | 37713ec | 2023-04-20 04:04:08 -0700 | [diff] [blame] | 130 | error!("Failed to initialize pre-shared pool {e}"); |
| 131 | RebootReason::InvalidFdt |
| 132 | })?; |
| 133 | } |
| 134 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 135 | let kernel_range = if let Some(r) = info.kernel_range { |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 136 | MEMORY.lock().as_mut().unwrap().alloc_range(&r).map_err(|e| { |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 137 | error!("Failed to obtain the kernel range with DT range: {e}"); |
| 138 | RebootReason::InternalError |
| 139 | })? |
| 140 | } else if cfg!(feature = "legacy") { |
| 141 | warn!("Failed to find the kernel range in the DT; falling back to legacy ABI"); |
| 142 | |
| 143 | let kernel_size = NonZeroUsize::new(kernel_size).ok_or_else(|| { |
| 144 | error!("Invalid kernel size: {kernel_size:#x}"); |
| 145 | RebootReason::InvalidPayload |
| 146 | })?; |
| 147 | |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 148 | MEMORY.lock().as_mut().unwrap().alloc(kernel, kernel_size).map_err(|e| { |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 149 | error!("Failed to obtain the kernel range with legacy range: {e}"); |
| 150 | RebootReason::InternalError |
| 151 | })? |
| 152 | } else { |
| 153 | error!("Failed to locate the kernel from the DT"); |
| 154 | return Err(RebootReason::InvalidPayload); |
| 155 | }; |
| 156 | |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 157 | // SAFETY - The tracker validated the range to be in main memory, mapped, and not overlap. |
| 158 | let kernel = |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 159 | unsafe { slice::from_raw_parts(kernel_range.start as *const u8, kernel_range.len()) }; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 160 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 161 | let ramdisk = if let Some(r) = info.initrd_range { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 162 | debug!("Located ramdisk at {r:?}"); |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 163 | let r = MEMORY.lock().as_mut().unwrap().alloc_range(&r).map_err(|e| { |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 164 | error!("Failed to obtain the initrd range: {e}"); |
| 165 | RebootReason::InvalidRamdisk |
| 166 | })?; |
| 167 | |
| 168 | // SAFETY - The region was validated by memory to be in main memory, mapped, and |
| 169 | // not overlap. |
| 170 | Some(unsafe { slice::from_raw_parts(r.start as *const u8, r.len()) }) |
| 171 | } else { |
| 172 | info!("Couldn't locate the ramdisk from the device tree"); |
| 173 | None |
| 174 | }; |
| 175 | |
| 176 | Ok(Self { fdt, kernel, ramdisk }) |
| 177 | } |
| 178 | } |
| 179 | |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 180 | /// Sets up the environment for main() and wraps its result for start(). |
| 181 | /// |
| 182 | /// Provide the abstractions necessary for start() to abort the pVM boot and for main() to run with |
| 183 | /// the assumption that its environment has been properly configured. |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 184 | fn main_wrapper( |
| 185 | fdt: usize, |
| 186 | payload: usize, |
| 187 | payload_size: usize, |
| 188 | ) -> Result<(usize, Range<usize>), RebootReason> { |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 189 | // Limitations in this function: |
| 190 | // - only access MMIO once (and while) it has been mapped and configured |
| 191 | // - only perform logging once the logger has been initialized |
| 192 | // - only access non-pvmfw memory once (and while) it has been mapped |
Pierre-Clément Tosi | fc53115 | 2022-10-20 12:22:23 +0100 | [diff] [blame] | 193 | |
Pierre-Clément Tosi | dbd7286 | 2022-10-21 14:31:02 +0100 | [diff] [blame] | 194 | logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 195 | |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 196 | // Use debug!() to avoid printing to the UART if we failed to configure it as only local |
| 197 | // builds that have tweaked the logger::init() call will actually attempt to log the message. |
| 198 | |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 199 | get_hypervisor().mmio_guard_init().map_err(|e| { |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 200 | debug!("{e}"); |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 201 | RebootReason::InternalError |
| 202 | })?; |
| 203 | |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 204 | get_hypervisor().mmio_guard_map(console::BASE_ADDRESS).map_err(|e| { |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 205 | debug!("Failed to configure the UART: {e}"); |
| 206 | RebootReason::InternalError |
| 207 | })?; |
| 208 | |
Pierre-Clément Tosi | 41748ed | 2023-03-31 18:20:40 +0100 | [diff] [blame] | 209 | crypto::init(); |
| 210 | |
Alice Wang | 807fa59 | 2023-06-02 09:54:43 +0000 | [diff] [blame] | 211 | let page_table = memory::init_page_table().map_err(|e| { |
Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 212 | error!("Failed to set up the dynamic page tables: {e}"); |
| 213 | RebootReason::InternalError |
| 214 | })?; |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 215 | |
| 216 | // SAFETY - We only get the appended payload from here, once. The region was statically mapped, |
| 217 | // then remapped by `init_page_table()`. |
| 218 | let appended_data = unsafe { get_appended_data_slice() }; |
| 219 | |
| 220 | let mut appended = AppendedPayload::new(appended_data).ok_or_else(|| { |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 221 | error!("No valid configuration found"); |
| 222 | RebootReason::InvalidConfig |
Pierre-Clément Tosi | a8a4a20 | 2022-11-03 14:16:46 +0000 | [diff] [blame] | 223 | })?; |
| 224 | |
Pierre-Clément Tosi | efe780c | 2023-02-21 21:36:30 +0000 | [diff] [blame] | 225 | let (bcc_slice, debug_policy) = appended.get_entries(); |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 226 | |
Jakob Vukalovic | 4c1edbe | 2023-04-17 19:10:57 +0100 | [diff] [blame] | 227 | // Up to this point, we were using the built-in static (from .rodata) page tables. |
Alice Wang | 4c70d14 | 2023-06-06 11:52:33 +0000 | [diff] [blame] | 228 | MEMORY.lock().replace(MemoryTracker::new( |
| 229 | page_table, |
Alice Wang | 63f4c9e | 2023-06-12 09:36:43 +0000 | [diff] [blame^] | 230 | crosvm::MEM_START..layout::MAX_VIRT_ADDR, |
Alice Wang | 89d2959 | 2023-06-12 09:41:29 +0000 | [diff] [blame] | 231 | crosvm::MMIO_RANGE, |
Alice Wang | 5bb7950 | 2023-06-12 09:25:07 +0000 | [diff] [blame] | 232 | Some(memory::appended_payload_range()), |
Alice Wang | 4c70d14 | 2023-06-06 11:52:33 +0000 | [diff] [blame] | 233 | )); |
Jakob Vukalovic | 4c1edbe | 2023-04-17 19:10:57 +0100 | [diff] [blame] | 234 | |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 235 | let slices = MemorySlices::new(fdt, payload, payload_size)?; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 236 | |
Pierre-Clément Tosi | a59103d | 2023-02-02 14:46:55 +0000 | [diff] [blame] | 237 | rand::init().map_err(|e| { |
| 238 | error!("Failed to initialize rand: {e}"); |
| 239 | RebootReason::InternalError |
| 240 | })?; |
| 241 | |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 242 | // This wrapper allows main() to be blissfully ignorant of platform details. |
Jakob Vukalovic | b99905d | 2023-04-20 15:46:02 +0100 | [diff] [blame] | 243 | let next_bcc = crate::main(slices.fdt, slices.kernel, slices.ramdisk, bcc_slice, debug_policy)?; |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 244 | |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 245 | // Writable-dirty regions will be flushed when MemoryTracker is dropped. |
| 246 | bcc_slice.zeroize(); |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 247 | |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 248 | info!("Expecting a bug making MMIO_GUARD_UNMAP return NOT_SUPPORTED on success"); |
Jakob Vukalovic | 85a00d7 | 2023-04-20 09:51:10 +0100 | [diff] [blame] | 249 | MEMORY.lock().as_mut().unwrap().mmio_unmap_all().map_err(|e| { |
Andrew Walbran | 1969063 | 2022-12-07 16:41:30 +0000 | [diff] [blame] | 250 | error!("Failed to unshare MMIO ranges: {e}"); |
| 251 | RebootReason::InternalError |
| 252 | })?; |
Pierre-Clément Tosi | f19c0e6 | 2023-05-02 13:56:58 +0000 | [diff] [blame] | 253 | // Call unshare_all_memory here (instead of relying on the dtor) while UART is still mapped. |
| 254 | MEMORY.lock().as_mut().unwrap().unshare_all_memory(); |
Alice Wang | 90e6f16 | 2023-04-17 13:49:45 +0000 | [diff] [blame] | 255 | get_hypervisor().mmio_guard_unmap(console::BASE_ADDRESS).map_err(|e| { |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 256 | error!("Failed to unshare the UART: {e}"); |
| 257 | RebootReason::InternalError |
| 258 | })?; |
Jakob Vukalovic | 4c1edbe | 2023-04-17 19:10:57 +0100 | [diff] [blame] | 259 | |
| 260 | // Drop MemoryTracker and deactivate page table. |
| 261 | drop(MEMORY.lock().take()); |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 262 | |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 263 | Ok((slices.kernel.as_ptr() as usize, next_bcc)) |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 264 | } |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 265 | |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 266 | fn jump_to_payload(fdt_address: u64, payload_start: u64, bcc: Range<usize>) -> ! { |
| 267 | const ASM_STP_ALIGN: usize = size_of::<u64>() * 2; |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 268 | const SCTLR_EL1_RES1: u64 = (0b11 << 28) | (0b101 << 20) | (0b1 << 11); |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 269 | // Stage 1 instruction access cacheability is unaffected. |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 270 | const SCTLR_EL1_I: u64 = 0b1 << 12; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 271 | // SETEND instruction disabled at EL0 in aarch32 mode. |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 272 | const SCTLR_EL1_SED: u64 = 0b1 << 8; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 273 | // Various IT instructions are disabled at EL0 in aarch32 mode. |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 274 | const SCTLR_EL1_ITD: u64 = 0b1 << 7; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 275 | |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 276 | const SCTLR_EL1_VAL: u64 = SCTLR_EL1_RES1 | SCTLR_EL1_ITD | SCTLR_EL1_SED | SCTLR_EL1_I; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 277 | |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 278 | let scratch = layout::scratch_range(); |
| 279 | |
| 280 | assert_ne!(scratch.len(), 0, "scratch memory is empty."); |
| 281 | assert_eq!(scratch.start % ASM_STP_ALIGN, 0, "scratch memory is misaligned."); |
| 282 | assert_eq!(scratch.end % ASM_STP_ALIGN, 0, "scratch memory is misaligned."); |
| 283 | |
| 284 | assert!(bcc.is_within(&scratch)); |
| 285 | assert_eq!(bcc.start % ASM_STP_ALIGN, 0, "Misaligned guest BCC."); |
| 286 | assert_eq!(bcc.end % ASM_STP_ALIGN, 0, "Misaligned guest BCC."); |
| 287 | |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 288 | let stack = memory::stack_range(); |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 289 | |
| 290 | assert_ne!(stack.len(), 0, "stack region is empty."); |
| 291 | assert_eq!(stack.start % ASM_STP_ALIGN, 0, "Misaligned stack region."); |
| 292 | assert_eq!(stack.end % ASM_STP_ALIGN, 0, "Misaligned stack region."); |
| 293 | |
| 294 | // Zero all memory that could hold secrets and that can't be safely written to from Rust. |
Pierre-Clément Tosi | 6c0d48b | 2022-11-07 11:00:32 +0000 | [diff] [blame] | 295 | // Disable the exception vector, caches and page table and then jump to the payload at the |
| 296 | // given address, passing it the given FDT pointer. |
| 297 | // |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 298 | // SAFETY - We're exiting pvmfw by passing the register values we need to a noreturn asm!(). |
| 299 | unsafe { |
| 300 | asm!( |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 301 | "cmp {scratch}, {bcc}", |
| 302 | "b.hs 1f", |
| 303 | |
| 304 | // Zero .data & .bss until BCC. |
| 305 | "0: stp xzr, xzr, [{scratch}], 16", |
| 306 | "cmp {scratch}, {bcc}", |
| 307 | "b.lo 0b", |
| 308 | |
| 309 | "1:", |
| 310 | // Skip BCC. |
| 311 | "mov {scratch}, {bcc_end}", |
| 312 | "cmp {scratch}, {scratch_end}", |
| 313 | "b.hs 1f", |
| 314 | |
| 315 | // Keep zeroing .data & .bss. |
| 316 | "0: stp xzr, xzr, [{scratch}], 16", |
| 317 | "cmp {scratch}, {scratch_end}", |
| 318 | "b.lo 0b", |
| 319 | |
| 320 | "1:", |
| 321 | // Flush d-cache over .data & .bss (including BCC). |
| 322 | "0: dc cvau, {cache_line}", |
| 323 | "add {cache_line}, {cache_line}, {dcache_line_size}", |
| 324 | "cmp {cache_line}, {scratch_end}", |
| 325 | "b.lo 0b", |
| 326 | |
| 327 | "mov {cache_line}, {stack}", |
| 328 | // Zero stack region. |
| 329 | "0: stp xzr, xzr, [{stack}], 16", |
| 330 | "cmp {stack}, {stack_end}", |
| 331 | "b.lo 0b", |
| 332 | |
| 333 | // Flush d-cache over stack region. |
| 334 | "0: dc cvau, {cache_line}", |
| 335 | "add {cache_line}, {cache_line}, {dcache_line_size}", |
| 336 | "cmp {cache_line}, {stack_end}", |
| 337 | "b.lo 0b", |
| 338 | |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 339 | "msr sctlr_el1, {sctlr_el1_val}", |
| 340 | "isb", |
| 341 | "mov x1, xzr", |
| 342 | "mov x2, xzr", |
| 343 | "mov x3, xzr", |
| 344 | "mov x4, xzr", |
| 345 | "mov x5, xzr", |
| 346 | "mov x6, xzr", |
| 347 | "mov x7, xzr", |
| 348 | "mov x8, xzr", |
| 349 | "mov x9, xzr", |
| 350 | "mov x10, xzr", |
| 351 | "mov x11, xzr", |
| 352 | "mov x12, xzr", |
| 353 | "mov x13, xzr", |
| 354 | "mov x14, xzr", |
| 355 | "mov x15, xzr", |
| 356 | "mov x16, xzr", |
| 357 | "mov x17, xzr", |
| 358 | "mov x18, xzr", |
| 359 | "mov x19, xzr", |
| 360 | "mov x20, xzr", |
| 361 | "mov x21, xzr", |
| 362 | "mov x22, xzr", |
| 363 | "mov x23, xzr", |
| 364 | "mov x24, xzr", |
| 365 | "mov x25, xzr", |
| 366 | "mov x26, xzr", |
| 367 | "mov x27, xzr", |
| 368 | "mov x28, xzr", |
| 369 | "mov x29, xzr", |
| 370 | "msr ttbr0_el1, xzr", |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 371 | // Ensure that CMOs have completed before entering payload. |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 372 | "dsb nsh", |
| 373 | "br x30", |
| 374 | sctlr_el1_val = in(reg) SCTLR_EL1_VAL, |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 375 | bcc = in(reg) u64::try_from(bcc.start).unwrap(), |
| 376 | bcc_end = in(reg) u64::try_from(bcc.end).unwrap(), |
| 377 | cache_line = in(reg) u64::try_from(scratch.start).unwrap(), |
| 378 | scratch = in(reg) u64::try_from(scratch.start).unwrap(), |
| 379 | scratch_end = in(reg) u64::try_from(scratch.end).unwrap(), |
| 380 | stack = in(reg) u64::try_from(stack.start).unwrap(), |
| 381 | stack_end = in(reg) u64::try_from(stack.end).unwrap(), |
Alice Wang | 3fa9b80 | 2023-06-06 07:52:31 +0000 | [diff] [blame] | 382 | dcache_line_size = in(reg) u64::try_from(min_dcache_line_size()).unwrap(), |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 383 | in("x0") fdt_address, |
| 384 | in("x30") payload_start, |
Pierre-Clément Tosi | 97f5249 | 2023-04-04 15:52:17 +0100 | [diff] [blame] | 385 | options(noreturn), |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 386 | ); |
| 387 | }; |
| 388 | } |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 389 | |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 390 | /// # Safety |
| 391 | /// |
| 392 | /// This must only be called once, since we are returning a mutable reference. |
| 393 | /// The appended data region must be mapped. |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 394 | unsafe fn get_appended_data_slice() -> &'static mut [u8] { |
Pierre-Clément Tosi | ad1fc75 | 2023-05-31 16:56:56 +0000 | [diff] [blame] | 395 | let range = memory::appended_payload_range(); |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 396 | // SAFETY: This region is mapped and the linker script prevents it from overlapping with other |
| 397 | // objects. |
Jakob Vukalovic | 44b1ce3 | 2023-04-17 19:10:10 +0100 | [diff] [blame] | 398 | unsafe { slice::from_raw_parts_mut(range.start as *mut u8, range.len()) } |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Pierre-Clément Tosi | 7aca7ff | 2022-12-12 14:04:30 +0000 | [diff] [blame] | 401 | enum AppendedConfigType { |
| 402 | Valid, |
| 403 | Invalid, |
| 404 | NotFound, |
| 405 | } |
| 406 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 407 | enum AppendedPayload<'a> { |
| 408 | /// Configuration data. |
| 409 | Config(config::Config<'a>), |
| 410 | /// Deprecated raw BCC, as used in Android T. |
| 411 | LegacyBcc(&'a mut [u8]), |
| 412 | } |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 413 | |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 414 | impl<'a> AppendedPayload<'a> { |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 415 | fn new(data: &'a mut [u8]) -> Option<Self> { |
| 416 | match Self::guess_config_type(data) { |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 417 | AppendedConfigType::Valid => { |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 418 | let config = config::Config::new(data); |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 419 | Some(Self::Config(config.unwrap())) |
| 420 | } |
Pierre-Clément Tosi | 7aca7ff | 2022-12-12 14:04:30 +0000 | [diff] [blame] | 421 | AppendedConfigType::NotFound if cfg!(feature = "legacy") => { |
Alice Wang | eacb738 | 2023-06-05 12:53:54 +0000 | [diff] [blame] | 422 | const BCC_SIZE: usize = SIZE_4KB; |
Pierre-Clément Tosi | 7aca7ff | 2022-12-12 14:04:30 +0000 | [diff] [blame] | 423 | warn!("Assuming the appended data at {:?} to be a raw BCC", data.as_ptr()); |
| 424 | Some(Self::LegacyBcc(&mut data[..BCC_SIZE])) |
| 425 | } |
| 426 | _ => None, |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 430 | fn guess_config_type(data: &mut [u8]) -> AppendedConfigType { |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 431 | // This function is necessary to prevent the borrow checker from getting confused |
| 432 | // about the ownership of data in new(); see https://users.rust-lang.org/t/78467. |
| 433 | let addr = data.as_ptr(); |
Alan Stokes | a0e4296 | 2023-04-14 17:59:50 +0100 | [diff] [blame] | 434 | |
Alan Stokes | c3829f1 | 2023-06-02 15:02:23 +0100 | [diff] [blame] | 435 | match config::Config::new(data) { |
Pierre-Clément Tosi | 7aca7ff | 2022-12-12 14:04:30 +0000 | [diff] [blame] | 436 | Err(config::Error::InvalidMagic) => { |
| 437 | warn!("No configuration data found at {addr:?}"); |
| 438 | AppendedConfigType::NotFound |
| 439 | } |
| 440 | Err(e) => { |
| 441 | error!("Invalid configuration data at {addr:?}: {e}"); |
| 442 | AppendedConfigType::Invalid |
| 443 | } |
| 444 | Ok(_) => AppendedConfigType::Valid, |
| 445 | } |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Pierre-Clément Tosi | efe780c | 2023-02-21 21:36:30 +0000 | [diff] [blame] | 448 | fn get_entries(&mut self) -> (&mut [u8], Option<&mut [u8]>) { |
Pierre-Clément Tosi | 20b6096 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 449 | match self { |
Pierre-Clément Tosi | efe780c | 2023-02-21 21:36:30 +0000 | [diff] [blame] | 450 | Self::Config(ref mut cfg) => cfg.get_entries(), |
| 451 | Self::LegacyBcc(ref mut bcc) => (bcc, None), |
Pierre-Clément Tosi | 8edf72e | 2022-12-06 16:02:57 +0000 | [diff] [blame] | 452 | } |
Pierre-Clément Tosi | e8726e4 | 2022-10-17 13:35:27 +0100 | [diff] [blame] | 453 | } |
| 454 | } |