22// SPDX-License-Identifier: Apache-2.0
33
44use kvm_bindings:: KVM_API_VERSION ;
5- #[ cfg( target_arch = "x86_64" ) ]
6- use kvm_bindings:: { CpuId , KVM_MAX_CPUID_ENTRIES , MsrList } ;
75use kvm_ioctls:: Kvm as KvmFd ;
86use serde:: { Deserialize , Serialize } ;
97
10- #[ cfg( target_arch = "x86_64" ) ]
11- use crate :: arch:: x86_64:: xstate:: { XstateError , request_dynamic_xstate_features} ;
128use crate :: cpu_config:: templates:: KvmCapability ;
139use crate :: vstate:: memory:: { GuestMemory , GuestMemoryMmap } ;
1410
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 , KvmError } ;
18+ #[ cfg( target_arch = "x86_64" ) ]
19+ pub use x86_64:: { Kvm , KvmArchError } ;
20+
1521/// Errors associated with the wrappers over KVM ioctls.
1622/// Needs `rustfmt::skip` to make multiline comments work
1723#[ rustfmt:: skip]
@@ -24,29 +30,10 @@ pub enum KvmError {
2430 /** Error creating KVM object: {0} Make sure the user launching the firecracker process is \
2531 configured on the /dev/kvm file's ACL. */
2632 Kvm ( kvm_ioctls:: Error ) ,
27- #[ cfg( target_arch = "x86_64" ) ]
28- /// Failed to get supported cpuid: {0}
29- GetSupportedCpuId ( kvm_ioctls:: Error ) ,
3033 /// The number of configured slots is bigger than the maximum reported by KVM
3134 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 )
5037}
5138
5239impl Kvm {
@@ -67,36 +54,7 @@ impl Kvm {
6754
6855 let max_memslots = kvm_fd. get_nr_memslots ( ) ;
6956
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) ?)
10058 }
10159
10260 /// Check guest memory does not have more regions than kvm allows.
@@ -144,55 +102,6 @@ impl Kvm {
144102 }
145103 }
146104}
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- }
196105
197106/// Structure holding an general specific VM state.
198107#[ derive( Debug , Default , Serialize , Deserialize ) ]
0 commit comments