blob: 18721c5c27a9a5ab94f8cee286c51c7c54630eec [file] [log] [blame]
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +01001// 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
17use crate::helpers::FDT_MAX_SIZE;
18use crate::jump_to_payload;
19use core::slice;
20use log::{error, LevelFilter};
21use vmbase::{logger, main, power::reboot};
22
23#[derive(Debug, Clone)]
24enum RebootReason {
25 /// An unexpected internal error happened.
26 InternalError,
27}
28
29main!(start);
30
31/// Entry point for pVM firmware.
32pub 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.
49fn 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 Tosidbd72862022-10-21 14:31:02 +010054 logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?;
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010055
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}