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 ((OPENSSL_sparcv9cap_P & (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
  22. (SPARCV9_PREFER_FPU | SPARCV9_VIS1))
  23. return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
  24. else
  25. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  26. }
  27. unsigned long _sparcv9_rdtick(void);
  28. void _sparcv9_vis1_probe(void);
  29. unsigned long _sparcv9_vis1_instrument(void);
  30. void _sparcv9_vis2_probe(void);
  31. void _sparcv9_fmadd_probe(void);
  32. unsigned long OPENSSL_rdtsc(void)
  33. {
  34. if (OPENSSL_sparcv9cap_P & SPARCV9_TICK_PRIVILEGED)
  35. #if defined(__sun) && defined(__SVR4)
  36. return gethrtime();
  37. #else
  38. return 0;
  39. #endif
  40. else
  41. return _sparcv9_rdtick();
  42. }
  43. #if 0 && defined(__sun) && defined(__SVR4)
  44. /*
  45. * This code path is disabled, because of incompatibility of libdevinfo.so.1
  46. * and libmalloc.so.1 (see below for details)
  47. */
  48. # include <malloc.h>
  49. # include <dlfcn.h>
  50. # include <libdevinfo.h>
  51. # include <sys/systeminfo.h>
  52. typedef di_node_t(*di_init_t) (const char *, uint_t);
  53. typedef void (*di_fini_t) (di_node_t);
  54. typedef char *(*di_node_name_t) (di_node_t);
  55. typedef int (*di_walk_node_t) (di_node_t, uint_t, di_node_name_t,
  56. int (*)(di_node_t, di_node_name_t));
  57. # define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
  58. static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
  59. {
  60. char *name = (*di_node_name) (node);
  61. /* This is expected to catch all UltraSPARC flavors prior T1 */
  62. if (!strcmp(name, "SUNW,UltraSPARC") ||
  63. /* covers II,III,IV */
  64. !strncmp(name, "SUNW,UltraSPARC-I", 17)) {
  65. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU | SPARCV9_VIS1;
  66. /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
  67. if (name[14] != '\0' && name[17] != '\0' && name[18] != '\0')
  68. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  69. return DI_WALK_TERMINATE;
  70. }
  71. /* This is expected to catch remaining UltraSPARCs, such as T1 */
  72. else if (!strncmp(name, "SUNW,UltraSPARC", 15)) {
  73. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  74. return DI_WALK_TERMINATE;
  75. }
  76. return DI_WALK_CONTINUE;
  77. }
  78. void OPENSSL_cpuid_setup(void)
  79. {
  80. void *h;
  81. char *e, si[256];
  82. static int trigger = 0;
  83. if (trigger)
  84. return;
  85. trigger = 1;
  86. if ((e = getenv("OPENSSL_sparcv9cap"))) {
  87. OPENSSL_sparcv9cap_P = strtoul(e, NULL, 0);
  88. return;
  89. }
  90. if (sysinfo(SI_MACHINE, si, sizeof(si)) > 0) {
  91. if (strcmp(si, "sun4v"))
  92. /* FPU is preferred for all CPUs, but US-T1/2 */
  93. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
  94. }
  95. if (sysinfo(SI_ISALIST, si, sizeof(si)) > 0) {
  96. if (strstr(si, "+vis"))
  97. OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
  98. if (strstr(si, "+vis2")) {
  99. OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
  100. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  101. return;
  102. }
  103. }
  104. # ifdef M_KEEP
  105. /*
  106. * Solaris libdevinfo.so.1 is effectively incomatible with
  107. * libmalloc.so.1. Specifically, if application is linked with
  108. * -lmalloc, it crashes upon startup with SIGSEGV in
  109. * free(3LIBMALLOC) called by di_fini. Prior call to
  110. * mallopt(M_KEEP,0) somehow helps... But not always...
  111. */
  112. if ((h = dlopen(NULL, RTLD_LAZY))) {
  113. union {
  114. void *p;
  115. int (*f) (int, int);
  116. } sym;
  117. if ((sym.p = dlsym(h, "mallopt")))
  118. (*sym.f) (M_KEEP, 0);
  119. dlclose(h);
  120. }
  121. # endif
  122. if ((h = dlopen("libdevinfo.so.1", RTLD_LAZY)))
  123. do {
  124. di_init_t di_init;
  125. di_fini_t di_fini;
  126. di_walk_node_t di_walk_node;
  127. di_node_name_t di_node_name;
  128. di_node_t root_node;
  129. if (!DLLINK(h, di_init))
  130. break;
  131. if (!DLLINK(h, di_fini))
  132. break;
  133. if (!DLLINK(h, di_walk_node))
  134. break;
  135. if (!DLLINK(h, di_node_name))
  136. break;
  137. if ((root_node = (*di_init) ("/", DINFOSUBTREE)) != DI_NODE_NIL) {
  138. (*di_walk_node) (root_node, DI_WALK_SIBFIRST,
  139. di_node_name, walk_nodename);
  140. (*di_fini) (root_node);
  141. }
  142. } while (0);
  143. if (h)
  144. dlclose(h);
  145. }
  146. #else
  147. static sigjmp_buf common_jmp;
  148. static void common_handler(int sig)
  149. {
  150. siglongjmp(common_jmp, sig);
  151. }
  152. void OPENSSL_cpuid_setup(void)
  153. {
  154. char *e;
  155. struct sigaction common_act, ill_oact, bus_oact;
  156. sigset_t all_masked, oset;
  157. int sig;
  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