[PATCH] <sys/platform/x86.h>: Remove the C preprocessor magic

Florian Weimer fweimer@redhat.com
Wed Jan 20 09:53:07 GMT 2021


* H. J. Lu via Libc-alpha:

> +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,
> +  COMMON_CPUID_INDEX_7_ECX_1,
> +  COMMON_CPUID_INDEX_19,
> +  /* Keep the following line at the end.  */
> +  COMMON_CPUID_INDEX_MAX
> +};

COMMON_CPUID_INDEX_MAX should not be in the public header because it
subject to changes.

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

Should the non-public interfaces use __?

> +enum cpu_features_kind
> +{
> +  arch_kind_unknown = 0,
> +  arch_kind_intel,
> +  arch_kind_amd,
> +  arch_kind_zhaoxin,
> +  arch_kind_other
> +};
> +
> +struct cpu_features_basic
> +{
> +  enum cpu_features_kind kind;
> +  int max_cpuid;
> +  unsigned int family;
> +  unsigned int model;
> +  unsigned int stepping;
> +};

Should we really expose all this?  It's not documented.  enum
cpu_features_kind doesn't seem to be compatible with how people use
virtualization.

> diff --git a/sysdeps/x86/dl-get-cpu-features.c b/sysdeps/x86/dl-get-cpu-features.c
> index 349472d99f..4636d9f4a7 100644
> --- a/sysdeps/x86/dl-get-cpu-features.c
> +++ b/sysdeps/x86/dl-get-cpu-features.c
> @@ -46,9 +46,9 @@ __ifunc (__x86_cpu_features, __x86_cpu_features, NULL, void,
>  #undef __x86_get_cpu_features
>  
>  const struct cpu_features *
> -__x86_get_cpu_features (unsigned int max)
> +__x86_get_cpu_features (unsigned int index)
>  {
> -  if (max > COMMON_CPUID_INDEX_MAX)
> +  if (index > COMMON_CPUID_INDEX_MAX * 8 * sizeof (unsigned int) * 4)
>      return NULL;
>    return &GLRO(dl_x86_cpu_features);
>  }

This should work in principle (but I haven't verified the boundary
condition).

Could you pass the COMMON_CPUID_INDEX_* value (without the register and
bit selectors)?  Then more calls can be subject to common subexpression
elimination in the caller, leading to more compact code.

You could perhaps return a pointer to a zero struct in case the index is
too large.  This way, the caller does not have to check for a null
pointer.

The result should be very close to what we had before in terms of
generated code, I hope.

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill



More information about the Libc-alpha mailing list