]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ppccap.c
fix some code with obvious wrong coding style
[thirdparty/openssl.git] / crypto / ppccap.c
CommitLineData
b1322259 1/*
4333b89f 2 * Copyright 2009-2021 The OpenSSL Project Authors. All Rights Reserved.
b1322259 3 *
0e9725bc 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
b4b48a10
AP
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <setjmp.h>
14#include <signal.h>
fd054957 15#include <unistd.h>
d5630dd6 16#if defined(__linux) || defined(_AIX)
0f113f3e 17# include <sys/utsname.h>
78c3e205 18#endif
2688d999
AP
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
0bd93bbe
AP
25#if defined(__APPLE__) && defined(__MACH__)
26# include <sys/types.h>
27# include <sys/sysctl.h>
28#endif
11252459 29#include <openssl/crypto.h>
449bdf37 30#include "internal/cryptlib.h"
3d178db7 31#include "crypto/ppc_arch.h"
b4b48a10 32
07f3e4f3 33unsigned int OPENSSL_ppccap_P = 0;
b4b48a10
AP
34
35static sigset_t all_masked;
36
b4b48a10 37static sigjmp_buf ill_jmp;
0f113f3e
MC
38static void ill_handler(int sig)
39{
40 siglongjmp(ill_jmp, sig);
41}
b4b48a10 42
2688d999 43void OPENSSL_fpu_probe(void);
70b76d39 44void OPENSSL_ppc64_probe(void);
fd054957 45void OPENSSL_altivec_probe(void);
de51e830 46void OPENSSL_crypto207_probe(void);
53385e1f 47void OPENSSL_madd300_probe(void);
70b76d39 48
c8f37048
BE
49long OPENSSL_rdtsc_mftb(void);
50long OPENSSL_rdtsc_mfspr268(void);
51
52uint32_t OPENSSL_rdtsc(void)
53{
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();
58 else
59 return 0;
60}
61
62size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
63size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
64
65size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
66{
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);
71 else
72 return 0;
73}
74
75size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
76size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
77
78size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
79{
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);
84 else
85 return 0;
86}
87
5f40dd15
RL
88#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
89# if __GLIBC_PREREQ(2, 16)
90# include <sys/auxv.h>
91# define OSSL_IMPLEMENT_GETAUXVAL
d5567d5f 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
97# endif
5f40dd15 98# endif
2688d999
AP
99#endif
100
b57ec739
DC
101#if defined(__FreeBSD__)
102# include <sys/param.h>
103# if __FreeBSD_version >= 1200000
104# include <sys/auxv.h>
105# define OSSL_IMPLEMENT_GETAUXVAL
106
107static unsigned long getauxval(unsigned long key)
108{
109 unsigned long val = 0ul;
110
111 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
112 return 0ul;
113
114 return val;
115}
116# endif
117#endif
118
2688d999
AP
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)
126
127#define HWCAP2 26 /* AT_HWCAP2 */
128#define HWCAP_VEC_CRYPTO (1U << 25)
e0e53282 129#define HWCAP_ARCH_3_00 (1U << 23)
2688d999
AP
130
131# if defined(__GNUC__) && __GNUC__>=2
132__attribute__ ((constructor))
133# endif
b4b48a10 134void OPENSSL_cpuid_setup(void)
0f113f3e
MC
135{
136 char *e;
137 struct sigaction ill_oact, ill_act;
138 sigset_t oset;
139 static int trigger = 0;
140
141 if (trigger)
142 return;
143 trigger = 1;
144
0f113f3e
MC
145 if ((e = getenv("OPENSSL_ppccap"))) {
146 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
147 return;
148 }
b4b48a10 149
0f113f3e 150 OPENSSL_ppccap_P = 0;
6415dd7b 151
fd054957 152#if defined(_AIX)
2688d999
AP
153 OPENSSL_ppccap_P |= PPC_FPU;
154
0f113f3e
MC
155 if (sizeof(size_t) == 4) {
156 struct utsname uts;
fd054957 157# if defined(_SC_AIX_KERNEL_BITMODE)
0f113f3e
MC
158 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
159 return;
fd054957 160# endif
0f113f3e
MC
161 if (uname(&uts) != 0 || atoi(uts.version) < 6)
162 return;
163 }
2688d999
AP
164
165# if defined(__power_set)
166 /*
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.
170 */
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;
175 } else {
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;
179 }
180
181 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
182 OPENSSL_ppccap_P |= PPC_ALTIVEC;
183
184 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
185 OPENSSL_ppccap_P |= PPC_CRYPTO207;
186
e0e53282
AP
187 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
188 OPENSSL_ppccap_P |= PPC_MADD300;
189
2688d999
AP
190 return;
191# endif
192#endif
193
0bd93bbe
AP
194#if defined(__APPLE__) && defined(__MACH__)
195 OPENSSL_ppccap_P |= PPC_FPU;
196
197 {
198 int val;
199 size_t len = sizeof(val);
200
201 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
202 if (val)
203 OPENSSL_ppccap_P |= PPC_FPU64;
204 }
205
206 len = sizeof(val);
207 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
208 if (val)
209 OPENSSL_ppccap_P |= PPC_ALTIVEC;
210 }
211
212 return;
213 }
214#endif
215
5f40dd15
RL
216#ifdef OSSL_IMPLEMENT_GETAUXVAL
217 {
2688d999 218 unsigned long hwcap = getauxval(HWCAP);
99592c73 219 unsigned long hwcap2 = getauxval(HWCAP2);
2688d999
AP
220
221 if (hwcap & HWCAP_FPU) {
dccd20d1 222 OPENSSL_ppccap_P |= PPC_FPU;
2688d999
AP
223
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;
228 } else {
229 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
230 if (hwcap & HWCAP_POWER6_EXT)
231 OPENSSL_ppccap_P |= PPC_FPU64;
232 }
233 }
234
235 if (hwcap & HWCAP_ALTIVEC) {
236 OPENSSL_ppccap_P |= PPC_ALTIVEC;
237
99592c73 238 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
2688d999
AP
239 OPENSSL_ppccap_P |= PPC_CRYPTO207;
240 }
241
99592c73 242 if (hwcap2 & HWCAP_ARCH_3_00) {
e0e53282
AP
243 OPENSSL_ppccap_P |= PPC_MADD300;
244 }
2688d999 245 }
5f40dd15 246#endif
2688d999
AP
247
248 sigfillset(&all_masked);
249 sigdelset(&all_masked, SIGILL);
250 sigdelset(&all_masked, SIGTRAP);
251#ifdef SIGEMT
252 sigdelset(&all_masked, SIGEMT);
fd054957 253#endif
2688d999
AP
254 sigdelset(&all_masked, SIGFPE);
255 sigdelset(&all_masked, SIGBUS);
256 sigdelset(&all_masked, SIGSEGV);
fd054957 257
0f113f3e
MC
258 memset(&ill_act, 0, sizeof(ill_act));
259 ill_act.sa_handler = ill_handler;
260 ill_act.sa_mask = all_masked;
6415dd7b 261
0f113f3e
MC
262 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
263 sigaction(SIGILL, &ill_act, &ill_oact);
6415dd7b 264
c8f37048 265#ifndef OSSL_IMPLEMENT_GETAUXVAL
1287dabd 266 if (sigsetjmp(ill_jmp, 1) == 0) {
2688d999
AP
267 OPENSSL_fpu_probe();
268 OPENSSL_ppccap_P |= PPC_FPU;
269
270 if (sizeof(size_t) == 4) {
c8f37048 271# ifdef __linux
2688d999
AP
272 struct utsname uts;
273 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
c8f37048 274# endif
2688d999
AP
275 if (sigsetjmp(ill_jmp, 1) == 0) {
276 OPENSSL_ppc64_probe();
277 OPENSSL_ppccap_P |= PPC_FPU64;
278 }
279 } else {
280 /*
281 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
282 */
283 }
0f113f3e
MC
284 }
285
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;
292 }
293 }
294
e0e53282
AP
295 if (sigsetjmp(ill_jmp, 1) == 0) {
296 OPENSSL_madd300_probe();
297 OPENSSL_ppccap_P |= PPC_MADD300;
298 }
c8f37048
BE
299#endif
300
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;
307 }
e0e53282 308
0f113f3e
MC
309 sigaction(SIGILL, &ill_oact, NULL);
310 sigprocmask(SIG_SETMASK, &oset, NULL);
311}