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_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
);
22 if (!(num
&1) && num
>=6)
24 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS3
))
25 return bn_mul_mont_vis3(rp
,ap
,bp
,np
,n0
,num
);
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
);
31 return bn_mul_mont_int(rp
,ap
,bp
,np
,n0
,num
);
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);
45 unsigned long OPENSSL_rdtsc(void)
47 if (OPENSSL_sparcv9cap_P
[0]&SPARCV9_TICK_PRIVILEGED
)
48 #if defined(__sun) && defined(__SVR4)
54 return _sparcv9_rdtick();
57 size_t OPENSSL_instrument_bus(unsigned int *out
,size_t cnt
)
59 if ((OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
)) ==
61 return _sparcv9_vis1_instrument_bus(out
,cnt
);
66 size_t OPENSSL_instrument_bus2(unsigned int *out
,size_t cnt
,size_t max
)
68 if ((OPENSSL_sparcv9cap_P
[0]&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
)) ==
70 return _sparcv9_vis1_instrument_bus2(out
,cnt
,max
);
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)
81 #include <libdevinfo.h>
82 #include <sys/systeminfo.h>
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
));
89 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
91 static int walk_nodename(di_node_t node
, di_node_name_t di_node_name
)
93 char *name
= (*di_node_name
)(node
);
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 */
99 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
|SPARCV9_VIS1
;
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
;
105 return DI_WALK_TERMINATE
;
107 /* This is expected to catch remaining UltraSPARCs, such as T1 */
108 else if (!strncmp(name
,"SUNW,UltraSPARC",15))
110 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
112 return DI_WALK_TERMINATE
;
115 return DI_WALK_CONTINUE
;
118 void OPENSSL_cpuid_setup(void)
122 static int trigger
=0;
127 if ((e
=getenv("OPENSSL_sparcv9cap")))
129 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
133 if (sysinfo(SI_MACHINE
,si
,sizeof(si
))>0)
135 if (strcmp(si
,"sun4v"))
136 /* FPU is preferred for all CPUs, but US-T1/2 */
137 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
;
140 if (sysinfo(SI_ISALIST
,si
,sizeof(si
))>0)
142 if (strstr(si
,"+vis"))
143 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
|SPARCV9_BLK
;
144 if (strstr(si
,"+vis2"))
146 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
147 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
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...
159 if ((h
= dlopen(NULL
,RTLD_LAZY
)))
161 union { void *p
; int (*f
)(int,int); } sym
;
162 if ((sym
.p
= dlsym(h
,"mallopt"))) (*sym
.f
)(M_KEEP
,0);
166 if ((h
= dlopen("libdevinfo.so.1",RTLD_LAZY
))) do
170 di_walk_node_t di_walk_node
;
171 di_node_name_t di_node_name
;
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;
179 if ((root_node
= (*di_init
)("/",DINFOSUBTREE
))!=DI_NODE_NIL
)
181 (*di_walk_node
)(root_node
,DI_WALK_SIBFIRST
,
182 di_node_name
,walk_nodename
);
183 (*di_fini
)(root_node
);
192 static sigjmp_buf common_jmp
;
193 static void common_handler(int sig
) { siglongjmp(common_jmp
,sig
); }
195 void OPENSSL_cpuid_setup(void)
198 struct sigaction common_act
,ill_oact
,bus_oact
;
199 sigset_t all_masked
,oset
;
200 static int trigger
=0;
205 if ((e
=getenv("OPENSSL_sparcv9cap")))
207 OPENSSL_sparcv9cap_P
[0]=strtoul(e
,NULL
,0);
208 if ((e
=strchr(e
,':')))
209 OPENSSL_sparcv9cap_P
[1]=strtoul(e
+1,NULL
,0);
213 /* Initial value, fits UltraSPARC-I&II... */
214 OPENSSL_sparcv9cap_P
[0] = SPARCV9_PREFER_FPU
|SPARCV9_TICK_PRIVILEGED
;
216 sigfillset(&all_masked
);
217 sigdelset(&all_masked
,SIGILL
);
218 sigdelset(&all_masked
,SIGTRAP
);
220 sigdelset(&all_masked
,SIGEMT
);
222 sigdelset(&all_masked
,SIGFPE
);
223 sigdelset(&all_masked
,SIGBUS
);
224 sigdelset(&all_masked
,SIGSEGV
);
225 sigprocmask(SIG_SETMASK
,&all_masked
,&oset
);
227 memset(&common_act
,0,sizeof(common_act
));
228 common_act
.sa_handler
= common_handler
;
229 common_act
.sa_mask
= all_masked
;
231 sigaction(SIGILL
,&common_act
,&ill_oact
);
232 sigaction(SIGBUS
,&common_act
,&bus_oact
);/* T1 fails 16-bit ldda [on Linux] */
234 if (sigsetjmp(common_jmp
,1) == 0)
237 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
240 if (sigsetjmp(common_jmp
,1) == 0)
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
);
249 _sparcv9_vis2_probe();
250 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
254 if (sigsetjmp(common_jmp
,1) == 0)
256 _sparcv9_fmadd_probe();
257 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FMADD
;
261 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
262 * because VIS3 defines even integer instructions.
264 if (sigsetjmp(common_jmp
,1) == 0)
266 _sparcv9_vis3_probe();
267 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS3
;
270 if (sigsetjmp(common_jmp
,1) == 0)
272 (void)_sparcv9_random();
273 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_RANDOM
;
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...
282 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS3
) &&
283 sigsetjmp(common_jmp
,1) == 0)
285 OPENSSL_sparcv9cap_P
[1] = (unsigned int)_sparcv9_rdcfr();
288 sigaction(SIGBUS
,&bus_oact
,NULL
);
289 sigaction(SIGILL
,&ill_oact
,NULL
);
291 sigprocmask(SIG_SETMASK
,&oset
,NULL
);