sparcv9cap.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. void OPENSSL_cpuid_setup(void)
  108. {
  109. char *e;
  110. struct sigaction common_act, ill_oact, bus_oact;
  111. sigset_t all_masked, oset;
  112. static int trigger = 0;
  113. if (trigger)
  114. return;
  115. trigger = 1;
  116. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  117. OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
  118. if ((e = strchr(e, ':')))
  119. OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
  120. return;
  121. }
  122. /* Initial value, fits UltraSPARC-I&II... */
  123. OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  124. sigfillset(&all_masked);
  125. sigdelset(&all_masked, SIGILL);
  126. sigdelset(&all_masked, SIGTRAP);
  127. # ifdef SIGEMT
  128. sigdelset(&all_masked, SIGEMT);
  129. # endif
  130. sigdelset(&all_masked, SIGFPE);
  131. sigdelset(&all_masked, SIGBUS);
  132. sigdelset(&all_masked, SIGSEGV);
  133. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  134. memset(&common_act, 0, sizeof(common_act));
  135. common_act.sa_handler = common_handler;
  136. common_act.sa_mask = all_masked;
  137. sigaction(SIGILL, &common_act, &ill_oact);
  138. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  139. * Linux] */
  140. if (sigsetjmp(common_jmp, 1) == 0) {
  141. _sparcv9_rdtick();
  142. OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
  143. }
  144. if (sigsetjmp(common_jmp, 1) == 0) {
  145. _sparcv9_vis1_probe();
  146. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
  147. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  148. if (_sparcv9_vis1_instrument() >= 12)
  149. OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  150. else {
  151. _sparcv9_vis2_probe();
  152. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
  153. }
  154. }
  155. if (sigsetjmp(common_jmp, 1) == 0) {
  156. _sparcv9_fmadd_probe();
  157. OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
  158. }
  159. /*
  160. * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
  161. * because VIS3 defines even integer instructions.
  162. */
  163. if (sigsetjmp(common_jmp, 1) == 0) {
  164. _sparcv9_vis3_probe();
  165. OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
  166. }
  167. /*
  168. * In wait for better solution _sparcv9_rdcfr is masked by
  169. * VIS3 flag, because it goes to uninterruptable endless
  170. * loop on UltraSPARC II running Solaris. Things might be
  171. * different on Linux...
  172. */
  173. if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
  174. sigsetjmp(common_jmp, 1) == 0) {
  175. OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
  176. }
  177. sigaction(SIGBUS, &bus_oact, NULL);
  178. sigaction(SIGILL, &ill_oact, NULL);
  179. sigprocmask(SIG_SETMASK, &oset, NULL);
  180. if (sizeof(size_t) == 8)
  181. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  182. # ifdef __linux
  183. else {
  184. int ret = syscall(340);
  185. if (ret >= 0 && ret & 1)
  186. OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
  187. }
  188. # endif
  189. }