cpu_linux_arm64.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2018 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. package cpu
  5. import (
  6. "strings"
  7. "syscall"
  8. )
  9. // HWCAP/HWCAP2 bits. These are exposed by Linux.
  10. const (
  11. hwcap_FP = 1 << 0
  12. hwcap_ASIMD = 1 << 1
  13. hwcap_EVTSTRM = 1 << 2
  14. hwcap_AES = 1 << 3
  15. hwcap_PMULL = 1 << 4
  16. hwcap_SHA1 = 1 << 5
  17. hwcap_SHA2 = 1 << 6
  18. hwcap_CRC32 = 1 << 7
  19. hwcap_ATOMICS = 1 << 8
  20. hwcap_FPHP = 1 << 9
  21. hwcap_ASIMDHP = 1 << 10
  22. hwcap_CPUID = 1 << 11
  23. hwcap_ASIMDRDM = 1 << 12
  24. hwcap_JSCVT = 1 << 13
  25. hwcap_FCMA = 1 << 14
  26. hwcap_LRCPC = 1 << 15
  27. hwcap_DCPOP = 1 << 16
  28. hwcap_SHA3 = 1 << 17
  29. hwcap_SM3 = 1 << 18
  30. hwcap_SM4 = 1 << 19
  31. hwcap_ASIMDDP = 1 << 20
  32. hwcap_SHA512 = 1 << 21
  33. hwcap_SVE = 1 << 22
  34. hwcap_ASIMDFHM = 1 << 23
  35. hwcap_DIT = 1 << 24
  36. hwcap2_SVE2 = 1 << 1
  37. hwcap2_I8MM = 1 << 13
  38. )
  39. // linuxKernelCanEmulateCPUID reports whether we're running
  40. // on Linux 4.11+. Ideally we'd like to ask the question about
  41. // whether the current kernel contains
  42. // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=77c97b4ee21290f5f083173d957843b615abbff2
  43. // but the version number will have to do.
  44. func linuxKernelCanEmulateCPUID() bool {
  45. var un syscall.Utsname
  46. syscall.Uname(&un)
  47. var sb strings.Builder
  48. for _, b := range un.Release[:] {
  49. if b == 0 {
  50. break
  51. }
  52. sb.WriteByte(byte(b))
  53. }
  54. major, minor, _, ok := parseRelease(sb.String())
  55. return ok && (major > 4 || major == 4 && minor >= 11)
  56. }
  57. func doinit() {
  58. if err := readHWCAP(); err != nil {
  59. // We failed to read /proc/self/auxv. This can happen if the binary has
  60. // been given extra capabilities(7) with /bin/setcap.
  61. //
  62. // When this happens, we have two options. If the Linux kernel is new
  63. // enough (4.11+), we can read the arm64 registers directly which'll
  64. // trap into the kernel and then return back to userspace.
  65. //
  66. // But on older kernels, such as Linux 4.4.180 as used on many Synology
  67. // devices, calling readARM64Registers (specifically getisar0) will
  68. // cause a SIGILL and we'll die. So for older kernels, parse /proc/cpuinfo
  69. // instead.
  70. //
  71. // See golang/go#57336.
  72. if linuxKernelCanEmulateCPUID() {
  73. readARM64Registers()
  74. } else {
  75. readLinuxProcCPUInfo()
  76. }
  77. return
  78. }
  79. // HWCAP feature bits
  80. ARM64.HasFP = isSet(hwCap, hwcap_FP)
  81. ARM64.HasASIMD = isSet(hwCap, hwcap_ASIMD)
  82. ARM64.HasEVTSTRM = isSet(hwCap, hwcap_EVTSTRM)
  83. ARM64.HasAES = isSet(hwCap, hwcap_AES)
  84. ARM64.HasPMULL = isSet(hwCap, hwcap_PMULL)
  85. ARM64.HasSHA1 = isSet(hwCap, hwcap_SHA1)
  86. ARM64.HasSHA2 = isSet(hwCap, hwcap_SHA2)
  87. ARM64.HasCRC32 = isSet(hwCap, hwcap_CRC32)
  88. ARM64.HasATOMICS = isSet(hwCap, hwcap_ATOMICS)
  89. ARM64.HasFPHP = isSet(hwCap, hwcap_FPHP)
  90. ARM64.HasASIMDHP = isSet(hwCap, hwcap_ASIMDHP)
  91. ARM64.HasCPUID = isSet(hwCap, hwcap_CPUID)
  92. ARM64.HasASIMDRDM = isSet(hwCap, hwcap_ASIMDRDM)
  93. ARM64.HasJSCVT = isSet(hwCap, hwcap_JSCVT)
  94. ARM64.HasFCMA = isSet(hwCap, hwcap_FCMA)
  95. ARM64.HasLRCPC = isSet(hwCap, hwcap_LRCPC)
  96. ARM64.HasDCPOP = isSet(hwCap, hwcap_DCPOP)
  97. ARM64.HasSHA3 = isSet(hwCap, hwcap_SHA3)
  98. ARM64.HasSM3 = isSet(hwCap, hwcap_SM3)
  99. ARM64.HasSM4 = isSet(hwCap, hwcap_SM4)
  100. ARM64.HasASIMDDP = isSet(hwCap, hwcap_ASIMDDP)
  101. ARM64.HasSHA512 = isSet(hwCap, hwcap_SHA512)
  102. ARM64.HasSVE = isSet(hwCap, hwcap_SVE)
  103. ARM64.HasASIMDFHM = isSet(hwCap, hwcap_ASIMDFHM)
  104. ARM64.HasDIT = isSet(hwCap, hwcap_DIT)
  105. // HWCAP2 feature bits
  106. ARM64.HasSVE2 = isSet(hwCap2, hwcap2_SVE2)
  107. ARM64.HasI8MM = isSet(hwCap2, hwcap2_I8MM)
  108. }
  109. func isSet(hwc uint, value uint) bool {
  110. return hwc&value != 0
  111. }