Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/crypto/cpuid.c
34865 views
1
/*
2
* Copyright 1998-2023 The OpenSSL Project Authors. All Rights Reserved.
3
*
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
8
*/
9
10
#include "internal/e_os.h"
11
#include "crypto/cryptlib.h"
12
13
#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
14
defined(__x86_64) || defined(__x86_64__) || \
15
defined(_M_AMD64) || defined(_M_X64)
16
17
extern unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES];
18
19
# if defined(OPENSSL_CPUID_OBJ)
20
21
/*
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*
28
* subroutines.
29
*/
30
# ifdef _WIN32
31
typedef WCHAR variant_char;
32
# define OPENSSL_IA32CAP_P_MAX_CHAR_SIZE 256
33
static variant_char *ossl_getenv(const char *name)
34
{
35
/*
36
* Since we pull only one environment variable, it's simpler to
37
* just ignore |name| and use equivalent wide-char L-literal.
38
* As well as to ignore excessively long values...
39
*/
40
static WCHAR value[OPENSSL_IA32CAP_P_MAX_CHAR_SIZE];
41
DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, OPENSSL_IA32CAP_P_MAX_CHAR_SIZE);
42
43
return (len > 0 && len < OPENSSL_IA32CAP_P_MAX_CHAR_SIZE) ? value : NULL;
44
}
45
# else
46
typedef char variant_char;
47
# define ossl_getenv getenv
48
# endif
49
50
# include "crypto/ctype.h"
51
52
static int todigit(variant_char c)
53
{
54
if (ossl_isdigit(c))
55
return c - '0';
56
else if (ossl_isxdigit(c))
57
return ossl_tolower(c) - 'a' + 10;
58
59
/* return largest base value to make caller terminate the loop */
60
return 16;
61
}
62
63
static uint64_t ossl_strtouint64(const variant_char *str)
64
{
65
uint64_t ret = 0;
66
unsigned int digit, base = 10;
67
68
if (*str == '0') {
69
base = 8, str++;
70
if (ossl_tolower(*str) == 'x')
71
base = 16, str++;
72
}
73
74
while ((digit = todigit(*str++)) < base)
75
ret = ret * base + digit;
76
77
return ret;
78
}
79
80
static variant_char *ossl_strchr(const variant_char *str, char srch)
81
{ variant_char c;
82
83
while ((c = *str)) {
84
if (c == srch)
85
return (variant_char *)str;
86
str++;
87
}
88
89
return NULL;
90
}
91
92
# define OPENSSL_CPUID_SETUP
93
typedef uint64_t IA32CAP;
94
95
void OPENSSL_cpuid_setup(void)
96
{
97
static int trigger = 0;
98
IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
99
IA32CAP vec;
100
const variant_char *env;
101
int index = 2;
102
103
if (trigger)
104
return;
105
106
trigger = 1;
107
if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) {
108
int off = (env[0] == '~') ? 1 : 0;
109
110
vec = ossl_strtouint64(env + off);
111
112
if (off) {
113
IA32CAP mask = vec;
114
vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
115
if (mask & (1<<24)) {
116
/*
117
* User disables FXSR bit, mask even other capabilities
118
* that operate exclusively on XMM, so we don't have to
119
* double-check all the time. We mask PCLMULQDQ, AMD XOP,
120
* AES-NI and AVX. Formally speaking we don't have to
121
* do it in x86_64 case, but we can safely assume that
122
* x86_64 users won't actually flip this flag.
123
*/
124
vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
125
}
126
} else if (env[0] == ':') {
127
vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
128
}
129
130
/* Processed indexes 0, 1 */
131
if ((env = ossl_strchr(env, ':')) != NULL)
132
env++;
133
for (; index < OPENSSL_IA32CAP_P_MAX_INDEXES; index += 2) {
134
if ((env != NULL) && (env[0] != '\0')) {
135
/* if env[0] == ':' current index is skipped */
136
if (env[0] != ':') {
137
IA32CAP vecx;
138
139
off = (env[0] == '~') ? 1 : 0;
140
vecx = ossl_strtouint64(env + off);
141
if (off) {
142
OPENSSL_ia32cap_P[index] &= ~(unsigned int)vecx;
143
OPENSSL_ia32cap_P[index + 1] &= ~(unsigned int)(vecx >> 32);
144
} else {
145
OPENSSL_ia32cap_P[index] = (unsigned int)vecx;
146
OPENSSL_ia32cap_P[index + 1] = (unsigned int)(vecx >> 32);
147
}
148
}
149
/* skip delimeter */
150
if ((env = ossl_strchr(env, ':')) != NULL)
151
env++;
152
} else { /* zeroize the next two indexes */
153
OPENSSL_ia32cap_P[index] = 0;
154
OPENSSL_ia32cap_P[index + 1] = 0;
155
}
156
}
157
158
/* If AVX10 is disabled, zero out its detailed cap bits */
159
if (!(OPENSSL_ia32cap_P[6] & (1 << 19)))
160
OPENSSL_ia32cap_P[9] = 0;
161
} else {
162
vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
163
}
164
165
/*
166
* |(1<<10) sets a reserved bit to signal that variable
167
* was initialized already... This is to avoid interference
168
* with cpuid snippets in ELF .init segment.
169
*/
170
OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
171
OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
172
}
173
# else
174
unsigned int OPENSSL_ia32cap_P[OPENSSL_IA32CAP_P_MAX_INDEXES];
175
# endif
176
#endif
177
178
#ifndef OPENSSL_CPUID_OBJ
179
# ifndef OPENSSL_CPUID_SETUP
180
void OPENSSL_cpuid_setup(void)
181
{
182
}
183
# endif
184
185
/*
186
* The rest are functions that are defined in the same assembler files as
187
* the CPUID functionality.
188
*/
189
190
/*
191
* The volatile is used to ensure that the compiler generates code that reads
192
* all values from the array and doesn't try to optimize this away. The standard
193
* doesn't actually require this behavior if the original data pointed to is
194
* not volatile, but compilers do this in practice anyway.
195
*
196
* There are also assembler versions of this function.
197
*/
198
# undef CRYPTO_memcmp
199
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
200
{
201
size_t i;
202
const volatile unsigned char *a = in_a;
203
const volatile unsigned char *b = in_b;
204
unsigned char x = 0;
205
206
for (i = 0; i < len; i++)
207
x |= a[i] ^ b[i];
208
209
return x;
210
}
211
212
/*
213
* For systems that don't provide an instruction counter register or equivalent.
214
*/
215
uint32_t OPENSSL_rdtsc(void)
216
{
217
return 0;
218
}
219
220
size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
221
{
222
return 0;
223
}
224
225
size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
226
{
227
return 0;
228
}
229
#endif
230
231