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