1 // ppc_power7.cpp - written and placed in the public domain by
2 // Jeffrey Walton, Uri Blumenthal and Marcel Raad.
3 //
4 // This source file uses intrinsics and built-ins to gain access to
5 // Power7 instructions. A separate source file is needed because
6 // additional CXXFLAGS are required to enable the appropriate
7 // instructions sets in some build configurations.
8
9 #include "pch.h"
10 #include "config.h"
11
12 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
13 # include <signal.h>
14 # include <setjmp.h>
15 #endif
16
17 #if defined(__ALTIVEC__) || defined(_ARCH_PWR7)
18 # include "ppc_simd.h"
19 #endif
20
21 // Squash MS LNK4221 and libtool warnings
22 extern const char PPC_POWER7_FNAME[] = __FILE__;
23
NAMESPACE_BEGIN(CryptoPP)24 NAMESPACE_BEGIN(CryptoPP)
25
26 // ************************* Feature Probes ************************* //
27
28 #ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
29 extern "C" {
30 typedef void (*SigHandler)(int);
31
32 static jmp_buf s_jmpSIGILL;
33 static void SigIllHandler(int)
34 {
35 longjmp(s_jmpSIGILL, 1);
36 }
37 }
38 #endif // CRYPTOPP_MS_STYLE_INLINE_ASSEMBLY
39
40 #if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
CPU_ProbePower7()41 bool CPU_ProbePower7()
42 {
43 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
44 return false;
45 #elif defined(CRYPTOPP_POWER7_AVAILABLE)
46 # if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
47
48 // longjmp and clobber warnings. Volatile is required.
49 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721854
50 volatile int result = false;
51
52 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
53 if (oldHandler == SIG_ERR)
54 return false;
55
56 volatile sigset_t oldMask;
57 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
58 {
59 signal(SIGILL, oldHandler);
60 return false;
61 }
62
63 if (setjmp(s_jmpSIGILL))
64 result = false;
65 else
66 {
67 // POWER7 added unaligned loads and store operations
68 byte b1[19] = {255, 255, 255, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, b2[17];
69
70 // See comments in ppc_simd.h for some of these defines.
71 #if defined(_AIX) && defined(_ARCH_PWR7) && ((__xlC__ & 0xff00) == 0x0c00)
72 vec_xstw4(vec_xlw4(0, (unsigned int*)(b1+3)), 0, (unsigned int*)(b2+1));
73 result = (0 == std::memcmp(b1+3, b2+1, 16));
74 #elif defined(_ARCH_PWR7) && defined(__VSX__)
75 vec_xst(vec_xl(0, (unsigned int*)(b1+3)), 0, (unsigned int*)(b2+1));
76 result = (0 == std::memcmp(b1+3, b2+1, 16));
77 #else
78 result = false;
79 #endif
80 }
81
82 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
83 signal(SIGILL, oldHandler);
84 return result;
85 # endif
86 #else
87 return false;
88 #endif // _ARCH_PWR7
89 }
90
91 #endif // PPC32 or PPC64
92
93 NAMESPACE_END
94