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