]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ppccap.c
Update copyright year
[thirdparty/openssl.git] / crypto / ppccap.c
CommitLineData
b1322259 1/*
fecb3aae 2 * Copyright 2009-2022 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);
f596bbe4 48void OPENSSL_brd31_probe(void);
70b76d39 49
c8f37048
BE
50long OPENSSL_rdtsc_mftb(void);
51long OPENSSL_rdtsc_mfspr268(void);
52
53uint32_t OPENSSL_rdtsc(void)
54{
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();
59 else
60 return 0;
61}
62
63size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
64size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
65
66size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
67{
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);
72 else
73 return 0;
74}
75
76size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
77size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
78
79size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
80{
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);
85 else
86 return 0;
87}
88
5f40dd15
RL
89#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
90# if __GLIBC_PREREQ(2, 16)
91# include <sys/auxv.h>
92# define OSSL_IMPLEMENT_GETAUXVAL
d5567d5f 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
98# endif
5f40dd15 99# endif
2688d999
AP
100#endif
101
b57ec739
DC
102#if defined(__FreeBSD__)
103# include <sys/param.h>
104# if __FreeBSD_version >= 1200000
105# include <sys/auxv.h>
106# define OSSL_IMPLEMENT_GETAUXVAL
107
108static unsigned long getauxval(unsigned long key)
109{
110 unsigned long val = 0ul;
111
112 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
113 return 0ul;
114
115 return val;
116}
117# endif
118#endif
119
2688d999 120/* I wish <sys/auxv.h> was universally available */
f5485b97 121#ifndef AT_HWCAP
122# define AT_HWCAP 16 /* AT_HWCAP */
123#endif
2688d999
AP
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)
129
f5485b97 130#ifndef AT_HWCAP2
131# define AT_HWCAP2 26 /* AT_HWCAP2 */
132#endif
2688d999 133#define HWCAP_VEC_CRYPTO (1U << 25)
e0e53282 134#define HWCAP_ARCH_3_00 (1U << 23)
f596bbe4 135#define HWCAP_ARCH_3_1 (1U << 18)
2688d999
AP
136
137# if defined(__GNUC__) && __GNUC__>=2
138__attribute__ ((constructor))
139# endif
b4b48a10 140void OPENSSL_cpuid_setup(void)
0f113f3e
MC
141{
142 char *e;
143 struct sigaction ill_oact, ill_act;
144 sigset_t oset;
145 static int trigger = 0;
146
147 if (trigger)
148 return;
149 trigger = 1;
150
0f113f3e
MC
151 if ((e = getenv("OPENSSL_ppccap"))) {
152 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
153 return;
154 }
b4b48a10 155
0f113f3e 156 OPENSSL_ppccap_P = 0;
6415dd7b 157
fd054957 158#if defined(_AIX)
2688d999
AP
159 OPENSSL_ppccap_P |= PPC_FPU;
160
0f113f3e
MC
161 if (sizeof(size_t) == 4) {
162 struct utsname uts;
fd054957 163# if defined(_SC_AIX_KERNEL_BITMODE)
0f113f3e
MC
164 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
165 return;
fd054957 166# endif
0f113f3e
MC
167 if (uname(&uts) != 0 || atoi(uts.version) < 6)
168 return;
169 }
2688d999
AP
170
171# if defined(__power_set)
172 /*
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.
176 */
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;
181 } else {
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;
185 }
186
187 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
188 OPENSSL_ppccap_P |= PPC_ALTIVEC;
189
190 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
191 OPENSSL_ppccap_P |= PPC_CRYPTO207;
192
e0e53282
AP
193 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
194 OPENSSL_ppccap_P |= PPC_MADD300;
195
f596bbe4
DB
196 if (__power_set(0xffffffffU<<18)) /* POWER10 and later */
197 OPENSSL_ppccap_P |= PPC_BRD31;
198
2688d999
AP
199 return;
200# endif
201#endif
202
0bd93bbe
AP
203#if defined(__APPLE__) && defined(__MACH__)
204 OPENSSL_ppccap_P |= PPC_FPU;
205
206 {
207 int val;
208 size_t len = sizeof(val);
209
210 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
211 if (val)
212 OPENSSL_ppccap_P |= PPC_FPU64;
213 }
214
215 len = sizeof(val);
216 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
217 if (val)
218 OPENSSL_ppccap_P |= PPC_ALTIVEC;
219 }
220
221 return;
222 }
223#endif
224
5f40dd15
RL
225#ifdef OSSL_IMPLEMENT_GETAUXVAL
226 {
f5485b97 227 unsigned long hwcap = getauxval(AT_HWCAP);
228 unsigned long hwcap2 = getauxval(AT_HWCAP2);
2688d999
AP
229
230 if (hwcap & HWCAP_FPU) {
dccd20d1 231 OPENSSL_ppccap_P |= PPC_FPU;
2688d999
AP
232
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;
237 } else {
238 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
239 if (hwcap & HWCAP_POWER6_EXT)
240 OPENSSL_ppccap_P |= PPC_FPU64;
241 }
242 }
243
244 if (hwcap & HWCAP_ALTIVEC) {
245 OPENSSL_ppccap_P |= PPC_ALTIVEC;
246
99592c73 247 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
2688d999
AP
248 OPENSSL_ppccap_P |= PPC_CRYPTO207;
249 }
250
99592c73 251 if (hwcap2 & HWCAP_ARCH_3_00) {
e0e53282
AP
252 OPENSSL_ppccap_P |= PPC_MADD300;
253 }
f596bbe4
DB
254
255 if (hwcap2 & HWCAP_ARCH_3_1) {
256 OPENSSL_ppccap_P |= PPC_BRD31;
257 }
2688d999 258 }
5f40dd15 259#endif
2688d999
AP
260
261 sigfillset(&all_masked);
262 sigdelset(&all_masked, SIGILL);
263 sigdelset(&all_masked, SIGTRAP);
264#ifdef SIGEMT
265 sigdelset(&all_masked, SIGEMT);
fd054957 266#endif
2688d999
AP
267 sigdelset(&all_masked, SIGFPE);
268 sigdelset(&all_masked, SIGBUS);
269 sigdelset(&all_masked, SIGSEGV);
fd054957 270
0f113f3e
MC
271 memset(&ill_act, 0, sizeof(ill_act));
272 ill_act.sa_handler = ill_handler;
273 ill_act.sa_mask = all_masked;
6415dd7b 274
0f113f3e
MC
275 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
276 sigaction(SIGILL, &ill_act, &ill_oact);
6415dd7b 277
c8f37048 278#ifndef OSSL_IMPLEMENT_GETAUXVAL
1287dabd 279 if (sigsetjmp(ill_jmp, 1) == 0) {
2688d999
AP
280 OPENSSL_fpu_probe();
281 OPENSSL_ppccap_P |= PPC_FPU;
282
283 if (sizeof(size_t) == 4) {
c8f37048 284# ifdef __linux
2688d999
AP
285 struct utsname uts;
286 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
c8f37048 287# endif
2688d999
AP
288 if (sigsetjmp(ill_jmp, 1) == 0) {
289 OPENSSL_ppc64_probe();
290 OPENSSL_ppccap_P |= PPC_FPU64;
291 }
292 } else {
293 /*
294 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
295 */
296 }
0f113f3e
MC
297 }
298
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;
305 }
306 }
307
e0e53282
AP
308 if (sigsetjmp(ill_jmp, 1) == 0) {
309 OPENSSL_madd300_probe();
310 OPENSSL_ppccap_P |= PPC_MADD300;
311 }
c8f37048
BE
312#endif
313
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;
320 }
e0e53282 321
0f113f3e
MC
322 sigaction(SIGILL, &ill_oact, NULL);
323 sigprocmask(SIG_SETMASK, &oset, NULL);
324}