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 | |
| 17 | use crate::helpers::FDT_MAX_SIZE; |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 18 | use crate::mmio_guard; |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 19 | use core::arch::asm; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 20 | use core::slice; |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 21 | use log::{debug, error, LevelFilter}; |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 22 | use vmbase::{console, logger, main, power::reboot}; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 23 | |
| 24 | #[derive(Debug, Clone)] |
| 25 | enum RebootReason { |
| 26 | /// An unexpected internal error happened. |
| 27 | InternalError, |
| 28 | } |
| 29 | |
| 30 | main!(start); |
| 31 | |
| 32 | /// Entry point for pVM firmware. |
| 33 | pub fn start(fdt_address: u64, payload_start: u64, payload_size: u64, _arg3: u64) { |
| 34 | // Limitations in this function: |
| 35 | // - can't access non-pvmfw memory (only statically-mapped memory) |
| 36 | // - can't access MMIO (therefore, no logging) |
| 37 | |
| 38 | match main_wrapper(fdt_address as usize, payload_start as usize, payload_size as usize) { |
| 39 | Ok(_) => jump_to_payload(fdt_address, payload_start), |
| 40 | Err(_) => reboot(), |
| 41 | } |
| 42 | |
| 43 | // if we reach this point and return, vmbase::entry::rust_entry() will call power::shutdown(). |
| 44 | } |
| 45 | |
| 46 | /// Sets up the environment for main() and wraps its result for start(). |
| 47 | /// |
| 48 | /// Provide the abstractions necessary for start() to abort the pVM boot and for main() to run with |
| 49 | /// the assumption that its environment has been properly configured. |
| 50 | fn main_wrapper(fdt: usize, payload: usize, payload_size: usize) -> Result<(), RebootReason> { |
| 51 | // Limitations in this function: |
| 52 | // - only access MMIO once (and while) it has been mapped and configured |
| 53 | // - only perform logging once the logger has been initialized |
| 54 | // - only access non-pvmfw memory once (and while) it has been mapped |
Pierre-Clément Tosi | dbd7286 | 2022-10-21 14:31:02 +0100 | [diff] [blame] | 55 | logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 56 | |
| 57 | // TODO: Check that the FDT is fully contained in RAM. |
| 58 | // SAFETY - We trust the VMM, for now. |
| 59 | let fdt = unsafe { slice::from_raw_parts_mut(fdt as *mut u8, FDT_MAX_SIZE) }; |
| 60 | // TODO: Check that the payload is fully contained in RAM and doesn't overlap with the FDT. |
| 61 | // SAFETY - We trust the VMM, for now. |
| 62 | let payload = unsafe { slice::from_raw_parts(payload as *const u8, payload_size) }; |
| 63 | |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 64 | // Use debug!() to avoid printing to the UART if we failed to configure it as only local |
| 65 | // builds that have tweaked the logger::init() call will actually attempt to log the message. |
| 66 | |
| 67 | mmio_guard::init().map_err(|e| { |
| 68 | debug!("{e}"); |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 69 | RebootReason::InternalError |
| 70 | })?; |
| 71 | |
Pierre-Clément Tosi | 072969b | 2022-10-19 17:32:24 +0100 | [diff] [blame] | 72 | mmio_guard::map(console::BASE_ADDRESS).map_err(|e| { |
| 73 | debug!("Failed to configure the UART: {e}"); |
| 74 | RebootReason::InternalError |
| 75 | })?; |
| 76 | |
| 77 | // This wrapper allows main() to be blissfully ignorant of platform details. |
| 78 | crate::main(fdt, payload); |
| 79 | |
Pierre-Clément Tosi | a99bfa6 | 2022-10-06 13:30:52 +0100 | [diff] [blame] | 80 | mmio_guard::unmap(console::BASE_ADDRESS).map_err(|e| { |
| 81 | error!("Failed to unshare the UART: {e}"); |
| 82 | RebootReason::InternalError |
| 83 | })?; |
| 84 | |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 85 | Ok(()) |
| 86 | } |
Pierre-Clément Tosi | 645e90e | 2022-10-21 13:27:19 +0100 | [diff] [blame] | 87 | |
| 88 | fn jump_to_payload(fdt_address: u64, payload_start: u64) -> ! { |
| 89 | const SCTLR_EL1_RES1: usize = (0b11 << 28) | (0b101 << 20) | (0b1 << 11); |
| 90 | // Stage 1 instruction access cacheability is unaffected. |
| 91 | const SCTLR_EL1_I: usize = 0b1 << 12; |
| 92 | // SETEND instruction disabled at EL0 in aarch32 mode. |
| 93 | const SCTLR_EL1_SED: usize = 0b1 << 8; |
| 94 | // Various IT instructions are disabled at EL0 in aarch32 mode. |
| 95 | const SCTLR_EL1_ITD: usize = 0b1 << 7; |
| 96 | |
| 97 | const SCTLR_EL1_VAL: usize = SCTLR_EL1_RES1 | SCTLR_EL1_ITD | SCTLR_EL1_SED | SCTLR_EL1_I; |
| 98 | |
| 99 | // SAFETY - We're exiting pvmfw by passing the register values we need to a noreturn asm!(). |
| 100 | unsafe { |
| 101 | asm!( |
| 102 | "msr sctlr_el1, {sctlr_el1_val}", |
| 103 | "isb", |
| 104 | "mov x1, xzr", |
| 105 | "mov x2, xzr", |
| 106 | "mov x3, xzr", |
| 107 | "mov x4, xzr", |
| 108 | "mov x5, xzr", |
| 109 | "mov x6, xzr", |
| 110 | "mov x7, xzr", |
| 111 | "mov x8, xzr", |
| 112 | "mov x9, xzr", |
| 113 | "mov x10, xzr", |
| 114 | "mov x11, xzr", |
| 115 | "mov x12, xzr", |
| 116 | "mov x13, xzr", |
| 117 | "mov x14, xzr", |
| 118 | "mov x15, xzr", |
| 119 | "mov x16, xzr", |
| 120 | "mov x17, xzr", |
| 121 | "mov x18, xzr", |
| 122 | "mov x19, xzr", |
| 123 | "mov x20, xzr", |
| 124 | "mov x21, xzr", |
| 125 | "mov x22, xzr", |
| 126 | "mov x23, xzr", |
| 127 | "mov x24, xzr", |
| 128 | "mov x25, xzr", |
| 129 | "mov x26, xzr", |
| 130 | "mov x27, xzr", |
| 131 | "mov x28, xzr", |
| 132 | "mov x29, xzr", |
| 133 | "msr ttbr0_el1, xzr", |
| 134 | "isb", |
| 135 | "dsb nsh", |
| 136 | "br x30", |
| 137 | sctlr_el1_val = in(reg) SCTLR_EL1_VAL, |
| 138 | in("x0") fdt_address, |
| 139 | in("x30") payload_start, |
| 140 | options(nomem, noreturn, nostack), |
| 141 | ); |
| 142 | }; |
| 143 | } |