aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/x86/tst-cpu-features-cpuinfo.c
blob: 981b2be5fd3e38e1735ec2f773b7048bd7dd1ed3 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Test CPU feature data against /proc/cpuinfo.
   This file is part of the GNU C Library.
   Copyright (C) 2012-2022 Free Software Foundation, Inc.

   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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

static char *cpu_flags;

/* Search for flags in /proc/cpuinfo and store line
   in cpu_flags.  */
void
get_cpuinfo (void)
{
  FILE *f;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;

  f = fopen ("/proc/cpuinfo", "r");
  if (f == NULL)
    {
      printf ("cannot open /proc/cpuinfo\n");
      exit (1);
    }

  while ((read = getline (&line, &len, f)) != -1)
    {
      if (strncmp (line, "flags", 5) == 0)
       {
         cpu_flags = strdup (line);
         break;
       }
    }
  fclose (f);
  free (line);
}

int
check_proc (const char *proc_name, const char *search_name, int flag,
	    int active, const char *name)
{
  int found = 0;

  printf ("Checking %s:\n", name);
  printf ("  %s: %d\n", name, flag);
  char *str = strstr (cpu_flags, search_name);
  if (str == NULL)
    {
      /* If searching for " XXX " failed, try " XXX\n".  */
      size_t len = strlen (search_name);
      char buffer[80];
      if (len >= sizeof buffer)
	abort ();
      memcpy (buffer, search_name, len + 1);
      buffer[len - 1] = '\n';
      str = strstr (cpu_flags, buffer);
    }
  if (str != NULL)
    found = 1;
  printf ("  cpuinfo (%s): %d\n", proc_name, found);

  if (found != flag)
    {
      if (found || active)
	printf (" *** failure ***\n");
      else
	{
	  printf (" *** missing in /proc/cpuinfo ***\n");
	  return 0;
	}
    }

  return (found != flag);
}

