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