@@ -15,7 +15,7 @@ use ioctls::Result;
15
15
#[ cfg( any( target_arch = "aarch64" ) ) ]
16
16
use kvm_bindings:: KVM_VM_TYPE_ARM_IPA_SIZE_MASK ;
17
17
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
18
- use kvm_bindings:: { CpuId , MsrList , KVM_MAX_MSR_ENTRIES } ;
18
+ use kvm_bindings:: { CpuId , MsrList , KVM_MAX_CPUID_ENTRIES , KVM_MAX_MSR_ENTRIES } ;
19
19
use kvm_ioctls:: * ;
20
20
use vmm_sys_util:: errno;
21
21
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
@@ -228,13 +228,18 @@ impl Kvm {
228
228
}
229
229
230
230
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
231
- fn get_cpuid ( & self , kind : u64 , max_entries_count : usize ) -> Result < CpuId > {
232
- let mut cpuid = CpuId :: new ( max_entries_count) ;
231
+ fn get_cpuid ( & self , kind : u64 , num_entries : usize ) -> Result < CpuId > {
232
+ if num_entries > KVM_MAX_CPUID_ENTRIES {
233
+ // Returns the same error the underlying `ioctl` would have sent.
234
+ return Err ( errno:: Error :: new ( libc:: ENOMEM ) ) ;
235
+ }
236
+
237
+ let mut cpuid = CpuId :: new ( num_entries) ;
233
238
234
239
let ret = unsafe {
235
240
// ioctl is unsafe. The kernel is trusted not to write beyond the bounds of the memory
236
241
// allocated for the struct. The limit is read from nent, which is set to the allocated
237
- // size(max_entries_count ) above.
242
+ // size(num_entries ) above.
238
243
ioctl_with_mut_ptr ( self , kind, cpuid. as_mut_fam_struct_ptr ( ) )
239
244
} ;
240
245
if ret < 0 {
@@ -250,7 +255,7 @@ impl Kvm {
250
255
///
251
256
/// # Arguments
252
257
///
253
- /// * `max_entries_count ` - Maximum number of CPUID entries. This function can return less than
258
+ /// * `num_entries ` - Maximum number of CPUID entries. This function can return less than
254
259
/// this when the hardware does not support so many CPUID entries.
255
260
///
256
261
/// # Example
@@ -267,8 +272,8 @@ impl Kvm {
267
272
/// ```
268
273
///
269
274
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
270
- pub fn get_emulated_cpuid ( & self , max_entries_count : usize ) -> Result < CpuId > {
271
- self . get_cpuid ( KVM_GET_EMULATED_CPUID ( ) , max_entries_count )
275
+ pub fn get_emulated_cpuid ( & self , num_entries : usize ) -> Result < CpuId > {
276
+ self . get_cpuid ( KVM_GET_EMULATED_CPUID ( ) , num_entries )
272
277
}
273
278
274
279
/// X86 specific call to get the system supported CPUID values.
@@ -277,7 +282,7 @@ impl Kvm {
277
282
///
278
283
/// # Arguments
279
284
///
280
- /// * `max_entries_count ` - Maximum number of CPUID entries. This function can return less than
285
+ /// * `num_entries ` - Maximum number of CPUID entries. This function can return less than
281
286
/// this when the hardware does not support so many CPUID entries.
282
287
///
283
288
/// # Example
@@ -294,8 +299,8 @@ impl Kvm {
294
299
/// ```
295
300
///
296
301
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
297
- pub fn get_supported_cpuid ( & self , max_entries_count : usize ) -> Result < CpuId > {
298
- self . get_cpuid ( KVM_GET_SUPPORTED_CPUID ( ) , max_entries_count )
302
+ pub fn get_supported_cpuid ( & self , num_entries : usize ) -> Result < CpuId > {
303
+ self . get_cpuid ( KVM_GET_SUPPORTED_CPUID ( ) , num_entries )
299
304
}
300
305
301
306
/// X86 specific call to get list of supported MSRS
@@ -593,6 +598,10 @@ mod tests {
593
598
let cpuid_entries = cpuid. as_mut_slice ( ) ;
594
599
assert ! ( !cpuid_entries. is_empty( ) ) ;
595
600
assert ! ( cpuid_entries. len( ) <= KVM_MAX_CPUID_ENTRIES ) ;
601
+
602
+ // Test case for more than MAX entries
603
+ let cpuid_err = kvm. get_emulated_cpuid ( KVM_MAX_CPUID_ENTRIES + 1 as usize ) ;
604
+ assert ! ( cpuid_err. is_err( ) ) ;
596
605
}
597
606
598
607
#[ test]
@@ -603,6 +612,10 @@ mod tests {
603
612
let cpuid_entries = cpuid. as_mut_slice ( ) ;
604
613
assert ! ( !cpuid_entries. is_empty( ) ) ;
605
614
assert ! ( cpuid_entries. len( ) <= KVM_MAX_CPUID_ENTRIES ) ;
615
+
616
+ // Test case for more than MAX entries
617
+ let cpuid_err = kvm. get_emulated_cpuid ( KVM_MAX_CPUID_ENTRIES + 1 as usize ) ;
618
+ assert ! ( cpuid_err. is_err( ) ) ;
606
619
}
607
620
608
621
#[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
0 commit comments