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,
18 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,
21 const BN_ULONG *np, const BN_ULONG *n0, int num);
22 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
23 const BN_ULONG *np, const BN_ULONG *n0, int num);
25 if (num >= 8 && !(num & 1) &&
26 (OPENSSL_sparcv9cap_P & (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
27 (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
28 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
30 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
33 unsigned long _sparcv9_rdtick(void);
34 void _sparcv9_vis1_probe(void);
35 unsigned long _sparcv9_vis1_instrument(void);
36 void _sparcv9_vis2_probe(void);
37 void _sparcv9_fmadd_probe(void);
39 unsigned long OPENSSL_rdtsc(void)
41 if (OPENSSL_sparcv9cap_P & SPARCV9_TICK_PRIVILEGED)
42 #if defined(__sun) && defined(__SVR4)
48 return _sparcv9_rdtick();
51 #if 0 && defined(__sun) && defined(__SVR4)
53 * This code path is disabled, because of incompatibility of libdevinfo.so.1
54 * and libmalloc.so.1 (see below for details)
58 # include <libdevinfo.h>
59 # include <sys/systeminfo.h>
61 typedef di_node_t(*di_init_t) (const char *, uint_t);
62 typedef void (*di_fini_t) (di_node_t);
63 typedef char *(*di_node_name_t) (di_node_t);
64 typedef int (*di_walk_node_t) (di_node_t, uint_t, di_node_name_t,
65 int (*)(di_node_t, di_node_name_t));
67 # define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
69 static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
71 char *name = (*di_node_name) (node);
73 /* This is expected to catch all UltraSPARC flavors prior T1 */
74 if (!strcmp(name, "SUNW,UltraSPARC") ||
75 /* covers II,III,IV */
76 !strncmp(name, "SUNW,UltraSPARC-I", 17)) {
77 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU | SPARCV9_VIS1;
79 /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
80 if (name[14] != '\0' && name[17] != '\0' && name[18] != '\0')
81 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
83 return DI_WALK_TERMINATE;
85 /* This is expected to catch remaining UltraSPARCs, such as T1 */
86 else if (!strncmp(name, "SUNW,UltraSPARC", 15)) {
87 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
89 return DI_WALK_TERMINATE;
92 return DI_WALK_CONTINUE;
95 void OPENSSL_cpuid_setup(void)
99 static int trigger = 0;
105 if ((e = getenv("OPENSSL_sparcv9cap"))) {
106 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
110 if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
111 if (strcmp(si, "sun4v"))
112 /* FPU is preferred for all CPUs, but US-T1/2 */
113 OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
116 if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
117 if (strstr(si, "+vis"))
118 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
119 if (strstr(si, "+vis2")) {
120 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
121 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
127 * Solaris libdevinfo.so.1 is effectively incomatible with
128 * libmalloc.so.1. Specifically, if application is linked with
129 * -lmalloc, it crashes upon startup with SIGSEGV in
130 * free(3LIBMALLOC) called by di_fini. Prior call to
131 * mallopt(M_KEEP,0) somehow helps... But not always...
133 if ((h = dlopen(NULL, RTLD_LAZY))) {
138 if ((sym.p = dlsym(h, "mallopt")))
139 (*sym.f) (M_KEEP, 0);
143 if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
147 di_walk_node_t di_walk_node;
148 di_node_name_t di_node_name;
151 if (!DLLINK(h, di_init))
153 if (!DLLINK(h, di_fini))
155 if (!DLLINK(h, di_walk_node))
157 if (!DLLINK(h, di_node_name))
160 if ((root_node = (*di_init) ("/", DINFOSUBTREE)) != DI_NODE_NIL) {
161 (*di_walk_node) (root_node, DI_WALK_SIBFIRST,
162 di_node_name, walk_nodename);
163 (*di_fini) (root_node);
173 static sigjmp_buf common_jmp;
174 static void common_handler(int sig)
176 siglongjmp(common_jmp, sig);
179 void OPENSSL_cpuid_setup(void)
182 struct sigaction common_act, ill_oact, bus_oact;
183 sigset_t all_masked, oset;
184 static int trigger = 0;
190 if ((e = getenv("OPENSSL_sparcv9cap"))) {
191 OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
195 /* Initial value, fits UltraSPARC-I&II... */
196 OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
198 sigfillset(&all_masked);
199 sigdelset(&all_masked, SIGILL);
200 sigdelset(&all_masked, SIGTRAP);
202 sigdelset(&all_masked, SIGEMT);
204 sigdelset(&all_masked, SIGFPE);
205 sigdelset(&all_masked, SIGBUS);
206 sigdelset(&all_masked, SIGSEGV);
207 sigprocmask(SIG_SETMASK, &all_masked, &oset);
209 memset(&common_act, 0, sizeof(common_act));
210 common_act.sa_handler = common_handler;
211 common_act.sa_mask = all_masked;
213 sigaction(SIGILL, &common_act, &ill_oact);
214 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
217 if (sigsetjmp(common_jmp, 1) == 0) {
219 OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
222 if (sigsetjmp(common_jmp, 1) == 0) {
223 _sparcv9_vis1_probe();
224 OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
225 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
226 if (_sparcv9_vis1_instrument() >= 12)
227 OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
229 _sparcv9_vis2_probe();
230 OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
234 if (sigsetjmp(common_jmp, 1) == 0) {
235 _sparcv9_fmadd_probe();
236 OPENSSL_sparcv9cap_P |= SPARCV9_FMADD;
239 sigaction(SIGBUS, &bus_oact, NULL);
240 sigaction(SIGILL, &ill_oact, NULL);
242 sigprocmask(SIG_SETMASK, &oset, NULL);