sparcv9cap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. typedef di_node_t (*di_init_t)(const char *,uint_t);
  38. typedef void (*di_fini_t)(di_node_t);
  39. typedef char * (*di_node_name_t)(di_node_t);
  40. typedef int (*di_walk_node_t)(di_node_t,uint_t,di_node_name_t,int (*)(di_node_t,di_node_name_t));
  41. #define DLLINK(h,name) (name=(name##_t)dlsym((h),#name))
  42. static int walk_nodename(di_node_t node, di_node_name_t di_node_name)
  43. {
  44. char *name = (*di_node_name)(node);
  45. /* This is expected to catch all UltraSPARC flavors prior T1 */
  46. if (!strcmp (name,"SUNW,UltraSPARC") ||
  47. !strncmp(name,"SUNW,UltraSPARC-I",17)) /* covers II,III,IV */
  48. {
  49. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
  50. /* %tick is privileged only on UltraSPARC-I/II, but not IIe */
  51. if (name[14]!='\0' && name[17]!='\0' && name[18]!='\0')
  52. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  53. return DI_WALK_TERMINATE;
  54. }
  55. /* This is expected to catch remaining UltraSPARCs, such as T1 */
  56. else if (!strncmp(name,"SUNW,UltraSPARC",15))
  57. {
  58. OPENSSL_sparcv9cap_P &= ~SPARCV9_TICK_PRIVILEGED;
  59. return DI_WALK_TERMINATE;
  60. }
  61. return DI_WALK_CONTINUE;
  62. }
  63. void OPENSSL_cpuid_setup(void)
  64. {
  65. void *h;
  66. char *e;
  67. static int trigger=0;
  68. if (trigger) return;
  69. trigger=1;
  70. if ((e=getenv("OPENSSL_sparcv9cap")))
  71. {
  72. OPENSSL_sparcv9cap_P=strtoul(e,NULL,0);
  73. return;
  74. }
  75. if ((h = dlopen("libdevinfo.so.1",RTLD_LAZY))) do
  76. {
  77. di_init_t di_init;
  78. di_fini_t di_fini;
  79. di_walk_node_t di_walk_node;
  80. di_node_name_t di_node_name;
  81. di_node_t root_node;
  82. if (!DLLINK(h,di_init)) break;
  83. if (!DLLINK(h,di_fini)) break;
  84. if (!DLLINK(h,di_walk_node)) break;
  85. if (!DLLINK(h,di_node_name)) break;
  86. if ((root_node = (*di_init)("/",DINFOSUBTREE))!=DI_NODE_NIL)
  87. {
  88. (*di_walk_node)(root_node,DI_WALK_SIBFIRST,
  89. di_node_name,walk_nodename);
  90. (*di_fini)(root_node);
  91. }
  92. } while(0);
  93. if (h) dlclose(h);
  94. }
  95. #else
  96. void OPENSSL_cpuid_setup(void)
  97. {
  98. char *e;
  99. if ((e=getenv("OPENSSL_sparcv9cap")))
  100. {
  101. OPENSSL_sparcv9cap_P=strtoul(env,NULL,0);
  102. return;
  103. }
  104. /* For now we assume that the rest supports UltraSPARC-I* only */
  105. OPENSSL_sparcv9cap_P |= SPARCV9_PREFER_FPU|SPARCV9_VIS1;
  106. }
  107. #endif