| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CPUCAP_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_CPUCAP_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM) || \ |
| 14 |
defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE) |
| 15 |
#define HAS_OPENSSL_CPUID_SETUP |
| 16 |
// OPENSSL_cpuid_setup initializes the platform-specific feature cache. |
| 17 |
void OPENSSL_cpuid_setup(void); |
| 18 |
#endif |
| 19 |
|
| 20 |
// Runtime CPU feature support |
| 21 |
|
| 22 |
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 23 |
// OPENSSL_ia32cap_P contains the Intel CPUID bits when running on an x86 or |
| 24 |
// x86-64 system. |
| 25 |
// |
| 26 |
// Index 0: |
| 27 |
// EDX for CPUID where EAX = 1 |
| 28 |
// Bit 20 is always zero |
| 29 |
// Bit 28 is adjusted to reflect whether the data cache is shared between |
| 30 |
// multiple logical cores |
| 31 |
// Bit 30 is used to indicate an Intel CPU |
| 32 |
// Index 1: |
| 33 |
// ECX for CPUID where EAX = 1 |
| 34 |
// Bit 11 is used to indicate AMD XOP support, not SDBG |
| 35 |
// Index 2: |
| 36 |
// EBX for CPUID where EAX = 7 |
| 37 |
// Index 3: |
| 38 |
// ECX for CPUID where EAX = 7 |
| 39 |
// |
| 40 |
// Note: the CPUID bits are pre-adjusted for the OSXSAVE bit and the YMM and XMM |
| 41 |
// bits in XCR0, so it is not necessary to check those. (WARNING: See caveats |
| 42 |
// in cpu_intel.c.) |
| 43 |
extern uint32_t OPENSSL_ia32cap_P[4]; |
| 44 |
|
| 45 |
#if defined(BORINGSSL_FIPS) && !defined(BORINGSSL_SHARED_LIBRARY) |
| 46 |
// The FIPS module, as a static library, requires an out-of-line version of |
| 47 |
// |OPENSSL_ia32cap_get| so accesses can be rewritten by delocate. Mark the |
| 48 |
// function const so multiple accesses can be optimized together. |
| 49 |
const uint32_t *OPENSSL_ia32cap_get(void) __attribute__((const)); |
| 50 |
#else |
| 51 |
OPENSSL_INLINE const uint32_t *OPENSSL_ia32cap_get(void) { |
| 52 |
return OPENSSL_ia32cap_P; |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
// See Intel manual, volume 2A, table 3-11. |
| 57 |
|
| 58 |
OPENSSL_INLINE int CRYPTO_is_FXSR_capable(void) { |
| 59 |
return (OPENSSL_ia32cap_get()[0] & (1 << 24)) != 0; |
| 60 |
} |
| 61 |
|
| 62 |
OPENSSL_INLINE int CRYPTO_is_intel_cpu(void) { |
| 63 |
// The reserved bit 30 is used to indicate an Intel CPU. |
| 64 |
return (OPENSSL_ia32cap_get()[0] & (1 << 30)) != 0; |
| 65 |
} |
| 66 |
|
| 67 |
// See Intel manual, volume 2A, table 3-10. |
| 68 |
|
| 69 |
OPENSSL_INLINE int CRYPTO_is_PCLMUL_capable(void) { |
| 70 |
return (OPENSSL_ia32cap_get()[1] & (1 << 1)) != 0; |
| 71 |
} |
| 72 |
|
| 73 |
OPENSSL_INLINE int CRYPTO_is_SSSE3_capable(void) { |
| 74 |
return (OPENSSL_ia32cap_get()[1] & (1 << 9)) != 0; |
| 75 |
} |
| 76 |
|
| 77 |
OPENSSL_INLINE int CRYPTO_is_SSE4_1_capable(void) { |
| 78 |
return (OPENSSL_ia32cap_get()[1] & (1 << 19)) != 0; |
| 79 |
} |
| 80 |
|
| 81 |
OPENSSL_INLINE int CRYPTO_is_MOVBE_capable(void) { |
| 82 |
return (OPENSSL_ia32cap_get()[1] & (1 << 22)) != 0; |
| 83 |
} |
| 84 |
|
| 85 |
OPENSSL_INLINE int CRYPTO_is_AESNI_capable(void) { |
| 86 |
return (OPENSSL_ia32cap_get()[1] & (1 << 25)) != 0; |
| 87 |
} |
| 88 |
|
| 89 |
// We intentionally avoid defining a |CRYPTO_is_XSAVE_capable| function. See |
| 90 |
// |CRYPTO_cpu_perf_is_like_silvermont|. |
| 91 |
|
| 92 |
OPENSSL_INLINE int CRYPTO_is_AVX_capable(void) { |
| 93 |
return (OPENSSL_ia32cap_get()[1] & (1 << 28)) != 0; |
| 94 |
} |
| 95 |
|
| 96 |
OPENSSL_INLINE int CRYPTO_is_RDRAND_capable(void) { |
| 97 |
return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0; |
| 98 |
} |
| 99 |
|
| 100 |
OPENSSL_INLINE int CRYPTO_is_AMD_XOP_support(void) { |
| 101 |
return (OPENSSL_ia32cap_get()[1] & (1 << 11)) != 0; |
| 102 |
} |
| 103 |
|
| 104 |
// See Intel manual, volume 2A, table 3-8. |
| 105 |
|
| 106 |
OPENSSL_INLINE int CRYPTO_is_BMI1_capable(void) { |
| 107 |
return (OPENSSL_ia32cap_get()[2] & (1 << 3)) != 0; |
| 108 |
} |
| 109 |
|
| 110 |
OPENSSL_INLINE int CRYPTO_is_AVX2_capable(void) { |
| 111 |
return (OPENSSL_ia32cap_get()[2] & (1 << 5)) != 0; |
| 112 |
} |
| 113 |
|
| 114 |
OPENSSL_INLINE int CRYPTO_is_BMI2_capable(void) { |
| 115 |
return (OPENSSL_ia32cap_get()[2] & (1 << 8)) != 0; |
| 116 |
} |
| 117 |
|
| 118 |
OPENSSL_INLINE int CRYPTO_is_ADX_capable(void) { |
| 119 |
return (OPENSSL_ia32cap_get()[2] & (1 << 19)) != 0; |
| 120 |
} |
| 121 |
|
| 122 |
OPENSSL_INLINE int CRYPTO_is_SHAEXT_capable(void) { |
| 123 |
return (OPENSSL_ia32cap_get()[2] & (1 << 29)) != 0; |
| 124 |
} |
| 125 |
|
| 126 |
// AVX512VL | AVX512BW | AVX512DQ | AVX512F |
| 127 |
// 1u << 31 | 1u << 30 | 1u << 17 | 1u << 16 |
| 128 |
// 1100_0000_0000_0011_0000_0000_0000_0000 |
| 129 |
#define CPU_CAP_AVX512_BITFLAGS 0xC0030000 |
| 130 |
OPENSSL_INLINE int CRYPTO_is_AVX512_capable(void) { |
| 131 |
return (OPENSSL_ia32cap_get()[2] & CPU_CAP_AVX512_BITFLAGS) == CPU_CAP_AVX512_BITFLAGS; |
| 132 |
} |
| 133 |
|
| 134 |
OPENSSL_INLINE int CRYPTO_is_VAES_capable(void) { |
| 135 |
return (OPENSSL_ia32cap_get()[3] & (1u << (41 - 32))) != 0; |
| 136 |
} |
| 137 |
|
| 138 |
OPENSSL_INLINE int CRYPTO_is_VPCLMULQDQ_capable(void) { |
| 139 |
return (OPENSSL_ia32cap_get()[3] & (1u << (42 - 32))) != 0; |
| 140 |
} |
| 141 |
|
| 142 |
// AVX512VL | AVX512BW | AVX512_IFMA | AVX512DQ | AVX512F |
| 143 |
// 1u << 31 | 1u << 30 | 1u << 21 | 1u << 17 | 1u << 16 |
| 144 |
// 1100_0000_0010_0011_0000_0000_0000_0000 |
| 145 |
#define CPU_CAP_AVX512IFMA_BITFLAGS 0xC0230000 |
| 146 |
OPENSSL_INLINE int CRYPTO_is_AVX512IFMA_capable(void) { |
| 147 |
return (OPENSSL_ia32cap_get()[2] & CPU_CAP_AVX512IFMA_BITFLAGS) == |
| 148 |
CPU_CAP_AVX512IFMA_BITFLAGS; |
| 149 |
} |
| 150 |
|
| 151 |
OPENSSL_INLINE int CRYPTO_is_VBMI2_capable(void) { |
| 152 |
return (OPENSSL_ia32cap_get()[3] & (1 << 6)) != 0; |
| 153 |
} |
| 154 |
|
| 155 |
// CRYPTO_cpu_perf_is_like_silvermont returns one if, based on a heuristic, the |
| 156 |
// CPU has Silvermont-like performance characteristics. It is often faster to |
| 157 |
// run different codepaths on these CPUs than the available instructions would |
| 158 |
// otherwise select. See chacha-x86_64.pl. |
| 159 |
// |
| 160 |
// Bonnell, Silvermont's predecessor in the Atom lineup, will also be matched by |
| 161 |
// this. |OPENSSL_cpuid_setup| forces Knights Landing to also be matched by |
| 162 |
// this. Goldmont (Silvermont's successor in the Atom lineup) added XSAVE so it |
| 163 |
// isn't matched by this. Various sources indicate AMD first implemented MOVBE |
| 164 |
// and XSAVE at the same time in Jaguar, so it seems like AMD chips will not be |
| 165 |
// matched by this. That seems to be the case for other x86(-64) CPUs. |
| 166 |
OPENSSL_INLINE int CRYPTO_cpu_perf_is_like_silvermont(void) { |
| 167 |
// WARNING: This MUST NOT be used to guard the execution of the XSAVE |
| 168 |
// instruction. This is the "hardware supports XSAVE" bit, not the OSXSAVE bit |
| 169 |
// that indicates whether we can safely execute XSAVE. This bit may be set |
| 170 |
// even when XSAVE is disabled (by the operating system). See the comment in |
| 171 |
// cpu_intel.c and check how the users of this bit use it. |
| 172 |
// |
| 173 |
// We do not use |__XSAVE__| for static detection because the hack in |
| 174 |
// |OPENSSL_cpuid_setup| for Knights Landing CPUs needs to override it. |
| 175 |
int hardware_supports_xsave = (OPENSSL_ia32cap_get()[1] & (1u << 26)) != 0; |
| 176 |
return !hardware_supports_xsave && CRYPTO_is_MOVBE_capable(); |
| 177 |
} |
| 178 |
|
| 179 |
#endif // OPENSSL_X86 || OPENSSL_X86_64 |
| 180 |
|
| 181 |
#if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 182 |
|
| 183 |
#if defined(OPENSSL_APPLE) && defined(OPENSSL_ARM) |
| 184 |
// We do not detect any features at runtime for Apple's 32-bit ARM platforms. On |
| 185 |
// 64-bit ARM, we detect some post-ARMv8.0 features. |
| 186 |
#define OPENSSL_STATIC_ARMCAP |
| 187 |
#endif |
| 188 |
|
| 189 |
#include <openssl/arm_arch.h> |
| 190 |
|
| 191 |
extern uint32_t OPENSSL_armcap_P; |
| 192 |
extern uint8_t OPENSSL_cpucap_initialized; |
| 193 |
|
| 194 |
// Normalize some older feature flags to their modern ACLE values. |
| 195 |
// https://developer.arm.com/architectures/system-architectures/software-standards/acle |
| 196 |
#if defined(__ARM_NEON__) && !defined(__ARM_NEON) |
| 197 |
#define __ARM_NEON 1 |
| 198 |
#endif |
| 199 |
#if defined(__ARM_FEATURE_CRYPTO) |
| 200 |
#if !defined(__ARM_FEATURE_AES) |
| 201 |
#define __ARM_FEATURE_AES 1 |
| 202 |
#endif |
| 203 |
#if !defined(__ARM_FEATURE_SHA2) |
| 204 |
#define __ARM_FEATURE_SHA2 1 |
| 205 |
#endif |
| 206 |
#endif |
| 207 |
|
| 208 |
// CRYPTO_is_NEON_capable returns true if the current CPU has a NEON unit. |
| 209 |
// If this is known statically, it is a constant inline function. |
| 210 |
// Otherwise, the capability is checked at runtime by checking the corresponding |
| 211 |
// bit in |OPENSSL_armcap_P|. This is also the same for |
| 212 |
// |CRYPTO_is_ARMv8_AES_capable| and |CRYPTO_is_ARMv8_PMULL_capable| |
| 213 |
// for checking the support for AES and PMULL instructions, respectively. |
| 214 |
OPENSSL_INLINE int CRYPTO_is_NEON_capable(void) { |
| 215 |
return (OPENSSL_armcap_P & ARMV7_NEON) != 0; |
| 216 |
} |
| 217 |
|
| 218 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_AES_capable(void) { |
| 219 |
return (OPENSSL_armcap_P & ARMV8_AES) != 0; |
| 220 |
} |
| 221 |
|
| 222 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_PMULL_capable(void) { |
| 223 |
return (OPENSSL_armcap_P & ARMV8_PMULL) != 0; |
| 224 |
} |
| 225 |
|
| 226 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA1_capable(void) { |
| 227 |
return (OPENSSL_armcap_P & ARMV8_SHA1) != 0; |
| 228 |
} |
| 229 |
|
| 230 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA256_capable(void) { |
| 231 |
return (OPENSSL_armcap_P & ARMV8_SHA256) != 0; |
| 232 |
} |
| 233 |
|
| 234 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA512_capable(void) { |
| 235 |
return (OPENSSL_armcap_P & ARMV8_SHA512) != 0; |
| 236 |
} |
| 237 |
|
| 238 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_SHA3_capable(void) { |
| 239 |
return (OPENSSL_armcap_P & ARMV8_SHA3) != 0; |
| 240 |
} |
| 241 |
|
| 242 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_GCM_8x_capable(void) { |
| 243 |
return (CRYPTO_is_ARMv8_SHA3_capable() && |
| 244 |
((OPENSSL_armcap_P & ARMV8_NEOVERSE_V1) != 0 || |
| 245 |
(OPENSSL_armcap_P & ARMV8_NEOVERSE_V2) != 0 || |
| 246 |
(OPENSSL_armcap_P & ARMV8_APPLE_M) != 0)); |
| 247 |
} |
| 248 |
|
| 249 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_wide_multiplier_capable(void) { |
| 250 |
return (OPENSSL_armcap_P & ARMV8_NEOVERSE_V1) != 0 || |
| 251 |
(OPENSSL_armcap_P & ARMV8_NEOVERSE_V2) != 0 || |
| 252 |
(OPENSSL_armcap_P & ARMV8_APPLE_M) != 0; |
| 253 |
} |
| 254 |
|
| 255 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_DIT_capable(void) { |
| 256 |
return (OPENSSL_armcap_P & (ARMV8_DIT | ARMV8_DIT_ALLOWED)) == |
| 257 |
(ARMV8_DIT | ARMV8_DIT_ALLOWED); |
| 258 |
} |
| 259 |
|
| 260 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_RNDR_capable(void) { |
| 261 |
return (OPENSSL_armcap_P & ARMV8_RNG) != 0; |
| 262 |
} |
| 263 |
|
| 264 |
OPENSSL_INLINE int CRYPTO_is_Neoverse_N1(void) { |
| 265 |
// It is Neoverse N1 if only ARMV8_NEOVERSE_N1 = 1 and |
| 266 |
// ARMV8_NEOVERSE_<V1|V2> = 0. |
| 267 |
return ((OPENSSL_armcap_P & ARMV8_NEOVERSE_N1) != 0 && |
| 268 |
(OPENSSL_armcap_P & (ARMV8_NEOVERSE_V1 | ARMV8_NEOVERSE_V2)) == 0); |
| 269 |
} |
| 270 |
|
| 271 |
OPENSSL_INLINE int CRYPTO_is_Neoverse_V1(void) { |
| 272 |
return (OPENSSL_armcap_P & ARMV8_NEOVERSE_V1) != 0; |
| 273 |
} |
| 274 |
|
| 275 |
OPENSSL_INLINE int CRYPTO_is_Neoverse_V2(void) { |
| 276 |
return (OPENSSL_armcap_P & ARMV8_NEOVERSE_V2) != 0; |
| 277 |
} |
| 278 |
|
| 279 |
OPENSSL_INLINE int CRYPTO_is_ARMv8_Apple_M(void) { |
| 280 |
return (OPENSSL_armcap_P & ARMV8_APPLE_M) != 0; |
| 281 |
} |
| 282 |
|
| 283 |
// This function is used only for testing; hence, not inlined |
| 284 |
OPENSSL_EXPORT int CRYPTO_is_ARMv8_DIT_capable_for_testing(void); |
| 285 |
|
| 286 |
#endif // OPENSSL_ARM || OPENSSL_AARCH64 |
| 287 |
|
| 288 |
#if defined(AARCH64_DIT_SUPPORTED) |
| 289 |
// (TODO): See if we can detect the DIT capability in Windows environment |
| 290 |
|
| 291 |
// armv8_get_dit gets the value of the DIT flag from the CPU. |
| 292 |
OPENSSL_EXPORT uint64_t armv8_get_dit(void); |
| 293 |
|
| 294 |
// armv8_set_dit sets the CPU DIT flag to 1 and returns its original value |
| 295 |
// before it was called. |
| 296 |
OPENSSL_EXPORT uint64_t armv8_set_dit(void); |
| 297 |
|
| 298 |
// armv8_restore_dit takes as input a value to restore the CPU DIT flag to. |
| 299 |
OPENSSL_EXPORT void armv8_restore_dit(volatile uint64_t *original_dit); |
| 300 |
|
| 301 |
#if defined(ENABLE_AUTO_SET_RESET_DIT) |
| 302 |
// SET_DIT_AUTO_RESET can be inserted in the caller's application at |
| 303 |
// the beginning of the code section that makes repeated calls to AWS-LC |
| 304 |
// functions. The flag will be automatically restored to its original value |
| 305 |
// at the end of the scope. |
| 306 |
// This can minimise the effect on performance of repeatedly setting and |
| 307 |
// disabling DIT. |
| 308 |
// Instead of the macro, the functions above can be used. |
| 309 |
// An example of their usage is present in the benchmarking function |
| 310 |
// `Speed()` in `tool/speed.cc` when the option `-dit` is passed in. |
| 311 |
#define SET_DIT_AUTO_RESET \ |
| 312 |
volatile uint64_t _dit_restore_orig \ |
| 313 |
__attribute__((cleanup(armv8_restore_dit))) \ |
| 314 |
OPENSSL_UNUSED = armv8_set_dit(); |
| 315 |
|
| 316 |
#else |
| 317 |
#define SET_DIT_AUTO_RESET |
| 318 |
#endif // ENABLE_AUTO_SET_RESET_DIT |
| 319 |
|
| 320 |
#else |
| 321 |
#define SET_DIT_AUTO_RESET |
| 322 |
#endif // AARCH64_DIT_SUPPORTED |
| 323 |
|
| 324 |
#if defined(OPENSSL_PPC64LE) |
| 325 |
|
| 326 |
// CRYPTO_is_PPC64LE_vcrypto_capable returns true iff the current CPU supports |
| 327 |
// the Vector.AES category of instructions. |
| 328 |
int CRYPTO_is_PPC64LE_vcrypto_capable(void); |
| 329 |
|
| 330 |
extern unsigned long OPENSSL_ppc64le_hwcap2; |
| 331 |
|
| 332 |
#endif // OPENSSL_PPC64LE |
| 333 |
|
| 334 |
#if defined(__cplusplus) |
| 335 |
} |
| 336 |
#endif |
| 337 |
|
| 338 |
#endif // OPENSSL_HEADER_CPUCAP_INTERNAL_H |