| 1 |
// Copyright 2011 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 runtime |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/goarch" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
type mOS struct{} |
| 13 |
|
| 14 |
//go:noescape |
| 15 |
//extern _umtx_op |
| 16 |
func sys_umtx_op(addr *uint32, mode int32, val uint32, uaddr1 uinptr, ts *umtx_time) int32 |
| 17 |
|
| 18 |
//go:noescape |
| 19 |
//extern sysctl |
| 20 |
func sysctl(*uint32, uint32, *byte, *uintptr, *byte, uintptr) int32 |
| 21 |
|
| 22 |
const ( |
| 23 |
_CTL_MAXNAME = 24 |
| 24 |
_CPU_LEVEL_WHICH = 3 |
| 25 |
_CPU_WHICH_PID = 2 |
| 26 |
) |
| 27 |
|
| 28 |
// From FreeBSD's <sys/sysctl.h> |
| 29 |
const ( |
| 30 |
_CTL_HW = 6 |
| 31 |
_HW_PAGESIZE = 7 |
| 32 |
) |
| 33 |
|
| 34 |
// Undocumented numbers from FreeBSD's lib/libc/gen/sysctlnametomib.c. |
| 35 |
const ( |
| 36 |
_CTL_QUERY = 0 |
| 37 |
_CTL_QUERY_MIB = 3 |
| 38 |
) |
| 39 |
|
| 40 |
// sysctlnametomib fill mib with dynamically assigned sysctl entries of name, |
| 41 |
// return count of effected mib slots, return 0 on error. |
| 42 |
func sysctlnametomib(name []byte, mib *[_CTL_MAXNAME]uint32) uint32 { |
| 43 |
oid := [2]uint32{_CTL_QUERY, _CTL_QUERY_MIB} |
| 44 |
miblen := uintptr(_CTL_MAXNAME) |
| 45 |
if sysctl(&oid[0], 2, (*byte)(unsafe.Pointer(mib)), &miblen, (*byte)(unsafe.Pointer(&name[0])), (uintptr)(len(name))) < 0 { |
| 46 |
return 0 |
| 47 |
} |
| 48 |
miblen /= unsafe.Sizeof(uint32(0)) |
| 49 |
if miblen <= 0 { |
| 50 |
return 0 |
| 51 |
} |
| 52 |
return uint32(miblen) |
| 53 |
} |
| 54 |
|
| 55 |
const ( |
| 56 |
_CPU_CURRENT_PID = -1 // Current process ID. |
| 57 |
) |
| 58 |
|
| 59 |
//go:noescape |
| 60 |
//extern cpuset_getaffinity |
| 61 |
func cpuset_getaffinity(level int32, which int32, id int64, size uintptr, mask *byte) int32 |
| 62 |
|
| 63 |
//go:systemstack |
| 64 |
func getncpu() int32 { |
| 65 |
// Use a large buffer for the CPU mask. We're on the system |
| 66 |
// stack, so this is fine, and we can't allocate memory for a |
| 67 |
// dynamically-sized buffer at this point. |
| 68 |
const maxCPUs = 64 * 1024 |
| 69 |
var mask [maxCPUs / 8]byte |
| 70 |
var mib [_CTL_MAXNAME]uint32 |
| 71 |
|
| 72 |
// According to FreeBSD's /usr/src/sys/kern/kern_cpuset.c, |
| 73 |
// cpuset_getaffinity return ERANGE when provided buffer size exceed the limits in kernel. |
| 74 |
// Querying kern.smp.maxcpus to calculate maximum buffer size. |
| 75 |
// See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=200802 |
| 76 |
|
| 77 |
// Variable kern.smp.maxcpus introduced at Dec 23 2003, revision 123766, |
| 78 |
// with dynamically assigned sysctl entries. |
| 79 |
miblen := sysctlnametomib([]byte("kern.smp.maxcpus"), &mib) |
| 80 |
if miblen == 0 { |
| 81 |
return 1 |
| 82 |
} |
| 83 |
|
| 84 |
// Query kern.smp.maxcpus. |
| 85 |
dstsize := uintptr(4) |
| 86 |
maxcpus := uint32(0) |
| 87 |
if sysctl(&mib[0], miblen, (*byte)(unsafe.Pointer(&maxcpus)), &dstsize, nil, 0) != 0 { |
| 88 |
return 1 |
| 89 |
} |
| 90 |
|
| 91 |
maskSize := int(maxcpus+7) / 8 |
| 92 |
if maskSize < goarch.PtrSize { |
| 93 |
maskSize = goarch.PtrSize |
| 94 |
} |
| 95 |
if maskSize > uintptr(len(mask)) { |
| 96 |
maskSize = uintptr(len(mask)) |
| 97 |
} |
| 98 |
|
| 99 |
if cpuset_getaffinity(_CPU_LEVEL_WHICH, _CPU_WHICH_PID, _CPU_CURRENT_PID, |
| 100 |
maskSize, (*byte)(unsafe.Pointer(&mask[0]))) != 0 { |
| 101 |
return 1 |
| 102 |
} |
| 103 |
n := int32(0) |
| 104 |
for _, v := range mask[:maskSize] { |
| 105 |
for v != 0 { |
| 106 |
n += int32(v & 1) |
| 107 |
v >>= 1 |
| 108 |
} |
| 109 |
} |
| 110 |
if n == 0 { |
| 111 |
return 1 |
| 112 |
} |
| 113 |
return n |
| 114 |
} |
| 115 |
|
| 116 |
func getPageSize() uintptr { |
| 117 |
mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} |
| 118 |
out := uint32(0) |
| 119 |
nout := unsafe.Sizeof(out) |
| 120 |
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) |
| 121 |
if ret >= 0 { |
| 122 |
return uintptr(out) |
| 123 |
} |
| 124 |
return 0 |
| 125 |
} |
| 126 |
|
| 127 |
// FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and |
| 128 |
// thus the code is largely similar. See Linux implementation |
| 129 |
// and lock_futex.go for comments. |
| 130 |
|
| 131 |
//go:nosplit |
| 132 |
func futexsleep(addr *uint32, val uint32, ns int64) { |
| 133 |
systemstack(func() { |
| 134 |
futexsleep1(addr, val, ns) |
| 135 |
}) |
| 136 |
} |
| 137 |
|
| 138 |
func futexsleep1(addr *uint32, val uint32, ns int64) { |
| 139 |
var utp *umtx_time |
| 140 |
if ns >= 0 { |
| 141 |
var ut umtx_time |
| 142 |
ut._clockid = _CLOCK_MONOTONIC |
| 143 |
ut._timeout.setNsec(ns) |
| 144 |
utp = &ut |
| 145 |
} |
| 146 |
ret := sys_umtx_op(addr, _UMTX_OP_WAIT_UINT_PRIVATE, val, unsafe.Sizeof(*utp), utp) |
| 147 |
if ret >= 0 || ret == -_EINTR || ret == -_ETIMEDOUT { |
| 148 |
return |
| 149 |
} |
| 150 |
print("umtx_wait addr=", addr, " val=", val, " ret=", ret, "\n") |
| 151 |
*(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005 |
| 152 |
} |
| 153 |
|
| 154 |
//go:nosplit |
| 155 |
func futexwakeup(addr *uint32, cnt uint32) { |
| 156 |
ret := sys_umtx_op(addr, _UMTX_OP_WAKE_PRIVATE, cnt, 0, nil) |
| 157 |
if ret >= 0 { |
| 158 |
return |
| 159 |
} |
| 160 |
|
| 161 |
systemstack(func() { |
| 162 |
print("umtx_wake_addr=", addr, " ret=", ret, "\n") |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
func osinit() { |
| 167 |
ncpu = getncpu() |
| 168 |
if physPageSize == 0 { |
| 169 |
physPageSize = getPageSize() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func sysargs(argc int32, argv **byte) { |
| 174 |
n := argc + 1 |
| 175 |
|
| 176 |
// skip over argv, envp to get to auxv |
| 177 |
for argv_index(argv, n) != nil { |
| 178 |
n++ |
| 179 |
} |
| 180 |
|
| 181 |
// skip NULL separator |
| 182 |
n++ |
| 183 |
|
| 184 |
// now argv+n is auxv |
| 185 |
auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*goarch.PtrSize)) |
| 186 |
sysauxv(auxv[:]) |
| 187 |
} |
| 188 |
|
| 189 |
const ( |
| 190 |
_AT_NULL = 0 // Terminates the vector |
| 191 |
_AT_PAGESZ = 6 // Page size in bytes |
| 192 |
_AT_TIMEKEEP = 22 // Pointer to timehands. |
| 193 |
_AT_HWCAP = 25 // CPU feature flags |
| 194 |
_AT_HWCAP2 = 26 // CPU feature flags 2 |
| 195 |
) |
| 196 |
|
| 197 |
func sysauxv(auxv []uintptr) { |
| 198 |
for i := 0; auxv[i] != _AT_NULL; i += 2 { |
| 199 |
tag, val := auxv[i], auxv[i+1] |
| 200 |
switch tag { |
| 201 |
// _AT_NCPUS from auxv shouldn't be used due to golang.org/issue/15206 |
| 202 |
case _AT_PAGESZ: |
| 203 |
physPageSize = val |
| 204 |
case _AT_TIMEKEEP: |
| 205 |
timekeepSharedPage = (*vdsoTimekeep)(unsafe.Pointer(val)) |
| 206 |
} |
| 207 |
|
| 208 |
archauxv(tag, val) |
| 209 |
} |
| 210 |
} |