8 #include <openssl/bn.h>
10 #include "sparc_arch.h"
12 #if defined(__GNUC__) && defined(__linux)
13 __attribute__((visibility("hidden")))
15 unsigned int OPENSSL_sparcv9cap_P
[2]={SPARCV9_TICK_PRIVILEGED
,0};
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
)
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
);
23 if (!(num
&1) && num
>=6)
25 if ((num
&15)==0 && num
<=64 &&
26 (OPENSSL_sparcv9cap_P
[1]&(CFR_MONTMUL
|CFR_MONTSQR
))==
27 (CFR_MONTMUL
|CFR_MONTSQR
))
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];
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
);
44 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS3
))
45 return bn_mul_mont_vis3(rp
,ap
,bp
,np
,n0
,num
);
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
);
51 return bn_mul_mont_int(rp
,ap
,bp
,np
,n0
,num
);
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);
65 unsigned long OPENSSL_rdtsc(void)
67 if (OPENSSL_sparcv9cap_P
[0]&SPARCV9_TICK_PRIVILEGED
)
68 #if defined(__sun) && defined(__SVR4)
74 return _sparcv9_rdtick();
77 size_t OPENSSL_instrument_bus(unsigned int *out
,size_t cnt
)
79 if ((OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
)) ==
81 return _sparcv9_vis1_instrument_bus(out
,cnt
);
86 size_t OPENSSL_instrument_bus2(unsigned int *out
,size_t cnt
,size_t max
)
88 if ((OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
)) ==
90 return _sparcv9_vis1_instrument_bus2(out
,cnt
,max
);
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)
101 #include <libdevinfo.h>
102 #include <sys/systeminfo.h>
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
));
109 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
111 static int walk_nodename(di_node_t node
, di_node_name_t di_node_name
)
113 char *name
= (*di_node_name
)(node
);
115 /* This is expected to catch all UltraSPARC flavors prior T1 */
116 if (!strcmp (name
,"SUNW,UltraSPARC") ||
117 !strncmp(name
,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
119 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
|SPARCV9_VIS1
;
121 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
122 if (name
[14]!='\0' && name
[17]!='\0' && name
[18]!='\0')
123 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
125 return DI_WALK_TERMINATE
;
127 /* This is expected to catch remaining UltraSPARCs, such as T1 */
128 else if (!strncmp(name
,"SUNW,UltraSPARC",15))
130 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
132 return DI_WALK_TERMINATE
;
135 return DI_WALK_CONTINUE
;
138 void OPENSSL_cpuid_setup(void)
142 static int trigger
=0;
147 if ((e
=getenv("OPENSSL_sparcv9cap")))
149 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
153 if (sysinfo(SI_MACHINE
,si
,sizeof(si
))>0)
155 if (strcmp(si
,"sun4v"))
156 /* FPU is preferred for all CPUs, but US-T1/2 */
157 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
;
160 if (sysinfo(SI_ISALIST
,si
,sizeof(si
))>0)
162 if (strstr(si
,"+vis"))
163 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
|SPARCV9_BLK
;
164 if (strstr(si
,"+vis2"))
166 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
167 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
173 * Solaris libdevinfo.so.1 is effectively incomatible with
174 * libmalloc.so.1. Specifically, if application is linked with
175 * -lmalloc, it crashes upon startup with SIGSEGV in
176 * free(3LIBMALLOC) called by di_fini. Prior call to
177 * mallopt(M_KEEP,0) somehow helps... But not always...
179 if ((h
= dlopen(NULL
,RTLD_LAZY
)))
181 union { void *p
; int (*f
)(int,int); } sym
;
182 if ((sym
.p
= dlsym(h
,"mallopt"))) (*sym
.f
)(M_KEEP
,0);
186 if ((h
= dlopen("libdevinfo.so.1",RTLD_LAZY
))) do
190 di_walk_node_t di_walk_node
;
191 di_node_name_t di_node_name
;
194 if (!DLLINK(h
,di_init
)) break;
195 if (!DLLINK(h
,di_fini
)) break;
196 if (!DLLINK(h
,di_walk_node
)) break;
197 if (!DLLINK(h
,di_node_name
)) break;
199 if ((root_node
= (*di_init
)("/",DINFOSUBTREE
))!=DI_NODE_NIL
)
201 (*di_walk_node
)(root_node
,DI_WALK_SIBFIRST
,
202 di_node_name
,walk_nodename
);
203 (*di_fini
)(root_node
);
212 static sigjmp_buf common_jmp
;
213 static void common_handler(int sig
) { siglongjmp(common_jmp
,sig
); }
215 void OPENSSL_cpuid_setup(void)
218 struct sigaction common_act
,ill_oact
,bus_oact
;
219 sigset_t all_masked
,oset
;
220 static int trigger
=0;
225 if ((e
=getenv("OPENSSL_sparcv9cap")))
227 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
228 if ((e
=strchr(e
,':')))
229 OPENSSL_sparcv9cap_P
[1]=strtoul(e
+1,NULL
,0);
233 /* Initial value, fits UltraSPARC-I&II... */
234 OPENSSL_sparcv9cap_P
[0] = SPARCV9_PREFER_FPU
|SPARCV9_TICK_PRIVILEGED
;
236 sigfillset(&all_masked
);
237 sigdelset(&all_masked
,SIGILL
);
238 sigdelset(&all_masked
,SIGTRAP
);
240 sigdelset(&all_masked
,SIGEMT
);
242 sigdelset(&all_masked
,SIGFPE
);
243 sigdelset(&all_masked
,SIGBUS
);
244 sigdelset(&all_masked
,SIGSEGV
);
245 sigprocmask(SIG_SETMASK
,&all_masked
,&oset
);
247 memset(&common_act
,0,sizeof(common_act
));
248 common_act
.sa_handler
= common_handler
;
249 common_act
.sa_mask
= all_masked
;
251 sigaction(SIGILL
,&common_act
,&ill_oact
);
252 sigaction(SIGBUS
,&common_act
,&bus_oact
);/* T1 fails 16-bit ldda [on Linux] */
254 if (sigsetjmp(common_jmp
,1) == 0)
257 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
260 if (sigsetjmp(common_jmp
,1) == 0)
262 _sparcv9_vis1_probe();
263 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
|SPARCV9_BLK
;
264 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
265 if (_sparcv9_vis1_instrument() >= 12)
266 OPENSSL_sparcv9cap_P
[0] &= ~(SPARCV9_VIS1
|SPARCV9_PREFER_FPU
);
269 _sparcv9_vis2_probe();
270 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
274 if (sigsetjmp(common_jmp
,1) == 0)
276 _sparcv9_fmadd_probe();
277 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FMADD
;
281 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
282 * because VIS3 defines even integer instructions.
284 if (sigsetjmp(common_jmp
,1) == 0)
286 _sparcv9_vis3_probe();
287 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS3
;
290 #if 0 /* was planned at some point but never implemented in hardware */
291 if (sigsetjmp(common_jmp
,1) == 0)
293 (void)_sparcv9_random();
294 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_RANDOM
;
299 * In wait for better solution _sparcv9_rdcfr is masked by
300 * VIS3 flag, because it goes to uninterruptable endless
301 * loop on UltraSPARC II running Solaris. Things might be
302 * different on Linux...
304 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS3
) &&
305 sigsetjmp(common_jmp
,1) == 0)
307 OPENSSL_sparcv9cap_P
[1] = (unsigned int)_sparcv9_rdcfr();
310 sigaction(SIGBUS
,&bus_oact
,NULL
);
311 sigaction(SIGILL
,&ill_oact
,NULL
);
313 sigprocmask(SIG_SETMASK
,&oset
,NULL
);
315 if (sizeof(size_t)==8)
316 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_64BIT_STACK
;
320 int ret
= syscall(340);
323 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_64BIT_STACK
;