7 #include <openssl/bn.h>
9 #include "sparc_arch.h"
11 #if defined(__GNUC__) && defined(__linux)
12 __attribute__((visibility("hidden")))
14 unsigned int OPENSSL_sparcv9cap_P
[2]={SPARCV9_TICK_PRIVILEGED
,0};
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
)
18 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
);
19 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 if (num
>=8 && !(num
&1) &&
22 (OPENSSL_sparcv9cap_P
[0]&(SPARCV9_PREFER_FPU
|SPARCV9_VIS1
)) ==
23 (SPARCV9_PREFER_FPU
|SPARCV9_VIS1
))
24 return bn_mul_mont_fpu(rp
,ap
,bp
,np
,n0
,num
);
26 return bn_mul_mont_int(rp
,ap
,bp
,np
,n0
,num
);
29 unsigned long _sparcv9_rdtick(void);
30 void _sparcv9_vis1_probe(void);
31 unsigned long _sparcv9_vis1_instrument(void);
32 void _sparcv9_vis2_probe(void);
33 void _sparcv9_fmadd_probe(void);
34 unsigned long _sparcv9_rdcfr(void);
35 void _sparcv9_vis3_probe(void);
36 unsigned long _sparcv9_random(void);
37 size_t _sparcv9_vis1_instrument_bus(unsigned int *,size_t);
38 size_t _sparcv8_vis1_instrument_bus2(unsigned int *,size_t,size_t);
40 unsigned long OPENSSL_rdtsc(void)
42 if (OPENSSL_sparcv9cap_P
[0]&SPARCV9_TICK_PRIVILEGED
)
43 #if defined(__sun) && defined(__SVR4)
49 return _sparcv9_rdtick();
52 size_t OPENSSL_instrument_bus(unsigned int *out
,size_t cnt
)
54 if (OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
) ==
56 return _sparcv9_vis1_instrument_bus(out
,cnt
);
61 size_t OPENSSL_instrument_bus2(unsigned int *out
,size_t cnt
,size_t max
)
63 if (OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
) ==
65 return _sparcv9_vis1_instrument_bus2(out
,cnt
,max
);
70 #if 0 && defined(__sun) && defined(__SVR4)
71 /* This code path is disabled, because of incompatibility of
72 * libdevinfo.so.1 and libmalloc.so.1 (see below for details)
76 #include <libdevinfo.h>
77 #include <sys/systeminfo.h>
79 typedef di_node_t (*di_init_t
)(const char *,uint_t
);
80 typedef void (*di_fini_t
)(di_node_t
);
81 typedef char * (*di_node_name_t
)(di_node_t
);
82 typedef int (*di_walk_node_t
)(di_node_t
,uint_t
,di_node_name_t
,int (*)(di_node_t
,di_node_name_t
));
84 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
86 static int walk_nodename(di_node_t node
, di_node_name_t di_node_name
)
88 char *name
= (*di_node_name
)(node
);
90 /* This is expected to catch all UltraSPARC flavors prior T1 */
91 if (!strcmp (name
,"SUNW,UltraSPARC") ||
92 !strncmp(name
,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
94 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
|SPARCV9_VIS1
;
96 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
97 if (name
[14]!='\0' && name
[17]!='\0' && name
[18]!='\0')
98 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
100 return DI_WALK_TERMINATE
;
102 /* This is expected to catch remaining UltraSPARCs, such as T1 */
103 else if (!strncmp(name
,"SUNW,UltraSPARC",15))
105 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
107 return DI_WALK_TERMINATE
;
110 return DI_WALK_CONTINUE
;
113 void OPENSSL_cpuid_setup(void)
117 static int trigger
=0;
122 if ((e
=getenv("OPENSSL_sparcv9cap")))
124 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
128 if (sysinfo(SI_MACHINE
,si
,sizeof(si
))>0)
130 if (strcmp(si
,"sun4v"))
131 /* FPU is preferred for all CPUs, but US-T1/2 */
132 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
;
135 if (sysinfo(SI_ISALIST
,si
,sizeof(si
))>0)
137 if (strstr(si
,"+vis"))
138 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
|SPARCV9_BLK
;
139 if (strstr(si
,"+vis2"))
141 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
142 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
148 * Solaris libdevinfo.so.1 is effectively incomatible with
149 * libmalloc.so.1. Specifically, if application is linked with
150 * -lmalloc, it crashes upon startup with SIGSEGV in
151 * free(3LIBMALLOC) called by di_fini. Prior call to
152 * mallopt(M_KEEP,0) somehow helps... But not always...
154 if ((h
= dlopen(NULL
,RTLD_LAZY
)))
156 union { void *p
; int (*f
)(int,int); } sym
;
157 if ((sym
.p
= dlsym(h
,"mallopt"))) (*sym
.f
)(M_KEEP
,0);
161 if ((h
= dlopen("libdevinfo.so.1",RTLD_LAZY
))) do
165 di_walk_node_t di_walk_node
;
166 di_node_name_t di_node_name
;
169 if (!DLLINK(h
,di_init
)) break;
170 if (!DLLINK(h
,di_fini
)) break;
171 if (!DLLINK(h
,di_walk_node
)) break;
172 if (!DLLINK(h
,di_node_name
)) break;
174 if ((root_node
= (*di_init
)("/",DINFOSUBTREE
))!=DI_NODE_NIL
)
176 (*di_walk_node
)(root_node
,DI_WALK_SIBFIRST
,
177 di_node_name
,walk_nodename
);
178 (*di_fini
)(root_node
);
187 static sigjmp_buf common_jmp
;
188 static void common_handler(int sig
) { siglongjmp(common_jmp
,sig
); }
190 void OPENSSL_cpuid_setup(void)
193 struct sigaction common_act
,ill_oact
,bus_oact
;
194 sigset_t all_masked
,oset
;
195 static int trigger
=0;
200 if ((e
=getenv("OPENSSL_sparcv9cap")))
202 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
203 if ((e
=strchr(e
,':')))
204 OPENSSL_sparcv9cap_P
[1]=strtoul(e
+1,NULL
,0);
208 /* Initial value, fits UltraSPARC-I&II... */
209 OPENSSL_sparcv9cap_P
[0] = SPARCV9_PREFER_FPU
|SPARCV9_TICK_PRIVILEGED
;
211 sigfillset(&all_masked
);
212 sigdelset(&all_masked
,SIGILL
);
213 sigdelset(&all_masked
,SIGTRAP
);
215 sigdelset(&all_masked
,SIGEMT
);
217 sigdelset(&all_masked
,SIGFPE
);
218 sigdelset(&all_masked
,SIGBUS
);
219 sigdelset(&all_masked
,SIGSEGV
);
220 sigprocmask(SIG_SETMASK
,&all_masked
,&oset
);
222 memset(&common_act
,0,sizeof(common_act
));
223 common_act
.sa_handler
= common_handler
;
224 common_act
.sa_mask
= all_masked
;
226 sigaction(SIGILL
,&common_act
,&ill_oact
);
227 sigaction(SIGBUS
,&common_act
,&bus_oact
);/* T1 fails 16-bit ldda [on Linux] */
229 if (sigsetjmp(common_jmp
,1) == 0)
232 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
235 if (sigsetjmp(common_jmp
,1) == 0)
237 _sparcv9_vis1_probe();
238 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
|SPARCV9_BLK
;
239 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
240 if (_sparcv9_vis1_instrument() >= 12)
241 OPENSSL_sparcv9cap_P
[0] &= ~(SPARCV9_VIS1
|SPARCV9_PREFER_FPU
);
244 _sparcv9_vis2_probe();
245 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
249 if (sigsetjmp(common_jmp
,1) == 0)
251 _sparcv9_fmadd_probe();
252 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FMADD
;
256 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
257 * because VIS3 defines even integer instructions.
259 if (sigsetjmp(common_jmp
,1) == 0)
261 _sparcv9_vis3_probe();
262 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS3
;
265 if (sigsetjmp(common_jmp
,1) == 0)
267 (void)_sparcv9_random();
268 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_RANDOM
;
272 * In wait for better solution _sparcv9_rdcfr is masked by
273 * VIS3 flag, because it goes to uninterruptable endless
274 * loop on UltraSPARC II running Solaris. Things might be
275 * different on Linux...
277 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS3
) &&
278 sigsetjmp(common_jmp
,1) == 0)
280 OPENSSL_sparcv9cap_P
[1] = (unsigned int)_sparcv9_rdcfr();
283 sigaction(SIGBUS
,&bus_oact
,NULL
);
284 sigaction(SIGILL
,&ill_oact
,NULL
);
286 sigprocmask(SIG_SETMASK
,&oset
,NULL
);