6
6
// found in the THIRD-PARTY file.
7
7
8
8
use std:: path:: PathBuf ;
9
- use std:: { fs, mem, result, u32} ;
10
9
11
10
use kvm_bindings:: * ;
12
11
use kvm_ioctls:: VcpuFd ;
@@ -19,7 +18,7 @@ use super::get_fdt_addr;
19
18
/// Struct describing a saved aarch64 register.
20
19
///
21
20
/// Used for interacting with `KVM_GET/SET_ONE_REG`.
22
- #[ derive( Debug , Clone , Versionize , PartialEq , Eq ) ]
21
+ #[ derive( Debug , Clone , PartialEq , Eq , Versionize ) ]
23
22
pub struct Aarch64Register {
24
23
/// The KVM register ID.
25
24
///
@@ -58,7 +57,6 @@ pub enum Error {
58
57
#[ error( "{0}" ) ]
59
58
GetMidrEl1 ( String ) ,
60
59
}
61
- type Result < T > = result:: Result < T , Error > ;
62
60
63
61
#[ allow( non_upper_case_globals) ]
64
62
// PSR (Processor State Register) bits.
@@ -123,7 +121,7 @@ macro_rules! arm64_core_reg_id {
123
121
KVM_REG_ARM64 as u64
124
122
| u64 :: from( KVM_REG_ARM_CORE )
125
123
| $size
126
- | ( ( $offset / mem:: size_of:: <u32 >( ) ) as u64 )
124
+ | ( ( $offset / std :: mem:: size_of:: <u32 >( ) ) as u64 )
127
125
} ;
128
126
}
129
127
@@ -171,7 +169,7 @@ arm64_sys_reg!(KVM_REG_ARM_TIMER_CNT, 3, 3, 14, 3, 2);
171
169
///
172
170
/// * `state` - Array slice of [`Aarch64Register`] structures, representing the registers of a VCPU
173
171
/// state.
174
- pub fn get_manufacturer_id_from_state ( state : & [ Aarch64Register ] ) -> Result < u32 > {
172
+ pub fn get_manufacturer_id_from_state ( state : & [ Aarch64Register ] ) -> Result < u32 , Error > {
175
173
let midr_el1 = state. iter ( ) . find ( |reg| reg. id == MIDR_EL1 ) ;
176
174
match midr_el1 {
177
175
Some ( register) => Ok ( register. value as u32 >> 24 ) ,
@@ -183,11 +181,11 @@ pub fn get_manufacturer_id_from_state(state: &[Aarch64Register]) -> Result<u32>
183
181
184
182
/// Extract the Manufacturer ID from the host.
185
183
/// The ID is found between bits 24-31 of MIDR_EL1 register.
186
- pub fn get_manufacturer_id_from_host ( ) -> Result < u32 > {
184
+ pub fn get_manufacturer_id_from_host ( ) -> Result < u32 , Error > {
187
185
let midr_el1_path =
188
186
& PathBuf :: from ( "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1" . to_string ( ) ) ;
189
187
190
- let midr_el1 = fs:: read_to_string ( midr_el1_path) . map_err ( |err| {
188
+ let midr_el1 = std :: fs:: read_to_string ( midr_el1_path) . map_err ( |err| {
191
189
Error :: GetMidrEl1 ( format ! ( "Failed to get MIDR_EL1 from host path: {err}" ) )
192
190
} ) ?;
193
191
let midr_el1_trimmed = midr_el1. trim_end ( ) . trim_start_matches ( "0x" ) ;
@@ -210,7 +208,7 @@ pub fn setup_boot_regs(
210
208
cpu_id : u8 ,
211
209
boot_ip : u64 ,
212
210
mem : & GuestMemoryMmap ,
213
- ) -> Result < ( ) > {
211
+ ) -> Result < ( ) , Error > {
214
212
let kreg_off = offset__of ! ( kvm_regs, regs) ;
215
213
216
214
// Get the register index of the PSTATE (Processor State) register.
@@ -244,7 +242,7 @@ pub fn setup_boot_regs(
244
242
/// # Arguments
245
243
///
246
244
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
247
- pub fn read_mpidr ( vcpu : & VcpuFd ) -> Result < u64 > {
245
+ pub fn read_mpidr ( vcpu : & VcpuFd ) -> Result < u64 , Error > {
248
246
match vcpu. get_one_reg ( MPIDR_EL1 ) {
249
247
Err ( err) => Err ( Error :: GetOneReg ( MPIDR_EL1 , err) ) ,
250
248
// MPIDR register is 64 bit wide on aarch64, this expect cannot fail
@@ -301,7 +299,7 @@ pub fn get_core_registers_ids() -> Vec<u64> {
301
299
let mut off = offset__of ! ( kvm_regs, fp_regs) + offset__of ! ( user_fpsimd_state, vregs) ;
302
300
for _ in 0 ..NR_FP_VREGS {
303
301
ids. push ( arm64_core_reg_id ! ( KVM_REG_SIZE_U128 , off) ) ;
304
- off += mem:: size_of :: < u128 > ( ) ;
302
+ off += std :: mem:: size_of :: < u128 > ( ) ;
305
303
}
306
304
307
305
// Floating-point Status Register.
@@ -321,7 +319,7 @@ pub fn get_core_registers_ids() -> Vec<u64> {
321
319
///
322
320
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
323
321
/// * `state` - Input/Output vector of registers states.
324
- pub fn save_core_registers ( vcpu : & VcpuFd , state : & mut Vec < Aarch64Register > ) -> Result < ( ) > {
322
+ pub fn save_core_registers ( vcpu : & VcpuFd , state : & mut Vec < Aarch64Register > ) -> Result < ( ) , Error > {
325
323
save_registers ( vcpu, & get_core_registers_ids ( ) , state)
326
324
}
327
325
@@ -331,7 +329,7 @@ pub fn save_core_registers(vcpu: &VcpuFd, state: &mut Vec<Aarch64Register>) -> R
331
329
///
332
330
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
333
331
/// * `state` - Input/Output vector of registers states.
334
- pub fn save_all_registers ( vcpu : & VcpuFd , state : & mut Vec < Aarch64Register > ) -> Result < ( ) > {
332
+ pub fn save_all_registers ( vcpu : & VcpuFd , state : & mut Vec < Aarch64Register > ) -> Result < ( ) , Error > {
335
333
// Call KVM_GET_REG_LIST to get all registers available to the guest. For ArmV8 there are
336
334
// less than 500 registers.
337
335
let mut reg_list = RegList :: new ( 500 ) . map_err ( Error :: Fam ) ?;
@@ -350,7 +348,11 @@ pub fn save_all_registers(vcpu: &VcpuFd, state: &mut Vec<Aarch64Register>) -> Re
350
348
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
351
349
/// * `ids` - Slice of registers ids to save.
352
350
/// * `state` - Input/Output vector of registers states.
353
- pub fn save_registers ( vcpu : & VcpuFd , ids : & [ u64 ] , state : & mut Vec < Aarch64Register > ) -> Result < ( ) > {
351
+ pub fn save_registers (
352
+ vcpu : & VcpuFd ,
353
+ ids : & [ u64 ] ,
354
+ state : & mut Vec < Aarch64Register > ,
355
+ ) -> Result < ( ) , Error > {
354
356
for id in ids. iter ( ) {
355
357
state. push ( Aarch64Register {
356
358
id : * id,
@@ -369,7 +371,7 @@ pub fn save_registers(vcpu: &VcpuFd, ids: &[u64], state: &mut Vec<Aarch64Registe
369
371
///
370
372
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
371
373
/// * `state` - Structure containing the state of the system registers.
372
- pub fn restore_registers ( vcpu : & VcpuFd , state : & [ Aarch64Register ] ) -> Result < ( ) > {
374
+ pub fn restore_registers ( vcpu : & VcpuFd , state : & [ Aarch64Register ] ) -> Result < ( ) , Error > {
373
375
for reg in state {
374
376
vcpu. set_one_reg ( reg. id , reg. value )
375
377
. map_err ( |e| Error :: SetOneReg ( reg. id , e) ) ?;
@@ -382,7 +384,7 @@ pub fn restore_registers(vcpu: &VcpuFd, state: &[Aarch64Register]) -> Result<()>
382
384
/// # Arguments
383
385
///
384
386
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
385
- pub fn get_mpstate ( vcpu : & VcpuFd ) -> Result < kvm_mp_state > {
387
+ pub fn get_mpstate ( vcpu : & VcpuFd ) -> Result < kvm_mp_state , Error > {
386
388
vcpu. get_mp_state ( ) . map_err ( Error :: GetMp )
387
389
}
388
390
@@ -392,7 +394,7 @@ pub fn get_mpstate(vcpu: &VcpuFd) -> Result<kvm_mp_state> {
392
394
///
393
395
/// * `vcpu` - Structure for the VCPU that holds the VCPU's fd.
394
396
/// * `state` - Structure for returning the state of the system registers.
395
- pub fn set_mpstate ( vcpu : & VcpuFd , state : kvm_mp_state ) -> Result < ( ) > {
397
+ pub fn set_mpstate ( vcpu : & VcpuFd , state : kvm_mp_state ) -> Result < ( ) , Error > {
396
398
vcpu. set_mp_state ( state) . map_err ( Error :: SetMp )
397
399
}
398
400
0 commit comments