| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/base.h> |
| 16 |
| 17 | #if !defined(OPENSSL_NO_ASM) && \ |
| 18 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64)) |
| 19 |
| 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 |
| 25 | #if defined(_MSC_VER) |
| 26 | #include <immintrin.h> |
| 27 | #include <intrin.h> |
| 28 | #endif |
| 29 |
| 30 | #include "internal.h" |
| 31 |
| 32 |
| 33 | // OPENSSL_cpuid runs the cpuid instruction. |leaf| is passed in as EAX and ECX |
| 34 | // is set to zero. It writes EAX, EBX, ECX, and EDX to |*out_eax| through |
| 35 | // |*out_edx|. |
| 36 | static void OPENSSL_cpuid(uint32_t *out_eax, uint32_t *out_ebx, |
| 37 | uint32_t *out_ecx, uint32_t *out_edx, uint32_t leaf) { |
| 38 | #if defined(_MSC_VER) |
| 39 | int tmp[4]; |
| 40 | __cpuid(tmp, (int)leaf); |
| 41 | *out_eax = (uint32_t)tmp[0]; |
| 42 | *out_ebx = (uint32_t)tmp[1]; |
| 43 | *out_ecx = (uint32_t)tmp[2]; |
| 44 | *out_edx = (uint32_t)tmp[3]; |
| 45 | #elif defined(__pic__) && defined(OPENSSL_32_BIT) |
| 46 | // Inline assembly may not clobber the PIC register. For 32-bit, this is EBX. |
| 47 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602. |
| 48 | __asm__ volatile( |
| 49 | "xor %%ecx, %%ecx\n" |
| 50 | "mov %%ebx, %%edi\n" |
| 51 | "cpuid\n" |
| 52 | "xchg %%edi, %%ebx\n" |
| 53 | : "=a"(*out_eax), "=D"(*out_ebx), "=c"(*out_ecx), "=d"(*out_edx) |
| 54 | : "a"(leaf)); |
| 55 | #else |
| 56 | __asm__ volatile( |
| 57 | "xor %%ecx, %%ecx\n" |
| 58 | "cpuid\n" |
| 59 | : "=a"(*out_eax), "=b"(*out_ebx), "=c"(*out_ecx), "=d"(*out_edx) |
| 60 | : "a"(leaf)); |
| 61 | #endif |
| 62 | } |
| 63 |
| 64 | // OPENSSL_xgetbv returns the value of an Intel Extended Control Register (XCR). |
| 65 | // Currently only XCR0 is defined by Intel so |xcr| should always be zero. |
| 66 | static uint64_t OPENSSL_xgetbv(uint32_t xcr) { |
| 67 | #if defined(_MSC_VER) |
| 68 | return (uint64_t)_xgetbv(xcr); |
| 69 | #else |
| 70 | uint32_t eax, edx; |
| 71 | __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
| 72 | return (((uint64_t)edx) << 32) | eax; |
| 73 | #endif |
| 74 | } |
| 75 |
| 76 | static bool os_supports_avx512(uint64_t xcr0) { |
| 77 | #if defined(__APPLE__) |
| 78 | // The Darwin kernel had a bug where it could corrupt the opmask registers. |
| 79 | // See |
| 80 | // https://community.intel.com/t5/Software-Tuning-Performance/MacOS-Darwin-kernel-bug-clobbers-AVX-512-opmask-register-state/m-p/1327259 |
| 81 | // Darwin also does not initially set the XCR0 bits for AVX512, but they are |
| 82 | // set if the thread tries to use AVX512 anyway. Thus, to safely and |
| 83 | // consistently use AVX512 on macOS we'd need to check the kernel version as |
| 84 | // well as detect AVX512 support using a macOS-specific method. We don't |
| 85 | // bother with this, especially given Apple's transition to arm64. |
| 86 | return false; |
| 87 | #else |
| 88 | return (xcr0 & 0xe6) == 0xe6; |
| 89 | #endif |
| 90 | } |
| 91 |
| 92 | // handle_cpu_env applies the value from |in| to the CPUID values in |out[0]| |
| 93 | // and |out[1]|. See the comment in |OPENSSL_cpuid_setup| about this. |
| 94 | static void handle_cpu_env(uint32_t *out, const char *in) { |
| 95 | const int invert_op = in[0] == '~'; |
| 96 | const int or_op = in[0] == '|'; |
| 97 | const int skip_first_byte = invert_op || or_op; |
| 98 | const int hex = in[skip_first_byte] == '0' && in[skip_first_byte + 1] == 'x'; |
| 99 |
| 100 | int sscanf_result; |
| 101 | uint64_t v; |
| 102 | if (hex) { |
| 103 | sscanf_result = sscanf(in + invert_op + 2, "%" PRIx64, &v); |
| 104 | } else { |
| 105 | sscanf_result = sscanf(in + invert_op, "%" PRIu64, &v); |
| 106 | } |
| 107 |
| 108 | if (!sscanf_result) { |
| 109 | return; |
| 110 | } |
| 111 |
| 112 | if (invert_op) { |
| 113 | out[0] &= ~v; |
| 114 | out[1] &= ~(v >> 32); |
| 115 | } else if (or_op) { |
| 116 | out[0] |= v; |
| 117 | out[1] |= (v >> 32); |
| 118 | } else { |
| 119 | out[0] = v; |
| 120 | out[1] = v >> 32; |
| 121 | } |
| 122 | } |
| 123 |
| 124 | void OPENSSL_cpuid_setup(void) { |
| 125 | // Determine the vendor and maximum input value. |
| 126 | uint32_t eax, ebx, ecx, edx; |
| 127 | OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 0); |
| 128 |
| 129 | uint32_t num_ids = eax; |
| 130 |
| 131 | int is_intel = ebx == 0x756e6547 /* Genu */ && // |
| 132 | edx == 0x49656e69 /* ineI */ && // |
| 133 | ecx == 0x6c65746e /* ntel */; |
| 134 | int is_amd = ebx == 0x68747541 /* Auth */ && // |
| 135 | edx == 0x69746e65 /* enti */ && // |
| 136 | ecx == 0x444d4163 /* cAMD */; |
| 137 |
| 138 | uint32_t extended_features[2] = {0}; |
| 139 | if (num_ids >= 7) { |
| 140 | OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 7); |
| 141 | extended_features[0] = ebx; |
| 142 | extended_features[1] = ecx; |
| 143 | } |
| 144 |
| 145 | OPENSSL_cpuid(&eax, &ebx, &ecx, &edx, 1); |
| 146 |
| 147 | const uint32_t base_family = (eax >> 8) & 15; |
| 148 | const uint32_t base_model = (eax >> 4) & 15; |
| 149 |
| 150 | uint32_t family = base_family; |
| 151 | uint32_t model = base_model; |
| 152 | if (base_family == 15) { |
| 153 | const uint32_t ext_family = (eax >> 20) & 255; |
| 154 | family += ext_family; |
| 155 | } |
| 156 | if (base_family == 6 || base_family == 15) { |
| 157 | const uint32_t ext_model = (eax >> 16) & 15; |
| 158 | model |= ext_model << 4; |
| 159 | } |
| 160 |
| 161 | if (is_amd) { |
| 162 | if (family < 0x17 || (family == 0x17 && 0x70 <= model && model <= 0x7f)) { |
| 163 | // Disable RDRAND on AMD families before 0x17 (Zen) due to reported |
| 164 | // failures after suspend. |
| 165 | // https://bugzilla.redhat.com/show_bug.cgi?id=1150286 |
| 166 | // Also disable for family 0x17, models 0x70–0x7f, due to possible RDRAND |
| 167 | // failures there too. |
| 168 | ecx &= ~(1u << 30); |
| 169 | } |
| 170 | } |
| 171 |
| 172 | // Reserved bit #30 is repurposed to signal an Intel CPU. |
| 173 | if (is_intel) { |
| 174 | edx |= (1u << 30); |
| 175 | } else { |
| 176 | edx &= ~(1u << 30); |
| 177 | } |
| 178 |
| 179 | uint64_t xcr0 = 0; |
| 180 | if (ecx & (1u << 27)) { |
| 181 | // XCR0 may only be queried if the OSXSAVE bit is set. |
| 182 | xcr0 = OPENSSL_xgetbv(0); |
| 183 | } |
| 184 | // See Intel manual, volume 1, section 14.3. |
| 185 | if ((xcr0 & 6) != 6) { |
| 186 | // YMM registers cannot be used. |
| 187 | ecx &= ~(1u << 28); // AVX |
| 188 | ecx &= ~(1u << 12); // FMA |
| 189 | ecx &= ~(1u << 11); // AMD XOP |
| 190 | extended_features[0] &= ~(1u << 5); // AVX2 |
| 191 | extended_features[1] &= ~(1u << 9); // VAES |
| 192 | extended_features[1] &= ~(1u << 10); // VPCLMULQDQ |
| 193 | } |
| 194 | // See Intel manual, volume 1, sections 15.2 ("Detection of AVX-512 Foundation |
| 195 | // Instructions") through 15.4 ("Detection of Intel AVX-512 Instruction Groups |
| 196 | // Operating at 256 and 128-bit Vector Lengths"). |
| 197 | if (!os_supports_avx512(xcr0)) { |
| 198 | // Without XCR0.111xx11x, no AVX512 feature can be used. This includes ZMM |
| 199 | // registers, masking, SIMD registers 16-31 (even if accessed as YMM or |
| 200 | // XMM), and EVEX-coded instructions (even on YMM or XMM). Even if only |
| 201 | // XCR0.ZMM_Hi256 is missing, it isn't valid to use AVX512 features on |
| 202 | // shorter vectors, since AVX512 ties everything to the availability of |
| 203 | // 512-bit vectors. See the above-mentioned sections of the Intel manual, |
| 204 | // which say that *all* these XCR0 bits must be checked even when just using |
| 205 | // 128-bit or 256-bit vectors, and also volume 2a section 2.7.11 ("#UD |
| 206 | // Equations for EVEX") which says that all EVEX-coded instructions raise an |
| 207 | // undefined-instruction exception if any of these XCR0 bits is zero. |
| 208 | extended_features[0] &= ~(1u << 16); // AVX512F |
| 209 | extended_features[0] &= ~(1u << 17); // AVX512DQ |
| 210 | extended_features[0] &= ~(1u << 21); // AVX512IFMA |
| 211 | extended_features[0] &= ~(1u << 26); // AVX512PF |
| 212 | extended_features[0] &= ~(1u << 27); // AVX512ER |
| 213 | extended_features[0] &= ~(1u << 28); // AVX512CD |
| 214 | extended_features[0] &= ~(1u << 30); // AVX512BW |
| 215 | extended_features[0] &= ~(1u << 31); // AVX512VL |
| 216 | extended_features[1] &= ~(1u << 1); // AVX512VBMI |
| 217 | extended_features[1] &= ~(1u << 6); // AVX512VBMI2 |
| 218 | extended_features[1] &= ~(1u << 11); // AVX512VNNI |
| 219 | extended_features[1] &= ~(1u << 12); // AVX512BITALG |
| 220 | extended_features[1] &= ~(1u << 14); // AVX512VPOPCNTDQ |
| 221 | } |
| 222 |
| 223 | // Repurpose the bit for the removed MPX feature to indicate when using zmm |
| 224 | // registers should be avoided even when they are supported. (When set, AVX512 |
| 225 | // features can still be used, but only using ymm or xmm registers.) Skylake |
| 226 | // suffered from severe downclocking when zmm registers were used, which |
| 227 | // affected unrelated code running on the system, making zmm registers not too |
| 228 | // useful outside of benchmarks. The situation improved significantly by Ice |
| 229 | // Lake, but a small amount of downclocking remained. (See |
| 230 | // https://lore.kernel.org/linux-crypto/e8ce1146-3952-6977-1d0e-a22758e58914@intel.com/) |
| 231 | // We take a conservative approach of not allowing zmm registers until after |
| 232 | // Ice Lake and Tiger Lake, i.e. until Sapphire Rapids on the server side. |
| 233 | // |
| 234 | // AMD CPUs, which support AVX512 starting with Zen 4, have not been reported |
| 235 | // to have any downclocking problem when zmm registers are used. |
| 236 | if (is_intel && family == 6 && |
| 237 | (model == 85 || // Skylake, Cascade Lake, Cooper Lake (server) |
| 238 | model == 106 || // Ice Lake (server) |
| 239 | model == 108 || // Ice Lake (micro server) |
| 240 | model == 125 || // Ice Lake (client) |
| 241 | model == 126 || // Ice Lake (mobile) |
| 242 | model == 140 || // Tiger Lake (mobile) |
| 243 | model == 141)) { // Tiger Lake (client) |
| 244 | extended_features[0] |= 1u << 14; |
| 245 | } else { |
| 246 | extended_features[0] &= ~(1u << 14); |
| 247 | } |
| 248 |
| 249 | OPENSSL_ia32cap_P[0] = edx; |
| 250 | OPENSSL_ia32cap_P[1] = ecx; |
| 251 | OPENSSL_ia32cap_P[2] = extended_features[0]; |
| 252 | OPENSSL_ia32cap_P[3] = extended_features[1]; |
| 253 |
| 254 | const char *env1, *env2; |
| 255 | env1 = getenv("OPENSSL_ia32cap"); |
| 256 | if (env1 == NULL) { |
| 257 | return; |
| 258 | } |
| 259 |
| 260 | // OPENSSL_ia32cap can contain zero, one or two values, separated with a ':'. |
| 261 | // Each value is a 64-bit, unsigned value which may start with "0x" to |
| 262 | // indicate a hex value. Prior to the 64-bit value, a '~' or '|' may be given. |
| 263 | // |
| 264 | // If the '~' prefix is present: |
| 265 | // the value is inverted and ANDed with the probed CPUID result |
| 266 | // If the '|' prefix is present: |
| 267 | // the value is ORed with the probed CPUID result |
| 268 | // Otherwise: |
| 269 | // the value is taken as the result of the CPUID |
| 270 | // |
| 271 | // The first value determines OPENSSL_ia32cap_P[0] and [1]. The second [2] |
| 272 | // and [3]. |
| 273 |
| 274 | handle_cpu_env(&OPENSSL_ia32cap_P[0], env1); |
| 275 | env2 = strchr(env1, ':'); |
| 276 | if (env2 != NULL) { |
| 277 | handle_cpu_env(&OPENSSL_ia32cap_P[2], env2 + 1); |
| 278 | } |
| 279 | } |
| 280 |
| 281 | #endif // !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64) |
| 282 |