cpuid.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. // Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file.
  2. // Package cpuid provides information about the CPU running the current program.
  3. //
  4. // CPU features are detected on startup, and kept for fast access through the life of the application.
  5. // Currently x86 / x64 (AMD64) is supported.
  6. //
  7. // You can access the CPU information by accessing the shared CPU variable of the cpuid library.
  8. //
  9. // Package home: https://github.com/klauspost/cpuid
  10. package cpuid
  11. import "strings"
  12. // Vendor is a representation of a CPU vendor.
  13. type Vendor int
  14. const (
  15. Other Vendor = iota
  16. Intel
  17. AMD
  18. VIA
  19. Transmeta
  20. NSC
  21. KVM // Kernel-based Virtual Machine
  22. MSVM // Microsoft Hyper-V or Windows Virtual PC
  23. VMware
  24. XenHVM
  25. )
  26. const (
  27. CMOV = 1 << iota // i686 CMOV
  28. NX // NX (No-Execute) bit
  29. AMD3DNOW // AMD 3DNOW
  30. AMD3DNOWEXT // AMD 3DNowExt
  31. MMX // standard MMX
  32. MMXEXT // SSE integer functions or AMD MMX ext
  33. SSE // SSE functions
  34. SSE2 // P4 SSE functions
  35. SSE3 // Prescott SSE3 functions
  36. SSSE3 // Conroe SSSE3 functions
  37. SSE4 // Penryn SSE4.1 functions
  38. SSE4A // AMD Barcelona microarchitecture SSE4a instructions
  39. SSE42 // Nehalem SSE4.2 functions
  40. AVX // AVX functions
  41. AVX2 // AVX2 functions
  42. FMA3 // Intel FMA 3
  43. FMA4 // Bulldozer FMA4 functions
  44. XOP // Bulldozer XOP functions
  45. F16C // Half-precision floating-point conversion
  46. BMI1 // Bit Manipulation Instruction Set 1
  47. BMI2 // Bit Manipulation Instruction Set 2
  48. TBM // AMD Trailing Bit Manipulation
  49. LZCNT // LZCNT instruction
  50. POPCNT // POPCNT instruction
  51. AESNI // Advanced Encryption Standard New Instructions
  52. CLMUL // Carry-less Multiplication
  53. HTT // Hyperthreading (enabled)
  54. HLE // Hardware Lock Elision
  55. RTM // Restricted Transactional Memory
  56. RDRAND // RDRAND instruction is available
  57. RDSEED // RDSEED instruction is available
  58. ADX // Intel ADX (Multi-Precision Add-Carry Instruction Extensions)
  59. SHA // Intel SHA Extensions
  60. AVX512F // AVX-512 Foundation
  61. AVX512DQ // AVX-512 Doubleword and Quadword Instructions
  62. AVX512IFMA // AVX-512 Integer Fused Multiply-Add Instructions
  63. AVX512PF // AVX-512 Prefetch Instructions
  64. AVX512ER // AVX-512 Exponential and Reciprocal Instructions
  65. AVX512CD // AVX-512 Conflict Detection Instructions
  66. AVX512BW // AVX-512 Byte and Word Instructions
  67. AVX512VL // AVX-512 Vector Length Extensions
  68. AVX512VBMI // AVX-512 Vector Bit Manipulation Instructions
  69. MPX // Intel MPX (Memory Protection Extensions)
  70. ERMS // Enhanced REP MOVSB/STOSB
  71. RDTSCP // RDTSCP Instruction
  72. CX16 // CMPXCHG16B Instruction
  73. SGX // Software Guard Extensions
  74. IBPB // Indirect Branch Restricted Speculation (IBRS) and Indirect Branch Predictor Barrier (IBPB)
  75. STIBP // Single Thread Indirect Branch Predictors
  76. // Performance indicators
  77. SSE2SLOW // SSE2 is supported, but usually not faster
  78. SSE3SLOW // SSE3 is supported, but usually not faster
  79. ATOM // Atom processor, some SSSE3 instructions are slower
  80. )
  81. var flagNames = map[Flags]string{
  82. CMOV: "CMOV", // i686 CMOV
  83. NX: "NX", // NX (No-Execute) bit
  84. AMD3DNOW: "AMD3DNOW", // AMD 3DNOW
  85. AMD3DNOWEXT: "AMD3DNOWEXT", // AMD 3DNowExt
  86. MMX: "MMX", // Standard MMX
  87. MMXEXT: "MMXEXT", // SSE integer functions or AMD MMX ext
  88. SSE: "SSE", // SSE functions
  89. SSE2: "SSE2", // P4 SSE2 functions
  90. SSE3: "SSE3", // Prescott SSE3 functions
  91. SSSE3: "SSSE3", // Conroe SSSE3 functions
  92. SSE4: "SSE4.1", // Penryn SSE4.1 functions
  93. SSE4A: "SSE4A", // AMD Barcelona microarchitecture SSE4a instructions
  94. SSE42: "SSE4.2", // Nehalem SSE4.2 functions
  95. AVX: "AVX", // AVX functions
  96. AVX2: "AVX2", // AVX functions
  97. FMA3: "FMA3", // Intel FMA 3
  98. FMA4: "FMA4", // Bulldozer FMA4 functions
  99. XOP: "XOP", // Bulldozer XOP functions
  100. F16C: "F16C", // Half-precision floating-point conversion
  101. BMI1: "BMI1", // Bit Manipulation Instruction Set 1
  102. BMI2: "BMI2", // Bit Manipulation Instruction Set 2
  103. TBM: "TBM", // AMD Trailing Bit Manipulation
  104. LZCNT: "LZCNT", // LZCNT instruction
  105. POPCNT: "POPCNT", // POPCNT instruction
  106. AESNI: "AESNI", // Advanced Encryption Standard New Instructions
  107. CLMUL: "CLMUL", // Carry-less Multiplication
  108. HTT: "HTT", // Hyperthreading (enabled)
  109. HLE: "HLE", // Hardware Lock Elision
  110. RTM: "RTM", // Restricted Transactional Memory
  111. RDRAND: "RDRAND", // RDRAND instruction is available
  112. RDSEED: "RDSEED", // RDSEED instruction is available
  113. ADX: "ADX", // Intel ADX (Multi-Precision Add-Carry Instruction Extensions)
  114. SHA: "SHA", // Intel SHA Extensions
  115. AVX512F: "AVX512F", // AVX-512 Foundation
  116. AVX512DQ: "AVX512DQ", // AVX-512 Doubleword and Quadword Instructions
  117. AVX512IFMA: "AVX512IFMA", // AVX-512 Integer Fused Multiply-Add Instructions
  118. AVX512PF: "AVX512PF", // AVX-512 Prefetch Instructions
  119. AVX512ER: "AVX512ER", // AVX-512 Exponential and Reciprocal Instructions
  120. AVX512CD: "AVX512CD", // AVX-512 Conflict Detection Instructions
  121. AVX512BW: "AVX512BW", // AVX-512 Byte and Word Instructions
  122. AVX512VL: "AVX512VL", // AVX-512 Vector Length Extensions
  123. AVX512VBMI: "AVX512VBMI", // AVX-512 Vector Bit Manipulation Instructions
  124. MPX: "MPX", // Intel MPX (Memory Protection Extensions)
  125. ERMS: "ERMS", // Enhanced REP MOVSB/STOSB
  126. RDTSCP: "RDTSCP", // RDTSCP Instruction
  127. CX16: "CX16", // CMPXCHG16B Instruction
  128. SGX: "SGX", // Software Guard Extensions
  129. IBPB: "IBPB", // Indirect Branch Restricted Speculation and Indirect Branch Predictor Barrier
  130. STIBP: "STIBP", // Single Thread Indirect Branch Predictors
  131. // Performance indicators
  132. SSE2SLOW: "SSE2SLOW", // SSE2 supported, but usually not faster
  133. SSE3SLOW: "SSE3SLOW", // SSE3 supported, but usually not faster
  134. ATOM: "ATOM", // Atom processor, some SSSE3 instructions are slower
  135. }
  136. // CPUInfo contains information about the detected system CPU.
  137. type CPUInfo struct {
  138. BrandName string // Brand name reported by the CPU
  139. VendorID Vendor // Comparable CPU vendor ID
  140. Features Flags // Features of the CPU
  141. PhysicalCores int // Number of physical processor cores in your CPU. Will be 0 if undetectable.
  142. ThreadsPerCore int // Number of threads per physical core. Will be 1 if undetectable.
  143. LogicalCores int // Number of physical cores times threads that can run on each core through the use of hyperthreading. Will be 0 if undetectable.
  144. Family int // CPU family number
  145. Model int // CPU model number
  146. CacheLine int // Cache line size in bytes. Will be 0 if undetectable.
  147. Cache struct {
  148. L1I int // L1 Instruction Cache (per core or shared). Will be -1 if undetected
  149. L1D int // L1 Data Cache (per core or shared). Will be -1 if undetected
  150. L2 int // L2 Cache (per core or shared). Will be -1 if undetected
  151. L3 int // L3 Instruction Cache (per core or shared). Will be -1 if undetected
  152. }
  153. SGX SGXSupport
  154. maxFunc uint32
  155. maxExFunc uint32
  156. }
  157. var cpuid func(op uint32) (eax, ebx, ecx, edx uint32)
  158. var cpuidex func(op, op2 uint32) (eax, ebx, ecx, edx uint32)
  159. var xgetbv func(index uint32) (eax, edx uint32)
  160. var rdtscpAsm func() (eax, ebx, ecx, edx uint32)
  161. // CPU contains information about the CPU as detected on startup,
  162. // or when Detect last was called.
  163. //
  164. // Use this as the primary entry point to you data,
  165. // this way queries are
  166. var CPU CPUInfo
  167. func init() {
  168. initCPU()
  169. Detect()
  170. }
  171. // Detect will re-detect current CPU info.
  172. // This will replace the content of the exported CPU variable.
  173. //
  174. // Unless you expect the CPU to change while you are running your program
  175. // you should not need to call this function.
  176. // If you call this, you must ensure that no other goroutine is accessing the
  177. // exported CPU variable.
  178. func Detect() {
  179. CPU.maxFunc = maxFunctionID()
  180. CPU.maxExFunc = maxExtendedFunction()
  181. CPU.BrandName = brandName()
  182. CPU.CacheLine = cacheLine()
  183. CPU.Family, CPU.Model = familyModel()
  184. CPU.Features = support()
  185. CPU.SGX = hasSGX(CPU.Features&SGX != 0)
  186. CPU.ThreadsPerCore = threadsPerCore()
  187. CPU.LogicalCores = logicalCores()
  188. CPU.PhysicalCores = physicalCores()
  189. CPU.VendorID = vendorID()
  190. CPU.cacheSize()
  191. }
  192. // Generated here: http://play.golang.org/p/BxFH2Gdc0G
  193. // Cmov indicates support of CMOV instructions
  194. func (c CPUInfo) Cmov() bool {
  195. return c.Features&CMOV != 0
  196. }
  197. // Amd3dnow indicates support of AMD 3DNOW! instructions
  198. func (c CPUInfo) Amd3dnow() bool {
  199. return c.Features&AMD3DNOW != 0
  200. }
  201. // Amd3dnowExt indicates support of AMD 3DNOW! Extended instructions
  202. func (c CPUInfo) Amd3dnowExt() bool {
  203. return c.Features&AMD3DNOWEXT != 0
  204. }
  205. // MMX indicates support of MMX instructions
  206. func (c CPUInfo) MMX() bool {
  207. return c.Features&MMX != 0
  208. }
  209. // MMXExt indicates support of MMXEXT instructions
  210. // (SSE integer functions or AMD MMX ext)
  211. func (c CPUInfo) MMXExt() bool {
  212. return c.Features&MMXEXT != 0
  213. }
  214. // SSE indicates support of SSE instructions
  215. func (c CPUInfo) SSE() bool {
  216. return c.Features&SSE != 0
  217. }
  218. // SSE2 indicates support of SSE 2 instructions
  219. func (c CPUInfo) SSE2() bool {
  220. return c.Features&SSE2 != 0
  221. }
  222. // SSE3 indicates support of SSE 3 instructions
  223. func (c CPUInfo) SSE3() bool {
  224. return c.Features&SSE3 != 0
  225. }
  226. // SSSE3 indicates support of SSSE 3 instructions
  227. func (c CPUInfo) SSSE3() bool {
  228. return c.Features&SSSE3 != 0
  229. }
  230. // SSE4 indicates support of SSE 4 (also called SSE 4.1) instructions
  231. func (c CPUInfo) SSE4() bool {
  232. return c.Features&SSE4 != 0
  233. }
  234. // SSE42 indicates support of SSE4.2 instructions
  235. func (c CPUInfo) SSE42() bool {
  236. return c.Features&SSE42 != 0
  237. }
  238. // AVX indicates support of AVX instructions
  239. // and operating system support of AVX instructions
  240. func (c CPUInfo) AVX() bool {
  241. return c.Features&AVX != 0
  242. }
  243. // AVX2 indicates support of AVX2 instructions
  244. func (c CPUInfo) AVX2() bool {
  245. return c.Features&AVX2 != 0
  246. }
  247. // FMA3 indicates support of FMA3 instructions
  248. func (c CPUInfo) FMA3() bool {
  249. return c.Features&FMA3 != 0
  250. }
  251. // FMA4 indicates support of FMA4 instructions
  252. func (c CPUInfo) FMA4() bool {
  253. return c.Features&FMA4 != 0
  254. }
  255. // XOP indicates support of XOP instructions
  256. func (c CPUInfo) XOP() bool {
  257. return c.Features&XOP != 0
  258. }
  259. // F16C indicates support of F16C instructions
  260. func (c CPUInfo) F16C() bool {
  261. return c.Features&F16C != 0
  262. }
  263. // BMI1 indicates support of BMI1 instructions
  264. func (c CPUInfo) BMI1() bool {
  265. return c.Features&BMI1 != 0
  266. }
  267. // BMI2 indicates support of BMI2 instructions
  268. func (c CPUInfo) BMI2() bool {
  269. return c.Features&BMI2 != 0
  270. }
  271. // TBM indicates support of TBM instructions
  272. // (AMD Trailing Bit Manipulation)
  273. func (c CPUInfo) TBM() bool {
  274. return c.Features&TBM != 0
  275. }
  276. // Lzcnt indicates support of LZCNT instruction
  277. func (c CPUInfo) Lzcnt() bool {
  278. return c.Features&LZCNT != 0
  279. }
  280. // Popcnt indicates support of POPCNT instruction
  281. func (c CPUInfo) Popcnt() bool {
  282. return c.Features&POPCNT != 0
  283. }
  284. // HTT indicates the processor has Hyperthreading enabled
  285. func (c CPUInfo) HTT() bool {
  286. return c.Features&HTT != 0
  287. }
  288. // SSE2Slow indicates that SSE2 may be slow on this processor
  289. func (c CPUInfo) SSE2Slow() bool {
  290. return c.Features&SSE2SLOW != 0
  291. }
  292. // SSE3Slow indicates that SSE3 may be slow on this processor
  293. func (c CPUInfo) SSE3Slow() bool {
  294. return c.Features&SSE3SLOW != 0
  295. }
  296. // AesNi indicates support of AES-NI instructions
  297. // (Advanced Encryption Standard New Instructions)
  298. func (c CPUInfo) AesNi() bool {
  299. return c.Features&AESNI != 0
  300. }
  301. // Clmul indicates support of CLMUL instructions
  302. // (Carry-less Multiplication)
  303. func (c CPUInfo) Clmul() bool {
  304. return c.Features&CLMUL != 0
  305. }
  306. // NX indicates support of NX (No-Execute) bit
  307. func (c CPUInfo) NX() bool {
  308. return c.Features&NX != 0
  309. }
  310. // SSE4A indicates support of AMD Barcelona microarchitecture SSE4a instructions
  311. func (c CPUInfo) SSE4A() bool {
  312. return c.Features&SSE4A != 0
  313. }
  314. // HLE indicates support of Hardware Lock Elision
  315. func (c CPUInfo) HLE() bool {
  316. return c.Features&HLE != 0
  317. }
  318. // RTM indicates support of Restricted Transactional Memory
  319. func (c CPUInfo) RTM() bool {
  320. return c.Features&RTM != 0
  321. }
  322. // Rdrand indicates support of RDRAND instruction is available
  323. func (c CPUInfo) Rdrand() bool {
  324. return c.Features&RDRAND != 0
  325. }
  326. // Rdseed indicates support of RDSEED instruction is available
  327. func (c CPUInfo) Rdseed() bool {
  328. return c.Features&RDSEED != 0
  329. }
  330. // ADX indicates support of Intel ADX (Multi-Precision Add-Carry Instruction Extensions)
  331. func (c CPUInfo) ADX() bool {
  332. return c.Features&ADX != 0
  333. }
  334. // SHA indicates support of Intel SHA Extensions
  335. func (c CPUInfo) SHA() bool {
  336. return c.Features&SHA != 0
  337. }
  338. // AVX512F indicates support of AVX-512 Foundation
  339. func (c CPUInfo) AVX512F() bool {
  340. return c.Features&AVX512F != 0
  341. }
  342. // AVX512DQ indicates support of AVX-512 Doubleword and Quadword Instructions
  343. func (c CPUInfo) AVX512DQ() bool {
  344. return c.Features&AVX512DQ != 0
  345. }
  346. // AVX512IFMA indicates support of AVX-512 Integer Fused Multiply-Add Instructions
  347. func (c CPUInfo) AVX512IFMA() bool {
  348. return c.Features&AVX512IFMA != 0
  349. }
  350. // AVX512PF indicates support of AVX-512 Prefetch Instructions
  351. func (c CPUInfo) AVX512PF() bool {
  352. return c.Features&AVX512PF != 0
  353. }
  354. // AVX512ER indicates support of AVX-512 Exponential and Reciprocal Instructions
  355. func (c CPUInfo) AVX512ER() bool {
  356. return c.Features&AVX512ER != 0
  357. }
  358. // AVX512CD indicates support of AVX-512 Conflict Detection Instructions
  359. func (c CPUInfo) AVX512CD() bool {
  360. return c.Features&AVX512CD != 0
  361. }
  362. // AVX512BW indicates support of AVX-512 Byte and Word Instructions
  363. func (c CPUInfo) AVX512BW() bool {
  364. return c.Features&AVX512BW != 0
  365. }
  366. // AVX512VL indicates support of AVX-512 Vector Length Extensions
  367. func (c CPUInfo) AVX512VL() bool {
  368. return c.Features&AVX512VL != 0
  369. }
  370. // AVX512VBMI indicates support of AVX-512 Vector Bit Manipulation Instructions
  371. func (c CPUInfo) AVX512VBMI() bool {
  372. return c.Features&AVX512VBMI != 0
  373. }
  374. // MPX indicates support of Intel MPX (Memory Protection Extensions)
  375. func (c CPUInfo) MPX() bool {
  376. return c.Features&MPX != 0
  377. }
  378. // ERMS indicates support of Enhanced REP MOVSB/STOSB
  379. func (c CPUInfo) ERMS() bool {
  380. return c.Features&ERMS != 0
  381. }
  382. // RDTSCP Instruction is available.
  383. func (c CPUInfo) RDTSCP() bool {
  384. return c.Features&RDTSCP != 0
  385. }
  386. // CX16 indicates if CMPXCHG16B instruction is available.
  387. func (c CPUInfo) CX16() bool {
  388. return c.Features&CX16 != 0
  389. }
  390. // TSX is split into HLE (Hardware Lock Elision) and RTM (Restricted Transactional Memory) detection.
  391. // So TSX simply checks that.
  392. func (c CPUInfo) TSX() bool {
  393. return c.Features&(HLE|RTM) == HLE|RTM
  394. }
  395. // Atom indicates an Atom processor
  396. func (c CPUInfo) Atom() bool {
  397. return c.Features&ATOM != 0
  398. }
  399. // Intel returns true if vendor is recognized as Intel
  400. func (c CPUInfo) Intel() bool {
  401. return c.VendorID == Intel
  402. }
  403. // AMD returns true if vendor is recognized as AMD
  404. func (c CPUInfo) AMD() bool {
  405. return c.VendorID == AMD
  406. }
  407. // Transmeta returns true if vendor is recognized as Transmeta
  408. func (c CPUInfo) Transmeta() bool {
  409. return c.VendorID == Transmeta
  410. }
  411. // NSC returns true if vendor is recognized as National Semiconductor
  412. func (c CPUInfo) NSC() bool {
  413. return c.VendorID == NSC
  414. }
  415. // VIA returns true if vendor is recognized as VIA
  416. func (c CPUInfo) VIA() bool {
  417. return c.VendorID == VIA
  418. }
  419. // RTCounter returns the 64-bit time-stamp counter
  420. // Uses the RDTSCP instruction. The value 0 is returned
  421. // if the CPU does not support the instruction.
  422. func (c CPUInfo) RTCounter() uint64 {
  423. if !c.RDTSCP() {
  424. return 0
  425. }
  426. a, _, _, d := rdtscpAsm()
  427. return uint64(a) | (uint64(d) << 32)
  428. }
  429. // Ia32TscAux returns the IA32_TSC_AUX part of the RDTSCP.
  430. // This variable is OS dependent, but on Linux contains information
  431. // about the current cpu/core the code is running on.
  432. // If the RDTSCP instruction isn't supported on the CPU, the value 0 is returned.
  433. func (c CPUInfo) Ia32TscAux() uint32 {
  434. if !c.RDTSCP() {
  435. return 0
  436. }
  437. _, _, ecx, _ := rdtscpAsm()
  438. return ecx
  439. }
  440. // LogicalCPU will return the Logical CPU the code is currently executing on.
  441. // This is likely to change when the OS re-schedules the running thread
  442. // to another CPU.
  443. // If the current core cannot be detected, -1 will be returned.
  444. func (c CPUInfo) LogicalCPU() int {
  445. if c.maxFunc < 1 {
  446. return -1
  447. }
  448. _, ebx, _, _ := cpuid(1)
  449. return int(ebx >> 24)
  450. }
  451. // VM Will return true if the cpu id indicates we are in
  452. // a virtual machine. This is only a hint, and will very likely
  453. // have many false negatives.
  454. func (c CPUInfo) VM() bool {
  455. switch c.VendorID {
  456. case MSVM, KVM, VMware, XenHVM:
  457. return true
  458. }
  459. return false
  460. }
  461. // Flags contains detected cpu features and caracteristics
  462. type Flags uint64
  463. // String returns a string representation of the detected
  464. // CPU features.
  465. func (f Flags) String() string {
  466. return strings.Join(f.Strings(), ",")
  467. }
  468. // Strings returns and array of the detected features.
  469. func (f Flags) Strings() []string {
  470. s := support()
  471. r := make([]string, 0, 20)
  472. for i := uint(0); i < 64; i++ {
  473. key := Flags(1 << i)
  474. val := flagNames[key]
  475. if s&key != 0 {
  476. r = append(r, val)
  477. }
  478. }
  479. return r
  480. }
  481. func maxExtendedFunction() uint32 {
  482. eax, _, _, _ := cpuid(0x80000000)
  483. return eax
  484. }
  485. func maxFunctionID() uint32 {
  486. a, _, _, _ := cpuid(0)
  487. return a
  488. }
  489. func brandName() string {
  490. if maxExtendedFunction() >= 0x80000004 {
  491. v := make([]uint32, 0, 48)
  492. for i := uint32(0); i < 3; i++ {
  493. a, b, c, d := cpuid(0x80000002 + i)
  494. v = append(v, a, b, c, d)
  495. }
  496. return strings.Trim(string(valAsString(v...)), " ")
  497. }
  498. return "unknown"
  499. }
  500. func threadsPerCore() int {
  501. mfi := maxFunctionID()
  502. if mfi < 0x4 || vendorID() != Intel {
  503. return 1
  504. }
  505. if mfi < 0xb {
  506. _, b, _, d := cpuid(1)
  507. if (d & (1 << 28)) != 0 {
  508. // v will contain logical core count
  509. v := (b >> 16) & 255
  510. if v > 1 {
  511. a4, _, _, _ := cpuid(4)
  512. // physical cores
  513. v2 := (a4 >> 26) + 1
  514. if v2 > 0 {
  515. return int(v) / int(v2)
  516. }
  517. }
  518. }
  519. return 1
  520. }
  521. _, b, _, _ := cpuidex(0xb, 0)
  522. if b&0xffff == 0 {
  523. return 1
  524. }
  525. return int(b & 0xffff)
  526. }
  527. func logicalCores() int {
  528. mfi := maxFunctionID()
  529. switch vendorID() {
  530. case Intel:
  531. // Use this on old Intel processors
  532. if mfi < 0xb {
  533. if mfi < 1 {
  534. return 0
  535. }
  536. // CPUID.1:EBX[23:16] represents the maximum number of addressable IDs (initial APIC ID)
  537. // that can be assigned to logical processors in a physical package.
  538. // The value may not be the same as the number of logical processors that are present in the hardware of a physical package.
  539. _, ebx, _, _ := cpuid(1)
  540. logical := (ebx >> 16) & 0xff
  541. return int(logical)
  542. }
  543. _, b, _, _ := cpuidex(0xb, 1)
  544. return int(b & 0xffff)
  545. case AMD:
  546. _, b, _, _ := cpuid(1)
  547. return int((b >> 16) & 0xff)
  548. default:
  549. return 0
  550. }
  551. }
  552. func familyModel() (int, int) {
  553. if maxFunctionID() < 0x1 {
  554. return 0, 0
  555. }
  556. eax, _, _, _ := cpuid(1)
  557. family := ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff)
  558. model := ((eax >> 4) & 0xf) + ((eax >> 12) & 0xf0)
  559. return int(family), int(model)
  560. }
  561. func physicalCores() int {
  562. switch vendorID() {
  563. case Intel:
  564. return logicalCores() / threadsPerCore()
  565. case AMD:
  566. if maxExtendedFunction() >= 0x80000008 {
  567. _, _, c, _ := cpuid(0x80000008)
  568. return int(c&0xff) + 1
  569. }
  570. }
  571. return 0
  572. }
  573. // Except from http://en.wikipedia.org/wiki/CPUID#EAX.3D0:_Get_vendor_ID
  574. var vendorMapping = map[string]Vendor{
  575. "AMDisbetter!": AMD,
  576. "AuthenticAMD": AMD,
  577. "CentaurHauls": VIA,
  578. "GenuineIntel": Intel,
  579. "TransmetaCPU": Transmeta,
  580. "GenuineTMx86": Transmeta,
  581. "Geode by NSC": NSC,
  582. "VIA VIA VIA ": VIA,
  583. "KVMKVMKVMKVM": KVM,
  584. "Microsoft Hv": MSVM,
  585. "VMwareVMware": VMware,
  586. "XenVMMXenVMM": XenHVM,
  587. }
  588. func vendorID() Vendor {
  589. _, b, c, d := cpuid(0)
  590. v := valAsString(b, d, c)
  591. vend, ok := vendorMapping[string(v)]
  592. if !ok {
  593. return Other
  594. }
  595. return vend
  596. }
  597. func cacheLine() int {
  598. if maxFunctionID() < 0x1 {
  599. return 0
  600. }
  601. _, ebx, _, _ := cpuid(1)
  602. cache := (ebx & 0xff00) >> 5 // cflush size
  603. if cache == 0 && maxExtendedFunction() >= 0x80000006 {
  604. _, _, ecx, _ := cpuid(0x80000006)
  605. cache = ecx & 0xff // cacheline size
  606. }
  607. // TODO: Read from Cache and TLB Information
  608. return int(cache)
  609. }
  610. func (c *CPUInfo) cacheSize() {
  611. c.Cache.L1D = -1
  612. c.Cache.L1I = -1
  613. c.Cache.L2 = -1
  614. c.Cache.L3 = -1
  615. vendor := vendorID()
  616. switch vendor {
  617. case Intel:
  618. if maxFunctionID() < 4 {
  619. return
  620. }
  621. for i := uint32(0); ; i++ {
  622. eax, ebx, ecx, _ := cpuidex(4, i)
  623. cacheType := eax & 15
  624. if cacheType == 0 {
  625. break
  626. }
  627. cacheLevel := (eax >> 5) & 7
  628. coherency := int(ebx&0xfff) + 1
  629. partitions := int((ebx>>12)&0x3ff) + 1
  630. associativity := int((ebx>>22)&0x3ff) + 1
  631. sets := int(ecx) + 1
  632. size := associativity * partitions * coherency * sets
  633. switch cacheLevel {
  634. case 1:
  635. if cacheType == 1 {
  636. // 1 = Data Cache
  637. c.Cache.L1D = size
  638. } else if cacheType == 2 {
  639. // 2 = Instruction Cache
  640. c.Cache.L1I = size
  641. } else {
  642. if c.Cache.L1D < 0 {
  643. c.Cache.L1I = size
  644. }
  645. if c.Cache.L1I < 0 {
  646. c.Cache.L1I = size
  647. }
  648. }
  649. case 2:
  650. c.Cache.L2 = size
  651. case 3:
  652. c.Cache.L3 = size
  653. }
  654. }
  655. case AMD:
  656. // Untested.
  657. if maxExtendedFunction() < 0x80000005 {
  658. return
  659. }
  660. _, _, ecx, edx := cpuid(0x80000005)
  661. c.Cache.L1D = int(((ecx >> 24) & 0xFF) * 1024)
  662. c.Cache.L1I = int(((edx >> 24) & 0xFF) * 1024)
  663. if maxExtendedFunction() < 0x80000006 {
  664. return
  665. }
  666. _, _, ecx, _ = cpuid(0x80000006)
  667. c.Cache.L2 = int(((ecx >> 16) & 0xFFFF) * 1024)
  668. }
  669. return
  670. }
  671. type SGXSupport struct {
  672. Available bool
  673. SGX1Supported bool
  674. SGX2Supported bool
  675. MaxEnclaveSizeNot64 int64
  676. MaxEnclaveSize64 int64
  677. }
  678. func hasSGX(available bool) (rval SGXSupport) {
  679. rval.Available = available
  680. if !available {
  681. return
  682. }
  683. a, _, _, d := cpuidex(0x12, 0)
  684. rval.SGX1Supported = a&0x01 != 0
  685. rval.SGX2Supported = a&0x02 != 0
  686. rval.MaxEnclaveSizeNot64 = 1 << (d & 0xFF) // pow 2
  687. rval.MaxEnclaveSize64 = 1 << ((d >> 8) & 0xFF) // pow 2
  688. return
  689. }
  690. func support() Flags {
  691. mfi := maxFunctionID()
  692. vend := vendorID()
  693. if mfi < 0x1 {
  694. return 0
  695. }
  696. rval := uint64(0)
  697. _, _, c, d := cpuid(1)
  698. if (d & (1 << 15)) != 0 {
  699. rval |= CMOV
  700. }
  701. if (d & (1 << 23)) != 0 {
  702. rval |= MMX
  703. }
  704. if (d & (1 << 25)) != 0 {
  705. rval |= MMXEXT
  706. }
  707. if (d & (1 << 25)) != 0 {
  708. rval |= SSE
  709. }
  710. if (d & (1 << 26)) != 0 {
  711. rval |= SSE2
  712. }
  713. if (c & 1) != 0 {
  714. rval |= SSE3
  715. }
  716. if (c & 0x00000200) != 0 {
  717. rval |= SSSE3
  718. }
  719. if (c & 0x00080000) != 0 {
  720. rval |= SSE4
  721. }
  722. if (c & 0x00100000) != 0 {
  723. rval |= SSE42
  724. }
  725. if (c & (1 << 25)) != 0 {
  726. rval |= AESNI
  727. }
  728. if (c & (1 << 1)) != 0 {
  729. rval |= CLMUL
  730. }
  731. if c&(1<<23) != 0 {
  732. rval |= POPCNT
  733. }
  734. if c&(1<<30) != 0 {
  735. rval |= RDRAND
  736. }
  737. if c&(1<<29) != 0 {
  738. rval |= F16C
  739. }
  740. if c&(1<<13) != 0 {
  741. rval |= CX16
  742. }
  743. if vend == Intel && (d&(1<<28)) != 0 && mfi >= 4 {
  744. if threadsPerCore() > 1 {
  745. rval |= HTT
  746. }
  747. }
  748. // Check XGETBV, OXSAVE and AVX bits
  749. if c&(1<<26) != 0 && c&(1<<27) != 0 && c&(1<<28) != 0 {
  750. // Check for OS support
  751. eax, _ := xgetbv(0)
  752. if (eax & 0x6) == 0x6 {
  753. rval |= AVX
  754. if (c & 0x00001000) != 0 {
  755. rval |= FMA3
  756. }
  757. }
  758. }
  759. // Check AVX2, AVX2 requires OS support, but BMI1/2 don't.
  760. if mfi >= 7 {
  761. _, ebx, ecx, edx := cpuidex(7, 0)
  762. if (rval&AVX) != 0 && (ebx&0x00000020) != 0 {
  763. rval |= AVX2
  764. }
  765. if (ebx & 0x00000008) != 0 {
  766. rval |= BMI1
  767. if (ebx & 0x00000100) != 0 {
  768. rval |= BMI2
  769. }
  770. }
  771. if ebx&(1<<2) != 0 {
  772. rval |= SGX
  773. }
  774. if ebx&(1<<4) != 0 {
  775. rval |= HLE
  776. }
  777. if ebx&(1<<9) != 0 {
  778. rval |= ERMS
  779. }
  780. if ebx&(1<<11) != 0 {
  781. rval |= RTM
  782. }
  783. if ebx&(1<<14) != 0 {
  784. rval |= MPX
  785. }
  786. if ebx&(1<<18) != 0 {
  787. rval |= RDSEED
  788. }
  789. if ebx&(1<<19) != 0 {
  790. rval |= ADX
  791. }
  792. if ebx&(1<<29) != 0 {
  793. rval |= SHA
  794. }
  795. if edx&(1<<26) != 0 {
  796. rval |= IBPB
  797. }
  798. if edx&(1<<27) != 0 {
  799. rval |= STIBP
  800. }
  801. // Only detect AVX-512 features if XGETBV is supported
  802. if c&((1<<26)|(1<<27)) == (1<<26)|(1<<27) {
  803. // Check for OS support
  804. eax, _ := xgetbv(0)
  805. // Verify that XCR0[7:5] = ‘111b’ (OPMASK state, upper 256-bit of ZMM0-ZMM15 and
  806. // ZMM16-ZMM31 state are enabled by OS)
  807. /// and that XCR0[2:1] = ‘11b’ (XMM state and YMM state are enabled by OS).
  808. if (eax>>5)&7 == 7 && (eax>>1)&3 == 3 {
  809. if ebx&(1<<16) != 0 {
  810. rval |= AVX512F
  811. }
  812. if ebx&(1<<17) != 0 {
  813. rval |= AVX512DQ
  814. }
  815. if ebx&(1<<21) != 0 {
  816. rval |= AVX512IFMA
  817. }
  818. if ebx&(1<<26) != 0 {
  819. rval |= AVX512PF
  820. }
  821. if ebx&(1<<27) != 0 {
  822. rval |= AVX512ER
  823. }
  824. if ebx&(1<<28) != 0 {
  825. rval |= AVX512CD
  826. }
  827. if ebx&(1<<30) != 0 {
  828. rval |= AVX512BW
  829. }
  830. if ebx&(1<<31) != 0 {
  831. rval |= AVX512VL
  832. }
  833. // ecx
  834. if ecx&(1<<1) != 0 {
  835. rval |= AVX512VBMI
  836. }
  837. }
  838. }
  839. }
  840. if maxExtendedFunction() >= 0x80000001 {
  841. _, _, c, d := cpuid(0x80000001)
  842. if (c & (1 << 5)) != 0 {
  843. rval |= LZCNT
  844. rval |= POPCNT
  845. }
  846. if (d & (1 << 31)) != 0 {
  847. rval |= AMD3DNOW
  848. }
  849. if (d & (1 << 30)) != 0 {
  850. rval |= AMD3DNOWEXT
  851. }
  852. if (d & (1 << 23)) != 0 {
  853. rval |= MMX
  854. }
  855. if (d & (1 << 22)) != 0 {
  856. rval |= MMXEXT
  857. }
  858. if (c & (1 << 6)) != 0 {
  859. rval |= SSE4A
  860. }
  861. if d&(1<<20) != 0 {
  862. rval |= NX
  863. }
  864. if d&(1<<27) != 0 {
  865. rval |= RDTSCP
  866. }
  867. /* Allow for selectively disabling SSE2 functions on AMD processors
  868. with SSE2 support but not SSE4a. This includes Athlon64, some
  869. Opteron, and some Sempron processors. MMX, SSE, or 3DNow! are faster
  870. than SSE2 often enough to utilize this special-case flag.
  871. AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
  872. so that SSE2 is used unless explicitly disabled by checking
  873. AV_CPU_FLAG_SSE2SLOW. */
  874. if vendorID() != Intel &&
  875. rval&SSE2 != 0 && (c&0x00000040) == 0 {
  876. rval |= SSE2SLOW
  877. }
  878. /* XOP and FMA4 use the AVX instruction coding scheme, so they can't be
  879. * used unless the OS has AVX support. */
  880. if (rval & AVX) != 0 {
  881. if (c & 0x00000800) != 0 {
  882. rval |= XOP
  883. }
  884. if (c & 0x00010000) != 0 {
  885. rval |= FMA4
  886. }
  887. }
  888. if vendorID() == Intel {
  889. family, model := familyModel()
  890. if family == 6 && (model == 9 || model == 13 || model == 14) {
  891. /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and
  892. * 6/14 (core1 "yonah") theoretically support sse2, but it's
  893. * usually slower than mmx. */
  894. if (rval & SSE2) != 0 {
  895. rval |= SSE2SLOW
  896. }
  897. if (rval & SSE3) != 0 {
  898. rval |= SSE3SLOW
  899. }
  900. }
  901. /* The Atom processor has SSSE3 support, which is useful in many cases,
  902. * but sometimes the SSSE3 version is slower than the SSE2 equivalent
  903. * on the Atom, but is generally faster on other processors supporting
  904. * SSSE3. This flag allows for selectively disabling certain SSSE3
  905. * functions on the Atom. */
  906. if family == 6 && model == 28 {
  907. rval |= ATOM
  908. }
  909. }
  910. }
  911. return Flags(rval)
  912. }
  913. func valAsString(values ...uint32) []byte {
  914. r := make([]byte, 4*len(values))
  915. for i, v := range values {
  916. dst := r[i*4:]
  917. dst[0] = byte(v & 0xff)
  918. dst[1] = byte((v >> 8) & 0xff)
  919. dst[2] = byte((v >> 16) & 0xff)
  920. dst[3] = byte((v >> 24) & 0xff)
  921. switch {
  922. case dst[0] == 0:
  923. return r[:i*4]
  924. case dst[1] == 0:
  925. return r[:i*4+1]
  926. case dst[2] == 0:
  927. return r[:i*4+2]
  928. case dst[3] == 0:
  929. return r[:i*4+3]
  930. }
  931. }
  932. return r
  933. }