Skip to content

Commit

Permalink
detect: Support run-time detection on aarch64 illumos
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Sep 2, 2024
1 parent 5dc8f4f commit 756565a
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ elems
espup
exynos
getauxval
getisax
getpid
gimli
Halfword
Expand Down Expand Up @@ -116,6 +117,7 @@ RAII
rcpc
reentrancy
revidr
richlowe
rsbegin
rsend
rsil
Expand Down
1 change: 1 addition & 0 deletions src/imp/atomic128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Here is the table of targets that support run-time CPU feature detection and the
| aarch64 | netbsd | sysctl | all | Enabled by default |
| aarch64 | openbsd | sysctl | all | Enabled by default |
| aarch64 | macos | sysctl | all | Currently only used in tests because FEAT_LSE and FEAT_LSE2 are always available at compile-time. |
| aarch64 | illumos | getisax | lse, lse2 | Enabled by default |
| aarch64 | windows | IsProcessorFeaturePresent | lse | Enabled by default |
| aarch64 | fuchsia | zx_system_get_features | lse | Enabled by default |
| powerpc64 | linux | getauxval | all | Disabled by default |
Expand Down
21 changes: 21 additions & 0 deletions src/imp/atomic128/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ mod detect;
#[path = "detect/aarch64_aa64reg.rs"]
mod detect;
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(
test,
not(all(
any(target_feature = "lse2", portable_atomic_target_feature = "lse2"),
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
)),
))]
#[cfg(target_os = "illumos")]
#[path = "detect/aarch64_illumos.rs"]
mod detect;
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[cfg(target_os = "fuchsia")]
#[path = "detect/aarch64_fuchsia.rs"]
Expand Down Expand Up @@ -174,6 +185,7 @@ macro_rules! debug_assert_lse {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "fuchsia",
target_os = "windows",
),
Expand Down Expand Up @@ -210,6 +222,7 @@ macro_rules! debug_assert_lse2 {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE2.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -251,6 +264,7 @@ macro_rules! debug_assert_lse128 {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE128.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -292,6 +306,7 @@ macro_rules! debug_assert_rcpc3 {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LRCPC3.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -475,6 +490,7 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE2.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -585,6 +601,7 @@ unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE2.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -884,6 +901,7 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE2.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -1004,6 +1022,7 @@ unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
// These don't support detection of FEAT_LSE2.
// target_os = "fuchsia",
// target_os = "windows",
Expand Down Expand Up @@ -1218,6 +1237,7 @@ unsafe fn atomic_compare_exchange(
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "fuchsia",
target_os = "windows",
),
Expand Down Expand Up @@ -1356,6 +1376,7 @@ unsafe fn atomic_compare_exchange(
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "illumos",
target_os = "fuchsia",
target_os = "windows",
),
Expand Down
73 changes: 73 additions & 0 deletions src/imp/atomic128/detect/aarch64_illumos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Run-time CPU feature detection on AArch64 illumos by using getisax.

include!("common.rs");

// core::ffi::c_* (except c_void) requires Rust 1.64, libc will soon require Rust 1.47
#[allow(non_camel_case_types)]
mod ffi {
pub(crate) use super::c_types::c_uint;

// Defined in sys/auxv_aarch64.h.
// /~https://github.com/richlowe/illumos-gate/blob/arm64-gate/usr/src/uts/common/sys/auxv_aarch64.h
pub(crate) const AV_AARCH64_LSE: u32 = 1 << 15;
pub(crate) const AV_AARCH64_2_LSE2: u32 = 1 << 2;

extern "C" {
// Defined in sys/auxv.h.
// https://illumos.org/man/2/getisax
// /~https://github.com/richlowe/illumos-gate/blob/arm64-gate/usr/src/uts/common/sys/auxv.h
pub(crate) fn getisax(array: *mut u32, n: c_uint) -> c_uint;
}
}

#[cold]
fn _detect(info: &mut CpuInfo) {
let mut out = [0_u32; 2];
// SAFETY: the pointer is valid because we got it from a reference.
unsafe {
ffi::getisax(out.as_mut_ptr(), 2);
}
if out[0] & ffi::AV_AARCH64_LSE != 0 {
info.set(CpuInfo::HAS_LSE);
}
if out[1] & ffi::AV_AARCH64_2_LSE2 != 0 {
info.set(CpuInfo::HAS_LSE2);
}
}

#[allow(
clippy::alloc_instead_of_core,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
clippy::undocumented_unsafe_blocks,
clippy::wildcard_imports
)]
#[cfg(test)]
mod tests {
use super::*;

// Static assertions for FFI bindings.
// This checks that FFI bindings defined in this crate and FFI bindings
// generated for the platform's latest header file using bindgen have
// compatible signatures (or the same values if constants).
// Since this is static assertion, we can detect problems with
// `cargo check --tests --target <target>` run in CI (via TESTS=1 build.sh)
// without actually running tests on these platforms.
// See also tools/codegen/src/ffi.rs.
// TODO(codegen): auto-generate this test
#[allow(
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_possible_truncation,
clippy::no_effect_underscore_binding
)]
const _: fn() = || {
use test_helper::sys;
let mut _getisax: unsafe extern "C" fn(*mut u32, ffi::c_uint) -> ffi::c_uint = ffi::getisax;
_getisax = sys::getisax;
static_assert!(ffi::AV_AARCH64_LSE == sys::AV_AARCH64_LSE);
static_assert!(ffi::AV_AARCH64_2_LSE2 == sys::AV_AARCH64_2_LSE2);
};
}
73 changes: 73 additions & 0 deletions tests/helper/src/gen/sys/aarch64_illumos/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/helper/src/gen/sys/aarch64_illumos/sys_auxv.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions tests/helper/src/gen/sys/aarch64_illumos/sys_auxv_aarch64.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/helper/src/gen/sys/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ default_targets=(
aarch64-unknown-linux-uclibc # custom target
aarch64-unknown-netbsd
aarch64-unknown-openbsd
aarch64-unknown-illumos
# aarch64 with FEAT_LSE & FEAT_LSE2
aarch64-apple-darwin
# aarch64 big endian
Expand Down
Loading

0 comments on commit 756565a

Please sign in to comment.