From patchwork Wed Jun 17 19:31:00 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: 39666 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 4CAF739CB81D; Wed, 17 Jun 2020 19:31:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CAF739CB81D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592422267; bh=9jgilVGLgtjo0EJwGVmZWL2bvGgsiVJj0XQ2KkJ6AJ0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IgQo34MKtzTlxgGQVXss6pvs7InJ/lA/YPNp9i6m56wiQdTkPDWMHe7QrneZ7UhAN EselIakW6oIa6MqiZ+LeLkzKN0H8oQ6VDqGHL46E6AUDviDoPS9ifeyy2BMHARs0ZY vBfbBRTcL5tgf2NPzp7yt1K5s03Yi6cWXn64Xo2I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by sourceware.org (Postfix) with ESMTPS id 4EEEA3851C0C for ; Wed, 17 Jun 2020 19:31:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4EEEA3851C0C Received: by mail-pl1-x644.google.com with SMTP id m7so1399337plt.5 for ; Wed, 17 Jun 2020 12:31:04 -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:subject:date:message-id:mime-version :content-transfer-encoding; bh=9jgilVGLgtjo0EJwGVmZWL2bvGgsiVJj0XQ2KkJ6AJ0=; b=mPwSP1lQnc0dM4K7wZkhkd5H2iuHEyJNyeaERDObsw5bZLD67IfQPZVBNAv3TPbPRa c8WOnrcnFsI1baBJ46UDXBjODnn9ZesvDFCgfFPF9GxDSk238Af35b7/B8tzWoy4TP7R rTnihiOP8YtJy17zbJ8uYTzyh0nH4cs9qkwMva/1orvt9exsi26hfvBVDLqyG5ia3DIL M7/8oy1t74kyPpX57YR1PpefnBiBTA2Dd/cGklLV8xECaXtUx44eryG885SJxVgztfYH i+YQWniwWi5AJusTtHukjOajnOKnxSfoY1HpcrARLYbL05isrWeKFZD281zxePAyi7QD DKKw== X-Gm-Message-State: AOAM532qqqtpKn31/gL078crmJ2EKNrIWo5BND/gV2e5WmzqOjzOx4xv O5akO7uUcGBpv8n6BxFrRgDNe1mz X-Google-Smtp-Source: ABdhPJxH9mnyub7qOCnEFrXOwjs/G0nf0FvAbtePPAQlZ+S5kaOFQDAYhw1kXnB5EC82QNDaYAcQuA== X-Received: by 2002:a17:90a:21a2:: with SMTP id q31mr592513pjc.230.1592422262800; Wed, 17 Jun 2020 12:31:02 -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 p6sm609826pfb.152.2020.06.17.12.31.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jun 2020 12:31:01 -0700 (PDT) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id EA8BA1A0227 for ; Wed, 17 Jun 2020 12:31:00 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] x86: Install [BZ #26124] Date: Wed, 17 Jun 2020 12:31:00 -0700 Message-Id: <20200617193100.1115529-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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" Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Install so that programmers can do #if __has_include() #include #endif ... if (HAS_CPU_FEATURE (SSE3)) ... if (CPU_FEATURE_USABLE (SSE3)) ... exports only: struct cpu_features { struct cpuid_registers cpuid[COMMON_CPUID_INDEX_MAX]; unsigned int feature[FEATURE_INDEX_MAX]; struct cpu_features_basic basic; }; /* Get a pointer to the CPU features structure. */ extern const struct cpu_features *__get_cpu_features (void) __attribute__ ((const)); Since all feature checks are done through macros, programs compiled with a newer header are compatible with the older ones as long as the layout of struct cpu_features is identical. Note: Although GCC has __builtin_cpu_supports, it only supports a subset of . --- sysdeps/unix/sysv/linux/i386/ld.abilist | 1 + sysdeps/unix/sysv/linux/x86_64/64/ld.abilist | 1 + sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist | 1 + sysdeps/x86/Makefile | 1 + sysdeps/x86/Versions | 2 +- sysdeps/x86/cpu-features.h | 58 ++--------- sysdeps/x86/include/cpu-features.h | 95 +++++++++++++++++++ 7 files changed, 106 insertions(+), 53 deletions(-) create mode 100644 sysdeps/x86/include/cpu-features.h diff --git a/sysdeps/unix/sysv/linux/i386/ld.abilist b/sysdeps/unix/sysv/linux/i386/ld.abilist index 0478e22071..2f522173b4 100644 --- a/sysdeps/unix/sysv/linux/i386/ld.abilist +++ b/sysdeps/unix/sysv/linux/i386/ld.abilist @@ -3,3 +3,4 @@ GLIBC_2.1 __libc_stack_end D 0x4 GLIBC_2.1 _dl_mcount F GLIBC_2.3 ___tls_get_addr F GLIBC_2.3 __tls_get_addr F +GLIBC_2.32 __get_cpu_features F diff --git a/sysdeps/unix/sysv/linux/x86_64/64/ld.abilist b/sysdeps/unix/sysv/linux/x86_64/64/ld.abilist index d3cdf7611e..5a6abd4da0 100644 --- a/sysdeps/unix/sysv/linux/x86_64/64/ld.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/64/ld.abilist @@ -2,3 +2,4 @@ GLIBC_2.2.5 __libc_stack_end D 0x8 GLIBC_2.2.5 _dl_mcount F GLIBC_2.2.5 _r_debug D 0x28 GLIBC_2.3 __tls_get_addr F +GLIBC_2.32 __get_cpu_features F diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist b/sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist index c70bccf782..1169d8135d 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist +++ b/sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist @@ -2,3 +2,4 @@ GLIBC_2.16 __libc_stack_end D 0x4 GLIBC_2.16 __tls_get_addr F GLIBC_2.16 _dl_mcount F GLIBC_2.16 _r_debug D 0x14 +GLIBC_2.32 __get_cpu_features F diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index beab426f67..47051a3c82 100644 --- a/sysdeps/x86/Makefile +++ b/sysdeps/x86/Makefile @@ -4,6 +4,7 @@ endif ifeq ($(subdir),elf) sysdep-dl-routines += dl-get-cpu-features +sysdep_headers += cpu-features.h tests += tst-get-cpu-features tst-get-cpu-features-static tests-static += tst-get-cpu-features-static diff --git a/sysdeps/x86/Versions b/sysdeps/x86/Versions index e02923708e..04596dd688 100644 --- a/sysdeps/x86/Versions +++ b/sysdeps/x86/Versions @@ -1,5 +1,5 @@ ld { - GLIBC_PRIVATE { + GLIBC_2.32 { __get_cpu_features; } } diff --git a/sysdeps/x86/cpu-features.h b/sysdeps/x86/cpu-features.h index de20044417..5c4e1a726a 100644 --- a/sysdeps/x86/cpu-features.h +++ b/sysdeps/x86/cpu-features.h @@ -1,4 +1,5 @@ -/* This file is part of the GNU C Library. +/* Data structure for x86 CPU features. + This file is part of the GNU C Library. Copyright (C) 2008-2020 Free Software Foundation, Inc. The GNU C Library is free software; you can redistribute it and/or @@ -15,8 +16,8 @@ License along with the GNU C Library; if not, see . */ -#ifndef cpu_features_h -#define cpu_features_h +#ifndef _CPU_FEATURES_H +#define _CPU_FEATURES_H enum { @@ -72,34 +73,12 @@ struct cpu_features struct cpuid_registers cpuid[COMMON_CPUID_INDEX_MAX]; unsigned int feature[FEATURE_INDEX_MAX]; struct cpu_features_basic basic; - /* The state size for XSAVEC or XSAVE. The type must be unsigned long - int so that we use - - sub xsave_state_size_offset(%rip) %RSP_LP - - in _dl_runtime_resolve. */ - unsigned long int xsave_state_size; - /* The full state size for XSAVE when XSAVEC is disabled by - - GLIBC_TUNABLES=glibc.cpu.hwcaps=-XSAVEC_Usable - */ - unsigned int xsave_state_full_size; - /* Data cache size for use in memory and string routines, typically - L1 size. */ - unsigned long int data_cache_size; - /* Shared cache size for use in memory and string routines, typically - L2 or L3 size. */ - unsigned long int shared_cache_size; - /* Threshold to use non temporal store. */ - unsigned long int non_temporal_threshold; }; -/* Used from outside of glibc to get access to the CPU features - structure. */ +/* Get a pointer to the CPU features structure. */ extern const struct cpu_features *__get_cpu_features (void) __attribute__ ((const)); -/* Only used directly in cpu-features.c. */ # define CPU_FEATURES_CPU_P(ptr, name) \ ((ptr->cpuid[index_cpu_##name].reg_##name & (bit_cpu_##name)) != 0) # define CPU_FEATURES_ARCH_P(ptr, name) \ @@ -976,29 +955,4 @@ extern const struct cpu_features *__get_cpu_features (void) #define bit_ZMM0_15_state (1u << 6) #define bit_ZMM16_31_state (1u << 7) -# if defined (_LIBC) && !IS_IN (nonlib) -/* 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])) -# endif - -#ifdef __x86_64__ -# define HAS_CPUID 1 -#elif (defined __i586__ || defined __pentium__ \ - || defined __geode__ || defined __k6__) -# define HAS_CPUID 1 -# define HAS_I586 1 -# define HAS_I686 HAS_ARCH_FEATURE (I686) -#elif defined __i486__ -# define HAS_CPUID 0 -# define HAS_I586 HAS_ARCH_FEATURE (I586) -# define HAS_I686 HAS_ARCH_FEATURE (I686) -#else -# define HAS_CPUID 1 -# define HAS_I586 1 -# define HAS_I686 1 -#endif - -#endif /* cpu_features_h */ +#endif /* _CPU_FEATURES_H */ diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h new file mode 100644 index 0000000000..e2a303dc1f --- /dev/null +++ b/sysdeps/x86/include/cpu-features.h @@ -0,0 +1,95 @@ +/* Data structure for x86 CPU features. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _PRIVATE_CPU_FEATURES_H +#define _PRIVATE_CPU_FEATURES_H 1 + +#ifdef _CPU_FEATURES_H +# error this should be impossible +#endif + +#ifndef _ISOMAC +/* Get most of the contents from the public header, but we define a + different `struct cpu_features' type for private use. */ +# define cpu_features cpu_features_public +# define __get_cpu_features __get_cpu_features_public +#endif + +#include + +#ifndef _ISOMAC + +# undef cpu_features +# undef __get_cpu_features + +/* Forward declaration. */ +struct cpu_features +{ + struct cpuid_registers cpuid[COMMON_CPUID_INDEX_MAX]; + unsigned int feature[FEATURE_INDEX_MAX]; + struct cpu_features_basic basic; + /* The state size for XSAVEC or XSAVE. The type must be unsigned long + int so that we use + + sub xsave_state_size_offset(%rip) %RSP_LP + + in _dl_runtime_resolve. */ + unsigned long int xsave_state_size; + /* The full state size for XSAVE when XSAVEC is disabled by + + GLIBC_TUNABLES=glibc.cpu.hwcaps=-XSAVEC_Usable + */ + unsigned int xsave_state_full_size; + /* Data cache size for use in memory and string routines, typically + L1 size. */ + unsigned long int data_cache_size; + /* Shared cache size for use in memory and string routines, typically + L2 or L3 size. */ + unsigned long int shared_cache_size; + /* Threshold to use non temporal store. */ + unsigned long int non_temporal_threshold; +}; + +# if defined (_LIBC) && !IS_IN (nonlib) +/* 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])) +# endif + +# ifdef __x86_64__ +# define HAS_CPUID 1 +# elif (defined __i586__ || defined __pentium__ \ + || defined __geode__ || defined __k6__) +# define HAS_CPUID 1 +# define HAS_I586 1 +# define HAS_I686 HAS_ARCH_FEATURE (I686) +# elif defined __i486__ +# define HAS_CPUID 0 +# define HAS_I586 HAS_ARCH_FEATURE (I586) +# define HAS_I686 HAS_ARCH_FEATURE (I686) +# else +# define HAS_CPUID 1 +# define HAS_I586 1 +# define HAS_I686 1 +# endif + +#endif /* !_ISOMAC */ + +#endif /* include/link.h */