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