| 1 |
// Copyright 2017 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 |
|
| 5 |
//go:build 386 || amd64 |
| 6 |
|
| 7 |
package cpu |
| 8 |
|
| 9 |
const CacheLinePadSize = 64 |
| 10 |
|
| 11 |
// cpuid is implemented in cpu_x86.s. |
| 12 |
func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) |
| 13 |
|
| 14 |
// xgetbv with ecx = 0 is implemented in cpu_x86.s. |
| 15 |
func xgetbv() (eax, edx uint32) |
| 16 |
|
| 17 |
// getGOAMD64level is implemented in cpu_x86.s. Returns number in [1,4]. |
| 18 |
func getGOAMD64level() int32 |
| 19 |
|
| 20 |
const ( |
| 21 |
// eax bits |
| 22 |
cpuid_AVXVNNI = 1 << 4 |
| 23 |
|
| 24 |
// ecx bits |
| 25 |
cpuid_SSE3 = 1 << 0 |
| 26 |
cpuid_PCLMULQDQ = 1 << 1 |
| 27 |
cpuid_AVX512VBMI = 1 << 1 |
| 28 |
cpuid_AVX512VBMI2 = 1 << 6 |
| 29 |
cpuid_SSSE3 = 1 << 9 |
| 30 |
cpuid_AVX512GFNI = 1 << 8 |
| 31 |
cpuid_VAES = 1 << 9 |
| 32 |
cpuid_AVX512VNNI = 1 << 11 |
| 33 |
cpuid_AVX512BITALG = 1 << 12 |
| 34 |
cpuid_FMA = 1 << 12 |
| 35 |
cpuid_AVX512VPOPCNTDQ = 1 << 14 |
| 36 |
cpuid_SSE41 = 1 << 19 |
| 37 |
cpuid_SSE42 = 1 << 20 |
| 38 |
cpuid_POPCNT = 1 << 23 |
| 39 |
cpuid_AES = 1 << 25 |
| 40 |
cpuid_OSXSAVE = 1 << 27 |
| 41 |
cpuid_AVX = 1 << 28 |
| 42 |
|
| 43 |
// "Extended Feature Flag" bits returned in EBX for CPUID EAX=0x7 ECX=0x0 |
| 44 |
cpuid_BMI1 = 1 << 3 |
| 45 |
cpuid_AVX2 = 1 << 5 |
| 46 |
cpuid_BMI2 = 1 << 8 |
| 47 |
cpuid_ERMS = 1 << 9 |
| 48 |
cpuid_AVX512F = 1 << 16 |
| 49 |
cpuid_AVX512DQ = 1 << 17 |
| 50 |
cpuid_ADX = 1 << 19 |
| 51 |
cpuid_AVX512CD = 1 << 28 |
| 52 |
cpuid_SHA = 1 << 29 |
| 53 |
cpuid_AVX512BW = 1 << 30 |
| 54 |
cpuid_AVX512VL = 1 << 31 |
| 55 |
|
| 56 |
// "Extended Feature Flag" bits returned in ECX for CPUID EAX=0x7 ECX=0x0 |
| 57 |
cpuid_AVX512_VBMI = 1 << 1 |
| 58 |
cpuid_AVX512_VBMI2 = 1 << 6 |
| 59 |
cpuid_GFNI = 1 << 8 |
| 60 |
cpuid_AVX512VPCLMULQDQ = 1 << 10 |
| 61 |
cpuid_AVX512_BITALG = 1 << 12 |
| 62 |
|
| 63 |
// edx bits |
| 64 |
cpuid_FSRM = 1 << 4 |
| 65 |
// edx bits for CPUID 0x80000001 |
| 66 |
cpuid_RDTSCP = 1 << 27 |
| 67 |
) |
| 68 |
|
| 69 |
var maxExtendedFunctionInformation uint32 |
| 70 |
|
| 71 |
func doinit() { |
| 72 |
options = []option{ |
| 73 |
{Name: "adx", Feature: &X86.HasADX}, |
| 74 |
{Name: "aes", Feature: &X86.HasAES}, |
| 75 |
{Name: "erms", Feature: &X86.HasERMS}, |
| 76 |
{Name: "fsrm", Feature: &X86.HasFSRM}, |
| 77 |
{Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ}, |
| 78 |
{Name: "rdtscp", Feature: &X86.HasRDTSCP}, |
| 79 |
{Name: "sha", Feature: &X86.HasSHA}, |
| 80 |
{Name: "vpclmulqdq", Feature: &X86.HasAVX512VPCLMULQDQ}, |
| 81 |
} |
| 82 |
level := getGOAMD64level() |
| 83 |
if level < 2 { |
| 84 |
// These options are required at level 2. At lower levels |
| 85 |
// they can be turned off. |
| 86 |
options = append(options, |
| 87 |
option{Name: "popcnt", Feature: &X86.HasPOPCNT}, |
| 88 |
option{Name: "sse3", Feature: &X86.HasSSE3}, |
| 89 |
option{Name: "sse41", Feature: &X86.HasSSE41}, |
| 90 |
option{Name: "sse42", Feature: &X86.HasSSE42}, |
| 91 |
option{Name: "ssse3", Feature: &X86.HasSSSE3}) |
| 92 |
} |
| 93 |
if level < 3 { |
| 94 |
// These options are required at level 3. At lower levels |
| 95 |
// they can be turned off. |
| 96 |
options = append(options, |
| 97 |
option{Name: "avx", Feature: &X86.HasAVX}, |
| 98 |
option{Name: "avx2", Feature: &X86.HasAVX2}, |
| 99 |
option{Name: "bmi1", Feature: &X86.HasBMI1}, |
| 100 |
option{Name: "bmi2", Feature: &X86.HasBMI2}, |
| 101 |
option{Name: "fma", Feature: &X86.HasFMA}) |
| 102 |
} |
| 103 |
if level < 4 { |
| 104 |
// These options are required at level 4. At lower levels |
| 105 |
// they can be turned off. |
| 106 |
options = append(options, |
| 107 |
option{Name: "avx512f", Feature: &X86.HasAVX512F}, |
| 108 |
option{Name: "avx512cd", Feature: &X86.HasAVX512CD}, |
| 109 |
option{Name: "avx512bw", Feature: &X86.HasAVX512BW}, |
| 110 |
option{Name: "avx512dq", Feature: &X86.HasAVX512DQ}, |
| 111 |
option{Name: "avx512vl", Feature: &X86.HasAVX512VL}, |
| 112 |
) |
| 113 |
} |
| 114 |
|
| 115 |
maxID, _, _, _ := cpuid(0, 0) |
| 116 |
|
| 117 |
if maxID < 1 { |
| 118 |
osInit() |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
maxExtendedFunctionInformation, _, _, _ = cpuid(0x80000000, 0) |
| 123 |
|
| 124 |
_, _, ecx1, _ := cpuid(1, 0) |
| 125 |
|
| 126 |
X86.HasSSE3 = isSet(ecx1, cpuid_SSE3) |
| 127 |
X86.HasPCLMULQDQ = isSet(ecx1, cpuid_PCLMULQDQ) |
| 128 |
X86.HasSSSE3 = isSet(ecx1, cpuid_SSSE3) |
| 129 |
X86.HasSSE41 = isSet(ecx1, cpuid_SSE41) |
| 130 |
X86.HasSSE42 = isSet(ecx1, cpuid_SSE42) |
| 131 |
X86.HasPOPCNT = isSet(ecx1, cpuid_POPCNT) |
| 132 |
X86.HasAES = isSet(ecx1, cpuid_AES) |
| 133 |
|
| 134 |
// OSXSAVE can be false when using older Operating Systems |
| 135 |
// or when explicitly disabled on newer Operating Systems by |
| 136 |
// e.g. setting the xsavedisable boot option on Windows 10. |
| 137 |
X86.HasOSXSAVE = isSet(ecx1, cpuid_OSXSAVE) |
| 138 |
|
| 139 |
osSupportsAVX := false |
| 140 |
osSupportsAVX512 := false |
| 141 |
// For XGETBV, OSXSAVE bit is required and sufficient. |
| 142 |
if X86.HasOSXSAVE { |
| 143 |
eax, _ := xgetbv() |
| 144 |
// Check if XMM and YMM registers have OS support. |
| 145 |
osSupportsAVX = isSet(eax, 1<<1) && isSet(eax, 1<<2) |
| 146 |
|
| 147 |
// AVX512 detection does not work on Darwin, |
| 148 |
// see https://github.com/golang/go/issues/49233 |
| 149 |
// |
| 150 |
// Check if opmask, ZMMhi256 and Hi16_ZMM have OS support. |
| 151 |
osSupportsAVX512 = osSupportsAVX && isSet(eax, 1<<5) && isSet(eax, 1<<6) && isSet(eax, 1<<7) |
| 152 |
} |
| 153 |
|
| 154 |
X86.HasAVX = isSet(ecx1, cpuid_AVX) && osSupportsAVX |
| 155 |
|
| 156 |
// The FMA instruction set extension requires both the FMA and AVX flags. |
| 157 |
// |
| 158 |
// Furthermore, the FMA instructions are all VEX prefixed instructions. |
| 159 |
// VEX prefixed instructions require OSXSAVE to be enabled. |
| 160 |
// See Intel 64 and IA-32 Architecture Software Developer’s Manual Volume 2 |
| 161 |
// Section 2.4 "AVX and SSE Instruction Exception Specification" |
| 162 |
X86.HasFMA = isSet(ecx1, cpuid_FMA) && X86.HasAVX && X86.HasOSXSAVE |
| 163 |
|
| 164 |
if maxID < 7 { |
| 165 |
osInit() |
| 166 |
return |
| 167 |
} |
| 168 |
|
| 169 |
eax7, ebx7, ecx7, edx7 := cpuid(7, 0) |
| 170 |
X86.HasBMI1 = isSet(ebx7, cpuid_BMI1) |
| 171 |
X86.HasAVX2 = isSet(ebx7, cpuid_AVX2) && osSupportsAVX |
| 172 |
X86.HasBMI2 = isSet(ebx7, cpuid_BMI2) |
| 173 |
X86.HasERMS = isSet(ebx7, cpuid_ERMS) |
| 174 |
X86.HasADX = isSet(ebx7, cpuid_ADX) |
| 175 |
X86.HasSHA = isSet(ebx7, cpuid_SHA) |
| 176 |
X86.HasVAES = isSet(ecx7, cpuid_VAES) && X86.HasAVX |
| 177 |
|
| 178 |
X86.HasAVX512F = isSet(ebx7, cpuid_AVX512F) && osSupportsAVX512 |
| 179 |
if X86.HasAVX512F { |
| 180 |
X86.HasAVX512CD = isSet(ebx7, cpuid_AVX512CD) |
| 181 |
X86.HasAVX512BW = isSet(ebx7, cpuid_AVX512BW) |
| 182 |
X86.HasAVX512DQ = isSet(ebx7, cpuid_AVX512DQ) |
| 183 |
X86.HasAVX512VL = isSet(ebx7, cpuid_AVX512VL) |
| 184 |
X86.HasAVX512GFNI = isSet(ecx7, cpuid_AVX512GFNI) |
| 185 |
X86.HasAVX512BITALG = isSet(ecx7, cpuid_AVX512BITALG) |
| 186 |
X86.HasAVX512VPOPCNTDQ = isSet(ecx7, cpuid_AVX512VPOPCNTDQ) |
| 187 |
X86.HasAVX512VBMI = isSet(ecx7, cpuid_AVX512VBMI) |
| 188 |
X86.HasAVX512VBMI2 = isSet(ecx7, cpuid_AVX512VBMI2) |
| 189 |
X86.HasAVX512VAES = isSet(ecx7, cpuid_VAES) && X86.HasAES && isSet(ebx7, cpuid_AVX512VL) |
| 190 |
X86.HasAVX512VNNI = isSet(ecx7, cpuid_AVX512VNNI) |
| 191 |
X86.HasAVX512VPCLMULQDQ = isSet(ecx7, cpuid_AVX512VPCLMULQDQ) |
| 192 |
X86.HasAVX512VBMI = isSet(ecx7, cpuid_AVX512_VBMI) |
| 193 |
X86.HasAVX512VBMI2 = isSet(ecx7, cpuid_AVX512_VBMI2) |
| 194 |
X86.HasGFNI = isSet(ecx7, cpuid_GFNI) |
| 195 |
X86.HasAVX512BITALG = isSet(ecx7, cpuid_AVX512_BITALG) |
| 196 |
} |
| 197 |
|
| 198 |
X86.HasFSRM = isSet(edx7, cpuid_FSRM) |
| 199 |
|
| 200 |
var maxExtendedInformation uint32 |
| 201 |
maxExtendedInformation, _, _, _ = cpuid(0x80000000, 0) |
| 202 |
|
| 203 |
if maxExtendedInformation < 0x80000001 { |
| 204 |
osInit() |
| 205 |
return |
| 206 |
} |
| 207 |
|
| 208 |
_, _, _, edxExt1 := cpuid(0x80000001, 0) |
| 209 |
X86.HasRDTSCP = isSet(edxExt1, cpuid_RDTSCP) |
| 210 |
|
| 211 |
doDerived = func() { |
| 212 |
// Rather than carefully gating on fundamental AVX-512 features, we have |
| 213 |
// a virtual "AVX512" feature that captures F+CD+BW+DQ+VL. BW, DQ, and |
| 214 |
// VL have a huge effect on which AVX-512 instructions are available, |
| 215 |
// and these have all been supported on everything except the earliest |
| 216 |
// Phi chips with AVX-512. No CPU has had CD without F, so we include |
| 217 |
// it. GOAMD64=v4 also implies exactly this set, and these are all |
| 218 |
// included in AVX10.1. |
| 219 |
X86.HasAVX512 = X86.HasAVX512F && X86.HasAVX512CD && X86.HasAVX512BW && X86.HasAVX512DQ && X86.HasAVX512VL |
| 220 |
} |
| 221 |
|
| 222 |
if eax7 >= 1 { |
| 223 |
eax71, _, _, _ := cpuid(7, 1) |
| 224 |
if X86.HasAVX { |
| 225 |
X86.HasAVXVNNI = isSet(eax71, cpuid_AVXVNNI) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
osInit() |
| 230 |
} |
| 231 |
|
| 232 |
func isSet(hwc uint32, value uint32) bool { |
| 233 |
return hwc&value != 0 |
| 234 |
} |
| 235 |
|
| 236 |
// Name returns the CPU name given by the vendor. |
| 237 |
// If the CPU name can not be determined an |
| 238 |
// empty string is returned. |
| 239 |
func Name() string { |
| 240 |
if maxExtendedFunctionInformation < 0x80000004 { |
| 241 |
return "" |
| 242 |
} |
| 243 |
|
| 244 |
data := make([]byte, 0, 3*4*4) |
| 245 |
|
| 246 |
var eax, ebx, ecx, edx uint32 |
| 247 |
eax, ebx, ecx, edx = cpuid(0x80000002, 0) |
| 248 |
data = appendBytes(data, eax, ebx, ecx, edx) |
| 249 |
eax, ebx, ecx, edx = cpuid(0x80000003, 0) |
| 250 |
data = appendBytes(data, eax, ebx, ecx, edx) |
| 251 |
eax, ebx, ecx, edx = cpuid(0x80000004, 0) |
| 252 |
data = appendBytes(data, eax, ebx, ecx, edx) |
| 253 |
|
| 254 |
// Trim leading spaces. |
| 255 |
for len(data) > 0 && data[0] == ' ' { |
| 256 |
data = data[1:] |
| 257 |
} |
| 258 |
|
| 259 |
// Trim tail after and including the first null byte. |
| 260 |
for i, c := range data { |
| 261 |
if c == '\x00' { |
| 262 |
data = data[:i] |
| 263 |
break |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return string(data) |
| 268 |
} |
| 269 |
|
| 270 |
func appendBytes(b []byte, args ...uint32) []byte { |
| 271 |
for _, arg := range args { |
| 272 |
b = append(b, |
| 273 |
byte((arg >> 0)), |
| 274 |
byte((arg >> 8)), |
| 275 |
byte((arg >> 16)), |
| 276 |
byte((arg >> 24))) |
| 277 |
} |
| 278 |
return b |
| 279 |
} |