OpenSSL: update to 1.0.2a
[tomato.git] / release / src / router / openssl / crypto / sparcv9cap.c
blob8bf2846929b17700379366e4c2592a292a91f1ca
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <setjmp.h>
5 #include <signal.h>
6 #include <sys/time.h>
7 #include <unistd.h>
8 #include <openssl/bn.h>
10 #include "sparc_arch.h"
12 #if defined(__GNUC__) && defined(__linux)
13 __attribute__ ((visibility("hidden")))
14 #endif
15 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
17 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
18 const BN_ULONG *np, const BN_ULONG *n0, int num)
20 int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
21 const BN_ULONG *np, const BN_ULONG *n0, int num);
22 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
23 const BN_ULONG *np, const BN_ULONG *n0, int num);
24 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
25 const BN_ULONG *np, const BN_ULONG *n0, int num);
27 if (!(num & 1) && num >= 6) {
28 if ((num & 15) == 0 && num <= 64 &&
29 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
30 (CFR_MONTMUL | CFR_MONTSQR)) {
31 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
32 const BN_ULONG *bp,
33 const BN_ULONG *np,
34 const BN_ULONG *n0);
35 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
36 const BN_ULONG *bp, const BN_ULONG *np,
37 const BN_ULONG *n0);
38 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
39 const BN_ULONG *bp, const BN_ULONG *np,
40 const BN_ULONG *n0);
41 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
42 const BN_ULONG *bp, const BN_ULONG *np,
43 const BN_ULONG *n0);
44 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
45 const BN_ULONG *bp, const BN_ULONG *np,
46 const BN_ULONG *n0);
47 static const bn_mul_mont_f funcs[4] = {
48 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
49 bn_mul_mont_t4_24, bn_mul_mont_t4_32
51 bn_mul_mont_f worker = funcs[num / 16 - 1];
53 if ((*worker) (rp, ap, bp, np, n0))
54 return 1;
55 /* retry once and fall back */
56 if ((*worker) (rp, ap, bp, np, n0))
57 return 1;
58 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
60 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
61 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
62 else if (num >= 8 &&
63 (OPENSSL_sparcv9cap_P[0] &
64 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
65 (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
66 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
68 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
71 unsigned long _sparcv9_rdtick(void);
72 void _sparcv9_vis1_probe(void);
73 unsigned long _sparcv9_vis1_instrument(void);
74 void _sparcv9_vis2_probe(void);
75 void _sparcv9_fmadd_probe(void);
76 unsigned long _sparcv9_rdcfr(void);
77 void _sparcv9_vis3_probe(void);
78 unsigned long _sparcv9_random(void);
79 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
80 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
82 unsigned long OPENSSL_rdtsc(void)
84 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
85 #if defined(__sun) && defined(__SVR4)
86 return gethrtime();
87 #else
88 return 0;
89 #endif
90 else
91 return _sparcv9_rdtick();
94 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
96 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
97 SPARCV9_BLK)
98 return _sparcv9_vis1_instrument_bus(out, cnt);
99 else
100 return 0;
103 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
105 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
106 SPARCV9_BLK)
107 return _sparcv9_vis1_instrument_bus2(out, cnt, max);
108 else
109 return 0;
112 #if 0 && defined(__sun) && defined(__SVR4)
114 * This code path is disabled, because of incompatibility of libdevinfo.so.1
115 * and libmalloc.so.1 (see below for details)
117 # include <malloc.h>
118 # include <dlfcn.h>
119 # include <libdevinfo.h>
120 # include <sys/systeminfo.h>
122 typedef di_node_t(*di_init_t) (const char *, uint_t);
123 typedef void (*di_fini_t) (di_node_t);
124 typedef char *(*di_node_name_t) (di_node_t);
125 typedef int (*di_walk_node_t) (di_node_t, uint_t, di_node_name_t,
126 int (*)(di_node_t, di_node_name_t));
128 # define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
130 static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
132 char *name = (*di_node_name) (node);
134 /* This is expected to catch all UltraSPARC flavors prior T1 */
135 if (!strcmp(name, "SUNW,UltraSPARC") ||
136 /* covers II,III,IV */
137 !strncmp(name, "SUNW,UltraSPARC-I", 17)) {
138 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU | SPARCV9_VIS1;
140 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
141 if (name[14] != '\0' && name[17] != '\0' && name[18] != '\0')
142 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
144 return DI_WALK_TERMINATE;
146 /* This is expected to catch remaining UltraSPARCs, such as T1 */
147 else if (!strncmp(name, "SUNW,UltraSPARC", 15)) {
148 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
150 return DI_WALK_TERMINATE;
153 return DI_WALK_CONTINUE;
156 void OPENSSL_cpuid_setup(void)
158 void *h;
159 char *e, si[256];
160 static int trigger = 0;
162 if (trigger)
163 return;
164 trigger = 1;
166 if ((e = getenv("OPENSSL_sparcv9cap"))) {
167 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
168 return;
171 if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
172 if (strcmp(si, "sun4v"))
173 /* FPU is preferred for all CPUs, but US-T1/2 */
174 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
177 if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
178 if (strstr(si, "+vis"))
179 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
180 if (strstr(si, "+vis2")) {
181 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
182 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
183 return;
186 # ifdef M_KEEP
188 * Solaris libdevinfo.so.1 is effectively incomatible with
189 * libmalloc.so.1. Specifically, if application is linked with
190 * -lmalloc, it crashes upon startup with SIGSEGV in
191 * free(3LIBMALLOC) called by di_fini. Prior call to
192 * mallopt(M_KEEP,0) somehow helps... But not always...
194 if ((h = dlopen(NULL, RTLD_LAZY))) {
195 union {
196 void *p;
197 int (*f) (int, int);
198 } sym;
199 if ((sym.p = dlsym(h, "mallopt")))
200 (*sym.f) (M_KEEP, 0);
201 dlclose(h);
203 # endif
204 if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
205 do {
206 di_init_t di_init;
207 di_fini_t di_fini;
208 di_walk_node_t di_walk_node;
209 di_node_name_t di_node_name;
210 di_node_t root_node;
212 if (!DLLINK(h, di_init))
213 break;
214 if (!DLLINK(h, di_fini))
215 break;
216 if (!DLLINK(h, di_walk_node))
217 break;
218 if (!DLLINK(h, di_node_name))
219 break;
221 if ((root_node = (*di_init) ("/", DINFOSUBTREE)) != DI_NODE_NIL) {
222 (*di_walk_node) (root_node, DI_WALK_SIBFIRST,
223 di_node_name, walk_nodename);
224 (*di_fini) (root_node);
226 } while (0);
228 if (h)
229 dlclose(h);
232 #else
234 static sigjmp_buf common_jmp;
235 static void common_handler(int sig)
237 siglongjmp(common_jmp, sig);
240 void OPENSSL_cpuid_setup(void)
242 char *e;
243 struct sigaction common_act, ill_oact, bus_oact;
244 sigset_t all_masked, oset;
245 static int trigger = 0;
247 if (trigger)
248 return;
249 trigger = 1;
251 if ((e = getenv("OPENSSL_sparcv9cap"))) {
252 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
253 if ((e = strchr(e, ':')))
254 OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
255 return;
258 /* Initial value, fits UltraSPARC-I&II... */
259 OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
261 sigfillset(&all_masked);
262 sigdelset(&all_masked, SIGILL);
263 sigdelset(&all_masked, SIGTRAP);
264 # ifdef SIGEMT
265 sigdelset(&all_masked, SIGEMT);
266 # endif
267 sigdelset(&all_masked, SIGFPE);
268 sigdelset(&all_masked, SIGBUS);
269 sigdelset(&all_masked, SIGSEGV);
270 sigprocmask(SIG_SETMASK, &all_masked, &oset);
272 memset(&common_act, 0, sizeof(common_act));
273 common_act.sa_handler = common_handler;
274 common_act.sa_mask = all_masked;
276 sigaction(SIGILL, &common_act, &ill_oact);
277 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
278 * Linux] */
280 if (sigsetjmp(common_jmp, 1) == 0) {
281 _sparcv9_rdtick();
282 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
285 if (sigsetjmp(common_jmp, 1) == 0) {
286 _sparcv9_vis1_probe();
287 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
288 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
289 if (_sparcv9_vis1_instrument() >= 12)
290 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
291 else {
292 _sparcv9_vis2_probe();
293 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
297 if (sigsetjmp(common_jmp, 1) == 0) {
298 _sparcv9_fmadd_probe();
299 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
303 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
304 * because VIS3 defines even integer instructions.
306 if (sigsetjmp(common_jmp, 1) == 0) {
307 _sparcv9_vis3_probe();
308 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
310 # if 0 /* was planned at some point but never
311 * implemented in hardware */
312 if (sigsetjmp(common_jmp, 1) == 0) {
313 (void)_sparcv9_random();
314 OPENSSL_sparcv9cap_P[0] |= SPARCV9_RANDOM;
316 # endif
319 * In wait for better solution _sparcv9_rdcfr is masked by
320 * VIS3 flag, because it goes to uninterruptable endless
321 * loop on UltraSPARC II running Solaris. Things might be
322 * different on Linux...
324 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
325 sigsetjmp(common_jmp, 1) == 0) {
326 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
329 sigaction(SIGBUS, &bus_oact, NULL);
330 sigaction(SIGILL, &ill_oact, NULL);
332 sigprocmask(SIG_SETMASK, &oset, NULL);
334 if (sizeof(size_t) == 8)
335 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
336 # ifdef __linux
337 else {
338 int ret = syscall(340);
340 if (ret >= 0 && ret & 1)
341 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
343 # endif
346 #endif