]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/sparcv9cap.c
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 };
28 * TODO(3.0): Temporarily disabled some assembler that hasn't been brought into
29 * the FIPS module yet.
32 int bn_mul_mont(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
33 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
)
35 int bn_mul_mont_vis3(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
36 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
);
37 int bn_mul_mont_fpu(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
38 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
);
39 int bn_mul_mont_int(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
40 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
);
42 if (!(num
& 1) && num
>= 6) {
43 if ((num
& 15) == 0 && num
<= 64 &&
44 (OPENSSL_sparcv9cap_P
[1] & (CFR_MONTMUL
| CFR_MONTSQR
)) ==
45 (CFR_MONTMUL
| CFR_MONTSQR
)) {
46 typedef int (*bn_mul_mont_f
) (BN_ULONG
*rp
, const BN_ULONG
*ap
,
50 int bn_mul_mont_t4_8(BN_ULONG
*rp
, const BN_ULONG
*ap
,
51 const BN_ULONG
*bp
, const BN_ULONG
*np
,
53 int bn_mul_mont_t4_16(BN_ULONG
*rp
, const BN_ULONG
*ap
,
54 const BN_ULONG
*bp
, const BN_ULONG
*np
,
56 int bn_mul_mont_t4_24(BN_ULONG
*rp
, const BN_ULONG
*ap
,
57 const BN_ULONG
*bp
, const BN_ULONG
*np
,
59 int bn_mul_mont_t4_32(BN_ULONG
*rp
, const BN_ULONG
*ap
,
60 const BN_ULONG
*bp
, const BN_ULONG
*np
,
62 static const bn_mul_mont_f funcs
[4] = {
63 bn_mul_mont_t4_8
, bn_mul_mont_t4_16
,
64 bn_mul_mont_t4_24
, bn_mul_mont_t4_32
66 bn_mul_mont_f worker
= funcs
[num
/ 16 - 1];
68 if ((*worker
) (rp
, ap
, bp
, np
, n0
))
70 /* retry once and fall back */
71 if ((*worker
) (rp
, ap
, bp
, np
, n0
))
73 return bn_mul_mont_vis3(rp
, ap
, bp
, np
, n0
, num
);
75 if ((OPENSSL_sparcv9cap_P
[0] & SPARCV9_VIS3
))
76 return bn_mul_mont_vis3(rp
, ap
, bp
, np
, n0
, num
);
79 * bn_mul_mont_fpu doesn't use FMADD, we just use the
80 * flag to detect when FPU path is preferable in cases
81 * when current heuristics is unreliable. [it works
82 * out because FMADD-capable processors where FPU
83 * code path is undesirable are also VIS3-capable and
84 * VIS3 code path takes precedence.]
86 ( (OPENSSL_sparcv9cap_P
[0] & SPARCV9_FMADD
) ||
87 (OPENSSL_sparcv9cap_P
[0] &
88 (SPARCV9_PREFER_FPU
| SPARCV9_VIS1
)) ==
89 (SPARCV9_PREFER_FPU
| SPARCV9_VIS1
) ))
90 return bn_mul_mont_fpu(rp
, ap
, bp
, np
, n0
, num
);
92 return bn_mul_mont_int(rp
, ap
, bp
, np
, n0
, num
);
94 #endif /* FIPS_MODE */
96 unsigned long _sparcv9_rdtick(void);
97 void _sparcv9_vis1_probe(void);
98 unsigned long _sparcv9_vis1_instrument(void);
99 void _sparcv9_vis2_probe(void);
100 void _sparcv9_fmadd_probe(void);
101 unsigned long _sparcv9_rdcfr(void);
102 void _sparcv9_vis3_probe(void);
103 void _sparcv9_fjaesx_probe(void);
104 unsigned long _sparcv9_random(void);
105 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
106 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
108 uint32_t OPENSSL_rdtsc(void)
110 if (OPENSSL_sparcv9cap_P
[0] & SPARCV9_TICK_PRIVILEGED
)
111 #if defined(__sun) && defined(__SVR4)
117 return _sparcv9_rdtick();
120 size_t OPENSSL_instrument_bus(unsigned int *out
, size_t cnt
)
122 if ((OPENSSL_sparcv9cap_P
[0] & (SPARCV9_TICK_PRIVILEGED
| SPARCV9_BLK
)) ==
124 return _sparcv9_vis1_instrument_bus(out
, cnt
);
129 size_t OPENSSL_instrument_bus2(unsigned int *out
, size_t cnt
, size_t max
)
131 if ((OPENSSL_sparcv9cap_P
[0] & (SPARCV9_TICK_PRIVILEGED
| SPARCV9_BLK
)) ==
133 return _sparcv9_vis1_instrument_bus2(out
, cnt
, max
);
138 static sigjmp_buf common_jmp
;
139 static void common_handler(int sig
)
141 siglongjmp(common_jmp
, sig
);
144 #if defined(__sun) && defined(__SVR4)
145 # if defined(__GNUC__) && __GNUC__>=2
146 extern unsigned int getisax(unsigned int vec
[], unsigned int sz
) __attribute__ ((weak
));
147 # elif defined(__SUNPRO_C)
149 extern unsigned int getisax(unsigned int vec
[], unsigned int sz
);
151 static unsigned int (*getisax
) (unsigned int vec
[], unsigned int sz
) = NULL
;
155 void OPENSSL_cpuid_setup(void)
158 struct sigaction common_act
, ill_oact
, bus_oact
;
159 sigset_t all_masked
, oset
;
160 static int trigger
= 0;
166 if ((e
= getenv("OPENSSL_sparcv9cap"))) {
167 OPENSSL_sparcv9cap_P
[0] = strtoul(e
, NULL
, 0);
168 if ((e
= strchr(e
, ':')))
169 OPENSSL_sparcv9cap_P
[1] = strtoul(e
+ 1, NULL
, 0);
173 #if defined(__sun) && defined(__SVR4)
174 if (getisax
!= NULL
) {
175 unsigned int vec
[2] = { 0, 0 };
177 if (getisax (vec
,2)) {
178 if (vec
[0]&0x00020) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
;
179 if (vec
[0]&0x00040) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
180 if (vec
[0]&0x00080) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_BLK
;
181 if (vec
[0]&0x00100) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FMADD
;
182 if (vec
[0]&0x00400) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS3
;
183 if (vec
[0]&0x01000) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FJHPCACE
;
184 if (vec
[0]&0x02000) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FJDESX
;
185 if (vec
[0]&0x08000) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_IMA
;
186 if (vec
[0]&0x10000) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FJAESX
;
187 if (vec
[1]&0x00008) OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS4
;
189 /* reconstruct %cfr copy */
190 OPENSSL_sparcv9cap_P
[1] = (vec
[0]>>17)&0x3ff;
191 OPENSSL_sparcv9cap_P
[1] |= (OPENSSL_sparcv9cap_P
[1]&CFR_MONTMUL
)<<1;
192 if (vec
[0]&0x20000000) OPENSSL_sparcv9cap_P
[1] |= CFR_CRC32C
;
193 if (vec
[1]&0x00000020) OPENSSL_sparcv9cap_P
[1] |= CFR_XMPMUL
;
194 if (vec
[1]&0x00000040)
195 OPENSSL_sparcv9cap_P
[1] |= CFR_XMONTMUL
|CFR_XMONTSQR
;
197 /* Some heuristics */
198 /* all known VIS2-capable CPUs have unprivileged tick counter */
199 if (OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS2
)
200 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
202 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_PREFER_FPU
;
204 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
205 if ((OPENSSL_sparcv9cap_P
[0]&SPARCV9_VIS1
) &&
206 _sparcv9_vis1_instrument() >= 12)
207 OPENSSL_sparcv9cap_P
[0] &= ~(SPARCV9_VIS1
| SPARCV9_PREFER_FPU
);
210 if (sizeof(size_t) == 8)
211 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_64BIT_STACK
;
217 /* Initial value, fits UltraSPARC-I&II... */
218 OPENSSL_sparcv9cap_P
[0] = SPARCV9_PREFER_FPU
| SPARCV9_TICK_PRIVILEGED
;
220 sigfillset(&all_masked
);
221 sigdelset(&all_masked
, SIGILL
);
222 sigdelset(&all_masked
, SIGTRAP
);
224 sigdelset(&all_masked
, SIGEMT
);
226 sigdelset(&all_masked
, SIGFPE
);
227 sigdelset(&all_masked
, SIGBUS
);
228 sigdelset(&all_masked
, SIGSEGV
);
229 sigprocmask(SIG_SETMASK
, &all_masked
, &oset
);
231 memset(&common_act
, 0, sizeof(common_act
));
232 common_act
.sa_handler
= common_handler
;
233 common_act
.sa_mask
= all_masked
;
235 sigaction(SIGILL
, &common_act
, &ill_oact
);
236 sigaction(SIGBUS
, &common_act
, &bus_oact
); /* T1 fails 16-bit ldda [on
239 if (sigsetjmp(common_jmp
, 1) == 0) {
241 OPENSSL_sparcv9cap_P
[0] &= ~SPARCV9_TICK_PRIVILEGED
;
244 if (sigsetjmp(common_jmp
, 1) == 0) {
245 _sparcv9_vis1_probe();
246 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS1
| SPARCV9_BLK
;
247 /* detect UltraSPARC-Tx, see sparccpud.S for details... */
248 if (_sparcv9_vis1_instrument() >= 12)
249 OPENSSL_sparcv9cap_P
[0] &= ~(SPARCV9_VIS1
| SPARCV9_PREFER_FPU
);
251 _sparcv9_vis2_probe();
252 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS2
;
256 if (sigsetjmp(common_jmp
, 1) == 0) {
257 _sparcv9_fmadd_probe();
258 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FMADD
;
262 * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
263 * because VIS3 defines even integer instructions.
265 if (sigsetjmp(common_jmp
, 1) == 0) {
266 _sparcv9_vis3_probe();
267 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_VIS3
;
270 if (sigsetjmp(common_jmp
, 1) == 0) {
271 _sparcv9_fjaesx_probe();
272 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_FJAESX
;
276 * In wait for better solution _sparcv9_rdcfr is masked by
277 * VIS3 flag, because it goes to uninterruptable endless
278 * loop on UltraSPARC II running Solaris. Things might be
279 * different on Linux...
281 if ((OPENSSL_sparcv9cap_P
[0] & SPARCV9_VIS3
) &&
282 sigsetjmp(common_jmp
, 1) == 0) {
283 OPENSSL_sparcv9cap_P
[1] = (unsigned int)_sparcv9_rdcfr();
286 sigaction(SIGBUS
, &bus_oact
, NULL
);
287 sigaction(SIGILL
, &ill_oact
, NULL
);
289 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
291 if (sizeof(size_t) == 8)
292 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_64BIT_STACK
;
295 int ret
= syscall(340);
297 if (ret
>= 0 && ret
& 1)
298 OPENSSL_sparcv9cap_P
[0] |= SPARCV9_64BIT_STACK
;