|
1 | 1 | // Copyright (c) 2020 Klaus Post, released under MIT License. See LICENSE file. |
2 | 2 |
|
| 3 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file located |
| 6 | +// here https://github.com/golang/sys/blob/master/LICENSE |
| 7 | + |
3 | 8 | //+build arm64 |
4 | 9 | //+build linux android |
5 | 10 |
|
6 | 11 | package cpuid |
7 | 12 |
|
8 | 13 | import ( |
| 14 | + "encoding/binary" |
| 15 | + "io/ioutil" |
9 | 16 | "runtime" |
10 | 17 | _ "unsafe" //required for go:linkname |
11 | 18 | ) |
@@ -38,14 +45,55 @@ const ( |
38 | 45 | hwcap_ASIMDFHM = 1 << 23 |
39 | 46 | ) |
40 | 47 |
|
41 | | -//go:linkname hwcap interval/cpu.HWCap |
| 48 | +//go:linkname hwcap internal/cpu.HWCap |
42 | 49 | var hwcap uint |
43 | 50 |
|
44 | 51 | func detectOS(c *CPUInfo) bool { |
45 | 52 | if hwcap == 0 { |
46 | | - // It may be wrong, allow unsafe if defined. |
47 | | - return false |
| 53 | + // We did not get values from the runtime. |
| 54 | + // Try reading /proc/self/auxv |
| 55 | + |
| 56 | + // From https://github.com/golang/sys |
| 57 | + const ( |
| 58 | + _AT_HWCAP = 16 |
| 59 | + _AT_HWCAP2 = 26 |
| 60 | + |
| 61 | + uintSize = int(32 << (^uint(0) >> 63)) |
| 62 | + ) |
| 63 | + |
| 64 | + buf, err := ioutil.ReadFile("/proc/self/auxv") |
| 65 | + if err != nil { |
| 66 | + // e.g. on android /proc/self/auxv is not accessible, so silently |
| 67 | + // ignore the error and leave Initialized = false. On some |
| 68 | + // architectures (e.g. arm64) doinit() implements a fallback |
| 69 | + // readout and will set Initialized = true again. |
| 70 | + return false |
| 71 | + } |
| 72 | + bo := binary.LittleEndian |
| 73 | + for len(buf) >= 2*(uintSize/8) { |
| 74 | + var tag, val uint |
| 75 | + switch uintSize { |
| 76 | + case 32: |
| 77 | + tag = uint(bo.Uint32(buf[0:])) |
| 78 | + val = uint(bo.Uint32(buf[4:])) |
| 79 | + buf = buf[8:] |
| 80 | + case 64: |
| 81 | + tag = uint(bo.Uint64(buf[0:])) |
| 82 | + val = uint(bo.Uint64(buf[8:])) |
| 83 | + buf = buf[16:] |
| 84 | + } |
| 85 | + switch tag { |
| 86 | + case _AT_HWCAP: |
| 87 | + hwcap = val |
| 88 | + case _AT_HWCAP2: |
| 89 | + // Not used |
| 90 | + } |
| 91 | + } |
| 92 | + if hwcap == 0 { |
| 93 | + return false |
| 94 | + } |
48 | 95 | } |
| 96 | + |
49 | 97 | // HWCap was populated by the runtime from the auxiliary vector. |
50 | 98 | // Use HWCap information since reading aarch64 system registers |
51 | 99 | // is not supported in user space on older linux kernels. |
|
0 commit comments