| 1 |
.. SPDX-License-Identifier: GPL-2.0 |
| 2 |
|
| 3 |
================= |
| 4 |
x86 Feature Flags |
| 5 |
================= |
| 6 |
|
| 7 |
Introduction |
| 8 |
============ |
| 9 |
|
| 10 |
The list of feature flags in /proc/cpuinfo is not complete and |
| 11 |
represents an ill-fated attempt from long time ago to put feature flags |
| 12 |
in an easy to find place for userspace. |
| 13 |
|
| 14 |
However, the number of feature flags is growing with each CPU generation, |
| 15 |
leading to unparseable and unwieldy /proc/cpuinfo. |
| 16 |
|
| 17 |
What is more, those feature flags do not even need to be in that file |
| 18 |
because userspace doesn't care about them - glibc et al already use |
| 19 |
CPUID to find out what the target machine supports and what not. |
| 20 |
|
| 21 |
And even if it doesn't show a particular feature flag - although the CPU |
| 22 |
still does have support for the respective hardware functionality and |
| 23 |
said CPU supports CPUID faulting - userspace can simply probe for the |
| 24 |
feature and figure out if it is supported or not, regardless of whether |
| 25 |
it is being advertised somewhere. |
| 26 |
|
| 27 |
Furthermore, those flag strings become an ABI the moment they appear |
| 28 |
there and maintaining them forever when nothing even uses them is a lot |
| 29 |
of wasted effort. |
| 30 |
|
| 31 |
So, the current use of /proc/cpuinfo is to show features which the |
| 32 |
kernel has *enabled* and *supports*. As in: the CPUID feature flag is |
| 33 |
there, there's an additional setup which the kernel has done while |
| 34 |
booting and the functionality is ready to use. A perfect example for |
| 35 |
that is "user_shstk" where additional code enablement is present in the |
| 36 |
kernel to support shadow stack for user programs. |
| 37 |
|
| 38 |
So, if users want to know if a feature is available on a given system, |
| 39 |
they try to find the flag in /proc/cpuinfo. If a given flag is present, |
| 40 |
it means that |
| 41 |
|
| 42 |
* the kernel knows about the feature enough to have an X86_FEATURE bit |
| 43 |
|
| 44 |
* the kernel supports it and is currently making it available either to |
| 45 |
userspace or some other part of the kernel |
| 46 |
|
| 47 |
* if the flag represents a hardware feature the hardware supports it. |
| 48 |
|
| 49 |
The absence of a flag in /proc/cpuinfo by itself means almost nothing to |
| 50 |
an end user. |
| 51 |
|
| 52 |
On the one hand, a feature like "vaes" might be fully available to user |
| 53 |
applications on a kernel that has not defined X86_FEATURE_VAES and thus |
| 54 |
there is no "vaes" in /proc/cpuinfo. |
| 55 |
|
| 56 |
On the other hand, a new kernel running on non-VAES hardware would also |
| 57 |
have no "vaes" in /proc/cpuinfo. There's no way for an application or |
| 58 |
user to tell the difference. |
| 59 |
|
| 60 |
The end result is that the flags field in /proc/cpuinfo is marginally |
| 61 |
useful for kernel debugging, but not really for anything else. |
| 62 |
Applications should instead use things like the glibc facilities for |
| 63 |
querying CPU support. Users should rely on tools like |
| 64 |
tools/arch/x86/kcpuid and cpuid(1). |
| 65 |
|
| 66 |
Regarding implementation, flags appearing in /proc/cpuinfo have an |
| 67 |
X86_FEATURE definition in arch/x86/include/asm/cpufeatures.h. These flags |
| 68 |
represent hardware features as well as software features. |
| 69 |
|
| 70 |
If the kernel cares about a feature or KVM want to expose the feature to |
| 71 |
a KVM guest, it should only then expose it to the guest when the guest |
| 72 |
needs to parse /proc/cpuinfo. Which, as mentioned above, is highly |
| 73 |
unlikely. KVM can synthesize the CPUID bit and the KVM guest can simply |
| 74 |
query CPUID and figure out what the hypervisor supports and what not. As |
| 75 |
already stated, /proc/cpuinfo is not a dumping ground for useless |
| 76 |
feature flags. |
| 77 |
|
| 78 |
|
| 79 |
How are feature flags created? |
| 80 |
============================== |
| 81 |
|
| 82 |
Feature flags can be derived from the contents of CPUID leaves |
| 83 |
-------------------------------------------------------------- |
| 84 |
|
| 85 |
These feature definitions are organized mirroring the layout of CPUID |
| 86 |
leaves and grouped in words with offsets as mapped in enum cpuid_leafs |
| 87 |
in cpufeatures.h (see arch/x86/include/asm/cpufeatures.h for details). |
| 88 |
If a feature is defined with a X86_FEATURE_<name> definition in |
| 89 |
cpufeatures.h, and if it is detected at run time, the flags will be |
| 90 |
displayed accordingly in /proc/cpuinfo. For example, the flag "avx2" |
| 91 |
comes from X86_FEATURE_AVX2 in cpufeatures.h. |
| 92 |
|
| 93 |
Flags can be from scattered CPUID-based features |
| 94 |
------------------------------------------------ |
| 95 |
|
| 96 |
Hardware features enumerated in sparsely populated CPUID leaves get |
| 97 |
software-defined values. Still, CPUID needs to be queried to determine |
| 98 |
if a given feature is present. This is done in init_scattered_cpuid_features(). |
| 99 |
For instance, X86_FEATURE_CQM_LLC is defined as 11*32 + 0 and its presence is |
| 100 |
checked at runtime in the respective CPUID leaf [EAX=f, ECX=0] bit EDX[1]. |
| 101 |
|
| 102 |
The intent of scattering CPUID leaves is to not bloat struct |
| 103 |
cpuinfo_x86.x86_capability[] unnecessarily. For instance, the CPUID leaf |
| 104 |
[EAX=7, ECX=0] has 30 features and is dense, but the CPUID leaf [EAX=7, EAX=1] |
| 105 |
has only one feature and would waste 31 bits of space in the x86_capability[] |
| 106 |
array. Since there is a struct cpuinfo_x86 for each possible CPU, the wasted |
| 107 |
memory is not trivial. |
| 108 |
|
| 109 |
Flags can be created synthetically under certain conditions for hardware features |
| 110 |
--------------------------------------------------------------------------------- |
| 111 |
|
| 112 |
Examples of conditions include whether certain features are present in |
| 113 |
MSR_IA32_CORE_CAPS or specific CPU models are identified. If the needed |
| 114 |
conditions are met, the features are enabled by the set_cpu_cap or |
| 115 |
setup_force_cpu_cap macros. For example, if bit 5 is set in MSR_IA32_CORE_CAPS, |
| 116 |
the feature X86_FEATURE_SPLIT_LOCK_DETECT will be enabled and |
| 117 |
"split_lock_detect" will be displayed. The flag "ring3mwait" will be |
| 118 |
displayed only when running on INTEL_XEON_PHI_[KNL|KNM] processors. |
| 119 |
|
| 120 |
Flags can represent purely software features |
| 121 |
-------------------------------------------- |
| 122 |
These flags do not represent hardware features. Instead, they represent a |
| 123 |
software feature implemented in the kernel. For example, Kernel Page Table |
| 124 |
Isolation is purely software feature and its feature flag X86_FEATURE_PTI is |
| 125 |
also defined in cpufeatures.h. |
| 126 |
|
| 127 |
Naming of Flags |
| 128 |
=============== |
| 129 |
|
| 130 |
The script arch/x86/kernel/cpu/mkcapflags.sh processes the |
| 131 |
#define X86_FEATURE_<name> from cpufeatures.h and generates the |
| 132 |
x86_cap/bug_flags[] arrays in kernel/cpu/capflags.c. The names in the |
| 133 |
resulting x86_cap/bug_flags[] are used to populate /proc/cpuinfo. The naming |
| 134 |
of flags in the x86_cap/bug_flags[] are as follows: |
| 135 |
|
| 136 |
Flags do not appear by default in /proc/cpuinfo |
| 137 |
----------------------------------------------- |
| 138 |
|
| 139 |
Feature flags are omitted by default from /proc/cpuinfo as it does not make |
| 140 |
sense for the feature to be exposed to userspace in most cases. For example, |
| 141 |
X86_FEATURE_ALWAYS is defined in cpufeatures.h but that flag is an internal |
| 142 |
kernel feature used in the alternative runtime patching functionality. So the |
| 143 |
flag does not appear in /proc/cpuinfo. |
| 144 |
|
| 145 |
Specify a flag name if absolutely needed |
| 146 |
---------------------------------------- |
| 147 |
|
| 148 |
If the comment on the line for the #define X86_FEATURE_* starts with a |
| 149 |
double-quote character (""), the string inside the double-quote characters |
| 150 |
will be the name of the flags. For example, the flag "sse4_1" comes from |
| 151 |
the comment "sse4_1" following the X86_FEATURE_XMM4_1 definition. |
| 152 |
|
| 153 |
There are situations in which overriding the displayed name of the flag is |
| 154 |
needed. For instance, /proc/cpuinfo is a userspace interface and must remain |
| 155 |
constant. If, for some reason, the naming of X86_FEATURE_<name> changes, one |
| 156 |
shall override the new naming with the name already used in /proc/cpuinfo. |
| 157 |
|
| 158 |
Flags are missing when one or more of these happen |
| 159 |
================================================== |
| 160 |
|
| 161 |
The hardware does not enumerate support for it |
| 162 |
---------------------------------------------- |
| 163 |
|
| 164 |
For example, when a new kernel is running on old hardware or the feature is |
| 165 |
not enabled by boot firmware. Even if the hardware is new, there might be a |
| 166 |
problem enabling the feature at run time, the flag will not be displayed. |
| 167 |
|
| 168 |
The kernel does not know about the flag |
| 169 |
--------------------------------------- |
| 170 |
|
| 171 |
For example, when an old kernel is running on new hardware. |
| 172 |
|
| 173 |
The kernel disabled support for it at compile-time |
| 174 |
-------------------------------------------------- |
| 175 |
|
| 176 |
For example, if Linear Address Masking (LAM) is not enabled when building (i.e., |
| 177 |
CONFIG_ADDRESS_MASKING is not selected) the flag "lam" will not show up. |
| 178 |
Even though the feature will still be detected via CPUID, the kernel disables |
| 179 |
it by clearing via setup_clear_cpu_cap(X86_FEATURE_LAM). |
| 180 |
|
| 181 |
The feature is disabled at boot-time |
| 182 |
------------------------------------ |
| 183 |
A feature can be disabled either using a command-line parameter or because |
| 184 |
it failed to be enabled. The command-line parameter clearcpuid= can be used |
| 185 |
to disable features using the feature number as defined in |
| 186 |
/arch/x86/include/asm/cpufeatures.h. For instance, User Mode Instruction |
| 187 |
Protection can be disabled using clearcpuid=514. The number 514 is calculated |
| 188 |
from #define X86_FEATURE_UMIP (16*32 + 2). |
| 189 |
|
| 190 |
In addition, there exists a variety of custom command-line parameters that |
| 191 |
disable specific features. The list of parameters includes, but is not limited |
| 192 |
to, nofsgsbase, nosgx, noxsave, etc. 5-level paging can also be disabled using |
| 193 |
"no5lvl". |
| 194 |
|
| 195 |
The feature was known to be non-functional |
| 196 |
------------------------------------------ |
| 197 |
|
| 198 |
The feature was known to be non-functional because a dependency was |
| 199 |
missing at runtime. For example, AVX flags will not show up if XSAVE feature |
| 200 |
is disabled since they depend on XSAVE feature. Another example would be broken |
| 201 |
CPUs and them missing microcode patches. Due to that, the kernel decides not to |
| 202 |
enable a feature. |