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; |
| 18 | use crate::jump_to_payload; |
| 19 | use core::slice; |
| 20 | use log::{error, LevelFilter}; |
| 21 | use vmbase::{logger, main, power::reboot}; |
| 22 | |
| 23 | #[derive(Debug, Clone)] |
| 24 | enum RebootReason { |
| 25 | /// An unexpected internal error happened. |
| 26 | InternalError, |
| 27 | } |
| 28 | |
| 29 | main!(start); |
| 30 | |
| 31 | /// Entry point for pVM firmware. |
| 32 | pub fn start(fdt_address: u64, payload_start: u64, payload_size: u64, _arg3: u64) { |
| 33 | // Limitations in this function: |
| 34 | // - can't access non-pvmfw memory (only statically-mapped memory) |
| 35 | // - can't access MMIO (therefore, no logging) |
| 36 | |
| 37 | match main_wrapper(fdt_address as usize, payload_start as usize, payload_size as usize) { |
| 38 | Ok(_) => jump_to_payload(fdt_address, payload_start), |
| 39 | Err(_) => reboot(), |
| 40 | } |
| 41 | |
| 42 | // if we reach this point and return, vmbase::entry::rust_entry() will call power::shutdown(). |
| 43 | } |
| 44 | |
| 45 | /// Sets up the environment for main() and wraps its result for start(). |
| 46 | /// |
| 47 | /// Provide the abstractions necessary for start() to abort the pVM boot and for main() to run with |
| 48 | /// the assumption that its environment has been properly configured. |
| 49 | fn main_wrapper(fdt: usize, payload: usize, payload_size: usize) -> Result<(), RebootReason> { |
| 50 | // Limitations in this function: |
| 51 | // - only access MMIO once (and while) it has been mapped and configured |
| 52 | // - only perform logging once the logger has been initialized |
| 53 | // - 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^] | 54 | logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?; |
Pierre-Clément Tosi | 5bbfca5 | 2022-10-21 12:14:35 +0100 | [diff] [blame] | 55 | |
| 56 | // TODO: Check that the FDT is fully contained in RAM. |
| 57 | // SAFETY - We trust the VMM, for now. |
| 58 | let fdt = unsafe { slice::from_raw_parts_mut(fdt as *mut u8, FDT_MAX_SIZE) }; |
| 59 | // TODO: Check that the payload is fully contained in RAM and doesn't overlap with the FDT. |
| 60 | // SAFETY - We trust the VMM, for now. |
| 61 | let payload = unsafe { slice::from_raw_parts(payload as *const u8, payload_size) }; |
| 62 | |
| 63 | // This wrapper allows main() to be blissfully ignorant of platform details. |
| 64 | crate::main(fdt, payload).map_err(|e| { |
| 65 | error!("{e}"); |
| 66 | RebootReason::InternalError |
| 67 | })?; |
| 68 | |
| 69 | Ok(()) |
| 70 | } |