devices: vfio: Avoid arch-dependent type conversion for IommuType

Since ioctl_with_val takes c_ulong, whose size is 64-bit in x86 but
32-bit in ARM, we must not assume it can be converted to u64.

BUG=none
TEST=cargo check

Change-Id: Ib0060bbbe2e06c94bf8802c8e775c808c07ff6a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3320123
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Auto-Submit: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Keiichi Watanabe 2021-12-07 20:31:48 +09:00 committed by Commit Bot
parent bbec8ebf65
commit 0d801c9cd2

View file

@ -8,6 +8,7 @@ use std::ffi::CString;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io; use std::io;
use std::mem; use std::mem;
use std::os::raw::c_ulong;
use std::os::unix::prelude::FileExt; use std::os::unix::prelude::FileExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
@ -90,9 +91,10 @@ enum KvmVfioGroupOps {
Delete, Delete,
} }
#[repr(u32)]
enum IommuType { enum IommuType {
Type1V2 = VFIO_TYPE1v2_IOMMU as isize, Type1V2 = VFIO_TYPE1v2_IOMMU,
NoIommu = VFIO_NOIOMMU_IOMMU as isize, NoIommu = VFIO_NOIOMMU_IOMMU,
} }
/// VfioContainer contain multi VfioGroup, and delegate an IOMMU domain table /// VfioContainer contain multi VfioGroup, and delegate an IOMMU domain table
@ -140,13 +142,13 @@ impl VfioContainer {
fn check_extension(&self, val: IommuType) -> bool { fn check_extension(&self, val: IommuType) -> bool {
// Safe as file is vfio container and make sure val is valid. // Safe as file is vfio container and make sure val is valid.
let ret = unsafe { ioctl_with_val(self, VFIO_CHECK_EXTENSION(), val as u64) }; let ret = unsafe { ioctl_with_val(self, VFIO_CHECK_EXTENSION(), val as c_ulong) };
ret == 1 ret == 1
} }
fn set_iommu(&self, val: IommuType) -> i32 { fn set_iommu(&self, val: IommuType) -> i32 {
// Safe as file is vfio container and make sure val is valid. // Safe as file is vfio container and make sure val is valid.
unsafe { ioctl_with_val(self, VFIO_SET_IOMMU(), val as u64) } unsafe { ioctl_with_val(self, VFIO_SET_IOMMU(), val as c_ulong) }
} }
pub unsafe fn vfio_dma_map( pub unsafe fn vfio_dma_map(