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 // HWCap may be initialized by archauxv and 10 // should not be changed after it was initialized. 11 var HWCap uint 12 13 // HWCAP bits. These are exposed by Linux. 14 const ( 15 hwcap_AES = 1 << 3 16 hwcap_PMULL = 1 << 4 17 hwcap_SHA1 = 1 << 5 18 hwcap_SHA2 = 1 << 6 19 hwcap_CRC32 = 1 << 7 20 hwcap_ATOMICS = 1 << 8 21 hwcap_CPUID = 1 << 11 22 ) 23 24 func hwcapInit(os string) { 25 // HWCap was populated by the runtime from the auxiliary vector. 26 // Use HWCap information since reading aarch64 system registers 27 // is not supported in user space on older linux kernels. 28 ARM64.HasAES = isSet(HWCap, hwcap_AES) 29 ARM64.HasPMULL = isSet(HWCap, hwcap_PMULL) 30 ARM64.HasSHA1 = isSet(HWCap, hwcap_SHA1) 31 ARM64.HasSHA2 = isSet(HWCap, hwcap_SHA2) 32 ARM64.HasCRC32 = isSet(HWCap, hwcap_CRC32) 33 ARM64.HasCPUID = isSet(HWCap, hwcap_CPUID) 34 35 // The Samsung S9+ kernel reports support for atomics, but not all cores 36 // actually support them, resulting in SIGILL. See issue #28431. 37 // TODO(elias.naur): Only disable the optimization on bad chipsets on android. 38 ARM64.HasATOMICS = isSet(HWCap, hwcap_ATOMICS) && os != "android" 39 40 // Check to see if executing on a NeoverseN1 and in order to do that, 41 // check the AUXV for the CPUID bit. The getMIDR function executes an 42 // instruction which would normally be an illegal instruction, but it's 43 // trapped by the kernel, the value sanitized and then returned. Without 44 // the CPUID bit the kernel will not trap the instruction and the process 45 // will be terminated with SIGILL. 46 if ARM64.HasCPUID { 47 midr := getMIDR() 48 part_num := uint16((midr >> 4) & 0xfff) 49 implementor := byte((midr >> 24) & 0xff) 50 51 if implementor == 'A' && part_num == 0xd0c { 52 ARM64.IsNeoverseN1 = true 53 } 54 if implementor == 'A' && part_num == 0xd40 { 55 ARM64.IsZeus = true 56 } 57 } 58 } 59 60 func isSet(hwc uint, value uint) bool { 61 return hwc&value != 0 62 } 63