]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
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 | ||
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 | |
11252459 | 25 | #include <openssl/crypto.h> |
b4b48a10 | 26 | |
0e716d92 | 27 | #include "ppc_arch.h" |
b4b48a10 | 28 | |
07f3e4f3 | 29 | unsigned int OPENSSL_ppccap_P = 0; |
b4b48a10 AP |
30 | |
31 | static sigset_t all_masked; | |
32 | ||
b4b48a10 | 33 | static sigjmp_buf ill_jmp; |
0f113f3e MC |
34 | static void ill_handler(int sig) |
35 | { | |
36 | siglongjmp(ill_jmp, sig); | |
37 | } | |
b4b48a10 | 38 | |
2688d999 | 39 | void OPENSSL_fpu_probe(void); |
70b76d39 | 40 | void OPENSSL_ppc64_probe(void); |
fd054957 | 41 | void OPENSSL_altivec_probe(void); |
de51e830 | 42 | void OPENSSL_crypto207_probe(void); |
53385e1f | 43 | void OPENSSL_madd300_probe(void); |
70b76d39 | 44 | |
2688d999 AP |
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) | |
e0e53282 | 67 | #define HWCAP_ARCH_3_00 (1U << 23) |
2688d999 AP |
68 | |
69 | # if defined(__GNUC__) && __GNUC__>=2 | |
70 | __attribute__ ((constructor)) | |
71 | # endif | |
b4b48a10 | 72 | void OPENSSL_cpuid_setup(void) |
0f113f3e MC |
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 | ||
0f113f3e MC |
83 | if ((e = getenv("OPENSSL_ppccap"))) { |
84 | OPENSSL_ppccap_P = strtoul(e, NULL, 0); | |
85 | return; | |
86 | } | |
b4b48a10 | 87 | |
0f113f3e | 88 | OPENSSL_ppccap_P = 0; |
6415dd7b | 89 | |
fd054957 | 90 | #if defined(_AIX) |
2688d999 AP |
91 | OPENSSL_ppccap_P |= PPC_FPU; |
92 | ||
0f113f3e MC |
93 | if (sizeof(size_t) == 4) { |
94 | struct utsname uts; | |
fd054957 | 95 | # if defined(_SC_AIX_KERNEL_BITMODE) |
0f113f3e MC |
96 | if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64) |
97 | return; | |
fd054957 | 98 | # endif |
0f113f3e MC |
99 | if (uname(&uts) != 0 || atoi(uts.version) < 6) |
100 | return; | |
101 | } | |
2688d999 AP |
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 | ||
e0e53282 AP |
125 | if (__power_set(0xffffffffU<<17)) /* POWER9 and later */ |
126 | OPENSSL_ppccap_P |= PPC_MADD300; | |
127 | ||
2688d999 AP |
128 | return; |
129 | # endif | |
130 | #endif | |
131 | ||
132 | if (getauxval != NULL) { | |
133 | unsigned long hwcap = getauxval(HWCAP); | |
134 | ||
135 | if (hwcap & HWCAP_FPU) { | |
dccd20d1 | 136 | OPENSSL_ppccap_P |= PPC_FPU; |
2688d999 AP |
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 | ||
e0e53282 AP |
156 | if (hwcap & HWCAP_ARCH_3_00) { |
157 | OPENSSL_ppccap_P |= PPC_MADD300; | |
158 | } | |
159 | ||
2688d999 AP |
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); | |
fd054957 | 168 | #endif |
2688d999 AP |
169 | sigdelset(&all_masked, SIGFPE); |
170 | sigdelset(&all_masked, SIGBUS); | |
171 | sigdelset(&all_masked, SIGSEGV); | |
fd054957 | 172 | |
0f113f3e MC |
173 | memset(&ill_act, 0, sizeof(ill_act)); |
174 | ill_act.sa_handler = ill_handler; | |
175 | ill_act.sa_mask = all_masked; | |
6415dd7b | 176 | |
0f113f3e MC |
177 | sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset); |
178 | sigaction(SIGILL, &ill_act, &ill_oact); | |
6415dd7b | 179 | |
2688d999 AP |
180 | if (sigsetjmp(ill_jmp,1) == 0) { |
181 | OPENSSL_fpu_probe(); | |
182 | OPENSSL_ppccap_P |= PPC_FPU; | |
183 | ||
184 | if (sizeof(size_t) == 4) { | |
78c3e205 | 185 | #ifdef __linux |
2688d999 AP |
186 | struct utsname uts; |
187 | if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0) | |
78c3e205 | 188 | #endif |
2688d999 AP |
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 | } | |
0f113f3e MC |
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 | ||
e0e53282 AP |
209 | if (sigsetjmp(ill_jmp, 1) == 0) { |
210 | OPENSSL_madd300_probe(); | |
211 | OPENSSL_ppccap_P |= PPC_MADD300; | |
212 | } | |
213 | ||
0f113f3e MC |
214 | sigaction(SIGILL, &ill_oact, NULL); |
215 | sigprocmask(SIG_SETMASK, &oset, NULL); | |
216 | } |