xref: /aosp_15_r20/external/crosvm/kvm_sys/tests/basic.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2017 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #![cfg(not(any(target_os = "windows", target_arch = "arm")))]
6 
7 use kvm_sys::*;
8 use libc::c_char;
9 use libc::ioctl;
10 use libc::open64;
11 use libc::O_RDWR;
12 
13 const KVM_PATH: &str = "/dev/kvm\0";
14 
15 #[test]
get_version()16 fn get_version() {
17     // SAFETY: KVM_PATH is expected to be valid and return value is checked.
18     let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
19     assert!(sys_fd >= 0);
20 
21     // SAFETY: sys_fd is expected to be valid and return value is checked.
22     let ret = unsafe { ioctl(sys_fd, KVM_GET_API_VERSION, 0) };
23     assert_eq!(ret as u32, KVM_API_VERSION);
24 }
25 
26 #[test]
create_vm_fd()27 fn create_vm_fd() {
28     // SAFETY: KVM_PATH is expected to be valid and return value is checked.
29     let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
30     assert!(sys_fd >= 0);
31 
32     // SAFETY: sys_fd is expected to be valid and return value is checked.
33     let vm_fd = unsafe { ioctl(sys_fd, KVM_CREATE_VM, 0) };
34     assert!(vm_fd >= 0);
35 }
36 
37 #[test]
check_vm_extension()38 fn check_vm_extension() {
39     // SAFETY: KVM_PATH is expected to be valid and return value is checked.
40     let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
41     assert!(sys_fd >= 0);
42 
43     // SAFETY: sys_fd is expected to be valid and return value is checked.
44     let has_user_memory = unsafe { ioctl(sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY) };
45     assert_eq!(has_user_memory, 1);
46 }
47