You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package cpu implements processor feature detection for
  5. // various CPU architectures.
  6. package cpu
  7. import (
  8. "os"
  9. "strings"
  10. )
  11. // Initialized reports whether the CPU features were initialized.
  12. //
  13. // For some GOOS/GOARCH combinations initialization of the CPU features depends
  14. // on reading an operating specific file, e.g. /proc/self/auxv on linux/arm
  15. // Initialized will report false if reading the file fails.
  16. var Initialized bool
  17. // CacheLinePad is used to pad structs to avoid false sharing.
  18. type CacheLinePad struct{ _ [cacheLineSize]byte }
  19. // X86 contains the supported CPU features of the
  20. // current X86/AMD64 platform. If the current platform
  21. // is not X86/AMD64 then all feature flags are false.
  22. //
  23. // X86 is padded to avoid false sharing. Further the HasAVX
  24. // and HasAVX2 are only set if the OS supports XMM and YMM
  25. // registers in addition to the CPUID feature bit being set.
  26. var X86 struct {
  27. _ CacheLinePad
  28. HasAES bool // AES hardware implementation (AES NI)
  29. HasADX bool // Multi-precision add-carry instruction extensions
  30. HasAVX bool // Advanced vector extension
  31. HasAVX2 bool // Advanced vector extension 2
  32. HasAVX512 bool // Advanced vector extension 512
  33. HasAVX512F bool // Advanced vector extension 512 Foundation Instructions
  34. HasAVX512CD bool // Advanced vector extension 512 Conflict Detection Instructions
  35. HasAVX512ER bool // Advanced vector extension 512 Exponential and Reciprocal Instructions
  36. HasAVX512PF bool // Advanced vector extension 512 Prefetch Instructions
  37. HasAVX512VL bool // Advanced vector extension 512 Vector Length Extensions
  38. HasAVX512BW bool // Advanced vector extension 512 Byte and Word Instructions
  39. HasAVX512DQ bool // Advanced vector extension 512 Doubleword and Quadword Instructions
  40. HasAVX512IFMA bool // Advanced vector extension 512 Integer Fused Multiply Add
  41. HasAVX512VBMI bool // Advanced vector extension 512 Vector Byte Manipulation Instructions
  42. HasAVX5124VNNIW bool // Advanced vector extension 512 Vector Neural Network Instructions Word variable precision
  43. HasAVX5124FMAPS bool // Advanced vector extension 512 Fused Multiply Accumulation Packed Single precision
  44. HasAVX512VPOPCNTDQ bool // Advanced vector extension 512 Double and quad word population count instructions
  45. HasAVX512VPCLMULQDQ bool // Advanced vector extension 512 Vector carry-less multiply operations
  46. HasAVX512VNNI bool // Advanced vector extension 512 Vector Neural Network Instructions
  47. HasAVX512GFNI bool // Advanced vector extension 512 Galois field New Instructions
  48. HasAVX512VAES bool // Advanced vector extension 512 Vector AES instructions
  49. HasAVX512VBMI2 bool // Advanced vector extension 512 Vector Byte Manipulation Instructions 2
  50. HasAVX512BITALG bool // Advanced vector extension 512 Bit Algorithms
  51. HasAVX512BF16 bool // Advanced vector extension 512 BFloat16 Instructions
  52. HasAMXTile bool // Advanced Matrix Extension Tile instructions
  53. HasAMXInt8 bool // Advanced Matrix Extension Int8 instructions
  54. HasAMXBF16 bool // Advanced Matrix Extension BFloat16 instructions
  55. HasBMI1 bool // Bit manipulation instruction set 1
  56. HasBMI2 bool // Bit manipulation instruction set 2
  57. HasCX16 bool // Compare and exchange 16 Bytes
  58. HasERMS bool // Enhanced REP for MOVSB and STOSB
  59. HasFMA bool // Fused-multiply-add instructions
  60. HasOSXSAVE bool // OS supports XSAVE/XRESTOR for saving/restoring XMM registers.
  61. HasPCLMULQDQ bool // PCLMULQDQ instruction - most often used for AES-GCM
  62. HasPOPCNT bool // Hamming weight instruction POPCNT.
  63. HasRDRAND bool // RDRAND instruction (on-chip random number generator)
  64. HasRDSEED bool // RDSEED instruction (on-chip random number generator)
  65. HasSSE2 bool // Streaming SIMD extension 2 (always available on amd64)
  66. HasSSE3 bool // Streaming SIMD extension 3
  67. HasSSSE3 bool // Supplemental streaming SIMD extension 3
  68. HasSSE41 bool // Streaming SIMD extension 4 and 4.1
  69. HasSSE42 bool // Streaming SIMD extension 4 and 4.2
  70. _ CacheLinePad
  71. }
  72. // ARM64 contains the supported CPU features of the
  73. // current ARMv8(aarch64) platform. If the current platform
  74. // is not arm64 then all feature flags are false.
  75. var ARM64 struct {
  76. _ CacheLinePad
  77. HasFP bool // Floating-point instruction set (always available)
  78. HasASIMD bool // Advanced SIMD (always available)
  79. HasEVTSTRM bool // Event stream support
  80. HasAES bool // AES hardware implementation
  81. HasPMULL bool // Polynomial multiplication instruction set
  82. HasSHA1 bool // SHA1 hardware implementation
  83. HasSHA2 bool // SHA2 hardware implementation
  84. HasCRC32 bool // CRC32 hardware implementation
  85. HasATOMICS bool // Atomic memory operation instruction set
  86. HasFPHP bool // Half precision floating-point instruction set
  87. HasASIMDHP bool // Advanced SIMD half precision instruction set
  88. HasCPUID bool // CPUID identification scheme registers
  89. HasASIMDRDM bool // Rounding double multiply add/subtract instruction set
  90. HasJSCVT bool // Javascript conversion from floating-point to integer
  91. HasFCMA bool // Floating-point multiplication and addition of complex numbers
  92. HasLRCPC bool // Release Consistent processor consistent support
  93. HasDCPOP bool // Persistent memory support
  94. HasSHA3 bool // SHA3 hardware implementation
  95. HasSM3 bool // SM3 hardware implementation
  96. HasSM4 bool // SM4 hardware implementation
  97. HasASIMDDP bool // Advanced SIMD double precision instruction set
  98. HasSHA512 bool // SHA512 hardware implementation
  99. HasSVE bool // Scalable Vector Extensions
  100. HasASIMDFHM bool // Advanced SIMD multiplication FP16 to FP32
  101. _ CacheLinePad
  102. }
  103. // ARM contains the supported CPU features of the current ARM (32-bit) platform.
  104. // All feature flags are false if:
  105. // 1. the current platform is not arm, or
  106. // 2. the current operating system is not Linux.
  107. var ARM struct {
  108. _ CacheLinePad
  109. HasSWP bool // SWP instruction support
  110. HasHALF bool // Half-word load and store support
  111. HasTHUMB bool // ARM Thumb instruction set
  112. Has26BIT bool // Address space limited to 26-bits
  113. HasFASTMUL bool // 32-bit operand, 64-bit result multiplication support
  114. HasFPA bool // Floating point arithmetic support
  115. HasVFP bool // Vector floating point support
  116. HasEDSP bool // DSP Extensions support
  117. HasJAVA bool // Java instruction set
  118. HasIWMMXT bool // Intel Wireless MMX technology support
  119. HasCRUNCH bool // MaverickCrunch context switching and handling
  120. HasTHUMBEE bool // Thumb EE instruction set
  121. HasNEON bool // NEON instruction set
  122. HasVFPv3 bool // Vector floating point version 3 support
  123. HasVFPv3D16 bool // Vector floating point version 3 D8-D15
  124. HasTLS bool // Thread local storage support
  125. HasVFPv4 bool // Vector floating point version 4 support
  126. HasIDIVA bool // Integer divide instruction support in ARM mode
  127. HasIDIVT bool // Integer divide instruction support in Thumb mode
  128. HasVFPD32 bool // Vector floating point version 3 D15-D31
  129. HasLPAE bool // Large Physical Address Extensions
  130. HasEVTSTRM bool // Event stream support
  131. HasAES bool // AES hardware implementation
  132. HasPMULL bool // Polynomial multiplication instruction set
  133. HasSHA1 bool // SHA1 hardware implementation
  134. HasSHA2 bool // SHA2 hardware implementation
  135. HasCRC32 bool // CRC32 hardware implementation
  136. _ CacheLinePad
  137. }
  138. // MIPS64X contains the supported CPU features of the current mips64/mips64le
  139. // platforms. If the current platform is not mips64/mips64le or the current
  140. // operating system is not Linux then all feature flags are false.
  141. var MIPS64X struct {
  142. _ CacheLinePad
  143. HasMSA bool // MIPS SIMD architecture
  144. _ CacheLinePad
  145. }
  146. // PPC64 contains the supported CPU features of the current ppc64/ppc64le platforms.
  147. // If the current platform is not ppc64/ppc64le then all feature flags are false.
  148. //
  149. // For ppc64/ppc64le, it is safe to check only for ISA level starting on ISA v3.00,
  150. // since there are no optional categories. There are some exceptions that also
  151. // require kernel support to work (DARN, SCV), so there are feature bits for
  152. // those as well. The struct is padded to avoid false sharing.
  153. var PPC64 struct {
  154. _ CacheLinePad
  155. HasDARN bool // Hardware random number generator (requires kernel enablement)
  156. HasSCV bool // Syscall vectored (requires kernel enablement)
  157. IsPOWER8 bool // ISA v2.07 (POWER8)
  158. IsPOWER9 bool // ISA v3.00 (POWER9), implies IsPOWER8
  159. _ CacheLinePad
  160. }
  161. // S390X contains the supported CPU features of the current IBM Z
  162. // (s390x) platform. If the current platform is not IBM Z then all
  163. // feature flags are false.
  164. //
  165. // S390X is padded to avoid false sharing. Further HasVX is only set
  166. // if the OS supports vector registers in addition to the STFLE
  167. // feature bit being set.
  168. var S390X struct {
  169. _ CacheLinePad
  170. HasZARCH bool // z/Architecture mode is active [mandatory]
  171. HasSTFLE bool // store facility list extended
  172. HasLDISP bool // long (20-bit) displacements
  173. HasEIMM bool // 32-bit immediates
  174. HasDFP bool // decimal floating point
  175. HasETF3EH bool // ETF-3 enhanced
  176. HasMSA bool // message security assist (CPACF)
  177. HasAES bool // KM-AES{128,192,256} functions
  178. HasAESCBC bool // KMC-AES{128,192,256} functions
  179. HasAESCTR bool // KMCTR-AES{128,192,256} functions
  180. HasAESGCM bool // KMA-GCM-AES{128,192,256} functions
  181. HasGHASH bool // KIMD-GHASH function
  182. HasSHA1 bool // K{I,L}MD-SHA-1 functions
  183. HasSHA256 bool // K{I,L}MD-SHA-256 functions
  184. HasSHA512 bool // K{I,L}MD-SHA-512 functions
  185. HasSHA3 bool // K{I,L}MD-SHA3-{224,256,384,512} and K{I,L}MD-SHAKE-{128,256} functions
  186. HasVX bool // vector facility
  187. HasVXE bool // vector-enhancements facility 1
  188. _ CacheLinePad
  189. }
  190. func init() {
  191. archInit()
  192. initOptions()
  193. processOptions()
  194. }
  195. // options contains the cpu debug options that can be used in GODEBUG.
  196. // Options are arch dependent and are added by the arch specific initOptions functions.
  197. // Features that are mandatory for the specific GOARCH should have the Required field set
  198. // (e.g. SSE2 on amd64).
  199. var options []option
  200. // Option names should be lower case. e.g. avx instead of AVX.
  201. type option struct {
  202. Name string
  203. Feature *bool
  204. Specified bool // whether feature value was specified in GODEBUG
  205. Enable bool // whether feature should be enabled
  206. Required bool // whether feature is mandatory and can not be disabled
  207. }
  208. func processOptions() {
  209. env := os.Getenv("GODEBUG")
  210. field:
  211. for env != "" {
  212. field := ""
  213. i := strings.IndexByte(env, ',')
  214. if i < 0 {
  215. field, env = env, ""
  216. } else {
  217. field, env = env[:i], env[i+1:]
  218. }
  219. if len(field) < 4 || field[:4] != "cpu." {
  220. continue
  221. }
  222. i = strings.IndexByte(field, '=')
  223. if i < 0 {
  224. print("GODEBUG sys/cpu: no value specified for \"", field, "\"\n")
  225. continue
  226. }
  227. key, value := field[4:i], field[i+1:] // e.g. "SSE2", "on"
  228. var enable bool
  229. switch value {
  230. case "on":
  231. enable = true
  232. case "off":
  233. enable = false
  234. default:
  235. print("GODEBUG sys/cpu: value \"", value, "\" not supported for cpu option \"", key, "\"\n")
  236. continue field
  237. }
  238. if key == "all" {
  239. for i := range options {
  240. options[i].Specified = true
  241. options[i].Enable = enable || options[i].Required
  242. }
  243. continue field
  244. }
  245. for i := range options {
  246. if options[i].Name == key {
  247. options[i].Specified = true
  248. options[i].Enable = enable
  249. continue field
  250. }
  251. }
  252. print("GODEBUG sys/cpu: unknown cpu feature \"", key, "\"\n")
  253. }
  254. for _, o := range options {
  255. if !o.Specified {
  256. continue
  257. }
  258. if o.Enable && !*o.Feature {
  259. print("GODEBUG sys/cpu: can not enable \"", o.Name, "\", missing CPU support\n")
  260. continue
  261. }
  262. if !o.Enable && o.Required {
  263. print("GODEBUG sys/cpu: can not disable \"", o.Name, "\", required CPU feature\n")
  264. continue
  265. }
  266. *o.Feature = o.Enable
  267. }
  268. }