sparcv9cap.c 6.7 KB

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