2 * Copyright 2005-2021 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"
19 #include "bn/bn_local.h" /* for definition of bn_mul_mont */
21 #include "sparc_arch.h"
23 #if defined(__GNUC__) && defined(__linux)
24 __attribute__ ((visibility("hidden")))
26 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
28 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
29 const BN_ULONG *np, const BN_ULONG *n0, int num)
31 int bn_mul_mont_vis3(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_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
34 const BN_ULONG *np, const BN_ULONG *n0, int num);
35 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
36 const BN_ULONG *np, const BN_ULONG *n0, int num);
38 if (!(num & 1) && num >= 6) {
39 if ((num & 15) == 0 && num <= 64 &&
40 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
41 (CFR_MONTMUL | CFR_MONTSQR)) {
42 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
46 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
47 const BN_ULONG *bp, const BN_ULONG *np,
49 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
50 const BN_ULONG *bp, const BN_ULONG *np,
52 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
53 const BN_ULONG *bp, const BN_ULONG *np,
55 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
56 const BN_ULONG *bp, const BN_ULONG *np,
58 static const bn_mul_mont_f funcs[4] = {
59 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
60 bn_mul_mont_t4_24, bn_mul_mont_t4_32
62 bn_mul_mont_f worker = funcs[num / 16 - 1];
64 if ((*worker) (rp, ap, bp, np, n0))
66 /* retry once and fall back */
67 if ((*worker) (rp, ap, bp, np, n0))
69 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
71 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
72 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
75 * bn_mul_mont_fpu doesn't use FMADD, we just use the
76 * flag to detect when FPU path is preferable in cases
77 * when current heuristics is unreliable. [it works
78 * out because FMADD-capable processors where FPU
79 * code path is undesirable are also VIS3-capable and
80 * VIS3 code path takes precedence.]
82 ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
83 (OPENSSL_sparcv9cap_P[0] &
84 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
85 (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
86 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
88 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
91 unsigned long _sparcv9_rdtick(void);
92 void _sparcv9_vis1_probe(void);
93 unsigned long _sparcv9_vis1_instrument(void);
94 void _sparcv9_vis2_probe(void);
95 void _sparcv9_fmadd_probe(void);
96 unsigned long _sparcv9_rdcfr(void);
97 void _sparcv9_vis3_probe(void);
98 void _sparcv9_fjaesx_probe(void);
99 unsigned long _sparcv9_random(void);
100 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
101 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
103 uint32_t OPENSSL_rdtsc(void)
105 if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
106 #if defined(__sun) && defined(__SVR4)
112 return _sparcv9_rdtick();
115 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
117 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
119 return _sparcv9_vis1_instrument_bus(out, cnt);
124 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
126 if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
128 return _sparcv9_vis1_instrument_bus2(out, cnt, max);
133 static sigjmp_buf common_jmp;
134 static void common_handler(int sig)
136 siglongjmp(common_jmp, sig);
139 #if defined(__sun) && defined(__SVR4)
140 # if defined(__GNUC__) && __GNUC__>=2
141 extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
142 # elif defined(__SUNPRO_C)
144 extern unsigned int getisax(unsigned int vec[], unsigned int sz);
146 static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
150 void OPENSSL_cpuid_setup(void)
153 struct sigaction common_act, ill_oact, bus_oact;
154 sigset_t all_masked, oset;
155 static int trigger = 0;
161 if ((e = getenv("OPENSSL_sparcv9cap"))) {
162 OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
163 if ((e = strchr(e, ':')))
164 OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
168 #if defined(__sun) && defined(__SVR4)
169 if (getisax != NULL) {
170 unsigned int vec[2] = { 0, 0 };
172 if (getisax (vec,2)) {
173 if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
174 if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
175 if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
176 if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
177 if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
178 if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
179 if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
180 if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
181 if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
182 if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
184 /* reconstruct %cfr copy */
185 OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
186 OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
187 if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
188 if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
189 if (vec[1]&0x00000040)
190 OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
192 /* Some heuristics */
193 /* all known VIS2-capable CPUs have unprivileged tick counter */
194 if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
195 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
197 OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
199 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
200 if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
201 _sparcv9_vis1_instrument() >= 12)
202 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
205 if (sizeof(size_t) == 8)
206 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
212 /* Initial value, fits UltraSPARC-I&II... */
213 OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
215 sigfillset(&all_masked);
216 sigdelset(&all_masked, SIGILL);
217 sigdelset(&all_masked, SIGTRAP);
219 sigdelset(&all_masked, SIGEMT);
221 sigdelset(&all_masked, SIGFPE);
222 sigdelset(&all_masked, SIGBUS);
223 sigdelset(&all_masked, SIGSEGV);
224 sigprocmask(SIG_SETMASK, &all_masked, &oset);
226 memset(&common_act, 0, sizeof(common_act));
227 common_act.sa_handler = common_handler;
228 common_act.sa_mask = all_masked;
230 sigaction(SIGILL, &common_act, &ill_oact);
231 sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
234 if (sigsetjmp(common_jmp, 1) == 0) {
236 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
239 if (sigsetjmp(common_jmp, 1) == 0) {
240 _sparcv9_vis1_probe();
241 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
242 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
243 if (_sparcv9_vis1_instrument() >= 12)
244 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
246 _sparcv9_vis2_probe();
247 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
251 if (sigsetjmp(common_jmp, 1) == 0) {
252 _sparcv9_fmadd_probe();
253 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
257 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
258 * because VIS3 defines even integer instructions.
260 if (sigsetjmp(common_jmp, 1) == 0) {
261 _sparcv9_vis3_probe();
262 OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
265 if (sigsetjmp(common_jmp, 1) == 0) {
266 _sparcv9_fjaesx_probe();
267 OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
271 * In wait for better solution _sparcv9_rdcfr is masked by
272 * VIS3 flag, because it goes to uninterruptible endless
273 * loop on UltraSPARC II running Solaris. Things might be
274 * different on Linux...
276 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
277 sigsetjmp(common_jmp, 1) == 0) {
278 OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
281 sigaction(SIGBUS, &bus_oact, NULL);
282 sigaction(SIGILL, &ill_oact, NULL);
284 sigprocmask(SIG_SETMASK, &oset, NULL);
286 if (sizeof(size_t) == 8)
287 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
290 int ret = syscall(340);
292 if (ret >= 0 && ret & 1)
293 OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;