Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpufeatures: aarch64 support (Linux and macOS/M4) #393

Merged
merged 1 commit into from
May 6, 2021

Conversation

tarcieri
Copy link
Member

@tarcieri tarcieri commented May 5, 2021

Adds preliminary support for runtime feature detection on aarch64 targets, presently restricted to the following set of target features which are present on both aarch64-unknown-linux-gnu and aarch64-apple-darwin targets:

  • aes: AES support
  • sha2: SHA1 and SHA256 support
  • sha3: SHA512 and SHA3 support

Closes #378

@tarcieri tarcieri requested a review from newpavlov May 5, 2021 18:05
Comment on lines +88 to +59
("aes", HWCAP_AES), // Enable AES support.
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
Copy link
Member Author

@tarcieri tarcieri May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the comments here are taken directly from the output of:

$ rustc --target=aarch64-unknown-linux-gnu --print target-features

The output for these target features is the same on an aarch64-apple-darwin target.

I'm a bit confused because the target features aren't isomorphic to either the Linux ARM64 ELF hwcaps or the macOS sysctl flags.

Per the Linux ARM64 ELF hwcaps docs:

https://www.kernel.org/doc/html/latest/arm64/elf_hwcaps.html

HWCAP_SHA1: Functionality implied by ID_AA64ISAR0_EL1.SHA1 == 0b0001.
HWCAP_SHA2: Functionality implied by ID_AA64ISAR0_EL1.SHA2 == 0b0001.
HWCAP_SHA3: Functionality implied by ID_AA64ISAR0_EL1.SHA3 == 0b0001.
HWCAP_SHA512: Functionality implied by ID_AA64ISAR0_EL1.SHA2 == 0b0010.

That would suggest that the SHA-1 and SHA-3 hwcaps are independent of the others, and that SHA-2 support implies SHA-512 support.

macOS/M4 likewise has separate sysctl(3) flags for SHA-512 and SHA-3:

hw.optional.armv8_2_sha512: 1
hw.optional.armv8_2_sha3: 1

...but the rustc target-features suggest that sha2 implies SHA-1 support, and that sha3 implies SHA-512 support.

@tarcieri tarcieri force-pushed the cpufeatures/aarch64 branch 4 times, most recently from 16e4ecf to 6515a6b Compare May 5, 2021 20:03
@tarcieri tarcieri changed the title [WIP] cpufeatures: aarch64 support [WIP] cpufeatures: aarch64 support (Linux and macOS/M4) May 5, 2021
@tarcieri tarcieri force-pushed the cpufeatures/aarch64 branch 6 times, most recently from 59b29d6 to cfbcd7e Compare May 6, 2021 00:04
@tarcieri tarcieri changed the title [WIP] cpufeatures: aarch64 support (Linux and macOS/M4) cpufeatures: aarch64 support (Linux and macOS/M4) May 6, 2021
@tarcieri tarcieri marked this pull request as ready for review May 6, 2021 00:05
Adds preliminary support for runtime feature detection on `aarch64`
targets, presently restricted to the following set of target features
which are present on both `aarch64-unknown-linux-gnu` and
`aarch64-apple-darwin` targets:

- `aes`: AES support
- `sha2`: SHA1 and SHA256 support
- `sha3`: SHA512 and SHA3 support
@tarcieri tarcieri force-pushed the cpufeatures/aarch64 branch from cfbcd7e to 6efb380 Compare May 6, 2021 00:13
@tarcieri
Copy link
Member Author

tarcieri commented May 6, 2021

Marking this as ready for review.

I was able to extract the CPU-specific bits from the new! macro and factor them into respective modules, so the core macro that sets up the AtomicU8s and caches the detection results is shared between targets.

Linux and MacOS targets now work on aarch64. I configured CI to test a Linux target using cross, and tested a MacOS target locally on a M1 Mini.

AFAICT this is not a breaking change and should be fully compatible with the existing v0.1.0 release.

I think the biggest remaining question is my earlier comment about how Rust's ARM64 target features don't seem to fully align with Linux HWCAPs or MacOS armcaps, though the OS-level strategies seem consistent with each other as well as the ARM capability registers. Not sure what to do about that but if anything is wrong, it seems to be Rust's own target feature mapping as everything else (ARM64 docs, Linux, and macOS) are all consistent with each other.

@newpavlov mind taking a look?

#[macro_export]
#[doc(hidden)]
macro_rules! __unless_target_features {
($($tf:tt),+ => $body:expr ) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Played around with a few different syntaxes for this macro and this is the best one I could come up with.

Open to suggestions for alternatives.

@tarcieri
Copy link
Member Author

tarcieri commented May 6, 2021

I'll move my concerns about the mappings of ARM CPU features Rust target features to a separate issue (edit: opened #395).

Going to go ahead and merge this, but I'll hold off on publishing the changes until next week to give @newpavlov some time to review.

Edit: okay, going to go ahead and ship a v0.1.1, but I won't ship any crates that depend on it. Everything that depends on it now only uses the x86/x86-64 support which should be unchanged by this PR.

If need be we can make breaking changes to the ARM backend.

@tarcieri tarcieri merged commit 32733f4 into master May 6, 2021
@tarcieri tarcieri deleted the cpufeatures/aarch64 branch May 6, 2021 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cpufeatures: ARM support
1 participant