Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/kvm_sys/tests/basic.rs
5392 views
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(target_os = "windows"))]
6
7
use std::ffi::CStr;
8
9
use kvm_sys::*;
10
use libc::c_char;
11
use libc::ioctl;
12
use libc::open64;
13
use libc::O_RDWR;
14
15
const KVM_PATH: &CStr = c"/dev/kvm";
16
17
#[test]
18
fn get_version() {
19
// SAFETY: KVM_PATH is expected to be valid and return value is checked.
20
let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
21
assert!(sys_fd >= 0);
22
23
// SAFETY: sys_fd is expected to be valid and return value is checked.
24
let ret = unsafe { ioctl(sys_fd, KVM_GET_API_VERSION, 0) };
25
assert_eq!(ret as u32, KVM_API_VERSION);
26
}
27
28
#[test]
29
fn create_vm_fd() {
30
// SAFETY: KVM_PATH is expected to be valid and return value is checked.
31
let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
32
assert!(sys_fd >= 0);
33
34
// SAFETY: sys_fd is expected to be valid and return value is checked.
35
let vm_fd = unsafe { ioctl(sys_fd, KVM_CREATE_VM, 0) };
36
assert!(vm_fd >= 0);
37
}
38
39
#[test]
40
fn check_vm_extension() {
41
// SAFETY: KVM_PATH is expected to be valid and return value is checked.
42
let sys_fd = unsafe { open64(KVM_PATH.as_ptr() as *const c_char, O_RDWR) };
43
assert!(sys_fd >= 0);
44
45
// SAFETY: sys_fd is expected to be valid and return value is checked.
46
let has_user_memory = unsafe { ioctl(sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY) };
47
assert_eq!(has_user_memory, 1);
48
}
49
50