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