Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structure change #4

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion src/vm/linux/types.rs → src/launch/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,67 @@

pub const NR_CPUID_CONFIGS: usize = 12;

/// Trust Domain eXtensions sub-ioctl() commands
#[repr(u32)]
pub enum CmdId {
GetCapabilities = 0,
InitVm = 1,
}

/// Contains information for the sub-ioctl() command to be run. This is
/// equivalent to `struct kvm_tdx_cmd` in the kernel.
#[derive(Default)]
#[repr(C)]
pub struct Cmd {
/// TDX command identifier
pub id: u32,

/// Flags for sub-command. If sub-command doesn't use it, set to zero.
pub flags: u32,

/// A u64 representing a generic pointer to the respective ioctl input.
/// This data is read differently according to the TDX ioctl identifier.
pub data: u64,

/// Auxiliary error code. The sub-command may return TDX SEAMCALL status
/// code in addition to -Exxx.
pub error: u64,

/// Reserved.
pub _unused: u64,
}

#[derive(Debug)]
pub struct TdxError {
pub code: i32,
pub message: String,
}

impl From<kvm_ioctls::Error> for TdxError {
fn from(kvm_err: kvm_ioctls::Error) -> Self {
TdxError::from(kvm_err.errno())
}
}

impl From<i32> for TdxError {
fn from(errno: i32) -> Self {
match errno {
7 => TdxError {
code: 7,
message: String::from("Invalid value for NR_CPUID_CONFIGS"),
},
25 => TdxError {
code: 25,
message: String::from("Inappropriate ioctl for device. Ensure the proper VM type is being used for the ioctl"),
},
_ => TdxError {
code: errno,
message: format!("errno: {}", errno),
},
}
}
}

/// CPUID_CONFIG is designed to enumerate how the host VMM may configure the
/// virtualization done by the Intel TDX module for a single CPUID leaf and
/// sub-leaf. This is equivalent to `struct kvm_tdx_cpuid_config` in the kernel.
Expand Down Expand Up @@ -97,6 +158,18 @@ impl Default for Capabilities {
}
}

impl From<&Capabilities> for Cmd {
fn from(caps: &Capabilities) -> Self {
Self {
id: CmdId::GetCapabilities as u32,
flags: 0,
data: caps as *const Capabilities as _,
error: 0,
_unused: 0,
}
}
}

/// TDX specific VM initialization information
#[derive(Debug)]
#[repr(C)]
Expand Down Expand Up @@ -141,7 +214,7 @@ impl Default for InitVm {
Self {
// Set the SEPT_VE_DISABLE bit by default to prevent an Extended Page Table
// (EPT) violation to #VE caused by guest TD access of PENDING pages
attributes: crate::vm::AttributesFlags::SEPT_VE_DISABLE.bits(),
attributes: super::AttributesFlags::SEPT_VE_DISABLE.bits(),
mrconfigid: [0; 6],
mrowner: [0; 6],
mrownerconfig: [0; 6],
Expand All @@ -152,3 +225,15 @@ impl Default for InitVm {
}
}
}

impl From<&InitVm> for Cmd {
fn from(init_vm: &InitVm) -> Self {
Self {
id: CmdId::InitVm as u32,
flags: 0,
data: init_vm as *const InitVm as _,
error: 0,
_unused: 0,
}
}
}
9 changes: 4 additions & 5 deletions src/vm/mod.rs → src/launch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

mod linux;

use crate::linux::{Cmd, TdxError};
use crate::vm::linux::types::{Capabilities, CpuidConfig, InitVm};
use linux::{Capabilities, Cmd, CpuidConfig, InitVm, TdxError};

use bitflags::bitflags;
use kvm_ioctls::{Kvm, VmFd};
use std::arch::x86_64;
Expand Down Expand Up @@ -51,8 +51,7 @@ impl TdxVm {
let cpuid = kvm_fd
.get_supported_cpuid(kvm_bindings::KVM_MAX_CPUID_ENTRIES)
.unwrap();
let mut cpuid_entries: Vec<kvm_bindings::kvm_cpuid_entry2> =
cpuid.as_slice().iter().map(|e| (*e).into()).collect();
let mut cpuid_entries: Vec<kvm_bindings::kvm_cpuid_entry2> = cpuid.as_slice().to_vec();

// resize to 256 entries to make sure that InitVm is 8KB
cpuid_entries.resize(256, kvm_bindings::kvm_cpuid_entry2::default());
Expand Down Expand Up @@ -90,7 +89,7 @@ impl TdxVm {
0x8000_0008 => {
// host physical address bits supported
let phys_bits = unsafe { x86_64::__cpuid(0x8000_0008).eax } & 0xff;
entry.eax = (entry.eax & 0xffff_ff00) | (phys_bits as u32 & 0xff);
entry.eax = (entry.eax & 0xffff_ff00) | (phys_bits & 0xff);
}
_ => (),
}
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// SPDX-License-Identifier: Apache-2.0

pub mod vcpu;
pub mod vm;

#[cfg(target_os = "linux")]
pub mod linux;
pub mod launch;
63 changes: 0 additions & 63 deletions src/linux/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/vcpu/linux/ioctl.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/vcpu/linux/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/vcpu/linux/types.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/vcpu/mod.rs

This file was deleted.

28 changes: 0 additions & 28 deletions src/vm/linux/ioctl.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/vm/linux/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use kvm_ioctls::Kvm;

use tdx::vm::TdxVm;
use tdx::launch::TdxVm;

#[test]
fn launch() {
Expand Down