1
2
3
4
5 package cpu
6
7 import (
8 "strings"
9 "syscall"
10 )
11
12
13 const (
14 hwcap_FP = 1 << 0
15 hwcap_ASIMD = 1 << 1
16 hwcap_EVTSTRM = 1 << 2
17 hwcap_AES = 1 << 3
18 hwcap_PMULL = 1 << 4
19 hwcap_SHA1 = 1 << 5
20 hwcap_SHA2 = 1 << 6
21 hwcap_CRC32 = 1 << 7
22 hwcap_ATOMICS = 1 << 8
23 hwcap_FPHP = 1 << 9
24 hwcap_ASIMDHP = 1 << 10
25 hwcap_CPUID = 1 << 11
26 hwcap_ASIMDRDM = 1 << 12
27 hwcap_JSCVT = 1 << 13
28 hwcap_FCMA = 1 << 14
29 hwcap_LRCPC = 1 << 15
30 hwcap_DCPOP = 1 << 16
31 hwcap_SHA3 = 1 << 17
32 hwcap_SM3 = 1 << 18
33 hwcap_SM4 = 1 << 19
34 hwcap_ASIMDDP = 1 << 20
35 hwcap_SHA512 = 1 << 21
36 hwcap_SVE = 1 << 22
37 hwcap_ASIMDFHM = 1 << 23
38 hwcap_DIT = 1 << 24
39
40 hwcap2_SVE2 = 1 << 1
41 hwcap2_I8MM = 1 << 13
42 )
43
44
45
46
47
48
49 func linuxKernelCanEmulateCPUID() bool {
50 var un syscall.Utsname
51 syscall.Uname(&un)
52 var sb strings.Builder
53 for _, b := range un.Release[:] {
54 if b == 0 {
55 break
56 }
57 sb.WriteByte(byte(b))
58 }
59 major, minor, _, ok := parseRelease(sb.String())
60 return ok && (major > 4 || major == 4 && minor >= 11)
61 }
62
63 func doinit() {
64 if err := readHWCAP(); err != nil {
65
66
67
68
69
70
71
72
73
74
75
76
77
78 if linuxKernelCanEmulateCPUID() {
79 readARM64Registers()
80 } else {
81 readLinuxProcCPUInfo()
82 }
83 return
84 }
85
86
87 ARM64.HasFP = isSet(hwCap, hwcap_FP)
88 ARM64.HasASIMD = isSet(hwCap, hwcap_ASIMD)
89 ARM64.HasEVTSTRM = isSet(hwCap, hwcap_EVTSTRM)
90 ARM64.HasAES = isSet(hwCap, hwcap_AES)
91 ARM64.HasPMULL = isSet(hwCap, hwcap_PMULL)
92 ARM64.HasSHA1 = isSet(hwCap, hwcap_SHA1)
93 ARM64.HasSHA2 = isSet(hwCap, hwcap_SHA2)
94 ARM64.HasCRC32 = isSet(hwCap, hwcap_CRC32)
95 ARM64.HasATOMICS = isSet(hwCap, hwcap_ATOMICS)
96 ARM64.HasFPHP = isSet(hwCap, hwcap_FPHP)
97 ARM64.HasASIMDHP = isSet(hwCap, hwcap_ASIMDHP)
98 ARM64.HasCPUID = isSet(hwCap, hwcap_CPUID)
99 ARM64.HasASIMDRDM = isSet(hwCap, hwcap_ASIMDRDM)
100 ARM64.HasJSCVT = isSet(hwCap, hwcap_JSCVT)
101 ARM64.HasFCMA = isSet(hwCap, hwcap_FCMA)
102 ARM64.HasLRCPC = isSet(hwCap, hwcap_LRCPC)
103 ARM64.HasDCPOP = isSet(hwCap, hwcap_DCPOP)
104 ARM64.HasSHA3 = isSet(hwCap, hwcap_SHA3)
105 ARM64.HasSM3 = isSet(hwCap, hwcap_SM3)
106 ARM64.HasSM4 = isSet(hwCap, hwcap_SM4)
107 ARM64.HasASIMDDP = isSet(hwCap, hwcap_ASIMDDP)
108 ARM64.HasSHA512 = isSet(hwCap, hwcap_SHA512)
109 ARM64.HasSVE = isSet(hwCap, hwcap_SVE)
110 ARM64.HasASIMDFHM = isSet(hwCap, hwcap_ASIMDFHM)
111 ARM64.HasDIT = isSet(hwCap, hwcap_DIT)
112
113
114 ARM64.HasSVE2 = isSet(hwCap2, hwcap2_SVE2)
115 ARM64.HasI8MM = isSet(hwCap2, hwcap2_I8MM)
116 }
117
118 func isSet(hwc uint, value uint) bool {
119 return hwc&value != 0
120 }
121
View as plain text