2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
17 #include <openssl/bn.h>
19 #include "sparc_arch.h"
21 #if defined(__GNUC__) && defined(__linux)
22 __attribute__ ((visibility("hidden")))
24 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
26 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
27 const BN_ULONG *np, const BN_ULONG *n0, int num)
29 int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
30 const BN_ULONG *np, const BN_ULONG *n0, int num);
31 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
32 const BN_ULONG *np, const BN_ULONG *n0, int num);
33 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
34 const BN_ULONG *np, const BN_ULONG *n0, int num);
36 if (!(num & 1) && num >= 6) {
37 if ((num & 15) == 0 && num <= 64 &&
38 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
39 (CFR_MONTMUL | CFR_MONTSQR)) {
40 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
44 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
45 const BN_ULONG *bp, const BN_ULONG *np,
47 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
48 const BN_ULONG *bp, const BN_ULONG *np,
50 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
51 const BN_ULONG *bp, const BN_ULONG *np,
53 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
54 const BN_ULONG *bp, const BN_ULONG *np,
56 static const bn_mul_mont_f funcs[4] = {
57 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
58 bn_mul_mont_t4_24, bn_mul_mont_t4_32
60 bn_mul_mont_f worker = funcs[num / 16 - 1];
62 if ((*worker) (rp, ap, bp, np, n0))
64 /* retry once and fall back */
65 if ((*worker) (rp, ap, bp, np, n0))
67 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
69 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
70 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
73 * bn_mul_mont_fpu doesn't use FMADD, we just use the
74 * flag to detect when FPU path is preferable in cases
75 * when current heuristics is unreliable. [it works
76 * out because FMADD-capable processors where FPU
77 * code path is undesirable are also VIS3-capable and
78 * VIS3 code path takes precedence.]
80 ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
81 (OPENSSL_sparcv9cap_P[0] &
82 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
83 (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
84 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
86 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
89 unsigned long _sparcv9_rdtick(void);
90 void _sparcv9_vis1_probe(void);
91 unsigned long _sparcv9_vis1_instrument(void);
92 void _sparcv9_vis2_probe(void);
93 void _sparcv9_fmadd_probe(void);
94 unsigned long _sparcv9_rdcfr(void);
95 void _sparcv9_vis3_probe(void);
96 unsigned long _sparcv9_random(void);
97 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
98 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
100 unsigned long OPENSSL_rdtsc(void)
102 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
103 #if defined(__sun) && defined(__SVR4)
109 return _sparcv9_rdtick();
112 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
114 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
116 return _sparcv9_vis1_instrument_bus(out, cnt);
121 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
123 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
125 return _sparcv9_vis1_instrument_bus2(out, cnt, max);
130 static sigjmp_buf common_jmp;
131 static void common_handler(int sig)
133 siglongjmp(common_jmp, sig);
136 #if defined(__sun) && defined(__SVR4)
137 # if defined(__GNUC__) && __GNUC__>=2
138 extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
139 # elif defined(__SUNPRO_C)
141 extern unsigned int getisax(unsigned int vec[], unsigned int sz);
143 static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
147 void OPENSSL_cpuid_setup(void)
150 struct sigaction common_act, ill_oact, bus_oact;
151 sigset_t all_masked, oset;
152 static int trigger = 0;
158 if ((e = getenv("OPENSSL_sparcv9cap"))) {
159 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
160 if ((e = strchr(e, ':')))
161 OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
165 #if defined(__sun) && defined(__SVR4)
166 if (getisax != NULL) {
167 unsigned int vec[2] = { 0, 0 };
169 if (getisax (vec,2)) {
170 if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
171 if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
172 if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
173 if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
174 if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
175 if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
176 if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
177 if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
178 if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
179 if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
181 /* reconstruct %cfr copy */
182 OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
183 OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
184 if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
185 if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
186 if (vec[1]&0x00000040)
187 OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
189 /* Some heuristics */
190 /* all known VIS2-capable CPUs have unprivileged tick counter */
191 if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
192 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
194 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
196 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
197 if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
198 _sparcv9_vis1_instrument() >= 12)
199 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
202 if (sizeof(size_t) == 8)
203 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
209 /* Initial value, fits UltraSPARC-I&II... */
210 OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
212 sigfillset(&all_masked);
213 sigdelset(&all_masked, SIGILL);
214 sigdelset(&all_masked, SIGTRAP);
216 sigdelset(&all_masked, SIGEMT);
218 sigdelset(&all_masked, SIGFPE);
219 sigdelset(&all_masked, SIGBUS);
220 sigdelset(&all_masked, SIGSEGV);
221 sigprocmask(SIG_SETMASK, &all_masked, &oset);
223 memset(&common_act, 0, sizeof(common_act));
224 common_act.sa_handler = common_handler;
225 common_act.sa_mask = all_masked;
227 sigaction(SIGILL, &common_act, &ill_oact);
228 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
231 if (sigsetjmp(common_jmp, 1) == 0) {
233 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
236 if (sigsetjmp(common_jmp, 1) == 0) {
237 _sparcv9_vis1_probe();
238 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
239 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
240 if (_sparcv9_vis1_instrument() >= 12)
241 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
243 _sparcv9_vis2_probe();
244 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
248 if (sigsetjmp(common_jmp, 1) == 0) {
249 _sparcv9_fmadd_probe();
250 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
254 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
255 * because VIS3 defines even integer instructions.
257 if (sigsetjmp(common_jmp, 1) == 0) {
258 _sparcv9_vis3_probe();
259 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
262 if (sigsetjmp(common_jmp, 1) == 0) {
263 _sparcv9_fjaesx_probe();
264 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
268 * In wait for better solution _sparcv9_rdcfr is masked by
269 * VIS3 flag, because it goes to uninterruptable endless
270 * loop on UltraSPARC II running Solaris. Things might be
271 * different on Linux...
273 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
274 sigsetjmp(common_jmp, 1) == 0) {
275 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
278 sigaction(SIGBUS, &bus_oact, NULL);
279 sigaction(SIGILL, &ill_oact, NULL);
281 sigprocmask(SIG_SETMASK, &oset, NULL);
283 if (sizeof(size_t) == 8)
284 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
287 int ret = syscall(340);
289 if (ret >= 0 && ret & 1)
290 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;