2 * Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/e_os.h"
11 #include "crypto/cryptlib.h"
13 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
14 defined(__x86_64) || defined(__x86_64__) || \
15 defined(_M_AMD64) || defined(_M_X64)
17 extern unsigned int OPENSSL_ia32cap_P[4];
19 # if defined(OPENSSL_CPUID_OBJ)
22 * Purpose of these minimalistic and character-type-agnostic subroutines
23 * is to break dependency on MSVCRT (on Windows) and locale. This makes
24 * OPENSSL_cpuid_setup safe to use as "constructor". "Character-type-
25 * agnostic" means that they work with either wide or 8-bit characters,
26 * exploiting the fact that first 127 characters can be simply casted
27 * between the sets, while the rest would be simply rejected by ossl_is*
31 typedef WCHAR variant_char;
33 static variant_char *ossl_getenv(const char *name)
36 * Since we pull only one environment variable, it's simpler to
37 * to just ignore |name| and use equivalent wide-char L-literal.
38 * As well as to ignore excessively long values...
40 static WCHAR value[48];
41 DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, 48);
43 return (len > 0 && len < 48) ? value : NULL;
46 typedef char variant_char;
47 # define ossl_getenv getenv
50 # include "crypto/ctype.h"
52 static int todigit(variant_char c)
56 else if (ossl_isxdigit(c))
57 return ossl_tolower(c) - 'a' + 10;
59 /* return largest base value to make caller terminate the loop */
63 static uint64_t ossl_strtouint64(const variant_char *str)
66 unsigned int digit, base = 10;
70 if (ossl_tolower(*str) == 'x')
74 while ((digit = todigit(*str++)) < base)
75 ret = ret * base + digit;
80 static variant_char *ossl_strchr(const variant_char *str, char srch)
85 return (variant_char *)str;
92 # define OPENSSL_CPUID_SETUP
93 typedef uint64_t IA32CAP;
95 void OPENSSL_cpuid_setup(void)
97 static int trigger = 0;
98 IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
100 const variant_char *env;
106 if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) {
107 int off = (env[0] == '~') ? 1 : 0;
109 vec = ossl_strtouint64(env + off);
113 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
114 if (mask & (1<<24)) {
116 * User disables FXSR bit, mask even other capabilities
117 * that operate exclusively on XMM, so we don't have to
118 * double-check all the time. We mask PCLMULQDQ, AMD XOP,
119 * AES-NI and AVX. Formally speaking we don't have to
120 * do it in x86_64 case, but we can safely assume that
121 * x86_64 users won't actually flip this flag.
123 vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
125 } else if (env[0] == ':') {
126 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
129 if ((env = ossl_strchr(env, ':')) != NULL) {
133 off = (env[0] == '~') ? 1 : 0;
134 vecx = ossl_strtouint64(env + off);
136 OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx;
137 OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32);
139 OPENSSL_ia32cap_P[2] = (unsigned int)vecx;
140 OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32);
143 OPENSSL_ia32cap_P[2] = 0;
144 OPENSSL_ia32cap_P[3] = 0;
147 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
151 * |(1<<10) sets a reserved bit to signal that variable
152 * was initialized already... This is to avoid interference
153 * with cpuid snippets in ELF .init segment.
155 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
156 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
159 unsigned int OPENSSL_ia32cap_P[4];
163 #ifndef OPENSSL_CPUID_OBJ
164 # ifndef OPENSSL_CPUID_SETUP
165 void OPENSSL_cpuid_setup(void)
171 * The rest are functions that are defined in the same assembler files as
172 * the CPUID functionality.
176 * The volatile is used to to ensure that the compiler generates code that reads
177 * all values from the array and doesn't try to optimize this away. The standard
178 * doesn't actually require this behavior if the original data pointed to is
179 * not volatile, but compilers do this in practice anyway.
181 * There are also assembler versions of this function.
183 # undef CRYPTO_memcmp
184 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
187 const volatile unsigned char *a = in_a;
188 const volatile unsigned char *b = in_b;
191 for (i = 0; i < len; i++)
198 * For systems that don't provide an instruction counter register or equivalent.
200 uint32_t OPENSSL_rdtsc(void)
205 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
210 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)