hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sysdeps/x86/cpu-tunables.c

Packit 6c4009
/* x86 CPU feature tuning.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#if HAVE_TUNABLES
Packit 6c4009
# define TUNABLE_NAMESPACE tune
Packit 6c4009
# include <stdbool.h>
Packit 6c4009
# include <stdint.h>
Packit 6c4009
# include <unistd.h>		/* Get STDOUT_FILENO for _dl_printf.  */
Packit 6c4009
# include <elf/dl-tunables.h>
Packit 6c4009
# include <string.h>
Packit 6c4009
# include <cpu-features.h>
Packit 6c4009
# include <ldsodefs.h>
Packit 6c4009
Packit 6c4009
/* We can't use IFUNC memcmp nor strlen in init_cpu_features from libc.a
Packit 6c4009
   since IFUNC must be set up by init_cpu_features.  */
Packit 6c4009
# if defined USE_MULTIARCH && !defined SHARED
Packit 6c4009
#  ifdef __x86_64__
Packit 6c4009
#   define DEFAULT_MEMCMP	__memcmp_sse2
Packit 6c4009
#  else
Packit 6c4009
#   define DEFAULT_MEMCMP	__memcmp_ia32
Packit 6c4009
#  endif
Packit 6c4009
extern __typeof (memcmp) DEFAULT_MEMCMP;
Packit 6c4009
# else
Packit 6c4009
#  define DEFAULT_MEMCMP	memcmp
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
# define CHECK_GLIBC_IFUNC_CPU_OFF(f, cpu_features, name, len)		\
Packit 6c4009
  _Static_assert (sizeof (#name) - 1 == len, #name " != " #len);	\
Packit 6c4009
  if (!DEFAULT_MEMCMP (f, #name, len))					\
Packit 6c4009
    {									\
Packit 6c4009
      cpu_features->cpuid[index_cpu_##name].reg_##name			\
Packit 6c4009
	&= ~bit_cpu_##name;						\
Packit 6c4009
      break;								\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
/* Disable an ARCH feature NAME.  We don't enable an ARCH feature which
Packit 6c4009
   isn't available.  */
Packit 6c4009
# define CHECK_GLIBC_IFUNC_ARCH_OFF(f, cpu_features, name, len)		\
Packit 6c4009
  _Static_assert (sizeof (#name) - 1 == len, #name " != " #len);	\
Packit 6c4009
  if (!DEFAULT_MEMCMP (f, #name, len))					\
Packit 6c4009
    {									\
Packit 6c4009
      cpu_features->feature[index_arch_##name]				\
Packit 6c4009
	&= ~bit_arch_##name;						\
Packit 6c4009
      break;								\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
/* Enable/disable an ARCH feature NAME.  */
Packit 6c4009
# define CHECK_GLIBC_IFUNC_ARCH_BOTH(f, cpu_features, name, disable,	\
Packit 6c4009
				    len)				\
Packit 6c4009
  _Static_assert (sizeof (#name) - 1 == len, #name " != " #len);	\
Packit 6c4009
  if (!DEFAULT_MEMCMP (f, #name, len))					\
Packit 6c4009
    {									\
Packit 6c4009
      if (disable)							\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  &= ~bit_arch_##name;						\
Packit 6c4009
      else								\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  |= bit_arch_##name;						\
Packit 6c4009
      break;								\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
/* Enable/disable an ARCH feature NAME.  Enable an ARCH feature only
Packit 6c4009
   if the ARCH feature NEED is also enabled.  */
Packit 6c4009
# define CHECK_GLIBC_IFUNC_ARCH_NEED_ARCH_BOTH(f, cpu_features, name,	\
Packit 6c4009
					       need, disable, len)	\
Packit 6c4009
  _Static_assert (sizeof (#name) - 1 == len, #name " != " #len);	\
Packit 6c4009
  if (!DEFAULT_MEMCMP (f, #name, len))					\
Packit 6c4009
    {									\
Packit 6c4009
      if (disable)							\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  &= ~bit_arch_##name;						\
Packit 6c4009
      else if (CPU_FEATURES_ARCH_P (cpu_features, need))		\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  |= bit_arch_##name;						\
Packit 6c4009
      break;								\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
/* Enable/disable an ARCH feature NAME.  Enable an ARCH feature only
Packit 6c4009
   if the CPU feature NEED is also enabled.  */
Packit 6c4009
# define CHECK_GLIBC_IFUNC_ARCH_NEED_CPU_BOTH(f, cpu_features, name,	\
Packit 6c4009
					      need, disable, len)	\
Packit 6c4009
  _Static_assert (sizeof (#name) - 1 == len, #name " != " #len);	\
Packit 6c4009
  if (!DEFAULT_MEMCMP (f, #name, len))					\
Packit 6c4009
    {									\
Packit 6c4009
      if (disable)							\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  &= ~bit_arch_##name;						\
Packit 6c4009
      else if (CPU_FEATURES_CPU_P (cpu_features, need))			\
Packit 6c4009
	cpu_features->feature[index_arch_##name]			\
Packit 6c4009
	  |= bit_arch_##name;						\
Packit 6c4009
      break;								\
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
attribute_hidden
Packit 6c4009
void
Packit 6c4009
TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
Packit 6c4009
{
Packit 6c4009
  /* The current IFUNC selection is based on microbenchmarks in glibc.
Packit 6c4009
     It should give the best performance for most workloads.  But other
Packit 6c4009
     choices may have better performance for a particular workload or on
Packit 6c4009
     the hardware which wasn't available when the selection was made.
Packit 6c4009
     The environment variable:
Packit 6c4009
Packit 6c4009
     GLIBC_TUNABLES=glibc.tune.hwcaps=-xxx,yyy,-zzz,....
Packit 6c4009
Packit 6c4009
     can be used to enable CPU/ARCH feature yyy, disable CPU/ARCH feature
Packit 6c4009
     yyy and zzz, where the feature name is case-sensitive and has to
Packit 6c4009
     match the ones in cpu-features.h.  It can be used by glibc developers
Packit 6c4009
     to tune for a new processor or override the IFUNC selection to
Packit 6c4009
     improve performance for a particular workload.
Packit 6c4009
Packit 6c4009
     NOTE: the IFUNC selection may change over time.  Please check all
Packit 6c4009
     multiarch implementations when experimenting.  */
Packit 6c4009
Packit 6c4009
  const char *p = valp->strval;
Packit 6c4009
  struct cpu_features *cpu_features = &GLRO(dl_x86_cpu_features);
Packit 6c4009
  size_t len;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      const char *c, *n;
Packit 6c4009
      bool disable;
Packit 6c4009
      size_t nl;
Packit 6c4009
Packit 6c4009
      for (c = p; *c != ','; c++)
Packit 6c4009
	if (*c == '\0')
Packit 6c4009
	  break;
Packit 6c4009
Packit 6c4009
      len = c - p;
Packit 6c4009
      disable = *p == '-';
Packit 6c4009
      if (disable)
Packit 6c4009
	{
Packit 6c4009
	  n = p + 1;
Packit 6c4009
	  nl = len - 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  n = p;
Packit 6c4009
	  nl = len;
Packit 6c4009
	}
Packit 6c4009
      switch (nl)
Packit 6c4009
	{
Packit 6c4009
	default:
Packit 6c4009
	  break;
Packit 6c4009
	case 3:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX, 3);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, CX8, 3);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, FMA, 3);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, HTT, 3);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, IBT, 3);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, RTM, 3);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 4:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX2, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, BMI1, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, BMI2, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, CMOV, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, ERMS, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, FMA4, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE2, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, I586, 4);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, I686, 4);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 5:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, LZCNT, 5);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, MOVBE, 5);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SHSTK, 5);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSSE3, 5);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 6:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, POPCNT, 6);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE4_1, 6);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, SSE4_2, 6);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 7:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512F, 7);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, OSXSAVE, 7);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 8:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512CD, 8);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512BW, 8);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512DQ, 8);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512ER, 8);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512PF, 8);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_CPU_OFF (n, cpu_features, AVX512VL, 8);
Packit 6c4009
	    }
Packit 6c4009
	  CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features, Slow_BSF,
Packit 6c4009
				       disable, 8);
Packit 6c4009
	  break;
Packit 6c4009
	case 10:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, AVX_Usable,
Packit 6c4009
					  10);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, FMA_Usable,
Packit 6c4009
					  10);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 11:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, AVX2_Usable,
Packit 6c4009
					  11);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features, FMA4_Usable,
