sparcv9cap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <openssl/bn.h>
  6. #define SPARCV9_TICK_PRIVILEGED (1<<0)
  7. #define SPARCV9_PREFER_FPU (1<<1)
  8. #define SPARCV9_VIS1 (1<<2)
  9. #define SPARCV9_VIS2 (1<<3) /* reserved */
  10. #define SPARCV9_FMADD (1<<4) /* reserved for SPARC64 V */
  11. static int OPENSSL_sparcv9cap_P=SPARCV9_TICK_PRIVILEGED;
  12. 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)
  13. {
  14. 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);
  15. 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);
  16. if ((OPENSSL_sparcv9cap_P&(SPARCV9_PREFER_FPU|SPARCV9_VIS1)) ==
  17. (SPARCV9_PREFER_FPU|SPARCV9_VIS1))
  18. return bn_mul_mont_fpu(rp,ap,bp,np,n0,num);
  19. else
  20. return bn_mul_mont_int(rp,ap,bp,np,n0,num);
  21. }
  22. unsigned long OPENSSL_rdtsc(void)
  23. {
  24. unsigned long _sparcv9_rdtick(void);
  25. if (OPENSSL_sparcv9cap_P&SPARCV9_TICK_PRIVILEGED)
  26. #if defined(__sun) && defined(__SVR4)
  27. return gethrtime();
  28. #else
  29. return 0;
  30. #endif
  31. else
  32. return _sparcv9_rdtick();
  33. }
  34. #if defined(__sun) && defined(__SVR4)
  35. #include <dlfcn.h>
  36. #include <libdevinfo.h>
  37. #include <sys/systeminfo.h>
  38. typedef di_node_t (*di_init_t)(const char *,uint_t);
  39. typedef void (*di_fini_t)(di_node_t);
  40. typedef char * (*di_node_name_t)(di_node_t);
  41. typedef int (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t));
  42. #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
  43. static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
  44. {
  45. char *name = (*di_node_name)(node);
  46. /* This is expected to catch all UltraSPARC flavors prior T1 */
  47. if (!strcmp (name,"SUNW,UltraSPARC") ||
  48. !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
  49. {
  50. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
  51. /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
  52. if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
  53. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  54. return DI_WALK_TERMINATE;
  55. }
  56. /* This is expected to catch remaining UltraSPARCs, such as T1 */
  57. else if (!strncmp(name,"SUNW,UltraSPARC",15))
  58. {
  59. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  60. return DI_WALK_TERMINATE;
  61. }
  62. return DI_WALK_CONTINUE;
  63. }
  64. void OPENSSL_cpuid_setup(void)
  65. {
  66. void *h;
  67. char *e,si[256];
  68. static int trigger=0;
  69. if (trigger) return;
  70. trigger=1;
  71. if ((e=getenv("OPENSSL_sparcv9cap")))
  72. {
  73. OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
  74. return;
  75. }
  76. if (sysinfo(SI_MACHINE,si,sizeof(si))>0)
  77. {
  78. if (strcmp(si,"sun4v"))
  79. /* FPU is preferred for all CPUs, but US-T1/2 */
  80. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU;
  81. }
  82. if (sysinfo(SI_ISALIST,si,sizeof(si))>0)
  83. {
  84. if (strstr(si,"+vis"))
  85. OPENSSL_sparcv9cap_P |= SPARCV9_VIS1;
  86. if (strstr(si,"+vis2"))
  87. {
  88. OPENSSL_sparcv9cap_P |= SPARCV9_VIS2;
  89. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  90. return;
  91. }
  92. }
  93. if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
  94. {
  95. di_init_t di_init;
  96. di_fini_t di_fini;
  97. di_walk_node_t di_walk_node;
  98. di_node_name_t di_node_name;
  99. di_node_t root_node;
  100. if (!DLLINK(h,di_init)) break;
  101. if (!DLLINK(h,di_fini)) break;
  102. if (!DLLINK(h,di_walk_node)) break;
  103. if (!DLLINK(h,di_node_name)) break;
  104. if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
  105. {
  106. (*di_walk_node)(root_node,DI_WALK_SIBFIRST,
  107. di_node_name,walk_nodename);
  108. (*di_fini)(root_node);
  109. }
  110. } while(0);
  111. if (h) dlclose(h);
  112. }
  113. #else
  114. void OPENSSL_cpuid_setup(void)
  115. {
  116. char *e;
  117. if ((e=getenv("OPENSSL_sparcv9cap")))
  118. {
  119. OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
  120. return;
  121. }
  122. /* For now we assume that the rest supports UltraSPARC-I* only */
  123. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
  124. }
  125. #endif