From 430ba19be9e5583af19664d345244e9c4a7e6be2 Mon Sep 17 00:00:00 2001 From: Claudio Carvalho Date: Wed, 31 Jan 2024 05:45:58 +0200 Subject: [PATCH] Initialize the vTPM Initialize the Microsoft TPM as the default vTPM backend. Signed-off-by: Claudio Carvalho --- src/svsm.rs | 7 +++++++ src/vtpm/mod.rs | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/svsm.rs b/src/svsm.rs index b983d53cd7..7fc17ab35a 100755 --- a/src/svsm.rs +++ b/src/svsm.rs @@ -53,6 +53,8 @@ use svsm::svsm_paging::{init_page_table, invalidate_early_boot_memory}; use svsm::task::{create_kernel_task, schedule_init, TASK_FLAG_SHARE_PT}; use svsm::types::{PageSize, GUEST_VMPL, PAGE_SIZE}; use svsm::utils::{halt, immut_after_init::ImmutAfterInitCell, zero_mem_region}; +#[cfg(feature = "vtpm")] +use svsm::vtpm::vtpm_init; use svsm::mm::validate::{init_valid_bitmap_ptr, migrate_valid_bitmap}; @@ -442,6 +444,11 @@ pub extern "C" fn svsm_main() { prepare_fw_launch(fw_meta).expect("Failed to setup guest VMSA/CAA"); } + #[cfg(feature = "vtpm")] + if let Err(e) = vtpm_init() { + panic!("vTPM failed to initialize - {:?}", e); + } + virt_log_usage(); if config.should_launch_fw() { diff --git a/src/vtpm/mod.rs b/src/vtpm/mod.rs index 881f5641fb..d17f7db24f 100644 --- a/src/vtpm/mod.rs +++ b/src/vtpm/mod.rs @@ -15,7 +15,8 @@ use core::{ }; use crate::protocols::vtpm::TpmPlatformCommand; -use crate::protocols::errors::SvsmReqError; +use crate::vtpm::mstpm::MsTpm as Vtpm; +use crate::{locking::SpinLock, protocols::errors::SvsmReqError}; /// Basic services required to perform the VTPM Protocol pub trait VtpmProtocolInterface { @@ -70,3 +71,16 @@ pub trait VtpmInterface: MsTpmSimulatorInterface { /// the TPM is manufactured. fn init(&mut self) -> Result<(), SvsmReqError>; } + +static VTPM: SpinLock = SpinLock::new(Vtpm::new()); + +/// Initialize the TPM by calling the init() implementation of the +/// [`VtpmInterface`] +pub fn vtpm_init() -> Result<(), SvsmReqError> { + let mut vtpm = VTPM.lock(); + if vtpm.is_powered_on() { + return Ok(()); + } + vtpm.init()?; + Ok(()) +}