Source file src/internal/cpu/cpu_x86.go

     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_AVX512VAES      = 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  	// The FMA instruction set extension only has VEX prefixed instructions.
   140  	// VEX prefixed instructions require OSXSAVE to be enabled.
   141  	// See Intel 64 and IA-32 Architecture Software Developer’s Manual Volume 2
   142  	// Section 2.4 "AVX and SSE Instruction Exception Specification"
   143  	X86.HasFMA = isSet(ecx1, cpuid_FMA) && X86.HasOSXSAVE
   144  
   145  	osSupportsAVX := false
   146  	osSupportsAVX512 := false
   147  	// For XGETBV, OSXSAVE bit is required and sufficient.
   148  	if X86.HasOSXSAVE {
   149  		eax, _ := xgetbv()
   150  		// Check if XMM and YMM registers have OS support.
   151  		osSupportsAVX = isSet(eax, 1<<1) && isSet(eax, 1<<2)
   152  
   153  		// AVX512 detection does not work on Darwin,
   154  		// see https://github.com/golang/go/issues/49233
   155  		//
   156  		// Check if opmask, ZMMhi256 and Hi16_ZMM have OS support.
   157  		osSupportsAVX512 = osSupportsAVX && isSet(eax, 1<<5) && isSet(eax, 1<<6) && isSet(eax, 1<<7)
   158  	}
   159  
   160  	X86.HasAVX = isSet(ecx1, cpuid_AVX) && osSupportsAVX
   161  
   162  	if maxID < 7 {
   163  		osInit()
   164  		return
   165  	}
   166  
   167  	eax7, ebx7, ecx7, edx7 := cpuid(7, 0)
   168  	X86.HasBMI1 = isSet(ebx7, cpuid_BMI1)
   169  	X86.HasAVX2 = isSet(ebx7, cpuid_AVX2) && osSupportsAVX
   170  	X86.HasBMI2 = isSet(ebx7, cpuid_BMI2)
   171  	X86.HasERMS = isSet(ebx7, cpuid_ERMS)
   172  	X86.HasADX = isSet(ebx7, cpuid_ADX)
   173  	X86.HasSHA = isSet(ebx7, cpuid_SHA)
   174  
   175  	X86.HasAVX512F = isSet(ebx7, cpuid_AVX512F) && osSupportsAVX512
   176  	if X86.HasAVX512F {
   177  		X86.HasAVX512CD = isSet(ebx7, cpuid_AVX512CD)
   178  		X86.HasAVX512BW = isSet(ebx7, cpuid_AVX512BW)
   179  		X86.HasAVX512DQ = isSet(ebx7, cpuid_AVX512DQ)
   180  		X86.HasAVX512VL = isSet(ebx7, cpuid_AVX512VL)
   181  		X86.HasAVX512GFNI = isSet(ecx7, cpuid_AVX512GFNI)
   182  		X86.HasAVX512BITALG = isSet(ecx7, cpuid_AVX512BITALG)
   183  		X86.HasAVX512VPOPCNTDQ = isSet(ecx7, cpuid_AVX512VPOPCNTDQ)
   184  		X86.HasAVX512VBMI = isSet(ecx7, cpuid_AVX512VBMI)
   185  		X86.HasAVX512VBMI2 = isSet(ecx7, cpuid_AVX512VBMI2)
   186  		X86.HasAVX512VAES = isSet(ecx7, cpuid_AVX512VAES)
   187  		X86.HasAVX512VNNI = isSet(ecx7, cpuid_AVX512VNNI)
   188  		X86.HasAVX512VPCLMULQDQ = isSet(ecx7, cpuid_AVX512VPCLMULQDQ)
   189  		X86.HasAVX512VBMI = isSet(ecx7, cpuid_AVX512_VBMI)
   190  		X86.HasAVX512VBMI2 = isSet(ecx7, cpuid_AVX512_VBMI2)
   191  		X86.HasGFNI = isSet(ecx7, cpuid_GFNI)
   192  		X86.HasAVX512BITALG = isSet(ecx7, cpuid_AVX512_BITALG)
   193  	}
   194  
   195  	X86.HasFSRM = isSet(edx7, cpuid_FSRM)
   196  
   197  	var maxExtendedInformation uint32
   198  	maxExtendedInformation, _, _, _ = cpuid(0x80000000, 0)
   199  
   200  	if maxExtendedInformation < 0x80000001 {
   201  		osInit()
   202  		return
   203  	}
   204  
   205  	_, _, _, edxExt1 := cpuid(0x80000001, 0)
   206  	X86.HasRDTSCP = isSet(edxExt1, cpuid_RDTSCP)
   207  
   208  	doDerived = func() {
   209  		// Rather than carefully gating on fundamental AVX-512 features, we have
   210  		// a virtual "AVX512" feature that captures F+CD+BW+DQ+VL. BW, DQ, and
   211  		// VL have a huge effect on which AVX-512 instructions are available,
   212  		// and these have all been supported on everything except the earliest
   213  		// Phi chips with AVX-512. No CPU has had CD without F, so we include
   214  		// it. GOAMD64=v4 also implies exactly this set, and these are all
   215  		// included in AVX10.1.
   216  		X86.HasAVX512 = X86.HasAVX512F && X86.HasAVX512CD && X86.HasAVX512BW && X86.HasAVX512DQ && X86.HasAVX512VL
   217  	}
   218  
   219  	if eax7 >= 1 {
   220  		eax71, _, _, _ := cpuid(7, 1)
   221  		if X86.HasAVX {
   222  			X86.HasAVXVNNI = isSet(4, eax71)
   223  		}
   224  	}
   225  
   226  	osInit()
   227  }
   228  
   229  func isSet(hwc uint32, value uint32) bool {
   230  	return hwc&value != 0
   231  }
   232  
   233  // Name returns the CPU name given by the vendor.
   234  // If the CPU name can not be determined an
   235  // empty string is returned.
   236  func Name() string {
   237  	if maxExtendedFunctionInformation < 0x80000004 {
   238  		return ""
   239  	}
   240  
   241  	data := make([]byte, 0, 3*4*4)
   242  
   243  	var eax, ebx, ecx, edx uint32
   244  	eax, ebx, ecx, edx = cpuid(0x80000002, 0)
   245  	data = appendBytes(data, eax, ebx, ecx, edx)
   246  	eax, ebx, ecx, edx = cpuid(0x80000003, 0)
   247  	data = appendBytes(data, eax, ebx, ecx, edx)
   248  	eax, ebx, ecx, edx = cpuid(0x80000004, 0)
   249  	data = appendBytes(data, eax, ebx, ecx, edx)
   250  
   251  	// Trim leading spaces.
   252  	for len(data) > 0 && data[0] == ' ' {
   253  		data = data[1:]
   254  	}
   255  
   256  	// Trim tail after and including the first null byte.
   257  	for i, c := range data {
   258  		if c == '\x00' {
   259  			data = data[:i]
   260  			break
   261  		}
   262  	}
   263  
   264  	return string(data)
   265  }
   266  
   267  func appendBytes(b []byte, args ...uint32) []byte {
   268  	for _, arg := range args {
   269  		b = append(b,
   270  			byte((arg >> 0)),
   271  			byte((arg >> 8)),
   272  			byte((arg >> 16)),
   273  			byte((arg >> 24)))
   274  	}
   275  	return b
   276  }
   277  

View as plain text