cpu.go 9.9 KB

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