From 8179d7edf6cb2b06666634d0cafddbca974931e1 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 19 Apr 2021 16:43:00 +0200 Subject: Add runtime aes/crypto check for ARM Yocto apparantly enables it hard at compile time. Change-Id: I1d4c7402eacc714859c61f469ebed85682d48b51 Reviewed-by: Thiago Macieira --- src/corelib/global/qsimd.cpp | 23 ++++++++++++++++++++--- src/corelib/global/qsimd_p.h | 5 +++++ src/corelib/tools/qhash.cpp | 4 +++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/corelib/global/qsimd.cpp b/src/corelib/global/qsimd.cpp index 13b35aa8a7..f76c6820ea 100644 --- a/src/corelib/global/qsimd.cpp +++ b/src/corelib/global/qsimd.cpp @@ -68,9 +68,11 @@ #define HWCAP_VFPv3D16 16384 // copied from (ARM): +#define HWCAP2_AES (1 << 0) #define HWCAP2_CRC32 (1 << 4) // copied from (Aarch64) +#define HWCAP_AES (1 << 3) #define HWCAP_CRC32 (1 << 7) // copied from @@ -93,12 +95,14 @@ QT_BEGIN_NAMESPACE /* Data: neon crc32 + aes */ static const char features_string[] = " neon\0" " crc32\0" + " aes\0" "\0"; -static const int features_indices[] = { 0, 6 }; +static const int features_indices[] = { 0, 6, 13 }; #elif defined(Q_PROCESSOR_MIPS) /* Data: dsp @@ -152,6 +156,8 @@ static inline quint64 detectProcessorFeatures() // For Aarch64: if (vector[i+1] & HWCAP_CRC32) features |= CpuFeatureCRC32; + if (vector[i+1] & HWCAP_AES) + features |= CpuFeatureAES; # endif // Aarch32, or ARMv7 or before: if (vector[i+1] & HWCAP_NEON) @@ -162,6 +168,8 @@ static inline quint64 detectProcessorFeatures() if (vector[i] == AT_HWCAP2) { if (vector[i+1] & HWCAP2_CRC32) features |= CpuFeatureCRC32; + if (vector[i+1] & HWCAP2_AES) + features |= CpuFeatureAES; } # endif } @@ -179,6 +187,9 @@ static inline quint64 detectProcessorFeatures() #if defined(__ARM_FEATURE_CRC32) features |= CpuFeatureCRC32; #endif +#if defined(__ARM_FEATURE_CRYPTO) + features |= CpuFeatureAES; +#endif return features; } @@ -586,6 +597,12 @@ Q_CORE_EXPORT QBasicAtomicInteger qt_cpu_features[2] = { Q_BASIC_ATOMI quint64 qDetectCpuFeatures() { + auto minFeatureTest = minFeature; +#if defined(Q_OS_LINUX) && defined(Q_PROCESSOR_ARM_64) + // Yocto hard-codes CRC32+AES on. Since they are unlikely to be used + // automatically by compilers, we can just add runtime check. + minFeatureTest &= ~(CpuFeatureAES|CpuFeatureCRC32); +#endif quint64 f = detectProcessorFeatures(); QByteArray disable = qgetenv("QT_NO_CPU_FEATURE"); if (!disable.isEmpty()) { @@ -601,8 +618,8 @@ quint64 qDetectCpuFeatures() #else bool runningOnValgrind = false; #endif - if (Q_UNLIKELY(!runningOnValgrind && minFeature != 0 && (f & minFeature) != minFeature)) { - quint64 missing = minFeature & ~f; + if (Q_UNLIKELY(!runningOnValgrind && minFeatureTest != 0 && (f & minFeatureTest) != minFeatureTest)) { + quint64 missing = minFeatureTest & ~f; fprintf(stderr, "Incompatible processor. This Qt build requires the following features:\n "); for (int i = 0; i < features_count; ++i) { if (missing & (Q_UINT64_C(1) << i)) diff --git a/src/corelib/global/qsimd_p.h b/src/corelib/global/qsimd_p.h index fbb5f0f021..2806a29cf1 100644 --- a/src/corelib/global/qsimd_p.h +++ b/src/corelib/global/qsimd_p.h @@ -311,6 +311,8 @@ enum CPUFeatures { CpuFeatureNEON = 2, CpuFeatureARM_NEON = CpuFeatureNEON, CpuFeatureCRC32 = 4, + CpuFeatureAES = 8, + CpuFeatureARM_CRYPTO = CpuFeatureAES, #elif defined(Q_PROCESSOR_MIPS) CpuFeatureDSP = 2, CpuFeatureDSPR2 = 4, @@ -327,6 +329,9 @@ static const quint64 qCompilerCpuFeatures = 0 #if defined __ARM_FEATURE_CRC32 | CpuFeatureCRC32 #endif +#if defined __ARM_FEATURE_CRYPTO + | CpuFeatureAES +#endif #if defined __mips_dsp | CpuFeatureDSP #endif diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index b036add70f..187839ca08 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -670,7 +670,9 @@ size_t qHashBits(const void *p, size_t size, size_t seed) noexcept if (seed && qCpuHasFeature(AES) && qCpuHasFeature(SSE4_2)) return aeshash(reinterpret_cast(p), size, seed); #elif defined(__ARM_FEATURE_CRYPTO) - if (seed) + // Do additional runtime check as Yocto hard enables Crypto extension for + // all armv8 configs + if (seed && (qCpuFeatures() & CpuFeatureAES)) return aeshash(reinterpret_cast(p), size, seed); #endif if (size <= QT_POINTER_SIZE) -- cgit v1.2.3