Packit 6c4009
					  11);
Packit 6c4009
	    }
Packit 6c4009
	  CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features, Prefer_ERMS,
Packit 6c4009
				       disable, 11);
Packit 6c4009
	  CHECK_GLIBC_IFUNC_ARCH_NEED_CPU_BOTH (n, cpu_features,
Packit 6c4009
						Slow_SSE4_2, SSE4_2,
Packit 6c4009
						disable, 11);
Packit 6c4009
	  CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features, Prefer_FSRM,
Packit 6c4009
				       disable, 11);
Packit 6c4009
	  break;
Packit 6c4009
	case 13:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      /* Update xsave_state_size to XSAVE state size.  */
Packit 6c4009
	      cpu_features->xsave_state_size
Packit 6c4009
		= cpu_features->xsave_state_full_size;
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features,
Packit 6c4009
					  XSAVEC_Usable, 13);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 14:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features,
Packit 6c4009
					  AVX512F_Usable, 14);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 15:
Packit 6c4009
	  if (disable)
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_OFF (n, cpu_features,
Packit 6c4009
					  AVX512DQ_Usable, 15);
Packit 6c4009
	    }
Packit 6c4009
	  CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features, Fast_Rep_String,
Packit 6c4009
				       disable, 15);
Packit 6c4009
	  break;
