...
Run Format

Source file src/internal/cpu/cpu.go

Documentation: internal/cpu

     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  // Package cpu implements processor feature detection
     6  // used by the Go standard library.
     7  package cpu
     8  
     9  // debugOptions is set to true by the runtime if go was compiled with GOEXPERIMENT=debugcpu
    10  // and GOOS is Linux or Darwin. This variable is linknamed in runtime/proc.go.
    11  var debugOptions bool
    12  
    13  var X86 x86
    14  
    15  // The booleans in x86 contain the correspondingly named cpuid feature bit.
    16  // HasAVX and HasAVX2 are only set if the OS does support XMM and YMM registers
    17  // in addition to the cpuid feature bit being set.
    18  // The struct is padded to avoid false sharing.
    19  type x86 struct {
    20  	_            [CacheLineSize]byte
    21  	HasAES       bool
    22  	HasADX       bool
    23  	HasAVX       bool
    24  	HasAVX2      bool
    25  	HasBMI1      bool
    26  	HasBMI2      bool
    27  	HasERMS      bool
    28  	HasFMA       bool
    29  	HasOSXSAVE   bool
    30  	HasPCLMULQDQ bool
    31  	HasPOPCNT    bool
    32  	HasSSE2      bool
    33  	HasSSE3      bool
    34  	HasSSSE3     bool
    35  	HasSSE41     bool
    36  	HasSSE42     bool
    37  	_            [CacheLineSize]byte
    38  }
    39  
    40  var PPC64 ppc64
    41  
    42  // For ppc64x, it is safe to check only for ISA level starting on ISA v3.00,
    43  // since there are no optional categories. There are some exceptions that also
    44  // require kernel support to work (darn, scv), so there are feature bits for
    45  // those as well. The minimum processor requirement is POWER8 (ISA 2.07), so we
    46  // maintain some of the old feature checks for optional categories for
    47  // safety.
    48  // The struct is padded to avoid false sharing.
    49  type ppc64 struct {
    50  	_          [CacheLineSize]byte
    51  	HasVMX     bool // Vector unit (Altivec)
    52  	HasDFP     bool // Decimal Floating Point unit
    53  	HasVSX     bool // Vector-scalar unit
    54  	HasHTM     bool // Hardware Transactional Memory
    55  	HasISEL    bool // Integer select
    56  	HasVCRYPTO bool // Vector cryptography
    57  	HasHTMNOSC bool // HTM: kernel-aborted transaction in syscalls
    58  	HasDARN    bool // Hardware random number generator (requires kernel enablement)
    59  	HasSCV     bool // Syscall vectored (requires kernel enablement)
    60  	IsPOWER8   bool // ISA v2.07 (POWER8)
    61  	IsPOWER9   bool // ISA v3.00 (POWER9)
    62  	_          [CacheLineSize]byte
    63  }
    64  
    65  var ARM64 arm64
    66  
    67  // The booleans in arm64 contain the correspondingly named cpu feature bit.
    68  // The struct is padded to avoid false sharing.
    69  type arm64 struct {
    70  	_           [CacheLineSize]byte
    71  	HasFP       bool
    72  	HasASIMD    bool
    73  	HasEVTSTRM  bool
    74  	HasAES      bool
    75  	HasPMULL    bool
    76  	HasSHA1     bool
    77  	HasSHA2     bool
    78  	HasCRC32    bool
    79  	HasATOMICS  bool
    80  	HasFPHP     bool
    81  	HasASIMDHP  bool
    82  	HasCPUID    bool
    83  	HasASIMDRDM bool
    84  	HasJSCVT    bool
    85  	HasFCMA     bool
    86  	HasLRCPC    bool
    87  	HasDCPOP    bool
    88  	HasSHA3     bool
    89  	HasSM3      bool
    90  	HasSM4      bool
    91  	HasASIMDDP  bool
    92  	HasSHA512   bool
    93  	HasSVE      bool
    94  	HasASIMDFHM bool
    95  	_           [CacheLineSize]byte
    96  }
    97  
    98  var S390X s390x
    99  
   100  type s390x struct {
   101  	_               [CacheLineSize]byte
   102  	HasZArch        bool // z architecture mode is active [mandatory]
   103  	HasSTFLE        bool // store facility list extended [mandatory]
   104  	HasLDisp        bool // long (20-bit) displacements [mandatory]
   105  	HasEImm         bool // 32-bit immediates [mandatory]
   106  	HasDFP          bool // decimal floating point
   107  	HasETF3Enhanced bool // ETF-3 enhanced
   108  	HasMSA          bool // message security assist (CPACF)
   109  	HasAES          bool // KM-AES{128,192,256} functions
   110  	HasAESCBC       bool // KMC-AES{128,192,256} functions
   111  	HasAESCTR       bool // KMCTR-AES{128,192,256} functions
   112  	HasAESGCM       bool // KMA-GCM-AES{128,192,256} functions
   113  	HasGHASH        bool // KIMD-GHASH function
   114  	HasSHA1         bool // K{I,L}MD-SHA-1 functions
   115  	HasSHA256       bool // K{I,L}MD-SHA-256 functions
   116  	HasSHA512       bool // K{I,L}MD-SHA-512 functions
   117  	HasVX           bool // vector facility. Note: the runtime sets this when it processes auxv records.
   118  	_               [CacheLineSize]byte
   119  }
   120  
   121  // initialize examines the processor and sets the relevant variables above.
   122  // This is called by the runtime package early in program initialization,
   123  // before normal init functions are run. env is set by runtime on Linux and Darwin
   124  // if go was compiled with GOEXPERIMENT=debugcpu.
   125  func initialize(env string) {
   126  	doinit()
   127  	processOptions(env)
   128  }
   129  
   130  // options contains the cpu debug options that can be used in GODEBUGCPU.
   131  // Options are arch dependent and are added by the arch specific doinit functions.
   132  // Features that are mandatory for the specific GOARCH should not be added to options
   133  // (e.g. SSE2 on amd64).
   134  var options []option
   135  
   136  // Option names should be lower case. e.g. avx instead of AVX.
   137  type option struct {
   138  	Name    string
   139  	Feature *bool
   140  }
   141  
   142  // processOptions disables CPU feature values based on the parsed env string.
   143  // The env string is expected to be of the form feature1=0,feature2=0...
   144  // where feature names is one of the architecture specifc list stored in the
   145  // cpu packages options variable. If env contains all=0 then all capabilities
   146  // referenced through the options variable are disabled. Other feature
   147  // names and values other than 0 are silently ignored.
   148  func processOptions(env string) {
   149  field:
   150  	for env != "" {
   151  		field := ""
   152  		i := indexByte(env, ',')
   153  		if i < 0 {
   154  			field, env = env, ""
   155  		} else {
   156  			field, env = env[:i], env[i+1:]
   157  		}
   158  		i = indexByte(field, '=')
   159  		if i < 0 {
   160  			continue
   161  		}
   162  		key, value := field[:i], field[i+1:]
   163  
   164  		// Only allow turning off CPU features by specifying '0'.
   165  		if value == "0" {
   166  			if key == "all" {
   167  				for _, v := range options {
   168  					*v.Feature = false
   169  				}
   170  				return
   171  			} else {
   172  				for _, v := range options {
   173  					if v.Name == key {
   174  						*v.Feature = false
   175  						continue field
   176  					}
   177  				}
   178  			}
   179  		}
   180  	}
   181  }
   182  
   183  // indexByte returns the index of the first instance of c in s,
   184  // or -1 if c is not present in s.
   185  func indexByte(s string, c byte) int {
   186  	for i := 0; i < len(s); i++ {
   187  		if s[i] == c {
   188  			return i
   189  		}
   190  	}
   191  	return -1
   192  }
   193  

View as plain text