4
4
/// Module with CPU templates for aarch64
5
5
pub mod static_cpu_templates;
6
6
7
- use kvm_ioctls:: VcpuFd ;
8
7
pub use static_cpu_templates:: * ;
9
8
10
- use super :: templates:: { CpuTemplateType , CustomCpuTemplate } ;
9
+ use super :: templates:: CustomCpuTemplate ;
11
10
use crate :: arch:: regs:: { Aarch64Register , Error as ArchError } ;
12
11
13
12
/// Errors thrown while configuring templates.
14
13
#[ derive( Debug , PartialEq , Eq , thiserror:: Error ) ]
15
14
#[ error( "Failed to create a guest cpu configuration: {0}" ) ]
16
- pub struct Error ( #[ from] ArchError ) ;
15
+ pub struct Error ( #[ from] pub ArchError ) ;
17
16
18
17
/// CPU configuration for aarch64
19
18
#[ derive( Debug , Default , Clone , PartialEq , Eq ) ]
@@ -24,56 +23,16 @@ pub struct CpuConfiguration {
24
23
25
24
impl CpuConfiguration {
26
25
/// Creates new guest CPU config based on the provided template
27
- pub fn new ( vcpu : & VcpuFd , template : & Option < CpuTemplateType > ) -> Result < Self , Error > {
28
- match template {
29
- Some ( ref cpu_template) => Self :: with_template ( vcpu, cpu_template) ,
30
- None => Ok ( Self :: default ( ) ) ,
26
+ pub fn apply_template ( mut self , template : & CustomCpuTemplate ) -> Result < Self , Error > {
27
+ for ( modifier, reg) in template. reg_modifiers . iter ( ) . zip ( self . regs . iter_mut ( ) ) {
28
+ reg. value = modifier. bitmap . apply ( reg. value ) ;
31
29
}
32
- }
33
-
34
- /// Creates new guest CPU config based on the provided template
35
- fn with_template ( vcpu : & VcpuFd , template : & CpuTemplateType ) -> Result < Self , Error > {
36
- match template {
37
- CpuTemplateType :: Custom ( template) => Self :: with_applied_template ( vcpu, template) ,
38
- CpuTemplateType :: Static ( StaticCpuTemplate :: V1N1 ) => {
39
- let template = static_cpu_templates:: v1n1:: v1n1 ( ) ;
40
- Self :: with_applied_template ( vcpu, & template)
41
- }
42
- _ => unreachable ! ( "Options other than V1N1 are invalid" ) ,
43
- }
44
- }
45
-
46
- /// Creates new guest CPU config based on the provided template
47
- fn with_applied_template ( vcpu : & VcpuFd , template : & CustomCpuTemplate ) -> Result < Self , Error > {
48
- let regs = template
49
- . reg_modifiers
50
- . iter ( )
51
- . map ( |modifier| {
52
- vcpu. get_one_reg ( modifier. addr )
53
- . map ( |value| Aarch64Register {
54
- id : modifier. addr ,
55
- value : modifier. bitmap . apply ( value) ,
56
- } )
57
- . map_err ( |e| Error ( ArchError :: GetSysRegister ( modifier. addr , e) ) )
58
- } )
59
- . collect :: < Result < Vec < _ > , Error > > ( ) ?;
60
-
61
- Ok ( Self { regs } )
30
+ Ok ( self )
62
31
}
63
32
64
33
/// Returns ids of registers that are changed
65
34
/// by this template
66
35
pub fn register_ids ( & self ) -> Vec < u64 > {
67
36
self . regs . iter ( ) . map ( |reg| reg. id ) . collect ( )
68
37
}
69
-
70
- /// Applies cpu template to vcpu
71
- /// Used inside Vcpu to configure it
72
- pub fn apply ( & self , vcpu : & VcpuFd ) -> Result < ( ) , ArchError > {
73
- for Aarch64Register { id, value } in self . regs . iter ( ) {
74
- vcpu. set_one_reg ( * id, * value)
75
- . map_err ( |error| ArchError :: SetSysRegister ( * id, error) ) ?;
76
- }
77
- Ok ( ( ) )
78
- }
79
38
}
0 commit comments