Skip to content

Commit 88a278f

Browse files
committed
detect: Minor cleanups
1 parent 05bef02 commit 88a278f

File tree

6 files changed

+46
-33
lines changed

6 files changed

+46
-33
lines changed

src/imp/atomic128/aarch64.rs

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ mod detect;
142142

143143
// test only
144144
#[cfg(test)]
145-
#[cfg(not(qemu))]
146145
#[cfg(not(valgrind))]
147146
#[cfg(not(portable_atomic_no_outline_atomics))]
148147
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]

src/imp/detect/aarch64_aa64reg.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ For now, this module is only used on NetBSD/OpenBSD.
3131
3232
On Linux/Android/FreeBSD, we use auxv.rs and this module is test-only because:
3333
- On Linux/Android, this approach requires a higher kernel version than Rust supports,
34-
and also does not work with qemu-user (as of 7.2) and Valgrind (as of 3.19).
34+
and also does not work with qemu-user (as of 7.2) and Valgrind (as of 3.24).
3535
(Looking into HWCAP_CPUID in auxvec, it appears that Valgrind is setting it
3636
to false correctly, but qemu-user is setting it to true.)
37+
- qemu-user issue seem to be fixed as of 9.2.
3738
- On FreeBSD, this approach does not work on FreeBSD 12 on QEMU (confirmed on
3839
FreeBSD 12.{2,3,4}), and we got SIGILL (worked on FreeBSD 13 and 14).
3940
*/

