From patchwork Thu Jun 18 00:08:52 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: 1311693 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=RN/RSXR7; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49nMkS6PyFz9sSF for ; Thu, 18 Jun 2020 10:09:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F114388E822; Thu, 18 Jun 2020 00:09:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F114388E822 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592438972; bh=eUkXEF3drk1aRZF6PDNWfw3txhtywLbtaaJLHOkozRU=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=RN/RSXR7vHQVkmdcP0aaMSWZnnlsEeK274wXQ4z16zaCowHibv9113p/RBhN31Gtg Jo1sj1rXoqY43BFYeZBDZHGIubYZoj0F4BVVObc6xkVKLbRUlvhl20GVNR/Zd4lfxb NPDq3xpY/LrM717FXWvssRfhlgs3jeyR/YwKACgo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x142.google.com (mail-il1-x142.google.com [IPv6:2607:f8b0:4864:20::142]) by sourceware.org (Postfix) with ESMTPS id D4EAC388E822 for ; Thu, 18 Jun 2020 00:09:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D4EAC388E822 Received: by mail-il1-x142.google.com with SMTP id b5so4129435iln.5 for ; Wed, 17 Jun 2020 17:09:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=eUkXEF3drk1aRZF6PDNWfw3txhtywLbtaaJLHOkozRU=; b=OR8Xck2KC+/etItPFpH/J0hGcyytMpmFQ43t5pWylDR9+Nj6ZcmRBOR3XElU9D7NMT p4z4mrZ5pTksyyDc69qZzHfD1CftXUdUOC1j3FZOyGUK8cf2NZ0APSUNCeDbGQ5wgi6O Gg66U9M9LI6yYLySSeZeQnz6vqFyHYMh3ggfkkp/WFd77ZLTWzwhzsROMaGGyBZC8f+F x7EAvQKt4Ztyo26gw98jsUlh97iWscs11tuHg0l4r83nRQ4ArEeiPsgTGZpDgU4Ro+Hh IpzhBxxapyNMAmrstp0qaRSCMJb/11z60FMfReeuX0v6+w1KyDK/V6yyS+Dv3PnvFK90 laFw== X-Gm-Message-State: AOAM5308V2s36afXFTY/Z0MPV3OiwyyRPWHo/+Nk5n/2K27ZTzaLrgFq yRpIWezviG2iagS5zS9NNp5mS15ttM5gWvARVNhQP5M2 X-Google-Smtp-Source: ABdhPJx9Gd0JZ1CTTCfyc8cVRJ9b8T+2OEKeaxSmbBRSANHQzUNLNiRwbItIdgtQg9SsMmKw/8hBa9LQSBqyzHdeN4Y= X-Received: by 2002:a92:d147:: with SMTP id t7mr1503131ilg.151.1592438968194; Wed, 17 Jun 2020 17:09:28 -0700 (PDT) MIME-Version: 1.0 References: <20200617193100.1115529-1-hjl.tools@gmail.com> In-Reply-To: Date: Wed, 17 Jun 2020 17:08:52 -0700 Message-ID: Subject: [PATCH] x86: Install [BZ #26124] To: Joseph Myers X-Spam-Status: No, score=-9.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" Cc: GNU C Library Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Wed, Jun 17, 2020 at 1:54 PM Joseph Myers wrote: > > On Wed, 17 Jun 2020, H.J. Lu via Libc-alpha wrote: > > > Install so that programmers can do > > This appears to be x86-specific. The convention for such > architecture-specific headers is names such as sys/platform/x86.h (with > corresponding naming conventions for individual symbols as documented in > maint.texi). Done. > This patch needs documentation in the glibc manual of the API provided by > this header (which would also say that it's x86-specific, and go in > platform.texi alongside other such documentation of architecture-specific > interfaces). Done. > New features should also have NEWS entries. > Done. Here is the updated patch to provide . Thanks. From 588eaccaf70fdecb6a3f76fe892a97c57ccf051b Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 17 Jun 2020 09:12:18 -0700 Subject: [PATCH] x86: Install [BZ #26124] 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 *__x86_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 . --- NEWS | 2 + manual/platform.texi | 28 +++++ 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 | 4 +- sysdeps/x86/dl-get-cpu-features.c | 4 +- sysdeps/x86/include/cpu-features.h | 102 ++++++++++++++++++ .../{cpu-features.h => sys/platform/x86.h} | 71 ++---------- sysdeps/x86/tst-get-cpu-features.c | 6 +- 11 files changed, 152 insertions(+), 69 deletions(-) create mode 100644 sysdeps/x86/include/cpu-features.h rename sysdeps/x86/{cpu-features.h => sys/platform/x86.h} (94%) diff --git a/NEWS b/NEWS index a660fc59a8..ae7d1ece35 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,8 @@ Version 2.32 Major new features: +* Add to provide query macros for x86 CPU features. + * Unicode 12.1.0 Support: Character encoding, character type info, and transliteration tables are all updated to Unicode 12.1.0, using generator scripts contributed by Mike FABIAN (Red Hat). diff --git a/manual/platform.texi b/manual/platform.texi index 504addc956..0a11f52612 100644 --- a/manual/platform.texi +++ b/manual/platform.texi @@ -7,6 +7,7 @@ @menu * PowerPC:: Facilities Specific to the PowerPC Architecture * RISC-V:: Facilities Specific to the RISC-V Architecture +* X86:: Facilities Specific to the X86 Architecture @end menu @node PowerPC @@ -134,3 +135,30 @@ all threads in the current process. Setting the ordering on only the current thread is necessary. All other flag bits are reserved. @end deftypefun + +@node X86 +@appendixsec X86-specific Facilities + +Facilities specific to X86 that are not specific to a particular +operating system are declared in @file{sys/platform/x86.h}. + +@deftypefun {const struct cpu_features *} __x86_get_cpu_features (void) +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} +Return a pointer to x86 CPU feature structure used by query macros for x86 +CPU features. +@end deftypefun + +@deftypefn Macro int HAS_CPU_FEATURE (@var{name}) +This macro returns a nonzero value (true) if the processor feature +@var{name} is available. +@end deftypefn + +@deftypefn Macro int CPU_FEATURE_USABLE (@var{name}) +This macro returns a nonzero value (true) if the processor feature +@var{name} is usable. +@end deftypefn + +@deftypefn Macro int HAS_ARCH_FEATURE (@var{name}) +This macro returns a nonzero value (true) if the architecture feature +@var{name} is available. +@end deftypefn diff --git a/sysdeps/unix/sysv/linux/i386/ld.abilist b/sysdeps/unix/sysv/linux/i386/ld.abilist index 0478e22071..1226876689 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 __x86_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..886e57abd5 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 __x86_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..0d2f8a2cc5 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 __x86_get_cpu_features F diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile index beab426f67..0e4d132803 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 += sys/platform/x86.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..7e3139dbb1 100644 --- a/sysdeps/x86/Versions +++ b/sysdeps/x86/Versions @@ -1,5 +1,5 @@ ld { - GLIBC_PRIVATE { - __get_cpu_features; + GLIBC_2.32 { + __x86_get_cpu_features; } } diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c index 9d61cd56be..fa1a1caa87 100644 --- a/sysdeps/x86/dl-get-cpu-features.c +++ b/sysdeps/x86/dl-get-cpu-features.c @@ -18,10 +18,10 @@ #include -#undef __get_cpu_features +#undef __x86_get_cpu_features const struct cpu_features * -__get_cpu_features (void) +__x86_get_cpu_features (void) { return &GLRO(dl_x86_cpu_features); } diff --git a/sysdeps/x86/include/cpu-features.h b/sysdeps/x86/include/cpu-features.h new file mode 100644 index 0000000000..081483898e --- /dev/null +++ b/sysdeps/x86/include/cpu-features.h @@ -0,0 +1,102 @@ +/* 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 __x86_get_cpu_features __x86_get_cpu_features_public +#endif + +#include + +#ifndef _ISOMAC + +# undef cpu_features +# undef __x86_get_cpu_features +# define __get_cpu_features __x86_get_cpu_features + +/* XCR0 Feature flags. */ +# define bit_XMM_state (1u << 1) +# define bit_YMM_state (1u << 2) +# define bit_Opmask_state (1u << 5) +# define bit_ZMM0_15_state (1u << 6) +# define bit_ZMM16_31_state (1u << 7) + +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 __x86_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/cpu-features.h */ diff --git a/sysdeps/x86/cpu-features.h b/sysdeps/x86/sys/platform/x86.h similarity index 94% rename from sysdeps/x86/cpu-features.h rename to sysdeps/x86/sys/platform/x86.h index de20044417..2acb7fb5fa 100644 --- a/sysdeps/x86/cpu-features.h +++ b/sysdeps/x86/sys/platform/x86.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 _SYS_PLATFORM_X86_H +#define _SYS_PLATFORM_X86_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. */ -extern const struct cpu_features *__get_cpu_features (void) +/* Get a pointer to the CPU features structure. */ +extern const struct cpu_features *__x86_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) \ @@ -107,11 +86,11 @@ extern const struct cpu_features *__get_cpu_features (void) /* HAS_CPU_FEATURE evaluates to true if CPU supports the feature. */ #define HAS_CPU_FEATURE(name) \ - CPU_FEATURES_CPU_P (__get_cpu_features (), name) + CPU_FEATURES_CPU_P (__x86_get_cpu_features (), name) /* HAS_ARCH_FEATURE evaluates to true if we may use the feature at runtime. */ # define HAS_ARCH_FEATURE(name) \ - CPU_FEATURES_ARCH_P (__get_cpu_features (), name) + CPU_FEATURES_ARCH_P (__x86_get_cpu_features (), name) /* CPU_FEATURE_USABLE evaluates to true if the feature is usable. */ #define CPU_FEATURE_USABLE(name) \ ((need_arch_feature_##name && HAS_ARCH_FEATURE (name##_Usable)) \ @@ -969,36 +948,4 @@ extern const struct cpu_features *__get_cpu_features (void) #define index_arch_MathVec_Prefer_No_AVX512 FEATURE_INDEX_2 #define index_arch_Prefer_FSRM FEATURE_INDEX_2 -/* XCR0 Feature flags. */ -#define bit_XMM_state (1u << 1) -#define bit_YMM_state (1u << 2) -#define bit_Opmask_state (1u << 5) -#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 /* _SYS_PLATFORM_X86_H */ diff --git a/sysdeps/x86/tst-get-cpu-features.c b/sysdeps/x86/tst-get-cpu-features.c index 815fdb664a..010c2e2224 100644 --- a/sysdeps/x86/tst-get-cpu-features.c +++ b/sysdeps/x86/tst-get-cpu-features.c @@ -1,4 +1,4 @@ -/* Test case for x86 __get_cpu_features interface +/* Test case for __x86_get_cpu_features interface Copyright (C) 2015-2020 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -18,7 +18,7 @@ #include #include -#include +#include #include #define CHECK_CPU_FEATURE(name) \ @@ -45,7 +45,7 @@ static const char * const cpu_kinds[] = static int do_test (void) { - const struct cpu_features *cpu_features = __get_cpu_features (); + const struct cpu_features *cpu_features = __x86_get_cpu_features (); switch (cpu_features->basic.kind) { -- 2.26.2