Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Commit 79f4d5a

Browse files
RuoqingHeroypat
authored andcommitted
riscv64: Introduce fam-wrappers feature
Implement `fam-wrappers` to get hold on `kvm_reg_list` over RISC-V platforms. Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
1 parent f82eed5 commit 79f4d5a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![allow(non_snake_case)]
99
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1010

11-
#[cfg(all(feature = "fam-wrappers", not(target_arch = "riscv64")))]
11+
#[cfg(feature = "fam-wrappers")]
1212
#[macro_use]
1313
extern crate vmm_sys_util;
1414

src/riscv64/fam_wrappers.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2024 © Institute of Software, CAS. All rights reserved.
2+
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
use vmm_sys_util::fam::{FamStruct, FamStructWrapper};
6+
7+
use riscv64::bindings::*;
8+
9+
// There is no constant in the kernel as far as the maximum number
10+
// of registers on RISC-V, but KVM_GET_REG_LIST usually returns around 160.
11+
const RISCV64_REGS_MAX: usize = 200;
12+
13+
// Implement the FamStruct trait for kvm_reg_list.
14+
generate_fam_struct_impl!(kvm_reg_list, u64, reg, u64, n, RISCV64_REGS_MAX);
15+
16+
// Implement the PartialEq trait for kvm_reg_list.
17+
impl PartialEq for kvm_reg_list {
18+
fn eq(&self, other: &kvm_reg_list) -> bool {
19+
// No need to call entries's eq, FamStructWrapper's PartialEq will do it for you
20+
self.n == other.n
21+
}
22+
}
23+
24+
/// Wrapper over the `kvm_reg_list` structure.
25+
///
26+
/// The `kvm_reg_list` structure contains a flexible array member. For details check the
27+
/// [KVM API KVM_GET_REG_LIST](https://docs.kernel.org/virt/kvm/api.html#kvm-get-reg-list)
28+
/// documentation. To provide safe access to the array elements, this type is
29+
/// implemented using [FamStructWrapper](../vmm_sys_util/fam/struct.FamStructWrapper.html).
30+
pub type RegList = FamStructWrapper<kvm_reg_list>;
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::RegList;
35+
36+
#[test]
37+
fn test_reg_list_eq() {
38+
let mut wrapper = RegList::new(1).unwrap();
39+
assert_eq!(wrapper.as_slice().len(), 1);
40+
41+
let mut wrapper2 = wrapper.clone();
42+
assert!(wrapper == wrapper2);
43+
44+
wrapper.as_mut_slice()[0] = 1;
45+
assert!(wrapper != wrapper2);
46+
wrapper2.as_mut_slice()[0] = 1;
47+
assert!(wrapper == wrapper2);
48+
}
49+
}

src/riscv64/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
#[allow(clippy::all)]
66
#[allow(clippy::undocumented_unsafe_blocks)]
77
pub mod bindings;
8+
#[cfg(feature = "fam-wrappers")]
9+
pub mod fam_wrappers;
810

911
pub use self::bindings::*;
12+
#[cfg(feature = "fam-wrappers")]
13+
pub use self::fam_wrappers::*;

0 commit comments

Comments
 (0)