Skip to content

Commit

Permalink
remove gmem, userspace_mem_region2, and mem_attrs code now that its u…
Browse files Browse the repository at this point in the history
…pstream in kvm-bindings and kvm-ioctls

Signed-off-by: Jake Correnti <[email protected]>
  • Loading branch information
jakecorrenti committed Aug 5, 2024
1 parent 4b2381b commit f0b1fbe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 121 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ rust-version = "1.71"

[dependencies]
bitflags = "2.4.2"
kvm-bindings = "0.7.0"
kvm-ioctls = "0.16.0"
kvm-bindings = { git= "https://www.github.com/rust-vmm/kvm-bindings.git", branch = "main"}
kvm-ioctls = { git= "https://www.github.com/rust-vmm/kvm-ioctls.git", branch = "main"}
# kvm-bindings = "0.7.0"
# kvm-ioctls = "0.16.0"
libc = "0.2.155"
uuid = "1.8.0"
vmm-sys-util = "0.12.1"

[patch.crates-io]
kvm-bindings = { git = "https://www.github.com/rust-vmm/kvm-bindings.git", branch = "main"}
149 changes: 30 additions & 119 deletions tests/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,36 @@ fn launch() {

for (slot, section) in sections.iter().enumerate() {
let userspace_address = ram_mmap(section.memory_data_size);
set_user_memory_region2(&tdx_vm.fd, slot as u32, userspace_address, &section);
set_memory_attributes(&tdx_vm.fd, &section);
let gmem = kvm_bindings::kvm_create_guest_memfd {
size: section.memory_data_size,
flags: 0,
reserved: [0; 6],
};
let gmem = tdx_vm.fd.create_guest_memfd(gmem).unwrap();
let region = kvm_bindings::kvm_userspace_memory_region2 {
slot: slot as u32,
// KVM_MEM_GUEST_MEMFD
flags: 1 << 2,
guest_phys_addr: section.memory_address,
memory_size: section.memory_data_size,
userspace_addr: userspace_address,
guest_memfd_offset: 0,
guest_memfd: gmem as u32,
pad1: 0,
pad2: [0; 14],
};
unsafe {
tdx_vm.fd.set_user_memory_region2(region).unwrap();
}

let attr = kvm_bindings::kvm_memory_attributes {
address: section.memory_address,
size: section.memory_data_size,
// KVM_MEMORY_ATTRIBUTE_PRIVATE
attributes: 1 << 3,
flags: 0,
};
tdx_vm.fd.set_memory_attributes(attr).unwrap();

if check_extension(KVM_CAP_MEMORY_MAPPING) {
// TODO(jakecorrenti): the current CentOS SIG doesn't support the KVM_MEMORY_MAPPING or
Expand Down Expand Up @@ -148,120 +176,3 @@ fn check_extension(i: u32) -> bool {
let kvm = Kvm::new().unwrap();
(unsafe { ioctl::ioctl_with_val(&kvm, KVM_CHECK_EXTENSION(), i.into()) }) > 0
}

// FIXME: All of the following code is not currently upstream at rust-vmm/kvm-ioctls. Therefore, we need to implement it ourselves.
// The work is currently ongoing as of 06/06/2024 and can be found at this link: https://github.com/rust-vmm/kvm-ioctls/pull/264
#[repr(C)]
#[derive(Debug)]
struct KvmCreateGuestMemfd {
size: u64,
flags: u64,
reserved: [u64; 6],
}

ioctl_iowr_nr!(
KVM_CREATE_GUEST_MEMFD,
kvm_bindings::KVMIO,
0xd4,
KvmCreateGuestMemfd
);

fn create_guest_memfd(vmfd: &kvm_ioctls::VmFd, section: &tdvf::TdvfSection) -> i32 {
let gmem = KvmCreateGuestMemfd {
size: section.memory_data_size,
flags: 0,
reserved: [0; 6],
};
linux_ioctls::create_guest_memfd(&vmfd, &gmem)
}

#[repr(C)]
#[derive(Debug)]
struct KvmUserspaceMemoryRegion2 {
slot: u32,
flags: u32,
guest_phys_addr: u64,
memory_size: u64,
userspace_addr: u64,
guest_memfd_offset: u64,
guest_memfd: u32,
pad1: u32,
pad2: [u64; 14],
}

ioctl_iow_nr!(
KVM_SET_USER_MEMORY_REGION2,
kvm_bindings::KVMIO,
0x49,
KvmUserspaceMemoryRegion2
);

fn set_user_memory_region2(
vmfd: &kvm_ioctls::VmFd,
slot: u32,
userspace_address: u64,
section: &tdvf::TdvfSection,
) {
const KVM_MEM_GUEST_MEMFD: u32 = 1 << 2;
let mem_region = KvmUserspaceMemoryRegion2 {
slot,
flags: KVM_MEM_GUEST_MEMFD,
guest_phys_addr: section.memory_address,
memory_size: section.memory_data_size,
userspace_addr: userspace_address,
guest_memfd_offset: 0,
guest_memfd: create_guest_memfd(vmfd, section) as u32,
pad1: 0,
pad2: [0; 14],
};
linux_ioctls::set_user_memory_region2(vmfd, &mem_region)
}

#[repr(C)]
#[derive(Debug)]
struct KvmMemoryAttributes {
address: u64,
size: u64,
attributes: u64,
flags: u64,
}

ioctl_iow_nr!(
KVM_SET_MEMORY_ATTRIBUTES,
kvm_bindings::KVMIO,
0xd2,
KvmMemoryAttributes
);

fn set_memory_attributes(vmfd: &kvm_ioctls::VmFd, section: &tdvf::TdvfSection) {
const KVM_MEMORY_ATTRIBUTE_PRIVATE: u64 = 1 << 3;
let attr = KvmMemoryAttributes {
address: section.memory_address,
size: section.memory_data_size,
attributes: KVM_MEMORY_ATTRIBUTE_PRIVATE,
flags: 0,
};
linux_ioctls::set_memory_attributes(vmfd, &attr)
}

mod linux_ioctls {
use super::*;

pub fn create_guest_memfd(fd: &kvm_ioctls::VmFd, gmem: &KvmCreateGuestMemfd) -> i32 {
unsafe { ioctl::ioctl_with_ref(fd, KVM_CREATE_GUEST_MEMFD(), gmem) }
}

pub fn set_user_memory_region2(fd: &kvm_ioctls::VmFd, mem_region: &KvmUserspaceMemoryRegion2) {
let ret = unsafe { ioctl::ioctl_with_ref(fd, KVM_SET_USER_MEMORY_REGION2(), mem_region) };
if ret != 0 {
panic!("Error: set_user_memory_region2: {}", errno::Error::last())
}
}

pub fn set_memory_attributes(fd: &kvm_ioctls::VmFd, attr: &KvmMemoryAttributes) {
let ret = unsafe { ioctl::ioctl_with_ref(fd, KVM_SET_MEMORY_ATTRIBUTES(), attr) };
if ret != 0 {
panic!("Error: set_memory_attributes: {}", errno::Error::last())
}
}
}

0 comments on commit f0b1fbe

Please sign in to comment.