src/imp/detect/aarch64_illumos.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ fn _detect(info: &mut CpuInfo) {
4242
ffi::getisax(out.as_mut_ptr(), OUT_LEN);
4343
}
4444
macro_rules! check {
45-
($flag:ident, $index:tt, $bit:ident) => {
46-
if out[$index] & ffi::$bit != 0 {
45+
($v:ident, $flag:ident, $bit:ident) => {
46+
if $v & ffi::$bit != 0 {
4747
info.set(CpuInfoFlag::$flag);
4848
}
4949
};
5050
}
51-
check!(lse, 0, AV_AARCH64_LSE);
52-
check!(lse2, 1, AV_AARCH64_2_LSE2);
51+
let v1 = out[0];
52+
check!(v1, lse, AV_AARCH64_LSE);
53+
let v2 = out[1];
54+
check!(v2, lse2, AV_AARCH64_2_LSE2);
5355
}

src/imp/detect/auxv.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ mod arch {
437437
// OpenBSD 7.6+
438438
// https://github.com/openbsd/src/commit/ef873df06dac50249b2dd380dc6100eee3b0d23d
439439
pub(crate) const HWCAP_ATOMICS: ffi::c_ulong = 1 << 8;
440+
// Linux 4.11+
441+
// https://github.com/torvalds/linux/commit/77c97b4ee21290f5f083173d957843b615abbff2
442+
// FreeBSD 13.0+/12.2+
443+
// https://github.com/freebsd/freebsd-src/blob/release/13.0.0/sys/arm64/include/elf.h
444+
// https://github.com/freebsd/freebsd-src/blob/release/12.2.0/sys/arm64/include/elf.h
445+
// OpenBSD 7.6+
446+
// https://github.com/openbsd/src/commit/ef873df06dac50249b2dd380dc6100eee3b0d23d
447+
#[cfg(test)]
448+
pub(crate) const HWCAP_CPUID: ffi::c_ulong = 1 << 11;
440449
// Linux 4.17+
441450
// https://github.com/torvalds/linux/commit/7206dc93a58fb76421c4411eefa3c003337bcb2d
442451
// FreeBSD 13.0+/12.2+
@@ -484,25 +493,25 @@ mod arch {
484493
}
485494
}
486495

487-
let hwcap = os::getauxval(ffi::AT_HWCAP);
488-
489-
if hwcap & HWCAP_ATOMICS != 0 {
490-
info.set(CpuInfoFlag::lse);
491-
}
492-
if hwcap & HWCAP_USCAT != 0 {
493-
info.set(CpuInfoFlag::lse2);
496+
macro_rules! check {
497+
($v:ident, $flag:ident, $bit:ident) => {
498+
if $v & $bit != 0 {
499+
info.set(CpuInfoFlag::$flag);
500+
}
501+
};
494502
}
503+
let hwcap = os::getauxval(ffi::AT_HWCAP);
504+
check!(hwcap, lse, HWCAP_ATOMICS);
505+
check!(hwcap, lse2, HWCAP_USCAT);
506+
#[cfg(test)]
507+
check!(hwcap, cpuid, HWCAP_CPUID);
495508
#[cfg(not(target_os = "openbsd"))]
496509
// HWCAP2 is not yet available on ILP32: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/tree/arch/arm64/include/uapi/asm/hwcap.h?h=staging/ilp32-5.1
497510
#[cfg(target_pointer_width = "64")]
498511
{
499512
let hwcap2 = os::getauxval(ffi::AT_HWCAP2);
500-
if hwcap2 & HWCAP2_LRCPC3 != 0 {
501-
info.set(CpuInfoFlag::rcpc3);
502-
}
503-
if hwcap2 & HWCAP2_LSE128 != 0 {
504-
info.set(CpuInfoFlag::lse128);
505-
}
513+
check!(hwcap2, rcpc3, HWCAP2_LRCPC3);
514+
check!(hwcap2, lse128, HWCAP2_LSE128);
506515
}
507516
}
508517
}
@@ -808,7 +817,7 @@ mod tests {
808817
let mut digits = release.split('.');
809818
let major = digits.next().unwrap().parse::<u32>().unwrap();
810819
let minor = digits.next().unwrap().parse::<u32>().unwrap();
811-
// TODO: qemu-user bug?
820+
// TODO: qemu-user bug (fails even on kernel >= 6.4) (as of 9.2)
812821
if (major, minor) < (6, 4) || cfg!(qemu) {
813822
std::eprintln!("kernel version: {}.{} (no pr_get_auxv)", major, minor);
814823
assert_eq!(getauxval_pr_get_auxv_libc(ffi::AT_HWCAP).unwrap_err(), -1);
@@ -822,7 +831,7 @@ mod tests {
822831
-libc::EINVAL
823832
);
824833
} else if cfg!(valgrind) {
825-
// TODO: valgrind bug
834+
// TODO: valgrind bug (result value mismatch) (as of 3.24)
826835
} else {
827836
std::eprintln!("kernel version: {}.{} (has pr_get_auxv)", major, minor);
828837
assert_eq!(

src/imp/detect/common.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! flags {
8787
};
8888
}
8989

90-
// rustc definitions: https://github.com/rust-lang/rust/blob/e6af292f91f21f12ac1aab6825efb7e1e3381cbb/compiler/rustc_target/src/target_features.rs#L179
90+
// rustc definitions: https://github.com/rust-lang/rust/blob/e6af292f91f21f12ac1aab6825efb7e1e3381cbb/compiler/rustc_target/src/target_features.rs
9191

9292
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/AArch64/AArch64Features.td
9393
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
@@ -126,6 +126,9 @@ flags! {
126126
// > If FEAT_LSFE is implemented, then FEAT_FP is implemented.
127127
#[cfg(test)]
128128
lsfe("lsfe", any(target_feature /* N/A */, portable_atomic_target_feature)),
129+
130+
#[cfg(test)] // test-only
131+
cpuid("cpuid", any(/* no corresponding target feature */)),
129132
}
130133

131134
// LLVM definitions: https://github.com/llvm/llvm-project/blob/llvmorg-20.1.0/llvm/lib/Target/PowerPC/PPC.td
@@ -152,15 +155,6 @@ flags! {
152155
vmovdqa_atomic("vmovdqa-atomic", any(/* no corresponding target feature */)),
153156
}
154157

155-
// Static assertions for C type definitions.
156-
// Assertions with core::ffi types are in crate::utils::ffi module.
157-
#[cfg(test)]
158-
#[cfg(not(any(windows, target_arch = "x86", target_arch = "x86_64")))]
159-
const _: fn() = || {
160-
use test_helper::sys;
161-
let _: crate::utils::ffi::c_char = 0 as sys::c_char;
162-
};
163-
164158
#[allow(
165159
clippy::alloc_instead_of_core,
166160
clippy::std_instead_of_alloc,
@@ -226,6 +220,14 @@ mod tests_common {
226220
test_helper::eprintln_nocapture!("{}", features);
227221
}
228222

223+
// Static assertions for C type definitions.
224+
// Assertions with core::ffi types are in crate::utils::ffi module.
225+
#[cfg(not(any(windows, target_arch = "x86", target_arch = "x86_64")))]
226+
const _: fn() = || {
227+
use test_helper::sys;
228+
let _: crate::utils::ffi::c_char = 0 as sys::c_char;
229+
};
230+
229231
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
230232
#[allow(clippy::collapsible_else_if)]
231233
#[test]

src/imp/detect/riscv_linux.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ fn _detect(info: &mut CpuInfo) {
110110
if riscv_hwprobe(&mut out) && out.key != -1 {
111111
let value = out.value;
112112
macro_rules! check {
113-
($flag:ident, $hwprobe_bit:ident) => {
114-
if value & ffi::$hwprobe_bit != 0 {
113+
($flag:ident, $bit:ident) => {
114+
if value & ffi::$bit != 0 {
115115
info.set(CpuInfoFlag::$flag);
116116
}
117117
};

0 commit comments

Comments
 (0)