]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ppccap.c
crypto/ppccap.c: add SIGILL-free processor capability detection code.
[thirdparty/openssl.git] / crypto / ppccap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <setjmp.h>
5 #include <signal.h>
6 #include <unistd.h>
7 #if defined(__linux) || defined(_AIX)
8 # include <sys/utsname.h>
9 #endif
10 #if defined(_AIX53) /* defined even on post-5.3 */
11 # include <sys/systemcfg.h>
12 # if !defined(__power_set)
13 # define __power_set(a) (_system_configuration.implementation & (a))
14 # endif
15 #endif
16 #include <openssl/crypto.h>
17 #include <openssl/bn.h>
18
19 #include "ppc_arch.h"
20
21 unsigned int OPENSSL_ppccap_P = 0;
22
23 static sigset_t all_masked;
24
25 #ifdef OPENSSL_BN_ASM_MONT
26 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
27 const BN_ULONG *np, const BN_ULONG *n0, int num)
28 {
29 int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap,
30 const BN_ULONG *bp, const BN_ULONG *np,
31 const BN_ULONG *n0, int num);
32 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
33 const BN_ULONG *np, const BN_ULONG *n0, int num);
34
35 if (sizeof(size_t) == 4) {
36 # if 1 || (defined(__APPLE__) && defined(__MACH__))
37 if (num >= 8 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64))
38 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
39 # else
40 /*
41 * boundary of 32 was experimentally determined on Linux 2.6.22,
42 * might have to be adjusted on AIX...
43 */
44 if (num >= 32 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64)) {
45 sigset_t oset;
46 int ret;
47
48 sigprocmask(SIG_SETMASK, &all_masked, &oset);
49 ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
50 sigprocmask(SIG_SETMASK, &oset, NULL);
51
52 return ret;
53 }
54 # endif
55 } else if ((OPENSSL_ppccap_P & PPC_FPU64))
56 /*
57 * this is a "must" on POWER6, but run-time detection is not
58 * implemented yet...
59 */
60 return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
61
62 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
63 }
64 #endif
65
66 void sha256_block_p8(void *ctx, const void *inp, size_t len);
67 void sha256_block_ppc(void *ctx, const void *inp, size_t len);
68 void sha256_block_data_order(void *ctx, const void *inp, size_t len)
69 {
70 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
71 sha256_block_ppc(ctx, inp, len);
72 }
73
74 void sha512_block_p8(void *ctx, const void *inp, size_t len);
75 void sha512_block_ppc(void *ctx, const void *inp, size_t len);
76 void sha512_block_data_order(void *ctx, const void *inp, size_t len)
77 {
78 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
79 sha512_block_ppc(ctx, inp, len);
80 }
81
82 static sigjmp_buf ill_jmp;
83 static void ill_handler(int sig)
84 {
85 siglongjmp(ill_jmp, sig);
86 }
87
88 void OPENSSL_fpu_probe(void);
89 void OPENSSL_ppc64_probe(void);
90 void OPENSSL_altivec_probe(void);
91 void OPENSSL_crypto207_probe(void);
92
93 /*
94 * Use a weak reference to getauxval() so we can use it if it is available
95 * but don't break the build if it is not. Note that this is *link-time*
96 * feature detection, not *run-time*. In other words if we link with
97 * symbol present, it's expected to be present even at run-time.
98 */
99 #if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
100 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
101 #else
102 static unsigned long (*getauxval) (unsigned long) = NULL;
103 #endif
104
105 /* I wish <sys/auxv.h> was universally available */
106 #define HWCAP 16 /* AT_HWCAP */
107 #define HWCAP_PPC64 (1U << 30)
108 #define HWCAP_ALTIVEC (1U << 28)
109 #define HWCAP_FPU (1U << 27)
110 #define HWCAP_POWER6_EXT (1U << 9)
111 #define HWCAP_VSX (1U << 7)
112
113 #define HWCAP2 26 /* AT_HWCAP2 */
114 #define HWCAP_VEC_CRYPTO (1U << 25)
115
116 # if defined(__GNUC__) && __GNUC__>=2
117 __attribute__ ((constructor))
118 # endif
119 void OPENSSL_cpuid_setup(void)
120 {
121 char *e;
122 struct sigaction ill_oact, ill_act;
123 sigset_t oset;
124 static int trigger = 0;
125
126 if (trigger)
127 return;
128 trigger = 1;
129
130 if ((e = getenv("OPENSSL_ppccap"))) {
131 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
132 return;
133 }
134
135 OPENSSL_ppccap_P = 0;
136
137 #if defined(_AIX)
138 OPENSSL_ppccap_P |= PPC_FPU;
139
140 if (sizeof(size_t) == 4) {
141 struct utsname uts;
142 # if defined(_SC_AIX_KERNEL_BITMODE)
143 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
144 return;
145 # endif
146 if (uname(&uts) != 0 || atoi(uts.version) < 6)
147 return;
148 }
149
150 # if defined(__power_set)
151 /*
152 * Value used in __power_set is a single-bit 1<<n one denoting
153 * specific processor class. Incidentally 0xffffffff<<n can be
154 * used to denote specific processor and its successors.
155 */
156 if (sizeof(size_t) == 4) {
157 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
158 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
159 OPENSSL_ppccap_P |= PPC_FPU64;
160 } else {
161 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
162 if (__power_set(0x1U<<14)) /* POWER6 */
163 OPENSSL_ppccap_P |= PPC_FPU64;
164 }
165
166 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
167 OPENSSL_ppccap_P |= PPC_ALTIVEC;
168
169 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
170 OPENSSL_ppccap_P |= PPC_CRYPTO207;
171
172 return;
173 # endif
174 #endif
175
176 if (getauxval != NULL) {
177 unsigned long hwcap = getauxval(HWCAP);
178
179 if (hwcap & HWCAP_FPU) {
180 OPENSSL_ppccap_P |= PPC_FPU;
181
182 if (sizeof(size_t) == 4) {
183 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
184 if (hwcap & HWCAP_PPC64)
185 OPENSSL_ppccap_P |= PPC_FPU64;
186 } else {
187 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
188 if (hwcap & HWCAP_POWER6_EXT)
189 OPENSSL_ppccap_P |= PPC_FPU64;
190 }
191 }
192
193 if (hwcap & HWCAP_ALTIVEC) {
194 OPENSSL_ppccap_P |= PPC_ALTIVEC;
195
196 if ((hwcap & HWCAP_VSX) && (getauxval(HWCAP2) & HWCAP_VEC_CRYPTO))
197 OPENSSL_ppccap_P |= PPC_CRYPTO207;
198 }
199
200 return;
201 }
202
203 sigfillset(&all_masked);
204 sigdelset(&all_masked, SIGILL);
205 sigdelset(&all_masked, SIGTRAP);
206 #ifdef SIGEMT
207 sigdelset(&all_masked, SIGEMT);
208 #endif
209 sigdelset(&all_masked, SIGFPE);
210 sigdelset(&all_masked, SIGBUS);
211 sigdelset(&all_masked, SIGSEGV);
212
213 memset(&ill_act, 0, sizeof(ill_act));
214 ill_act.sa_handler = ill_handler;
215 ill_act.sa_mask = all_masked;
216
217 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
218 sigaction(SIGILL, &ill_act, &ill_oact);
219
220 if (sigsetjmp(ill_jmp,1) == 0) {
221 OPENSSL_fpu_probe();
222 OPENSSL_ppccap_P |= PPC_FPU;
223
224 if (sizeof(size_t) == 4) {
225 #ifdef __linux
226 struct utsname uts;
227 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
228 #endif
229 if (sigsetjmp(ill_jmp, 1) == 0) {
230 OPENSSL_ppc64_probe();
231 OPENSSL_ppccap_P |= PPC_FPU64;
232 }
233 } else {
234 /*
235 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
236 */
237 }
238 }
239
240 if (sigsetjmp(ill_jmp, 1) == 0) {
241 OPENSSL_altivec_probe();
242 OPENSSL_ppccap_P |= PPC_ALTIVEC;
243 if (sigsetjmp(ill_jmp, 1) == 0) {
244 OPENSSL_crypto207_probe();
245 OPENSSL_ppccap_P |= PPC_CRYPTO207;
246 }
247 }
248
249 sigaction(SIGILL, &ill_oact, NULL);
250 sigprocmask(SIG_SETMASK, &oset, NULL);
251 }