sparcv9cap.c 8.9 KB

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