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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 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. HasBMI1 bool // Bit manipulation instruction set 1
  53. HasBMI2 bool // Bit manipulation instruction set 2
  54. HasERMS bool // Enhanced REP for MOVSB and STOSB
  55. HasFMA bool // Fused-multiply-add instructions
  56. HasOSXSAVE bool // OS supports XSAVE/XRESTOR for saving/restoring XMM registers.
  57. HasPCLMULQDQ bool // PCLMULQDQ instruction - most often used for AES-GCM
  58. HasPOPCNT bool // Hamming weight instruction POPCNT.
  59. HasRDRAND bool // RDRAND instruction (on-chip random number generator)
  60. HasRDSEED bool // RDSEED instruction (on-chip random number generator)
  61. HasSSE2 bool // Streaming SIMD extension 2 (always available on amd64)
  62. HasSSE3 bool // Streaming SIMD extension 3
  63. HasSSSE3 bool // Supplemental streaming SIMD extension 3
  64. HasSSE41 bool // Streaming SIMD extension 4 and 4.1
  65. HasSSE42 bool // Streaming SIMD extension 4 and 4.2
  66. _ CacheLinePad
  67. }
  68. // ARM64 contains the supported CPU features of the
  69. // current ARMv8(aarch64) platform. If the current platform
  70. // is not arm64 then all feature flags are false.
  71. var ARM64 struct {
  72. _ CacheLinePad
  73. HasFP bool // Floating-point instruction set (always available)
  74. HasASIMD bool // Advanced SIMD (always available)
  75. HasEVTSTRM bool // Event stream support
  76. HasAES bool // AES hardware implementation
  77. HasPMULL bool // Polynomial multiplication instruction set
  78. HasSHA1 bool // SHA1 hardware implementation
  79. HasSHA2 bool // SHA2 hardware implementation
  80. HasCRC32 bool // CRC32 hardware implementation
  81. HasATOMICS bool // Atomic memory operation instruction set
  82. HasFPHP bool // Half precision floating-point instruction set
  83. HasASIMDHP bool // Advanced SIMD half precision instruction set
  84. HasCPUID bool // CPUID identification scheme registers
  85. HasASIMDRDM bool // Rounding double multiply add/subtract instruction set
  86. HasJSCVT bool // Javascript conversion from floating-point to integer
  87. HasFCMA bool // Floating-point multiplication and addition of complex numbers
  88. HasLRCPC bool // Release Consistent processor consistent support
  89. HasDCPOP bool // Persistent memory support
  90. HasSHA3 bool // SHA3 hardware implementation
  91. HasSM3 bool // SM3 hardware implementation
  92. HasSM4 bool // SM4 hardware implementation
  93. HasASIMDDP bool // Advanced SIMD double precision instruction set
  94. HasSHA512 bool // SHA512 hardware implementation
  95. HasSVE bool // Scalable Vector Extensions
  96. HasASIMDFHM bool // Advanced SIMD multiplication FP16 to FP32
  97. _ CacheLinePad
  98. }
  99. // ARM contains the supported CPU features of the current ARM (32-bit) platform.
  100. // All feature flags are false if:
  101. // 1. the current platform is not arm, or
  102. // 2. the current operating system is not Linux.
  103. var ARM struct {
  104. _ CacheLinePad
  105. HasSWP bool // SWP instruction support
  106. HasHALF bool // Half-word load and store support
  107. HasTHUMB bool // ARM Thumb instruction set
  108. Has26BIT bool // Address space limited to 26-bits
  109. HasFASTMUL bool // 32-bit operand, 64-bit result multiplication support
  110. HasFPA bool // Floating point arithmetic support
  111. HasVFP bool // Vector floating point support
  112. HasEDSP bool // DSP Extensions support
  113. HasJAVA bool // Java instruction set
  114. HasIWMMXT bool // Intel Wireless MMX technology support
  115. HasCRUNCH bool // MaverickCrunch context switching and handling
  116. HasTHUMBEE bool // Thumb EE instruction set
  117. HasNEON bool // NEON instruction set
  118. HasVFPv3 bool // Vector floating point version 3 support
  119. HasVFPv3D16 bool // Vector floating point version 3 D8-D15
  120. HasTLS bool // Thread local storage support
  121. HasVFPv4 bool // Vector floating point version 4 support
  122. HasIDIVA bool // Integer divide instruction support in ARM mode
  123. HasIDIVT bool // Integer divide instruction support in Thumb mode
  124. HasVFPD32 bool // Vector floating point version 3 D15-D31
  125. HasLPAE bool // Large Physical Address Extensions
  126. HasEVTSTRM bool // Event stream support
  127. HasAES bool // AES hardware implementation
  128. HasPMULL bool // Polynomial multiplication instruction set
  129. HasSHA1 bool // SHA1 hardware implementation
  130. HasSHA2 bool // SHA2 hardware implementation
  131. HasCRC32 bool // CRC32 hardware implementation
  132. _ CacheLinePad
  133. }
  134. // MIPS64X contains the supported CPU features of the current mips64/mips64le
  135. // platforms. If the current platform is not mips64/mips64le or the current
  136. // operating system is not Linux then all feature flags are false.
  137. var MIPS64X struct {
  138. _ CacheLinePad
  139. HasMSA bool // MIPS SIMD architecture
  140. _ CacheLinePad
  141. }
  142. // PPC64 contains the supported CPU features of the current ppc64/ppc64le platforms.
  143. // If the current platform is not ppc64/ppc64le then all feature flags are false.
  144. //
  145. // For ppc64/ppc64le, it is safe to check only for ISA level starting on ISA v3.00,
  146. // since there are no optional categories. There are some exceptions that also
  147. // require kernel support to work (DARN, SCV), so there are feature bits for
  148. // those as well. The minimum processor requirement is POWER8 (ISA 2.07).
  149. // The struct is padded to avoid false sharing.
  150. var PPC64 struct {
  151. _ CacheLinePad
  152. HasDARN bool // Hardware random number generator (requires kernel enablement)
  153. HasSCV bool // Syscall vectored (requires kernel enablement)
  154. IsPOWER8 bool // ISA v2.07 (POWER8)
  155. IsPOWER9 bool // ISA v3.00 (POWER9)
  156. _ CacheLinePad
  157. }
  158. // S390X contains the supported CPU features of the current IBM Z
  159. // (s390x) platform. If the current platform is not IBM Z then all
  160. // feature flags are false.
  161. //
  162. // S390X is padded to avoid false sharing. Further HasVX is only set
  163. // if the OS supports vector registers in addition to the STFLE
  164. // feature bit being set.
  165. var S390X struct {
  166. _ CacheLinePad
  167. HasZARCH bool // z/Architecture mode is active [mandatory]
  168. HasSTFLE bool // store facility list extended
  169. HasLDISP bool // long (20-bit) displacements
  170. HasEIMM bool // 32-bit immediates
  171. HasDFP bool // decimal floating point
  172. HasETF3EH bool // ETF-3 enhanced
  173. HasMSA bool // message security assist (CPACF)
  174. HasAES bool // KM-AES{128,192,256} functions
  175. HasAESCBC bool // KMC-AES{128,192,256} functions
  176. HasAESCTR bool // KMCTR-AES{128,192,256} functions
  177. HasAESGCM bool // KMA-GCM-AES{128,192,256} functions
  178. HasGHASH bool // KIMD-GHASH function
  179. HasSHA1 bool // K{I,L}MD-SHA-1 functions
  180. HasSHA256 bool // K{I,L}MD-SHA-256 functions
  181. HasSHA512 bool // K{I,L}MD-SHA-512 functions
  182. HasSHA3 bool // K{I,L}MD-SHA3-{224,256,384,512} and K{I,L}MD-SHAKE-{128,256} functions
  183. HasVX bool // vector facility
  184. HasVXE bool // vector-enhancements facility 1
  185. _ CacheLinePad
  186. }
  187. func init() {
  188. archInit()
  189. initOptions()
  190. processOptions()
  191. }
  192. // options contains the cpu debug options that can be used in GODEBUG.
  193. // Options are arch dependent and are added by the arch specific initOptions functions.
  194. // Features that are mandatory for the specific GOARCH should have the Required field set
  195. // (e.g. SSE2 on amd64).
  196. var options []option
  197. // Option names should be lower case. e.g. avx instead of AVX.
  198. type option struct {
  199. Name string
  200. Feature *bool
  201. Specified bool // whether feature value was specified in GODEBUG
  202. Enable bool // whether feature should be enabled
  203. Required bool // whether feature is mandatory and can not be disabled
  204. }
  205. func processOptions() {
  206. env := os.Getenv("GODEBUG")
  207. field:
  208. for env != "" {
  209. field := ""
  210. i := strings.IndexByte(env, ',')
  211. if i < 0 {
  212. field, env = env, ""
  213. } else {
  214. field, env = env[:i], env[i+1:]
  215. }
  216. if len(field) < 4 || field[:4] != "cpu." {
  217. continue
  218. }
  219. i = strings.IndexByte(field, '=')
  220. if i < 0 {
  221. print("GODEBUG sys/cpu: no value specified for \"", field, "\"\n")
  222. continue
  223. }
  224. key, value := field[4:i], field[i+1:] // e.g. "SSE2", "on"
  225. var enable bool
  226. switch value {
  227. case "on":
  228. enable = true
  229. case "off":
  230. enable = false
  231. default:
  232. print("GODEBUG sys/cpu: value \"", value, "\" not supported for cpu option \"", key, "\"\n")
  233. continue field
  234. }
  235. if key == "all" {
  236. for i := range options {
  237. options[i].Specified = true
  238. options[i].Enable = enable || options[i].Required
  239. }
  240. continue field
  241. }
  242. for i := range options {
  243. if options[i].Name == key {
  244. options[i].Specified = true
  245. options[i].Enable = enable
  246. continue field
  247. }
  248. }
  249. print("GODEBUG sys/cpu: unknown cpu feature \"", key, "\"\n")
  250. }
  251. for _, o := range options {
  252. if !o.Specified {
  253. continue
  254. }
  255. if o.Enable && !*o.Feature {
  256. print("GODEBUG sys/cpu: can not enable \"", o.Name, "\", missing CPU support\n")
  257. continue
  258. }
  259. if !o.Enable && o.Required {
  260. print("GODEBUG sys/cpu: can not disable \"", o.Name, "\", required CPU feature\n")
  261. continue
  262. }
  263. *o.Feature = o.Enable
  264. }
  265. }