You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hwcap_linux.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 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. "os"
  7. )
  8. const (
  9. _AT_HWCAP = 16
  10. _AT_HWCAP2 = 26
  11. procAuxv = "/proc/self/auxv"
  12. uintSize = int(32 << (^uint(0) >> 63))
  13. )
  14. // For those platforms don't have a 'cpuid' equivalent we use HWCAP/HWCAP2
  15. // These are initialized in cpu_$GOARCH.go
  16. // and should not be changed after they are initialized.
  17. var hwCap uint
  18. var hwCap2 uint
  19. func readHWCAP() error {
  20. // For Go 1.21+, get auxv from the Go runtime.
  21. if a := getAuxv(); len(a) > 0 {
  22. for len(a) >= 2 {
  23. tag, val := a[0], uint(a[1])
  24. a = a[2:]
  25. switch tag {
  26. case _AT_HWCAP:
  27. hwCap = val
  28. case _AT_HWCAP2:
  29. hwCap2 = val
  30. }
  31. }
  32. return nil
  33. }
  34. buf, err := os.ReadFile(procAuxv)
  35. if err != nil {
  36. // e.g. on android /proc/self/auxv is not accessible, so silently
  37. // ignore the error and leave Initialized = false. On some
  38. // architectures (e.g. arm64) doinit() implements a fallback
  39. // readout and will set Initialized = true again.
  40. return err
  41. }
  42. bo := hostByteOrder()
  43. for len(buf) >= 2*(uintSize/8) {
  44. var tag, val uint
  45. switch uintSize {
  46. case 32:
  47. tag = uint(bo.Uint32(buf[0:]))
  48. val = uint(bo.Uint32(buf[4:]))
  49. buf = buf[8:]
  50. case 64:
  51. tag = uint(bo.Uint64(buf[0:]))
  52. val = uint(bo.Uint64(buf[8:]))
  53. buf = buf[16:]
  54. }
  55. switch tag {
  56. case _AT_HWCAP:
  57. hwCap = val
  58. case _AT_HWCAP2:
  59. hwCap2 = val
  60. }
  61. }
  62. return nil
  63. }