dl-get-cpu-features.c 3.2 KB

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