Skip to content

Commit 2da1413

Browse files
committed
cpu: get hwcap/auxv from the Go 1.21+ runtime
Depends on https://go.dev/cl/458256 This change only does Linux for now. Updates golang/go#57336 Change-Id: I0659697c1bdc6e2577c6251b964a0df32047ee12 Reviewed-on: https://go-review.googlesource.com/c/sys/+/465295 Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 4fee21c commit 2da1413

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

cpu/hwcap_linux.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ var hwCap uint
2424
var hwCap2 uint
2525

2626
func readHWCAP() error {
27+
// For Go 1.21+, get auxv from the Go runtime.
28+
if a := getAuxv(); len(a) > 0 {
29+
for len(a) >= 2 {
30+
tag, val := a[0], uint(a[1])
31+
a = a[2:]
32+
switch tag {
33+
case _AT_HWCAP:
34+
hwCap = val
35+
case _AT_HWCAP2:
36+
hwCap2 = val
37+
}
38+
}
39+
return nil
40+
}
41+
2742
buf, err := ioutil.ReadFile(procAuxv)
2843
if err != nil {
2944
// e.g. on android /proc/self/auxv is not accessible, so silently

cpu/runtime_auxv.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2023 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+
// getAuxvFn is non-nil on Go 1.21+ (via runtime_auxv_go121.go init)
8+
// on platforms that use auxv.
9+
var getAuxvFn func() []uintptr
10+
11+
func getAuxv() []uintptr {
12+
if getAuxvFn == nil {
13+
return nil
14+
}
15+
return getAuxvFn()
16+
}

cpu/runtime_auxv_go121.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 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+
//go:build go1.21
6+
// +build go1.21
7+
8+
package cpu
9+
10+
import (
11+
_ "unsafe" // for linkname
12+
)
13+
14+
//go:linkname runtime_getAuxv runtime.getAuxv
15+
func runtime_getAuxv() []uintptr
16+
17+
func init() {
18+
getAuxvFn = runtime_getAuxv
19+
}

cpu/runtime_auxv_go121_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2023 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+
//go:build go1.21
6+
// +build go1.21
7+
8+
package cpu
9+
10+
import (
11+
"runtime"
12+
"testing"
13+
)
14+
15+
func TestAuxvFromRuntime(t *testing.T) {
16+
got := getAuxv()
17+
t.Logf("got: %v", got) // notably: we didn't panic
18+
if runtime.GOOS == "linux" && len(got) == 0 {
19+
t.Errorf("expected auxv on linux; got zero entries")
20+
}
21+
}

0 commit comments

Comments
 (0)