2
0

sparcv9cap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include <openssl/bn.h>
  17. #include "internal/cryptlib.h"
  18. #include "sparc_arch.h"
  19. #if defined(__GNUC__) && defined(__linux)
  20. __attribute__ ((visibility("hidden")))
  21. #endif
  22. unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
  23. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  24. const BN_ULONG *np, const BN_ULONG *n0, int num)
  25. {
  26. int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  27. const BN_ULONG *np, const BN_ULONG *n0, int num);
  28. int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  29. const BN_ULONG *np, const BN_ULONG *n0, int num);
  30. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  31. const BN_ULONG *np, const BN_ULONG *n0, int num);
  32. if (!(num & 1) && num >= 6) {
  33. if ((num & 15) == 0 && num <= 64 &&
  34. (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
  35. (CFR_MONTMUL | CFR_MONTSQR)) {
  36. typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
  37. const BN_ULONG *bp,
  38. const BN_ULONG *np,
  39. const BN_ULONG *n0);
  40. int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
  41. const BN_ULONG *bp, const BN_ULONG *np,
  42. const BN_ULONG *n0);
  43. int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
  44. const BN_ULONG *bp, const BN_ULONG *np,
  45. const BN_ULONG *n0);
  46. int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
  47. const BN_ULONG *bp, const BN_ULONG *np,
  48. const BN_ULONG *n0);
  49. int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
  50. const BN_ULONG *bp, const BN_ULONG *np,
  51. const BN_ULONG *n0);
  52. static const bn_mul_mont_f funcs[4] = {
  53. bn_mul_mont_t4_8, bn_mul_mont_t4_16,
  54. bn_mul_mont_t4_24, bn_mul_mont_t4_32
  55. };
  56. bn_mul_mont_f worker = funcs[num / 16 - 1];
  57. if ((*worker) (rp, ap, bp, np, n0))
  58. return 1;
  59. /* retry once and fall back */
  60. if ((*worker) (rp, ap, bp, np, n0))
  61. return 1;
  62. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  63. }
  64. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
  65. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  66. else if (num >= 8 &&
  67. /*
  68. * bn_mul_mont_fpu doesn't use FMADD, we just use the
  69. * flag to detect when FPU path is preferable in cases
  70. * when current heuristics is unreliable. [it works
  71. * out because FMADD-capable processors where FPU
  72. * code path is undesirable are also VIS3-capable and
  73. * VIS3 code path takes precedence.]
  74. */
  75. ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
  76. (OPENSSL_sparcv9cap_P[0] &
  77. (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
  78. (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
  79. return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
  80. }
  81. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  82. }
  83. unsigned long _sparcv9_rdtick(void);
  84. void _sparcv9_vis1_probe(void);
  85. unsigned long _sparcv9_vis1_instrument(void);
  86. void _sparcv9_vis2_probe(void);
  87. void _sparcv9_fmadd_probe(void);
  88. unsigned long _sparcv9_rdcfr(void);
  89. void _sparcv9_vis3_probe(void);
  90. void _sparcv9_fjaesx_probe(void);
  91. unsigned long _sparcv9_random(void);
  92. size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
  93. size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
  94. uint32_t OPENSSL_rdtsc(void)
  95. {
  96. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
  97. #if defined(__sun) && defined(__SVR4)
  98. return gethrtime();
  99. #else
  100. return 0;
  101. #endif
  102. else
  103. return _sparcv9_rdtick();
  104. }
  105. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  106. {
  107. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  108. SPARCV9_BLK)
  109. return _sparcv9_vis1_instrument_bus(out, cnt);
  110. else
  111. return 0;
  112. }
  113. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  114. {
  115. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  116. SPARCV9_BLK)
  117. return _sparcv9_vis1_instrument_bus2(out, cnt, max);
  118. else
  119. return 0;
  120. }
  121. static sigjmp_buf common_jmp;
  122. static void common_handler(int sig)
  123. {
  124. siglongjmp(common_jmp, sig);
  125. }
  126. #if defined(__sun) && defined(__SVR4)
  127. # if defined(__GNUC__) && __GNUC__>=2
  128. extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
  129. # elif defined(__SUNPRO_C)
  130. #pragma weak getisax
  131. extern unsigned int getisax(unsigned int vec[], unsigned int sz);
  132. # else
  133. static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
  134. # endif
  135. #endif
  136. void OPENSSL_cpuid_setup(void)
  137. {
  138. char *e;
  139. struct sigaction common_act, ill_oact, bus_oact;
  140. sigset_t all_masked, oset;
  141. static int trigger = 0;
  142. if (trigger)
  143. return;
  144. trigger = 1;
  145. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  146. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  147. if ((e = strchr(e, ':')))
  148. OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
  149. return;
  150. }
  151. #if defined(__sun) && defined(__SVR4)
  152. if (getisax != NULL) {
  153. unsigned int vec[2] = { 0, 0 };
  154. if (getisax (vec,2)) {
  155. if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
  156. if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  157. if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
  158. if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  159. if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  160. if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
  161. if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
  162. if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
  163. if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  164. if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
  165. /* reconstruct %cfr copy */
  166. OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
  167. OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
  168. if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
  169. if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
  170. if (vec[1]&0x00000040)
  171. OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
  172. /* Some heuristics */
  173. /* all known VIS2-capable CPUs have unprivileged tick counter */
  174. if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
  175. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  176. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
  177. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  178. if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
  179. _sparcv9_vis1_instrument() >= 12)
  180. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  181. }
  182. if (sizeof(size_t) == 8)
  183. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  184. return;
  185. }
  186. #endif
  187. /* Initial value, fits UltraSPARC-I&II... */
  188. OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  189. sigfillset(&all_masked);
  190. sigdelset(&all_masked, SIGILL);
  191. sigdelset(&all_masked, SIGTRAP);
  192. # ifdef SIGEMT
  193. sigdelset(&all_masked, SIGEMT);
  194. # endif
  195. sigdelset(&all_masked, SIGFPE);
  196. sigdelset(&all_masked, SIGBUS);
  197. sigdelset(&all_masked, SIGSEGV);
  198. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  199. memset(&common_act, 0, sizeof(common_act));
  200. common_act.sa_handler = common_handler;
  201. common_act.sa_mask = all_masked;
  202. sigaction(SIGILL, &common_act, &ill_oact);
  203. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  204. * Linux] */
  205. if (sigsetjmp(common_jmp, 1) == 0) {
  206. _sparcv9_rdtick();
  207. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  208. }
  209. if (sigsetjmp(common_jmp, 1) == 0) {
  210. _sparcv9_vis1_probe();
  211. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  212. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  213. if (_sparcv9_vis1_instrument() >= 12)
  214. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  215. else {
  216. _sparcv9_vis2_probe();
  217. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  218. }
  219. }
  220. if (sigsetjmp(common_jmp, 1) == 0) {
  221. _sparcv9_fmadd_probe();
  222. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  223. }
  224. /*
  225. * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
  226. * because VIS3 defines even integer instructions.
  227. */
  228. if (sigsetjmp(common_jmp, 1) == 0) {
  229. _sparcv9_vis3_probe();
  230. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  231. }
  232. if (sigsetjmp(common_jmp, 1) == 0) {
  233. _sparcv9_fjaesx_probe();
  234. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  235. }
  236. /*
  237. * In wait for better solution _sparcv9_rdcfr is masked by
  238. * VIS3 flag, because it goes to uninterruptable endless
  239. * loop on UltraSPARC II running Solaris. Things might be
  240. * different on Linux...
  241. */
  242. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
  243. sigsetjmp(common_jmp, 1) == 0) {
  244. OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
  245. }
  246. sigaction(SIGBUS, &bus_oact, NULL);
  247. sigaction(SIGILL, &ill_oact, NULL);
  248. sigprocmask(SIG_SETMASK, &oset, NULL);
  249. if (sizeof(size_t) == 8)
  250. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  251. # ifdef __linux
  252. else {
  253. int ret = syscall(340);
  254. if (ret >= 0 && ret & 1)
  255. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  256. }
  257. # endif
  258. }