8 #include <openssl/bn.h>
10 #include "sparc_arch.h"
12 #if defined(__GNUC__) && defined(__linux)
13 __attribute__ ((visibility("hidden")))
15 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
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_vis3(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_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
23 const BN_ULONG *np, const BN_ULONG *n0, int num);
24 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
25 const BN_ULONG *np, const BN_ULONG *n0, int num);
27 if (!(num & 1) && num >= 6) {
28 if ((num & 15) == 0 && num <= 64 &&
29 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
30 (CFR_MONTMUL | CFR_MONTSQR)) {
31 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
35 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
36 const BN_ULONG *bp, const BN_ULONG *np,
38 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
39 const BN_ULONG *bp, const BN_ULONG *np,
41 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
42 const BN_ULONG *bp, const BN_ULONG *np,
44 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
45 const BN_ULONG *bp, const BN_ULONG *np,
47 static const bn_mul_mont_f funcs[4] = {
48 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
49 bn_mul_mont_t4_24, bn_mul_mont_t4_32
51 bn_mul_mont_f worker = funcs[num / 16 - 1];
53 if ((*worker) (rp, ap, bp, np, n0))
55 /* retry once and fall back */
56 if ((*worker) (rp, ap, bp, np, n0))
58 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
60 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
61 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
63 (OPENSSL_sparcv9cap_P[0] &
64 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
65 (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
66 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
68 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
71 unsigned long _sparcv9_rdtick(void);
72 void _sparcv9_vis1_probe(void);
73 unsigned long _sparcv9_vis1_instrument(void);
74 void _sparcv9_vis2_probe(void);
75 void _sparcv9_fmadd_probe(void);
76 unsigned long _sparcv9_rdcfr(void);
77 void _sparcv9_vis3_probe(void);
78 unsigned long _sparcv9_random(void);
79 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
80 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
82 unsigned long OPENSSL_rdtsc(void)
84 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
85 #if defined(__sun) && defined(__SVR4)
91 return _sparcv9_rdtick();
94 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
96 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
98 return _sparcv9_vis1_instrument_bus(out, cnt);
103 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
105 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
107 return _sparcv9_vis1_instrument_bus2(out, cnt, max);
112 static sigjmp_buf common_jmp;
113 static void common_handler(int sig)
115 siglongjmp(common_jmp, sig);
118 void OPENSSL_cpuid_setup(void)
121 struct sigaction common_act, ill_oact, bus_oact;
122 sigset_t all_masked, oset;
123 static int trigger = 0;
129 if ((e = getenv("OPENSSL_sparcv9cap"))) {
130 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
131 if ((e = strchr(e, ':')))
132 OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
136 /* Initial value, fits UltraSPARC-I&II... */
137 OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
139 sigfillset(&all_masked);
140 sigdelset(&all_masked, SIGILL);
141 sigdelset(&all_masked, SIGTRAP);
143 sigdelset(&all_masked, SIGEMT);
145 sigdelset(&all_masked, SIGFPE);
146 sigdelset(&all_masked, SIGBUS);
147 sigdelset(&all_masked, SIGSEGV);
148 sigprocmask(SIG_SETMASK, &all_masked, &oset);
150 memset(&common_act, 0, sizeof(common_act));
151 common_act.sa_handler = common_handler;
152 common_act.sa_mask = all_masked;
154 sigaction(SIGILL, &common_act, &ill_oact);
155 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
158 if (sigsetjmp(common_jmp, 1) == 0) {
160 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
163 if (sigsetjmp(common_jmp, 1) == 0) {
164 _sparcv9_vis1_probe();
165 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
166 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
167 if (_sparcv9_vis1_instrument() >= 12)
168 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
170 _sparcv9_vis2_probe();
171 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
175 if (sigsetjmp(common_jmp, 1) == 0) {
176 _sparcv9_fmadd_probe();
177 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
181 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
182 * because VIS3 defines even integer instructions.
184 if (sigsetjmp(common_jmp, 1) == 0) {
185 _sparcv9_vis3_probe();
186 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
190 * In wait for better solution _sparcv9_rdcfr is masked by
191 * VIS3 flag, because it goes to uninterruptable endless
192 * loop on UltraSPARC II running Solaris. Things might be
193 * different on Linux...
195 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
196 sigsetjmp(common_jmp, 1) == 0) {
197 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
200 sigaction(SIGBUS, &bus_oact, NULL);
201 sigaction(SIGILL, &ill_oact, NULL);
203 sigprocmask(SIG_SETMASK, &oset, NULL);
205 if (sizeof(size_t) == 8)
206 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
209 int ret = syscall(340);
211 if (ret >= 0 && ret & 1)
212 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;