Source file src/internal/cpu/cpu_arm64_hwcap.go

     1  // Copyright 2020 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 arm64 && linux
     6  
     7  package cpu
     8  
     9  import _ "unsafe" // for linkname
    10  
    11  // HWCap may be initialized by archauxv and
    12  // should not be changed after it was initialized.
    13  //
    14  // Other widely used packages
    15  // access HWCap using linkname as well, most notably:
    16  //   - github.com/klauspost/cpuid/v2
    17  //
    18  // Do not remove or change the type signature.
    19  // See go.dev/issue/67401.
    20  //
    21  //go:linkname HWCap
    22  var HWCap uint
    23  
    24  // HWCAP bits. These are exposed by Linux.
    25  const (
    26  	hwcap_AES     = 1 << 3
    27  	hwcap_PMULL   = 1 << 4
    28  	hwcap_SHA1    = 1 << 5
    29  	hwcap_SHA2    = 1 << 6
    30  	hwcap_CRC32   = 1 << 7
    31  	hwcap_ATOMICS = 1 << 8
    32  	hwcap_CPUID   = 1 << 11
    33  	hwcap_SHA512  = 1 << 21
    34  	hwcap_DIT     = 1 << 24
    35  )
    36  
    37  func hwcapInit(os string) {
    38  	// HWCap was populated by the runtime from the auxiliary vector.
    39  	// Use HWCap information since reading aarch64 system registers
    40  	// is not supported in user space on older linux kernels.
    41  	ARM64.HasAES = isSet(HWCap, hwcap_AES)
    42  	ARM64.HasPMULL = isSet(HWCap, hwcap_PMULL)
    43  	ARM64.HasSHA1 = isSet(HWCap, hwcap_SHA1)
    44  	ARM64.HasSHA2 = isSet(HWCap, hwcap_SHA2)
    45  	ARM64.HasCRC32 = isSet(HWCap, hwcap_CRC32)
    46  	ARM64.HasCPUID = isSet(HWCap, hwcap_CPUID)
    47  	ARM64.HasSHA512 = isSet(HWCap, hwcap_SHA512)
    48  	ARM64.HasDIT = isSet(HWCap, hwcap_DIT)
    49  
    50  	// The Samsung S9+ kernel reports support for atomics, but not all cores
    51  	// actually support them, resulting in SIGILL. See issue #28431.
    52  	// TODO(elias.naur): Only disable the optimization on bad chipsets on android.
    53  	ARM64.HasATOMICS = isSet(HWCap, hwcap_ATOMICS) && os != "android"
    54  
    55  	// Check to see if executing on a Neoverse core and in order to do that,
    56  	// check the AUXV for the CPUID bit. The getMIDR function executes an
    57  	// instruction which would normally be an illegal instruction, but it's
    58  	// trapped by the kernel, the value sanitized and then returned.
    59  	// Without the CPUID bit the kernel will not trap the instruction and the
    60  	// process will be terminated with SIGILL.
    61  	if ARM64.HasCPUID {
    62  		midr := getMIDR()
    63  		part_num := uint16((midr >> 4) & 0xfff)
    64  		implementer := byte((midr >> 24) & 0xff)
    65  
    66  		// d0c - NeoverseN1
    67  		// d40 - NeoverseV1
    68  		// d49 - NeoverseN2
    69  		// d4f - NeoverseV2
    70  		if implementer == 'A' && (part_num == 0xd0c || part_num == 0xd40 ||
    71  			part_num == 0xd49 || part_num == 0xd4f) {
    72  			ARM64.IsNeoverse = true
    73  		}
    74  	}
    75  }
    76  
    77  func isSet(hwc uint, value uint) bool {
    78  	return hwc&value != 0
    79  }
    80  

View as plain text