cpu.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. HasAVXIFMA bool // Advanced vector extension Integer Fused Multiply Add
  71. HasAVXVNNI bool // Advanced vector extension Vector Neural Network Instructions
  72. HasAVXVNNIInt8 bool // Advanced vector extension Vector Neural Network Int8 instructions
  73. _ CacheLinePad
  74. }
  75. // ARM64 contains the supported CPU features of the
  76. // current ARMv8(aarch64) platform. If the current platform
  77. // is not arm64 then all feature flags are false.
  78. var ARM64 struct {
  79. _ CacheLinePad
  80. HasFP bool // Floating-point instruction set (always available)
  81. HasASIMD bool // Advanced SIMD (always available)
  82. HasEVTSTRM bool // Event stream support
  83. HasAES bool // AES hardware implementation
  84. HasPMULL bool // Polynomial multiplication instruction set
  85. HasSHA1 bool // SHA1 hardware implementation
  86. HasSHA2 bool // SHA2 hardware implementation
  87. HasCRC32 bool // CRC32 hardware implementation
  88. HasATOMICS bool // Atomic memory operation instruction set
  89. HasFPHP bool // Half precision floating-point instruction set
  90. HasASIMDHP bool // Advanced SIMD half precision instruction set
  91. HasCPUID bool // CPUID identification scheme registers
  92. HasASIMDRDM bool // Rounding double multiply add/subtract instruction set
  93. HasJSCVT bool // Javascript conversion from floating-point to integer
  94. HasFCMA bool // Floating-point multiplication and addition of complex numbers
  95. HasLRCPC bool // Release Consistent processor consistent support
  96. HasDCPOP bool // Persistent memory support
  97. HasSHA3 bool // SHA3 hardware implementation
  98. HasSM3 bool // SM3 hardware implementation
  99. HasSM4 bool // SM4 hardware implementation
  100. HasASIMDDP bool // Advanced SIMD double precision instruction set
  101. HasSHA512 bool // SHA512 hardware implementation
  102. HasSVE bool // Scalable Vector Extensions
  103. HasSVE2 bool // Scalable Vector Extensions 2
  104. HasASIMDFHM bool // Advanced SIMD multiplication FP16 to FP32
  105. HasDIT bool // Data Independent Timing support
  106. HasI8MM bool // Advanced SIMD Int8 matrix multiplication instructions
  107. _ CacheLinePad
  108. }
  109. // ARM contains the supported CPU features of the current ARM (32-bit) platform.
  110. // All feature flags are false if:
  111. // 1. the current platform is not arm, or
  112. // 2. the current operating system is not Linux.
  113. var ARM struct {
  114. _ CacheLinePad
  115. HasSWP bool // SWP instruction support
  116. HasHALF bool // Half-word load and store support
  117. HasTHUMB bool // ARM Thumb instruction set
  118. Has26BIT bool // Address space limited to 26-bits
  119. HasFASTMUL bool // 32-bit operand, 64-bit result multiplication support
  120. HasFPA bool // Floating point arithmetic support
  121. HasVFP bool // Vector floating point support
  122. HasEDSP bool // DSP Extensions support
  123. HasJAVA bool // Java instruction set
  124. HasIWMMXT bool // Intel Wireless MMX technology support
  125. HasCRUNCH bool // MaverickCrunch context switching and handling
  126. HasTHUMBEE bool // Thumb EE instruction set
  127. HasNEON bool // NEON instruction set
  128. HasVFPv3 bool // Vector floating point version 3 support
  129. HasVFPv3D16 bool // Vector floating point version 3 D8-D15
  130. HasTLS bool // Thread local storage support
  131. HasVFPv4 bool // Vector floating point version 4 support
  132. HasIDIVA bool // Integer divide instruction support in ARM mode
  133. HasIDIVT bool // Integer divide instruction support in Thumb mode
  134. HasVFPD32 bool // Vector floating point version 3 D15-D31
  135. HasLPAE bool // Large Physical Address Extensions
  136. HasEVTSTRM bool // Event stream support
  137. HasAES bool // AES hardware implementation
  138. HasPMULL bool // Polynomial multiplication instruction set
  139. HasSHA1 bool // SHA1 hardware implementation
  140. HasSHA2 bool // SHA2 hardware implementation
  141. HasCRC32 bool // CRC32 hardware implementation
  142. _ CacheLinePad
  143. }
  144. // The booleans in Loong64 contain the correspondingly named cpu feature bit.
  145. // The struct is padded to avoid false sharing.
  146. var Loong64 struct {
  147. _ CacheLinePad
  148. HasLSX bool // support 128-bit vector extension
  149. HasLASX bool // support 256-bit vector extension
  150. HasCRC32 bool // support CRC instruction
  151. HasLAM_BH bool // support AM{SWAP/ADD}[_DB].{B/H} instruction
  152. HasLAMCAS bool // support AMCAS[_DB].{B/H/W/D} instruction
  153. _ CacheLinePad
  154. }
  155. // MIPS64X contains the supported CPU features of the current mips64/mips64le
  156. // platforms. If the current platform is not mips64/mips64le or the current
  157. // operating system is not Linux then all feature flags are false.
  158. var MIPS64X struct {
  159. _ CacheLinePad
  160. HasMSA bool // MIPS SIMD architecture
  161. _ CacheLinePad
  162. }
  163. // PPC64 contains the supported CPU features of the current ppc64/ppc64le platforms.
  164. // If the current platform is not ppc64/ppc64le then all feature flags are false.
  165. //
  166. // For ppc64/ppc64le, it is safe to check only for ISA level starting on ISA v3.00,
  167. // since there are no optional categories. There are some exceptions that also
  168. // require kernel support to work (DARN, SCV), so there are feature bits for
  169. // those as well. The struct is padded to avoid false sharing.
  170. var PPC64 struct {
  171. _ CacheLinePad
  172. HasDARN bool // Hardware random number generator (requires kernel enablement)
  173. HasSCV bool // Syscall vectored (requires kernel enablement)
  174. IsPOWER8 bool // ISA v2.07 (POWER8)
  175. IsPOWER9 bool // ISA v3.00 (POWER9), implies IsPOWER8
  176. _ CacheLinePad
  177. }
  178. // S390X contains the supported CPU features of the current IBM Z
  179. // (s390x) platform. If the current platform is not IBM Z then all
  180. // feature flags are false.
  181. //
  182. // S390X is padded to avoid false sharing. Further HasVX is only set
  183. // if the OS supports vector registers in addition to the STFLE
  184. // feature bit being set.
  185. var S390X struct {
  186. _ CacheLinePad
  187. HasZARCH bool // z/Architecture mode is active [mandatory]
  188. HasSTFLE bool // store facility list extended
  189. HasLDISP bool // long (20-bit) displacements
  190. HasEIMM bool // 32-bit immediates
  191. HasDFP bool // decimal floating point
  192. HasETF3EH bool // ETF-3 enhanced
  193. HasMSA bool // message security assist (CPACF)
  194. HasAES bool // KM-AES{128,192,256} functions
  195. HasAESCBC bool // KMC-AES{128,192,256} functions
  196. HasAESCTR bool // KMCTR-AES{128,192,256} functions
  197. HasAESGCM bool // KMA-GCM-AES{128,192,256} functions
  198. HasGHASH bool // KIMD-GHASH function
  199. HasSHA1 bool // K{I,L}MD-SHA-1 functions
  200. HasSHA256 bool // K{I,L}MD-SHA-256 functions
  201. HasSHA512 bool // K{I,L}MD-SHA-512 functions
  202. HasSHA3 bool // K{I,L}MD-SHA3-{224,256,384,512} and K{I,L}MD-SHAKE-{128,256} functions
  203. HasVX bool // vector facility
  204. HasVXE bool // vector-enhancements facility 1
  205. _ CacheLinePad
  206. }
  207. // RISCV64 contains the supported CPU features and performance characteristics for riscv64
  208. // platforms. The booleans in RISCV64, with the exception of HasFastMisaligned, indicate
  209. // the presence of RISC-V extensions.
  210. //
  211. // It is safe to assume that all the RV64G extensions are supported and so they are omitted from
  212. // this structure. As riscv64 Go programs require at least RV64G, the code that populates
  213. // this structure cannot run successfully if some of the RV64G extensions are missing.
  214. // The struct is padded to avoid false sharing.
  215. var RISCV64 struct {
  216. _ CacheLinePad
  217. HasFastMisaligned bool // Fast misaligned accesses
  218. HasC bool // Compressed instruction-set extension
  219. HasV bool // Vector extension compatible with RVV 1.0
  220. HasZba bool // Address generation instructions extension
  221. HasZbb bool // Basic bit-manipulation extension
  222. HasZbs bool // Single-bit instructions extension
  223. HasZvbb bool // Vector Basic Bit-manipulation
  224. HasZvbc bool // Vector Carryless Multiplication
  225. HasZvkb bool // Vector Cryptography Bit-manipulation
  226. HasZvkt bool // Vector Data-Independent Execution Latency
  227. HasZvkg bool // Vector GCM/GMAC
  228. HasZvkn bool // NIST Algorithm Suite (AES/SHA256/SHA512)
  229. HasZvknc bool // NIST Algorithm Suite with carryless multiply
  230. HasZvkng bool // NIST Algorithm Suite with GCM
  231. HasZvks bool // ShangMi Algorithm Suite
  232. HasZvksc bool // ShangMi Algorithm Suite with carryless multiplication
  233. HasZvksg bool // ShangMi Algorithm Suite with GCM
  234. _ CacheLinePad
  235. }
  236. func init() {
  237. archInit()
  238. initOptions()
  239. processOptions()
  240. }
  241. // options contains the cpu debug options that can be used in GODEBUG.
  242. // Options are arch dependent and are added by the arch specific initOptions functions.
  243. // Features that are mandatory for the specific GOARCH should have the Required field set
  244. // (e.g. SSE2 on amd64).
  245. var options []option
  246. // Option names should be lower case. e.g. avx instead of AVX.
  247. type option struct {
  248. Name string
  249. Feature *bool
  250. Specified bool // whether feature value was specified in GODEBUG
  251. Enable bool // whether feature should be enabled
  252. Required bool // whether feature is mandatory and can not be disabled
  253. }
  254. func processOptions() {
  255. env := os.Getenv("GODEBUG")
  256. field:
  257. for env != "" {
  258. field := ""
  259. i := strings.IndexByte(env, ',')
  260. if i < 0 {
  261. field, env = env, ""
  262. } else {
  263. field, env = env[:i], env[i+1:]
  264. }
  265. if len(field) < 4 || field[:4] != "cpu." {
  266. continue
  267. }
  268. i = strings.IndexByte(field, '=')
  269. if i < 0 {
  270. print("GODEBUG sys/cpu: no value specified for \"", field, "\"\n")
  271. continue
  272. }
  273. key, value := field[4:i], field[i+1:] // e.g. "SSE2", "on"
  274. var enable bool
  275. switch value {
  276. case "on":
  277. enable = true
  278. case "off":
  279. enable = false
  280. default:
  281. print("GODEBUG sys/cpu: value \"", value, "\" not supported for cpu option \"", key, "\"\n")
  282. continue field
  283. }
  284. if key == "all" {
  285. for i := range options {
  286. options[i].Specified = true
  287. options[i].Enable = enable || options[i].Required
  288. }
  289. continue field
  290. }
  291. for i := range options {
  292. if options[i].Name == key {
  293. options[i].Specified = true
  294. options[i].Enable = enable
  295. continue field
  296. }
  297. }
  298. print("GODEBUG sys/cpu: unknown cpu feature \"", key, "\"\n")
  299. }
  300. for _, o := range options {
  301. if !o.Specified {
  302. continue
  303. }
  304. if o.Enable && !*o.Feature {
  305. print("GODEBUG sys/cpu: can not enable \"", o.Name, "\", missing CPU support\n")
  306. continue
  307. }
  308. if !o.Enable && o.Required {
  309. print("GODEBUG sys/cpu: can not disable \"", o.Name, "\", required CPU feature\n")
  310. continue
  311. }
  312. *o.Feature = o.Enable
  313. }
  314. }