@@ -41,7 +41,8 @@ impl Kvm {
41
41
pub fn new ( ) -> Result < Self > {
42
42
// Open `/dev/kvm` using `O_CLOEXEC` flag.
43
43
let fd = Self :: open_with_cloexec ( true ) ?;
44
- // Safe because we verify that the fd is valid in `open_with_cloexec` and we own the fd.
44
+ // SAFETY: Safe because we verify that the fd is valid in `open_with_cloexec` and we own
45
+ // the fd.
45
46
Ok ( unsafe { Self :: from_raw_fd ( fd) } )
46
47
}
47
48
@@ -68,7 +69,7 @@ impl Kvm {
68
69
/// ```
69
70
pub fn open_with_cloexec ( close_on_exec : bool ) -> Result < RawFd > {
70
71
let open_flags = O_RDWR | if close_on_exec { O_CLOEXEC } else { 0 } ;
71
- // Safe because we give a constant nul-terminated string and verify the result.
72
+ // SAFETY: Safe because we give a constant nul-terminated string and verify the result.
72
73
let ret = unsafe { open ( "/dev/kvm\0 " . as_ptr ( ) as * const c_char , open_flags) } ;
73
74
if ret < 0 {
74
75
Err ( errno:: Error :: last ( ) )
@@ -89,8 +90,8 @@ impl Kvm {
89
90
/// assert_eq!(kvm.get_api_version(), 12);
90
91
/// ```
91
92
pub fn get_api_version ( & self ) -> i32 {
92
- // Safe because we know that our file is a KVM fd and that the request is one of the ones
93
- // defined by kernel.
93
+ // SAFETY: Safe because we know that our file is a KVM fd and that the request is one of
94
+ // the ones defined by kernel.
94
95
unsafe { ioctl ( self , KVM_GET_API_VERSION ( ) ) }
95
96
}
96
97
@@ -137,8 +138,8 @@ impl Kvm {
137
138
/// assert!(kvm.check_extension_int(Cap::MaxVcpuId) > 0);
138
139
/// ```
139
140
pub fn check_extension_int ( & self , c : Cap ) -> i32 {
140
- // Safe because we know that our file is a KVM fd and that the extension is one of the ones
141
- // defined by kernel.
141
+ // SAFETY: Safe because we know that our file is a KVM fd and that the extension is one of
142
+ // the ones defined by kernel.
142
143
unsafe { ioctl_with_val ( self , KVM_CHECK_EXTENSION ( ) , c as c_ulong ) }
143
144
}
144
145
@@ -177,7 +178,7 @@ impl Kvm {
177
178
/// assert!(kvm.get_vcpu_mmap_size().unwrap() > 0);
178
179
/// ```
179
180
pub fn get_vcpu_mmap_size ( & self ) -> Result < usize > {
180
- // Safe because we know that our file is a KVM fd and we verify the return result.
181
+ // SAFETY: Safe because we know that our file is a KVM fd and we verify the return result.
181
182
let res = unsafe { ioctl ( self , KVM_GET_VCPU_MMAP_SIZE ( ) ) } ;
182
183
if res > 0 {
183
184
Ok ( res as usize )
@@ -279,11 +280,10 @@ impl Kvm {
279
280
}
280
281
281
282
let mut cpuid = CpuId :: new ( num_entries) . map_err ( |_| errno:: Error :: new ( libc:: ENOMEM ) ) ?;
282
-
283
+ // SAFETY: The kernel is trusted not to write beyond the bounds of the memory
284
+ // allocated for the struct. The limit is read from nent, which is set to the allocated
285
+ // size(num_entries) above.
283
286
let ret = unsafe {
284
- // ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
285
- // allocated for the struct. The limit is read from nent, which is set to the allocated
286
- // size(num_entries) above.
287
287
ioctl_with_mut_ptr ( self , kind, cpuid. as_mut_fam_struct_ptr ( ) )
288
288
} ;
289
289
if ret < 0 {
@@ -368,10 +368,10 @@ impl Kvm {
368
368
let mut msr_list =
369
369
MsrList :: new ( KVM_MAX_MSR_ENTRIES ) . map_err ( |_| errno:: Error :: new ( libc:: ENOMEM ) ) ?;
370
370
371
+ // SAFETY: The kernel is trusted not to write beyond the bounds of the memory
372
+ // allocated for the struct. The limit is read from nmsrs, which is set to the allocated
373
+ // size (MAX_KVM_MSR_ENTRIES) above.
371
374
let ret = unsafe {
372
- // ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
373
- // allocated for the struct. The limit is read from nmsrs, which is set to the allocated
374
- // size (MAX_KVM_MSR_ENTRIES) above.
375
375
ioctl_with_mut_ptr (
376
376
self ,
377
377
KVM_GET_MSR_INDEX_LIST ( ) ,
@@ -483,11 +483,11 @@ impl Kvm {
483
483
/// assert!(vm.run_size() == kvm.get_vcpu_mmap_size().unwrap());
484
484
/// ```
485
485
pub fn create_vm_with_type ( & self , vm_type : u64 ) -> Result < VmFd > {
486
- // Safe because we know `self.kvm` is a real KVM fd as this module is the only one that
487
- // create Kvm objects.
486
+ // SAFETY: Safe because we know `self.kvm` is a real KVM fd as this module is the only one
487
+ // that create Kvm objects.
488
488
let ret = unsafe { ioctl_with_val ( & self . kvm , KVM_CREATE_VM ( ) , vm_type) } ;
489
489
if ret >= 0 {
490
- // Safe because we verify the value of ret and we are the owners of the fd.
490
+ // SAFETY: Safe because we verify the value of ret and we are the owners of the fd.
491
491
let vm_file = unsafe { File :: from_raw_fd ( ret) } ;
492
492
let run_mmap_size = self . get_vcpu_mmap_size ( ) ?;
493
493
Ok ( new_vmfd ( vm_file, run_mmap_size) )
@@ -572,6 +572,7 @@ impl FromRawFd for Kvm {
572
572
573
573
#[ cfg( test) ]
574
574
mod tests {
575
+ #![ allow( clippy:: undocumented_unsafe_blocks) ]
575
576
use super :: * ;
576
577
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
577
578
use kvm_bindings:: KVM_MAX_CPUID_ENTRIES ;
0 commit comments