Crypto++ 8.0 Free C++ class library of cryptographic schemes

ppc_power9.cpp
1 // ppc_power9.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 // Power9 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(_ARCH_PWR9)
18 # include "ppc_simd.h"
19 #endif
20
21 // Squash MS LNK4221 and libtool warnings
22 extern const char PPC_POWER9_FNAME[] = __FILE__;
23
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)
41
42 bool CPU_ProbePower9()
43 {
44 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
45 return false;
46 #elif defined(CRYPTOPP_POWER9_AVAILABLE)
47 # if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
48 // longjmp and clobber warnings. Volatile is required.
49 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721954
50 volatile int result = true;
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 return false;
59
60 if (setjmp(s_jmpSIGILL))
61 result = false;
62 else
63 {
64 // This is "darn r3, 0". It provides a conditioned 32-bit
65 // word. It is available on both 32-bit and 64-bit.
66 #if CRYPTOPP_BIG_ENDIAN
67 __asm__ __volatile__ (".byte 0x7c, 0x60, 0x05, 0xe6 \n" : : : "r3");
68 #else
69 __asm__ __volatile__ (".byte 0xe6, 0x05, 0x60, 0x7c \n" : : : "r3");
70 #endif
71 result = true;
72 }
73
74 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
75 signal(SIGILL, oldHandler);
76 return result;
77 # endif
78 #else
79 return false;
80 #endif // _ARCH_PWR9
81 }
82
83 // The DARN probe is not guarded with a preprocessor macro at the moment. We
84 // don't use CRYPTOPP_POWER9_AVAILABLE because old compilers, like GCC 4.8 on
85 // CentOS 7, will report NO even though we can produce the random numbers.
86 // Other Power9 implementations which use builtins will use the preprocessor
87 // macro guard. This strategy also gets into a situation where Power9 is not
88 // available but DARN is.
89 bool CPU_ProbeDARN()
90 {
91 #if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
92 return false;
93 #else
94 # if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
95 // longjmp and clobber warnings. Volatile is required.
96 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721954
97 volatile int result = true;
98
99 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
100 if (oldHandler == SIG_ERR)
101 return false;
102
103 volatile sigset_t oldMask;
104 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
105 return false;
106
107 if (setjmp(s_jmpSIGILL))
108 result = false;
109 else
110 {
111 // This is "darn r3, 0". It provides a conditioned 32-bit
112 // word. It is available on both 32-bit and 64-bit.
113 #if CRYPTOPP_BIG_ENDIAN
114 __asm__ __volatile__ (".byte 0x7c, 0x60, 0x05, 0xe6 \n" : : : "r3");
115 #else
116 __asm__ __volatile__ (".byte 0xe6, 0x05, 0x60, 0x7c \n" : : : "r3");
117 #endif
118 result = true;
119 }
120
121 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
122 signal(SIGILL, oldHandler);
123 return result;
124 # endif
125 #endif // DARN
126 }
127
128 #endif // PPC32 or PPC64
129
130 NAMESPACE_END
config.h
Library configuration file.
ppc_simd.h
Support functions for PowerPC and vector operations.
pch.h
Precompiled header file.
CryptoPP
Crypto++ library namespace.