Skip to content

Commit e4216d5

Browse files
committed
internal/cpu: add ARM64.HasSHA3
For #69536 Change-Id: If237226ba03e282443b4fc90484968c903198cb1 Reviewed-on: https://go-review.googlesource.com/c/go/+/616715 Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent c06eef2 commit e4216d5

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/internal/cpu/cpu.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var ARM64 struct {
7070
HasSHA1 bool
7171
HasSHA2 bool
7272
HasSHA512 bool
73+
HasSHA3 bool
7374
HasCRC32 bool
7475
HasATOMICS bool
7576
HasCPUID bool

src/internal/cpu/cpu_arm64.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func doinit() {
1616
{Name: "sha1", Feature: &ARM64.HasSHA1},
1717
{Name: "sha2", Feature: &ARM64.HasSHA2},
1818
{Name: "sha512", Feature: &ARM64.HasSHA512},
19+
{Name: "sha3", Feature: &ARM64.HasSHA3},
1920
{Name: "crc32", Feature: &ARM64.HasCRC32},
2021
{Name: "atomics", Feature: &ARM64.HasATOMICS},
2122
{Name: "cpuid", Feature: &ARM64.HasCPUID},
@@ -38,6 +39,7 @@ func extractBits(data uint64, start, end uint) uint {
3839

3940
func parseARM64SystemRegisters(isar0, pfr0 uint64) {
4041
// ID_AA64ISAR0_EL1
42+
// https://developer.arm.com/documentation/ddi0601/2025-03/AArch64-Registers/ID-AA64ISAR0-EL1--AArch64-Instruction-Set-Attribute-Register-0
4143
switch extractBits(isar0, 4, 7) {
4244
case 1:
4345
ARM64.HasAES = true
@@ -69,6 +71,11 @@ func parseARM64SystemRegisters(isar0, pfr0 uint64) {
6971
ARM64.HasATOMICS = true
7072
}
7173

74+
switch extractBits(isar0, 32, 35) {
75+
case 1:
76+
ARM64.HasSHA3 = true
77+
}
78+
7279
switch extractBits(pfr0, 48, 51) {
7380
case 1:
7481
ARM64.HasDIT = true

src/internal/cpu/cpu_arm64_darwin.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ package cpu
99
import _ "unsafe" // for linkname
1010

1111
func osInit() {
12+
// macOS 12 moved these to the hw.optional.arm tree, but as of Go 1.24 we
13+
// still support macOS 11. See [Determine Encryption Capabilities].
14+
//
15+
// [Determine Encryption Capabilities]: https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics#3918855
1216
ARM64.HasATOMICS = sysctlEnabled([]byte("hw.optional.armv8_1_atomics\x00"))
1317
ARM64.HasCRC32 = sysctlEnabled([]byte("hw.optional.armv8_crc32\x00"))
1418
ARM64.HasSHA512 = sysctlEnabled([]byte("hw.optional.armv8_2_sha512\x00"))
19+
ARM64.HasSHA3 = sysctlEnabled([]byte("hw.optional.armv8_2_sha3\x00"))
20+
1521
ARM64.HasDIT = sysctlEnabled([]byte("hw.optional.arm.FEAT_DIT\x00"))
1622

17-
// There are no hw.optional sysctl values for the below features on Mac OS 11.0
18-
// to detect their supported state dynamically. Assume the CPU features that
19-
// Apple Silicon M1 supports to be available as a minimal set of features
20-
// to all Go programs running on darwin/arm64.
23+
// There are no hw.optional sysctl values for the below features on macOS 11
24+
// to detect their supported state dynamically (although they are available
25+
// in the hw.optional.arm tree on macOS 12). Assume the CPU features that
26+
// Apple Silicon M1 supports to be available on all future iterations.
2127
ARM64.HasAES = true
2228
ARM64.HasPMULL = true
2329
ARM64.HasSHA1 = true

src/internal/cpu/cpu_arm64_hwcap.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import _ "unsafe" // for linkname
2222
var HWCap uint
2323

2424
// HWCAP bits. These are exposed by Linux.
25+
// See arch/arm64/include/uapi/asm/hwcap.h.
2526
const (
2627
hwcap_AES = 1 << 3
2728
hwcap_PMULL = 1 << 4
@@ -30,18 +31,21 @@ const (
3031
hwcap_CRC32 = 1 << 7
3132
hwcap_ATOMICS = 1 << 8
3233
hwcap_CPUID = 1 << 11
34+
hwcap_SHA3 = 1 << 17
3335
hwcap_SHA512 = 1 << 21
3436
hwcap_DIT = 1 << 24
3537
)
3638

3739
func hwcapInit(os string) {
3840
// HWCap was populated by the runtime from the auxiliary vector.
41+
// See https://docs.kernel.org/arch/arm64/elf_hwcaps.html.
3942
// Use HWCap information since reading aarch64 system registers
4043
// is not supported in user space on older linux kernels.
4144
ARM64.HasAES = isSet(HWCap, hwcap_AES)
4245
ARM64.HasPMULL = isSet(HWCap, hwcap_PMULL)
4346
ARM64.HasSHA1 = isSet(HWCap, hwcap_SHA1)
4447
ARM64.HasSHA2 = isSet(HWCap, hwcap_SHA2)
48+
ARM64.HasSHA3 = isSet(HWCap, hwcap_SHA3)
4549
ARM64.HasCRC32 = isSet(HWCap, hwcap_CRC32)
4650
ARM64.HasCPUID = isSet(HWCap, hwcap_CPUID)
4751
ARM64.HasSHA512 = isSet(HWCap, hwcap_SHA512)

0 commit comments

Comments
 (0)