modes/cfb128.c: make it indent-friendly.
[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 <openssl/bn.h>
8
9 #define SPARCV9_TICK_PRIVILEGED (1<<0)
10 #define SPARCV9_PREFER_FPU      (1<<1)
11 #define SPARCV9_VIS1            (1<<2)
12 #define SPARCV9_VIS2            (1<<3)  /* reserved */
13 #define SPARCV9_FMADD           (1<<4)  /* reserved for SPARC64 V */
14
15 static int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED;
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&(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
36 unsigned long OPENSSL_rdtsc(void)
37         {
38         if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
39 #if defined(__sun) && defined(__SVR4)
40                 return gethrtime();
41 #else
42                 return 0;
43 #endif
44         else
45                 return _sparcv9_rdtick();
46         }
47
48 #if 0 && defined(__sun) && defined(__SVR4)
49 /* This code path is disabled, because of incompatibility of
50  * libdevinfo.so.1 and libmalloc.so.1 (see below for details)
51  */
52 #include <malloc.h>
53 #include <dlfcn.h>
54 #include <libdevinfo.h>
55 #include <sys/systeminfo.h>
56
57 typedef di_node_t (*di_init_t)(const char *,uint_t);
58 typedef void      (*di_fini_t)(di_node_t);
59 typedef char *    (*di_node_name_t)(di_node_t);
60 typedef int       (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t));
61
62 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
63
64 static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
65         {
66         char *name = (*di_node_name)(node);
67
68         /* This is expected to catch all UltraSPARC flavors prior T1 */
69         if (!strcmp (name,"SUNW,UltraSPARC") ||
70                 /* covers II,III,IV */
71             !strncmp(name,"SUNW,UltraSPARC-I",17))
72                 {
73                 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
74
75                 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
76                 if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
77                         OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
78
79                 return DI_WALK_TERMINATE;
80                 }
81         /* This is expected to catch remaining UltraSPARCs, such as T1 */
82         else if (!strncmp(name,"SUNW,UltraSPARC",15))
83                 {
84                 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
85
86                 return DI_WALK_TERMINATE;
87                 }
88
89         return DI_WALK_CONTINUE;
90         }
91
92 void OPENSSL_cpuid_setup(void)
93         {
94         void *h;
95         char *e,si[256];
96         static int trigger=0;
97
98         if (trigger) return;
99         trigger=1;
100
101         if ((e=getenv("OPENSSL_sparcv9cap")))
102                 {
103                 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
104                 return;
105                 }
106
107         if (sysinfo(SI_MACHINE,si,sizeof(si))>0)
108                 {
109                 if (strcmp(si,"sun4v"))
110                         /* FPU is preferred for all CPUs, but US-T1/2 */
111                         OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
112                 }
113
114         if (sysinfo(SI_ISALIST,si,sizeof(si))>0)
115                 {
116                 if (strstr(si,"+vis"))
117                         OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
118                 if (strstr(si,"+vis2"))
119                         {
120                         OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
121                         OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
122                         return;
123                         }
124                 }
125 #ifdef M_KEEP
126         /*
127          * Solaris libdevinfo.so.1 is effectively incomatible with
128          * libmalloc.so.1. Specifically, if application is linked with
129          * -lmalloc, it crashes upon startup with SIGSEGV in
130          * free(3LIBMALLOC) called by di_fini. Prior call to
131          * mallopt(M_KEEP,0) somehow helps... But not always...
132          */
133         if ((h = dlopen(NULL,RTLD_LAZY)))
134                 {
135                 union { void *p; int (*f)(int,int); } sym;
136                 if ((sym.p = dlsym(h,"mallopt"))) (*sym.f)(M_KEEP,0);
137                 dlclose(h);
138                 }
139 #endif
140         if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
141                 {
142                 di_init_t       di_init;
143                 di_fini_t       di_fini;
144                 di_walk_node_t  di_walk_node;
145                 di_node_name_t  di_node_name;
146                 di_node_t       root_node;
147
148                 if (!DLLINK(h,di_init))         break;
149                 if (!DLLINK(h,di_fini))         break;
150                 if (!DLLINK(h,di_walk_node))    break;
151                 if (!DLLINK(h,di_node_name))    break;
152
153                 if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
154                         {
155                         (*di_walk_node)(root_node,DI_WALK_SIBFIRST,
156                                         di_node_name,walk_nodename);
157                         (*di_fini)(root_node);
158                         }
159                 } while(0);
160
161         if (h) dlclose(h);
162         }
163
164 #else
165
166 static sigjmp_buf common_jmp;
167 static void common_handler(int sig) { siglongjmp(common_jmp,sig); }
168
169 void OPENSSL_cpuid_setup(void)
170         {
171         char *e;
172         struct sigaction        common_act,ill_oact,bus_oact;
173         sigset_t                all_masked,oset;
174         static int trigger=0;
175
176         if (trigger) return;
177         trigger=1;
178  
179         if ((e=getenv("OPENSSL_sparcv9cap")))
180                 {
181                 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
182                 return;
183                 }
184
185         /* Initial value, fits UltraSPARC-I&II... */
186         OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU|SPARCV9_TICK_PRIVILEGED;
187
188         sigfillset(&all_masked);
189         sigdelset(&all_masked,SIGILL);
190         sigdelset(&all_masked,SIGTRAP);
191 #ifdef SIGEMT
192         sigdelset(&all_masked,SIGEMT);
193 #endif
194         sigdelset(&all_masked,SIGFPE);
195         sigdelset(&all_masked,SIGBUS);
196         sigdelset(&all_masked,SIGSEGV);
197         sigprocmask(SIG_SETMASK,&all_masked,&oset);
198
199         memset(&common_act,0,sizeof(common_act));
200         common_act.sa_handler = common_handler;
201         common_act.sa_mask    = all_masked;
202
203         sigaction(SIGILL,&common_act,&ill_oact);
204         sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda [on Linux] */
205
206         if (sigsetjmp(common_jmp,1) == 0)
207                 {
208                 _sparcv9_rdtick();
209                 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
210                 }
211
212         if (sigsetjmp(common_jmp,1) == 0)
213                 {
214                 _sparcv9_vis1_probe();
215                 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
216                 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
217                 if (_sparcv9_vis1_instrument() >= 12)
218                         OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1|SPARCV9_PREFER_FPU);
219                 else
220                         {
221                         _sparcv9_vis2_probe();
222                         OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
223                         }
224                 }
225
226         if (sigsetjmp(common_jmp,1) == 0)
227                 {
228                 _sparcv9_fmadd_probe();
229                 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD;
230                 }
231
232         sigaction(SIGBUS,&bus_oact,NULL);
233         sigaction(SIGILL,&ill_oact,NULL);
234
235         sigprocmask(SIG_SETMASK,&oset,NULL);
236         }
237
238 #endif