sparcv9cap.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <openssl/bn.h>
  8. #define SPARCV9_TICK_PRIVILEGED (1<<0)
  9. #define SPARCV9_PREFER_FPU (1<<1)
  10. #define SPARCV9_VIS1 (1<<2)
  11. #define SPARCV9_VIS2 (1<<3) /* reserved */
  12. #define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */
  13. static int OPENSSL_sparcv9cap_P = SPARCV9_TICK_PRIVILEGED;
  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_fpu(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_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  20. const BN_ULONG *np, const BN_ULONG *n0, int num);
  21. if (num >= 8 && !(num & 1) &&
  22. (OPENSSL_sparcv9cap_P & (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
  23. (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
  24. return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
  25. else
  26. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  27. }
  28. unsigned long _sparcv9_rdtick(void);
  29. void _sparcv9_vis1_probe(void);
  30. unsigned long _sparcv9_vis1_instrument(void);
  31. void _sparcv9_vis2_probe(void);
  32. void _sparcv9_fmadd_probe(void);
  33. unsigned long OPENSSL_rdtsc(void)
  34. {
  35. if (OPENSSL_sparcv9cap_P & SPARCV9_TICK_PRIVILEGED)
  36. #if defined(__sun) && defined(__SVR4)
  37. return gethrtime();
  38. #else
  39. return 0;
  40. #endif
  41. else
  42. return _sparcv9_rdtick();
  43. }
  44. #if 0 && defined(__sun) && defined(__SVR4)
  45. /*
  46. * This code path is disabled, because of incompatibility of libdevinfo.so.1
  47. * and libmalloc.so.1 (see below for details)
  48. */
  49. # include <malloc.h>
  50. # include <dlfcn.h>
  51. # include <libdevinfo.h>
  52. # include <sys/systeminfo.h>
  53. typedef di_node_t(*di_init_t) (const char *, uint_t);
  54. typedef void (*di_fini_t) (di_node_t);
  55. typedef char *(*di_node_name_t) (di_node_t);
  56. typedef int (*di_walk_node_t) (di_node_t, uint_t, di_node_name_t,
  57. int (*)(di_node_t, di_node_name_t));
  58. # define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
  59. static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
  60. {
  61. char *name = (*di_node_name) (node);
  62. /* This is expected to catch all UltraSPARC flavors prior T1 */
  63. if (!strcmp(name, "SUNW,UltraSPARC") ||
  64. /* covers II,III,IV */
  65. !strncmp(name, "SUNW,UltraSPARC-I", 17)) {
  66. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU | SPARCV9_VIS1;
  67. /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
  68. if (name[14] != '\0' && name[17] != '\0' && name[18] != '\0')
  69. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  70. return DI_WALK_TERMINATE;
  71. }
  72. /* This is expected to catch remaining UltraSPARCs, such as T1 */
  73. else if (!strncmp(name, "SUNW,UltraSPARC", 15)) {
  74. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  75. return DI_WALK_TERMINATE;
  76. }
  77. return DI_WALK_CONTINUE;
  78. }
  79. void OPENSSL_cpuid_setup(void)
  80. {
  81. void *h;
  82. char *e, si[256];
  83. static int trigger = 0;
  84. if (trigger)
  85. return;
  86. trigger = 1;
  87. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  88. OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
  89. return;
  90. }
  91. if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
  92. if (strcmp(si, "sun4v"))
  93. /* FPU is preferred for all CPUs, but US-T1/2 */
  94. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
  95. }
  96. if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
  97. if (strstr(si, "+vis"))
  98. OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
  99. if (strstr(si, "+vis2")) {
  100. OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
  101. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  102. return;
  103. }
  104. }
  105. # ifdef M_KEEP
  106. /*
  107. * Solaris libdevinfo.so.1 is effectively incomatible with
  108. * libmalloc.so.1. Specifically, if application is linked with
  109. * -lmalloc, it crashes upon startup with SIGSEGV in
  110. * free(3LIBMALLOC) called by di_fini. Prior call to
  111. * mallopt(M_KEEP,0) somehow helps... But not always...
  112. */
  113. if ((h = dlopen(NULL, RTLD_LAZY))) {
  114. union {
  115. void *p;
  116. int (*f) (int, int);
  117. } sym;
  118. if ((sym.p = dlsym(h, "mallopt")))
  119. (*sym.f) (M_KEEP, 0);
  120. dlclose(h);
  121. }
  122. # endif
  123. if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
  124. do {
  125. di_init_t di_init;
  126. di_fini_t di_fini;
  127. di_walk_node_t di_walk_node;
  128. di_node_name_t di_node_name;
  129. di_node_t root_node;
  130. if (!DLLINK(h, di_init))
  131. break;
  132. if (!DLLINK(h, di_fini))
  133. break;
  134. if (!DLLINK(h, di_walk_node))
  135. break;
  136. if (!DLLINK(h, di_node_name))
  137. break;
  138. if ((root_node = (*di_init) ("/", DINFOSUBTREE)) != DI_NODE_NIL) {
  139. (*di_walk_node) (root_node, DI_WALK_SIBFIRST,
  140. di_node_name, walk_nodename);
  141. (*di_fini) (root_node);
  142. }
  143. } while (0);
  144. if (h)
  145. dlclose(h);
  146. }
  147. #else
  148. static sigjmp_buf common_jmp;
  149. static void common_handler(int sig)
  150. {
  151. siglongjmp(common_jmp, sig);
  152. }
  153. void OPENSSL_cpuid_setup(void)
  154. {
  155. char *e;
  156. struct sigaction common_act, ill_oact, bus_oact;
  157. sigset_t all_masked, oset;
  158. static int trigger = 0;
  159. if (trigger)
  160. return;
  161. trigger = 1;
  162. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  163. OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
  164. return;
  165. }
  166. /* Initial value, fits UltraSPARC-I&II... */
  167. OPENSSL_sparcv9cap_P = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
  168. sigfillset(&all_masked);
  169. sigdelset(&all_masked, SIGILL);
  170. sigdelset(&all_masked, SIGTRAP);
  171. # ifdef SIGEMT
  172. sigdelset(&all_masked, SIGEMT);
  173. # endif
  174. sigdelset(&all_masked, SIGFPE);
  175. sigdelset(&all_masked, SIGBUS);
  176. sigdelset(&all_masked, SIGSEGV);
  177. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  178. memset(&common_act, 0, sizeof(common_act));
  179. common_act.sa_handler = common_handler;
  180. common_act.sa_mask = all_masked;
  181. sigaction(SIGILL, &common_act, &ill_oact);
  182. sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
  183. * Linux] */
  184. if (sigsetjmp(common_jmp, 1) == 0) {
  185. _sparcv9_rdtick();
  186. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  187. }
  188. if (sigsetjmp(common_jmp, 1) == 0) {
  189. _sparcv9_vis1_probe();
  190. OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
  191. /* detect UltraSPARC-Tx, see sparccpud.S for details... */
  192. if (_sparcv9_vis1_instrument() >= 12)
  193. OPENSSL_sparcv9cap_P &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
  194. else {
  195. _sparcv9_vis2_probe();
  196. OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
  197. }
  198. }
  199. if (sigsetjmp(common_jmp, 1) == 0) {
  200. _sparcv9_fmadd_probe();
  201. OPENSSL_sparcv9cap_P |= SPARCV9_FMADD;
  202. }
  203. sigaction(SIGBUS, &bus_oact, NULL);
  204. sigaction(SIGILL, &ill_oact, NULL);
  205. sigprocmask(SIG_SETMASK, &oset, NULL);
  206. }
  207. #endif