|
1 | | -//! Run-time feature detection for RISC-V on Linux. |
2 | | -
|
3 | | -use super::auxvec; |
4 | | -use crate::detect::{bit, cache, Feature}; |
5 | | - |
6 | | -/// Read list of supported features from the auxiliary vector. |
7 | | -pub(crate) fn detect_features() -> cache::Initializer { |
8 | | - let mut value = cache::Initializer::default(); |
9 | | - let enable_feature = |value: &mut cache::Initializer, feature, enable| { |
10 | | - if enable { |
11 | | - value.set(feature as u32); |
12 | | - } |
13 | | - }; |
14 | | - let enable_features = |value: &mut cache::Initializer, feature_slice: &[Feature], enable| { |
15 | | - if enable { |
16 | | - for feature in feature_slice { |
17 | | - value.set(*feature as u32); |
18 | | - } |
19 | | - } |
20 | | - }; |
21 | | - |
22 | | - // The values are part of the platform-specific [asm/hwcap.h][hwcap] |
23 | | - // |
24 | | - // [hwcap]: https://github.com/torvalds/linux/blob/master/arch/riscv/include/asm/hwcap.h |
25 | | - let auxv = auxvec::auxv().expect("read auxvec"); // should not fail on RISC-V platform |
26 | | - enable_feature( |
27 | | - &mut value, |
28 | | - Feature::a, |
29 | | - bit::test(auxv.hwcap, (b'a' - b'a').into()), |
30 | | - ); |
31 | | - enable_feature( |
32 | | - &mut value, |
33 | | - Feature::c, |
34 | | - bit::test(auxv.hwcap, (b'c' - b'a').into()), |
35 | | - ); |
36 | | - enable_features( |
37 | | - &mut value, |
38 | | - &[Feature::d, Feature::f, Feature::zicsr], |
39 | | - bit::test(auxv.hwcap, (b'd' - b'a').into()), |
40 | | - ); |
41 | | - enable_features( |
42 | | - &mut value, |
43 | | - &[Feature::f, Feature::zicsr], |
44 | | - bit::test(auxv.hwcap, (b'f' - b'a').into()), |
45 | | - ); |
46 | | - let has_i = bit::test(auxv.hwcap, (b'i' - b'a').into()); |
47 | | - // If future RV128I is supported, implement with `enable_feature` here |
48 | | - #[cfg(target_pointer_width = "64")] |
49 | | - enable_feature(&mut value, Feature::rv64i, has_i); |
50 | | - #[cfg(target_pointer_width = "32")] |
51 | | - enable_feature(&mut value, Feature::rv32i, has_i); |
52 | | - #[cfg(target_pointer_width = "32")] |
53 | | - enable_feature( |
54 | | - &mut value, |
55 | | - Feature::rv32e, |
56 | | - bit::test(auxv.hwcap, (b'e' - b'a').into()), |
57 | | - ); |
58 | | - enable_feature( |
59 | | - &mut value, |
60 | | - Feature::h, |
61 | | - bit::test(auxv.hwcap, (b'h' - b'a').into()), |
62 | | - ); |
63 | | - enable_feature( |
64 | | - &mut value, |
65 | | - Feature::m, |
66 | | - bit::test(auxv.hwcap, (b'm' - b'a').into()), |
67 | | - ); |
68 | | - // FIXME: Auxvec does not show supervisor feature support, but this mode may be useful |
69 | | - // to detect when Rust is used to write Linux kernel modules. |
70 | | - // These should be more than Auxvec way to detect supervisor features. |
71 | | - |
72 | | - value |
73 | | -} |
| 1 | +//! Run-time feature detection for RISC-V on Linux. |
| 2 | +
|
| 3 | +use super::auxvec; |
| 4 | +use crate::detect::{bit, cache, Feature}; |
| 5 | + |
| 6 | +/// Read list of supported features from the auxiliary vector. |
| 7 | +pub(crate) fn detect_features() -> cache::Initializer { |
| 8 | + let mut value = cache::Initializer::default(); |
| 9 | + let enable_feature = |value: &mut cache::Initializer, feature, enable| { |
| 10 | + if enable { |
| 11 | + value.set(feature as u32); |
| 12 | + } |
| 13 | + }; |
| 14 | + let enable_features = |value: &mut cache::Initializer, feature_slice: &[Feature], enable| { |
| 15 | + if enable { |
| 16 | + for feature in feature_slice { |
| 17 | + value.set(*feature as u32); |
| 18 | + } |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + // The values are part of the platform-specific [asm/hwcap.h][hwcap] |
| 23 | + // |
| 24 | + // [hwcap]: https://github.com/torvalds/linux/blob/master/arch/riscv/include/asm/hwcap.h |
| 25 | + let auxv = auxvec::auxv().expect("read auxvec"); // should not fail on RISC-V platform |
| 26 | + enable_feature( |
| 27 | + &mut value, |
| 28 | + Feature::a, |
| 29 | + bit::test(auxv.hwcap, (b'a' - b'a').into()), |
| 30 | + ); |
| 31 | + enable_feature( |
| 32 | + &mut value, |
| 33 | + Feature::c, |
| 34 | + bit::test(auxv.hwcap, (b'c' - b'a').into()), |
| 35 | + ); |
| 36 | + enable_features( |
| 37 | + &mut value, |
| 38 | + &[Feature::d, Feature::f, Feature::zicsr], |
| 39 | + bit::test(auxv.hwcap, (b'd' - b'a').into()), |
| 40 | + ); |
| 41 | + enable_features( |
| 42 | + &mut value, |
| 43 | + &[Feature::f, Feature::zicsr], |
| 44 | + bit::test(auxv.hwcap, (b'f' - b'a').into()), |
| 45 | + ); |
| 46 | + let has_i = bit::test(auxv.hwcap, (b'i' - b'a').into()); |
| 47 | + // If future RV128I is supported, implement with `enable_feature` here |
| 48 | + #[cfg(target_pointer_width = "64")] |
| 49 | + enable_feature(&mut value, Feature::rv64i, has_i); |
| 50 | + #[cfg(target_pointer_width = "32")] |
| 51 | + enable_feature(&mut value, Feature::rv32i, has_i); |
| 52 | + #[cfg(target_pointer_width = "32")] |
| 53 | + enable_feature( |
| 54 | + &mut value, |
| 55 | + Feature::rv32e, |
| 56 | + bit::test(auxv.hwcap, (b'e' - b'a').into()), |
| 57 | + ); |
| 58 | + enable_feature( |
| 59 | + &mut value, |
| 60 | + Feature::h, |
| 61 | + bit::test(auxv.hwcap, (b'h' - b'a').into()), |
| 62 | + ); |
| 63 | + enable_feature( |
| 64 | + &mut value, |
| 65 | + Feature::m, |
| 66 | + bit::test(auxv.hwcap, (b'm' - b'a').into()), |
| 67 | + ); |
| 68 | + // FIXME: Auxvec does not show supervisor feature support, but this mode may be useful |
| 69 | + // to detect when Rust is used to write Linux kernel modules. |
| 70 | + // These should be more than Auxvec way to detect supervisor features. |
| 71 | + |
| 72 | + value |
| 73 | +} |
0 commit comments