Skip to content

Commit

Permalink
Initialize the vTPM
Browse files Browse the repository at this point in the history
Initialize the Microsoft TPM as the default vTPM backend.

Signed-off-by: Claudio Carvalho <[email protected]>
  • Loading branch information
cclaudio committed Jan 31, 2024
1 parent 9c6c7e9 commit 564202b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ use svsm::svsm_paging::{init_page_table, invalidate_early_boot_memory};
use svsm::task::{create_task, 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};

Expand Down Expand Up @@ -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() {
Expand Down
16 changes: 15 additions & 1 deletion src/vtpm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -70,3 +71,16 @@ pub trait VtpmInterface: MsTpmSimulatorInterface {
/// the TPM is manufactured.
fn init(&mut self) -> Result<(), SvsmReqError>;
}

static VTPM: SpinLock<Vtpm> = 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(())
}

0 comments on commit 564202b

Please sign in to comment.