1 /* 2 * Copyright 2011-2017 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <setjmp.h> 14 #include <signal.h> 15 #include <openssl/crypto.h> 16 17 #include "arm_arch.h" 18 19 unsigned int OPENSSL_armcap_P = 0; 20 21 #if __ARM_MAX_ARCH__<7 22 void OPENSSL_cpuid_setup(void) 23 { 24 } 25 26 unsigned long OPENSSL_rdtsc(void) 27 { 28 return 0; 29 } 30 #else 31 static sigset_t all_masked; 32 33 static sigjmp_buf ill_jmp; 34 static void ill_handler(int sig) 35 { 36 siglongjmp(ill_jmp, sig); 37 } 38 39 /* 40 * Following subroutines could have been inlined, but it's not all 41 * ARM compilers support inline assembler... 42 */ 43 #if __ARM_MAX_ARCH__>=7 44 void _armv7_neon_probe(void); 45 void _armv8_aes_probe(void); 46 void _armv8_sha1_probe(void); 47 void _armv8_sha256_probe(void); 48 void _armv8_pmull_probe(void); 49 unsigned long _armv7_tick(void); 50 #endif 51 52 unsigned long OPENSSL_rdtsc(void) 53 { 54 #if __ARM_MAX_ARCH__>=7 55 if (OPENSSL_armcap_P & ARMV7_TICK) 56 return _armv7_tick(); 57 else 58 #endif 59 return 0; 60 } 61 62 # if defined(__GNUC__) && __GNUC__>=2 63 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor)); 64 # endif 65 /* 66 * Use a weak reference to getauxval() so we can use it if it is available but 67 * don't break the build if it is not. 68 */ 69 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) 70 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak)); 71 # else 72 static unsigned long (*getauxval) (unsigned long) = NULL; 73 # endif 74 75 /* 76 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas 77 * AArch64 used AT_HWCAP. 78 */ 79 # if defined(__arm__) || defined (__arm) 80 # define HWCAP 16 81 /* AT_HWCAP */ 82 # define HWCAP_NEON (1 << 12) 83 84 # define HWCAP_CE 26 85 /* AT_HWCAP2 */ 86 # define HWCAP_CE_AES (1 << 0) 87 # define HWCAP_CE_PMULL (1 << 1) 88 # define HWCAP_CE_SHA1 (1 << 2) 89 # define HWCAP_CE_SHA256 (1 << 3) 90 # elif defined(__aarch64__) 91 # define HWCAP 16 92 /* AT_HWCAP */ 93 # define HWCAP_NEON (1 << 1) 94 95 # define HWCAP_CE HWCAP 96 # define HWCAP_CE_AES (1 << 3) 97 # define HWCAP_CE_PMULL (1 << 4) 98 # define HWCAP_CE_SHA1 (1 << 5) 99 # define HWCAP_CE_SHA256 (1 << 6) 100 # endif 101 102 void OPENSSL_cpuid_setup(void) 103 { 104 char *e; 105 struct sigaction ill_oact, ill_act; 106 sigset_t oset; 107 static int trigger = 0; 108 109 if (trigger) 110 return; 111 trigger = 1; 112 113 if ((e = getenv("OPENSSL_armcap"))) { 114 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0); 115 return; 116 } 117 118 # if defined(__APPLE__) && !defined(__aarch64__) 119 /* 120 * Capability probing by catching SIGILL appears to be problematic 121 * on iOS. But since Apple universe is "monocultural", it's actually 122 * possible to simply set pre-defined processor capability mask. 123 */ 124 if (1) { 125 OPENSSL_armcap_P = ARMV7_NEON; 126 return; 127 } 128 /* 129 * One could do same even for __aarch64__ iOS builds. It's not done 130 * exclusively for reasons of keeping code unified across platforms. 131 * Unified code works because it never triggers SIGILL on Apple 132 * devices... 133 */ 134 # endif 135 136 sigfillset(&all_masked); 137 sigdelset(&all_masked, SIGILL); 138 sigdelset(&all_masked, SIGTRAP); 139 sigdelset(&all_masked, SIGFPE); 140 sigdelset(&all_masked, SIGBUS); 141 sigdelset(&all_masked, SIGSEGV); 142 143 OPENSSL_armcap_P = 0; 144 145 memset(&ill_act, 0, sizeof(ill_act)); 146 ill_act.sa_handler = ill_handler; 147 ill_act.sa_mask = all_masked; 148 149 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset); 150 sigaction(SIGILL, &ill_act, &ill_oact); 151 152 #if __ARM_MAX_ARCH__>=7 153 if (getauxval != NULL) { 154 if (getauxval(HWCAP) & HWCAP_NEON) { 155 unsigned long hwcap = getauxval(HWCAP_CE); 156 157 OPENSSL_armcap_P |= ARMV7_NEON; 158 159 if (hwcap & HWCAP_CE_AES) 160 OPENSSL_armcap_P |= ARMV8_AES; 161 162 if (hwcap & HWCAP_CE_PMULL) 163 OPENSSL_armcap_P |= ARMV8_PMULL; 164 165 if (hwcap & HWCAP_CE_SHA1) 166 OPENSSL_armcap_P |= ARMV8_SHA1; 167 168 if (hwcap & HWCAP_CE_SHA256) 169 OPENSSL_armcap_P |= ARMV8_SHA256; 170 } 171 } else if (sigsetjmp(ill_jmp, 1) == 0) { 172 _armv7_neon_probe(); 173 OPENSSL_armcap_P |= ARMV7_NEON; 174 if (sigsetjmp(ill_jmp, 1) == 0) { 175 _armv8_pmull_probe(); 176 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES; 177 } else if (sigsetjmp(ill_jmp, 1) == 0) { 178 _armv8_aes_probe(); 179 OPENSSL_armcap_P |= ARMV8_AES; 180 } 181 if (sigsetjmp(ill_jmp, 1) == 0) { 182 _armv8_sha1_probe(); 183 OPENSSL_armcap_P |= ARMV8_SHA1; 184 } 185 if (sigsetjmp(ill_jmp, 1) == 0) { 186 _armv8_sha256_probe(); 187 OPENSSL_armcap_P |= ARMV8_SHA256; 188 } 189 } 190 if (sigsetjmp(ill_jmp, 1) == 0) { 191 _armv7_tick(); 192 OPENSSL_armcap_P |= ARMV7_TICK; 193 } 194 #endif 195 196 sigaction(SIGILL, &ill_oact, NULL); 197 sigprocmask(SIG_SETMASK, &oset, NULL); 198 } 199 #endif 200