cpu_openbsd_arm64.go 1.7 KB

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