* [PATCH] x86: Initialize CPU info via IFUNC relocation [BZ 26203]
@ 2020-07-04 15:18 H.J. Lu
0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2020-07-04 15:18 UTC (permalink / raw)
To: libc-alpha
X86 CPU features in ld.so are initialized by init_cpu_features, which is
invoked by DL_PLATFORM_INIT from _dl_sysdep_start. But when ld.so is
loaded by static executable, DL_PLATFORM_INIT is never called. Also
x86 cache info in libc.o and libc.a is initialized by a constructor
which may be called too late. Instead, we should initialize x86 CPU
features and cache info by initializing dummy function pointers via
IFUNC relocation.
Note: _dl_x86_init_cpu_features can be called more than once from
DL_PLATFORM_INIT and during relocation.
---
sysdeps/i386/dl-machine.h | 3 +--
sysdeps/x86/cacheinfo.c | 10 +++++++++-
sysdeps/x86/cpu-features.h | 3 +--
sysdeps/x86/dl-get-cpu-features.c | 23 ++++++++++++++++++++++-
sysdeps/x86_64/dl-machine.h | 3 +--
5 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
index 0f08079e48..5e22e795cc 100644
--- a/sysdeps/i386/dl-machine.h
+++ b/sysdeps/i386/dl-machine.h
@@ -25,7 +25,6 @@
#include <sysdep.h>
#include <tls.h>
#include <dl-tlsdesc.h>
-#include <cpu-features.c>
/* Return nonzero iff ELF header is compatible with the running host. */
static inline int __attribute__ ((unused))
@@ -250,7 +249,7 @@ dl_platform_init (void)
#if IS_IN (rtld)
/* init_cpu_features has been called early from __libc_start_main in
static executable. */
- init_cpu_features (&GLRO(dl_x86_cpu_features));
+ _dl_x86_init_cpu_features ();
#else
if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
/* Avoid an empty string which would disturb us. */
diff --git a/sysdeps/x86/cacheinfo.c b/sysdeps/x86/cacheinfo.c
index 311502dee3..d90126c9fd 100644
--- a/sysdeps/x86/cacheinfo.c
+++ b/sysdeps/x86/cacheinfo.c
@@ -755,7 +755,6 @@ intel_bug_no_cache_info:
static void
-__attribute__((constructor))
init_cacheinfo (void)
{
/* Find out what brand of processor. */
@@ -769,6 +768,8 @@ init_cacheinfo (void)
unsigned int threads = 0;
const struct cpu_features *cpu_features = __get_cpu_features ();
+ assert (cpu_features->basic.kind != arch_kind_unknown);
+
if (cpu_features->basic.kind == arch_kind_intel)
{
data = handle_intel (_SC_LEVEL1_DCACHE_SIZE, cpu_features);
@@ -874,4 +875,11 @@ init_cacheinfo (void)
: __x86_shared_cache_size * threads * 3 / 4);
}
+/* NB: Call init_cacheinfo by initializing a dummy function pointer via
+ IFUNC relocation. */
+extern void __x86_cacheinfo (void) attribute_hidden;
+const void (*__x86_cacheinfo_p) (void) attribute_hidden
+ = __x86_cacheinfo;
+
+__ifunc (__x86_cacheinfo, __x86_cacheinfo, NULL, void, init_cacheinfo);
#endif
diff --git a/sysdeps/x86/cpu-features.h b/sysdeps/x86/cpu-features.h
index d66dc206f7..5ca61b5099 100644
--- a/sysdeps/x86/cpu-features.h
+++ b/sysdeps/x86/cpu-features.h
@@ -844,8 +844,7 @@ extern const struct cpu_features *__get_cpu_features (void)
/* Unused for x86. */
# define INIT_ARCH()
# define __get_cpu_features() (&GLRO(dl_x86_cpu_features))
-# define x86_get_cpuid_registers(i) \
- (&(GLRO(dl_x86_cpu_features).cpuid[i]))
+extern void _dl_x86_init_cpu_features (void) attribute_hidden;
# endif
#ifdef __x86_64__
diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
index 9d61cd56be..a31c41de5b 100644
--- a/sysdeps/x86/dl-get-cpu-features.c
+++ b/sysdeps/x86/dl-get-cpu-features.c
@@ -1,4 +1,4 @@
-/* This file is part of the GNU C Library.
+/* Initialize CPU feature data via IFUNC relocation.
Copyright (C) 2015-2020 Free Software Foundation, Inc.
The GNU C Library is free software; you can redistribute it and/or
@@ -18,6 +18,27 @@
#include <ldsodefs.h>
+#ifdef SHARED
+# include <cpu-features.c>
+
+/* NB: Call init_cpu_features by initializing a dummy function pointer
+ via IFUNC relocation. */
+extern void __x86_cpu_features (void) attribute_hidden;
+const void (*__x86_cpu_features_p) (void) attribute_hidden
+ = __x86_cpu_features;
+
+void
+_dl_x86_init_cpu_features (void)
+{
+ struct cpu_features *cpu_features = __get_cpu_features ();
+ if (cpu_features->basic.kind == arch_kind_unknown)
+ init_cpu_features (cpu_features);
+}
+
+__ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
+ _dl_x86_init_cpu_features);
+#endif
+
#undef __get_cpu_features
const struct cpu_features *
diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h
index 8e9baffeb4..1105bd7775 100644
--- a/sysdeps/x86_64/dl-machine.h
+++ b/sysdeps/x86_64/dl-machine.h
@@ -26,7 +26,6 @@
#include <sysdep.h>
#include <tls.h>
#include <dl-tlsdesc.h>
-#include <cpu-features.c>
/* Return nonzero iff ELF header is compatible with the running host. */
static inline int __attribute__ ((unused))
@@ -225,7 +224,7 @@ dl_platform_init (void)
#if IS_IN (rtld)
/* init_cpu_features has been called early from __libc_start_main in
static executable. */
- init_cpu_features (&GLRO(dl_x86_cpu_features));
+ _dl_x86_init_cpu_features ();
#else
if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
/* Avoid an empty string which would disturb us. */
--
2.26.2
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-07-04 15:19 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-04 15:18 [PATCH] x86: Initialize CPU info via IFUNC relocation [BZ 26203] H.J. Lu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).