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 */
15 static int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED;
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_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 ((OPENSSL_sparcv9cap_P&(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 unsigned long _sparcv9_rdwrasi(unsigned long);
31 void _sparcv9_vis1_probe(void);
33 unsigned long OPENSSL_rdtsc(void)
35 if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
36 #if defined(__sun) && defined(__SVR4)
42 return _sparcv9_rdtick();
45 #if defined(__sun) && defined(__SVR4)
48 #include <libdevinfo.h>
49 #include <sys/systeminfo.h>
51 typedef di_node_t (*di_init_t)(const char *,uint_t);
52 typedef void (*di_fini_t)(di_node_t);
53 typedef char * (*di_node_name_t)(di_node_t);
54 typedef int (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t));
56 #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
58 static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
60 char *name = (*di_node_name)(node);
62 /* This is expected to catch all UltraSPARC flavors prior T1 */
63 if (!strcmp (name,"SUNW,UltraSPARC") ||
64 !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
66 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
68 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
69 if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
70 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
72 return DI_WALK_TERMINATE;
74 /* This is expected to catch remaining UltraSPARCs, such as T1 */
75 else if (!strncmp(name,"SUNW,UltraSPARC",15))
77 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
79 return DI_WALK_TERMINATE;
82 return DI_WALK_CONTINUE;
85 void OPENSSL_cpuid_setup(void)
94 if ((e=getenv("OPENSSL_sparcv9cap")))
96 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
100 if (sysinfo(SI_MACHINE,si,sizeof(si))>0)
102 if (strcmp(si,"sun4v"))
103 /* FPU is preferred for all CPUs, but US-T1/2 */
104 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
107 if (sysinfo(SI_ISALIST,si,sizeof(si))>0)
109 if (strstr(si,"+vis"))
110 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
111 if (strstr(si,"+vis2"))
113 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
114 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
119 if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
123 di_walk_node_t di_walk_node;
124 di_node_name_t di_node_name;
127 if (!DLLINK(h,di_init)) break;
128 if (!DLLINK(h,di_fini)) break;
129 if (!DLLINK(h,di_walk_node)) break;
130 if (!DLLINK(h,di_node_name)) break;
132 if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
134 (*di_walk_node)(root_node,DI_WALK_SIBFIRST,
135 di_node_name,walk_nodename);
136 (*di_fini)(root_node);
145 static sigjmp_buf common_jmp;
146 static void common_handler(int sig) { siglongjmp(common_jmp,sig); }
148 void OPENSSL_cpuid_setup(void)
151 struct sigaction common_act,ill_oact,bus_oact;
152 sigset_t all_masked,oset;
156 if ((e=getenv("OPENSSL_sparcv9cap")))
158 OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
162 /* For now we assume that the rest supports UltraSPARC-I* only */
163 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
165 sigfillset(&all_masked);
166 sigdelset(&all_masked,SIGILL);
167 sigdelset(&all_masked,SIGTRAP);
169 sigdelset(&all_masked,SIGEMT);
171 sigdelset(&all_masked,SIGFPE);
172 sigdelset(&all_masked,SIGBUS);
173 sigdelset(&all_masked,SIGSEGV);
174 sigprocmask(SIG_SETMASK,&all_masked,&oset);
176 memset(&common_act,0,sizeof(common_act));
177 common_act.sa_handler = common_handler;
178 common_act.sa_mask = all_masked;
180 sigaction(SIGILL,&common_act,&ill_oact);
181 sigaction(SIGBUS,&common_act,&bus_oact);/* T1 fails 16-bit ldda */
182 oasi = _sparcv9_rdwrasi(0xD2); /* ASI_FL16_P */
183 if ((sig=sigsetjmp(common_jmp,0)) == 0)
185 _sparcv9_vis1_probe();
186 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
188 else if (sig == SIGBUS) /* T1 fails 16-bit ldda */
190 OPENSSL_sparcv9cap_P &= ~SPARCV9_PREFER_FPU;
194 OPENSSL_sparcv9cap_P &= ~SPARCV9_VIS1;
196 _sparcv9_rdwrasi(oasi);
197 sigaction(SIGBUS,&bus_oact,NULL);
198 sigaction(SIGILL,&ill_oact,NULL);
200 sigaction(SIGILL,&common_act,&ill_oact);
201 if (sigsetjmp(common_jmp,0) == 0)
204 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
208 OPENSSL_sparcv9cap_P |= SPARCV9_TICK_PRIVILEGED;
210 sigaction(SIGILL,&ill_oact,NULL);
212 sigprocmask(SIG_SETMASK,&oset,NULL);