1 /*
2 * Copyright 2009-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 #if defined(__APPLE__) && defined(__MACH__)
26 # include <sys/types.h>
27 # include <sys/sysctl.h>
28 #endif
29 #include <openssl/crypto.h>
30 #include "internal/cryptlib.h"
31 #include "crypto/ppc_arch.h"
32
33 unsigned int OPENSSL_ppccap_P = 0;
34
35 static sigset_t all_masked;
36
37 static sigjmp_buf ill_jmp;
ill_handler(int sig)38 static void ill_handler(int sig)
39 {
40 siglongjmp(ill_jmp, sig);
41 }
42
43 void OPENSSL_fpu_probe(void);
44 void OPENSSL_ppc64_probe(void);
45 void OPENSSL_altivec_probe(void);
46 void OPENSSL_crypto207_probe(void);
47 void OPENSSL_madd300_probe(void);
48 void OPENSSL_brd31_probe(void);
49
50 long OPENSSL_rdtsc_mftb(void);
51 long OPENSSL_rdtsc_mfspr268(void);
52
OPENSSL_rdtsc(void)53 uint32_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
63 size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
64 size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
65
OPENSSL_instrument_bus(unsigned int * out,size_t cnt)66 size_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
76 size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
77 size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
78
OPENSSL_instrument_bus2(unsigned int * out,size_t cnt,size_t max)79 size_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
89 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
90 # if __GLIBC_PREREQ(2, 16)
91 # include <sys/auxv.h>
92 # define OSSL_IMPLEMENT_GETAUXVAL
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
99 # endif
100 #endif
101
102 #if defined(__FreeBSD__) || defined(__OpenBSD__)
103 # include <sys/param.h>
104 # if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
105 (defined(__OpenBSD__) && OpenBSD >= 202409)
106 # include <sys/auxv.h>
107 # define OSSL_IMPLEMENT_GETAUXVAL
108
getauxval(unsigned long key)109 static unsigned long getauxval(unsigned long key)
110 {
111 unsigned long val = 0ul;
112
113 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
114 return 0ul;
115
116 return val;
117 }
118 # endif
119 #endif
120
121 /* I wish <sys/auxv.h> was universally available */
122 #ifndef AT_HWCAP
123 # define AT_HWCAP 16 /* AT_HWCAP */
124 #endif
125 #define HWCAP_PPC64 (1U << 30)
126 #define HWCAP_ALTIVEC (1U << 28)
127 #define HWCAP_FPU (1U << 27)
128 #define HWCAP_POWER6_EXT (1U << 9)
129 #define HWCAP_VSX (1U << 7)
130
131 #ifndef AT_HWCAP2
132 # define AT_HWCAP2 26 /* AT_HWCAP2 */
133 #endif
134 #define HWCAP_VEC_CRYPTO (1U << 25)
135 #define HWCAP_ARCH_3_00 (1U << 23)
136 #define HWCAP_ARCH_3_1 (1U << 18)
137
OPENSSL_cpuid_setup(void)138 void OPENSSL_cpuid_setup(void)
139 {
140 char *e;
141 struct sigaction ill_oact, ill_act;
142 sigset_t oset;
143 static int trigger = 0;
144
145 if (trigger)
146 return;
147 trigger = 1;
148
149 if ((e = getenv("OPENSSL_ppccap"))) {
150 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
151 return;
152 }
153
154 OPENSSL_ppccap_P = 0;
155
156 #if defined(_AIX)
157 OPENSSL_ppccap_P |= PPC_FPU;
158
159 if (sizeof(size_t) == 4) {
160 struct utsname uts;
161 # if defined(_SC_AIX_KERNEL_BITMODE)
162 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
163 return;
164 # endif
165 if (uname(&uts) != 0 || atoi(uts.version) < 6)
166 return;
167 }
168
169 # if defined(__power_set)
170 /*
171 * Value used in __power_set is a single-bit 1<<n one denoting
172 * specific processor class. Incidentally 0xffffffff<<n can be
173 * used to denote specific processor and its successors.
174 */
175 if (sizeof(size_t) == 4) {
176 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
177 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
178 OPENSSL_ppccap_P |= PPC_FPU64;
179 } else {
180 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
181 if (__power_set(0x1U<<14)) /* POWER6 */
182 OPENSSL_ppccap_P |= PPC_FPU64;
183 }
184
185 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
186 OPENSSL_ppccap_P |= PPC_ALTIVEC;
187
188 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
189 OPENSSL_ppccap_P |= PPC_CRYPTO207;
190
191 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
192 OPENSSL_ppccap_P |= PPC_MADD300;
193
194 if (__power_set(0xffffffffU<<18)) /* POWER10 and later */
195 OPENSSL_ppccap_P |= PPC_BRD31;
196
197 return;
198 # endif
199 #endif
200
201 #if defined(__APPLE__) && defined(__MACH__)
202 OPENSSL_ppccap_P |= PPC_FPU;
203
204 {
205 int val;
206 size_t len = sizeof(val);
207
208 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
209 if (val)
210 OPENSSL_ppccap_P |= PPC_FPU64;
211 }
212
213 len = sizeof(val);
214 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
215 if (val)
216 OPENSSL_ppccap_P |= PPC_ALTIVEC;
217 }
218
219 return;
220 }
221 #endif
222
223 #ifdef OSSL_IMPLEMENT_GETAUXVAL
224 {
225 unsigned long hwcap = getauxval(AT_HWCAP);
226 unsigned long hwcap2 = getauxval(AT_HWCAP2);
227
228 if (hwcap & HWCAP_FPU) {
229 OPENSSL_ppccap_P |= PPC_FPU;
230
231 if (sizeof(size_t) == 4) {
232 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
233 if (hwcap & HWCAP_PPC64)
234 OPENSSL_ppccap_P |= PPC_FPU64;
235 } else {
236 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
237 if (hwcap & HWCAP_POWER6_EXT)
238 OPENSSL_ppccap_P |= PPC_FPU64;
239 }
240 }
241
242 if (hwcap & HWCAP_ALTIVEC) {
243 OPENSSL_ppccap_P |= PPC_ALTIVEC;
244
245 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
246 OPENSSL_ppccap_P |= PPC_CRYPTO207;
247 }
248
249 if (hwcap2 & HWCAP_ARCH_3_00) {
250 OPENSSL_ppccap_P |= PPC_MADD300;
251 }
252
253 if (hwcap2 & HWCAP_ARCH_3_1) {
254 OPENSSL_ppccap_P |= PPC_BRD31;
255 }
256 }
257 #endif
258
259 sigfillset(&all_masked);
260 sigdelset(&all_masked, SIGILL);
261 sigdelset(&all_masked, SIGTRAP);
262 #ifdef SIGEMT
263 sigdelset(&all_masked, SIGEMT);
264 #endif
265 sigdelset(&all_masked, SIGFPE);
266 sigdelset(&all_masked, SIGBUS);
267 sigdelset(&all_masked, SIGSEGV);
268
269 memset(&ill_act, 0, sizeof(ill_act));
270 ill_act.sa_handler = ill_handler;
271 ill_act.sa_mask = all_masked;
272
273 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
274 sigaction(SIGILL, &ill_act, &ill_oact);
275
276 #ifndef OSSL_IMPLEMENT_GETAUXVAL
277 if (sigsetjmp(ill_jmp, 1) == 0) {
278 OPENSSL_fpu_probe();
279 OPENSSL_ppccap_P |= PPC_FPU;
280
281 if (sizeof(size_t) == 4) {
282 # ifdef __linux
283 struct utsname uts;
284 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
285 # endif
286 if (sigsetjmp(ill_jmp, 1) == 0) {
287 OPENSSL_ppc64_probe();
288 OPENSSL_ppccap_P |= PPC_FPU64;
289 }
290 } else {
291 /*
292 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
293 */
294 }
295 }
296
297 if (sigsetjmp(ill_jmp, 1) == 0) {
298 OPENSSL_altivec_probe();
299 OPENSSL_ppccap_P |= PPC_ALTIVEC;
300 if (sigsetjmp(ill_jmp, 1) == 0) {
301 OPENSSL_crypto207_probe();
302 OPENSSL_ppccap_P |= PPC_CRYPTO207;
303 }
304 }
305
306 if (sigsetjmp(ill_jmp, 1) == 0) {
307 OPENSSL_madd300_probe();
308 OPENSSL_ppccap_P |= PPC_MADD300;
309 }
310 #endif
311
312 if (sigsetjmp(ill_jmp, 1) == 0) {
313 OPENSSL_rdtsc_mftb();
314 OPENSSL_ppccap_P |= PPC_MFTB;
315 } else if (sigsetjmp(ill_jmp, 1) == 0) {
316 OPENSSL_rdtsc_mfspr268();
317 OPENSSL_ppccap_P |= PPC_MFSPR268;
318 }
319
320 sigaction(SIGILL, &ill_oact, NULL);
321 sigprocmask(SIG_SETMASK, &oset, NULL);
322 }
323