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