Skip to content

Commit bbba57f

Browse files
committed
std_detect: Support run-time detection on aarch64 OpenBSD
1 parent c7e045a commit bbba57f

File tree

4 files changed

+91
-24
lines changed

4 files changed

+91
-24
lines changed

crates/std_detect/src/detect/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ cfg_if! {
5656
mod aarch64;
5757
#[path = "os/freebsd/mod.rs"]
5858
mod os;
59+
} else if #[cfg(all(target_os = "openbsd", target_arch = "aarch64", feature = "libc"))] {
60+
#[allow(dead_code)]
61+
#[path = "os/aarch64.rs"]
62+
mod aarch64;
63+
#[path = "os/openbsd/aarch64.rs"]
64+
mod os;
5965
} else if #[cfg(all(target_os = "windows", target_arch = "aarch64"))] {
6066
#[path = "os/windows/aarch64.rs"]
6167
mod os;

crates/std_detect/src/detect/os/aarch64.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pub(crate) fn detect_features() -> cache::Initializer {
5353
);
5454
}
5555

56-
parse_system_registers(aa64isar0, aa64isar1, aa64pfr0)
56+
parse_system_registers(aa64isar0, aa64isar1, Some(aa64pfr0))
5757
}
5858

5959
pub(crate) fn parse_system_registers(
6060
aa64isar0: u64,
6161
aa64isar1: u64,
62-
aa64pfr0: u64,
62+
aa64pfr0: Option<u64>,
6363
) -> cache::Initializer {
6464
let mut value = cache::Initializer::default();
6565

@@ -76,26 +76,28 @@ pub(crate) fn parse_system_registers(
7676
enable_feature(Feature::crc, bits_shift(aa64isar0, 19, 16) >= 1);
7777

7878
// ID_AA64PFR0_EL1 - Processor Feature Register 0
79-
let fp = bits_shift(aa64pfr0, 19, 16) < 0xF;
80-
let fphp = bits_shift(aa64pfr0, 19, 16) >= 1;
81-
let asimd = bits_shift(aa64pfr0, 23, 20) < 0xF;
82-
let asimdhp = bits_shift(aa64pfr0, 23, 20) >= 1;
83-
enable_feature(Feature::fp, fp);
84-
enable_feature(Feature::fp16, fphp);
85-
// SIMD support requires float support - if half-floats are
86-
// supported, it also requires half-float support:
87-
enable_feature(Feature::asimd, fp && asimd && (!fphp | asimdhp));
88-
// SIMD extensions require SIMD support:
89-
enable_feature(Feature::aes, asimd && bits_shift(aa64isar0, 7, 4) >= 1);
90-
let sha1 = bits_shift(aa64isar0, 11, 8) >= 1;
91-
let sha2 = bits_shift(aa64isar0, 15, 12) >= 1;
92-
enable_feature(Feature::sha2, asimd && sha1 && sha2);
93-
enable_feature(Feature::rdm, asimd && bits_shift(aa64isar0, 31, 28) >= 1);
94-
enable_feature(
95-
Feature::dotprod,
96-
asimd && bits_shift(aa64isar0, 47, 44) >= 1,
97-
);
98-
enable_feature(Feature::sve, asimd && bits_shift(aa64pfr0, 35, 32) >= 1);
79+
if let Some(aa64pfr0) = aa64pfr0 {
80+
let fp = bits_shift(aa64pfr0, 19, 16) < 0xF;
81+
let fphp = bits_shift(aa64pfr0, 19, 16) >= 1;
82+
let asimd = bits_shift(aa64pfr0, 23, 20) < 0xF;
83+
let asimdhp = bits_shift(aa64pfr0, 23, 20) >= 1;
84+
enable_feature(Feature::fp, fp);
85+
enable_feature(Feature::fp16, fphp);
86+
// SIMD support requires float support - if half-floats are
87+
// supported, it also requires half-float support:
88+
enable_feature(Feature::asimd, fp && asimd && (!fphp | asimdhp));
89+
// SIMD extensions require SIMD support:
90+
enable_feature(Feature::aes, asimd && bits_shift(aa64isar0, 7, 4) >= 1);
91+
let sha1 = bits_shift(aa64isar0, 11, 8) >= 1;
92+
let sha2 = bits_shift(aa64isar0, 15, 12) >= 1;
93+
enable_feature(Feature::sha2, asimd && sha1 && sha2);
94+
enable_feature(Feature::rdm, asimd && bits_shift(aa64isar0, 31, 28) >= 1);
95+
enable_feature(
96+
Feature::dotprod,
97+
asimd && bits_shift(aa64isar0, 47, 44) >= 1,
98+
);
99+
enable_feature(Feature::sve, asimd && bits_shift(aa64pfr0, 35, 32) >= 1);
100+
}
99101

100102
// ID_AA64PFR0_EL1 - Processor Feature Register 0
101103
// Check for either APA or API field
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Run-time feature detection for Aarch64 on OpenBSD.
2+
//!
3+
//! OpenBSD doesn't trap the mrs instruction, but exposes the system registers through sysctl.
4+
//! https://github.com/openbsd/src/commit/d335af936b9d7dd9cf655cae1ce19560c45de6c8
5+
//! https://github.com/golang/go/commit/cd54ef1f61945459486e9eea2f016d99ef1da925
6+
7+
use crate::detect::cache;
8+
use core::{mem::MaybeUninit, ptr};
9+
10+
// Defined in sys/sysctl.h.
11+
// https://github.com/openbsd/src/blob/72ccc03bd11da614f31f7ff76e3f6fce99bc1c79/sys/sys/sysctl.h#L82
12+
const CTL_MACHDEP: libc::c_int = 7;
13+
// Defined in machine/cpu.h.
14+
// https://github.com/openbsd/src/blob/72ccc03bd11da614f31f7ff76e3f6fce99bc1c79/sys/arch/arm64/include/cpu.h#L29
15+
const CPU_ID_AA64ISAR0: libc::c_int = 2;
16+
const CPU_ID_AA64ISAR1: libc::c_int = 3;
17+
const CPU_ID_AA64PFR0: libc::c_int = 8;
18+
19+
/// Try to read the features from the system registers.
20+
pub(crate) fn detect_features() -> cache::Initializer {
21+
// ID_AA64ISAR0_EL1 and ID_AA64ISAR1_EL1 are supported on OpenBSD 7.1+.
22+
// https://github.com/openbsd/src/commit/d335af936b9d7dd9cf655cae1ce19560c45de6c8
23+
// Others are supported on OpenBSD 7.3+.
24+
// https://github.com/openbsd/src/commit/c7654cd65262d532212f65123ee3905ba200365c
25+
// sysctl returns an unsupported error if operation is not supported,
26+
// so we can safely use this function on older versions of OpenBSD.
27+
let aa64isar0 = sysctl64(&[CTL_MACHDEP, CPU_ID_AA64ISAR0]).unwrap_or(0);
28+
let aa64isar1 = sysctl64(&[CTL_MACHDEP, CPU_ID_AA64ISAR1]).unwrap_or(0);
29+
// Do not use unwrap_or(0) because in fp and asimd fields, 0 indicates that
30+
// the feature is available.
31+
let aa64pfr0 = sysctl64(&[CTL_MACHDEP, CPU_ID_AA64PFR0]);
32+
33+
super::aarch64::parse_system_registers(aa64isar0, aa64isar1, aa64pfr0)
34+
}
35+
36+
#[inline]
37+
fn sysctl64(mib: &[libc::c_int]) -> Option<u64> {
38+
const OUT_LEN: libc::size_t = core::mem::size_of::<u64>();
39+
let mut out = MaybeUninit::<u64>::uninit();
40+
let mut out_len = OUT_LEN;
41+
let res = unsafe {
42+
libc::sysctl(
43+
mib.as_ptr(),
44+
mib.len() as libc::c_uint,
45+
out.as_mut_ptr() as *mut libc::c_void,
46+
&mut out_len,
47+
ptr::null_mut(),
48+
0,
49+
)
50+
};
51+
if res == -1 || out_len != OUT_LEN {
52+
return None;
53+
}
54+
// SAFETY: we've checked that sysctl was succeed.
55+
Some(unsafe { out.assume_init() })
56+
}

crates/std_detect/tests/cpu-detection.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ fn aarch64_windows() {
9898
}
9999

100100
#[test]
101-
#[cfg(all(target_arch = "aarch64", target_os = "freebsd"))]
102-
fn aarch64_freebsd() {
101+
#[cfg(all(
102+
target_arch = "aarch64",
103+
any(target_os = "freebsd", target_os = "openbsd")
104+
))]
105+
fn aarch64_bsd() {
103106
println!("asimd: {:?}", is_aarch64_feature_detected!("asimd"));
104107
println!("pmull: {:?}", is_aarch64_feature_detected!("pmull"));
105108
println!("fp: {:?}", is_aarch64_feature_detected!("fp"));

0 commit comments

Comments
 (0)