// Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file.
// Package cpuid provides information about the CPU running the current program.//// CPU features are detected on startup, and kept for fast access through the life of the application.// Currently x86 / x64 (AMD64) as well as arm64 is supported.//// You can access the CPU information by accessing the shared CPU variable of the cpuid library.//// Package home: https://github.com/klauspost/cpuid
package cpuidimport ()// AMD refererence: https://www.amd.com/system/files/TechDocs/25481.pdf// and Processor Programming Reference (PPR)// Vendor is a representation of a CPU vendor.typeVendorintconst (VendorUnknownVendor = iotaIntelAMDVIATransmetaNSCKVM// Kernel-based Virtual MachineMSVM// Microsoft Hyper-V or Windows Virtual PCVMwareXenHVMBhyveHygonSiSRDCAmpereARMBroadcomCaviumDECFujitsuInfineonMotorolaNVIDIAAMCCQualcommMarvellQEMUQNXACRNSREApple lastVendor)//go:generate stringer -type=FeatureID,Vendor// FeatureID is the ID of a specific cpu feature.typeFeatureIDintconst (// Keep index -1 as unknownUNKNOWN = -1// x86 featuresADXFeatureID = iota// Intel ADX (Multi-Precision Add-Carry Instruction Extensions)AESNI// Advanced Encryption Standard New InstructionsAMD3DNOW// AMD 3DNOWAMD3DNOWEXT// AMD 3DNowExtAMXBF16// Tile computational operations on BFLOAT16 numbersAMXFP16// Tile computational operations on FP16 numbersAMXINT8// Tile computational operations on 8-bit integersAMXFP8// Tile computational operations on FP8 numbersAMXTILE// Tile architectureAMXTF32// Tile architectureAMXCOMPLEX// Matrix Multiplication of TF32 Tiles into Packed Single Precision TileAMXTRANSPOSE// Tile multiply where the first operand is transposedAPX_F// Intel APXAVX// AVX functionsAVX10// If set the Intel AVX10 Converged Vector ISA is supportedAVX10_128// If set indicates that AVX10 128-bit vector support is presentAVX10_256// If set indicates that AVX10 256-bit vector support is presentAVX10_512// If set indicates that AVX10 512-bit vector support is presentAVX2// AVX2 functionsAVX512BF16// AVX-512 BFLOAT16 InstructionsAVX512BITALG// AVX-512 Bit AlgorithmsAVX512BW// AVX-512 Byte and Word InstructionsAVX512CD// AVX-512 Conflict Detection InstructionsAVX512DQ// AVX-512 Doubleword and Quadword InstructionsAVX512ER// AVX-512 Exponential and Reciprocal InstructionsAVX512F// AVX-512 FoundationAVX512FP16// AVX-512 FP16 InstructionsAVX512IFMA// AVX-512 Integer Fused Multiply-Add InstructionsAVX512PF// AVX-512 Prefetch InstructionsAVX512VBMI// AVX-512 Vector Bit Manipulation InstructionsAVX512VBMI2// AVX-512 Vector Bit Manipulation Instructions, Version 2AVX512VL// AVX-512 Vector Length ExtensionsAVX512VNNI// AVX-512 Vector Neural Network InstructionsAVX512VP2INTERSECT// AVX-512 Intersect for D/QAVX512VPOPCNTDQ// AVX-512 Vector Population Count Doubleword and QuadwordAVXIFMA// AVX-IFMA instructionsAVXNECONVERT// AVX-NE-CONVERT instructionsAVXSLOW// Indicates the CPU performs 2 128 bit operations instead of oneAVXVNNI// AVX (VEX encoded) VNNI neural network instructionsAVXVNNIINT8// AVX-VNNI-INT8 instructionsAVXVNNIINT16// AVX-VNNI-INT16 instructionsBHI_CTRL// Branch History Injection and Intra-mode Branch Target Injection / CVE-2022-0001, CVE-2022-0002 / INTEL-SA-00598BMI1// Bit Manipulation Instruction Set 1BMI2// Bit Manipulation Instruction Set 2CETIBT// Intel CET Indirect Branch TrackingCETSS// Intel CET Shadow StackCLDEMOTE// Cache Line DemoteCLMUL// Carry-less MultiplicationCLZERO// CLZERO instruction supportedCMOV// i686 CMOVCMPCCXADD// CMPCCXADD instructionsCMPSB_SCADBS_SHORT// Fast short CMPSB and SCASBCMPXCHG8// CMPXCHG8 instructionCPBOOST// Core Performance BoostCPPC// AMD: Collaborative Processor Performance ControlCX16// CMPXCHG16B InstructionEFER_LMSLE_UNS// AMD: =Core::X86::Msr::EFER[LMSLE] is not supported, and MBZENQCMD// Enqueue CommandERMS// Enhanced REP MOVSB/STOSBF16C// Half-precision floating-point conversionFLUSH_L1D// Flush L1D cacheFMA3// Intel FMA 3. Does not imply AVX.FMA4// Bulldozer FMA4 functionsFP128// AMD: When set, the internal FP/SIMD execution datapath is no more than 128-bits wideFP256// AMD: When set, the internal FP/SIMD execution datapath is no more than 256-bits wideFSRM// Fast Short Rep MovFXSR// FXSAVE, FXRESTOR instructions, CR4 bit 9FXSROPT// FXSAVE/FXRSTOR optimizationsGFNI// Galois Field New Instructions. May require other features (AVX, AVX512VL,AVX512F) based on usage.HLE// Hardware Lock ElisionHRESET// If set CPU supports history reset and the IA32_HRESET_ENABLE MSRHTT// Hyperthreading (enabled)HWA// Hardware assert supported. Indicates support for MSRC001_10HYBRID_CPU// This part has CPUs of more than one type.HYPERVISOR// This bit has been reserved by Intel & AMD for use by hypervisorsIA32_ARCH_CAP// IA32_ARCH_CAPABILITIES MSR (Intel)IA32_CORE_CAP// IA32_CORE_CAPABILITIES MSRIBPB// Indirect Branch Restricted Speculation (IBRS) and Indirect Branch Predictor Barrier (IBPB)IBPB_BRTYPE// Indicates that MSR 49h (PRED_CMD) bit 0 (IBPB) flushes all branch type predictions from the CPU branch predictorIBRS// AMD: Indirect Branch Restricted SpeculationIBRS_PREFERRED// AMD: IBRS is preferred over software solutionIBRS_PROVIDES_SMP// AMD: IBRS provides Same Mode ProtectionIBS// Instruction Based Sampling (AMD)IBSBRNTRGT// Instruction Based Sampling Feature (AMD)IBSFETCHSAM// Instruction Based Sampling Feature (AMD)IBSFFV// Instruction Based Sampling Feature (AMD)IBSOPCNT// Instruction Based Sampling Feature (AMD)IBSOPCNTEXT// Instruction Based Sampling Feature (AMD)IBSOPSAM// Instruction Based Sampling Feature (AMD)IBSRDWROPCNT// Instruction Based Sampling Feature (AMD)IBSRIPINVALIDCHK// Instruction Based Sampling Feature (AMD)IBS_FETCH_CTLX// AMD: IBS fetch control extended MSR supportedIBS_OPDATA4// AMD: IBS op data 4 MSR supportedIBS_OPFUSE// AMD: Indicates support for IbsOpFuseIBS_PREVENTHOST// Disallowing IBS use by the host supportedIBS_ZEN4// AMD: Fetch and Op IBS support IBS extensions added with Zen4IDPRED_CTRL// IPRED_DISINT_WBINVD// WBINVD/WBNOINVD are interruptible.INVLPGB// NVLPGB and TLBSYNC instruction supportedKEYLOCKER// Key lockerKEYLOCKERW// Key locker wideLAHF// LAHF/SAHF in long modeLAM// If set, CPU supports Linear Address MaskingLBRVIRT// LBR virtualizationLZCNT// LZCNT instructionMCAOVERFLOW// MCA overflow recovery support.MCDT_NO// Processor do not exhibit MXCSR Configuration Dependent Timing behavior and do not need to mitigate it.MCOMMIT// MCOMMIT instruction supportedMD_CLEAR// VERW clears CPU buffersMMX// standard MMXMMXEXT// SSE integer functions or AMD MMX extMOVBE// MOVBE instruction (big-endian)MOVDIR64B// Move 64 Bytes as Direct StoreMOVDIRI// Move Doubleword as Direct StoreMOVSB_ZL// Fast Zero-Length MOVSBMOVU// AMD: MOVU SSE instructions are more efficient and should be preferred to SSE MOVL/MOVH. MOVUPS is more efficient than MOVLPS/MOVHPS. MOVUPD is more efficient than MOVLPD/MOVHPDMPX// Intel MPX (Memory Protection Extensions)MSRIRC// Instruction Retired Counter MSR availableMSRLIST// Read/Write List of Model Specific RegistersMSR_PAGEFLUSH// Page Flush MSR availableNRIPS// Indicates support for NRIP save on VMEXITNX// NX (No-Execute) bitOSXSAVE// XSAVE enabled by OSPCONFIG// PCONFIG for Intel Multi-Key Total Memory EncryptionPOPCNT// POPCNT instructionPPIN// AMD: Protected Processor Inventory Number support. Indicates that Protected Processor Inventory Number (PPIN) capability can be enabledPREFETCHI// PREFETCHIT0/1 instructionsPSFD// Predictive Store Forward DisableRDPRU// RDPRU instruction supportedRDRAND// RDRAND instruction is availableRDSEED// RDSEED instruction is availableRDTSCP// RDTSCP InstructionRRSBA_CTRL// Restricted RSB AlternateRTM// Restricted Transactional MemoryRTM_ALWAYS_ABORT// Indicates that the loaded microcode is forcing RTM abort.SBPB// Indicates support for the Selective Branch Predictor BarrierSERIALIZE// Serialize Instruction ExecutionSEV// AMD Secure Encrypted Virtualization supportedSEV_64BIT// AMD SEV guest execution only allowed from a 64-bit hostSEV_ALTERNATIVE// AMD SEV Alternate Injection supportedSEV_DEBUGSWAP// Full debug state swap supported for SEV-ES guestsSEV_ES// AMD SEV Encrypted State supportedSEV_RESTRICTED// AMD SEV Restricted Injection supportedSEV_SNP// AMD SEV Secure Nested Paging supportedSGX// Software Guard ExtensionsSGXLC// Software Guard Extensions Launch ControlSGXPQC// Software Guard Extensions 256-bit EncryptionSHA// Intel SHA ExtensionsSME// AMD Secure Memory Encryption supportedSME_COHERENT// AMD Hardware cache coherency across encryption domains enforcedSM3_X86// SM3 instructionsSM4_X86// SM4 instructionsSPEC_CTRL_SSBD// Speculative Store Bypass DisableSRBDS_CTRL// SRBDS mitigation MSR availableSRSO_MSR_FIX// Indicates that software may use MSR BP_CFG[BpSpecReduce] to mitigate SRSO.SRSO_NO// Indicates the CPU is not subject to the SRSO vulnerabilitySRSO_USER_KERNEL_NO// Indicates the CPU is not subject to the SRSO vulnerability across user/kernel boundariesSSE// SSE functionsSSE2// P4 SSE functionsSSE3// Prescott SSE3 functionsSSE4// Penryn SSE4.1 functionsSSE42// Nehalem SSE4.2 functionsSSE4A// AMD Barcelona microarchitecture SSE4a instructionsSSSE3// Conroe SSSE3 functionsSTIBP// Single Thread Indirect Branch PredictorsSTIBP_ALWAYSON// AMD: Single Thread Indirect Branch Prediction Mode has Enhanced Performance and may be left Always OnSTOSB_SHORT// Fast short STOSBSUCCOR// Software uncorrectable error containment and recovery capability.SVM// AMD Secure Virtual MachineSVMDA// Indicates support for the SVM decode assists.SVMFBASID// SVM, Indicates that TLB flush events, including CR3 writes and CR4.PGE toggles, flush only the current ASID's TLB entries. Also indicates support for the extended VMCBTLB_ControlSVML// AMD SVM lock. Indicates support for SVM-Lock.SVMNP// AMD SVM nested pagingSVMPF// SVM pause intercept filter. Indicates support for the pause intercept filterSVMPFT// SVM PAUSE filter threshold. Indicates support for the PAUSE filter cycle count thresholdSYSCALL// System-Call Extension (SCE): SYSCALL and SYSRET instructions.SYSEE// SYSENTER and SYSEXIT instructionsTBM// AMD Trailing Bit ManipulationTDX_GUEST// Intel Trust Domain Extensions GuestTLB_FLUSH_NESTED// AMD: Flushing includes all the nested translations for guest translationsTME// Intel Total Memory Encryption. The following MSRs are supported: IA32_TME_CAPABILITY, IA32_TME_ACTIVATE, IA32_TME_EXCLUDE_MASK, and IA32_TME_EXCLUDE_BASE.TOPEXT// TopologyExtensions: topology extensions support. Indicates support for CPUID Fn8000_001D_EAX_x[N:0]-CPUID Fn8000_001E_EDX.TSA_L1_NO// AMD only: Not vulnerable to TSA-L1TSA_SQ_NO// AM onlyD: Not vulnerable to TSA-SQTSA_VERW_CLEAR// If set, the memory form of the VERW instruction may be used to help mitigate TSATSCRATEMSR// MSR based TSC rate control. Indicates support for MSR TSC ratio MSRC000_0104TSXLDTRK// Intel TSX Suspend Load Address TrackingVAES// Vector AES. AVX(512) versions requires additional checks.VMCBCLEAN// VMCB clean bits. Indicates support for VMCB clean bits.VMPL// AMD VM Permission Levels supportedVMSA_REGPROT// AMD VMSA Register Protection supportedVMX// Virtual Machine ExtensionsVPCLMULQDQ// Carry-Less Multiplication Quadword. Requires AVX for 3 register versions.VTE// AMD Virtual Transparent Encryption supportedWAITPKG// TPAUSE, UMONITOR, UMWAITWBNOINVD// Write Back and Do Not Invalidate CacheWRMSRNS// Non-Serializing Write to Model Specific RegisterX87// FPUXGETBV1// Supports XGETBV with ECX = 1XOP// Bulldozer XOP functionsXSAVE// XSAVE, XRESTOR, XSETBV, XGETBVXSAVEC// Supports XSAVEC and the compacted form of XRSTOR.XSAVEOPT// XSAVEOPT availableXSAVES// Supports XSAVES/XRSTORS and IA32_XSS// ARM features:AESARM// AES instructionsARMCPUID// Some CPU ID registers readable at user-levelASIMD// Advanced SIMDASIMDDP// SIMD Dot ProductASIMDHP// Advanced SIMD half-precision floating pointASIMDRDM// Rounding Double Multiply Accumulate/Subtract (SQRDMLAH/SQRDMLSH)ATOMICS// Large System Extensions (LSE)CRC32// CRC32/CRC32C instructionsDCPOP// Data cache clean to Point of Persistence (DC CVAP)EVTSTRM// Generic timerFCMA// Floating point complex number addition and multiplicationFHM// FMLAL and FMLSL instructionsFP// Single-precision and double-precision floating pointFPHP// Half-precision floating pointGPA// Generic Pointer AuthenticationJSCVT// Javascript-style double->int convert (FJCVTZS)LRCPC// Weaker release consistency (LDAPR, etc)PMULL// Polynomial Multiply instructions (PMULL/PMULL2)RNDR// Random Number instructionsTLB// Outer Shareable and TLB range maintenance instructionsTS// Flag manipulation instructionsSHA1// SHA-1 instructions (SHA1C, etc)SHA2// SHA-2 instructions (SHA256H, etc)SHA3// SHA-3 instructions (EOR3, RAXI, XAR, BCAX)SHA512// SHA512 instructionsSM3// SM3 instructionsSM4// SM4 instructionsSVE// Scalable Vector Extension// PMUPMU_FIXEDCOUNTER_CYCLESPMU_FIXEDCOUNTER_REFCYCLESPMU_FIXEDCOUNTER_INSTRUCTIONSPMU_FIXEDCOUNTER_TOPDOWN_SLOTS// Keep it last. It automatically defines the size of []flagSet lastID firstID FeatureID = UNKNOWN + 1)// CPUInfo contains information about the detected system CPU.typeCPUInfostruct { BrandName string// Brand name reported by the CPU VendorID Vendor// Comparable CPU vendor ID VendorString string// Raw vendor string. HypervisorVendorID Vendor// Hypervisor vendor HypervisorVendorString string// Raw hypervisor vendor string featureSet flagSet// Features of the CPU PhysicalCores int// Number of physical processor cores in your CPU. Will be 0 if undetectable. ThreadsPerCore int// Number of threads per physical core. Will be 1 if undetectable. LogicalCores int// Number of physical cores times threads that can run on each core through the use of hyperthreading. Will be 0 if undetectable. Family int// CPU family number Model int// CPU model number Stepping int// CPU stepping info CacheLine int// Cache line size in bytes. Will be 0 if undetectable. Hz int64// Clock speed, if known, 0 otherwise. Will attempt to contain base clock speed. BoostFreq int64// Max clock speed, if known, 0 otherwise Cache struct { L1I int// L1 Instruction Cache (per core or shared). Will be -1 if undetected L1D int// L1 Data Cache (per core or shared). Will be -1 if undetected L2 int// L2 Cache (per core or shared). Will be -1 if undetected L3 int// L3 Cache (per core, per ccx or shared). Will be -1 if undetected } SGX SGXSupport AMDMemEncryption AMDMemEncryptionSupport AVX10Level uint8 PMU PerformanceMonitoringInfo// holds information about the PMU maxFunc uint32 maxExFunc uint32}// PerformanceMonitoringInfo holds information about CPU performance monitoring capabilities.// This is primarily populated from CPUID leaf 0xAh on x86typePerformanceMonitoringInfostruct {// VersionID (x86 only): Version ID of architectural performance monitoring. // A value of 0 means architectural performance monitoring is not supported or information is unavailable. VersionID uint8// NumGPPMC: Number of General-Purpose Performance Monitoring Counters per logical processor. // On ARM, this is derived from PMCR_EL0.N (number of event counters). NumGPCounters uint8// GPPMCWidth: Bit width of General-Purpose Performance Monitoring Counters. // On ARM, typically 64 for PMU event counters. GPPMCWidth uint8// NumFixedPMC: Number of Fixed-Function Performance Counters. // Valid on x86 if VersionID > 1. On ARM, this typically includes at least the cycle counter (PMCCNTR_EL0). NumFixedPMC uint8// FixedPMCWidth: Bit width of Fixed-Function Performance Counters. // Valid on x86 if VersionID > 1. On ARM, the cycle counter (PMCCNTR_EL0) is 64-bit. FixedPMCWidth uint8// Raw register output from CPUID leaf 0xAh. RawEBX uint32 RawEAX uint32 RawEDX uint32}var cpuid func(op uint32) (eax, ebx, ecx, edx uint32)var cpuidex func(op, op2 uint32) (eax, ebx, ecx, edx uint32)var xgetbv func(index uint32) (eax, edx uint32)var rdtscpAsm func() (eax, ebx, ecx, edx uint32)var darwinHasAVX512 = func() bool { returnfalse }// CPU contains information about the CPU as detected on startup,// or when Detect last was called.//// Use this as the primary entry point to you data.varCPUCPUInfofunc init() {initCPU()Detect()}// Detect will re-detect current CPU info.// This will replace the content of the exported CPU variable.//// Unless you expect the CPU to change while you are running your program// you should not need to call this function.// If you call this, you must ensure that no other goroutine is accessing the// exported CPU variable.func () {// Set defaultsCPU.ThreadsPerCore = 1CPU.Cache.L1I = -1CPU.Cache.L1D = -1CPU.Cache.L2 = -1CPU.Cache.L3 = -1 := trueifdetectArmFlag != nil { = !*detectArmFlag }addInfo(&CPU, )ifdisplayFeats != nil && *displayFeats {fmt.Println("cpu features:", strings.Join(CPU.FeatureSet(), ","))// Exit with non-zero so tests will print value.os.Exit(1) }ifdisableFlag != nil { := strings.Split(*disableFlag, ",")for , := range { := ParseFeature(strings.TrimSpace())if != UNKNOWN {CPU.featureSet.unset() } } }}// DetectARM will detect ARM64 features.// This is NOT done automatically since it can potentially crash// if the OS does not handle the command.// If in the future this can be done safely this function may not// do anything.func () {addInfo(&CPU, false)}var detectArmFlag *boolvar displayFeats *boolvar disableFlag *string// Flags will enable flags.// This must be called *before* flag.Parse AND// Detect must be called after the flags have been parsed.// Note that this means that any detection used in init() functions// will not contain these flags.func () {disableFlag = flag.String("cpu.disable", "", "disable cpu features; comma separated list")displayFeats = flag.Bool("cpu.features", false, "lists cpu features and exits")detectArmFlag = flag.Bool("cpu.arm", false, "allow ARM features to be detected; can potentially crash")}// Supports returns whether the CPU supports all of the requested features.func ( CPUInfo) ( ...FeatureID) bool {for , := range {if !.featureSet.inSet() {returnfalse } }returntrue}// Has allows for checking a single feature.// Should be inlined by the compiler.func ( *CPUInfo) ( FeatureID) bool {return .featureSet.inSet()}// AnyOf returns whether the CPU supports one or more of the requested features.func ( CPUInfo) ( ...FeatureID) bool {for , := range {if .featureSet.inSet() {returntrue } }returnfalse}// Features contains several features combined for a fast check using// CpuInfo.HasAlltypeFeatures *flagSet// CombineFeatures allows to combine several features for a close to constant time lookup.func ( ...FeatureID) Features {varflagSetfor , := range { .set() }return &}func ( *CPUInfo) ( Features) bool {return .featureSet.hasSetP()}// https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levelsvar oneOfLevel = CombineFeatures(SYSEE, SYSCALL)var level1Features = CombineFeatures(CMOV, CMPXCHG8, X87, FXSR, MMX, SSE, SSE2)var level2Features = CombineFeatures(CMOV, CMPXCHG8, X87, FXSR, MMX, SSE, SSE2, CX16, LAHF, POPCNT, SSE3, SSE4, SSE42, SSSE3)var level3Features = CombineFeatures(CMOV, CMPXCHG8, X87, FXSR, MMX, SSE, SSE2, CX16, LAHF, POPCNT, SSE3, SSE4, SSE42, SSSE3, AVX, AVX2, BMI1, BMI2, F16C, FMA3, LZCNT, MOVBE, OSXSAVE)var level4Features = CombineFeatures(CMOV, CMPXCHG8, X87, FXSR, MMX, SSE, SSE2, CX16, LAHF, POPCNT, SSE3, SSE4, SSE42, SSSE3, AVX, AVX2, BMI1, BMI2, F16C, FMA3, LZCNT, MOVBE, OSXSAVE, AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL)// X64Level returns the microarchitecture level detected on the CPU.// If features are lacking or non x64 mode, 0 is returned.// See https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levelsfunc ( CPUInfo) () int {if !.featureSet.hasOneOf(oneOfLevel) {return0 }if .featureSet.hasSetP(level4Features) {return4 }if .featureSet.hasSetP(level3Features) {return3 }if .featureSet.hasSetP(level2Features) {return2 }if .featureSet.hasSetP(level1Features) {return1 }return0}// Disable will disable one or several features.func ( *CPUInfo) ( ...FeatureID) bool {for , := range { .featureSet.unset() }returntrue}// Enable will disable one or several features even if they were undetected.// This is of course not recommended for obvious reasons.func ( *CPUInfo) ( ...FeatureID) bool {for , := range { .featureSet.set() }returntrue}// IsVendor returns true if vendor is recognized as Intelfunc ( CPUInfo) ( Vendor) bool {return .VendorID == }// FeatureSet returns all available features as strings.func ( CPUInfo) () []string { := make([]string, 0, .featureSet.nEnabled()) = append(, .featureSet.Strings()...)return}// RTCounter returns the 64-bit time-stamp counter// Uses the RDTSCP instruction. The value 0 is returned// if the CPU does not support the instruction.func ( CPUInfo) () uint64 {if !.Has(RDTSCP) {return0 } , , , := rdtscpAsm()returnuint64() | (uint64() << 32)}// Ia32TscAux returns the IA32_TSC_AUX part of the RDTSCP.// This variable is OS dependent, but on Linux contains information// about the current cpu/core the code is running on.// If the RDTSCP instruction isn't supported on the CPU, the value 0 is returned.func ( CPUInfo) () uint32 {if !.Has(RDTSCP) {return0 } , , , := rdtscpAsm()return}// SveLengths returns arm SVE vector and predicate lengths in bits.// Will return 0, 0 if SVE is not enabled or otherwise unable to detect.func ( CPUInfo) () (, uint64) {if !.Has(SVE) {return0, 0 }returngetVectorLength()}// LogicalCPU will return the Logical CPU the code is currently executing on.// This is likely to change when the OS re-schedules the running thread// to another CPU.// If the current core cannot be detected, -1 will be returned.func ( CPUInfo) () int {if .maxFunc < 1 {return -1 } , , , := cpuid(1)returnint( >> 24)}// frequencies tries to compute the clock speed of the CPU. If leaf 15 is// supported, use it, otherwise parse the brand string. Yes, really.func ( *CPUInfo) () { .Hz, .BoostFreq = 0, 0 := maxFunctionID()if >= 0x15 { , , , := cpuid(0x15)if != 0 && != 0 && != 0 { .Hz = (int64() * int64()) / int64() } }if >= 0x16 { , , , := cpuid(0x16)// Base...if &0xffff > 0 { .Hz = int64(&0xffff) * 1_000_000 }// Boost...if &0xffff > 0 { .BoostFreq = int64(&0xffff) * 1_000_000 } }if .Hz > 0 {return }// computeHz determines the official rated speed of a CPU from its brand // string. This insanity is *actually the official documented way to do // this according to Intel*, prior to leaf 0x15 existing. The official // documentation only shows this working for exactly `x.xx` or `xxxx` // cases, e.g., `2.50GHz` or `1300MHz`; this parser will accept other // sizes. := .BrandName := strings.LastIndex(, "Hz")if < 3 {return }varint64switch [-1] {case'M': = 1000 * 1000case'G': = 1000 * 1000 * 1000case'T': = 1000 * 1000 * 1000 * 1000 }if == 0 {return } := int64(0) := int64(0) := int64(1)varintfor = - 2; >= 0 && [] != ' '; -- {if [] >= '0' && [] <= '9' { += int64([]-'0') * *= 10 } elseif [] == '.' {if != 0 {return } = } else {return } }// we didn't find a spaceif < 0 {return }if != 0 { .Hz = ( * ) / return } .Hz = * }// VM Will return true if the cpu id indicates we are in// a virtual machine.func ( CPUInfo) () bool {returnCPU.featureSet.inSet(HYPERVISOR)}// flags contains detected cpu features and characteristicstype flags uint64// log2(bits_in_uint64)const flagBitsLog2 = 6const flagBits = 1 << flagBitsLog2const flagMask = flagBits - 1// flagSet contains detected cpu features and characteristics in an array of flagstype flagSet [(lastID + flagMask) / flagBits]flagsfunc ( *flagSet) ( FeatureID) bool {return [>>flagBitsLog2]&(1<<(&flagMask)) != 0}func ( *flagSet) ( FeatureID) { [>>flagBitsLog2] |= 1 << ( & flagMask)}// setIf will set a feature if boolean is true.func ( *flagSet) ( bool, ...FeatureID) {if {for , := range { [>>flagBitsLog2] |= 1 << ( & flagMask) } }}func ( *flagSet) ( FeatureID) { := flags(1 << ( & flagMask)) [>>flagBitsLog2] = [>>flagBitsLog2] & ^}// or with another flagset.func ( *flagSet) ( flagSet) {for , := range [:] { [] |= }}// hasSet returns whether all features are present.func ( *flagSet) ( flagSet) bool {for , := range [:] {if []& != {returnfalse } }returntrue}// hasSet returns whether all features are present.func ( *flagSet) ( *flagSet) bool {for , := range [:] {if []& != {returnfalse } }returntrue}// hasOneOf returns whether one or more features are present.func ( *flagSet) ( *flagSet) bool {for , := range [:] {if []& != 0 {returntrue } }returnfalse}// nEnabled will return the number of enabled flags.func ( *flagSet) () ( int) {for , := range [:] { += bits.OnesCount64(uint64()) }return}func flagSetWith( ...FeatureID) flagSet {varflagSetfor , := range { .set() }return}// ParseFeature will parse the string and return the ID of the matching feature.// Will return UNKNOWN if not found.func ( string) FeatureID { = strings.ToUpper()for := firstID; < lastID; ++ {if .String() == {return } }returnUNKNOWN}// Strings returns an array of the detected features for FlagsSet.func ( flagSet) () []string {iflen() == 0 {return []string{""} } := make([]string, 0)for := firstID; < lastID; ++ {if .inSet() { = append(, .String()) } }return}func maxExtendedFunction() uint32 { , , , := cpuid(0x80000000)return}func maxFunctionID() uint32 { , , , := cpuid(0)return}func brandName() string {ifmaxExtendedFunction() >= 0x80000004 { := make([]uint32, 0, 48)for := uint32(0); < 3; ++ { , , , := cpuid(0x80000002 + ) = append(, , , , ) }returnstrings.Trim(string(valAsString(...)), " ") }return"unknown"}func threadsPerCore() int { := maxFunctionID() , := vendorID()if < 0x4 || ( != Intel && != AMD) {return1 }if < 0xb {if != Intel {return1 } , , , := cpuid(1)if ( & (1 << 28)) != 0 {// v will contain logical core count := ( >> 16) & 255if > 1 { , , , := cpuid(4)// physical cores := ( >> 26) + 1if > 0 {returnint() / int() } } }return1 } , , , := cpuidex(0xb, 0)if &0xffff == 0 {if == AMD {// if >= Zen 2 0x8000001e EBX 15-8 bits means threads per core. // The number of threads per core is ThreadsPerCore+1 // See PPR for AMD Family 17h Models 00h-0Fh (page 82) , , := familyModel() , , , := cpuid(1)if (&(1<<28)) != 0 && >= 23 {ifmaxExtendedFunction() >= 0x8000001e { , , , := cpuid(0x8000001e)returnint((>>8)&0xff) + 1 }return2 } }return1 }returnint( & 0xffff)}func logicalCores() int { := maxFunctionID() , := vendorID()switch {caseIntel:// Use this on old Intel processorsif < 0xb {if < 1 {return0 }// CPUID.1:EBX[23:16] represents the maximum number of addressable IDs (initial APIC ID) // that can be assigned to logical processors in a physical package. // The value may not be the same as the number of logical processors that are present in the hardware of a physical package. , , , := cpuid(1) := ( >> 16) & 0xffreturnint() } , , , := cpuidex(0xb, 1)returnint( & 0xffff)caseAMD, Hygon: , , , := cpuid(1)returnint(( >> 16) & 0xff)default:return0 }}func familyModel() (, , int) {ifmaxFunctionID() < 0x1 {return0, 0, 0 } , , , := cpuid(1)// If BaseFamily[3:0] is less than Fh then ExtendedFamily[7:0] is reserved and Family is equal to BaseFamily[3:0]. = int(( >> 8) & 0xf) := == 0x6// Intel is 0x6, needs extended model.if == 0xf {// Add ExtFamily += int(( >> 20) & 0xff) = true }// If BaseFamily[3:0] is less than 0Fh then ExtendedModel[3:0] is reserved and Model is equal to BaseModel[3:0]. = int(( >> 4) & 0xf)if {// Add ExtModel += int(( >> 12) & 0xf0) } = int( & 0xf)return , , }func physicalCores() int { , := vendorID()switch {caseIntel: := logicalCores() := threadsPerCore()if > 0 && > 0 {return / }return0caseAMD, Hygon: := logicalCores() := threadsPerCore()if > 0 && > 0 {return / }// The following is inaccurate on AMD EPYC 7742 64-Core ProcessorifmaxExtendedFunction() >= 0x80000008 { , , , := cpuid(0x80000008)if &0xff > 0 {returnint(&0xff) + 1 } } }return0}// Except from http://en.wikipedia.org/wiki/CPUID#EAX.3D0:_Get_vendor_IDvar vendorMapping = map[string]Vendor{"AMDisbetter!": AMD,"AuthenticAMD": AMD,"CentaurHauls": VIA,"GenuineIntel": Intel,"TransmetaCPU": Transmeta,"GenuineTMx86": Transmeta,"Geode by NSC": NSC,"VIA VIA VIA ": VIA,"KVMKVMKVM": KVM,"Linux KVM Hv": KVM,"TCGTCGTCGTCG": QEMU,"Microsoft Hv": MSVM,"VMwareVMware": VMware,"XenVMMXenVMM": XenHVM,"bhyve bhyve ": Bhyve,"HygonGenuine": Hygon,"Vortex86 SoC": SiS,"SiS SiS SiS ": SiS,"RiseRiseRise": SiS,"Genuine RDC": RDC,"QNXQVMBSQG": QNX,"ACRNACRNACRN": ACRN,"SRESRESRESRE": SRE,"Apple VZ": Apple,}func vendorID() (Vendor, string) { , , , := cpuid(0) := string(valAsString(, , )) , := vendorMapping[]if ! {returnVendorUnknown, }return , }func hypervisorVendorID() (Vendor, string) {// https://lwn.net/Articles/301888/ , , , := cpuid(0x40000000) := string(valAsString(, , )) , := vendorMapping[]if ! {returnVendorUnknown, }return , }func cacheLine() int {ifmaxFunctionID() < 0x1 {return0 } , , , := cpuid(1) := ( & 0xff00) >> 5// cflush sizeif == 0 && maxExtendedFunction() >= 0x80000006 { , , , := cpuid(0x80000006) = & 0xff// cacheline size }// TODO: Read from Cache and TLB Informationreturnint()}func ( *CPUInfo) () { .Cache.L1D = -1 .Cache.L1I = -1 .Cache.L2 = -1 .Cache.L3 = -1 , := vendorID()switch {caseIntel:ifmaxFunctionID() < 4 {return } .Cache.L1I, .Cache.L1D, .Cache.L2, .Cache.L3 = 0, 0, 0, 0for := uint32(0); ; ++ { , , , := cpuidex(4, ) := & 15if == 0 {break } := ( >> 5) & 7 := int(&0xfff) + 1 := int((>>12)&0x3ff) + 1 := int((>>22)&0x3ff) + 1 := int() + 1 := * * * switch {case1:if == 1 {// 1 = Data Cache .Cache.L1D = } elseif == 2 {// 2 = Instruction Cache .Cache.L1I = } else {if .Cache.L1D < 0 { .Cache.L1I = }if .Cache.L1I < 0 { .Cache.L1I = } }case2: .Cache.L2 = case3: .Cache.L3 = } }caseAMD, Hygon:// Untested.ifmaxExtendedFunction() < 0x80000005 {return } , , , := cpuid(0x80000005) .Cache.L1D = int((( >> 24) & 0xFF) * 1024) .Cache.L1I = int((( >> 24) & 0xFF) * 1024)ifmaxExtendedFunction() < 0x80000006 {return } _, _, , _ = cpuid(0x80000006) .Cache.L2 = int((( >> 16) & 0xFFFF) * 1024)// CPUID Fn8000_001D_EAX_x[N:0] Cache PropertiesifmaxExtendedFunction() < 0x8000001D || !.Has(TOPEXT) {return }// Xen Hypervisor is buggy and returns the same entry no matter ECX value. // Hack: When we encounter the same entry 100 times we break. := 0varuint32for := uint32(0); < math.MaxUint32; ++ { , , , := cpuidex(0x8000001D, ) := ( >> 5) & 7 := + 1 := 1 + ( & 2047) := 1 + (( >> 12) & 511) := 1 + (( >> 22) & 511) := & 15 := int( * * * )if == 0 {return }// Check for the same value repeated. := ^ ^ if == { ++if == 100 {return } } = switch {case1:switch {case1:// Data cache .Cache.L1D = case2:// Inst cache .Cache.L1I = default:if .Cache.L1D < 0 { .Cache.L1I = }if .Cache.L1I < 0 { .Cache.L1I = } }case2: .Cache.L2 = case3: .Cache.L3 = } } }}typeSGXEPCSectionstruct { BaseAddress uint64 EPCSize uint64}typeSGXSupportstruct { Available bool LaunchControl bool SGX1Supported bool SGX2Supported bool MaxEnclaveSizeNot64 int64 MaxEnclaveSize64 int64 EPCSections []SGXEPCSection}func hasSGX(, bool) ( SGXSupport) { .Available = if ! {return } .LaunchControl = , , , := cpuidex(0x12, 0) .SGX1Supported = &0x01 != 0 .SGX2Supported = &0x02 != 0 .MaxEnclaveSizeNot64 = 1 << ( & 0xFF) // pow 2 .MaxEnclaveSize64 = 1 << (( >> 8) & 0xFF) // pow 2 .EPCSections = make([]SGXEPCSection, 0)for := uint32(2); < 2+8; ++ { , , , := cpuidex(0x12, ) := & 0xfif == 0 {// Invalid subleaf, stop iteratingbreak } elseif == 1 {// EPC Section subleaf := uint64(&0xfffff000) + (uint64(&0x000fffff) << 32) := uint64(&0xfffff000) + (uint64(&0x000fffff) << 32) := SGXEPCSection{BaseAddress: , EPCSize: } .EPCSections = append(.EPCSections, ) } }return}typeAMDMemEncryptionSupportstruct { Available bool CBitPossition uint32 NumVMPL uint32 PhysAddrReduction uint32 NumEntryptedGuests uint32 MinSevNoEsAsid uint32}func hasAMDMemEncryption( bool) ( AMDMemEncryptionSupport) { .Available = if ! {return } , , , := cpuidex(0x8000001f, 0) .CBitPossition = & 0x3f .PhysAddrReduction = ( >> 6) & 0x3F .NumVMPL = ( >> 12) & 0xf .NumEntryptedGuests = .MinSevNoEsAsid = return}func support() flagSet {varflagSet := maxFunctionID() , := vendorID()if < 0x1 {return } , , := familyModel() , , , := cpuid(1) .setIf((&(1<<0)) != 0, X87) .setIf((&(1<<8)) != 0, CMPXCHG8) .setIf((&(1<<11)) != 0, SYSEE) .setIf((&(1<<15)) != 0, CMOV) .setIf((&(1<<23)) != 0, MMX) .setIf((&(1<<24)) != 0, FXSR) .setIf((&(1<<25)) != 0, FXSROPT) .setIf((&(1<<25)) != 0, SSE) .setIf((&(1<<26)) != 0, SSE2) .setIf((&1) != 0, SSE3) .setIf((&(1<<5)) != 0, VMX) .setIf((&(1<<9)) != 0, SSSE3) .setIf((&(1<<19)) != 0, SSE4) .setIf((&(1<<20)) != 0, SSE42) .setIf((&(1<<25)) != 0, AESNI) .setIf((&(1<<1)) != 0, CLMUL) .setIf(&(1<<22) != 0, MOVBE) .setIf(&(1<<23) != 0, POPCNT) .setIf(&(1<<30) != 0, RDRAND)// This bit has been reserved by Intel & AMD for use by hypervisors, // and indicates the presence of a hypervisor. .setIf(&(1<<31) != 0, HYPERVISOR) .setIf(&(1<<29) != 0, F16C) .setIf(&(1<<13) != 0, CX16)if == Intel && (&(1<<28)) != 0 && >= 4 { .setIf(threadsPerCore() > 1, HTT) }if == AMD && (&(1<<28)) != 0 && >= 4 { .setIf(threadsPerCore() > 1, HTT) } .setIf(&1<<26 != 0, XSAVE) .setIf(&1<<27 != 0, OSXSAVE)// Check XGETBV/XSAVE (26), OXSAVE (27) and AVX (28) bitsconst = 1<<26 | 1<<27 | 1<<28if & == {// Check for OS support , := xgetbv(0)if ( & 0x6) == 0x6 { .set(AVX)switch {caseIntel:// Older than Haswell. .setIf( == 6 && < 60, AVXSLOW)caseAMD:// Older than Zen 2 .setIf( < 23 || ( == 23 && < 49), AVXSLOW) } } }// FMA3 can be used with SSE registers, so no OS support is strictly needed. // fma3 and OSXSAVE needed.const = 1<<12 | 1<<27 .setIf(& == , FMA3)// Check AVX2, AVX2 requires OS support, but BMI1/2 don't.if >= 7 { , , , := cpuidex(7, 0)if .inSet(AVX) && (&0x00000020) != 0 { .set(AVX2) }// CPUID.(EAX=7, ECX=0).EBXif ( & 0x00000008) != 0 { .set(BMI1) .setIf((&0x00000100) != 0, BMI2) } .setIf(&(1<<2) != 0, SGX) .setIf(&(1<<4) != 0, HLE) .setIf(&(1<<9) != 0, ERMS) .setIf(&(1<<11) != 0, RTM) .setIf(&(1<<14) != 0, MPX) .setIf(&(1<<18) != 0, RDSEED) .setIf(&(1<<19) != 0, ADX) .setIf(&(1<<29) != 0, SHA)// CPUID.(EAX=7, ECX=0).ECX .setIf(&(1<<5) != 0, WAITPKG) .setIf(&(1<<7) != 0, CETSS) .setIf(&(1<<8) != 0, GFNI) .setIf(&(1<<9) != 0, VAES) .setIf(&(1<<10) != 0, VPCLMULQDQ) .setIf(&(1<<13) != 0, TME) .setIf(&(1<<25) != 0, CLDEMOTE) .setIf(&(1<<23) != 0, KEYLOCKER) .setIf(&(1<<27) != 0, MOVDIRI) .setIf(&(1<<28) != 0, MOVDIR64B) .setIf(&(1<<29) != 0, ENQCMD) .setIf(&(1<<30) != 0, SGXLC)// CPUID.(EAX=7, ECX=0).EDX .setIf(&(1<<4) != 0, FSRM) .setIf(&(1<<9) != 0, SRBDS_CTRL) .setIf(&(1<<10) != 0, MD_CLEAR) .setIf(&(1<<11) != 0, RTM_ALWAYS_ABORT) .setIf(&(1<<14) != 0, SERIALIZE) .setIf(&(1<<15) != 0, HYBRID_CPU) .setIf(&(1<<16) != 0, TSXLDTRK) .setIf(&(1<<18) != 0, PCONFIG) .setIf(&(1<<20) != 0, CETIBT) .setIf(&(1<<26) != 0, IBPB) .setIf(&(1<<27) != 0, STIBP) .setIf(&(1<<28) != 0, FLUSH_L1D) .setIf(&(1<<29) != 0, IA32_ARCH_CAP) .setIf(&(1<<30) != 0, IA32_CORE_CAP) .setIf(&(1<<31) != 0, SPEC_CTRL_SSBD)// CPUID.(EAX=7, ECX=1).EAX , , , := cpuidex(7, 1) .setIf(.inSet(AVX) && &(1<<4) != 0, AVXVNNI) .setIf(&(1<<1) != 0, SM3_X86) .setIf(&(1<<2) != 0, SM4_X86) .setIf(&(1<<7) != 0, CMPCCXADD) .setIf(&(1<<10) != 0, MOVSB_ZL) .setIf(&(1<<11) != 0, STOSB_SHORT) .setIf(&(1<<12) != 0, CMPSB_SCADBS_SHORT) .setIf(&(1<<22) != 0, HRESET) .setIf(&(1<<23) != 0, AVXIFMA) .setIf(&(1<<26) != 0, LAM)// CPUID.(EAX=7, ECX=1).EDX .setIf(&(1<<4) != 0, AVXVNNIINT8) .setIf(&(1<<5) != 0, AVXNECONVERT) .setIf(&(1<<6) != 0, AMXTRANSPOSE) .setIf(&(1<<7) != 0, AMXTF32) .setIf(&(1<<8) != 0, AMXCOMPLEX) .setIf(&(1<<10) != 0, AVXVNNIINT16) .setIf(&(1<<14) != 0, PREFETCHI) .setIf(&(1<<19) != 0, AVX10) .setIf(&(1<<21) != 0, APX_F)// Only detect AVX-512 features if XGETBV is supportedif &((1<<26)|(1<<27)) == (1<<26)|(1<<27) {// Check for OS support , := xgetbv(0)// Verify that XCR0[7:5] = ‘111b’ (OPMASK state, upper 256-bit of ZMM0-ZMM15 and // ZMM16-ZMM31 state are enabled by OS) /// and that XCR0[2:1] = ‘11b’ (XMM state and YMM state are enabled by OS). := (>>5)&7 == 7 && (>>1)&3 == 3ifruntime.GOOS == "darwin" { = .inSet(AVX) && darwinHasAVX512() }if { .setIf(&(1<<16) != 0, AVX512F) .setIf(&(1<<17) != 0, AVX512DQ) .setIf(&(1<<21) != 0, AVX512IFMA) .setIf(&(1<<26) != 0, AVX512PF) .setIf(&(1<<27) != 0, AVX512ER) .setIf(&(1<<28) != 0, AVX512CD) .setIf(&(1<<30) != 0, AVX512BW) .setIf(&(1<<31) != 0, AVX512VL)// ecx .setIf(&(1<<1) != 0, AVX512VBMI) .setIf(&(1<<3) != 0, AMXFP8) .setIf(&(1<<6) != 0, AVX512VBMI2) .setIf(&(1<<11) != 0, AVX512VNNI) .setIf(&(1<<12) != 0, AVX512BITALG) .setIf(&(1<<14) != 0, AVX512VPOPCNTDQ)// edx .setIf(&(1<<8) != 0, AVX512VP2INTERSECT) .setIf(&(1<<22) != 0, AMXBF16) .setIf(&(1<<23) != 0, AVX512FP16) .setIf(&(1<<24) != 0, AMXTILE) .setIf(&(1<<25) != 0, AMXINT8)// eax1 = CPUID.(EAX=7, ECX=1).EAX .setIf(&(1<<5) != 0, AVX512BF16) .setIf(&(1<<19) != 0, WRMSRNS) .setIf(&(1<<21) != 0, AMXFP16) .setIf(&(1<<27) != 0, MSRLIST) } }// CPUID.(EAX=7, ECX=2) _, _, _, = cpuidex(7, 2) .setIf(&(1<<0) != 0, PSFD) .setIf(&(1<<1) != 0, IDPRED_CTRL) .setIf(&(1<<2) != 0, RRSBA_CTRL) .setIf(&(1<<4) != 0, BHI_CTRL) .setIf(&(1<<5) != 0, MCDT_NO)if .inSet(SGX) { , , , := cpuidex(0x12, 0) .setIf(&(1<<12) != 0, SGXPQC) }// Add keylocker features.if .inSet(KEYLOCKER) && >= 0x19 { , , , := cpuidex(0x19, 0) .setIf(&5 == 5, KEYLOCKERW) // Bit 0 and 2 (1+4) }// Add AVX10 features.if .inSet(AVX10) && >= 0x24 { , , , := cpuidex(0x24, 0) .setIf(&(1<<16) != 0, AVX10_128) .setIf(&(1<<17) != 0, AVX10_256) .setIf(&(1<<18) != 0, AVX10_512) } }// Processor Extended State Enumeration Sub-leaf (EAX = 0DH, ECX = 1) // EAX // Bit 00: XSAVEOPT is available. // Bit 01: Supports XSAVEC and the compacted form of XRSTOR if set. // Bit 02: Supports XGETBV with ECX = 1 if set. // Bit 03: Supports XSAVES/XRSTORS and IA32_XSS if set. // Bits 31 - 04: Reserved. // EBX // Bits 31 - 00: The size in bytes of the XSAVE area containing all states enabled by XCRO | IA32_XSS. // ECX // Bits 31 - 00: Reports the supported bits of the lower 32 bits of the IA32_XSS MSR. IA32_XSS[n] can be set to 1 only if ECX[n] is 1. // EDX? // Bits 07 - 00: Used for XCR0. Bit 08: PT state. Bit 09: Used for XCR0. Bits 12 - 10: Reserved. Bit 13: HWP state. Bits 31 - 14: Reserved.if >= 0xd {if .inSet(XSAVE) { , , , := cpuidex(0xd, 1) .setIf(&(1<<0) != 0, XSAVEOPT) .setIf(&(1<<1) != 0, XSAVEC) .setIf(&(1<<2) != 0, XGETBV1) .setIf(&(1<<3) != 0, XSAVES) } }ifmaxExtendedFunction() >= 0x80000001 { , , , := cpuid(0x80000001)if ( & (1 << 5)) != 0 { .set(LZCNT) .set(POPCNT) }// ECX .setIf((&(1<<0)) != 0, LAHF) .setIf((&(1<<2)) != 0, SVM) .setIf((&(1<<6)) != 0, SSE4A) .setIf((&(1<<10)) != 0, IBS) .setIf((&(1<<22)) != 0, TOPEXT)// EDX .setIf(&(1<<11) != 0, SYSCALL) .setIf(&(1<<20) != 0, NX) .setIf(&(1<<22) != 0, MMXEXT) .setIf(&(1<<23) != 0, MMX) .setIf(&(1<<24) != 0, FXSR) .setIf(&(1<<25) != 0, FXSROPT) .setIf(&(1<<27) != 0, RDTSCP) .setIf(&(1<<30) != 0, AMD3DNOWEXT) .setIf(&(1<<31) != 0, AMD3DNOW)/* XOP and FMA4 use the AVX instruction coding scheme, so they can't be * used unless the OS has AVX support. */if .inSet(AVX) { .setIf((&(1<<11)) != 0, XOP) .setIf((&(1<<16)) != 0, FMA4) } }ifmaxExtendedFunction() >= 0x80000007 { , , , := cpuid(0x80000007) .setIf((&(1<<0)) != 0, MCAOVERFLOW) .setIf((&(1<<1)) != 0, SUCCOR) .setIf((&(1<<2)) != 0, HWA) .setIf((&(1<<9)) != 0, CPBOOST) }ifmaxExtendedFunction() >= 0x80000008 { , , , := cpuid(0x80000008) .setIf(&(1<<28) != 0, PSFD) .setIf(&(1<<27) != 0, CPPC) .setIf(&(1<<24) != 0, SPEC_CTRL_SSBD) .setIf(&(1<<23) != 0, PPIN) .setIf(&(1<<21) != 0, TLB_FLUSH_NESTED) .setIf(&(1<<20) != 0, EFER_LMSLE_UNS) .setIf(&(1<<19) != 0, IBRS_PROVIDES_SMP) .setIf(&(1<<18) != 0, IBRS_PREFERRED) .setIf(&(1<<17) != 0, STIBP_ALWAYSON) .setIf(&(1<<15) != 0, STIBP) .setIf(&(1<<14) != 0, IBRS) .setIf((&(1<<13)) != 0, INT_WBINVD) .setIf(&(1<<12) != 0, IBPB) .setIf((&(1<<9)) != 0, WBNOINVD) .setIf((&(1<<8)) != 0, MCOMMIT) .setIf((&(1<<4)) != 0, RDPRU) .setIf((&(1<<3)) != 0, INVLPGB) .setIf((&(1<<1)) != 0, MSRIRC) .setIf((&(1<<0)) != 0, CLZERO) }if .inSet(SVM) && maxExtendedFunction() >= 0x8000000A { , , , := cpuid(0x8000000A) .setIf((>>0)&1 == 1, SVMNP) .setIf((>>1)&1 == 1, LBRVIRT) .setIf((>>2)&1 == 1, SVML) .setIf((>>3)&1 == 1, NRIPS) .setIf((>>4)&1 == 1, TSCRATEMSR) .setIf((>>5)&1 == 1, VMCBCLEAN) .setIf((>>6)&1 == 1, SVMFBASID) .setIf((>>7)&1 == 1, SVMDA) .setIf((>>10)&1 == 1, SVMPF) .setIf((>>12)&1 == 1, SVMPFT) }ifmaxExtendedFunction() >= 0x8000001a { , , , := cpuid(0x8000001a) .setIf((>>0)&1 == 1, FP128) .setIf((>>1)&1 == 1, MOVU) .setIf((>>2)&1 == 1, FP256) }ifmaxExtendedFunction() >= 0x8000001b && .inSet(IBS) { , , , := cpuid(0x8000001b) .setIf((>>0)&1 == 1, IBSFFV) .setIf((>>1)&1 == 1, IBSFETCHSAM) .setIf((>>2)&1 == 1, IBSOPSAM) .setIf((>>3)&1 == 1, IBSRDWROPCNT) .setIf((>>4)&1 == 1, IBSOPCNT) .setIf((>>5)&1 == 1, IBSBRNTRGT) .setIf((>>6)&1 == 1, IBSOPCNTEXT) .setIf((>>7)&1 == 1, IBSRIPINVALIDCHK) .setIf((>>8)&1 == 1, IBS_OPFUSE) .setIf((>>9)&1 == 1, IBS_FETCH_CTLX) .setIf((>>10)&1 == 1, IBS_OPDATA4) // Doc says "Fixed,0. IBS op data 4 MSR supported", but assuming they mean 1. .setIf((>>11)&1 == 1, IBS_ZEN4) }ifmaxExtendedFunction() >= 0x8000001f && == AMD { , , , := cpuid(0x8000001f) .setIf((>>0)&1 == 1, SME) .setIf((>>1)&1 == 1, SEV) .setIf((>>2)&1 == 1, MSR_PAGEFLUSH) .setIf((>>3)&1 == 1, SEV_ES) .setIf((>>4)&1 == 1, SEV_SNP) .setIf((>>5)&1 == 1, VMPL) .setIf((>>10)&1 == 1, SME_COHERENT) .setIf((>>11)&1 == 1, SEV_64BIT) .setIf((>>12)&1 == 1, SEV_RESTRICTED) .setIf((>>13)&1 == 1, SEV_ALTERNATIVE) .setIf((>>14)&1 == 1, SEV_DEBUGSWAP) .setIf((>>15)&1 == 1, IBS_PREVENTHOST) .setIf((>>16)&1 == 1, VTE) .setIf((>>24)&1 == 1, VMSA_REGPROT) }ifmaxExtendedFunction() >= 0x80000021 && == AMD { , , , := cpuid(0x80000021) .setIf((>>31)&1 == 1, SRSO_MSR_FIX) .setIf((>>30)&1 == 1, SRSO_USER_KERNEL_NO) .setIf((>>29)&1 == 1, SRSO_NO) .setIf((>>28)&1 == 1, IBPB_BRTYPE) .setIf((>>27)&1 == 1, SBPB) .setIf((>>1)&1 == 1, TSA_L1_NO) .setIf((>>2)&1 == 1, TSA_SQ_NO) .setIf((>>5)&1 == 1, TSA_VERW_CLEAR) }if == AMD {if < 0x19 {// AMD CPUs that are older than Family 19h are not vulnerable to TSA but do not set TSA_L1_NO or TSA_SQ_NO. // Source: https://www.amd.com/content/dam/amd/en/documents/resources/bulletin/technical-guidance-for-mitigating-transient-scheduler-attacks.pdf .set(TSA_L1_NO) .set(TSA_SQ_NO) } elseif == 0x1a {// AMD Family 1Ah models 00h-4Fh and 60h-7Fh are also not vulnerable to TSA but do not set TSA_L1_NO or TSA_SQ_NO. // Future AMD CPUs will set these CPUID bits if appropriate. CPUs will be designed to set these CPUID bits if appropriate. := <= 0x4f || ( >= 0x60 && <= 0x7f) .setIf(, TSA_L1_NO, TSA_SQ_NO) } }if >= 0x20 {// Microsoft has decided to purposefully hide the information // of the guest TEE when VMs are being created using Hyper-V. // // This leads us to check for the Hyper-V cpuid features // (0x4000000C), and then for the `ebx` value set. // // For Intel TDX, `ebx` is set as `0xbe3`, being 3 the part // we're mostly interested about,according to: // https://github.com/torvalds/linux/blob/d2f51b3516dade79269ff45eae2a7668ae711b25/arch/x86/include/asm/hyperv-tlfs.h#L169-L174 , , , := cpuid(0x4000000C) .setIf( == 0xbe3, TDX_GUEST) }if >= 0x21 {// Intel Trusted Domain Extensions Guests have their own cpuid leaf (0x21). , , , := cpuid(0x21) := string(valAsString(, , )) .setIf( == "IntelTDX ", TDX_GUEST) }return}func ( *CPUInfo) () uint8 {if .maxFunc >= 0x24 && .featureSet.inSet(AVX10) { , , , := cpuidex(0x24, 0)returnuint8() }return0}func valAsString( ...uint32) []byte { := make([]byte, 4*len())for , := range { := [*4:] [0] = byte( & 0xff) [1] = byte(( >> 8) & 0xff) [2] = byte(( >> 16) & 0xff) [3] = byte(( >> 24) & 0xff)switch {case [0] == 0:return [:*4]case [1] == 0:return [:*4+1]case [2] == 0:return [:*4+2]case [3] == 0:return [:*4+3] } }return}func parseLeaf0AH( *CPUInfo, , , uint32) ( PerformanceMonitoringInfo) { .VersionID = uint8( & 0xFF) .NumGPCounters = uint8(( >> 8) & 0xFF) .GPPMCWidth = uint8(( >> 16) & 0xFF) .RawEBX = .RawEAX = .RawEDX = if .VersionID > 1 { // This information is only valid if VersionID > 1 .NumFixedPMC = uint8( & 0x1F) // Bits 4:0 .FixedPMCWidth = uint8(( >> 5) & 0xFF) // Bits 12:5 }if .VersionID > 0 {// first 4 fixed events are always instructions retired, cycles, ref cycles and topdown slotsif == 0x0 && .NumFixedPMC == 3 { .featureSet.set(PMU_FIXEDCOUNTER_INSTRUCTIONS) .featureSet.set(PMU_FIXEDCOUNTER_CYCLES) .featureSet.set(PMU_FIXEDCOUNTER_REFCYCLES) }if == 0x0 && .NumFixedPMC == 4 { .featureSet.set(PMU_FIXEDCOUNTER_INSTRUCTIONS) .featureSet.set(PMU_FIXEDCOUNTER_CYCLES) .featureSet.set(PMU_FIXEDCOUNTER_REFCYCLES) .featureSet.set(PMU_FIXEDCOUNTER_TOPDOWN_SLOTS) }if != 0x0 {if (( >> 0) & 1) == 0 { .featureSet.set(PMU_FIXEDCOUNTER_INSTRUCTIONS) }if (( >> 1) & 1) == 0 { .featureSet.set(PMU_FIXEDCOUNTER_CYCLES) }if (( >> 2) & 1) == 0 { .featureSet.set(PMU_FIXEDCOUNTER_REFCYCLES) }if (( >> 3) & 1) == 0 { .featureSet.set(PMU_FIXEDCOUNTER_TOPDOWN_SLOTS) } } }return}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.