public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* RFC: Add <sys/platform/x86.h> to glibc 2.29
@ 2018-07-29 16:16 H.J. Lu
  2018-07-31 13:32 ` Carlos O'Donell
  2018-08-17 14:50 ` Florian Weimer
  0 siblings, 2 replies; 12+ messages in thread
From: H.J. Lu @ 2018-07-29 16:16 UTC (permalink / raw)
  To: GNU C Library

I am proposing to add <sys/platform/x86.h> to glibc 2.29 to provide
an API to access x86 specific platform features:

enum
  {
    /* The integer bit array index of architecture feature bits.  */
    FEATURE_INDEX_1 = 0,
    FEATURE_INDEX_2,
    /* The current maximum size of the feature integer bit array.  */
    FEATURE_INDEX_MAX
  };

enum
  {
    COMMON_CPUID_INDEX_1 = 0,
    COMMON_CPUID_INDEX_7,
    COMMON_CPUID_INDEX_80000001,
    COMMON_CPUID_INDEX_D_ECX_1,
    COMMON_CPUID_INDEX_80000007,
    COMMON_CPUID_INDEX_80000008,
    /* Keep the following line at the end.  */
    COMMON_CPUID_INDEX_MAX
  };

enum cpu_features_kind
  {
    arch_kind_unknown = 0,
    arch_kind_intel,
    arch_kind_amd,
    arch_kind_other
  };

struct cpuid_registers
  {
    unsigned int eax;
    unsigned int ebx;
    unsigned int ecx;
    unsigned int edx;
  };

extern enum cpu_features_kind __x86_get_cpu_kind (void)
     __attribute__ ((const));

extern const struct cpuid_registers *__x86_get_cpuid_registers
     (unsigned int) __attribute__ ((const));

extern unsigned int __x86_get_arch_feature (unsigned int)
     __attribute__ ((const));

extern unsigned long long __x86_tsc_to_ns (unsigned long long);
extern unsigned long long __x86_ns_to_tsc (unsigned long long);

/* HAS_* evaluates to true if we may use the feature at runtime.  */
#define HAS_CPU_FEATURE(name) \
   ((__x86_get_cpuid_registers (index_cpu_##name)->reg_##name \
     & (bit_cpu_##name)) != 0)
#define HAS_ARCH_FEATURE(name) \
   ((__x86_get_arch_feature (index_arch_##name) & (bit_arch_##name)) != 0)

Users can use <sys/platform/x86.h> to detect if a CPU feature is
supported at run-time with

HAS_CPU_FEATURE (AVX)

which returns true if glibc detects that AVX is available.  Before AVX
can be used, he/she should check

HAS_ARCH_FEATURE (AVX_Usable)

which returns true if glibc detects that AVX is available and usable.

extern unsigned long long __x86_tsc_to_ns (unsigned long long);
extern unsigned long long __x86_ns_to_tsc (unsigned long long);

can be used to convert between TSCs and nanoseconds if

HAS_ARCH_FEATURE (TSC_To_NS_Usable)

returns true.

Backward binary compatibility is provided by:

1. __x86_get_cpu_kind will get a new version if a new CPU kind is added
to cpu_features_kind.
2. __x86_get_cpuid_registers returns a pointer to a cpuid_registers
with all zeros if cpuid index >= COMMON_CPUID_INDEX_MAX.
3. __x86_get_arch_feature returns 0 if architecture index >=
FEATURE_INDEX_MAX.

This means:

1. Applications linked against the new __x86_get_cpu_kind will only
run with the newer libc.so.
2. Applications linked against the new __x86_get_cpuid_registers and
__x86_get_arch_feature will run with the older libc.so.  But new
CPU and architecture features won't be detected by the older libc.so.

This program:

https://github.com/hjl-tools/glibc/blob/hjl/platform/master/sysdeps/x86/tst-x86-platform-1.c

may display:

[hjl@gnu-efi-2 build-x86_64-linux]$ ./elf/tst-x86-platform-1
Vendor: Intel
CPU features:
  SSE3
  PCLMULQDQ
  DTES64
  MONITOR
  DS_CPL
  VMX
  EST
  TM2
  SSSE3
  SDBG
  FMA
  CMPXCHG16B
  XTPRUPDCTRL
  PDCM
  PCID
  SSE4_1
  SSE4_2
  X2APIC
  MOVBE
  POPCOUNT
  TSC_DEADLINE
  AES
  XSAVE
  OSXSAVE
  AVX
  F16C
  RDRAND
  FPU
  VME
  DE
  PSE
  TSC
  MSR
  PAE
  MCE
  CX8
  APIC
  SEP
  MTRR
  PGE
  MCA
  CMOV
  PAT
  PSE_36
  DS
  ACPI
  MMX
  FXSR
  SSE
  SSE2
  SS
  HTT
  TM
  PBE
  FSGSBASE
  TSC_ADJUST
  SGX
  BMI1
  AVX2
  BMI2
  ERMS
  INVPCID
  MPX
  RDSEED
  ADX
  SMAP
  TRACE
  LAHF64_SAHF64
  LZCNT
  PREFETCHW
  SYSCALL_SYSRET
  NX
  PAGE1GB
  RDTSCP
  LM
  XSAVEOPT
  XSAVEC
  XGETBV_ECX_1
  XSAVES
  INVARIANT_TSC
Architecture features:
  AVX_Usable
  AVX2_Usable
  FMA_Usable
  XSAVEC_Usable
  TSC_To_NS_Usable
[hjl@gnu-efi-2 build-x86_64-linux]$

Any comments?


H.J.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2018-08-20 20:55 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-29 16:16 RFC: Add <sys/platform/x86.h> to glibc 2.29 H.J. Lu
2018-07-31 13:32 ` Carlos O'Donell
2018-07-31 14:49   ` H.J. Lu
2018-07-31 15:06     ` Carlos O'Donell
2018-07-31 16:17       ` H.J. Lu
2018-08-17 14:50 ` Florian Weimer
2018-08-17 15:46   ` H.J. Lu
2018-08-17 16:19     ` Florian Weimer
2018-08-17 16:44       ` H.J. Lu
2018-08-17 19:11         ` Florian Weimer
2018-08-20 11:29         ` Florian Weimer
2018-08-20 20:55           ` 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).