7 #include <openssl/bn.h>
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 */
16 static int OPENSSL_sparcv9cap_P
=SPARCV9_TICK_PRIVILEGED
;
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
)
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 ((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
);
27 return bn_mul_mont_int(rp
,ap
,bp
,np
,n0
,num
);
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);
38 unsigned long OPENSSL_rdtsc(void)
40 if (OPENSSL_sparcv9cap_P
&SPARCV9_TICK_PRIVILEGED
)
41 #if defined(__sun) && defined(__SVR4)
47 return _sparcv9_rdtick();
50 size_t OPENSSL_instrument_bus(unsigned int *out
,size_t cnt
)
52 if (OPENSSL_sparcv9cap_P
&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
) ==
54 return _sparcv9_vis1_instrument_bus(out
,cnt
);
59 size_t OPENSSL_instrument_bus2(unsigned int *out
,size_t cnt
,size_t max
)
61 if (OPENSSL_sparcv9cap_P
&(SPARCV9_TICK_PRIVILEGED
|SPARCV9_BLK
) ==
63 return _sparcv9_vis1_instrument_bus2(out
,cnt
,max
);
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)
74 #include <libdevinfo.h>
75 #include <sys/systeminfo.h>
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
));
82 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
84 static int walk_nodename(di_node_t node
, di_node_name_t di_node_name
)
86 char *name
= (*di_node_name
)(node
);
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 */
92 OPENSSL_sparcv9cap_P
|= SPARCV9_PREFER_FPU
|SPARCV9_VIS1
;
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
;
98 return DI_WALK_TERMINATE
;
100 /* This is expected to catch remaining UltraSPARCs, such as T1 */
101 else if (!strncmp(name
,"SUNW,UltraSPARC",15))
103 OPENSSL_sparcv9cap_P
&= ~SPARCV9_TICK_PRIVILEGED
;
105 return DI_WALK_TERMINATE
;
108 return DI_WALK_CONTINUE
;
111 void OPENSSL_cpuid_setup(void)
115 static int trigger
=0;
120 if ((e
=getenv("OPENSSL_sparcv9cap")))
122 OPENSSL_sparcv9cap_P
=strtoul(e
,NULL
,0);
126 if (sysinfo(SI_MACHINE
,si
,sizeof(si
))>0)
128 if (strcmp(si
,"sun4v"))
129 /* FPU is preferred for all CPUs, but US-T1/2 */
130 OPENSSL_sparcv9cap_P
|= SPARCV9_PREFER_FPU
;
133 if (sysinfo(SI_ISALIST
,si
,sizeof(si
))>0)
135 if (strstr(si
,"+vis"))
136 OPENSSL_sparcv9cap_P
|= SPARCV9_VIS1
|SPARCV9_BLK
;
137 if (strstr(si
,"+vis2"))
139 OPENSSL_sparcv9cap_P
|= SPARCV9_VIS2
;
140 OPENSSL_sparcv9cap_P
&= ~SPARCV9_TICK_PRIVILEGED
;
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...
152 if ((h
= dlopen(NULL
,RTLD_LAZY
)))
154 union { void *p
; int (*f
)(int,int); } sym
;
155 if ((sym
.p
= dlsym(h
,"mallopt"))) (*sym
.f
)(M_KEEP
,0);
159 if ((h
= dlopen("libdevinfo.so.1",RTLD_LAZY
))) do
163 di_walk_node_t di_walk_node
;
164 di_node_name_t di_node_name
;
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;
172 if ((root_node
= (*di_init
)("/",DINFOSUBTREE
))!=DI_NODE_NIL
)
174 (*di_walk_node
)(root_node
,DI_WALK_SIBFIRST
,
175 di_node_name
,walk_nodename
);
176 (*di_fini
)(root_node
);
185 static sigjmp_buf common_jmp
;
186 static void common_handler(int sig
) { siglongjmp(common_jmp
,sig
); }
188 void OPENSSL_cpuid_setup(void)
191 struct sigaction common_act
,ill_oact
,bus_oact
;
192 sigset_t all_masked
,oset
;
193 static int trigger
=0;
198 if ((e
=getenv("OPENSSL_sparcv9cap")))
200 OPENSSL_sparcv9cap_P
=strtoul(e
,NULL
,0);
204 /* Initial value, fits UltraSPARC-I&II... */
205 OPENSSL_sparcv9cap_P
= SPARCV9_PREFER_FPU
|SPARCV9_TICK_PRIVILEGED
;
207 sigfillset(&all_masked
);
208 sigdelset(&all_masked
,SIGILL
);
209 sigdelset(&all_masked
,SIGTRAP
);
211 sigdelset(&all_masked
,SIGEMT
);
213 sigdelset(&all_masked
,SIGFPE
);
214 sigdelset(&all_masked
,SIGBUS
);
215 sigdelset(&all_masked
,SIGSEGV
);
216 sigprocmask(SIG_SETMASK
,&all_masked
,&oset
);
218 memset(&common_act
,0,sizeof(common_act
));
219 common_act
.sa_handler
= common_handler
;
220 common_act
.sa_mask
= all_masked
;
222 sigaction(SIGILL
,&common_act
,&ill_oact
);
223 sigaction(SIGBUS
,&common_act
,&bus_oact
);/* T1 fails 16-bit ldda [on Linux] */
225 if (sigsetjmp(common_jmp
,1) == 0)
228 OPENSSL_sparcv9cap_P
&= ~SPARCV9_TICK_PRIVILEGED
;
231 if (sigsetjmp(common_jmp
,1) == 0)
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
);
240 _sparcv9_vis2_probe();
241 OPENSSL_sparcv9cap_P
|= SPARCV9_VIS2
;
245 if (sigsetjmp(common_jmp
,1) == 0)
247 _sparcv9_fmadd_probe();
248 OPENSSL_sparcv9cap_P
|= SPARCV9_FMADD
;
251 sigaction(SIGBUS
,&bus_oact
,NULL
);
252 sigaction(SIGILL
,&ill_oact
,NULL
);
254 sigprocmask(SIG_SETMASK
,&oset
,NULL
);