Skip to content

Commit

Permalink
trying to do something with the flash ptr to see if that does any magic
Browse files Browse the repository at this point in the history
Signed-off-by: Jake Correnti <[email protected]>
  • Loading branch information
jakecorrenti committed Aug 5, 2024
1 parent f0b1fbe commit 4f79dcf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kvm-ioctls = { git= "https://www.github.com/rust-vmm/kvm-ioctls.git", branch = "
# kvm-bindings = "0.7.0"
# kvm-ioctls = "0.16.0"
libc = "0.2.155"
memmap2 = "0.9.4"
uuid = "1.8.0"
vmm-sys-util = "0.12.1"

Expand Down
49 changes: 45 additions & 4 deletions tests/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ fn launch() {
let caps = tdx_vm.get_capabilities().unwrap();
let _ = tdx_vm.init_vm(&kvm_fd, &caps).unwrap();

// get tdvf sections

// Use File::metadata to get the metadata and therefore get the length of the file. Keep in mind that you have to sync the file (by using File::sync_all) with the underlying filesystem to update the metadata and get the correct value.
// let mut firmware = std::fs::File::open("/usr/share/edk2/ovmf/OVM.inteltdx.fd").unwrap();
// firmware.sync_all();
// let firmware_userspace = unsafe { ram_mmap(firmware.metadata().unwrap().len()) };
// unsafe {
// libc::memcpy(
// firmware_userspace as *mut libc::c_void,
// firmware as *const libc::c_void,
// firmware.metadata().unwrap().len() as usize,
// )
// };
use memmap2::Mmap;

let mut firmware = std::fs::File::open("/usr/share/edk2/ovmf/OVMF.inteltdx.fd").unwrap();
let mmap = unsafe { Mmap::map(&firmware).unwrap() };
let flash_ptr = mmap.as_ptr();

let sections = tdvf::parse_sections(&mut firmware).unwrap();
let hob_section = tdvf::get_hob_section(&sections).unwrap();

// create vcpu
let mut vcpufd = tdx_vm.fd.create_vcpu(10).unwrap();
let tdx_vcpu = TdxVcpu::try_from((&mut vcpufd, &mut kvm_fd)).unwrap();
let mut firmware = std::fs::File::open("./tests/data/OVMF.inteltdx.fd").unwrap();
let sections = tdvf::parse_sections(&mut firmware).unwrap();
let hob_section = tdvf::get_hob_section(&sections).unwrap();
tdx_vcpu.init(hob_section.memory_address).unwrap();

// map memory to guest
Expand All @@ -31,7 +50,17 @@ fn launch() {
}

for (slot, section) in sections.iter().enumerate() {
let userspace_address = ram_mmap(section.memory_data_size);
let mut userspace_address = 0u64;

match section.section_type {
tdvf::TdvfSectionType::Bfv | tdvf::TdvfSectionType::Cfv => {
userspace_address = unsafe { flash_ptr.add(section.data_offset as usize) as u64 };
}
_ => {
userspace_address = ram_mmap(section.memory_data_size);
}
}

let gmem = kvm_bindings::kvm_create_guest_memfd {
size: section.memory_data_size,
flags: 0,
Expand Down Expand Up @@ -73,6 +102,18 @@ fn launch() {

// finalize measurement
tdx_vm.finalize().unwrap();

// 6. Run code on the vCPU.
loop {
println!("running");
match tdx_vcpu.fd.run().expect("run failed") {
kvm_ioctls::VcpuExit::Hlt => {
println!("halt reached");
break;
}
r => panic!("Unexpected exit reason: {:?}", r),
}
}
}

/// Round number down to multiple
Expand Down

0 comments on commit 4f79dcf

Please sign in to comment.