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.

56 lines
1.2 KiB

  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. "io/ioutil"
  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. buf, err := ioutil.ReadFile(procAuxv)
  21. if err != nil {
  22. // e.g. on android /proc/self/auxv is not accessible, so silently
  23. // ignore the error and leave Initialized = false. On some
  24. // architectures (e.g. arm64) doinit() implements a fallback
  25. // readout and will set Initialized = true again.
  26. return err
  27. }
  28. bo := hostByteOrder()
  29. for len(buf) >= 2*(uintSize/8) {
  30. var tag, val uint
  31. switch uintSize {
  32. case 32:
  33. tag = uint(bo.Uint32(buf[0:]))
  34. val = uint(bo.Uint32(buf[4:]))
  35. buf = buf[8:]
  36. case 64:
  37. tag = uint(bo.Uint64(buf[0:]))
  38. val = uint(bo.Uint64(buf[8:]))
  39. buf = buf[16:]
  40. }
  41. switch tag {
  42. case _AT_HWCAP:
  43. hwCap = val
  44. case _AT_HWCAP2:
  45. hwCap2 = val
  46. }
  47. }
  48. return nil
  49. }