From 0c21f811bd7e1ccc6aacd044c65a8e5526b805ff Mon Sep 17 00:00:00 2001 From: Alice Wang Date: Wed, 12 Apr 2023 12:22:46 +0000 Subject: [PATCH] [pvmfw] Extract smccc into a separate library for reuse Test: m pvmfw_img Bug: 272226230 Change-Id: Ie6e03e92acca8f0947febefa80587c0db0010c49 --- libs/smccc/Android.bp | 18 ++++++++++++++++++ libs/smccc/src/lib.rs | 22 ++++++++++++++++++++++ {pvmfw => libs/smccc}/src/smccc.rs | 12 +++++++++++- pvmfw/Android.bp | 2 +- pvmfw/src/hvc.rs | 2 +- pvmfw/src/hvc/trng.rs | 1 - pvmfw/src/hypervisor.rs | 1 - pvmfw/src/main.rs | 1 - pvmfw/src/memory.rs | 1 - pvmfw/src/mmio_guard.rs | 1 - 10 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 libs/smccc/Android.bp create mode 100644 libs/smccc/src/lib.rs rename {pvmfw => libs/smccc}/src/smccc.rs (78%) diff --git a/libs/smccc/Android.bp b/libs/smccc/Android.bp new file mode 100644 index 00000000..96943d8a --- /dev/null +++ b/libs/smccc/Android.bp @@ -0,0 +1,18 @@ +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +rust_library_rlib { + name: "libsmccc", + crate_name: "smccc", + srcs: ["src/lib.rs"], + prefer_rlib: true, + rustlibs: [ + "libpsci", + ], + no_stdlibs: true, + stdlibs: [ + "libcore.rust_sysroot", + ], + apex_available: ["com.android.virt"], +} diff --git a/libs/smccc/src/lib.rs b/libs/smccc/src/lib.rs new file mode 100644 index 00000000..2cd31dc6 --- /dev/null +++ b/libs/smccc/src/lib.rs @@ -0,0 +1,22 @@ +// Copyright 2023, 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. + +//! Structs and functions for making SMCCC calls following the SMC Calling +//! Convention version 1.4. + +#![no_std] + +mod smccc; + +pub use smccc::{checked_hvc64, checked_hvc64_expect_zero, hvc64, Error, Result}; diff --git a/pvmfw/src/smccc.rs b/libs/smccc/src/smccc.rs similarity index 78% rename from pvmfw/src/smccc.rs rename to libs/smccc/src/smccc.rs index 16f5a625..c0070e05 100644 --- a/pvmfw/src/smccc.rs +++ b/libs/smccc/src/smccc.rs @@ -12,8 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Structs and functions for making SMCCC calls. + use core::{fmt, result}; -use psci::smccc::hvc64; +// Ideally, smccc shouldn't depend on psci. Smccc isn't split as a separate +// upstream crate currently mostly for maintenance consideration. +// See b/245889995 for more context. +pub use psci::smccc::hvc64; /// Standard SMCCC error values as described in DEN 0028E. #[derive(Debug, Clone)] @@ -42,8 +47,11 @@ impl fmt::Display for Error { } } +/// Result type with smccc::Error. pub type Result = result::Result; +/// Makes a checked HVC64 call to the hypervisor, following the SMC Calling Convention version 1.4. +/// Returns Ok only when the return code is 0. pub fn checked_hvc64_expect_zero(function: u32, args: [u64; 17]) -> Result<()> { match checked_hvc64(function, args)? { 0 => Ok(()), @@ -51,6 +59,8 @@ pub fn checked_hvc64_expect_zero(function: u32, args: [u64; 17]) -> Result<()> { } } +/// Makes a checked HVC64 call to the hypervisor, following the SMC Calling Convention version 1.4. +/// Returns Ok with the return code only when the return code >= 0. pub fn checked_hvc64(function: u32, args: [u64; 17]) -> Result { match hvc64(function, args)[0] as i64 { ret if ret >= 0 => Ok(ret as u64), diff --git a/pvmfw/Android.bp b/pvmfw/Android.bp index 6d5226ac..a446c901 100644 --- a/pvmfw/Android.bp +++ b/pvmfw/Android.bp @@ -20,10 +20,10 @@ rust_ffi_static { "liblibfdt", "liblog_rust_nostd", "libonce_cell_nostd", - "libpsci", "libpvmfw_avb_nostd", "libpvmfw_embedded_key", "libpvmfw_fdt_template", + "libsmccc", "libstatic_assertions", "libtinyvec_nostd", "libuuid_nostd", diff --git a/pvmfw/src/hvc.rs b/pvmfw/src/hvc.rs index b5b09091..f44e26e8 100644 --- a/pvmfw/src/hvc.rs +++ b/pvmfw/src/hvc.rs @@ -16,8 +16,8 @@ pub mod trng; -use crate::smccc::{self, checked_hvc64, checked_hvc64_expect_zero}; use log::info; +use smccc::{self, checked_hvc64, checked_hvc64_expect_zero}; const ARM_SMCCC_TRNG_VERSION: u32 = 0x8400_0050; #[allow(dead_code)] diff --git a/pvmfw/src/hvc/trng.rs b/pvmfw/src/hvc/trng.rs index 53d58813..05ecc6b1 100644 --- a/pvmfw/src/hvc/trng.rs +++ b/pvmfw/src/hvc/trng.rs @@ -14,7 +14,6 @@ use core::fmt; use core::result; -use psci::smccc; /// Standard SMCCC TRNG error values as described in DEN 0098 1.0 REL0. #[derive(Debug, Clone)] diff --git a/pvmfw/src/hypervisor.rs b/pvmfw/src/hypervisor.rs index e06d809e..22609c5d 100644 --- a/pvmfw/src/hypervisor.rs +++ b/pvmfw/src/hypervisor.rs @@ -15,7 +15,6 @@ //! Wrappers around hypervisor back-ends. use crate::hvc; -use crate::smccc; pub fn hyp_meminfo() -> smccc::Result { hvc::kvm_hyp_meminfo() diff --git a/pvmfw/src/main.rs b/pvmfw/src/main.rs index 06cc81e7..fd6054fb 100644 --- a/pvmfw/src/main.rs +++ b/pvmfw/src/main.rs @@ -35,7 +35,6 @@ mod memory; mod mmio_guard; mod mmu; mod rand; -mod smccc; mod virtio; use alloc::boxed::Box; diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs index b223f82e..d26a4ba9 100644 --- a/pvmfw/src/memory.rs +++ b/pvmfw/src/memory.rs @@ -20,7 +20,6 @@ use crate::helpers::{self, align_down, align_up, page_4kb_of, SIZE_4KB}; use crate::hypervisor::{hyp_meminfo, mem_share, mem_unshare}; use crate::mmio_guard; use crate::mmu; -use crate::smccc; use alloc::alloc::alloc_zeroed; use alloc::alloc::dealloc; use alloc::alloc::handle_alloc_error; diff --git a/pvmfw/src/mmio_guard.rs b/pvmfw/src/mmio_guard.rs index dac26e0f..95a1b7f6 100644 --- a/pvmfw/src/mmio_guard.rs +++ b/pvmfw/src/mmio_guard.rs @@ -16,7 +16,6 @@ use crate::helpers; use crate::hypervisor::{mmio_guard_enroll, mmio_guard_info, mmio_guard_map, mmio_guard_unmap}; -use crate::smccc; use core::{fmt, result}; #[derive(Debug, Clone)]