ppccap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #if defined(__linux) || defined(_AIX)
  8. # include <sys/utsname.h>
  9. #endif
  10. #if defined(_AIX53) /* defined even on post-5.3 */
  11. # include <sys/systemcfg.h>
  12. # if !defined(__power_set)
  13. # define __power_set(a) (_system_configuration.implementation & (a))
  14. # endif
  15. #endif
  16. #include <crypto.h>
  17. #include <openssl/bn.h>
  18. #define PPC_FPU64 (1<<0)
  19. #define PPC_ALTIVEC (1<<1)
  20. #define PPC_CRYPTO207 (1<<2)
  21. int OPENSSL_ppccap_P = 0;
  22. static sigset_t all_masked;
  23. #ifdef OPENSSL_BN_ASM_MONT
  24. 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)
  25. {
  26. int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
  27. 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);
  28. if (sizeof(size_t)==4)
  29. {
  30. #if (defined(__APPLE__) && defined(__MACH__))
  31. if (num>=8 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64))
  32. return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
  33. #else
  34. /* boundary of 32 was experimentally determined on
  35. Linux 2.6.22, might have to be adjusted on AIX... */
  36. if (num>=32 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64))
  37. {
  38. sigset_t oset;
  39. int ret;
  40. sigprocmask(SIG_SETMASK,&all_masked,&oset);
  41. ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
  42. sigprocmask(SIG_SETMASK,&oset,NULL);
  43. return ret;
  44. }
  45. #endif
  46. }
  47. else if ((OPENSSL_ppccap_P&PPC_FPU64))
  48. /* this is a "must" on POWER6, but run-time detection
  49. * is not implemented yet... */
  50. return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
  51. return bn_mul_mont_int(rp,ap,bp,np,n0,num);
  52. }
  53. #endif
  54. void sha256_block_p8(void *ctx, const void *inp, size_t len);
  55. void sha256_block_ppc(void *ctx, const void *inp, size_t len);
  56. void sha256_block_data_order(void *ctx, const void *inp, size_t len)
  57. {
  58. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
  59. sha256_block_ppc(ctx, inp, len);
  60. }
  61. void sha512_block_p8(void *ctx, const void *inp, size_t len);
  62. void sha512_block_ppc(void *ctx, const void *inp, size_t len);
  63. void sha512_block_data_order(void *ctx, const void *inp, size_t len)
  64. {
  65. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
  66. sha512_block_ppc(ctx, inp, len);
  67. }
  68. static sigjmp_buf ill_jmp;
  69. static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
  70. void OPENSSL_ppc64_probe(void);
  71. void OPENSSL_altivec_probe(void);
  72. void OPENSSL_crypto207_probe(void);
  73. void OPENSSL_cpuid_setup(void)
  74. {
  75. char *e;
  76. struct sigaction ill_oact,ill_act;
  77. sigset_t oset;
  78. static int trigger=0;
  79. if (trigger) return;
  80. trigger=1;
  81. sigfillset(&all_masked);
  82. sigdelset(&all_masked,SIGILL);
  83. sigdelset(&all_masked,SIGTRAP);
  84. #ifdef SIGEMT
  85. sigdelset(&all_masked,SIGEMT);
  86. #endif
  87. sigdelset(&all_masked,SIGFPE);
  88. sigdelset(&all_masked,SIGBUS);
  89. sigdelset(&all_masked,SIGSEGV);
  90. if ((e=getenv("OPENSSL_ppccap")))
  91. {
  92. OPENSSL_ppccap_P=strtoul(e,NULL,0);
  93. return;
  94. }
  95. OPENSSL_ppccap_P = 0;
  96. #if defined(_AIX)
  97. if (sizeof(size_t) == 4) {
  98. struct utsname uts;
  99. # if defined(_SC_AIX_KERNEL_BITMODE)
  100. if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
  101. return;
  102. # endif
  103. if (uname(&uts) != 0 || atoi(uts.version) < 6)
  104. return;
  105. }
  106. # if defined(__power_set)
  107. /*
  108. * Value used in __power_set is a single-bit 1<<n one denoting
  109. * specific processor class. Incidentally 0xffffffff<<n can be
  110. * used to denote specific processor and its successors.
  111. */
  112. if (sizeof(size_t) == 4) {
  113. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  114. if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
  115. OPENSSL_ppccap_P |= PPC_FPU64;
  116. } else {
  117. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  118. # if 0 /* to keep compatibility with previous validations */
  119. if (__power_set(0x1U<<14)) /* POWER6 */
  120. OPENSSL_ppccap_P |= PPC_FPU64;
  121. # endif
  122. }
  123. if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
  124. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  125. if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
  126. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  127. return;
  128. # endif
  129. #endif
  130. memset(&ill_act,0,sizeof(ill_act));
  131. ill_act.sa_handler = ill_handler;
  132. ill_act.sa_mask = all_masked;
  133. sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset);
  134. sigaction(SIGILL,&ill_act,&ill_oact);
  135. if (sizeof(size_t)==4)
  136. {
  137. if (sigsetjmp(ill_jmp,1) == 0)
  138. {
  139. OPENSSL_ppc64_probe();
  140. OPENSSL_ppccap_P |= PPC_FPU64;
  141. }
  142. }
  143. else
  144. {
  145. /*
  146. * Wanted code detecting POWER6 CPU and setting PPC_FPU64
  147. */
  148. }
  149. if (sigsetjmp(ill_jmp,1) == 0)
  150. {
  151. OPENSSL_altivec_probe();
  152. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  153. if (sigsetjmp(ill_jmp, 1) == 0)
  154. {
  155. OPENSSL_crypto207_probe();
  156. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  157. }
  158. }
  159. sigaction (SIGILL,&ill_oact,NULL);
  160. sigprocmask(SIG_SETMASK,&oset,NULL);
  161. }