Add TDX support for OSDK test

This commit is contained in:
Hsy-Intel
2024-07-10 16:52:13 +08:00
committed by Tate, Hongliang Tian
parent d6925e7c93
commit 3a6768ecb2
7 changed files with 101 additions and 14 deletions

View File

@ -60,5 +60,5 @@ fn cli_new_crate_with_hyphen() {
.unwrap(); .unwrap();
assert_success(&output); assert_success(&output);
assert!(fs::metadata("my-first-os").is_ok()); assert!(fs::metadata("my-first-os").is_ok());
fs::remove_dir_all("my-first-os"); let _ = fs::remove_dir_all("my-first-os");
} }

View File

@ -2,6 +2,8 @@
//! Test the `run` command //! Test the `run` command
use crate::util::is_tdx_enabled;
const WORKSPACE: &str = "/tmp/kernel_test_workspace/run_command"; const WORKSPACE: &str = "/tmp/kernel_test_workspace/run_command";
mod workspace { mod workspace {
@ -67,6 +69,10 @@ mod qemu_gdb_feature {
#[test] #[test]
fn basic_debug() { fn basic_debug() {
// Test skipped because TDX is enabled.
if is_tdx_enabled() {
return;
}
let workspace = workspace::WorkSpace::new(WORKSPACE, "basic_debug"); let workspace = workspace::WorkSpace::new(WORKSPACE, "basic_debug");
let unix_socket = { let unix_socket = {
let path = Path::new(&workspace.os_dir()).join("qemu-gdb-sock"); let path = Path::new(&workspace.os_dir()).join("qemu-gdb-sock");
@ -109,6 +115,10 @@ mod qemu_gdb_feature {
#[test] #[test]
fn vsc_launch_file() { fn vsc_launch_file() {
// Test skipped because TDX is enabled.
if is_tdx_enabled() {
return;
}
let kernel_name = "vsc_launch_file"; let kernel_name = "vsc_launch_file";
let workspace = workspace::WorkSpace::new(WORKSPACE, kernel_name); let workspace = workspace::WorkSpace::new(WORKSPACE, kernel_name);
let addr = ":50001"; let addr = ":50001";

View File

@ -2,7 +2,7 @@
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use crate::util::{cargo_osdk, depends_on_local_ostd}; use crate::util::{cargo_osdk, edit_config_files};
#[test] #[test]
fn create_and_run_kernel() { fn create_and_run_kernel() {
@ -20,8 +20,7 @@ fn create_and_run_kernel() {
command.ok().unwrap(); command.ok().unwrap();
// Makes the kernel depend on local OSTD // Makes the kernel depend on local OSTD
let manifest_path = os_dir.join("Cargo.toml"); edit_config_files(&os_dir);
depends_on_local_ostd(&manifest_path);
let mut command = cargo_osdk(&["build"]); let mut command = cargo_osdk(&["build"]);
command.current_dir(&os_dir); command.current_dir(&os_dir);
@ -52,8 +51,7 @@ fn create_and_test_library() {
command.current_dir(work_dir); command.current_dir(work_dir);
command.ok().unwrap(); command.ok().unwrap();
let manifest_path = module_dir.join("Cargo.toml"); edit_config_files(&module_dir);
depends_on_local_ostd(manifest_path);
let mut command = cargo_osdk(&["test"]); let mut command = cargo_osdk(&["test"]);
command.current_dir(&module_dir); command.current_dir(&module_dir);

View File

@ -6,7 +6,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use crate::util::{cargo_osdk, depends_on_local_ostd}; use crate::util::{add_tdx_scheme, cargo_osdk, depends_on_local_ostd, is_tdx_enabled};
#[test] #[test]
fn work_in_workspace() { fn work_in_workspace() {
@ -58,6 +58,12 @@ fn work_in_workspace() {
let kernel_manifest_path = workspace_dir.join(kernel).join("Cargo.toml"); let kernel_manifest_path = workspace_dir.join(kernel).join("Cargo.toml");
assert!(kernel_manifest_path.is_file()); assert!(kernel_manifest_path.is_file());
depends_on_local_ostd(&kernel_manifest_path); depends_on_local_ostd(&kernel_manifest_path);
if is_tdx_enabled() {
add_tdx_scheme(workspace_dir.join("OSDK.toml")).unwrap();
}
let kernel_manifest_path = workspace_dir.join(kernel).join("Cargo.toml");
let mut kernel_manifest_file = OpenOptions::new() let mut kernel_manifest_file = OpenOptions::new()
.append(true) .append(true)
.open(&kernel_manifest_path) .open(&kernel_manifest_path)

View File

@ -4,7 +4,7 @@ use std::{fs, path::PathBuf, process::Command};
use assert_cmd::output::OutputOkExt; use assert_cmd::output::OutputOkExt;
use crate::util::{cargo_osdk, depends_on_local_ostd}; use crate::util::{cargo_osdk, edit_config_files};
#[test] #[test]
fn write_a_kernel_in_100_lines() { fn write_a_kernel_in_100_lines() {
@ -23,9 +23,7 @@ fn write_a_kernel_in_100_lines() {
.ok() .ok()
.unwrap(); .unwrap();
// Depends on local OSTD edit_config_files(&os_dir);
let manifest_path = os_dir.join("Cargo.toml");
depends_on_local_ostd(manifest_path);
// Copies the kernel content // Copies the kernel content
let kernel_contents = include_str!("write_a_kernel_in_100_lines_templates/lib.rs"); let kernel_contents = include_str!("write_a_kernel_in_100_lines_templates/lib.rs");

View File

@ -4,7 +4,8 @@
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
fs::{self, create_dir_all}, fs::{self, create_dir_all, OpenOptions},
io::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Output, process::Output,
}; };
@ -12,13 +13,24 @@ use std::{
use assert_cmd::Command; use assert_cmd::Command;
use toml::{Table, Value}; use toml::{Table, Value};
pub fn cargo_osdk<T: AsRef<OsStr>, I: IntoIterator<Item = T>>(args: I) -> Command { pub fn cargo_osdk<T: AsRef<OsStr>, I: IntoIterator<Item = T> + Copy>(args: I) -> Command {
let mut command = Command::cargo_bin("cargo-osdk").unwrap(); let mut command = Command::cargo_bin("cargo-osdk").unwrap();
command.arg("osdk"); command.arg("osdk");
command.args(args); command.args(args);
conditionally_add_tdx_args(&mut command, args);
command command
} }
pub fn edit_config_files(dir: &Path) {
let manifest_path = dir.join("Cargo.toml");
assert!(manifest_path.is_file());
depends_on_local_ostd(manifest_path);
if is_tdx_enabled() {
let osdk_path = dir.join("OSDK.toml");
add_tdx_scheme(osdk_path).unwrap();
};
}
pub fn assert_success(output: &Output) { pub fn assert_success(output: &Output) {
assert!( assert!(
output.status.success(), output.status.success(),
@ -91,7 +103,7 @@ pub fn add_member_to_workspace(workspace: impl AsRef<Path>, new_member: &str) {
/// instead of ostd from remote source(git repo/crates.io). /// instead of ostd from remote source(git repo/crates.io).
/// ///
/// Each crate created by `cargo ostd new` should add this patch. /// Each crate created by `cargo ostd new` should add this patch.
pub fn depends_on_local_ostd(manifest_path: impl AsRef<Path>) { pub(crate) fn depends_on_local_ostd(manifest_path: impl AsRef<Path>) {
let crate_dir = env!("CARGO_MANIFEST_DIR"); let crate_dir = env!("CARGO_MANIFEST_DIR");
let ostd_dir = PathBuf::from(crate_dir) let ostd_dir = PathBuf::from(crate_dir)
.join("..") .join("..")
@ -118,3 +130,40 @@ pub fn depends_on_local_ostd(manifest_path: impl AsRef<Path>) {
fs::write(manifest_path, manifest.to_string().as_bytes()).unwrap(); fs::write(manifest_path, manifest.to_string().as_bytes()).unwrap();
} }
pub(crate) fn add_tdx_scheme(osdk_path: impl AsRef<Path>) -> std::io::Result<()> {
let template_path = Path::new(file!())
.parent()
.unwrap()
.join("scheme.tdx.template");
let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(osdk_path)?;
let tdx_qemu_cfg = fs::read_to_string(template_path)?;
file.write_all(format!("\n\n{}", tdx_qemu_cfg).as_bytes())?;
Ok(())
}
pub(crate) fn is_tdx_enabled() -> bool {
std::env::var("INTEL_TDX").is_ok()
}
fn conditionally_add_tdx_args<T: AsRef<OsStr>, I: IntoIterator<Item = T> + Copy>(
command: &mut Command,
args: I,
) {
if is_tdx_enabled() && contains_build_run_or_test(args) {
command.args(&["--scheme", "tdx"]);
}
}
fn contains_build_run_or_test<T: AsRef<OsStr>, I: IntoIterator<Item = T>>(args: I) -> bool {
args.into_iter().any(|arg| {
if let Some(arg_str) = arg.as_ref().to_str() {
arg_str == "build" || arg_str == "run" || arg_str == "test"
} else {
false
}
})
}

View File

@ -0,0 +1,26 @@
[scheme."tdx"]
boot.method = "grub-qcow2"
grub.boot_protocol = "linux"
qemu.args = """
-accel kvm \
-name process=tdxvm,debug-threads=on \
-m 8G \
-smp 1 \
-vga none \
-nographic \
-monitor pty \
-no-hpet \
-nodefaults \
-bios /usr/share/qemu/OVMF.fd \
-object tdx-guest,sept-ve-disable=on,id=tdx,quote-generation-service=vsock:2:4050 \
-cpu host,-kvm-steal-time,pmu=off \
-machine q35,kernel_irqchip=split,confidential-guest-support=tdx,memory-backend=ram1 \
-object memory-backend-memfd-private,id=ram1,size=8G \
-device virtio-keyboard-pci,disable-legacy=on,disable-modern=off \
-chardev stdio,id=mux,mux=on,logfile=qemu.log \
-device virtio-serial,romfile= \
-device virtconsole,chardev=mux \
-device isa-debug-exit,iobase=0xf4,iosize=0x04 \
-monitor chardev:mux \
-serial chardev:mux \
"""