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  // See arch/arm64/include/uapi/asm/hwcap.h.
    26  const (
    27  	hwcap_AES     = 1 << 3
    28  	hwcap_PMULL   = 1 << 4
    29  	hwcap_SHA1    = 1 << 5
    30  	hwcap_SHA2    = 1 << 6
    31  	hwcap_CRC32   = 1 << 7
    32  	hwcap_ATOMICS = 1 << 8
    33  	hwcap_CPUID   = 1 << 11
    34  	hwcap_SHA3    = 1 << 17
    35  	hwcap_SHA512  = 1 << 21
    36  	hwcap_DIT     = 1 << 24
    37  	hwcap_SB      = 1 << 29
    38  )
    39  
    40  func hwcapInit(os string) {
    41  	// HWCap was populated by the runtime from the auxiliary vector.
    42  	// See https://docs.kernel.org/arch/arm64/elf_hwcaps.html.
    43  	// Use HWCap information since reading aarch64 system registers
    44  	// is not supported in user space on older linux kernels.
    45  	ARM64.HasAES = isSet(HWCap, hwcap_AES)
    46  	ARM64.HasPMULL = isSet(HWCap, hwcap_PMULL)
    47  	ARM64.HasSHA1 = isSet(HWCap, hwcap_SHA1)
    48  	ARM64.HasSHA2 = isSet(HWCap, hwcap_SHA2)
    49  	ARM64.HasSHA3 = isSet(HWCap, hwcap_SHA3)
    50  	ARM64.HasCRC32 = isSet(HWCap, hwcap_CRC32)
    51  	ARM64.HasCPUID = isSet(HWCap, hwcap_CPUID)
    52  	ARM64.HasSHA512 = isSet(HWCap, hwcap_SHA512)
    53  	ARM64.HasDIT = isSet(HWCap, hwcap_DIT)
    54  	ARM64.HasSB = isSet(HWCap, hwcap_SB)
    55  
    56  	// The Samsung S9+ kernel reports support for atomics, but not all cores
    57  	// actually support them, resulting in SIGILL. See issue #28431.
    58  	// TODO(elias.naur): Only disable the optimization on bad chipsets on android.
    59  	ARM64.HasATOMICS = isSet(HWCap, hwcap_ATOMICS) && os != "android"
    60  
    61  	// Check to see if executing on a Neoverse core and in order to do that,
    62  	// check the AUXV for the CPUID bit. The getMIDR function executes an
    63  	// instruction which would normally be an illegal instruction, but it's
    64  	// trapped by the kernel, the value sanitized and then returned.
    65  	// Without the CPUID bit the kernel will not trap the instruction and the
    66  	// process will be terminated with SIGILL.
    67  	if ARM64.HasCPUID {
    68  		midr := getMIDR()
    69  		part_num := uint16((midr >> 4) & 0xfff)
    70  		implementer := byte((midr >> 24) & 0xff)
    71  
    72  		// d0c - NeoverseN1
    73  		// d40 - NeoverseV1
    74  		// d49 - NeoverseN2
    75  		// d4f - NeoverseV2
    76  		// d8e - NeoverseN3
    77  		// d84 - NeoverseV3
    78  		// d83 - NeoverseV3ae
    79  		if implementer == 'A' {
    80  			switch part_num {
    81  			case 0xd0c, 0xd40, 0xd49, 0xd4f, 0xd8e, 0xd84, 0xd83:
    82  				ARM64.IsNeoverse = true
    83  			}
    84  		}
    85  	}
    86  }
    87  
    88  func isSet(hwc uint, value uint) bool {
    89  	return hwc&value != 0
    90  }
    91  

View as plain text