]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ppccap.c
Move algorithm specific ppccap code from crypto/ppccap.c
[thirdparty/openssl.git] / crypto / ppccap.c
1 /*
2 * Copyright 2009-2016 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 <unistd.h>
16 #if defined(__linux) || defined(_AIX)
17 # include <sys/utsname.h>
18 #endif
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))
23 # endif
24 #endif
25 #include <openssl/crypto.h>
26
27 #include "ppc_arch.h"
28
29 unsigned int OPENSSL_ppccap_P = 0;
30
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 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);
44
45 /*
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.
50 */
51 #if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
52 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
53 #else
54 static unsigned long (*getauxval) (unsigned long) = NULL;
55 #endif
56
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)
64
65 #define HWCAP2 26 /* AT_HWCAP2 */
66 #define HWCAP_VEC_CRYPTO (1U << 25)
67 #define HWCAP_ARCH_3_00 (1U << 23)
68
69 # if defined(__GNUC__) && __GNUC__>=2
70 __attribute__ ((constructor))
71 # endif
72 void OPENSSL_cpuid_setup(void)
73 {
74 char *e;
75 struct sigaction ill_oact, ill_act;
76 sigset_t oset;
77 static int trigger = 0;
78
79 if (trigger)
80 return;
81 trigger = 1;
82
83 if ((e = getenv("OPENSSL_ppccap"))) {
84 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
85 return;
86 }
87
88 OPENSSL_ppccap_P = 0;
89
90 #if defined(_AIX)
91 OPENSSL_ppccap_P |= PPC_FPU;
92
93 if (sizeof(size_t) == 4) {
94 struct utsname uts;
95 # if defined(_SC_AIX_KERNEL_BITMODE)
96 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
97 return;
98 # endif
99 if (uname(&uts) != 0 || atoi(uts.version) < 6)
100 return;
101 }
102
103 # if defined(__power_set)
104 /*
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.
108 */
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;
113 } else {
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;
117 }
118
119 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
120 OPENSSL_ppccap_P |= PPC_ALTIVEC;
121
122 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
123 OPENSSL_ppccap_P |= PPC_CRYPTO207;
124
125 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
126 OPENSSL_ppccap_P |= PPC_MADD300;
127
128 return;
129 # endif
130 #endif
131
132 if (getauxval != NULL) {
133 unsigned long hwcap = getauxval(HWCAP);
134
135 if (hwcap & HWCAP_FPU) {
136 OPENSSL_ppccap_P |= PPC_FPU;
137
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;
142 } else {
143 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
144 if (hwcap & HWCAP_POWER6_EXT)
145 OPENSSL_ppccap_P |= PPC_FPU64;
146 }
147 }
148
149 if (hwcap & HWCAP_ALTIVEC) {
150 OPENSSL_ppccap_P |= PPC_ALTIVEC;
151
152 if ((hwcap & HWCAP_VSX) && (getauxval(HWCAP2) & HWCAP_VEC_CRYPTO))
153 OPENSSL_ppccap_P |= PPC_CRYPTO207;
154 }
155
156 if (hwcap & HWCAP_ARCH_3_00) {
157 OPENSSL_ppccap_P |= PPC_MADD300;
158 }
159
160 return;
161 }
162
163 sigfillset(&all_masked);
164 sigdelset(&all_masked, SIGILL);
165 sigdelset(&all_masked, SIGTRAP);
166 #ifdef SIGEMT
167 sigdelset(&all_masked, SIGEMT);
168 #endif
169 sigdelset(&all_masked, SIGFPE);
170 sigdelset(&all_masked, SIGBUS);
171 sigdelset(&all_masked, SIGSEGV);
172
173 memset(&ill_act, 0, sizeof(ill_act));
174 ill_act.sa_handler = ill_handler;
175 ill_act.sa_mask = all_masked;
176
177 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
178 sigaction(SIGILL, &ill_act, &ill_oact);
179
180 if (sigsetjmp(ill_jmp,1) == 0) {
181 OPENSSL_fpu_probe();
182 OPENSSL_ppccap_P |= PPC_FPU;
183
184 if (sizeof(size_t) == 4) {
185 #ifdef __linux
186 struct utsname uts;
187 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
188 #endif
189 if (sigsetjmp(ill_jmp, 1) == 0) {
190 OPENSSL_ppc64_probe();
191 OPENSSL_ppccap_P |= PPC_FPU64;
192 }
193 } else {
194 /*
195 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
196 */
197 }
198 }
199
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;
206 }
207 }
208
209 if (sigsetjmp(ill_jmp, 1) == 0) {
210 OPENSSL_madd300_probe();
211 OPENSSL_ppccap_P |= PPC_MADD300;
212 }
213
214 sigaction(SIGILL, &ill_oact, NULL);
215 sigprocmask(SIG_SETMASK, &oset, NULL);
216 }