From patchwork Fri Sep 18 16:07:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 40439 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7852E397200A; Fri, 18 Sep 2020 16:07:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7852E397200A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600445235; bh=+h8xV0dWthirKzx0QU7g0gxH545aoHrORC+SZt3IMWE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=o96f6wTfmRonsDatPL1cMZVQxUMonBiUC7nTwaDysv4uTJ2BHZ7dbDW90Dj/GiZb9 fsQCahvx7BVioGNpJYvZkEFPM8G3VIi/9tZ7C+leiW2NGbua9oV3aW85Lh3PnDuHpz 101A4JgoiBR5JCsEtnBrDBCT5pyhFr1ZMeTXNuv0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id BB49C3870904 for ; Fri, 18 Sep 2020 16:07:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BB49C3870904 Received: by mail-pl1-x635.google.com with SMTP id e4so3213969pln.10 for ; Fri, 18 Sep 2020 09:07:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+h8xV0dWthirKzx0QU7g0gxH545aoHrORC+SZt3IMWE=; b=eFpceCGHGEd/dEg7eVAlhaulBaazku2SF1DtrnGo5abZar+kioTJHZWuJCMpIaC4Oe ynajMqpMorn6JtlQFLwMjQcQ8HOXd3W2X+gDrDSHAnjqpB40krn+NCh8zKj1y2kSTsQX M3/3Ae5cDl1QWvgTsYkexmbGLHPkFXHrJUuqhc/QC0UxcXlCh9JWyZ9o4g3ZUWK92P18 y9MIXy2dq8AOuirvcTdDlBLxiP80pLZnDIDF6xvEU1gg/eQ9H+nIiHbjRKzZGKyErkiR 21hKUR0SUe8PKEwM7DUjE4d6nvd9oTJgSBew5qIzlMjFSCsTZ4XOqrPBfxYuxC3mi6fi U8OA== X-Gm-Message-State: AOAM530a6+KXZpCc3DVBOoFfzNzotlTbG6EnGE4lOz6cd6A6+Q72lYQb j5Hz8U9gvHbNandQOlMWloRWt2EoDRg= X-Google-Smtp-Source: ABdhPJy/cwkXQwSKkLD7VgA+CTkv5NGqdLPF5MY1D/T07CgdTPbei7PHmeRyGYz5i6Qf1nmVzvEjAg== X-Received: by 2002:a17:902:8214:b029:d1:9be4:9ce2 with SMTP id x20-20020a1709028214b02900d19be49ce2mr32876236pln.30.1600445231812; Fri, 18 Sep 2020 09:07:11 -0700 (PDT) Received: from gnu-cfl-2.localdomain (c-69-181-90-243.hsd1.ca.comcast.net. [69.181.90.243]) by smtp.gmail.com with ESMTPSA id s28sm3791569pfd.111.2020.09.18.09.07.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2020 09:07:10 -0700 (PDT) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id D27CA1A03A6; Fri, 18 Sep 2020 09:07:09 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 1/4] x86: Initialize CPU info via IFUNC relocation [BZ 26203] Date: Fri, 18 Sep 2020 09:07:06 -0700 Message-Id: <20200918160709.949608-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200918160709.949608-1-hjl.tools@gmail.com> References: <20200918160709.949608-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "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. Since _rtld_global_ro in ld.so is initialized by dynamic relocation, we can also initialize x86 CPU features in _rtld_global_ro in ld.so and cache info in libc.so by initializing dummy function pointers in ld.so and libc.so via IFUNC relocation. Note: _dl_x86_init_cpu_features can be called more than once from DL_PLATFORM_INIT and during relocation in ld.so. --- sysdeps/i386/dl-machine.h | 3 +-- sysdeps/x86/cacheinfo.c | 10 +++++++++- sysdeps/x86/dl-get-cpu-features.c | 25 ++++++++++++++++++++++++- sysdeps/x86/include/cpu-features.h | 1 + sysdeps/x86_64/dl-machine.h | 3 +-- 5 files changed, 36 insertions(+), 6 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 #include #include -#include /* 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 217c21c34f..7a325ab70e 100644 --- a/sysdeps/x86/cacheinfo.c +++ b/sysdeps/x86/cacheinfo.c @@ -756,7 +756,6 @@ intel_bug_no_cache_info: static void -__attribute__((constructor)) init_cacheinfo (void) { /* Find out what brand of processor. */ @@ -770,6 +769,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); @@ -894,4 +895,11 @@ init_cacheinfo (void) # endif } +/* 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/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c index 5f9e46b0c6..da4697b895 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,29 @@ #include +#ifdef SHARED +# include + +/* NB: Normally, DL_PLATFORM_INIT calls init_cpu_features to initialize + CPU features. But when loading ld.so inside of static executable, + DL_PLATFORM_INIT isn't called. Call init_cpu_features by initializing + a dummy function pointer via IFUNC relocation for ld.so. */ +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 __x86_get_cpu_features const struct cpu_features * diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h index dcf29b6fe8..f62be0b9b3 100644 --- a/sysdeps/x86/include/cpu-features.h +++ b/sysdeps/x86/include/cpu-features.h @@ -159,6 +159,7 @@ struct cpu_features /* Unused for x86. */ # define INIT_ARCH() # define __x86_get_cpu_features(max) (&GLRO(dl_x86_cpu_features)) +extern void _dl_x86_init_cpu_features (void) attribute_hidden; # endif # ifdef __x86_64__ diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h index ca73d8fef9..773e94c8bb 100644 --- a/sysdeps/x86_64/dl-machine.h +++ b/sysdeps/x86_64/dl-machine.h @@ -26,7 +26,6 @@ #include #include #include -#include /* 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. */