blob: c0ad8783de4989d30821f3332762b5e9c298f0b8 [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 Tosie8726e42022-10-17 13:35:27 +010022use log::debug;
23use log::error;
24use log::LevelFilter;
25use vmbase::{console, layout, logger, main, power::reboot};
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010026
27#[derive(Debug, Clone)]
28enum RebootReason {
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010029 /// A malformed BCC was received.
30 InvalidBcc,
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010031 /// An unexpected internal error happened.
32 InternalError,
33}
34
35main!(start);
36
37/// Entry point for pVM firmware.
38pub fn start(fdt_address: u64, payload_start: u64, payload_size: u64, _arg3: u64) {
39 // Limitations in this function:
40 // - can't access non-pvmfw memory (only statically-mapped memory)
41 // - can't access MMIO (therefore, no logging)
42
43 match main_wrapper(fdt_address as usize, payload_start as usize, payload_size as usize) {
44 Ok(_) => jump_to_payload(fdt_address, payload_start),
45 Err(_) => reboot(),
46 }
47
48 // if we reach this point and return, vmbase::entry::rust_entry() will call power::shutdown().
49}
50
51/// Sets up the environment for main() and wraps its result for start().
52///
53/// Provide the abstractions necessary for start() to abort the pVM boot and for main() to run with
54/// the assumption that its environment has been properly configured.
55fn main_wrapper(fdt: usize, payload: usize, payload_size: usize) -> Result<(), RebootReason> {
56 // Limitations in this function:
57 // - only access MMIO once (and while) it has been mapped and configured
58 // - only perform logging once the logger has been initialized
59 // - only access non-pvmfw memory once (and while) it has been mapped
Pierre-Clément Tosifc531152022-10-20 12:22:23 +010060
61 // SAFETY - This function should and will only be called once, here.
62 unsafe { heap::init() };
63
Pierre-Clément Tosidbd72862022-10-21 14:31:02 +010064 logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?;
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010065
Pierre-Clément Tosia1d3ea32022-11-01 15:05:11 +000066 const FDT_MAX_SIZE: usize = helpers::SIZE_2MB;
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010067 // TODO: Check that the FDT is fully contained in RAM.
68 // SAFETY - We trust the VMM, for now.
69 let fdt = unsafe { slice::from_raw_parts_mut(fdt as *mut u8, FDT_MAX_SIZE) };
70 // TODO: Check that the payload is fully contained in RAM and doesn't overlap with the FDT.
71 // SAFETY - We trust the VMM, for now.
72 let payload = unsafe { slice::from_raw_parts(payload as *const u8, payload_size) };
73
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010074 // Use debug!() to avoid printing to the UART if we failed to configure it as only local
75 // builds that have tweaked the logger::init() call will actually attempt to log the message.
76
77 mmio_guard::init().map_err(|e| {
78 debug!("{e}");
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +010079 RebootReason::InternalError
80 })?;
81
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010082 mmio_guard::map(console::BASE_ADDRESS).map_err(|e| {
83 debug!("Failed to configure the UART: {e}");
84 RebootReason::InternalError
85 })?;
86
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010087 // SAFETY - We only get the appended payload from here, once. It is mapped and the linker
88 // script prevents it from overlapping with other objects.
89 let bcc = as_bcc(unsafe { get_appended_data_slice() }).ok_or_else(|| {
90 error!("Invalid BCC");
91 RebootReason::InvalidBcc
92 })?;
93
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010094 // This wrapper allows main() to be blissfully ignorant of platform details.
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +010095 crate::main(fdt, payload, bcc);
96
97 // TODO: Overwrite BCC before jumping to payload to avoid leaking our sealing key.
Pierre-Clément Tosi072969b2022-10-19 17:32:24 +010098
Pierre-Clément Tosia99bfa62022-10-06 13:30:52 +010099 mmio_guard::unmap(console::BASE_ADDRESS).map_err(|e| {
100 error!("Failed to unshare the UART: {e}");
101 RebootReason::InternalError
102 })?;
103
Pierre-Clément Tosi5bbfca52022-10-21 12:14:35 +0100104 Ok(())
105}
Pierre-Clément Tosi645e90e2022-10-21 13:27:19 +0100106
107fn jump_to_payload(fdt_address: u64, payload_start: u64) -> ! {
108 const SCTLR_EL1_RES1: usize = (0b11 << 28) | (0b101 << 20) | (0b1 << 11);
109 // Stage 1 instruction access cacheability is unaffected.
110 const SCTLR_EL1_I: usize = 0b1 << 12;
111 // SETEND instruction disabled at EL0 in aarch32 mode.
112 const SCTLR_EL1_SED: usize = 0b1 << 8;
113 // Various IT instructions are disabled at EL0 in aarch32 mode.
114 const SCTLR_EL1_ITD: usize = 0b1 << 7;
115
116 const SCTLR_EL1_VAL: usize = SCTLR_EL1_RES1 | SCTLR_EL1_ITD | SCTLR_EL1_SED | SCTLR_EL1_I;
117
118 // SAFETY - We're exiting pvmfw by passing the register values we need to a noreturn asm!().
119 unsafe {
120 asm!(
121 "msr sctlr_el1, {sctlr_el1_val}",
122 "isb",
123 "mov x1, xzr",
124 "mov x2, xzr",
125 "mov x3, xzr",
126 "mov x4, xzr",
127 "mov x5, xzr",
128 "mov x6, xzr",
129 "mov x7, xzr",
130 "mov x8, xzr",
131 "mov x9, xzr",
132 "mov x10, xzr",
133 "mov x11, xzr",
134 "mov x12, xzr",
135 "mov x13, xzr",
136 "mov x14, xzr",
137 "mov x15, xzr",
138 "mov x16, xzr",
139 "mov x17, xzr",
140 "mov x18, xzr",
141 "mov x19, xzr",
142 "mov x20, xzr",
143 "mov x21, xzr",
144 "mov x22, xzr",
145 "mov x23, xzr",
146 "mov x24, xzr",
147 "mov x25, xzr",
148 "mov x26, xzr",
149 "mov x27, xzr",
150 "mov x28, xzr",
151 "mov x29, xzr",
152 "msr ttbr0_el1, xzr",
153 "isb",
154 "dsb nsh",
155 "br x30",
156 sctlr_el1_val = in(reg) SCTLR_EL1_VAL,
157 in("x0") fdt_address,
158 in("x30") payload_start,
159 options(nomem, noreturn, nostack),
160 );
161 };
162}
Pierre-Clément Tosie8726e42022-10-17 13:35:27 +0100163
164unsafe fn get_appended_data_slice() -> &'static mut [u8] {
165 let base = helpers::align_up(layout::binary_end(), helpers::SIZE_4KB).unwrap();
166 // pvmfw is contained in a 2MiB region so the payload can't be larger than the 2MiB alignment.
167 let size = helpers::align_up(base, helpers::SIZE_2MB).unwrap() - base;
168
169 slice::from_raw_parts_mut(base as *mut u8, size)
170}
171
172fn as_bcc(data: &mut [u8]) -> Option<&mut [u8]> {
173 const BCC_SIZE: usize = helpers::SIZE_4KB;
174
175 if cfg!(feature = "legacy") {
176 // TODO(b/256148034): return None if BccHandoverParse(bcc) != kDiceResultOk.
177 Some(&mut data[..BCC_SIZE])
178 } else {
179 None
180 }
181}