Skip to content

Commit

Permalink
Merge branch 'main' into enhancement/cell-deref
Browse files Browse the repository at this point in the history
  • Loading branch information
joergroedel authored Oct 30, 2024
2 parents f7ba8d2 + f49b375 commit cbb3dcd
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bootlib/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Author: Jon Lange ([email protected])

/// Defines the underlying platform type on which the SVSM will run.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(C)]
pub enum SvsmPlatformType {
Native = 0,
Expand Down
4 changes: 0 additions & 4 deletions kernel/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ fn main() {
println!("cargo:rustc-link-arg-bin=svsm=--no-relax");
println!("cargo:rustc-link-arg-bin=svsm=-Tkernel/src/svsm.lds");
println!("cargo:rustc-link-arg-bin=svsm=-no-pie");
if std::env::var("CARGO_FEATURE_MSTPM").is_ok() && std::env::var("CARGO_CFG_TEST").is_err() {
println!("cargo:rustc-link-arg-bin=svsm=-Llibmstpm");
println!("cargo:rustc-link-arg-bin=svsm=-lmstpm");
}

// Extra linker args for tests.
println!("cargo:rerun-if-env-changed=LINK_TEST");
Expand Down
26 changes: 26 additions & 0 deletions kernel/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::platform::native::NativePlatform;
use crate::platform::snp::SnpPlatform;
use crate::platform::tdp::TdpPlatform;
use crate::types::PageSize;
use crate::utils;
use crate::utils::immut_after_init::ImmutAfterInitCell;
use crate::utils::MemoryRegion;

Expand All @@ -25,6 +26,7 @@ pub mod native;
pub mod snp;
pub mod tdp;

static SVSM_PLATFORM_TYPE: ImmutAfterInitCell<SvsmPlatformType> = ImmutAfterInitCell::uninit();
pub static SVSM_PLATFORM: ImmutAfterInitCell<SvsmPlatformCell> = ImmutAfterInitCell::uninit();

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -52,6 +54,14 @@ pub enum PageValidateOp {
/// This defines a platform abstraction to permit the SVSM to run on different
/// underlying architectures.
pub trait SvsmPlatform {
/// Halts the system as required by the platform.
fn halt()
where
Self: Sized,
{
utils::halt();
}

/// Performs basic early initialization of the runtime environment.
fn env_setup(&mut self, debug_serial_port: u16, vtom: usize) -> Result<(), SvsmError>;

Expand Down Expand Up @@ -144,6 +154,7 @@ pub enum SvsmPlatformCell {

impl SvsmPlatformCell {
pub fn new(platform_type: SvsmPlatformType) -> Self {
assert_eq!(platform_type, *SVSM_PLATFORM_TYPE);
match platform_type {
SvsmPlatformType::Native => SvsmPlatformCell::Native(NativePlatform::new()),
SvsmPlatformType::Snp => SvsmPlatformCell::Snp(SnpPlatform::new()),
Expand Down Expand Up @@ -173,3 +184,18 @@ impl DerefMut for SvsmPlatformCell {
}
}
}

pub fn init_platform_type(platform_type: SvsmPlatformType) {
SVSM_PLATFORM_TYPE.init(&platform_type).unwrap();
}

pub fn halt() {
// Use a platform-specific halt. However, the SVSM_PLATFORM global may not
// yet be initialized, so go choose the halt implementation based on the
// platform-specific halt instead.
match *SVSM_PLATFORM_TYPE {
SvsmPlatformType::Native => NativePlatform::halt(),
SvsmPlatformType::Snp => SnpPlatform::halt(),
SvsmPlatformType::Tdp => TdpPlatform::halt(),
}
}
6 changes: 5 additions & 1 deletion kernel/src/platform/tdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::types::PageSize;
use crate::utils::immut_after_init::ImmutAfterInitCell;
use crate::utils::{zero_mem_region, MemoryRegion};
use tdx_tdcall::tdx::{
td_accept_memory, tdvmcall_io_read_16, tdvmcall_io_read_32, tdvmcall_io_read_8,
td_accept_memory, tdvmcall_halt, tdvmcall_io_read_16, tdvmcall_io_read_32, tdvmcall_io_read_8,
tdvmcall_io_write_16, tdvmcall_io_write_32, tdvmcall_io_write_8,
};

Expand All @@ -39,6 +39,10 @@ impl Default for TdpPlatform {
}

impl SvsmPlatform for TdpPlatform {
fn halt() {
tdvmcall_halt();
}

fn env_setup(&mut self, debug_serial_port: u16, vtom: usize) -> Result<(), SvsmError> {
VTOM.init(&vtom).map_err(|_| SvsmError::PlatformInit)?;
// Serial console device can be initialized immediately
Expand Down
11 changes: 8 additions & 3 deletions kernel/src/stage2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ use svsm::mm::validate::{
init_valid_bitmap_alloc, valid_bitmap_addr, valid_bitmap_set_valid_range,
};
use svsm::mm::{init_kernel_mapping_info, FixedAddressMappingRange, SVSM_PERCPU_BASE};
use svsm::platform::{PageStateChangeOp, PageValidateOp, SvsmPlatform, SvsmPlatformCell};
use svsm::platform;
use svsm::platform::{
init_platform_type, PageStateChangeOp, PageValidateOp, SvsmPlatform, SvsmPlatformCell,
};
use svsm::types::{PageSize, PAGE_SIZE, PAGE_SIZE_2M};
use svsm::utils::{halt, is_aligned, MemoryRegion};
use svsm::utils::{is_aligned, MemoryRegion};

extern "C" {
static mut pgtable: PageTable;
Expand Down Expand Up @@ -345,6 +348,8 @@ fn prepare_heap(
#[no_mangle]
pub extern "C" fn stage2_main(launch_info: &Stage2LaunchInfo) {
let platform_type = SvsmPlatformType::from(launch_info.platform_type);

init_platform_type(platform_type);
let mut platform = SvsmPlatformCell::new(platform_type);

let config =
Expand Down Expand Up @@ -465,6 +470,6 @@ pub extern "C" fn stage2_main(launch_info: &Stage2LaunchInfo) {
fn panic(info: &PanicInfo<'_>) -> ! {
log::error!("Panic: {}", info);
loop {
halt();
platform::halt();
}
}
9 changes: 6 additions & 3 deletions kernel/src/svsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ use svsm::mm::memory::{init_memory_map, write_guest_memory_map};
use svsm::mm::pagetable::paging_init;
use svsm::mm::virtualrange::virt_log_usage;
use svsm::mm::{init_kernel_mapping_info, FixedAddressMappingRange, PerCPUPageMappingGuard};
use svsm::platform::{SvsmPlatformCell, SVSM_PLATFORM};
use svsm::platform;
use svsm::platform::{init_platform_type, SvsmPlatformCell, SVSM_PLATFORM};
use svsm::requests::{request_loop, request_processing_main, update_mappings};
use svsm::sev::utils::{rmp_adjust, RMPFlags};
use svsm::sev::{secrets_page, secrets_page_mut};
use svsm::svsm_paging::{init_page_table, invalidate_early_boot_memory};
use svsm::task::exec_user;
use svsm::task::{create_kernel_task, schedule_init};
use svsm::types::{PageSize, GUEST_VMPL, PAGE_SIZE};
use svsm::utils::{halt, immut_after_init::ImmutAfterInitCell, zero_mem_region};
use svsm::utils::{immut_after_init::ImmutAfterInitCell, zero_mem_region};
#[cfg(all(feature = "mstpm", not(test)))]
use svsm::vtpm::vtpm_init;

Expand Down Expand Up @@ -276,6 +277,8 @@ fn init_cpuid_table(addr: VirtAddr) {
#[no_mangle]
pub extern "C" fn svsm_start(li: &KernelLaunchInfo, vb_addr: usize) {
let launch_info: KernelLaunchInfo = *li;
init_platform_type(launch_info.platform_type);

let vb_ptr = core::ptr::NonNull::new(VirtAddr::new(vb_addr).as_mut_ptr::<u64>()).unwrap();

mapping_info_init(&launch_info);
Expand Down Expand Up @@ -479,6 +482,6 @@ fn panic(info: &PanicInfo<'_>) -> ! {

loop {
debug_break();
halt();
platform::halt();
}
}
2 changes: 0 additions & 2 deletions libmstpm/.gitignore

This file was deleted.

15 changes: 6 additions & 9 deletions libmstpm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ MSTPM_MAKEFILE = $(MSTPM_DIR)/Makefile

LIBS = $(LIBCRT) $(LIBCRYPTO) $(LIBTPM) $(LIBPLATFORM)

all: libmstpm.a src/bindings.rs
OUT_DIR ?= $(CWD)

libmstpm.a: $(LIBS)
all: $(OUT_DIR)/libmstpm.a $(OUT_DIR)/bindings.rs

$(OUT_DIR)/libmstpm.a: $(LIBS)
rm -f $@
ar rcsTPD $@ $^

Expand Down Expand Up @@ -123,13 +125,8 @@ $(MSTPM_MAKEFILE):
BINDGEN_FLAGS = --use-core
CLANG_FLAGS = -Wno-incompatible-library-redeclaration

src/bindings.rs: deps/libmstpm.h $(LIBTPM)
echo "#![allow(non_upper_case_globals)]" > $@
echo "#![allow(non_camel_case_types)]" >> $@
echo "#![allow(non_snake_case)]" >> $@
echo "#![allow(unused)]" >> $@
echo "#![allow(improper_ctypes)]" >> $@
bindgen $(BINDGEN_FLAGS) deps/libmstpm.h -- $(CLANG_FLAGS) >> $@
$(OUT_DIR)/bindings.rs: deps/libmstpm.h $(LIBTPM)
bindgen $(BINDGEN_FLAGS) --output $@ deps/libmstpm.h -- $(CLANG_FLAGS)

clean: $(OPENSSL_MAKEFILE) $(MSTPM_MAKEFILE)
make -C $(LIBCRT_DIR) clean
Expand Down
20 changes: 15 additions & 5 deletions libmstpm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
//
// Authors: Claudio Carvalho <[email protected]>

use std::env::current_dir;
use std::process::Command;
use std::process::Stdio;

fn main() {
let output = Command::new("make")
// Build libmstpm.
let status = Command::new("make")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.status()
.unwrap();
assert!(status.success());

if !output.status.success() {
panic!();
}
// Tell cargo to link libmstpm and where to find it.
let out_dir = std::env::var("OUT_DIR").unwrap();
println!("cargo:rustc-link-search={out_dir}");
println!("cargo:rustc-link-lib=mstpm");

// Tell cargo not to rerun the build-script unless anything in this
// directory changes.
let cwd = current_dir().unwrap();
let cwd = cwd.as_os_str().to_str().unwrap();
println!("cargo:rerun-if-changed={cwd}");
}
10 changes: 9 additions & 1 deletion libmstpm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
#![no_std]

/// C bindings
pub mod bindings;
pub mod bindings {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]
#![allow(improper_ctypes)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}

0 comments on commit cbb3dcd

Please sign in to comment.