2 * Copyright 2009-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 #if defined(__APPLE__) && defined(__MACH__)
26 # include <sys/types.h>
27 # include <sys/sysctl.h>
29 #include <openssl/crypto.h>
30 #include "internal/cryptlib.h"
31 #include "crypto/ppc_arch.h"
33 unsigned int OPENSSL_ppccap_P = 0;
35 static sigset_t all_masked;
37 static sigjmp_buf ill_jmp;
38 static void ill_handler(int sig)
40 siglongjmp(ill_jmp, sig);
43 void OPENSSL_fpu_probe(void);
44 void OPENSSL_ppc64_probe(void);
45 void OPENSSL_altivec_probe(void);
46 void OPENSSL_crypto207_probe(void);
47 void OPENSSL_madd300_probe(void);
49 long OPENSSL_rdtsc_mftb(void);
50 long OPENSSL_rdtsc_mfspr268(void);
52 uint32_t OPENSSL_rdtsc(void)
54 if (OPENSSL_ppccap_P & PPC_MFTB)
55 return OPENSSL_rdtsc_mftb();
56 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
57 return OPENSSL_rdtsc_mfspr268();
62 size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
63 size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
65 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
67 if (OPENSSL_ppccap_P & PPC_MFTB)
68 return OPENSSL_instrument_bus_mftb(out, cnt);
69 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
70 return OPENSSL_instrument_bus_mfspr268(out, cnt);
75 size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
76 size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
78 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
80 if (OPENSSL_ppccap_P & PPC_MFTB)
81 return OPENSSL_instrument_bus2_mftb(out, cnt, max);
82 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
83 return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
88 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
89 # if __GLIBC_PREREQ(2, 16)
90 # include <sys/auxv.h>
91 # define OSSL_IMPLEMENT_GETAUXVAL
92 # elif defined(__ANDROID_API__)
93 /* see https://developer.android.google.cn/ndk/guides/cpu-features */
94 # if __ANDROID_API__ >= 18
95 # include <sys/auxv.h>
96 # define OSSL_IMPLEMENT_GETAUXVAL
101 #if defined(__FreeBSD__)
102 # include <sys/param.h>
103 # if __FreeBSD_version >= 1200000
104 # include <sys/auxv.h>
105 # define OSSL_IMPLEMENT_GETAUXVAL
107 static unsigned long getauxval(unsigned long key)
109 unsigned long val = 0ul;
111 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
119 /* I wish <sys/auxv.h> was universally available */
120 #define HWCAP 16 /* AT_HWCAP */
121 #define HWCAP_PPC64 (1U << 30)
122 #define HWCAP_ALTIVEC (1U << 28)
123 #define HWCAP_FPU (1U << 27)
124 #define HWCAP_POWER6_EXT (1U << 9)
125 #define HWCAP_VSX (1U << 7)
127 #define HWCAP2 26 /* AT_HWCAP2 */
128 #define HWCAP_VEC_CRYPTO (1U << 25)
129 #define HWCAP_ARCH_3_00 (1U << 23)
131 # if defined(__GNUC__) && __GNUC__>=2
132 __attribute__ ((constructor))
134 void OPENSSL_cpuid_setup(void)
137 struct sigaction ill_oact, ill_act;
139 static int trigger = 0;
145 if ((e = getenv("OPENSSL_ppccap"))) {
146 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
150 OPENSSL_ppccap_P = 0;
153 OPENSSL_ppccap_P |= PPC_FPU;
155 if (sizeof(size_t) == 4) {
157 # if defined(_SC_AIX_KERNEL_BITMODE)
158 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
161 if (uname(&uts) != 0 || atoi(uts.version) < 6)
165 # if defined(__power_set)
167 * Value used in __power_set is a single-bit 1<<n one denoting
168 * specific processor class. Incidentally 0xffffffff<<n can be
169 * used to denote specific processor and its successors.
171 if (sizeof(size_t) == 4) {
172 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
173 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
174 OPENSSL_ppccap_P |= PPC_FPU64;
176 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
177 if (__power_set(0x1U<<14)) /* POWER6 */
178 OPENSSL_ppccap_P |= PPC_FPU64;
181 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
182 OPENSSL_ppccap_P |= PPC_ALTIVEC;
184 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
185 OPENSSL_ppccap_P |= PPC_CRYPTO207;
187 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
188 OPENSSL_ppccap_P |= PPC_MADD300;
194 #if defined(__APPLE__) && defined(__MACH__)
195 OPENSSL_ppccap_P |= PPC_FPU;
199 size_t len = sizeof(val);
201 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
203 OPENSSL_ppccap_P |= PPC_FPU64;
207 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
209 OPENSSL_ppccap_P |= PPC_ALTIVEC;
216 #ifdef OSSL_IMPLEMENT_GETAUXVAL
218 unsigned long hwcap = getauxval(HWCAP);
219 unsigned long hwcap2 = getauxval(HWCAP2);
221 if (hwcap & HWCAP_FPU) {
222 OPENSSL_ppccap_P |= PPC_FPU;
224 if (sizeof(size_t) == 4) {
225 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
226 if (hwcap & HWCAP_PPC64)
227 OPENSSL_ppccap_P |= PPC_FPU64;
229 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
230 if (hwcap & HWCAP_POWER6_EXT)
231 OPENSSL_ppccap_P |= PPC_FPU64;
235 if (hwcap & HWCAP_ALTIVEC) {
236 OPENSSL_ppccap_P |= PPC_ALTIVEC;
238 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
239 OPENSSL_ppccap_P |= PPC_CRYPTO207;
242 if (hwcap2 & HWCAP_ARCH_3_00) {
243 OPENSSL_ppccap_P |= PPC_MADD300;
248 sigfillset(&all_masked);
249 sigdelset(&all_masked, SIGILL);
250 sigdelset(&all_masked, SIGTRAP);
252 sigdelset(&all_masked, SIGEMT);
254 sigdelset(&all_masked, SIGFPE);
255 sigdelset(&all_masked, SIGBUS);
256 sigdelset(&all_masked, SIGSEGV);
258 memset(&ill_act, 0, sizeof(ill_act));
259 ill_act.sa_handler = ill_handler;
260 ill_act.sa_mask = all_masked;
262 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
263 sigaction(SIGILL, &ill_act, &ill_oact);
265 #ifndef OSSL_IMPLEMENT_GETAUXVAL
266 if (sigsetjmp(ill_jmp, 1) == 0) {
268 OPENSSL_ppccap_P |= PPC_FPU;
270 if (sizeof(size_t) == 4) {
273 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
275 if (sigsetjmp(ill_jmp, 1) == 0) {
276 OPENSSL_ppc64_probe();
277 OPENSSL_ppccap_P |= PPC_FPU64;
281 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
286 if (sigsetjmp(ill_jmp, 1) == 0) {
287 OPENSSL_altivec_probe();
288 OPENSSL_ppccap_P |= PPC_ALTIVEC;
289 if (sigsetjmp(ill_jmp, 1) == 0) {
290 OPENSSL_crypto207_probe();
291 OPENSSL_ppccap_P |= PPC_CRYPTO207;
295 if (sigsetjmp(ill_jmp, 1) == 0) {
296 OPENSSL_madd300_probe();
297 OPENSSL_ppccap_P |= PPC_MADD300;
301 if (sigsetjmp(ill_jmp, 1) == 0) {
302 OPENSSL_rdtsc_mftb();
303 OPENSSL_ppccap_P |= PPC_MFTB;
304 } else if (sigsetjmp(ill_jmp, 1) == 0) {
305 OPENSSL_rdtsc_mfspr268();
306 OPENSSL_ppccap_P |= PPC_MFSPR268;
309 sigaction(SIGILL, &ill_oact, NULL);
310 sigprocmask(SIG_SETMASK, &oset, NULL);