1 /* Initialize CPU feature data via IFUNC relocation.
2    Copyright (C) 2015-2022 Free Software Foundation, Inc.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 
19 #include <ldsodefs.h>
20 
21 #ifdef SHARED
22 # include <cpu-features.c>
23 # include <gcc-macros.h>
24 
25 /* NB: Normally, DL_PLATFORM_INIT calls init_cpu_features to initialize
26    CPU features in dynamic executable.  But when loading ld.so inside of
27    static executable, DL_PLATFORM_INIT isn't called and IFUNC relocation
28    is used to call init_cpu_features.  In static executable, it is called
29    once by IFUNC relocation.  In dynamic executable, it is called twice
30    by DL_PLATFORM_INIT and by IFUNC relocation.  */
31 extern void __x86_cpu_features (void) attribute_hidden;
32 void (*const __x86_cpu_features_p) (void) attribute_hidden
33   = __x86_cpu_features;
34 
35 void
_dl_x86_init_cpu_features(void)36 _dl_x86_init_cpu_features (void)
37 {
38   struct cpu_features *cpu_features = __get_cpu_features ();
39   if (cpu_features->basic.kind == arch_kind_unknown)
40     {
41       init_cpu_features (cpu_features);
42 
43 # if IS_IN (rtld)
44       /* See isa-level.c.  */
45 #  if defined GCCMACRO__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16		\
46   && defined HAVE_X86_LAHF_SAHF && defined GCCMACRO__POPCNT__		\
47   && defined GCCMACRO__SSE3__ && defined GCCMACRO__SSSE3__		\
48   && defined GCCMACRO__SSE4_1__ && defined GCCMACRO__SSE4_2__
49       if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V2))
50 	_dl_fatal_printf ("\
51 Fatal glibc error: CPU does not support x86-64-v%d\n", 2);
52 #   if defined GCCMACRO__AVX__ && defined GCCMACRO__AVX2__ \
53   && defined GCCMACRO__F16C__ && defined GCCMACRO__FMA__   \
54   && defined GCCMACRO__LZCNT__ && defined HAVE_X86_MOVBE
55       if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V3))
56 	_dl_fatal_printf ("\
57 Fatal glibc error: CPU does not support x86-64-v%d\n", 3);
58 #    if defined GCCMACRO__AVX512F__ && defined GCCMACRO__AVX512BW__ \
59      && defined GCCMACRO__AVX512CD__ && defined GCCMACRO__AVX512DQ__ \
60      && defined GCCMACRO__AVX512VL__
61       if (!(cpu_features->isa_1 & GNU_PROPERTY_X86_ISA_1_V4))
62 	_dl_fatal_printf ("\
63 Fatal glibc error: CPU does not support x86-64-v%d\n", 4);
64 #    endif /* ISA level 4 */
65 #   endif /* ISA level 3 */
66 #  endif /* ISA level 2 */
67 # endif /* IS_IN (rtld) */
68     }
69 }
70 
71 __ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
72 	 _dl_x86_init_cpu_features);
73 #endif
74 
75 #undef _dl_x86_get_cpu_features
76 
77 const struct cpu_features *
_dl_x86_get_cpu_features(void)78 _dl_x86_get_cpu_features (void)
79 {
80   return &GLRO(dl_x86_cpu_features);
81 }
82