1
0
Fork 0
mirror of https://github.com/hermitcore/libhermit.git synced 2025-03-09 00:00:03 +01:00

Use of intended ioctl! syntax

The syntax ioctl!(write <fnc> with KVMIO, <id>; <type>) is shorter than
ioctl!(<fnc> with iow!(KVMIO, <id>; mem::size_of::<type>())). Futhermore
the filedescriptor of vcpu.rs was invalid, because the destructor of
File::from_raw was immediately called. The vcpu struct contains now
the copy of the fd in the Rust world, perhaps there is an easier way to
pass the fd to memmap.
This commit is contained in:
l_schmid 2017-05-11 20:05:42 +02:00
parent 5226d4a19a
commit d687208b01
3 changed files with 25 additions and 22 deletions

View file

@ -19,29 +19,30 @@ pub mod ioctl {
ioctl!(get_version with io!(KVMIO, 0x00));
ioctl!(create_vm with io!(KVMIO, 0x01));
ioctl!(get_msr_index_list with iorw!(KVMIO, 0x02, mem::size_of::<kvm_msr_list>()));
ioctl!(read get_msr_index_list with KVMIO, 0x02; kvm_msr_list);
ioctl!(check_extension with io!(KVMIO, 0x03));
ioctl!(get_vcpu_mmap_size with io!(KVMIO, 0x04));
ioctl!(get_supported_cpuid with iorw!(KVMIO, 0x05, mem::size_of::<kvm_cpuid2>()));
ioctl!(get_emulated_cpuid with iorw!(KVMIO, 0x09, mem::size_of::<kvm_cpuid2>()));
ioctl!(set_cpuid2 with iow!(KVMIO, 0x90, mem::size_of::<kvm_cpuid2>()));
ioctl!(read get_supported_cpuid with KVMIO, 0x05; kvm_cpuid2);
ioctl!(read get_emulated_cpuid with KVMIO,0x09; kvm_cpuid2);
ioctl!(write set_cpuid2 with KVMIO, 0x90; kvm_cpuid2);
ioctl!(set_memory_region with iorw!(KVMIO, 0x40,mem::size_of::<kvm_memory_region>()));
ioctl!(create_vcpu with io!(KVMIO, 0x41));
ioctl!(get_dirty_log with iow!(KVMIO, 0x42, mem::size_of::<kvm_dirty_log>()));
ioctl!(set_memory_alias with iow!(KVMIO, 0x43, mem::size_of::<kvm_memory_alias>()));
ioctl!(read get_dirty_log with KVMIO, 0x42; kvm_dirty_log);
ioctl!(write set_memory_alias with KVMIO, 0x43; kvm_memory_alias);
ioctl!(set_nr_mmu_pages with io!(KVMIO, 0x44));
ioctl!(get_nr_mmu_pages with io!(KVMIO, 0x45));
ioctl!(set_user_memory_region with iow!(KVMIO, 0x46, mem::size_of::<kvm_userspace_memory_region>()));
ioctl!(write set_memory_region with KVMIO, 0x40; kvm_memory_region);
ioctl!(write set_user_memory_region with KVMIO, 0x46; kvm_userspace_memory_region);
ioctl!(create_irqchip with io!(KVMIO, 0x60));
ioctl!(run with io!(KVMIO, 0x80));
ioctl!(get_regs with ior!(KVMIO, 0x81, mem::size_of::<kvm_regs>()));
ioctl!(set_regs with iow!(KVMIO, 0x82, mem::size_of::<kvm_regs>()));
ioctl!(get_sregs with ior!(KVMIO, 0x83, mem::size_of::<kvm_sregs>()));
ioctl!(set_sregs with iow!(KVMIO, 0x84, mem::size_of::<kvm_sregs>()));
ioctl!(read get_regs with KVMIO, 0x81; kvm_regs);
ioctl!(write set_regs with KVMIO, 0x82; kvm_regs);
ioctl!(read get_sregs with KVMIO, 0x83; kvm_sregs);
ioctl!(write set_sregs with KVMIO, 0x84; kvm_sregs);
}

View file

