Fix strange formatting by indent
[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                 /* covers II,III,IV */
118             !strncmp(name,"SUNW,UltraSPARC-I",17))
119                 {
120                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
121
122                 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
123                 if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
124                         OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
125
126                 return DI_WALK_TERMINATE;
127                 }
128         /* This is expected to catch remaining UltraSPARCs, such as T1 */
129         else if (!strncmp(name,"SUNW,UltraSPARC",15))
130                 {
131                 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
132
133                 return DI_WALK_TERMINATE;
134                 }
135
136         return DI_WALK_CONTINUE;
137         }
138
139 void OPENSSL_cpuid_setup(void)
140         {
141         void *h;
142         char *e,si[256];
143         static int trigger=0;
144
145         if (trigger) return;
146         trigger=1;
147
148         if ((e=getenv("OPENSSL_sparcv9cap")))
149                 {
150                 OPENSSL_sparcv9cap_P[0]=strtoul(e,NULL,0);
151                 return;
152                 }
153
154         if (sysinfo(SI_MACHINE,si,sizeof(si))>0)
155                 {
156                 if (strcmp(si,"sun4v"))
157                         /* FPU is preferred for all CPUs, but US-T1/2 */
158                         OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
159                 }
160
161         if (sysinfo(SI_ISALIST,si,sizeof(si))>0)
162                 {
163                 if (strstr(si,"+vis"))
164                         OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1|SPARCV9_BLK;
165                 if (strstr(si,"+vis2"))
166                         {
167                         OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
168                         OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
169                         return;
170                         }
171                 }
172 #ifdef M_KEEP
173         /*
174          * Solaris libdevinfo.so.1 is effectively incomatible with
175          * libmalloc.so.1. Specifically, if application is linked with
176          * -lmalloc, it crashes upon startup with SIGSEGV in
177          * free(3LIBMALLOC) called by di_fini. Prior call to
178          * mallopt(M_KEEP,0) somehow helps... But not always...
179          */
180         if ((h = dlopen(NULL,RTLD_LAZY)))
181                 {
182                 union { void *p; int (*f)(int,int); } sym;
183                 if ((sym.p = dlsym(h,"mallopt"))) (*sym.f)(M_KEEP,0);
184                 dlclose(h);
185                 }
186 #endif
187         if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
188                 {
189                 di_init_t       di_init;
190                 di_fini_t       di_fini;
191                 di_walk_node_t  di_walk_node;
192                 di_node_name_t  di_node_name;
193                 di_node_t       root_node;
194
195                 if (!DLLINK(h,di_init))         break;
196                 if (!DLLINK(h,di_fini))         break;
197                 if (!DLLINK(h,di_walk_node))    break;
198                 if (!DLLINK(h,di_node_name))    break;
199
200                 if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
201                         {
202                         (*di_walk_node)(root_node,DI_WALK_SIBFIRST,
203                                         di_node_name,walk_nodename);
204                         (*di_fini)(root_node);
205                         }
206                 } while(0);
207
208         if (h) dlclose(h);
209         }
210
211 #else
212
213 static sigjmp_buf common_jmp;
214 static void common_handler(int sig) { siglongjmp(common_jmp,sig); }
215
216 void OPENSSL_cpuid_setup(void)
217         {
218         char *e;
219         struct sigaction        common_act,ill_oact,bus_oact;
220         sigset_t                all_masked,oset;
221         static int trigger=0;
222
223         if (trigger) return;
224         trigger=1;
225  
226         if ((e=getenv("OPENSSL_sparcv9cap")))
227                 {
228                 OPENSSL_sparcv9cap_P[0]=strtoul(e,NULL,0);
229                 if ((e=strchr(e,':')))
230                         OPENSSL_sparcv9cap_P[1]=strtoul(e+1,NULL,0);
231                 return;
232                 }
233
234         /* Initial value, fits UltraSPARC-I&II... */
235         OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED;
236
237         sigfillset(&all_masked);
238         sigdelset(&all_masked,SIGILL);
239         sigdelset(&all_masked,SIGTRAP);
240 #ifdef SIGEMT
241         sigdelset(&all_masked,SIGEMT);
242 #endif
243         sigdelset(&all_masked,SIGFPE);
244         sigdelset(&all_masked,SIGBUS);
245         sigdelset(&all_masked,SIGSEGV);
246         sigprocmask(SIG_SETMASK,&all_masked,&oset);
247
248         memset(&common_act,0,sizeof(common_act));
249         common_act.sa_handler = common_handler;
250         common_act.sa_mask    = all_masked;
251
252         sigaction(SIGILL,&common_act,&ill_oact);
253         sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */
254
255         if (sigsetjmp(common_jmp,1) == 0)
256                 {
257                 _sparcv9_rdtick();
258                 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
259                 }
260
261         if (sigsetjmp(common_jmp,1) == 0)
262                 {
263                 _sparcv9_vis1_probe();
264                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1|SPARCV9_BLK;
265                 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
266                 if (_sparcv9_vis1_instrument() >= 12)
267                         OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU);
268                 else
269                         {
270                         _sparcv9_vis2_probe();
271                         OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
272                         }
273                 }
274
275         if (sigsetjmp(common_jmp,1) == 0)
276                 {
277                 _sparcv9_fmadd_probe();
278                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
279                 }
280
281         /*
282          * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
283          * because VIS3 defines even integer instructions.
284          */
285         if (sigsetjmp(common_jmp,1) == 0)
286                 {
287                 _sparcv9_vis3_probe();
288                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
289                 }
290
291 #if 0   /* was planned at some point but never implemented in hardware */
292         if (sigsetjmp(common_jmp,1) == 0)
293                 {
294                 (void)_sparcv9_random();
295                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_RANDOM;
296                 }
297 #endif
298
299         /*
300          * In wait for better solution _sparcv9_rdcfr is masked by
301          * VIS3 flag, because it goes to uninterruptable endless
302          * loop on UltraSPARC II running Solaris. Things might be
303          * different on Linux...
304          */
305         if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS3) &&
306             sigsetjmp(common_jmp,1) == 0)
307                 {
308                 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
309                 }
310
311         sigaction(SIGBUS,&bus_oact,NULL);
312         sigaction(SIGILL,&ill_oact,NULL);
313
314         sigprocmask(SIG_SETMASK,&oset,NULL);
315
316         if (sizeof(size_t)==8)
317                 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
318 #ifdef __linux
319         else
320                 {
321                 int ret = syscall(340);
322
323                 if (ret>=0 && ret&1)
324                         OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
325                 }
326 #endif
327         }
328
329 #endif