b1dca6
commit 0f09154c64005e78b61484ae87b5ea2028051ea0
b1dca6
Author: H.J. Lu <hjl.tools@gmail.com>
b1dca6
Date:   Sat Jul 4 06:35:49 2020 -0700
b1dca6
b1dca6
    x86: Initialize CPU info via IFUNC relocation [BZ 26203]
b1dca6
    
b1dca6
    X86 CPU features in ld.so are initialized by init_cpu_features, which is
b1dca6
    invoked by DL_PLATFORM_INIT from _dl_sysdep_start.  But when ld.so is
b1dca6
    loaded by static executable, DL_PLATFORM_INIT is never called.  Also
b1dca6
    x86 cache info in libc.o and libc.a is initialized by a constructor
b1dca6
    which may be called too late.  Since some fields in _rtld_global_ro
b1dca6
    in ld.so are initialized by dynamic relocation, we can also initialize
b1dca6
    x86 CPU features in _rtld_global_ro in ld.so and cache info in libc.so
b1dca6
    by initializing dummy function pointers in ld.so and libc.so via IFUNC
b1dca6
    relocation.
b1dca6
    
b1dca6
    Key points:
b1dca6
    
b1dca6
    1. IFUNC is always supported, independent of --enable-multi-arch or
b1dca6
    --disable-multi-arch.  Linker generates IFUNC relocations from input
b1dca6
    IFUNC objects and ld.so performs IFUNC relocations.
b1dca6
    2. There are no IFUNC dependencies in ld.so before dynamic relocation
b1dca6
    have been performed,
b1dca6
    3. The x86 CPU features in ld.so is initialized by DL_PLATFORM_INIT
b1dca6
    in dynamic executable and by IFUNC relocation in dlopen in static
b1dca6
    executable.
b1dca6
    4. The x86 cache info in libc.o is initialized by IFUNC relocation.
b1dca6
    5. In libc.a, both x86 CPU features and cache info are initialized from
b1dca6
    ARCH_INIT_CPU_FEATURES, not by IFUNC relocation, before __libc_early_init
b1dca6
    is called.
b1dca6
    
b1dca6
    Note: _dl_x86_init_cpu_features can be called more than once from
b1dca6
    DL_PLATFORM_INIT and during relocation in ld.so.
b1dca6
b1dca6
Conflicts:
b1dca6
	sysdeps/x86/cacheinfo.c
b1dca6
	  (Copyright year difference, and AMD Zen cache computation
b1dca6
	   backports downstream.  These changes were reapplied to
b1dca6
	   sysdeps/x86/cacheinfo.h, mirroring the upstream refactoring
b1dca6
	   in the backported commit.)
b1dca6
	sysdeps/x86/dl-get-cpu-features.c
b1dca6
	  (Copyright year difference.)
b1dca6
b1dca6
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
b1dca6
index 8c959e39457c8c41..e5776ef7bc8ad749 100644
b1dca6
--- a/sysdeps/i386/dl-machine.h
b1dca6
+++ b/sysdeps/i386/dl-machine.h
b1dca6
@@ -25,7 +25,6 @@
b1dca6
 #include <sysdep.h>
b1dca6
 #include <tls.h>
b1dca6
 #include <dl-tlsdesc.h>
b1dca6
-#include <cpu-features.c>
b1dca6
 
b1dca6
 /* Return nonzero iff ELF header is compatible with the running host.  */
b1dca6
 static inline int __attribute__ ((unused))
b1dca6
@@ -248,9 +247,9 @@ static inline void __attribute__ ((unused))
b1dca6
 dl_platform_init (void)