#define CHECK_PROC(str, name) \
  check_proc (#str, " "#str" ", HAS_CPU_FEATURE (name), \
	      CPU_FEATURE_USABLE (name), \
	      "HAS_CPU_FEATURE (" #name ")")

static int
do_test (int argc, char **argv)
{
  int fails = 0;
  const struct cpu_features *cpu_features = __get_cpu_features ();

  get_cpuinfo ();
  fails += CHECK_PROC (acpi, ACPI);
  fails += CHECK_PROC (adx, ADX);
  fails += CHECK_PROC (apic, APIC);
  fails += CHECK_PROC (aes, AES);
  fails += CHECK_PROC (amx_bf16, AMX_BF16);
  fails += CHECK_PROC (amx_int8, AMX_INT8);
  fails += CHECK_PROC (amx_tile, AMX_TILE);
  fails += CHECK_PROC (arch_capabilities, ARCH_CAPABILITIES);
  fails += CHECK_PROC (avx, AVX);
  fails += CHECK_PROC (avx2, AVX2);
  fails += CHECK_PROC (avx512_4fmaps, AVX512_4FMAPS);
  fails += CHECK_PROC (avx512_4vnniw, AVX512_4VNNIW);
  fails += CHECK_PROC (avx512_bf16, AVX512_BF16);
  fails += CHECK_PROC (avx512_bitalg, AVX512_BITALG);
  fails += CHECK_PROC (avx512ifma, AVX512_IFMA);
  fails += CHECK_PROC (avx512vbmi, AVX512_VBMI);
  fails += CHECK_PROC (avx512_vbmi2, AVX512_VBMI2);
  fails += CHECK_PROC (avx512_vnni, AVX512_VNNI);
  fails += CHECK_PROC (avx512_vp2intersect, AVX512_VP2INTERSECT);
  fails += CHECK_PROC (avx512_vpopcntdq, AVX512_VPOPCNTDQ);
  fails += CHECK_PROC (avx512bw, AVX512BW);
  fails += CHECK_PROC (avx512cd, AVX512CD);
  fails += CHECK_PROC (avx512er, AVX512ER);
  fails += CHECK_PROC (avx512dq, AVX512DQ);
  fails += CHECK_PROC (avx512f, AVX512F);
  fails += CHECK_PROC (avx512pf, AVX512PF);
  fails += CHECK_PROC (avx512vl, AVX512VL);
  fails += CHECK_PROC (bmi1, BMI1);
  fails += CHECK_PROC (bmi2, BMI2);
  fails += CHECK_PROC (cldemote, CLDEMOTE);
  fails += CHECK_PROC (clflushopt, CLFLUSHOPT);
  fails += CHECK_PROC (clflush, CLFSH);
  fails += CHECK_PROC (clwb, CLWB);
  fails += CHECK_PROC (cmov, CMOV);
  fails += CHECK_PROC (cx16, CMPXCHG16B);
  fails += CHECK_PROC (cnxt_id, CNXT_ID);
  fails += CHECK_PROC (core_capabilities, CORE_CAPABILITIES);
  fails += CHECK_PROC (cx8, CX8);
  fails += CHECK_PROC (dca, DCA);
  fails += CHECK_PROC (de, DE);
  fails += CHECK_PROC (zero_fcs_fds, DEPR_FPU_CS_DS);
  fails += CHECK_PROC (dts, DS);
  fails += CHECK_PROC (ds_cpl, DS_CPL);
  fails += CHECK_PROC (dtes64, DTES64);
  fails += CHECK_PROC (est, EIST);
  fails += CHECK_PROC (enqcmd, ENQCMD);
  fails += CHECK_PROC (erms, ERMS);
  fails += CHECK_PROC (f16c, F16C);
  fails += CHECK_PROC (fma, FMA);
  fails += CHECK_PROC (fma4, FMA4);
  fails += CHECK_PROC (fpu, FPU);
  fails += CHECK_PROC (fsgsbase, FSGSBASE);
  fails += CHECK_PROC (fsrm, FSRM);
  fails += CHECK_PROC (fxsr, FXSR);
  fails += CHECK_PROC (gfni, GFNI);
  fails += CHECK_PROC (hle, HLE);
  fails += CHECK_PROC (ht, HTT);
  fails += CHECK_PROC (hybrid, HYBRID);
  if (cpu_features->basic.kind == arch_kind_intel)
    {
      fails += CHECK_PROC (ibrs, IBRS_IBPB);
      fails += CHECK_PROC (stibp, STIBP);
    }
  else if (cpu_features->basic.kind == arch_kind_amd)
    {
      fails += CHECK_PROC (ibpb, AMD_IBPB);

      /* The IBRS feature on AMD processors is reported using the Intel feature
       * on KVM guests (synthetic bit).  In both cases the cpuinfo entry is the
       * same.  */
      if (HAS_CPU_FEATURE (IBRS_IBPB))
        fails += CHECK_PROC (ibrs, IBRS_IBPB);
      else
        fails += CHECK_PROC (ibrs, AMD_IBRS);
      fails += CHECK_PROC (stibp, AMD_STIBP);
    }
  fails += CHECK_PROC (ibt, IBT);
  fails += CHECK_PROC (invariant_tsc, INVARIANT_TSC);
  fails += CHECK_PROC (invpcid, INVPCID);
  fails += CHECK_PROC (flush_l1d, L1D_FLUSH);
  fails += CHECK_PROC (lahf_lm, LAHF64_SAHF64);
  fails += CHECK_PROC (lm, LM);
  fails += CHECK_PROC (lwp, LWP);
  fails += CHECK_PROC (abm, LZCNT);
  fails += CHECK_PROC (mca, MCA);
  fails += CHECK_PROC (mce, MCE);
  fails += CHECK_PROC (md_clear, MD_CLEAR);
  fails += CHECK_PROC (mmx, MMX);
  fails += CHECK_PROC (monitor, MONITOR);
  fails += CHECK_PROC (movbe, MOVBE);
  fails += CHECK_PROC (movdiri, MOVDIRI);
  fails += CHECK_PROC (movdir64b, MOVDIR64B);
  fails += CHECK_PROC (mpx, MPX);
  fails += CHECK_PROC (msr, MSR);
  fails += CHECK_PROC (mtrr, MTRR);
  fails += CHECK_PROC (nx, NX);
  fails += CHECK_PROC (ospke, OSPKE);
#if 0
  /* NB: /proc/cpuinfo doesn't report this feature.  */
  fails += CHECK_PROC (osxsave, OSXSAVE);
#endif
  fails += CHECK_PROC (pae, PAE);
  fails += CHECK_PROC (pdpe1gb, PAGE1GB);
  fails += CHECK_PROC (pat, PAT);
  fails += CHECK_PROC (pbe, PBE);
  fails += CHECK_PROC (pcid, PCID);
  fails += CHECK_PROC (pclmulqdq, PCLMULQDQ);
  fails += CHECK_PROC (pconfig, PCONFIG);
  fails += CHECK_PROC (pdcm, PDCM);
  fails += CHECK_PROC (pge, PGE);
  fails += CHECK_PROC (pks, PKS);
  fails += CHECK_PROC (pku, PKU);
  fails += CHECK_PROC (popcnt, POPCNT);
  fails += CHECK_PROC (3dnowprefetch, PREFETCHW);
  fails += CHECK_PROC (prefetchwt1, PREFETCHWT1);
#if 0
  /* NB: /proc/cpuinfo doesn't report this feature.  */
  fails += CHECK_PROC (ptwrite, PTWRITE);
#endif
  fails += CHECK_PROC (pse, PSE);
  fails += CHECK_PROC (pse36, PSE_36);
  fails += CHECK_PROC (psn, PSN);
  fails += CHECK_PROC (rdpid, RDPID);
  fails += CHECK_PROC (rdrand, RDRAND);
  fails += CHECK_PROC (rdseed, RDSEED);
  fails += CHECK_PROC (rdt_a, RDT_A);
  fails += CHECK_PROC (cqm, RDT_M);
  fails += CHECK_PROC (rdtscp, RDTSCP);
  fails += CHECK_PROC (rtm, RTM);
  fails += CHECK_PROC (sdbg, SDBG);
  fails += CHECK_PROC (sep, SEP);
  fails += CHECK_PROC (serialize, SERIALIZE);
  fails += CHECK_PROC (sgx, SGX);
  fails += CHECK_PROC (sgx_lc, SGX_LC);
  fails += CHECK_PROC (sha_ni, SHA);
  fails += CHECK_PROC (shstk, SHSTK);
  fails += CHECK_PROC (smap, SMAP);
  fails += CHECK_PROC (smep, SMEP);
  fails += CHECK_PROC (smx, SMX);
  fails += CHECK_PROC (ss, SS);
  if (cpu_features->basic.kind == arch_kind_intel)
    fails += CHECK_PROC (ssbd, SSBD);
  else if (cpu_features->basic.kind == arch_kind_amd)
    {
      /* This feature is implemented in 2 different ways on AMD processors:
	 newer systems provides AMD_SSBD (function 8000_0008, EBX[24]),
	 while older system proviseds AMD_VIRT_SSBD (function 8000_008,
	 EBX[25]).  However for AMD_VIRT_SSBD, kernel shows both 'ssbd'
	 and 'virt_ssbd' on /proc/cpuinfo; while for AMD_SSBD only 'ssbd'
	 is provided.  */
      if (HAS_CPU_FEATURE (AMD_SSBD))
	fails += CHECK_PROC (ssbd, AMD_SSBD);
      else if (HAS_CPU_FEATURE (AMD_VIRT_SSBD))
	fails += CHECK_PROC (virt_ssbd, AMD_VIRT_SSBD);
    }
  fails += CHECK_PROC (sse, SSE);
  fails += CHECK_PROC (sse2, SSE2);
  fails += CHECK_PROC (pni, SSE3);
  fails += CHECK_PROC (sse4_1, SSE4_1);
  fails += CHECK_PROC (sse4_2, SSE4_2);
  fails += CHECK_PROC (sse4a, SSE4A);
  fails += CHECK_PROC (ssse3, SSSE3);
  fails += CHECK_PROC (svm, SVM);
#ifdef __x86_64__
  /* NB: SYSCALL_SYSRET is 64-bit only.  */
  fails += CHECK_PROC (syscall, SYSCALL_SYSRET);
#endif
  fails += CHECK_PROC (tbm, TBM);
  fails += CHECK_PROC (tm, TM);
  fails += CHECK_PROC (tm2, TM2);
  fails += CHECK_PROC (intel_pt, TRACE);
  fails += CHECK_PROC (tsc, TSC);
  fails += CHECK_PROC (tsc_adjust, TSC_ADJUST);
  fails += CHECK_PROC (tsc_deadline_timer, TSC_DEADLINE);
  fails += CHECK_PROC (tsxldtrk, TSXLDTRK);
  fails += CHECK_PROC (umip, UMIP);
  fails += CHECK_PROC (vaes, VAES);
  fails += CHECK_PROC (vme, VME);
  fails += CHECK_PROC (vmx, VMX);
  fails += CHECK_PROC (vpclmulqdq, VPCLMULQDQ);
  fails += CHECK_PROC (waitpkg, WAITPKG);
  fails += CHECK_PROC (wbnoinvd, WBNOINVD);
  fails += CHECK_PROC (x2apic, X2APIC);
  fails += CHECK_PROC (xfd, XFD);
  fails += CHECK_PROC (xgetbv1, XGETBV_ECX_1);
  fails += CHECK_PROC (xop, XOP);
  fails += CHECK_PROC (xsave, XSAVE);
  fails += CHECK_PROC (xsavec, XSAVEC);
  fails += CHECK_PROC (xsaveopt, XSAVEOPT);
  fails += CHECK_PROC (xsaves, XSAVES);
  fails += CHECK_PROC (xtpr, XTPRUPDCTRL);

  printf ("%d differences between /proc/cpuinfo and glibc code.\n", fails);

  return (fails != 0);
}

#include "../../../test-skeleton.c"