1 // Copyright 2020 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.
12 // Minimal copy of functionality from x/sys/unix so the cpu package can call
13 // sysctl without depending on x/sys/unix.
18 _SYSCTL_VERS_1 = 0x1000000
23 func sysctl(mib []int32, old *byte, oldlen *uintptr, new *byte, newlen uintptr) (err error) {
24 var _p0 unsafe.Pointer
26 _p0 = unsafe.Pointer(&mib[0])
28 _p0 = unsafe.Pointer(&_zero)
30 _, _, errno := syscall.Syscall6(
34 uintptr(unsafe.Pointer(old)),
35 uintptr(unsafe.Pointer(oldlen)),
36 uintptr(unsafe.Pointer(new)),
44 type sysctlNode struct {
53 _sysctl_parent [8]byte
57 func sysctlNodes(mib []int32) ([]sysctlNode, error) {
60 // Get a list of all sysctl nodes below the given MIB by performing
61 // a sysctl for the given MIB with CTL_QUERY appended.
62 mib = append(mib, _CTL_QUERY)
63 qnode := sysctlNode{Flags: _SYSCTL_VERS_1}
64 qp := (*byte)(unsafe.Pointer(&qnode))
65 sz := unsafe.Sizeof(qnode)
66 if err := sysctl(mib, nil, &olen, qp, sz); err != nil {
70 // Now that we know the size, get the actual nodes.
71 nodes := make([]sysctlNode, olen/sz)
72 np := (*byte)(unsafe.Pointer(&nodes[0]))
73 if err := sysctl(mib, np, &olen, qp, sz); err != nil {
80 func nametomib(name string) ([]int32, error) {
81 // Split name into components.
84 for i := 0; i < len(name); i++ {
86 parts = append(parts, name[last:i])
90 parts = append(parts, name[last:])
93 // Discover the nodes and construct the MIB OID.
94 for partno, part := range parts {
95 nodes, err := sysctlNodes(mib)
99 for _, node := range nodes {
101 for i := range node.Name {
102 if node.Name[i] != 0 {
103 n = append(n, byte(node.Name[i]))
106 if string(n) == part {
107 mib = append(mib, int32(node.Num))
111 if len(mib) != partno+1 {
119 // aarch64SysctlCPUID is struct aarch64_sysctl_cpu_id from NetBSD's <aarch64/armreg.h>
120 type aarch64SysctlCPUID struct {
121 midr uint64 /* Main ID Register */
122 revidr uint64 /* Revision ID Register */
123 mpidr uint64 /* Multiprocessor Affinity Register */
124 aa64dfr0 uint64 /* A64 Debug Feature Register 0 */
125 aa64dfr1 uint64 /* A64 Debug Feature Register 1 */
126 aa64isar0 uint64 /* A64 Instruction Set Attribute Register 0 */
127 aa64isar1 uint64 /* A64 Instruction Set Attribute Register 1 */
128 aa64mmfr0 uint64 /* A64 Memory Model Feature Register 0 */
129 aa64mmfr1 uint64 /* A64 Memory Model Feature Register 1 */
130 aa64mmfr2 uint64 /* A64 Memory Model Feature Register 2 */
131 aa64pfr0 uint64 /* A64 Processor Feature Register 0 */
132 aa64pfr1 uint64 /* A64 Processor Feature Register 1 */
133 aa64zfr0 uint64 /* A64 SVE Feature ID Register 0 */
134 mvfr0 uint32 /* Media and VFP Feature Register 0 */
135 mvfr1 uint32 /* Media and VFP Feature Register 1 */
136 mvfr2 uint32 /* Media and VFP Feature Register 2 */
138 clidr uint64 /* Cache Level ID Register */
139 ctr uint64 /* Cache Type Register */
142 func sysctlCPUID(name string) (*aarch64SysctlCPUID, error) {
143 mib, err := nametomib(name)
148 out := aarch64SysctlCPUID{}
149 n := unsafe.Sizeof(out)
150 _, _, errno := syscall.Syscall6(
151 syscall.SYS___SYSCTL,
152 uintptr(unsafe.Pointer(&mib[0])),
154 uintptr(unsafe.Pointer(&out)),
155 uintptr(unsafe.Pointer(&n)),
165 cpuid, err := sysctlCPUID("machdep.cpu0.cpu_id")
170 parseARM64SystemRegisters(cpuid.aa64isar0, cpuid.aa64isar1, cpuid.aa64pfr0)