2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
use kvm_bindings:: KVM_API_VERSION ;
5
- #[ cfg( target_arch = "x86_64" ) ]
6
- use kvm_bindings:: { CpuId , KVM_MAX_CPUID_ENTRIES , MsrList } ;
7
5
use kvm_ioctls:: Kvm as KvmFd ;
8
6
use serde:: { Deserialize , Serialize } ;
9
7
10
- #[ cfg( target_arch = "x86_64" ) ]
11
- use crate :: arch:: x86_64:: xstate:: { XstateError , request_dynamic_xstate_features} ;
12
8
use crate :: cpu_config:: templates:: KvmCapability ;
13
9
use crate :: vstate:: memory:: { GuestMemory , GuestMemoryMmap } ;
14
10
11
+ #[ cfg( target_arch = "aarch64" ) ]
12
+ mod aarch64;
13
+ #[ cfg( target_arch = "x86_64" ) ]
14
+ mod x86_64;
15
+
16
+ #[ cfg( target_arch = "aarch64" ) ]
17
+ pub use aarch64:: { Kvm , KvmArchError , OptionalCapabilities } ;
18
+ #[ cfg( target_arch = "x86_64" ) ]
19
+ pub use x86_64:: { Kvm , KvmArchError } ;
20
+
15
21
/// Errors associated with the wrappers over KVM ioctls.
16
22
/// Needs `rustfmt::skip` to make multiline comments work
17
23
#[ rustfmt:: skip]
@@ -24,29 +30,10 @@ pub enum KvmError {
24
30
/** Error creating KVM object: {0} Make sure the user launching the firecracker process is \
25
31
configured on the /dev/kvm file's ACL. */
26
32
Kvm ( kvm_ioctls:: Error ) ,
27
- #[ cfg( target_arch = "x86_64" ) ]
28
- /// Failed to get supported cpuid: {0}
29
- GetSupportedCpuId ( kvm_ioctls:: Error ) ,
30
33
/// The number of configured slots is bigger than the maximum reported by KVM
31
34
NotEnoughMemorySlots ,
32
- #[ cfg( target_arch = "x86_64" ) ]
33
- /// Failed to request permission for dynamic XSTATE features: {0}
34
- XstateFeatures ( XstateError ) ,
35
- }
36
-
37
- /// Struct with kvm fd and kvm associated paramenters.
38
- #[ derive( Debug ) ]
39
- pub struct Kvm {
40
- /// KVM fd.
41
- pub fd : KvmFd ,
42
- /// Maximum number of memory slots allowed by KVM.
43
- pub max_memslots : usize ,
44
- /// Additional capabilities that were specified in cpu template.
45
- pub kvm_cap_modifiers : Vec < KvmCapability > ,
46
-
47
- #[ cfg( target_arch = "x86_64" ) ]
48
- /// Supported CpuIds.
49
- pub supported_cpuid : CpuId ,
35
+ /// Architecture specific error: {0}
36
+ ArchError ( #[ from] KvmArchError )
50
37
}
51
38
52
39
impl Kvm {
@@ -67,36 +54,7 @@ impl Kvm {
67
54
68
55
let max_memslots = kvm_fd. get_nr_memslots ( ) ;
69
56
70
- #[ cfg( target_arch = "aarch64" ) ]
71
- {
72
- Ok ( Self {
73
- fd : kvm_fd,
74
- max_memslots,
75
- kvm_cap_modifiers,
76
- } )
77
- }
78
-
79
- #[ cfg( target_arch = "x86_64" ) ]
80
- {
81
- request_dynamic_xstate_features ( ) . map_err ( KvmError :: XstateFeatures ) ?;
82
-
83
- let supported_cpuid = kvm_fd
84
- . get_supported_cpuid ( KVM_MAX_CPUID_ENTRIES )
85
- . map_err ( KvmError :: GetSupportedCpuId ) ?;
86
-
87
- Ok ( Kvm {
88
- fd : kvm_fd,
89
- max_memslots,
90
- kvm_cap_modifiers,
91
- supported_cpuid,
92
- } )
93
- }
94
- }
95
-
96
- /// Msrs needed to be saved on snapshot creation.
97
- #[ cfg( target_arch = "x86_64" ) ]
98
- pub fn msrs_to_save ( & self ) -> Result < MsrList , crate :: arch:: x86_64:: msr:: MsrError > {
99
- crate :: arch:: x86_64:: msr:: get_msrs_to_save ( & self . fd )
57
+ Ok ( Kvm :: init_arch ( kvm_fd, max_memslots, kvm_cap_modifiers) ?)
100
58
}
101
59
102
60
/// Check guest memory does not have more regions than kvm allows.
@@ -144,55 +102,6 @@ impl Kvm {
144
102
}
145
103
}
146
104
}
147
- #[ cfg( target_arch = "aarch64" ) ]
148
- /// Optional capabilities.
149
- #[ derive( Debug , Default ) ]
150
- pub struct OptionalCapabilities {
151
- /// KVM_CAP_COUNTER_OFFSET
152
- pub counter_offset : bool ,
153
- }
154
- #[ cfg( target_arch = "aarch64" ) ]
155
- impl Kvm {
156
- const DEFAULT_CAPABILITIES : [ u32 ; 7 ] = [
157
- kvm_bindings:: KVM_CAP_IOEVENTFD ,
158
- kvm_bindings:: KVM_CAP_IRQFD ,
159
- kvm_bindings:: KVM_CAP_USER_MEMORY ,
160
- kvm_bindings:: KVM_CAP_ARM_PSCI_0_2 ,
161
- kvm_bindings:: KVM_CAP_DEVICE_CTRL ,
162
- kvm_bindings:: KVM_CAP_MP_STATE ,
163
- kvm_bindings:: KVM_CAP_ONE_REG ,
164
- ] ;
165
-
166
- /// Returns struct with optional capabilities statuses.
167
- pub fn optional_capabilities ( & self ) -> OptionalCapabilities {
168
- OptionalCapabilities {
169
- counter_offset : self
170
- . fd
171
- . check_extension_raw ( kvm_bindings:: KVM_CAP_COUNTER_OFFSET . into ( ) )
172
- != 0 ,
173
- }
174
- }
175
- }
176
-
177
- #[ cfg( target_arch = "x86_64" ) ]
178
- impl Kvm {
179
- const DEFAULT_CAPABILITIES : [ u32 ; 14 ] = [
180
- kvm_bindings:: KVM_CAP_IRQCHIP ,
181
- kvm_bindings:: KVM_CAP_IOEVENTFD ,
182
- kvm_bindings:: KVM_CAP_IRQFD ,
183
- kvm_bindings:: KVM_CAP_USER_MEMORY ,
184
- kvm_bindings:: KVM_CAP_SET_TSS_ADDR ,
185
- kvm_bindings:: KVM_CAP_PIT2 ,
186
- kvm_bindings:: KVM_CAP_PIT_STATE2 ,
187
- kvm_bindings:: KVM_CAP_ADJUST_CLOCK ,
188
- kvm_bindings:: KVM_CAP_DEBUGREGS ,
189
- kvm_bindings:: KVM_CAP_MP_STATE ,
190
- kvm_bindings:: KVM_CAP_VCPU_EVENTS ,
191
- kvm_bindings:: KVM_CAP_XCRS ,
192
- kvm_bindings:: KVM_CAP_XSAVE ,
193
- kvm_bindings:: KVM_CAP_EXT_CPUID ,
194
- ] ;
195
- }
196
105
197
106
/// Structure holding an general specific VM state.
198
107
#[ derive( Debug , Default , Serialize , Deserialize ) ]
0 commit comments