2 * Copyright 2009-2022 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);
48 void OPENSSL_brd31_probe(void);
50 long OPENSSL_rdtsc_mftb(void);
51 long OPENSSL_rdtsc_mfspr268(void);
53 uint32_t OPENSSL_rdtsc(void)
55 if (OPENSSL_ppccap_P & PPC_MFTB)
56 return OPENSSL_rdtsc_mftb();
57 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
58 return OPENSSL_rdtsc_mfspr268();
63 size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
64 size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
66 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
68 if (OPENSSL_ppccap_P & PPC_MFTB)
69 return OPENSSL_instrument_bus_mftb(out, cnt);
70 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
71 return OPENSSL_instrument_bus_mfspr268(out, cnt);
76 size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
77 size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
79 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
81 if (OPENSSL_ppccap_P & PPC_MFTB)
82 return OPENSSL_instrument_bus2_mftb(out, cnt, max);
83 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
84 return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
89 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
90 # if __GLIBC_PREREQ(2, 16)
91 # include <sys/auxv.h>
92 # define OSSL_IMPLEMENT_GETAUXVAL
93 # elif defined(__ANDROID_API__)
94 /* see https://developer.android.google.cn/ndk/guides/cpu-features */
95 # if __ANDROID_API__ >= 18
96 # include <sys/auxv.h>
97 # define OSSL_IMPLEMENT_GETAUXVAL
102 #if defined(__FreeBSD__)
103 # include <sys/param.h>
104 # if __FreeBSD_version >= 1200000
105 # include <sys/auxv.h>
106 # define OSSL_IMPLEMENT_GETAUXVAL
108 static unsigned long getauxval(unsigned long key)
110 unsigned long val = 0ul;
112 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
120 /* I wish <sys/auxv.h> was universally available */
122 # define AT_HWCAP 16 /* AT_HWCAP */
124 #define HWCAP_PPC64 (1U << 30)
125 #define HWCAP_ALTIVEC (1U << 28)
126 #define HWCAP_FPU (1U << 27)
127 #define HWCAP_POWER6_EXT (1U << 9)
128 #define HWCAP_VSX (1U << 7)
131 # define AT_HWCAP2 26 /* AT_HWCAP2 */
133 #define HWCAP_VEC_CRYPTO (1U << 25)
134 #define HWCAP_ARCH_3_00 (1U << 23)
135 #define HWCAP_ARCH_3_1 (1U << 18)
137 # if defined(__GNUC__) && __GNUC__>=2
138 __attribute__ ((constructor))
140 void OPENSSL_cpuid_setup(void)
143 struct sigaction ill_oact, ill_act;
145 static int trigger = 0;
151 if ((e = getenv("OPENSSL_ppccap"))) {
152 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
156 OPENSSL_ppccap_P = 0;
159 OPENSSL_ppccap_P |= PPC_FPU;
161 if (sizeof(size_t) == 4) {
163 # if defined(_SC_AIX_KERNEL_BITMODE)
164 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
167 if (uname(&uts) != 0 || atoi(uts.version) < 6)
171 # if defined(__power_set)
173 * Value used in __power_set is a single-bit 1<<n one denoting
174 * specific processor class. Incidentally 0xffffffff<<n can be
175 * used to denote specific processor and its successors.
177 if (sizeof(size_t) == 4) {
178 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
179 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
180 OPENSSL_ppccap_P |= PPC_FPU64;
182 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
183 if (__power_set(0x1U<<14)) /* POWER6 */
184 OPENSSL_ppccap_P |= PPC_FPU64;
187 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
188 OPENSSL_ppccap_P |= PPC_ALTIVEC;
190 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
191 OPENSSL_ppccap_P |= PPC_CRYPTO207;
193 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
194 OPENSSL_ppccap_P |= PPC_MADD300;
196 if (__power_set(0xffffffffU<<18)) /* POWER10 and later */
197 OPENSSL_ppccap_P |= PPC_BRD31;
203 #if defined(__APPLE__) && defined(__MACH__)
204 OPENSSL_ppccap_P |= PPC_FPU;
208 size_t len = sizeof(val);
210 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
212 OPENSSL_ppccap_P |= PPC_FPU64;
216 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
218 OPENSSL_ppccap_P |= PPC_ALTIVEC;
225 #ifdef OSSL_IMPLEMENT_GETAUXVAL
227 unsigned long hwcap = getauxval(AT_HWCAP);
228 unsigned long hwcap2 = getauxval(AT_HWCAP2);
230 if (hwcap & HWCAP_FPU) {
231 OPENSSL_ppccap_P |= PPC_FPU;
233 if (sizeof(size_t) == 4) {
234 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
235 if (hwcap & HWCAP_PPC64)
236 OPENSSL_ppccap_P |= PPC_FPU64;
238 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
239 if (hwcap & HWCAP_POWER6_EXT)
240 OPENSSL_ppccap_P |= PPC_FPU64;
244 if (hwcap & HWCAP_ALTIVEC) {
245 OPENSSL_ppccap_P |= PPC_ALTIVEC;
247 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
248 OPENSSL_ppccap_P |= PPC_CRYPTO207;
251 if (hwcap2 & HWCAP_ARCH_3_00) {
252 OPENSSL_ppccap_P |= PPC_MADD300;
255 if (hwcap2 & HWCAP_ARCH_3_1) {
256 OPENSSL_ppccap_P |= PPC_BRD31;
261 sigfillset(&all_masked);
262 sigdelset(&all_masked, SIGILL);
263 sigdelset(&all_masked, SIGTRAP);
265 sigdelset(&all_masked, SIGEMT);
267 sigdelset(&all_masked, SIGFPE);
268 sigdelset(&all_masked, SIGBUS);
269 sigdelset(&all_masked, SIGSEGV);
271 memset(&ill_act, 0, sizeof(ill_act));
272 ill_act.sa_handler = ill_handler;
273 ill_act.sa_mask = all_masked;
275 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
276 sigaction(SIGILL, &ill_act, &ill_oact);
278 #ifndef OSSL_IMPLEMENT_GETAUXVAL
279 if (sigsetjmp(ill_jmp, 1) == 0) {
281 OPENSSL_ppccap_P |= PPC_FPU;
283 if (sizeof(size_t) == 4) {
286 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
288 if (sigsetjmp(ill_jmp, 1) == 0) {
289 OPENSSL_ppc64_probe();
290 OPENSSL_ppccap_P |= PPC_FPU64;
294 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
299 if (sigsetjmp(ill_jmp, 1) == 0) {
300 OPENSSL_altivec_probe();
301 OPENSSL_ppccap_P |= PPC_ALTIVEC;
302 if (sigsetjmp(ill_jmp, 1) == 0) {
303 OPENSSL_crypto207_probe();
304 OPENSSL_ppccap_P |= PPC_CRYPTO207;
308 if (sigsetjmp(ill_jmp, 1) == 0) {
309 OPENSSL_madd300_probe();
310 OPENSSL_ppccap_P |= PPC_MADD300;
314 if (sigsetjmp(ill_jmp, 1) == 0) {
315 OPENSSL_rdtsc_mftb();
316 OPENSSL_ppccap_P |= PPC_MFTB;
317 } else if (sigsetjmp(ill_jmp, 1) == 0) {
318 OPENSSL_rdtsc_mfspr268();
319 OPENSSL_ppccap_P |= PPC_MFSPR268;
322 sigaction(SIGILL, &ill_oact, NULL);
323 sigprocmask(SIG_SETMASK, &oset, NULL);