cpuinfo.rst 9.2 KB

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