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  const (
    18  	// edx bits
    19  	cpuid_SSE2 = 1 << 26
    20  
    21  	// ecx bits
    22  	cpuid_SSE3      = 1 << 0
    23  	cpuid_PCLMULQDQ = 1 << 1
    24  	cpuid_SSSE3     = 1 << 9
    25  	cpuid_FMA       = 1 << 12
    26  	cpuid_SSE41     = 1 << 19
    27  	cpuid_SSE42     = 1 << 20
    28  	cpuid_POPCNT    = 1 << 23
    29  	cpuid_AES       = 1 << 25
    30  	cpuid_OSXSAVE   = 1 << 27
    31  	cpuid_AVX       = 1 << 28
    32  
    33  	// ebx bits
    34  	cpuid_BMI1 = 1 << 3
    35  	cpuid_AVX2 = 1 << 5
    36  	cpuid_BMI2 = 1 << 8
    37  	cpuid_ERMS = 1 << 9
    38  	cpuid_ADX  = 1 << 19
    39  
    40  	// edx bits for CPUID 0x80000001
    41  	cpuid_RDTSCP = 1 << 27
    42  )
    43  
    44  var maxExtendedFunctionInformation uint32
    45  
    46  func doinit() {
    47  	options = []option{
    48  		{Name: "adx", Feature: &X86.HasADX},
    49  		{Name: "aes", Feature: &X86.HasAES},
    50  		{Name: "avx", Feature: &X86.HasAVX},
    51  		{Name: "avx2", Feature: &X86.HasAVX2},
    52  		{Name: "bmi1", Feature: &X86.HasBMI1},
    53  		{Name: "bmi2", Feature: &X86.HasBMI2},
    54  		{Name: "erms", Feature: &X86.HasERMS},
    55  		{Name: "fma", Feature: &X86.HasFMA},
    56  		{Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ},
    57  		{Name: "popcnt", Feature: &X86.HasPOPCNT},
    58  		{Name: "rdtscp", Feature: &X86.HasRDTSCP},
    59  		{Name: "sse3", Feature: &X86.HasSSE3},
    60  		{Name: "sse41", Feature: &X86.HasSSE41},
    61  		{Name: "sse42", Feature: &X86.HasSSE42},
    62  		{Name: "ssse3", Feature: &X86.HasSSSE3},
    63  	}
    64  
    65  	maxID, _, _, _ := cpuid(0, 0)
    66  
    67  	if maxID < 1 {
    68  		return
    69  	}
    70  
    71  	maxExtendedFunctionInformation, _, _, _ = cpuid(0x80000000, 0)
    72  
    73  	_, _, ecx1, _ := cpuid(1, 0)
    74  
    75  	X86.HasSSE3 = isSet(ecx1, cpuid_SSE3)
    76  	X86.HasPCLMULQDQ = isSet(ecx1, cpuid_PCLMULQDQ)
    77  	X86.HasSSSE3 = isSet(ecx1, cpuid_SSSE3)
    78  	X86.HasSSE41 = isSet(ecx1, cpuid_SSE41)
    79  	X86.HasSSE42 = isSet(ecx1, cpuid_SSE42)
    80  	X86.HasPOPCNT = isSet(ecx1, cpuid_POPCNT)
    81  	X86.HasAES = isSet(ecx1, cpuid_AES)
    82  
    83  	// OSXSAVE can be false when using older Operating Systems
    84  	// or when explicitly disabled on newer Operating Systems by
    85  	// e.g. setting the xsavedisable boot option on Windows 10.
    86  	X86.HasOSXSAVE = isSet(ecx1, cpuid_OSXSAVE)
    87  
    88  	// The FMA instruction set extension only has VEX prefixed instructions.
    89  	// VEX prefixed instructions require OSXSAVE to be enabled.
    90  	// See Intel 64 and IA-32 Architecture Software Developer’s Manual Volume 2
    91  	// Section 2.4 "AVX and SSE Instruction Exception Specification"
    92  	X86.HasFMA = isSet(ecx1, cpuid_FMA) && X86.HasOSXSAVE
    93  
    94  	osSupportsAVX := false
    95  	// For XGETBV, OSXSAVE bit is required and sufficient.
    96  	if X86.HasOSXSAVE {
    97  		eax, _ := xgetbv()
    98  		// Check if XMM and YMM registers have OS support.
    99  		osSupportsAVX = isSet(eax, 1<<1) && isSet(eax, 1<<2)
   100  	}
   101  
   102  	X86.HasAVX = isSet(ecx1, cpuid_AVX) && osSupportsAVX
   103  
   104  	if maxID < 7 {
   105  		return
   106  	}
   107  
   108  	_, ebx7, _, _ := cpuid(7, 0)
   109  	X86.HasBMI1 = isSet(ebx7, cpuid_BMI1)
   110  	X86.HasAVX2 = isSet(ebx7, cpuid_AVX2) && osSupportsAVX
   111  	X86.HasBMI2 = isSet(ebx7, cpuid_BMI2)
   112  	X86.HasERMS = isSet(ebx7, cpuid_ERMS)
   113  	X86.HasADX = isSet(ebx7, cpuid_ADX)
   114  
   115  	var maxExtendedInformation uint32
   116  	maxExtendedInformation, _, _, _ = cpuid(0x80000000, 0)
   117  
   118  	if maxExtendedInformation < 0x80000001 {
   119  		return
   120  	}
   121  
   122  	_, _, _, edxExt1 := cpuid(0x80000001, 0)
   123  	X86.HasRDTSCP = isSet(edxExt1, cpuid_RDTSCP)
   124  }
   125  
   126  func isSet(hwc uint32, value uint32) bool {
   127  	return hwc&value != 0
   128  }
   129  
   130  // Name returns the CPU name given by the vendor.
   131  // If the CPU name can not be determined an
   132  // empty string is returned.
   133  func Name() string {
   134  	if maxExtendedFunctionInformation < 0x80000004 {
   135  		return ""
   136  	}
   137  
   138  	data := make([]byte, 0, 3*4*4)
   139  
   140  	var eax, ebx, ecx, edx uint32
   141  	eax, ebx, ecx, edx = cpuid(0x80000002, 0)
   142  	data = appendBytes(data, eax, ebx, ecx, edx)
   143  	eax, ebx, ecx, edx = cpuid(0x80000003, 0)
   144  	data = appendBytes(data, eax, ebx, ecx, edx)
   145  	eax, ebx, ecx, edx = cpuid(0x80000004, 0)
   146  	data = appendBytes(data, eax, ebx, ecx, edx)
   147  
   148  	// Trim leading spaces.
   149  	for len(data) > 0 && data[0] == ' ' {
   150  		data = data[1:]
   151  	}
   152  
   153  	// Trim tail after and including the first null byte.
   154  	for i, c := range data {
   155  		if c == '\x00' {
   156  			data = data[:i]
   157  			break
   158  		}
   159  	}
   160  
   161  	return string(data)
   162  }
   163  
   164  func appendBytes(b []byte, args ...uint32) []byte {
   165  	for _, arg := range args {
   166  		b = append(b,
   167  			byte((arg >> 0)),
   168  			byte((arg >> 8)),
   169  			byte((arg >> 16)),
   170  			byte((arg >> 24)))
   171  	}
   172  	return b
   173  }
   174  

View as plain text