2 * Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
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
16 #if defined(__linux) || defined(_AIX)
17 # include <sys/utsname.h>
19 #if defined(_AIX53) /* defined even on post-5.3 */
20 # include <sys/systemcfg.h>
21 # if !defined(__power_set)
22 # define __power_set(a) (_system_configuration.implementation & (a))
25 #include <openssl/crypto.h>
29 unsigned int OPENSSL_ppccap_P = 0;
31 static sigset_t all_masked;
33 static sigjmp_buf ill_jmp;
34 static void ill_handler(int sig)
36 siglongjmp(ill_jmp, sig);
39 void OPENSSL_fpu_probe(void);
40 void OPENSSL_ppc64_probe(void);
41 void OPENSSL_altivec_probe(void);
42 void OPENSSL_crypto207_probe(void);
43 void OPENSSL_madd300_probe(void);
46 * Use a weak reference to getauxval() so we can use it if it is available
47 * but don't break the build if it is not. Note that this is *link-time*
48 * feature detection, not *run-time*. In other words if we link with
49 * symbol present, it's expected to be present even at run-time.
51 #if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
52 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
54 static unsigned long (*getauxval) (unsigned long) = NULL;
57 /* I wish <sys/auxv.h> was universally available */
58 #define HWCAP 16 /* AT_HWCAP */
59 #define HWCAP_PPC64 (1U << 30)
60 #define HWCAP_ALTIVEC (1U << 28)
61 #define HWCAP_FPU (1U << 27)
62 #define HWCAP_POWER6_EXT (1U << 9)
63 #define HWCAP_VSX (1U << 7)
65 #define HWCAP2 26 /* AT_HWCAP2 */
66 #define HWCAP_VEC_CRYPTO (1U << 25)
67 #define HWCAP_ARCH_3_00 (1U << 23)
69 # if defined(__GNUC__) && __GNUC__>=2
70 __attribute__ ((constructor))
72 void OPENSSL_cpuid_setup(void)
75 struct sigaction ill_oact, ill_act;
77 static int trigger = 0;
83 if ((e = getenv("OPENSSL_ppccap"))) {
84 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
91 OPENSSL_ppccap_P |= PPC_FPU;
93 if (sizeof(size_t) == 4) {
95 # if defined(_SC_AIX_KERNEL_BITMODE)
96 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
99 if (uname(&uts) != 0 || atoi(uts.version) < 6)
103 # if defined(__power_set)
105 * Value used in __power_set is a single-bit 1<<n one denoting
106 * specific processor class. Incidentally 0xffffffff<<n can be
107 * used to denote specific processor and its successors.
109 if (sizeof(size_t) == 4) {
110 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
111 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
112 OPENSSL_ppccap_P |= PPC_FPU64;
114 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
115 if (__power_set(0x1U<<14)) /* POWER6 */
116 OPENSSL_ppccap_P |= PPC_FPU64;
119 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
120 OPENSSL_ppccap_P |= PPC_ALTIVEC;
122 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
123 OPENSSL_ppccap_P |= PPC_CRYPTO207;
125 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
126 OPENSSL_ppccap_P |= PPC_MADD300;
132 if (getauxval != NULL) {
133 unsigned long hwcap = getauxval(HWCAP);
135 if (hwcap & HWCAP_FPU) {
136 OPENSSL_ppccap_P |= PPC_FPU;
138 if (sizeof(size_t) == 4) {
139 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
140 if (hwcap & HWCAP_PPC64)
141 OPENSSL_ppccap_P |= PPC_FPU64;
143 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
144 if (hwcap & HWCAP_POWER6_EXT)
145 OPENSSL_ppccap_P |= PPC_FPU64;
149 if (hwcap & HWCAP_ALTIVEC) {
150 OPENSSL_ppccap_P |= PPC_ALTIVEC;
152 if ((hwcap & HWCAP_VSX) && (getauxval(HWCAP2) & HWCAP_VEC_CRYPTO))
153 OPENSSL_ppccap_P |= PPC_CRYPTO207;
156 if (hwcap & HWCAP_ARCH_3_00) {
157 OPENSSL_ppccap_P |= PPC_MADD300;
163 sigfillset(&all_masked);
164 sigdelset(&all_masked, SIGILL);
165 sigdelset(&all_masked, SIGTRAP);
167 sigdelset(&all_masked, SIGEMT);
169 sigdelset(&all_masked, SIGFPE);
170 sigdelset(&all_masked, SIGBUS);
171 sigdelset(&all_masked, SIGSEGV);
173 memset(&ill_act, 0, sizeof(ill_act));
174 ill_act.sa_handler = ill_handler;
175 ill_act.sa_mask = all_masked;
177 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
178 sigaction(SIGILL, &ill_act, &ill_oact);
180 if (sigsetjmp(ill_jmp,1) == 0) {
182 OPENSSL_ppccap_P |= PPC_FPU;
184 if (sizeof(size_t) == 4) {
187 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
189 if (sigsetjmp(ill_jmp, 1) == 0) {
190 OPENSSL_ppc64_probe();
191 OPENSSL_ppccap_P |= PPC_FPU64;
195 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
200 if (sigsetjmp(ill_jmp, 1) == 0) {
201 OPENSSL_altivec_probe();
202 OPENSSL_ppccap_P |= PPC_ALTIVEC;
203 if (sigsetjmp(ill_jmp, 1) == 0) {
204 OPENSSL_crypto207_probe();
205 OPENSSL_ppccap_P |= PPC_CRYPTO207;
209 if (sigsetjmp(ill_jmp, 1) == 0) {
210 OPENSSL_madd300_probe();
211 OPENSSL_ppccap_P |= PPC_MADD300;
214 sigaction(SIGILL, &ill_oact, NULL);
215 sigprocmask(SIG_SETMASK, &oset, NULL);