1// Copyright 2024 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// Minimal copy from internal/cpu and runtime to make sysctl calls.
 6
 7//go:build darwin && arm64 && gc
 8
 9package cpu
10
11import (
12	"syscall"
13	"unsafe"
14)
15
16type Errno = syscall.Errno
17
18// adapted from internal/cpu/cpu_arm64_darwin.go
19func darwinSysctlEnabled(name []byte) bool {
20	out := int32(0)
21	nout := unsafe.Sizeof(out)
22	if ret := sysctlbyname(&name[0], (*byte)(unsafe.Pointer(&out)), &nout, nil, 0); ret != nil {
23		return false
24	}
25	return out > 0
26}
27
28//go:cgo_import_dynamic libc_sysctl sysctl "/usr/lib/libSystem.B.dylib"
29
30var libc_sysctlbyname_trampoline_addr uintptr
31
32// adapted from runtime/sys_darwin.go in the pattern of sysctl() above, as defined in x/sys/unix
33func sysctlbyname(name *byte, old *byte, oldlen *uintptr, new *byte, newlen uintptr) error {
34	if _, _, err := syscall_syscall6(
35		libc_sysctlbyname_trampoline_addr,
36		uintptr(unsafe.Pointer(name)),
37		uintptr(unsafe.Pointer(old)),
38		uintptr(unsafe.Pointer(oldlen)),
39		uintptr(unsafe.Pointer(new)),
40		uintptr(newlen),
41		0,
42	); err != 0 {
43		return err
44	}
45
46	return nil
47}
48
49//go:cgo_import_dynamic libc_sysctlbyname sysctlbyname "/usr/lib/libSystem.B.dylib"
50
51// Implemented in the runtime package (runtime/sys_darwin.go)
52func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
53
54//go:linkname syscall_syscall6 syscall.syscall6