ppccap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <crypto.h>
  8. #include <openssl/bn.h>
  9. #define PPC_FPU64 (1<<0)
  10. #define PPC_ALTIVEC (1<<1)
  11. static int OPENSSL_ppccap_P = 0;
  12. static sigset_t all_masked;
  13. #ifdef OPENSSL_BN_ASM_MONT
  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_fpu64(BN_ULONG *rp, const BN_ULONG *ap,
  18. const BN_ULONG *bp, const BN_ULONG *np,
  19. const BN_ULONG *n0, int num);
  20. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  21. const BN_ULONG *np, const BN_ULONG *n0, int num);
  22. if (sizeof(size_t) == 4) {
  23. # if (defined(__APPLE__) && defined(__MACH__))
  24. if (num >= 8 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64))
  25. return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  26. # else
  27. /*
  28. * boundary of 32 was experimentally determined on Linux 2.6.22,
  29. * might have to be adjusted on AIX...
  30. */
  31. if (num >= 32 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64)) {
  32. sigset_t oset;
  33. int ret;
  34. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  35. ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  36. sigprocmask(SIG_SETMASK, &oset, NULL);
  37. return ret;
  38. }
  39. # endif
  40. } else if ((OPENSSL_ppccap_P & PPC_FPU64))
  41. /*
  42. * this is a "must" on POWER6, but run-time detection is not
  43. * implemented yet...
  44. */
  45. return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  46. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  47. }
  48. #endif
  49. static sigjmp_buf ill_jmp;
  50. static void ill_handler(int sig)
  51. {
  52. siglongjmp(ill_jmp, sig);
  53. }
  54. void OPENSSL_ppc64_probe(void);
  55. void OPENSSL_altivec_probe(void);
  56. void OPENSSL_cpuid_setup(void)
  57. {
  58. char *e;
  59. struct sigaction ill_oact, ill_act;
  60. sigset_t oset;
  61. static int trigger = 0;
  62. if (trigger)
  63. return;
  64. trigger = 1;
  65. sigfillset(&all_masked);
  66. sigdelset(&all_masked, SIGILL);
  67. sigdelset(&all_masked, SIGTRAP);
  68. #ifdef SIGEMT
  69. sigdelset(&all_masked, SIGEMT);
  70. #endif
  71. sigdelset(&all_masked, SIGFPE);
  72. sigdelset(&all_masked, SIGBUS);
  73. sigdelset(&all_masked, SIGSEGV);
  74. if ((e = getenv("OPENSSL_ppccap"))) {
  75. OPENSSL_ppccap_P = strtoul(e, NULL, 0);
  76. return;
  77. }
  78. OPENSSL_ppccap_P = 0;
  79. #if defined(_AIX)
  80. if (sizeof(size_t) == 4
  81. # if defined(_SC_AIX_KERNEL_BITMODE)
  82. && sysconf(_SC_AIX_KERNEL_BITMODE) != 64
  83. # endif
  84. )
  85. return;
  86. #endif
  87. memset(&ill_act, 0, sizeof(ill_act));
  88. ill_act.sa_handler = ill_handler;
  89. ill_act.sa_mask = all_masked;
  90. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  91. sigaction(SIGILL, &ill_act, &ill_oact);
  92. if (sizeof(size_t) == 4) {
  93. if (sigsetjmp(ill_jmp, 1) == 0) {
  94. OPENSSL_ppc64_probe();
  95. OPENSSL_ppccap_P |= PPC_FPU64;
  96. }
  97. } else {
  98. /*
  99. * Wanted code detecting POWER6 CPU and setting PPC_FPU64
  100. */
  101. }
  102. if (sigsetjmp(ill_jmp, 1) == 0) {
  103. OPENSSL_altivec_probe();
  104. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  105. }
  106. sigaction(SIGILL, &ill_oact, NULL);
  107. sigprocmask(SIG_SETMASK, &oset, NULL);
  108. }