Crypto++ 8.9
Free C++ class library of cryptographic schemes
power9_ppc.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
22extern const char PPC_POWER9_FNAME[] = __FILE__;
23
24NAMESPACE_BEGIN(CryptoPP)
25
26// ************************* Feature Probes ************************* //
27
28#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
29extern "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
42bool 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 {
59 signal(SIGILL, oldHandler);
60 return false;
61 }
62
63 if (setjmp(s_jmpSIGILL))
64 result = false;
65 else
66 {
67 // This is "darn r3, 0" from POWER9. We cannot use __builtin_darn
68 // and friends because Clang and IBM XL C/C++ does not offer them.
69#if CRYPTOPP_BIG_ENDIAN
70 __asm__ __volatile__ (".byte 0x7c, 0x60, 0x05, 0xe6 \n" : : : "r3");
71#else
72 __asm__ __volatile__ (".byte 0xe6, 0x05, 0x60, 0x7c \n" : : : "r3");
73#endif
74 result = true;
75 }
76
77 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
78 signal(SIGILL, oldHandler);
79 return result;
80# endif
81#else
82 return false;
83#endif // _ARCH_PWR9
84}
85
86// The DARN probe is not guarded with a preprocessor macro at the moment. We
87// don't use CRYPTOPP_POWER9_AVAILABLE because old compilers, like GCC 4.8 on
88// CentOS 7, will report NO even though we can produce the random numbers.
89// Other Power9 implementations which use builtins will use the preprocessor
90// macro guard. This strategy also gets into a situation where Power9 is not
91// available but DARN is.
92bool CPU_ProbeDARN()
93{
94#if defined(CRYPTOPP_NO_CPU_FEATURE_PROBES)
95 return false;
96#else
97# if defined(CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY)
98 // longjmp and clobber warnings. Volatile is required.
99 // http://github.com/weidai11/cryptopp/issues/24 and http://stackoverflow.com/q/7721954
100 volatile int result = true;
101
102 volatile SigHandler oldHandler = signal(SIGILL, SigIllHandler);
103 if (oldHandler == SIG_ERR)
104 return false;
105
106 volatile sigset_t oldMask;
107 if (sigprocmask(0, NULLPTR, (sigset_t*)&oldMask))
108 {
109 signal(SIGILL, oldHandler);
110 return false;
111 }
112
113 if (setjmp(s_jmpSIGILL))
114 result = false;
115 else
116 {
117 // This is "darn r3, 0" from POWER9. We cannot use __builtin_darn
118 // and friends because Clang and IBM XL C/C++ does not offer them.
119#if CRYPTOPP_BIG_ENDIAN
120 __asm__ __volatile__ (".byte 0x7c, 0x60, 0x05, 0xe6 \n" : : : "r3");
121#else
122 __asm__ __volatile__ (".byte 0xe6, 0x05, 0x60, 0x7c \n" : : : "r3");
123#endif
124 result = true;
125 }
126
127 sigprocmask(SIG_SETMASK, (sigset_t*)&oldMask, NULLPTR);
128 signal(SIGILL, oldHandler);
129 return result;
130# endif
131#endif // DARN
132}
133
134#endif // PPC32 or PPC64
135
136NAMESPACE_END
Library configuration file.
Crypto++ library namespace.
Precompiled header file.
Support functions for PowerPC and vector operations.