b1dca6
 {
b1dca6
 #if IS_IN (rtld)
b1dca6
-  /* init_cpu_features has been called early from __libc_start_main in
b1dca6
-     static executable.  */
b1dca6
-  init_cpu_features (&GLRO(dl_x86_cpu_features));
b1dca6
+  /* _dl_x86_init_cpu_features is a wrapper for init_cpu_features which
b1dca6
+     has been called early from __libc_start_main in static executable.  */
b1dca6
+  _dl_x86_init_cpu_features ();
b1dca6
 #else
b1dca6
   if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
b1dca6
     /* Avoid an empty string which would disturb us.  */
b1dca6
diff --git a/sysdeps/x86/cacheinfo.c b/sysdeps/x86/cacheinfo.c
b1dca6
index fdfe2684759d968c..84b10f6dd8d23a51 100644
b1dca6
--- a/sysdeps/x86/cacheinfo.c
b1dca6
+++ b/sysdeps/x86/cacheinfo.c
b1dca6
@@ -1,5 +1,5 @@
b1dca6
-/* x86_64 cache info.
b1dca6
-   Copyright (C) 2003-2018 Free Software Foundation, Inc.
b1dca6
+/* x86 cache info.
b1dca6
+   Copyright (C) 2003-2020 Free Software Foundation, Inc.
b1dca6
    This file is part of the GNU C Library.
b1dca6
 
b1dca6
    The GNU C Library is free software; you can redistribute it and/or
b1dca6
@@ -19,473 +19,10 @@
b1dca6
 #if IS_IN (libc)
b1dca6
 
b1dca6
 #include <assert.h>
b1dca6
-#include <stdbool.h>
b1dca6
-#include <stdlib.h>
b1dca6
 #include <unistd.h>
b1dca6
 #include <cpuid.h>
b1dca6
-#include <init-arch.h>
b1dca6
-
b1dca6
-static const struct intel_02_cache_info
b1dca6
-{
b1dca6
-  unsigned char idx;
b1dca6
-  unsigned char assoc;
b1dca6
-  unsigned char linesize;
b1dca6
-  unsigned char rel_name;
b1dca6
-  unsigned int size;
b1dca6
-} intel_02_known [] =
b1dca6
-  {
b1dca6
-#define M(sc) ((sc) - _SC_LEVEL1_ICACHE_SIZE)
b1dca6
-    { 0x06,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),    8192 },
b1dca6
-    { 0x08,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),   16384 },
b1dca6
-    { 0x09,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),   32768 },
b1dca6
-    { 0x0a,  2, 32, M(_SC_LEVEL1_DCACHE_SIZE),    8192 },
b1dca6
-    { 0x0c,  4, 32, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
-    { 0x0d,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
-    { 0x0e,  6, 64, M(_SC_LEVEL1_DCACHE_SIZE),   24576 },
b1dca6
-    { 0x21,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x22,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),   524288 },
b1dca6
-    { 0x23,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
-    { 0x25,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
-    { 0x29,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
-    { 0x2c,  8, 64, M(_SC_LEVEL1_DCACHE_SIZE),   32768 },
b1dca6
-    { 0x30,  8, 64, M(_SC_LEVEL1_ICACHE_SIZE),   32768 },
b1dca6
-    { 0x39,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
-    { 0x3a,  6, 64, M(_SC_LEVEL2_CACHE_SIZE),   196608 },
b1dca6
-    { 0x3b,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
-    { 0x3c,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x3d,  6, 64, M(_SC_LEVEL2_CACHE_SIZE),   393216 },
b1dca6
-    { 0x3e,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x3f,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x41,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
-    { 0x42,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x43,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x44,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
-    { 0x45,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
-    { 0x46,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
-    { 0x47,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
-    { 0x48, 12, 64, M(_SC_LEVEL2_CACHE_SIZE),  3145728 },
b1dca6
-    { 0x49, 16, 64, M(_SC_LEVEL2_CACHE_SIZE),  4194304 },
b1dca6
-    { 0x4a, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  6291456 },
b1dca6
-    { 0x4b, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
-    { 0x4c, 12, 64, M(_SC_LEVEL3_CACHE_SIZE), 12582912 },
b1dca6
-    { 0x4d, 16, 64, M(_SC_LEVEL3_CACHE_SIZE), 16777216 },
b1dca6
-    { 0x4e, 24, 64, M(_SC_LEVEL2_CACHE_SIZE),  6291456 },
b1dca6
-    { 0x60,  8, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
-    { 0x66,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),    8192 },
b1dca6
-    { 0x67,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
-    { 0x68,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   32768 },
b1dca6
-    { 0x78,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
-    { 0x79,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
-    { 0x7a,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x7b,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x7c,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
-    { 0x7d,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
-    { 0x7f,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x80,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x82,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
-    { 0x83,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x84,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
-    { 0x85,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
-    { 0x86,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
-    { 0x87,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
-    { 0xd0,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),   524288 },
b1dca6
-    { 0xd1,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
-    { 0xd2,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
-    { 0xd6,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
-    { 0xd7,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
-    { 0xd8,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
-    { 0xdc, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
-    { 0xdd, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
-    { 0xde, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
-    { 0xe2, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
-    { 0xe3, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
-    { 0xe4, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
-    { 0xea, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 12582912 },
b1dca6
-    { 0xeb, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 18874368 },
b1dca6
-    { 0xec, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 25165824 },
b1dca6
-  };
b1dca6
-
b1dca6
-#define nintel_02_known (sizeof (intel_02_known) / sizeof (intel_02_known [0]))
b1dca6
-
b1dca6
-static int
b1dca6
-intel_02_known_compare (const void *p1, const void *p2)
b1dca6
-{
b1dca6
-  const struct intel_02_cache_info *i1;
b1dca6
-  const struct intel_02_cache_info *i2;
b1dca6
-
b1dca6
-  i1 = (const struct intel_02_cache_info *) p1;
b1dca6
-  i2 = (const struct intel_02_cache_info *) p2;
b1dca6
-
b1dca6
-  if (i1->idx == i2->idx)
b1dca6
-    return 0;
b1dca6
-
b1dca6
-  return i1->idx < i2->idx ? -1 : 1;
b1dca6
-}
b1dca6
-
b1dca6
-
b1dca6
-static long int
b1dca6
-__attribute__ ((noinline))
b1dca6
-intel_check_word (int name, unsigned int value, bool *has_level_2,
b1dca6
-		  bool *no_level_2_or_3,
b1dca6
-		  const struct cpu_features *cpu_features)
b1dca6
-{
b1dca6
-  if ((value & 0x80000000) != 0)
b1dca6
-    /* The register value is reserved.  */
b1dca6
-    return 0;
b1dca6
-
b1dca6
-  /* Fold the name.  The _SC_ constants are always in the order SIZE,
b1dca6
-     ASSOC, LINESIZE.  */
b1dca6
-  int folded_rel_name = (M(name) / 3) * 3;
b1dca6
-
b1dca6
-  while (value != 0)
b1dca6
-    {
b1dca6
-      unsigned int byte = value & 0xff;
b1dca6
-
b1dca6
-      if (byte == 0x40)
b1dca6
-	{
b1dca6
-	  *no_level_2_or_3 = true;
b1dca6
-
b1dca6
-	  if (folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
-	    /* No need to look further.  */
b1dca6
-	    break;
b1dca6
-	}
b1dca6
-      else if (byte == 0xff)
b1dca6
-	{
b1dca6
-	  /* CPUID leaf 0x4 contains all the information.  We need to
b1dca6
-	     iterate over it.  */
b1dca6
-	  unsigned int eax;
b1dca6
-	  unsigned int ebx;
b1dca6
-	  unsigned int ecx;
b1dca6
-	  unsigned int edx;
b1dca6
-
b1dca6
-	  unsigned int round = 0;
b1dca6
-	  while (1)
b1dca6
-	    {
b1dca6
-	      __cpuid_count (4, round, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-	      enum { null = 0, data = 1, inst = 2, uni = 3 } type = eax & 0x1f;
b1dca6
-	      if (type == null)
b1dca6
-		/* That was the end.  */
b1dca6
-		break;
b1dca6
-
b1dca6
-	      unsigned int level = (eax >> 5) & 0x7;
b1dca6
-
b1dca6
-	      if ((level == 1 && type == data
b1dca6
-		   && folded_rel_name == M(_SC_LEVEL1_DCACHE_SIZE))
b1dca6
-		  || (level == 1 && type == inst
b1dca6
-		      && folded_rel_name == M(_SC_LEVEL1_ICACHE_SIZE))
b1dca6
-		  || (level == 2 && folded_rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
-		  || (level == 3 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
-		  || (level == 4 && folded_rel_name == M(_SC_LEVEL4_CACHE_SIZE)))
b1dca6
-		{
b1dca6
-		  unsigned int offset = M(name) - folded_rel_name;
b1dca6
-
b1dca6
-		  if (offset == 0)
b1dca6
-		    /* Cache size.  */
b1dca6
-		    return (((ebx >> 22) + 1)
b1dca6
-			    * (((ebx >> 12) & 0x3ff) + 1)
b1dca6
-			    * ((ebx & 0xfff) + 1)
b1dca6
-			    * (ecx + 1));
b1dca6
-		  if (offset == 1)
b1dca6
-		    return (ebx >> 22) + 1;
b1dca6
-
b1dca6
-		  assert (offset == 2);
b1dca6
-		  return (ebx & 0xfff) + 1;
b1dca6
-		}
b1dca6
-
b1dca6
-	      ++round;
b1dca6
-	    }
b1dca6
-	  /* There is no other cache information anywhere else.  */
b1dca6
-	  break;
b1dca6
-	}
b1dca6
-      else
b1dca6
-	{
b1dca6
-	  if (byte == 0x49 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
-	    {
b1dca6
-	      /* Intel reused this value.  For family 15, model 6 it
b1dca6
-		 specifies the 3rd level cache.  Otherwise the 2nd
b1dca6
-		 level cache.  */
b1dca6
-	      unsigned int family = cpu_features->basic.family;
b1dca6
-	      unsigned int model = cpu_features->basic.model;
b1dca6
-
b1dca6
-	      if (family == 15 && model == 6)
b1dca6
-		{
b1dca6
-		  /* The level 3 cache is encoded for this model like
b1dca6
-		     the level 2 cache is for other models.  Pretend
b1dca6
-		     the caller asked for the level 2 cache.  */
b1dca6
-		  name = (_SC_LEVEL2_CACHE_SIZE
b1dca6
-			  + (name - _SC_LEVEL3_CACHE_SIZE));
b1dca6
-		  folded_rel_name = M(_SC_LEVEL2_CACHE_SIZE);
b1dca6
-		}
b1dca6
-	    }
b1dca6
-
b1dca6
-	  struct intel_02_cache_info *found;
b1dca6
-	  struct intel_02_cache_info search;
b1dca6
-
b1dca6
-	  search.idx = byte;
b1dca6
-	  found = bsearch (&search, intel_02_known, nintel_02_known,
b1dca6
-			   sizeof (intel_02_known[0]), intel_02_known_compare);
b1dca6
-	  if (found != NULL)
b1dca6
-	    {
b1dca6
-	      if (found->rel_name == folded_rel_name)
b1dca6
-		{
b1dca6
-		  unsigned int offset = M(name) - folded_rel_name;
b1dca6
-
b1dca6
-		  if (offset == 0)
b1dca6
-		    /* Cache size.  */
b1dca6
-		    return found->size;
b1dca6
-		  if (offset == 1)
b1dca6
-		    return found->assoc;
b1dca6
-
b1dca6
-		  assert (offset == 2);
b1dca6
-		  return found->linesize;
b1dca6
-		}
b1dca6
-
b1dca6
-	      if (found->rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
-		*has_level_2 = true;
b1dca6
-	    }
b1dca6
-	}
b1dca6
-
b1dca6
-      /* Next byte for the next round.  */
b1dca6
-      value >>= 8;
b1dca6
-    }
b1dca6
-
b1dca6
-  /* Nothing found.  */
b1dca6
-  return 0;
b1dca6
-}
b1dca6
-
b1dca6
-
b1dca6
-static long int __attribute__ ((noinline))
b1dca6
-handle_intel (int name, const struct cpu_features *cpu_features)
b1dca6
-{
b1dca6
-  unsigned int maxidx = cpu_features->basic.max_cpuid;
b1dca6
-
b1dca6
-  /* Return -1 for older CPUs.  */
b1dca6
-  if (maxidx < 2)
b1dca6
-    return -1;
b1dca6
-
b1dca6
-  /* OK, we can use the CPUID instruction to get all info about the
b1dca6
-     caches.  */
b1dca6
-  unsigned int cnt = 0;
b1dca6
-  unsigned int max = 1;
b1dca6
-  long int result = 0;
b1dca6
-  bool no_level_2_or_3 = false;
b1dca6
-  bool has_level_2 = false;
b1dca6
-
b1dca6
-  while (cnt++ < max)
b1dca6
-    {
b1dca6
-      unsigned int eax;
b1dca6
-      unsigned int ebx;
b1dca6
-      unsigned int ecx;
b1dca6
-      unsigned int edx;
b1dca6
-      __cpuid (2, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-      /* The low byte of EAX in the first round contain the number of
b1dca6
-	 rounds we have to make.  At least one, the one we are already
b1dca6
-	 doing.  */
b1dca6
-      if (cnt == 1)
b1dca6
-	{
b1dca6
-	  max = eax & 0xff;
b1dca6
-	  eax &= 0xffffff00;
b1dca6
-	}
b1dca6
-
b1dca6
-      /* Process the individual registers' value.  */
b1dca6
-      result = intel_check_word (name, eax, &has_level_2,
b1dca6
-				 &no_level_2_or_3, cpu_features);
b1dca6
-      if (result != 0)
b1dca6
-	return result;
b1dca6
-
b1dca6
-      result = intel_check_word (name, ebx, &has_level_2,
b1dca6
-				 &no_level_2_or_3, cpu_features);
b1dca6
-      if (result != 0)
b1dca6
-	return result;
b1dca6
-
b1dca6
-      result = intel_check_word (name, ecx, &has_level_2,
b1dca6
-				 &no_level_2_or_3, cpu_features);
b1dca6
-      if (result != 0)
b1dca6
-	return result;
b1dca6
-
b1dca6
-      result = intel_check_word (name, edx, &has_level_2,
b1dca6
-				 &no_level_2_or_3, cpu_features);
b1dca6
-      if (result != 0)
b1dca6
-	return result;
b1dca6
-    }
b1dca6
-
b1dca6
-  if (name >= _SC_LEVEL2_CACHE_SIZE && name <= _SC_LEVEL3_CACHE_LINESIZE
b1dca6
-      && no_level_2_or_3)
b1dca6
-    return -1;
b1dca6
-
b1dca6
-  return 0;
b1dca6
-}
b1dca6
-
b1dca6
-
b1dca6
-static long int __attribute__ ((noinline))
b1dca6
-handle_amd (int name)
b1dca6
-{
b1dca6
-  unsigned int eax;
b1dca6
-  unsigned int ebx;
b1dca6
-  unsigned int ecx;
b1dca6
-  unsigned int edx;
b1dca6
-  __cpuid (0x80000000, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-  /* No level 4 cache (yet).  */
b1dca6
-  if (name > _SC_LEVEL3_CACHE_LINESIZE)
b1dca6
-    return 0;
b1dca6
-
b1dca6
-  unsigned int fn = 0x80000005 + (name >= _SC_LEVEL2_CACHE_SIZE);
b1dca6
-  if (eax < fn)
b1dca6
-    return 0;
b1dca6
-
b1dca6
-  __cpuid (fn, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-  if (name < _SC_LEVEL1_DCACHE_SIZE)
b1dca6
-    {
b1dca6
-      name += _SC_LEVEL1_DCACHE_SIZE - _SC_LEVEL1_ICACHE_SIZE;
b1dca6
-      ecx = edx;
b1dca6
-    }
b1dca6
-
b1dca6
-  switch (name)
b1dca6
-    {
b1dca6
-    case _SC_LEVEL1_DCACHE_SIZE:
b1dca6
-      return (ecx >> 14) & 0x3fc00;
b1dca6
-
b1dca6
-    case _SC_LEVEL1_DCACHE_ASSOC:
b1dca6
-      ecx >>= 16;
b1dca6
-      if ((ecx & 0xff) == 0xff)
b1dca6
-	/* Fully associative.  */
b1dca6
-	return (ecx << 2) & 0x3fc00;
b1dca6
-      return ecx & 0xff;
b1dca6
-
b1dca6
-    case _SC_LEVEL1_DCACHE_LINESIZE:
b1dca6
-      return ecx & 0xff;
b1dca6
-
b1dca6
-    case _SC_LEVEL2_CACHE_SIZE:
b1dca6
-      return (ecx & 0xf000) == 0 ? 0 : (ecx >> 6) & 0x3fffc00;
b1dca6
-
b1dca6
-    case _SC_LEVEL2_CACHE_ASSOC:
b1dca6
-      switch ((ecx >> 12) & 0xf)
b1dca6
-	{
b1dca6
-	case 0:
b1dca6
-	case 1:
b1dca6
-	case 2:
b1dca6
-	case 4:
b1dca6
-	  return (ecx >> 12) & 0xf;
b1dca6
-	case 6:
b1dca6
-	  return 8;
b1dca6
-	case 8:
b1dca6
-	  return 16;
b1dca6
-	case 10:
b1dca6
-	  return 32;
b1dca6
-	case 11:
b1dca6
-	  return 48;
b1dca6
-	case 12:
b1dca6
-	  return 64;
b1dca6
-	case 13:
b1dca6
-	  return 96;
b1dca6
-	case 14:
b1dca6
-	  return 128;
b1dca6
-	case 15:
b1dca6
-	  return ((ecx >> 6) & 0x3fffc00) / (ecx & 0xff);
b1dca6
-	default:
b1dca6
-	  return 0;
b1dca6
-	}
b1dca6
-      /* NOTREACHED */
b1dca6
-
b1dca6
-    case _SC_LEVEL2_CACHE_LINESIZE:
b1dca6
-      return (ecx & 0xf000) == 0 ? 0 : ecx & 0xff;
b1dca6
-
b1dca6
-    case _SC_LEVEL3_CACHE_SIZE:
b1dca6
-      return (edx & 0xf000) == 0 ? 0 : (edx & 0x3ffc0000) << 1;
b1dca6
-
b1dca6
-    case _SC_LEVEL3_CACHE_ASSOC:
b1dca6
-      switch ((edx >> 12) & 0xf)
b1dca6
-	{
b1dca6
-	case 0:
b1dca6
-	case 1:
b1dca6
-	case 2:
b1dca6
-	case 4:
b1dca6
-	  return (edx >> 12) & 0xf;
b1dca6
-	case 6:
b1dca6
-	  return 8;
b1dca6
-	case 8:
b1dca6
-	  return 16;
b1dca6
-	case 10:
b1dca6
-	  return 32;
b1dca6
-	case 11:
b1dca6
-	  return 48;
b1dca6
-	case 12:
b1dca6
-	  return 64;
b1dca6
-	case 13:
b1dca6
-	  return 96;
b1dca6
-	case 14:
b1dca6
-	  return 128;
b1dca6
-	case 15:
b1dca6
-	  return ((edx & 0x3ffc0000) << 1) / (edx & 0xff);
b1dca6
-	default:
b1dca6
-	  return 0;
b1dca6
-	}
b1dca6
-      /* NOTREACHED */
b1dca6
-
b1dca6
-    case _SC_LEVEL3_CACHE_LINESIZE:
b1dca6
-      return (edx & 0xf000) == 0 ? 0 : edx & 0xff;
b1dca6
-
b1dca6
-    default:
b1dca6
-      assert (! "cannot happen");
b1dca6
-    }
b1dca6
-  return -1;
b1dca6
-}
b1dca6
-
b1dca6
-
b1dca6
-static long int __attribute__ ((noinline))
b1dca6
-handle_zhaoxin (int name)
b1dca6
-{
b1dca6
-  unsigned int eax;
b1dca6
-  unsigned int ebx;
b1dca6
-  unsigned int ecx;
b1dca6
-  unsigned int edx;
b1dca6
-
b1dca6
-  int folded_rel_name = (M(name) / 3) * 3;
b1dca6
-
b1dca6
-  unsigned int round = 0;
b1dca6
-  while (1)
b1dca6
-    {
b1dca6
-      __cpuid_count (4, round, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-      enum { null = 0, data = 1, inst = 2, uni = 3 } type = eax & 0x1f;
b1dca6
-      if (type == null)
b1dca6
-        break;
b1dca6
-
b1dca6
-      unsigned int level = (eax >> 5) & 0x7;
b1dca6
-
b1dca6
-      if ((level == 1 && type == data
b1dca6
-        && folded_rel_name == M(_SC_LEVEL1_DCACHE_SIZE))
b1dca6
-        || (level == 1 && type == inst
b1dca6
-            && folded_rel_name == M(_SC_LEVEL1_ICACHE_SIZE))
b1dca6
-        || (level == 2 && folded_rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
-        || (level == 3 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE)))
b1dca6
-        {
b1dca6
-          unsigned int offset = M(name) - folded_rel_name;
b1dca6
-
b1dca6
-          if (offset == 0)
b1dca6
-            /* Cache size.  */
b1dca6
-            return (((ebx >> 22) + 1)
b1dca6
-                * (((ebx >> 12) & 0x3ff) + 1)
b1dca6
-                * ((ebx & 0xfff) + 1)
b1dca6
-                * (ecx + 1));
b1dca6
-          if (offset == 1)
b1dca6
-            return (ebx >> 22) + 1;
b1dca6
-
b1dca6
-          assert (offset == 2);
b1dca6
-          return (ebx & 0xfff) + 1;
b1dca6
-        }
b1dca6
-
b1dca6
-      ++round;
b1dca6
-    }
b1dca6
-
b1dca6
-  /* Nothing found.  */
b1dca6
-  return 0;
b1dca6
-}
b1dca6
-
b1dca6
+#include <ldsodefs.h>
b1dca6
+#include <dl-cacheinfo.h>
b1dca6
 
b1dca6
 /* Get the value of the system variable NAME.  */
b1dca6
 long int
b1dca6
@@ -509,409 +46,18 @@ __cache_sysconf (int name)
b1dca6
   return 0;
b1dca6
 }
b1dca6
 
b1dca6
+# ifdef SHARED
b1dca6
+/* NB: In libc.a, cacheinfo.h is included in libc-start.c.  In libc.so,
b1dca6
+   cacheinfo.h is included here and call init_cacheinfo by initializing
b1dca6
+   a dummy function pointer via IFUNC relocation after CPU features in
b1dca6
+   ld.so have been initialized by DL_PLATFORM_INIT or IFUNC relocation.  */
b1dca6
+# include <cacheinfo.h>
b1dca6
+# include <ifunc-init.h>
b1dca6
 
b1dca6
-/* Data cache size for use in memory and string routines, typically
b1dca6
-   L1 size, rounded to multiple of 256 bytes.  */
b1dca6
-long int __x86_data_cache_size_half attribute_hidden = 32 * 1024 / 2;
b1dca6
-long int __x86_data_cache_size attribute_hidden = 32 * 1024;
b1dca6
-/* Similar to __x86_data_cache_size_half, but not rounded.  */
b1dca6
-long int __x86_raw_data_cache_size_half attribute_hidden = 32 * 1024 / 2;
b1dca6
-/* Similar to __x86_data_cache_size, but not rounded.  */
b1dca6
-long int __x86_raw_data_cache_size attribute_hidden = 32 * 1024;
b1dca6
-/* Shared cache size for use in memory and string routines, typically
b1dca6
-   L2 or L3 size, rounded to multiple of 256 bytes.  */
b1dca6
-long int __x86_shared_cache_size_half attribute_hidden = 1024 * 1024 / 2;
b1dca6
-long int __x86_shared_cache_size attribute_hidden = 1024 * 1024;
b1dca6
-/* Similar to __x86_shared_cache_size_half, but not rounded.  */
b1dca6
-long int __x86_raw_shared_cache_size_half attribute_hidden = 1024 * 1024 / 2;
b1dca6
-/* Similar to __x86_shared_cache_size, but not rounded.  */
b1dca6
-long int __x86_raw_shared_cache_size attribute_hidden = 1024 * 1024;
b1dca6
-
b1dca6
-/* Threshold to use non temporal store.  */
b1dca6
-long int __x86_shared_non_temporal_threshold attribute_hidden;
b1dca6
-
b1dca6
-/* Threshold to use Enhanced REP MOVSB.  */
b1dca6
-long int __x86_rep_movsb_threshold attribute_hidden = 2048;
b1dca6
-
b1dca6
-/* Threshold to use Enhanced REP STOSB.  */
b1dca6
-long int __x86_rep_stosb_threshold attribute_hidden = 2048;
b1dca6
-
b1dca6
-
b1dca6
-static void
b1dca6
-get_common_cache_info (long int *shared_ptr, unsigned int *threads_ptr,
b1dca6
-                long int core)
b1dca6
-{
b1dca6
-  unsigned int eax;
b1dca6
-  unsigned int ebx;
b1dca6
-  unsigned int ecx;
b1dca6
-  unsigned int edx;
b1dca6
-
b1dca6
-  /* Number of logical processors sharing L2 cache.  */
b1dca6
-  int threads_l2;
b1dca6
-
b1dca6
-  /* Number of logical processors sharing L3 cache.  */
b1dca6
-  int threads_l3;
b1dca6
-
b1dca6
-  const struct cpu_features *cpu_features = __get_cpu_features ();
b1dca6
-  int max_cpuid = cpu_features->basic.max_cpuid;
b1dca6
-  unsigned int family = cpu_features->basic.family;
b1dca6
-  unsigned int model = cpu_features->basic.model;
b1dca6
-  long int shared = *shared_ptr;
b1dca6
-  unsigned int threads = *threads_ptr;
b1dca6
-  bool inclusive_cache = true;
b1dca6
-  bool support_count_mask = true;
b1dca6
-
b1dca6
-  /* Try L3 first.  */
b1dca6
-  unsigned int level = 3;
b1dca6
-
b1dca6
-  if (cpu_features->basic.kind == arch_kind_zhaoxin && family == 6)
b1dca6
-    support_count_mask = false;
b1dca6
-
b1dca6
-  if (shared <= 0)
b1dca6
-    {
b1dca6
-      /* Try L2 otherwise.  */
b1dca6
-      level  = 2;
b1dca6
-      shared = core;
b1dca6
-      threads_l2 = 0;
b1dca6
-      threads_l3 = -1;
b1dca6
-    }
b1dca6
-  else
b1dca6
-    {
b1dca6
-      threads_l2 = 0;
b1dca6
-      threads_l3 = 0;
b1dca6
-    }
b1dca6
-
b1dca6
-  /* A value of 0 for the HTT bit indicates there is only a single
b1dca6
-     logical processor.  */
b1dca6
-  if (CPU_FEATURE_USABLE (HTT))
b1dca6
-    {
b1dca6
-      /* Figure out the number of logical threads that share the
b1dca6
-         highest cache level.  */
b1dca6
-      if (max_cpuid >= 4)
b1dca6
-        {
b1dca6
-          int i = 0;
b1dca6
-
b1dca6
-          /* Query until cache level 2 and 3 are enumerated.  */
b1dca6
-          int check = 0x1 | (threads_l3 == 0) << 1;
b1dca6
-          do
b1dca6
-            {
b1dca6
-              __cpuid_count (4, i++, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-              /* There seems to be a bug in at least some Pentium Ds
b1dca6
-                 which sometimes fail to iterate all cache parameters.
b1dca6
-                 Do not loop indefinitely here, stop in this case and
b1dca6
-                 assume there is no such information.  */
b1dca6
-              if (cpu_features->basic.kind == arch_kind_intel
b1dca6
-                  && (eax & 0x1f) == 0 )
b1dca6
-                goto intel_bug_no_cache_info;
b1dca6
-
b1dca6
-              switch ((eax >> 5) & 0x7)
b1dca6
-                {
b1dca6
-                  default:
b1dca6
-                    break;
b1dca6
-                  case 2:
b1dca6
-                    if ((check & 0x1))
b1dca6
-                      {
b1dca6
-                        /* Get maximum number of logical processors
b1dca6
-                           sharing L2 cache.  */
b1dca6
-                        threads_l2 = (eax >> 14) & 0x3ff;
b1dca6
-                        check &= ~0x1;
b1dca6
-                      }
b1dca6
-                    break;
b1dca6
-                  case 3:
b1dca6
-                    if ((check & (0x1 << 1)))
b1dca6
-                      {
b1dca6
-                        /* Get maximum number of logical processors
b1dca6
-                           sharing L3 cache.  */
b1dca6
-                        threads_l3 = (eax >> 14) & 0x3ff;
b1dca6
-
b1dca6
-                        /* Check if L2 and L3 caches are inclusive.  */
b1dca6
-                        inclusive_cache = (edx & 0x2) != 0;
b1dca6
-                        check &= ~(0x1 << 1);
b1dca6
-                      }
b1dca6
-                    break;
b1dca6
-                }
b1dca6
-            }
b1dca6
-          while (check);
b1dca6
-
b1dca6
-          /* If max_cpuid >= 11, THREADS_L2/THREADS_L3 are the maximum
b1dca6
-             numbers of addressable IDs for logical processors sharing
b1dca6
-             the cache, instead of the maximum number of threads
b1dca6
-             sharing the cache.  */
b1dca6
-          if (max_cpuid >= 11 && support_count_mask)
b1dca6
-            {
b1dca6
-              /* Find the number of logical processors shipped in
b1dca6
-                 one core and apply count mask.  */
b1dca6
-              i = 0;
b1dca6
-
b1dca6
-              /* Count SMT only if there is L3 cache.  Always count
b1dca6
-                 core if there is no L3 cache.  */
b1dca6
-              int count = ((threads_l2 > 0 && level == 3)
b1dca6
-                           | ((threads_l3 > 0
b1dca6
-                               || (threads_l2 > 0 && level == 2)) << 1));
b1dca6
-
b1dca6
-              while (count)
b1dca6
-                {
b1dca6
-                  __cpuid_count (11, i++, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-                  int shipped = ebx & 0xff;
b1dca6
-                  int type = ecx & 0xff00;
b1dca6
-                  if (shipped == 0 || type == 0)
b1dca6
-                    break;
b1dca6
-                  else if (type == 0x100)
b1dca6
-                    {
b1dca6
-                      /* Count SMT.  */
b1dca6
-                      if ((count & 0x1))
b1dca6
-                        {
b1dca6
-                          int count_mask;
b1dca6
-
b1dca6
-                          /* Compute count mask.  */
b1dca6
-                          asm ("bsr %1, %0"
b1dca6
-                               : "=r" (count_mask) : "g" (threads_l2));
b1dca6
-                          count_mask = ~(-1 << (count_mask + 1));
b1dca6
-                          threads_l2 = (shipped - 1) & count_mask;
b1dca6
-                          count &= ~0x1;
b1dca6
-                        }
b1dca6
-                    }
b1dca6
-                  else if (type == 0x200)
b1dca6
-                    {
b1dca6
-                      /* Count core.  */
b1dca6
-                      if ((count & (0x1 << 1)))
b1dca6
-                        {
b1dca6
-                          int count_mask;
b1dca6
-                          int threads_core
b1dca6
-                            = (level == 2 ? threads_l2 : threads_l3);
b1dca6
-
b1dca6
-                          /* Compute count mask.  */
b1dca6
-                          asm ("bsr %1, %0"
b1dca6
-                               : "=r" (count_mask) : "g" (threads_core));
b1dca6
-                          count_mask = ~(-1 << (count_mask + 1));
b1dca6
-                          threads_core = (shipped - 1) & count_mask;
b1dca6
-                          if (level == 2)
b1dca6
-                            threads_l2 = threads_core;
b1dca6
-                          else
b1dca6
-                            threads_l3 = threads_core;
b1dca6
-                          count &= ~(0x1 << 1);
b1dca6
-                        }
b1dca6
-                    }
b1dca6
-                }
b1dca6
-            }
b1dca6
-          if (threads_l2 > 0)
b1dca6
-            threads_l2 += 1;
b1dca6
-          if (threads_l3 > 0)
b1dca6
-            threads_l3 += 1;
b1dca6
-          if (level == 2)
b1dca6
-            {
b1dca6
-              if (threads_l2)
b1dca6
-                {
b1dca6
-                  threads = threads_l2;
b1dca6
-                  if (cpu_features->basic.kind == arch_kind_intel
b1dca6
-                      && threads > 2
b1dca6
-                      && family == 6)
b1dca6
-                    switch (model)
b1dca6
-                      {
b1dca6
-                        case 0x37:
b1dca6
-                        case 0x4a:
b1dca6
-                        case 0x4d:
b1dca6
-                        case 0x5a:
b1dca6
-                        case 0x5d:
b1dca6
-                          /* Silvermont has L2 cache shared by 2 cores.  */
b1dca6
-                          threads = 2;
b1dca6
-                          break;
b1dca6
-                        default:
b1dca6
-                          break;
b1dca6
-                      }
b1dca6
-                }
b1dca6
-            }
b1dca6
-          else if (threads_l3)
b1dca6
-            threads = threads_l3;
b1dca6
-        }
b1dca6
-      else
b1dca6
-        {
b1dca6
-intel_bug_no_cache_info:
b1dca6
-          /* Assume that all logical threads share the highest cache
b1dca6
-             level.  */
b1dca6
-          threads
b1dca6
-            = ((cpu_features->features[COMMON_CPUID_INDEX_1].cpuid.ebx
b1dca6
-                >> 16) & 0xff);
b1dca6
-        }
b1dca6
-
b1dca6
-        /* Cap usage of highest cache level to the number of supported
b1dca6
-           threads.  */
b1dca6
-        if (shared > 0 && threads > 0)
b1dca6
-          shared /= threads;
b1dca6
-    }
b1dca6
-
b1dca6
-  /* Account for non-inclusive L2 and L3 caches.  */
b1dca6
-  if (!inclusive_cache)
b1dca6
-    {
b1dca6
-      if (threads_l2 > 0)
b1dca6
-        core /= threads_l2;
b1dca6
-      shared += core;
b1dca6
-    }
b1dca6
-
b1dca6
-  *shared_ptr = shared;
b1dca6
-  *threads_ptr = threads;
b1dca6
-}
b1dca6
-
b1dca6
-
b1dca6
-static void
b1dca6
-__attribute__((constructor))
b1dca6
-init_cacheinfo (void)
b1dca6
-{
b1dca6
-  /* Find out what brand of processor.  */
b1dca6
-  unsigned int ebx;
b1dca6
-  unsigned int ecx;
b1dca6
-  unsigned int edx;
b1dca6
-  int max_cpuid_ex;
b1dca6
-  long int data = -1;
b1dca6
-  long int shared = -1;
b1dca6
-  long int core;
b1dca6
-  unsigned int threads = 0;
b1dca6
-  const struct cpu_features *cpu_features = __get_cpu_features ();
b1dca6
-
b1dca6
-  if (cpu_features->basic.kind == arch_kind_intel)
b1dca6
-    {
b1dca6
-      data = handle_intel (_SC_LEVEL1_DCACHE_SIZE, cpu_features);
b1dca6
-      core = handle_intel (_SC_LEVEL2_CACHE_SIZE, cpu_features);
b1dca6
-      shared = handle_intel (_SC_LEVEL3_CACHE_SIZE, cpu_features);
b1dca6
-
b1dca6
-      get_common_cache_info (&shared, &threads, core);
b1dca6
-    }
b1dca6
-  else if (cpu_features->basic.kind == arch_kind_zhaoxin)
b1dca6
-    {
b1dca6
-      data = handle_zhaoxin (_SC_LEVEL1_DCACHE_SIZE);
b1dca6
-      core = handle_zhaoxin (_SC_LEVEL2_CACHE_SIZE);
b1dca6
-      shared = handle_zhaoxin (_SC_LEVEL3_CACHE_SIZE);
b1dca6
-
b1dca6
-      get_common_cache_info (&shared, &threads, core);
b1dca6
-    }
b1dca6
-  else if (cpu_features->basic.kind == arch_kind_amd)
b1dca6
-    {
b1dca6
-      data   = handle_amd (_SC_LEVEL1_DCACHE_SIZE);
b1dca6
-      long int core = handle_amd (_SC_LEVEL2_CACHE_SIZE);
b1dca6
-      shared = handle_amd (_SC_LEVEL3_CACHE_SIZE);
b1dca6
+extern void __x86_cacheinfo (void) attribute_hidden;
b1dca6
+const void (*__x86_cacheinfo_p) (void) attribute_hidden
b1dca6
+  = __x86_cacheinfo;
b1dca6
 
b1dca6
-      /* Get maximum extended function. */
b1dca6
-      __cpuid (0x80000000, max_cpuid_ex, ebx, ecx, edx);
b1dca6
-
b1dca6
-      if (shared <= 0)
b1dca6
-	/* No shared L3 cache.  All we have is the L2 cache.  */
b1dca6
-	shared = core;
b1dca6
-      else
b1dca6
-	{
b1dca6
-	  /* Figure out the number of logical threads that share L3.  */
b1dca6
-	  if (max_cpuid_ex >= 0x80000008)
b1dca6
-	    {
b1dca6
-	      /* Get width of APIC ID.  */
b1dca6
-	      __cpuid (0x80000008, max_cpuid_ex, ebx, ecx, edx);
b1dca6
-	      threads = 1 << ((ecx >> 12) & 0x0f);
b1dca6
-	    }
b1dca6
-
b1dca6
-	  if (threads == 0 || cpu_features->basic.family >= 0x17)
b1dca6
-	    {
b1dca6
-	      /* If APIC ID width is not available, use logical
b1dca6
-		 processor count.  */
b1dca6
-	      __cpuid (0x00000001, max_cpuid_ex, ebx, ecx, edx);
b1dca6
-
b1dca6
-	      if ((edx & (1 << 28)) != 0)
b1dca6
-		threads = (ebx >> 16) & 0xff;
b1dca6
-	    }
b1dca6
-
b1dca6
-	  /* Cap usage of highest cache level to the number of
b1dca6
-	     supported threads.  */
b1dca6
-	  if (threads > 0)
b1dca6
-	    shared /= threads;
b1dca6
-
b1dca6
-	  /* Get shared cache per ccx for Zen architectures.  */
b1dca6
-	  if (cpu_features->basic.family >= 0x17)
b1dca6
-	    {
b1dca6
-	      unsigned int eax;
b1dca6
-
b1dca6
-	      /* Get number of threads share the L3 cache in CCX.  */
b1dca6
-	      __cpuid_count (0x8000001D, 0x3, eax, ebx, ecx, edx);
b1dca6
-
b1dca6
-	      unsigned int threads_per_ccx = ((eax >> 14) & 0xfff) + 1;
b1dca6
-	      shared *= threads_per_ccx;
b1dca6
-	    }
b1dca6
-	  else
b1dca6
-	    {
b1dca6
-	      /* Account for exclusive L2 and L3 caches.  */
b1dca6
-	      shared += core;
b1dca6
-            }
b1dca6
-	}
b1dca6
-    }
b1dca6
-
b1dca6
-  if (cpu_features->data_cache_size != 0)
b1dca6
-    data = cpu_features->data_cache_size;
b1dca6
-
b1dca6
-  if (data > 0)
b1dca6
-    {
b1dca6
-      __x86_raw_data_cache_size_half = data / 2;
b1dca6
-      __x86_raw_data_cache_size = data;
b1dca6
-      /* Round data cache size to multiple of 256 bytes.  */
b1dca6
-      data = data & ~255L;
b1dca6
-      __x86_data_cache_size_half = data / 2;
b1dca6
-      __x86_data_cache_size = data;
b1dca6
-    }
b1dca6
-
b1dca6
-  if (cpu_features->shared_cache_size != 0)
b1dca6
-    shared = cpu_features->shared_cache_size;
b1dca6
-
b1dca6
-  if (shared > 0)
b1dca6
-    {
b1dca6
-      __x86_raw_shared_cache_size_half = shared / 2;
b1dca6
-      __x86_raw_shared_cache_size = shared;
b1dca6
-      /* Round shared cache size to multiple of 256 bytes.  */
b1dca6
-      shared = shared & ~255L;
b1dca6
-      __x86_shared_cache_size_half = shared / 2;
b1dca6
-      __x86_shared_cache_size = shared;
b1dca6
-    }
b1dca6
-
b1dca6
-  /* The default setting for the non_temporal threshold is 3/4 of one
b1dca6
-     thread's share of the chip's cache. For most Intel and AMD processors
b1dca6
-     with an initial release date between 2017 and 2020, a thread's typical
b1dca6
-     share of the cache is from 500 KBytes to 2 MBytes. Using the 3/4
b1dca6
-     threshold leaves 125 KBytes to 500 KBytes of the thread's data
b1dca6
-     in cache after a maximum temporal copy, which will maintain
b1dca6
-     in cache a reasonable portion of the thread's stack and other
b1dca6
-     active data. If the threshold is set higher than one thread's
b1dca6
-     share of the cache, it has a substantial risk of negatively
b1dca6
-     impacting the performance of other threads running on the chip. */
b1dca6
-  __x86_shared_non_temporal_threshold
b1dca6
-    = (cpu_features->non_temporal_threshold != 0
b1dca6
-       ? cpu_features->non_temporal_threshold
b1dca6
-       : __x86_shared_cache_size * 3 / 4);
b1dca6
-
b1dca6
-  /* NB: The REP MOVSB threshold must be greater than VEC_SIZE * 8.  */
b1dca6
-  unsigned int minimum_rep_movsb_threshold;
b1dca6
-  /* NB: The default REP MOVSB threshold is 2048 * (VEC_SIZE / 16).  */
b1dca6
-  unsigned int rep_movsb_threshold;
b1dca6
-  if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F)
b1dca6
-      && !CPU_FEATURE_PREFERRED_P (cpu_features, Prefer_No_AVX512))
b1dca6
-    {
b1dca6
-      rep_movsb_threshold = 2048 * (64 / 16);
b1dca6
-      minimum_rep_movsb_threshold = 64 * 8;
b1dca6
-    }
b1dca6
-  else if (CPU_FEATURE_PREFERRED_P (cpu_features,
b1dca6
-				    AVX_Fast_Unaligned_Load))
b1dca6
-    {
b1dca6
-      rep_movsb_threshold = 2048 * (32 / 16);
b1dca6
-      minimum_rep_movsb_threshold = 32 * 8;
b1dca6
-    }
b1dca6
-  else
b1dca6
-    {
b1dca6
-      rep_movsb_threshold = 2048 * (16 / 16);
b1dca6
-      minimum_rep_movsb_threshold = 16 * 8;
b1dca6
-    }
b1dca6
-  if (cpu_features->rep_movsb_threshold > minimum_rep_movsb_threshold)
b1dca6
-    __x86_rep_movsb_threshold = cpu_features->rep_movsb_threshold;
b1dca6
-  else
b1dca6
-    __x86_rep_movsb_threshold = rep_movsb_threshold;
b1dca6
-
b1dca6
-# if HAVE_TUNABLES
b1dca6
-  __x86_rep_stosb_threshold = cpu_features->rep_stosb_threshold;
b1dca6
+__ifunc (__x86_cacheinfo, __x86_cacheinfo, NULL, void, init_cacheinfo);
b1dca6
 # endif
b1dca6
-}
b1dca6
-
b1dca6
 #endif
b1dca6
diff --git a/sysdeps/x86/cacheinfo.h b/sysdeps/x86/cacheinfo.h
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..0255696163b7b8ff
b1dca6
--- /dev/null
b1dca6
+++ b/sysdeps/x86/cacheinfo.h
b1dca6
@@ -0,0 +1,427 @@
b1dca6
+/* x86 cache info.
b1dca6
+   Copyright (C) 2020 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <https://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+#include <assert.h>
b1dca6
+#include <unistd.h>
b1dca6
+
b1dca6
+/* Data cache size for use in memory and string routines, typically
b1dca6
+   L1 size, rounded to multiple of 256 bytes.  */
b1dca6
+long int __x86_data_cache_size_half attribute_hidden = 32 * 1024 / 2;
b1dca6
+long int __x86_data_cache_size attribute_hidden = 32 * 1024;
b1dca6
+/* Similar to __x86_data_cache_size_half, but not rounded.  */
b1dca6
+long int __x86_raw_data_cache_size_half attribute_hidden = 32 * 1024 / 2;
b1dca6
+/* Similar to __x86_data_cache_size, but not rounded.  */
b1dca6
+long int __x86_raw_data_cache_size attribute_hidden = 32 * 1024;
b1dca6
+/* Shared cache size for use in memory and string routines, typically
b1dca6
+   L2 or L3 size, rounded to multiple of 256 bytes.  */
b1dca6
+long int __x86_shared_cache_size_half attribute_hidden = 1024 * 1024 / 2;
b1dca6
+long int __x86_shared_cache_size attribute_hidden = 1024 * 1024;
b1dca6
+/* Similar to __x86_shared_cache_size_half, but not rounded.  */
b1dca6
+long int __x86_raw_shared_cache_size_half attribute_hidden = 1024 * 1024 / 2;
b1dca6
+/* Similar to __x86_shared_cache_size, but not rounded.  */
b1dca6
+long int __x86_raw_shared_cache_size attribute_hidden = 1024 * 1024;
b1dca6
+
b1dca6
+/* Threshold to use non temporal store.  */
b1dca6
+long int __x86_shared_non_temporal_threshold attribute_hidden;
b1dca6
+
b1dca6
+/* Threshold to use Enhanced REP MOVSB.  */
b1dca6
+long int __x86_rep_movsb_threshold attribute_hidden = 2048;
b1dca6
+
b1dca6
+/* Threshold to use Enhanced REP STOSB.  */
b1dca6
+long int __x86_rep_stosb_threshold attribute_hidden = 2048;
b1dca6
+
b1dca6
+static void
b1dca6
+get_common_cache_info (long int *shared_ptr, unsigned int *threads_ptr,
b1dca6
+		       long int core)
b1dca6
+{
b1dca6
+  unsigned int eax;
b1dca6
+  unsigned int ebx;
b1dca6
+  unsigned int ecx;
b1dca6
+  unsigned int edx;
b1dca6
+
b1dca6
+  /* Number of logical processors sharing L2 cache.  */
b1dca6
+  int threads_l2;
b1dca6
+
b1dca6
+  /* Number of logical processors sharing L3 cache.  */
b1dca6
+  int threads_l3;
b1dca6
+
b1dca6
+  const struct cpu_features *cpu_features = __get_cpu_features ();
b1dca6
+  int max_cpuid = cpu_features->basic.max_cpuid;
b1dca6
+  unsigned int family = cpu_features->basic.family;
b1dca6
+  unsigned int model = cpu_features->basic.model;
b1dca6
+  long int shared = *shared_ptr;
b1dca6
+  unsigned int threads = *threads_ptr;
b1dca6
+  bool inclusive_cache = true;
b1dca6
+  bool support_count_mask = true;
b1dca6
+
b1dca6
+  /* Try L3 first.  */
b1dca6
+  unsigned int level = 3;
b1dca6
+
b1dca6
+  if (cpu_features->basic.kind == arch_kind_zhaoxin && family == 6)
b1dca6
+    support_count_mask = false;
b1dca6
+
b1dca6
+  if (shared <= 0)
b1dca6
+    {
b1dca6
+      /* Try L2 otherwise.  */
b1dca6
+      level  = 2;
b1dca6
+      shared = core;
b1dca6
+      threads_l2 = 0;
b1dca6
+      threads_l3 = -1;
b1dca6
+    }
b1dca6
+  else
b1dca6
+    {
b1dca6
+      threads_l2 = 0;
b1dca6
+      threads_l3 = 0;
b1dca6
+    }
b1dca6
+
b1dca6
+  /* A value of 0 for the HTT bit indicates there is only a single
b1dca6
+     logical processor.  */
b1dca6
+  if (HAS_CPU_FEATURE (HTT))
b1dca6
+    {
b1dca6
+      /* Figure out the number of logical threads that share the
b1dca6
+         highest cache level.  */
b1dca6
+      if (max_cpuid >= 4)
b1dca6
+        {
b1dca6
+          int i = 0;
b1dca6
+
b1dca6
+          /* Query until cache level 2 and 3 are enumerated.  */
b1dca6
+          int check = 0x1 | (threads_l3 == 0) << 1;
b1dca6
+          do
b1dca6
+            {
b1dca6
+              __cpuid_count (4, i++, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+              /* There seems to be a bug in at least some Pentium Ds
b1dca6
+                 which sometimes fail to iterate all cache parameters.
b1dca6
+                 Do not loop indefinitely here, stop in this case and
b1dca6
+                 assume there is no such information.  */
b1dca6
+              if (cpu_features->basic.kind == arch_kind_intel
b1dca6
+                  && (eax & 0x1f) == 0 )
b1dca6
+                goto intel_bug_no_cache_info;
b1dca6
+
b1dca6
+              switch ((eax >> 5) & 0x7)
b1dca6
+                {
b1dca6
+                  default:
b1dca6
+                    break;
b1dca6
+                  case 2:
b1dca6
+                    if ((check & 0x1))
b1dca6
+                      {
b1dca6
+                        /* Get maximum number of logical processors
b1dca6
+                           sharing L2 cache.  */
b1dca6
+                        threads_l2 = (eax >> 14) & 0x3ff;
b1dca6
+                        check &= ~0x1;
b1dca6
+                      }
b1dca6
+                    break;
b1dca6
+                  case 3:
b1dca6
+                    if ((check & (0x1 << 1)))
b1dca6
+                      {
b1dca6
+                        /* Get maximum number of logical processors
b1dca6
+                           sharing L3 cache.  */
b1dca6
+                        threads_l3 = (eax >> 14) & 0x3ff;
b1dca6
+
b1dca6
+                        /* Check if L2 and L3 caches are inclusive.  */
b1dca6
+                        inclusive_cache = (edx & 0x2) != 0;
b1dca6
+                        check &= ~(0x1 << 1);
b1dca6
+                      }
b1dca6
+                    break;
b1dca6
+                }
b1dca6
+            }
b1dca6
+          while (check);
b1dca6
+
b1dca6
+          /* If max_cpuid >= 11, THREADS_L2/THREADS_L3 are the maximum
b1dca6
+             numbers of addressable IDs for logical processors sharing
b1dca6
+             the cache, instead of the maximum number of threads
b1dca6
+             sharing the cache.  */
b1dca6
+          if (max_cpuid >= 11 && support_count_mask)
b1dca6
+            {
b1dca6
+              /* Find the number of logical processors shipped in
b1dca6
+                 one core and apply count mask.  */
b1dca6
+              i = 0;
b1dca6
+
b1dca6
+              /* Count SMT only if there is L3 cache.  Always count
b1dca6
+                 core if there is no L3 cache.  */
b1dca6
+              int count = ((threads_l2 > 0 && level == 3)
b1dca6
+                           | ((threads_l3 > 0
b1dca6
+                               || (threads_l2 > 0 && level == 2)) << 1));
b1dca6
+
b1dca6
+              while (count)
b1dca6
+                {
b1dca6
+                  __cpuid_count (11, i++, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+                  int shipped = ebx & 0xff;
b1dca6
+                  int type = ecx & 0xff00;
b1dca6
+                  if (shipped == 0 || type == 0)
b1dca6
+                    break;
b1dca6
+                  else if (type == 0x100)
b1dca6
+                    {
b1dca6
+                      /* Count SMT.  */
b1dca6
+                      if ((count & 0x1))
b1dca6
+                        {
b1dca6
+                          int count_mask;
b1dca6
+
b1dca6
+                          /* Compute count mask.  */
b1dca6
+                          asm ("bsr %1, %0"
b1dca6
+                               : "=r" (count_mask) : "g" (threads_l2));
b1dca6
+                          count_mask = ~(-1 << (count_mask + 1));
b1dca6
+                          threads_l2 = (shipped - 1) & count_mask;
b1dca6
+                          count &= ~0x1;
b1dca6
+                        }
b1dca6
+                    }
b1dca6
+                  else if (type == 0x200)
b1dca6
+                    {
b1dca6
+                      /* Count core.  */
b1dca6
+                      if ((count & (0x1 << 1)))
b1dca6
+                        {
b1dca6
+                          int count_mask;
b1dca6
+                          int threads_core
b1dca6
+                            = (level == 2 ? threads_l2 : threads_l3);
b1dca6
+
b1dca6
+                          /* Compute count mask.  */
b1dca6
+                          asm ("bsr %1, %0"
b1dca6
+                               : "=r" (count_mask) : "g" (threads_core));
b1dca6
+                          count_mask = ~(-1 << (count_mask + 1));
b1dca6
+                          threads_core = (shipped - 1) & count_mask;
b1dca6
+                          if (level == 2)
b1dca6
+                            threads_l2 = threads_core;
b1dca6
+                          else
b1dca6
+                            threads_l3 = threads_core;
b1dca6
+                          count &= ~(0x1 << 1);
b1dca6
+                        }
b1dca6
+                    }
b1dca6
+                }
b1dca6
+            }
b1dca6
+          if (threads_l2 > 0)
b1dca6
+            threads_l2 += 1;
b1dca6
+          if (threads_l3 > 0)
b1dca6
+            threads_l3 += 1;
b1dca6
+          if (level == 2)
b1dca6
+            {
b1dca6
+              if (threads_l2)
b1dca6
+                {
b1dca6
+                  threads = threads_l2;
b1dca6
+                  if (cpu_features->basic.kind == arch_kind_intel
b1dca6
+                      && threads > 2
b1dca6
+                      && family == 6)
b1dca6
+                    switch (model)
b1dca6
+                      {
b1dca6
+                        case 0x37:
b1dca6
+                        case 0x4a:
b1dca6
+                        case 0x4d:
b1dca6
+                        case 0x5a:
b1dca6
+                        case 0x5d:
b1dca6
+                          /* Silvermont has L2 cache shared by 2 cores.  */
b1dca6
+                          threads = 2;
b1dca6
+                          break;
b1dca6
+                        default:
b1dca6
+                          break;
b1dca6
+                      }
b1dca6
+                }
b1dca6
+            }
b1dca6
+          else if (threads_l3)
b1dca6
+            threads = threads_l3;
b1dca6
+        }
b1dca6
+      else
b1dca6
+        {
b1dca6
+intel_bug_no_cache_info:
b1dca6
+          /* Assume that all logical threads share the highest cache
b1dca6
+             level.  */
b1dca6
+          threads
b1dca6
+            = ((cpu_features->features[COMMON_CPUID_INDEX_1].cpuid.ebx
b1dca6
+                >> 16) & 0xff);
b1dca6
+        }
b1dca6
+
b1dca6
+        /* Cap usage of highest cache level to the number of supported
b1dca6
+           threads.  */
b1dca6
+        if (shared > 0 && threads > 0)
b1dca6
+          shared /= threads;
b1dca6
+    }
b1dca6
+
b1dca6
+  /* Account for non-inclusive L2 and L3 caches.  */
b1dca6
+  if (!inclusive_cache)
b1dca6
+    {
b1dca6
+      if (threads_l2 > 0)
b1dca6
+        core /= threads_l2;
b1dca6
+      shared += core;
b1dca6
+    }
b1dca6
+
b1dca6
+  *shared_ptr = shared;
b1dca6
+  *threads_ptr = threads;
b1dca6
+}
b1dca6
+
b1dca6
+static void
b1dca6
+init_cacheinfo (void)
b1dca6
+{
b1dca6
+  /* Find out what brand of processor.  */
b1dca6
+  unsigned int ebx;
b1dca6
+  unsigned int ecx;
b1dca6
+  unsigned int edx;
b1dca6
+  int max_cpuid_ex;
b1dca6
+  long int data = -1;
b1dca6
+  long int shared = -1;
b1dca6
+  long int core;
b1dca6
+  unsigned int threads = 0;
b1dca6
+  const struct cpu_features *cpu_features = __get_cpu_features ();
b1dca6
+
b1dca6
+  /* NB: In libc.so, cpu_features is defined in ld.so and is initialized
b1dca6
+     by DL_PLATFORM_INIT or IFUNC relocation before init_cacheinfo is
b1dca6
+     called by IFUNC relocation.  In libc.a, init_cacheinfo is called
b1dca6
+     from init_cpu_features by ARCH_INIT_CPU_FEATURES.  */
b1dca6
+  assert (cpu_features->basic.kind != arch_kind_unknown);
b1dca6
+
b1dca6
+  if (cpu_features->basic.kind == arch_kind_intel)
b1dca6
+    {
b1dca6
+      data = handle_intel (_SC_LEVEL1_DCACHE_SIZE, cpu_features);
b1dca6
+      core = handle_intel (_SC_LEVEL2_CACHE_SIZE, cpu_features);
b1dca6
+      shared = handle_intel (_SC_LEVEL3_CACHE_SIZE, cpu_features);
b1dca6
+
b1dca6
+      get_common_cache_info (&shared, &threads, core);
b1dca6
+    }
b1dca6
+  else if (cpu_features->basic.kind == arch_kind_zhaoxin)
b1dca6
+    {
b1dca6
+      data = handle_zhaoxin (_SC_LEVEL1_DCACHE_SIZE);
b1dca6
+      core = handle_zhaoxin (_SC_LEVEL2_CACHE_SIZE);
b1dca6
+      shared = handle_zhaoxin (_SC_LEVEL3_CACHE_SIZE);
b1dca6
+
b1dca6
+      get_common_cache_info (&shared, &threads, core);
b1dca6
+    }
b1dca6
+  else if (cpu_features->basic.kind == arch_kind_amd)
b1dca6
+    {
b1dca6
+      data   = handle_amd (_SC_LEVEL1_DCACHE_SIZE);
b1dca6
+      long int core = handle_amd (_SC_LEVEL2_CACHE_SIZE);
b1dca6
+      shared = handle_amd (_SC_LEVEL3_CACHE_SIZE);
b1dca6
+
b1dca6
+      /* Get maximum extended function. */
b1dca6
+      __cpuid (0x80000000, max_cpuid_ex, ebx, ecx, edx);
b1dca6
+
b1dca6
+      if (shared <= 0)
b1dca6
+	/* No shared L3 cache.  All we have is the L2 cache.  */
b1dca6
+	shared = core;
b1dca6
+      else
b1dca6
+	{
b1dca6
+	  /* Figure out the number of logical threads that share L3.  */
b1dca6
+	  if (max_cpuid_ex >= 0x80000008)
b1dca6
+	    {
b1dca6
+	      /* Get width of APIC ID.  */
b1dca6
+	      __cpuid (0x80000008, max_cpuid_ex, ebx, ecx, edx);
b1dca6
+	      threads = 1 << ((ecx >> 12) & 0x0f);
b1dca6
+	    }
b1dca6
+
b1dca6
+	  if (threads == 0 || cpu_features->basic.family >= 0x17)
b1dca6
+	    {
b1dca6
+	      /* If APIC ID width is not available, use logical
b1dca6
+		 processor count.  */
b1dca6
+	      __cpuid (0x00000001, max_cpuid_ex, ebx, ecx, edx);
b1dca6
+
b1dca6
+	      if ((edx & (1 << 28)) != 0)
b1dca6
+		threads = (ebx >> 16) & 0xff;
b1dca6
+	    }
b1dca6
+
b1dca6
+	  /* Cap usage of highest cache level to the number of
b1dca6
+	     supported threads.  */
b1dca6
+	  if (threads > 0)
b1dca6
+	    shared /= threads;
b1dca6
+
b1dca6
+	  /* Get shared cache per ccx for Zen architectures.  */
b1dca6
+	  if (cpu_features->basic.family >= 0x17)
b1dca6
+	    {
b1dca6
+	      unsigned int eax;
b1dca6
+
b1dca6
+	      /* Get number of threads share the L3 cache in CCX.  */
b1dca6
+	      __cpuid_count (0x8000001D, 0x3, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+	      unsigned int threads_per_ccx = ((eax >> 14) & 0xfff) + 1;
b1dca6
+	      shared *= threads_per_ccx;
b1dca6
+	    }
b1dca6
+	  else
b1dca6
+	    {
b1dca6
+	      /* Account for exclusive L2 and L3 caches.  */
b1dca6
+	      shared += core;
b1dca6
+            }
b1dca6
+	}
b1dca6
+    }
b1dca6
+
b1dca6
+  if (cpu_features->data_cache_size != 0)
b1dca6
+    data = cpu_features->data_cache_size;
b1dca6
+
b1dca6
+  if (data > 0)
b1dca6
+    {
b1dca6
+      __x86_raw_data_cache_size_half = data / 2;
b1dca6
+      __x86_raw_data_cache_size = data;
b1dca6
+      /* Round data cache size to multiple of 256 bytes.  */
b1dca6
+      data = data & ~255L;
b1dca6
+      __x86_data_cache_size_half = data / 2;
b1dca6
+      __x86_data_cache_size = data;
b1dca6
+    }
b1dca6
+
b1dca6
+  if (cpu_features->shared_cache_size != 0)
b1dca6
+    shared = cpu_features->shared_cache_size;
b1dca6
+
b1dca6
+  if (shared > 0)
b1dca6
+    {
b1dca6
+      __x86_raw_shared_cache_size_half = shared / 2;
b1dca6
+      __x86_raw_shared_cache_size = shared;
b1dca6
+      /* Round shared cache size to multiple of 256 bytes.  */
b1dca6
+      shared = shared & ~255L;
b1dca6
+      __x86_shared_cache_size_half = shared / 2;
b1dca6
+      __x86_shared_cache_size = shared;
b1dca6
+    }
b1dca6
+
b1dca6
+  /* The default setting for the non_temporal threshold is 3/4 of one
b1dca6
+     thread's share of the chip's cache. For most Intel and AMD processors
b1dca6
+     with an initial release date between 2017 and 2020, a thread's typical
b1dca6
+     share of the cache is from 500 KBytes to 2 MBytes. Using the 3/4
b1dca6
+     threshold leaves 125 KBytes to 500 KBytes of the thread's data
b1dca6
+     in cache after a maximum temporal copy, which will maintain
b1dca6
+     in cache a reasonable portion of the thread's stack and other
b1dca6
+     active data. If the threshold is set higher than one thread's
b1dca6
+     share of the cache, it has a substantial risk of negatively
b1dca6
+     impacting the performance of other threads running on the chip. */
b1dca6
+  __x86_shared_non_temporal_threshold
b1dca6
+    = (cpu_features->non_temporal_threshold != 0
b1dca6
+       ? cpu_features->non_temporal_threshold
b1dca6
+       : __x86_shared_cache_size * 3 / 4);
b1dca6
+
b1dca6
+  /* NB: The REP MOVSB threshold must be greater than VEC_SIZE * 8.  */
b1dca6
+  unsigned int minimum_rep_movsb_threshold;
b1dca6
+  /* NB: The default REP MOVSB threshold is 2048 * (VEC_SIZE / 16).  */
b1dca6
+  unsigned int rep_movsb_threshold;
b1dca6
+  if (CPU_FEATURE_USABLE_P (cpu_features, AVX512F)
b1dca6
+      && !CPU_FEATURE_PREFERRED_P (cpu_features, Prefer_No_AVX512))
b1dca6
+    {
b1dca6
+      rep_movsb_threshold = 2048 * (64 / 16);
b1dca6
+      minimum_rep_movsb_threshold = 64 * 8;
b1dca6
+    }
b1dca6
+  else if (CPU_FEATURE_PREFERRED_P (cpu_features,
b1dca6
+				    AVX_Fast_Unaligned_Load))
b1dca6
+    {
b1dca6
+      rep_movsb_threshold = 2048 * (32 / 16);
b1dca6
+      minimum_rep_movsb_threshold = 32 * 8;
b1dca6
+    }
b1dca6
+  else
b1dca6
+    {
b1dca6
+      rep_movsb_threshold = 2048 * (16 / 16);
b1dca6
+      minimum_rep_movsb_threshold = 16 * 8;
b1dca6
+    }
b1dca6
+  if (cpu_features->rep_movsb_threshold > minimum_rep_movsb_threshold)
b1dca6
+    __x86_rep_movsb_threshold = cpu_features->rep_movsb_threshold;
b1dca6
+  else
b1dca6
+    __x86_rep_movsb_threshold = rep_movsb_threshold;
b1dca6
+
b1dca6
+# if HAVE_TUNABLES
b1dca6
+  __x86_rep_stosb_threshold = cpu_features->rep_stosb_threshold;
b1dca6
+# endif
b1dca6
+}
b1dca6
diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c
b1dca6
index 5f0548fe08134236..4c9c15a44b618fed 100644
b1dca6
--- a/sysdeps/x86/cpu-features.c
b1dca6
+++ b/sysdeps/x86/cpu-features.c
b1dca6
@@ -17,9 +17,14 @@
b1dca6
    <http://www.gnu.org/licenses/>.  */
b1dca6
 
b1dca6
 #include <cpuid.h>
b1dca6
-#include <cpu-features.h>
b1dca6
 #include <dl-hwcap.h>
b1dca6
 #include <libc-pointer-arith.h>
b1dca6
+#if IS_IN (libc) && !defined SHARED
b1dca6
+# include <assert.h>
b1dca6
+# include <unistd.h>
b1dca6
+# include <dl-cacheinfo.h>
b1dca6
+# include <cacheinfo.h>
b1dca6
+#endif
b1dca6
 
b1dca6
 #if HAVE_TUNABLES
b1dca6
 # define TUNABLE_NAMESPACE cpu
b1dca6
@@ -752,4 +757,9 @@ no_cpuid:
b1dca6
 # endif
b1dca6
     }
b1dca6
 #endif
b1dca6
+
b1dca6
+#ifndef SHARED
b1dca6
+  /* NB: In libc.a, call init_cacheinfo.  */
b1dca6
+  init_cacheinfo ();
b1dca6
+#endif
b1dca6
 }
b1dca6
diff --git a/sysdeps/x86/dl-cacheinfo.h b/sysdeps/x86/dl-cacheinfo.h
b1dca6
new file mode 100644
b1dca6
index 0000000000000000..b2b90074b0e98a60
b1dca6
--- /dev/null
b1dca6
+++ b/sysdeps/x86/dl-cacheinfo.h
b1dca6
@@ -0,0 +1,478 @@
b1dca6
+/* Initialize x86 cache info.
b1dca6
+   Copyright (C) 2020 Free Software Foundation, Inc.
b1dca6
+   This file is part of the GNU C Library.
b1dca6
+
b1dca6
+   The GNU C Library is free software; you can redistribute it and/or
b1dca6
+   modify it under the terms of the GNU Lesser General Public
b1dca6
+   License as published by the Free Software Foundation; either
b1dca6
+   version 2.1 of the License, or (at your option) any later version.
b1dca6
+
b1dca6
+   The GNU C Library is distributed in the hope that it will be useful,
b1dca6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b1dca6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b1dca6
+   Lesser General Public License for more details.
b1dca6
+
b1dca6
+   You should have received a copy of the GNU Lesser General Public
b1dca6
+   License along with the GNU C Library; if not, see
b1dca6
+   <https://www.gnu.org/licenses/>.  */
b1dca6
+
b1dca6
+static const struct intel_02_cache_info
b1dca6
+{
b1dca6
+  unsigned char idx;
b1dca6
+  unsigned char assoc;
b1dca6
+  unsigned char linesize;
b1dca6
+  unsigned char rel_name;
b1dca6
+  unsigned int size;
b1dca6
+} intel_02_known [] =
b1dca6
+  {
b1dca6
+#define M(sc) ((sc) - _SC_LEVEL1_ICACHE_SIZE)
b1dca6
+    { 0x06,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),    8192 },
b1dca6
+    { 0x08,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),   16384 },
b1dca6
+    { 0x09,  4, 32, M(_SC_LEVEL1_ICACHE_SIZE),   32768 },
b1dca6
+    { 0x0a,  2, 32, M(_SC_LEVEL1_DCACHE_SIZE),    8192 },
b1dca6
+    { 0x0c,  4, 32, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
+    { 0x0d,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
+    { 0x0e,  6, 64, M(_SC_LEVEL1_DCACHE_SIZE),   24576 },
b1dca6
+    { 0x21,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x22,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),   524288 },
b1dca6
+    { 0x23,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
+    { 0x25,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
+    { 0x29,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
+    { 0x2c,  8, 64, M(_SC_LEVEL1_DCACHE_SIZE),   32768 },
b1dca6
+    { 0x30,  8, 64, M(_SC_LEVEL1_ICACHE_SIZE),   32768 },
b1dca6
+    { 0x39,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
+    { 0x3a,  6, 64, M(_SC_LEVEL2_CACHE_SIZE),   196608 },
b1dca6
+    { 0x3b,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
+    { 0x3c,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x3d,  6, 64, M(_SC_LEVEL2_CACHE_SIZE),   393216 },
b1dca6
+    { 0x3e,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x3f,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x41,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
+    { 0x42,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x43,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x44,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
+    { 0x45,  4, 32, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
+    { 0x46,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
+    { 0x47,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
+    { 0x48, 12, 64, M(_SC_LEVEL2_CACHE_SIZE),  3145728 },
b1dca6
+    { 0x49, 16, 64, M(_SC_LEVEL2_CACHE_SIZE),  4194304 },
b1dca6
+    { 0x4a, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  6291456 },
b1dca6
+    { 0x4b, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
+    { 0x4c, 12, 64, M(_SC_LEVEL3_CACHE_SIZE), 12582912 },
b1dca6
+    { 0x4d, 16, 64, M(_SC_LEVEL3_CACHE_SIZE), 16777216 },
b1dca6
+    { 0x4e, 24, 64, M(_SC_LEVEL2_CACHE_SIZE),  6291456 },
b1dca6
+    { 0x60,  8, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
+    { 0x66,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),    8192 },
b1dca6
+    { 0x67,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   16384 },
b1dca6
+    { 0x68,  4, 64, M(_SC_LEVEL1_DCACHE_SIZE),   32768 },
b1dca6
+    { 0x78,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
+    { 0x79,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   131072 },
b1dca6
+    { 0x7a,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x7b,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x7c,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
+    { 0x7d,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
+    { 0x7f,  2, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x80,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x82,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),   262144 },
b1dca6
+    { 0x83,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x84,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
+    { 0x85,  8, 32, M(_SC_LEVEL2_CACHE_SIZE),  2097152 },
b1dca6
+    { 0x86,  4, 64, M(_SC_LEVEL2_CACHE_SIZE),   524288 },
b1dca6
+    { 0x87,  8, 64, M(_SC_LEVEL2_CACHE_SIZE),  1048576 },
b1dca6
+    { 0xd0,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),   524288 },
b1dca6
+    { 0xd1,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
+    { 0xd2,  4, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
+    { 0xd6,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  1048576 },
b1dca6
+    { 0xd7,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
+    { 0xd8,  8, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
+    { 0xdc, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
+    { 0xdd, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
+    { 0xde, 12, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
+    { 0xe2, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  2097152 },
b1dca6
+    { 0xe3, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  4194304 },
b1dca6
+    { 0xe4, 16, 64, M(_SC_LEVEL3_CACHE_SIZE),  8388608 },
b1dca6
+    { 0xea, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 12582912 },
b1dca6
+    { 0xeb, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 18874368 },
b1dca6
+    { 0xec, 24, 64, M(_SC_LEVEL3_CACHE_SIZE), 25165824 },
b1dca6
+  };
b1dca6
+
b1dca6
+#define nintel_02_known (sizeof (intel_02_known) / sizeof (intel_02_known [0]))
b1dca6
+
b1dca6
+static int
b1dca6
+intel_02_known_compare (const void *p1, const void *p2)
b1dca6
+{
b1dca6
+  const struct intel_02_cache_info *i1;
b1dca6
+  const struct intel_02_cache_info *i2;
b1dca6
+
b1dca6
+  i1 = (const struct intel_02_cache_info *) p1;
b1dca6
+  i2 = (const struct intel_02_cache_info *) p2;
b1dca6
+
b1dca6
+  if (i1->idx == i2->idx)
b1dca6
+    return 0;
b1dca6
+
b1dca6
+  return i1->idx < i2->idx ? -1 : 1;
b1dca6
+}
b1dca6
+
b1dca6
+
b1dca6
+static long int
b1dca6
+__attribute__ ((noinline))
b1dca6
+intel_check_word (int name, unsigned int value, bool *has_level_2,
b1dca6
+		  bool *no_level_2_or_3,
b1dca6
+		  const struct cpu_features *cpu_features)
b1dca6
+{
b1dca6
+  if ((value & 0x80000000) != 0)
b1dca6
+    /* The register value is reserved.  */
b1dca6
+    return 0;
b1dca6
+
b1dca6
+  /* Fold the name.  The _SC_ constants are always in the order SIZE,
b1dca6
+     ASSOC, LINESIZE.  */
b1dca6
+  int folded_rel_name = (M(name) / 3) * 3;
b1dca6
+
b1dca6
+  while (value != 0)
b1dca6
+    {
b1dca6
+      unsigned int byte = value & 0xff;
b1dca6
+
b1dca6
+      if (byte == 0x40)
b1dca6
+	{
b1dca6
+	  *no_level_2_or_3 = true;
b1dca6
+
b1dca6
+	  if (folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
+	    /* No need to look further.  */
b1dca6
+	    break;
b1dca6
+	}
b1dca6
+      else if (byte == 0xff)
b1dca6
+	{
b1dca6
+	  /* CPUID leaf 0x4 contains all the information.  We need to
b1dca6
+	     iterate over it.  */
b1dca6
+	  unsigned int eax;
b1dca6
+	  unsigned int ebx;
b1dca6
+	  unsigned int ecx;
b1dca6
+	  unsigned int edx;
b1dca6
+
b1dca6
+	  unsigned int round = 0;
b1dca6
+	  while (1)
b1dca6
+	    {
b1dca6
+	      __cpuid_count (4, round, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+	      enum { null = 0, data = 1, inst = 2, uni = 3 } type = eax & 0x1f;
b1dca6
+	      if (type == null)
b1dca6
+		/* That was the end.  */
b1dca6
+		break;
b1dca6
+
b1dca6
+	      unsigned int level = (eax >> 5) & 0x7;
b1dca6
+
b1dca6
+	      if ((level == 1 && type == data
b1dca6
+		   && folded_rel_name == M(_SC_LEVEL1_DCACHE_SIZE))
b1dca6
+		  || (level == 1 && type == inst
b1dca6
+		      && folded_rel_name == M(_SC_LEVEL1_ICACHE_SIZE))
b1dca6
+		  || (level == 2 && folded_rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
+		  || (level == 3 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
+		  || (level == 4 && folded_rel_name == M(_SC_LEVEL4_CACHE_SIZE)))
b1dca6
+		{
b1dca6
+		  unsigned int offset = M(name) - folded_rel_name;
b1dca6
+
b1dca6
+		  if (offset == 0)
b1dca6
+		    /* Cache size.  */
b1dca6
+		    return (((ebx >> 22) + 1)
b1dca6
+			    * (((ebx >> 12) & 0x3ff) + 1)
b1dca6
+			    * ((ebx & 0xfff) + 1)
b1dca6
+			    * (ecx + 1));
b1dca6
+		  if (offset == 1)
b1dca6
+		    return (ebx >> 22) + 1;
b1dca6
+
b1dca6
+		  assert (offset == 2);
b1dca6
+		  return (ebx & 0xfff) + 1;
b1dca6
+		}
b1dca6
+
b1dca6
+	      ++round;
b1dca6
+	    }
b1dca6
+	  /* There is no other cache information anywhere else.  */
b1dca6
+	  break;
b1dca6
+	}
b1dca6
+      else
b1dca6
+	{
b1dca6
+	  if (byte == 0x49 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE))
b1dca6
+	    {
b1dca6
+	      /* Intel reused this value.  For family 15, model 6 it
b1dca6
+		 specifies the 3rd level cache.  Otherwise the 2nd
b1dca6
+		 level cache.  */
b1dca6
+	      unsigned int family = cpu_features->basic.family;
b1dca6
+	      unsigned int model = cpu_features->basic.model;
b1dca6
+
b1dca6
+	      if (family == 15 && model == 6)
b1dca6
+		{
b1dca6
+		  /* The level 3 cache is encoded for this model like
b1dca6
+		     the level 2 cache is for other models.  Pretend
b1dca6
+		     the caller asked for the level 2 cache.  */
b1dca6
+		  name = (_SC_LEVEL2_CACHE_SIZE
b1dca6
+			  + (name - _SC_LEVEL3_CACHE_SIZE));
b1dca6
+		  folded_rel_name = M(_SC_LEVEL2_CACHE_SIZE);
b1dca6
+		}
b1dca6
+	    }
b1dca6
+
b1dca6
+	  struct intel_02_cache_info *found;
b1dca6
+	  struct intel_02_cache_info search;
b1dca6
+
b1dca6
+	  search.idx = byte;
b1dca6
+	  found = bsearch (&search, intel_02_known, nintel_02_known,
b1dca6
+			   sizeof (intel_02_known[0]), intel_02_known_compare);
b1dca6
+	  if (found != NULL)
b1dca6
+	    {
b1dca6
+	      if (found->rel_name == folded_rel_name)
b1dca6
+		{
b1dca6
+		  unsigned int offset = M(name) - folded_rel_name;
b1dca6
+
b1dca6
+		  if (offset == 0)
b1dca6
+		    /* Cache size.  */
b1dca6
+		    return found->size;
b1dca6
+		  if (offset == 1)
b1dca6
+		    return found->assoc;
b1dca6
+
b1dca6
+		  assert (offset == 2);
b1dca6
+		  return found->linesize;
b1dca6
+		}
b1dca6
+
b1dca6
+	      if (found->rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
+		*has_level_2 = true;
b1dca6
+	    }
b1dca6
+	}
b1dca6
+
b1dca6
+      /* Next byte for the next round.  */
b1dca6
+      value >>= 8;
b1dca6
+    }
b1dca6
+
b1dca6
+  /* Nothing found.  */
b1dca6
+  return 0;
b1dca6
+}
b1dca6
+
b1dca6
+
b1dca6
+static long int __attribute__ ((noinline))
b1dca6
+handle_intel (int name, const struct cpu_features *cpu_features)
b1dca6
+{
b1dca6
+  unsigned int maxidx = cpu_features->basic.max_cpuid;
b1dca6
+
b1dca6
+  /* Return -1 for older CPUs.  */
b1dca6
+  if (maxidx < 2)
b1dca6
+    return -1;
b1dca6
+
b1dca6
+  /* OK, we can use the CPUID instruction to get all info about the
b1dca6
+     caches.  */
b1dca6
+  unsigned int cnt = 0;
b1dca6
+  unsigned int max = 1;
b1dca6
+  long int result = 0;
b1dca6
+  bool no_level_2_or_3 = false;
b1dca6
+  bool has_level_2 = false;
b1dca6
+
b1dca6
+  while (cnt++ < max)
b1dca6
+    {
b1dca6
+      unsigned int eax;
b1dca6
+      unsigned int ebx;
b1dca6
+      unsigned int ecx;
b1dca6
+      unsigned int edx;
b1dca6
+      __cpuid (2, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+      /* The low byte of EAX in the first round contain the number of
b1dca6
+	 rounds we have to make.  At least one, the one we are already
b1dca6
+	 doing.  */
b1dca6
+      if (cnt == 1)
b1dca6
+	{
b1dca6
+	  max = eax & 0xff;
b1dca6
+	  eax &= 0xffffff00;
b1dca6
+	}
b1dca6
+
b1dca6
+      /* Process the individual registers' value.  */
b1dca6
+      result = intel_check_word (name, eax, &has_level_2,
b1dca6
+				 &no_level_2_or_3, cpu_features);
b1dca6
+      if (result != 0)
b1dca6
+	return result;
b1dca6
+
b1dca6
+      result = intel_check_word (name, ebx, &has_level_2,
b1dca6
+				 &no_level_2_or_3, cpu_features);
b1dca6
+      if (result != 0)
b1dca6
+	return result;
b1dca6
+
b1dca6
+      result = intel_check_word (name, ecx, &has_level_2,
b1dca6
+				 &no_level_2_or_3, cpu_features);
b1dca6
+      if (result != 0)
b1dca6
+	return result;
b1dca6
+
b1dca6
+      result = intel_check_word (name, edx, &has_level_2,
b1dca6
+				 &no_level_2_or_3, cpu_features);
b1dca6
+      if (result != 0)
b1dca6
+	return result;
b1dca6
+    }
b1dca6
+
b1dca6
+  if (name >= _SC_LEVEL2_CACHE_SIZE && name <= _SC_LEVEL3_CACHE_LINESIZE
b1dca6
+      && no_level_2_or_3)
b1dca6
+    return -1;
b1dca6
+
b1dca6
+  return 0;
b1dca6
+}
b1dca6
+
b1dca6
+
b1dca6
+static long int __attribute__ ((noinline))
b1dca6
+handle_amd (int name)
b1dca6
+{
b1dca6
+  unsigned int eax;
b1dca6
+  unsigned int ebx;
b1dca6
+  unsigned int ecx;
b1dca6
+  unsigned int edx;
b1dca6
+  __cpuid (0x80000000, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+  /* No level 4 cache (yet).  */
b1dca6
+  if (name > _SC_LEVEL3_CACHE_LINESIZE)
b1dca6
+    return 0;
b1dca6
+
b1dca6
+  unsigned int fn = 0x80000005 + (name >= _SC_LEVEL2_CACHE_SIZE);
b1dca6
+  if (eax < fn)
b1dca6
+    return 0;
b1dca6
+
b1dca6
+  __cpuid (fn, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+  if (name < _SC_LEVEL1_DCACHE_SIZE)
b1dca6
+    {
b1dca6
+      name += _SC_LEVEL1_DCACHE_SIZE - _SC_LEVEL1_ICACHE_SIZE;
b1dca6
+      ecx = edx;
b1dca6
+    }
b1dca6
+
b1dca6
+  switch (name)
b1dca6
+    {
b1dca6
+    case _SC_LEVEL1_DCACHE_SIZE:
b1dca6
+      return (ecx >> 14) & 0x3fc00;
b1dca6
+
b1dca6
+    case _SC_LEVEL1_DCACHE_ASSOC:
b1dca6
+      ecx >>= 16;
b1dca6
+      if ((ecx & 0xff) == 0xff)
b1dca6
+	/* Fully associative.  */
b1dca6
+	return (ecx << 2) & 0x3fc00;
b1dca6
+      return ecx & 0xff;
b1dca6
+
b1dca6
+    case _SC_LEVEL1_DCACHE_LINESIZE:
b1dca6
+      return ecx & 0xff;
b1dca6
+
b1dca6
+    case _SC_LEVEL2_CACHE_SIZE:
b1dca6
+      return (ecx & 0xf000) == 0 ? 0 : (ecx >> 6) & 0x3fffc00;
b1dca6
+
b1dca6
+    case _SC_LEVEL2_CACHE_ASSOC:
b1dca6
+      switch ((ecx >> 12) & 0xf)
b1dca6
+	{
b1dca6
+	case 0:
b1dca6
+	case 1:
b1dca6
+	case 2:
b1dca6
+	case 4:
b1dca6
+	  return (ecx >> 12) & 0xf;
b1dca6
+	case 6:
b1dca6
+	  return 8;
b1dca6
+	case 8:
b1dca6
+	  return 16;
b1dca6
+	case 10:
b1dca6
+	  return 32;
b1dca6
+	case 11:
b1dca6
+	  return 48;
b1dca6
+	case 12:
b1dca6
+	  return 64;
b1dca6
+	case 13:
b1dca6
+	  return 96;
b1dca6
+	case 14:
b1dca6
+	  return 128;
b1dca6
+	case 15:
b1dca6
+	  return ((ecx >> 6) & 0x3fffc00) / (ecx & 0xff);
b1dca6
+	default:
b1dca6
+	  return 0;
b1dca6
+	}
b1dca6
+      /* NOTREACHED */
b1dca6
+
b1dca6
+    case _SC_LEVEL2_CACHE_LINESIZE:
b1dca6
+      return (ecx & 0xf000) == 0 ? 0 : ecx & 0xff;
b1dca6
+
b1dca6
+    case _SC_LEVEL3_CACHE_SIZE:
b1dca6
+      return (edx & 0xf000) == 0 ? 0 : (edx & 0x3ffc0000) << 1;
b1dca6
+
b1dca6
+    case _SC_LEVEL3_CACHE_ASSOC:
b1dca6
+      switch ((edx >> 12) & 0xf)
b1dca6
+	{
b1dca6
+	case 0:
b1dca6
+	case 1:
b1dca6
+	case 2:
b1dca6
+	case 4:
b1dca6
+	  return (edx >> 12) & 0xf;
b1dca6
+	case 6:
b1dca6
+	  return 8;
b1dca6
+	case 8:
b1dca6
+	  return 16;
b1dca6
+	case 10:
b1dca6
+	  return 32;
b1dca6
+	case 11:
b1dca6
+	  return 48;
b1dca6
+	case 12:
b1dca6
+	  return 64;
b1dca6
+	case 13:
b1dca6
+	  return 96;
b1dca6
+	case 14:
b1dca6
+	  return 128;
b1dca6
+	case 15:
b1dca6
+	  return ((edx & 0x3ffc0000) << 1) / (edx & 0xff);
b1dca6
+	default:
b1dca6
+	  return 0;
b1dca6
+	}
b1dca6
+      /* NOTREACHED */
b1dca6
+
b1dca6
+    case _SC_LEVEL3_CACHE_LINESIZE:
b1dca6
+      return (edx & 0xf000) == 0 ? 0 : edx & 0xff;
b1dca6
+
b1dca6
+    default:
b1dca6
+      assert (! "cannot happen");
b1dca6
+    }
b1dca6
+  return -1;
b1dca6
+}
b1dca6
+
b1dca6
+
b1dca6
+static long int __attribute__ ((noinline))
b1dca6
+handle_zhaoxin (int name)
b1dca6
+{
b1dca6
+  unsigned int eax;
b1dca6
+  unsigned int ebx;
b1dca6
+  unsigned int ecx;
b1dca6
+  unsigned int edx;
b1dca6
+
b1dca6
+  int folded_rel_name = (M(name) / 3) * 3;
b1dca6
+
b1dca6
+  unsigned int round = 0;
b1dca6
+  while (1)
b1dca6
+    {
b1dca6
+      __cpuid_count (4, round, eax, ebx, ecx, edx);
b1dca6
+
b1dca6
+      enum { null = 0, data = 1, inst = 2, uni = 3 } type = eax & 0x1f;
b1dca6
+      if (type == null)
b1dca6
+        break;
b1dca6
+
b1dca6
+      unsigned int level = (eax >> 5) & 0x7;
b1dca6
+
b1dca6
+      if ((level == 1 && type == data
b1dca6
+        && folded_rel_name == M(_SC_LEVEL1_DCACHE_SIZE))
b1dca6
+        || (level == 1 && type == inst
b1dca6
+            && folded_rel_name == M(_SC_LEVEL1_ICACHE_SIZE))
b1dca6
+        || (level == 2 && folded_rel_name == M(_SC_LEVEL2_CACHE_SIZE))
b1dca6
+        || (level == 3 && folded_rel_name == M(_SC_LEVEL3_CACHE_SIZE)))
b1dca6
+        {
b1dca6
+          unsigned int offset = M(name) - folded_rel_name;
b1dca6
+
b1dca6
+          if (offset == 0)
b1dca6
+            /* Cache size.  */
b1dca6
+            return (((ebx >> 22) + 1)
b1dca6
+                * (((ebx >> 12) & 0x3ff) + 1)
b1dca6
+                * ((ebx & 0xfff) + 1)
b1dca6
+                * (ecx + 1));
b1dca6
+          if (offset == 1)
b1dca6
+            return (ebx >> 22) + 1;
b1dca6
+
b1dca6
+          assert (offset == 2);
b1dca6
+          return (ebx & 0xfff) + 1;
b1dca6
+        }
b1dca6
+
b1dca6
+      ++round;
b1dca6
+    }
b1dca6
+
b1dca6
+  /* Nothing found.  */
b1dca6
+  return 0;
b1dca6
+}
b1dca6
diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
b1dca6
index 2aba0d167129b336..82772cc12f0c7e54 100644
b1dca6
--- a/sysdeps/x86/dl-get-cpu-features.c
b1dca6
+++ b/sysdeps/x86/dl-get-cpu-features.c
b1dca6
@@ -1,5 +1,5 @@
b1dca6
-/* This file is part of the GNU C Library.
b1dca6
-   Copyright (C) 2015-2018 Free Software Foundation, Inc.
b1dca6
+/* Initialize CPU feature data via IFUNC relocation.
b1dca6
+   Copyright (C) 2015-2020 Free Software Foundation, Inc.
b1dca6
 
b1dca6
    The GNU C Library is free software; you can redistribute it and/or
b1dca6
    modify it under the terms of the GNU Lesser General Public
b1dca6
@@ -18,6 +18,31 @@
b1dca6
 
b1dca6
 #include <ldsodefs.h>
b1dca6
 
b1dca6
+#ifdef SHARED
b1dca6
+# include <cpu-features.c>
b1dca6
+
b1dca6
+/* NB: Normally, DL_PLATFORM_INIT calls init_cpu_features to initialize
b1dca6
+   CPU features in dynamic executable.  But when loading ld.so inside of
b1dca6
+   static executable, DL_PLATFORM_INIT isn't called and IFUNC relocation
b1dca6
+   is used to call init_cpu_features.  In static executable, it is called
b1dca6
+   once by IFUNC relocation.  In dynamic executable, it is called twice
b1dca6
+   by DL_PLATFORM_INIT and by IFUNC relocation.  */
b1dca6
+extern void __x86_cpu_features (void) attribute_hidden;
b1dca6
+const void (*__x86_cpu_features_p) (void) attribute_hidden
b1dca6
+  = __x86_cpu_features;
b1dca6
+
b1dca6
+void
b1dca6
+_dl_x86_init_cpu_features (void)
b1dca6
+{
b1dca6
+  struct cpu_features *cpu_features = __get_cpu_features ();
b1dca6
+  if (cpu_features->basic.kind == arch_kind_unknown)
b1dca6
+    init_cpu_features (cpu_features);
b1dca6
+}
b1dca6
+
b1dca6
+__ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
b1dca6
+	 _dl_x86_init_cpu_features);
b1dca6
+#endif
b1dca6
+
b1dca6
 #undef __x86_get_cpu_features
b1dca6
 
b1dca6
 const struct cpu_features *
b1dca6
diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h
b1dca6
index dcf29b6fe8578078..f62be0b9b3746675 100644
b1dca6
--- a/sysdeps/x86/include/cpu-features.h
b1dca6
+++ b/sysdeps/x86/include/cpu-features.h
b1dca6
@@ -159,6 +159,7 @@ struct cpu_features
b1dca6
 /* Unused for x86.  */
b1dca6
 #  define INIT_ARCH()
b1dca6
 #  define __x86_get_cpu_features(max) (&GLRO(dl_x86_cpu_features))
b1dca6
+extern void _dl_x86_init_cpu_features (void) attribute_hidden;
b1dca6
 # endif
b1dca6
 
b1dca6
 # ifdef __x86_64__
b1dca6
diff --git a/sysdeps/x86/libc-start.c b/sysdeps/x86/libc-start.c
b1dca6
index eb5335c154096384..60f2c34ab5511350 100644
b1dca6
--- a/sysdeps/x86/libc-start.c
b1dca6
+++ b/sysdeps/x86/libc-start.c
b1dca6
@@ -20,7 +20,6 @@
b1dca6
    PIE.  */
b1dca6
 # include <startup.h>
b1dca6
 # include <ldsodefs.h>
b1dca6
-# include <cpu-features.h>
b1dca6
 # include <cpu-features.c>
b1dca6
 
b1dca6
 extern struct cpu_features _dl_x86_cpu_features;
b1dca6
diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
b1dca6
index d58298d787ef352c..e308b662d245cc63 100644
b1dca6
--- a/sysdeps/x86_64/dl-machine.h
b1dca6
+++ b/sysdeps/x86_64/dl-machine.h
b1dca6
@@ -26,7 +26,6 @@
b1dca6
 #include <sysdep.h>
b1dca6
 #include <tls.h>
b1dca6
 #include <dl-tlsdesc.h>
b1dca6
-#include <cpu-features.c>
b1dca6
 
b1dca6
 /* Return nonzero iff ELF header is compatible with the running host.  */
b1dca6
 static inline int __attribute__ ((unused))
b1dca6
@@ -223,9 +222,9 @@ static inline void __attribute__ ((unused))
b1dca6
 dl_platform_init (void)
b1dca6
 {
b1dca6
 #if IS_IN (rtld)
b1dca6
-  /* init_cpu_features has been called early from __libc_start_main in
b1dca6
-     static executable.  */
b1dca6
-  init_cpu_features (&GLRO(dl_x86_cpu_features));
b1dca6
+  /* _dl_x86_init_cpu_features is a wrapper for init_cpu_features which
b1dca6
+     has been called early from __libc_start_main in static executable.  */
b1dca6
+  _dl_x86_init_cpu_features ();
b1dca6
 #else
b1dca6
   if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
b1dca6
     /* Avoid an empty string which would disturb us.  */