sparcv9cap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. /*
  24. * TODO(3.0): Temporarily disabled some assembler that hasn't been brought into
  25. * the FIPS module yet.
  26. */
  27. #ifndef FIPS_MODE
  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)
  30. {
  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);
  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,
  42. const BN_ULONG *bp,
  43. const BN_ULONG *np,
  44. const BN_ULONG *n0);
  45. int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
  46. const BN_ULONG *bp, const BN_ULONG *np,
  47. const BN_ULONG *n0);
  48. int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
  49. const BN_ULONG *bp, const BN_ULONG *np,
  50. const BN_ULONG *n0);
  51. int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
  52. const BN_ULONG *bp, const BN_ULONG *np,
  53. const BN_ULONG *n0);
  54. int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
  55. const BN_ULONG *bp, const BN_ULONG *np,
  56. const BN_ULONG *n0);
  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
  60. };
  61. bn_mul_mont_f worker = funcs[num / 16 - 1];
  62. if ((*worker) (rp, ap, bp, np, n0))
  63. return 1;
  64. /* retry once and fall back */
  65. if ((*worker) (rp, ap, bp, np, n0))
  66. return 1;
  67. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  68. }
  69. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
  70. return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
  71. else if (num >= 8 &&
  72. /*
  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.]
  79. */
  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);
  85. }
  86. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  87. }
  88. #endif /* FIPS_MODE */
  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. void _sparcv9_fjaesx_probe(void);
  97. unsigned long _sparcv9_random(void);
  98. size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
  99. size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
  100. uint32_t OPENSSL_rdtsc(void)
  101. {
  102. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
  103. #if defined(__sun) && defined(__SVR4)
  104. return gethrtime();
  105. #else
  106. return 0;
  107. #endif
  108. else
  109. return _sparcv9_rdtick();
  110. }
  111. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  112. {
  113. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  114. SPARCV9_BLK)
  115. return _sparcv9_vis1_instrument_bus(out, cnt);
  116. else
  117. return 0;
  118. }
  119. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  120. {
  121. if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
  122. SPARCV9_BLK)
  123. return _sparcv9_vis1_instrument_bus2(out, cnt, max);
  124. else
  125. return 0;
  126. }
  127. static sigjmp_buf common_jmp;
  128. static void common_handler(int sig)
  129. {
  130. siglongjmp(common_jmp, sig);
  131. }
  132. #if defined(__sun) && defined(__SVR4)
  133. # if defined(__GNUC__) && __GNUC__>=2
  134. extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
  135. # elif defined(__SUNPRO_C)
  136. #pragma weak getisax
  137. extern unsigned int getisax(unsigned int vec[], unsigned int sz);
  138. # else
  139. static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
  140. # endif
  141. #endif
  142. void OPENSSL_cpuid_setup(void)
  143. {
  144. char *e;
  145. struct sigaction common_act, ill_oact, bus_oact;
  146. sigset_t all_masked, oset;
  147. static int trigger = 0;
  148. if (trigger)
  149. return;
  150. trigger = 1;
  151. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  152. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  153. if ((e = strchr(e, ':')))
  154. OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
  155. return;
  156. }
  157. #if defined(__sun) && defined(__SVR4)
  158. if (getisax != NULL) {
  159. unsigned int vec[2] = { 0, 0 };
  160. if (getisax (vec,2)) {
  161. if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
  162. if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  163. if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
  164. if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  165. if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  166. if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
  167. if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
  168. if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
  169. if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  170. if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
  171. /* reconstruct %cfr copy */
  172. OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
  173. OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
  174. if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
  175. if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
  176. if (vec[1]&0x00000040)
  177. OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
  178. /* Some heuristics */
  179. /* all known VIS2-capable CPUs have unprivileged tick counter */
  180. if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
  181. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  182. OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
  183. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  184. if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
  185. _sparcv9_vis1_instrument() >= 12)
  186. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  187. }
  188. if (sizeof(size_t) == 8)
  189. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  190. return;
  191. }
  192. #endif
  193. /* Initial value, fits UltraSPARC-I&II... */
  194. OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  195. sigfillset(&all_masked);
  196. sigdelset(&all_masked, SIGILL);
  197. sigdelset(&all_masked, SIGTRAP);
  198. # ifdef SIGEMT
  199. sigdelset(&all_masked, SIGEMT);
  200. # endif
  201. sigdelset(&all_masked, SIGFPE);
  202. sigdelset(&all_masked, SIGBUS);
  203. sigdelset(&all_masked, SIGSEGV);
  204. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  205. memset(&common_act, 0, sizeof(common_act));
  206. common_act.sa_handler = common_handler;
  207. common_act.sa_mask = all_masked;
  208. sigaction(SIGILL, &common_act, &ill_oact);
  209. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  210. * Linux] */
  211. if (sigsetjmp(common_jmp, 1) == 0) {
  212. _sparcv9_rdtick();
  213. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  214. }
  215. if (sigsetjmp(common_jmp, 1) == 0) {
  216. _sparcv9_vis1_probe();
  217. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  218. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  219. if (_sparcv9_vis1_instrument() >= 12)
  220. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  221. else {
  222. _sparcv9_vis2_probe();
  223. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  224. }
  225. }
  226. if (sigsetjmp(common_jmp, 1) == 0) {
  227. _sparcv9_fmadd_probe();
  228. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  229. }
  230. /*
  231. * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
  232. * because VIS3 defines even integer instructions.
  233. */
  234. if (sigsetjmp(common_jmp, 1) == 0) {
  235. _sparcv9_vis3_probe();
  236. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  237. }
  238. if (sigsetjmp(common_jmp, 1) == 0) {
  239. _sparcv9_fjaesx_probe();
  240. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
  241. }
  242. /*
  243. * In wait for better solution _sparcv9_rdcfr is masked by
  244. * VIS3 flag, because it goes to uninterruptable endless
  245. * loop on UltraSPARC II running Solaris. Things might be
  246. * different on Linux...
  247. */
  248. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
  249. sigsetjmp(common_jmp, 1) == 0) {
  250. OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
  251. }
  252. sigaction(SIGBUS, &bus_oact, NULL);
  253. sigaction(SIGILL, &ill_oact, NULL);
  254. sigprocmask(SIG_SETMASK, &oset, NULL);
  255. if (sizeof(size_t) == 8)
  256. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  257. # ifdef __linux
  258. else {
  259. int ret = syscall(340);
  260. if (ret >= 0 && ret & 1)
  261. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  262. }
  263. # endif
  264. }