2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
18 #include "internal/cryptlib.h"
20 #include "sparc_arch.h"
22 #if defined(__GNUC__) && defined(__linux)
23 __attribute__ ((visibility("hidden")))
25 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
27 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
28 const BN_ULONG *np, const BN_ULONG *n0, int num)
30 int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
31 const BN_ULONG *np, const BN_ULONG *n0, int num);
32 int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
33 const BN_ULONG *np, const BN_ULONG *n0, int num);
34 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
35 const BN_ULONG *np, const BN_ULONG *n0, int num);
37 if (!(num & 1) && num >= 6) {
38 if ((num & 15) == 0 && num <= 64 &&
39 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
40 (CFR_MONTMUL | CFR_MONTSQR)) {
41 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
45 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
46 const BN_ULONG *bp, const BN_ULONG *np,
48 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
49 const BN_ULONG *bp, const BN_ULONG *np,
51 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
52 const BN_ULONG *bp, const BN_ULONG *np,
54 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
55 const BN_ULONG *bp, const BN_ULONG *np,
57 static const bn_mul_mont_f funcs[4] = {
58 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
59 bn_mul_mont_t4_24, bn_mul_mont_t4_32
61 bn_mul_mont_f worker = funcs[num / 16 - 1];
63 if ((*worker) (rp, ap, bp, np, n0))
65 /* retry once and fall back */
66 if ((*worker) (rp, ap, bp, np, n0))
68 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
70 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
71 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
74 * bn_mul_mont_fpu doesn't use FMADD, we just use the
75 * flag to detect when FPU path is preferable in cases
76 * when current heuristics is unreliable. [it works
77 * out because FMADD-capable processors where FPU
78 * code path is undesirable are also VIS3-capable and
79 * VIS3 code path takes precedence.]
81 ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
82 (OPENSSL_sparcv9cap_P[0] &
83 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
84 (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
85 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
87 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
90 unsigned long _sparcv9_rdtick(void);
91 void _sparcv9_vis1_probe(void);
92 unsigned long _sparcv9_vis1_instrument(void);
93 void _sparcv9_vis2_probe(void);
94 void _sparcv9_fmadd_probe(void);
95 unsigned long _sparcv9_rdcfr(void);
96 void _sparcv9_vis3_probe(void);
97 void _sparcv9_fjaesx_probe(void);
98 unsigned long _sparcv9_random(void);
99 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
100 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
102 uint32_t OPENSSL_rdtsc(void)
104 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
105 #if defined(__sun) && defined(__SVR4)
111 return _sparcv9_rdtick();
114 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
116 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
118 return _sparcv9_vis1_instrument_bus(out, cnt);
123 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
125 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
127 return _sparcv9_vis1_instrument_bus2(out, cnt, max);
132 static sigjmp_buf common_jmp;
133 static void common_handler(int sig)
135 siglongjmp(common_jmp, sig);
138 #if defined(__sun) && defined(__SVR4)
139 # if defined(__GNUC__) && __GNUC__>=2
140 extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
141 # elif defined(__SUNPRO_C)
143 extern unsigned int getisax(unsigned int vec[], unsigned int sz);
145 static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
149 void OPENSSL_cpuid_setup(void)
152 struct sigaction common_act, ill_oact, bus_oact;
153 sigset_t all_masked, oset;
154 static int trigger = 0;
160 if ((e = getenv("OPENSSL_sparcv9cap"))) {
161 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
162 if ((e = strchr(e, ':')))
163 OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
167 #if defined(__sun) && defined(__SVR4)
168 if (getisax != NULL) {
169 unsigned int vec[2] = { 0, 0 };
171 if (getisax (vec,2)) {
172 if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
173 if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
174 if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
175 if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
176 if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
177 if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
178 if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
179 if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
180 if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
181 if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
183 /* reconstruct %cfr copy */
184 OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
185 OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
186 if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
187 if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
188 if (vec[1]&0x00000040)
189 OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
191 /* Some heuristics */
192 /* all known VIS2-capable CPUs have unprivileged tick counter */
193 if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
194 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
196 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
198 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
199 if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
200 _sparcv9_vis1_instrument() >= 12)
201 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
204 if (sizeof(size_t) == 8)
205 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
211 /* Initial value, fits UltraSPARC-I&II... */
212 OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
214 sigfillset(&all_masked);
215 sigdelset(&all_masked, SIGILL);
216 sigdelset(&all_masked, SIGTRAP);
218 sigdelset(&all_masked, SIGEMT);
220 sigdelset(&all_masked, SIGFPE);
221 sigdelset(&all_masked, SIGBUS);
222 sigdelset(&all_masked, SIGSEGV);
223 sigprocmask(SIG_SETMASK, &all_masked, &oset);
225 memset(&common_act, 0, sizeof(common_act));
226 common_act.sa_handler = common_handler;
227 common_act.sa_mask = all_masked;
229 sigaction(SIGILL, &common_act, &ill_oact);
230 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
233 if (sigsetjmp(common_jmp, 1) == 0) {
235 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
238 if (sigsetjmp(common_jmp, 1) == 0) {
239 _sparcv9_vis1_probe();
240 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
241 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
242 if (_sparcv9_vis1_instrument() >= 12)
243 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
245 _sparcv9_vis2_probe();
246 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
250 if (sigsetjmp(common_jmp, 1) == 0) {
251 _sparcv9_fmadd_probe();
252 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
256 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
257 * because VIS3 defines even integer instructions.
259 if (sigsetjmp(common_jmp, 1) == 0) {
260 _sparcv9_vis3_probe();
261 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
264 if (sigsetjmp(common_jmp, 1) == 0) {
265 _sparcv9_fjaesx_probe();
266 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
270 * In wait for better solution _sparcv9_rdcfr is masked by
271 * VIS3 flag, because it goes to uninterruptable endless
272 * loop on UltraSPARC II running Solaris. Things might be
273 * different on Linux...
275 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
276 sigsetjmp(common_jmp, 1) == 0) {
277 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
280 sigaction(SIGBUS, &bus_oact, NULL);
281 sigaction(SIGILL, &ill_oact, NULL);
283 sigprocmask(SIG_SETMASK, &oset, NULL);
285 if (sizeof(size_t) == 8)
286 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
289 int ret = syscall(340);
291 if (ret >= 0 && ret & 1)
292 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;