Re: SIGILL in detect_arm_hwf_by_toolchain under FreeBSD arm64 on a Raspberry Pi 4B

Jussi Kivilinna <[email protected]>
Newsgroups gmane.comp.encryption.gpg.libgcrypt.devel
Message-ID <[email protected]>
On 10.7.2024 19.49, Naram Qashat via Gcrypt-devel wrote:
> Hello,
> 
> On 2024-07-10 12:39, Jussi Kivilinna wrote:
>> Hello,
>>
>> On 9.7.2024 20.49, Naram Qashat via Gcrypt-devel wrote:
>>> I don't know if this started to happen after I updated to FreeBSD 14.1 on my RPi4B, but libgcrypt crashes with a SIGILL in detect_arm_hwf_by_toolchain on line 527 when trying to run the assembly code there. As a result, I cannot use gnupg either. I asked about this in one of the FreeBSD IRC channels and this was the conversation about it:
>>
>> Looking at documentation for __ARM_FEATURE_CRYPTO (https://github.com/ARM-software/acle/releases/download/r2024Q2/acle-2024Q2.pdf):
>>
>> "__ARM_FEATURE_CRYPTO is defined to 1 if the Armv8-A Crypto instructions are supported and intrinsics targeting them are available. These instructions include AES{E, D}, SHA1{C, P, M} and others. This also implies __ARM_FEATURE_AES and __ARM_FEATURE_SHA2."
>>
>> And as __ARM_FEATURE_AES is implied:
>>
>> "__ARM_FEATURE_AES is defined to 1 if there is hardware support for the Advanced SIMD AES Crypto instructions from Armv8-A ..."
>>
>> Based on this, libgcrypt has been compiled using CPU target that supports crypto-extensions, but actually HW does not support. Did you use "-mcpu=cortex-a72" for compiler flags to select CPU type? If compiler is enabling crypto-extensions for "-mcpu=cortex-a72" and crypto-extensions are actually optional for Cortex-A72 then bug must be in compiler... "-mcpu=cortex-a72" setting should not be selecting optional crypto-extensions by default. Which compiler are you using? GCC or clang or something else? Which version?
> 
> I am using -mcpu=cortex-a72 in my compiler flags. From what I've read recently, the Raspberry Pi 4B, despite running a Cortex-A72, the SoC being used done not have the crypto extensions at all. I don't think it is a compiler bug, but rather just the hardware excluding the feature.

I think this LLVM issue and comments there give some background on CPU feature selection for clang on ARM:
  https://github.com/llvm/llvm-project/issues/90365
  https://github.com/llvm/llvm-project/issues/90365#issuecomment-2117644294

> 
> I am using clang version 18.1.5 from FreeBSD's base system.
> 
> libcrypt was specifically built through FreeBSD's ports tree instead of the poudriere package builder, but I don't think that has anything to do with this as the file with the relevant function is still the same as upstream's 1.11.0.
> 
> Another thing I read is that code doing this should be attempting to catch the SIGILL signal when trying to check for the crypto extensions that don't exist. In a way, I agree with jhibbits that the code in libgcrypt needs fixing.
> 
No need for SIGILL checks, toolchain configuration (mcpu setting in this case) needs to be such that compiler does not indicate crypto-extension instruction support. Inline assembly code in "src/hwf-arm.c" is exactly for detecting these kind of configuration errors, having compiler indicate support for instruction sets that are not actually supported by target HW.

Anyway, you need to use "-mcpu=cortex-a72+nocrypto" with clang when targeting build for RPi 4B.

-Jussi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.