Skip to content

Commit b172068

Browse files
committed
kvm: refactor kvm and kvm_gen
- move autogenerated code from kvm_gen to kvm - sanity.rs is now unit test inside a module of the kvm crate - label x86 and x86_64 specific code - remove x86 clasification inside kvm_gen since the bindings are common to all architectures Signed-off-by: Diana Popa <[email protected]>
1 parent d62c09d commit b172068

File tree

5 files changed

+132
-113
lines changed

5 files changed

+132
-113
lines changed

kvm/src/ioctl_wrap.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use kvm_gen::*;
5+
6+
// Declares necessary ioctls specific to their platform.
7+
8+
ioctl_io_nr!(KVM_GET_API_VERSION, KVMIO, 0x00);
9+
ioctl_io_nr!(KVM_CREATE_VM, KVMIO, 0x01);
10+
ioctl_io_nr!(KVM_CHECK_EXTENSION, KVMIO, 0x03);
11+
ioctl_io_nr!(KVM_GET_VCPU_MMAP_SIZE, KVMIO, 0x04);
12+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13+
ioctl_iowr_nr!(KVM_GET_SUPPORTED_CPUID, KVMIO, 0x05, kvm_cpuid2);
14+
ioctl_io_nr!(KVM_CREATE_VCPU, KVMIO, 0x41);
15+
ioctl_iow_nr!(
16+
KVM_SET_USER_MEMORY_REGION,
17+
KVMIO,
18+
0x46,
19+
kvm_userspace_memory_region
20+
);
21+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
22+
ioctl_io_nr!(KVM_SET_TSS_ADDR, KVMIO, 0x47);
23+
#[cfg(any(
24+
target_arch = "x86",
25+
target_arch = "x86_64",
26+
target_arch = "arm",
27+
target_arch = "aarch64",
28+
target_arch = "s390"
29+
))]
30+
ioctl_io_nr!(KVM_CREATE_IRQCHIP, KVMIO, 0x60);
31+
#[cfg(any(
32+
target_arch = "x86",
33+
target_arch = "x86_64",
34+
target_arch = "arm",
35+
target_arch = "aarch64",
36+
target_arch = "s390"
37+
))]
38+
ioctl_iow_nr!(KVM_IRQFD, KVMIO, 0x76, kvm_irqfd);
39+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
40+
ioctl_iow_nr!(KVM_CREATE_PIT2, KVMIO, 0x77, kvm_pit_config);
41+
ioctl_iow_nr!(KVM_IOEVENTFD, KVMIO, 0x79, kvm_ioeventfd);
42+
ioctl_io_nr!(KVM_RUN, KVMIO, 0x80);
43+
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
44+
ioctl_ior_nr!(KVM_GET_REGS, KVMIO, 0x81, kvm_regs);
45+
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
46+
ioctl_iow_nr!(KVM_SET_REGS, KVMIO, 0x82, kvm_regs);
47+
#[cfg(any(
48+
target_arch = "x86",
49+
target_arch = "x86_64",
50+
target_arch = "powerpc",
51+
target_arch = "powerpc64"
52+
))]
53+
ioctl_ior_nr!(KVM_GET_SREGS, KVMIO, 0x83, kvm_sregs);
54+
#[cfg(any(
55+
target_arch = "x86",
56+
target_arch = "x86_64",
57+
target_arch = "powerpc",
58+
target_arch = "powerpc64"
59+
))]
60+
ioctl_iow_nr!(KVM_SET_SREGS, KVMIO, 0x84, kvm_sregs);
61+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
62+
ioctl_iowr_nr!(KVM_GET_MSRS, KVMIO, 0x88, kvm_msrs);
63+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
64+
ioctl_iow_nr!(KVM_SET_MSRS, KVMIO, 0x89, kvm_msrs);
65+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
66+
ioctl_iow_nr!(KVM_SET_CPUID2, KVMIO, 0x90, kvm_cpuid2);
67+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
68+
ioctl_ior_nr!(KVM_GET_FPU, KVMIO, 0x8c, kvm_fpu);
69+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
70+
ioctl_iow_nr!(KVM_SET_FPU, KVMIO, 0x8d, kvm_fpu);
71+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
72+
ioctl_ior_nr!(KVM_GET_LAPIC, KVMIO, 0x8e, kvm_lapic_state);
73+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
74+
ioctl_iow_nr!(KVM_SET_LAPIC, KVMIO, 0x8f, kvm_lapic_state);
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use std::fs::File;
79+
use std::os::unix::io::FromRawFd;
80+
81+
use libc::{c_char, open, O_RDWR};
82+
83+
use super::*;
84+
use sys_util::{ioctl, ioctl_with_val};
85+
86+
const KVM_PATH: &'static str = "/dev/kvm\0";
87+
88+
#[test]
89+
fn get_version() {
90+
let sys_fd = unsafe { open(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
91+
assert!(sys_fd >= 0);
92+
93+
let ret = unsafe { ioctl(&File::from_raw_fd(sys_fd), KVM_GET_API_VERSION()) };
94+
assert_eq!(ret as u32, KVM_API_VERSION);
95+
}
96+
97+
#[test]
98+
fn create_vm_fd() {
99+
let sys_fd = unsafe { open(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
100+
assert!(sys_fd >= 0);
101+
102+
let vm_fd = unsafe { ioctl(&File::from_raw_fd(sys_fd), KVM_CREATE_VM()) };
103+
assert!(vm_fd >= 0);
104+
}
105+
106+
#[test]
107+
fn check_vm_extension() {
108+
let sys_fd = unsafe { open(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
109+
assert!(sys_fd >= 0);
110+
111+
let has_user_memory = unsafe {
112+
ioctl_with_val(
113+
&File::from_raw_fd(sys_fd),
114+
KVM_CHECK_EXTENSION(),
115+
KVM_CAP_USER_MEMORY.into(),
116+
)
117+
};
118+
assert_eq!(has_user_memory, 1);
119+
}
120+
}

kvm/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ extern crate libc;
1313

1414
extern crate kvm_gen;
1515
extern crate memory_model;
16+
#[macro_use]
1617
extern crate sys_util;
1718

1819
mod cap;
20+
mod ioctl_wrap;
1921

2022
use std::fs::File;
2123
use std::os::raw::*;
@@ -26,11 +28,12 @@ use libc::{open, EINVAL, ENOSPC, O_RDWR};
2628
use kvm_gen::*;
2729
use memory_model::MemoryMapping;
2830
use sys_util::{errno_result, Error, EventFd, Result};
29-
use sys_util::{
30-
ioctl, ioctl_with_mut_ptr, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref, ioctl_with_val,
31-
};
31+
use sys_util::{ioctl, ioctl_with_ref, ioctl_with_val};
32+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
33+
use sys_util::{ioctl_with_mut_ptr, ioctl_with_mut_ref, ioctl_with_ptr};
3234

3335
pub use cap::*;
36+
use ioctl_wrap::*;
3437
pub use kvm_gen::KVM_API_VERSION;
3538

3639
/// Taken from Linux Kernel v4.14.13 (arch/x86/include/asm/kvm_host.h)
@@ -161,9 +164,11 @@ impl Kvm {
161164
// Safe because we verify the value of ret and we are the owners of the fd.
162165
let vm_file = unsafe { File::from_raw_fd(ret) };
163166
let run_mmap_size = self.get_vcpu_mmap_size()?;
167+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
164168
let kvm_cpuid: CpuId = self.get_supported_cpuid(MAX_KVM_CPUID_ENTRIES)?;
165169
Ok(VmFd {
166170
vm: vm_file,
171+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
167172
supported_cpuid: kvm_cpuid,
168173
run_size: run_mmap_size,
169174
})
@@ -197,11 +202,13 @@ impl Into<u64> for NoDatamatch {
197202
/// A wrapper around creating and using a VM.
198203
pub struct VmFd {
199204
vm: File,
205+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
200206
supported_cpuid: CpuId,
201207
run_size: usize,
202208
}
203209

204210
impl VmFd {
211+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
205212
/// Returns a clone of the system supported CPUID values associated with this VmFd
206213
pub fn get_supported_cpuid(&self) -> CpuId {
207214
self.supported_cpuid.clone()
File renamed without changes.

kvm_gen/src/lib.rs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,8 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
//
4-
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
5-
// Use of this source code is governed by a BSD-style license that can be
6-
// found in the THIRD-PARTY file.
73

84
#![allow(non_upper_case_globals)]
95
#![allow(non_camel_case_types)]
106
#![allow(non_snake_case)]
11-
12-
#[macro_use]
13-
extern crate sys_util;
14-
15-
// Each of the below modules defines ioctls specific to their platform (currently, x86 and x86_64).
16-
17-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
18-
pub mod x86 {
19-
// generated with:
20-
// bindgen include/uapi/linux/kvm.h -o bindings.rs
21-
// --no-rustfmt-bindings
22-
// --with-derive-default
23-
// -- -Iinclude -Iarch/x86/include/
24-
pub mod bindings;
25-
pub use bindings::*;
26-
27-
ioctl_iowr_nr!(KVM_GET_SUPPORTED_CPUID, KVMIO, 0x05, kvm_cpuid2);
28-
ioctl_iowr_nr!(KVM_GET_MSRS, KVMIO, 0x88, kvm_msrs);
29-
ioctl_iow_nr!(KVM_SET_MSRS, KVMIO, 0x89, kvm_msrs);
30-
ioctl_ior_nr!(KVM_GET_LAPIC, KVMIO, 0x8e, kvm_lapic_state);
31-
ioctl_iow_nr!(KVM_SET_LAPIC, KVMIO, 0x8f, kvm_lapic_state);
32-
ioctl_iow_nr!(KVM_SET_CPUID2, KVMIO, 0x90, kvm_cpuid2);
33-
// we do use KVM_SET_CPUID2, however KVM_GET_CPUID2 is never used
34-
// should be used to unit test the SET!!!
35-
ioctl_iowr_nr!(KVM_GET_CPUID2, KVMIO, 0x91, kvm_cpuid2);
36-
}
37-
38-
// These ioctls are commonly defined on all/multiple platforms.
39-
ioctl_io_nr!(KVM_GET_API_VERSION, KVMIO, 0x00);
40-
ioctl_io_nr!(KVM_CREATE_VM, KVMIO, 0x01);
41-
ioctl_io_nr!(KVM_CHECK_EXTENSION, KVMIO, 0x03);
42-
ioctl_io_nr!(KVM_GET_VCPU_MMAP_SIZE, KVMIO, 0x04);
43-
ioctl_io_nr!(KVM_CREATE_VCPU, KVMIO, 0x41);
44-
ioctl_iow_nr!(
45-
KVM_SET_USER_MEMORY_REGION,
46-
KVMIO,
47-
0x46,
48-
kvm_userspace_memory_region
49-
);
50-
ioctl_io_nr!(KVM_SET_TSS_ADDR, KVMIO, 0x47);
51-
ioctl_io_nr!(KVM_CREATE_IRQCHIP, KVMIO, 0x60);
52-
ioctl_iow_nr!(KVM_IRQFD, KVMIO, 0x76, kvm_irqfd);
53-
ioctl_iow_nr!(KVM_CREATE_PIT2, KVMIO, 0x77, kvm_pit_config);
54-
ioctl_iow_nr!(KVM_IOEVENTFD, KVMIO, 0x79, kvm_ioeventfd);
55-
ioctl_io_nr!(KVM_RUN, KVMIO, 0x80);
56-
ioctl_ior_nr!(KVM_GET_REGS, KVMIO, 0x81, kvm_regs);
57-
ioctl_iow_nr!(KVM_SET_REGS, KVMIO, 0x82, kvm_regs);
58-
ioctl_ior_nr!(KVM_GET_SREGS, KVMIO, 0x83, kvm_sregs);
59-
ioctl_iow_nr!(KVM_SET_SREGS, KVMIO, 0x84, kvm_sregs);
60-
ioctl_ior_nr!(KVM_GET_FPU, KVMIO, 0x8c, kvm_fpu);
61-
ioctl_iow_nr!(KVM_SET_FPU, KVMIO, 0x8d, kvm_fpu);
62-
63-
// Along with the common ioctls, we reexport the ioctls of the current
64-
// platform.
65-
pub use x86::*;
7+
mod bindings;
8+
pub use bindings::*;

kvm_gen/tests/sanity.rs

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)