@@ -11,24 +11,56 @@ use serde::de::Error as SerdeError;
11
11
use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
12
12
13
13
use super :: { CpuTemplateType , GetCpuTemplate , GetCpuTemplateError , StaticCpuTemplate } ;
14
+ use crate :: guest_config:: cpuid:: common:: get_vendor_id_from_host;
14
15
use crate :: guest_config:: cpuid:: cpuid_ffi:: KvmCpuidFlags ;
16
+ use crate :: guest_config:: cpuid:: { VENDOR_ID_AMD , VENDOR_ID_INTEL } ;
15
17
use crate :: guest_config:: x86_64:: static_cpu_templates_new:: { c3, t2, t2a, t2cl, t2s} ;
16
18
17
19
impl GetCpuTemplate for Option < CpuTemplateType > {
18
20
fn get_cpu_template ( & self ) -> Result < Cow < CustomCpuTemplate > , GetCpuTemplateError > {
21
+ use GetCpuTemplateError :: * ;
22
+
19
23
match self {
20
24
Some ( template_type) => match template_type {
21
25
CpuTemplateType :: Custom ( template) => Ok ( Cow :: Borrowed ( template) ) ,
22
- CpuTemplateType :: Static ( template) => match template {
23
- StaticCpuTemplate :: C3 => Ok ( Cow :: Owned ( c3:: c3 ( ) ) ) ,
24
- StaticCpuTemplate :: T2 => Ok ( Cow :: Owned ( t2:: t2 ( ) ) ) ,
25
- StaticCpuTemplate :: T2S => Ok ( Cow :: Owned ( t2s:: t2s ( ) ) ) ,
26
- StaticCpuTemplate :: T2CL => Ok ( Cow :: Owned ( t2cl:: t2cl ( ) ) ) ,
27
- StaticCpuTemplate :: T2A => Ok ( Cow :: Owned ( t2a:: t2a ( ) ) ) ,
28
- StaticCpuTemplate :: None => Err ( GetCpuTemplateError :: InvalidStaticCpuTemplate (
29
- StaticCpuTemplate :: None ,
30
- ) ) ,
31
- } ,
26
+ CpuTemplateType :: Static ( template) => {
27
+ let vendor_id = get_vendor_id_from_host ( ) . map_err ( GetCpuVendor ) ?;
28
+ match template {
29
+ StaticCpuTemplate :: C3 => {
30
+ if & vendor_id != VENDOR_ID_INTEL {
31
+ return Err ( CpuVendorMismatched ) ;
32
+ }
33
+ Ok ( Cow :: Owned ( c3:: c3 ( ) ) )
34
+ }
35
+ StaticCpuTemplate :: T2 => {
36
+ if & vendor_id != VENDOR_ID_INTEL {
37
+ return Err ( CpuVendorMismatched ) ;
38
+ }
39
+ Ok ( Cow :: Owned ( t2:: t2 ( ) ) )
40
+ }
41
+ StaticCpuTemplate :: T2S => {
42
+ if & vendor_id != VENDOR_ID_INTEL {
43
+ return Err ( CpuVendorMismatched ) ;
44
+ }
45
+ Ok ( Cow :: Owned ( t2s:: t2s ( ) ) )
46
+ }
47
+ StaticCpuTemplate :: T2CL => {
48
+ if & vendor_id != VENDOR_ID_INTEL {
49
+ return Err ( CpuVendorMismatched ) ;
50
+ }
51
+ Ok ( Cow :: Owned ( t2cl:: t2cl ( ) ) )
52
+ }
53
+ StaticCpuTemplate :: T2A => {
54
+ if & vendor_id != VENDOR_ID_AMD {
55
+ return Err ( CpuVendorMismatched ) ;
56
+ }
57
+ Ok ( Cow :: Owned ( t2a:: t2a ( ) ) )
58
+ }
59
+ StaticCpuTemplate :: None => {
60
+ Err ( InvalidStaticCpuTemplate ( StaticCpuTemplate :: None ) )
61
+ }
62
+ }
63
+ }
32
64
} ,
33
65
None => Ok ( Cow :: Owned ( CustomCpuTemplate :: default ( ) ) ) ,
34
66
}
@@ -478,56 +510,91 @@ mod tests {
478
510
#[ test]
479
511
fn test_get_cpu_template_with_c3_static_template ( ) {
480
512
// Test `get_cpu_template()` when C3 static CPU template is specified. The owned
481
- // `CustomCpuTemplate` should be returned.
513
+ // `CustomCpuTemplate` should be returned if CPU vendor is Intel. Otherwise, it should fail .
482
514
let cpu_template = Some ( CpuTemplateType :: Static ( StaticCpuTemplate :: C3 ) ) ;
483
- assert_eq ! (
484
- cpu_template. get_cpu_template( ) . unwrap( ) ,
485
- Cow :: Owned ( c3:: c3( ) )
486
- ) ;
515
+ if & get_vendor_id_from_host ( ) . unwrap ( ) == VENDOR_ID_INTEL {
516
+ assert_eq ! (
517
+ cpu_template. get_cpu_template( ) . unwrap( ) ,
518
+ Cow :: Owned ( c3:: c3( ) )
519
+ ) ;
520
+ } else {
521
+ assert_eq ! (
522
+ cpu_template. get_cpu_template( ) . unwrap_err( ) ,
523
+ GetCpuTemplateError :: CpuVendorMismatched ,
524
+ ) ;
525
+ }
487
526
}
488
527
489
528
#[ test]
490
529
fn test_get_cpu_template_with_t2_static_template ( ) {
491
530
// Test `get_cpu_template()` when T2 static CPU template is specified. The owned
492
- // `CustomCpuTemplate` should be returned.
531
+ // `CustomCpuTemplate` should be returned if CPU vendor is Intel. Otherwise, it should fail .
493
532
let cpu_template = Some ( CpuTemplateType :: Static ( StaticCpuTemplate :: T2 ) ) ;
494
- assert_eq ! (
495
- cpu_template. get_cpu_template( ) . unwrap( ) ,
496
- Cow :: Owned ( t2:: t2( ) )
497
- ) ;
533
+ if & get_vendor_id_from_host ( ) . unwrap ( ) == VENDOR_ID_INTEL {
534
+ assert_eq ! (
535
+ cpu_template. get_cpu_template( ) . unwrap( ) ,
536
+ Cow :: Owned ( t2:: t2( ) )
537
+ ) ;
538
+ } else {
539
+ assert_eq ! (
540
+ cpu_template. get_cpu_template( ) . unwrap_err( ) ,
541
+ GetCpuTemplateError :: CpuVendorMismatched ,
542
+ ) ;
543
+ }
498
544
}
499
545
500
546
#[ test]
501
547
fn test_get_cpu_template_with_t2s_static_template ( ) {
502
548
// Test `get_cpu_template()` when T2S static CPU template is specified. The owned
503
- // `CustomCpuTemplate` should be returned.
549
+ // `CustomCpuTemplate` should be returned if CPU vendor is Intel. Otherwise, it should fail .
504
550
let cpu_template = Some ( CpuTemplateType :: Static ( StaticCpuTemplate :: T2S ) ) ;
505
- assert_eq ! (
506
- cpu_template. get_cpu_template( ) . unwrap( ) ,
507
- Cow :: Owned ( t2s:: t2s( ) )
508
- ) ;
551
+ if & get_vendor_id_from_host ( ) . unwrap ( ) == VENDOR_ID_INTEL {
552
+ assert_eq ! (
553
+ cpu_template. get_cpu_template( ) . unwrap( ) ,
554
+ Cow :: Owned ( t2s:: t2s( ) )
555
+ ) ;
556
+ } else {
557
+ assert_eq ! (
558
+ cpu_template. get_cpu_template( ) . unwrap_err( ) ,
559
+ GetCpuTemplateError :: CpuVendorMismatched ,
560
+ ) ;
561
+ }
509
562
}
510
563
511
564
#[ test]
512
565
fn test_get_cpu_template_with_t2cl_static_template ( ) {
513
566
// Test `get_cpu_template()` when T2CL static CPU template is specified. The owned
514
- // `CustomCpuTemplate` should be returned.
567
+ // `CustomCpuTemplate` should be returned if CPU vendor is Intel. Otherwise, it should fail .
515
568
let cpu_template = Some ( CpuTemplateType :: Static ( StaticCpuTemplate :: T2CL ) ) ;
516
- assert_eq ! (
517
- cpu_template. get_cpu_template( ) . unwrap( ) ,
518
- Cow :: Owned ( t2cl:: t2cl( ) )
519
- ) ;
569
+ if & get_vendor_id_from_host ( ) . unwrap ( ) == VENDOR_ID_INTEL {
570
+ assert_eq ! (
571
+ cpu_template. get_cpu_template( ) . unwrap( ) ,
572
+ Cow :: Owned ( t2cl:: t2cl( ) )
573
+ ) ;
574
+ } else {
575
+ assert_eq ! (
576
+ cpu_template. get_cpu_template( ) . unwrap_err( ) ,
577
+ GetCpuTemplateError :: CpuVendorMismatched ,
578
+ ) ;
579
+ }
520
580
}
521
581
522
582
#[ test]
523
583
fn test_get_cpu_template_with_t2a_static_template ( ) {
524
584
// Test `get_cpu_template()` when T2A static CPU template is specified. The owned
525
- // `CustomCpuTemplate` should be returned.
585
+ // `CustomCpuTemplate` should be returned if CPU vendor is AMD. Otherwise it should fail .
526
586
let cpu_template = Some ( CpuTemplateType :: Static ( StaticCpuTemplate :: T2A ) ) ;
527
- assert_eq ! (
528
- cpu_template. get_cpu_template( ) . unwrap( ) ,
529
- Cow :: Owned ( t2a:: t2a( ) )
530
- ) ;
587
+ if & get_vendor_id_from_host ( ) . unwrap ( ) == VENDOR_ID_AMD {
588
+ assert_eq ! (
589
+ cpu_template. get_cpu_template( ) . unwrap( ) ,
590
+ Cow :: Owned ( t2a:: t2a( ) )
591
+ ) ;
592
+ } else {
593
+ assert_eq ! (
594
+ cpu_template. get_cpu_template( ) . unwrap_err( ) ,
595
+ GetCpuTemplateError :: CpuVendorMismatched ,
596
+ ) ;
597
+ }
531
598
}
532
599
533
600
#[ test]
0 commit comments