5
5
// Use of this source code is governed by a BSD-style license that can be
6
6
// found in the THIRD-PARTY file.
7
7
8
- use std:: result;
9
-
10
8
use kvm_ioctls:: * ;
11
9
use logger:: { error, IncMetric , METRICS } ;
12
10
use utils:: vm_memory:: { Address , GuestAddress , GuestMemoryMmap } ;
@@ -18,7 +16,7 @@ use crate::arch::aarch64::regs::{
18
16
set_registers, setup_boot_regs, Aarch64Register , Error as ArchError , KVM_REG_ARM_TIMER_CNT ,
19
17
} ;
20
18
use crate :: cpu_config:: templates:: CpuConfiguration ;
21
- use crate :: vcpu:: VcpuConfig ;
19
+ use crate :: vcpu:: { Error as VcpuError , VcpuConfig } ;
22
20
use crate :: vstate:: vcpu:: VcpuEmulation ;
23
21
use crate :: vstate:: vm:: Vm ;
24
22
@@ -43,8 +41,6 @@ pub enum Error {
43
41
SaveState ( ArchError ) ,
44
42
}
45
43
46
- type Result < T > = result:: Result < T , Error > ;
47
-
48
44
pub type KvmVcpuConfigureError = Error ;
49
45
50
46
/// A wrapper around creating and using a kvm aarch64 vcpu.
@@ -64,7 +60,7 @@ impl KvmVcpu {
64
60
///
65
61
/// * `index` - Represents the 0-based CPU index between [0, max vcpus).
66
62
/// * `vm` - The vm to which this vcpu will get attached.
67
- pub fn new ( index : u8 , vm : & Vm ) -> Result < Self > {
63
+ pub fn new ( index : u8 , vm : & Vm ) -> Result < Self , Error > {
68
64
let kvm_vcpu = vm
69
65
. fd ( )
70
66
. create_vcpu ( index. into ( ) )
@@ -95,7 +91,7 @@ impl KvmVcpu {
95
91
guest_mem : & GuestMemoryMmap ,
96
92
kernel_load_addr : GuestAddress ,
97
93
vcpu_config : & VcpuConfig ,
98
- ) -> std :: result :: Result < ( ) , Error > {
94
+ ) -> Result < ( ) , Error > {
99
95
for Aarch64Register { id, value } in vcpu_config. cpu_config . regs . iter ( ) {
100
96
self . fd
101
97
. set_one_reg ( * id, * value)
@@ -120,7 +116,7 @@ impl KvmVcpu {
120
116
/// # Arguments
121
117
///
122
118
/// * `vm_fd` - The kvm `VmFd` for this microvm.
123
- pub fn init ( & self , vm_fd : & VmFd ) -> Result < ( ) > {
119
+ pub fn init ( & self , vm_fd : & VmFd ) -> Result < ( ) , Error > {
124
120
let mut kvi: kvm_bindings:: kvm_vcpu_init = kvm_bindings:: kvm_vcpu_init:: default ( ) ;
125
121
126
122
// This reads back the kernel's preferred target type.
@@ -137,7 +133,7 @@ impl KvmVcpu {
137
133
}
138
134
139
135
/// Save the KVM internal state.
140
- pub fn save_state ( & self ) -> Result < VcpuState > {
136
+ pub fn save_state ( & self ) -> Result < VcpuState , Error > {
141
137
let mut state = VcpuState {
142
138
mp_state : get_mpstate ( & self . fd ) . map_err ( Error :: SaveState ) ?,
143
139
..Default :: default ( )
@@ -148,14 +144,14 @@ impl KvmVcpu {
148
144
}
149
145
150
146
/// Use provided state to populate KVM internal state.
151
- pub fn restore_state ( & self , state : & VcpuState ) -> Result < ( ) > {
147
+ pub fn restore_state ( & self , state : & VcpuState ) -> Result < ( ) , Error > {
152
148
set_registers ( & self . fd , & state. regs ) . map_err ( Error :: RestoreState ) ?;
153
149
set_mpstate ( & self . fd , state. mp_state ) . map_err ( Error :: RestoreState ) ?;
154
150
Ok ( ( ) )
155
151
}
156
152
157
153
/// Dumps CPU configuration.
158
- pub fn dump_cpu_config ( & self ) -> Result < CpuConfiguration > {
154
+ pub fn dump_cpu_config ( & self ) -> Result < CpuConfiguration , Error > {
159
155
let mut reg_list = get_all_registers_ids ( & self . fd ) . map_err ( Error :: DumpCpuConfig ) ?;
160
156
161
157
// KVM_REG_ARM_TIMER_CNT should be removed, because it depends on the elapsed time and
@@ -173,12 +169,12 @@ impl KvmVcpu {
173
169
/// Runs the vCPU in KVM context and handles the kvm exit reason.
174
170
///
175
171
/// Returns error or enum specifying whether emulation was handled or interrupted.
176
- pub fn run_arch_emulation ( & self , exit : VcpuExit ) -> super :: Result < VcpuEmulation > {
172
+ pub fn run_arch_emulation ( & self , exit : VcpuExit ) -> Result < VcpuEmulation , VcpuError > {
177
173
METRICS . vcpu . failures . inc ( ) ;
178
174
// TODO: Are we sure we want to finish running a vcpu upon
179
175
// receiving a vm exit that is not necessarily an error?
180
176
error ! ( "Unexpected exit reason on vcpu run: {:?}" , exit) ;
181
- Err ( super :: Error :: UnhandledKvmExit ( format ! ( "{:?}" , exit) ) )
177
+ Err ( VcpuError :: UnhandledKvmExit ( format ! ( "{:?}" , exit) ) )
182
178
}
183
179
}
184
180
@@ -200,7 +196,7 @@ mod tests {
200
196
201
197
use utils:: vm_memory:: GuestMemoryMmap ;
202
198
203
- use super :: * ;
199
+ use super :: { Error , * } ;
204
200
use crate :: cpu_config:: aarch64:: CpuConfiguration ;
205
201
use crate :: vcpu:: VcpuConfig ;
206
202
use crate :: vstate:: vm:: tests:: setup_vm;
@@ -216,7 +212,7 @@ mod tests {
216
212
}
217
213
218
214
fn init_vcpu ( vcpu : & VcpuFd , vm : & VmFd ) {
219
- let mut kvi: kvm_bindings :: kvm_vcpu_init = kvm_bindings:: kvm_vcpu_init:: default ( ) ;
215
+ let mut kvi = kvm_bindings:: kvm_vcpu_init:: default ( ) ;
220
216
vm. get_preferred_target ( & mut kvi) . unwrap ( ) ;
221
217
vcpu. vcpu_init ( & kvi) . unwrap ( ) ;
222
218
}
@@ -262,23 +258,8 @@ mod tests {
262
258
assert ! ( err. is_err( ) ) ;
263
259
assert_eq ! (
264
260
err. unwrap_err( ) ,
265
- super :: Error :: ConfigureRegisters ( ArchError :: SetOneReg (
266
- 6931039826524241986 ,
267
- kvm_ioctls:: Error :: new( 9 )
268
- ) )
269
- ) ;
270
-
271
- let ( _vm, mut vcpu, vm_mem) = setup_vcpu ( 0x10000 ) ;
272
- unsafe { libc:: close ( vcpu. fd . as_raw_fd ( ) ) } ;
273
- let err = vcpu. configure (
274
- & vm_mem,
275
- GuestAddress ( crate :: arch:: get_kernel_start ( ) ) ,
276
- & vcpu_config,
277
- ) ;
278
- assert_eq ! (
279
- err. unwrap_err( ) ,
280
- super :: Error :: ConfigureRegisters ( ArchError :: SetOneReg (
281
- 6931039826524241986 ,
261
+ Error :: ConfigureRegisters ( ArchError :: SetOneReg (
262
+ 0x6030000000100042 ,
282
263
kvm_ioctls:: Error :: new( 9 )
283
264
) )
284
265
) ;
@@ -307,7 +288,7 @@ mod tests {
307
288
assert ! ( res. is_err( ) ) ;
308
289
assert_eq ! (
309
290
res. unwrap_err( ) ,
310
- super :: Error :: SaveState ( ArchError :: GetRegList ( kvm_ioctls:: Error :: new( 8 ) ) ) ,
291
+ Error :: SaveState ( ArchError :: GetRegList ( kvm_ioctls:: Error :: new( 8 ) ) ) ,
311
292
) ;
312
293
313
294
// Try to restore the register using a faulty state.
@@ -320,7 +301,7 @@ mod tests {
320
301
assert ! ( res. is_err( ) ) ;
321
302
assert_eq ! (
322
303
res. unwrap_err( ) ,
323
- super :: Error :: RestoreState ( ArchError :: SetOneReg ( 0 , kvm_ioctls:: Error :: new( 8 ) ) )
304
+ Error :: RestoreState ( ArchError :: SetOneReg ( 0 , kvm_ioctls:: Error :: new( 8 ) ) )
324
305
) ;
325
306
326
307
init_vcpu ( & vcpu. fd , vm. fd ( ) ) ;
0 commit comments