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 #if defined(__APPLE__) && defined(__MACH__)
26 # include <sys/types.h>
27 # include <sys/sysctl.h>
29 #include <openssl/crypto.h>
30 #include <openssl/bn.h>
34 unsigned int OPENSSL_ppccap_P = 0;
36 static sigset_t all_masked;
38 #ifdef OPENSSL_BN_ASM_MONT
39 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
40 const BN_ULONG *np, const BN_ULONG *n0, int num)
42 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap,
43 const BN_ULONG *bp, const BN_ULONG *np,
44 const BN_ULONG *n0, int num);
45 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
46 const BN_ULONG *np, const BN_ULONG *n0, int num);
48 if (sizeof(size_t) == 4) {
49 # if 1 || (defined(__APPLE__) && defined(__MACH__))
50 if (num >= 8 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64))
51 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
54 * boundary of 32 was experimentally determined on Linux 2.6.22,
55 * might have to be adjusted on AIX...
57 if (num >= 32 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64)) {
61 sigprocmask(SIG_SETMASK, &all_masked, &oset);
62 ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
63 sigprocmask(SIG_SETMASK, &oset, NULL);
68 } else if ((OPENSSL_ppccap_P & PPC_FPU64))
70 * this is a "must" on POWER6, but run-time detection is not
73 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
75 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
79 void sha256_block_p8(void *ctx, const void *inp, size_t len);
80 void sha256_block_ppc(void *ctx, const void *inp, size_t len);
81 void sha256_block_data_order(void *ctx, const void *inp, size_t len)
83 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
84 sha256_block_ppc(ctx, inp, len);
87 void sha512_block_p8(void *ctx, const void *inp, size_t len);
88 void sha512_block_ppc(void *ctx, const void *inp, size_t len);
89 void sha512_block_data_order(void *ctx, const void *inp, size_t len)
91 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
92 sha512_block_ppc(ctx, inp, len);
95 #ifndef OPENSSL_NO_CHACHA
96 void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
97 size_t len, const unsigned int key[8],
98 const unsigned int counter[4]);
99 void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
100 size_t len, const unsigned int key[8],
101 const unsigned int counter[4]);
102 void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
103 size_t len, const unsigned int key[8],
104 const unsigned int counter[4])
106 OPENSSL_ppccap_P & PPC_ALTIVEC
107 ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
108 : ChaCha20_ctr32_int(out, inp, len, key, counter);
112 #ifndef OPENSSL_NO_POLY1305
113 void poly1305_init_int(void *ctx, const unsigned char key[16]);
114 void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
115 unsigned int padbit);
116 void poly1305_emit(void *ctx, unsigned char mac[16],
117 const unsigned int nonce[4]);
118 void poly1305_init_fpu(void *ctx, const unsigned char key[16]);
119 void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len,
120 unsigned int padbit);
121 void poly1305_emit_fpu(void *ctx, unsigned char mac[16],
122 const unsigned int nonce[4]);
123 int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
125 if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
126 poly1305_init_fpu(ctx, key);
127 func[0] = poly1305_blocks_fpu;
128 func[1] = poly1305_emit_fpu;
130 poly1305_init_int(ctx, key);
131 func[0] = poly1305_blocks;
132 func[1] = poly1305_emit;
138 static sigjmp_buf ill_jmp;
139 static void ill_handler(int sig)
141 siglongjmp(ill_jmp, sig);
144 void OPENSSL_fpu_probe(void);
145 void OPENSSL_ppc64_probe(void);
146 void OPENSSL_altivec_probe(void);
147 void OPENSSL_crypto207_probe(void);
148 void OPENSSL_madd300_probe(void);
151 * Use a weak reference to getauxval() so we can use it if it is available
152 * but don't break the build if it is not. Note that this is *link-time*
153 * feature detection, not *run-time*. In other words if we link with
154 * symbol present, it's expected to be present even at run-time.
156 #if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
157 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
159 static unsigned long (*getauxval) (unsigned long) = NULL;
162 /* I wish <sys/auxv.h> was universally available */
163 #define HWCAP 16 /* AT_HWCAP */
164 #define HWCAP_PPC64 (1U << 30)
165 #define HWCAP_ALTIVEC (1U << 28)
166 #define HWCAP_FPU (1U << 27)
167 #define HWCAP_POWER6_EXT (1U << 9)
168 #define HWCAP_VSX (1U << 7)
170 #define HWCAP2 26 /* AT_HWCAP2 */
171 #define HWCAP_VEC_CRYPTO (1U << 25)
172 #define HWCAP_ARCH_3_00 (1U << 23)
174 # if defined(__GNUC__) && __GNUC__>=2
175 __attribute__ ((constructor))
177 void OPENSSL_cpuid_setup(void)
180 struct sigaction ill_oact, ill_act;
182 static int trigger = 0;
188 if ((e = getenv("OPENSSL_ppccap"))) {
189 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
193 OPENSSL_ppccap_P = 0;
196 OPENSSL_ppccap_P |= PPC_FPU;
198 if (sizeof(size_t) == 4) {
200 # if defined(_SC_AIX_KERNEL_BITMODE)
201 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
204 if (uname(&uts) != 0 || atoi(uts.version) < 6)
208 # if defined(__power_set)
210 * Value used in __power_set is a single-bit 1<<n one denoting
211 * specific processor class. Incidentally 0xffffffff<<n can be
212 * used to denote specific processor and its successors.
214 if (sizeof(size_t) == 4) {
215 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
216 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
217 OPENSSL_ppccap_P |= PPC_FPU64;
219 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
220 if (__power_set(0x1U<<14)) /* POWER6 */
221 OPENSSL_ppccap_P |= PPC_FPU64;
224 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
225 OPENSSL_ppccap_P |= PPC_ALTIVEC;
227 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
228 OPENSSL_ppccap_P |= PPC_CRYPTO207;
230 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
231 OPENSSL_ppccap_P |= PPC_MADD300;
237 #if defined(__APPLE__) && defined(__MACH__)
238 OPENSSL_ppccap_P |= PPC_FPU;
242 size_t len = sizeof(val);
244 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
246 OPENSSL_ppccap_P |= PPC_FPU64;
250 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
252 OPENSSL_ppccap_P |= PPC_ALTIVEC;
259 if (getauxval != NULL) {
260 unsigned long hwcap = getauxval(HWCAP);
262 if (hwcap & HWCAP_FPU) {
263 OPENSSL_ppccap_P |= PPC_FPU;
265 if (sizeof(size_t) == 4) {
266 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
267 if (hwcap & HWCAP_PPC64)
268 OPENSSL_ppccap_P |= PPC_FPU64;
270 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
271 if (hwcap & HWCAP_POWER6_EXT)
272 OPENSSL_ppccap_P |= PPC_FPU64;
276 if (hwcap & HWCAP_ALTIVEC) {
277 OPENSSL_ppccap_P |= PPC_ALTIVEC;
279 if ((hwcap & HWCAP_VSX) && (getauxval(HWCAP2) & HWCAP_VEC_CRYPTO))
280 OPENSSL_ppccap_P |= PPC_CRYPTO207;
283 if (hwcap & HWCAP_ARCH_3_00) {
284 OPENSSL_ppccap_P |= PPC_MADD300;
290 sigfillset(&all_masked);
291 sigdelset(&all_masked, SIGILL);
292 sigdelset(&all_masked, SIGTRAP);
294 sigdelset(&all_masked, SIGEMT);
296 sigdelset(&all_masked, SIGFPE);
297 sigdelset(&all_masked, SIGBUS);
298 sigdelset(&all_masked, SIGSEGV);
300 memset(&ill_act, 0, sizeof(ill_act));
301 ill_act.sa_handler = ill_handler;
302 ill_act.sa_mask = all_masked;
304 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
305 sigaction(SIGILL, &ill_act, &ill_oact);
307 if (sigsetjmp(ill_jmp,1) == 0) {
309 OPENSSL_ppccap_P |= PPC_FPU;
311 if (sizeof(size_t) == 4) {
314 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
316 if (sigsetjmp(ill_jmp, 1) == 0) {
317 OPENSSL_ppc64_probe();
318 OPENSSL_ppccap_P |= PPC_FPU64;
322 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
327 if (sigsetjmp(ill_jmp, 1) == 0) {
328 OPENSSL_altivec_probe();
329 OPENSSL_ppccap_P |= PPC_ALTIVEC;
330 if (sigsetjmp(ill_jmp, 1) == 0) {
331 OPENSSL_crypto207_probe();
332 OPENSSL_ppccap_P |= PPC_CRYPTO207;
336 if (sigsetjmp(ill_jmp, 1) == 0) {
337 OPENSSL_madd300_probe();
338 OPENSSL_ppccap_P |= PPC_MADD300;
341 sigaction(SIGILL, &ill_oact, NULL);
342 sigprocmask(SIG_SETMASK, &oset, NULL);