^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) x86 Feature Flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) =================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Introduction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) ============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) On x86, flags appearing in /proc/cpuinfo have an X86_FEATURE definition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) in arch/x86/include/asm/cpufeatures.h. If the kernel cares about a feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) or KVM want to expose the feature to a KVM guest, it can and should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) an X86_FEATURE_* defined. These flags represent hardware features as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) well as software features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) If users want to know if a feature is available on a given system, they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) try to find the flag in /proc/cpuinfo. If a given flag is present, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) means that the kernel supports it and is currently making it available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) If such flag represents a hardware feature, it also means that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) hardware supports it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) If the expected flag does not appear in /proc/cpuinfo, things are murkier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) Users need to find out the reason why the flag is missing and find the way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) how to enable it, which is not always easy. There are several factors that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) can explain missing flags: the expected feature failed to enable, the feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) is missing in hardware, platform firmware did not enable it, the feature is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) disabled at build or run time, an old kernel is in use, or the kernel does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) not support the feature and thus has not enabled it. In general, /proc/cpuinfo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) shows features which the kernel supports. For a full list of CPUID flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) which the CPU supports, use tools/arch/x86/kcpuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) How are feature flags created?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) a: Feature flags can be derived from the contents of CPUID leaves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) These feature definitions are organized mirroring the layout of CPUID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) leaves and grouped in words with offsets as mapped in enum cpuid_leafs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) in cpufeatures.h (see arch/x86/include/asm/cpufeatures.h for details).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) If a feature is defined with a X86_FEATURE_<name> definition in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) cpufeatures.h, and if it is detected at run time, the flags will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) displayed accordingly in /proc/cpuinfo. For example, the flag "avx2"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) comes from X86_FEATURE_AVX2 in cpufeatures.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) b: Flags can be from scattered CPUID-based features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) Hardware features enumerated in sparsely populated CPUID leaves get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) software-defined values. Still, CPUID needs to be queried to determine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if a given feature is present. This is done in init_scattered_cpuid_features().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) For instance, X86_FEATURE_CQM_LLC is defined as 11*32 + 0 and its presence is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) checked at runtime in the respective CPUID leaf [EAX=f, ECX=0] bit EDX[1].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) The intent of scattering CPUID leaves is to not bloat struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) cpuinfo_x86.x86_capability[] unnecessarily. For instance, the CPUID leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) [EAX=7, ECX=0] has 30 features and is dense, but the CPUID leaf [EAX=7, EAX=1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) has only one feature and would waste 31 bits of space in the x86_capability[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) array. Since there is a struct cpuinfo_x86 for each possible CPU, the wasted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) memory is not trivial.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) c: Flags can be created synthetically under certain conditions for hardware features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) -------------------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) Examples of conditions include whether certain features are present in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) MSR_IA32_CORE_CAPS or specific CPU models are identified. If the needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) conditions are met, the features are enabled by the set_cpu_cap or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) setup_force_cpu_cap macros. For example, if bit 5 is set in MSR_IA32_CORE_CAPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) the feature X86_FEATURE_SPLIT_LOCK_DETECT will be enabled and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) "split_lock_detect" will be displayed. The flag "ring3mwait" will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) displayed only when running on INTEL_FAM6_XEON_PHI_[KNL|KNM] processors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) d: Flags can represent purely software features.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) These flags do not represent hardware features. Instead, they represent a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) software feature implemented in the kernel. For example, Kernel Page Table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) Isolation is purely software feature and its feature flag X86_FEATURE_PTI is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) also defined in cpufeatures.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) Naming of Flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) The script arch/x86/kernel/cpu/mkcapflags.sh processes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define X86_FEATURE_<name> from cpufeatures.h and generates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) x86_cap/bug_flags[] arrays in kernel/cpu/capflags.c. The names in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) resulting x86_cap/bug_flags[] are used to populate /proc/cpuinfo. The naming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) of flags in the x86_cap/bug_flags[] are as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) a: The name of the flag is from the string in X86_FEATURE_<name> by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ----------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) By default, the flag <name> in /proc/cpuinfo is extracted from the respective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) X86_FEATURE_<name> in cpufeatures.h. For example, the flag "avx2" is from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) X86_FEATURE_AVX2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) b: The naming can be overridden.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) --------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) If the comment on the line for the #define X86_FEATURE_* starts with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) double-quote character (""), the string inside the double-quote characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) will be the name of the flags. For example, the flag "sse4_1" comes from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) the comment "sse4_1" following the X86_FEATURE_XMM4_1 definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) There are situations in which overriding the displayed name of the flag is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) needed. For instance, /proc/cpuinfo is a userspace interface and must remain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) constant. If, for some reason, the naming of X86_FEATURE_<name> changes, one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) shall override the new naming with the name already used in /proc/cpuinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) c: The naming override can be "", which means it will not appear in /proc/cpuinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ----------------------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) The feature shall be omitted from /proc/cpuinfo if it does not make sense for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) the feature to be exposed to userspace. For example, X86_FEATURE_ALWAYS is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) defined in cpufeatures.h but that flag is an internal kernel feature used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) in the alternative runtime patching functionality. So, its name is overridden
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) with "". Its flag will not appear in /proc/cpuinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) Flags are missing when one or more of these happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ==================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) a: The hardware does not enumerate support for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) --------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) For example, when a new kernel is running on old hardware or the feature is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) not enabled by boot firmware. Even if the hardware is new, there might be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) problem enabling the feature at run time, the flag will not be displayed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) b: The kernel does not know about the flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) -------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) For example, when an old kernel is running on new hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) c: The kernel disabled support for it at compile-time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) For example, if 5-level-paging is not enabled when building (i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) CONFIG_X86_5LEVEL is not selected) the flag "la57" will not show up [#f1]_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) Even though the feature will still be detected via CPUID, the kernel disables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) it by clearing via setup_clear_cpu_cap(X86_FEATURE_LA57).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) d: The feature is disabled at boot-time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ----------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) A feature can be disabled either using a command-line parameter or because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) it failed to be enabled. The command-line parameter clearcpuid= can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) to disable features using the feature number as defined in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /arch/x86/include/asm/cpufeatures.h. For instance, User Mode Instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) Protection can be disabled using clearcpuid=514. The number 514 is calculated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) from #define X86_FEATURE_UMIP (16*32 + 2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) In addition, there exists a variety of custom command-line parameters that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) disable specific features. The list of parameters includes, but is not limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) to, nofsgsbase, nosmap, and nosmep. 5-level paging can also be disabled using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) "no5lvl". SMAP and SMEP are disabled with the aforementioned parameters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) e: The feature was known to be non-functional.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ----------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) The feature was known to be non-functional because a dependency was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) missing at runtime. For example, AVX flags will not show up if XSAVE feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) is disabled since they depend on XSAVE feature. Another example would be broken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) CPUs and them missing microcode patches. Due to that, the kernel decides not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) enable a feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .. [#f1] 5-level paging uses linear address of 57 bits.