@ -41,6 +41,7 @@ pub struct VirtualCPU {
vm_fd: libc::c_int,
pub vcpu_fd: libc::c_int,
id: u8,
file: File,
run_mem: Mmap
}
@ -53,13 +54,15 @@ impl VirtualCPU {
debug!("Got fd {}", fd);
let file = unsafe { File::from_raw_fd(fd) };
let run_mem = unsafe {
Mmap::open_with_offset(&File::from_raw_fd(fd), Protection::ReadWrite, 0, VirtualCPU::get_mmap_size(kvm_fd)?)
Mmap::open_with_offset(&file, Protection::ReadWrite, 0, VirtualCPU::get_mmap_size(kvm_fd)?)
.map_err(|x| panic!("{:?}", x) )//Error::InvalidFile)
}?;
let cpu = VirtualCPU {
vm_fd: vm_fd, vcpu_fd: fd, id: id, run_mem: run_mem
vm_fd: vm_fd, vcpu_fd: fd, id: id, file: file, run_mem: run_mem
};
if id == 0 {
@ -95,7 +98,7 @@ impl VirtualCPU {
pub fn get_sregs(&self) -> Result<kvm_sregs> {
let mut sregs = kvm_sregs::empty();
unsafe {
uhyve::ioctl::get_sregs(self.vcpu_fd, (&mut sregs) as *mut kvm_sregs as *mut u8)
uhyve::ioctl::get_sregs(self.vcpu_fd, (&mut sregs) as *mut kvm_sregs)
.map_err(|x| Error::FailedIOCTL(x))?;
}
@ -104,7 +107,7 @@ impl VirtualCPU {
pub fn set_sregs(&self, mut sregs: kvm_sregs) -> Result<()> {
unsafe {
uhyve::ioctl::set_sregs(self.vcpu_fd, (&mut sregs) as *mut kvm_sregs as *mut u8)
uhyve::ioctl::set_sregs(self.vcpu_fd, (&mut sregs) as *mut kvm_sregs)
.map_err(|x| Error::FailedIOCTL(x))?;
}
@ -113,7 +116,7 @@ impl VirtualCPU {
pub fn set_regs(&self, mut regs: kvm_regs) -> Result<()> {
unsafe {
uhyve::ioctl::set_regs(self.vcpu_fd, (&mut regs) as *mut kvm_regs as *mut u8)
uhyve::ioctl::set_regs(self.vcpu_fd, (&mut regs) as *mut kvm_regs)
.map_err(|x| Error::FailedIOCTL(x))?;
}
@ -123,7 +126,7 @@ impl VirtualCPU {
pub fn get_supported_cpuid(&self) -> Result<kvm_cpuid2> {
let mut cpuid = kvm_cpuid2::empty();
unsafe {
uhyve::ioctl::get_supported_cpuid(self.vm_fd, (&mut cpuid) as *mut kvm_cpuid2 as *mut u8)
uhyve::ioctl::get_supported_cpuid(self.vm_fd, (&mut cpuid) as *mut kvm_cpuid2)
.map_err(|x| Error::FailedIOCTL(x))?;
}
@ -132,7 +135,7 @@ impl VirtualCPU {
pub fn set_cpuid2(&self, mut cpuid: kvm_cpuid2) -> Result<()> {
unsafe {
uhyve::ioctl::set_cpuid2(self.vm_fd, (&mut cpuid) as *mut kvm_cpuid2 as *mut u8)
uhyve::ioctl::set_cpuid2(self.vm_fd, (&mut cpuid) as *mut kvm_cpuid2)
.map_err(|x| Error::FailedIOCTL(x))?;
}

View file

@ -115,8 +115,7 @@ impl VirtualMachine {
pub fn set_user_memory_region(&self, mut region: kvm_userspace_memory_region) -> Result<()> {
unsafe {
let p: *mut kvm_userspace_memory_region = &mut region;
uhyve::ioctl::set_user_memory_region(self.vm_fd, p as *mut u8)
uhyve::ioctl::set_user_memory_region(self.vm_fd, (&mut region) as *mut kvm_userspace_memory_region)
.map_err(|x| Error::FailedIOCTL(x)).map(|_| ())
}
}