Re: [RESEND v1 2/3] x86/cpufeatures: Generate a feature mask header based on build config

From: Xin Li
Date: Sat Jun 22 2024 - 13:35:29 EST


On 6/17/2024 6:46 AM, H. Peter Anvin wrote:
On June 16, 2024 12:26:48 AM PDT, Xin Li <xin@xxxxxxxxx> wrote:
On 5/9/2024 1:53 PM, Xin Li (Intel) wrote:
From: "H. Peter Anvin (Intel)" <hpa@xxxxxxxxx>

Introduce an AWK script to auto-generate a header with required and
disabled feature masks based on <asm/cpufeatures.h> and current build
config. Thus for any CPU feature with a build config, e.g., X86_FRED,
simply add

...

+
+ printf "\n#define %s_MASKS ", s;
+ pfx = "{";
+ for (i = 0; i < ncapints; i++) {
+ printf "%s \\\n\t%s_MASK_%d", pfx, s, i;
+ pfx = ",";
+ }
+ printf " \\\n}\n\n";
+
+ printf "#define %s_FEATURE(x) \\\n", s;
+ printf "\t((( ";
+ for (i = 0; i < ncapints; i++) {
+ if (masks[i]) {
+ printf "\\\n\t\t((x) >> 5) == %2d ? %s_MASK%-3d : ", i, s, i;
+ }
+ }
+ printf "0 \\\n";
+ printf "\t) >> ((x) & 31)) & 1)\n\n";

This code generates macros {REQUIRED,DISABLED}_FEATURE(x) to tell if a
CPU feature, e.g., X86_FEATURE_FRED, is a required or disabled feature
for this particular compile-time configuration.

But they are NOT currently used, so I prefer to remove them for now.

Thanks!
Xin

The goal with these is that it can eliminate the handwritten code that tests a long list of masks. Again, automation.


This piece of code is added back in the last patch of v3:

https://lore.kernel.org/lkml/20240622171435.3725548-5-xin@xxxxxxxxx/

The generated macros {REQUIRED|DISABLED}_MASK_BIT_SET are way shorter
than that in the original form:

#define REQUIRED_FEATURE(x) \
(( \
((x) >> 5) == 0 ? REQUIRED_MASK0 : \
((x) >> 5) == 1 ? REQUIRED_MASK1 : \
((x) >> 5) == 3 ? REQUIRED_MASK3 : 0 \
) & (1 << ((x) & 31)))

#define REQUIRED_MASK_BIT_SET(x) \
(REQUIRED_FEATURE(x) || BUILD_BUG_ON_ZERO(NCAPINTS != 22))

Thanks!
Xin