aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/x86_64/dl-x86_cpu_feature_diagnostics.c
blob: 6486ebba5dc0986bb1030159d48f4483c3bc0c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* CPU diagnostics probing.  Linux/x86-64 version.
   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <cpu-features.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/wait.h>
#include <sysdep.h>

static void
_dl_x86_probe (struct x86_cpu_feature_diagnostics *diag, bool reported,
               void (*probe) (void))
{
  if (reported)
    diag->reported |= 1ULL << diag->count;

  /* Use fork/waitid for crash handling.  This is simpler than using
     signal handling: it does not need global data to communicate with
     the handler, nor building out-of-line helper functions to the
     baseline ISA, and it avoids dealing differences in sigset_t size.  */
  long int ret = INTERNAL_SYSCALL_CALL (fork);
  if (ret == 0)
    {
      /* New process that runs the probe.  This may trigger a crash.  */
      probe ();

      INTERNAL_SYSCALL_CALL (exit_group, 0);
    }
  else if (ret > 0)
    {
      siginfo_t si;
      ret = INTERNAL_SYSCALL_CALL (waitid, P_PID, ret, &si, WEXITED, NULL);
      if (ret >=0 && si.si_status == 0)
        /* Probe was successful.   */
        diag->probed |= 1ULL << diag->count;
    }

  ++diag->count;
}

void
_dl_x86_cpu_feature_diagnostics_run (const struct cpu_features *cpu_features,
                                     struct x86_cpu_feature_diagnostics *diag)
{
  /* x86-64-v2 features.  */
  extern void _dl_x86_probe_cmpxchg16b (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, CMPXCHG16B),
                 _dl_x86_probe_cmpxchg16b);

  extern void _dl_x86_probe_sahf (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, LAHF64_SAHF64),
                 _dl_x86_probe_sahf);

  extern void _dl_x86_probe_popcnt (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, POPCNT),
                 _dl_x86_probe_popcnt);

  extern void _dl_x86_probe_sse3 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, SSE3),
                 _dl_x86_probe_sse3);

  extern void _dl_x86_probe_sse4_1 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, SSE4_1),
                 _dl_x86_probe_sse4_1);

  extern void _dl_x86_probe_sse4_2 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, SSE4_2),
                 _dl_x86_probe_sse4_2);

  extern void _dl_x86_probe_ssse3 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, SSSE3),
                 _dl_x86_probe_ssse3);

  /* x86-64-v3 features.  */
  extern void _dl_x86_probe_avx (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX),
                 _dl_x86_probe_avx);

  /* AVX probe using xmm registers.  */
  extern void _dl_x86_probe_avx_xmm (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX),
                 _dl_x86_probe_avx_xmm);

  extern void _dl_x86_probe_avx2 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX2),
                 _dl_x86_probe_avx2);

  extern void _dl_x86_probe_bmi1 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, BMI1),
                 _dl_x86_probe_bmi1);

  /* Alternative BMI1 probe.  Perhaps harder to mask.  */
  extern void _dl_x86_probe_bmi1_tzcnt (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, BMI1),
                 _dl_x86_probe_bmi1_tzcnt);

  extern void _dl_x86_probe_bmi2 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, BMI2),
                 _dl_x86_probe_bmi2);

  extern void _dl_x86_probe_f16c (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, F16C),
                 _dl_x86_probe_f16c);

  extern void _dl_x86_probe_fma (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, FMA),
                 _dl_x86_probe_fma);

  /* FMA4 is not part of x86-64-v3, but may produce a useful hint.  */
  extern void _dl_x86_probe_fma4 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, FMA4),
                 _dl_x86_probe_fma4);

  extern void _dl_x86_probe_lzcnt (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, LZCNT),
                 _dl_x86_probe_lzcnt);

  extern void _dl_x86_probe_movbe (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, MOVBE),
                 _dl_x86_probe_movbe);

  extern void _dl_x86_probe_osxsave (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, OSXSAVE),
                 _dl_x86_probe_osxsave);

  /* x86-64-v4 features.  */
  extern void _dl_x86_probe_avx512f (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512F),
                 _dl_x86_probe_avx512f);

  extern void _dl_x86_probe_avx512bw (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512BW),
                 _dl_x86_probe_avx512bw);

  extern void _dl_x86_probe_avx512bw_ymm (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512BW),
                 _dl_x86_probe_avx512bw_ymm);

  extern void _dl_x86_probe_avx512cd (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512CD),
                 _dl_x86_probe_avx512cd);

  extern void _dl_x86_probe_avx512cd_ymm0 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512CD),
                 _dl_x86_probe_avx512cd_ymm0);

  extern void _dl_x86_probe_avx512vl (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512VL),
                 _dl_x86_probe_avx512vl);

  /* Other CPU features, not part of microarchitecture levels.  */
  extern void _dl_x86_probe_adx (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, ADX),
                 _dl_x86_probe_adx);

  extern void _dl_x86_probe_aes (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AES),
                 _dl_x86_probe_aes);

  extern void _dl_x86_probe_aes_avx (void) attribute_hidden;
  _dl_x86_probe (diag,
                 CPU_FEATURE_USABLE_P (cpu_features, AES)
                 && CPU_FEATURE_USABLE_P (cpu_features, AVX),
                 _dl_x86_probe_aes_avx);

  extern void _dl_x86_probe_vaes (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, VAES),
                 _dl_x86_probe_vaes);

  extern void _dl_x86_probe_sha (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, SHA),
                 _dl_x86_probe_sha);

  extern void _dl_x86_probe_avx512_vbmi (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_VBMI),
                 _dl_x86_probe_avx512_vbmi);

  extern void _dl_x86_probe_avx512_vbmi_xmm (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_VBMI),
                 _dl_x86_probe_avx512_vbmi_xmm);

  extern void _dl_x86_probe_avx512_vbmi2 (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_VBMI2),
                 _dl_x86_probe_avx512_vbmi2);

  extern void _dl_x86_probe_avx512_vbmi2_xmm (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_VBMI2),
                 _dl_x86_probe_avx512_vbmi2_xmm);

  extern void _dl_x86_probe_avx_vnni (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX_VNNI),
                 _dl_x86_probe_avx_vnni);

  extern void _dl_x86_probe_avx512_vnni (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_VNNI),
                 _dl_x86_probe_avx512_vnni);

  extern void _dl_x86_probe_avx512_ifma (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, AVX512_IFMA),
                 _dl_x86_probe_avx512_ifma);

  serialize
  tpause
  ptwrite
  xsusldtrk
  clmul
  crc32

  extern void _dl_x86_probe_apx_f (void) attribute_hidden;
  _dl_x86_probe (diag, CPU_FEATURE_USABLE_P (cpu_features, APX_F),
                 _dl_x86_probe_apx_f);
}