pvmfw: Refactor MMIO_GUARD initialization
Refactor the MMIO_GUARD setup into its own module, add a clearer error
type allowing us to identify the faulting HVC in main_wrapper (e.g. for
logs - if they get through - or to map them to a future SYSTEM_RESET2
reboot reason) and let main() assume that logging is working.
This removes the only reason for main() to fail so temporarily remove
the Error type.
Test: atest MicrodroidTestApp
Change-Id: I3dba728fee2c21e24195005e39593305aa3650f6
diff --git a/pvmfw/src/entry.rs b/pvmfw/src/entry.rs
index 18721c5..95dc0b0 100644
--- a/pvmfw/src/entry.rs
+++ b/pvmfw/src/entry.rs
@@ -16,9 +16,10 @@
use crate::helpers::FDT_MAX_SIZE;
use crate::jump_to_payload;
+use crate::mmio_guard;
use core::slice;
-use log::{error, LevelFilter};
-use vmbase::{logger, main, power::reboot};
+use log::{debug, LevelFilter};
+use vmbase::{console, logger, main, power::reboot};
#[derive(Debug, Clone)]
enum RebootReason {
@@ -60,11 +61,21 @@
// SAFETY - We trust the VMM, for now.
let payload = unsafe { slice::from_raw_parts(payload as *const u8, payload_size) };
- // This wrapper allows main() to be blissfully ignorant of platform details.
- crate::main(fdt, payload).map_err(|e| {
- error!("{e}");
+ // Use debug!() to avoid printing to the UART if we failed to configure it as only local
+ // builds that have tweaked the logger::init() call will actually attempt to log the message.
+
+ mmio_guard::init().map_err(|e| {
+ debug!("{e}");
RebootReason::InternalError
})?;
+ mmio_guard::map(console::BASE_ADDRESS).map_err(|e| {
+ debug!("Failed to configure the UART: {e}");
+ RebootReason::InternalError
+ })?;
+
+ // This wrapper allows main() to be blissfully ignorant of platform details.
+ crate::main(fdt, payload);
+
Ok(())
}
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index d8fb3ed..cade62e 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -22,15 +22,6 @@
addr & !(page_size - 1)
}
-/// Validates a page size and computes the address of the page containing a given address.
-pub const fn checked_page_of(addr: usize, page_size: usize) -> Option<usize> {
- if page_size.is_power_of_two() {
- Some(page_of(addr, page_size))
- } else {
- None
- }
-}
-
/// Computes the address of the 4KiB page containing a given address.
pub const fn page_4kb_of(addr: usize) -> usize {
page_of(addr, SIZE_4KB)
diff --git a/pvmfw/src/main.rs b/pvmfw/src/main.rs
index 1fadf22..d1951b3 100644
--- a/pvmfw/src/main.rs
+++ b/pvmfw/src/main.rs
@@ -20,35 +20,12 @@
mod entry;
mod exceptions;
mod helpers;
+mod mmio_guard;
mod smccc;
-use core::fmt;
-use helpers::checked_page_of;
use log::{debug, info};
-use vmbase::console;
-#[derive(Debug, Clone)]
-enum Error {
- /// Failed to configure the UART; no logs available.
- FailedUartSetup,
-}
-
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let msg = match self {
- Self::FailedUartSetup => "Failed to configure the UART",
- };
- write!(f, "{}", msg)
- }
-}
-
-fn main(fdt: &mut [u8], payload: &[u8]) -> Result<(), Error> {
- // We need to inform the hypervisor that the MMIO page containing the UART may be shared back.
- let mmio_granule = smccc::mmio_guard_info().map_err(|_| Error::FailedUartSetup)?;
- let uart_page = checked_page_of(console::BASE_ADDRESS, mmio_granule as usize)
- .ok_or(Error::FailedUartSetup)?;
- smccc::mmio_guard_map(uart_page as u64).map_err(|_| Error::FailedUartSetup)?;
-
+fn main(fdt: &mut [u8], payload: &[u8]) {
info!("pVM firmware");
debug!(
"fdt_address={:#018x}, payload_start={:#018x}, payload_size={:#018x}",
@@ -58,8 +35,6 @@
);
info!("Starting payload...");
-
- Ok(())
}
fn jump_to_payload(fdt_address: u64, payload_start: u64) {
diff --git a/pvmfw/src/mmio_guard.rs b/pvmfw/src/mmio_guard.rs
new file mode 100644
index 0000000..4cde737
--- /dev/null
+++ b/pvmfw/src/mmio_guard.rs
@@ -0,0 +1,53 @@
+// Copyright 2022, The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Safe MMIO_GUARD support.
+
+use crate::helpers;
+use crate::smccc;
+use core::{fmt, result};
+
+#[derive(Debug, Clone)]
+pub enum Error {
+ /// Failed to obtain the MMIO_GUARD granule size.
+ InfoFailed(smccc::Error),
+ /// Failed to MMIO_GUARD_MAP a page.
+ MapFailed(smccc::Error),
+ /// The MMIO_GUARD granule used by the hypervisor is not supported.
+ UnsupportedGranule(usize),
+}
+
+type Result<T> = result::Result<T, Error>;
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self {
+ Self::InfoFailed(e) => write!(f, "Failed to get the MMIO_GUARD granule: {e}"),
+ Self::MapFailed(e) => write!(f, "Failed to MMIO_GUARD map: {e}"),
+ Self::UnsupportedGranule(g) => write!(f, "Unsupported MMIO_GUARD granule: {g}"),
+ }
+ }
+}
+
+pub fn init() -> Result<()> {
+ let mmio_granule = smccc::mmio_guard_info().map_err(Error::InfoFailed)? as usize;
+ if mmio_granule != helpers::SIZE_4KB {
+ return Err(Error::UnsupportedGranule(mmio_granule));
+ }
+ Ok(())
+}
+
+pub fn map(addr: usize) -> Result<()> {
+ smccc::mmio_guard_map(helpers::page_4kb_of(addr) as u64).map_err(Error::MapFailed)
+}