Skip to content

Commit 3d627bb

Browse files
4a6f656cgopherbot
authored andcommitted
cpu: implement CPU feature detection for openbsd/arm64
OpenBSD 7.1 onwards expose the aarch64 ISAR0 and ISAR1 registers via sysctl: $ sysctl machdep machdep.compatible=apple,j274 machdep.id_aa64isar0=153421459058925856 machdep.id_aa64isar1=1172796674562 Implement CPU feature detection for openbsd/arm64 based on this information. Updates golang/go#31746 Change-Id: I6dc473d93ccff720582c05b75456de51269bc3e5 Reviewed-on: https://go-review.googlesource.com/c/sys/+/421799 Run-TryBot: Joel Sing <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent e9af53b commit 3d627bb

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

cpu/cpu_arm64.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ func archInit() {
4141
switch runtime.GOOS {
4242
case "freebsd":
4343
readARM64Registers()
44-
case "linux", "netbsd":
44+
case "linux", "netbsd", "openbsd":
4545
doinit()
4646
default:
47-
// Most platforms don't seem to allow reading these registers.
48-
//
49-
// OpenBSD:
50-
// See https://golang.org/issue/31746
47+
// Many platforms don't seem to allow reading these registers.
5148
setMinimalFeatures()
5249
}
5350
}

cpu/cpu_openbsd_arm64.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 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+
"syscall"
9+
"unsafe"
10+
)
11+
12+
// Minimal copy of functionality from x/sys/unix so the cpu package can call
13+
// sysctl without depending on x/sys/unix.
14+
15+
const (
16+
// From OpenBSD's sys/sysctl.h.
17+
_CTL_MACHDEP = 7
18+
19+
// From OpenBSD's machine/cpu.h.
20+
_CPU_ID_AA64ISAR0 = 2
21+
_CPU_ID_AA64ISAR1 = 3
22+
)
23+
24+
// Implemented in the runtime package (runtime/sys_openbsd3.go)
25+
func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
26+
27+
//go:linkname syscall_syscall6 syscall.syscall6
28+
29+
func sysctl(mib []uint32, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) {
30+
_, _, errno := syscall_syscall6(libc_sysctl_trampoline_addr, uintptr(unsafe.Pointer(&mib[0])), uintptr(len(mib)), uintptr(unsafe.Pointer(old)), uintptr(unsafe.Pointer(oldlen)), uintptr(unsafe.Pointer(new)), uintptr(newlen))
31+
if errno != 0 {
32+
return errno
33+
}
34+
return nil
35+
}
36+
37+
var libc_sysctl_trampoline_addr uintptr
38+
39+
//go:cgo_import_dynamic libc_sysctl sysctl "libc.so"
40+
41+
func sysctlUint64(mib []uint32) (uint64, bool) {
42+
var out uint64
43+
nout := unsafe.Sizeof(out)
44+
if err := sysctl(mib, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0); err != nil {
45+
return 0, false
46+
}
47+
return out, true
48+
}
49+
50+
func doinit() {
51+
setMinimalFeatures()
52+
53+
// Get ID_AA64ISAR0 and ID_AA64ISAR1 from sysctl.
54+
isar0, ok := sysctlUint64([]uint32{_CTL_MACHDEP, _CPU_ID_AA64ISAR0})
55+
if !ok {
56+
return
57+
}
58+
isar1, ok := sysctlUint64([]uint32{_CTL_MACHDEP, _CPU_ID_AA64ISAR1})
59+
if !ok {
60+
return
61+
}
62+
parseARM64SystemRegisters(isar0, isar1, 0)
63+
64+
Initialized = true
65+
}

cpu/cpu_openbsd_arm64.s

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 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+
#include "textflag.h"
6+
7+
TEXT libc_sysctl_trampoline<>(SB),NOSPLIT,$0-0
8+
JMP libc_sysctl(SB)
9+
10+
GLOBL ·libc_sysctl_trampoline_addr(SB), RODATA, $8
11+
DATA ·libc_sysctl_trampoline_addr(SB)/8, $libc_sysctl_trampoline<>(SB)

cpu/cpu_other_arm64.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !linux && !netbsd && arm64
6-
// +build !linux,!netbsd,arm64
5+
//go:build !linux && !netbsd && !openbsd && arm64
6+
// +build !linux,!netbsd,!openbsd,arm64
77

88
package cpu
99

0 commit comments

Comments
 (0)