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