sys_util: add ioctl module

Factor out common ioctl wrappers and macros into their own module.

Signed-off-by: Stephen Barber <smbarber@chromium.org>

BUG=none
TEST=cargo test

Change-Id: Ibede8a853f5cc6c6d62565930f312b11024cc5b5
Reviewed-on: https://chromium-review.googlesource.com/566540
Commit-Ready: Stephen Barber <smbarber@chromium.org>
Tested-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
Stephen Barber 2017-07-10 18:33:19 -07:00 committed by chrome-bot
parent 8686d9ff5b
commit c4968f7de9
5 changed files with 231 additions and 155 deletions

View file

@ -20,6 +20,8 @@ use libc::{open, O_RDWR, EINVAL, ENOSPC};
use kvm_sys::*;
use sys_util::{GuestAddress, GuestMemory, MemoryMapping, EventFd, Error, Result};
use sys_util::{ioctl, ioctl_with_val, ioctl_with_ref, ioctl_with_mut_ref, ioctl_with_ptr,
ioctl_with_mut_ptr};
pub use cap::*;
@ -27,30 +29,6 @@ fn errno_result<T>() -> Result<T> {
Err(Error::last())
}
unsafe fn ioctl<F: AsRawFd>(fd: &F, nr: c_ulong) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, 0)
}
unsafe fn ioctl_with_val<F: AsRawFd>(fd: &F, nr: c_ulong, arg: c_ulong) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg)
}
unsafe fn ioctl_with_ref<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: &T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *const T as *const c_void)
}
unsafe fn ioctl_with_mut_ref<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: &mut T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *mut T as *mut c_void)
}
unsafe fn ioctl_with_ptr<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: *const T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *const c_void)
}
unsafe fn ioctl_with_mut_ptr<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: *mut T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *mut c_void)
}
unsafe fn set_user_memory_region<F: AsRawFd>(fd: &F, slot: u32, guest_addr: u64, memory_size: u64, userspace_addr: u64) -> Result<()> {
let region = kvm_userspace_memory_region {
slot: slot,

View file

@ -5,3 +5,6 @@ authors = ["The Chromium OS Authors"]
[dev-dependencies]
libc = "0.2.21"
[dependencies]
sys_util = { path = "../sys_util" }

View file

@ -6,46 +6,12 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#[macro_use]
extern crate sys_util;
// Somehow this one gets missed by bindgen
pub const KVM_EXIT_IO_OUT: ::std::os::raw::c_uint = 1;
// Each ioctl number gets a function instead of a constant because size_of can
// not be used in const expressions.
macro_rules! ioctl_ioc_nr {
($name:ident, $dir:expr, $ty:expr, $nr:expr, $size:expr) => (
pub fn $name() -> ::std::os::raw::c_ulong {
(($dir << _IOC_DIRSHIFT) |
($ty << _IOC_TYPESHIFT) |
($nr<< _IOC_NRSHIFT) |
($size << _IOC_SIZESHIFT)) as ::std::os::raw::c_ulong
}
)
}
macro_rules! ioctl_io_nr {
($name:ident, $nr:expr) => (
ioctl_ioc_nr!($name, _IOC_NONE, KVMIO, $nr, 0);
)
}
macro_rules! ioctl_ior_nr {
($name:ident, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, _IOC_READ, KVMIO, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
macro_rules! ioctl_iow_nr {
($name:ident, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, _IOC_WRITE, KVMIO, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
macro_rules! ioctl_iowr_nr {
($name:ident, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, _IOC_READ|_IOC_WRITE, KVMIO, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
// Each of the below modules defines ioctls specific to their platform.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
@ -54,31 +20,31 @@ pub mod x86 {
pub mod bindings;
pub use bindings::*;
ioctl_iowr_nr!(KVM_GET_MSR_INDEX_LIST, 0x02, kvm_msr_list);
ioctl_iowr_nr!(KVM_GET_SUPPORTED_CPUID, 0x05, kvm_cpuid2);
ioctl_iowr_nr!(KVM_GET_EMULATED_CPUID, 0x09, kvm_cpuid2);
ioctl_iow_nr!(KVM_SET_MEMORY_ALIAS, 0x43, kvm_memory_alias);
ioctl_iow_nr!(KVM_XEN_HVM_CONFIG, 0x7a, kvm_xen_hvm_config);
ioctl_ior_nr!(KVM_GET_PIT2, 0x9f, kvm_pit_state2);
ioctl_iow_nr!(KVM_SET_PIT2, 0xa0, kvm_pit_state2);
ioctl_iowr_nr!(KVM_GET_MSRS, 0x88, kvm_msrs);
ioctl_iow_nr!(KVM_SET_MSRS, 0x89, kvm_msrs);
ioctl_iow_nr!(KVM_SET_CPUID, 0x8a, kvm_cpuid);
ioctl_ior_nr!(KVM_GET_LAPIC, 0x8e, kvm_lapic_state);
ioctl_iow_nr!(KVM_SET_LAPIC, 0x8f, kvm_lapic_state);
ioctl_iow_nr!(KVM_SET_CPUID2, 0x90, kvm_cpuid2);
ioctl_iowr_nr!(KVM_GET_CPUID2, 0x91, kvm_cpuid2);
ioctl_iow_nr!(KVM_X86_SETUP_MCE, 0x9c, __u64);
ioctl_ior_nr!(KVM_X86_GET_MCE_CAP_SUPPORTED, 0x9d, __u64);
ioctl_iow_nr!(KVM_X86_SET_MCE, 0x9e, kvm_x86_mce);
ioctl_ior_nr!(KVM_GET_VCPU_EVENTS, 0x9f, kvm_vcpu_events);
ioctl_iow_nr!(KVM_SET_VCPU_EVENTS, 0xa0, kvm_vcpu_events);
ioctl_ior_nr!(KVM_GET_DEBUGREGS, 0xa1, kvm_debugregs);
ioctl_iow_nr!(KVM_SET_DEBUGREGS, 0xa2, kvm_debugregs);
ioctl_ior_nr!(KVM_GET_XSAVE, 0xa4, kvm_xsave);
ioctl_iow_nr!(KVM_SET_XSAVE, 0xa5, kvm_xsave);
ioctl_ior_nr!(KVM_GET_XCRS, 0xa6, kvm_xcrs);
ioctl_iowr_nr!(KVM_SET_XCRS, 0xa7, kvm_xcrs);
ioctl_iowr_nr!(KVM_GET_MSR_INDEX_LIST, KVMIO, 0x02, kvm_msr_list);
ioctl_iowr_nr!(KVM_GET_SUPPORTED_CPUID, KVMIO, 0x05, kvm_cpuid2);
ioctl_iowr_nr!(KVM_GET_EMULATED_CPUID, KVMIO, 0x09, kvm_cpuid2);
ioctl_iow_nr!(KVM_SET_MEMORY_ALIAS, KVMIO, 0x43, kvm_memory_alias);
ioctl_iow_nr!(KVM_XEN_HVM_CONFIG, KVMIO, 0x7a, kvm_xen_hvm_config);
ioctl_ior_nr!(KVM_GET_PIT2, KVMIO, 0x9f, kvm_pit_state2);
ioctl_iow_nr!(KVM_SET_PIT2, KVMIO, 0xa0, kvm_pit_state2);
ioctl_iowr_nr!(KVM_GET_MSRS, KVMIO, 0x88, kvm_msrs);
ioctl_iow_nr!(KVM_SET_MSRS, KVMIO, 0x89, kvm_msrs);
ioctl_iow_nr!(KVM_SET_CPUID, KVMIO, 0x8a, kvm_cpuid);
ioctl_ior_nr!(KVM_GET_LAPIC, KVMIO, 0x8e, kvm_lapic_state);
ioctl_iow_nr!(KVM_SET_LAPIC, KVMIO, 0x8f, kvm_lapic_state);
ioctl_iow_nr!(KVM_SET_CPUID2, KVMIO, 0x90, kvm_cpuid2);
ioctl_iowr_nr!(KVM_GET_CPUID2, KVMIO, 0x91, kvm_cpuid2);
ioctl_iow_nr!(KVM_X86_SETUP_MCE, KVMIO, 0x9c, __u64);
ioctl_ior_nr!(KVM_X86_GET_MCE_CAP_SUPPORTED, KVMIO, 0x9d, __u64);
ioctl_iow_nr!(KVM_X86_SET_MCE, KVMIO, 0x9e, kvm_x86_mce);
ioctl_ior_nr!(KVM_GET_VCPU_EVENTS, KVMIO, 0x9f, kvm_vcpu_events);
ioctl_iow_nr!(KVM_SET_VCPU_EVENTS, KVMIO, 0xa0, kvm_vcpu_events);
ioctl_ior_nr!(KVM_GET_DEBUGREGS, KVMIO, 0xa1, kvm_debugregs);
ioctl_iow_nr!(KVM_SET_DEBUGREGS, KVMIO, 0xa2, kvm_debugregs);
ioctl_ior_nr!(KVM_GET_XSAVE, KVMIO, 0xa4, kvm_xsave);
ioctl_iow_nr!(KVM_SET_XSAVE, KVMIO, 0xa5, kvm_xsave);
ioctl_ior_nr!(KVM_GET_XCRS, KVMIO, 0xa6, kvm_xcrs);
ioctl_iowr_nr!(KVM_SET_XCRS, KVMIO, 0xa7, kvm_xcrs);
}
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
@ -87,80 +53,80 @@ pub mod arm {
pub mod bindings;
pub use bindings::*;
ioctl_iow_nr!(KVM_ARM_SET_DEVICE_ADDR, 0xab, kvm_arm_device_addr);
ioctl_iow_nr!(KVM_ARM_VCPU_INIT, 0xae, kvm_vcpu_init);
ioctl_ior_nr!(KVM_ARM_PREFERRED_TARGET, 0xaf, kvm_vcpu_init);
ioctl_iow_nr!(KVM_ARM_SET_DEVICE_ADDR, KVMIO, 0xab, kvm_arm_device_addr);
ioctl_iow_nr!(KVM_ARM_VCPU_INIT, KVMIO, 0xae, kvm_vcpu_init);
ioctl_ior_nr!(KVM_ARM_PREFERRED_TARGET, KVMIO, 0xaf, kvm_vcpu_init);
}
// These ioctls are commonly defined on all/multiple platforms.
ioctl_io_nr!(KVM_GET_API_VERSION, 0x00);
ioctl_io_nr!(KVM_CREATE_VM, 0x01);
ioctl_io_nr!(KVM_CHECK_EXTENSION, 0x03);
ioctl_io_nr!(KVM_GET_VCPU_MMAP_SIZE, 0x04) /* in bytes */;
ioctl_iow_nr!(KVM_SET_MEMORY_REGION, 0x40, kvm_memory_region);
ioctl_io_nr!(KVM_CREATE_VCPU, 0x41);
ioctl_iow_nr!(KVM_GET_DIRTY_LOG, 0x42, kvm_dirty_log);
ioctl_io_nr!(KVM_SET_NR_MMU_PAGES, 0x44);
ioctl_io_nr!(KVM_GET_NR_MMU_PAGES, 0x45);
ioctl_iow_nr!(KVM_SET_USER_MEMORY_REGION,0x46, kvm_userspace_memory_region);
ioctl_io_nr!(KVM_SET_TSS_ADDR, 0x47);
ioctl_iow_nr!(KVM_SET_IDENTITY_MAP_ADDR, 0x48, __u64);
ioctl_io_nr!(KVM_CREATE_IRQCHIP, 0x60);
ioctl_iow_nr!(KVM_IRQ_LINE, 0x61, kvm_irq_level);
ioctl_iowr_nr!(KVM_GET_IRQCHIP, 0x62, kvm_irqchip);
ioctl_ior_nr!(KVM_SET_IRQCHIP, 0x63, kvm_irqchip);
ioctl_io_nr!(KVM_CREATE_PIT, 0x64);
ioctl_iowr_nr!(KVM_IRQ_LINE_STATUS, 0x67, kvm_irq_level);
ioctl_iow_nr!(KVM_REGISTER_COALESCED_MMIO, 0x67, kvm_coalesced_mmio_zone);
ioctl_iow_nr!(KVM_UNREGISTER_COALESCED_MMIO, 0x68, kvm_coalesced_mmio_zone);
ioctl_ior_nr!(KVM_ASSIGN_PCI_DEVICE, 0x69, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_ASSIGN_DEV_IRQ, 0x70, kvm_assigned_irq);
ioctl_io_nr!(KVM_REINJECT_CONTROL, 0x71);
ioctl_iow_nr!(KVM_DEASSIGN_PCI_DEVICE, 0x72, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_ASSIGN_SET_MSIX_NR, 0x73, kvm_assigned_msix_nr);
ioctl_iow_nr!(KVM_ASSIGN_SET_MSIX_ENTRY, 0x74, kvm_assigned_msix_entry);
ioctl_iow_nr!(KVM_DEASSIGN_DEV_IRQ, 0x75, kvm_assigned_irq);
ioctl_iow_nr!(KVM_IRQFD, 0x76, kvm_irqfd);
ioctl_iow_nr!(KVM_CREATE_PIT2, 0x77, kvm_pit_config);
ioctl_io_nr!(KVM_SET_BOOT_CPU_ID, 0x78);
ioctl_iow_nr!(KVM_IOEVENTFD, 0x79, kvm_ioeventfd);
ioctl_iow_nr!(KVM_SET_CLOCK, 0x7b, kvm_clock_data);
ioctl_ior_nr!(KVM_GET_CLOCK, 0x7c, kvm_clock_data);
ioctl_io_nr!(KVM_SET_TSC_KHZ, 0xa2);
ioctl_io_nr!(KVM_GET_TSC_KHZ, 0xa3);
ioctl_iow_nr!(KVM_ASSIGN_SET_INTX_MASK, 0xa4, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_SIGNAL_MSI, 0xa5, kvm_msi);
ioctl_iowr_nr!(KVM_CREATE_DEVICE, 0xe0, kvm_create_device);
ioctl_iow_nr!(KVM_SET_DEVICE_ATTR, 0xe1, kvm_device_attr);
ioctl_iow_nr!(KVM_GET_DEVICE_ATTR, 0xe2, kvm_device_attr);
ioctl_iow_nr!(KVM_HAS_DEVICE_ATTR, 0xe3, kvm_device_attr);
ioctl_io_nr!(KVM_RUN, 0x80);
ioctl_io_nr!(KVM_GET_API_VERSION, KVMIO, 0x00);
ioctl_io_nr!(KVM_CREATE_VM, KVMIO, 0x01);
ioctl_io_nr!(KVM_CHECK_EXTENSION, KVMIO, 0x03);
ioctl_io_nr!(KVM_GET_VCPU_MMAP_SIZE, KVMIO, 0x04) /* in bytes */;
ioctl_iow_nr!(KVM_SET_MEMORY_REGION, KVMIO, 0x40, kvm_memory_region);
ioctl_io_nr!(KVM_CREATE_VCPU, KVMIO, 0x41);
ioctl_iow_nr!(KVM_GET_DIRTY_LOG, KVMIO, 0x42, kvm_dirty_log);
ioctl_io_nr!(KVM_SET_NR_MMU_PAGES, KVMIO, 0x44);
ioctl_io_nr!(KVM_GET_NR_MMU_PAGES, KVMIO, 0x45);
ioctl_iow_nr!(KVM_SET_USER_MEMORY_REGION,KVMIO, 0x46, kvm_userspace_memory_region);
ioctl_io_nr!(KVM_SET_TSS_ADDR, KVMIO, 0x47);
ioctl_iow_nr!(KVM_SET_IDENTITY_MAP_ADDR, KVMIO, 0x48, __u64);
ioctl_io_nr!(KVM_CREATE_IRQCHIP, KVMIO, 0x60);
ioctl_iow_nr!(KVM_IRQ_LINE, KVMIO, 0x61, kvm_irq_level);
ioctl_iowr_nr!(KVM_GET_IRQCHIP, KVMIO, 0x62, kvm_irqchip);
ioctl_ior_nr!(KVM_SET_IRQCHIP, KVMIO, 0x63, kvm_irqchip);
ioctl_io_nr!(KVM_CREATE_PIT, KVMIO, 0x64);
ioctl_iowr_nr!(KVM_IRQ_LINE_STATUS, KVMIO, 0x67, kvm_irq_level);
ioctl_iow_nr!(KVM_REGISTER_COALESCED_MMIO, KVMIO, 0x67, kvm_coalesced_mmio_zone);
ioctl_iow_nr!(KVM_UNREGISTER_COALESCED_MMIO, KVMIO, 0x68, kvm_coalesced_mmio_zone);
ioctl_ior_nr!(KVM_ASSIGN_PCI_DEVICE, KVMIO, 0x69, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_ASSIGN_DEV_IRQ, KVMIO, 0x70, kvm_assigned_irq);
ioctl_io_nr!(KVM_REINJECT_CONTROL, KVMIO, 0x71);
ioctl_iow_nr!(KVM_DEASSIGN_PCI_DEVICE, KVMIO, 0x72, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_ASSIGN_SET_MSIX_NR, KVMIO, 0x73, kvm_assigned_msix_nr);
ioctl_iow_nr!(KVM_ASSIGN_SET_MSIX_ENTRY, KVMIO, 0x74, kvm_assigned_msix_entry);
ioctl_iow_nr!(KVM_DEASSIGN_DEV_IRQ, KVMIO, 0x75, kvm_assigned_irq);
ioctl_iow_nr!(KVM_IRQFD, KVMIO, 0x76, kvm_irqfd);
ioctl_iow_nr!(KVM_CREATE_PIT2, KVMIO, 0x77, kvm_pit_config);
ioctl_io_nr!(KVM_SET_BOOT_CPU_ID, KVMIO, 0x78);
ioctl_iow_nr!(KVM_IOEVENTFD, KVMIO, 0x79, kvm_ioeventfd);
ioctl_iow_nr!(KVM_SET_CLOCK, KVMIO, 0x7b, kvm_clock_data);
ioctl_ior_nr!(KVM_GET_CLOCK, KVMIO, 0x7c, kvm_clock_data);
ioctl_io_nr!(KVM_SET_TSC_KHZ, KVMIO, 0xa2);
ioctl_io_nr!(KVM_GET_TSC_KHZ, KVMIO, 0xa3);
ioctl_iow_nr!(KVM_ASSIGN_SET_INTX_MASK, KVMIO, 0xa4, kvm_assigned_pci_dev);
ioctl_iow_nr!(KVM_SIGNAL_MSI, KVMIO, 0xa5, kvm_msi);
ioctl_iowr_nr!(KVM_CREATE_DEVICE, KVMIO, 0xe0, kvm_create_device);
ioctl_iow_nr!(KVM_SET_DEVICE_ATTR, KVMIO, 0xe1, kvm_device_attr);
ioctl_iow_nr!(KVM_GET_DEVICE_ATTR, KVMIO, 0xe2, kvm_device_attr);
ioctl_iow_nr!(KVM_HAS_DEVICE_ATTR, KVMIO, 0xe3, kvm_device_attr);
ioctl_io_nr!(KVM_RUN, KVMIO, 0x80);
// The following two ioctls are commonly defined but specifically excluded
// from arm platforms.
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
ioctl_ior_nr!(KVM_GET_REGS, 0x81, kvm_regs);
ioctl_ior_nr!(KVM_GET_REGS, KVMIO, 0x81, kvm_regs);
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
ioctl_iow_nr!(KVM_SET_REGS, 0x82, kvm_regs);
ioctl_ior_nr!(KVM_GET_SREGS, 0x83, kvm_sregs);
ioctl_iow_nr!(KVM_SET_SREGS, 0x84, kvm_sregs);
ioctl_iowr_nr!(KVM_TRANSLATE, 0x85, kvm_translation);
ioctl_iow_nr!(KVM_INTERRUPT, 0x86, kvm_interrupt);
ioctl_iow_nr!(KVM_SET_SIGNAL_MASK, 0x8b, kvm_signal_mask);
ioctl_ior_nr!(KVM_GET_FPU, 0x8c, kvm_fpu);
ioctl_iow_nr!(KVM_SET_FPU, 0x8d, kvm_fpu);
ioctl_iowr_nr!(KVM_TPR_ACCESS_REPORTING, 0x92, kvm_tpr_access_ctl);
ioctl_iow_nr!(KVM_SET_VAPIC_ADDR, 0x93, kvm_vapic_addr);
ioctl_ior_nr!(KVM_GET_MP_STATE, 0x98, kvm_mp_state);
ioctl_iow_nr!(KVM_SET_MP_STATE, 0x99, kvm_mp_state);
ioctl_io_nr!(KVM_NMI, 0x9a);
ioctl_iow_nr!(KVM_SET_GUEST_DEBUG, 0x9b, kvm_guest_debug);
ioctl_iow_nr!(KVM_ENABLE_CAP, 0xa3, kvm_enable_cap);
ioctl_iow_nr!(KVM_DIRTY_TLB, 0xaa, kvm_dirty_tlb);
ioctl_iow_nr!(KVM_GET_ONE_REG, 0xab, kvm_one_reg);
ioctl_iow_nr!(KVM_SET_ONE_REG, 0xac, kvm_one_reg);
ioctl_io_nr!(KVM_KVMCLOCK_CTRL, 0xad);
ioctl_iowr_nr!(KVM_GET_REG_LIST, 0xb0, kvm_reg_list);
ioctl_io_nr!(KVM_SMI, 0xb7);
ioctl_iow_nr!(KVM_SET_REGS, KVMIO, 0x82, kvm_regs);
ioctl_ior_nr!(KVM_GET_SREGS, KVMIO, 0x83, kvm_sregs);
ioctl_iow_nr!(KVM_SET_SREGS, KVMIO, 0x84, kvm_sregs);
ioctl_iowr_nr!(KVM_TRANSLATE, KVMIO, 0x85, kvm_translation);
ioctl_iow_nr!(KVM_INTERRUPT, KVMIO, 0x86, kvm_interrupt);
ioctl_iow_nr!(KVM_SET_SIGNAL_MASK, KVMIO, 0x8b, kvm_signal_mask);
ioctl_ior_nr!(KVM_GET_FPU, KVMIO, 0x8c, kvm_fpu);
ioctl_iow_nr!(KVM_SET_FPU, KVMIO, 0x8d, kvm_fpu);
ioctl_iowr_nr!(KVM_TPR_ACCESS_REPORTING, KVMIO, 0x92, kvm_tpr_access_ctl);
ioctl_iow_nr!(KVM_SET_VAPIC_ADDR, KVMIO, 0x93, kvm_vapic_addr);
ioctl_ior_nr!(KVM_GET_MP_STATE, KVMIO, 0x98, kvm_mp_state);
ioctl_iow_nr!(KVM_SET_MP_STATE, KVMIO, 0x99, kvm_mp_state);
ioctl_io_nr!(KVM_NMI, KVMIO, 0x9a);
ioctl_iow_nr!(KVM_SET_GUEST_DEBUG, KVMIO, 0x9b, kvm_guest_debug);
ioctl_iow_nr!(KVM_ENABLE_CAP, KVMIO, 0xa3, kvm_enable_cap);
ioctl_iow_nr!(KVM_DIRTY_TLB, KVMIO, 0xaa, kvm_dirty_tlb);
ioctl_iow_nr!(KVM_GET_ONE_REG, KVMIO, 0xab, kvm_one_reg);
ioctl_iow_nr!(KVM_SET_ONE_REG, KVMIO, 0xac, kvm_one_reg);
ioctl_io_nr!(KVM_KVMCLOCK_CTRL, KVMIO, 0xad);
ioctl_iowr_nr!(KVM_GET_REG_LIST, KVMIO, 0xb0, kvm_reg_list);
ioctl_io_nr!(KVM_SMI, KVMIO, 0xb7);
// Along with the common ioctls, we reexport the ioctls of the current
// platform.

126
sys_util/src/ioctl.rs Normal file
View file

@ -0,0 +1,126 @@
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Macros and wrapper functions for dealing with ioctls.
use std::os::raw::*;
use std::os::unix::io::AsRawFd;
use libc;
/// Raw macro to declare a function that returns an ioctl number.
#[macro_export]
macro_rules! ioctl_ioc_nr {
($name:ident, $dir:expr, $ty:expr, $nr:expr, $size:expr) => (
#[allow(non_snake_case)]
pub fn $name() -> ::std::os::raw::c_ulong {
(($dir << $crate::ioctl::_IOC_DIRSHIFT) |
($ty << $crate::ioctl::_IOC_TYPESHIFT) |
($nr<< $crate::ioctl::_IOC_NRSHIFT) |
($size << $crate::ioctl::_IOC_SIZESHIFT)) as ::std::os::raw::c_ulong
}
)
}
/// Declare an ioctl that transfers no data.
#[macro_export]
macro_rules! ioctl_io_nr {
($name:ident, $ty:expr, $nr:expr) => (
ioctl_ioc_nr!($name, $crate::ioctl::_IOC_NONE, $ty, $nr, 0);
)
}
/// Declare an ioctl that reads data.
#[macro_export]
macro_rules! ioctl_ior_nr {
($name:ident, $ty:expr, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, $crate::ioctl::_IOC_READ, $ty, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
/// Declare an ioctl that writes data.
#[macro_export]
macro_rules! ioctl_iow_nr {
($name:ident, $ty:expr, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, $crate::ioctl::_IOC_WRITE, $ty, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
/// Declare an ioctl that reads and writes data.
#[macro_export]
macro_rules! ioctl_iowr_nr {
($name:ident, $ty:expr, $nr:expr, $size:ty) => (
ioctl_ioc_nr!($name, $crate::ioctl::_IOC_READ | $crate::ioctl::_IOC_WRITE, $ty, $nr, ::std::mem::size_of::<$size>() as u32);
)
}
pub const _IOC_NRBITS: c_uint = 8;
pub const _IOC_TYPEBITS: c_uint = 8;
pub const _IOC_SIZEBITS: c_uint = 14;
pub const _IOC_DIRBITS: c_uint = 2;
pub const _IOC_NRMASK: c_uint = 255;
pub const _IOC_TYPEMASK: c_uint = 255;
pub const _IOC_SIZEMASK: c_uint = 16383;
pub const _IOC_DIRMASK: c_uint = 3;
pub const _IOC_NRSHIFT: c_uint = 0;
pub const _IOC_TYPESHIFT: c_uint = 8;
pub const _IOC_SIZESHIFT: c_uint = 16;
pub const _IOC_DIRSHIFT: c_uint = 30;
pub const _IOC_NONE: c_uint = 0;
pub const _IOC_WRITE: c_uint = 1;
pub const _IOC_READ: c_uint = 2;
pub const IOC_IN: c_uint = 1073741824;
pub const IOC_OUT: c_uint = 2147483648;
pub const IOC_INOUT: c_uint = 3221225472;
pub const IOCSIZE_MASK: c_uint = 1073676288;
pub const IOCSIZE_SHIFT: c_uint = 16;
/// Run an ioctl with no arguments.
pub unsafe fn ioctl<F: AsRawFd>(fd: &F, nr: c_ulong) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, 0)
}
/// Run an ioctl with a single value argument.
pub unsafe fn ioctl_with_val<F: AsRawFd>(fd: &F, nr: c_ulong, arg: c_ulong) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg)
}
/// Run an ioctl with an immutable reference.
pub unsafe fn ioctl_with_ref<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: &T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *const T as *const c_void)
}
/// Run an ioctl with a mutable reference.
pub unsafe fn ioctl_with_mut_ref<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: &mut T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *mut T as *mut c_void)
}
/// Run an ioctl with a raw pointer.
pub unsafe fn ioctl_with_ptr<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: *const T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *const c_void)
}
/// Run an ioctl with a mutable raw pointer.
pub unsafe fn ioctl_with_mut_ptr<F: AsRawFd, T>(fd: &F, nr: c_ulong, arg: *mut T) -> c_int {
libc::ioctl(fd.as_raw_fd(), nr, arg as *mut c_void)
}
#[cfg(test)]
mod tests {
const TUNTAP: ::std::os::raw::c_uint = 0x54;
const VHOST: ::std::os::raw::c_uint = 0xaf;
ioctl_io_nr!(VHOST_SET_OWNER, VHOST, 0x01);
ioctl_ior_nr!(TUNGETFEATURES, TUNTAP, 0xcf, ::std::os::raw::c_uint);
ioctl_iow_nr!(TUNSETQUEUE, TUNTAP, 0xd9, ::std::os::raw::c_int);
ioctl_iowr_nr!(VHOST_GET_VRING_BASE, VHOST, 0x12, ::std::os::raw::c_int);
#[test]
fn ioctl_macros() {
assert_eq!(0x0000af01, VHOST_SET_OWNER());
assert_eq!(0x800454cf, TUNGETFEATURES());
assert_eq!(0x400454d9, TUNSETQUEUE());
assert_eq!(0xc004af12, VHOST_GET_VRING_BASE());
}
}

View file

@ -10,6 +10,10 @@ extern crate syscall_defines;
#[macro_use]
pub mod handle_eintr;
#[macro_use]
pub mod ioctl;
#[macro_use]
pub mod syslog;
mod mmap;
mod shm;
mod eventfd;
@ -21,8 +25,6 @@ mod struct_util;
mod tempdir;
mod terminal;
mod signal;
#[macro_use]
pub mod syslog;
mod fork;
mod signalfd;
@ -40,6 +42,7 @@ pub use terminal::*;
pub use signal::*;
pub use fork::*;
pub use signalfd::*;
pub use ioctl::*;
pub use guest_memory::Error as GuestMemoryError;
pub use signalfd::Error as SignalFdError;