PR: 2149
[oweals/openssl.git] / crypto / ppccap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <setjmp.h>
5 #include <signal.h>
6 #include <openssl/bn.h>
7
8 #define PPC_FPU64       (1<<0)
9
10 static int OPENSSL_ppccap_P = 0;
11
12 static sigset_t all_masked;
13
14 #ifdef OPENSSL_BN_ASM_MONT
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_fpu64(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
20         if (sizeof(size_t)==4)
21                 {
22 #if (defined(__APPLE__) && defined(__MACH__))
23                 if ((OPENSSL_ppccap_P&PPC_FPU64))
24                         return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
25 #else
26                 /* boundary of 32 was experimentally determined on
27                    Linux 2.6.22, might have to be adjusted on AIX... */
28                 if (num>=32 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64))
29                         {
30                         sigset_t oset;
31                         int ret;
32
33                         sigprocmask(SIG_SETMASK,&all_masked,&oset);
34                         ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
35                         sigprocmask(SIG_SETMASK,&oset,NULL);
36
37                         return ret;
38                         }
39 #endif
40                 }
41         else if ((OPENSSL_ppccap_P&PPC_FPU64))
42                 /* this is a "must" on Power 6, but run-time detection
43                  * is not implemented yet... */
44                 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
45
46         return bn_mul_mont_int(rp,ap,bp,np,n0,num);
47         }
48 #endif
49
50 static sigjmp_buf ill_jmp;
51 static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
52
53 void OPENSSL_ppc64_probe(void);
54
55 void OPENSSL_cpuid_setup(void)
56         {
57         char *e;
58
59         sigfillset(&all_masked);
60         sigdelset(&all_masked,SIGILL);
61         sigdelset(&all_masked,SIGTRAP);
62         sigdelset(&all_masked,SIGEMT);
63         sigdelset(&all_masked,SIGFPE);
64         sigdelset(&all_masked,SIGBUS);
65         sigdelset(&all_masked,SIGSEGV);
66
67         if ((e=getenv("OPENSSL_ppccap")))
68                 {
69                 OPENSSL_ppccap_P=strtoul(e,NULL,0);
70                 return;
71                 }
72
73         if (sizeof(size_t)==4)
74                 {
75                 struct sigaction        ill_oact,ill_act;
76                 sigset_t                oset;
77
78                 memset(&ill_act,0,sizeof(ill_act));
79                 ill_act.sa_handler = ill_handler;
80                 ill_act.sa_mask    = all_masked;
81                 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset);
82                 sigaction (SIGILL,&ill_act,&ill_oact);
83                 if (sigsetjmp(ill_jmp,0) == 0)
84                         {
85                         OPENSSL_ppc64_probe();
86                         OPENSSL_ppccap_P |= PPC_FPU64;
87                         }
88                 else
89                         {
90                         OPENSSL_ppccap_P &= ~PPC_FPU64;
91                         }
92                 sigaction (SIGILL,&ill_oact,NULL);
93                 sigprocmask(SIG_SETMASK,&oset,NULL);
94                 }
95         }