Packit 6c4009
	case 16:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_NEED_ARCH_BOTH
Packit 6c4009
		(n, cpu_features, Prefer_No_AVX512, AVX512F_Usable,
Packit 6c4009
		 disable, 16);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 18:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features,
Packit 6c4009
					   Fast_Copy_Backward, disable,
Packit 6c4009
					   18);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 19:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features,
Packit 6c4009
					   Fast_Unaligned_Load, disable,
Packit 6c4009
					   19);
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features,
Packit 6c4009
					   Fast_Unaligned_Copy, disable,
Packit 6c4009
					   19);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 20:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_NEED_ARCH_BOTH
Packit 6c4009
		(n, cpu_features, Prefer_No_VZEROUPPER, AVX_Usable,
Packit 6c4009
		 disable, 20);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 21:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_BOTH (n, cpu_features,
Packit 6c4009
					   Prefer_MAP_32BIT_EXEC, disable,
Packit 6c4009
					   21);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 23:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_NEED_ARCH_BOTH
Packit 6c4009
		(n, cpu_features, AVX_Fast_Unaligned_Load, AVX_Usable,
Packit 6c4009
		 disable, 23);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 24:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_NEED_ARCH_BOTH
Packit 6c4009
		(n, cpu_features, MathVec_Prefer_No_AVX512,
Packit 6c4009
		 AVX512F_Usable, disable, 24);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	case 26:
Packit 6c4009
	    {
Packit 6c4009
	      CHECK_GLIBC_IFUNC_ARCH_NEED_CPU_BOTH
Packit 6c4009
		(n, cpu_features, Prefer_PMINUB_for_stringop, SSE2,
Packit 6c4009
		 disable, 26);
Packit 6c4009
	    }
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      p += len + 1;
Packit 6c4009
    }
Packit 6c4009
  while (*p != '\0');
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
# if CET_ENABLED
Packit 6c4009
Packit 6c4009
attribute_hidden
Packit 6c4009
void
Packit 6c4009
TUNABLE_CALLBACK (set_x86_ibt) (tunable_val_t *valp)
Packit 6c4009
{
Packit 6c4009
  if (DEFAULT_MEMCMP (valp->strval, "on", sizeof ("on")) == 0)
85cee3
    GL(dl_x86_feature_control).ibt = cet_always_on;
Packit 6c4009
  else if (DEFAULT_MEMCMP (valp->strval, "off", sizeof ("off")) == 0)
85cee3
    GL(dl_x86_feature_control).ibt = cet_always_off;
Packit 6c4009
  else if (DEFAULT_MEMCMP (valp->strval, "permissive",
Packit 6c4009
			   sizeof ("permissive")) == 0)
85cee3
    GL(dl_x86_feature_control).ibt = cet_permissive;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
attribute_hidden
Packit 6c4009
void
Packit 6c4009
TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *valp)
Packit 6c4009
{
Packit 6c4009
  if (DEFAULT_MEMCMP (valp->strval, "on", sizeof ("on")) == 0)
85cee3
    GL(dl_x86_feature_control).shstk = cet_always_on;
Packit 6c4009
  else if (DEFAULT_MEMCMP (valp->strval, "off", sizeof ("off")) == 0)
85cee3
    GL(dl_x86_feature_control).shstk = cet_always_off;
Packit 6c4009
  else if (DEFAULT_MEMCMP (valp->strval, "permissive",
Packit 6c4009
			   sizeof ("permissive")) == 0)
85cee3
    GL(dl_x86_feature_control).shstk = cet_permissive;
Packit 6c4009
}
Packit 6c4009
# endif
Packit 6c4009
#endif