| 1 |
// Copyright 2014 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/abi" |
| 9 |
"internal/goarch" |
| 10 |
"runtime/internal/atomic" |
| 11 |
"unsafe" |
| 12 |
) |
| 13 |
|
| 14 |
type mOS struct { |
| 15 |
waitsemacount uint32 |
| 16 |
} |
| 17 |
|
| 18 |
func getProcID() uint64 { |
| 19 |
return uint64(lwp_self()) |
| 20 |
} |
| 21 |
|
| 22 |
//extern-sysinfo _lwp_self |
| 23 |
func lwp_self() int32 |
| 24 |
|
| 25 |
//go:noescape |
| 26 |
//extern-sysinfo _lwp_park |
| 27 |
func lwp_park(ts int32, rel int32, abstime *timespec, unpark int32, hint, unparkhint unsafe.Pointer) int32 |
| 28 |
|
| 29 |
//go:noescape |
| 30 |
//extern-sysinfo _lwp_unpark |
| 31 |
func lwp_unpark(lwp int32, hint unsafe.Pointer) int32 |
| 32 |
|
| 33 |
//go:noescape |
| 34 |
//extern-sysinfo sysctl |
| 35 |
func sysctl(*uint32, uint32, *byte, *uintptr, *byte, uintptr) int32 |
| 36 |
|
| 37 |
func sysctlInt(mib []uint32) (int32, bool) { |
| 38 |
var out int32 |
| 39 |
nout := unsafe.Sizeof(out) |
| 40 |
ret := sysctl(&mib[0], uint32(len(mib)), (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) |
| 41 |
if ret < 0 { |
| 42 |
return 0, false |
| 43 |
} |
| 44 |
return out, true |
| 45 |
} |
| 46 |
|
| 47 |
func getncpu() int32 { |
| 48 |
if n, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPUONLINE}); ok { |
| 49 |
return int32(n) |
| 50 |
} |
| 51 |
if n, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPU}); ok { |
| 52 |
return int32(n) |
| 53 |
} |
| 54 |
return 1 |
| 55 |
} |
| 56 |
|
| 57 |
func getPageSize() uintptr { |
| 58 |
mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} |
| 59 |
out := uint32(0) |
| 60 |
nout := unsafe.Sizeof(out) |
| 61 |
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) |
| 62 |
if ret >= 0 { |
| 63 |
return uintptr(out) |
| 64 |
} |
| 65 |
return 0 |
| 66 |
} |
| 67 |
|
| 68 |
func getOSRev() int { |
| 69 |
if osrev, ok := sysctlInt([]uint32{_CTL_KERN, _KERN_OSREV}); ok { |
| 70 |
return int(osrev) |
| 71 |
} |
| 72 |
return 0 |
| 73 |
} |
| 74 |
|
| 75 |
//go:nosplit |
| 76 |
func semacreate(mp *m) { |
| 77 |
} |
| 78 |
|
| 79 |
//go:nosplit |
| 80 |
func semasleep(ns int64) int32 { |
| 81 |
_g_ := getg() |
| 82 |
var deadline int64 |
| 83 |
if ns >= 0 { |
| 84 |
deadline = nanotime() + ns |
| 85 |
} |
| 86 |
|
| 87 |
for { |
| 88 |
v := atomic.Load(&_g_.m.waitsemacount) |
| 89 |
if v > 0 { |
| 90 |
if atomic.Cas(&_g_.m.waitsemacount, v, v-1) { |
| 91 |
return 0 // semaphore acquired |
| 92 |
} |
| 93 |
continue |
| 94 |
} |
| 95 |
|
| 96 |
// Sleep until unparked by semawakeup or timeout. |
| 97 |
var tsp *timespec |
| 98 |
var ts timespec |
| 99 |
if ns >= 0 { |
| 100 |
wait := deadline - nanotime() |
| 101 |
if wait <= 0 { |
| 102 |
return -1 |
| 103 |
} |
| 104 |
ts.setNsec(wait) |
| 105 |
tsp = &ts |
| 106 |
} |
| 107 |
ret := lwp_park(_CLOCK_MONOTONIC, _TIMER_RELTIME, tsp, 0, unsafe.Pointer(&_g_.m.waitsemacount), nil) |
| 108 |
if ret != 0 && errno() == _ETIMEDOUT { |
| 109 |
return -1 |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
//go:nosplit |
| 115 |
func semawakeup(mp *m) { |
| 116 |
atomic.Xadd(&mp.waitsemacount, 1) |
| 117 |
// From NetBSD's _lwp_unpark(2) manual: |
| 118 |
// "If the target LWP is not currently waiting, it will return |
| 119 |
// immediately upon the next call to _lwp_park()." |
| 120 |
ret := lwp_unpark(int32(mp.procid), unsafe.Pointer(&mp.waitsemacount)) |
| 121 |
if ret != 0 && errno() != _ESRCH { |
| 122 |
// semawakeup can be called on signal stack. |
| 123 |
systemstack(func() { |
| 124 |
print("thrwakeup addr=", &mp.waitsemacount, " sem=", mp.waitsemacount, " errno=", errno(), "\n") |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func osinit() { |
| 130 |
ncpu = getncpu() |
| 131 |
if physPageSize == 0 { |
| 132 |
physPageSize = getPageSize() |
| 133 |
} |
| 134 |
needSysmonWorkaround = getOSRev() < 902000000 // NetBSD 9.2 |
| 135 |
} |
| 136 |
|
| 137 |
func sysargs(argc int32, argv **byte) { |
| 138 |
n := argc + 1 |
| 139 |
|
| 140 |
// skip over argv, envp to get to auxv |
| 141 |
for argv_index(argv, n) != nil { |
| 142 |
n++ |
| 143 |
} |
| 144 |
|
| 145 |
// skip NULL separator |
| 146 |
n++ |
| 147 |
|
| 148 |
// now argv+n is auxv |
| 149 |
auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*goarch.PtrSize)) |
| 150 |
sysauxv(auxv[:]) |
| 151 |
} |
| 152 |
|
| 153 |
const ( |
| 154 |
_AT_NULL = 0 // Terminates the vector |
| 155 |
_AT_PAGESZ = 6 // Page size in bytes |
| 156 |
) |
| 157 |
|
| 158 |
func sysauxv(auxv []uintptr) { |
| 159 |
for i := 0; auxv[i] != _AT_NULL; i += 2 { |
| 160 |
tag, val := auxv[i], auxv[i+1] |
| 161 |
switch tag { |
| 162 |
case _AT_PAGESZ: |
| 163 |
physPageSize = val |
| 164 |
} |
| 165 |
} |
| 166 |
} |